translate.c 308.6 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
B
bellard 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
B
bellard 已提交
19
 */
B
bellard 已提交
20 21 22 23 24 25
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

B
bellard 已提交
26
#include "cpu.h"
B
bellard 已提交
27
#include "exec-all.h"
B
bellard 已提交
28
#include "disas.h"
B
bellard 已提交
29
#include "tcg-op.h"
30
#include "qemu-common.h"
B
bellard 已提交
31

P
pbrook 已提交
32 33 34 35
#include "helper.h"
#define GEN_HELPER 1
#include "helper.h"

36 37 38 39
#define CPU_SINGLE_STEP 0x1
#define CPU_BRANCH_STEP 0x2
#define GDBSTUB_SINGLE_STEP 0x4

40
/* Include definitions for instructions classes and implementations flags */
A
aurel32 已提交
41
//#define DO_SINGLE_STEP
42
//#define PPC_DEBUG_DISAS
43
//#define DO_PPC_STATISTICS
B
bellard 已提交
44

45
#ifdef PPC_DEBUG_DISAS
46
#  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
47 48 49
#else
#  define LOG_DISAS(...) do { } while (0)
#endif
50 51
/*****************************************************************************/
/* Code translation helpers                                                  */
B
bellard 已提交
52

A
aurel32 已提交
53
/* global register indexes */
P
pbrook 已提交
54
static TCGv_ptr cpu_env;
55
static char cpu_reg_names[10*3 + 22*4 /* GPR */
A
aurel32 已提交
56
#if !defined(TARGET_PPC64)
57
    + 10*4 + 22*5 /* SPE GPRh */
A
aurel32 已提交
58
#endif
A
aurel32 已提交
59
    + 10*4 + 22*5 /* FPR */
A
aurel32 已提交
60 61
    + 2*(10*6 + 22*7) /* AVRh, AVRl */
    + 8*5 /* CRF */];
A
aurel32 已提交
62 63 64 65
static TCGv cpu_gpr[32];
#if !defined(TARGET_PPC64)
static TCGv cpu_gprh[32];
#endif
P
pbrook 已提交
66 67 68
static TCGv_i64 cpu_fpr[32];
static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
static TCGv_i32 cpu_crf[8];
A
aurel32 已提交
69
static TCGv cpu_nip;
70
static TCGv cpu_msr;
A
aurel32 已提交
71 72
static TCGv cpu_ctr;
static TCGv cpu_lr;
A
aurel32 已提交
73
static TCGv cpu_xer;
74
static TCGv cpu_reserve;
P
pbrook 已提交
75
static TCGv_i32 cpu_fpscr;
A
aurel32 已提交
76
static TCGv_i32 cpu_access_type;
A
aurel32 已提交
77

P
pbrook 已提交
78 79 80 81
#include "gen-icount.h"

void ppc_translate_init(void)
{
A
aurel32 已提交
82 83
    int i;
    char* p;
P
pbrook 已提交
84
    static int done_init = 0;
A
aurel32 已提交
85

P
pbrook 已提交
86 87
    if (done_init)
        return;
A
aurel32 已提交
88

P
pbrook 已提交
89 90
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");

A
aurel32 已提交
91
    p = cpu_reg_names;
A
aurel32 已提交
92 93 94

    for (i = 0; i < 8; i++) {
        sprintf(p, "crf%d", i);
P
pbrook 已提交
95 96
        cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
                                            offsetof(CPUState, crf[i]), p);
A
aurel32 已提交
97 98 99
        p += 5;
    }

A
aurel32 已提交
100 101
    for (i = 0; i < 32; i++) {
        sprintf(p, "r%d", i);
P
pbrook 已提交
102
        cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
103 104 105 106
                                        offsetof(CPUState, gpr[i]), p);
        p += (i < 10) ? 3 : 4;
#if !defined(TARGET_PPC64)
        sprintf(p, "r%dH", i);
P
pbrook 已提交
107 108
        cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
                                             offsetof(CPUState, gprh[i]), p);
A
aurel32 已提交
109 110
        p += (i < 10) ? 4 : 5;
#endif
111

A
aurel32 已提交
112
        sprintf(p, "fp%d", i);
P
pbrook 已提交
113 114
        cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
                                            offsetof(CPUState, fpr[i]), p);
A
aurel32 已提交
115
        p += (i < 10) ? 4 : 5;
A
aurel32 已提交
116

117
        sprintf(p, "avr%dH", i);
118 119 120 121
#ifdef WORDS_BIGENDIAN
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
                                             offsetof(CPUState, avr[i].u64[0]), p);
#else
P
pbrook 已提交
122
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
123 124
                                             offsetof(CPUState, avr[i].u64[1]), p);
#endif
125
        p += (i < 10) ? 6 : 7;
A
aurel32 已提交
126

127
        sprintf(p, "avr%dL", i);
128 129 130 131
#ifdef WORDS_BIGENDIAN
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
                                             offsetof(CPUState, avr[i].u64[1]), p);
#else
P
pbrook 已提交
132
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
133 134
                                             offsetof(CPUState, avr[i].u64[0]), p);
#endif
135
        p += (i < 10) ? 6 : 7;
A
aurel32 已提交
136
    }
A
aurel32 已提交
137

P
pbrook 已提交
138
    cpu_nip = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
139 140
                                 offsetof(CPUState, nip), "nip");

141 142 143
    cpu_msr = tcg_global_mem_new(TCG_AREG0,
                                 offsetof(CPUState, msr), "msr");

P
pbrook 已提交
144
    cpu_ctr = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
145 146
                                 offsetof(CPUState, ctr), "ctr");

P
pbrook 已提交
147
    cpu_lr = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
148 149
                                offsetof(CPUState, lr), "lr");

P
pbrook 已提交
150
    cpu_xer = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
151 152
                                 offsetof(CPUState, xer), "xer");

153 154 155
    cpu_reserve = tcg_global_mem_new(TCG_AREG0,
                                     offsetof(CPUState, reserve), "reserve");

P
pbrook 已提交
156 157
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
                                       offsetof(CPUState, fpscr), "fpscr");
158

A
aurel32 已提交
159 160 161
    cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
                                             offsetof(CPUState, access_type), "access_type");

A
aurel32 已提交
162
    /* register helpers */
P
pbrook 已提交
163
#define GEN_HELPER 2
A
aurel32 已提交
164 165
#include "helper.h"

P
pbrook 已提交
166 167 168
    done_init = 1;
}

B
bellard 已提交
169 170 171
/* internal defines */
typedef struct DisasContext {
    struct TranslationBlock *tb;
B
bellard 已提交
172
    target_ulong nip;
B
bellard 已提交
173
    uint32_t opcode;
174
    uint32_t exception;
B
bellard 已提交
175 176
    /* Routine used to access memory */
    int mem_idx;
A
aurel32 已提交
177
    int access_type;
B
bellard 已提交
178
    /* Translation flags */
A
aurel32 已提交
179
    int le_mode;
180 181
#if defined(TARGET_PPC64)
    int sf_mode;
182
#endif
B
bellard 已提交
183
    int fpu_enabled;
184
    int altivec_enabled;
185
    int spe_enabled;
186
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
187
    int singlestep_enabled;
B
bellard 已提交
188 189
} DisasContext;

190
struct opc_handler_t {
B
bellard 已提交
191 192
    /* invalid bits */
    uint32_t inval;
193
    /* instruction type */
194
    uint64_t type;
B
bellard 已提交
195 196
    /* handler */
    void (*handler)(DisasContext *ctx);
197
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
198
    const char *oname;
199 200
#endif
#if defined(DO_PPC_STATISTICS)
201 202
    uint64_t count;
#endif
203
};
B
bellard 已提交
204

205 206 207
static always_inline void gen_reset_fpstatus (void)
{
#ifdef CONFIG_SOFTFLOAT
208
    gen_helper_reset_fpstatus();
209 210 211
#endif
}

212
static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
213
{
214
    TCGv_i32 t0 = tcg_temp_new_i32();
A
aurel32 已提交
215

216 217
    if (set_fprf != 0) {
        /* This case might be optimized later */
218
        tcg_gen_movi_i32(t0, 1);
A
aurel32 已提交
219
        gen_helper_compute_fprf(t0, arg, t0);
P
pbrook 已提交
220
        if (unlikely(set_rc)) {
221
            tcg_gen_mov_i32(cpu_crf[1], t0);
P
pbrook 已提交
222
        }
A
aurel32 已提交
223
        gen_helper_float_check_status();
224 225
    } else if (unlikely(set_rc)) {
        /* We always need to compute fpcc */
226
        tcg_gen_movi_i32(t0, 0);
A
aurel32 已提交
227
        gen_helper_compute_fprf(t0, arg, t0);
228
        tcg_gen_mov_i32(cpu_crf[1], t0);
229
    }
A
aurel32 已提交
230

231
    tcg_temp_free_i32(t0);
232 233
}

A
aurel32 已提交
234
static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
A
aurel32 已提交
235
{
A
aurel32 已提交
236 237 238 239
    if (ctx->access_type != access_type) {
        tcg_gen_movi_i32(cpu_access_type, access_type);
        ctx->access_type = access_type;
    }
A
aurel32 已提交
240 241
}

242
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
243 244 245
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
A
aurel32 已提交
246
        tcg_gen_movi_tl(cpu_nip, nip);
247 248
    else
#endif
A
aurel32 已提交
249
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
250 251
}

A
aurel32 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264
static always_inline void gen_exception_err (DisasContext *ctx, uint32_t excp, uint32_t error)
{
    TCGv_i32 t0, t1;
    if (ctx->exception == POWERPC_EXCP_NONE) {
        gen_update_nip(ctx, ctx->nip);
    }
    t0 = tcg_const_i32(excp);
    t1 = tcg_const_i32(error);
    gen_helper_raise_exception_err(t0, t1);
    tcg_temp_free_i32(t0);
    tcg_temp_free_i32(t1);
    ctx->exception = (excp);
}
265

A
aurel32 已提交
266 267 268 269 270 271 272 273 274 275 276
static always_inline void gen_exception (DisasContext *ctx, uint32_t excp)
{
    TCGv_i32 t0;
    if (ctx->exception == POWERPC_EXCP_NONE) {
        gen_update_nip(ctx, ctx->nip);
    }
    t0 = tcg_const_i32(excp);
    gen_helper_raise_exception(t0);
    tcg_temp_free_i32(t0);
    ctx->exception = (excp);
}
277

A
aurel32 已提交
278 279 280
static always_inline void gen_debug_exception (DisasContext *ctx)
{
    TCGv_i32 t0;
B
blueswir1 已提交
281 282 283

    if (ctx->exception != POWERPC_EXCP_BRANCH)
        gen_update_nip(ctx, ctx->nip);
A
aurel32 已提交
284 285 286 287
    t0 = tcg_const_i32(EXCP_DEBUG);
    gen_helper_raise_exception(t0);
    tcg_temp_free_i32(t0);
}
288

A
aurel32 已提交
289 290 291 292
static always_inline void gen_inval_exception (DisasContext *ctx, uint32_t error)
{
    gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
}
293

294
/* Stop translation */
A
aurel32 已提交
295
static always_inline void gen_stop_exception (DisasContext *ctx)
296
{
297
    gen_update_nip(ctx, ctx->nip);
298
    ctx->exception = POWERPC_EXCP_STOP;
299 300
}

301
/* No need to update nip here, as execution flow will change */
A
aurel32 已提交
302
static always_inline void gen_sync_exception (DisasContext *ctx)
303
{
304
    ctx->exception = POWERPC_EXCP_SYNC;
305 306
}

B
bellard 已提交
307 308 309 310 311
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
static void gen_##name (DisasContext *ctx)

312 313 314 315 316
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
static void gen_##name (DisasContext *ctx)

B
bellard 已提交
317 318
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
T
ths 已提交
319
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
320 321 322 323
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
324
    opc_handler_t handler;
325
    const char *oname;
B
bellard 已提交
326 327
} opcode_t;

328
/*****************************************************************************/
B
bellard 已提交
329 330
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
331
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
332 333 334 335 336
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
337
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
338
{                                                                             \
339
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
}

/* Opcode part 1 */
EXTRACT_HELPER(opc1, 26, 6);
/* Opcode part 2 */
EXTRACT_HELPER(opc2, 1, 5);
/* Opcode part 3 */
EXTRACT_HELPER(opc3, 6, 5);
/* Update Cr0 flags */
EXTRACT_HELPER(Rc, 0, 1);
/* Destination */
EXTRACT_HELPER(rD, 21, 5);
/* Source */
EXTRACT_HELPER(rS, 21, 5);
/* First operand */
EXTRACT_HELPER(rA, 16, 5);
/* Second operand */
EXTRACT_HELPER(rB, 11, 5);
/* Third operand */
EXTRACT_HELPER(rC, 6, 5);
/***                               Get CRn                                 ***/
EXTRACT_HELPER(crfD, 23, 3);
EXTRACT_HELPER(crfS, 18, 3);
EXTRACT_HELPER(crbD, 21, 5);
EXTRACT_HELPER(crbA, 16, 5);
EXTRACT_HELPER(crbB, 11, 5);
/* SPR / TBL */
367
EXTRACT_HELPER(_SPR, 11, 10);
368
static always_inline uint32_t SPR (uint32_t opcode)
369 370 371 372 373
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
374 375 376 377 378 379
/***                              Get constants                            ***/
EXTRACT_HELPER(IMM, 12, 8);
/* 16 bits signed immediate value */
EXTRACT_SHELPER(SIMM, 0, 16);
/* 16 bits unsigned immediate value */
EXTRACT_HELPER(UIMM, 0, 16);
380 381
/* 5 bits signed immediate value */
EXTRACT_HELPER(SIMM5, 16, 5);
382 383
/* 5 bits signed immediate value */
EXTRACT_HELPER(UIMM5, 16, 5);
B
bellard 已提交
384 385 386 387
/* Bit count */
EXTRACT_HELPER(NB, 11, 5);
/* Shift count */
EXTRACT_HELPER(SH, 11, 5);
A
aurel32 已提交
388 389
/* Vector shift count */
EXTRACT_HELPER(VSH, 6, 4);
B
bellard 已提交
390 391 392 393
/* Mask start */
EXTRACT_HELPER(MB, 6, 5);
/* Mask end */
EXTRACT_HELPER(ME, 1, 5);
B
bellard 已提交
394 395
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
396 397 398 399

EXTRACT_HELPER(CRM, 12, 8);
EXTRACT_HELPER(FM, 17, 8);
EXTRACT_HELPER(SR, 16, 4);
A
aurel32 已提交
400
EXTRACT_HELPER(FPIMM, 12, 4);
B
bellard 已提交
401

B
bellard 已提交
402 403 404 405
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
406
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
407 408 409 410
{
    return (opcode >> 0) & 0x03FFFFFC;
}

411
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
412 413 414 415 416 417 418 419 420 421 422 423
{
    return (opcode >> 0) & 0xFFFC;
}

EXTRACT_HELPER(BO, 21, 5);
EXTRACT_HELPER(BI, 16, 5);
/* Absolute/relative address */
EXTRACT_HELPER(AA, 1, 1);
/* Link */
EXTRACT_HELPER(LK, 0, 1);

/* Create a mask between <start> and <end> bits */
424
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
425
{
426
    target_ulong ret;
B
bellard 已提交
427

428 429
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
430
        ret = UINT64_MAX << (63 - end);
431
    } else if (likely(end == 63)) {
432
        ret = UINT64_MAX >> start;
433 434 435
    }
#else
    if (likely(start == 0)) {
436
        ret = UINT32_MAX << (31  - end);
437
    } else if (likely(end == 31)) {
438
        ret = UINT32_MAX >> start;
439 440 441 442 443 444 445 446
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
447 448 449 450

    return ret;
}

451 452 453
/*****************************************************************************/
/* PowerPC Instructions types definitions                                    */
enum {
454
    PPC_NONE           = 0x0000000000000000ULL,
455
    /* PowerPC base instructions set                                         */
456 457
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
    /*   integer operations instructions                                     */
458
#define PPC_INTEGER PPC_INSNS_BASE
459
    /*   flow control instructions                                           */
460
#define PPC_FLOW    PPC_INSNS_BASE
461
    /*   virtual memory instructions                                         */
462
#define PPC_MEM     PPC_INSNS_BASE
463
    /*   ld/st with reservation instructions                                 */
464
#define PPC_RES     PPC_INSNS_BASE
465
    /*   spr/msr access instructions                                         */
466
#define PPC_MISC    PPC_INSNS_BASE
467 468
    /* Deprecated instruction sets                                           */
    /*   Original POWER instruction set                                      */
469
    PPC_POWER          = 0x0000000000000002ULL,
470
    /*   POWER2 instruction set extension                                    */
471
    PPC_POWER2         = 0x0000000000000004ULL,
472
    /*   Power RTC support                                                   */
473
    PPC_POWER_RTC      = 0x0000000000000008ULL,
474
    /*   Power-to-PowerPC bridge (601)                                       */
475
    PPC_POWER_BR       = 0x0000000000000010ULL,
476
    /* 64 bits PowerPC instruction set                                       */
477
    PPC_64B            = 0x0000000000000020ULL,
478
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
479
    PPC_64BX           = 0x0000000000000040ULL,
480
    /*   64 bits hypervisor extensions                                       */
481
    PPC_64H            = 0x0000000000000080ULL,
482
    /*   New wait instruction (PowerPC 2.0x)                                 */
483
    PPC_WAIT           = 0x0000000000000100ULL,
484
    /*   Time base mftb instruction                                          */
485
    PPC_MFTB           = 0x0000000000000200ULL,
486 487 488

    /* Fixed-point unit extensions                                           */
    /*   PowerPC 602 specific                                                */
489
    PPC_602_SPEC       = 0x0000000000000400ULL,
490 491 492 493 494 495
    /*   isel instruction                                                    */
    PPC_ISEL           = 0x0000000000000800ULL,
    /*   popcntb instruction                                                 */
    PPC_POPCNTB        = 0x0000000000001000ULL,
    /*   string load / store                                                 */
    PPC_STRING         = 0x0000000000002000ULL,
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

    /* Floating-point unit extensions                                        */
    /*   Optional floating point instructions                                */
    PPC_FLOAT          = 0x0000000000010000ULL,
    /* New floating-point extensions (PowerPC 2.0x)                          */
    PPC_FLOAT_EXT      = 0x0000000000020000ULL,
    PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
    PPC_FLOAT_FRES     = 0x0000000000080000ULL,
    PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
    PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
    PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
    PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,

    /* Vector/SIMD extensions                                                */
    /*   Altivec support                                                     */
    PPC_ALTIVEC        = 0x0000000001000000ULL,
    /*   PowerPC 2.03 SPE extension                                          */
513
    PPC_SPE            = 0x0000000002000000ULL,
514 515 516 517
    /*   PowerPC 2.03 SPE single-precision floating-point extension          */
    PPC_SPE_SINGLE     = 0x0000000004000000ULL,
    /*   PowerPC 2.03 SPE double-precision floating-point extension          */
    PPC_SPE_DOUBLE     = 0x0000000008000000ULL,
518

519
    /* Optional memory control instructions                                  */
520 521 522 523 524 525 526 527 528
    PPC_MEM_TLBIA      = 0x0000000010000000ULL,
    PPC_MEM_TLBIE      = 0x0000000020000000ULL,
    PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
    /*   sync instruction                                                    */
    PPC_MEM_SYNC       = 0x0000000080000000ULL,
    /*   eieio instruction                                                   */
    PPC_MEM_EIEIO      = 0x0000000100000000ULL,

    /* Cache control instructions                                            */
529
    PPC_CACHE          = 0x0000000200000000ULL,
530
    /*   icbi instruction                                                    */
531
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
532
    /*   dcbz instruction with fixed cache line size                         */
533
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
534
    /*   dcbz instruction with tunable cache line size                       */
535
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
536
    /*   dcba instruction                                                    */
537 538 539
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
    /*   Freescale cache locking instructions                                */
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
540 541 542

    /* MMU related extensions                                                */
    /*   external control instructions                                       */
543
    PPC_EXTERN         = 0x0000010000000000ULL,
544
    /*   segment register access instructions                                */
545
    PPC_SEGMENT        = 0x0000020000000000ULL,
546
    /*   PowerPC 6xx TLB management instructions                             */
547
    PPC_6xx_TLB        = 0x0000040000000000ULL,
548
    /* PowerPC 74xx TLB management instructions                              */
549
    PPC_74xx_TLB       = 0x0000080000000000ULL,
550
    /*   PowerPC 40x TLB management instructions                             */
551
    PPC_40x_TLB        = 0x0000100000000000ULL,
552
    /*   segment register access instructions for PowerPC 64 "bridge"        */
553
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
554
    /*   SLB management                                                      */
555
    PPC_SLBI           = 0x0000400000000000ULL,
556

557
    /* Embedded PowerPC dedicated instructions                               */
558
    PPC_WRTEE          = 0x0001000000000000ULL,
559
    /* PowerPC 40x exception model                                           */
560
    PPC_40x_EXCP       = 0x0002000000000000ULL,
561
    /* PowerPC 405 Mac instructions                                          */
562
    PPC_405_MAC        = 0x0004000000000000ULL,
563
    /* PowerPC 440 specific instructions                                     */
564
    PPC_440_SPEC       = 0x0008000000000000ULL,
565
    /* BookE (embedded) PowerPC specification                                */
566 567 568 569 570 571 572
    PPC_BOOKE          = 0x0010000000000000ULL,
    /* mfapidi instruction                                                   */
    PPC_MFAPIDI        = 0x0020000000000000ULL,
    /* tlbiva instruction                                                    */
    PPC_TLBIVA         = 0x0040000000000000ULL,
    /* tlbivax instruction                                                   */
    PPC_TLBIVAX        = 0x0080000000000000ULL,
573
    /* PowerPC 4xx dedicated instructions                                    */
574
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
575
    /* PowerPC 40x ibct instructions                                         */
576
    PPC_40x_ICBT       = 0x0200000000000000ULL,
577
    /* rfmci is not implemented in all BookE PowerPC                         */
578 579 580 581 582 583 584
    PPC_RFMCI          = 0x0400000000000000ULL,
    /* rfdi instruction                                                      */
    PPC_RFDI           = 0x0800000000000000ULL,
    /* DCR accesses                                                          */
    PPC_DCR            = 0x1000000000000000ULL,
    /* DCR extended accesse                                                  */
    PPC_DCRX           = 0x2000000000000000ULL,
585
    /* user-mode DCR access, implemented in PowerPC 460                      */
586
    PPC_DCRUX          = 0x4000000000000000ULL,
587 588 589 590
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
591 592 593 594 595
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
596
#if defined(__APPLE__)
597
#define OPCODES_SECTION                                                       \
598
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
599
#else
600
#define OPCODES_SECTION                                                       \
601
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
602 603
#endif

604
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
605
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
606
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
607 608 609
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
610
    .pad  = { 0, },                                                           \
B
bellard 已提交
611 612
    .handler = {                                                              \
        .inval   = invl,                                                      \
613
        .type = _typ,                                                         \
B
bellard 已提交
614
        .handler = &gen_##name,                                               \
615
        .oname = stringify(name),                                             \
B
bellard 已提交
616
    },                                                                        \
617
    .oname = stringify(name),                                                 \
B
bellard 已提交
618
}
619 620 621 622 623 624 625 626 627 628 629 630 631 632
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
        .oname = onam,                                                        \
    },                                                                        \
    .oname = onam,                                                            \
}
633 634 635 636 637 638 639 640 641 642 643 644 645 646
#else
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = stringify(name),                                                 \
}
647 648 649 650 651 652 653 654 655 656 657 658 659
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = onam,                                                            \
}
660
#endif
B
bellard 已提交
661 662

#define GEN_OPCODE_MARK(name)                                                 \
663
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
664 665 666
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
667
    .pad  = { 0, },                                                           \
B
bellard 已提交
668 669
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
670
        .type = 0x00,                                                         \
B
bellard 已提交
671 672
        .handler = NULL,                                                      \
    },                                                                        \
673
    .oname = stringify(name),                                                 \
B
bellard 已提交
674 675
}

676 677 678 679 680 681 682 683 684 685 686
/* SPR load/store helpers */
static always_inline void gen_load_spr(TCGv t, int reg)
{
    tcg_gen_ld_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
}

static always_inline void gen_store_spr(int reg, TCGv t)
{
    tcg_gen_st_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
}

B
bellard 已提交
687 688 689 690
/* Start opcode list */
GEN_OPCODE_MARK(start);

/* Invalid instruction */
691 692
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
A
aurel32 已提交
693
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
694 695
}

B
bellard 已提交
696 697
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
698
    .type    = PPC_NONE,
B
bellard 已提交
699 700 701
    .handler = gen_invalid,
};

702 703
/***                           Integer comparison                          ***/

704
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
705 706 707
{
    int l1, l2, l3;

708 709
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
710 711 712 713 714 715
    tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);

    l1 = gen_new_label();
    l2 = gen_new_label();
    l3 = gen_new_label();
    if (s) {
716 717
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
718
    } else {
719 720
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
721 722 723 724 725 726 727 728 729 730 731
    }
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
    tcg_gen_br(l3);
    gen_set_label(l1);
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
    tcg_gen_br(l3);
    gen_set_label(l2);
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
    gen_set_label(l3);
}

732
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
733
{
734 735 736
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp(arg0, t0, s, crf);
    tcg_temp_free(t0);
737 738 739
}

#if defined(TARGET_PPC64)
740
static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
741
{
742
    TCGv t0, t1;
P
pbrook 已提交
743 744
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
745
    if (s) {
746 747
        tcg_gen_ext32s_tl(t0, arg0);
        tcg_gen_ext32s_tl(t1, arg1);
748
    } else {
749 750
        tcg_gen_ext32u_tl(t0, arg0);
        tcg_gen_ext32u_tl(t1, arg1);
751
    }
752 753 754
    gen_op_cmp(t0, t1, s, crf);
    tcg_temp_free(t1);
    tcg_temp_free(t0);
755 756
}

757
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
758
{
759 760 761
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp32(arg0, t0, s, crf);
    tcg_temp_free(t0);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
}
#endif

static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
{
#if defined(TARGET_PPC64)
    if (!(ctx->sf_mode))
        gen_op_cmpi32(reg, 0, 1, 0);
    else
#endif
        gen_op_cmpi(reg, 0, 1, 0);
}

/* cmp */
GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
{
#if defined(TARGET_PPC64)
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     1, crfD(ctx->opcode));
    else
#endif
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                   1, crfD(ctx->opcode));
}

/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
#if defined(TARGET_PPC64)
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                      1, crfD(ctx->opcode));
    else
#endif
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                    1, crfD(ctx->opcode));
}

/* cmpl */
GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
{
#if defined(TARGET_PPC64)
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     0, crfD(ctx->opcode));
    else
#endif
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                   0, crfD(ctx->opcode));
}

/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
#if defined(TARGET_PPC64)
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                      0, crfD(ctx->opcode));
    else
#endif
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                    0, crfD(ctx->opcode));
}

/* isel (PowerPC 2.03 specification) */
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
{
    int l1, l2;
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;
P
pbrook 已提交
833
    TCGv_i32 t0;
834 835 836 837 838

    l1 = gen_new_label();
    l2 = gen_new_label();

    mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
839
    t0 = tcg_temp_new_i32();
840 841
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
842 843 844 845 846 847 848 849
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    else
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    gen_set_label(l2);
P
pbrook 已提交
850
    tcg_temp_free_i32(t0);
851 852
}

B
bellard 已提交
853 854
/***                           Integer arithmetic                          ***/

855 856 857 858
static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
{
    int l1;
    TCGv t0;
B
bellard 已提交
859

860 861 862
    l1 = gen_new_label();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
P
pbrook 已提交
863
    t0 = tcg_temp_local_new();
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    tcg_gen_xor_tl(t0, arg0, arg1);
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        tcg_gen_ext32s_tl(t0, t0);
#endif
    if (sub)
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
    else
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
    tcg_gen_xor_tl(t0, arg1, arg2);
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        tcg_gen_ext32s_tl(t0, t0);
#endif
    if (sub)
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
    else
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    gen_set_label(l1);
    tcg_temp_free(t0);
B
bellard 已提交
885 886
}

887 888 889
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
{
    int l1 = gen_new_label();
890 891

#if defined(TARGET_PPC64)
892 893
    if (!(ctx->sf_mode)) {
        TCGv t0, t1;
P
pbrook 已提交
894 895
        t0 = tcg_temp_new();
        t1 = tcg_temp_new();
896

897 898 899 900
        tcg_gen_ext32u_tl(t0, arg1);
        tcg_gen_ext32u_tl(t1, arg2);
        if (sub) {
            tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
A
aurel32 已提交
901
        } else {
902 903
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
        }
904 905 906 907
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
        gen_set_label(l1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
908 909
    } else
#endif
910 911 912 913 914 915 916 917
    {
        if (sub) {
            tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
        } else {
            tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
        }
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
        gen_set_label(l1);
918
    }
919 920
}

921 922 923 924 925
/* Common add function */
static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                           int add_ca, int compute_ca, int compute_ov)
{
    TCGv t0, t1;
926

927
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
928
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
929 930
        t0 = ret;
    } else {
P
pbrook 已提交
931
        t0 = tcg_temp_local_new();
932
    }
B
bellard 已提交
933

934
    if (add_ca) {
P
pbrook 已提交
935
        t1 = tcg_temp_local_new();
936 937 938
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
    }
B
bellard 已提交
939

940 941 942 943 944 945 946 947 948 949
    if (compute_ca && compute_ov) {
        /* Start with XER CA and OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
    } else if (compute_ca) {
        /* Start with XER CA disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    } else if (compute_ov) {
        /* Start with XER OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
B
bellard 已提交
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    tcg_gen_add_tl(t0, arg1, arg2);

    if (compute_ca) {
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
    }
    if (add_ca) {
        tcg_gen_add_tl(t0, t0, t1);
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
        tcg_temp_free(t1);
    }
    if (compute_ov) {
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
    }

    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, t0);

P
pbrook 已提交
968
    if (!TCGV_EQUAL(t0, ret)) {
969 970 971
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
    }
A
aurel32 已提交
972
}
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
/* Add functions with two operands */
#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER)                  \
{                                                                             \
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
                     add_ca, compute_ca, compute_ov);                         \
}
/* Add functions with one operand and one immediate */
#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
                                add_ca, compute_ca, compute_ov)               \
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER)                  \
{                                                                             \
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
                     cpu_gpr[rA(ctx->opcode)], t0,                            \
                     add_ca, compute_ca, compute_ov);                         \
    tcg_temp_free(t0);                                                        \
}

/* add  add.  addo  addo. */
GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
/* addc  addc.  addco  addco. */
GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
/* adde  adde.  addeo  addeo. */
GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
/* addme  addme.  addmeo  addmeo.  */
GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
/* addze  addze.  addzeo  addzeo.*/
GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1010
{
1011 1012 1013 1014 1015 1016 1017 1018
    target_long simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        /* li case */
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
    } else {
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
    }
1019
}
1020 1021 1022
/* addic  addic.*/
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
                                        int compute_Rc0)
1023
{
1024 1025 1026 1027 1028 1029
    target_long simm = SIMM(ctx->opcode);

    /* Start with XER CA and OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));

    if (likely(simm != 0)) {
P
pbrook 已提交
1030
        TCGv t0 = tcg_temp_local_new();
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        tcg_gen_addi_tl(t0, arg1, simm);
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
    } else {
        tcg_gen_mov_tl(ret, arg1);
    }
    if (compute_Rc0) {
        gen_set_Rc0(ctx, ret);
    }
1041
}
1042
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1043
{
1044
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1045
}
1046
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1047
{
1048
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1049
}
1050 1051
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1052
{
1053 1054 1055 1056 1057 1058 1059 1060
    target_long simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        /* lis case */
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
    } else {
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
    }
1061
}
1062 1063 1064

static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                             int sign, int compute_ov)
1065
{
1066 1067
    int l1 = gen_new_label();
    int l2 = gen_new_label();
P
pbrook 已提交
1068 1069
    TCGv_i32 t0 = tcg_temp_local_new_i32();
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1070

1071 1072 1073
    tcg_gen_trunc_tl_i32(t0, arg1);
    tcg_gen_trunc_tl_i32(t1, arg2);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1074
    if (sign) {
1075 1076 1077
        int l3 = gen_new_label();
        tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
        tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1078
        gen_set_label(l3);
1079
        tcg_gen_div_i32(t0, t0, t1);
1080
    } else {
1081
        tcg_gen_divu_i32(t0, t0, t1);
1082 1083 1084 1085 1086 1087 1088
    }
    if (compute_ov) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
    if (sign) {
1089
        tcg_gen_sari_i32(t0, t0, 31);
1090 1091 1092 1093 1094 1095 1096
    } else {
        tcg_gen_movi_i32(t0, 0);
    }
    if (compute_ov) {
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    }
    gen_set_label(l2);
1097
    tcg_gen_extu_i32_tl(ret, t0);
P
pbrook 已提交
1098 1099
    tcg_temp_free_i32(t0);
    tcg_temp_free_i32(t1);
1100 1101
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
1102
}
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
/* Div functions */
#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)                  \
{                                                                             \
    gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
                     sign, compute_ov);                                       \
}
/* divwu  divwu.  divwuo  divwuo.   */
GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
/* divw  divw.  divwo  divwo.   */
GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1117
#if defined(TARGET_PPC64)
1118 1119
static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                             int sign, int compute_ov)
1120
{
1121 1122
    int l1 = gen_new_label();
    int l2 = gen_new_label();
1123 1124 1125

    tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
    if (sign) {
1126
        int l3 = gen_new_label();
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
        tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
        tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
        gen_set_label(l3);
        tcg_gen_div_i64(ret, arg1, arg2);
    } else {
        tcg_gen_divu_i64(ret, arg1, arg2);
    }
    if (compute_ov) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
    if (sign) {
        tcg_gen_sari_i64(ret, arg1, 63);
    } else {
        tcg_gen_movi_i64(ret, 0);
    }
    if (compute_ov) {
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    }
    gen_set_label(l2);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
1150
}
1151 1152 1153
#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
{                                                                             \
1154 1155 1156
    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
                      sign, compute_ov);                                      \
1157 1158 1159 1160 1161 1162 1163
}
/* divwu  divwu.  divwuo  divwuo.   */
GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
/* divw  divw.  divwo  divwo.   */
GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1164
#endif
1165 1166 1167

/* mulhw  mulhw. */
GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1168
{
P
pbrook 已提交
1169
    TCGv_i64 t0, t1;
1170

P
pbrook 已提交
1171 1172
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
#if defined(TARGET_PPC64)
    tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
#else
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
P
pbrook 已提交
1185 1186
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1187 1188
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1189
}
1190 1191
/* mulhwu  mulhwu.  */
GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1192
{
P
pbrook 已提交
1193
    TCGv_i64 t0, t1;
1194

P
pbrook 已提交
1195 1196
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1197
#if defined(TARGET_PPC64)
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
#else
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
P
pbrook 已提交
1209 1210
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1211 1212
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1213
}
1214 1215
/* mullw  mullw. */
GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1216
{
1217 1218
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                   cpu_gpr[rB(ctx->opcode)]);
1219
    tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1220 1221
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1222
}
1223 1224
/* mullwo  mullwo. */
GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1225
{
1226
    int l1;
P
pbrook 已提交
1227
    TCGv_i64 t0, t1;
1228

P
pbrook 已提交
1229 1230
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1231 1232 1233 1234 1235 1236 1237 1238 1239
    l1 = gen_new_label();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
#if defined(TARGET_PPC64)
    tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
#else
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1240
#endif
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    tcg_gen_mul_i64(t0, t0, t1);
#if defined(TARGET_PPC64)
    tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
#else
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_ext32s_i64(t1, t0);
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
#endif
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    gen_set_label(l1);
P
pbrook 已提交
1252 1253
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1254 1255
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1256
}
1257 1258
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1259
{
1260 1261
    tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                    SIMM(ctx->opcode));
1262 1263
}
#if defined(TARGET_PPC64)
1264 1265 1266
#define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
{                                                                             \
P
pbrook 已提交
1267
    gen_helper_##name (cpu_gpr[rD(ctx->opcode)],                              \
1268 1269 1270
                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);   \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1271
}
1272 1273 1274 1275 1276 1277
/* mulhd  mulhd. */
GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
/* mulhdu  mulhdu. */
GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
/* mulld  mulld. */
GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1278
{
1279 1280 1281 1282
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                   cpu_gpr[rB(ctx->opcode)]);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1283
}
1284 1285
/* mulldo  mulldo. */
GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1286
#endif
1287 1288

/* neg neg. nego nego. */
A
aurel32 已提交
1289
static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1290
{
A
aurel32 已提交
1291 1292
    int l1 = gen_new_label();
    int l2 = gen_new_label();
P
pbrook 已提交
1293
    TCGv t0 = tcg_temp_local_new();
1294
#if defined(TARGET_PPC64)
1295
    if (ctx->sf_mode) {
A
aurel32 已提交
1296
        tcg_gen_mov_tl(t0, arg1);
A
aurel32 已提交
1297 1298 1299 1300 1301
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
    } else
#endif
    {
        tcg_gen_ext32s_tl(t0, arg1);
1302 1303 1304 1305 1306 1307 1308 1309
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
    }
    tcg_gen_neg_tl(ret, arg1);
    if (ov_check) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
A
aurel32 已提交
1310
    tcg_gen_mov_tl(ret, t0);
1311 1312 1313 1314
    if (ov_check) {
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    }
    gen_set_label(l2);
A
aurel32 已提交
1315
    tcg_temp_free(t0);
1316 1317 1318 1319
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
}
GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1320
{
A
aurel32 已提交
1321
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1322
}
1323
GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
B
bellard 已提交
1324
{
A
aurel32 已提交
1325
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
B
bellard 已提交
1326
}
1327 1328 1329 1330

/* Common subf function */
static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                            int add_ca, int compute_ca, int compute_ov)
B
bellard 已提交
1331
{
1332
    TCGv t0, t1;
1333

1334
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
1335
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1336
        t0 = ret;
J
j_mayer 已提交
1337
    } else {
P
pbrook 已提交
1338
        t0 = tcg_temp_local_new();
1339
    }
1340

1341
    if (add_ca) {
P
pbrook 已提交
1342
        t1 = tcg_temp_local_new();
1343 1344
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
1345
    }
B
bellard 已提交
1346

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    if (compute_ca && compute_ov) {
        /* Start with XER CA and OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
    } else if (compute_ca) {
        /* Start with XER CA disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    } else if (compute_ov) {
        /* Start with XER OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }

    if (add_ca) {
        tcg_gen_not_tl(t0, arg1);
        tcg_gen_add_tl(t0, t0, arg2);
        gen_op_arith_compute_ca(ctx, t0, arg2, 0);
        tcg_gen_add_tl(t0, t0, t1);
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
        tcg_temp_free(t1);
B
bellard 已提交
1365
    } else {
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
        tcg_gen_sub_tl(t0, arg2, arg1);
        if (compute_ca) {
            gen_op_arith_compute_ca(ctx, t0, arg2, 1);
        }
    }
    if (compute_ov) {
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
    }

    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, t0);

P
pbrook 已提交
1378
    if (!TCGV_EQUAL(t0, ret)) {
1379 1380
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
B
bellard 已提交
1381 1382
    }
}
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
/* Sub functions with Two operands functions */
#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER)                  \
{                                                                             \
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
                      add_ca, compute_ca, compute_ov);                        \
}
/* Sub functions with one operand and one immediate */
#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
                                add_ca, compute_ca, compute_ov)               \
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER)                  \
{                                                                             \
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                      cpu_gpr[rA(ctx->opcode)], t0,                           \
                      add_ca, compute_ca, compute_ov);                        \
    tcg_temp_free(t0);                                                        \
}
/* subf  subf.  subfo  subfo. */
GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
/* subfc  subfc.  subfco  subfco. */
GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
/* subfe  subfe.  subfeo  subfo. */
GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
/* subfme  subfme.  subfmeo  subfmeo.  */
GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
/* subfze  subfze.  subfzeo  subfzeo.*/
GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
B
bellard 已提交
1417 1418 1419
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1420 1421
    /* Start with XER CA and OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
P
pbrook 已提交
1422
    TCGv t0 = tcg_temp_local_new();
1423 1424 1425 1426 1427 1428
    TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
    tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
    gen_op_arith_compute_ca(ctx, t0, t1, 1);
    tcg_temp_free(t1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
B
bellard 已提交
1429 1430 1431
}

/***                            Integer logical                            ***/
1432 1433
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
B
bellard 已提交
1434
{                                                                             \
1435 1436
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
       cpu_gpr[rB(ctx->opcode)]);                                             \
1437
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1438
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1439 1440
}

1441
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1442
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1443
{                                                                             \
1444
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1445
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1446
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1447 1448 1449
}

/* and & and. */
1450
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
B
bellard 已提交
1451
/* andc & andc. */
1452
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
B
bellard 已提交
1453
/* andi. */
1454
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1455
{
1456 1457
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1458 1459
}
/* andis. */
1460
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1461
{
1462 1463
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1464 1465
}
/* cntlzw */
1466 1467
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
{
P
pbrook 已提交
1468
    gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1469
    if (unlikely(Rc(ctx->opcode) != 0))
P
pbrook 已提交
1470
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1471
}
B
bellard 已提交
1472
/* eqv & eqv. */
1473
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
B
bellard 已提交
1474
/* extsb & extsb. */
1475
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
B
bellard 已提交
1476
/* extsh & extsh. */
1477
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
B
bellard 已提交
1478
/* nand & nand. */
1479
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
B
bellard 已提交
1480
/* nor & nor. */
1481
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
B
bellard 已提交
1482
/* or & or. */
1483 1484
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1485 1486 1487 1488 1489 1490 1491
    int rs, ra, rb;

    rs = rS(ctx->opcode);
    ra = rA(ctx->opcode);
    rb = rB(ctx->opcode);
    /* Optimisation for mr. ri case */
    if (rs != ra || rs != rb) {
1492 1493 1494 1495
        if (rs != rb)
            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
        else
            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1496
        if (unlikely(Rc(ctx->opcode) != 0))
1497
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1498
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1499
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1500 1501
#if defined(TARGET_PPC64)
    } else {
1502 1503
        int prio = 0;

1504 1505 1506
        switch (rs) {
        case 1:
            /* Set process priority to low */
1507
            prio = 2;
1508 1509 1510
            break;
        case 6:
            /* Set process priority to medium-low */
1511
            prio = 3;
1512 1513 1514
            break;
        case 2:
            /* Set process priority to normal */
1515
            prio = 4;
1516
            break;
1517 1518
#if !defined(CONFIG_USER_ONLY)
        case 31:
A
aurel32 已提交
1519
            if (ctx->mem_idx > 0) {
1520
                /* Set process priority to very low */
1521
                prio = 1;
1522 1523 1524
            }
            break;
        case 5:
A
aurel32 已提交
1525
            if (ctx->mem_idx > 0) {
1526
                /* Set process priority to medium-hight */
1527
                prio = 5;
1528 1529 1530
            }
            break;
        case 3:
A
aurel32 已提交
1531
            if (ctx->mem_idx > 0) {
1532
                /* Set process priority to high */
1533
                prio = 6;
1534 1535 1536
            }
            break;
        case 7:
A
aurel32 已提交
1537
            if (ctx->mem_idx > 1) {
1538
                /* Set process priority to very high */
1539
                prio = 7;
1540 1541 1542
            }
            break;
#endif
1543 1544 1545 1546
        default:
            /* nop */
            break;
        }
1547
        if (prio) {
P
pbrook 已提交
1548
            TCGv t0 = tcg_temp_new();
1549
            gen_load_spr(t0, SPR_PPR);
1550 1551
            tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
            tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1552
            gen_store_spr(SPR_PPR, t0);
1553
            tcg_temp_free(t0);
1554
        }
1555
#endif
1556 1557
    }
}
B
bellard 已提交
1558
/* orc & orc. */
1559
GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
B
bellard 已提交
1560
/* xor & xor. */
1561 1562 1563
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    /* Optimisation for "set to zero" case */
1564
    if (rS(ctx->opcode) != rB(ctx->opcode))
A
aurel32 已提交
1565
        tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1566 1567
    else
        tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1568
    if (unlikely(Rc(ctx->opcode) != 0))
1569
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1570
}
B
bellard 已提交
1571 1572 1573
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1574
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1575

1576 1577
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1578
        /* XXX: should handle special NOPs for POWER series */
1579
        return;
1580
    }
1581
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1582 1583 1584 1585
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1586
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1587

1588 1589 1590
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1591
    }
1592
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1593 1594 1595 1596
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1597
    target_ulong uimm = UIMM(ctx->opcode);
1598 1599 1600 1601 1602

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1603
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1604 1605 1606 1607
}
/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1608
    target_ulong uimm = UIMM(ctx->opcode);
1609 1610 1611 1612 1613

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1614
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1615
}
1616
/* popcntb : PowerPC 2.03 specification */
1617
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1618 1619 1620
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
P
pbrook 已提交
1621
        gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1622 1623
    else
#endif
P
pbrook 已提交
1624
        gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1625 1626 1627 1628
}

#if defined(TARGET_PPC64)
/* extsw & extsw. */
1629
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1630
/* cntlzd */
1631 1632
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
{
P
pbrook 已提交
1633
    gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1634 1635 1636
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1637 1638
#endif

B
bellard 已提交
1639 1640 1641 1642
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1643
    uint32_t mb, me, sh;
B
bellard 已提交
1644 1645 1646

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1647
    sh = SH(ctx->opcode);
1648 1649 1650 1651
    if (likely(sh == 0 && mb == 0 && me == 31)) {
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    } else {
        target_ulong mask;
P
pbrook 已提交
1652 1653
        TCGv t1;
        TCGv t0 = tcg_temp_new();
1654
#if defined(TARGET_PPC64)
P
pbrook 已提交
1655 1656 1657 1658 1659
        TCGv_i32 t2 = tcg_temp_new_i32();
        tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_rotli_i32(t2, t2, sh);
        tcg_gen_extu_i32_i64(t0, t2);
        tcg_temp_free_i32(t2);
1660 1661 1662
#else
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
#endif
1663
#if defined(TARGET_PPC64)
1664 1665
        mb += 32;
        me += 32;
1666
#endif
1667
        mask = MASK(mb, me);
P
pbrook 已提交
1668
        t1 = tcg_temp_new();
1669 1670 1671 1672 1673 1674
        tcg_gen_andi_tl(t0, t0, mask);
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
    }
1675
    if (unlikely(Rc(ctx->opcode) != 0))
1676
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1677 1678 1679 1680 1681
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1682

B
bellard 已提交
1683 1684 1685
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1686 1687 1688 1689 1690

    if (likely(mb == 0 && me == (31 - sh))) {
        if (likely(sh == 0)) {
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
        } else {
P
pbrook 已提交
1691
            TCGv t0 = tcg_temp_new();
1692 1693 1694 1695
            tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_shli_tl(t0, t0, sh);
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
            tcg_temp_free(t0);
B
bellard 已提交
1696
        }
1697
    } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
P
pbrook 已提交
1698
        TCGv t0 = tcg_temp_new();
1699 1700 1701 1702 1703
        tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_shri_tl(t0, t0, mb);
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
        tcg_temp_free(t0);
    } else {
P
pbrook 已提交
1704
        TCGv t0 = tcg_temp_new();
1705
#if defined(TARGET_PPC64)
P
pbrook 已提交
1706
        TCGv_i32 t1 = tcg_temp_new_i32();
1707 1708 1709
        tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_rotli_i32(t1, t1, sh);
        tcg_gen_extu_i32_i64(t0, t1);
P
pbrook 已提交
1710
        tcg_temp_free_i32(t1);
1711 1712 1713
#else
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
#endif
1714
#if defined(TARGET_PPC64)
1715 1716
        mb += 32;
        me += 32;
1717
#endif
1718 1719 1720
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
        tcg_temp_free(t0);
    }
1721
    if (unlikely(Rc(ctx->opcode) != 0))
1722
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1723 1724 1725 1726 1727
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;
1728 1729
    TCGv t0;
#if defined(TARGET_PPC64)
P
pbrook 已提交
1730
    TCGv_i32 t1, t2;
1731
#endif
B
bellard 已提交
1732 1733 1734

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
P
pbrook 已提交
1735
    t0 = tcg_temp_new();
1736
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1737
#if defined(TARGET_PPC64)
P
pbrook 已提交
1738 1739
    t1 = tcg_temp_new_i32();
    t2 = tcg_temp_new_i32();
1740 1741 1742 1743
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_trunc_i64_i32(t2, t0);
    tcg_gen_rotl_i32(t1, t1, t2);
    tcg_gen_extu_i32_i64(t0, t1);
P
pbrook 已提交
1744 1745
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
1746 1747 1748
#else
    tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
#endif
1749 1750 1751 1752 1753
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
1754
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1755
    } else {
1756
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
B
bellard 已提交
1757
    }
1758
    tcg_temp_free(t0);
1759
    if (unlikely(Rc(ctx->opcode) != 0))
1760
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1761 1762
}

1763 1764
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1765
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1766 1767 1768
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1769 1770
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1771 1772 1773 1774
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1775
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1776 1777 1778
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1779 1780
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1781 1782 1783
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1784 1785
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1786 1787 1788
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1789 1790
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1791 1792 1793
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1794

1795 1796
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1797
{
1798 1799 1800 1801 1802
    if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
        tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
    } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
        tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
    } else {
P
pbrook 已提交
1803
        TCGv t0 = tcg_temp_new();
1804
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1805
        if (likely(mb == 0 && me == 63)) {
1806
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1807 1808
        } else {
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
J
j_mayer 已提交
1809
        }
1810
        tcg_temp_free(t0);
J
j_mayer 已提交
1811 1812
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1813
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
J
j_mayer 已提交
1814
}
1815
/* rldicl - rldicl. */
1816
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1817
{
J
j_mayer 已提交
1818
    uint32_t sh, mb;
1819

J
j_mayer 已提交
1820 1821
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1822
    gen_rldinm(ctx, mb, 63, sh);
1823
}
J
j_mayer 已提交
1824
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1825
/* rldicr - rldicr. */
1826
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1827
{
J
j_mayer 已提交
1828
    uint32_t sh, me;
1829

J
j_mayer 已提交
1830 1831
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1832
    gen_rldinm(ctx, 0, me, sh);
1833
}
J
j_mayer 已提交
1834
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1835
/* rldic - rldic. */
1836
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1837
{
J
j_mayer 已提交
1838
    uint32_t sh, mb;
1839

J
j_mayer 已提交
1840 1841
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1842 1843 1844 1845
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1846 1847
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1848
{
1849
    TCGv t0;
1850 1851 1852

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
P
pbrook 已提交
1853
    t0 = tcg_temp_new();
1854
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1855
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
J
j_mayer 已提交
1856
    if (unlikely(mb != 0 || me != 63)) {
1857 1858 1859 1860 1861
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
    } else {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    }
    tcg_temp_free(t0);
J
j_mayer 已提交
1862
    if (unlikely(Rc(ctx->opcode) != 0))
1863
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1864
}
J
j_mayer 已提交
1865

1866
/* rldcl - rldcl. */
1867
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1868
{
J
j_mayer 已提交
1869
    uint32_t mb;
1870

J
j_mayer 已提交
1871
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1872
    gen_rldnm(ctx, mb, 63);
1873
}
1874
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1875
/* rldcr - rldcr. */
1876
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1877
{
J
j_mayer 已提交
1878
    uint32_t me;
1879

J
j_mayer 已提交
1880
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1881
    gen_rldnm(ctx, 0, me);
1882
}
1883
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1884
/* rldimi - rldimi. */
1885
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1886
{
1887
    uint32_t sh, mb, me;
1888

J
j_mayer 已提交
1889 1890
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1891
    me = 63 - sh;
1892 1893 1894 1895 1896 1897
    if (unlikely(sh == 0 && mb == 0)) {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    } else {
        TCGv t0, t1;
        target_ulong mask;

P
pbrook 已提交
1898
        t0 = tcg_temp_new();
1899
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
P
pbrook 已提交
1900
        t1 = tcg_temp_new();
1901 1902 1903 1904 1905 1906
        mask = MASK(mb, me);
        tcg_gen_andi_tl(t0, t0, mask);
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
J
j_mayer 已提交
1907 1908
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1909
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1910
}
1911
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1912 1913
#endif

B
bellard 已提交
1914 1915
/***                             Integer shift                             ***/
/* slw & slw. */
1916 1917
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
{
1918
    TCGv t0;
1919 1920 1921 1922
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

P
pbrook 已提交
1923
    t0 = tcg_temp_local_new();
A
aurel32 已提交
1924 1925
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1926 1927 1928
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
1929
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1930 1931
    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
1932
    tcg_temp_free(t0);
1933 1934 1935
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
B
bellard 已提交
1936
/* sraw & sraw. */
1937 1938
GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
{
P
pbrook 已提交
1939 1940
    gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1941 1942 1943
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
B
bellard 已提交
1944 1945 1946
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1947 1948 1949
    int sh = SH(ctx->opcode);
    if (sh != 0) {
        int l1, l2;
1950
        TCGv t0;
1951 1952
        l1 = gen_new_label();
        l2 = gen_new_label();
P
pbrook 已提交
1953
        t0 = tcg_temp_local_new();
1954 1955 1956 1957
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1958
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1959 1960
        tcg_gen_br(l2);
        gen_set_label(l1);
1961
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1962
        gen_set_label(l2);
1963 1964 1965
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
        tcg_temp_free(t0);
1966 1967
    } else {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1968
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1969
    }
1970
    if (unlikely(Rc(ctx->opcode) != 0))
1971
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1972 1973
}
/* srw & srw. */
1974 1975
GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
{
1976
    TCGv t0, t1;
1977 1978 1979
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();
1980

P
pbrook 已提交
1981
    t0 = tcg_temp_local_new();
A
aurel32 已提交
1982 1983
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1984 1985 1986
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
P
pbrook 已提交
1987
    t1 = tcg_temp_new();
1988 1989 1990
    tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
    tcg_temp_free(t1);
1991
    gen_set_label(l2);
1992
    tcg_temp_free(t0);
1993 1994 1995
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1996 1997
#if defined(TARGET_PPC64)
/* sld & sld. */
1998 1999
GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
{
2000
    TCGv t0;
2001 2002 2003 2004
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

P
pbrook 已提交
2005
    t0 = tcg_temp_local_new();
A
aurel32 已提交
2006 2007
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2008 2009 2010
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
2011
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2012
    gen_set_label(l2);
2013
    tcg_temp_free(t0);
2014 2015 2016
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
2017
/* srad & srad. */
2018 2019
GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
{
P
pbrook 已提交
2020 2021
    gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2022 2023 2024
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
2025
/* sradi & sradi. */
2026
static always_inline void gen_sradi (DisasContext *ctx, int n)
2027
{
2028
    int sh = SH(ctx->opcode) + (n << 5);
2029
    if (sh != 0) {
2030
        int l1, l2;
2031
        TCGv t0;
2032 2033
        l1 = gen_new_label();
        l2 = gen_new_label();
P
pbrook 已提交
2034
        t0 = tcg_temp_local_new();
2035
        tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2036 2037
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2038
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2039 2040
        tcg_gen_br(l2);
        gen_set_label(l1);
2041
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2042
        gen_set_label(l2);
2043
        tcg_temp_free(t0);
2044 2045 2046
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
    } else {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2047
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2048 2049
    }
    if (unlikely(Rc(ctx->opcode) != 0))
2050
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2051
}
2052
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2053 2054 2055
{
    gen_sradi(ctx, 0);
}
2056
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2057 2058 2059 2060
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
2061 2062
GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
{
2063
    TCGv t0;
2064 2065 2066 2067
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

P
pbrook 已提交
2068
    t0 = tcg_temp_local_new();
A
aurel32 已提交
2069 2070
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2071 2072 2073
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
2074
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2075
    gen_set_label(l2);
2076
    tcg_temp_free(t0);
2077 2078 2079
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
2080
#endif
B
bellard 已提交
2081 2082

/***                       Floating-Point arithmetic                       ***/
2083
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2084
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2085
{                                                                             \
2086
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2087
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2088 2089
        return;                                                               \
    }                                                                         \
2090 2091
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2092
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2093 2094
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2095
    if (isfloat) {                                                            \
A
aurel32 已提交
2096
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2097
    }                                                                         \
A
aurel32 已提交
2098 2099
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
                     Rc(ctx->opcode) != 0);                                   \
2100 2101
}

2102 2103 2104
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2105

2106 2107
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2108
{                                                                             \
2109
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2110
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2111 2112
        return;                                                               \
    }                                                                         \
2113 2114
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2115
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2116 2117
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rB(ctx->opcode)]);                               \
2118
    if (isfloat) {                                                            \
A
aurel32 已提交
2119
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2120
    }                                                                         \
A
aurel32 已提交
2121 2122
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2123
}
2124 2125 2126
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2127

2128 2129
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2130
{                                                                             \
2131
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2132
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2133 2134
        return;                                                               \
    }                                                                         \
2135 2136
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2137
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2138 2139
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                       cpu_fpr[rC(ctx->opcode)]);                             \
2140
    if (isfloat) {                                                            \
A
aurel32 已提交
2141
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2142
    }                                                                         \
A
aurel32 已提交
2143 2144
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2145
}
2146 2147 2148
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2149

2150
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2151
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2152
{                                                                             \
2153
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2154
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2155 2156
        return;                                                               \
    }                                                                         \
2157 2158
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2159
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2160 2161 2162
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
2163 2164
}

2165
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2166
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2167
{                                                                             \
2168
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2169
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2170 2171
        return;                                                               \
    }                                                                         \
2172 2173
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2174
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2175 2176 2177
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
2178 2179
}

2180
/* fadd - fadds */
2181
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2182
/* fdiv - fdivs */
2183
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2184
/* fmul - fmuls */
2185
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
2186

2187
/* fre */
2188
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2189

2190
/* fres */
2191
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
2192

2193
/* frsqrte */
2194 2195 2196
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);

/* frsqrtes */
A
aurel32 已提交
2197
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2198
{
A
aurel32 已提交
2199
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2200
        gen_exception(ctx, POWERPC_EXCP_FPU);
A
aurel32 已提交
2201 2202
        return;
    }
2203 2204
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
2205 2206 2207 2208
    gen_reset_fpstatus();
    gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2209
}
B
bellard 已提交
2210

2211
/* fsel */
2212
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2213
/* fsub - fsubs */
2214
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
2215 2216
/* Optional: */
/* fsqrt */
2217
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2218
{
2219
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2220
        gen_exception(ctx, POWERPC_EXCP_FPU);
2221 2222
        return;
    }
2223 2224
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2225
    gen_reset_fpstatus();
A
aurel32 已提交
2226 2227
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2228
}
B
bellard 已提交
2229

2230
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
2231
{
2232
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2233
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2234 2235
        return;
    }
2236 2237
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2238
    gen_reset_fpstatus();
A
aurel32 已提交
2239 2240 2241
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
B
bellard 已提交
2242 2243 2244
}

/***                     Floating-Point multiply-and-add                   ***/
2245
/* fmadd - fmadds */
2246
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2247
/* fmsub - fmsubs */
2248
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2249
/* fnmadd - fnmadds */
2250
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2251
/* fnmsub - fnmsubs */
2252
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
B
bellard 已提交
2253 2254 2255

/***                     Floating-Point round & convert                    ***/
/* fctiw */
2256
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2257
/* fctiwz */
2258
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2259
/* frsp */
2260
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
J
j_mayer 已提交
2261 2262
#if defined(TARGET_PPC64)
/* fcfid */
2263
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
J
j_mayer 已提交
2264
/* fctid */
2265
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2266
/* fctidz */
2267
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2268
#endif
B
bellard 已提交
2269

2270
/* frin */
2271
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2272
/* friz */
2273
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2274
/* frip */
2275
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2276
/* frim */
2277
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2278

B
bellard 已提交
2279 2280
/***                         Floating-Point compare                        ***/
/* fcmpo */
2281
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2282
{
A
aurel32 已提交
2283
    TCGv_i32 crf;
2284
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2285
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2286 2287
        return;
    }
2288 2289
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2290
    gen_reset_fpstatus();
A
aurel32 已提交
2291 2292
    crf = tcg_const_i32(crfD(ctx->opcode));
    gen_helper_fcmpo(cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)], crf);
A
aurel32 已提交
2293
    tcg_temp_free_i32(crf);
A
aurel32 已提交
2294
    gen_helper_float_check_status();
B
bellard 已提交
2295 2296 2297
}

/* fcmpu */
2298
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2299
{
A
aurel32 已提交
2300
    TCGv_i32 crf;
2301
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2302
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2303 2304
        return;
    }
2305 2306
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2307
    gen_reset_fpstatus();
A
aurel32 已提交
2308 2309
    crf = tcg_const_i32(crfD(ctx->opcode));
    gen_helper_fcmpu(cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)], crf);
A
aurel32 已提交
2310
    tcg_temp_free_i32(crf);
A
aurel32 已提交
2311
    gen_helper_float_check_status();
B
bellard 已提交
2312 2313
}

2314 2315
/***                         Floating-point move                           ***/
/* fabs */
2316 2317
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2318 2319

/* fmr  - fmr. */
2320
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2321 2322
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
2323
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2324
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2325 2326
        return;
    }
A
aurel32 已提交
2327 2328
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2329 2330 2331
}

/* fnabs */
2332 2333
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2334
/* fneg */
2335 2336
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2337

B
bellard 已提交
2338 2339 2340 2341
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
2342 2343
    int bfa;

2344
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2345
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2346 2347
        return;
    }
2348
    bfa = 4 * (7 - crfS(ctx->opcode));
2349 2350
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
A
aurel32 已提交
2351
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
B
bellard 已提交
2352 2353 2354 2355 2356
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
2357
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2358
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2359 2360
        return;
    }
2361
    gen_reset_fpstatus();
A
aurel32 已提交
2362 2363
    tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
B
bellard 已提交
2364 2365 2366 2367 2368
}

/* mtfsb0 */
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
2369
    uint8_t crb;
2370

2371
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2372
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2373 2374
        return;
    }
A
aurel32 已提交
2375
    crb = 31 - crbD(ctx->opcode);
2376
    gen_reset_fpstatus();
A
aurel32 已提交
2377
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2378 2379 2380 2381
        TCGv_i32 t0;
        /* NIP cannot be restored if the memory exception comes from an helper */
        gen_update_nip(ctx, ctx->nip - 4);
        t0 = tcg_const_i32(crb);
A
aurel32 已提交
2382 2383 2384
        gen_helper_fpscr_clrbit(t0);
        tcg_temp_free_i32(t0);
    }
2385
    if (unlikely(Rc(ctx->opcode) != 0)) {
2386
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2387
    }
B
bellard 已提交
2388 2389 2390 2391 2392
}

/* mtfsb1 */
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
2393
    uint8_t crb;
2394

2395
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2396
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2397 2398
        return;
    }
A
aurel32 已提交
2399
    crb = 31 - crbD(ctx->opcode);
2400 2401
    gen_reset_fpstatus();
    /* XXX: we pretend we can only do IEEE floating-point computations */
A
aurel32 已提交
2402
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2403 2404 2405 2406
        TCGv_i32 t0;
        /* NIP cannot be restored if the memory exception comes from an helper */
        gen_update_nip(ctx, ctx->nip - 4);
        t0 = tcg_const_i32(crb);
A
aurel32 已提交
2407
        gen_helper_fpscr_setbit(t0);
2408
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2409
    }
2410
    if (unlikely(Rc(ctx->opcode) != 0)) {
2411
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2412 2413
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2414
    gen_helper_float_check_status();
B
bellard 已提交
2415 2416 2417 2418 2419
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
2420
    TCGv_i32 t0;
A
aurel32 已提交
2421

2422
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2423
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2424 2425
        return;
    }
2426 2427
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2428
    gen_reset_fpstatus();
A
aurel32 已提交
2429 2430
    t0 = tcg_const_i32(FM(ctx->opcode));
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2431
    tcg_temp_free_i32(t0);
2432
    if (unlikely(Rc(ctx->opcode) != 0)) {
2433
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2434 2435
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2436
    gen_helper_float_check_status();
B
bellard 已提交
2437 2438 2439 2440 2441
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
2442
    int bf, sh;
2443 2444
    TCGv_i64 t0;
    TCGv_i32 t1;
2445

2446
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2447
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2448 2449
        return;
    }
2450 2451
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
2452 2453
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2454
    gen_reset_fpstatus();
2455
    t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
A
aurel32 已提交
2456 2457
    t1 = tcg_const_i32(1 << sh);
    gen_helper_store_fpscr(t0, t1);
2458 2459
    tcg_temp_free_i64(t0);
    tcg_temp_free_i32(t1);
2460
    if (unlikely(Rc(ctx->opcode) != 0)) {
2461
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2462 2463
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2464
    gen_helper_float_check_status();
B
bellard 已提交
2465 2466
}

2467 2468
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
A
aurel32 已提交
2469
static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2470 2471 2472
{
    target_long simm = SIMM(ctx->opcode);

2473
    simm &= ~maskl;
A
aurel32 已提交
2474 2475 2476 2477 2478 2479
    if (rA(ctx->opcode) == 0) {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_movi_tl(EA, (uint32_t)simm);
        } else
#endif
2480
        tcg_gen_movi_tl(EA, simm);
A
aurel32 已提交
2481
    } else if (likely(simm != 0)) {
2482
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
A
aurel32 已提交
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    } else {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
        } else
#endif
2494
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
2495
    }
2496 2497
}

A
aurel32 已提交
2498
static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2499
{
A
aurel32 已提交
2500 2501 2502 2503 2504 2505
    if (rA(ctx->opcode) == 0) {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
        } else
#endif
2506
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2507
    } else {
2508
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2509 2510 2511 2512 2513 2514
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
2515 2516
}

A
aurel32 已提交
2517
static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2518
{
A
aurel32 已提交
2519
    if (rA(ctx->opcode) == 0) {
2520
        tcg_gen_movi_tl(EA, 0);
A
aurel32 已提交
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
    } else {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
        } else
#endif
            tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
    }
}

static always_inline void gen_addr_add (DisasContext *ctx, TCGv ret, TCGv arg1, target_long val)
{
    tcg_gen_addi_tl(ret, arg1, val);
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode) {
        tcg_gen_ext32u_tl(ret, ret);
    }
#endif
2539 2540
}

2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
static always_inline void gen_check_align (DisasContext *ctx, TCGv EA, int mask)
{
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1, t2;
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    tcg_gen_andi_tl(t0, EA, mask);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
    t2 = tcg_const_i32(0);
    gen_helper_raise_exception_err(t1, t2);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    gen_set_label(l1);
    tcg_temp_free(t0);
}

2559
/***                             Integer load                              ***/
A
aurel32 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
static always_inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
{
    tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
}

static always_inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
{
    tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
}

static always_inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
{
    tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
A
aurel32 已提交
2574
#if defined(TARGET_PPC64)
A
aurel32 已提交
2575 2576
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
2577
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2578
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2579
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2580 2581 2582 2583
#else
        tcg_gen_bswap16_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2584 2585
}

A
aurel32 已提交
2586
static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2587
{
A
aurel32 已提交
2588 2589
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2590
        TCGv_i32 t0;
A
aurel32 已提交
2591
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
P
pbrook 已提交
2592
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2593
        tcg_gen_trunc_tl_i32(t0, arg1);
2594
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2595 2596
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_gen_ext16s_tl(arg1, arg1);
P
pbrook 已提交
2597
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2598 2599 2600 2601 2602 2603 2604 2605
#else
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
        tcg_gen_bswap16_i32(arg1, arg1);
        tcg_gen_ext16s_i32(arg1, arg1);
#endif
    } else {
        tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2606 2607
}

A
aurel32 已提交
2608
static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2609
{
A
aurel32 已提交
2610 2611 2612 2613 2614
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
2615
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2616
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2617
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2618 2619 2620 2621
#else
        tcg_gen_bswap_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2622 2623
}

A
aurel32 已提交
2624 2625
#if defined(TARGET_PPC64)
static always_inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2626
{
B
blueswir1 已提交
2627
    if (unlikely(ctx->le_mode)) {
P
pbrook 已提交
2628
        TCGv_i32 t0;
A
aurel32 已提交
2629
        tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
P
pbrook 已提交
2630
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2631
        tcg_gen_trunc_tl_i32(t0, arg1);
2632
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2633
        tcg_gen_ext_i32_tl(arg1, t0);
P
pbrook 已提交
2634
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2635
    } else
A
aurel32 已提交
2636
        tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2637
}
A
aurel32 已提交
2638
#endif
A
aurel32 已提交
2639

A
aurel32 已提交
2640
static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2641
{
A
aurel32 已提交
2642 2643 2644 2645
    tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
        tcg_gen_bswap_i64(arg1, arg1);
    }
A
aurel32 已提交
2646 2647
}

A
aurel32 已提交
2648
static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2649
{
A
aurel32 已提交
2650
    tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2651 2652
}

A
aurel32 已提交
2653
static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2654
{
A
aurel32 已提交
2655 2656
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2657
        TCGv_i32 t0;
A
aurel32 已提交
2658
        TCGv t1;
P
pbrook 已提交
2659
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2660
        tcg_gen_trunc_tl_i32(t0, arg1);
2661 2662
        tcg_gen_ext16u_i32(t0, t0);
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2663
        t1 = tcg_temp_new();
2664
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2665
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext16u_tl(t0, arg1);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2678 2679
}

A
aurel32 已提交
2680
static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2681
{
A
aurel32 已提交
2682 2683
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2684
        TCGv_i32 t0;
A
aurel32 已提交
2685
        TCGv t1;
P
pbrook 已提交
2686
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2687
        tcg_gen_trunc_tl_i32(t0, arg1);
2688
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2689
        t1 = tcg_temp_new();
2690
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2691
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new_i32();
        tcg_gen_bswap_i32(t0, arg1);
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2703 2704
}

A
aurel32 已提交
2705
static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2706
{
A
aurel32 已提交
2707
    if (unlikely(ctx->le_mode)) {
P
pbrook 已提交
2708
        TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
2709 2710
        tcg_gen_bswap_i64(t0, arg1);
        tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
P
pbrook 已提交
2711
        tcg_temp_free_i64(t0);
A
aurel32 已提交
2712
    } else
A
aurel32 已提交
2713
        tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2714 2715
}

2716 2717
#define GEN_LD(name, ldop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2718
{                                                                             \
A
aurel32 已提交
2719 2720 2721 2722 2723
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2724
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2725 2726
}

2727 2728
#define GEN_LDU(name, ldop, opc, type)                                        \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2729
{                                                                             \
A
aurel32 已提交
2730
    TCGv EA;                                                                  \
2731 2732
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2733
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2734
        return;                                                               \
2735
    }                                                                         \
A
aurel32 已提交
2736
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2737
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2738
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2739
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2740
    else                                                                      \
A
aurel32 已提交
2741 2742
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2743 2744
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2745 2746
}

2747 2748
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2749
{                                                                             \
A
aurel32 已提交
2750
    TCGv EA;                                                                  \
2751 2752
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2753
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2754
        return;                                                               \
2755
    }                                                                         \
A
aurel32 已提交
2756
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2757
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2758 2759
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2760 2761
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2762 2763
}

2764 2765
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2766
{                                                                             \
A
aurel32 已提交
2767 2768 2769 2770 2771
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2772
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2773 2774
}

2775 2776 2777 2778 2779
#define GEN_LDS(name, ldop, op, type)                                         \
GEN_LD(name, ldop, op | 0x20, type);                                          \
GEN_LDU(name, ldop, op | 0x21, type);                                         \
GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
GEN_LDX(name, ldop, 0x17, op | 0x00, type)
B
bellard 已提交
2780 2781

/* lbz lbzu lbzux lbzx */
2782
GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
B
bellard 已提交
2783
/* lha lhau lhaux lhax */
2784
GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
B
bellard 已提交
2785
/* lhz lhzu lhzux lhzx */
2786
GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
B
bellard 已提交
2787
/* lwz lwzu lwzux lwzx */
2788
GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2789 2790
#if defined(TARGET_PPC64)
/* lwaux */
2791
GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2792
/* lwax */
2793
GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2794
/* ldux */
2795
GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2796
/* ldx */
2797
GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2798 2799
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
A
aurel32 已提交
2800
    TCGv EA;
2801 2802 2803
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
A
aurel32 已提交
2804
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2805 2806 2807
            return;
        }
    }
A
aurel32 已提交
2808
    gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2809
    EA = tcg_temp_new();
A
aurel32 已提交
2810
    gen_addr_imm_index(ctx, EA, 0x03);
2811 2812
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
A
aurel32 已提交
2813
        gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2814 2815
    } else {
        /* ld - ldu */
A
aurel32 已提交
2816
        gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2817 2818
    }
    if (Rc(ctx->opcode))
A
aurel32 已提交
2819 2820
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
    tcg_temp_free(EA);
2821
}
2822 2823 2824 2825
/* lq */
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
2826
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2827 2828
#else
    int ra, rd;
A
aurel32 已提交
2829
    TCGv EA;
2830 2831

    /* Restore CPU state */
A
aurel32 已提交
2832
    if (unlikely(ctx->mem_idx == 0)) {
A
aurel32 已提交
2833
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2834 2835 2836 2837 2838
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
A
aurel32 已提交
2839
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2840 2841
        return;
    }
A
aurel32 已提交
2842
    if (unlikely(ctx->le_mode)) {
2843
        /* Little-endian mode is not handled */
A
aurel32 已提交
2844
        gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2845 2846
        return;
    }
A
aurel32 已提交
2847
    gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2848
    EA = tcg_temp_new();
A
aurel32 已提交
2849 2850 2851 2852
    gen_addr_imm_index(ctx, EA, 0x0F);
    gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
    gen_addr_add(ctx, EA, EA, 8);
    gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
A
aurel32 已提交
2853
    tcg_temp_free(EA);
2854 2855
#endif
}
2856
#endif
B
bellard 已提交
2857 2858

/***                              Integer store                            ***/
2859 2860
#define GEN_ST(name, stop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2861
{                                                                             \
A
aurel32 已提交
2862 2863 2864 2865 2866
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2867
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2868 2869
}

2870 2871
#define GEN_STU(name, stop, opc, type)                                        \
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2872
{                                                                             \
A
aurel32 已提交
2873
    TCGv EA;                                                                  \
2874
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2875
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2876
        return;                                                               \
2877
    }                                                                         \
A
aurel32 已提交
2878
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2879
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2880
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2881
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2882
    else                                                                      \
A
aurel32 已提交
2883 2884
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2885 2886
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2887 2888
}

2889 2890
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2891
{                                                                             \
A
aurel32 已提交
2892
    TCGv EA;                                                                  \
2893
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2894
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2895
        return;                                                               \
2896
    }                                                                         \
A
aurel32 已提交
2897
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2898
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2899 2900
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2901 2902
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2903 2904
}

2905 2906
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2907
{                                                                             \
A
aurel32 已提交
2908 2909 2910 2911 2912
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2913
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2914 2915
}

2916 2917 2918 2919 2920
#define GEN_STS(name, stop, op, type)                                         \
GEN_ST(name, stop, op | 0x20, type);                                          \
GEN_STU(name, stop, op | 0x21, type);                                         \
GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
GEN_STX(name, stop, 0x17, op | 0x00, type)
B
bellard 已提交
2921 2922

/* stb stbu stbux stbx */
2923
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
B
bellard 已提交
2924
/* sth sthu sthux sthx */
2925
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
B
bellard 已提交
2926
/* stw stwu stwux stwx */
2927
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2928
#if defined(TARGET_PPC64)
2929 2930
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2931
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2932
{
2933
    int rs;
A
aurel32 已提交
2934
    TCGv EA;
2935 2936 2937 2938

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
2939
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2940 2941
#else
        /* stq */
A
aurel32 已提交
2942
        if (unlikely(ctx->mem_idx == 0)) {
A
aurel32 已提交
2943
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2944 2945 2946
            return;
        }
        if (unlikely(rs & 1)) {
A
aurel32 已提交
2947
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2948 2949
            return;
        }
A
aurel32 已提交
2950
        if (unlikely(ctx->le_mode)) {
2951
            /* Little-endian mode is not handled */
A
aurel32 已提交
2952
            gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2953 2954
            return;
        }
A
aurel32 已提交
2955
        gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2956
        EA = tcg_temp_new();
A
aurel32 已提交
2957 2958 2959 2960
        gen_addr_imm_index(ctx, EA, 0x03);
        gen_qemu_st64(ctx, cpu_gpr[rs], EA);
        gen_addr_add(ctx, EA, EA, 8);
        gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
A
aurel32 已提交
2961
        tcg_temp_free(EA);
2962 2963 2964 2965 2966
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
A
aurel32 已提交
2967
                gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2968 2969 2970
                return;
            }
        }
A
aurel32 已提交
2971
        gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2972
        EA = tcg_temp_new();
A
aurel32 已提交
2973 2974
        gen_addr_imm_index(ctx, EA, 0x03);
        gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2975
        if (Rc(ctx->opcode))
A
aurel32 已提交
2976 2977
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
        tcg_temp_free(EA);
2978 2979 2980
    }
}
#endif
B
bellard 已提交
2981 2982
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
A
aurel32 已提交
2983
static void always_inline gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2984
{
A
aurel32 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
    tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_temp_free_i32(t0);
#else
        tcg_gen_bswap16_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2997
}
2998
GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
A
aurel32 已提交
2999

B
bellard 已提交
3000
/* lwbrx */
A
aurel32 已提交
3001
static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3002
{
A
aurel32 已提交
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_bswap_i32(t0, t0);
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_temp_free_i32(t0);
#else
        tcg_gen_bswap_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
3015
}
3016
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
A
aurel32 已提交
3017

B
bellard 已提交
3018
/* sthbrx */
A
aurel32 已提交
3019
static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3020
{
A
aurel32 已提交
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0;
        TCGv t1;
        t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_ext16u_i32(t0, t0);
        tcg_gen_bswap16_i32(t0, t0);
        t1 = tcg_temp_new();
        tcg_gen_extu_i32_tl(t1, t0);
        tcg_temp_free_i32(t0);
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext16u_tl(t0, arg1);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
3044
}
3045
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
A
aurel32 已提交
3046

B
bellard 已提交
3047
/* stwbrx */
A
aurel32 已提交
3048
static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3049
{
A
aurel32 已提交
3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0;
        TCGv t1;
        t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_bswap_i32(t0, t0);
        t1 = tcg_temp_new();
        tcg_gen_extu_i32_tl(t1, t0);
        tcg_temp_free_i32(t0);
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new_i32();
        tcg_gen_bswap_i32(t0, arg1);
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
3071
}
3072
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
3073 3074 3075 3076 3077

/***                    Integer load and store multiple                    ***/
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
3078 3079 3080
    TCGv t0;
    TCGv_i32 t1;
    gen_set_access_type(ctx, ACCESS_INT);
3081
    /* NIP cannot be restored if the memory exception comes from an helper */
3082
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3083 3084 3085
    t0 = tcg_temp_new();
    t1 = tcg_const_i32(rD(ctx->opcode));
    gen_addr_imm_index(ctx, t0, 0);
3086 3087 3088
    gen_helper_lmw(t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3089 3090 3091 3092 3093
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
3094 3095 3096
    TCGv t0;
    TCGv_i32 t1;
    gen_set_access_type(ctx, ACCESS_INT);
3097
    /* NIP cannot be restored if the memory exception comes from an helper */
3098
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3099 3100 3101
    t0 = tcg_temp_new();
    t1 = tcg_const_i32(rS(ctx->opcode));
    gen_addr_imm_index(ctx, t0, 0);
3102 3103 3104
    gen_helper_stmw(t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3105 3106 3107 3108
}

/***                    Integer load and store strings                     ***/
/* lswi */
3109
/* PowerPC32 specification says we must generate an exception if
3110 3111 3112 3113
 * rA is in the range of registers to be loaded.
 * In an other hand, IBM says this is valid, but rA won't be loaded.
 * For now, I'll follow the spec...
 */
3114
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
B
bellard 已提交
3115
{
3116 3117
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3118 3119
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
3120
    int ra = rA(ctx->opcode);
B
bellard 已提交
3121 3122 3123 3124 3125
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
3126 3127 3128
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
A
aurel32 已提交
3129
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3130
        return;
B
bellard 已提交
3131
    }
A
aurel32 已提交
3132
    gen_set_access_type(ctx, ACCESS_INT);
3133
    /* NIP cannot be restored if the memory exception comes from an helper */
3134
    gen_update_nip(ctx, ctx->nip - 4);
3135
    t0 = tcg_temp_new();
A
aurel32 已提交
3136
    gen_addr_register(ctx, t0);
3137 3138 3139 3140 3141 3142
    t1 = tcg_const_i32(nb);
    t2 = tcg_const_i32(start);
    gen_helper_lsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3143 3144 3145
}

/* lswx */
3146
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
B
bellard 已提交
3147
{
A
aurel32 已提交
3148 3149 3150
    TCGv t0;
    TCGv_i32 t1, t2, t3;
    gen_set_access_type(ctx, ACCESS_INT);
3151
    /* NIP cannot be restored if the memory exception comes from an helper */
3152
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3153 3154 3155 3156 3157
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    t1 = tcg_const_i32(rD(ctx->opcode));
    t2 = tcg_const_i32(rA(ctx->opcode));
    t3 = tcg_const_i32(rB(ctx->opcode));
3158 3159 3160 3161 3162
    gen_helper_lswx(t0, t1, t2, t3);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    tcg_temp_free_i32(t3);
B
bellard 已提交
3163 3164 3165
}

/* stswi */
3166
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
B
bellard 已提交
3167
{
A
aurel32 已提交
3168 3169
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3170
    int nb = NB(ctx->opcode);
A
aurel32 已提交
3171
    gen_set_access_type(ctx, ACCESS_INT);
3172
    /* NIP cannot be restored if the memory exception comes from an helper */
3173
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3174 3175
    t0 = tcg_temp_new();
    gen_addr_register(ctx, t0);
B
bellard 已提交
3176 3177
    if (nb == 0)
        nb = 32;
3178
    t1 = tcg_const_i32(nb);
A
aurel32 已提交
3179
    t2 = tcg_const_i32(rS(ctx->opcode));
3180 3181 3182 3183
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3184 3185 3186
}

/* stswx */
3187
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
B
bellard 已提交
3188
{
A
aurel32 已提交
3189 3190 3191
    TCGv t0;
    TCGv_i32 t1, t2;
    gen_set_access_type(ctx, ACCESS_INT);
3192
    /* NIP cannot be restored if the memory exception comes from an helper */
3193
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3194 3195 3196
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    t1 = tcg_temp_new_i32();
3197 3198
    tcg_gen_trunc_tl_i32(t1, cpu_xer);
    tcg_gen_andi_i32(t1, t1, 0x7F);
A
aurel32 已提交
3199
    t2 = tcg_const_i32(rS(ctx->opcode));
3200 3201 3202 3203
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3204 3205 3206 3207
}

/***                        Memory synchronisation                         ***/
/* eieio */
3208
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
3209 3210 3211 3212
{
}

/* isync */
3213
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
3214
{
A
aurel32 已提交
3215
    gen_stop_exception(ctx);
B
bellard 已提交
3216 3217
}

3218
/* lwarx */
3219
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
3220
{
A
aurel32 已提交
3221 3222 3223 3224
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3225
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
3226
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3227 3228
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
B
bellard 已提交
3229 3230 3231
}

/* stwcx. */
3232
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
3233
{
A
aurel32 已提交
3234 3235 3236 3237 3238
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3239 3240 3241 3242
    gen_check_align(ctx, t0, 0x03);
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
A
aurel32 已提交
3243
    l1 = gen_new_label();
3244 3245
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
A
aurel32 已提交
3246
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3247 3248 3249
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
B
bellard 已提交
3250 3251
}

J
j_mayer 已提交
3252 3253
#if defined(TARGET_PPC64)
/* ldarx */
3254
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
3255
{
A
aurel32 已提交
3256 3257 3258 3259
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3260
    gen_check_align(ctx, t0, 0x07);
A
aurel32 已提交
3261
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3262 3263
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
3264 3265 3266
}

/* stdcx. */
3267
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
3268
{
A
aurel32 已提交
3269 3270 3271 3272 3273
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3274 3275 3276 3277
    gen_check_align(ctx, t0, 0x07);
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
A
aurel32 已提交
3278
    l1 = gen_new_label();
3279 3280
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
A
aurel32 已提交
3281
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3282 3283 3284
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
J
j_mayer 已提交
3285 3286 3287
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
3288
/* sync */
3289
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
3290 3291 3292
{
}

3293 3294 3295
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
3296 3297 3298
    TCGv_i32 t0 = tcg_temp_new_i32();
    tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
    tcg_temp_free_i32(t0);
3299
    /* Stop translation, as the CPU is supposed to sleep from now */
A
aurel32 已提交
3300
    gen_exception_err(ctx, EXCP_HLT, 1);
3301 3302
}

B
bellard 已提交
3303
/***                         Floating-point load                           ***/
3304 3305
#define GEN_LDF(name, ldop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3306
{                                                                             \
3307
    TCGv EA;                                                                  \
3308
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3309
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3310 3311
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3312
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3313
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3314 3315
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3316
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3317 3318
}

3319 3320
#define GEN_LDUF(name, ldop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3321
{                                                                             \
3322
    TCGv EA;                                                                  \
3323
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3324
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3325 3326
        return;                                                               \
    }                                                                         \
3327
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3328
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3329
        return;                                                               \
3330
    }                                                                         \
A
aurel32 已提交
3331
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3332
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3333 3334
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3335 3336
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3337 3338
}

3339 3340
#define GEN_LDUXF(name, ldop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3341
{                                                                             \
3342
    TCGv EA;                                                                  \
3343
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3344
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3345 3346
        return;                                                               \
    }                                                                         \
3347
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3348
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3349
        return;                                                               \
3350
    }                                                                         \
A
aurel32 已提交
3351
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3352
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3353 3354
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3355 3356
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3357 3358
}

3359 3360
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3361
{                                                                             \
3362
    TCGv EA;                                                                  \
3363
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3364
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3365 3366
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3367
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3368
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3369 3370
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3371
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3372 3373
}

3374 3375 3376 3377 3378 3379
#define GEN_LDFS(name, ldop, op, type)                                        \
GEN_LDF(name, ldop, op | 0x20, type);                                         \
GEN_LDUF(name, ldop, op | 0x21, type);                                        \
GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
GEN_LDXF(name, ldop, 0x17, op | 0x00, type)

A
aurel32 已提交
3380
static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3381 3382 3383
{
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1 = tcg_temp_new_i32();
A
aurel32 已提交
3384
    gen_qemu_ld32u(ctx, t0, arg2);
3385 3386 3387 3388 3389
    tcg_gen_trunc_tl_i32(t1, t0);
    tcg_temp_free(t0);
    gen_helper_float32_to_float64(arg1, t1);
    tcg_temp_free_i32(t1);
}
B
bellard 已提交
3390

3391 3392 3393 3394
 /* lfd lfdu lfdux lfdx */
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
 /* lfs lfsu lfsux lfsx */
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
B
bellard 已提交
3395 3396

/***                         Floating-point store                          ***/
3397 3398
#define GEN_STF(name, stop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3399
{                                                                             \
3400
    TCGv EA;                                                                  \
3401
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3402
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3403 3404
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3405
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3406
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3407 3408
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3409
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3410 3411
}

3412 3413
#define GEN_STUF(name, stop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3414
{                                                                             \
3415
    TCGv EA;                                                                  \
3416
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3417
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3418 3419
        return;                                                               \
    }                                                                         \
3420
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3421
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3422
        return;                                                               \
3423
    }                                                                         \
A
aurel32 已提交
3424
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3425
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3426 3427
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3428 3429
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3430 3431
}

3432 3433
#define GEN_STUXF(name, stop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3434
{                                                                             \
3435
    TCGv EA;                                                                  \
3436
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3437
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3438 3439
        return;                                                               \
    }                                                                         \
3440
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3441
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3442
        return;                                                               \
3443
    }                                                                         \
A
aurel32 已提交
3444
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3445
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3446 3447
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3448 3449
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3450 3451
}

3452 3453
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3454
{                                                                             \
3455
    TCGv EA;                                                                  \
3456
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3457
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3458 3459
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3460
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3461
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3462 3463
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3464
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3465 3466
}

3467 3468 3469 3470 3471 3472
#define GEN_STFS(name, stop, op, type)                                        \
GEN_STF(name, stop, op | 0x20, type);                                         \
GEN_STUF(name, stop, op | 0x21, type);                                        \
GEN_STUXF(name, stop, op | 0x01, type);                                       \
GEN_STXF(name, stop, 0x17, op | 0x00, type)

A
aurel32 已提交
3473
static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3474 3475 3476 3477 3478 3479
{
    TCGv_i32 t0 = tcg_temp_new_i32();
    TCGv t1 = tcg_temp_new();
    gen_helper_float64_to_float32(t0, arg1);
    tcg_gen_extu_i32_tl(t1, t0);
    tcg_temp_free_i32(t0);
A
aurel32 已提交
3480
    gen_qemu_st32(ctx, t1, arg2);
3481 3482
    tcg_temp_free(t1);
}
B
bellard 已提交
3483 3484

/* stfd stfdu stfdux stfdx */
3485
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
B
bellard 已提交
3486
/* stfs stfsu stfsux stfsx */
3487
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3488 3489

/* Optional: */
A
aurel32 已提交
3490
static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3491 3492 3493
{
    TCGv t0 = tcg_temp_new();
    tcg_gen_trunc_i64_tl(t0, arg1),
A
aurel32 已提交
3494
    gen_qemu_st32(ctx, t0, arg2);
3495 3496
    tcg_temp_free(t0);
}
B
bellard 已提交
3497
/* stfiwx */
3498
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3499 3500

/***                                Branch                                 ***/
3501 3502
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
3503 3504 3505
{
    TranslationBlock *tb;
    tb = ctx->tb;
3506 3507 3508 3509
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        dest = (uint32_t) dest;
#endif
B
bellard 已提交
3510
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3511
        likely(!ctx->singlestep_enabled)) {
B
bellard 已提交
3512
        tcg_gen_goto_tb(n);
3513
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
B
bellard 已提交
3514
        tcg_gen_exit_tb((long)tb + n);
3515
    } else {
3516
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3517 3518
        if (unlikely(ctx->singlestep_enabled)) {
            if ((ctx->singlestep_enabled &
A
aurel32 已提交
3519
                (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3520 3521 3522
                ctx->exception == POWERPC_EXCP_BRANCH) {
                target_ulong tmp = ctx->nip;
                ctx->nip = dest;
A
aurel32 已提交
3523
                gen_exception(ctx, POWERPC_EXCP_TRACE);
3524 3525 3526
                ctx->nip = tmp;
            }
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
A
aurel32 已提交
3527
                gen_debug_exception(ctx);
3528 3529
            }
        }
B
bellard 已提交
3530
        tcg_gen_exit_tb(0);
3531
    }
B
bellard 已提交
3532 3533
}

3534
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3535 3536
{
#if defined(TARGET_PPC64)
3537 3538
    if (ctx->sf_mode == 0)
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3539 3540
    else
#endif
3541
        tcg_gen_movi_tl(cpu_lr, nip);
3542 3543
}

B
bellard 已提交
3544 3545 3546
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3547
    target_ulong li, target;
B
bellard 已提交
3548

3549
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3550
    /* sign extend LI */
3551
#if defined(TARGET_PPC64)
3552 3553 3554
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3555
#endif
3556
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3557
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3558
        target = ctx->nip + li - 4;
B
bellard 已提交
3559
    else
3560
        target = li;
3561 3562
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3563
    gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3564 3565
}

3566 3567 3568 3569
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3570
static always_inline void gen_bcond (DisasContext *ctx, int type)
3571 3572
{
    uint32_t bo = BO(ctx->opcode);
3573 3574
    int l1 = gen_new_label();
    TCGv target;
3575

3576
    ctx->exception = POWERPC_EXCP_BRANCH;
3577
    if (type == BCOND_LR || type == BCOND_CTR) {
P
pbrook 已提交
3578
        target = tcg_temp_local_new();
3579 3580 3581 3582
        if (type == BCOND_CTR)
            tcg_gen_mov_tl(target, cpu_ctr);
        else
            tcg_gen_mov_tl(target, cpu_lr);
3583
    }
3584 3585
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3586 3587 3588
    l1 = gen_new_label();
    if ((bo & 0x4) == 0) {
        /* Decrement and test CTR */
P
pbrook 已提交
3589
        TCGv temp = tcg_temp_new();
3590
        if (unlikely(type == BCOND_CTR)) {
A
aurel32 已提交
3591
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3592 3593 3594
            return;
        }
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3595
#if defined(TARGET_PPC64)
3596 3597 3598
        if (!ctx->sf_mode)
            tcg_gen_ext32u_tl(temp, cpu_ctr);
        else
3599
#endif
3600 3601 3602 3603 3604
            tcg_gen_mov_tl(temp, cpu_ctr);
        if (bo & 0x2) {
            tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
        } else {
            tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3605
        }
P
pbrook 已提交
3606
        tcg_temp_free(temp);
3607 3608 3609 3610 3611
    }
    if ((bo & 0x10) == 0) {
        /* Test CR */
        uint32_t bi = BI(ctx->opcode);
        uint32_t mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
3612
        TCGv_i32 temp = tcg_temp_new_i32();
3613

3614
        if (bo & 0x8) {
3615 3616
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3617
        } else {
3618 3619
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3620
        }
P
pbrook 已提交
3621
        tcg_temp_free_i32(temp);
3622
    }
3623
    if (type == BCOND_IM) {
3624 3625 3626 3627 3628 3629
        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
            gen_goto_tb(ctx, 0, ctx->nip + li - 4);
        } else {
            gen_goto_tb(ctx, 0, li);
        }
B
bellard 已提交
3630
        gen_set_label(l1);
3631
        gen_goto_tb(ctx, 1, ctx->nip);
3632
    } else {
3633
#if defined(TARGET_PPC64)
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
        if (!(ctx->sf_mode))
            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
        else
#endif
            tcg_gen_andi_tl(cpu_nip, target, ~3);
        tcg_gen_exit_tb(0);
        gen_set_label(l1);
#if defined(TARGET_PPC64)
        if (!(ctx->sf_mode))
            tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3644 3645
        else
#endif
3646
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
B
bellard 已提交
3647
        tcg_gen_exit_tb(0);
J
j_mayer 已提交
3648
    }
3649 3650 3651
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3652
{
3653 3654 3655 3656
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3657
{
3658 3659 3660 3661
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3662
{
3663 3664
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3665 3666

/***                      Condition register logical                       ***/
3667 3668
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
B
bellard 已提交
3669
{                                                                             \
3670 3671
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
P
pbrook 已提交
3672
    TCGv_i32 t0, t1;                                                          \
3673
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
P
pbrook 已提交
3674
    t0 = tcg_temp_new_i32();                                                  \
3675
    if (sh > 0)                                                               \
3676
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3677
    else if (sh < 0)                                                          \
3678
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3679
    else                                                                      \
3680
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
P
pbrook 已提交
3681
    t1 = tcg_temp_new_i32();                                                  \
3682 3683
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
3684
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3685
    else if (sh < 0)                                                          \
3686
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3687
    else                                                                      \
3688 3689
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
    tcg_op(t0, t0, t1);                                                       \
3690
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3691 3692 3693
    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
P
pbrook 已提交
3694 3695
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
B
bellard 已提交
3696 3697 3698
}

/* crand */
3699
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
B
bellard 已提交
3700
/* crandc */
3701
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
B
bellard 已提交
3702
/* creqv */
3703
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
B
bellard 已提交
3704
/* crnand */
3705
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
B
bellard 已提交
3706
/* crnor */
3707
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
B
bellard 已提交
3708
/* cror */
3709
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
B
bellard 已提交
3710
/* crorc */
3711
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
B
bellard 已提交
3712
/* crxor */
3713
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
B
bellard 已提交
3714 3715 3716
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
A
aurel32 已提交
3717
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
B
bellard 已提交
3718 3719 3720
}

/***                           System linkage                              ***/
A
aurel32 已提交
3721
/* rfi (mem_idx only) */
3722
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3723
{
3724
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3725
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3726 3727
#else
    /* Restore CPU state */
A
aurel32 已提交
3728
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3729
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3730
        return;
3731
    }
3732
    gen_helper_rfi();
A
aurel32 已提交
3733
    gen_sync_exception(ctx);
3734
#endif
B
bellard 已提交
3735 3736
}

J
j_mayer 已提交
3737
#if defined(TARGET_PPC64)
3738
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3739 3740
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3741
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3742 3743
#else
    /* Restore CPU state */
A
aurel32 已提交
3744
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3745
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3746 3747
        return;
    }
3748
    gen_helper_rfid();
A
aurel32 已提交
3749
    gen_sync_exception(ctx);
J
j_mayer 已提交
3750 3751 3752
#endif
}

J
j_mayer 已提交
3753
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3754 3755
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3756
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3757 3758
#else
    /* Restore CPU state */
A
aurel32 已提交
3759
    if (unlikely(ctx->mem_idx <= 1)) {
A
aurel32 已提交
3760
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3761 3762
        return;
    }
3763
    gen_helper_hrfid();
A
aurel32 已提交
3764
    gen_sync_exception(ctx);
3765 3766 3767 3768
#endif
}
#endif

B
bellard 已提交
3769
/* sc */
3770 3771 3772 3773 3774
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3775
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3776
{
3777 3778 3779
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
A
aurel32 已提交
3780
    gen_exception_err(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3781 3782 3783 3784
}

/***                                Trap                                   ***/
/* tw */
3785
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3786
{
3787
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3788
    /* Update the nip since this might generate a trap exception */
3789
    gen_update_nip(ctx, ctx->nip);
3790 3791
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
B
bellard 已提交
3792 3793 3794 3795 3796
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3797 3798
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3799 3800
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3801 3802 3803
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3804 3805
}

3806 3807 3808 3809
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
3810
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3811 3812
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3813 3814
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
3815 3816 3817 3818 3819
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
3820 3821
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3822 3823
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3824 3825 3826
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
3827 3828 3829
}
#endif

B
bellard 已提交
3830 3831 3832 3833
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
A
aurel32 已提交
3834 3835
    tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3836
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
B
bellard 已提交
3837 3838 3839
}

/* mfcr */
3840
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3841
{
3842
    uint32_t crm, crn;
3843

3844 3845
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
M
malc 已提交
3846 3847
        if (likely(crm && ((crm & (crm - 1)) == 0))) {
            crn = ffs (crm) - 1;
3848
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
M
malc 已提交
3849 3850
            tcg_gen_shli_i32(cpu_gpr[rD(ctx->opcode)],
                             cpu_gpr[rD(ctx->opcode)], crn * 4);
3851
        }
3852
    } else {
P
pbrook 已提交
3853
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3854
    }
B
bellard 已提交
3855 3856 3857 3858 3859
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3860
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3861
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3862
#else
A
aurel32 已提交
3863
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3864
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3865
        return;
3866
    }
3867
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3868
#endif
B
bellard 已提交
3869 3870
}

J
j_mayer 已提交
3871
#if 1
3872
#define SPR_NOACCESS ((void *)(-1UL))
3873 3874 3875 3876 3877 3878 3879 3880 3881
#else
static void spr_noaccess (void *opaque, int sprn)
{
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
    printf("ERROR: try to access SPR %d !\n", sprn);
}
#define SPR_NOACCESS (&spr_noaccess)
#endif

B
bellard 已提交
3882
/* mfspr */
3883
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3884
{
A
aurel32 已提交
3885
    void (*read_cb)(void *opaque, int gprn, int sprn);
B
bellard 已提交
3886 3887
    uint32_t sprn = SPR(ctx->opcode);

3888
#if !defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3889
    if (ctx->mem_idx == 2)
3890
        read_cb = ctx->spr_cb[sprn].hea_read;
A
aurel32 已提交
3891
    else if (ctx->mem_idx)
3892 3893
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3894
#endif
3895
        read_cb = ctx->spr_cb[sprn].uea_read;
3896 3897
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
A
aurel32 已提交
3898
            (*read_cb)(ctx, rD(ctx->opcode), sprn);
3899 3900
        } else {
            /* Privilege exception */
3901 3902 3903 3904 3905
            /* This is a hack to avoid warnings when running Linux:
             * this OS breaks the PowerPC virtualisation model,
             * allowing userland application to read the PVR
             */
            if (sprn != SPR_PVR) {
3906
                qemu_log("Trying to read privileged spr %d %03x at "
J
j_mayer 已提交
3907 3908 3909
                            ADDRX "\n", sprn, sprn, ctx->nip);
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
                       sprn, sprn, ctx->nip);
3910
            }
A
aurel32 已提交
3911
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
B
bellard 已提交
3912
        }
3913 3914
    } else {
        /* Not defined */
3915
        qemu_log("Trying to read invalid spr %d %03x at "
J
j_mayer 已提交
3916 3917 3918
                    ADDRX "\n", sprn, sprn, ctx->nip);
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
A
aurel32 已提交
3919
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3920 3921 3922
    }
}

3923
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3924
{
3925
    gen_op_mfspr(ctx);
3926
}
3927 3928

/* mftb */
3929
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3930 3931
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3932 3933 3934
}

/* mtcrf */
3935
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3936
{
3937
    uint32_t crm, crn;
3938

3939
    crm = CRM(ctx->opcode);
M
malc 已提交
3940 3941 3942 3943 3944 3945 3946 3947 3948
    if (likely((ctx->opcode & 0x00100000))) {
        if (crm && ((crm & (crm - 1)) == 0)) {
            TCGv_i32 temp = tcg_temp_new_i32();
            crn = ffs (crm) - 1;
            tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
            tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
            tcg_temp_free_i32(temp);
        }
3949
    } else {
P
pbrook 已提交
3950 3951 3952
        TCGv_i32 temp = tcg_const_i32(crm);
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
        tcg_temp_free_i32(temp);
3953
    }
B
bellard 已提交
3954 3955 3956
}

/* mtmsr */
J
j_mayer 已提交
3957
#if defined(TARGET_PPC64)
3958
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3959 3960
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3961
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3962
#else
A
aurel32 已提交
3963
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3964
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3965 3966
        return;
    }
3967 3968
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
3969 3970 3971 3972 3973
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
        tcg_temp_free(t0);
3974
    } else {
3975 3976 3977 3978
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3979
        gen_update_nip(ctx, ctx->nip);
3980
        gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3981 3982
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
3983
        gen_stop_exception(ctx);
3984
    }
J
j_mayer 已提交
3985 3986 3987 3988
#endif
}
#endif

B
bellard 已提交
3989 3990
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3991
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3992
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3993
#else
A
aurel32 已提交
3994
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3995
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3996
        return;
3997
    }
3998 3999
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
4000 4001 4002 4003 4004
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
        tcg_temp_free(t0);
4005
    } else {
4006 4007 4008 4009
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
4010
        gen_update_nip(ctx, ctx->nip);
4011
#if defined(TARGET_PPC64)
4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
        if (!ctx->sf_mode) {
            TCGv t0 = tcg_temp_new();
            TCGv t1 = tcg_temp_new();
            tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
            tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_temp_free(t1);
            gen_helper_store_msr(t0);
            tcg_temp_free(t0);
        } else
4022
#endif
4023
            gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
4024
        /* Must stop the translation as machine state (may have) changed */
4025
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
4026
        gen_stop_exception(ctx);
4027
    }
4028
#endif
B
bellard 已提交
4029 4030 4031 4032 4033
}

/* mtspr */
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
{
A
aurel32 已提交
4034
    void (*write_cb)(void *opaque, int sprn, int gprn);
B
bellard 已提交
4035 4036
    uint32_t sprn = SPR(ctx->opcode);

4037
#if !defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4038
    if (ctx->mem_idx == 2)
4039
        write_cb = ctx->spr_cb[sprn].hea_write;
A
aurel32 已提交
4040
    else if (ctx->mem_idx)
4041 4042
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
4043
#endif
4044
        write_cb = ctx->spr_cb[sprn].uea_write;
4045 4046
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
A
aurel32 已提交
4047
            (*write_cb)(ctx, sprn, rS(ctx->opcode));
4048 4049
        } else {
            /* Privilege exception */
4050
            qemu_log("Trying to write privileged spr %d %03x at "
J
j_mayer 已提交
4051 4052 4053
                        ADDRX "\n", sprn, sprn, ctx->nip);
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
                   sprn, sprn, ctx->nip);
A
aurel32 已提交
4054
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4055
        }
4056 4057
    } else {
        /* Not defined */
4058
        qemu_log("Trying to write invalid spr %d %03x at "
J
j_mayer 已提交
4059 4060 4061
                    ADDRX "\n", sprn, sprn, ctx->nip);
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
A
aurel32 已提交
4062
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
4063 4064 4065 4066 4067
    }
}

/***                         Cache management                              ***/
/* dcbf */
4068
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
4069
{
J
j_mayer 已提交
4070
    /* XXX: specification says this is treated as a load by the MMU */
A
aurel32 已提交
4071 4072 4073 4074 4075
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld8u(ctx, t0, t0);
4076
    tcg_temp_free(t0);
B
bellard 已提交
4077 4078 4079
}

/* dcbi (Supervisor only) */
4080
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4081
{
4082
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4083
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4084
#else
A
aurel32 已提交
4085
    TCGv EA, val;
A
aurel32 已提交
4086
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4087
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4088
        return;
4089
    }
P
pbrook 已提交
4090
    EA = tcg_temp_new();
A
aurel32 已提交
4091 4092
    gen_set_access_type(ctx, ACCESS_CACHE);
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
4093
    val = tcg_temp_new();
4094
    /* XXX: specification says this should be treated as a store by the MMU */
A
aurel32 已提交
4095 4096
    gen_qemu_ld8u(ctx, val, EA);
    gen_qemu_st8(ctx, val, EA);
A
aurel32 已提交
4097 4098
    tcg_temp_free(val);
    tcg_temp_free(EA);
4099
#endif
B
bellard 已提交
4100 4101 4102
}

/* dcdst */
4103
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4104
{
4105
    /* XXX: specification say this is treated as a load by the MMU */
A
aurel32 已提交
4106 4107 4108 4109 4110
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld8u(ctx, t0, t0);
4111
    tcg_temp_free(t0);
B
bellard 已提交
4112 4113 4114
}

/* dcbt */
4115
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
4116
{
4117
    /* interpreted as no-op */
4118 4119 4120
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4121 4122 4123
}

/* dcbtst */
4124
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
4125
{
4126
    /* interpreted as no-op */
4127 4128 4129
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4130 4131 4132
}

/* dcbz */
4133
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
4134
{
A
aurel32 已提交
4135 4136
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4137 4138
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4139 4140
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4141 4142
    gen_helper_dcbz(t0);
    tcg_temp_free(t0);
4143 4144
}

4145
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4146
{
A
aurel32 已提交
4147 4148
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4149 4150
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4151 4152
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4153
    if (ctx->opcode & 0x00200000)
4154
        gen_helper_dcbz(t0);
4155
    else
4156 4157
        gen_helper_dcbz_970(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4158 4159
}

4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
/* dst / dstt */
GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC)
{
    if (rA(ctx->opcode) == 0) {
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
    } else {
        /* interpreted as no-op */
    }
}

/* dstst /dststt */
GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC)
{
    if (rA(ctx->opcode) == 0) {
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
    } else {
        /* interpreted as no-op */
    }

}

/* dss / dssall */
GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC)
{
    /* interpreted as no-op */
}

B
bellard 已提交
4187
/* icbi */
4188
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
4189
{
A
aurel32 已提交
4190 4191
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4192 4193
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4194 4195
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4196 4197
    gen_helper_icbi(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4198 4199 4200 4201
}

/* Optional: */
/* dcba */
4202
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
4203
{
4204 4205 4206 4207
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4208 4209 4210 4211 4212 4213 4214
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
4215
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4216
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4217
#else
4218
    TCGv t0;
A
aurel32 已提交
4219
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4220
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4221
        return;
4222
    }
4223 4224 4225
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4226
#endif
B
bellard 已提交
4227 4228 4229
}

/* mfsrin */
4230
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4231
{
4232
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4233
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4234
#else
4235
    TCGv t0;
A
aurel32 已提交
4236
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4237
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4238
        return;
4239
    }
4240 4241 4242 4243 4244
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4245
#endif
B
bellard 已提交
4246 4247 4248
}

/* mtsr */
B
bellard 已提交
4249
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
4250
{
4251
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4252
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4253
#else
4254
    TCGv t0;
A
aurel32 已提交
4255
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4256
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4257
        return;
4258
    }
4259 4260 4261
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4262
#endif
B
bellard 已提交
4263 4264 4265
}

/* mtsrin */
4266
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4267
{
4268
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4269
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4270
#else
4271
    TCGv t0;
A
aurel32 已提交
4272
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4273
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4274
        return;
4275
    }
4276 4277 4278 4279 4280
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_store_sr(t0, cpu_gpr[rD(ctx->opcode)]);
    tcg_temp_free(t0);
4281
#endif
B
bellard 已提交
4282 4283
}

4284 4285 4286
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
4287
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4288 4289
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4290
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4291
#else
4292
    TCGv t0;
A
aurel32 已提交
4293
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4294
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4295 4296
        return;
    }
4297 4298 4299
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4300 4301 4302 4303
#endif
}

/* mfsrin */
4304 4305
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
4306 4307
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4308
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4309
#else
4310
    TCGv t0;
A
aurel32 已提交
4311
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4312
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4313 4314
        return;
    }
4315 4316 4317 4318 4319
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4320 4321 4322 4323
#endif
}

/* mtsr */
4324
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4325 4326
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4327
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4328
#else
4329
    TCGv t0;
A
aurel32 已提交
4330
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4331
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4332 4333
        return;
    }
4334 4335 4336
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4337 4338 4339 4340
#endif
}

/* mtsrin */
4341 4342
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
4343 4344
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4345
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4346
#else
4347
    TCGv t0;
A
aurel32 已提交
4348
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4349
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4350 4351
        return;
    }
4352 4353 4354 4355 4356
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4357 4358 4359 4360
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4361
/***                      Lookaside buffer management                      ***/
A
aurel32 已提交
4362
/* Optional & mem_idx only: */
B
bellard 已提交
4363
/* tlbia */
4364
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4365
{
4366
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4367
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4368
#else
A
aurel32 已提交
4369
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4370
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4371
        return;
4372
    }
4373
    gen_helper_tlbia();
4374
#endif
B
bellard 已提交
4375 4376 4377
}

/* tlbie */
4378
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4379
{
4380
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4381
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4382
#else
A
aurel32 已提交
4383
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4384
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4385
        return;
4386
    }
4387
#if defined(TARGET_PPC64)
4388 4389 4390 4391 4392 4393
    if (!ctx->sf_mode) {
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
        gen_helper_tlbie(t0);
        tcg_temp_free(t0);
    } else
4394
#endif
4395
        gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4396
#endif
B
bellard 已提交
4397 4398 4399
}

/* tlbsync */
4400
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4401
{
4402
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4403
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4404
#else
A
aurel32 已提交
4405
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4406
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4407
        return;
4408 4409 4410 4411
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
A
aurel32 已提交
4412
    gen_stop_exception(ctx);
4413
#endif
B
bellard 已提交
4414 4415
}

J
j_mayer 已提交
4416 4417 4418 4419 4420
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4421
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4422
#else
A
aurel32 已提交
4423
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4424
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4425 4426
        return;
    }
4427
    gen_helper_slbia();
J
j_mayer 已提交
4428 4429 4430 4431 4432 4433 4434
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4435
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4436
#else
A
aurel32 已提交
4437
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4438
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4439 4440
        return;
    }
4441
    gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
4442 4443 4444 4445
#endif
}
#endif

B
bellard 已提交
4446 4447
/***                              External control                         ***/
/* Optional: */
4448
/* eciwx */
B
bellard 已提交
4449 4450
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4451
    TCGv t0;
4452
    /* Should check EAR[E] ! */
A
aurel32 已提交
4453 4454 4455
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4456
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4457
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4458
    tcg_temp_free(t0);
4459 4460 4461 4462 4463
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4464
    TCGv t0;
4465
    /* Should check EAR[E] ! */
A
aurel32 已提交
4466 4467 4468
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4469
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4470
    gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4471
    tcg_temp_free(t0);
4472 4473 4474 4475 4476 4477
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
4478 4479 4480 4481 4482 4483 4484 4485
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
4486
    if (unlikely(Rc(ctx->opcode) != 0))
4487
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4488 4489 4490 4491 4492
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    int l3 = gen_new_label();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l3);
    gen_set_label(l2);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l3);
4508
    if (unlikely(Rc(ctx->opcode) != 0))
4509
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4510 4511 4512
}

/* clcs */
4513
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4514
{
4515 4516 4517
    TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
    gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
4518
    /* Rc=1 sets CR0 to an undefined state */
4519 4520 4521 4522 4523
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
4524
    gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4525
    if (unlikely(Rc(ctx->opcode) != 0))
4526
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4527 4528 4529 4530 4531
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
4532
    gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4533
    if (unlikely(Rc(ctx->opcode) != 0))
4534
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4535 4536 4537 4538 4539
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
4540
    gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4541
    if (unlikely(Rc(ctx->opcode) != 0))
4542
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4543 4544 4545 4546 4547
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
4548
    gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4549
    if (unlikely(Rc(ctx->opcode) != 0))
4550
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4551 4552 4553 4554 4555
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
4556 4557 4558 4559 4560 4561 4562 4563
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
    tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
4564
    if (unlikely(Rc(ctx->opcode) != 0))
4565
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4566 4567 4568 4569 4570
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
    tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4593
    if (unlikely(Rc(ctx->opcode) != 0))
4594
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4595 4596 4597 4598 4599
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610
    target_long simm = SIMM(ctx->opcode);
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
    tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4611 4612 4613 4614 4615
}

/* lscbx - lscbx. */
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
{
4616 4617 4618 4619
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
    TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
    TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4620

A
aurel32 已提交
4621
    gen_addr_reg_index(ctx, t0);
4622
    /* NIP cannot be restored if the memory exception comes from an helper */
4623
    gen_update_nip(ctx, ctx->nip - 4);
4624 4625 4626 4627
    gen_helper_lscbx(t0, t0, t1, t2, t3);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    tcg_temp_free_i32(t3);
A
aurel32 已提交
4628
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4629
    tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4630
    if (unlikely(Rc(ctx->opcode) != 0))
4631 4632
        gen_set_Rc0(ctx, t0);
    tcg_temp_free(t0);
4633 4634 4635 4636 4637
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    TCGv t3 = tcg_temp_new();
    tcg_gen_movi_tl(t3, 0xFFFFFFFF);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
    tcg_gen_addi_tl(t2, t0, 1);
    tcg_gen_shr_tl(t2, t3, t2);
    tcg_gen_shr_tl(t3, t3, t1);
    tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
    tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
    tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(t3);
4657
    if (unlikely(Rc(ctx->opcode) != 0))
4658
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4659 4660 4661 4662 4663
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
4664 4665 4666 4667 4668 4669 4670
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4671
    if (unlikely(Rc(ctx->opcode) != 0))
4672
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4673 4674 4675 4676 4677
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
    TCGv_i64 t0 = tcg_temp_new_i64();
    TCGv_i64 t1 = tcg_temp_new_i64();
    TCGv t2 = tcg_temp_new();
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_trunc_i64_tl(t2, t0);
    gen_store_spr(SPR_MQ, t2);
    tcg_gen_shri_i64(t1, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
    tcg_temp_free(t2);
4691
    if (unlikely(Rc(ctx->opcode) != 0))
4692
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4693 4694 4695 4696 4697
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717
    int l1 = gen_new_label();
    TCGv_i64 t0 = tcg_temp_new_i64();
    TCGv_i64 t1 = tcg_temp_new_i64();
    TCGv t2 = tcg_temp_new();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_trunc_i64_tl(t2, t0);
    gen_store_spr(SPR_MQ, t2);
    tcg_gen_shri_i64(t1, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
    tcg_gen_ext32s_i64(t1, t0);
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    gen_set_label(l1);
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
    tcg_temp_free(t2);
4718
    if (unlikely(Rc(ctx->opcode) != 0))
4719
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4720 4721 4722 4723 4724
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
4725 4726 4727 4728 4729 4730 4731 4732
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
4733
    if (unlikely(Rc(ctx->opcode) != 0))
4734
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4735 4736 4737 4738 4739
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
    /* nabs never overflows */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4750
    if (unlikely(Rc(ctx->opcode) != 0))
4751
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4752 4753 4754 4755 4756
}

/* rlmi - rlmi. */
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4757 4758 4759 4760 4761 4762 4763 4764 4765
    uint32_t mb = MB(ctx->opcode);
    uint32_t me = ME(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    tcg_gen_andi_tl(t0, t0, MASK(mb, me));
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
    tcg_temp_free(t0);
4766
    if (unlikely(Rc(ctx->opcode) != 0))
4767
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4768 4769 4770 4771 4772
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0x80000000);
    tcg_gen_shr_tl(t1, t1, t0);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    tcg_gen_and_tl(t0, t0, t1);
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4784
    if (unlikely(Rc(ctx->opcode) != 0))
4785
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4786 4787 4788 4789 4790
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4802
    if (unlikely(Rc(ctx->opcode) != 0))
4803
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4804 4805 4806 4807 4808
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t2, 0xFFFFFFFF);
    tcg_gen_shl_tl(t2, t2, t0);
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_and_tl(t0, t0, t2);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4824
    if (unlikely(Rc(ctx->opcode) != 0))
4825
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4826 4827 4828 4829 4830
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
4831 4832 4833 4834 4835 4836 4837 4838 4839 4840
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4841
    if (unlikely(Rc(ctx->opcode) != 0))
4842
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4843 4844 4845 4846 4847
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4859
    if (unlikely(Rc(ctx->opcode) != 0))
4860
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4861 4862 4863 4864 4865
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_local_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shl_tl(t1, t1, t2);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    gen_load_spr(t0, SPR_MQ);
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    gen_load_spr(t2, SPR_MQ);
    tcg_gen_andc_tl(t1, t2, t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4888
    if (unlikely(Rc(ctx->opcode) != 0))
4889
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4890 4891 4892 4893 4894
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    gen_store_spr(SPR_MQ, t1);
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4911
    if (unlikely(Rc(ctx->opcode) != 0))
4912
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4913 4914
}

4915
/* sraiq - sraiq. */
4916 4917
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933
    int sh = SH(ctx->opcode);
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t0, t0, t1);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
    gen_set_label(l1);
    tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4934
    if (unlikely(Rc(ctx->opcode) != 0))
4935
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4936 4937 4938 4939 4940
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_subfi_tl(t2, 32, t2);
    tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_or_tl(t0, t0, t2);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
    tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
    gen_set_label(l2);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4967
    if (unlikely(Rc(ctx->opcode) != 0))
4968
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4969 4970 4971 4972 4973
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4985
    if (unlikely(Rc(ctx->opcode) != 0))
4986
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4987 4988 4989 4990 4991
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
4992 4993 4994 4995 4996 4997 4998 4999
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5000
    if (unlikely(Rc(ctx->opcode) != 0))
5001
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5002 5003 5004 5005 5006
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shr_tl(t1, t1, t0);
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    gen_load_spr(t2, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_and_tl(t0, t0, t1);
    tcg_gen_andc_tl(t2, t2, t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
5022
    if (unlikely(Rc(ctx->opcode) != 0))
5023
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5024 5025 5026 5027 5028
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5039
    if (unlikely(Rc(ctx->opcode) != 0))
5040
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5041 5042 5043 5044 5045
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5057
    if (unlikely(Rc(ctx->opcode) != 0))
5058
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5059 5060 5061 5062 5063
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_local_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shr_tl(t2, t1, t2);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    gen_load_spr(t0, SPR_MQ);
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_and_tl(t0, t0, t2);
    gen_load_spr(t1, SPR_MQ);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
5087
    if (unlikely(Rc(ctx->opcode) != 0))
5088
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5089 5090 5091 5092 5093
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    gen_store_spr(SPR_MQ, t1);
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5110
    if (unlikely(Rc(ctx->opcode) != 0))
5111
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5112 5113 5114 5115 5116 5117 5118
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5119
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5120 5121 5122 5123 5124 5125
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5126
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5127 5128 5129 5130 5131 5132
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5133
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5134
#else
A
aurel32 已提交
5135
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5136
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5137 5138
        return;
    }
5139
    gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5140 5141 5142 5143 5144
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
5145
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5146 5147
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5148
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5149
#else
A
aurel32 已提交
5150
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5151
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5152 5153
        return;
    }
5154
    gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5155 5156 5157 5158
#endif
}

/* tlbli */
5159
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5160 5161
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5162
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5163
#else
A
aurel32 已提交
5164
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5165
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5166 5167
        return;
    }
5168
    gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5169 5170 5171
#endif
}

5172 5173
/* 74xx TLB management */
/* tlbld */
5174
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5175 5176
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5177
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5178
#else
A
aurel32 已提交
5179
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5180
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5181 5182
        return;
    }
5183
    gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5184 5185 5186 5187
#endif
}

/* tlbli */
5188
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5189 5190
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5191
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5192
#else
A
aurel32 已提交
5193
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5194
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5195 5196
        return;
    }
5197
    gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5198 5199 5200
#endif
}

5201 5202 5203 5204 5205 5206 5207 5208 5209 5210
/* POWER instructions not in PowerPC 601 */
/* clf */
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
{
    /* Cache line flush: implemented as no-op */
}

/* cli */
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
{
B
blueswir1 已提交
5211
    /* Cache line invalidate: privileged and treated as no-op */
5212
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5213
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5214
#else
A
aurel32 已提交
5215
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5216
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230
        return;
    }
#endif
}

/* dclst */
GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
{
    /* Data cache line store: treated as no-op */
}

GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5231
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5232
#else
5233 5234 5235
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);
    TCGv t0;
A
aurel32 已提交
5236
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5237
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5238 5239
        return;
    }
5240
    t0 = tcg_temp_new();
A
aurel32 已提交
5241
    gen_addr_reg_index(ctx, t0);
5242 5243 5244 5245
    tcg_gen_shri_tl(t0, t0, 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_sr(cpu_gpr[rd], t0);
    tcg_temp_free(t0);
5246
    if (ra != 0 && ra != rd)
5247
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5248 5249 5250 5251 5252 5253
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5254
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5255
#else
5256
    TCGv t0;
A
aurel32 已提交
5257
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5258
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5259 5260
        return;
    }
5261
    t0 = tcg_temp_new();
A
aurel32 已提交
5262
    gen_addr_reg_index(ctx, t0);
5263 5264
    gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
5265 5266 5267 5268 5269 5270
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5271
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5272
#else
A
aurel32 已提交
5273
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5274
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5275 5276
        return;
    }
5277
    gen_helper_rfsvc();
A
aurel32 已提交
5278
    gen_sync_exception(ctx);
5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
5290
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5291 5292 5293 5294 5295 5296 5297
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5298
    tcg_temp_free(t0);
5299 5300 5301 5302 5303 5304
}

/* lfqu */
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5305
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5306 5307 5308 5309 5310 5311 5312 5313
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    t1 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5314
    if (ra != 0)
5315 5316 5317
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5318 5319 5320 5321 5322 5323
}

/* lfqux */
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5324
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5325 5326 5327 5328 5329 5330 5331 5332 5333
    gen_set_access_type(ctx, ACCESS_FLOAT);
    TCGv t0, t1;
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5334
    if (ra != 0)
5335 5336
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5337 5338 5339 5340 5341
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
5342
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5343 5344 5345 5346 5347 5348 5349
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5350
    tcg_temp_free(t0);
5351 5352 5353 5354 5355
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
5356
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5357 5358 5359 5360 5361 5362 5363
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5364
    tcg_temp_free(t0);
5365 5366 5367 5368 5369 5370
}

/* stfqu */
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5371
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5372 5373 5374 5375 5376 5377 5378 5379 5380
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5381
    if (ra != 0)
5382 5383
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5384 5385 5386 5387 5388 5389
}

/* stfqux */
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5390
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5391 5392 5393 5394 5395 5396 5397 5398 5399
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5400
    if (ra != 0)
5401 5402
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5403 5404 5405 5406 5407
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
5408
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5409 5410 5411 5412 5413 5414 5415
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5416
    tcg_temp_free(t0);
5417 5418 5419
}

/* BookE specific instructions */
5420
/* XXX: not implemented on 440 ? */
5421
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5422 5423
{
    /* XXX: TODO */
A
aurel32 已提交
5424
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5425 5426
}

5427
/* XXX: not implemented on 440 ? */
5428
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5429 5430
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5431
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5432
#else
5433
    TCGv t0;
A
aurel32 已提交
5434
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5435
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5436 5437
        return;
    }
5438
    t0 = tcg_temp_new();
A
aurel32 已提交
5439
    gen_addr_reg_index(ctx, t0);
5440 5441
    gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
    tcg_temp_free(t0);
5442 5443 5444 5445
#endif
}

/* All 405 MAC instructions are translated here */
5446 5447 5448
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
5449
{
5450 5451
    TCGv t0, t1;

P
pbrook 已提交
5452 5453
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
5454

5455 5456 5457 5458 5459 5460 5461
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
5462 5463 5464
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16s_tl(t1, t1);
5465 5466 5467 5468 5469
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
5470 5471 5472
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16u_tl(t1, t1);
5473 5474 5475 5476 5477 5478 5479
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
5480 5481 5482 5483
        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
        tcg_gen_ext16s_tl(t0, t0);
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16s_tl(t1, t1);
5484 5485 5486 5487 5488
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
5489 5490 5491 5492
        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
        tcg_gen_ext16u_tl(t0, t0);
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16u_tl(t1, t1);
5493 5494 5495 5496 5497 5498 5499
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
5500 5501
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5502 5503 5504 5505 5506
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
5507 5508
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5509 5510 5511
        break;
    }
    if (opc2 & 0x04) {
5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535
        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
        tcg_gen_mul_tl(t1, t0, t1);
        if (opc2 & 0x02) {
            /* nmultiply-and-accumulate (0x0E) */
            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
        } else {
            /* multiply-and-accumulate (0x0C) */
            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
        }

        if (opc3 & 0x12) {
            /* Check overflow and/or saturate */
            int l1 = gen_new_label();

            if (opc3 & 0x10) {
                /* Start with XER OV disabled, the most likely case */
                tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
            }
            if (opc3 & 0x01) {
                /* Signed */
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
A
aurel32 已提交
5536
                if (opc3 & 0x02) {
5537 5538 5539 5540 5541 5542 5543
                    /* Saturate */
                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
                }
            } else {
                /* Unsigned */
                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
A
aurel32 已提交
5544
                if (opc3 & 0x02) {
5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557
                    /* Saturate */
                    tcg_gen_movi_tl(t0, UINT32_MAX);
                }
            }
            if (opc3 & 0x10) {
                /* Check overflow */
                tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
            }
            gen_set_label(l1);
            tcg_gen_mov_tl(cpu_gpr[rt], t0);
        }
    } else {
        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5558
    }
5559 5560
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5561 5562
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
5563
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5564 5565 5566
    }
}

5567 5568
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5569 5570 5571 5572 5573 5574
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5575
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5576
/* macchwo   - macchwo.   */
5577
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5578
/* macchws   - macchws.   */
5579
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5580
/* macchwso  - macchwso.  */
5581
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5582
/* macchwsu  - macchwsu.  */
5583
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5584
/* macchwsuo - macchwsuo. */
5585
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5586
/* macchwu   - macchwu.   */
5587
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5588
/* macchwuo  - macchwuo.  */
5589
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5590
/* machhw    - machhw.    */
5591
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5592
/* machhwo   - machhwo.   */
5593
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5594
/* machhws   - machhws.   */
5595
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5596
/* machhwso  - machhwso.  */
5597
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5598
/* machhwsu  - machhwsu.  */
5599
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5600
/* machhwsuo - machhwsuo. */
5601
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5602
/* machhwu   - machhwu.   */
5603
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5604
/* machhwuo  - machhwuo.  */
5605
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5606
/* maclhw    - maclhw.    */
5607
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5608
/* maclhwo   - maclhwo.   */
5609
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5610
/* maclhws   - maclhws.   */
5611
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5612
/* maclhwso  - maclhwso.  */
5613
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5614
/* maclhwu   - maclhwu.   */
5615
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5616
/* maclhwuo  - maclhwuo.  */
5617
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5618
/* maclhwsu  - maclhwsu.  */
5619
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5620
/* maclhwsuo - maclhwsuo. */
5621
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5622
/* nmacchw   - nmacchw.   */
5623
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5624
/* nmacchwo  - nmacchwo.  */
5625
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5626
/* nmacchws  - nmacchws.  */
5627
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5628
/* nmacchwso - nmacchwso. */
5629
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5630
/* nmachhw   - nmachhw.   */
5631
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5632
/* nmachhwo  - nmachhwo.  */
5633
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5634
/* nmachhws  - nmachhws.  */
5635
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5636
/* nmachhwso - nmachhwso. */
5637
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5638
/* nmaclhw   - nmaclhw.   */
5639
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5640
/* nmaclhwo  - nmaclhwo.  */
5641
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5642
/* nmaclhws  - nmaclhws.  */
5643
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5644
/* nmaclhwso - nmaclhwso. */
5645
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5646 5647

/* mulchw  - mulchw.  */
5648
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5649
/* mulchwu - mulchwu. */
5650
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5651
/* mulhhw  - mulhhw.  */
5652
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5653
/* mulhhwu - mulhhwu. */
5654
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5655
/* mullhw  - mullhw.  */
5656
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5657
/* mullhwu - mullhwu. */
5658
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5659 5660

/* mfdcr */
5661
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5662 5663
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5664
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5665
#else
5666
    TCGv dcrn;
A
aurel32 已提交
5667
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5668
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5669 5670
        return;
    }
5671 5672 5673 5674 5675
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    dcrn = tcg_const_tl(SPR(ctx->opcode));
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
    tcg_temp_free(dcrn);
5676 5677 5678 5679
#endif
}

/* mtdcr */
5680
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5681 5682
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5683
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5684
#else
5685
    TCGv dcrn;
A
aurel32 已提交
5686
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5687
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5688 5689
        return;
    }
5690 5691 5692 5693 5694
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    dcrn = tcg_const_tl(SPR(ctx->opcode));
    gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(dcrn);
5695 5696 5697 5698
#endif
}

/* mfdcrx */
5699
/* XXX: not implemented on 440 ? */
5700
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5701 5702
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5703
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5704
#else
A
aurel32 已提交
5705
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5706
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5707 5708
        return;
    }
5709 5710 5711
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5712
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5713 5714 5715 5716
#endif
}

/* mtdcrx */
5717
/* XXX: not implemented on 440 ? */
5718
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5719 5720
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5721
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5722
#else
A
aurel32 已提交
5723
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5724
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5725 5726
        return;
    }
5727 5728 5729
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5730
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5731 5732 5733
#endif
}

5734 5735 5736
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
5737 5738 5739
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5740 5741 5742 5743 5744 5745
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

/* mtdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
{
5746 5747 5748
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5749 5750 5751
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

5752 5753 5754 5755
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5756
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5757
#else
A
aurel32 已提交
5758
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5759
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5760 5761 5762 5763 5764 5765 5766 5767 5768 5769
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5770
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5771
#else
A
aurel32 已提交
5772
    TCGv EA, val;
A
aurel32 已提交
5773
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5774
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5775 5776
        return;
    }
A
aurel32 已提交
5777
    gen_set_access_type(ctx, ACCESS_CACHE);
P
pbrook 已提交
5778
    EA = tcg_temp_new();
A
aurel32 已提交
5779
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
5780
    val = tcg_temp_new();
A
aurel32 已提交
5781
    gen_qemu_ld32u(ctx, val, EA);
A
aurel32 已提交
5782 5783 5784
    tcg_temp_free(val);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
    tcg_temp_free(EA);
5785 5786 5787 5788
#endif
}

/* icbt */
5789
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
}

/* iccci */
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5801
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5802
#else
A
aurel32 已提交
5803
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5804
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5805 5806 5807 5808 5809 5810 5811 5812 5813 5814
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5815
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5816
#else
A
aurel32 已提交
5817
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5818
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5819 5820 5821 5822 5823 5824
        return;
    }
    /* interpreted as no-op */
#endif
}

A
aurel32 已提交
5825
/* rfci (mem_idx only) */
5826
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5827 5828
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5829
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5830
#else
A
aurel32 已提交
5831
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5832
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5833 5834 5835
        return;
    }
    /* Restore CPU state */
5836
    gen_helper_40x_rfci();
A
aurel32 已提交
5837
    gen_sync_exception(ctx);
5838 5839 5840 5841 5842 5843
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5844
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5845
#else
A
aurel32 已提交
5846
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5847
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5848 5849 5850
        return;
    }
    /* Restore CPU state */
5851
    gen_helper_rfci();
A
aurel32 已提交
5852
    gen_sync_exception(ctx);
5853 5854 5855 5856
#endif
}

/* BookE specific */
5857
/* XXX: not implemented on 440 ? */
5858
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5859 5860
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5861
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5862
#else
A
aurel32 已提交
5863
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5864
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5865 5866 5867
        return;
    }
    /* Restore CPU state */
5868
    gen_helper_rfdi();
A
aurel32 已提交
5869
    gen_sync_exception(ctx);
5870 5871 5872
#endif
}

5873
/* XXX: not implemented on 440 ? */
5874
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5875 5876
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5877
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5878
#else
A
aurel32 已提交
5879
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5880
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5881 5882 5883
        return;
    }
    /* Restore CPU state */
5884
    gen_helper_rfmci();
A
aurel32 已提交
5885
    gen_sync_exception(ctx);
5886 5887
#endif
}
5888

5889
/* TLB management - PowerPC 405 implementation */
5890
/* tlbre */
5891
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5892 5893
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5894
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5895
#else
A
aurel32 已提交
5896
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5897
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5898 5899 5900 5901
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5902
        gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5903 5904
        break;
    case 1:
5905
        gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5906 5907
        break;
    default:
A
aurel32 已提交
5908
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5909
        break;
5910
    }
5911 5912 5913
#endif
}

5914
/* tlbsx - tlbsx. */
5915
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5916 5917
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5918
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5919
#else
5920
    TCGv t0;
A
aurel32 已提交
5921
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5922
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5923 5924
        return;
    }
5925
    t0 = tcg_temp_new();
A
aurel32 已提交
5926
    gen_addr_reg_index(ctx, t0);
5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937
    gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
    if (Rc(ctx->opcode)) {
        int l1 = gen_new_label();
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
        gen_set_label(l1);
    }
5938
#endif
B
bellard 已提交
5939 5940
}

5941
/* tlbwe */
5942
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5943
{
5944
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5945
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5946
#else
A
aurel32 已提交
5947
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5948
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5949 5950 5951 5952
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5953
        gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5954 5955
        break;
    case 1:
5956
        gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5957 5958
        break;
    default:
A
aurel32 已提交
5959
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5960
        break;
5961
    }
5962 5963 5964
#endif
}

5965
/* TLB management - PowerPC 440 implementation */
5966
/* tlbre */
5967
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5968 5969
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5970
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5971
#else
A
aurel32 已提交
5972
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5973
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5974 5975 5976 5977 5978 5979
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
5980 5981 5982 5983 5984
        {
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
            gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
            tcg_temp_free_i32(t0);
        }
5985 5986
        break;
    default:
A
aurel32 已提交
5987
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5988 5989 5990 5991 5992 5993
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5994
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5995 5996
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5997
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5998
#else
5999
    TCGv t0;
A
aurel32 已提交
6000
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6001
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6002 6003
        return;
    }
6004
    t0 = tcg_temp_new();
A
aurel32 已提交
6005
    gen_addr_reg_index(ctx, t0);
6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016
    gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
    if (Rc(ctx->opcode)) {
        int l1 = gen_new_label();
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
        gen_set_label(l1);
    }
6017 6018 6019 6020
#endif
}

/* tlbwe */
6021
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
6022 6023
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6024
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6025
#else
A
aurel32 已提交
6026
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6027
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6028 6029 6030 6031 6032 6033
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
6034 6035 6036 6037 6038
        {
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
            gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
            tcg_temp_free_i32(t0);
        }
6039 6040
        break;
    default:
A
aurel32 已提交
6041
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6042 6043 6044 6045 6046
        break;
    }
#endif
}

6047
/* wrtee */
6048
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
6049 6050
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6051
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6052
#else
6053
    TCGv t0;
A
aurel32 已提交
6054
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6055
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6056 6057
        return;
    }
6058 6059 6060 6061 6062
    t0 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
    tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
    tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
6063 6064 6065
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
A
aurel32 已提交
6066
    gen_stop_exception(ctx);
6067 6068 6069 6070
#endif
}

/* wrteei */
6071
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6072 6073
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6074
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6075
#else
A
aurel32 已提交
6076
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6077
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6078 6079
        return;
    }
6080 6081 6082
    if (ctx->opcode & 0x00010000) {
        tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
        /* Stop translation to have a chance to raise an exception */
A
aurel32 已提交
6083
        gen_stop_exception(ctx);
6084
    } else {
A
aurel32 已提交
6085
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6086
    }
6087 6088 6089
#endif
}

J
j_mayer 已提交
6090
/* PowerPC 440 specific instructions */
6091 6092 6093
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
6094 6095 6096 6097
    TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
    gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
                     cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
6098 6099 6100
}

/* mbar replaces eieio on 440 */
A
aurel32 已提交
6101
GEN_HANDLER(mbar, 0x1F, 0x16, 0x1a, 0x001FF801, PPC_BOOKE)
6102 6103 6104 6105 6106
{
    /* interpreted as no-op */
}

/* msync replaces sync on 440 */
6107
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6108 6109 6110 6111 6112
{
    /* interpreted as no-op */
}

/* icbt */
6113
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6114 6115 6116 6117 6118
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
6119 6120
}

6121 6122 6123
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */

A
aurel32 已提交
6124 6125
static always_inline TCGv_ptr gen_avr_ptr(int reg)
{
A
aurel32 已提交
6126
    TCGv_ptr r = tcg_temp_new_ptr();
A
aurel32 已提交
6127 6128 6129 6130
    tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
    return r;
}

6131
#define GEN_VR_LDX(name, opc2, opc3)                                          \
6132
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
6133
{                                                                             \
6134
    TCGv EA;                                                                  \
6135
    if (unlikely(!ctx->altivec_enabled)) {                                    \
A
aurel32 已提交
6136
        gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6137 6138
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
6139
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6140
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
6141
    gen_addr_reg_index(ctx, EA);                                              \
6142
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
A
aurel32 已提交
6143 6144
    if (ctx->le_mode) {                                                       \
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6145
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6146
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6147
    } else {                                                                  \
A
aurel32 已提交
6148
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6149
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6150
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6151 6152
    }                                                                         \
    tcg_temp_free(EA);                                                        \
6153 6154 6155 6156 6157
}

#define GEN_VR_STX(name, opc2, opc3)                                          \
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
{                                                                             \
6158
    TCGv EA;                                                                  \
6159
    if (unlikely(!ctx->altivec_enabled)) {                                    \
A
aurel32 已提交
6160
        gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6161 6162
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
6163
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6164
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
6165
    gen_addr_reg_index(ctx, EA);                                              \
6166
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
A
aurel32 已提交
6167 6168
    if (ctx->le_mode) {                                                       \
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6169
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6170
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6171
    } else {                                                                  \
A
aurel32 已提交
6172
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6173
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6174
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6175 6176
    }                                                                         \
    tcg_temp_free(EA);                                                        \
6177 6178
}

A
aurel32 已提交
6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214
#define GEN_VR_LVE(name, opc2, opc3)                                    \
    GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)   \
    {                                                                   \
        TCGv EA;                                                        \
        TCGv_ptr rs;                                                    \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        gen_set_access_type(ctx, ACCESS_INT);                           \
        EA = tcg_temp_new();                                            \
        gen_addr_reg_index(ctx, EA);                                    \
        rs = gen_avr_ptr(rS(ctx->opcode));                              \
        gen_helper_lve##name (rs, EA);                                  \
        tcg_temp_free(EA);                                              \
        tcg_temp_free_ptr(rs);                                          \
    }

#define GEN_VR_STVE(name, opc2, opc3)                                   \
    GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)  \
    {                                                                   \
        TCGv EA;                                                        \
        TCGv_ptr rs;                                                    \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        gen_set_access_type(ctx, ACCESS_INT);                           \
        EA = tcg_temp_new();                                            \
        gen_addr_reg_index(ctx, EA);                                    \
        rs = gen_avr_ptr(rS(ctx->opcode));                              \
        gen_helper_stve##name (rs, EA);                                 \
        tcg_temp_free(EA);                                              \
        tcg_temp_free_ptr(rs);                                          \
    }

6215
GEN_VR_LDX(lvx, 0x07, 0x03);
6216
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6217
GEN_VR_LDX(lvxl, 0x07, 0x0B);
6218

A
aurel32 已提交
6219 6220 6221 6222
GEN_VR_LVE(bx, 0x07, 0x00);
GEN_VR_LVE(hx, 0x07, 0x01);
GEN_VR_LVE(wx, 0x07, 0x02);

6223
GEN_VR_STX(svx, 0x07, 0x07);
6224
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6225
GEN_VR_STX(svxl, 0x07, 0x0F);
6226

A
aurel32 已提交
6227 6228 6229 6230
GEN_VR_STVE(bx, 0x07, 0x04);
GEN_VR_STVE(hx, 0x07, 0x05);
GEN_VR_STVE(wx, 0x07, 0x06);

A
aurel32 已提交
6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262
GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC)
{
    TCGv_ptr rd;
    TCGv EA;
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
    EA = tcg_temp_new();
    gen_addr_reg_index(ctx, EA);
    rd = gen_avr_ptr(rD(ctx->opcode));
    gen_helper_lvsl(rd, EA);
    tcg_temp_free(EA);
    tcg_temp_free_ptr(rd);
}

GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC)
{
    TCGv_ptr rd;
    TCGv EA;
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
    EA = tcg_temp_new();
    gen_addr_reg_index(ctx, EA);
    rd = gen_avr_ptr(rD(ctx->opcode));
    gen_helper_lvsr(rd, EA);
    tcg_temp_free(EA);
    tcg_temp_free_ptr(rd);
}

6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273
GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC)
{
    TCGv_i32 t;
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
    tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
    t = tcg_temp_new_i32();
    tcg_gen_ld_i32(t, cpu_env, offsetof(CPUState, vscr));
    tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
6274
    tcg_temp_free_i32(t);
6275 6276 6277 6278
}

GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC)
{
A
aurel32 已提交
6279
    TCGv_ptr p;
6280 6281 6282 6283
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
A
aurel32 已提交
6284 6285 6286
    p = gen_avr_ptr(rD(ctx->opcode));
    gen_helper_mtvscr(p);
    tcg_temp_free_ptr(p);
6287 6288
}

6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
/* Logical operations */
#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)            \
{                                                                       \
    if (unlikely(!ctx->altivec_enabled)) {                              \
        gen_exception(ctx, POWERPC_EXCP_VPU);                           \
        return;                                                         \
    }                                                                   \
    tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
    tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
}

GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);

6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323
#define GEN_VXFORM(name, opc2, opc3)                                    \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)            \
{                                                                       \
    TCGv_ptr ra, rb, rd;                                                \
    if (unlikely(!ctx->altivec_enabled)) {                              \
        gen_exception(ctx, POWERPC_EXCP_VPU);                           \
        return;                                                         \
    }                                                                   \
    ra = gen_avr_ptr(rA(ctx->opcode));                                  \
    rb = gen_avr_ptr(rB(ctx->opcode));                                  \
    rd = gen_avr_ptr(rD(ctx->opcode));                                  \
    gen_helper_##name (rd, ra, rb);                                     \
    tcg_temp_free_ptr(ra);                                              \
    tcg_temp_free_ptr(rb);                                              \
    tcg_temp_free_ptr(rd);                                              \
}

A
aurel32 已提交
6324 6325 6326 6327 6328 6329
GEN_VXFORM(vaddubm, 0, 0);
GEN_VXFORM(vadduhm, 0, 1);
GEN_VXFORM(vadduwm, 0, 2);
GEN_VXFORM(vsububm, 0, 16);
GEN_VXFORM(vsubuhm, 0, 17);
GEN_VXFORM(vsubuwm, 0, 18);
6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341
GEN_VXFORM(vmaxub, 1, 0);
GEN_VXFORM(vmaxuh, 1, 1);
GEN_VXFORM(vmaxuw, 1, 2);
GEN_VXFORM(vmaxsb, 1, 4);
GEN_VXFORM(vmaxsh, 1, 5);
GEN_VXFORM(vmaxsw, 1, 6);
GEN_VXFORM(vminub, 1, 8);
GEN_VXFORM(vminuh, 1, 9);
GEN_VXFORM(vminuw, 1, 10);
GEN_VXFORM(vminsb, 1, 12);
GEN_VXFORM(vminsh, 1, 13);
GEN_VXFORM(vminsw, 1, 14);
A
aurel32 已提交
6342 6343 6344 6345 6346 6347
GEN_VXFORM(vavgub, 1, 16);
GEN_VXFORM(vavguh, 1, 17);
GEN_VXFORM(vavguw, 1, 18);
GEN_VXFORM(vavgsb, 1, 20);
GEN_VXFORM(vavgsh, 1, 21);
GEN_VXFORM(vavgsw, 1, 22);
A
aurel32 已提交
6348 6349 6350 6351 6352 6353
GEN_VXFORM(vmrghb, 6, 0);
GEN_VXFORM(vmrghh, 6, 1);
GEN_VXFORM(vmrghw, 6, 2);
GEN_VXFORM(vmrglb, 6, 4);
GEN_VXFORM(vmrglh, 6, 5);
GEN_VXFORM(vmrglw, 6, 6);
A
aurel32 已提交
6354 6355 6356 6357 6358 6359 6360 6361
GEN_VXFORM(vmuloub, 4, 0);
GEN_VXFORM(vmulouh, 4, 1);
GEN_VXFORM(vmulosb, 4, 4);
GEN_VXFORM(vmulosh, 4, 5);
GEN_VXFORM(vmuleub, 4, 8);
GEN_VXFORM(vmuleuh, 4, 9);
GEN_VXFORM(vmulesb, 4, 12);
GEN_VXFORM(vmulesh, 4, 13);
A
aurel32 已提交
6362 6363 6364
GEN_VXFORM(vslb, 2, 4);
GEN_VXFORM(vslh, 2, 5);
GEN_VXFORM(vslw, 2, 6);
A
aurel32 已提交
6365 6366 6367 6368 6369 6370
GEN_VXFORM(vsrb, 2, 8);
GEN_VXFORM(vsrh, 2, 9);
GEN_VXFORM(vsrw, 2, 10);
GEN_VXFORM(vsrab, 2, 12);
GEN_VXFORM(vsrah, 2, 13);
GEN_VXFORM(vsraw, 2, 14);
A
aurel32 已提交
6371 6372
GEN_VXFORM(vslo, 6, 16);
GEN_VXFORM(vsro, 6, 17);
A
aurel32 已提交
6373 6374
GEN_VXFORM(vaddcuw, 0, 6);
GEN_VXFORM(vsubcuw, 0, 22);
6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386
GEN_VXFORM(vaddubs, 0, 8);
GEN_VXFORM(vadduhs, 0, 9);
GEN_VXFORM(vadduws, 0, 10);
GEN_VXFORM(vaddsbs, 0, 12);
GEN_VXFORM(vaddshs, 0, 13);
GEN_VXFORM(vaddsws, 0, 14);
GEN_VXFORM(vsububs, 0, 24);
GEN_VXFORM(vsubuhs, 0, 25);
GEN_VXFORM(vsubuws, 0, 26);
GEN_VXFORM(vsubsbs, 0, 28);
GEN_VXFORM(vsubshs, 0, 29);
GEN_VXFORM(vsubsws, 0, 30);
A
aurel32 已提交
6387 6388 6389
GEN_VXFORM(vrlb, 2, 0);
GEN_VXFORM(vrlh, 2, 1);
GEN_VXFORM(vrlw, 2, 2);
A
aurel32 已提交
6390 6391
GEN_VXFORM(vsl, 2, 7);
GEN_VXFORM(vsr, 2, 11);
6392 6393 6394 6395 6396 6397 6398 6399
GEN_VXFORM(vpkuhum, 7, 0);
GEN_VXFORM(vpkuwum, 7, 1);
GEN_VXFORM(vpkuhus, 7, 2);
GEN_VXFORM(vpkuwus, 7, 3);
GEN_VXFORM(vpkshus, 7, 4);
GEN_VXFORM(vpkswus, 7, 5);
GEN_VXFORM(vpkshss, 7, 6);
GEN_VXFORM(vpkswss, 7, 7);
A
aurel32 已提交
6400
GEN_VXFORM(vpkpx, 7, 12);
6401 6402 6403 6404 6405
GEN_VXFORM(vsum4ubs, 4, 24);
GEN_VXFORM(vsum4sbs, 4, 28);
GEN_VXFORM(vsum4shs, 4, 25);
GEN_VXFORM(vsum2sws, 4, 26);
GEN_VXFORM(vsumsws, 4, 30);
6406 6407
GEN_VXFORM(vaddfp, 5, 0);
GEN_VXFORM(vsubfp, 5, 1);
6408 6409
GEN_VXFORM(vmaxfp, 5, 16);
GEN_VXFORM(vminfp, 5, 17);
A
aurel32 已提交
6410

6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431
#define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
    GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC)   \
    {                                                                   \
        TCGv_ptr ra, rb, rd;                                            \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        ra = gen_avr_ptr(rA(ctx->opcode));                              \
        rb = gen_avr_ptr(rB(ctx->opcode));                              \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        gen_helper_##opname (rd, ra, rb);                               \
        tcg_temp_free_ptr(ra);                                          \
        tcg_temp_free_ptr(rb);                                          \
        tcg_temp_free_ptr(rd);                                          \
    }

#define GEN_VXRFORM(name, opc2, opc3)                                \
    GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
    GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))

6432 6433 6434 6435 6436 6437 6438 6439 6440
GEN_VXRFORM(vcmpequb, 3, 0)
GEN_VXRFORM(vcmpequh, 3, 1)
GEN_VXRFORM(vcmpequw, 3, 2)
GEN_VXRFORM(vcmpgtsb, 3, 12)
GEN_VXRFORM(vcmpgtsh, 3, 13)
GEN_VXRFORM(vcmpgtsw, 3, 14)
GEN_VXRFORM(vcmpgtub, 3, 8)
GEN_VXRFORM(vcmpgtuh, 3, 9)
GEN_VXRFORM(vcmpgtuw, 3, 10)
6441 6442 6443 6444
GEN_VXRFORM(vcmpeqfp, 3, 3)
GEN_VXRFORM(vcmpgefp, 3, 7)
GEN_VXRFORM(vcmpgtfp, 3, 11)
GEN_VXRFORM(vcmpbfp, 3, 15)
6445

A
aurel32 已提交
6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465
#define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
    GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)        \
    {                                                                   \
        TCGv_ptr rd;                                                    \
        TCGv_i32 simm;                                                  \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        gen_helper_##name (rd, simm);                                   \
        tcg_temp_free_i32(simm);                                        \
        tcg_temp_free_ptr(rd);                                          \
    }

GEN_VXFORM_SIMM(vspltisb, 6, 12);
GEN_VXFORM_SIMM(vspltish, 6, 13);
GEN_VXFORM_SIMM(vspltisw, 6, 14);

6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480
#define GEN_VXFORM_NOA(name, opc2, opc3)                                \
    GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)        \
    {                                                                   \
        TCGv_ptr rb, rd;                                                \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        rb = gen_avr_ptr(rB(ctx->opcode));                              \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        gen_helper_##name (rd, rb);                                     \
        tcg_temp_free_ptr(rb);                                          \
        tcg_temp_free_ptr(rd);                                         \
    }

A
aurel32 已提交
6481 6482 6483 6484
GEN_VXFORM_NOA(vupkhsb, 7, 8);
GEN_VXFORM_NOA(vupkhsh, 7, 9);
GEN_VXFORM_NOA(vupklsb, 7, 10);
GEN_VXFORM_NOA(vupklsh, 7, 11);
A
aurel32 已提交
6485 6486
GEN_VXFORM_NOA(vupkhpx, 7, 13);
GEN_VXFORM_NOA(vupklpx, 7, 15);
A
aurel32 已提交
6487
GEN_VXFORM_NOA(vrefp, 5, 4);
A
aurel32 已提交
6488
GEN_VXFORM_NOA(vrsqrtefp, 5, 5);
6489
GEN_VXFORM_NOA(vlogefp, 5, 7);
A
aurel32 已提交
6490 6491 6492 6493
GEN_VXFORM_NOA(vrfim, 5, 8);
GEN_VXFORM_NOA(vrfin, 5, 9);
GEN_VXFORM_NOA(vrfip, 5, 10);
GEN_VXFORM_NOA(vrfiz, 5, 11);
A
aurel32 已提交
6494

6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510
#define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
    GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)        \
    {                                                                   \
        TCGv_ptr rd;                                                    \
        TCGv_i32 simm;                                                  \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        gen_helper_##name (rd, simm);                                   \
        tcg_temp_free_i32(simm);                                        \
        tcg_temp_free_ptr(rd);                                          \
    }

6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528
#define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
    GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)        \
    {                                                                   \
        TCGv_ptr rb, rd;                                                \
        TCGv_i32 uimm;                                                  \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
        rb = gen_avr_ptr(rB(ctx->opcode));                              \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        gen_helper_##name (rd, rb, uimm);                               \
        tcg_temp_free_i32(uimm);                                        \
        tcg_temp_free_ptr(rb);                                          \
        tcg_temp_free_ptr(rd);                                          \
    }

A
aurel32 已提交
6529 6530 6531
GEN_VXFORM_UIMM(vspltb, 6, 8);
GEN_VXFORM_UIMM(vsplth, 6, 9);
GEN_VXFORM_UIMM(vspltw, 6, 10);
A
aurel32 已提交
6532 6533
GEN_VXFORM_UIMM(vcfux, 5, 12);
GEN_VXFORM_UIMM(vcfsx, 5, 13);
6534 6535
GEN_VXFORM_UIMM(vctuxs, 5, 14);
GEN_VXFORM_UIMM(vctsxs, 5, 15);
A
aurel32 已提交
6536

A
aurel32 已提交
6537 6538 6539
GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC)
{
    TCGv_ptr ra, rb, rd;
6540
    TCGv_i32 sh;
A
aurel32 已提交
6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
    ra = gen_avr_ptr(rA(ctx->opcode));
    rb = gen_avr_ptr(rB(ctx->opcode));
    rd = gen_avr_ptr(rD(ctx->opcode));
    sh = tcg_const_i32(VSH(ctx->opcode));
    gen_helper_vsldoi (rd, ra, rb, sh);
    tcg_temp_free_ptr(ra);
    tcg_temp_free_ptr(rb);
    tcg_temp_free_ptr(rd);
6553
    tcg_temp_free_i32(sh);
A
aurel32 已提交
6554 6555
}

6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578
#define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
    GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC) \
    {                                                                   \
        TCGv_ptr ra, rb, rc, rd;                                        \
        if (unlikely(!ctx->altivec_enabled)) {                          \
            gen_exception(ctx, POWERPC_EXCP_VPU);                       \
            return;                                                     \
        }                                                               \
        ra = gen_avr_ptr(rA(ctx->opcode));                              \
        rb = gen_avr_ptr(rB(ctx->opcode));                              \
        rc = gen_avr_ptr(rC(ctx->opcode));                              \
        rd = gen_avr_ptr(rD(ctx->opcode));                              \
        if (Rc(ctx->opcode)) {                                          \
            gen_helper_##name1 (rd, ra, rb, rc);                        \
        } else {                                                        \
            gen_helper_##name0 (rd, ra, rb, rc);                        \
        }                                                               \
        tcg_temp_free_ptr(ra);                                          \
        tcg_temp_free_ptr(rb);                                          \
        tcg_temp_free_ptr(rc);                                          \
        tcg_temp_free_ptr(rd);                                          \
    }

A
aurel32 已提交
6579 6580
GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)

A
aurel32 已提交
6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598
GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC)
{
    TCGv_ptr ra, rb, rc, rd;
    if (unlikely(!ctx->altivec_enabled)) {
        gen_exception(ctx, POWERPC_EXCP_VPU);
        return;
    }
    ra = gen_avr_ptr(rA(ctx->opcode));
    rb = gen_avr_ptr(rB(ctx->opcode));
    rc = gen_avr_ptr(rC(ctx->opcode));
    rd = gen_avr_ptr(rD(ctx->opcode));
    gen_helper_vmladduhm(rd, ra, rb, rc);
    tcg_temp_free_ptr(ra);
    tcg_temp_free_ptr(rb);
    tcg_temp_free_ptr(rc);
    tcg_temp_free_ptr(rd);
}

A
aurel32 已提交
6599
GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
A
aurel32 已提交
6600
GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
A
aurel32 已提交
6601
GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
A
aurel32 已提交
6602
GEN_VAFORM_PAIRED(vsel, vperm, 21)
6603
GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
A
aurel32 已提交
6604

6605 6606
/***                           SPE extension                               ***/
/* Register moves */
6607

P
pbrook 已提交
6608
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
A
aurel32 已提交
6609 6610 6611
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
#else
P
pbrook 已提交
6612
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6613
#endif
A
aurel32 已提交
6614
}
6615

P
pbrook 已提交
6616
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
A
aurel32 已提交
6617 6618 6619
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(cpu_gpr[reg], t);
#else
P
pbrook 已提交
6620
    TCGv_i64 tmp = tcg_temp_new_i64();
A
aurel32 已提交
6621 6622 6623
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
    tcg_gen_shri_i64(tmp, t, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
P
pbrook 已提交
6624
    tcg_temp_free_i64(tmp);
6625
#endif
A
aurel32 已提交
6626
}
6627

6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
{                                                                             \
    if (Rc(ctx->opcode))                                                      \
        gen_##name1(ctx);                                                     \
    else                                                                      \
        gen_##name0(ctx);                                                     \
}

/* Handler for undefined SPE opcodes */
6638
static always_inline void gen_speundef (DisasContext *ctx)
6639
{
A
aurel32 已提交
6640
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6641 6642
}

6643 6644 6645
/* SPE logic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6646
static always_inline void gen_##name (DisasContext *ctx)                      \
6647 6648
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6649
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6650 6651
        return;                                                               \
    }                                                                         \
6652 6653 6654 6655 6656 6657 6658 6659
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
}
#else
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6660
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6661 6662 6663 6664 6665 6666
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
           cpu_gprh[rB(ctx->opcode)]);                                        \
6667
}
6668 6669 6670 6671 6672 6673 6674 6675 6676 6677
#endif

GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6678

6679 6680 6681
/* SPE logic immediate */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6682 6683 6684
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6685
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6686 6687
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6688 6689 6690
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6691 6692 6693 6694
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_opi(t0, t0, rB(ctx->opcode));                                         \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6695
    tcg_temp_free_i64(t2);                                                    \
6696 6697
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6698 6699
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6700
}
6701 6702
#else
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6703
static always_inline void gen_##name (DisasContext *ctx)                      \
6704 6705
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6706
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6707 6708
        return;                                                               \
    }                                                                         \
6709 6710 6711 6712
    tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
            rB(ctx->opcode));                                                 \
    tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
            rB(ctx->opcode));                                                 \
6713
}
6714 6715 6716 6717 6718
#endif
GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6719

6720 6721 6722
/* SPE arithmetic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6723
static always_inline void gen_##name (DisasContext *ctx)                      \
6724 6725
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6726
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6727 6728
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6729 6730 6731
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6732 6733 6734 6735
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_op(t0, t0);                                                           \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6736
    tcg_temp_free_i64(t2);                                                    \
6737 6738
    tcg_op(t1, t1);                                                           \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6739 6740
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6741
}
6742
#else
P
pbrook 已提交
6743
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6744 6745 6746
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6747
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6748 6749 6750 6751 6752 6753
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
}
#endif
6754

P
pbrook 已提交
6755
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6756 6757 6758
{
    int l1 = gen_new_label();
    int l2 = gen_new_label();
6759

6760 6761 6762 6763
    tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
    tcg_gen_neg_i32(ret, arg1);
    tcg_gen_br(l2);
    gen_set_label(l1);
P
pbrook 已提交
6764
    tcg_gen_mov_i32(ret, arg1);
6765 6766 6767 6768 6769 6770
    gen_set_label(l2);
}
GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
P
pbrook 已提交
6771
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6772
{
6773 6774 6775 6776
    tcg_gen_addi_i32(ret, arg1, 0x8000);
    tcg_gen_ext16u_i32(ret, ret);
}
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
P
pbrook 已提交
6777 6778
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6779

6780 6781 6782
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6783 6784
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6785
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6786 6787
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6788 6789 6790
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6791
    TCGv_i64 t3 = tcg_temp_local_new_i64();                                   \
6792 6793 6794 6795 6796 6797 6798
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_op(t0, t0, t2);                                                       \
    tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t3);                                            \
    tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t2, t3);                                            \
P
pbrook 已提交
6799
    tcg_temp_free_i64(t3);                                                    \
6800
    tcg_op(t1, t1, t2);                                                       \
P
pbrook 已提交
6801
    tcg_temp_free_i32(t2);                                                    \
6802
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6803 6804
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6805
}
6806 6807 6808
#else
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6809 6810
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6811
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6812 6813
        return;                                                               \
    }                                                                         \
6814 6815 6816 6817
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
           cpu_gprh[rB(ctx->opcode)]);                                        \
6818
}
6819
#endif
6820

P
pbrook 已提交
6821
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6822
{
P
pbrook 已提交
6823
    TCGv_i32 t0;
6824
    int l1, l2;
6825

6826 6827
    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6828
    t0 = tcg_temp_local_new_i32();
6829 6830 6831 6832 6833 6834 6835 6836
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_shr_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6837
    tcg_temp_free_i32(t0);
6838 6839
}
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
P
pbrook 已提交
6840
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6841
{
P
pbrook 已提交
6842
    TCGv_i32 t0;
6843 6844 6845 6846
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6847
    t0 = tcg_temp_local_new_i32();
6848 6849 6850 6851 6852 6853 6854 6855
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_sar_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6856
    tcg_temp_free_i32(t0);
6857 6858
}
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
P
pbrook 已提交
6859
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6860
{
P
pbrook 已提交
6861
    TCGv_i32 t0;
6862 6863 6864 6865
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6866
    t0 = tcg_temp_local_new_i32();
6867 6868 6869 6870 6871 6872 6873 6874
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_shl_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6875
    tcg_temp_free_i32(t0);
6876 6877
}
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
P
pbrook 已提交
6878
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6879
{
P
pbrook 已提交
6880
    TCGv_i32 t0 = tcg_temp_new_i32();
6881 6882
    tcg_gen_andi_i32(t0, arg2, 0x1F);
    tcg_gen_rotl_i32(ret, arg1, t0);
P
pbrook 已提交
6883
    tcg_temp_free_i32(t0);
6884 6885 6886 6887 6888
}
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
static always_inline void gen_evmergehi (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6889
        gen_exception(ctx, POWERPC_EXCP_APU);
6890 6891 6892
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6893 6894
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
}
GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
P
pbrook 已提交
6906
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6907
{
6908 6909 6910
    tcg_gen_sub_i32(ret, arg2, arg1);
}
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6911

6912 6913 6914 6915 6916 6917
/* SPE arithmetic immediate */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6918
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6919 6920
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6921 6922 6923
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6924 6925 6926 6927
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_op(t0, t0, rA(ctx->opcode));                                          \
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
A
aurel32 已提交
6928
    tcg_temp_free_i64(t2);                                                    \
6929 6930
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6931 6932
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6933 6934 6935 6936 6937 6938
}
#else
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6939
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
           rA(ctx->opcode));                                                  \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
           rA(ctx->opcode));                                                  \
}
#endif
GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);

/* SPE comparison */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6957
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6958 6959 6960 6961 6962 6963
        return;                                                               \
    }                                                                         \
    int l1 = gen_new_label();                                                 \
    int l2 = gen_new_label();                                                 \
    int l3 = gen_new_label();                                                 \
    int l4 = gen_new_label();                                                 \
P
pbrook 已提交
6964 6965 6966
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6967 6968 6969
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
P
pbrook 已提交
6970
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6971 6972 6973 6974 6975 6976 6977 6978 6979
    tcg_gen_br(l2);                                                           \
    gen_set_label(l1);                                                        \
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
    gen_set_label(l2);                                                        \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t0, t2);                                            \
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6980
    tcg_temp_free_i64(t2);                                                    \
6981 6982 6983 6984 6985 6986 6987 6988
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
    tcg_gen_br(l4);                                                           \
    gen_set_label(l3);                                                        \
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
                    CRF_CH | CRF_CH_OR_CL);                                   \
    gen_set_label(l4);                                                        \
P
pbrook 已提交
6989 6990
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6991 6992 6993 6994 6995 6996
}
#else
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6997
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033
        return;                                                               \
    }                                                                         \
    int l1 = gen_new_label();                                                 \
    int l2 = gen_new_label();                                                 \
    int l3 = gen_new_label();                                                 \
    int l4 = gen_new_label();                                                 \
                                                                              \
    tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
                       cpu_gpr[rB(ctx->opcode)], l1);                         \
    tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
    tcg_gen_br(l2);                                                           \
    gen_set_label(l1);                                                        \
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
    gen_set_label(l2);                                                        \
    tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
                       cpu_gprh[rB(ctx->opcode)], l3);                        \
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
    tcg_gen_br(l4);                                                           \
    gen_set_label(l3);                                                        \
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
                    CRF_CH | CRF_CH_OR_CL);                                   \
    gen_set_label(l4);                                                        \
}
#endif
GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);

/* SPE misc */
static always_inline void gen_brinc (DisasContext *ctx)
{
    /* Note: brinc is usable even if SPE is disabled */
P
pbrook 已提交
7034 7035
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
7036
}
7037 7038 7039
static always_inline void gen_evmergelo (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7040
        gen_exception(ctx, POWERPC_EXCP_APU);
7041 7042 7043
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
7044 7045
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evmergehilo (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7059
        gen_exception(ctx, POWERPC_EXCP_APU);
7060 7061 7062
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
7063 7064
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evmergelohi (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7078
        gen_exception(ctx, POWERPC_EXCP_APU);
7079 7080 7081
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
7082 7083
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evsplati (DisasContext *ctx)
{
7096
    uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
7097

7098
#if defined(TARGET_PPC64)
7099
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
7100 7101 7102 7103 7104
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
}
7105
static always_inline void gen_evsplatfi (DisasContext *ctx)
7106
{
7107
    uint64_t imm = rA(ctx->opcode) << 11;
7108

7109
#if defined(TARGET_PPC64)
7110
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
7111 7112 7113 7114
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
7115 7116
}

7117 7118 7119 7120 7121 7122
static always_inline void gen_evsel (DisasContext *ctx)
{
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    int l3 = gen_new_label();
    int l4 = gen_new_label();
P
pbrook 已提交
7123
    TCGv_i32 t0 = tcg_temp_local_new_i32();
7124
#if defined(TARGET_PPC64)
P
pbrook 已提交
7125 7126
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157
#endif
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
    tcg_gen_br(l2);
    gen_set_label(l1);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
#endif
    gen_set_label(l2);
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
#else
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
    tcg_gen_br(l4);
    gen_set_label(l3);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
#else
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
#endif
    gen_set_label(l4);
P
pbrook 已提交
7158
    tcg_temp_free_i32(t0);
7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180
#if defined(TARGET_PPC64)
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
#endif
}
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207

GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////

7208
/* SPE load and stores */
A
aurel32 已提交
7209
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
7210 7211 7212
{
    target_ulong uimm = rB(ctx->opcode);

A
aurel32 已提交
7213
    if (rA(ctx->opcode) == 0) {
7214
        tcg_gen_movi_tl(EA, uimm << sh);
A
aurel32 已提交
7215
    } else {
7216
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
A
aurel32 已提交
7217 7218 7219 7220 7221 7222
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
7223
}
7224 7225 7226 7227

static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
7228
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7229 7230
#else
    TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
7231
    gen_qemu_ld64(ctx, t0, addr);
7232 7233 7234 7235 7236
    tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_temp_free_i64(t0);
#endif
7237
}
7238 7239 7240

static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
{
7241
#if defined(TARGET_PPC64)
7242
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7243
    gen_qemu_ld32u(ctx, t0, addr);
7244
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
A
aurel32 已提交
7245 7246
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_ld32u(ctx, t0, addr);
7247 7248 7249
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7250 7251 7252
    gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7253
#endif
7254
}
7255 7256 7257 7258 7259

static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
7260
    gen_qemu_ld16u(ctx, t0, addr);
7261
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
A
aurel32 已提交
7262 7263
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7264 7265
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
7266 7267
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7268 7269
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
7270 7271
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7272
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7273
#else
A
aurel32 已提交
7274
    gen_qemu_ld16u(ctx, t0, addr);
7275
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
7276 7277
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7278
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
A
aurel32 已提交
7279 7280
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7281
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
7282 7283
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7284
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
7285
#endif
7286
    tcg_temp_free(t0);
7287 7288
}

7289 7290 7291
static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7292
    gen_qemu_ld16u(ctx, t0, addr);
7293 7294 7295 7296 7297 7298 7299 7300 7301 7302
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
7303 7304
}

7305 7306 7307
static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7308
    gen_qemu_ld16u(ctx, t0, addr);
7309 7310 7311 7312 7313 7314 7315 7316
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
7317 7318
}

7319 7320 7321
static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7322
    gen_qemu_ld16s(ctx, t0, addr);
7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_ext32u_tl(t0, t0);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
7338
    gen_qemu_ld16u(ctx, t0, addr);
7339
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
A
aurel32 已提交
7340 7341
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7342 7343 7344
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
A
aurel32 已提交
7345
    gen_qemu_ld16u(ctx, t0, addr);
7346
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
7347 7348
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7349 7350 7351 7352 7353 7354 7355 7356 7357
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7358 7359 7360
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7361 7362 7363 7364
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7365 7366 7367
    gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7368 7369 7370 7371 7372 7373 7374
#endif
}

static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7375
    gen_qemu_ld16s(ctx, t0, addr);
7376
    tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
7377 7378
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16s(ctx, t0, addr);
7379 7380 7381 7382
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7383 7384 7385
    gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
7386 7387 7388 7389 7390 7391
#endif
}

static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
7392
    gen_qemu_ld32u(ctx, t0, addr);
7393
#if defined(TARGET_PPC64)
7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
7407
    gen_qemu_ld16u(ctx, t0, addr);
7408 7409 7410
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
7411 7412
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7413 7414 7415 7416
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
A
aurel32 已提交
7417
    gen_qemu_ld16u(ctx, t0, addr);
7418 7419
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
A
aurel32 已提交
7420 7421
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7422 7423
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
7424
#endif
7425 7426 7427 7428 7429 7430
    tcg_temp_free(t0);
}

static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
7431
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7432
#else
7433 7434
    TCGv_i64 t0 = tcg_temp_new_i64();
    tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
A
aurel32 已提交
7435
    gen_qemu_st64(ctx, t0, addr);
7436 7437 7438 7439 7440 7441
    tcg_temp_free_i64(t0);
#endif
}

static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
{
7442
#if defined(TARGET_PPC64)
7443 7444
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7445
    gen_qemu_st32(ctx, t0, addr);
7446 7447
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7448
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7449
#endif
A
aurel32 已提交
7450 7451
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7452 7453 7454 7455 7456 7457 7458 7459 7460 7461
}

static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
#else
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
#endif
A
aurel32 已提交
7462 7463
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
7464 7465
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7466
    gen_qemu_st16(ctx, t0, addr);
7467
#else
A
aurel32 已提交
7468
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7469
#endif
A
aurel32 已提交
7470
    gen_addr_add(ctx, addr, addr, 2);
7471
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
7472
    gen_qemu_st16(ctx, t0, addr);
7473
    tcg_temp_free(t0);
A
aurel32 已提交
7474 7475
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7476 7477 7478 7479 7480 7481 7482 7483 7484 7485
}

static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
#else
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
#endif
A
aurel32 已提交
7486 7487
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
7488
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
7489
    gen_qemu_st16(ctx, t0, addr);
7490 7491 7492 7493 7494 7495 7496 7497
    tcg_temp_free(t0);
}

static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7498
    gen_qemu_st16(ctx, t0, addr);
7499 7500
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7501
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7502
#endif
A
aurel32 已提交
7503 7504
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7505 7506 7507 7508 7509 7510 7511
}

static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7512
    gen_qemu_st32(ctx, t0, addr);
7513 7514
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7515
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7516 7517 7518 7519 7520
#endif
}

static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
{
A
aurel32 已提交
7521
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7522 7523 7524
}

#define GEN_SPEOP_LDST(name, opc2, sh)                                        \
A
aurel32 已提交
7525
GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                      \
7526 7527 7528
{                                                                             \
    TCGv t0;                                                                  \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7529
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7530 7531
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7532
    gen_set_access_type(ctx, ACCESS_INT);                                     \
7533 7534
    t0 = tcg_temp_new();                                                      \
    if (Rc(ctx->opcode)) {                                                    \
A
aurel32 已提交
7535
        gen_addr_spe_imm_index(ctx, t0, sh);                                  \
7536
    } else {                                                                  \
A
aurel32 已提交
7537
        gen_addr_reg_index(ctx, t0);                                          \
7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561
    }                                                                         \
    gen_op_##name(ctx, t0);                                                   \
    tcg_temp_free(t0);                                                        \
}

GEN_SPEOP_LDST(evldd, 0x00, 3);
GEN_SPEOP_LDST(evldw, 0x01, 3);
GEN_SPEOP_LDST(evldh, 0x02, 3);
GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
GEN_SPEOP_LDST(evlwhe, 0x08, 2);
GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);

GEN_SPEOP_LDST(evstdd, 0x10, 3);
GEN_SPEOP_LDST(evstdw, 0x11, 3);
GEN_SPEOP_LDST(evstdh, 0x12, 3);
GEN_SPEOP_LDST(evstwhe, 0x18, 2);
GEN_SPEOP_LDST(evstwho, 0x1A, 2);
GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639

/* Multiply and add - TODO */
#if 0
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);

GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);

GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, PPC_SPE);
GEN_SPE(evmra,          speundef,      0x07, 0x13, 0x0000F800, PPC_SPE);

GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);

GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);

GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);

GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
#endif

/***                      SPE floating-point extension                     ***/
A
aurel32 已提交
7640 7641
#if defined(TARGET_PPC64)
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7642
static always_inline void gen_##name (DisasContext *ctx)                      \
7643
{                                                                             \
A
aurel32 已提交
7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655
    TCGv_i32 t0;                                                              \
    TCGv t1;                                                                  \
    t0 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(t0, t0);                                                \
    t1 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t1, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
    tcg_temp_free(t1);                                                        \
7656
}
A
aurel32 已提交
7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i32 t0;                                                              \
    TCGv t1;                                                                  \
    t0 = tcg_temp_new_i32();                                                  \
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
    t1 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t1, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
    tcg_temp_free(t1);                                                        \
}
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i32 t0 = tcg_temp_new_i32();                                         \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
    tcg_temp_free_i32(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7686 7687
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7688 7689
    TCGv_i32 t0, t1;                                                          \
    TCGv_i64 t2;                                                              \
7690
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7691
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7692 7693
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706
    t0 = tcg_temp_new_i32();                                                  \
    t1 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(t0, t0, t1);                                            \
    tcg_temp_free_i32(t1);                                                    \
    t2 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t2, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
    tcg_temp_free(t2);                                                        \
7707
}
A
aurel32 已提交
7708
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7709 7710 7711
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7712
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7713 7714
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7715 7716
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
                      cpu_gpr[rB(ctx->opcode)]);                              \
7717
}
A
aurel32 已提交
7718
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7719 7720
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7721
    TCGv_i32 t0, t1;                                                          \
7722
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7723
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7724 7725
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737
    t0 = tcg_temp_new_i32();                                                  \
    t1 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
}
#define GEN_SPEFPUOP_COMP_64(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7738
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7739 7740 7741 7742 7743 7744 7745 7746 7747 7748
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#else
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7749
}
A
aurel32 已提交
7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
    gen_helper_##name(t0, t0);                                                \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7779
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7780 7781 7782 7783 7784 7785 7786 7787 7788 7789
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0, t1;                                                          \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7790
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805
        return;                                                               \
    }                                                                         \
    t0 = tcg_temp_new_i64();                                                  \
    t1 = tcg_temp_new_i64();                                                  \
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
    gen_helper_##name(t0, t0, t1);                                            \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
    tcg_temp_free_i64(t1);                                                    \
}
#define GEN_SPEFPUOP_COMP_32(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7806
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7807 7808 7809 7810 7811 7812 7813 7814 7815 7816
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_COMP_64(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0, t1;                                                          \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7817
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828
        return;                                                               \
    }                                                                         \
    t0 = tcg_temp_new_i64();                                                  \
    t1 = tcg_temp_new_i64();                                                  \
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
    tcg_temp_free_i64(t0);                                                    \
    tcg_temp_free_i64(t1);                                                    \
}
#endif
7829

7830 7831
/* Single precision floating-point vectors operations */
/* Arithmetic */
A
aurel32 已提交
7832 7833 7834 7835 7836 7837 7838
GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
GEN_SPEFPUOP_ARITH2_64_64(evfssub);
GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
static always_inline void gen_evfsabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7839
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
#else
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
#endif
}
static always_inline void gen_evfsnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7852
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
#else
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}
static always_inline void gen_evfsneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7865
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7866 7867 7868 7869 7870 7871 7872 7873 7874 7875
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
#else
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}

7876
/* Conversion */
A
aurel32 已提交
7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887
GEN_SPEFPUOP_CONV_64_64(evfscfui);
GEN_SPEFPUOP_CONV_64_64(evfscfsi);
GEN_SPEFPUOP_CONV_64_64(evfscfuf);
GEN_SPEFPUOP_CONV_64_64(evfscfsf);
GEN_SPEFPUOP_CONV_64_64(evfsctui);
GEN_SPEFPUOP_CONV_64_64(evfsctsi);
GEN_SPEFPUOP_CONV_64_64(evfsctuf);
GEN_SPEFPUOP_CONV_64_64(evfsctsf);
GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
GEN_SPEFPUOP_CONV_64_64(evfsctsiz);

7888
/* Comparison */
A
aurel32 已提交
7889 7890 7891 7892 7893 7894
GEN_SPEFPUOP_COMP_64(evfscmpgt);
GEN_SPEFPUOP_COMP_64(evfscmplt);
GEN_SPEFPUOP_COMP_64(evfscmpeq);
GEN_SPEFPUOP_COMP_64(evfststgt);
GEN_SPEFPUOP_COMP_64(evfststlt);
GEN_SPEFPUOP_COMP_64(evfststeq);
7895 7896

/* Opcodes definitions */
7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPE_SINGLE); //
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPE_SINGLE); //
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPE_SINGLE); //
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPE_SINGLE); //
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPE_SINGLE); //
7911 7912 7913

/* Single precision floating-point operations */
/* Arithmetic */
A
aurel32 已提交
7914 7915 7916 7917 7918 7919 7920
GEN_SPEFPUOP_ARITH2_32_32(efsadd);
GEN_SPEFPUOP_ARITH2_32_32(efssub);
GEN_SPEFPUOP_ARITH2_32_32(efsmul);
GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
static always_inline void gen_efsabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7921
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7922 7923 7924 7925 7926 7927 7928
        return;
    }
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
}
static always_inline void gen_efsnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7929
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7930 7931 7932 7933 7934 7935 7936
        return;
    }
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
}
static always_inline void gen_efsneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7937
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7938 7939 7940 7941 7942
        return;
    }
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
}

7943
/* Conversion */
A
aurel32 已提交
7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955
GEN_SPEFPUOP_CONV_32_32(efscfui);
GEN_SPEFPUOP_CONV_32_32(efscfsi);
GEN_SPEFPUOP_CONV_32_32(efscfuf);
GEN_SPEFPUOP_CONV_32_32(efscfsf);
GEN_SPEFPUOP_CONV_32_32(efsctui);
GEN_SPEFPUOP_CONV_32_32(efsctsi);
GEN_SPEFPUOP_CONV_32_32(efsctuf);
GEN_SPEFPUOP_CONV_32_32(efsctsf);
GEN_SPEFPUOP_CONV_32_32(efsctuiz);
GEN_SPEFPUOP_CONV_32_32(efsctsiz);
GEN_SPEFPUOP_CONV_32_64(efscfd);

7956
/* Comparison */
A
aurel32 已提交
7957 7958 7959 7960 7961 7962
GEN_SPEFPUOP_COMP_32(efscmpgt);
GEN_SPEFPUOP_COMP_32(efscmplt);
GEN_SPEFPUOP_COMP_32(efscmpeq);
GEN_SPEFPUOP_COMP_32(efststgt);
GEN_SPEFPUOP_COMP_32(efststlt);
GEN_SPEFPUOP_COMP_32(efststeq);
7963 7964

/* Opcodes definitions */
7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPE_SINGLE); //
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPE_SINGLE); //
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPE_SINGLE); //
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPE_SINGLE); //
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPE_SINGLE); //
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPE_SINGLE); //
7979 7980 7981

/* Double precision floating-point operations */
/* Arithmetic */
A
aurel32 已提交
7982 7983 7984 7985 7986 7987 7988
GEN_SPEFPUOP_ARITH2_64_64(efdadd);
GEN_SPEFPUOP_ARITH2_64_64(efdsub);
GEN_SPEFPUOP_ARITH2_64_64(efdmul);
GEN_SPEFPUOP_ARITH2_64_64(efddiv);
static always_inline void gen_efdabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7989
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
#else
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
#endif
}
static always_inline void gen_efdnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
8001
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
#else
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}
static always_inline void gen_efdneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
8013
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
8014 8015 8016 8017 8018 8019 8020 8021 8022
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
#else
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}

8023
/* Conversion */
A
aurel32 已提交
8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038
GEN_SPEFPUOP_CONV_64_32(efdcfui);
GEN_SPEFPUOP_CONV_64_32(efdcfsi);
GEN_SPEFPUOP_CONV_64_32(efdcfuf);
GEN_SPEFPUOP_CONV_64_32(efdcfsf);
GEN_SPEFPUOP_CONV_32_64(efdctui);
GEN_SPEFPUOP_CONV_32_64(efdctsi);
GEN_SPEFPUOP_CONV_32_64(efdctuf);
GEN_SPEFPUOP_CONV_32_64(efdctsf);
GEN_SPEFPUOP_CONV_32_64(efdctuiz);
GEN_SPEFPUOP_CONV_32_64(efdctsiz);
GEN_SPEFPUOP_CONV_64_32(efdcfs);
GEN_SPEFPUOP_CONV_64_64(efdcfuid);
GEN_SPEFPUOP_CONV_64_64(efdcfsid);
GEN_SPEFPUOP_CONV_64_64(efdctuidz);
GEN_SPEFPUOP_CONV_64_64(efdctsidz);
8039 8040

/* Comparison */
A
aurel32 已提交
8041 8042 8043 8044 8045 8046
GEN_SPEFPUOP_COMP_64(efdcmpgt);
GEN_SPEFPUOP_COMP_64(efdcmplt);
GEN_SPEFPUOP_COMP_64(efdcmpeq);
GEN_SPEFPUOP_COMP_64(efdtstgt);
GEN_SPEFPUOP_COMP_64(efdtstlt);
GEN_SPEFPUOP_COMP_64(efdtsteq);
8047 8048

/* Opcodes definitions */
8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPE_DOUBLE); //
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPE_DOUBLE); //
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPE_DOUBLE); //
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPE_DOUBLE); //
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPE_DOUBLE); //
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPE_DOUBLE); //
8065

B
bellard 已提交
8066 8067 8068
/* End opcode list */
GEN_OPCODE_MARK(end);

8069
#include "translate_init.c"
8070
#include "helper_regs.h"
B
bellard 已提交
8071

8072
/*****************************************************************************/
8073
/* Misc PowerPC helpers */
8074 8075 8076
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
8077
{
8078 8079 8080
#define RGPL  4
#define RFPL  4

B
bellard 已提交
8081 8082
    int i;

J
j_mayer 已提交
8083
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
A
aurel32 已提交
8084
                env->nip, env->lr, env->ctr, env->xer);
8085 8086
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
8087
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
8088
    cpu_fprintf(f, "TB %08x %08x "
8089 8090 8091 8092
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
8093
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
8094 8095 8096 8097
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
8098
#endif
8099
    for (i = 0; i < 32; i++) {
8100 8101
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
8102
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
8103
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
8104
            cpu_fprintf(f, "\n");
8105
    }
8106
    cpu_fprintf(f, "CR ");
8107
    for (i = 0; i < 8; i++)
B
bellard 已提交
8108 8109
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
8110 8111 8112 8113 8114 8115 8116 8117
    for (i = 0; i < 8; i++) {
        char a = '-';
        if (env->crf[i] & 0x08)
            a = 'L';
        else if (env->crf[i] & 0x04)
            a = 'G';
        else if (env->crf[i] & 0x02)
            a = 'E';
B
bellard 已提交
8118
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
8119
    }
8120
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
8121 8122 8123
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
8124
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
8125
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
8126
            cpu_fprintf(f, "\n");
B
bellard 已提交
8127
    }
8128
    cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
8129
#if !defined(CONFIG_USER_ONLY)
8130
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
8131
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
8132
#endif
B
bellard 已提交
8133

8134 8135
#undef RGPL
#undef RFPL
B
bellard 已提交
8136 8137
}

8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184
void cpu_dump_statistics (CPUState *env, FILE*f,
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                          int flags)
{
#if defined(DO_PPC_STATISTICS)
    opc_handler_t **t1, **t2, **t3, *handler;
    int op1, op2, op3;

    t1 = env->opcodes;
    for (op1 = 0; op1 < 64; op1++) {
        handler = t1[op1];
        if (is_indirect_opcode(handler)) {
            t2 = ind_table(handler);
            for (op2 = 0; op2 < 32; op2++) {
                handler = t2[op2];
                if (is_indirect_opcode(handler)) {
                    t3 = ind_table(handler);
                    for (op3 = 0; op3 < 32; op3++) {
                        handler = t3[op3];
                        if (handler->count == 0)
                            continue;
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
                                    "%016llx %lld\n",
                                    op1, op2, op3, op1, (op3 << 5) | op2,
                                    handler->oname,
                                    handler->count, handler->count);
                    }
                } else {
                    if (handler->count == 0)
                        continue;
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
                                "%016llx %lld\n",
                                op1, op2, op1, op2, handler->oname,
                                handler->count, handler->count);
                }
            }
        } else {
            if (handler->count == 0)
                continue;
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
                        op1, op1, handler->oname,
                        handler->count, handler->count);
        }
    }
#endif
}

8185
/*****************************************************************************/
8186 8187 8188
static always_inline void gen_intermediate_code_internal (CPUState *env,
                                                          TranslationBlock *tb,
                                                          int search_pc)
B
bellard 已提交
8189
{
8190
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
8191
    opc_handler_t **table, *handler;
B
bellard 已提交
8192
    target_ulong pc_start;
B
bellard 已提交
8193
    uint16_t *gen_opc_end;
8194
    CPUBreakpoint *bp;
B
bellard 已提交
8195
    int j, lj = -1;
P
pbrook 已提交
8196 8197
    int num_insns;
    int max_insns;
B
bellard 已提交
8198 8199 8200

    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
8201
    ctx.nip = pc_start;
B
bellard 已提交
8202
    ctx.tb = tb;
8203
    ctx.exception = POWERPC_EXCP_NONE;
8204
    ctx.spr_cb = env->spr_cb;
A
aurel32 已提交
8205 8206 8207
    ctx.mem_idx = env->mmu_idx;
    ctx.access_type = -1;
    ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
8208 8209
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
8210
#endif
B
bellard 已提交
8211
    ctx.fpu_enabled = msr_fp;
8212
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
8213 8214 8215
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
8216 8217 8218 8219
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
8220
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
8221
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
8222
    else
8223
        ctx.singlestep_enabled = 0;
8224
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
8225 8226 8227
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
    if (unlikely(env->singlestep_enabled))
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
8228
#if defined (DO_SINGLE_STEP) && 0
8229 8230 8231
    /* Single step trace mode */
    msr_se = 1;
#endif
P
pbrook 已提交
8232 8233 8234 8235 8236 8237
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;

    gen_icount_start();
8238
    /* Set env in case of segfault during code fetch */
8239
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
8240 8241
        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
8242
                if (bp->pc == ctx.nip) {
A
aurel32 已提交
8243
                    gen_debug_exception(ctxp);
8244 8245 8246 8247
                    break;
                }
            }
        }
8248
        if (unlikely(search_pc)) {
B
bellard 已提交
8249 8250 8251 8252 8253
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8254
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
8255
                gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
8256
                gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8257 8258
            }
        }
8259 8260 8261
        LOG_DISAS("----------------\n");
        LOG_DISAS("nip=" ADDRX " super=%d ir=%d\n",
                  ctx.nip, ctx.mem_idx, (int)msr_ir);
P
pbrook 已提交
8262 8263
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();
A
aurel32 已提交
8264
        if (unlikely(ctx.le_mode)) {
8265 8266 8267
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
8268
        }
8269
        LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
8270
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
8271
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
8272
        ctx.nip += 4;
8273
        table = env->opcodes;
P
pbrook 已提交
8274
        num_insns++;
B
bellard 已提交
8275 8276 8277 8278 8279 8280 8281 8282 8283 8284
        handler = table[opc1(ctx.opcode)];
        if (is_indirect_opcode(handler)) {
            table = ind_table(handler);
            handler = table[opc2(ctx.opcode)];
            if (is_indirect_opcode(handler)) {
                table = ind_table(handler);
                handler = table[opc3(ctx.opcode)];
            }
        }
        /* Is opcode *REALLY* valid ? */
8285
        if (unlikely(handler->handler == &gen_invalid)) {
8286 8287 8288 8289 8290
            if (qemu_log_enabled()) {
                qemu_log("invalid/unsupported opcode: "
                          "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
                          opc1(ctx.opcode), opc2(ctx.opcode),
                          opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
8291 8292
            } else {
                printf("invalid/unsupported opcode: "
8293
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
B
bellard 已提交
8294
                       opc1(ctx.opcode), opc2(ctx.opcode),
8295
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
8296
            }
8297 8298
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
8299 8300 8301 8302 8303 8304
                if (qemu_log_enabled()) {
                    qemu_log("invalid bits: %08x for opcode: "
                              "%02x - %02x - %02x (%08x) " ADDRX "\n",
                              ctx.opcode & handler->inval, opc1(ctx.opcode),
                              opc2(ctx.opcode), opc3(ctx.opcode),
                              ctx.opcode, ctx.nip - 4);
8305 8306
                } else {
                    printf("invalid bits: %08x for opcode: "
8307
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
8308 8309
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
8310
                           ctx.opcode, ctx.nip - 4);
8311
                }
A
aurel32 已提交
8312
                gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
B
bellard 已提交
8313
                break;
B
bellard 已提交
8314 8315
            }
        }
B
bellard 已提交
8316
        (*(handler->handler))(&ctx);
8317 8318 8319
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
8320
        /* Check trace mode exceptions */
8321 8322 8323 8324 8325
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
                     ctx.exception != POWERPC_SYSCALL &&
                     ctx.exception != POWERPC_EXCP_TRAP &&
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
A
aurel32 已提交
8326
            gen_exception(ctxp, POWERPC_EXCP_TRACE);
8327
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
P
pbrook 已提交
8328 8329
                            (env->singlestep_enabled) ||
                            num_insns >= max_insns)) {
8330 8331 8332
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
8333
            break;
8334
        }
8335 8336 8337 8338
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
P
pbrook 已提交
8339 8340
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8341
    if (ctx.exception == POWERPC_EXCP_NONE) {
8342
        gen_goto_tb(&ctx, 0, ctx.nip);
8343
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
8344
        if (unlikely(env->singlestep_enabled)) {
A
aurel32 已提交
8345
            gen_debug_exception(ctxp);
8346
        }
8347
        /* Generate the return instruction */
B
bellard 已提交
8348
        tcg_gen_exit_tb(0);
8349
    }
P
pbrook 已提交
8350
    gen_icount_end(tb, num_insns);
B
bellard 已提交
8351
    *gen_opc_ptr = INDEX_op_end;
8352
    if (unlikely(search_pc)) {
8353 8354 8355 8356 8357
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
8358
        tb->size = ctx.nip - pc_start;
P
pbrook 已提交
8359
        tb->icount = num_insns;
8360
    }
8361
#if defined(DEBUG_DISAS)
8362 8363
    qemu_log_mask(CPU_LOG_TB_CPU, "---------------- excp: %04x\n", ctx.exception);
    log_cpu_state_mask(CPU_LOG_TB_CPU, env, 0);
8364
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
8365
        int flags;
8366
        flags = env->bfd_mach;
A
aurel32 已提交
8367
        flags |= ctx.le_mode << 16;
8368 8369 8370
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
        log_target_disas(pc_start, ctx.nip - pc_start, flags);
        qemu_log("\n");
8371
    }
B
bellard 已提交
8372 8373 8374
#endif
}

8375
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
8376
{
8377
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
8378 8379
}

8380
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
8381
{
8382
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
8383
}
A
aurel32 已提交
8384 8385 8386 8387 8388 8389

void gen_pc_load(CPUState *env, TranslationBlock *tb,
                unsigned long searched_pc, int pc_pos, void *puc)
{
    env->nip = gen_opc_pc[pc_pos];
}