translate.c 243.9 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 18 19
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
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"
A
aurel32 已提交
29
#include "helper.h"
B
bellard 已提交
30
#include "tcg-op.h"
31
#include "qemu-common.h"
B
bellard 已提交
32

33 34 35 36
#define CPU_SINGLE_STEP 0x1
#define CPU_BRANCH_STEP 0x2
#define GDBSTUB_SINGLE_STEP 0x4

37
/* Include definitions for instructions classes and implementations flags */
B
bellard 已提交
38
//#define DO_SINGLE_STEP
39
//#define PPC_DEBUG_DISAS
40
//#define DO_PPC_STATISTICS
41
//#define OPTIMIZE_FPRF_UPDATE
B
bellard 已提交
42

43 44
/*****************************************************************************/
/* Code translation helpers                                                  */
B
bellard 已提交
45

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

/* dyngen register indexes */
static TCGv cpu_T[3];
#if defined(TARGET_PPC64)
#define cpu_T64 cpu_T
#else
static TCGv cpu_T64[3];
#endif
A
aurel32 已提交
75
static TCGv cpu_FT[3];
76
static TCGv cpu_AVRh[3], cpu_AVRl[3];
P
pbrook 已提交
77 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
    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
A
aurel32 已提交
90 91 92 93 94 95 96 97 98 99 100 101
#if TARGET_LONG_BITS > HOST_LONG_BITS
    cpu_T[0] = tcg_global_mem_new(TCG_TYPE_TL,
                                  TCG_AREG0, offsetof(CPUState, t0), "T0");
    cpu_T[1] = tcg_global_mem_new(TCG_TYPE_TL,
                                  TCG_AREG0, offsetof(CPUState, t1), "T1");
    cpu_T[2] = tcg_global_mem_new(TCG_TYPE_TL,
                                  TCG_AREG0, offsetof(CPUState, t2), "T2");
#else
    cpu_T[0] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG1, "T0");
    cpu_T[1] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG2, "T1");
    cpu_T[2] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG3, "T2");
#endif
A
aurel32 已提交
102 103
#if !defined(TARGET_PPC64)
    cpu_T64[0] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
104
                                    TCG_AREG0, offsetof(CPUState, t0_64),
A
aurel32 已提交
105 106
                                    "T0_64");
    cpu_T64[1] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
107
                                    TCG_AREG0, offsetof(CPUState, t1_64),
A
aurel32 已提交
108 109
                                    "T1_64");
    cpu_T64[2] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
110
                                    TCG_AREG0, offsetof(CPUState, t2_64),
A
aurel32 已提交
111 112
                                    "T2_64");
#endif
A
aurel32 已提交
113 114 115 116 117 118 119 120

    cpu_FT[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                   offsetof(CPUState, ft0), "FT0");
    cpu_FT[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                   offsetof(CPUState, ft1), "FT1");
    cpu_FT[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                   offsetof(CPUState, ft2), "FT2");

121 122 123 124 125 126 127 128 129 130 131 132 133
    cpu_AVRh[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr0.u64[0]), "AVR0H");
    cpu_AVRl[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr0.u64[1]), "AVR0L");
    cpu_AVRh[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr1.u64[0]), "AVR1H");
    cpu_AVRl[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr1.u64[1]), "AVR1L");
    cpu_AVRh[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr2.u64[0]), "AVR2H");
    cpu_AVRl[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                     offsetof(CPUState, avr2.u64[1]), "AVR2L");

A
aurel32 已提交
134
    p = cpu_reg_names;
A
aurel32 已提交
135 136 137 138 139 140 141 142

    for (i = 0; i < 8; i++) {
        sprintf(p, "crf%d", i);
        cpu_crf[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
                                        offsetof(CPUState, crf[i]), p);
        p += 5;
    }

A
aurel32 已提交
143 144 145 146 147 148 149 150 151 152 153
    for (i = 0; i < 32; i++) {
        sprintf(p, "r%d", i);
        cpu_gpr[i] = tcg_global_mem_new(TCG_TYPE_TL, TCG_AREG0,
                                        offsetof(CPUState, gpr[i]), p);
        p += (i < 10) ? 3 : 4;
#if !defined(TARGET_PPC64)
        sprintf(p, "r%dH", i);
        cpu_gprh[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
                                         offsetof(CPUState, gprh[i]), p);
        p += (i < 10) ? 4 : 5;
#endif
154

A
aurel32 已提交
155 156 157
        sprintf(p, "fp%d", i);
        cpu_fpr[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                        offsetof(CPUState, fpr[i]), p);
A
aurel32 已提交
158
        p += (i < 10) ? 4 : 5;
A
aurel32 已提交
159

160 161 162 163
        sprintf(p, "avr%dH", i);
        cpu_avrh[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                         offsetof(CPUState, avr[i].u64[0]), p);
        p += (i < 10) ? 6 : 7;
A
aurel32 已提交
164

165 166 167 168
        sprintf(p, "avr%dL", i);
        cpu_avrl[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
                                         offsetof(CPUState, avr[i].u64[1]), p);
        p += (i < 10) ? 6 : 7;
A
aurel32 已提交
169
    }
A
aurel32 已提交
170

A
aurel32 已提交
171 172 173
    cpu_nip = tcg_global_mem_new(TCG_TYPE_TL, TCG_AREG0,
                                 offsetof(CPUState, nip), "nip");

A
aurel32 已提交
174 175 176 177 178 179
    cpu_ctr = tcg_global_mem_new(TCG_TYPE_TL, TCG_AREG0,
                                 offsetof(CPUState, ctr), "ctr");

    cpu_lr = tcg_global_mem_new(TCG_TYPE_TL, TCG_AREG0,
                                offsetof(CPUState, lr), "lr");

A
aurel32 已提交
180 181 182
    cpu_xer = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
                                 offsetof(CPUState, xer), "xer");

183 184 185
    cpu_fpscr = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
                                   offsetof(CPUState, fpscr), "fpscr");

A
aurel32 已提交
186 187 188 189 190
    /* register helpers */
#undef DEF_HELPER
#define DEF_HELPER(ret, name, params) tcg_register_helper(name, #name);
#include "helper.h"

P
pbrook 已提交
191 192 193
    done_init = 1;
}

194 195 196 197
#if defined(OPTIMIZE_FPRF_UPDATE)
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
static uint16_t **gen_fprf_ptr;
#endif
B
bellard 已提交
198 199 200 201

/* internal defines */
typedef struct DisasContext {
    struct TranslationBlock *tb;
B
bellard 已提交
202
    target_ulong nip;
B
bellard 已提交
203
    uint32_t opcode;
204
    uint32_t exception;
B
bellard 已提交
205 206 207
    /* Routine used to access memory */
    int mem_idx;
    /* Translation flags */
208
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
209
    int supervisor;
210 211 212
#endif
#if defined(TARGET_PPC64)
    int sf_mode;
213
#endif
B
bellard 已提交
214
    int fpu_enabled;
215
    int altivec_enabled;
216
    int spe_enabled;
217
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
218
    int singlestep_enabled;
219
    int dcache_line_size;
B
bellard 已提交
220 221
} DisasContext;

222
struct opc_handler_t {
B
bellard 已提交
223 224
    /* invalid bits */
    uint32_t inval;
225
    /* instruction type */
226
    uint64_t type;
B
bellard 已提交
227 228
    /* handler */
    void (*handler)(DisasContext *ctx);
229
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
230
    const char *oname;
231 232
#endif
#if defined(DO_PPC_STATISTICS)
233 234
    uint64_t count;
#endif
235
};
B
bellard 已提交
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static always_inline void gen_reset_fpstatus (void)
{
#ifdef CONFIG_SOFTFLOAT
    gen_op_reset_fpstatus();
#endif
}

static always_inline void gen_compute_fprf (int set_fprf, int set_rc)
{
    if (set_fprf != 0) {
        /* This case might be optimized later */
#if defined(OPTIMIZE_FPRF_UPDATE)
        *gen_fprf_ptr++ = gen_opc_ptr;
#endif
        gen_op_compute_fprf(1);
        if (unlikely(set_rc))
A
aurel32 已提交
253
            tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
254 255 256 257
        gen_op_float_check_status();
    } else if (unlikely(set_rc)) {
        /* We always need to compute fpcc */
        gen_op_compute_fprf(0);
A
aurel32 已提交
258
        tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        if (set_fprf)
            gen_op_float_check_status();
    }
}

static always_inline void gen_optimize_fprf (void)
{
#if defined(OPTIMIZE_FPRF_UPDATE)
    uint16_t **ptr;

    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
        *ptr = INDEX_op_nop1;
    gen_fprf_ptr = gen_fprf_buf;
#endif
}

275
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
276 277 278
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
A
aurel32 已提交
279
        tcg_gen_movi_tl(cpu_nip, nip);
280 281
    else
#endif
A
aurel32 已提交
282
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
283 284
}

285
#define GEN_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
286
do {                                                                          \
287
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
288
        gen_update_nip(ctx, (ctx)->nip);                                      \
289 290 291
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
292 293
} while (0)

294 295 296
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
297

298 299 300
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
301

302 303 304 305 306 307 308 309 310
#define GEN_EXCP_PRIVREG(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)

#define GEN_EXCP_NO_FP(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)

#define GEN_EXCP_NO_AP(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
311

312 313 314
#define GEN_EXCP_NO_VR(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)

315
/* Stop translation */
316
static always_inline void GEN_STOP (DisasContext *ctx)
317
{
318
    gen_update_nip(ctx, ctx->nip);
319
    ctx->exception = POWERPC_EXCP_STOP;
320 321
}

322
/* No need to update nip here, as execution flow will change */
323
static always_inline void GEN_SYNC (DisasContext *ctx)
324
{
325
    ctx->exception = POWERPC_EXCP_SYNC;
326 327
}

B
bellard 已提交
328 329 330 331 332
#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)

333 334 335 336 337
#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 已提交
338 339
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
T
ths 已提交
340
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
341 342 343 344
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
345
    opc_handler_t handler;
346
    const char *oname;
B
bellard 已提交
347 348
} opcode_t;

349
/*****************************************************************************/
B
bellard 已提交
350 351
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
352
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
353 354 355 356 357
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
358
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
359
{                                                                             \
360
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
}

/* 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 */
388
EXTRACT_HELPER(_SPR, 11, 10);
389
static always_inline uint32_t SPR (uint32_t opcode)
390 391 392 393 394
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408
/***                              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);
/* Bit count */
EXTRACT_HELPER(NB, 11, 5);
/* Shift count */
EXTRACT_HELPER(SH, 11, 5);
/* Mask start */
EXTRACT_HELPER(MB, 6, 5);
/* Mask end */
EXTRACT_HELPER(ME, 1, 5);
B
bellard 已提交
409 410
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
411 412 413 414

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

B
bellard 已提交
417 418 419 420
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
421
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
422 423 424 425
{
    return (opcode >> 0) & 0x03FFFFFC;
}

426
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
427 428 429 430 431 432 433 434 435 436 437 438
{
    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 */
439
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
440
{
441
    target_ulong ret;
B
bellard 已提交
442

443 444
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
445
        ret = UINT64_MAX << (63 - end);
446
    } else if (likely(end == 63)) {
447
        ret = UINT64_MAX >> start;
448 449 450
    }
#else
    if (likely(start == 0)) {
451
        ret = UINT32_MAX << (31  - end);
452
    } else if (likely(end == 31)) {
453
        ret = UINT32_MAX >> start;
454 455 456 457 458 459 460 461
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
462 463 464 465

    return ret;
}

466 467 468
/*****************************************************************************/
/* PowerPC Instructions types definitions                                    */
enum {
469
    PPC_NONE           = 0x0000000000000000ULL,
470
    /* PowerPC base instructions set                                         */
471 472
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
    /*   integer operations instructions                                     */
473
#define PPC_INTEGER PPC_INSNS_BASE
474
    /*   flow control instructions                                           */
475
#define PPC_FLOW    PPC_INSNS_BASE
476
    /*   virtual memory instructions                                         */
477
#define PPC_MEM     PPC_INSNS_BASE
478
    /*   ld/st with reservation instructions                                 */
479
#define PPC_RES     PPC_INSNS_BASE
480
    /*   spr/msr access instructions                                         */
481
#define PPC_MISC    PPC_INSNS_BASE
482 483
    /* Deprecated instruction sets                                           */
    /*   Original POWER instruction set                                      */
484
    PPC_POWER          = 0x0000000000000002ULL,
485
    /*   POWER2 instruction set extension                                    */
486
    PPC_POWER2         = 0x0000000000000004ULL,
487
    /*   Power RTC support                                                   */
488
    PPC_POWER_RTC      = 0x0000000000000008ULL,
489
    /*   Power-to-PowerPC bridge (601)                                       */
490
    PPC_POWER_BR       = 0x0000000000000010ULL,
491
    /* 64 bits PowerPC instruction set                                       */
492
    PPC_64B            = 0x0000000000000020ULL,
493
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
494
    PPC_64BX           = 0x0000000000000040ULL,
495
    /*   64 bits hypervisor extensions                                       */
496
    PPC_64H            = 0x0000000000000080ULL,
497
    /*   New wait instruction (PowerPC 2.0x)                                 */
498
    PPC_WAIT           = 0x0000000000000100ULL,
499
    /*   Time base mftb instruction                                          */
500
    PPC_MFTB           = 0x0000000000000200ULL,
501 502 503

    /* Fixed-point unit extensions                                           */
    /*   PowerPC 602 specific                                                */
504
    PPC_602_SPEC       = 0x0000000000000400ULL,
505 506 507 508 509 510
    /*   isel instruction                                                    */
    PPC_ISEL           = 0x0000000000000800ULL,
    /*   popcntb instruction                                                 */
    PPC_POPCNTB        = 0x0000000000001000ULL,
    /*   string load / store                                                 */
    PPC_STRING         = 0x0000000000002000ULL,
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

    /* 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                                          */
528
    PPC_SPE            = 0x0000000002000000ULL,
529
    /*   PowerPC 2.03 SPE floating-point extension                           */
530
    PPC_SPEFPU         = 0x0000000004000000ULL,
531

532
    /* Optional memory control instructions                                  */
533 534 535 536 537 538 539 540 541
    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                                            */
542
    PPC_CACHE          = 0x0000000200000000ULL,
543
    /*   icbi instruction                                                    */
544
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
545
    /*   dcbz instruction with fixed cache line size                         */
546
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
547
    /*   dcbz instruction with tunable cache line size                       */
548
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
549
    /*   dcba instruction                                                    */
550 551 552
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
    /*   Freescale cache locking instructions                                */
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
553 554 555

    /* MMU related extensions                                                */
    /*   external control instructions                                       */
556
    PPC_EXTERN         = 0x0000010000000000ULL,
557
    /*   segment register access instructions                                */
558
    PPC_SEGMENT        = 0x0000020000000000ULL,
559
    /*   PowerPC 6xx TLB management instructions                             */
560
    PPC_6xx_TLB        = 0x0000040000000000ULL,
561
    /* PowerPC 74xx TLB management instructions                              */
562
    PPC_74xx_TLB       = 0x0000080000000000ULL,
563
    /*   PowerPC 40x TLB management instructions                             */
564
    PPC_40x_TLB        = 0x0000100000000000ULL,
565
    /*   segment register access instructions for PowerPC 64 "bridge"        */
566
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
567
    /*   SLB management                                                      */
568
    PPC_SLBI           = 0x0000400000000000ULL,
569

570
    /* Embedded PowerPC dedicated instructions                               */
571
    PPC_WRTEE          = 0x0001000000000000ULL,
572
    /* PowerPC 40x exception model                                           */
573
    PPC_40x_EXCP       = 0x0002000000000000ULL,
574
    /* PowerPC 405 Mac instructions                                          */
575
    PPC_405_MAC        = 0x0004000000000000ULL,
576
    /* PowerPC 440 specific instructions                                     */
577
    PPC_440_SPEC       = 0x0008000000000000ULL,
578
    /* BookE (embedded) PowerPC specification                                */
579 580 581 582 583 584 585
    PPC_BOOKE          = 0x0010000000000000ULL,
    /* mfapidi instruction                                                   */
    PPC_MFAPIDI        = 0x0020000000000000ULL,
    /* tlbiva instruction                                                    */
    PPC_TLBIVA         = 0x0040000000000000ULL,
    /* tlbivax instruction                                                   */
    PPC_TLBIVAX        = 0x0080000000000000ULL,
586
    /* PowerPC 4xx dedicated instructions                                    */
587
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
588
    /* PowerPC 40x ibct instructions                                         */
589
    PPC_40x_ICBT       = 0x0200000000000000ULL,
590
    /* rfmci is not implemented in all BookE PowerPC                         */
591 592 593 594 595 596 597
    PPC_RFMCI          = 0x0400000000000000ULL,
    /* rfdi instruction                                                      */
    PPC_RFDI           = 0x0800000000000000ULL,
    /* DCR accesses                                                          */
    PPC_DCR            = 0x1000000000000000ULL,
    /* DCR extended accesse                                                  */
    PPC_DCRX           = 0x2000000000000000ULL,
598
    /* user-mode DCR access, implemented in PowerPC 460                      */
599
    PPC_DCRUX          = 0x4000000000000000ULL,
600 601 602 603
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
604 605 606 607 608
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
609
#if defined(__APPLE__)
610
#define OPCODES_SECTION                                                       \
611
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
612
#else
613
#define OPCODES_SECTION                                                       \
614
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
615 616
#endif

617
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
618
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
619
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
620 621 622
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
623
    .pad  = { 0, },                                                           \
B
bellard 已提交
624 625
    .handler = {                                                              \
        .inval   = invl,                                                      \
626
        .type = _typ,                                                         \
B
bellard 已提交
627
        .handler = &gen_##name,                                               \
628
        .oname = stringify(name),                                             \
B
bellard 已提交
629
    },                                                                        \
630
    .oname = stringify(name),                                                 \
B
bellard 已提交
631
}
632 633 634 635 636 637 638 639 640 641 642 643 644 645
#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,                                                            \
}
646 647 648 649 650 651 652 653 654 655 656 657 658 659
#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),                                                 \
}
660 661 662 663 664 665 666 667 668 669 670 671 672
#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,                                                            \
}
673
#endif
B
bellard 已提交
674 675

#define GEN_OPCODE_MARK(name)                                                 \
676
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
677 678 679
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
680
    .pad  = { 0, },                                                           \
B
bellard 已提交
681 682
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
683
        .type = 0x00,                                                         \
B
bellard 已提交
684 685
        .handler = NULL,                                                      \
    },                                                                        \
686
    .oname = stringify(name),                                                 \
B
bellard 已提交
687 688 689 690 691 692
}

/* Start opcode list */
GEN_OPCODE_MARK(start);

/* Invalid instruction */
693 694
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
695
    GEN_EXCP_INVAL(ctx);
696 697
}

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

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 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 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
/***                           Integer comparison                          ***/

static always_inline void gen_op_cmp(TCGv t0, TCGv t1, int s, int crf)
{
    int l1, l2, l3;

    tcg_gen_shri_i32(cpu_crf[crf], cpu_xer, XER_SO);
    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) {
        tcg_gen_brcond_tl(TCG_COND_LT, t0, t1, l1);
        tcg_gen_brcond_tl(TCG_COND_GT, t0, t1, l2);
    } else {
        tcg_gen_brcond_tl(TCG_COND_LTU, t0, t1, l1);
        tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l2);
    }
    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);
}

static always_inline void gen_op_cmpi(TCGv t0, target_ulong t1, int s, int crf)
{
    TCGv temp = tcg_const_local_tl(t1);
    gen_op_cmp(t0, temp, s, crf);
    tcg_temp_free(temp);
}

#if defined(TARGET_PPC64)
static always_inline void gen_op_cmp32(TCGv t0, TCGv t1, int s, int crf)
{
    TCGv t0_32, t1_32;
    t0_32 = tcg_temp_local_new(TCG_TYPE_TL);
    t1_32 = tcg_temp_local_new(TCG_TYPE_TL);
    if (s) {
        tcg_gen_ext32s_tl(t0_32, t0);
        tcg_gen_ext32s_tl(t1_32, t1);
    } else {
        tcg_gen_ext32u_tl(t0_32, t0);
        tcg_gen_ext32u_tl(t1_32, t1);
    }
    gen_op_cmp(t0_32, t1_32, s, crf);
    tcg_temp_free(t1_32);
    tcg_temp_free(t0_32);
}

static always_inline void gen_op_cmpi32(TCGv t0, target_ulong t1, int s, int crf)
{
    TCGv temp = tcg_const_local_tl(t1);
    gen_op_cmp32(t0, temp, s, crf);
    tcg_temp_free(temp);
}
#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;
    TCGv temp;

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

    mask = 1 << (3 - (bi & 0x03));
    temp = tcg_temp_new(TCG_TYPE_I32);
    tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
    tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
    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);
}

B
bellard 已提交
853
/***                           Integer arithmetic                          ***/
854 855
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
856
{                                                                             \
A
aurel32 已提交
857 858
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
B
bellard 已提交
859
    gen_op_##name();                                                          \
A
aurel32 已提交
860
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
861
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
862
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
B
bellard 已提交
863 864
}

865 866
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
867
{                                                                             \
A
aurel32 已提交
868 869
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
B
bellard 已提交
870
    gen_op_##name();                                                          \
A
aurel32 已提交
871
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
872
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
873
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
B
bellard 已提交
874 875
}

876 877
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
878
{                                                                             \
A
aurel32 已提交
879
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
B
bellard 已提交
880
    gen_op_##name();                                                          \
A
aurel32 已提交
881
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
882
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
883
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
B
bellard 已提交
884
}
885 886
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
887
{                                                                             \
A
aurel32 已提交
888
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
B
bellard 已提交
889
    gen_op_##name();                                                          \
A
aurel32 已提交
890
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
891
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
892
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
B
bellard 已提交
893 894 895
}

/* Two operands arithmetic functions */
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)

/* Two operands arithmetic functions with no overflow allowed */
#define GEN_INT_ARITHN(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000400, type)

/* One operand arithmetic functions */
#define GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                                \
__GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10, type)

#if defined(TARGET_PPC64)
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
A
aurel32 已提交
913 914
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
915 916 917 918
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
919
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
920
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
921
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
922 923 924 925 926
}

#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
A
aurel32 已提交
927 928
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
929 930 931 932
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
933
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
934
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
935
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
936 937 938 939 940
}

#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
A
aurel32 已提交
941
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
942 943 944 945
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
946
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
947
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
948
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
949 950 951 952
}
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
A
aurel32 已提交
953
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
954 955 956 957
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
958
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
959
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
960
        gen_set_Rc0(ctx, cpu_T[0]);                                           \
961 962 963 964 965 966
}

/* Two operands arithmetic functions */
#define GEN_INT_ARITH2_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000000, type)                 \
__GEN_INT_ARITH2_O_64(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
B
bellard 已提交
967 968

/* Two operands arithmetic functions with no overflow allowed */
969 970
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
971 972

/* One operand arithmetic functions */
973 974 975 976 977 978 979 980
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
#else
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
#endif
B
bellard 已提交
981 982

/* add    add.    addo    addo.    */
A
aurel32 已提交
983 984 985 986
static always_inline void gen_op_add (void)
{
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}
987
static always_inline void gen_op_addo (void)
988
{
989
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
990
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
991 992 993 994
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
995
static always_inline void gen_op_addo_64 (void)
996
{
997
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
998
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
999 1000 1001 1002
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
B
bellard 已提交
1003
/* addc   addc.   addco   addco.   */
1004
static always_inline void gen_op_addc (void)
1005
{
1006
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1007
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1008 1009
    gen_op_check_addc();
}
1010
static always_inline void gen_op_addco (void)
1011
{
1012
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1013
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1014 1015 1016 1017
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
1018
static always_inline void gen_op_addc_64 (void)
1019
{
1020
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1021
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1022 1023
    gen_op_check_addc_64();
}
1024
static always_inline void gen_op_addco_64 (void)
1025
{
1026
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1027
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1028 1029 1030 1031 1032
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
B
bellard 已提交
1033
/* adde   adde.   addeo   addeo.   */
1034
static always_inline void gen_op_addeo (void)
1035
{
1036
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1037 1038 1039 1040
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
1041
static always_inline void gen_op_addeo_64 (void)
1042
{
1043
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1044 1045 1046 1047 1048
    gen_op_adde_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
B
bellard 已提交
1049
/* addme  addme.  addmeo  addmeo.  */
1050
static always_inline void gen_op_addme (void)
1051
{
1052
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1053 1054 1055
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
1056
static always_inline void gen_op_addme_64 (void)
1057
{
1058
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1059 1060 1061 1062
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
1063
/* addze  addze.  addzeo  addzeo.  */
1064
static always_inline void gen_op_addze (void)
1065
{
1066
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1067 1068 1069
    gen_op_add_ze();
    gen_op_check_addc();
}
1070
static always_inline void gen_op_addzeo (void)
1071
{
1072
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1073 1074 1075 1076 1077
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
1078
static always_inline void gen_op_addze_64 (void)
1079
{
1080
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1081 1082 1083
    gen_op_add_ze();
    gen_op_check_addc_64();
}
1084
static always_inline void gen_op_addzeo_64 (void)
1085
{
1086
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1087 1088 1089 1090 1091 1092
    gen_op_add_ze();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
B
bellard 已提交
1093
/* divw   divw.   divwo   divwo.   */
1094
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
1095
/* divwu  divwu.  divwuo  divwuo.  */
1096
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
1097
/* mulhw  mulhw.                   */
1098
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
1099
/* mulhwu mulhwu.                  */
1100
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
1101
/* mullw  mullw.  mullwo  mullwo.  */
1102
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
1103
/* neg    neg.    nego    nego.    */
1104
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
1105
/* subf   subf.   subfo   subfo.   */
A
aurel32 已提交
1106 1107 1108 1109
static always_inline void gen_op_subf (void)
{
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
}
1110
static always_inline void gen_op_subfo (void)
1111
{
A
aurel32 已提交
1112
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1113
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1114
    gen_op_check_addo();
1115 1116 1117
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
1118
static always_inline void gen_op_subfo_64 (void)
1119
{
A
aurel32 已提交
1120
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1121
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1122
    gen_op_check_addo_64();
1123 1124 1125
}
#endif
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
B
bellard 已提交
1126
/* subfc  subfc.  subfco  subfco.  */
1127
static always_inline void gen_op_subfc (void)
1128
{
A
aurel32 已提交
1129
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1130 1131
    gen_op_check_subfc();
}
1132
static always_inline void gen_op_subfco (void)
1133
{
A
aurel32 已提交
1134
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1135
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1136
    gen_op_check_subfc();
1137
    gen_op_check_addo();
1138 1139
}
#if defined(TARGET_PPC64)
1140
static always_inline void gen_op_subfc_64 (void)
1141
{
A
aurel32 已提交
1142
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1143 1144
    gen_op_check_subfc_64();
}
1145
static always_inline void gen_op_subfco_64 (void)
1146
{
A
aurel32 已提交
1147
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1148
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1149
    gen_op_check_subfc_64();
1150
    gen_op_check_addo_64();
1151 1152 1153
}
#endif
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
B
bellard 已提交
1154
/* subfe  subfe.  subfeo  subfeo.  */
1155
static always_inline void gen_op_subfeo (void)
1156
{
A
aurel32 已提交
1157
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
1158
    gen_op_subfe();
1159
    gen_op_check_addo();
1160 1161 1162
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
1163
static always_inline void gen_op_subfeo_64 (void)
1164
{
A
aurel32 已提交
1165
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
1166
    gen_op_subfe_64();
1167
    gen_op_check_addo_64();
1168 1169 1170
}
#endif
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
B
bellard 已提交
1171
/* subfme subfme. subfmeo subfmeo. */
1172
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
1173
/* subfze subfze. subfzeo subfzeo. */
1174
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
1175 1176 1177
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1178
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
1179 1180

    if (rA(ctx->opcode) == 0) {
1181
        /* li case */
A
aurel32 已提交
1182
        tcg_gen_movi_tl(cpu_T[0], simm);
B
bellard 已提交
1183
    } else {
A
aurel32 已提交
1184
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1185
        if (likely(simm != 0))
A
aurel32 已提交
1186
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
B
bellard 已提交
1187
    }
A
aurel32 已提交
1188
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1189 1190 1191 1192
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1193 1194
    target_long simm = SIMM(ctx->opcode);

A
aurel32 已提交
1195
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1196
    if (likely(simm != 0)) {
1197
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1198
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
1199 1200 1201 1202 1203 1204
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
1205
    } else {
A
aurel32 已提交
1206
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
1207
    }
A
aurel32 已提交
1208
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1209 1210
}
/* addic. */
1211
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1212
{
1213 1214
    target_long simm = SIMM(ctx->opcode);

A
aurel32 已提交
1215
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1216
    if (likely(simm != 0)) {
1217
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1218
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
1219 1220 1221 1222 1223 1224
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
1225
    } else {
A
aurel32 已提交
1226
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
1227
    }
A
aurel32 已提交
1228
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1229
    gen_set_Rc0(ctx, cpu_T[0]);
B
bellard 已提交
1230 1231 1232 1233
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1234
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
1235 1236

    if (rA(ctx->opcode) == 0) {
1237
        /* lis case */
A
aurel32 已提交
1238
        tcg_gen_movi_tl(cpu_T[0], simm << 16);
B
bellard 已提交
1239
    } else {
A
aurel32 已提交
1240
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1241
        if (likely(simm != 0))
A
aurel32 已提交
1242
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm << 16);
B
bellard 已提交
1243
    }
A
aurel32 已提交
1244
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1245 1246 1247 1248
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
1249
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1250
    gen_op_mulli(SIMM(ctx->opcode));
A
aurel32 已提交
1251
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1252 1253 1254 1255
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
1256
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1257 1258 1259 1260 1261 1262
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_subfic_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_subfic(SIMM(ctx->opcode));
A
aurel32 已提交
1263
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1264 1265
}

1266 1267
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
1268
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1269
/* mulhdu mulhdu.                  */
1270
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1271
/* mulld  mulld.  mulldo  mulldo.  */
1272
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1273
/* divd   divd.   divdo   divdo.   */
1274
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1275
/* divdu  divdu.  divduo  divduo.  */
1276
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1277 1278
#endif

B
bellard 已提交
1279
/***                            Integer logical                            ***/
1280 1281
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
B
bellard 已提交
1282
{                                                                             \
1283 1284
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
       cpu_gpr[rB(ctx->opcode)]);                                             \
1285
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1286
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1287 1288
}

1289
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1290
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1291
{                                                                             \
1292
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1293
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1294
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1295 1296 1297
}

/* and & and. */
1298
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
B
bellard 已提交
1299
/* andc & andc. */
1300
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
B
bellard 已提交
1301
/* andi. */
1302
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1303
{
1304 1305
    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 已提交
1306 1307
}
/* andis. */
1308
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1309
{
1310 1311
    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 已提交
1312 1313
}
/* cntlzw */
1314 1315 1316 1317
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
{
    tcg_gen_helper_1_1(helper_cntlzw, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    if (unlikely(Rc(ctx->opcode) != 0))
P
pbrook 已提交
1318
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1319
}
B
bellard 已提交
1320
/* eqv & eqv. */
1321
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
B
bellard 已提交
1322
/* extsb & extsb. */
1323
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
B
bellard 已提交
1324
/* extsh & extsh. */
1325
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
B
bellard 已提交
1326
/* nand & nand. */
1327
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
B
bellard 已提交
1328
/* nor & nor. */
1329
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
B
bellard 已提交
1330
/* or & or. */
1331 1332
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1333 1334 1335 1336 1337 1338 1339
    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) {
1340 1341 1342 1343
        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]);
1344
        if (unlikely(Rc(ctx->opcode) != 0))
1345
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1346
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1347
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1348 1349
#if defined(TARGET_PPC64)
    } else {
1350 1351
        int prio = 0;

1352 1353 1354
        switch (rs) {
        case 1:
            /* Set process priority to low */
1355
            prio = 2;
1356 1357 1358
            break;
        case 6:
            /* Set process priority to medium-low */
1359
            prio = 3;
1360 1361 1362
            break;
        case 2:
            /* Set process priority to normal */
1363
            prio = 4;
1364
            break;
1365 1366 1367 1368
#if !defined(CONFIG_USER_ONLY)
        case 31:
            if (ctx->supervisor > 0) {
                /* Set process priority to very low */
1369
                prio = 1;
1370 1371 1372 1373 1374
            }
            break;
        case 5:
            if (ctx->supervisor > 0) {
                /* Set process priority to medium-hight */
1375
                prio = 5;
1376 1377 1378 1379 1380
            }
            break;
        case 3:
            if (ctx->supervisor > 0) {
                /* Set process priority to high */
1381
                prio = 6;
1382 1383 1384 1385 1386
            }
            break;
        case 7:
            if (ctx->supervisor > 1) {
                /* Set process priority to very high */
1387
                prio = 7;
1388 1389 1390
            }
            break;
#endif
1391 1392 1393 1394
        default:
            /* nop */
            break;
        }
1395 1396 1397 1398 1399 1400 1401 1402
        if (prio) {
            TCGv temp = tcg_temp_new(TCG_TYPE_TL);
            tcg_gen_ld_tl(temp, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
            tcg_gen_andi_tl(temp, temp, ~0x001C000000000000ULL);
            tcg_gen_ori_tl(temp, temp, ((uint64_t)prio) << 50);
            tcg_gen_st_tl(temp, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
            tcg_temp_free(temp);
        }
1403
#endif
1404 1405
    }
}
B
bellard 已提交
1406
/* orc & orc. */
1407
GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
B
bellard 已提交
1408
/* xor & xor. */
1409 1410 1411
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    /* Optimisation for "set to zero" case */
1412
    if (rS(ctx->opcode) != rB(ctx->opcode))
A
aurel32 已提交
1413
        tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1414 1415
    else
        tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1416
    if (unlikely(Rc(ctx->opcode) != 0))
1417
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1418
}
B
bellard 已提交
1419 1420 1421
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1422
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1423

1424 1425
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1426
        /* XXX: should handle special NOPs for POWER series */
1427
        return;
1428
    }
1429
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1430 1431 1432 1433
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1434
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1435

1436 1437 1438
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1439
    }
1440
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1441 1442 1443 1444
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1445
    target_ulong uimm = UIMM(ctx->opcode);
1446 1447 1448 1449 1450

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1451
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1452 1453 1454 1455
}
/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1456
    target_ulong uimm = UIMM(ctx->opcode);
1457 1458 1459 1460 1461

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1462
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1463
}
1464
/* popcntb : PowerPC 2.03 specification */
1465
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1466 1467 1468
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
1469
        tcg_gen_helper_1_1(helper_popcntb_64, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1470 1471
    else
#endif
1472
        tcg_gen_helper_1_1(helper_popcntb, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1473 1474 1475 1476
}

#if defined(TARGET_PPC64)
/* extsw & extsw. */
1477
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1478
/* cntlzd */
1479 1480 1481 1482 1483 1484
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
{
    tcg_gen_helper_1_1(helper_cntlzd, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1485 1486
#endif

B
bellard 已提交
1487 1488 1489 1490
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1491
    uint32_t mb, me, sh;
B
bellard 已提交
1492 1493 1494

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1495
    sh = SH(ctx->opcode);
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
    if (likely(sh == 0 && mb == 0 && me == 31)) {
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    } else {
        TCGv t0, t1;
        target_ulong mask;

        t0 = tcg_temp_new(TCG_TYPE_TL);
        t1 = tcg_temp_new(TCG_TYPE_TL);
        if (likely(sh == 0)) {
            tcg_gen_mov_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        } else {
            tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_shli_tl(t0, t1, sh);
            tcg_gen_shri_tl(t1, t1, 32 - sh);
            tcg_gen_or_tl(t0, t0, t1);
1511 1512
        }
#if defined(TARGET_PPC64)
1513 1514
        mb += 32;
        me += 32;
1515
#endif
1516 1517 1518 1519 1520 1521 1522
        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);
    }
1523
    if (unlikely(Rc(ctx->opcode) != 0))
1524
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1525 1526 1527 1528 1529
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1530

B
bellard 已提交
1531 1532 1533
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

    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 {
            TCGv t0 = tcg_temp_new(TCG_TYPE_TL);
            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 已提交
1544
        }
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
    } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
        TCGv t0 = tcg_temp_new(TCG_TYPE_TL);
        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 {
        TCGv t0 = tcg_temp_new(TCG_TYPE_TL);
        if (likely(sh != 0)) {
            TCGv t1 = tcg_temp_new(TCG_TYPE_TL);
            tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_shli_tl(t1, t0, sh);
            tcg_gen_shri_tl(t0, t0, 32 - sh);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_temp_free(t1);
        } else {
            tcg_gen_mov_tl(t0, cpu_gpr[rS(ctx->opcode)]);
B
bellard 已提交
1562
        }
1563
#if defined(TARGET_PPC64)
1564 1565
        mb += 32;
        me += 32;
1566
#endif
1567 1568 1569
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
        tcg_temp_free(t0);
    }
1570
    if (unlikely(Rc(ctx->opcode) != 0))
1571
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1572 1573 1574 1575 1576
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;
1577
    TCGv t0, t1, t2, t3;
B
bellard 已提交
1578 1579 1580

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    t0 = tcg_temp_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
    t1 = tcg_temp_new(TCG_TYPE_TL);
    tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
    t2 = tcg_temp_new(TCG_TYPE_TL);
    tcg_gen_shl_tl(t2, t1, t0);
    t3 = tcg_const_tl(32);
    tcg_gen_sub_tl(t0, t3, t0);
    tcg_temp_free(t3);
    tcg_gen_shr_tl(t1, t1, t0);
    tcg_temp_free(t0);
    tcg_gen_or_tl(t2, t2, t1);
    tcg_temp_free(t1);
1594 1595 1596 1597 1598
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
1599 1600 1601
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t2, MASK(mb, me));
    } else {
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t2);
B
bellard 已提交
1602
    }
1603
    tcg_temp_free(t2);
1604
    if (unlikely(Rc(ctx->opcode) != 0))
1605
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1606 1607
}

1608 1609
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1610
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1611 1612 1613
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1614 1615
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1616 1617 1618 1619
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1620
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1621 1622 1623
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1624 1625
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1626 1627 1628
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1629 1630
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1631 1632 1633
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1634 1635
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1636 1637 1638
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1639

1640 1641
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1642
{
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    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 {
        TCGv t0 = tcg_temp_new(TCG_TYPE_TL);
        if (likely(sh != 0)) {
            TCGv t1 = tcg_temp_new(TCG_TYPE_TL);
            tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
            tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 64 - sh);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_temp_free(t1);
        } else {
            tcg_gen_mov_tl(t0, cpu_gpr[rS(ctx->opcode)]);
J
j_mayer 已提交
1657
        }
1658 1659 1660 1661
        if (likely(mb == 0 && me == 63)) {
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
        } else {
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
J
j_mayer 已提交
1662
        }
1663
        tcg_temp_free(t0);
J
j_mayer 已提交
1664 1665
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1666
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
J
j_mayer 已提交
1667
}
1668
/* rldicl - rldicl. */
1669
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1670
{
J
j_mayer 已提交
1671
    uint32_t sh, mb;
1672

J
j_mayer 已提交
1673 1674
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1675
    gen_rldinm(ctx, mb, 63, sh);
1676
}
J
j_mayer 已提交
1677
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1678
/* rldicr - rldicr. */
1679
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1680
{
J
j_mayer 已提交
1681
    uint32_t sh, me;
1682

J
j_mayer 已提交
1683 1684
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1685
    gen_rldinm(ctx, 0, me, sh);
1686
}
J
j_mayer 已提交
1687
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1688
/* rldic - rldic. */
1689
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1690
{
J
j_mayer 已提交
1691
    uint32_t sh, mb;
1692

J
j_mayer 已提交
1693 1694
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1695 1696 1697 1698
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1699 1700
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1701
{
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
    TCGv t0, t1, t2;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    t0 = tcg_temp_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
    t1 = tcg_temp_new(TCG_TYPE_TL);
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t0);
    t2 = tcg_const_tl(32);
    tcg_gen_sub_tl(t0, t2, t0);
    tcg_temp_free(t2);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    tcg_gen_or_tl(t1, t1, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
1716
    if (unlikely(mb != 0 || me != 63)) {
1717 1718 1719 1720
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t1, MASK(mb, me));
    } else
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
    tcg_temp_free(t1);
J
j_mayer 已提交
1721
    if (unlikely(Rc(ctx->opcode) != 0))
1722
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1723
}
J
j_mayer 已提交
1724

1725
/* rldcl - rldcl. */
1726
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1727
{
J
j_mayer 已提交
1728
    uint32_t mb;
1729

J
j_mayer 已提交
1730
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1731
    gen_rldnm(ctx, mb, 63);
1732
}
1733
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1734
/* rldcr - rldcr. */
1735
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1736
{
J
j_mayer 已提交
1737
    uint32_t me;
1738

J
j_mayer 已提交
1739
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1740
    gen_rldnm(ctx, 0, me);
1741
}
1742
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1743
/* rldimi - rldimi. */
1744
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1745
{
1746
    uint32_t sh, mb, me;
1747

J
j_mayer 已提交
1748 1749
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1750
    me = 63 - sh;
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    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;

        t0 = tcg_temp_new(TCG_TYPE_TL);
        t1 = tcg_temp_new(TCG_TYPE_TL);
        if (likely(sh == 0)) {
            tcg_gen_mov_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        } else {
            tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
            tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 64 - sh);
            tcg_gen_or_tl(t0, t0, t1);
J
j_mayer 已提交
1765
        }
1766 1767 1768 1769 1770 1771
        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 已提交
1772 1773
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1774
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1775
}
1776
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1777 1778
#endif

B
bellard 已提交
1779 1780
/***                             Integer shift                             ***/
/* slw & slw. */
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
{
    TCGv temp;
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

    temp = tcg_temp_local_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x3f);
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], temp);
    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
    tcg_temp_free(temp);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
B
bellard 已提交
1802
/* sraw & sraw. */
1803 1804 1805 1806 1807 1808 1809
GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
{
    tcg_gen_helper_1_2(helper_sraw, cpu_gpr[rA(ctx->opcode)],
                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
B
bellard 已提交
1810 1811 1812
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    int sh = SH(ctx->opcode);
    if (sh != 0) {
        int l1, l2;
        TCGv temp;
        l1 = gen_new_label();
        l2 = gen_new_label();
        temp = tcg_temp_local_new(TCG_TYPE_TL);
        tcg_gen_ext32s_tl(temp, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_brcondi_tl(TCG_COND_GE, temp, 0, l1);
        tcg_gen_andi_tl(temp, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
        tcg_gen_ori_i32(cpu_xer, cpu_xer, 1 << XER_CA);
        tcg_gen_br(l2);
        gen_set_label(l1);
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
        gen_set_label(l2);
        tcg_gen_ext32s_tl(temp, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], temp, sh);
        tcg_temp_free(temp);
    } else {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
1835
    }
1836
    if (unlikely(Rc(ctx->opcode) != 0))
1837
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1838 1839
}
/* srw & srw. */
1840 1841 1842 1843 1844 1845
GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
{
    TCGv temp;
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();
1846

1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
    temp = tcg_temp_local_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x3f);
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], temp);
    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
    tcg_temp_free(temp);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1861 1862
#if defined(TARGET_PPC64)
/* sld & sld. */
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
{
    TCGv temp;
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

    temp = tcg_temp_local_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x40);
    tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x7f);
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], temp);
    gen_set_label(l2);
    tcg_temp_free(temp);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1883
/* srad & srad. */
1884 1885 1886 1887 1888 1889 1890
GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
{
    tcg_gen_helper_1_2(helper_srad, cpu_gpr[rA(ctx->opcode)],
                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1891
/* sradi & sradi. */
1892
static always_inline void gen_sradi (DisasContext *ctx, int n)
1893
{
1894
    int sh = SH(ctx->opcode) + (n << 5);
1895
    if (sh != 0) {
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
        int l1, l2;
        TCGv temp;
        l1 = gen_new_label();
        l2 = gen_new_label();
        tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
        temp = tcg_temp_new(TCG_TYPE_TL);
        tcg_gen_andi_tl(temp, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
        tcg_gen_ori_i32(cpu_xer, cpu_xer, 1 << XER_CA);
        tcg_gen_br(l2);
        gen_set_label(l1);
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
        gen_set_label(l2);
        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)]);
        tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_CA));
1913 1914
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1915
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1916
}
1917
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1918 1919 1920
{
    gen_sradi(ctx, 0);
}
1921
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1922 1923 1924 1925
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
{
    TCGv temp;
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

    temp = tcg_temp_local_new(TCG_TYPE_TL);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x40);
    tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_andi_tl(temp, cpu_gpr[rB(ctx->opcode)], 0x7f);
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], temp);
    gen_set_label(l2);
    tcg_temp_free(temp);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1946
#endif
B
bellard 已提交
1947 1948

/***                       Floating-Point arithmetic                       ***/
1949
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1950
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1951
{                                                                             \
1952
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1953
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1954 1955
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1956 1957 1958
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rC(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[2], cpu_fpr[rB(ctx->opcode)]);                     \
1959
    gen_reset_fpstatus();                                                     \
1960 1961 1962 1963
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
1964
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1965
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1966 1967
}

1968 1969 1970
#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);
1971

1972 1973
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1974
{                                                                             \
1975
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1976
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1977 1978
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1979 1980
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);                     \
1981
    gen_reset_fpstatus();                                                     \
1982 1983 1984 1985
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
1986
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1987
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1988
}
1989 1990 1991
#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);
1992

1993 1994
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1995
{                                                                             \
1996
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1997
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1998 1999
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
2000 2001
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rC(ctx->opcode)]);                     \
2002
    gen_reset_fpstatus();                                                     \
2003 2004 2005 2006
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
2007
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2008
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
2009
}
2010 2011 2012
#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);
2013

2014
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2015
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2016
{                                                                             \
2017
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2018
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
2019 2020
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
2021
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
2022
    gen_reset_fpstatus();                                                     \
2023
    gen_op_f##name();                                                         \
A
aurel32 已提交
2024
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2025
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
2026 2027
}

2028
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2029
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2030
{                                                                             \
2031
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2032
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
2033 2034
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
2035
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
2036
    gen_reset_fpstatus();                                                     \
2037
    gen_op_f##name();                                                         \
A
aurel32 已提交
2038
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2039
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
2040 2041
}

2042
/* fadd - fadds */
2043
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2044
/* fdiv - fdivs */
2045
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2046
/* fmul - fmuls */
2047
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
2048

2049
/* fre */
2050
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2051

2052
/* fres */
2053
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
2054

2055
/* frsqrte */
2056 2057 2058 2059 2060 2061 2062 2063
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);

/* frsqrtes */
static always_inline void gen_op_frsqrtes (void)
{
    gen_op_frsqrte();
    gen_op_frsp();
}
2064
GEN_FLOAT_BS(rsqrtes, 0x3B, 0x1A, 1, PPC_FLOAT_FRSQRTES);
B
bellard 已提交
2065

2066
/* fsel */
2067
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2068
/* fsub - fsubs */
2069
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
2070 2071
/* Optional: */
/* fsqrt */
2072
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2073
{
2074
    if (unlikely(!ctx->fpu_enabled)) {
2075
        GEN_EXCP_NO_FP(ctx);
2076 2077
        return;
    }
A
aurel32 已提交
2078
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2079
    gen_reset_fpstatus();
2080
    gen_op_fsqrt();
A
aurel32 已提交
2081
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2082
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
2083
}
B
bellard 已提交
2084

2085
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
2086
{
2087
    if (unlikely(!ctx->fpu_enabled)) {
2088
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2089 2090
        return;
    }
A
aurel32 已提交
2091
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2092
    gen_reset_fpstatus();
2093 2094
    gen_op_fsqrt();
    gen_op_frsp();
A
aurel32 已提交
2095
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2096
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
B
bellard 已提交
2097 2098 2099
}

/***                     Floating-Point multiply-and-add                   ***/
2100
/* fmadd - fmadds */
2101
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2102
/* fmsub - fmsubs */
2103
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2104
/* fnmadd - fnmadds */
2105
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2106
/* fnmsub - fnmsubs */
2107
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
B
bellard 已提交
2108 2109 2110

/***                     Floating-Point round & convert                    ***/
/* fctiw */
2111
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2112
/* fctiwz */
2113
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2114
/* frsp */
2115
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
J
j_mayer 已提交
2116 2117
#if defined(TARGET_PPC64)
/* fcfid */
2118
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
J
j_mayer 已提交
2119
/* fctid */
2120
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2121
/* fctidz */
2122
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2123
#endif
B
bellard 已提交
2124

2125
/* frin */
2126
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2127
/* friz */
2128
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2129
/* frip */
2130
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2131
/* frim */
2132
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2133

B
bellard 已提交
2134 2135
/***                         Floating-Point compare                        ***/
/* fcmpo */
2136
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2137
{
2138
    if (unlikely(!ctx->fpu_enabled)) {
2139
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2140 2141
        return;
    }
A
aurel32 已提交
2142 2143
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
2144
    gen_reset_fpstatus();
2145
    tcg_gen_helper_1_0(helper_fcmpo, cpu_crf[crfD(ctx->opcode)]);
2146
    gen_op_float_check_status();
B
bellard 已提交
2147 2148 2149
}

/* fcmpu */
2150
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2151
{
2152
    if (unlikely(!ctx->fpu_enabled)) {
2153
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2154 2155
        return;
    }
A
aurel32 已提交
2156 2157
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
2158
    gen_reset_fpstatus();
2159
    tcg_gen_helper_1_0(helper_fcmpu, cpu_crf[crfD(ctx->opcode)]);
2160
    gen_op_float_check_status();
B
bellard 已提交
2161 2162
}

2163 2164
/***                         Floating-point move                           ***/
/* fabs */
2165 2166
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2167 2168

/* fmr  - fmr. */
2169
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2170 2171
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
2172
    if (unlikely(!ctx->fpu_enabled)) {
2173
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2174 2175
        return;
    }
A
aurel32 已提交
2176 2177
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2178
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
2179 2180 2181
}

/* fnabs */
2182 2183
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2184
/* fneg */
2185 2186
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2187

B
bellard 已提交
2188 2189 2190 2191
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
2192 2193
    int bfa;

2194
    if (unlikely(!ctx->fpu_enabled)) {
2195
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2196 2197
        return;
    }
2198 2199
    gen_optimize_fprf();
    bfa = 4 * (7 - crfS(ctx->opcode));
2200 2201
    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);
2202
    gen_op_fpscr_resetbit(~(0xF << bfa));
B
bellard 已提交
2203 2204 2205 2206 2207
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
2208
    if (unlikely(!ctx->fpu_enabled)) {
2209
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2210 2211
        return;
    }
2212 2213 2214
    gen_optimize_fprf();
    gen_reset_fpstatus();
    gen_op_load_fpscr_FT0();
A
aurel32 已提交
2215
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2216
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
B
bellard 已提交
2217 2218 2219 2220 2221
}

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

2224
    if (unlikely(!ctx->fpu_enabled)) {
2225
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2226 2227
        return;
    }
2228 2229 2230 2231 2232 2233
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    if (likely(crb != 30 && crb != 29))
        gen_op_fpscr_resetbit(~(1 << crb));
    if (unlikely(Rc(ctx->opcode) != 0)) {
2234
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2235
    }
B
bellard 已提交
2236 2237 2238 2239 2240
}

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

2243
    if (unlikely(!ctx->fpu_enabled)) {
2244
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2245 2246
        return;
    }
2247 2248 2249 2250 2251 2252 2253
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    /* XXX: we pretend we can only do IEEE floating-point computations */
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI))
        gen_op_fpscr_setbit(crb);
    if (unlikely(Rc(ctx->opcode) != 0)) {
2254
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2255 2256 2257
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2258 2259 2260 2261 2262
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
2263
    if (unlikely(!ctx->fpu_enabled)) {
2264
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2265 2266
        return;
    }
2267
    gen_optimize_fprf();
A
aurel32 已提交
2268
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2269
    gen_reset_fpstatus();
2270
    gen_op_store_fpscr(FM(ctx->opcode));
2271
    if (unlikely(Rc(ctx->opcode) != 0)) {
2272
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2273 2274 2275
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2276 2277 2278 2279 2280
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
2281 2282
    int bf, sh;

2283
    if (unlikely(!ctx->fpu_enabled)) {
2284
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2285 2286
        return;
    }
2287 2288 2289
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
    gen_optimize_fprf();
2290
    tcg_gen_movi_i64(cpu_FT[0], FPIMM(ctx->opcode) << (4 * sh));
2291 2292 2293
    gen_reset_fpstatus();
    gen_op_store_fpscr(1 << sh);
    if (unlikely(Rc(ctx->opcode) != 0)) {
2294
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2295 2296 2297
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2298 2299
}

2300 2301
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2302 2303
static always_inline void gen_addr_imm_index (TCGv EA,
                                              DisasContext *ctx,
2304
                                              target_long maskl)
2305 2306 2307
{
    target_long simm = SIMM(ctx->opcode);

2308
    simm &= ~maskl;
2309 2310 2311 2312 2313 2314
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(EA, simm);
    else if (likely(simm != 0))
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
    else
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2315 2316
}

2317 2318
static always_inline void gen_addr_reg_index (TCGv EA,
                                              DisasContext *ctx)
2319
{
2320 2321 2322 2323
    if (rA(ctx->opcode) == 0)
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
    else
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2324 2325
}

2326 2327
static always_inline void gen_addr_register (TCGv EA,
                                             DisasContext *ctx)
2328
{
2329 2330 2331 2332
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(EA, 0);
    else
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2333 2334
}

2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
#if defined(TARGET_PPC64)
#define _GEN_MEM_FUNCS(name, mode)                                            \
    &gen_op_##name##_##mode,                                                  \
    &gen_op_##name##_le_##mode,                                               \
    &gen_op_##name##_64_##mode,                                               \
    &gen_op_##name##_le_64_##mode
#else
#define _GEN_MEM_FUNCS(name, mode)                                            \
    &gen_op_##name##_##mode,                                                  \
    &gen_op_##name##_le_##mode
#endif
2346
#if defined(CONFIG_USER_ONLY)
2347
#if defined(TARGET_PPC64)
2348
#define NB_MEM_FUNCS 4
2349
#else
2350
#define NB_MEM_FUNCS 2
2351
#endif
2352 2353
#define GEN_MEM_FUNCS(name)                                                   \
    _GEN_MEM_FUNCS(name, raw)
2354
#else
2355
#if defined(TARGET_PPC64)
2356
#define NB_MEM_FUNCS 12
2357
#else
2358
#define NB_MEM_FUNCS 6
2359
#endif
2360 2361 2362 2363 2364 2365 2366 2367
#define GEN_MEM_FUNCS(name)                                                   \
    _GEN_MEM_FUNCS(name, user),                                               \
    _GEN_MEM_FUNCS(name, kernel),                                             \
    _GEN_MEM_FUNCS(name, hypv)
#endif

/***                             Integer load                              ***/
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
2368
#define OP_LD_TABLE(width)                                                    \
2369 2370
static GenOpFunc *gen_op_l##width[NB_MEM_FUNCS] = {                           \
    GEN_MEM_FUNCS(l##width),                                                  \
2371 2372
};
#define OP_ST_TABLE(width)                                                    \
2373 2374
static GenOpFunc *gen_op_st##width[NB_MEM_FUNCS] = {                          \
    GEN_MEM_FUNCS(st##width),                                                 \
2375
};
2376

A
aurel32 已提交
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486

#if defined(TARGET_PPC64)
#define GEN_QEMU_LD_PPC64(width)                                                 \
static always_inline void gen_qemu_ld##width##_ppc64(TCGv t0, TCGv t1, int flags)\
{                                                                                \
    if (likely(flags & 2))                                                       \
        tcg_gen_qemu_ld##width(t0, t1, flags >> 2);                              \
    else {                                                                       \
        TCGv addr = tcg_temp_new(TCG_TYPE_TL);                                   \
        tcg_gen_ext32u_tl(addr, t1);                                             \
        tcg_gen_qemu_ld##width(t0, addr, flags >> 2);                            \
        tcg_temp_free(addr);                                                     \
    }                                                                            \
}
GEN_QEMU_LD_PPC64(8u)
GEN_QEMU_LD_PPC64(8s)
GEN_QEMU_LD_PPC64(16u)
GEN_QEMU_LD_PPC64(16s)
GEN_QEMU_LD_PPC64(32u)
GEN_QEMU_LD_PPC64(32s)
GEN_QEMU_LD_PPC64(64)

#define GEN_QEMU_ST_PPC64(width)                                                 \
static always_inline void gen_qemu_st##width##_ppc64(TCGv t0, TCGv t1, int flags)\
{                                                                                \
    if (likely(flags & 2))                                                       \
        tcg_gen_qemu_st##width(t0, t1, flags >> 2);                              \
    else {                                                                       \
        TCGv addr = tcg_temp_new(TCG_TYPE_TL);                                   \
        tcg_gen_ext32u_tl(addr, t1);                                             \
        tcg_gen_qemu_st##width(t0, addr, flags >> 2);                            \
        tcg_temp_free(addr);                                                     \
    }                                                                            \
}
GEN_QEMU_ST_PPC64(8)
GEN_QEMU_ST_PPC64(16)
GEN_QEMU_ST_PPC64(32)
GEN_QEMU_ST_PPC64(64)

static always_inline void gen_qemu_ld8u(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld8u_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld8s(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld8s_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld16u(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv t0_32;
        gen_qemu_ld16u_ppc64(t0, t1, flags);
        t0_32 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(t0_32, t0);
        tcg_gen_bswap16_i32(t0_32, t0_32);
        tcg_gen_extu_i32_tl(t0, t0_32);
        tcg_temp_free(t0_32);
    } else
        gen_qemu_ld16u_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld16s(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv t0_32;
        gen_qemu_ld16u_ppc64(t0, t1, flags);
        t0_32 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(t0_32, t0);
        tcg_gen_bswap16_i32(t0_32, t0_32);
        tcg_gen_extu_i32_tl(t0, t0_32);
        tcg_gen_ext16s_tl(t0, t0);
        tcg_temp_free(t0_32);
    } else
        gen_qemu_ld16s_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld32u(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv t0_32;
        gen_qemu_ld32u_ppc64(t0, t1, flags);
        t0_32 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(t0_32, t0);
        tcg_gen_bswap_i32(t0_32, t0_32);
        tcg_gen_extu_i32_tl(t0, t0_32);
        tcg_temp_free(t0_32);
    } else
        gen_qemu_ld32u_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld32s(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv t0_32;
        gen_qemu_ld32u_ppc64(t0, t1, flags);
        t0_32 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(t0_32, t0);
        tcg_gen_bswap_i32(t0_32, t0_32);
        tcg_gen_ext_i32_tl(t0, t0_32);
        tcg_temp_free(t0_32);
    } else
        gen_qemu_ld32s_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_ld64(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld64_ppc64(t0, t1, flags);
    if (unlikely(flags & 1))
A
aurel32 已提交
2487
        tcg_gen_bswap_i64(t0, t0);
A
aurel32 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
}

static always_inline void gen_qemu_st8(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_st8_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_st16(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp1, temp2;
        temp1 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(temp1, t0);
        tcg_gen_ext16u_i32(temp1, temp1);
        tcg_gen_bswap16_i32(temp1, temp1);
        temp2 = tcg_temp_new(TCG_TYPE_I64);
        tcg_gen_extu_i32_tl(temp2, temp1);
        tcg_temp_free(temp1);
        gen_qemu_st16_ppc64(temp2, t1, flags);
        tcg_temp_free(temp2);
    } else
        gen_qemu_st16_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_st32(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp1, temp2;
        temp1 = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_trunc_tl_i32(temp1, t0);
        tcg_gen_bswap_i32(temp1, temp1);
        temp2 = tcg_temp_new(TCG_TYPE_I64);
        tcg_gen_extu_i32_tl(temp2, temp1);
        tcg_temp_free(temp1);
        gen_qemu_st32_ppc64(temp2, t1, flags);
        tcg_temp_free(temp2);
    } else
        gen_qemu_st32_ppc64(t0, t1, flags);
}

static always_inline void gen_qemu_st64(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp = tcg_temp_new(TCG_TYPE_I64);
        tcg_gen_bswap_i64(temp, t0);
        gen_qemu_st64_ppc64(temp, t1, flags);
A
aurel32 已提交
2534
        tcg_temp_free(temp);
A
aurel32 已提交
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
    } else
        gen_qemu_st64_ppc64(t0, t1, flags);
}


#else /* defined(TARGET_PPC64) */
#define GEN_QEMU_LD_PPC32(width)                                                 \
static always_inline void gen_qemu_ld##width##_ppc32(TCGv t0, TCGv t1, int flags)\
{                                                                                \
    tcg_gen_qemu_ld##width(t0, t1, flags >> 1);                                  \
}
GEN_QEMU_LD_PPC32(8u)
GEN_QEMU_LD_PPC32(8s)
GEN_QEMU_LD_PPC32(16u)
GEN_QEMU_LD_PPC32(16s)
GEN_QEMU_LD_PPC32(32u)
GEN_QEMU_LD_PPC32(32s)
GEN_QEMU_LD_PPC32(64)

#define GEN_QEMU_ST_PPC32(width)                                                 \
static always_inline void gen_qemu_st##width##_ppc32(TCGv t0, TCGv t1, int flags)\
{                                                                                \
    tcg_gen_qemu_st##width(t0, t1, flags >> 1);                                  \
}
GEN_QEMU_ST_PPC32(8)
GEN_QEMU_ST_PPC32(16)
GEN_QEMU_ST_PPC32(32)
GEN_QEMU_ST_PPC32(64)

static always_inline void gen_qemu_ld8u(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld8u_ppc32(t0, t1, flags >> 1);
}

static always_inline void gen_qemu_ld8s(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld8s_ppc32(t0, t1, flags >> 1);
}

static always_inline void gen_qemu_ld16u(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld16u_ppc32(t0, t1, flags >> 1);
    if (unlikely(flags & 1))
        tcg_gen_bswap16_i32(t0, t0);
}

static always_inline void gen_qemu_ld16s(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        gen_qemu_ld16u_ppc32(t0, t1, flags);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_ext16s_i32(t0, t0);
    } else
        gen_qemu_ld16s_ppc32(t0, t1, flags);
}

static always_inline void gen_qemu_ld32u(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld32u_ppc32(t0, t1, flags);
    if (unlikely(flags & 1))
        tcg_gen_bswap_i32(t0, t0);
}

static always_inline void gen_qemu_ld64(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_ld64_ppc32(t0, t1, flags);
    if (unlikely(flags & 1))
        tcg_gen_bswap_i64(t0, t0);
}

static always_inline void gen_qemu_st8(TCGv t0, TCGv t1, int flags)
{
    gen_qemu_st8_ppc32(t0, t1, flags >> 1);
}

static always_inline void gen_qemu_st16(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_ext16u_i32(temp, t0);
        tcg_gen_bswap16_i32(temp, temp);
        gen_qemu_st16_ppc32(temp, t1, flags >> 1);
A
aurel32 已提交
2617
        tcg_temp_free(temp);
A
aurel32 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
    } else
        gen_qemu_st16_ppc32(t0, t1, flags >> 1);
}

static always_inline void gen_qemu_st32(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp = tcg_temp_new(TCG_TYPE_I32);
        tcg_gen_bswap_i32(temp, t0);
        gen_qemu_st32_ppc32(temp, t1, flags >> 1);
A
aurel32 已提交
2628
        tcg_temp_free(temp);
A
aurel32 已提交
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
    } else
        gen_qemu_st32_ppc32(t0, t1, flags >> 1);
}

static always_inline void gen_qemu_st64(TCGv t0, TCGv t1, int flags)
{
    if (unlikely(flags & 1)) {
        TCGv temp = tcg_temp_new(TCG_TYPE_I64);
        tcg_gen_bswap_i64(temp, t0);
        gen_qemu_st64_ppc32(temp, t1, flags >> 1);
A
aurel32 已提交
2639
        tcg_temp_free(temp);
A
aurel32 已提交
2640 2641 2642 2643 2644 2645
    } else
        gen_qemu_st64_ppc32(t0, t1, flags >> 1);
}

#endif

2646 2647
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2648
{                                                                             \
A
aurel32 已提交
2649 2650 2651 2652
    TCGv EA = tcg_temp_new(TCG_TYPE_TL);                                      \
    gen_addr_imm_index(EA, ctx, 0);                                           \
    gen_qemu_ld##width(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2653 2654
}

2655 2656
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2657
{                                                                             \
A
aurel32 已提交
2658
    TCGv EA;                                                                  \
2659 2660
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2661
        GEN_EXCP_INVAL(ctx);                                                  \
2662
        return;                                                               \
2663
    }                                                                         \
A
aurel32 已提交
2664
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
J
j_mayer 已提交
2665
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2666
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
J
j_mayer 已提交
2667
    else                                                                      \
A
aurel32 已提交
2668 2669 2670 2671
        gen_addr_imm_index(EA, ctx, 0);                                       \
    gen_qemu_ld##width(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2672 2673
}

2674 2675
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2676
{                                                                             \
A
aurel32 已提交
2677
    TCGv EA;                                                                  \
2678 2679
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2680
        GEN_EXCP_INVAL(ctx);                                                  \
2681
        return;                                                               \
2682
    }                                                                         \
A
aurel32 已提交
2683 2684 2685 2686 2687
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
    gen_addr_reg_index(EA, ctx);                                              \
    gen_qemu_ld##width(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2688 2689
}

2690 2691
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2692
{                                                                             \
A
aurel32 已提交
2693 2694 2695 2696
    TCGv EA = tcg_temp_new(TCG_TYPE_TL);                                      \
    gen_addr_reg_index(EA, ctx);                                              \
    gen_qemu_ld##width(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2697 2698
}

2699 2700 2701 2702 2703
#define GEN_LDS(width, op, type)                                              \
GEN_LD(width, op | 0x20, type);                                               \
GEN_LDU(width, op | 0x21, type);                                              \
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
GEN_LDX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2704 2705

/* lbz lbzu lbzux lbzx */
A
aurel32 已提交
2706
GEN_LDS(8u, 0x02, PPC_INTEGER);
B
bellard 已提交
2707
/* lha lhau lhaux lhax */
A
aurel32 已提交
2708
GEN_LDS(16s, 0x0A, PPC_INTEGER);
B
bellard 已提交
2709
/* lhz lhzu lhzux lhzx */
A
aurel32 已提交
2710
GEN_LDS(16u, 0x08, PPC_INTEGER);
B
bellard 已提交
2711
/* lwz lwzu lwzux lwzx */
A
aurel32 已提交
2712
GEN_LDS(32u, 0x00, PPC_INTEGER);
2713 2714
#if defined(TARGET_PPC64)
/* lwaux */
A
aurel32 已提交
2715
GEN_LDUX(32s, 0x15, 0x0B, PPC_64B);
2716
/* lwax */
A
aurel32 已提交
2717
GEN_LDX(32s, 0x15, 0x0A, PPC_64B);
2718
/* ldux */
A
aurel32 已提交
2719
GEN_LDUX(64, 0x15, 0x01, PPC_64B);
2720
/* ldx */
A
aurel32 已提交
2721
GEN_LDX(64, 0x15, 0x00, PPC_64B);
2722 2723
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
A
aurel32 已提交
2724
    TCGv EA;
2725 2726 2727
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2728
            GEN_EXCP_INVAL(ctx);
2729 2730 2731
            return;
        }
    }
A
aurel32 已提交
2732 2733
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_imm_index(EA, ctx, 0x03);
2734 2735
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
A
aurel32 已提交
2736
        gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2737 2738
    } else {
        /* ld - ldu */
A
aurel32 已提交
2739
        gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2740 2741
    }
    if (Rc(ctx->opcode))
A
aurel32 已提交
2742 2743
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
    tcg_temp_free(EA);
2744
}
2745 2746 2747 2748 2749 2750 2751
/* lq */
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    int ra, rd;
A
aurel32 已提交
2752
    TCGv EA;
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769

    /* Restore CPU state */
    if (unlikely(ctx->supervisor == 0)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
        GEN_EXCP_INVAL(ctx);
        return;
    }
    if (unlikely(ctx->mem_idx & 1)) {
        /* Little-endian mode is not handled */
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
        return;
    }
A
aurel32 已提交
2770 2771 2772 2773 2774 2775
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_imm_index(EA, ctx, 0x0F);
    gen_qemu_ld64(cpu_gpr[rd], EA, ctx->mem_idx);
    tcg_gen_addi_tl(EA, EA, 8);
    gen_qemu_ld64(cpu_gpr[rd+1], EA, ctx->mem_idx);
    tcg_temp_free(EA);
2776 2777
#endif
}
2778
#endif
B
bellard 已提交
2779 2780

/***                              Integer store                            ***/
2781 2782
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2783
{                                                                             \
A
aurel32 已提交
2784 2785 2786 2787
    TCGv EA = tcg_temp_new(TCG_TYPE_TL);                                      \
    gen_addr_imm_index(EA, ctx, 0);                                           \
    gen_qemu_st##width(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);       \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2788 2789
}

2790 2791
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2792
{                                                                             \
A
aurel32 已提交
2793
    TCGv EA;                                                                  \
2794
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2795
        GEN_EXCP_INVAL(ctx);                                                  \
2796
        return;                                                               \
2797
    }                                                                         \
A
aurel32 已提交
2798
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
J
j_mayer 已提交
2799
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2800
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
J
j_mayer 已提交
2801
    else                                                                      \
A
aurel32 已提交
2802 2803 2804 2805
        gen_addr_imm_index(EA, ctx, 0);                                       \
    gen_qemu_st##width(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2806 2807
}

2808 2809
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2810
{                                                                             \
A
aurel32 已提交
2811
    TCGv EA;                                                                  \
2812
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2813
        GEN_EXCP_INVAL(ctx);                                                  \
2814
        return;                                                               \
2815
    }                                                                         \
A
aurel32 已提交
2816 2817 2818 2819 2820
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
    gen_addr_reg_index(EA, ctx);                                              \
    gen_qemu_st##width(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2821 2822
}

2823 2824
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2825
{                                                                             \
A
aurel32 已提交
2826 2827 2828 2829
    TCGv EA = tcg_temp_new(TCG_TYPE_TL);                                      \
    gen_addr_reg_index(EA, ctx);                                              \
    gen_qemu_st##width(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);           \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2830 2831
}

2832 2833 2834 2835 2836
#define GEN_STS(width, op, type)                                              \
GEN_ST(width, op | 0x20, type);                                               \
GEN_STU(width, op | 0x21, type);                                              \
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
GEN_STX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2837 2838

/* stb stbu stbux stbx */
A
aurel32 已提交
2839
GEN_STS(8, 0x06, PPC_INTEGER);
B
bellard 已提交
2840
/* sth sthu sthux sthx */
A
aurel32 已提交
2841
GEN_STS(16, 0x0C, PPC_INTEGER);
B
bellard 已提交
2842
/* stw stwu stwux stwx */
A
aurel32 已提交
2843
GEN_STS(32, 0x04, PPC_INTEGER);
2844
#if defined(TARGET_PPC64)
A
aurel32 已提交
2845 2846
GEN_STUX(64, 0x15, 0x05, PPC_64B);
GEN_STX(64, 0x15, 0x04, PPC_64B);
2847
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2848
{
2849
    int rs;
A
aurel32 已提交
2850
    TCGv EA;
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
        GEN_EXCP_PRIVOPC(ctx);
#else
        /* stq */
        if (unlikely(ctx->supervisor == 0)) {
            GEN_EXCP_PRIVOPC(ctx);
            return;
        }
        if (unlikely(rs & 1)) {
2863
            GEN_EXCP_INVAL(ctx);
2864 2865
            return;
        }
2866 2867 2868 2869 2870
        if (unlikely(ctx->mem_idx & 1)) {
            /* Little-endian mode is not handled */
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
            return;
        }
A
aurel32 已提交
2871 2872 2873 2874 2875 2876
        EA = tcg_temp_new(TCG_TYPE_TL);
        gen_addr_imm_index(EA, ctx, 0x03);
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
        tcg_gen_addi_tl(EA, EA, 8);
        gen_qemu_st64(cpu_gpr[rs+1], EA, ctx->mem_idx);
        tcg_temp_free(EA);
2877 2878 2879 2880 2881 2882 2883 2884 2885
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
                GEN_EXCP_INVAL(ctx);
                return;
            }
        }
A
aurel32 已提交
2886 2887 2888
        EA = tcg_temp_new(TCG_TYPE_TL);
        gen_addr_imm_index(EA, ctx, 0x03);
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
2889
        if (Rc(ctx->opcode))
A
aurel32 已提交
2890 2891
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
        tcg_temp_free(EA);
2892 2893 2894
    }
}
#endif
B
bellard 已提交
2895 2896
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
A
aurel32 已提交
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
void always_inline gen_qemu_ld16ur(TCGv t0, TCGv t1, int flags)
{
    TCGv temp = tcg_temp_new(TCG_TYPE_I32);
    gen_qemu_ld16u(temp, t1, flags);
    tcg_gen_bswap16_i32(temp, temp);
    tcg_gen_extu_i32_tl(t0, temp);
    tcg_temp_free(temp);
}
GEN_LDX(16ur, 0x16, 0x18, PPC_INTEGER);

B
bellard 已提交
2907
/* lwbrx */
A
aurel32 已提交
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917
void always_inline gen_qemu_ld32ur(TCGv t0, TCGv t1, int flags)
{
    TCGv temp = tcg_temp_new(TCG_TYPE_I32);
    gen_qemu_ld32u(temp, t1, flags);
    tcg_gen_bswap_i32(temp, temp);
    tcg_gen_extu_i32_tl(t0, temp);
    tcg_temp_free(temp);
}
GEN_LDX(32ur, 0x16, 0x10, PPC_INTEGER);

B
bellard 已提交
2918
/* sthbrx */
A
aurel32 已提交
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
void always_inline gen_qemu_st16r(TCGv t0, TCGv t1, int flags)
{
    TCGv temp = tcg_temp_new(TCG_TYPE_I32);
    tcg_gen_trunc_tl_i32(temp, t0);
    tcg_gen_ext16u_i32(temp, temp);
    tcg_gen_bswap16_i32(temp, temp);
    gen_qemu_st16(temp, t1, flags);
    tcg_temp_free(temp);
}
GEN_STX(16r, 0x16, 0x1C, PPC_INTEGER);

B
bellard 已提交
2930
/* stwbrx */
A
aurel32 已提交
2931 2932 2933 2934 2935 2936 2937 2938 2939
void always_inline gen_qemu_st32r(TCGv t0, TCGv t1, int flags)
{
    TCGv temp = tcg_temp_new(TCG_TYPE_I32);
    tcg_gen_trunc_tl_i32(temp, t0);
    tcg_gen_bswap_i32(temp, temp);
    gen_qemu_st32(temp, t1, flags);
    tcg_temp_free(temp);
}
GEN_STX(32r, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
2940 2941

/***                    Integer load and store multiple                    ***/
2942
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2943 2944
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lmw),
2945
};
2946 2947
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stmw),
2948
};
2949

B
bellard 已提交
2950 2951 2952
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2953
    /* NIP cannot be restored if the memory exception comes from an helper */
2954
    gen_update_nip(ctx, ctx->nip - 4);
2955
    gen_addr_imm_index(cpu_T[0], ctx, 0);
2956
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2957 2958 2959 2960 2961
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2962
    /* NIP cannot be restored if the memory exception comes from an helper */
2963
    gen_update_nip(ctx, ctx->nip - 4);
2964
    gen_addr_imm_index(cpu_T[0], ctx, 0);
2965
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2966 2967 2968
}

/***                    Integer load and store strings                     ***/
2969 2970
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2971 2972 2973 2974 2975 2976 2977 2978 2979
/* string load & stores are by definition endian-safe */
#define gen_op_lswi_le_raw       gen_op_lswi_raw
#define gen_op_lswi_le_user      gen_op_lswi_user
#define gen_op_lswi_le_kernel    gen_op_lswi_kernel
#define gen_op_lswi_le_hypv      gen_op_lswi_hypv
#define gen_op_lswi_le_64_raw    gen_op_lswi_raw
#define gen_op_lswi_le_64_user   gen_op_lswi_user
#define gen_op_lswi_le_64_kernel gen_op_lswi_kernel
#define gen_op_lswi_le_64_hypv   gen_op_lswi_hypv
2980 2981
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswi),
2982
};
2983 2984 2985 2986 2987 2988 2989 2990
#define gen_op_lswx_le_raw       gen_op_lswx_raw
#define gen_op_lswx_le_user      gen_op_lswx_user
#define gen_op_lswx_le_kernel    gen_op_lswx_kernel
#define gen_op_lswx_le_hypv      gen_op_lswx_hypv
#define gen_op_lswx_le_64_raw    gen_op_lswx_raw
#define gen_op_lswx_le_64_user   gen_op_lswx_user
#define gen_op_lswx_le_64_kernel gen_op_lswx_kernel
#define gen_op_lswx_le_64_hypv   gen_op_lswx_hypv
2991 2992
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswx),
2993
};
2994 2995 2996 2997 2998 2999 3000 3001
#define gen_op_stsw_le_raw       gen_op_stsw_raw
#define gen_op_stsw_le_user      gen_op_stsw_user
#define gen_op_stsw_le_kernel    gen_op_stsw_kernel
#define gen_op_stsw_le_hypv      gen_op_stsw_hypv
#define gen_op_stsw_le_64_raw    gen_op_stsw_raw
#define gen_op_stsw_le_64_user   gen_op_stsw_user
#define gen_op_stsw_le_64_kernel gen_op_stsw_kernel
#define gen_op_stsw_le_64_hypv   gen_op_stsw_hypv
3002 3003
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stsw),
3004 3005
};

B
bellard 已提交
3006
/* lswi */
3007
/* PowerPC32 specification says we must generate an exception if
3008 3009 3010 3011
 * 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...
 */
3012
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
B
bellard 已提交
3013 3014 3015
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
3016
    int ra = rA(ctx->opcode);
B
bellard 已提交
3017 3018 3019 3020 3021
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
3022 3023 3024
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3025 3026
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
3027
        return;
B
bellard 已提交
3028
    }
3029
    /* NIP cannot be restored if the memory exception comes from an helper */
3030
    gen_update_nip(ctx, ctx->nip - 4);
3031
    gen_addr_register(cpu_T[0], ctx);
3032
    tcg_gen_movi_tl(cpu_T[1], nb);
3033
    op_ldsts(lswi, start);
B
bellard 已提交
3034 3035 3036
}

/* lswx */
3037
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
B
bellard 已提交
3038
{
3039 3040 3041
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

3042
    /* NIP cannot be restored if the memory exception comes from an helper */
3043
    gen_update_nip(ctx, ctx->nip - 4);
3044
    gen_addr_reg_index(cpu_T[0], ctx);
3045 3046
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
3047
    }
A
aurel32 已提交
3048
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3049
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
3050 3051 3052
}

/* stswi */
3053
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
B
bellard 已提交
3054
{
B
bellard 已提交
3055 3056
    int nb = NB(ctx->opcode);

3057
    /* NIP cannot be restored if the memory exception comes from an helper */
3058
    gen_update_nip(ctx, ctx->nip - 4);
3059
    gen_addr_register(cpu_T[0], ctx);
B
bellard 已提交
3060 3061
    if (nb == 0)
        nb = 32;
3062
    tcg_gen_movi_tl(cpu_T[1], nb);
3063
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
3064 3065 3066
}

/* stswx */
3067
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
B
bellard 已提交
3068
{
3069
    /* NIP cannot be restored if the memory exception comes from an helper */
3070
    gen_update_nip(ctx, ctx->nip - 4);
3071
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
3072
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3073
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
3074 3075 3076 3077
}

/***                        Memory synchronisation                         ***/
/* eieio */
3078
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
3079 3080 3081 3082
{
}

/* isync */
3083
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
3084
{
3085
    GEN_STOP(ctx);
B
bellard 已提交
3086 3087
}

3088 3089
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
3090 3091
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lwarx),
3092
};
3093 3094
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stwcx),
B
bellard 已提交
3095
};
3096

3097
/* lwarx */
3098
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
3099
{
3100 3101
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3102
    gen_addr_reg_index(cpu_T[0], ctx);
B
bellard 已提交
3103
    op_lwarx();
A
aurel32 已提交
3104
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
B
bellard 已提交
3105 3106 3107
}

/* stwcx. */
3108
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
3109
{
3110 3111
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3112
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
3113
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3114
    op_stwcx();
B
bellard 已提交
3115 3116
}

J
j_mayer 已提交
3117 3118 3119
#if defined(TARGET_PPC64)
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
3120 3121
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ldarx),
J
j_mayer 已提交
3122
};
3123 3124
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stdcx),
J
j_mayer 已提交
3125 3126 3127
};

/* ldarx */
3128
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
3129
{
3130 3131
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3132
    gen_addr_reg_index(cpu_T[0], ctx);
J
j_mayer 已提交
3133
    op_ldarx();
A
aurel32 已提交
3134
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
J
j_mayer 已提交
3135 3136 3137
}

/* stdcx. */
3138
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
3139
{
3140 3141
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3142
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
3143
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
J
j_mayer 已提交
3144 3145 3146 3147
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
3148
/* sync */
3149
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
3150 3151 3152
{
}

3153 3154 3155 3156
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
    /* Stop translation, as the CPU is supposed to sleep from now */
3157 3158
    gen_op_wait();
    GEN_EXCP(ctx, EXCP_HLT, 1);
3159 3160
}

B
bellard 已提交
3161
/***                         Floating-point load                           ***/
3162 3163
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
3164
{                                                                             \
3165
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3166
        GEN_EXCP_NO_FP(ctx);                                                  \
3167 3168
        return;                                                               \
    }                                                                         \
3169
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
3170
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3171
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
B
bellard 已提交
3172 3173
}

3174 3175
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
3176
{                                                                             \
3177
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3178
        GEN_EXCP_NO_FP(ctx);                                                  \
3179 3180
        return;                                                               \
    }                                                                         \
3181
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3182
        GEN_EXCP_INVAL(ctx);                                                  \
3183
        return;                                                               \
3184
    }                                                                         \
3185
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
3186
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3187
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
A
aurel32 已提交
3188
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3189 3190
}

3191 3192
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
3193
{                                                                             \
3194
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3195
        GEN_EXCP_NO_FP(ctx);                                                  \
3196 3197
        return;                                                               \
    }                                                                         \
3198
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3199
        GEN_EXCP_INVAL(ctx);                                                  \
3200
        return;                                                               \
3201
    }                                                                         \
3202
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
3203
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3204
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
A
aurel32 已提交
3205
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3206 3207
}

3208 3209
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
3210
{                                                                             \
3211
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3212
        GEN_EXCP_NO_FP(ctx);                                                  \
3213 3214
        return;                                                               \
    }                                                                         \
3215
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
3216
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3217
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
B
bellard 已提交
3218 3219
}

3220
#define GEN_LDFS(width, op, type)                                             \
3221
OP_LD_TABLE(width);                                                           \
3222 3223 3224 3225
GEN_LDF(width, op | 0x20, type);                                              \
GEN_LDUF(width, op | 0x21, type);                                             \
GEN_LDUXF(width, op | 0x01, type);                                            \
GEN_LDXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
3226 3227

/* lfd lfdu lfdux lfdx */
3228
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
3229
/* lfs lfsu lfsux lfsx */
3230
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
3231 3232

/***                         Floating-point store                          ***/
3233 3234
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
3235
{                                                                             \
3236
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3237
        GEN_EXCP_NO_FP(ctx);                                                  \
3238 3239
        return;                                                               \
    }                                                                         \
3240
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
A
aurel32 已提交
3241
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3242
    op_ldst(st##width);                                                       \
B
bellard 已提交
3243 3244
}

3245 3246
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
3247
{                                                                             \
3248
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3249
        GEN_EXCP_NO_FP(ctx);                                                  \
3250 3251
        return;                                                               \
    }                                                                         \
3252
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3253
        GEN_EXCP_INVAL(ctx);                                                  \
3254
        return;                                                               \
3255
    }                                                                         \
3256
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
A
aurel32 已提交
3257
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3258
    op_ldst(st##width);                                                       \
A
aurel32 已提交
3259
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3260 3261
}

3262 3263
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
3264
{                                                                             \
3265
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3266
        GEN_EXCP_NO_FP(ctx);                                                  \
3267 3268
        return;                                                               \
    }                                                                         \
3269
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3270
        GEN_EXCP_INVAL(ctx);                                                  \
3271
        return;                                                               \
3272
    }                                                                         \
3273
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
3274
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3275
    op_ldst(st##width);                                                       \
A
aurel32 已提交
3276
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3277 3278
}

3279 3280
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
3281
{                                                                             \
3282
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3283
        GEN_EXCP_NO_FP(ctx);                                                  \
3284 3285
        return;                                                               \
    }                                                                         \
3286
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
3287
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3288
    op_ldst(st##width);                                                       \
B
bellard 已提交
3289 3290
}

3291
#define GEN_STFS(width, op, type)                                             \
3292
OP_ST_TABLE(width);                                                           \
3293 3294 3295 3296
GEN_STF(width, op | 0x20, type);                                              \
GEN_STUF(width, op | 0x21, type);                                             \
GEN_STUXF(width, op | 0x01, type);                                            \
GEN_STXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
3297 3298

/* stfd stfdu stfdux stfdx */
3299
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
3300
/* stfs stfsu stfsux stfsx */
3301
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3302 3303 3304

/* Optional: */
/* stfiwx */
J
j_mayer 已提交
3305 3306
OP_ST_TABLE(fiw);
GEN_STXF(fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3307 3308

/***                                Branch                                 ***/
3309 3310
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
3311 3312 3313
{
    TranslationBlock *tb;
    tb = ctx->tb;
3314 3315 3316 3317
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        dest = (uint32_t) dest;
#endif
B
bellard 已提交
3318
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3319
        likely(!ctx->singlestep_enabled)) {
B
bellard 已提交
3320
        tcg_gen_goto_tb(n);
3321
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
B
bellard 已提交
3322
        tcg_gen_exit_tb((long)tb + n);
3323
    } else {
3324
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
        if (unlikely(ctx->singlestep_enabled)) {
            if ((ctx->singlestep_enabled &
                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
                ctx->exception == POWERPC_EXCP_BRANCH) {
                target_ulong tmp = ctx->nip;
                ctx->nip = dest;
                GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
                ctx->nip = tmp;
            }
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
                gen_update_nip(ctx, dest);
                gen_op_debug();
            }
        }
B
bellard 已提交
3339
        tcg_gen_exit_tb(0);
3340
    }
B
bellard 已提交
3341 3342
}

3343
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3344 3345
{
#if defined(TARGET_PPC64)
3346 3347
    if (ctx->sf_mode == 0)
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3348 3349
    else
#endif
3350
        tcg_gen_movi_tl(cpu_lr, nip);
3351 3352
}

B
bellard 已提交
3353 3354 3355
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3356
    target_ulong li, target;
B
bellard 已提交
3357

3358
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3359
    /* sign extend LI */
3360
#if defined(TARGET_PPC64)
3361 3362 3363
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3364
#endif
3365
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3366
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3367
        target = ctx->nip + li - 4;
B
bellard 已提交
3368
    else
3369
        target = li;
3370 3371
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3372
    gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3373 3374
}

3375 3376 3377 3378
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3379
static always_inline void gen_bcond (DisasContext *ctx, int type)
3380 3381
{
    uint32_t bo = BO(ctx->opcode);
3382 3383
    int l1 = gen_new_label();
    TCGv target;
3384

3385
    ctx->exception = POWERPC_EXCP_BRANCH;
3386 3387 3388 3389 3390 3391
    if (type == BCOND_LR || type == BCOND_CTR) {
        target = tcg_temp_local_new(TCG_TYPE_TL);
        if (type == BCOND_CTR)
            tcg_gen_mov_tl(target, cpu_ctr);
        else
            tcg_gen_mov_tl(target, cpu_lr);
3392
    }
3393 3394
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3395 3396 3397 3398 3399 3400 3401 3402 3403
    l1 = gen_new_label();
    if ((bo & 0x4) == 0) {
        /* Decrement and test CTR */
        TCGv temp = tcg_temp_new(TCG_TYPE_TL);
        if (unlikely(type == BCOND_CTR)) {
            GEN_EXCP_INVAL(ctx);
            return;
        }
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3404
#if defined(TARGET_PPC64)
3405 3406 3407
        if (!ctx->sf_mode)
            tcg_gen_ext32u_tl(temp, cpu_ctr);
        else
3408
#endif
3409 3410 3411 3412 3413
            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);
3414
        }
3415 3416 3417 3418 3419 3420 3421
    }
    if ((bo & 0x10) == 0) {
        /* Test CR */
        uint32_t bi = BI(ctx->opcode);
        uint32_t mask = 1 << (3 - (bi & 0x03));
        TCGv temp = tcg_temp_new(TCG_TYPE_I32);

3422
        if (bo & 0x8) {
3423 3424
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3425
        } else {
3426 3427
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3428 3429
        }
    }
3430
    if (type == BCOND_IM) {
3431 3432 3433 3434 3435 3436 3437

        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 已提交
3438
        gen_set_label(l1);
3439
        gen_goto_tb(ctx, 1, ctx->nip);
3440
    } else {
3441
#if defined(TARGET_PPC64)
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
        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);
3452 3453
        else
#endif
3454
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
B
bellard 已提交
3455
        tcg_gen_exit_tb(0);
J
j_mayer 已提交
3456
    }
3457 3458 3459
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3460
{
3461 3462 3463 3464
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3465
{
3466 3467 3468 3469
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3470
{
3471 3472
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3473 3474

/***                      Condition register logical                       ***/
3475 3476
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
B
bellard 已提交
3477
{                                                                             \
3478 3479
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
3480
    TCGv temp1, temp2;                                                        \
3481
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3482
    temp1 = tcg_temp_new(TCG_TYPE_I32);                                       \
3483
    if (sh > 0)                                                               \
3484
        tcg_gen_shri_i32(temp1, cpu_crf[crbA(ctx->opcode) >> 2], sh);         \
3485
    else if (sh < 0)                                                          \
3486 3487
        tcg_gen_shli_i32(temp1, cpu_crf[crbA(ctx->opcode) >> 2], -sh);        \
    else                                                                      \
P
pbrook 已提交
3488
        tcg_gen_mov_i32(temp1, cpu_crf[crbA(ctx->opcode) >> 2]);              \
3489
    temp2 = tcg_temp_new(TCG_TYPE_I32);                                       \
3490 3491
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
3492
        tcg_gen_shri_i32(temp2, cpu_crf[crbB(ctx->opcode) >> 2], sh);         \
3493
    else if (sh < 0)                                                          \
3494 3495 3496 3497
        tcg_gen_shli_i32(temp2, cpu_crf[crbB(ctx->opcode) >> 2], -sh);        \
    else                                                                      \
        tcg_gen_mov_i32(temp2, cpu_crf[crbB(ctx->opcode) >> 2]);              \
    tcg_op(temp1, temp1, temp2);                                              \
3498
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3499 3500 3501 3502 3503
    tcg_gen_andi_i32(temp1, temp1, bitmask);                                  \
    tcg_gen_andi_i32(temp2, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);       \
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], temp1, temp2);            \
    tcg_temp_free(temp1);                                                     \
    tcg_temp_free(temp2);                                                     \
B
bellard 已提交
3504 3505 3506
}

/* crand */
3507
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
B
bellard 已提交
3508
/* crandc */
3509
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
B
bellard 已提交
3510
/* creqv */
3511
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
B
bellard 已提交
3512
/* crnand */
3513
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
B
bellard 已提交
3514
/* crnor */
3515
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
B
bellard 已提交
3516
/* cror */
3517
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
B
bellard 已提交
3518
/* crorc */
3519
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
B
bellard 已提交
3520
/* crxor */
3521
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
B
bellard 已提交
3522 3523 3524
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
A
aurel32 已提交
3525
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
B
bellard 已提交
3526 3527 3528 3529
}

/***                           System linkage                              ***/
/* rfi (supervisor only) */
3530
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3531
{
3532
#if defined(CONFIG_USER_ONLY)
3533
    GEN_EXCP_PRIVOPC(ctx);
3534 3535
#else
    /* Restore CPU state */
3536
    if (unlikely(!ctx->supervisor)) {
3537
        GEN_EXCP_PRIVOPC(ctx);
3538
        return;
3539
    }
3540
    gen_op_rfi();
3541
    GEN_SYNC(ctx);
3542
#endif
B
bellard 已提交
3543 3544
}

J
j_mayer 已提交
3545
#if defined(TARGET_PPC64)
3546
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3547 3548
{
#if defined(CONFIG_USER_ONLY)
3549
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3550 3551 3552
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3553
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3554 3555
        return;
    }
3556
    gen_op_rfid();
3557
    GEN_SYNC(ctx);
J
j_mayer 已提交
3558 3559 3560
#endif
}

J
j_mayer 已提交
3561
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    /* Restore CPU state */
    if (unlikely(ctx->supervisor <= 1)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_hrfid();
    GEN_SYNC(ctx);
#endif
}
#endif

B
bellard 已提交
3577
/* sc */
3578 3579 3580 3581 3582
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3583
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3584
{
3585 3586 3587
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3588
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3589 3590 3591 3592
}

/***                                Trap                                   ***/
/* tw */
3593
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3594
{
A
aurel32 已提交
3595 3596
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3597
    /* Update the nip since this might generate a trap exception */
3598
    gen_update_nip(ctx, ctx->nip);
3599
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3600 3601 3602 3603 3604
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
A
aurel32 已提交
3605
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
3606
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3607 3608
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3609
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3610 3611
}

3612 3613 3614 3615
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
A
aurel32 已提交
3616 3617
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3618 3619 3620 3621 3622 3623 3624 3625
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
A
aurel32 已提交
3626
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
3627
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3628 3629 3630 3631 3632 3633
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}
#endif

B
bellard 已提交
3634 3635 3636 3637
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
A
aurel32 已提交
3638 3639 3640
    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);
    tcg_gen_andi_i32(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
B
bellard 已提交
3641 3642 3643
}

/* mfcr */
3644
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3645
{
3646
    uint32_t crm, crn;
3647

3648 3649 3650 3651
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
3652
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3653
        }
3654
    } else {
3655
        tcg_gen_helper_1_0(helper_load_cr, cpu_gpr[rD(ctx->opcode)]);
3656
    }
B
bellard 已提交
3657 3658 3659 3660 3661
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3662
#if defined(CONFIG_USER_ONLY)
3663
    GEN_EXCP_PRIVREG(ctx);
3664
#else
3665
    if (unlikely(!ctx->supervisor)) {
3666
        GEN_EXCP_PRIVREG(ctx);
3667
        return;
3668
    }
A
aurel32 已提交
3669
    gen_op_load_msr();
A
aurel32 已提交
3670
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3671
#endif
B
bellard 已提交
3672 3673
}

J
j_mayer 已提交
3674
#if 1
3675
#define SPR_NOACCESS ((void *)(-1UL))
3676 3677 3678 3679 3680 3681 3682 3683 3684
#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 已提交
3685
/* mfspr */
3686
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3687
{
3688
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3689 3690
    uint32_t sprn = SPR(ctx->opcode);

3691
#if !defined(CONFIG_USER_ONLY)
3692 3693
    if (ctx->supervisor == 2)
        read_cb = ctx->spr_cb[sprn].hea_read;
3694
    else if (ctx->supervisor)
3695 3696
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3697
#endif
3698
        read_cb = ctx->spr_cb[sprn].uea_read;
3699 3700
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3701
            (*read_cb)(ctx, sprn);
A
aurel32 已提交
3702
            tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3703 3704
        } else {
            /* Privilege exception */
3705 3706 3707 3708 3709 3710
            /* 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) {
                if (loglevel != 0) {
3711
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
J
j_mayer 已提交
3712
                            ADDRX "\n", sprn, sprn, ctx->nip);
3713
                }
J
j_mayer 已提交
3714 3715
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
                       sprn, sprn, ctx->nip);
3716
            }
3717
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3718
        }
3719 3720
    } else {
        /* Not defined */
J
j_mayer 已提交
3721
        if (loglevel != 0) {
J
j_mayer 已提交
3722 3723
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3724
        }
J
j_mayer 已提交
3725 3726
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3727 3728
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3729 3730 3731
    }
}

3732
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3733
{
3734
    gen_op_mfspr(ctx);
3735
}
3736 3737

/* mftb */
3738
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3739 3740
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3741 3742 3743
}

/* mtcrf */
3744
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3745
{
3746
    uint32_t crm, crn;
3747

3748 3749 3750
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
        crn = ffs(crm);
3751 3752
        tcg_gen_shri_i32(cpu_crf[7 - crn], cpu_gpr[rS(ctx->opcode)], crn * 4);
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3753
    } else {
3754 3755 3756
        TCGv temp = tcg_const_tl(crm);
        tcg_gen_helper_0_2(helper_store_cr, cpu_gpr[rS(ctx->opcode)], temp);
        tcg_temp_free(temp);
3757
    }
B
bellard 已提交
3758 3759 3760
}

/* mtmsr */
J
j_mayer 已提交
3761
#if defined(TARGET_PPC64)
3762
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3763 3764
{
#if defined(CONFIG_USER_ONLY)
3765
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3766 3767
#else
    if (unlikely(!ctx->supervisor)) {
3768
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3769 3770
        return;
    }
A
aurel32 已提交
3771
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3772 3773 3774 3775
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3776 3777 3778 3779
        /* 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
         */
3780
        gen_update_nip(ctx, ctx->nip);
A
aurel32 已提交
3781
        gen_op_store_msr();
3782 3783
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
3784
        ctx->exception = POWERPC_EXCP_STOP;
3785
    }
J
j_mayer 已提交
3786 3787 3788 3789
#endif
}
#endif

B
bellard 已提交
3790 3791
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3792
#if defined(CONFIG_USER_ONLY)
3793
    GEN_EXCP_PRIVREG(ctx);
3794
#else
3795
    if (unlikely(!ctx->supervisor)) {
3796
        GEN_EXCP_PRIVREG(ctx);
3797
        return;
3798
    }
A
aurel32 已提交
3799
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3800 3801 3802 3803
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3804 3805 3806 3807
        /* 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
         */
3808
        gen_update_nip(ctx, ctx->nip);
3809
#if defined(TARGET_PPC64)
3810
        if (!ctx->sf_mode)
A
aurel32 已提交
3811
            gen_op_store_msr_32();
3812
        else
3813
#endif
A
aurel32 已提交
3814
            gen_op_store_msr();
3815 3816
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsrd is not always defined as context-synchronizing */
3817
        ctx->exception = POWERPC_EXCP_STOP;
3818
    }
3819
#endif
B
bellard 已提交
3820 3821 3822 3823 3824
}

/* mtspr */
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
{
3825
    void (*write_cb)(void *opaque, int sprn);
B
bellard 已提交
3826 3827
    uint32_t sprn = SPR(ctx->opcode);

3828
#if !defined(CONFIG_USER_ONLY)
3829 3830
    if (ctx->supervisor == 2)
        write_cb = ctx->spr_cb[sprn].hea_write;
3831
    else if (ctx->supervisor)
3832 3833
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3834
#endif
3835
        write_cb = ctx->spr_cb[sprn].uea_write;
3836 3837
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
A
aurel32 已提交
3838
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3839 3840 3841
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3842
            if (loglevel != 0) {
J
j_mayer 已提交
3843 3844
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
                        ADDRX "\n", sprn, sprn, ctx->nip);
3845
            }
J
j_mayer 已提交
3846 3847
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
                   sprn, sprn, ctx->nip);
3848
            GEN_EXCP_PRIVREG(ctx);
3849
        }
3850 3851
    } else {
        /* Not defined */
J
j_mayer 已提交
3852
        if (loglevel != 0) {
J
j_mayer 已提交
3853 3854
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3855
        }
J
j_mayer 已提交
3856 3857
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3858 3859
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3860 3861 3862 3863 3864
    }
}

/***                         Cache management                              ***/
/* dcbf */
3865
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
3866
{
J
j_mayer 已提交
3867
    /* XXX: specification says this is treated as a load by the MMU */
A
aurel32 已提交
3868 3869 3870 3871
    TCGv temp = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_reg_index(temp, ctx);
    gen_qemu_ld8u(temp, temp, ctx->mem_idx);
    tcg_temp_free(temp);
B
bellard 已提交
3872 3873 3874
}

/* dcbi (Supervisor only) */
3875
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3876
{
3877
#if defined(CONFIG_USER_ONLY)
3878
    GEN_EXCP_PRIVOPC(ctx);
3879
#else
A
aurel32 已提交
3880
    TCGv EA, val;
3881
    if (unlikely(!ctx->supervisor)) {
3882
        GEN_EXCP_PRIVOPC(ctx);
3883
        return;
3884
    }
A
aurel32 已提交
3885 3886
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_reg_index(EA, ctx);
A
aurel32 已提交
3887
    val = tcg_temp_new(TCG_TYPE_TL);
3888
    /* XXX: specification says this should be treated as a store by the MMU */
A
aurel32 已提交
3889 3890 3891 3892
    gen_qemu_ld8u(val, EA, ctx->mem_idx);
    gen_qemu_st8(val, EA, ctx->mem_idx);
    tcg_temp_free(val);
    tcg_temp_free(EA);
3893
#endif
B
bellard 已提交
3894 3895 3896
}

/* dcdst */
3897
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3898
{
3899
    /* XXX: specification say this is treated as a load by the MMU */
A
aurel32 已提交
3900 3901 3902 3903
    TCGv temp = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_reg_index(temp, ctx);
    gen_qemu_ld8u(temp, temp, ctx->mem_idx);
    tcg_temp_free(temp);
B
bellard 已提交
3904 3905 3906
}

/* dcbt */
3907
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
3908
{
3909
    /* interpreted as no-op */
3910 3911 3912
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3913 3914 3915
}

/* dcbtst */
3916
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
3917
{
3918
    /* interpreted as no-op */
3919 3920 3921
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3922 3923 3924
}

/* dcbz */
3925
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3926 3927
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
    /* 32 bytes cache line size */
3928
    {
3929 3930 3931 3932 3933 3934 3935 3936 3937
#define gen_op_dcbz_l32_le_raw        gen_op_dcbz_l32_raw
#define gen_op_dcbz_l32_le_user       gen_op_dcbz_l32_user
#define gen_op_dcbz_l32_le_kernel     gen_op_dcbz_l32_kernel
#define gen_op_dcbz_l32_le_hypv       gen_op_dcbz_l32_hypv
#define gen_op_dcbz_l32_le_64_raw     gen_op_dcbz_l32_64_raw
#define gen_op_dcbz_l32_le_64_user    gen_op_dcbz_l32_64_user
#define gen_op_dcbz_l32_le_64_kernel  gen_op_dcbz_l32_64_kernel
#define gen_op_dcbz_l32_le_64_hypv    gen_op_dcbz_l32_64_hypv
        GEN_MEM_FUNCS(dcbz_l32),
3938
    },
3939
    /* 64 bytes cache line size */
3940
    {
3941 3942 3943 3944 3945 3946 3947 3948 3949
#define gen_op_dcbz_l64_le_raw        gen_op_dcbz_l64_raw
#define gen_op_dcbz_l64_le_user       gen_op_dcbz_l64_user
#define gen_op_dcbz_l64_le_kernel     gen_op_dcbz_l64_kernel
#define gen_op_dcbz_l64_le_hypv       gen_op_dcbz_l64_hypv
#define gen_op_dcbz_l64_le_64_raw     gen_op_dcbz_l64_64_raw
#define gen_op_dcbz_l64_le_64_user    gen_op_dcbz_l64_64_user
#define gen_op_dcbz_l64_le_64_kernel  gen_op_dcbz_l64_64_kernel
#define gen_op_dcbz_l64_le_64_hypv    gen_op_dcbz_l64_64_hypv
        GEN_MEM_FUNCS(dcbz_l64),
3950
    },
3951
    /* 128 bytes cache line size */
3952
    {
3953 3954 3955 3956 3957 3958 3959 3960 3961
#define gen_op_dcbz_l128_le_raw       gen_op_dcbz_l128_raw
#define gen_op_dcbz_l128_le_user      gen_op_dcbz_l128_user
#define gen_op_dcbz_l128_le_kernel    gen_op_dcbz_l128_kernel
#define gen_op_dcbz_l128_le_hypv      gen_op_dcbz_l128_hypv
#define gen_op_dcbz_l128_le_64_raw    gen_op_dcbz_l128_64_raw
#define gen_op_dcbz_l128_le_64_user   gen_op_dcbz_l128_64_user
#define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
#define gen_op_dcbz_l128_le_64_hypv   gen_op_dcbz_l128_64_hypv
        GEN_MEM_FUNCS(dcbz_l128),
3962
    },
3963
    /* tunable cache line size */
3964
    {
3965 3966 3967 3968 3969 3970 3971 3972 3973
#define gen_op_dcbz_le_raw            gen_op_dcbz_raw
#define gen_op_dcbz_le_user           gen_op_dcbz_user
#define gen_op_dcbz_le_kernel         gen_op_dcbz_kernel
#define gen_op_dcbz_le_hypv           gen_op_dcbz_hypv
#define gen_op_dcbz_le_64_raw         gen_op_dcbz_64_raw
#define gen_op_dcbz_le_64_user        gen_op_dcbz_64_user
#define gen_op_dcbz_le_64_kernel      gen_op_dcbz_64_kernel
#define gen_op_dcbz_le_64_hypv        gen_op_dcbz_64_hypv
        GEN_MEM_FUNCS(dcbz),
3974
    },
3975
};
3976

3977 3978
static always_inline void handler_dcbz (DisasContext *ctx,
                                        int dcache_line_size)
3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
{
    int n;

    switch (dcache_line_size) {
    case 32:
        n = 0;
        break;
    case 64:
        n = 1;
        break;
    case 128:
        n = 2;
        break;
    default:
        n = 3;
        break;
    }
    op_dcbz(n);
}

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
4000
{
4001
    gen_addr_reg_index(cpu_T[0], ctx);
4002 4003 4004 4005
    handler_dcbz(ctx, ctx->dcache_line_size);
    gen_op_check_reservation();
}

4006
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4007
{
4008
    gen_addr_reg_index(cpu_T[0], ctx);
4009 4010 4011 4012
    if (ctx->opcode & 0x00200000)
        handler_dcbz(ctx, ctx->dcache_line_size);
    else
        handler_dcbz(ctx, -1);
B
bellard 已提交
4013
    gen_op_check_reservation();
B
bellard 已提交
4014 4015 4016
}

/* icbi */
4017
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
4018 4019 4020 4021 4022 4023 4024 4025 4026 4027
#define gen_op_icbi_le_raw       gen_op_icbi_raw
#define gen_op_icbi_le_user      gen_op_icbi_user
#define gen_op_icbi_le_kernel    gen_op_icbi_kernel
#define gen_op_icbi_le_hypv      gen_op_icbi_hypv
#define gen_op_icbi_le_64_raw    gen_op_icbi_64_raw
#define gen_op_icbi_le_64_user   gen_op_icbi_64_user
#define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
#define gen_op_icbi_le_64_hypv   gen_op_icbi_64_hypv
static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(icbi),
4028
};
4029

4030
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
4031
{
4032 4033
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
4034
    gen_addr_reg_index(cpu_T[0], ctx);
4035
    op_icbi();
B
bellard 已提交
4036 4037 4038 4039
}

/* Optional: */
/* dcba */
4040
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
4041
{
4042 4043 4044 4045
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4046 4047 4048 4049 4050 4051 4052
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
4053
#if defined(CONFIG_USER_ONLY)
4054
    GEN_EXCP_PRIVREG(ctx);
4055
#else
4056
    if (unlikely(!ctx->supervisor)) {
4057
        GEN_EXCP_PRIVREG(ctx);
4058
        return;
4059
    }
4060
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4061
    gen_op_load_sr();
A
aurel32 已提交
4062
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4063
#endif
B
bellard 已提交
4064 4065 4066
}

/* mfsrin */
4067
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4068
{
4069
#if defined(CONFIG_USER_ONLY)
4070
    GEN_EXCP_PRIVREG(ctx);
4071
#else
4072
    if (unlikely(!ctx->supervisor)) {
4073
        GEN_EXCP_PRIVREG(ctx);
4074
        return;
4075
    }
A
aurel32 已提交
4076
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4077 4078
    gen_op_srli_T1(28);
    gen_op_load_sr();
A
aurel32 已提交
4079
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4080
#endif
B
bellard 已提交
4081 4082 4083
}

/* mtsr */
B
bellard 已提交
4084
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
4085
{
4086
#if defined(CONFIG_USER_ONLY)
4087
    GEN_EXCP_PRIVREG(ctx);
4088
#else
4089
    if (unlikely(!ctx->supervisor)) {
4090
        GEN_EXCP_PRIVREG(ctx);
4091
        return;
4092
    }
A
aurel32 已提交
4093
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4094
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4095
    gen_op_store_sr();
4096
#endif
B
bellard 已提交
4097 4098 4099
}

/* mtsrin */
4100
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4101
{
4102
#if defined(CONFIG_USER_ONLY)
4103
    GEN_EXCP_PRIVREG(ctx);
4104
#else
4105
    if (unlikely(!ctx->supervisor)) {
4106
        GEN_EXCP_PRIVREG(ctx);
4107
        return;
4108
    }
A
aurel32 已提交
4109 4110
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4111 4112
    gen_op_srli_T1(28);
    gen_op_store_sr();
4113
#endif
B
bellard 已提交
4114 4115
}

4116 4117 4118
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
4119
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4120 4121 4122 4123 4124 4125 4126 4127
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
4128
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4129
    gen_op_load_slb();
A
aurel32 已提交
4130
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4131 4132 4133 4134
#endif
}

/* mfsrin */
4135 4136
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
4137 4138 4139 4140 4141 4142 4143 4144
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4145
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4146 4147
    gen_op_srli_T1(28);
    gen_op_load_slb();
A
aurel32 已提交
4148
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4149 4150 4151 4152
#endif
}

/* mtsr */
4153
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4154 4155 4156 4157 4158 4159 4160 4161
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4162
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4163
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4164 4165 4166 4167 4168
    gen_op_store_slb();
#endif
}

/* mtsrin */
4169 4170
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
4171 4172 4173 4174 4175 4176 4177 4178
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4179 4180
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4181 4182 4183 4184 4185 4186
    gen_op_srli_T1(28);
    gen_op_store_slb();
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4187 4188 4189
/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
4190
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4191
{
4192
#if defined(CONFIG_USER_ONLY)
4193
    GEN_EXCP_PRIVOPC(ctx);
4194
#else
4195
    if (unlikely(!ctx->supervisor)) {
4196
        GEN_EXCP_PRIVOPC(ctx);
4197
        return;
4198 4199 4200
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
4201 4202 4203
}

/* tlbie */
4204
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4205
{
4206
#if defined(CONFIG_USER_ONLY)
4207
    GEN_EXCP_PRIVOPC(ctx);
4208
#else
4209
    if (unlikely(!ctx->supervisor)) {
4210
        GEN_EXCP_PRIVOPC(ctx);
4211
        return;
4212
    }
A
aurel32 已提交
4213
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4214 4215 4216 4217 4218 4219
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4220
#endif
B
bellard 已提交
4221 4222 4223
}

/* tlbsync */
4224
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4225
{
4226
#if defined(CONFIG_USER_ONLY)
4227
    GEN_EXCP_PRIVOPC(ctx);
4228
#else
4229
    if (unlikely(!ctx->supervisor)) {
4230
        GEN_EXCP_PRIVOPC(ctx);
4231
        return;
4232 4233 4234 4235
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
4236
    GEN_STOP(ctx);
4237
#endif
B
bellard 已提交
4238 4239
}

J
j_mayer 已提交
4240 4241 4242 4243 4244
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4245
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4246 4247
#else
    if (unlikely(!ctx->supervisor)) {
4248
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4249 4250 4251 4252 4253 4254 4255 4256 4257 4258
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4259
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4260 4261
#else
    if (unlikely(!ctx->supervisor)) {
4262
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4263 4264
        return;
    }
A
aurel32 已提交
4265
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
4266 4267 4268 4269 4270
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
4271 4272
/***                              External control                         ***/
/* Optional: */
4273 4274
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4275 4276
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(eciwx),
4277
};
4278 4279
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ecowx),
4280
};
4281

4282
/* eciwx */
B
bellard 已提交
4283 4284
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
4285
    /* Should check EAR[E] & alignment ! */
4286
    gen_addr_reg_index(cpu_T[0], ctx);
4287
    op_eciwx();
A
aurel32 已提交
4288
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4289 4290 4291 4292 4293 4294
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
    /* Should check EAR[E] & alignment ! */
4295
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
4296
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4297 4298 4299 4300 4301 4302 4303
    op_ecowx();
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
A
aurel32 已提交
4304
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4305
    gen_op_POWER_abs();
A
aurel32 已提交
4306
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4307
    if (unlikely(Rc(ctx->opcode) != 0))
4308
        gen_set_Rc0(ctx, cpu_T[0]);
4309 4310 4311 4312 4313
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
A
aurel32 已提交
4314
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4315
    gen_op_POWER_abso();
A
aurel32 已提交
4316
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4317
    if (unlikely(Rc(ctx->opcode) != 0))
4318
        gen_set_Rc0(ctx, cpu_T[0]);
4319 4320 4321
}

/* clcs */
4322
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4323
{
A
aurel32 已提交
4324
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4325
    gen_op_POWER_clcs();
4326
    /* Rc=1 sets CR0 to an undefined state */
A
aurel32 已提交
4327
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4328 4329 4330 4331 4332
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4333 4334
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4335
    gen_op_POWER_div();
A
aurel32 已提交
4336
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4337
    if (unlikely(Rc(ctx->opcode) != 0))
4338
        gen_set_Rc0(ctx, cpu_T[0]);
4339 4340 4341 4342 4343
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4344 4345
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4346
    gen_op_POWER_divo();
A
aurel32 已提交
4347
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4348
    if (unlikely(Rc(ctx->opcode) != 0))
4349
        gen_set_Rc0(ctx, cpu_T[0]);
4350 4351 4352 4353 4354
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4355 4356
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4357
    gen_op_POWER_divs();
A
aurel32 已提交
4358
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4359
    if (unlikely(Rc(ctx->opcode) != 0))
4360
        gen_set_Rc0(ctx, cpu_T[0]);
4361 4362 4363 4364 4365
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4366 4367
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4368
    gen_op_POWER_divso();
A
aurel32 已提交
4369
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4370
    if (unlikely(Rc(ctx->opcode) != 0))
4371
        gen_set_Rc0(ctx, cpu_T[0]);
4372 4373 4374 4375 4376
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4377 4378
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4379
    gen_op_POWER_doz();
A
aurel32 已提交
4380
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4381
    if (unlikely(Rc(ctx->opcode) != 0))
4382
        gen_set_Rc0(ctx, cpu_T[0]);
4383 4384 4385 4386 4387
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4388 4389
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4390
    gen_op_POWER_dozo();
A
aurel32 已提交
4391
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4392
    if (unlikely(Rc(ctx->opcode) != 0))
4393
        gen_set_Rc0(ctx, cpu_T[0]);
4394 4395 4396 4397 4398
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4399
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4400
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4401
    gen_op_POWER_doz();
A
aurel32 已提交
4402
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4403 4404
}

4405 4406 4407
/* As lscbx load from memory byte after byte, it's always endian safe.
 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
 */
4408
#define op_POWER_lscbx(start, ra, rb)                                         \
4409
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423
#define gen_op_POWER_lscbx_64_raw       gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_64_user      gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_64_kernel    gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_64_hypv      gen_op_POWER_lscbx_hypv
#define gen_op_POWER_lscbx_le_raw       gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_le_user      gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_le_kernel    gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_le_hypv      gen_op_POWER_lscbx_hypv
#define gen_op_POWER_lscbx_le_64_raw    gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_le_64_user   gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_le_64_hypv   gen_op_POWER_lscbx_hypv
static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER_lscbx),
4424 4425 4426 4427 4428 4429 4430 4431
};

/* lscbx - lscbx. */
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
{
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

4432
    gen_addr_reg_index(cpu_T[0], ctx);
4433 4434 4435 4436
    if (ra == 0) {
        ra = rb;
    }
    /* NIP cannot be restored if the memory exception comes from an helper */
4437
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4438 4439 4440
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
    tcg_gen_shri_tl(cpu_T[2], cpu_xer, XER_CMP);
    tcg_gen_andi_tl(cpu_T[2], cpu_T[2], 0xFF);
4441
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
A
aurel32 已提交
4442 4443
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
4444
    if (unlikely(Rc(ctx->opcode) != 0))
4445
        gen_set_Rc0(ctx, cpu_T[0]);
4446 4447 4448 4449 4450
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4451 4452
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4453
    gen_op_POWER_maskg();
A
aurel32 已提交
4454
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4455
    if (unlikely(Rc(ctx->opcode) != 0))
4456
        gen_set_Rc0(ctx, cpu_T[0]);
4457 4458 4459 4460 4461
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4462 4463 4464
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4465
    gen_op_POWER_maskir();
A
aurel32 已提交
4466
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4467
    if (unlikely(Rc(ctx->opcode) != 0))
4468
        gen_set_Rc0(ctx, cpu_T[0]);
4469 4470 4471 4472 4473
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4474 4475
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4476
    gen_op_POWER_mul();
A
aurel32 已提交
4477
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4478
    if (unlikely(Rc(ctx->opcode) != 0))
4479
        gen_set_Rc0(ctx, cpu_T[0]);
4480 4481 4482 4483 4484
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4485 4486
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4487
    gen_op_POWER_mulo();
A
aurel32 已提交
4488
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4489
    if (unlikely(Rc(ctx->opcode) != 0))
4490
        gen_set_Rc0(ctx, cpu_T[0]);
4491 4492 4493 4494 4495
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4496
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4497
    gen_op_POWER_nabs();
A
aurel32 已提交
4498
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4499
    if (unlikely(Rc(ctx->opcode) != 0))
4500
        gen_set_Rc0(ctx, cpu_T[0]);
4501 4502 4503 4504 4505
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4506
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4507
    gen_op_POWER_nabso();
A
aurel32 已提交
4508
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4509
    if (unlikely(Rc(ctx->opcode) != 0))
4510
        gen_set_Rc0(ctx, cpu_T[0]);
4511 4512 4513 4514 4515 4516 4517 4518 4519
}

/* rlmi - rlmi. */
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
A
aurel32 已提交
4520 4521 4522
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4523
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
A
aurel32 已提交
4524
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4525
    if (unlikely(Rc(ctx->opcode) != 0))
4526
        gen_set_Rc0(ctx, cpu_T[0]);
4527 4528 4529 4530 4531
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4532 4533 4534
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4535
    gen_op_POWER_rrib();
A
aurel32 已提交
4536
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4537
    if (unlikely(Rc(ctx->opcode) != 0))
4538
        gen_set_Rc0(ctx, cpu_T[0]);
4539 4540 4541 4542 4543
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4544 4545
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4546
    gen_op_POWER_sle();
A
aurel32 已提交
4547
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4548
    if (unlikely(Rc(ctx->opcode) != 0))
4549
        gen_set_Rc0(ctx, cpu_T[0]);
4550 4551 4552 4553 4554
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4555 4556
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4557
    gen_op_POWER_sleq();
A
aurel32 已提交
4558
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4559
    if (unlikely(Rc(ctx->opcode) != 0))
4560
        gen_set_Rc0(ctx, cpu_T[0]);
4561 4562 4563 4564 4565
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4566
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4567
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4568
    gen_op_POWER_sle();
A
aurel32 已提交
4569
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4570
    if (unlikely(Rc(ctx->opcode) != 0))
4571
        gen_set_Rc0(ctx, cpu_T[0]);
4572 4573 4574 4575 4576
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4577
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4578
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4579
    gen_op_POWER_sleq();
A
aurel32 已提交
4580
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4581
    if (unlikely(Rc(ctx->opcode) != 0))
4582
        gen_set_Rc0(ctx, cpu_T[0]);
4583 4584 4585 4586 4587
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4588 4589
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4590
    gen_op_POWER_sllq();
A
aurel32 已提交
4591
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4592
    if (unlikely(Rc(ctx->opcode) != 0))
4593
        gen_set_Rc0(ctx, cpu_T[0]);
4594 4595 4596 4597 4598
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4599 4600
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4601
    gen_op_POWER_slq();
A
aurel32 已提交
4602
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4603
    if (unlikely(Rc(ctx->opcode) != 0))
4604
        gen_set_Rc0(ctx, cpu_T[0]);
4605 4606
}

4607
/* sraiq - sraiq. */
4608 4609
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4610
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4611
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4612
    gen_op_POWER_sraq();
A
aurel32 已提交
4613
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4614
    if (unlikely(Rc(ctx->opcode) != 0))
4615
        gen_set_Rc0(ctx, cpu_T[0]);
4616 4617 4618 4619 4620
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4621 4622
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4623
    gen_op_POWER_sraq();
A
aurel32 已提交
4624
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4625
    if (unlikely(Rc(ctx->opcode) != 0))
4626
        gen_set_Rc0(ctx, cpu_T[0]);
4627 4628 4629 4630 4631
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4632 4633
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4634
    gen_op_POWER_sre();
A
aurel32 已提交
4635
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4636
    if (unlikely(Rc(ctx->opcode) != 0))
4637
        gen_set_Rc0(ctx, cpu_T[0]);
4638 4639 4640 4641 4642
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4643 4644
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4645
    gen_op_POWER_srea();
A
aurel32 已提交
4646
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4647
    if (unlikely(Rc(ctx->opcode) != 0))
4648
        gen_set_Rc0(ctx, cpu_T[0]);
4649 4650 4651 4652 4653
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4654 4655
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4656
    gen_op_POWER_sreq();
A
aurel32 已提交
4657
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4658
    if (unlikely(Rc(ctx->opcode) != 0))
4659
        gen_set_Rc0(ctx, cpu_T[0]);
4660 4661 4662 4663 4664
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4665
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4666
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4667
    gen_op_POWER_srq();
A
aurel32 已提交
4668
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4669
    if (unlikely(Rc(ctx->opcode) != 0))
4670
        gen_set_Rc0(ctx, cpu_T[0]);
4671 4672 4673 4674 4675
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4676 4677
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4678
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4679
    gen_op_POWER_srlq();
A
aurel32 已提交
4680
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4681
    if (unlikely(Rc(ctx->opcode) != 0))
4682
        gen_set_Rc0(ctx, cpu_T[0]);
4683 4684 4685 4686 4687
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4688 4689
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4690
    gen_op_POWER_srlq();
A
aurel32 已提交
4691
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4692
    if (unlikely(Rc(ctx->opcode) != 0))
4693
        gen_set_Rc0(ctx, cpu_T[0]);
4694 4695 4696 4697 4698
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4699 4700
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4701
    gen_op_POWER_srq();
A
aurel32 已提交
4702
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4703
    if (unlikely(Rc(ctx->opcode) != 0))
4704
        gen_set_Rc0(ctx, cpu_T[0]);
4705 4706 4707 4708 4709 4710 4711
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4712
    GEN_EXCP_INVAL(ctx);
4713 4714 4715 4716 4717 4718
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4719
    GEN_EXCP_INVAL(ctx);
4720 4721 4722 4723 4724 4725
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4726
    GEN_EXCP_PRIVOPC(ctx);
4727 4728
#else
    if (unlikely(!ctx->supervisor)) {
4729
        GEN_EXCP_PRIVOPC(ctx);
4730 4731
        return;
    }
A
aurel32 已提交
4732
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4733
    gen_op_602_mfrom();
A
aurel32 已提交
4734
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4735 4736 4737 4738 4739
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
4740
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4741 4742
{
#if defined(CONFIG_USER_ONLY)
4743
    GEN_EXCP_PRIVOPC(ctx);
4744 4745
#else
    if (unlikely(!ctx->supervisor)) {
4746
        GEN_EXCP_PRIVOPC(ctx);
4747 4748
        return;
    }
A
aurel32 已提交
4749
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4750 4751 4752 4753 4754
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
4755
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4756 4757
{
#if defined(CONFIG_USER_ONLY)
4758
    GEN_EXCP_PRIVOPC(ctx);
4759 4760
#else
    if (unlikely(!ctx->supervisor)) {
4761
        GEN_EXCP_PRIVOPC(ctx);
4762 4763
        return;
    }
A
aurel32 已提交
4764
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4765 4766 4767 4768
    gen_op_6xx_tlbli();
#endif
}

4769 4770
/* 74xx TLB management */
/* tlbld */
4771
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4772 4773 4774 4775 4776 4777 4778 4779
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
A
aurel32 已提交
4780
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4781 4782 4783 4784 4785
    gen_op_74xx_tlbld();
#endif
}

/* tlbli */
4786
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4787 4788 4789 4790 4791 4792 4793 4794
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
A
aurel32 已提交
4795
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4796 4797 4798 4799
    gen_op_74xx_tlbli();
#endif
}

4800 4801 4802 4803 4804 4805 4806 4807 4808 4809
/* 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 已提交
4810
    /* Cache line invalidate: privileged and treated as no-op */
4811
#if defined(CONFIG_USER_ONLY)
4812
    GEN_EXCP_PRIVOPC(ctx);
4813 4814
#else
    if (unlikely(!ctx->supervisor)) {
4815
        GEN_EXCP_PRIVOPC(ctx);
4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829
        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)
4830
    GEN_EXCP_PRIVOPC(ctx);
4831 4832
#else
    if (unlikely(!ctx->supervisor)) {
4833
        GEN_EXCP_PRIVOPC(ctx);
4834 4835 4836 4837 4838
        return;
    }
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);

4839
    gen_addr_reg_index(cpu_T[0], ctx);
4840
    gen_op_POWER_mfsri();
A
aurel32 已提交
4841
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
4842
    if (ra != 0 && ra != rd)
A
aurel32 已提交
4843
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
4844 4845 4846 4847 4848 4849
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4850
    GEN_EXCP_PRIVOPC(ctx);
4851 4852
#else
    if (unlikely(!ctx->supervisor)) {
4853
        GEN_EXCP_PRIVOPC(ctx);
4854 4855
        return;
    }
4856
    gen_addr_reg_index(cpu_T[0], ctx);
4857
    gen_op_POWER_rac();
A
aurel32 已提交
4858
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4859 4860 4861 4862 4863 4864
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4865
    GEN_EXCP_PRIVOPC(ctx);
4866 4867
#else
    if (unlikely(!ctx->supervisor)) {
4868
        GEN_EXCP_PRIVOPC(ctx);
4869 4870 4871
        return;
    }
    gen_op_POWER_rfsvc();
4872
    GEN_SYNC(ctx);
4873 4874 4875 4876 4877 4878 4879
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */
4880
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
4881 4882
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
#define gen_op_POWER2_lfq_64_raw        gen_op_POWER2_lfq_raw
#define gen_op_POWER2_lfq_64_user       gen_op_POWER2_lfq_user
#define gen_op_POWER2_lfq_64_kernel     gen_op_POWER2_lfq_kernel
#define gen_op_POWER2_lfq_64_hypv       gen_op_POWER2_lfq_hypv
#define gen_op_POWER2_lfq_le_64_raw     gen_op_POWER2_lfq_le_raw
#define gen_op_POWER2_lfq_le_64_user    gen_op_POWER2_lfq_le_user
#define gen_op_POWER2_lfq_le_64_kernel  gen_op_POWER2_lfq_le_kernel
#define gen_op_POWER2_lfq_le_64_hypv    gen_op_POWER2_lfq_le_hypv
#define gen_op_POWER2_stfq_64_raw       gen_op_POWER2_stfq_raw
#define gen_op_POWER2_stfq_64_user      gen_op_POWER2_stfq_user
#define gen_op_POWER2_stfq_64_kernel    gen_op_POWER2_stfq_kernel
#define gen_op_POWER2_stfq_64_hypv      gen_op_POWER2_stfq_hypv
#define gen_op_POWER2_stfq_le_64_raw    gen_op_POWER2_stfq_le_raw
#define gen_op_POWER2_stfq_le_64_user   gen_op_POWER2_stfq_le_user
#define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
#define gen_op_POWER2_stfq_le_64_hypv   gen_op_POWER2_stfq_le_hypv
static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER2_lfq),
4901
};
4902 4903
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER2_stfq),
4904 4905 4906 4907 4908 4909
};

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4910
    gen_update_nip(ctx, ctx->nip - 4);
4911
    gen_addr_imm_index(cpu_T[0], ctx, 0);
4912
    op_POWER2_lfq();
A
aurel32 已提交
4913 4914
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4915 4916 4917 4918 4919 4920 4921 4922
}

/* lfqu */
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4923
    gen_update_nip(ctx, ctx->nip - 4);
4924
    gen_addr_imm_index(cpu_T[0], ctx, 0);
4925
    op_POWER2_lfq();
A
aurel32 已提交
4926 4927
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4928
    if (ra != 0)
A
aurel32 已提交
4929
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4930 4931 4932 4933 4934 4935 4936 4937
}

/* lfqux */
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4938
    gen_update_nip(ctx, ctx->nip - 4);
4939
    gen_addr_reg_index(cpu_T[0], ctx);
4940
    op_POWER2_lfq();
A
aurel32 已提交
4941 4942
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4943
    if (ra != 0)
A
aurel32 已提交
4944
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4945 4946 4947 4948 4949 4950
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4951
    gen_update_nip(ctx, ctx->nip - 4);
4952
    gen_addr_reg_index(cpu_T[0], ctx);
4953
    op_POWER2_lfq();
A
aurel32 已提交
4954 4955
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4956 4957 4958 4959 4960 4961
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4962
    gen_update_nip(ctx, ctx->nip - 4);
4963
    gen_addr_imm_index(cpu_T[0], ctx, 0);
A
aurel32 已提交
4964 4965
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4966 4967 4968 4969 4970 4971 4972 4973 4974
    op_POWER2_stfq();
}

/* stfqu */
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4975
    gen_update_nip(ctx, ctx->nip - 4);
4976
    gen_addr_imm_index(cpu_T[0], ctx, 0);
A
aurel32 已提交
4977 4978
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4979 4980
    op_POWER2_stfq();
    if (ra != 0)
A
aurel32 已提交
4981
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4982 4983 4984 4985 4986 4987 4988 4989
}

/* stfqux */
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4990
    gen_update_nip(ctx, ctx->nip - 4);
4991
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
4992 4993
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4994 4995
    op_POWER2_stfq();
    if (ra != 0)
A
aurel32 已提交
4996
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4997 4998 4999 5000 5001 5002
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
5003
    gen_update_nip(ctx, ctx->nip - 4);
5004
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
5005 5006
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5007 5008 5009 5010
    op_POWER2_stfq();
}

/* BookE specific instructions */
5011
/* XXX: not implemented on 440 ? */
5012
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5013 5014
{
    /* XXX: TODO */
5015
    GEN_EXCP_INVAL(ctx);
5016 5017
}

5018
/* XXX: not implemented on 440 ? */
5019
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5020 5021
{
#if defined(CONFIG_USER_ONLY)
5022
    GEN_EXCP_PRIVOPC(ctx);
5023 5024
#else
    if (unlikely(!ctx->supervisor)) {
5025
        GEN_EXCP_PRIVOPC(ctx);
5026 5027
        return;
    }
5028
    gen_addr_reg_index(cpu_T[0], ctx);
5029
    /* Use the same micro-ops as for tlbie */
5030 5031 5032 5033 5034 5035
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
5036 5037 5038 5039
#endif
}

/* All 405 MAC instructions are translated here */
5040 5041 5042
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
5043
{
A
aurel32 已提交
5044 5045
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[ra]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rb]);
5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
        gen_op_405_mulchw();
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
        gen_op_405_mulchwu();
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
        gen_op_405_mulhhw();
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
        gen_op_405_mulhhwu();
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
        gen_op_405_mullhw();
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
        gen_op_405_mullhwu();
        break;
    }
    if (opc2 & 0x02) {
        /* nmultiply-and-accumulate (0x0E) */
        gen_op_neg();
    }
    if (opc2 & 0x04) {
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
A
aurel32 已提交
5096
        tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rt]);
5097
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
5098 5099 5100 5101 5102
        gen_op_405_add_T0_T2();
    }
    if (opc3 & 0x10) {
        /* Check overflow */
        if (opc3 & 0x01)
5103
            gen_op_check_addo();
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113
        else
            gen_op_405_check_ovu();
    }
    if (opc3 & 0x02) {
        /* Saturate */
        if (opc3 & 0x01)
            gen_op_405_check_sat();
        else
            gen_op_405_check_satu();
    }
A
aurel32 已提交
5114
    tcg_gen_mov_tl(cpu_gpr[rt], cpu_T[0]);
5115 5116
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
5117
        gen_set_Rc0(ctx, cpu_T[0]);
5118 5119 5120
    }
}

5121 5122
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5123 5124 5125 5126 5127 5128
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5129
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5130
/* macchwo   - macchwo.   */
5131
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5132
/* macchws   - macchws.   */
5133
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5134
/* macchwso  - macchwso.  */
5135
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5136
/* macchwsu  - macchwsu.  */
5137
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5138
/* macchwsuo - macchwsuo. */
5139
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5140
/* macchwu   - macchwu.   */
5141
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5142
/* macchwuo  - macchwuo.  */
5143
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5144
/* machhw    - machhw.    */
5145
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5146
/* machhwo   - machhwo.   */
5147
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5148
/* machhws   - machhws.   */
5149
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5150
/* machhwso  - machhwso.  */
5151
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5152
/* machhwsu  - machhwsu.  */
5153
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5154
/* machhwsuo - machhwsuo. */
5155
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5156
/* machhwu   - machhwu.   */
5157
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5158
/* machhwuo  - machhwuo.  */
5159
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5160
/* maclhw    - maclhw.    */
5161
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5162
/* maclhwo   - maclhwo.   */
5163
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5164
/* maclhws   - maclhws.   */
5165
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5166
/* maclhwso  - maclhwso.  */
5167
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5168
/* maclhwu   - maclhwu.   */
5169
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5170
/* maclhwuo  - maclhwuo.  */
5171
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5172
/* maclhwsu  - maclhwsu.  */
5173
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5174
/* maclhwsuo - maclhwsuo. */
5175
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5176
/* nmacchw   - nmacchw.   */
5177
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5178
/* nmacchwo  - nmacchwo.  */
5179
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5180
/* nmacchws  - nmacchws.  */
5181
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5182
/* nmacchwso - nmacchwso. */
5183
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5184
/* nmachhw   - nmachhw.   */
5185
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5186
/* nmachhwo  - nmachhwo.  */
5187
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5188
/* nmachhws  - nmachhws.  */
5189
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5190
/* nmachhwso - nmachhwso. */
5191
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5192
/* nmaclhw   - nmaclhw.   */
5193
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5194
/* nmaclhwo  - nmaclhwo.  */
5195
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5196
/* nmaclhws  - nmaclhws.  */
5197
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5198
/* nmaclhwso - nmaclhwso. */
5199
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5200 5201

/* mulchw  - mulchw.  */
5202
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5203
/* mulchwu - mulchwu. */
5204
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5205
/* mulhhw  - mulhhw.  */
5206
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5207
/* mulhhwu - mulhhwu. */
5208
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5209
/* mullhw  - mullhw.  */
5210
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5211
/* mullhwu - mullhwu. */
5212
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5213 5214

/* mfdcr */
5215
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5216 5217
{
#if defined(CONFIG_USER_ONLY)
5218
    GEN_EXCP_PRIVREG(ctx);
5219 5220 5221 5222
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5223
        GEN_EXCP_PRIVREG(ctx);
5224 5225
        return;
    }
5226
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5227
    gen_op_load_dcr();
A
aurel32 已提交
5228
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5229 5230 5231 5232
#endif
}

/* mtdcr */
5233
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5234 5235
{
#if defined(CONFIG_USER_ONLY)
5236
    GEN_EXCP_PRIVREG(ctx);
5237 5238 5239 5240
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5241
        GEN_EXCP_PRIVREG(ctx);
5242 5243
        return;
    }
5244
    tcg_gen_movi_tl(cpu_T[0], dcrn);
A
aurel32 已提交
5245
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5246 5247 5248 5249 5250
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
5251
/* XXX: not implemented on 440 ? */
5252
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5253 5254
{
#if defined(CONFIG_USER_ONLY)
5255
    GEN_EXCP_PRIVREG(ctx);
5256 5257
#else
    if (unlikely(!ctx->supervisor)) {
5258
        GEN_EXCP_PRIVREG(ctx);
5259 5260
        return;
    }
A
aurel32 已提交
5261
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5262
    gen_op_load_dcr();
A
aurel32 已提交
5263
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5264
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5265 5266 5267 5268
#endif
}

/* mtdcrx */
5269
/* XXX: not implemented on 440 ? */
5270
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5271 5272
{
#if defined(CONFIG_USER_ONLY)
5273
    GEN_EXCP_PRIVREG(ctx);
5274 5275
#else
    if (unlikely(!ctx->supervisor)) {
5276
        GEN_EXCP_PRIVREG(ctx);
5277 5278
        return;
    }
A
aurel32 已提交
5279 5280
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5281
    gen_op_store_dcr();
5282
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5283 5284 5285
#endif
}

5286 5287 5288
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
A
aurel32 已提交
5289
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5290
    gen_op_load_dcr();
A
aurel32 已提交
5291
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5292 5293 5294 5295 5296 5297
    /* 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)
{
A
aurel32 已提交
5298 5299
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5300 5301 5302 5303
    gen_op_store_dcr();
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

5304 5305 5306 5307
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5308
    GEN_EXCP_PRIVOPC(ctx);
5309 5310
#else
    if (unlikely(!ctx->supervisor)) {
5311
        GEN_EXCP_PRIVOPC(ctx);
5312 5313 5314 5315 5316 5317 5318 5319 5320 5321
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5322
    GEN_EXCP_PRIVOPC(ctx);
5323
#else
A
aurel32 已提交
5324
    TCGv EA, val;
5325
    if (unlikely(!ctx->supervisor)) {
5326
        GEN_EXCP_PRIVOPC(ctx);
5327 5328
        return;
    }
A
aurel32 已提交
5329 5330 5331 5332 5333 5334 5335
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_reg_index(EA, ctx);
    val = tcg_temp_new(TCG_TYPE_TL);
    gen_qemu_ld32u(val, EA, ctx->mem_idx);
    tcg_temp_free(val);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
    tcg_temp_free(EA);
5336 5337 5338 5339
#endif
}

/* icbt */
5340
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351
{
    /* 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)
5352
    GEN_EXCP_PRIVOPC(ctx);
5353 5354
#else
    if (unlikely(!ctx->supervisor)) {
5355
        GEN_EXCP_PRIVOPC(ctx);
5356 5357 5358 5359 5360 5361 5362 5363 5364 5365
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5366
    GEN_EXCP_PRIVOPC(ctx);
5367 5368
#else
    if (unlikely(!ctx->supervisor)) {
5369
        GEN_EXCP_PRIVOPC(ctx);
5370 5371 5372 5373 5374 5375 5376
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
5377
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5378 5379
{
#if defined(CONFIG_USER_ONLY)
5380
    GEN_EXCP_PRIVOPC(ctx);
5381 5382
#else
    if (unlikely(!ctx->supervisor)) {
5383
        GEN_EXCP_PRIVOPC(ctx);
5384 5385 5386 5387
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
5388
    GEN_SYNC(ctx);
5389 5390 5391 5392 5393 5394
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
5395
    GEN_EXCP_PRIVOPC(ctx);
5396 5397
#else
    if (unlikely(!ctx->supervisor)) {
5398
        GEN_EXCP_PRIVOPC(ctx);
5399 5400 5401 5402
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
5403
    GEN_SYNC(ctx);
5404 5405 5406 5407
#endif
}

/* BookE specific */
5408
/* XXX: not implemented on 440 ? */
5409
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5410 5411
{
#if defined(CONFIG_USER_ONLY)
5412
    GEN_EXCP_PRIVOPC(ctx);
5413 5414
#else
    if (unlikely(!ctx->supervisor)) {
5415
        GEN_EXCP_PRIVOPC(ctx);
5416 5417 5418
        return;
    }
    /* Restore CPU state */
5419
    gen_op_rfdi();
5420
    GEN_SYNC(ctx);
5421 5422 5423
#endif
}

5424
/* XXX: not implemented on 440 ? */
5425
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5426 5427
{
#if defined(CONFIG_USER_ONLY)
5428
    GEN_EXCP_PRIVOPC(ctx);
5429 5430
#else
    if (unlikely(!ctx->supervisor)) {
5431
        GEN_EXCP_PRIVOPC(ctx);
5432 5433 5434 5435
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
5436
    GEN_SYNC(ctx);
5437 5438
#endif
}
5439

5440
/* TLB management - PowerPC 405 implementation */
5441
/* tlbre */
5442
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5443 5444
{
#if defined(CONFIG_USER_ONLY)
5445
    GEN_EXCP_PRIVOPC(ctx);
5446 5447
#else
    if (unlikely(!ctx->supervisor)) {
5448
        GEN_EXCP_PRIVOPC(ctx);
5449 5450 5451 5452
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
A
aurel32 已提交
5453
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5454
        gen_op_4xx_tlbre_hi();
A
aurel32 已提交
5455
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5456 5457
        break;
    case 1:
A
aurel32 已提交
5458
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5459
        gen_op_4xx_tlbre_lo();
A
aurel32 已提交
5460
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5461 5462
        break;
    default:
5463
        GEN_EXCP_INVAL(ctx);
5464
        break;
5465
    }
5466 5467 5468
#endif
}

5469
/* tlbsx - tlbsx. */
5470
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5471 5472
{
#if defined(CONFIG_USER_ONLY)
5473
    GEN_EXCP_PRIVOPC(ctx);
5474 5475
#else
    if (unlikely(!ctx->supervisor)) {
5476
        GEN_EXCP_PRIVOPC(ctx);
5477 5478
        return;
    }
5479
    gen_addr_reg_index(cpu_T[0], ctx);
5480
    gen_op_4xx_tlbsx();
5481
    if (Rc(ctx->opcode))
5482
        gen_op_4xx_tlbsx_check();
A
aurel32 已提交
5483
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5484
#endif
B
bellard 已提交
5485 5486
}

5487
/* tlbwe */
5488
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5489
{
5490
#if defined(CONFIG_USER_ONLY)
5491
    GEN_EXCP_PRIVOPC(ctx);
5492 5493
#else
    if (unlikely(!ctx->supervisor)) {
5494
        GEN_EXCP_PRIVOPC(ctx);
5495 5496 5497 5498
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
A
aurel32 已提交
5499 5500
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5501 5502 5503
        gen_op_4xx_tlbwe_hi();
        break;
    case 1:
A
aurel32 已提交
5504 5505
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5506 5507 5508
        gen_op_4xx_tlbwe_lo();
        break;
    default:
5509
        GEN_EXCP_INVAL(ctx);
5510
        break;
5511
    }
5512 5513 5514
#endif
}

5515
/* TLB management - PowerPC 440 implementation */
5516
/* tlbre */
5517
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5518 5519
{
#if defined(CONFIG_USER_ONLY)
5520
    GEN_EXCP_PRIVOPC(ctx);
5521 5522
#else
    if (unlikely(!ctx->supervisor)) {
5523
        GEN_EXCP_PRIVOPC(ctx);
5524 5525 5526 5527 5528 5529
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
A
aurel32 已提交
5530
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5531
        gen_op_440_tlbre(rB(ctx->opcode));
A
aurel32 已提交
5532
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5533 5534
        break;
    default:
5535
        GEN_EXCP_INVAL(ctx);
5536 5537 5538 5539 5540 5541
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5542
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5543 5544
{
#if defined(CONFIG_USER_ONLY)
5545
    GEN_EXCP_PRIVOPC(ctx);
5546 5547
#else
    if (unlikely(!ctx->supervisor)) {
5548
        GEN_EXCP_PRIVOPC(ctx);
5549 5550
        return;
    }
5551
    gen_addr_reg_index(cpu_T[0], ctx);
5552
    gen_op_440_tlbsx();
5553
    if (Rc(ctx->opcode))
5554
        gen_op_4xx_tlbsx_check();
A
aurel32 已提交
5555
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5556 5557 5558 5559
#endif
}

/* tlbwe */
5560
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5561 5562
{
#if defined(CONFIG_USER_ONLY)
5563
    GEN_EXCP_PRIVOPC(ctx);
5564 5565
#else
    if (unlikely(!ctx->supervisor)) {
5566
        GEN_EXCP_PRIVOPC(ctx);
5567 5568 5569 5570 5571 5572
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
A
aurel32 已提交
5573 5574
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5575
        gen_op_440_tlbwe(rB(ctx->opcode));
5576 5577
        break;
    default:
5578
        GEN_EXCP_INVAL(ctx);
5579 5580 5581 5582 5583
        break;
    }
#endif
}

5584
/* wrtee */
5585
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5586 5587
{
#if defined(CONFIG_USER_ONLY)
5588
    GEN_EXCP_PRIVOPC(ctx);
5589 5590
#else
    if (unlikely(!ctx->supervisor)) {
5591
        GEN_EXCP_PRIVOPC(ctx);
5592 5593
        return;
    }
A
aurel32 已提交
5594
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5595
    gen_op_wrte();
J
j_mayer 已提交
5596 5597 5598
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5599
    GEN_STOP(ctx);
5600 5601 5602 5603
#endif
}

/* wrteei */
5604
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5605 5606
{
#if defined(CONFIG_USER_ONLY)
5607
    GEN_EXCP_PRIVOPC(ctx);
5608 5609
#else
    if (unlikely(!ctx->supervisor)) {
5610
        GEN_EXCP_PRIVOPC(ctx);
5611 5612
        return;
    }
5613
    tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5614
    gen_op_wrte();
J
j_mayer 已提交
5615 5616 5617
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5618
    GEN_STOP(ctx);
5619 5620 5621
#endif
}

J
j_mayer 已提交
5622
/* PowerPC 440 specific instructions */
5623 5624 5625
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
A
aurel32 已提交
5626 5627
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5628
    gen_op_440_dlmzb();
A
aurel32 已提交
5629
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
A
aurel32 已提交
5630 5631
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
5632 5633
    if (Rc(ctx->opcode)) {
        gen_op_440_dlmzb_update_Rc();
A
aurel32 已提交
5634
        tcg_gen_andi_i32(cpu_crf[0], cpu_T[0], 0xf);
5635 5636 5637 5638 5639 5640 5641 5642 5643 5644
    }
}

/* mbar replaces eieio on 440 */
GEN_HANDLER(mbar, 0x1F, 0x16, 0x13, 0x001FF801, PPC_BOOKE)
{
    /* interpreted as no-op */
}

/* msync replaces sync on 440 */
5645
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5646 5647 5648 5649 5650
{
    /* interpreted as no-op */
}

/* icbt */
5651
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5652 5653 5654 5655 5656
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
5657 5658
}

5659 5660 5661
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */

5662 5663 5664 5665 5666 5667 5668 5669 5670
static always_inline void gen_load_avr(int t, int reg) {
    tcg_gen_mov_i64(cpu_AVRh[t], cpu_avrh[reg]);
    tcg_gen_mov_i64(cpu_AVRl[t], cpu_avrl[reg]);
}

static always_inline void gen_store_avr(int reg, int t) {
    tcg_gen_mov_i64(cpu_avrh[reg], cpu_AVRh[t]);
    tcg_gen_mov_i64(cpu_avrl[reg], cpu_AVRl[t]);
}
5671 5672 5673

#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_VR_LD_TABLE(name)                                                  \
5674 5675
static GenOpFunc *gen_op_vr_l##name[NB_MEM_FUNCS] = {                         \
    GEN_MEM_FUNCS(vr_l##name),                                                \
5676 5677
};
#define OP_VR_ST_TABLE(name)                                                  \
5678 5679
static GenOpFunc *gen_op_vr_st##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(vr_st##name),                                               \
5680 5681 5682 5683 5684 5685 5686 5687 5688
};

#define GEN_VR_LDX(name, opc2, opc3)                                          \
GEN_HANDLER(l##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)               \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
5689
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5690
    op_vr_ldst(vr_l##name);                                                   \
5691
    gen_store_avr(rD(ctx->opcode), 0);                                        \
5692 5693 5694 5695 5696 5697 5698 5699 5700
}

#define GEN_VR_STX(name, opc2, opc3)                                          \
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
5701
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5702
    gen_load_avr(0, rS(ctx->opcode));                                         \
5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717
    op_vr_ldst(vr_st##name);                                                  \
}

OP_VR_LD_TABLE(vx);
GEN_VR_LDX(vx, 0x07, 0x03);
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
#define gen_op_vr_lvxl gen_op_vr_lvx
GEN_VR_LDX(vxl, 0x07, 0x0B);

OP_VR_ST_TABLE(vx);
GEN_VR_STX(vx, 0x07, 0x07);
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
#define gen_op_vr_stvxl gen_op_vr_stvx
GEN_VR_STX(vxl, 0x07, 0x0F);

5718 5719
/***                           SPE extension                               ***/
/* Register moves */
5720

A
aurel32 已提交
5721 5722 5723 5724
static always_inline void gen_load_gpr64(TCGv t, int reg) {
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
#else
P
pbrook 已提交
5725
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
5726
#endif
A
aurel32 已提交
5727
}
5728

A
aurel32 已提交
5729 5730 5731 5732 5733
static always_inline void gen_store_gpr64(int reg, TCGv t) {
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(cpu_gpr[reg], t);
#else
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
5734
    TCGv tmp = tcg_temp_new(TCG_TYPE_I64);
A
aurel32 已提交
5735 5736 5737
    tcg_gen_shri_i64(tmp, t, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
    tcg_temp_free(tmp);
5738
#endif
A
aurel32 已提交
5739
}
5740

5741 5742 5743 5744 5745 5746 5747 5748 5749 5750
#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 */
5751
static always_inline void gen_speundef (DisasContext *ctx)
5752
{
5753
    GEN_EXCP_INVAL(ctx);
5754 5755 5756
}

/* SPE load and stores */
5757
static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
5758 5759 5760
{
    target_long simm = rB(ctx->opcode);

5761 5762 5763 5764 5765 5766
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(EA, simm << sh);
    else if (likely(simm != 0))
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm << sh);
    else
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
5767 5768 5769 5770
}

#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_SPE_LD_TABLE(name)                                                 \
5771 5772
static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(spe_l##name),                                               \
5773 5774
};
#define OP_SPE_ST_TABLE(name)                                                 \
5775 5776
static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = {                       \
    GEN_MEM_FUNCS(spe_st##name),                                              \
5777
};
5778 5779

#define GEN_SPE_LD(name, sh)                                                  \
5780
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5781 5782
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5783
        GEN_EXCP_NO_AP(ctx);                                                  \
5784 5785
        return;                                                               \
    }                                                                         \
5786
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
5787
    op_spe_ldst(spe_l##name);                                                 \
A
aurel32 已提交
5788
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5789 5790 5791
}

#define GEN_SPE_LDX(name)                                                     \
5792
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
5793 5794
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5795
        GEN_EXCP_NO_AP(ctx);                                                  \
5796 5797
        return;                                                               \
    }                                                                         \
5798
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5799
    op_spe_ldst(spe_l##name);                                                 \
A
aurel32 已提交
5800
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5801 5802 5803 5804 5805 5806 5807 5808
}

#define GEN_SPEOP_LD(name, sh)                                                \
OP_SPE_LD_TABLE(name);                                                        \
GEN_SPE_LD(name, sh);                                                         \
GEN_SPE_LDX(name)

#define GEN_SPE_ST(name, sh)                                                  \
5809
static always_inline void gen_evst##name (DisasContext *ctx)                  \
5810 5811
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5812
        GEN_EXCP_NO_AP(ctx);                                                  \
5813 5814
        return;                                                               \
    }                                                                         \
5815
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
A
aurel32 已提交
5816
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5817 5818 5819 5820
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPE_STX(name)                                                     \
5821
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
5822 5823
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5824
        GEN_EXCP_NO_AP(ctx);                                                  \
5825 5826
        return;                                                               \
    }                                                                         \
5827
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
5828
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPEOP_ST(name, sh)                                                \
OP_SPE_ST_TABLE(name);                                                        \
GEN_SPE_ST(name, sh);                                                         \
GEN_SPE_STX(name)

#define GEN_SPEOP_LDST(name, sh)                                              \
GEN_SPEOP_LD(name, sh);                                                       \
GEN_SPEOP_ST(name, sh)

/* SPE arithmetic and logic */
#define GEN_SPEOP_ARITH2(name)                                                \
5843
static always_inline void gen_##name (DisasContext *ctx)                      \
5844 5845
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5846
        GEN_EXCP_NO_AP(ctx);                                                  \
5847 5848
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5849 5850
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5851
    gen_op_##name();                                                          \
A
aurel32 已提交
5852
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5853 5854
}

5855
#define GEN_SPEOP_TCG_ARITH2(name, tcg_op)                                    \
5856 5857 5858 5859 5860 5861 5862 5863 5864 5865
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
        GEN_EXCP_NO_AP(ctx);                                                  \
        return;                                                               \
    }                                                                         \
    TCGv t0 = tcg_temp_new(TCG_TYPE_I64);                                     \
    TCGv t1 = tcg_temp_new(TCG_TYPE_I64);                                     \
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
5866
    tcg_op(t0, t0, t1);                                                       \
5867 5868 5869 5870 5871
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free(t0);                                                        \
    tcg_temp_free(t1);                                                        \
}

5872
#define GEN_SPEOP_ARITH1(name)                                                \
5873
static always_inline void gen_##name (DisasContext *ctx)                      \
5874 5875
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5876
        GEN_EXCP_NO_AP(ctx);                                                  \
5877 5878
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5879
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5880
    gen_op_##name();                                                          \
A
aurel32 已提交
5881
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5882 5883 5884
}

#define GEN_SPEOP_COMP(name)                                                  \
5885
static always_inline void gen_##name (DisasContext *ctx)                      \
5886 5887
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5888
        GEN_EXCP_NO_AP(ctx);                                                  \
5889 5890
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5891 5892
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5893
    gen_op_##name();                                                          \
A
aurel32 已提交
5894
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
5895 5896 5897
}

/* Logical */
5898 5899 5900 5901 5902 5903 5904 5905
GEN_SPEOP_TCG_ARITH2(evand, tcg_gen_and_i64);
GEN_SPEOP_TCG_ARITH2(evandc, tcg_gen_andc_i64);
GEN_SPEOP_TCG_ARITH2(evxor, tcg_gen_xor_i64);
GEN_SPEOP_TCG_ARITH2(evor, tcg_gen_or_i64);
GEN_SPEOP_TCG_ARITH2(evnor, tcg_gen_nor_i64);
GEN_SPEOP_TCG_ARITH2(eveqv, tcg_gen_eqv_i64);
GEN_SPEOP_TCG_ARITH2(evorc, tcg_gen_orc_i64);
GEN_SPEOP_TCG_ARITH2(evnand, tcg_gen_nand_i64);
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924
GEN_SPEOP_ARITH2(evsrwu);
GEN_SPEOP_ARITH2(evsrws);
GEN_SPEOP_ARITH2(evslw);
GEN_SPEOP_ARITH2(evrlw);
GEN_SPEOP_ARITH2(evmergehi);
GEN_SPEOP_ARITH2(evmergelo);
GEN_SPEOP_ARITH2(evmergehilo);
GEN_SPEOP_ARITH2(evmergelohi);

/* Arithmetic */
GEN_SPEOP_ARITH2(evaddw);
GEN_SPEOP_ARITH2(evsubfw);
GEN_SPEOP_ARITH1(evabs);
GEN_SPEOP_ARITH1(evneg);
GEN_SPEOP_ARITH1(evextsb);
GEN_SPEOP_ARITH1(evextsh);
GEN_SPEOP_ARITH1(evrndw);
GEN_SPEOP_ARITH1(evcntlzw);
GEN_SPEOP_ARITH1(evcntlsw);
5925
static always_inline void gen_brinc (DisasContext *ctx)
5926 5927
{
    /* Note: brinc is usable even if SPE is disabled */
A
aurel32 已提交
5928 5929
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5930
    gen_op_brinc();
A
aurel32 已提交
5931
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5932 5933 5934
}

#define GEN_SPEOP_ARITH_IMM2(name)                                            \
5935
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5936 5937
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5938
        GEN_EXCP_NO_AP(ctx);                                                  \
5939 5940
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5941
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
5942 5943
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
A
aurel32 已提交
5944
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5945 5946 5947
}

#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
5948
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5949 5950
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5951
        GEN_EXCP_NO_AP(ctx);                                                  \
5952 5953
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5954
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5955 5956
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
A
aurel32 已提交
5957
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970
}

GEN_SPEOP_ARITH_IMM2(evaddw);
#define gen_evaddiw gen_evaddwi
GEN_SPEOP_ARITH_IMM2(evsubfw);
#define gen_evsubifw gen_evsubfwi
GEN_SPEOP_LOGIC_IMM2(evslw);
GEN_SPEOP_LOGIC_IMM2(evsrwu);
#define gen_evsrwis gen_evsrwsi
GEN_SPEOP_LOGIC_IMM2(evsrws);
#define gen_evsrwiu gen_evsrwui
GEN_SPEOP_LOGIC_IMM2(evrlw);

5971
static always_inline void gen_evsplati (DisasContext *ctx)
5972 5973 5974 5975
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

    gen_op_splatwi_T0_64(imm);
A
aurel32 已提交
5976
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5977 5978
}

5979
static always_inline void gen_evsplatfi (DisasContext *ctx)
5980 5981 5982 5983
{
    uint32_t imm = rA(ctx->opcode) << 27;

    gen_op_splatwi_T0_64(imm);
A
aurel32 已提交
5984
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019
}

/* Comparison */
GEN_SPEOP_COMP(evcmpgtu);
GEN_SPEOP_COMP(evcmpgts);
GEN_SPEOP_COMP(evcmpltu);
GEN_SPEOP_COMP(evcmplts);
GEN_SPEOP_COMP(evcmpeq);

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); ////

6020
static always_inline void gen_evsel (DisasContext *ctx)
6021 6022
{
    if (unlikely(!ctx->spe_enabled)) {
6023
        GEN_EXCP_NO_AP(ctx);
6024 6025
        return;
    }
A
aurel32 已提交
6026
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[ctx->opcode & 0x7]);
A
aurel32 已提交
6027 6028
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));
6029
    gen_op_evsel();
A
aurel32 已提交
6030
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
6031 6032
}

6033
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6034 6035 6036
{
    gen_evsel(ctx);
}
6037
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6038 6039 6040
{
    gen_evsel(ctx);
}
6041
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6042 6043 6044
{
    gen_evsel(ctx);
}
6045
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059
{
    gen_evsel(ctx);
}

/* Load and stores */
GEN_SPEOP_LDST(dd, 3);
GEN_SPEOP_LDST(dw, 3);
GEN_SPEOP_LDST(dh, 3);
GEN_SPEOP_LDST(whe, 2);
GEN_SPEOP_LD(whou, 2);
GEN_SPEOP_LD(whos, 2);
GEN_SPEOP_ST(who, 2);

#define _GEN_OP_SPE_STWWE(suffix)                                             \
6060
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
6061 6062 6063 6064 6065
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
6066
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
6067 6068 6069 6070 6071 6072 6073 6074
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_##suffix();                                           \
}
#if defined(TARGET_PPC64)
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
6075
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
6076 6077 6078 6079
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
6080
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_64_##suffix();                                        \
}
#else
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix)
#endif
#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_STWWE(raw);
#else /* defined(CONFIG_USER_ONLY) */
GEN_OP_SPE_STWWE(user);
6094 6095
GEN_OP_SPE_STWWE(kernel);
GEN_OP_SPE_STWWE(hypv);
6096 6097 6098 6099 6100
#endif /* defined(CONFIG_USER_ONLY) */
GEN_SPEOP_ST(wwe, 2);
GEN_SPEOP_ST(wwo, 2);

#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
6101
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
6102 6103 6104 6105 6106 6107
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
6108
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
6109 6110 6111 6112 6113 6114
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
6115
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_extsh_T1_64();                                                     \
}

#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_LHE(raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
GEN_OP_SPE_LHE(le_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
GEN_OP_SPE_LHX(raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
GEN_OP_SPE_LHX(le_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
GEN_OP_SPE_LHE(le_64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
GEN_OP_SPE_LHX(64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
GEN_OP_SPE_LHX(le_64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
#endif
#else
GEN_OP_SPE_LHE(user);
6146 6147
GEN_OP_SPE_LHE(kernel);
GEN_OP_SPE_LHE(hypv);
6148
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6149 6150
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
6151
GEN_OP_SPE_LHE(le_user);
6152 6153
GEN_OP_SPE_LHE(le_kernel);
GEN_OP_SPE_LHE(le_hypv);
6154
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6155 6156
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
6157
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6158 6159
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
6160
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6161 6162
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
6163
GEN_OP_SPE_LHX(user);
6164 6165
GEN_OP_SPE_LHX(kernel);
GEN_OP_SPE_LHX(hypv);
6166
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6167 6168
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
6169
GEN_OP_SPE_LHX(le_user);
6170 6171
GEN_OP_SPE_LHX(le_kernel);
GEN_OP_SPE_LHX(le_hypv);
6172
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6173 6174
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
6175 6176
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_user);
6177 6178
GEN_OP_SPE_LHE(64_kernel);
GEN_OP_SPE_LHE(64_hypv);
6179
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6180 6181
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
6182
GEN_OP_SPE_LHE(le_64_user);
6183 6184
GEN_OP_SPE_LHE(le_64_kernel);
GEN_OP_SPE_LHE(le_64_hypv);
6185
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6186 6187
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
6188
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6189 6190
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
6191
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6192 6193
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
6194
GEN_OP_SPE_LHX(64_user);
6195 6196
GEN_OP_SPE_LHX(64_kernel);
GEN_OP_SPE_LHX(64_hypv);
6197
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6198 6199
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
6200
GEN_OP_SPE_LHX(le_64_user);
6201 6202
GEN_OP_SPE_LHX(le_64_kernel);
GEN_OP_SPE_LHX(le_64_hypv);
6203
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6204 6205
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 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 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310
#endif
#endif
GEN_SPEOP_LD(hhesplat, 1);
GEN_SPEOP_LD(hhousplat, 1);
GEN_SPEOP_LD(hhossplat, 1);
GEN_SPEOP_LD(wwsplat, 2);
GEN_SPEOP_LD(whsplat, 2);

GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //

/* 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                     ***/
#define GEN_SPEFPUOP_CONV(name)                                               \
6311
static always_inline void gen_##name (DisasContext *ctx)                      \
6312
{                                                                             \
A
aurel32 已提交
6313
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
6314
    gen_op_##name();                                                          \
A
aurel32 已提交
6315
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391
}

/* Single precision floating-point vectors operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(evfsadd);
GEN_SPEOP_ARITH2(evfssub);
GEN_SPEOP_ARITH2(evfsmul);
GEN_SPEOP_ARITH2(evfsdiv);
GEN_SPEOP_ARITH1(evfsabs);
GEN_SPEOP_ARITH1(evfsnabs);
GEN_SPEOP_ARITH1(evfsneg);
/* Conversion */
GEN_SPEFPUOP_CONV(evfscfui);
GEN_SPEFPUOP_CONV(evfscfsi);
GEN_SPEFPUOP_CONV(evfscfuf);
GEN_SPEFPUOP_CONV(evfscfsf);
GEN_SPEFPUOP_CONV(evfsctui);
GEN_SPEFPUOP_CONV(evfsctsi);
GEN_SPEFPUOP_CONV(evfsctuf);
GEN_SPEFPUOP_CONV(evfsctsf);
GEN_SPEFPUOP_CONV(evfsctuiz);
GEN_SPEFPUOP_CONV(evfsctsiz);
/* Comparison */
GEN_SPEOP_COMP(evfscmpgt);
GEN_SPEOP_COMP(evfscmplt);
GEN_SPEOP_COMP(evfscmpeq);
GEN_SPEOP_COMP(evfststgt);
GEN_SPEOP_COMP(evfststlt);
GEN_SPEOP_COMP(evfststeq);

/* Opcodes definitions */
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //

/* Single precision floating-point operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(efsadd);
GEN_SPEOP_ARITH2(efssub);
GEN_SPEOP_ARITH2(efsmul);
GEN_SPEOP_ARITH2(efsdiv);
GEN_SPEOP_ARITH1(efsabs);
GEN_SPEOP_ARITH1(efsnabs);
GEN_SPEOP_ARITH1(efsneg);
/* Conversion */
GEN_SPEFPUOP_CONV(efscfui);
GEN_SPEFPUOP_CONV(efscfsi);
GEN_SPEFPUOP_CONV(efscfuf);
GEN_SPEFPUOP_CONV(efscfsf);
GEN_SPEFPUOP_CONV(efsctui);
GEN_SPEFPUOP_CONV(efsctsi);
GEN_SPEFPUOP_CONV(efsctuf);
GEN_SPEFPUOP_CONV(efsctsf);
GEN_SPEFPUOP_CONV(efsctuiz);
GEN_SPEFPUOP_CONV(efsctsiz);
GEN_SPEFPUOP_CONV(efscfd);
/* Comparison */
GEN_SPEOP_COMP(efscmpgt);
GEN_SPEOP_COMP(efscmplt);
GEN_SPEOP_COMP(efscmpeq);
GEN_SPEOP_COMP(efststgt);
GEN_SPEOP_COMP(efststlt);
GEN_SPEOP_COMP(efststeq);

/* Opcodes definitions */
6392
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
6393 6394 6395 6396 6397 6398 6399 6400 6401
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
6402 6403
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //

/* Double precision floating-point operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(efdadd);
GEN_SPEOP_ARITH2(efdsub);
GEN_SPEOP_ARITH2(efdmul);
GEN_SPEOP_ARITH2(efddiv);
GEN_SPEOP_ARITH1(efdabs);
GEN_SPEOP_ARITH1(efdnabs);
GEN_SPEOP_ARITH1(efdneg);
/* Conversion */

GEN_SPEFPUOP_CONV(efdcfui);
GEN_SPEFPUOP_CONV(efdcfsi);
GEN_SPEFPUOP_CONV(efdcfuf);
GEN_SPEFPUOP_CONV(efdcfsf);
GEN_SPEFPUOP_CONV(efdctui);
GEN_SPEFPUOP_CONV(efdctsi);
GEN_SPEFPUOP_CONV(efdctuf);
GEN_SPEFPUOP_CONV(efdctsf);
GEN_SPEFPUOP_CONV(efdctuiz);
GEN_SPEFPUOP_CONV(efdctsiz);
GEN_SPEFPUOP_CONV(efdcfs);
GEN_SPEFPUOP_CONV(efdcfuid);
GEN_SPEFPUOP_CONV(efdcfsid);
GEN_SPEFPUOP_CONV(efdctuidz);
GEN_SPEFPUOP_CONV(efdctsidz);
/* Comparison */
GEN_SPEOP_COMP(efdcmpgt);
GEN_SPEOP_COMP(efdcmplt);
GEN_SPEOP_COMP(efdcmpeq);
GEN_SPEOP_COMP(efdtstgt);
GEN_SPEOP_COMP(efdtstlt);
GEN_SPEOP_COMP(efdtsteq);

/* Opcodes definitions */
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //

B
bellard 已提交
6459 6460 6461
/* End opcode list */
GEN_OPCODE_MARK(end);

6462
#include "translate_init.c"
6463
#include "helper_regs.h"
B
bellard 已提交
6464

6465
/*****************************************************************************/
6466
/* Misc PowerPC helpers */
6467 6468 6469
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
6470
{
6471 6472 6473
#define RGPL  4
#define RFPL  4

B
bellard 已提交
6474 6475
    int i;

J
j_mayer 已提交
6476
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
A
aurel32 已提交
6477
                env->nip, env->lr, env->ctr, env->xer);
6478 6479
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
6480
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
6481
    cpu_fprintf(f, "TB %08x %08x "
6482 6483 6484 6485
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
6486
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6487 6488 6489 6490
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
6491
#endif
6492
    for (i = 0; i < 32; i++) {
6493 6494
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
6495
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
6496
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
6497
            cpu_fprintf(f, "\n");
6498
    }
6499
    cpu_fprintf(f, "CR ");
6500
    for (i = 0; i < 8; i++)
B
bellard 已提交
6501 6502
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
6503 6504 6505 6506 6507 6508 6509 6510
    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 已提交
6511
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6512
    }
6513
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
6514 6515 6516
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
6517
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6518
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
6519
            cpu_fprintf(f, "\n");
B
bellard 已提交
6520
    }
6521
#if !defined(CONFIG_USER_ONLY)
6522
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
6523
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6524
#endif
B
bellard 已提交
6525

6526 6527
#undef RGPL
#undef RFPL
B
bellard 已提交
6528 6529
}

6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576
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
}

6577
/*****************************************************************************/
6578 6579 6580
static always_inline void gen_intermediate_code_internal (CPUState *env,
                                                          TranslationBlock *tb,
                                                          int search_pc)
B
bellard 已提交
6581
{
6582
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
6583
    opc_handler_t **table, *handler;
B
bellard 已提交
6584
    target_ulong pc_start;
B
bellard 已提交
6585
    uint16_t *gen_opc_end;
6586
    int supervisor, little_endian;
B
bellard 已提交
6587
    int j, lj = -1;
P
pbrook 已提交
6588 6589
    int num_insns;
    int max_insns;
B
bellard 已提交
6590 6591 6592

    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
6593 6594 6595
#if defined(OPTIMIZE_FPRF_UPDATE)
    gen_fprf_ptr = gen_fprf_buf;
#endif
B
bellard 已提交
6596
    ctx.nip = pc_start;
B
bellard 已提交
6597
    ctx.tb = tb;
6598
    ctx.exception = POWERPC_EXCP_NONE;
6599
    ctx.spr_cb = env->spr_cb;
6600 6601
    supervisor = env->mmu_idx;
#if !defined(CONFIG_USER_ONLY)
6602
    ctx.supervisor = supervisor;
6603
#endif
6604
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
6605 6606
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
6607
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
6608
#else
6609
    ctx.mem_idx = (supervisor << 1) | little_endian;
6610
#endif
6611
    ctx.dcache_line_size = env->dcache_line_size;
B
bellard 已提交
6612
    ctx.fpu_enabled = msr_fp;
6613
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6614 6615 6616
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
6617 6618 6619 6620
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
6621
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
6622
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
6623
    else
6624
        ctx.singlestep_enabled = 0;
6625
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
6626 6627 6628
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
    if (unlikely(env->singlestep_enabled))
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
6629
#if defined (DO_SINGLE_STEP) && 0
6630 6631 6632
    /* Single step trace mode */
    msr_se = 1;
#endif
P
pbrook 已提交
6633 6634 6635 6636 6637 6638
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;

    gen_icount_start();
6639
    /* Set env in case of segfault during code fetch */
6640
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6641 6642
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6643
                if (env->breakpoints[j] == ctx.nip) {
6644
                    gen_update_nip(&ctx, ctx.nip);
6645 6646 6647 6648 6649
                    gen_op_debug();
                    break;
                }
            }
        }
6650
        if (unlikely(search_pc)) {
B
bellard 已提交
6651 6652 6653 6654 6655
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6656
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6657
                gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
6658
                gen_opc_icount[lj] = num_insns;
B
bellard 已提交
6659 6660
            }
        }
6661 6662
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6663
            fprintf(logfile, "----------------\n");
6664
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6665
                    ctx.nip, supervisor, (int)msr_ir);
6666 6667
        }
#endif
P
pbrook 已提交
6668 6669
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();
6670 6671 6672 6673
        if (unlikely(little_endian)) {
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
6674
        }
6675 6676
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6677
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6678
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6679
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
6680 6681
        }
#endif
B
bellard 已提交
6682
        ctx.nip += 4;
6683
        table = env->opcodes;
P
pbrook 已提交
6684
        num_insns++;
B
bellard 已提交
6685 6686 6687 6688 6689 6690 6691 6692 6693 6694
        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 ? */
6695
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6696
            if (loglevel != 0) {
6697
                fprintf(logfile, "invalid/unsupported opcode: "
6698
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
6699
                        opc1(ctx.opcode), opc2(ctx.opcode),
6700
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6701 6702
            } else {
                printf("invalid/unsupported opcode: "
6703
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
B
bellard 已提交
6704
                       opc1(ctx.opcode), opc2(ctx.opcode),
6705
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6706
            }
6707 6708
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6709
                if (loglevel != 0) {
B
bellard 已提交
6710
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6711
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
B
bellard 已提交
6712 6713
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6714
                            ctx.opcode, ctx.nip - 4);
6715 6716
                } else {
                    printf("invalid bits: %08x for opcode: "
6717
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
6718 6719
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6720
                           ctx.opcode, ctx.nip - 4);
6721
                }
6722
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6723
                break;
B
bellard 已提交
6724 6725
            }
        }
B
bellard 已提交
6726
        (*(handler->handler))(&ctx);
6727 6728 6729
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6730
        /* Check trace mode exceptions */
6731 6732 6733 6734 6735
        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)) {
6736
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6737
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
P
pbrook 已提交
6738 6739
                            (env->singlestep_enabled) ||
                            num_insns >= max_insns)) {
6740 6741 6742
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
6743
            break;
6744
        }
6745 6746 6747 6748
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
P
pbrook 已提交
6749 6750
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
6751
    if (ctx.exception == POWERPC_EXCP_NONE) {
6752
        gen_goto_tb(&ctx, 0, ctx.nip);
6753
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6754 6755 6756 6757
        if (unlikely(env->singlestep_enabled)) {
            gen_update_nip(&ctx, ctx.nip);
            gen_op_debug();
        }
6758
        /* Generate the return instruction */
B
bellard 已提交
6759
        tcg_gen_exit_tb(0);
6760
    }
P
pbrook 已提交
6761
    gen_icount_end(tb, num_insns);
B
bellard 已提交
6762
    *gen_opc_ptr = INDEX_op_end;
6763
    if (unlikely(search_pc)) {
6764 6765 6766 6767 6768
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6769
        tb->size = ctx.nip - pc_start;
P
pbrook 已提交
6770
        tb->icount = num_insns;
6771
    }
6772
#if defined(DEBUG_DISAS)
6773
    if (loglevel & CPU_LOG_TB_CPU) {
6774
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6775
        cpu_dump_state(env, logfile, fprintf, 0);
6776 6777
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6778
        int flags;
6779
        flags = env->bfd_mach;
6780
        flags |= little_endian << 16;
B
bellard 已提交
6781
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6782
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6783
        fprintf(logfile, "\n");
6784
    }
B
bellard 已提交
6785 6786 6787
#endif
}

6788
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6789
{
6790
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
6791 6792
}

6793
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6794
{
6795
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
6796
}
A
aurel32 已提交
6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838

void gen_pc_load(CPUState *env, TranslationBlock *tb,
                unsigned long searched_pc, int pc_pos, void *puc)
{
    int type, c;
    /* for PPC, we need to look at the micro operation to get the
     * access type */
    env->nip = gen_opc_pc[pc_pos];
    c = gen_opc_buf[pc_pos];
    switch(c) {
#if defined(CONFIG_USER_ONLY)
#define CASE3(op)\
    case INDEX_op_ ## op ## _raw
#else
#define CASE3(op)\
    case INDEX_op_ ## op ## _user:\
    case INDEX_op_ ## op ## _kernel:\
    case INDEX_op_ ## op ## _hypv
#endif

    CASE3(stfd):
    CASE3(stfs):
    CASE3(lfd):
    CASE3(lfs):
        type = ACCESS_FLOAT;
        break;
    CASE3(lwarx):
        type = ACCESS_RES;
        break;
    CASE3(stwcx):
        type = ACCESS_RES;
        break;
    CASE3(eciwx):
    CASE3(ecowx):
        type = ACCESS_EXT;
        break;
    default:
        type = ACCESS_INT;
        break;
    }
    env->access_type = type;
}