translate.c 237.4 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 66 67 68 69 70 71 72

/* 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 已提交
73
static TCGv cpu_FT[3];
74
static TCGv cpu_AVRh[3], cpu_AVRl[3];
P
pbrook 已提交
75 76 77 78 79

#include "gen-icount.h"

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

P
pbrook 已提交
84 85
    if (done_init)
        return;
A
aurel32 已提交
86

P
pbrook 已提交
87
    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
A
aurel32 已提交
88 89 90 91 92 93 94 95 96 97 98 99
#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 已提交
100 101
#if !defined(TARGET_PPC64)
    cpu_T64[0] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
102
                                    TCG_AREG0, offsetof(CPUState, t0_64),
A
aurel32 已提交
103 104
                                    "T0_64");
    cpu_T64[1] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
105
                                    TCG_AREG0, offsetof(CPUState, t1_64),
A
aurel32 已提交
106 107
                                    "T1_64");
    cpu_T64[2] = tcg_global_mem_new(TCG_TYPE_I64,
A
aurel32 已提交
108
                                    TCG_AREG0, offsetof(CPUState, t2_64),
A
aurel32 已提交
109 110
                                    "T2_64");
#endif
A
aurel32 已提交
111 112 113 114 115 116 117 118

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

119 120 121 122 123 124 125 126 127 128 129 130 131
    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 已提交
132
    p = cpu_reg_names;
A
aurel32 已提交
133 134 135 136 137 138 139 140

    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 已提交
141 142 143 144 145 146 147 148 149 150 151
    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
152

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

158 159 160 161
        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 已提交
162

163 164 165 166
        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 已提交
167
    }
A
aurel32 已提交
168

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

A
aurel32 已提交
172 173 174 175 176 177
    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 已提交
178 179 180 181 182
    /* register helpers */
#undef DEF_HELPER
#define DEF_HELPER(ret, name, params) tcg_register_helper(name, #name);
#include "helper.h"

P
pbrook 已提交
183 184 185
    done_init = 1;
}

186 187 188 189
#if defined(OPTIMIZE_FPRF_UPDATE)
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
static uint16_t **gen_fprf_ptr;
#endif
B
bellard 已提交
190 191 192 193

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

214
struct opc_handler_t {
B
bellard 已提交
215 216
    /* invalid bits */
    uint32_t inval;
217
    /* instruction type */
218
    uint64_t type;
B
bellard 已提交
219 220
    /* handler */
    void (*handler)(DisasContext *ctx);
221
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
222
    const char *oname;
223 224
#endif
#if defined(DO_PPC_STATISTICS)
225 226
    uint64_t count;
#endif
227
};
B
bellard 已提交
228

229
static always_inline void gen_set_Rc0 (DisasContext *ctx)
230
{
231 232 233 234 235 236
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_cmpi_64(0);
    else
#endif
        gen_op_cmpi(0);
237 238 239
    gen_op_set_Rc0();
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
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 已提交
256
            tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
257 258 259 260
        gen_op_float_check_status();
    } else if (unlikely(set_rc)) {
        /* We always need to compute fpcc */
        gen_op_compute_fprf(0);
A
aurel32 已提交
261
        tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        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
}

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

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

297 298 299
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
300

301 302 303
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
304

305 306 307 308 309 310 311 312 313
#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)
314

315 316 317
#define GEN_EXCP_NO_VR(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)

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

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

B
bellard 已提交
331 332 333 334 335
#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)

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

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

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

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

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411
/***                              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 已提交
412 413
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
414 415 416 417

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

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

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

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

    return ret;
}

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

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

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

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

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

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

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

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

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

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

/* Invalid instruction */
696 697
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
698
    GEN_EXCP_INVAL(ctx);
699 700
}

B
bellard 已提交
701 702
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
703
    .type    = PPC_NONE,
B
bellard 已提交
704 705 706 707
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
708 709
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
710
{                                                                             \
A
aurel32 已提交
711 712
    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 已提交
713
    gen_op_##name();                                                          \
A
aurel32 已提交
714
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
715 716
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
717 718
}

719 720
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
721
{                                                                             \
A
aurel32 已提交
722 723
    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 已提交
724
    gen_op_##name();                                                          \
A
aurel32 已提交
725
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
726 727
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
728 729
}

730 731
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
732
{                                                                             \
A
aurel32 已提交
733
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
B
bellard 已提交
734
    gen_op_##name();                                                          \
A
aurel32 已提交
735
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
736 737
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
738
}
739 740
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
741
{                                                                             \
A
aurel32 已提交
742
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
B
bellard 已提交
743
    gen_op_##name();                                                          \
A
aurel32 已提交
744
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
745 746
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
747 748 749
}

/* Two operands arithmetic functions */
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
#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 已提交
767 768
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
769 770 771 772
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
773
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
774 775 776 777 778 779 780
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
A
aurel32 已提交
781 782
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
783 784 785 786
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
787
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
788 789 790 791 792 793 794
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
A
aurel32 已提交
795
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
796 797 798 799
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
800
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
801 802 803 804 805 806
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
A
aurel32 已提交
807
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
808 809 810 811
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
812
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
813 814 815 816 817 818 819 820
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

/* 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 已提交
821 822

/* Two operands arithmetic functions with no overflow allowed */
823 824
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
825 826

/* One operand arithmetic functions */
827 828 829 830 831 832 833 834
#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 已提交
835 836

/* add    add.    addo    addo.    */
A
aurel32 已提交
837 838 839 840
static always_inline void gen_op_add (void)
{
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}
841
static always_inline void gen_op_addo (void)
842
{
843
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
844
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
845 846 847 848
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
849
static always_inline void gen_op_addo_64 (void)
850
{
851
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
852
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
853 854 855 856
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
B
bellard 已提交
857
/* addc   addc.   addco   addco.   */
858
static always_inline void gen_op_addc (void)
859
{
860
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
861
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
862 863
    gen_op_check_addc();
}
864
static always_inline void gen_op_addco (void)
865
{
866
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
867
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
868 869 870 871
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
872
static always_inline void gen_op_addc_64 (void)
873
{
874
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
875
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
876 877
    gen_op_check_addc_64();
}
878
static always_inline void gen_op_addco_64 (void)
879
{
880
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
881
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
882 883 884 885 886
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
B
bellard 已提交
887
/* adde   adde.   addeo   addeo.   */
888
static always_inline void gen_op_addeo (void)
889
{
890
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
891 892 893 894
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
895
static always_inline void gen_op_addeo_64 (void)
896
{
897
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
898 899 900 901 902
    gen_op_adde_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
B
bellard 已提交
903
/* addme  addme.  addmeo  addmeo.  */
904
static always_inline void gen_op_addme (void)
905
{
906
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
907 908 909
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
910
static always_inline void gen_op_addme_64 (void)
911
{
912
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
913 914 915 916
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
917
/* addze  addze.  addzeo  addzeo.  */
918
static always_inline void gen_op_addze (void)
919
{
920
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
921 922 923
    gen_op_add_ze();
    gen_op_check_addc();
}
924
static always_inline void gen_op_addzeo (void)
925
{
926
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
927 928 929 930 931
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
932
static always_inline void gen_op_addze_64 (void)
933
{
934
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
935 936 937
    gen_op_add_ze();
    gen_op_check_addc_64();
}
938
static always_inline void gen_op_addzeo_64 (void)
939
{
940
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
941 942 943 944 945 946
    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 已提交
947
/* divw   divw.   divwo   divwo.   */
948
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
949
/* divwu  divwu.  divwuo  divwuo.  */
950
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
951
/* mulhw  mulhw.                   */
952
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
953
/* mulhwu mulhwu.                  */
954
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
955
/* mullw  mullw.  mullwo  mullwo.  */
956
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
957
/* neg    neg.    nego    nego.    */
958
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
959
/* subf   subf.   subfo   subfo.   */
A
aurel32 已提交
960 961 962 963
static always_inline void gen_op_subf (void)
{
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
}
964
static always_inline void gen_op_subfo (void)
965
{
A
aurel32 已提交
966
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
967
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
968
    gen_op_check_addo();
969 970 971
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
972
static always_inline void gen_op_subfo_64 (void)
973
{
A
aurel32 已提交
974
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
975
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
976
    gen_op_check_addo_64();
977 978 979
}
#endif
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
B
bellard 已提交
980
/* subfc  subfc.  subfco  subfco.  */
981
static always_inline void gen_op_subfc (void)
982
{
A
aurel32 已提交
983
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
984 985
    gen_op_check_subfc();
}
986
static always_inline void gen_op_subfco (void)
987
{
A
aurel32 已提交
988
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
989
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
990
    gen_op_check_subfc();
991
    gen_op_check_addo();
992 993
}
#if defined(TARGET_PPC64)
994
static always_inline void gen_op_subfc_64 (void)
995
{
A
aurel32 已提交
996
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
997 998
    gen_op_check_subfc_64();
}
999
static always_inline void gen_op_subfco_64 (void)
1000
{
A
aurel32 已提交
1001
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1002
    tcg_gen_sub_tl(cpu_T[0], cpu_T[1], cpu_T[0]);
1003
    gen_op_check_subfc_64();
1004
    gen_op_check_addo_64();
1005 1006 1007
}
#endif
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
B
bellard 已提交
1008
/* subfe  subfe.  subfeo  subfeo.  */
1009
static always_inline void gen_op_subfeo (void)
1010
{
A
aurel32 已提交
1011
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
1012
    gen_op_subfe();
1013
    gen_op_check_addo();
1014 1015 1016
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
1017
static always_inline void gen_op_subfeo_64 (void)
1018
{
A
aurel32 已提交
1019
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
1020
    gen_op_subfe_64();
1021
    gen_op_check_addo_64();
1022 1023 1024
}
#endif
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
B
bellard 已提交
1025
/* subfme subfme. subfmeo subfmeo. */
1026
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
1027
/* subfze subfze. subfzeo subfzeo. */
1028
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
1029 1030 1031
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1032
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
1033 1034

    if (rA(ctx->opcode) == 0) {
1035
        /* li case */
A
aurel32 已提交
1036
        tcg_gen_movi_tl(cpu_T[0], simm);
B
bellard 已提交
1037
    } else {
A
aurel32 已提交
1038
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1039
        if (likely(simm != 0))
A
aurel32 已提交
1040
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
B
bellard 已提交
1041
    }
A
aurel32 已提交
1042
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1043 1044 1045 1046
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1047 1048
    target_long simm = SIMM(ctx->opcode);

A
aurel32 已提交
1049
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1050
    if (likely(simm != 0)) {
1051
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1052
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
1053 1054 1055 1056 1057 1058
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
1059 1060
    } else {
        gen_op_clear_xer_ca();
1061
    }
A
aurel32 已提交
1062
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1063 1064
}
/* addic. */
1065
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1066
{
1067 1068
    target_long simm = SIMM(ctx->opcode);

A
aurel32 已提交
1069
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1070
    if (likely(simm != 0)) {
1071
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
A
aurel32 已提交
1072
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm);
1073 1074 1075 1076 1077 1078
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
1079 1080
    } else {
        gen_op_clear_xer_ca();
1081
    }
A
aurel32 已提交
1082
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1083
    gen_set_Rc0(ctx);
B
bellard 已提交
1084 1085 1086 1087
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1088
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
1089 1090

    if (rA(ctx->opcode) == 0) {
1091
        /* lis case */
A
aurel32 已提交
1092
        tcg_gen_movi_tl(cpu_T[0], simm << 16);
B
bellard 已提交
1093
    } else {
A
aurel32 已提交
1094
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1095
        if (likely(simm != 0))
A
aurel32 已提交
1096
            tcg_gen_addi_tl(cpu_T[0], cpu_T[0], simm << 16);
B
bellard 已提交
1097
    }
A
aurel32 已提交
1098
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1099 1100 1101 1102
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
1103
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1104
    gen_op_mulli(SIMM(ctx->opcode));
A
aurel32 已提交
1105
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1106 1107 1108 1109
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
1110
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1111 1112 1113 1114 1115 1116
#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 已提交
1117
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1118 1119
}

1120 1121
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
1122
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1123
/* mulhdu mulhdu.                  */
1124
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1125
/* mulld  mulld.  mulldo  mulldo.  */
1126
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1127
/* divd   divd.   divdo   divdo.   */
1128
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1129
/* divdu  divdu.  divduo  divduo.  */
1130
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1131 1132
#endif

B
bellard 已提交
1133
/***                           Integer comparison                          ***/
1134 1135 1136 1137
#if defined(TARGET_PPC64)
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
{                                                                             \
A
aurel32 已提交
1138 1139
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
1140
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
1141 1142 1143
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
A
aurel32 已提交
1144
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
1145 1146 1147 1148
}
#else
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
B
bellard 已提交
1149
{                                                                             \
A
aurel32 已提交
1150 1151
    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 已提交
1152
    gen_op_##name();                                                          \
A
aurel32 已提交
1153
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
B
bellard 已提交
1154
}
1155
#endif
B
bellard 已提交
1156 1157

/* cmp */
1158
GEN_CMP(cmp, 0x00, PPC_INTEGER);
B
bellard 已提交
1159 1160 1161
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
A
aurel32 已提交
1162
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1163
#if defined(TARGET_PPC64)
1164
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1165 1166 1167 1168
        gen_op_cmpi_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_cmpi(SIMM(ctx->opcode));
A
aurel32 已提交
1169
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
B
bellard 已提交
1170 1171
}
/* cmpl */
1172
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
B
bellard 已提交
1173 1174 1175
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
A
aurel32 已提交
1176
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1177
#if defined(TARGET_PPC64)
1178
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1179 1180 1181 1182
        gen_op_cmpli_64(UIMM(ctx->opcode));
    else
#endif
        gen_op_cmpli(UIMM(ctx->opcode));
A
aurel32 已提交
1183
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
B
bellard 已提交
1184 1185
}

1186
/* isel (PowerPC 2.03 specification) */
A
aurel32 已提交
1187
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
1188 1189 1190 1191 1192
{
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;

    if (rA(ctx->opcode) == 0) {
A
aurel32 已提交
1193
        tcg_gen_movi_tl(cpu_T[0], 0);
1194
    } else {
A
aurel32 已提交
1195
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1196
    }
A
aurel32 已提交
1197
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
1198
    mask = 1 << (3 - (bi & 0x03));
A
aurel32 已提交
1199
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[bi >> 2]);
1200 1201
    gen_op_test_true(mask);
    gen_op_isel();
A
aurel32 已提交
1202
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1203 1204
}

B
bellard 已提交
1205
/***                            Integer logical                            ***/
1206 1207
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
B
bellard 已提交
1208
{                                                                             \
A
aurel32 已提交
1209 1210
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);                       \
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
B
bellard 已提交
1211
    gen_op_##name();                                                          \
A
aurel32 已提交
1212
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
1213 1214
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1215
}
1216 1217
#define GEN_LOGICAL2(name, opc, type)                                         \
__GEN_LOGICAL2(name, 0x1C, opc, type)
B
bellard 已提交
1218

1219 1220
#define GEN_LOGICAL1(name, opc, type)                                         \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1221
{                                                                             \
A
aurel32 已提交
1222
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);                       \
B
bellard 已提交
1223
    gen_op_##name();                                                          \
A
aurel32 已提交
1224
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
1225 1226
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1227 1228 1229
}

/* and & and. */
1230
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
B
bellard 已提交
1231
/* andc & andc. */
1232
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
B
bellard 已提交
1233
/* andi. */
1234
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1235
{
A
aurel32 已提交
1236
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
A
aurel32 已提交
1237
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], UIMM(ctx->opcode));
A
aurel32 已提交
1238
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1239
    gen_set_Rc0(ctx);
B
bellard 已提交
1240 1241
}
/* andis. */
1242
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1243
{
A
aurel32 已提交
1244
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
A
aurel32 已提交
1245
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], UIMM(ctx->opcode) << 16);
A
aurel32 已提交
1246
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1247
    gen_set_Rc0(ctx);
B
bellard 已提交
1248 1249 1250
}

/* cntlzw */
1251
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
B
bellard 已提交
1252
/* eqv & eqv. */
1253
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
B
bellard 已提交
1254
/* extsb & extsb. */
1255
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
B
bellard 已提交
1256
/* extsh & extsh. */
1257
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
B
bellard 已提交
1258
/* nand & nand. */
1259
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
B
bellard 已提交
1260
/* nor & nor. */
1261
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1262

B
bellard 已提交
1263
/* or & or. */
1264 1265
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1266 1267 1268 1269 1270 1271 1272
    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) {
A
aurel32 已提交
1273
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rs]);
1274
        if (rs != rb) {
A
aurel32 已提交
1275
            tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rb]);
1276 1277
            gen_op_or();
        }
A
aurel32 已提交
1278
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
1279 1280 1281
        if (unlikely(Rc(ctx->opcode) != 0))
            gen_set_Rc0(ctx);
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
A
aurel32 已提交
1282
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rs]);
1283
        gen_set_Rc0(ctx);
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
#if defined(TARGET_PPC64)
    } else {
        switch (rs) {
        case 1:
            /* Set process priority to low */
            gen_op_store_pri(2);
            break;
        case 6:
            /* Set process priority to medium-low */
            gen_op_store_pri(3);
            break;
        case 2:
            /* Set process priority to normal */
            gen_op_store_pri(4);
            break;
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
#if !defined(CONFIG_USER_ONLY)
        case 31:
            if (ctx->supervisor > 0) {
                /* Set process priority to very low */
                gen_op_store_pri(1);
            }
            break;
        case 5:
            if (ctx->supervisor > 0) {
                /* Set process priority to medium-hight */
                gen_op_store_pri(5);
            }
            break;
        case 3:
            if (ctx->supervisor > 0) {
                /* Set process priority to high */
                gen_op_store_pri(6);
            }
            break;
        case 7:
            if (ctx->supervisor > 1) {
                /* Set process priority to very high */
                gen_op_store_pri(7);
            }
            break;
#endif
1325 1326 1327 1328 1329
        default:
            /* nop */
            break;
        }
#endif
1330 1331 1332
    }
}

B
bellard 已提交
1333
/* orc & orc. */
1334
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
B
bellard 已提交
1335
/* xor & xor. */
1336 1337
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
1338
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1339 1340
    /* Optimisation for "set to zero" case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
A
aurel32 已提交
1341
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
1342 1343
        gen_op_xor();
    } else {
1344
        tcg_gen_movi_tl(cpu_T[0], 0);
1345
    }
A
aurel32 已提交
1346
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1347 1348
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1349
}
B
bellard 已提交
1350 1351 1352
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1353
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1354

1355 1356
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1357
        /* XXX: should handle special NOPs for POWER series */
1358
        return;
1359
    }
A
aurel32 已提交
1360
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1361
    if (likely(uimm != 0))
B
bellard 已提交
1362
        gen_op_ori(uimm);
A
aurel32 已提交
1363
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1364 1365 1366 1367
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1368
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1369

1370 1371 1372
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1373
    }
A
aurel32 已提交
1374
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1375
    if (likely(uimm != 0))
B
bellard 已提交
1376
        gen_op_ori(uimm << 16);
A
aurel32 已提交
1377
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1378 1379 1380 1381
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1382
    target_ulong uimm = UIMM(ctx->opcode);
1383 1384 1385 1386 1387

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
A
aurel32 已提交
1388
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1389 1390
    if (likely(uimm != 0))
        gen_op_xori(uimm);
A
aurel32 已提交
1391
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1392 1393 1394 1395 1396
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1397
    target_ulong uimm = UIMM(ctx->opcode);
1398 1399 1400 1401 1402

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
A
aurel32 已提交
1403
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1404 1405
    if (likely(uimm != 0))
        gen_op_xori(uimm << 16);
A
aurel32 已提交
1406
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
1407 1408
}

1409
/* popcntb : PowerPC 2.03 specification */
1410
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1411
{
A
aurel32 已提交
1412
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1413 1414
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
A
aurel32 已提交
1415
        gen_op_popcntb_64();
1416 1417
    else
#endif
A
aurel32 已提交
1418
        gen_op_popcntb();
A
aurel32 已提交
1419
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1420 1421 1422 1423 1424 1425 1426 1427 1428
}

#if defined(TARGET_PPC64)
/* extsw & extsw. */
GEN_LOGICAL1(extsw, 0x1E, PPC_64B);
/* cntlzd */
GEN_LOGICAL1(cntlzd, 0x01, PPC_64B);
#endif

B
bellard 已提交
1429 1430 1431 1432
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1433 1434
    target_ulong mask;
    uint32_t mb, me, sh;
B
bellard 已提交
1435 1436 1437

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1438 1439 1440
    sh = SH(ctx->opcode);
    if (likely(sh == 0)) {
        if (likely(mb == 0 && me == 31)) {
A
aurel32 已提交
1441
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1442 1443
            goto do_store;
        } else if (likely(mb == 31 && me == 0)) {
A
aurel32 已提交
1444
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1445 1446
            goto do_store;
        }
A
aurel32 已提交
1447 1448
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1449 1450
        goto do_mask;
    }
A
aurel32 已提交
1451 1452
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1453 1454 1455 1456 1457 1458 1459
    gen_op_rotli32_T0(SH(ctx->opcode));
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    mask = MASK(mb, me);
A
aurel32 已提交
1460 1461
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], mask);
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], ~mask);
1462 1463
    gen_op_or();
 do_store:
A
aurel32 已提交
1464
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1465 1466
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1467 1468 1469 1470 1471
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1472

B
bellard 已提交
1473 1474 1475
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
A
aurel32 已提交
1476
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 31)) {
            gen_op_rotli32_T0(sh);
            goto do_store;
        } else if (likely(me == (31 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
B
bellard 已提交
1487
        }
1488 1489 1490 1491
    } else if (likely(me == 31)) {
        if (likely(sh == (32 - mb))) {
            gen_op_srli_T0(mb);
            goto do_store;
B
bellard 已提交
1492 1493
        }
    }
1494 1495 1496 1497 1498 1499
    gen_op_rotli32_T0(sh);
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
A
aurel32 已提交
1500
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], MASK(mb, me));
1501
 do_store:
A
aurel32 已提交
1502
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1503 1504
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1505 1506 1507 1508 1509 1510 1511 1512
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
A
aurel32 已提交
1513 1514
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
1515 1516 1517 1518 1519 1520
    gen_op_rotl32_T0_T1();
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
A
aurel32 已提交
1521
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], MASK(mb, me));
B
bellard 已提交
1522
    }
A
aurel32 已提交
1523
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1524 1525
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1526 1527
}

1528 1529
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1530
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1531 1532 1533
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1534 1535
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1536 1537 1538 1539
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1540
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1541 1542 1543
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1544 1545
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1546 1547 1548
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1549 1550
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1551 1552 1553
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1554 1555
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1556 1557 1558
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1559

1560 1561
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1562
{
A
aurel32 已提交
1563
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
J
j_mayer 已提交
1564 1565 1566 1567 1568
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 63)) {
1569
            gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1570 1571 1572 1573 1574 1575 1576
            goto do_store;
        } else if (likely(me == (63 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
        }
    } else if (likely(me == 63)) {
        if (likely(sh == (64 - mb))) {
1577
            gen_op_srli_T0_64(mb);
J
j_mayer 已提交
1578 1579 1580 1581 1582
            goto do_store;
        }
    }
    gen_op_rotli64_T0(sh);
 do_mask:
A
aurel32 已提交
1583
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], MASK(mb, me));
J
j_mayer 已提交
1584
 do_store:
A
aurel32 已提交
1585
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
J
j_mayer 已提交
1586 1587 1588
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1589
/* rldicl - rldicl. */
1590
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1591
{
J
j_mayer 已提交
1592
    uint32_t sh, mb;
1593

J
j_mayer 已提交
1594 1595
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1596
    gen_rldinm(ctx, mb, 63, sh);
1597
}
J
j_mayer 已提交
1598
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1599
/* rldicr - rldicr. */
1600
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1601
{
J
j_mayer 已提交
1602
    uint32_t sh, me;
1603

J
j_mayer 已提交
1604 1605
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1606
    gen_rldinm(ctx, 0, me, sh);
1607
}
J
j_mayer 已提交
1608
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1609
/* rldic - rldic. */
1610
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1611
{
J
j_mayer 已提交
1612
    uint32_t sh, mb;
1613

J
j_mayer 已提交
1614 1615
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1616 1617 1618 1619
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1620 1621
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1622
{
A
aurel32 已提交
1623 1624
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
1625 1626
    gen_op_rotl64_T0_T1();
    if (unlikely(mb != 0 || me != 63)) {
A
aurel32 已提交
1627
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], MASK(mb, me));
J
j_mayer 已提交
1628
    }
A
aurel32 已提交
1629
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
J
j_mayer 已提交
1630 1631
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1632
}
J
j_mayer 已提交
1633

1634
/* rldcl - rldcl. */
1635
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1636
{
J
j_mayer 已提交
1637
    uint32_t mb;
1638

J
j_mayer 已提交
1639
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1640
    gen_rldnm(ctx, mb, 63);
1641
}
1642
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1643
/* rldcr - rldcr. */
1644
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1645
{
J
j_mayer 已提交
1646
    uint32_t me;
1647

J
j_mayer 已提交
1648
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1649
    gen_rldnm(ctx, 0, me);
1650
}
1651
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1652
/* rldimi - rldimi. */
1653
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1654
{
J
j_mayer 已提交
1655
    uint64_t mask;
1656
    uint32_t sh, mb, me;
1657

J
j_mayer 已提交
1658 1659
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1660
    me = 63 - sh;
J
j_mayer 已提交
1661 1662
    if (likely(sh == 0)) {
        if (likely(mb == 0)) {
A
aurel32 已提交
1663
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
J
j_mayer 已提交
1664 1665
            goto do_store;
        }
A
aurel32 已提交
1666 1667
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
J
j_mayer 已提交
1668 1669
        goto do_mask;
    }
A
aurel32 已提交
1670 1671
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1672
    gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1673
 do_mask:
1674
    mask = MASK(mb, me);
A
aurel32 已提交
1675 1676
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], mask);
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], ~mask);
J
j_mayer 已提交
1677 1678
    gen_op_or();
 do_store:
A
aurel32 已提交
1679
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
J
j_mayer 已提交
1680 1681
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1682
}
1683
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1684 1685
#endif

B
bellard 已提交
1686 1687
/***                             Integer shift                             ***/
/* slw & slw. */
1688
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
B
bellard 已提交
1689
/* sraw & sraw. */
1690
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
B
bellard 已提交
1691 1692 1693
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1694
    int mb, me;
A
aurel32 已提交
1695
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1696
    if (SH(ctx->opcode) != 0) {
1697
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1698 1699 1700 1701 1702 1703 1704 1705
        mb = 32 - SH(ctx->opcode);
        me = 31;
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
    }
A
aurel32 已提交
1706
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1707 1708
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1709 1710
}
/* srw & srw. */
1711 1712 1713 1714 1715 1716 1717 1718
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);

#if defined(TARGET_PPC64)
/* sld & sld. */
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
/* srad & srad. */
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
/* sradi & sradi. */
1719
static always_inline void gen_sradi (DisasContext *ctx, int n)
1720 1721 1722 1723
{
    uint64_t mask;
    int sh, mb, me;

A
aurel32 已提交
1724
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1725 1726
    sh = SH(ctx->opcode) + (n << 5);
    if (sh != 0) {
1727
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1728 1729 1730 1731 1732
        mb = 64 - SH(ctx->opcode);
        me = 63;
        mask = MASK(mb, me);
        gen_op_sradi(sh, mask >> 32, mask);
    }
A
aurel32 已提交
1733
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1734 1735 1736
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1737
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1738 1739 1740
{
    gen_sradi(ctx, 0);
}
1741
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1742 1743 1744 1745 1746 1747
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
#endif
B
bellard 已提交
1748 1749

/***                       Floating-Point arithmetic                       ***/
1750
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1751
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1752
{                                                                             \
1753
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1754
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1755 1756
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1757 1758 1759
    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)]);                     \
1760
    gen_reset_fpstatus();                                                     \
1761 1762 1763 1764
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
1765
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1766
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1767 1768
}

1769 1770 1771
#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);
1772

1773 1774
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1775
{                                                                             \
1776
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1777
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1778 1779
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1780 1781
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);                     \
1782
    gen_reset_fpstatus();                                                     \
1783 1784 1785 1786
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
1787
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1788
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1789
}
1790 1791 1792
#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);
1793

1794 1795
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1796
{                                                                             \
1797
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1798
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1799 1800
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1801 1802
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rC(ctx->opcode)]);                     \
1803
    gen_reset_fpstatus();                                                     \
1804 1805 1806 1807
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
A
aurel32 已提交
1808
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1809
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1810
}
1811 1812 1813
#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);
1814

1815
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
1816
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1817
{                                                                             \
1818
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1819
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1820 1821
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1822
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
1823
    gen_reset_fpstatus();                                                     \
1824
    gen_op_f##name();                                                         \
A
aurel32 已提交
1825
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1826
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
1827 1828
}

1829
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
1830
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1831
{                                                                             \
1832
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1833
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1834 1835
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
1836
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
1837
    gen_reset_fpstatus();                                                     \
1838
    gen_op_f##name();                                                         \
A
aurel32 已提交
1839
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1840
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
1841 1842
}

1843
/* fadd - fadds */
1844
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
1845
/* fdiv - fdivs */
1846
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
1847
/* fmul - fmuls */
1848
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
1849

1850
/* fre */
1851
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
1852

1853
/* fres */
1854
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
1855

1856
/* frsqrte */
1857 1858 1859 1860 1861 1862 1863 1864
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();
}
1865
GEN_FLOAT_BS(rsqrtes, 0x3B, 0x1A, 1, PPC_FLOAT_FRSQRTES);
B
bellard 已提交
1866

1867
/* fsel */
1868
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
1869
/* fsub - fsubs */
1870
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
1871 1872
/* Optional: */
/* fsqrt */
1873
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1874
{
1875
    if (unlikely(!ctx->fpu_enabled)) {
1876
        GEN_EXCP_NO_FP(ctx);
1877 1878
        return;
    }
A
aurel32 已提交
1879
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
1880
    gen_reset_fpstatus();
1881
    gen_op_fsqrt();
A
aurel32 已提交
1882
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
1883
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1884
}
B
bellard 已提交
1885

1886
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
1887
{
1888
    if (unlikely(!ctx->fpu_enabled)) {
1889
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1890 1891
        return;
    }
A
aurel32 已提交
1892
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
1893
    gen_reset_fpstatus();
1894 1895
    gen_op_fsqrt();
    gen_op_frsp();
A
aurel32 已提交
1896
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
1897
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
B
bellard 已提交
1898 1899 1900
}

/***                     Floating-Point multiply-and-add                   ***/
1901
/* fmadd - fmadds */
1902
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
1903
/* fmsub - fmsubs */
1904
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
1905
/* fnmadd - fnmadds */
1906
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
1907
/* fnmsub - fnmsubs */
1908
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
B
bellard 已提交
1909 1910 1911

/***                     Floating-Point round & convert                    ***/
/* fctiw */
1912
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
1913
/* fctiwz */
1914
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
1915
/* frsp */
1916
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
J
j_mayer 已提交
1917 1918
#if defined(TARGET_PPC64)
/* fcfid */
1919
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
J
j_mayer 已提交
1920
/* fctid */
1921
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
J
j_mayer 已提交
1922
/* fctidz */
1923
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
J
j_mayer 已提交
1924
#endif
B
bellard 已提交
1925

1926
/* frin */
1927
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
1928
/* friz */
1929
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
1930
/* frip */
1931
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
1932
/* frim */
1933
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
1934

B
bellard 已提交
1935 1936
/***                         Floating-Point compare                        ***/
/* fcmpo */
1937
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1938
{
1939
    if (unlikely(!ctx->fpu_enabled)) {
1940
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1941 1942
        return;
    }
A
aurel32 已提交
1943 1944
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
1945
    gen_reset_fpstatus();
1946
    gen_op_fcmpo();
A
aurel32 已提交
1947
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1948
    gen_op_float_check_status();
B
bellard 已提交
1949 1950 1951
}

/* fcmpu */
1952
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1953
{
1954
    if (unlikely(!ctx->fpu_enabled)) {
1955
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1956 1957
        return;
    }
A
aurel32 已提交
1958 1959
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
1960
    gen_reset_fpstatus();
1961
    gen_op_fcmpu();
A
aurel32 已提交
1962
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1963
    gen_op_float_check_status();
B
bellard 已提交
1964 1965
}

1966 1967
/***                         Floating-point move                           ***/
/* fabs */
1968 1969
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
1970 1971

/* fmr  - fmr. */
1972
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
1973 1974
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
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[rB(ctx->opcode)]);
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
1981
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
1982 1983 1984
}

/* fnabs */
1985 1986
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
1987
/* fneg */
1988 1989
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
1990

B
bellard 已提交
1991 1992 1993 1994
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
1995 1996
    int bfa;

1997
    if (unlikely(!ctx->fpu_enabled)) {
1998
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1999 2000
        return;
    }
2001 2002 2003
    gen_optimize_fprf();
    bfa = 4 * (7 - crfS(ctx->opcode));
    gen_op_load_fpscr_T0(bfa);
A
aurel32 已提交
2004
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
2005
    gen_op_fpscr_resetbit(~(0xF << bfa));
B
bellard 已提交
2006 2007 2008 2009 2010
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
2011
    if (unlikely(!ctx->fpu_enabled)) {
2012
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2013 2014
        return;
    }
2015 2016 2017
    gen_optimize_fprf();
    gen_reset_fpstatus();
    gen_op_load_fpscr_FT0();
A
aurel32 已提交
2018
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2019
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
B
bellard 已提交
2020 2021 2022 2023 2024
}

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

2027
    if (unlikely(!ctx->fpu_enabled)) {
2028
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2029 2030
        return;
    }
2031 2032 2033 2034 2035 2036 2037 2038 2039
    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)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
B
bellard 已提交
2040 2041 2042 2043 2044
}

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

2047
    if (unlikely(!ctx->fpu_enabled)) {
2048
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2049 2050
        return;
    }
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
    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)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2063 2064 2065 2066 2067
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
2068
    if (unlikely(!ctx->fpu_enabled)) {
2069
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2070 2071
        return;
    }
2072
    gen_optimize_fprf();
A
aurel32 已提交
2073
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2074
    gen_reset_fpstatus();
2075
    gen_op_store_fpscr(FM(ctx->opcode));
2076 2077 2078 2079 2080 2081
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2082 2083 2084 2085 2086
}

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

2089
    if (unlikely(!ctx->fpu_enabled)) {
2090
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2091 2092
        return;
    }
2093 2094 2095
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
    gen_optimize_fprf();
2096
    tcg_gen_movi_i64(cpu_FT[0], FPIMM(ctx->opcode) << (4 * sh));
2097 2098 2099 2100 2101 2102 2103 2104
    gen_reset_fpstatus();
    gen_op_store_fpscr(1 << sh);
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2105 2106
}

2107 2108
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2109 2110
static always_inline void gen_addr_imm_index (TCGv EA,
                                              DisasContext *ctx,
2111
                                              target_long maskl)
2112 2113 2114
{
    target_long simm = SIMM(ctx->opcode);

2115
    simm &= ~maskl;
2116 2117 2118 2119 2120 2121
    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)]);
2122 2123
}

2124 2125
static always_inline void gen_addr_reg_index (TCGv EA,
                                              DisasContext *ctx)
2126
{
2127 2128 2129 2130
    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)]);
2131 2132
}

2133 2134
static always_inline void gen_addr_register (TCGv EA,
                                             DisasContext *ctx)
2135
{
2136 2137 2138 2139
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(EA, 0);
    else
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2140 2141
}

2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
#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
2153
#if defined(CONFIG_USER_ONLY)
2154
#if defined(TARGET_PPC64)
2155
#define NB_MEM_FUNCS 4
2156
#else
2157
#define NB_MEM_FUNCS 2
2158
#endif
2159 2160
#define GEN_MEM_FUNCS(name)                                                   \
    _GEN_MEM_FUNCS(name, raw)
2161
#else
2162
#if defined(TARGET_PPC64)
2163
#define NB_MEM_FUNCS 12
2164
#else
2165
#define NB_MEM_FUNCS 6
2166
#endif
2167 2168 2169 2170 2171 2172 2173 2174
#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])()
2175
#define OP_LD_TABLE(width)                                                    \
2176 2177
static GenOpFunc *gen_op_l##width[NB_MEM_FUNCS] = {                           \
    GEN_MEM_FUNCS(l##width),                                                  \
2178 2179
};
#define OP_ST_TABLE(width)                                                    \
2180 2181
static GenOpFunc *gen_op_st##width[NB_MEM_FUNCS] = {                          \
    GEN_MEM_FUNCS(st##width),                                                 \
2182
};
2183

A
aurel32 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 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

#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))
	tcg_gen_bswap_i64(t0, t0);
}

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);
	tcg_temp_free(temp);
    } 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);
	tcg_temp_free(temp);
    } 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);
	tcg_temp_free(temp);
    } 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);
	tcg_temp_free(temp);
    } else
        gen_qemu_st64_ppc32(t0, t1, flags >> 1);
}

#endif

2453 2454
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2455
{                                                                             \
A
aurel32 已提交
2456 2457 2458 2459
    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 已提交
2460 2461
}

2462 2463
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2464
{                                                                             \
A
aurel32 已提交
2465
    TCGv EA;                                                                  \
2466 2467
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2468
        GEN_EXCP_INVAL(ctx);                                                  \
2469
        return;                                                               \
2470
    }                                                                         \
A
aurel32 已提交
2471
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
J
j_mayer 已提交
2472
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2473
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
J
j_mayer 已提交
2474
    else                                                                      \
A
aurel32 已提交
2475 2476 2477 2478
        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 已提交
2479 2480
}

2481 2482
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2483
{                                                                             \
A
aurel32 已提交
2484
    TCGv EA;                                                                  \
2485 2486
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2487
        GEN_EXCP_INVAL(ctx);                                                  \
2488
        return;                                                               \
2489
    }                                                                         \
A
aurel32 已提交
2490 2491 2492 2493 2494
    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 已提交
2495 2496
}

2497 2498
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2499
{                                                                             \
A
aurel32 已提交
2500 2501 2502 2503
    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 已提交
2504 2505
}

2506 2507 2508 2509 2510
#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 已提交
2511 2512

/* lbz lbzu lbzux lbzx */
A
aurel32 已提交
2513
GEN_LDS(8u, 0x02, PPC_INTEGER);
B
bellard 已提交
2514
/* lha lhau lhaux lhax */
A
aurel32 已提交
2515
GEN_LDS(16s, 0x0A, PPC_INTEGER);
B
bellard 已提交
2516
/* lhz lhzu lhzux lhzx */
A
aurel32 已提交
2517
GEN_LDS(16u, 0x08, PPC_INTEGER);
B
bellard 已提交
2518
/* lwz lwzu lwzux lwzx */
A
aurel32 已提交
2519
GEN_LDS(32u, 0x00, PPC_INTEGER);
2520 2521
#if defined(TARGET_PPC64)
/* lwaux */
A
aurel32 已提交
2522
GEN_LDUX(32s, 0x15, 0x0B, PPC_64B);
2523
/* lwax */
A
aurel32 已提交
2524
GEN_LDX(32s, 0x15, 0x0A, PPC_64B);
2525
/* ldux */
A
aurel32 已提交
2526
GEN_LDUX(64, 0x15, 0x01, PPC_64B);
2527
/* ldx */
A
aurel32 已提交
2528
GEN_LDX(64, 0x15, 0x00, PPC_64B);
2529 2530
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
A
aurel32 已提交
2531
    TCGv EA;
2532 2533 2534
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2535
            GEN_EXCP_INVAL(ctx);
2536 2537 2538
            return;
        }
    }
A
aurel32 已提交
2539 2540
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_imm_index(EA, ctx, 0x03);
2541 2542
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
A
aurel32 已提交
2543
        gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2544 2545
    } else {
        /* ld - ldu */
A
aurel32 已提交
2546
        gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2547 2548
    }
    if (Rc(ctx->opcode))
A
aurel32 已提交
2549 2550
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
    tcg_temp_free(EA);
2551
}
2552 2553 2554 2555 2556 2557 2558
/* 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 已提交
2559
    TCGv EA;
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576

    /* 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 已提交
2577 2578 2579 2580 2581 2582
    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);
2583 2584
#endif
}
2585
#endif
B
bellard 已提交
2586 2587

/***                              Integer store                            ***/
2588 2589
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2590
{                                                                             \
A
aurel32 已提交
2591 2592 2593 2594
    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 已提交
2595 2596
}

2597 2598
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2599
{                                                                             \
A
aurel32 已提交
2600
    TCGv EA;                                                                  \
2601
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2602
        GEN_EXCP_INVAL(ctx);                                                  \
2603
        return;                                                               \
2604
    }                                                                         \
A
aurel32 已提交
2605
    EA = tcg_temp_new(TCG_TYPE_TL);                                           \
J
j_mayer 已提交
2606
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2607
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
J
j_mayer 已提交
2608
    else                                                                      \
A
aurel32 已提交
2609 2610 2611 2612
        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 已提交
2613 2614
}

2615 2616
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2617
{                                                                             \
A
aurel32 已提交
2618
    TCGv EA;                                                                  \
2619
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2620
        GEN_EXCP_INVAL(ctx);                                                  \
2621
        return;                                                               \
2622
    }                                                                         \
A
aurel32 已提交
2623 2624 2625 2626 2627
    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 已提交
2628 2629
}

2630 2631
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2632
{                                                                             \
A
aurel32 已提交
2633 2634 2635 2636
    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 已提交
2637 2638
}

2639 2640 2641 2642 2643
#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 已提交
2644 2645

/* stb stbu stbux stbx */
A
aurel32 已提交
2646
GEN_STS(8, 0x06, PPC_INTEGER);
B
bellard 已提交
2647
/* sth sthu sthux sthx */
A
aurel32 已提交
2648
GEN_STS(16, 0x0C, PPC_INTEGER);
B
bellard 已提交
2649
/* stw stwu stwux stwx */
A
aurel32 已提交
2650
GEN_STS(32, 0x04, PPC_INTEGER);
2651
#if defined(TARGET_PPC64)
A
aurel32 已提交
2652 2653
GEN_STUX(64, 0x15, 0x05, PPC_64B);
GEN_STX(64, 0x15, 0x04, PPC_64B);
2654
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2655
{
2656
    int rs;
A
aurel32 已提交
2657
    TCGv EA;
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669

    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)) {
2670
            GEN_EXCP_INVAL(ctx);
2671 2672
            return;
        }
2673 2674 2675 2676 2677
        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 已提交
2678 2679 2680 2681 2682 2683
        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);
2684 2685 2686 2687 2688 2689 2690 2691 2692
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
                GEN_EXCP_INVAL(ctx);
                return;
            }
        }
A
aurel32 已提交
2693 2694 2695
        EA = tcg_temp_new(TCG_TYPE_TL);
        gen_addr_imm_index(EA, ctx, 0x03);
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
2696
        if (Rc(ctx->opcode))
A
aurel32 已提交
2697 2698
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
        tcg_temp_free(EA);
2699 2700 2701
    }
}
#endif
B
bellard 已提交
2702 2703
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
A
aurel32 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
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 已提交
2714
/* lwbrx */
A
aurel32 已提交
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
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 已提交
2725
/* sthbrx */
A
aurel32 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
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 已提交
2737
/* stwbrx */
A
aurel32 已提交
2738 2739 2740 2741 2742 2743 2744 2745 2746
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 已提交
2747 2748

/***                    Integer load and store multiple                    ***/
2749
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2750 2751
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lmw),
2752
};
2753 2754
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stmw),
2755
};
2756

B
bellard 已提交
2757 2758 2759
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2760
    /* NIP cannot be restored if the memory exception comes from an helper */
2761
    gen_update_nip(ctx, ctx->nip - 4);
2762
    gen_addr_imm_index(cpu_T[0], ctx, 0);
2763
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2764 2765 2766 2767 2768
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2769
    /* NIP cannot be restored if the memory exception comes from an helper */
2770
    gen_update_nip(ctx, ctx->nip - 4);
2771
    gen_addr_imm_index(cpu_T[0], ctx, 0);
2772
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2773 2774 2775
}

/***                    Integer load and store strings                     ***/
2776 2777
#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)
2778 2779 2780 2781 2782 2783 2784 2785 2786
/* 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
2787 2788
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswi),
2789
};
2790 2791 2792 2793 2794 2795 2796 2797
#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
2798 2799
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswx),
2800
};
2801 2802 2803 2804 2805 2806 2807 2808
#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
2809 2810
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stsw),
2811 2812
};

B
bellard 已提交
2813
/* lswi */
2814
/* PowerPC32 specification says we must generate an exception if
2815 2816 2817 2818
 * 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...
 */
2819
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
B
bellard 已提交
2820 2821 2822
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
2823
    int ra = rA(ctx->opcode);
B
bellard 已提交
2824 2825 2826 2827 2828
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
2829 2830 2831
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2832 2833
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2834
        return;
B
bellard 已提交
2835
    }
2836
    /* NIP cannot be restored if the memory exception comes from an helper */
2837
    gen_update_nip(ctx, ctx->nip - 4);
2838
    gen_addr_register(cpu_T[0], ctx);
2839
    tcg_gen_movi_tl(cpu_T[1], nb);
2840
    op_ldsts(lswi, start);
B
bellard 已提交
2841 2842 2843
}

/* lswx */
2844
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
B
bellard 已提交
2845
{
2846 2847 2848
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

2849
    /* NIP cannot be restored if the memory exception comes from an helper */
2850
    gen_update_nip(ctx, ctx->nip - 4);
2851
    gen_addr_reg_index(cpu_T[0], ctx);
2852 2853
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
2854
    }
2855 2856
    gen_op_load_xer_bc();
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
2857 2858 2859
}

/* stswi */
2860
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
B
bellard 已提交
2861
{
B
bellard 已提交
2862 2863
    int nb = NB(ctx->opcode);

2864
    /* NIP cannot be restored if the memory exception comes from an helper */
2865
    gen_update_nip(ctx, ctx->nip - 4);
2866
    gen_addr_register(cpu_T[0], ctx);
B
bellard 已提交
2867 2868
    if (nb == 0)
        nb = 32;
2869
    tcg_gen_movi_tl(cpu_T[1], nb);
2870
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2871 2872 2873
}

/* stswx */
2874
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
B
bellard 已提交
2875
{
2876
    /* NIP cannot be restored if the memory exception comes from an helper */
2877
    gen_update_nip(ctx, ctx->nip - 4);
2878
    gen_addr_reg_index(cpu_T[0], ctx);
2879
    gen_op_load_xer_bc();
2880
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2881 2882 2883 2884
}

/***                        Memory synchronisation                         ***/
/* eieio */
2885
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
2886 2887 2888 2889
{
}

/* isync */
2890
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2891
{
2892
    GEN_STOP(ctx);
B
bellard 已提交
2893 2894
}

2895 2896
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2897 2898
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lwarx),
2899
};
2900 2901
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stwcx),
B
bellard 已提交
2902
};
2903

2904
/* lwarx */
2905
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
2906
{
2907 2908
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2909
    gen_addr_reg_index(cpu_T[0], ctx);
B
bellard 已提交
2910
    op_lwarx();
A
aurel32 已提交
2911
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
B
bellard 已提交
2912 2913 2914
}

/* stwcx. */
2915
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
2916
{
2917 2918
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2919
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
2920
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
2921
    op_stwcx();
B
bellard 已提交
2922 2923
}

J
j_mayer 已提交
2924 2925 2926
#if defined(TARGET_PPC64)
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
2927 2928
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ldarx),
J
j_mayer 已提交
2929
};
2930 2931
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stdcx),
J
j_mayer 已提交
2932 2933 2934
};

/* ldarx */
2935
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
2936
{
2937 2938
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2939
    gen_addr_reg_index(cpu_T[0], ctx);
J
j_mayer 已提交
2940
    op_ldarx();
A
aurel32 已提交
2941
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
J
j_mayer 已提交
2942 2943 2944
}

/* stdcx. */
2945
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
2946
{
2947 2948
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2949
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
2950
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
J
j_mayer 已提交
2951 2952 2953 2954
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
2955
/* sync */
2956
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
2957 2958 2959
{
}

2960 2961 2962 2963
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
    /* Stop translation, as the CPU is supposed to sleep from now */
2964 2965
    gen_op_wait();
    GEN_EXCP(ctx, EXCP_HLT, 1);
2966 2967
}

B
bellard 已提交
2968
/***                         Floating-point load                           ***/
2969 2970
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2971
{                                                                             \
2972
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2973
        GEN_EXCP_NO_FP(ctx);                                                  \
2974 2975
        return;                                                               \
    }                                                                         \
2976
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
2977
    op_ldst(l##width);                                                        \
A
aurel32 已提交
2978
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
B
bellard 已提交
2979 2980
}

2981 2982
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2983
{                                                                             \
2984
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2985
        GEN_EXCP_NO_FP(ctx);                                                  \
2986 2987
        return;                                                               \
    }                                                                         \
2988
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2989
        GEN_EXCP_INVAL(ctx);                                                  \
2990
        return;                                                               \
2991
    }                                                                         \
2992
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
2993
    op_ldst(l##width);                                                        \
A
aurel32 已提交
2994
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
A
aurel32 已提交
2995
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
2996 2997
}

2998 2999
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
3000
{                                                                             \
3001
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3002
        GEN_EXCP_NO_FP(ctx);                                                  \
3003 3004
        return;                                                               \
    }                                                                         \
3005
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3006
        GEN_EXCP_INVAL(ctx);                                                  \
3007
        return;                                                               \
3008
    }                                                                         \
3009
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
3010
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3011
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
A
aurel32 已提交
3012
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3013 3014
}

3015 3016
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
3017
{                                                                             \
3018
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3019
        GEN_EXCP_NO_FP(ctx);                                                  \
3020 3021
        return;                                                               \
    }                                                                         \
3022
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
3023
    op_ldst(l##width);                                                        \
A
aurel32 已提交
3024
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
B
bellard 已提交
3025 3026
}

3027
#define GEN_LDFS(width, op, type)                                             \
3028
OP_LD_TABLE(width);                                                           \
3029 3030 3031 3032
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 已提交
3033 3034

/* lfd lfdu lfdux lfdx */
3035
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
3036
/* lfs lfsu lfsux lfsx */
3037
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
3038 3039

/***                         Floating-point store                          ***/
3040 3041
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
3042
{                                                                             \
3043
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3044
        GEN_EXCP_NO_FP(ctx);                                                  \
3045 3046
        return;                                                               \
    }                                                                         \
3047
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
A
aurel32 已提交
3048
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3049
    op_ldst(st##width);                                                       \
B
bellard 已提交
3050 3051
}

3052 3053
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
3054
{                                                                             \
3055
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3056
        GEN_EXCP_NO_FP(ctx);                                                  \
3057 3058
        return;                                                               \
    }                                                                         \
3059
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3060
        GEN_EXCP_INVAL(ctx);                                                  \
3061
        return;                                                               \
3062
    }                                                                         \
3063
    gen_addr_imm_index(cpu_T[0], ctx, 0);                                     \
A
aurel32 已提交
3064
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3065
    op_ldst(st##width);                                                       \
A
aurel32 已提交
3066
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3067 3068
}

3069 3070
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
3071
{                                                                             \
3072
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3073
        GEN_EXCP_NO_FP(ctx);                                                  \
3074 3075
        return;                                                               \
    }                                                                         \
3076
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3077
        GEN_EXCP_INVAL(ctx);                                                  \
3078
        return;                                                               \
3079
    }                                                                         \
3080
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
3081
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3082
    op_ldst(st##width);                                                       \
A
aurel32 已提交
3083
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
B
bellard 已提交
3084 3085
}

3086 3087
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
3088
{                                                                             \
3089
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3090
        GEN_EXCP_NO_FP(ctx);                                                  \
3091 3092
        return;                                                               \
    }                                                                         \
3093
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
3094
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
3095
    op_ldst(st##width);                                                       \
B
bellard 已提交
3096 3097
}

3098
#define GEN_STFS(width, op, type)                                             \
3099
OP_ST_TABLE(width);                                                           \
3100 3101 3102 3103
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 已提交
3104 3105

/* stfd stfdu stfdux stfdx */
3106
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
3107
/* stfs stfsu stfsux stfsx */
3108
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3109 3110 3111

/* Optional: */
/* stfiwx */
J
j_mayer 已提交
3112 3113
OP_ST_TABLE(fiw);
GEN_STXF(fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3114 3115

/***                                Branch                                 ***/
3116 3117
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
3118 3119 3120
{
    TranslationBlock *tb;
    tb = ctx->tb;
B
bellard 已提交
3121
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3122
        likely(!ctx->singlestep_enabled)) {
B
bellard 已提交
3123
        tcg_gen_goto_tb(n);
A
aurel32 已提交
3124
        tcg_gen_movi_tl(cpu_T[1], dest);
3125 3126
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
A
aurel32 已提交
3127
            tcg_gen_andi_tl(cpu_nip, cpu_T[1], ~3);
3128 3129
        else
#endif
A
aurel32 已提交
3130
            tcg_gen_andi_tl(cpu_nip, cpu_T[1], (uint32_t)~3);
B
bellard 已提交
3131
        tcg_gen_exit_tb((long)tb + n);
3132
    } else {
A
aurel32 已提交
3133
        tcg_gen_movi_tl(cpu_T[1], dest);
3134 3135
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
A
aurel32 已提交
3136
            tcg_gen_andi_tl(cpu_nip, cpu_T[1], ~3);
3137 3138
        else
#endif
A
aurel32 已提交
3139
            tcg_gen_andi_tl(cpu_nip, cpu_T[1], (uint32_t)~3);
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
        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 已提交
3154
        tcg_gen_exit_tb(0);
3155
    }
B
bellard 已提交
3156 3157
}

3158
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3159 3160 3161 3162 3163 3164 3165 3166 3167
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode != 0 && (nip >> 32))
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
    else
#endif
        gen_op_setlr(ctx->nip);
}

B
bellard 已提交
3168 3169 3170
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3171
    target_ulong li, target;
B
bellard 已提交
3172

3173
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3174
    /* sign extend LI */
3175
#if defined(TARGET_PPC64)
3176 3177 3178
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3179
#endif
3180
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3181
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3182
        target = ctx->nip + li - 4;
B
bellard 已提交
3183
    else
3184
        target = li;
3185
#if defined(TARGET_PPC64)
3186 3187
    if (!ctx->sf_mode)
        target = (uint32_t)target;
3188
#endif
3189 3190
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3191
    gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3192 3193
}

3194 3195 3196 3197
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3198
static always_inline void gen_bcond (DisasContext *ctx, int type)
3199
{
3200 3201
    target_ulong target = 0;
    target_ulong li;
3202 3203 3204
    uint32_t bo = BO(ctx->opcode);
    uint32_t bi = BI(ctx->opcode);
    uint32_t mask;
3205

3206
    ctx->exception = POWERPC_EXCP_BRANCH;
3207
    if ((bo & 0x4) == 0)
3208
        gen_op_dec_ctr();
3209 3210
    switch(type) {
    case BCOND_IM:
3211 3212
        li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
B
bellard 已提交
3213
            target = ctx->nip + li - 4;
3214 3215 3216
        } else {
            target = li;
        }
3217 3218 3219 3220
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode)
            target = (uint32_t)target;
#endif
3221 3222 3223 3224 3225 3226 3227 3228 3229
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
3230 3231
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3232
    if (bo & 0x10) {
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
        /* No CR condition */
        switch (bo & 0x6) {
        case 0:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctr_64();
            else
#endif
                gen_op_test_ctr();
            break;
        case 2:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctrz_64();
            else
#endif
                gen_op_test_ctrz();
3250 3251
            break;
        default:
3252 3253
        case 4:
        case 6:
3254
            if (type == BCOND_IM) {
3255
                gen_goto_tb(ctx, 0, target);
3256
                return;
3257
            } else {
3258 3259
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
A
aurel32 已提交
3260
                    tcg_gen_andi_tl(cpu_nip, cpu_T[1], ~3);
3261 3262
                else
#endif
A
aurel32 已提交
3263
                    tcg_gen_andi_tl(cpu_nip, cpu_T[1], (uint32_t)~3);
3264
                goto no_test;
3265
            }
3266
            break;
3267
        }
3268 3269
    } else {
        mask = 1 << (3 - (bi & 0x03));
A
aurel32 已提交
3270
        tcg_gen_mov_i32(cpu_T[0], cpu_crf[bi >> 2]);
3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
        if (bo & 0x8) {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_true_64(mask);
                else
#endif
                    gen_op_test_ctr_true(mask);
                break;
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_true_64(mask);
                else
#endif
                    gen_op_test_ctrz_true(mask);
                break;
            default:
            case 4:
            case 6:
3292
                gen_op_test_true(mask);
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
                break;
            }
        } else {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_false_64(mask);
                else
#endif
                    gen_op_test_ctr_false(mask);
3304
                break;
3305 3306 3307 3308 3309 3310 3311 3312
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_false_64(mask);
                else
#endif
                    gen_op_test_ctrz_false(mask);
                break;
3313
            default:
3314 3315
            case 4:
            case 6:
3316
                gen_op_test_false(mask);
3317 3318 3319 3320
                break;
            }
        }
    }
3321
    if (type == BCOND_IM) {
B
bellard 已提交
3322 3323
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
3324
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3325
        gen_set_label(l1);
3326
        gen_goto_tb(ctx, 1, ctx->nip);
3327
    } else {
3328 3329 3330 3331 3332 3333
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
        else
#endif
            gen_op_btest_T1(ctx->nip);
3334
    no_test:
B
bellard 已提交
3335
        tcg_gen_exit_tb(0);
J
j_mayer 已提交
3336
    }
3337 3338 3339
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3340
{
3341 3342 3343 3344
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3345
{
3346 3347 3348 3349
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3350
{
3351 3352
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3353 3354 3355 3356 3357

/***                      Condition register logical                       ***/
#define GEN_CRLOGIC(op, opc)                                                  \
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
{                                                                             \
3358 3359
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
A
aurel32 已提交
3360
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[crbA(ctx->opcode) >> 2]);               \
3361 3362 3363 3364 3365
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
        gen_op_srli_T0(sh);                                                   \
    else if (sh < 0)                                                          \
        gen_op_sli_T0(-sh);                                                   \
A
aurel32 已提交
3366
    tcg_gen_mov_i32(cpu_T[1], cpu_crf[crbB(ctx->opcode) >> 2]);               \
3367 3368 3369 3370 3371
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
        gen_op_srli_T1(sh);                                                   \
    else if (sh < 0)                                                          \
        gen_op_sli_T1(-sh);                                                   \
B
bellard 已提交
3372
    gen_op_##op();                                                            \
3373
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
A
aurel32 已提交
3374
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], bitmask);                             \
A
aurel32 已提交
3375
    tcg_gen_andi_i32(cpu_T[1], cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);    \
3376
    gen_op_or();                                                              \
A
aurel32 已提交
3377
    tcg_gen_andi_i32(cpu_crf[crbD(ctx->opcode) >> 2], cpu_T[0], 0xf);         \
B
bellard 已提交
3378 3379 3380
}

/* crand */
3381
GEN_CRLOGIC(and, 0x08);
B
bellard 已提交
3382
/* crandc */
3383
GEN_CRLOGIC(andc, 0x04);
B
bellard 已提交
3384
/* creqv */
3385
GEN_CRLOGIC(eqv, 0x09);
B
bellard 已提交
3386
/* crnand */
3387
GEN_CRLOGIC(nand, 0x07);
B
bellard 已提交
3388
/* crnor */
3389
GEN_CRLOGIC(nor, 0x01);
B
bellard 已提交
3390
/* cror */
3391
GEN_CRLOGIC(or, 0x0E);
B
bellard 已提交
3392
/* crorc */
3393
GEN_CRLOGIC(orc, 0x0D);
B
bellard 已提交
3394
/* crxor */
3395
GEN_CRLOGIC(xor, 0x06);
B
bellard 已提交
3396 3397 3398
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
A
aurel32 已提交
3399
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
B
bellard 已提交
3400 3401 3402 3403
}

/***                           System linkage                              ***/
/* rfi (supervisor only) */
3404
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3405
{
3406
#if defined(CONFIG_USER_ONLY)
3407
    GEN_EXCP_PRIVOPC(ctx);
3408 3409
#else
    /* Restore CPU state */
3410
    if (unlikely(!ctx->supervisor)) {
3411
        GEN_EXCP_PRIVOPC(ctx);
3412
        return;
3413
    }
3414
    gen_op_rfi();
3415
    GEN_SYNC(ctx);
3416
#endif
B
bellard 已提交
3417 3418
}

J
j_mayer 已提交
3419
#if defined(TARGET_PPC64)
3420
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3421 3422
{
#if defined(CONFIG_USER_ONLY)
3423
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3424 3425 3426
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3427
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3428 3429
        return;
    }
3430
    gen_op_rfid();
3431
    GEN_SYNC(ctx);
J
j_mayer 已提交
3432 3433 3434
#endif
}

J
j_mayer 已提交
3435
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
{
#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 已提交
3451
/* sc */
3452 3453 3454 3455 3456
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3457
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3458
{
3459 3460 3461
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3462
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3463 3464 3465 3466
}

/***                                Trap                                   ***/
/* tw */
3467
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3468
{
A
aurel32 已提交
3469 3470
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3471
    /* Update the nip since this might generate a trap exception */
3472
    gen_update_nip(ctx, ctx->nip);
3473
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3474 3475 3476 3477 3478
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
A
aurel32 已提交
3479
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
3480
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3481 3482
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3483
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3484 3485
}

3486 3487 3488 3489
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
A
aurel32 已提交
3490 3491
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3492 3493 3494 3495 3496 3497 3498 3499
    /* 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 已提交
3500
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
3501
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3502 3503 3504 3505 3506 3507
    /* 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 已提交
3508 3509 3510 3511 3512
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
    gen_op_load_xer_cr();
A
aurel32 已提交
3513
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
J
j_mayer 已提交
3514 3515
    gen_op_clear_xer_ov();
    gen_op_clear_xer_ca();
B
bellard 已提交
3516 3517 3518
}

/* mfcr */
3519
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3520
{
3521
    uint32_t crm, crn;
3522

3523 3524 3525 3526
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
A
aurel32 已提交
3527
            tcg_gen_mov_i32(cpu_T[0], cpu_crf[7 - crn]);
3528
        }
3529
    } else {
A
aurel32 已提交
3530
        gen_op_load_cr();
3531
    }
A
aurel32 已提交
3532
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
B
bellard 已提交
3533 3534 3535 3536 3537
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3538
#if defined(CONFIG_USER_ONLY)
3539
    GEN_EXCP_PRIVREG(ctx);
3540
#else
3541
    if (unlikely(!ctx->supervisor)) {
3542
        GEN_EXCP_PRIVREG(ctx);
3543
        return;
3544
    }
A
aurel32 已提交
3545
    gen_op_load_msr();
A
aurel32 已提交
3546
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3547
#endif
B
bellard 已提交
3548 3549
}

J
j_mayer 已提交
3550
#if 1
3551
#define SPR_NOACCESS ((void *)(-1UL))
3552 3553 3554 3555 3556 3557 3558 3559 3560
#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 已提交
3561
/* mfspr */
3562
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3563
{
3564
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3565 3566
    uint32_t sprn = SPR(ctx->opcode);

3567
#if !defined(CONFIG_USER_ONLY)
3568 3569
    if (ctx->supervisor == 2)
        read_cb = ctx->spr_cb[sprn].hea_read;
3570
    else if (ctx->supervisor)
3571 3572
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3573
#endif
3574
        read_cb = ctx->spr_cb[sprn].uea_read;
3575 3576
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3577
            (*read_cb)(ctx, sprn);
A
aurel32 已提交
3578
            tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3579 3580
        } else {
            /* Privilege exception */
3581 3582 3583 3584 3585 3586
            /* 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) {
3587
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
J
j_mayer 已提交
3588
                            ADDRX "\n", sprn, sprn, ctx->nip);
3589
                }
J
j_mayer 已提交
3590 3591
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
                       sprn, sprn, ctx->nip);
3592
            }
3593
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3594
        }
3595 3596
    } else {
        /* Not defined */
J
j_mayer 已提交
3597
        if (loglevel != 0) {
J
j_mayer 已提交
3598 3599
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3600
        }
J
j_mayer 已提交
3601 3602
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3603 3604
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3605 3606 3607
    }
}

3608
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3609
{
3610
    gen_op_mfspr(ctx);
3611
}
3612 3613

/* mftb */
3614
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3615 3616
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3617 3618 3619
}

/* mtcrf */
3620
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3621
{
3622
    uint32_t crm, crn;
3623

A
aurel32 已提交
3624
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3625 3626 3627 3628
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
        crn = ffs(crm);
        gen_op_srli_T0(crn * 4);
A
aurel32 已提交
3629
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_T[0], 0xf);
3630
    } else {
A
aurel32 已提交
3631
        gen_op_store_cr(crm);
3632
    }
B
bellard 已提交
3633 3634 3635
}

/* mtmsr */
J
j_mayer 已提交
3636
#if defined(TARGET_PPC64)
3637
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3638 3639
{
#if defined(CONFIG_USER_ONLY)
3640
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3641 3642
#else
    if (unlikely(!ctx->supervisor)) {
3643
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3644 3645
        return;
    }
A
aurel32 已提交
3646
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3647 3648 3649 3650
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3651 3652 3653 3654
        /* 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
         */
3655
        gen_update_nip(ctx, ctx->nip);
A
aurel32 已提交
3656
        gen_op_store_msr();
3657 3658
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
3659
        ctx->exception = POWERPC_EXCP_STOP;
3660
    }
J
j_mayer 已提交
3661 3662 3663 3664
#endif
}
#endif

B
bellard 已提交
3665 3666
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3667
#if defined(CONFIG_USER_ONLY)
3668
    GEN_EXCP_PRIVREG(ctx);
3669
#else
3670
    if (unlikely(!ctx->supervisor)) {
3671
        GEN_EXCP_PRIVREG(ctx);
3672
        return;
3673
    }
A
aurel32 已提交
3674
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3675 3676 3677 3678
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3679 3680 3681 3682
        /* 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
         */
3683
        gen_update_nip(ctx, ctx->nip);
3684
#if defined(TARGET_PPC64)
3685
        if (!ctx->sf_mode)
A
aurel32 已提交
3686
            gen_op_store_msr_32();
3687
        else
3688
#endif
A
aurel32 已提交
3689
            gen_op_store_msr();
3690 3691
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsrd is not always defined as context-synchronizing */
3692
        ctx->exception = POWERPC_EXCP_STOP;
3693
    }
3694
#endif
B
bellard 已提交
3695 3696 3697 3698 3699
}

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

3703
#if !defined(CONFIG_USER_ONLY)
3704 3705
    if (ctx->supervisor == 2)
        write_cb = ctx->spr_cb[sprn].hea_write;
3706
    else if (ctx->supervisor)
3707 3708
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3709
#endif
3710
        write_cb = ctx->spr_cb[sprn].uea_write;
3711 3712
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
A
aurel32 已提交
3713
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3714 3715 3716
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3717
            if (loglevel != 0) {
J
j_mayer 已提交
3718 3719
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
                        ADDRX "\n", sprn, sprn, ctx->nip);
3720
            }
J
j_mayer 已提交
3721 3722
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
                   sprn, sprn, ctx->nip);
3723
            GEN_EXCP_PRIVREG(ctx);
3724
        }
3725 3726
    } else {
        /* Not defined */
J
j_mayer 已提交
3727
        if (loglevel != 0) {
J
j_mayer 已提交
3728 3729
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3730
        }
J
j_mayer 已提交
3731 3732
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3733 3734
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3735 3736 3737 3738 3739
    }
}

/***                         Cache management                              ***/
/* dcbf */
3740
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
3741
{
J
j_mayer 已提交
3742
    /* XXX: specification says this is treated as a load by the MMU */
A
aurel32 已提交
3743 3744 3745 3746
    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 已提交
3747 3748 3749
}

/* dcbi (Supervisor only) */
3750
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3751
{
3752
#if defined(CONFIG_USER_ONLY)
3753
    GEN_EXCP_PRIVOPC(ctx);
3754
#else
A
aurel32 已提交
3755
    TCGv EA, val;
3756
    if (unlikely(!ctx->supervisor)) {
3757
        GEN_EXCP_PRIVOPC(ctx);
3758
        return;
3759
    }
A
aurel32 已提交
3760 3761
    EA = tcg_temp_new(TCG_TYPE_TL);
    gen_addr_reg_index(EA, ctx);
3762
    /* XXX: specification says this should be treated as a store by the MMU */
A
aurel32 已提交
3763 3764 3765 3766
    gen_qemu_ld8u(val, EA, ctx->mem_idx);
    gen_qemu_st8(val, EA, ctx->mem_idx);
    tcg_temp_free(val);
    tcg_temp_free(EA);
3767
#endif
B
bellard 已提交
3768 3769 3770
}

/* dcdst */
3771
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3772
{
3773
    /* XXX: specification say this is treated as a load by the MMU */
A
aurel32 已提交
3774 3775 3776 3777
    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 已提交
3778 3779 3780
}

/* dcbt */
3781
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
3782
{
3783
    /* interpreted as no-op */
3784 3785 3786
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3787 3788 3789
}

/* dcbtst */
3790
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
3791
{
3792
    /* interpreted as no-op */
3793 3794 3795
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3796 3797 3798
}

/* dcbz */
3799
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3800 3801
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
    /* 32 bytes cache line size */
3802
    {
3803 3804 3805 3806 3807 3808 3809 3810 3811
#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),
3812
    },
3813
    /* 64 bytes cache line size */
3814
    {
3815 3816 3817 3818 3819 3820 3821 3822 3823
#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),
3824
    },
3825
    /* 128 bytes cache line size */
3826
    {
3827 3828 3829 3830 3831 3832 3833 3834 3835
#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),
3836
    },
3837
    /* tunable cache line size */
3838
    {
3839 3840 3841 3842 3843 3844 3845 3846 3847
#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),
3848
    },
3849
};
3850

3851 3852
static always_inline void handler_dcbz (DisasContext *ctx,
                                        int dcache_line_size)
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
{
    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 已提交
3874
{
3875
    gen_addr_reg_index(cpu_T[0], ctx);
3876 3877 3878 3879
    handler_dcbz(ctx, ctx->dcache_line_size);
    gen_op_check_reservation();
}

3880
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3881
{
3882
    gen_addr_reg_index(cpu_T[0], ctx);
3883 3884 3885 3886
    if (ctx->opcode & 0x00200000)
        handler_dcbz(ctx, ctx->dcache_line_size);
    else
        handler_dcbz(ctx, -1);
B
bellard 已提交
3887
    gen_op_check_reservation();
B
bellard 已提交
3888 3889 3890
}

/* icbi */
3891
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
#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),
3902
};
3903

3904
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
3905
{
3906 3907
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3908
    gen_addr_reg_index(cpu_T[0], ctx);
3909
    op_icbi();
B
bellard 已提交
3910 3911 3912 3913
}

/* Optional: */
/* dcba */
3914
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
3915
{
3916 3917 3918 3919
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3920 3921 3922 3923 3924 3925 3926
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
3927
#if defined(CONFIG_USER_ONLY)
3928
    GEN_EXCP_PRIVREG(ctx);
3929
#else
3930
    if (unlikely(!ctx->supervisor)) {
3931
        GEN_EXCP_PRIVREG(ctx);
3932
        return;
3933
    }
3934
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3935
    gen_op_load_sr();
A
aurel32 已提交
3936
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3937
#endif
B
bellard 已提交
3938 3939 3940
}

/* mfsrin */
3941
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3942
{
3943
#if defined(CONFIG_USER_ONLY)
3944
    GEN_EXCP_PRIVREG(ctx);
3945
#else
3946
    if (unlikely(!ctx->supervisor)) {
3947
        GEN_EXCP_PRIVREG(ctx);
3948
        return;
3949
    }
A
aurel32 已提交
3950
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3951 3952
    gen_op_srli_T1(28);
    gen_op_load_sr();
A
aurel32 已提交
3953
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3954
#endif
B
bellard 已提交
3955 3956 3957
}

/* mtsr */
B
bellard 已提交
3958
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
3959
{
3960
#if defined(CONFIG_USER_ONLY)
3961
    GEN_EXCP_PRIVREG(ctx);
3962
#else
3963
    if (unlikely(!ctx->supervisor)) {
3964
        GEN_EXCP_PRIVREG(ctx);
3965
        return;
3966
    }
A
aurel32 已提交
3967
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3968
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3969
    gen_op_store_sr();
3970
#endif
B
bellard 已提交
3971 3972 3973
}

/* mtsrin */
3974
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3975
{
3976
#if defined(CONFIG_USER_ONLY)
3977
    GEN_EXCP_PRIVREG(ctx);
3978
#else
3979
    if (unlikely(!ctx->supervisor)) {
3980
        GEN_EXCP_PRIVREG(ctx);
3981
        return;
3982
    }
A
aurel32 已提交
3983 3984
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3985 3986
    gen_op_srli_T1(28);
    gen_op_store_sr();
3987
#endif
B
bellard 已提交
3988 3989
}

3990 3991 3992
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
3993
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
3994 3995 3996 3997 3998 3999 4000 4001
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
4002
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4003
    gen_op_load_slb();
A
aurel32 已提交
4004
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4005 4006 4007 4008
#endif
}

/* mfsrin */
4009 4010
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
4011 4012 4013 4014 4015 4016 4017 4018
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4019
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4020 4021
    gen_op_srli_T1(28);
    gen_op_load_slb();
A
aurel32 已提交
4022
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4023 4024 4025 4026
#endif
}

/* mtsr */
4027
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4028 4029 4030 4031 4032 4033 4034 4035
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4036
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4037
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4038 4039 4040 4041 4042
    gen_op_store_slb();
#endif
}

/* mtsrin */
4043 4044
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
4045 4046 4047 4048 4049 4050 4051 4052
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
A
aurel32 已提交
4053 4054
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4055 4056 4057 4058 4059 4060
    gen_op_srli_T1(28);
    gen_op_store_slb();
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4061 4062 4063
/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
4064
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4065
{
4066
#if defined(CONFIG_USER_ONLY)
4067
    GEN_EXCP_PRIVOPC(ctx);
4068
#else
4069
    if (unlikely(!ctx->supervisor)) {
4070
        GEN_EXCP_PRIVOPC(ctx);
4071
        return;
4072 4073 4074
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
4075 4076 4077
}

/* tlbie */
4078
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4079
{
4080
#if defined(CONFIG_USER_ONLY)
4081
    GEN_EXCP_PRIVOPC(ctx);
4082
#else
4083
    if (unlikely(!ctx->supervisor)) {
4084
        GEN_EXCP_PRIVOPC(ctx);
4085
        return;
4086
    }
A
aurel32 已提交
4087
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4088 4089 4090 4091 4092 4093
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4094
#endif
B
bellard 已提交
4095 4096 4097
}

/* tlbsync */
4098
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4099
{
4100
#if defined(CONFIG_USER_ONLY)
4101
    GEN_EXCP_PRIVOPC(ctx);
4102
#else
4103
    if (unlikely(!ctx->supervisor)) {
4104
        GEN_EXCP_PRIVOPC(ctx);
4105
        return;
4106 4107 4108 4109
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
4110
    GEN_STOP(ctx);
4111
#endif
B
bellard 已提交
4112 4113
}

J
j_mayer 已提交
4114 4115 4116 4117 4118
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4119
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4120 4121
#else
    if (unlikely(!ctx->supervisor)) {
4122
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4123 4124 4125 4126 4127 4128 4129 4130 4131 4132
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4133
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4134 4135
#else
    if (unlikely(!ctx->supervisor)) {
4136
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4137 4138
        return;
    }
A
aurel32 已提交
4139
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
4140 4141 4142 4143 4144
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
4145 4146
/***                              External control                         ***/
/* Optional: */
4147 4148
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4149 4150
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(eciwx),
4151
};
4152 4153
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ecowx),
4154
};
4155

4156
/* eciwx */
B
bellard 已提交
4157 4158
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
4159
    /* Should check EAR[E] & alignment ! */
4160
    gen_addr_reg_index(cpu_T[0], ctx);
4161
    op_eciwx();
A
aurel32 已提交
4162
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4163 4164 4165 4166 4167 4168
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
    /* Should check EAR[E] & alignment ! */
4169
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
4170
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4171 4172 4173 4174 4175 4176 4177
    op_ecowx();
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
A
aurel32 已提交
4178
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4179
    gen_op_POWER_abs();
A
aurel32 已提交
4180
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4181 4182 4183 4184 4185 4186 4187
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
A
aurel32 已提交
4188
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4189
    gen_op_POWER_abso();
A
aurel32 已提交
4190
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4191 4192 4193 4194 4195
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* clcs */
4196
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4197
{
A
aurel32 已提交
4198
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4199
    gen_op_POWER_clcs();
4200
    /* Rc=1 sets CR0 to an undefined state */
A
aurel32 已提交
4201
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4202 4203 4204 4205 4206
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4207 4208
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4209
    gen_op_POWER_div();
A
aurel32 已提交
4210
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4211 4212 4213 4214 4215 4216 4217
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4218 4219
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4220
    gen_op_POWER_divo();
A
aurel32 已提交
4221
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4222 4223 4224 4225 4226 4227 4228
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4229 4230
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4231
    gen_op_POWER_divs();
A
aurel32 已提交
4232
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4233 4234 4235 4236 4237 4238 4239
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4240 4241
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4242
    gen_op_POWER_divso();
A
aurel32 已提交
4243
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4244 4245 4246 4247 4248 4249 4250
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4251 4252
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4253
    gen_op_POWER_doz();
A
aurel32 已提交
4254
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4255 4256 4257 4258 4259 4260 4261
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4262 4263
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4264
    gen_op_POWER_dozo();
A
aurel32 已提交
4265
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4266 4267 4268 4269 4270 4271 4272
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4273
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4274
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4275
    gen_op_POWER_doz();
A
aurel32 已提交
4276
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4277 4278
}

4279 4280 4281
/* 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
 */
4282
#define op_POWER_lscbx(start, ra, rb)                                         \
4283
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297
#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),
4298 4299 4300 4301 4302 4303 4304 4305
};

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

4306
    gen_addr_reg_index(cpu_T[0], ctx);
4307 4308 4309 4310
    if (ra == 0) {
        ra = rb;
    }
    /* NIP cannot be restored if the memory exception comes from an helper */
4311
    gen_update_nip(ctx, ctx->nip - 4);
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322
    gen_op_load_xer_bc();
    gen_op_load_xer_cmp();
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
    gen_op_store_xer_bc();
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4323 4324
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4325
    gen_op_POWER_maskg();
A
aurel32 已提交
4326
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4327 4328 4329 4330 4331 4332 4333
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4334 4335 4336
    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)]);
4337
    gen_op_POWER_maskir();
A
aurel32 已提交
4338
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4339 4340 4341 4342 4343 4344 4345
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

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

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

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4368
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4369
    gen_op_POWER_nabs();
A
aurel32 已提交
4370
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4371 4372 4373 4374 4375 4376 4377
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

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

/* 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 已提交
4392 4393 4394
    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)]);
4395
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
A
aurel32 已提交
4396
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4397 4398 4399 4400 4401 4402 4403
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4404 4405 4406
    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)]);
4407
    gen_op_POWER_rrib();
A
aurel32 已提交
4408
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4409 4410 4411 4412 4413 4414 4415
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4416 4417
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4418
    gen_op_POWER_sle();
A
aurel32 已提交
4419
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4420 4421 4422 4423 4424 4425 4426
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4427 4428
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4429
    gen_op_POWER_sleq();
A
aurel32 已提交
4430
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4431 4432 4433 4434 4435 4436 4437
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4438
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4439
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4440
    gen_op_POWER_sle();
A
aurel32 已提交
4441
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4442 4443 4444 4445 4446 4447 4448
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4449
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4450
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4451
    gen_op_POWER_sleq();
A
aurel32 已提交
4452
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4453 4454 4455 4456 4457 4458 4459
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4460 4461
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4462
    gen_op_POWER_sllq();
A
aurel32 已提交
4463
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4464 4465 4466 4467 4468 4469 4470
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4471 4472
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4473
    gen_op_POWER_slq();
A
aurel32 已提交
4474
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4475 4476 4477 4478
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

4479
/* sraiq - sraiq. */
4480 4481
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4482
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4483
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4484
    gen_op_POWER_sraq();
A
aurel32 已提交
4485
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4486 4487 4488 4489 4490 4491 4492
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4493 4494
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4495
    gen_op_POWER_sraq();
A
aurel32 已提交
4496
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4497 4498 4499 4500 4501 4502 4503
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4504 4505
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4506
    gen_op_POWER_sre();
A
aurel32 已提交
4507
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4508 4509 4510 4511 4512 4513 4514
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4515 4516
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4517
    gen_op_POWER_srea();
A
aurel32 已提交
4518
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4519 4520 4521 4522 4523 4524 4525
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4526 4527
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4528
    gen_op_POWER_sreq();
A
aurel32 已提交
4529
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4530 4531 4532 4533 4534 4535 4536
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4537
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4538
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4539
    gen_op_POWER_srq();
A
aurel32 已提交
4540
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4541 4542 4543 4544 4545 4546 4547
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4548 4549
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4550
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4551
    gen_op_POWER_srlq();
A
aurel32 已提交
4552
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4553 4554 4555 4556 4557 4558 4559
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4560 4561
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4562
    gen_op_POWER_srlq();
A
aurel32 已提交
4563
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4564 4565 4566 4567 4568 4569 4570
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
A
aurel32 已提交
4571 4572
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4573
    gen_op_POWER_srq();
A
aurel32 已提交
4574
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4575 4576 4577 4578 4579 4580 4581 4582 4583
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4584
    GEN_EXCP_INVAL(ctx);
4585 4586 4587 4588 4589 4590
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4591
    GEN_EXCP_INVAL(ctx);
4592 4593 4594 4595 4596 4597
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4598
    GEN_EXCP_PRIVOPC(ctx);
4599 4600
#else
    if (unlikely(!ctx->supervisor)) {
4601
        GEN_EXCP_PRIVOPC(ctx);
4602 4603
        return;
    }
A
aurel32 已提交
4604
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4605
    gen_op_602_mfrom();
A
aurel32 已提交
4606
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4607 4608 4609 4610 4611
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
4612
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4613 4614
{
#if defined(CONFIG_USER_ONLY)
4615
    GEN_EXCP_PRIVOPC(ctx);
4616 4617
#else
    if (unlikely(!ctx->supervisor)) {
4618
        GEN_EXCP_PRIVOPC(ctx);
4619 4620
        return;
    }
A
aurel32 已提交
4621
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4622 4623 4624 4625 4626
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
4627
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4628 4629
{
#if defined(CONFIG_USER_ONLY)
4630
    GEN_EXCP_PRIVOPC(ctx);
4631 4632
#else
    if (unlikely(!ctx->supervisor)) {
4633
        GEN_EXCP_PRIVOPC(ctx);
4634 4635
        return;
    }
A
aurel32 已提交
4636
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4637 4638 4639 4640
    gen_op_6xx_tlbli();
#endif
}

4641 4642
/* 74xx TLB management */
/* tlbld */
4643
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4644 4645 4646 4647 4648 4649 4650 4651
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
A
aurel32 已提交
4652
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4653 4654 4655 4656 4657
    gen_op_74xx_tlbld();
#endif
}

/* tlbli */
4658
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4659 4660 4661 4662 4663 4664 4665 4666
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
A
aurel32 已提交
4667
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4668 4669 4670 4671
    gen_op_74xx_tlbli();
#endif
}

4672 4673 4674 4675 4676 4677 4678 4679 4680 4681
/* 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 已提交
4682
    /* Cache line invalidate: privileged and treated as no-op */
4683
#if defined(CONFIG_USER_ONLY)
4684
    GEN_EXCP_PRIVOPC(ctx);
4685 4686
#else
    if (unlikely(!ctx->supervisor)) {
4687
        GEN_EXCP_PRIVOPC(ctx);
4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
        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)
4702
    GEN_EXCP_PRIVOPC(ctx);
4703 4704
#else
    if (unlikely(!ctx->supervisor)) {
4705
        GEN_EXCP_PRIVOPC(ctx);
4706 4707 4708 4709 4710
        return;
    }
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);

4711
    gen_addr_reg_index(cpu_T[0], ctx);
4712
    gen_op_POWER_mfsri();
A
aurel32 已提交
4713
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
4714
    if (ra != 0 && ra != rd)
A
aurel32 已提交
4715
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
4716 4717 4718 4719 4720 4721
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4722
    GEN_EXCP_PRIVOPC(ctx);
4723 4724
#else
    if (unlikely(!ctx->supervisor)) {
4725
        GEN_EXCP_PRIVOPC(ctx);
4726 4727
        return;
    }
4728
    gen_addr_reg_index(cpu_T[0], ctx);
4729
    gen_op_POWER_rac();
A
aurel32 已提交
4730
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4731 4732 4733 4734 4735 4736
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4737
    GEN_EXCP_PRIVOPC(ctx);
4738 4739
#else
    if (unlikely(!ctx->supervisor)) {
4740
        GEN_EXCP_PRIVOPC(ctx);
4741 4742 4743
        return;
    }
    gen_op_POWER_rfsvc();
4744
    GEN_SYNC(ctx);
4745 4746 4747 4748 4749 4750 4751
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */
4752
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
4753 4754
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
#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),
4773
};
4774 4775
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER2_stfq),
4776 4777 4778 4779 4780 4781
};

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4782
    gen_update_nip(ctx, ctx->nip - 4);
4783
    gen_addr_imm_index(cpu_T[0], ctx, 0);
4784
    op_POWER2_lfq();
A
aurel32 已提交
4785 4786
    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]);
4787 4788 4789 4790 4791 4792 4793 4794
}

/* 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 */
4795
    gen_update_nip(ctx, ctx->nip - 4);
4796
    gen_addr_imm_index(cpu_T[0], ctx, 0);
4797
    op_POWER2_lfq();
A
aurel32 已提交
4798 4799
    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]);
4800
    if (ra != 0)
A
aurel32 已提交
4801
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4802 4803 4804 4805 4806 4807 4808 4809
}

/* 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 */
4810
    gen_update_nip(ctx, ctx->nip - 4);
4811
    gen_addr_reg_index(cpu_T[0], ctx);
4812
    op_POWER2_lfq();
A
aurel32 已提交
4813 4814
    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]);
4815
    if (ra != 0)
A
aurel32 已提交
4816
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4817 4818 4819 4820 4821 4822
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4823
    gen_update_nip(ctx, ctx->nip - 4);
4824
    gen_addr_reg_index(cpu_T[0], ctx);
4825
    op_POWER2_lfq();
A
aurel32 已提交
4826 4827
    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]);
4828 4829 4830 4831 4832 4833
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4834
    gen_update_nip(ctx, ctx->nip - 4);
4835
    gen_addr_imm_index(cpu_T[0], ctx, 0);
A
aurel32 已提交
4836 4837
    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]);
4838 4839 4840 4841 4842 4843 4844 4845 4846
    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 */
4847
    gen_update_nip(ctx, ctx->nip - 4);
4848
    gen_addr_imm_index(cpu_T[0], ctx, 0);
A
aurel32 已提交
4849 4850
    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]);
4851 4852
    op_POWER2_stfq();
    if (ra != 0)
A
aurel32 已提交
4853
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4854 4855 4856 4857 4858 4859 4860 4861
}

/* 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 */
4862
    gen_update_nip(ctx, ctx->nip - 4);
4863
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
4864 4865
    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]);
4866 4867
    op_POWER2_stfq();
    if (ra != 0)
A
aurel32 已提交
4868
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4869 4870 4871 4872 4873 4874
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4875
    gen_update_nip(ctx, ctx->nip - 4);
4876
    gen_addr_reg_index(cpu_T[0], ctx);
A
aurel32 已提交
4877 4878
    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]);
4879 4880 4881 4882
    op_POWER2_stfq();
}

/* BookE specific instructions */
4883
/* XXX: not implemented on 440 ? */
4884
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
4885 4886
{
    /* XXX: TODO */
4887
    GEN_EXCP_INVAL(ctx);
4888 4889
}

4890
/* XXX: not implemented on 440 ? */
4891
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
4892 4893
{
#if defined(CONFIG_USER_ONLY)
4894
    GEN_EXCP_PRIVOPC(ctx);
4895 4896
#else
    if (unlikely(!ctx->supervisor)) {
4897
        GEN_EXCP_PRIVOPC(ctx);
4898 4899
        return;
    }
4900
    gen_addr_reg_index(cpu_T[0], ctx);
4901
    /* Use the same micro-ops as for tlbie */
4902 4903 4904 4905 4906 4907
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4908 4909 4910 4911
#endif
}

/* All 405 MAC instructions are translated here */
4912 4913 4914
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
4915
{
A
aurel32 已提交
4916 4917
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[ra]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rb]);
4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967
    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 已提交
4968
        tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rt]);
4969
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
4970 4971 4972 4973 4974
        gen_op_405_add_T0_T2();
    }
    if (opc3 & 0x10) {
        /* Check overflow */
        if (opc3 & 0x01)
4975
            gen_op_check_addo();
4976 4977 4978 4979 4980 4981 4982 4983 4984 4985
        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 已提交
4986
    tcg_gen_mov_tl(cpu_gpr[rt], cpu_T[0]);
4987 4988 4989 4990 4991 4992
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
        gen_set_Rc0(ctx);
    }
}

4993 4994
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4995 4996 4997 4998 4999 5000
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5001
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5002
/* macchwo   - macchwo.   */
5003
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5004
/* macchws   - macchws.   */
5005
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5006
/* macchwso  - macchwso.  */
5007
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5008
/* macchwsu  - macchwsu.  */
5009
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5010
/* macchwsuo - macchwsuo. */
5011
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5012
/* macchwu   - macchwu.   */
5013
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5014
/* macchwuo  - macchwuo.  */
5015
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5016
/* machhw    - machhw.    */
5017
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5018
/* machhwo   - machhwo.   */
5019
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5020
/* machhws   - machhws.   */
5021
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5022
/* machhwso  - machhwso.  */
5023
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5024
/* machhwsu  - machhwsu.  */
5025
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5026
/* machhwsuo - machhwsuo. */
5027
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5028
/* machhwu   - machhwu.   */
5029
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5030
/* machhwuo  - machhwuo.  */
5031
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5032
/* maclhw    - maclhw.    */
5033
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5034
/* maclhwo   - maclhwo.   */
5035
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5036
/* maclhws   - maclhws.   */
5037
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5038
/* maclhwso  - maclhwso.  */
5039
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5040
/* maclhwu   - maclhwu.   */
5041
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5042
/* maclhwuo  - maclhwuo.  */
5043
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5044
/* maclhwsu  - maclhwsu.  */
5045
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5046
/* maclhwsuo - maclhwsuo. */
5047
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5048
/* nmacchw   - nmacchw.   */
5049
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5050
/* nmacchwo  - nmacchwo.  */
5051
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5052
/* nmacchws  - nmacchws.  */
5053
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5054
/* nmacchwso - nmacchwso. */
5055
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5056
/* nmachhw   - nmachhw.   */
5057
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5058
/* nmachhwo  - nmachhwo.  */
5059
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5060
/* nmachhws  - nmachhws.  */
5061
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5062
/* nmachhwso - nmachhwso. */
5063
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5064
/* nmaclhw   - nmaclhw.   */
5065
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5066
/* nmaclhwo  - nmaclhwo.  */
5067
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5068
/* nmaclhws  - nmaclhws.  */
5069
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5070
/* nmaclhwso - nmaclhwso. */
5071
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5072 5073

/* mulchw  - mulchw.  */
5074
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5075
/* mulchwu - mulchwu. */
5076
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5077
/* mulhhw  - mulhhw.  */
5078
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5079
/* mulhhwu - mulhhwu. */
5080
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5081
/* mullhw  - mullhw.  */
5082
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5083
/* mullhwu - mullhwu. */
5084
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5085 5086

/* mfdcr */
5087
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5088 5089
{
#if defined(CONFIG_USER_ONLY)
5090
    GEN_EXCP_PRIVREG(ctx);
5091 5092 5093 5094
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5095
        GEN_EXCP_PRIVREG(ctx);
5096 5097
        return;
    }
5098
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5099
    gen_op_load_dcr();
A
aurel32 已提交
5100
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5101 5102 5103 5104
#endif
}

/* mtdcr */
5105
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5106 5107
{
#if defined(CONFIG_USER_ONLY)
5108
    GEN_EXCP_PRIVREG(ctx);
5109 5110 5111 5112
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5113
        GEN_EXCP_PRIVREG(ctx);
5114 5115
        return;
    }
5116
    tcg_gen_movi_tl(cpu_T[0], dcrn);
A
aurel32 已提交
5117
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5118 5119 5120 5121 5122
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
5123
/* XXX: not implemented on 440 ? */
5124
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5125 5126
{
#if defined(CONFIG_USER_ONLY)
5127
    GEN_EXCP_PRIVREG(ctx);
5128 5129
#else
    if (unlikely(!ctx->supervisor)) {
5130
        GEN_EXCP_PRIVREG(ctx);
5131 5132
        return;
    }
A
aurel32 已提交
5133
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5134
    gen_op_load_dcr();
A
aurel32 已提交
5135
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5136
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5137 5138 5139 5140
#endif
}

/* mtdcrx */
5141
/* XXX: not implemented on 440 ? */
5142
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5143 5144
{
#if defined(CONFIG_USER_ONLY)
5145
    GEN_EXCP_PRIVREG(ctx);
5146 5147
#else
    if (unlikely(!ctx->supervisor)) {
5148
        GEN_EXCP_PRIVREG(ctx);
5149 5150
        return;
    }
A
aurel32 已提交
5151 5152
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5153
    gen_op_store_dcr();
5154
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5155 5156 5157
#endif
}

5158 5159 5160
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
A
aurel32 已提交
5161
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5162
    gen_op_load_dcr();
A
aurel32 已提交
5163
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5164 5165 5166 5167 5168 5169
    /* 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 已提交
5170 5171
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5172 5173 5174 5175
    gen_op_store_dcr();
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

5176 5177 5178 5179
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5180
    GEN_EXCP_PRIVOPC(ctx);
5181 5182
#else
    if (unlikely(!ctx->supervisor)) {
5183
        GEN_EXCP_PRIVOPC(ctx);
5184 5185 5186 5187 5188 5189 5190 5191 5192 5193
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5194
    GEN_EXCP_PRIVOPC(ctx);
5195
#else
A
aurel32 已提交
5196
    TCGv EA, val;
5197
    if (unlikely(!ctx->supervisor)) {
5198
        GEN_EXCP_PRIVOPC(ctx);
5199 5200
        return;
    }
A
aurel32 已提交
5201 5202 5203 5204 5205 5206 5207
    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);
5208 5209 5210 5211
#endif
}

/* icbt */
5212
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223
{
    /* 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)
5224
    GEN_EXCP_PRIVOPC(ctx);
5225 5226
#else
    if (unlikely(!ctx->supervisor)) {
5227
        GEN_EXCP_PRIVOPC(ctx);
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5238
    GEN_EXCP_PRIVOPC(ctx);
5239 5240
#else
    if (unlikely(!ctx->supervisor)) {
5241
        GEN_EXCP_PRIVOPC(ctx);
5242 5243 5244 5245 5246 5247 5248
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
5249
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5250 5251
{
#if defined(CONFIG_USER_ONLY)
5252
    GEN_EXCP_PRIVOPC(ctx);
5253 5254
#else
    if (unlikely(!ctx->supervisor)) {
5255
        GEN_EXCP_PRIVOPC(ctx);
5256 5257 5258 5259
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
5260
    GEN_SYNC(ctx);
5261 5262 5263 5264 5265 5266
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
5267
    GEN_EXCP_PRIVOPC(ctx);
5268 5269
#else
    if (unlikely(!ctx->supervisor)) {
5270
        GEN_EXCP_PRIVOPC(ctx);
5271 5272 5273 5274
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
5275
    GEN_SYNC(ctx);
5276 5277 5278 5279
#endif
}

/* BookE specific */
5280
/* XXX: not implemented on 440 ? */
5281
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5282 5283
{
#if defined(CONFIG_USER_ONLY)
5284
    GEN_EXCP_PRIVOPC(ctx);
5285 5286
#else
    if (unlikely(!ctx->supervisor)) {
5287
        GEN_EXCP_PRIVOPC(ctx);
5288 5289 5290
        return;
    }
    /* Restore CPU state */
5291
    gen_op_rfdi();
5292
    GEN_SYNC(ctx);
5293 5294 5295
#endif
}

5296
/* XXX: not implemented on 440 ? */
5297
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5298 5299
{
#if defined(CONFIG_USER_ONLY)
5300
    GEN_EXCP_PRIVOPC(ctx);
5301 5302
#else
    if (unlikely(!ctx->supervisor)) {
5303
        GEN_EXCP_PRIVOPC(ctx);
5304 5305 5306 5307
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
5308
    GEN_SYNC(ctx);
5309 5310
#endif
}
5311

5312
/* TLB management - PowerPC 405 implementation */
5313
/* tlbre */
5314
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5315 5316
{
#if defined(CONFIG_USER_ONLY)
5317
    GEN_EXCP_PRIVOPC(ctx);
5318 5319
#else
    if (unlikely(!ctx->supervisor)) {
5320
        GEN_EXCP_PRIVOPC(ctx);
5321 5322 5323 5324
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
A
aurel32 已提交
5325
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5326
        gen_op_4xx_tlbre_hi();
A
aurel32 已提交
5327
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5328 5329
        break;
    case 1:
A
aurel32 已提交
5330
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5331
        gen_op_4xx_tlbre_lo();
A
aurel32 已提交
5332
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5333 5334
        break;
    default:
5335
        GEN_EXCP_INVAL(ctx);
5336
        break;
5337
    }
5338 5339 5340
#endif
}

5341
/* tlbsx - tlbsx. */
5342
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5343 5344
{
#if defined(CONFIG_USER_ONLY)
5345
    GEN_EXCP_PRIVOPC(ctx);
5346 5347
#else
    if (unlikely(!ctx->supervisor)) {
5348
        GEN_EXCP_PRIVOPC(ctx);
5349 5350
        return;
    }
5351
    gen_addr_reg_index(cpu_T[0], ctx);
5352
    gen_op_4xx_tlbsx();
5353
    if (Rc(ctx->opcode))
5354
        gen_op_4xx_tlbsx_check();
A
aurel32 已提交
5355
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5356
#endif
B
bellard 已提交
5357 5358
}

5359
/* tlbwe */
5360
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5361
{
5362
#if defined(CONFIG_USER_ONLY)
5363
    GEN_EXCP_PRIVOPC(ctx);
5364 5365
#else
    if (unlikely(!ctx->supervisor)) {
5366
        GEN_EXCP_PRIVOPC(ctx);
5367 5368 5369 5370
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
A
aurel32 已提交
5371 5372
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5373 5374 5375
        gen_op_4xx_tlbwe_hi();
        break;
    case 1:
A
aurel32 已提交
5376 5377
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5378 5379 5380
        gen_op_4xx_tlbwe_lo();
        break;
    default:
5381
        GEN_EXCP_INVAL(ctx);
5382
        break;
5383
    }
5384 5385 5386
#endif
}

5387
/* TLB management - PowerPC 440 implementation */
5388
/* tlbre */
5389
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5390 5391
{
#if defined(CONFIG_USER_ONLY)
5392
    GEN_EXCP_PRIVOPC(ctx);
5393 5394
#else
    if (unlikely(!ctx->supervisor)) {
5395
        GEN_EXCP_PRIVOPC(ctx);
5396 5397 5398 5399 5400 5401
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
A
aurel32 已提交
5402
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5403
        gen_op_440_tlbre(rB(ctx->opcode));
A
aurel32 已提交
5404
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5405 5406
        break;
    default:
5407
        GEN_EXCP_INVAL(ctx);
5408 5409 5410 5411 5412 5413
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5414
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5415 5416
{
#if defined(CONFIG_USER_ONLY)
5417
    GEN_EXCP_PRIVOPC(ctx);
5418 5419
#else
    if (unlikely(!ctx->supervisor)) {
5420
        GEN_EXCP_PRIVOPC(ctx);
5421 5422
        return;
    }
5423
    gen_addr_reg_index(cpu_T[0], ctx);
5424
    gen_op_440_tlbsx();
5425
    if (Rc(ctx->opcode))
5426
        gen_op_4xx_tlbsx_check();
A
aurel32 已提交
5427
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5428 5429 5430 5431
#endif
}

/* tlbwe */
5432
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5433 5434
{
#if defined(CONFIG_USER_ONLY)
5435
    GEN_EXCP_PRIVOPC(ctx);
5436 5437
#else
    if (unlikely(!ctx->supervisor)) {
5438
        GEN_EXCP_PRIVOPC(ctx);
5439 5440 5441 5442 5443 5444
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
A
aurel32 已提交
5445 5446
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5447
        gen_op_440_tlbwe(rB(ctx->opcode));
5448 5449
        break;
    default:
5450
        GEN_EXCP_INVAL(ctx);
5451 5452 5453 5454 5455
        break;
    }
#endif
}

5456
/* wrtee */
5457
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5458 5459
{
#if defined(CONFIG_USER_ONLY)
5460
    GEN_EXCP_PRIVOPC(ctx);
5461 5462
#else
    if (unlikely(!ctx->supervisor)) {
5463
        GEN_EXCP_PRIVOPC(ctx);
5464 5465
        return;
    }
A
aurel32 已提交
5466
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5467
    gen_op_wrte();
J
j_mayer 已提交
5468 5469 5470
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5471
    GEN_STOP(ctx);
5472 5473 5474 5475
#endif
}

/* wrteei */
5476
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5477 5478
{
#if defined(CONFIG_USER_ONLY)
5479
    GEN_EXCP_PRIVOPC(ctx);
5480 5481
#else
    if (unlikely(!ctx->supervisor)) {
5482
        GEN_EXCP_PRIVOPC(ctx);
5483 5484
        return;
    }
5485
    tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5486
    gen_op_wrte();
J
j_mayer 已提交
5487 5488 5489
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5490
    GEN_STOP(ctx);
5491 5492 5493
#endif
}

J
j_mayer 已提交
5494
/* PowerPC 440 specific instructions */
5495 5496 5497
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
A
aurel32 已提交
5498 5499
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5500
    gen_op_440_dlmzb();
A
aurel32 已提交
5501
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
5502 5503 5504
    gen_op_store_xer_bc();
    if (Rc(ctx->opcode)) {
        gen_op_440_dlmzb_update_Rc();
A
aurel32 已提交
5505
        tcg_gen_andi_i32(cpu_crf[0], cpu_T[0], 0xf);
5506 5507 5508 5509 5510 5511 5512 5513 5514 5515
    }
}

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

/* msync replaces sync on 440 */
5516
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5517 5518 5519 5520 5521
{
    /* interpreted as no-op */
}

/* icbt */
5522
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5523 5524 5525 5526 5527
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
5528 5529
}

5530 5531 5532
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */

5533 5534 5535 5536 5537 5538 5539 5540 5541
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]);
}
5542 5543 5544

#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_VR_LD_TABLE(name)                                                  \
5545 5546
static GenOpFunc *gen_op_vr_l##name[NB_MEM_FUNCS] = {                         \
    GEN_MEM_FUNCS(vr_l##name),                                                \
5547 5548
};
#define OP_VR_ST_TABLE(name)                                                  \
5549 5550
static GenOpFunc *gen_op_vr_st##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(vr_st##name),                                               \
5551 5552 5553 5554 5555 5556 5557 5558 5559
};

#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;                                                               \
    }                                                                         \
5560
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5561
    op_vr_ldst(vr_l##name);                                                   \
5562
    gen_store_avr(rD(ctx->opcode), 0);                                        \
5563 5564 5565 5566 5567 5568 5569 5570 5571
}

#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;                                                               \
    }                                                                         \
5572
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5573
    gen_load_avr(0, rS(ctx->opcode));                                         \
5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588
    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);

5589 5590
/***                           SPE extension                               ***/
/* Register moves */
5591

A
aurel32 已提交
5592 5593 5594 5595
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 已提交
5596
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
5597
#endif
A
aurel32 已提交
5598
}
5599

A
aurel32 已提交
5600 5601 5602 5603 5604
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);
5605
    TCGv tmp = tcg_temp_new(TCG_TYPE_I64);
A
aurel32 已提交
5606 5607 5608
    tcg_gen_shri_i64(tmp, t, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
    tcg_temp_free(tmp);
5609
#endif
A
aurel32 已提交
5610
}
5611

5612 5613 5614 5615 5616 5617 5618 5619 5620 5621
#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 */
5622
static always_inline void gen_speundef (DisasContext *ctx)
5623
{
5624
    GEN_EXCP_INVAL(ctx);
5625 5626 5627
}

/* SPE load and stores */
5628
static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
5629 5630 5631
{
    target_long simm = rB(ctx->opcode);

5632 5633 5634 5635 5636 5637
    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)]);
5638 5639 5640 5641
}

#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_SPE_LD_TABLE(name)                                                 \
5642 5643
static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(spe_l##name),                                               \
5644 5645
};
#define OP_SPE_ST_TABLE(name)                                                 \
5646 5647
static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = {                       \
    GEN_MEM_FUNCS(spe_st##name),                                              \
5648
};
5649 5650

#define GEN_SPE_LD(name, sh)                                                  \
5651
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5652 5653
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5654
        GEN_EXCP_NO_AP(ctx);                                                  \
5655 5656
        return;                                                               \
    }                                                                         \
5657
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
5658
    op_spe_ldst(spe_l##name);                                                 \
A
aurel32 已提交
5659
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5660 5661 5662
}

#define GEN_SPE_LDX(name)                                                     \
5663
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
5664 5665
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5666
        GEN_EXCP_NO_AP(ctx);                                                  \
5667 5668
        return;                                                               \
    }                                                                         \
5669
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
5670
    op_spe_ldst(spe_l##name);                                                 \
A
aurel32 已提交
5671
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5672 5673 5674 5675 5676 5677 5678 5679
}

#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)                                                  \
5680
static always_inline void gen_evst##name (DisasContext *ctx)                  \
5681 5682
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5683
        GEN_EXCP_NO_AP(ctx);                                                  \
5684 5685
        return;                                                               \
    }                                                                         \
5686
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
A
aurel32 已提交
5687
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5688 5689 5690 5691
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPE_STX(name)                                                     \
5692
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
5693 5694
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5695
        GEN_EXCP_NO_AP(ctx);                                                  \
5696 5697
        return;                                                               \
    }                                                                         \
5698
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
A
aurel32 已提交
5699
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713
    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)                                                \
5714
static always_inline void gen_##name (DisasContext *ctx)                      \
5715 5716
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5717
        GEN_EXCP_NO_AP(ctx);                                                  \
5718 5719
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5720 5721
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5722
    gen_op_##name();                                                          \
A
aurel32 已提交
5723
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5724 5725
}

5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
#define GEN_SPEOP_TCG_ARITH2(name)                                            \
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));                                      \
    gen_op_##name(t0, t1);                                                    \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free(t0);                                                        \
    tcg_temp_free(t1);                                                        \
}

5743
#define GEN_SPEOP_ARITH1(name)                                                \
5744
static always_inline void gen_##name (DisasContext *ctx)                      \
5745 5746
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5747
        GEN_EXCP_NO_AP(ctx);                                                  \
5748 5749
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5750
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5751
    gen_op_##name();                                                          \
A
aurel32 已提交
5752
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5753 5754 5755
}

#define GEN_SPEOP_COMP(name)                                                  \
5756
static always_inline void gen_##name (DisasContext *ctx)                      \
5757 5758
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5759
        GEN_EXCP_NO_AP(ctx);                                                  \
5760 5761
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5762 5763
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5764
    gen_op_##name();                                                          \
A
aurel32 已提交
5765
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
5766 5767 5768
}

/* Logical */
5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821
static always_inline void gen_op_evand (TCGv t0, TCGv t1)
{
    tcg_gen_and_i64(t0, t0, t1);
}

static always_inline void gen_op_evandc (TCGv t0, TCGv t1)
{
    tcg_gen_not_i64(t1, t1);
    tcg_gen_and_i64(t0, t0, t1);
}

static always_inline void gen_op_evxor (TCGv t0, TCGv t1)
{
    tcg_gen_xor_i64(t0, t0, t1);
}

static always_inline void gen_op_evor (TCGv t0, TCGv t1)
{
    tcg_gen_or_i64(t0, t0, t1);
}

static always_inline void gen_op_evnor (TCGv t0, TCGv t1)
{
    tcg_gen_or_i64(t0, t0, t1);
    tcg_gen_not_i64(t0, t0);
}

static always_inline void gen_op_eveqv (TCGv t0, TCGv t1)
{
    tcg_gen_xor_i64(t0, t0, t1);
    tcg_gen_not_i64(t0, t0);
}

static always_inline void gen_op_evorc (TCGv t0, TCGv t1)
{
    tcg_gen_not_i64(t1, t1);
    tcg_gen_or_i64(t0, t0, t1);
}

static always_inline void gen_op_evnand (TCGv t0, TCGv t1)
{
    tcg_gen_and_i64(t0, t0, t1);
    tcg_gen_not_i64(t0, t0);
}

GEN_SPEOP_TCG_ARITH2(evand);
GEN_SPEOP_TCG_ARITH2(evandc);
GEN_SPEOP_TCG_ARITH2(evxor);
GEN_SPEOP_TCG_ARITH2(evor);
GEN_SPEOP_TCG_ARITH2(evnor);
GEN_SPEOP_TCG_ARITH2(eveqv);
GEN_SPEOP_TCG_ARITH2(evorc);
GEN_SPEOP_TCG_ARITH2(evnand);
5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840
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);
5841
static always_inline void gen_brinc (DisasContext *ctx)
5842 5843
{
    /* Note: brinc is usable even if SPE is disabled */
A
aurel32 已提交
5844 5845
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5846
    gen_op_brinc();
A
aurel32 已提交
5847
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5848 5849 5850
}

#define GEN_SPEOP_ARITH_IMM2(name)                                            \
5851
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5852 5853
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5854
        GEN_EXCP_NO_AP(ctx);                                                  \
5855 5856
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5857
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
5858 5859
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
A
aurel32 已提交
5860
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5861 5862 5863
}

#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
5864
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5865 5866
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5867
        GEN_EXCP_NO_AP(ctx);                                                  \
5868 5869
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
5870
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5871 5872
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
A
aurel32 已提交
5873
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886
}

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

5887
static always_inline void gen_evsplati (DisasContext *ctx)
5888 5889 5890 5891
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

    gen_op_splatwi_T0_64(imm);
A
aurel32 已提交
5892
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5893 5894
}

5895
static always_inline void gen_evsplatfi (DisasContext *ctx)
5896 5897 5898 5899
{
    uint32_t imm = rA(ctx->opcode) << 27;

    gen_op_splatwi_T0_64(imm);
A
aurel32 已提交
5900
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935
}

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

5936
static always_inline void gen_evsel (DisasContext *ctx)
5937 5938
{
    if (unlikely(!ctx->spe_enabled)) {
5939
        GEN_EXCP_NO_AP(ctx);
5940 5941
        return;
    }
A
aurel32 已提交
5942
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[ctx->opcode & 0x7]);
A
aurel32 已提交
5943 5944
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));
5945
    gen_op_evsel();
A
aurel32 已提交
5946
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5947 5948
}

5949
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
5950 5951 5952
{
    gen_evsel(ctx);
}
5953
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
5954 5955 5956
{
    gen_evsel(ctx);
}
5957
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
5958 5959 5960
{
    gen_evsel(ctx);
}
5961
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975
{
    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)                                             \
5976
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
5977 5978 5979 5980 5981
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
5982
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
5983 5984 5985 5986 5987 5988 5989 5990
{                                                                             \
    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);                                                 \
5991
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
5992 5993 5994 5995
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
5996
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009
{                                                                             \
    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);
6010 6011
GEN_OP_SPE_STWWE(kernel);
GEN_OP_SPE_STWWE(hypv);
6012 6013 6014 6015 6016
#endif /* defined(CONFIG_USER_ONLY) */
GEN_SPEOP_ST(wwe, 2);
GEN_SPEOP_ST(wwo, 2);

#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
6017
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
6018 6019 6020 6021 6022 6023
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
6024
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
6025 6026 6027 6028 6029 6030
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
6031
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061
{                                                                             \
    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);
6062 6063
GEN_OP_SPE_LHE(kernel);
GEN_OP_SPE_LHE(hypv);
6064
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6065 6066
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
6067
GEN_OP_SPE_LHE(le_user);
6068 6069
GEN_OP_SPE_LHE(le_kernel);
GEN_OP_SPE_LHE(le_hypv);
6070
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6071 6072
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
6073
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6074 6075
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
6076
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6077 6078
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
6079
GEN_OP_SPE_LHX(user);
6080 6081
GEN_OP_SPE_LHX(kernel);
GEN_OP_SPE_LHX(hypv);
6082
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6083 6084
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
6085
GEN_OP_SPE_LHX(le_user);
6086 6087
GEN_OP_SPE_LHX(le_kernel);
GEN_OP_SPE_LHX(le_hypv);
6088
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6089 6090
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
6091 6092
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_user);
6093 6094
GEN_OP_SPE_LHE(64_kernel);
GEN_OP_SPE_LHE(64_hypv);
6095
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6096 6097
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
6098
GEN_OP_SPE_LHE(le_64_user);
6099 6100
GEN_OP_SPE_LHE(le_64_kernel);
GEN_OP_SPE_LHE(le_64_hypv);
6101
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6102 6103
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
6104
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6105 6106
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
6107
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6108 6109
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
6110
GEN_OP_SPE_LHX(64_user);
6111 6112
GEN_OP_SPE_LHX(64_kernel);
GEN_OP_SPE_LHX(64_hypv);
6113
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6114 6115
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
6116
GEN_OP_SPE_LHX(le_64_user);
6117 6118
GEN_OP_SPE_LHX(le_64_kernel);
GEN_OP_SPE_LHX(le_64_hypv);
6119
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6120 6121
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226
#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)                                               \
6227
static always_inline void gen_##name (DisasContext *ctx)                      \
6228
{                                                                             \
A
aurel32 已提交
6229
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
6230
    gen_op_##name();                                                          \
A
aurel32 已提交
6231
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
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
}

/* 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 */
6308
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
6309 6310 6311 6312 6313 6314 6315 6316 6317
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); //
6318 6319
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
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
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 已提交
6375 6376 6377
/* End opcode list */
GEN_OPCODE_MARK(end);

6378
#include "translate_init.c"
6379
#include "helper_regs.h"
B
bellard 已提交
6380

6381
/*****************************************************************************/
6382
/* Misc PowerPC helpers */
6383 6384 6385
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
6386
{
6387 6388 6389
#define RGPL  4
#define RFPL  4

B
bellard 已提交
6390 6391
    int i;

J
j_mayer 已提交
6392 6393
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
                env->nip, env->lr, env->ctr, hreg_load_xer(env));
6394 6395
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
6396
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
6397
    cpu_fprintf(f, "TB %08x %08x "
6398 6399 6400 6401
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
6402
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6403 6404 6405 6406
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
6407
#endif
6408
    for (i = 0; i < 32; i++) {
6409 6410
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
6411
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
6412
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
6413
            cpu_fprintf(f, "\n");
6414
    }
6415
    cpu_fprintf(f, "CR ");
6416
    for (i = 0; i < 8; i++)
B
bellard 已提交
6417 6418
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
6419 6420 6421 6422 6423 6424 6425 6426
    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 已提交
6427
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6428
    }
6429
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
6430 6431 6432
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
6433
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6434
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
6435
            cpu_fprintf(f, "\n");
B
bellard 已提交
6436
    }
6437
#if !defined(CONFIG_USER_ONLY)
6438
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
6439
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6440
#endif
B
bellard 已提交
6441

6442 6443
#undef RGPL
#undef RFPL
B
bellard 已提交
6444 6445
}

6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492
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
}

6493
/*****************************************************************************/
6494 6495 6496
static always_inline void gen_intermediate_code_internal (CPUState *env,
                                                          TranslationBlock *tb,
                                                          int search_pc)
B
bellard 已提交
6497
{
6498
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
6499
    opc_handler_t **table, *handler;
B
bellard 已提交
6500
    target_ulong pc_start;
B
bellard 已提交
6501
    uint16_t *gen_opc_end;
6502
    int supervisor, little_endian;
B
bellard 已提交
6503
    int j, lj = -1;
P
pbrook 已提交
6504 6505
    int num_insns;
    int max_insns;
B
bellard 已提交
6506 6507 6508

    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
6509 6510 6511
#if defined(OPTIMIZE_FPRF_UPDATE)
    gen_fprf_ptr = gen_fprf_buf;
#endif
B
bellard 已提交
6512
    ctx.nip = pc_start;
B
bellard 已提交
6513
    ctx.tb = tb;
6514
    ctx.exception = POWERPC_EXCP_NONE;
6515
    ctx.spr_cb = env->spr_cb;
6516 6517
    supervisor = env->mmu_idx;
#if !defined(CONFIG_USER_ONLY)
6518
    ctx.supervisor = supervisor;
6519
#endif
6520
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
6521 6522
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
6523
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
6524
#else
6525
    ctx.mem_idx = (supervisor << 1) | little_endian;
6526
#endif
6527
    ctx.dcache_line_size = env->dcache_line_size;
B
bellard 已提交
6528
    ctx.fpu_enabled = msr_fp;
6529
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6530 6531 6532
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
6533 6534 6535 6536
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
6537
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
6538
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
6539
    else
6540
        ctx.singlestep_enabled = 0;
6541
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
6542 6543 6544
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
    if (unlikely(env->singlestep_enabled))
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
6545
#if defined (DO_SINGLE_STEP) && 0
6546 6547 6548
    /* Single step trace mode */
    msr_se = 1;
#endif
P
pbrook 已提交
6549 6550 6551 6552 6553 6554
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;

    gen_icount_start();
6555
    /* Set env in case of segfault during code fetch */
6556
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6557 6558
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6559
                if (env->breakpoints[j] == ctx.nip) {
6560
                    gen_update_nip(&ctx, ctx.nip);
6561 6562 6563 6564 6565
                    gen_op_debug();
                    break;
                }
            }
        }
6566
        if (unlikely(search_pc)) {
B
bellard 已提交
6567 6568 6569 6570 6571
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6572
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6573
                gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
6574
                gen_opc_icount[lj] = num_insns;
B
bellard 已提交
6575 6576
            }
        }
6577 6578
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6579
            fprintf(logfile, "----------------\n");
6580
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6581
                    ctx.nip, supervisor, (int)msr_ir);
6582 6583
        }
#endif
P
pbrook 已提交
6584 6585
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();
6586 6587 6588 6589
        if (unlikely(little_endian)) {
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
6590
        }
6591 6592
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6593
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6594
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6595
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
6596 6597
        }
#endif
B
bellard 已提交
6598
        ctx.nip += 4;
6599
        table = env->opcodes;
P
pbrook 已提交
6600
        num_insns++;
B
bellard 已提交
6601 6602 6603 6604 6605 6606 6607 6608 6609 6610
        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 ? */
6611
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6612
            if (loglevel != 0) {
6613
                fprintf(logfile, "invalid/unsupported opcode: "
6614
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
6615
                        opc1(ctx.opcode), opc2(ctx.opcode),
6616
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6617 6618
            } else {
                printf("invalid/unsupported opcode: "
6619
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
B
bellard 已提交
6620
                       opc1(ctx.opcode), opc2(ctx.opcode),
6621
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6622
            }
6623 6624
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6625
                if (loglevel != 0) {
B
bellard 已提交
6626
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6627
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
B
bellard 已提交
6628 6629
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6630
                            ctx.opcode, ctx.nip - 4);
6631 6632
                } else {
                    printf("invalid bits: %08x for opcode: "
6633
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
6634 6635
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6636
                           ctx.opcode, ctx.nip - 4);
6637
                }
6638
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6639
                break;
B
bellard 已提交
6640 6641
            }
        }
B
bellard 已提交
6642
        (*(handler->handler))(&ctx);
6643 6644 6645
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6646
        /* Check trace mode exceptions */
6647 6648 6649 6650 6651
        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)) {
6652
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6653
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
P
pbrook 已提交
6654 6655
                            (env->singlestep_enabled) ||
                            num_insns >= max_insns)) {
6656 6657 6658
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
6659
            break;
6660
        }
6661 6662 6663 6664
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
P
pbrook 已提交
6665 6666
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
6667
    if (ctx.exception == POWERPC_EXCP_NONE) {
6668
        gen_goto_tb(&ctx, 0, ctx.nip);
6669
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6670 6671 6672 6673
        if (unlikely(env->singlestep_enabled)) {
            gen_update_nip(&ctx, ctx.nip);
            gen_op_debug();
        }
6674
        /* Generate the return instruction */
B
bellard 已提交
6675
        tcg_gen_exit_tb(0);
6676
    }
P
pbrook 已提交
6677
    gen_icount_end(tb, num_insns);
B
bellard 已提交
6678
    *gen_opc_ptr = INDEX_op_end;
6679
    if (unlikely(search_pc)) {
6680 6681 6682 6683 6684
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6685
        tb->size = ctx.nip - pc_start;
P
pbrook 已提交
6686
        tb->icount = num_insns;
6687
    }
6688
#if defined(DEBUG_DISAS)
6689
    if (loglevel & CPU_LOG_TB_CPU) {
6690
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6691
        cpu_dump_state(env, logfile, fprintf, 0);
6692 6693
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6694
        int flags;
6695
        flags = env->bfd_mach;
6696
        flags |= little_endian << 16;
B
bellard 已提交
6697
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6698
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6699
        fprintf(logfile, "\n");
6700
    }
B
bellard 已提交
6701 6702 6703
#endif
}

6704
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6705
{
6706
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
6707 6708
}

6709
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6710
{
6711
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
6712
}
A
aurel32 已提交
6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754

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;
}