translate.c 292.7 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"
B
bellard 已提交
29
#include "tcg-op.h"
30
#include "qemu-common.h"
B
bellard 已提交
31

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

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

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

45 46
/*****************************************************************************/
/* Code translation helpers                                                  */
B
bellard 已提交
47

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

P
pbrook 已提交
73 74 75 76
#include "gen-icount.h"

void ppc_translate_init(void)
{
A
aurel32 已提交
77 78
    int i;
    char* p;
P
pbrook 已提交
79
    static int done_init = 0;
A
aurel32 已提交
80

P
pbrook 已提交
81 82
    if (done_init)
        return;
A
aurel32 已提交
83

P
pbrook 已提交
84 85
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");

A
aurel32 已提交
86
    p = cpu_reg_names;
A
aurel32 已提交
87 88 89

    for (i = 0; i < 8; i++) {
        sprintf(p, "crf%d", i);
P
pbrook 已提交
90 91
        cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
                                            offsetof(CPUState, crf[i]), p);
A
aurel32 已提交
92 93 94
        p += 5;
    }

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

A
aurel32 已提交
107
        sprintf(p, "fp%d", i);
P
pbrook 已提交
108 109
        cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
                                            offsetof(CPUState, fpr[i]), p);
A
aurel32 已提交
110
        p += (i < 10) ? 4 : 5;
A
aurel32 已提交
111

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

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

P
pbrook 已提交
133
    cpu_nip = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
134 135
                                 offsetof(CPUState, nip), "nip");

136 137 138
    cpu_msr = tcg_global_mem_new(TCG_AREG0,
                                 offsetof(CPUState, msr), "msr");

P
pbrook 已提交
139
    cpu_ctr = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
140 141
                                 offsetof(CPUState, ctr), "ctr");

P
pbrook 已提交
142
    cpu_lr = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
143 144
                                offsetof(CPUState, lr), "lr");

P
pbrook 已提交
145
    cpu_xer = tcg_global_mem_new(TCG_AREG0,
A
aurel32 已提交
146 147
                                 offsetof(CPUState, xer), "xer");

148 149 150
    cpu_reserve = tcg_global_mem_new(TCG_AREG0,
                                     offsetof(CPUState, reserve), "reserve");

P
pbrook 已提交
151 152
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
                                       offsetof(CPUState, fpscr), "fpscr");
153

A
aurel32 已提交
154 155 156
    cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
                                             offsetof(CPUState, access_type), "access_type");

A
aurel32 已提交
157
    /* register helpers */
P
pbrook 已提交
158
#define GEN_HELPER 2
A
aurel32 已提交
159 160
#include "helper.h"

P
pbrook 已提交
161 162 163
    done_init = 1;
}

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

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

200 201 202
static always_inline void gen_reset_fpstatus (void)
{
#ifdef CONFIG_SOFTFLOAT
203
    gen_helper_reset_fpstatus();
204 205 206
#endif
}

207
static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
208
{
209
    TCGv_i32 t0 = tcg_temp_new_i32();
A
aurel32 已提交
210

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

226
    tcg_temp_free_i32(t0);
227 228
}

A
aurel32 已提交
229
static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
A
aurel32 已提交
230
{
A
aurel32 已提交
231 232 233 234
    if (ctx->access_type != access_type) {
        tcg_gen_movi_i32(cpu_access_type, access_type);
        ctx->access_type = access_type;
    }
A
aurel32 已提交
235 236
}

237
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
238 239 240
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
A
aurel32 已提交
241
        tcg_gen_movi_tl(cpu_nip, nip);
242 243
    else
#endif
A
aurel32 已提交
244
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
245 246
}

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

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

A
aurel32 已提交
273 274 275 276 277 278 279 280
static always_inline void gen_debug_exception (DisasContext *ctx)
{
    TCGv_i32 t0;
    gen_update_nip(ctx, ctx->nip);
    t0 = tcg_const_i32(EXCP_DEBUG);
    gen_helper_raise_exception(t0);
    tcg_temp_free_i32(t0);
}
281

A
aurel32 已提交
282 283 284 285
static always_inline void gen_inval_exception (DisasContext *ctx, uint32_t error)
{
    gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
}
286

287
/* Stop translation */
A
aurel32 已提交
288
static always_inline void gen_stop_exception (DisasContext *ctx)
289
{
290
    gen_update_nip(ctx, ctx->nip);
291
    ctx->exception = POWERPC_EXCP_STOP;
292 293
}

294
/* No need to update nip here, as execution flow will change */
A
aurel32 已提交
295
static always_inline void gen_sync_exception (DisasContext *ctx)
296
{
297
    ctx->exception = POWERPC_EXCP_SYNC;
298 299
}

B
bellard 已提交
300 301 302 303 304
#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)

305 306 307 308 309
#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 已提交
310 311
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
T
ths 已提交
312
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
313 314 315 316
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
317
    opc_handler_t handler;
318
    const char *oname;
B
bellard 已提交
319 320
} opcode_t;

321
/*****************************************************************************/
B
bellard 已提交
322 323
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
324
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
325 326 327 328 329
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
330
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
331
{                                                                             \
332
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}

/* 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 */
360
EXTRACT_HELPER(_SPR, 11, 10);
361
static always_inline uint32_t SPR (uint32_t opcode)
362 363 364 365 366
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380
/***                              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 已提交
381 382
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
383 384 385 386

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

B
bellard 已提交
389 390 391 392
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
393
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
394 395 396 397
{
    return (opcode >> 0) & 0x03FFFFFC;
}

398
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
399 400 401 402 403 404 405 406 407 408 409 410
{
    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 */
411
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
412
{
413
    target_ulong ret;
B
bellard 已提交
414

415 416
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
417
        ret = UINT64_MAX << (63 - end);
418
    } else if (likely(end == 63)) {
419
        ret = UINT64_MAX >> start;
420 421 422
    }
#else
    if (likely(start == 0)) {
423
        ret = UINT32_MAX << (31  - end);
424
    } else if (likely(end == 31)) {
425
        ret = UINT32_MAX >> start;
426 427 428 429 430 431 432 433
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
434 435 436 437

    return ret;
}

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

    /* Fixed-point unit extensions                                           */
    /*   PowerPC 602 specific                                                */
476
    PPC_602_SPEC       = 0x0000000000000400ULL,
477 478 479 480 481 482
    /*   isel instruction                                                    */
    PPC_ISEL           = 0x0000000000000800ULL,
    /*   popcntb instruction                                                 */
    PPC_POPCNTB        = 0x0000000000001000ULL,
    /*   string load / store                                                 */
    PPC_STRING         = 0x0000000000002000ULL,
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

    /* 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                                          */
500
    PPC_SPE            = 0x0000000002000000ULL,
501
    /*   PowerPC 2.03 SPE floating-point extension                           */
502
    PPC_SPEFPU         = 0x0000000004000000ULL,
503

504
    /* Optional memory control instructions                                  */
505 506 507 508 509 510 511 512 513
    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                                            */
514
    PPC_CACHE          = 0x0000000200000000ULL,
515
    /*   icbi instruction                                                    */
516
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
517
    /*   dcbz instruction with fixed cache line size                         */
518
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
519
    /*   dcbz instruction with tunable cache line size                       */
520
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
521
    /*   dcba instruction                                                    */
522 523 524
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
    /*   Freescale cache locking instructions                                */
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
525 526 527

    /* MMU related extensions                                                */
    /*   external control instructions                                       */
528
    PPC_EXTERN         = 0x0000010000000000ULL,
529
    /*   segment register access instructions                                */
530
    PPC_SEGMENT        = 0x0000020000000000ULL,
531
    /*   PowerPC 6xx TLB management instructions                             */
532
    PPC_6xx_TLB        = 0x0000040000000000ULL,
533
    /* PowerPC 74xx TLB management instructions                              */
534
    PPC_74xx_TLB       = 0x0000080000000000ULL,
535
    /*   PowerPC 40x TLB management instructions                             */
536
    PPC_40x_TLB        = 0x0000100000000000ULL,
537
    /*   segment register access instructions for PowerPC 64 "bridge"        */
538
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
539
    /*   SLB management                                                      */
540
    PPC_SLBI           = 0x0000400000000000ULL,
541

542
    /* Embedded PowerPC dedicated instructions                               */
543
    PPC_WRTEE          = 0x0001000000000000ULL,
544
    /* PowerPC 40x exception model                                           */
545
    PPC_40x_EXCP       = 0x0002000000000000ULL,
546
    /* PowerPC 405 Mac instructions                                          */
547
    PPC_405_MAC        = 0x0004000000000000ULL,
548
    /* PowerPC 440 specific instructions                                     */
549
    PPC_440_SPEC       = 0x0008000000000000ULL,
550
    /* BookE (embedded) PowerPC specification                                */
551 552 553 554 555 556 557
    PPC_BOOKE          = 0x0010000000000000ULL,
    /* mfapidi instruction                                                   */
    PPC_MFAPIDI        = 0x0020000000000000ULL,
    /* tlbiva instruction                                                    */
    PPC_TLBIVA         = 0x0040000000000000ULL,
    /* tlbivax instruction                                                   */
    PPC_TLBIVAX        = 0x0080000000000000ULL,
558
    /* PowerPC 4xx dedicated instructions                                    */
559
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
560
    /* PowerPC 40x ibct instructions                                         */
561
    PPC_40x_ICBT       = 0x0200000000000000ULL,
562
    /* rfmci is not implemented in all BookE PowerPC                         */
563 564 565 566 567 568 569
    PPC_RFMCI          = 0x0400000000000000ULL,
    /* rfdi instruction                                                      */
    PPC_RFDI           = 0x0800000000000000ULL,
    /* DCR accesses                                                          */
    PPC_DCR            = 0x1000000000000000ULL,
    /* DCR extended accesse                                                  */
    PPC_DCRX           = 0x2000000000000000ULL,
570
    /* user-mode DCR access, implemented in PowerPC 460                      */
571
    PPC_DCRUX          = 0x4000000000000000ULL,
572 573 574 575
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
576 577 578 579 580
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
581
#if defined(__APPLE__)
582
#define OPCODES_SECTION                                                       \
583
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
584
#else
585
#define OPCODES_SECTION                                                       \
586
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
587 588
#endif

589
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
590
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
591
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
592 593 594
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
595
    .pad  = { 0, },                                                           \
B
bellard 已提交
596 597
    .handler = {                                                              \
        .inval   = invl,                                                      \
598
        .type = _typ,                                                         \
B
bellard 已提交
599
        .handler = &gen_##name,                                               \
600
        .oname = stringify(name),                                             \
B
bellard 已提交
601
    },                                                                        \
602
    .oname = stringify(name),                                                 \
B
bellard 已提交
603
}
604 605 606 607 608 609 610 611 612 613 614 615 616 617
#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,                                                            \
}
618 619 620 621 622 623 624 625 626 627 628 629 630 631
#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),                                                 \
}
632 633 634 635 636 637 638 639 640 641 642 643 644
#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,                                                            \
}
645
#endif
B
bellard 已提交
646 647

#define GEN_OPCODE_MARK(name)                                                 \
648
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
649 650 651
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
652
    .pad  = { 0, },                                                           \
B
bellard 已提交
653 654
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
655
        .type = 0x00,                                                         \
B
bellard 已提交
656 657
        .handler = NULL,                                                      \
    },                                                                        \
658
    .oname = stringify(name),                                                 \
B
bellard 已提交
659 660
}

661 662 663 664 665 666 667 668 669 670 671
/* SPR load/store helpers */
static always_inline void gen_load_spr(TCGv t, int reg)
{
    tcg_gen_ld_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
}

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

B
bellard 已提交
672 673 674 675
/* Start opcode list */
GEN_OPCODE_MARK(start);

/* Invalid instruction */
676 677
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
A
aurel32 已提交
678
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
679 680
}

B
bellard 已提交
681 682
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
683
    .type    = PPC_NONE,
B
bellard 已提交
684 685 686
    .handler = gen_invalid,
};

687 688
/***                           Integer comparison                          ***/

689
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
690 691 692
{
    int l1, l2, l3;

693 694
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
695 696 697 698 699 700
    tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);

    l1 = gen_new_label();
    l2 = gen_new_label();
    l3 = gen_new_label();
    if (s) {
701 702
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
703
    } else {
704 705
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
706 707 708 709 710 711 712 713 714 715 716
    }
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
    tcg_gen_br(l3);
    gen_set_label(l1);
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
    tcg_gen_br(l3);
    gen_set_label(l2);
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
    gen_set_label(l3);
}

717
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
718
{
719 720 721
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp(arg0, t0, s, crf);
    tcg_temp_free(t0);
722 723 724
}

#if defined(TARGET_PPC64)
725
static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
726
{
727
    TCGv t0, t1;
P
pbrook 已提交
728 729
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
730
    if (s) {
731 732
        tcg_gen_ext32s_tl(t0, arg0);
        tcg_gen_ext32s_tl(t1, arg1);
733
    } else {
734 735
        tcg_gen_ext32u_tl(t0, arg0);
        tcg_gen_ext32u_tl(t1, arg1);
736
    }
737 738 739
    gen_op_cmp(t0, t1, s, crf);
    tcg_temp_free(t1);
    tcg_temp_free(t0);
740 741
}

742
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
743
{
744 745 746
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp32(arg0, t0, s, crf);
    tcg_temp_free(t0);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}
#endif

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

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

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

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

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

/* isel (PowerPC 2.03 specification) */
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
{
    int l1, l2;
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;
P
pbrook 已提交
818
    TCGv_i32 t0;
819 820 821 822 823

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

    mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
824
    t0 = tcg_temp_new_i32();
825 826
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
827 828 829 830 831 832 833 834
    if (rA(ctx->opcode) == 0)
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    else
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    gen_set_label(l2);
P
pbrook 已提交
835
    tcg_temp_free_i32(t0);
836 837
}

B
bellard 已提交
838 839
/***                           Integer arithmetic                          ***/

840 841 842 843
static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
{
    int l1;
    TCGv t0;
B
bellard 已提交
844

845 846 847
    l1 = gen_new_label();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
P
pbrook 已提交
848
    t0 = tcg_temp_local_new();
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    tcg_gen_xor_tl(t0, arg0, arg1);
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        tcg_gen_ext32s_tl(t0, t0);
#endif
    if (sub)
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
    else
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
    tcg_gen_xor_tl(t0, arg1, arg2);
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        tcg_gen_ext32s_tl(t0, t0);
#endif
    if (sub)
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
    else
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    gen_set_label(l1);
    tcg_temp_free(t0);
B
bellard 已提交
870 871
}

872 873 874
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
{
    int l1 = gen_new_label();
875 876

#if defined(TARGET_PPC64)
877 878
    if (!(ctx->sf_mode)) {
        TCGv t0, t1;
P
pbrook 已提交
879 880
        t0 = tcg_temp_new();
        t1 = tcg_temp_new();
881

882 883 884 885
        tcg_gen_ext32u_tl(t0, arg1);
        tcg_gen_ext32u_tl(t1, arg2);
        if (sub) {
            tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
A
aurel32 已提交
886
        } else {
887 888
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
        }
889 890 891 892
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
        gen_set_label(l1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
893 894
    } else
#endif
895 896 897 898 899 900 901 902
    {
        if (sub) {
            tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
        } else {
            tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
        }
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
        gen_set_label(l1);
903
    }
904 905
}

906 907 908 909 910
/* Common add function */
static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                           int add_ca, int compute_ca, int compute_ov)
{
    TCGv t0, t1;
911

912
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
913
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
914 915
        t0 = ret;
    } else {
P
pbrook 已提交
916
        t0 = tcg_temp_local_new();
917
    }
B
bellard 已提交
918

919
    if (add_ca) {
P
pbrook 已提交
920
        t1 = tcg_temp_local_new();
921 922 923
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
    }
B
bellard 已提交
924

925 926 927 928 929 930 931 932 933 934
    if (compute_ca && compute_ov) {
        /* Start with XER CA and OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
    } else if (compute_ca) {
        /* Start with XER CA disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    } else if (compute_ov) {
        /* Start with XER OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
B
bellard 已提交
935

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
    tcg_gen_add_tl(t0, arg1, arg2);

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

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

P
pbrook 已提交
953
    if (!TCGV_EQUAL(t0, ret)) {
954 955 956
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
    }
A
aurel32 已提交
957
}
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
/* Add functions with two operands */
#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER)                  \
{                                                                             \
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
                     add_ca, compute_ca, compute_ov);                         \
}
/* Add functions with one operand and one immediate */
#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
                                add_ca, compute_ca, compute_ov)               \
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER)                  \
{                                                                             \
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
                     cpu_gpr[rA(ctx->opcode)], t0,                            \
                     add_ca, compute_ca, compute_ov);                         \
    tcg_temp_free(t0);                                                        \
}

/* add  add.  addo  addo. */
GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
/* addc  addc.  addco  addco. */
GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
/* adde  adde.  addeo  addeo. */
GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
/* addme  addme.  addmeo  addmeo.  */
GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
/* addze  addze.  addzeo  addzeo.*/
GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
995
{
996 997 998 999 1000 1001 1002 1003
    target_long simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        /* li case */
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
    } else {
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
    }
1004
}
1005 1006 1007
/* addic  addic.*/
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
                                        int compute_Rc0)
1008
{
1009 1010 1011 1012 1013 1014
    target_long simm = SIMM(ctx->opcode);

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

    if (likely(simm != 0)) {
P
pbrook 已提交
1015
        TCGv t0 = tcg_temp_local_new();
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        tcg_gen_addi_tl(t0, arg1, simm);
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
    } else {
        tcg_gen_mov_tl(ret, arg1);
    }
    if (compute_Rc0) {
        gen_set_Rc0(ctx, ret);
    }
1026
}
1027
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1028
{
1029
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1030
}
1031
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1032
{
1033
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1034
}
1035 1036
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1037
{
1038 1039 1040 1041 1042 1043 1044 1045
    target_long simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        /* lis case */
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
    } else {
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
    }
1046
}
1047 1048 1049

static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                             int sign, int compute_ov)
1050
{
1051 1052
    int l1 = gen_new_label();
    int l2 = gen_new_label();
P
pbrook 已提交
1053 1054
    TCGv_i32 t0 = tcg_temp_local_new_i32();
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1055

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

    tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
    if (sign) {
1111
        int l3 = gen_new_label();
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
        tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
        gen_set_label(l3);
        tcg_gen_div_i64(ret, arg1, arg2);
    } else {
        tcg_gen_divu_i64(ret, arg1, arg2);
    }
    if (compute_ov) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
    if (sign) {
        tcg_gen_sari_i64(ret, arg1, 63);
    } else {
        tcg_gen_movi_i64(ret, 0);
    }
    if (compute_ov) {
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    }
    gen_set_label(l2);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
1135
}
1136 1137 1138
#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
{                                                                             \
1139 1140 1141
    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
                      sign, compute_ov);                                      \
1142 1143 1144 1145 1146 1147 1148
}
/* divwu  divwu.  divwuo  divwuo.   */
GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
/* divw  divw.  divwo  divwo.   */
GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1149
#endif
1150 1151 1152

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

P
pbrook 已提交
1156 1157
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
#if defined(TARGET_PPC64)
    tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
#else
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
P
pbrook 已提交
1170 1171
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1172 1173
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1174
}
1175 1176
/* mulhwu  mulhwu.  */
GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1177
{
P
pbrook 已提交
1178
    TCGv_i64 t0, t1;
1179

P
pbrook 已提交
1180 1181
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1182
#if defined(TARGET_PPC64)
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
#else
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
P
pbrook 已提交
1194 1195
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1196 1197
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1198
}
1199 1200
/* mullw  mullw. */
GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1201
{
1202 1203
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                   cpu_gpr[rB(ctx->opcode)]);
1204
    tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1205 1206
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1207
}
1208 1209
/* mullwo  mullwo. */
GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1210
{
1211
    int l1;
P
pbrook 已提交
1212
    TCGv_i64 t0, t1;
1213

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

/* neg neg. nego nego. */
A
aurel32 已提交
1274
static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1275
{
A
aurel32 已提交
1276 1277
    int l1 = gen_new_label();
    int l2 = gen_new_label();
P
pbrook 已提交
1278
    TCGv t0 = tcg_temp_local_new();
1279
#if defined(TARGET_PPC64)
1280
    if (ctx->sf_mode) {
A
aurel32 已提交
1281
        tcg_gen_mov_tl(t0, arg1);
A
aurel32 已提交
1282 1283 1284 1285 1286
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
    } else
#endif
    {
        tcg_gen_ext32s_tl(t0, arg1);
1287 1288 1289 1290 1291 1292 1293 1294
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
    }
    tcg_gen_neg_tl(ret, arg1);
    if (ov_check) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
A
aurel32 已提交
1295
    tcg_gen_mov_tl(ret, t0);
1296 1297 1298 1299
    if (ov_check) {
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    }
    gen_set_label(l2);
A
aurel32 已提交
1300
    tcg_temp_free(t0);
1301 1302 1303 1304
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
}
GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1305
{
A
aurel32 已提交
1306
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1307
}
1308
GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
B
bellard 已提交
1309
{
A
aurel32 已提交
1310
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
B
bellard 已提交
1311
}
1312 1313 1314 1315

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

1319
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
1320
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1321
        t0 = ret;
J
j_mayer 已提交
1322
    } else {
P
pbrook 已提交
1323
        t0 = tcg_temp_local_new();
1324
    }
1325

1326
    if (add_ca) {
P
pbrook 已提交
1327
        t1 = tcg_temp_local_new();
1328 1329
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
1330
    }
B
bellard 已提交
1331

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    if (compute_ca && compute_ov) {
        /* Start with XER CA and OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
    } else if (compute_ca) {
        /* Start with XER CA disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    } else if (compute_ov) {
        /* Start with XER OV disabled, the most likely case */
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }

    if (add_ca) {
        tcg_gen_not_tl(t0, arg1);
        tcg_gen_add_tl(t0, t0, arg2);
        gen_op_arith_compute_ca(ctx, t0, arg2, 0);
        tcg_gen_add_tl(t0, t0, t1);
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
        tcg_temp_free(t1);
B
bellard 已提交
1350
    } else {
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        tcg_gen_sub_tl(t0, arg2, arg1);
        if (compute_ca) {
            gen_op_arith_compute_ca(ctx, t0, arg2, 1);
        }
    }
    if (compute_ov) {
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
    }

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

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

/***                            Integer logical                            ***/
1417 1418
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
B
bellard 已提交
1419
{                                                                             \
1420 1421
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
       cpu_gpr[rB(ctx->opcode)]);                                             \
1422
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1423
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1424 1425
}

1426
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1427
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1428
{                                                                             \
1429
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1430
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1431
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
B
bellard 已提交
1432 1433 1434
}

/* and & and. */
1435
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
B
bellard 已提交
1436
/* andc & andc. */
1437
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
B
bellard 已提交
1438
/* andi. */
1439
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1440
{
1441 1442
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1443 1444
}
/* andis. */
1445
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1446
{
1447 1448
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1449 1450
}
/* cntlzw */
1451 1452
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
{
P
pbrook 已提交
1453
    gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1454
    if (unlikely(Rc(ctx->opcode) != 0))
P
pbrook 已提交
1455
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1456
}
B
bellard 已提交
1457
/* eqv & eqv. */
1458
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
B
bellard 已提交
1459
/* extsb & extsb. */
1460
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
B
bellard 已提交
1461
/* extsh & extsh. */
1462
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
B
bellard 已提交
1463
/* nand & nand. */
1464
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
B
bellard 已提交
1465
/* nor & nor. */
1466
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
B
bellard 已提交
1467
/* or & or. */
1468 1469
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1470 1471 1472 1473 1474 1475 1476
    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) {
1477 1478 1479 1480
        if (rs != rb)
            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
        else
            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1481
        if (unlikely(Rc(ctx->opcode) != 0))
1482
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1483
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1484
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1485 1486
#if defined(TARGET_PPC64)
    } else {
1487 1488
        int prio = 0;

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

1561 1562
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1563
        /* XXX: should handle special NOPs for POWER series */
1564
        return;
1565
    }
1566
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1567 1568 1569 1570
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1571
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1572

1573 1574 1575
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1576
    }
1577
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1578 1579 1580 1581
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1582
    target_ulong uimm = UIMM(ctx->opcode);
1583 1584 1585 1586 1587

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1588
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
B
bellard 已提交
1589 1590 1591 1592
}
/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1593
    target_ulong uimm = UIMM(ctx->opcode);
1594 1595 1596 1597 1598

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
1599
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
B
bellard 已提交
1600
}
1601
/* popcntb : PowerPC 2.03 specification */
1602
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1603 1604 1605
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
P
pbrook 已提交
1606
        gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1607 1608
    else
#endif
P
pbrook 已提交
1609
        gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1610 1611 1612 1613
}

#if defined(TARGET_PPC64)
/* extsw & extsw. */
1614
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1615
/* cntlzd */
1616 1617
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
{
P
pbrook 已提交
1618
    gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1619 1620 1621
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1622 1623
#endif

B
bellard 已提交
1624 1625 1626 1627
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1628
    uint32_t mb, me, sh;
B
bellard 已提交
1629 1630 1631

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1632
    sh = SH(ctx->opcode);
1633 1634 1635 1636
    if (likely(sh == 0 && mb == 0 && me == 31)) {
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    } else {
        target_ulong mask;
P
pbrook 已提交
1637 1638
        TCGv t1;
        TCGv t0 = tcg_temp_new();
1639
#if defined(TARGET_PPC64)
P
pbrook 已提交
1640 1641 1642 1643 1644
        TCGv_i32 t2 = tcg_temp_new_i32();
        tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_rotli_i32(t2, t2, sh);
        tcg_gen_extu_i32_i64(t0, t2);
        tcg_temp_free_i32(t2);
1645 1646 1647
#else
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
#endif
1648
#if defined(TARGET_PPC64)
1649 1650
        mb += 32;
        me += 32;
1651
#endif
1652
        mask = MASK(mb, me);
P
pbrook 已提交
1653
        t1 = tcg_temp_new();
1654 1655 1656 1657 1658 1659
        tcg_gen_andi_tl(t0, t0, mask);
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
    }
1660
    if (unlikely(Rc(ctx->opcode) != 0))
1661
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1662 1663 1664 1665 1666
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1667

B
bellard 已提交
1668 1669 1670
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1671 1672 1673 1674 1675

    if (likely(mb == 0 && me == (31 - sh))) {
        if (likely(sh == 0)) {
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
        } else {
P
pbrook 已提交
1676
            TCGv t0 = tcg_temp_new();
1677 1678 1679 1680
            tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_shli_tl(t0, t0, sh);
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
            tcg_temp_free(t0);
B
bellard 已提交
1681
        }
1682
    } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
P
pbrook 已提交
1683
        TCGv t0 = tcg_temp_new();
1684 1685 1686 1687 1688
        tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_shri_tl(t0, t0, mb);
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
        tcg_temp_free(t0);
    } else {
P
pbrook 已提交
1689
        TCGv t0 = tcg_temp_new();
1690
#if defined(TARGET_PPC64)
P
pbrook 已提交
1691
        TCGv_i32 t1 = tcg_temp_new_i32();
1692 1693 1694
        tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_rotli_i32(t1, t1, sh);
        tcg_gen_extu_i32_i64(t0, t1);
P
pbrook 已提交
1695
        tcg_temp_free_i32(t1);
1696 1697 1698
#else
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
#endif
1699
#if defined(TARGET_PPC64)
1700 1701
        mb += 32;
        me += 32;
1702
#endif
1703 1704 1705
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
        tcg_temp_free(t0);
    }
1706
    if (unlikely(Rc(ctx->opcode) != 0))
1707
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1708 1709 1710 1711 1712
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;
1713 1714
    TCGv t0;
#if defined(TARGET_PPC64)
P
pbrook 已提交
1715
    TCGv_i32 t1, t2;
1716
#endif
B
bellard 已提交
1717 1718 1719

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
P
pbrook 已提交
1720
    t0 = tcg_temp_new();
1721
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1722
#if defined(TARGET_PPC64)
P
pbrook 已提交
1723 1724
    t1 = tcg_temp_new_i32();
    t2 = tcg_temp_new_i32();
1725 1726 1727 1728
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_trunc_i64_i32(t2, t0);
    tcg_gen_rotl_i32(t1, t1, t2);
    tcg_gen_extu_i32_i64(t0, t1);
P
pbrook 已提交
1729 1730
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
1731 1732 1733
#else
    tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
#endif
1734 1735 1736 1737 1738
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
1739
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1740
    } else {
1741
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
B
bellard 已提交
1742
    }
1743
    tcg_temp_free(t0);
1744
    if (unlikely(Rc(ctx->opcode) != 0))
1745
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1746 1747
}

1748 1749
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1750
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1751 1752 1753
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1754 1755
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1756 1757 1758 1759
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1760
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1761 1762 1763
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1764 1765
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1766 1767 1768
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1769 1770
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1771 1772 1773
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1774 1775
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1776 1777 1778
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1779

1780 1781
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1782
{
1783 1784 1785 1786 1787
    if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
        tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
    } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
        tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
    } else {
P
pbrook 已提交
1788
        TCGv t0 = tcg_temp_new();
1789
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1790
        if (likely(mb == 0 && me == 63)) {
1791
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1792 1793
        } else {
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
J
j_mayer 已提交
1794
        }
1795
        tcg_temp_free(t0);
J
j_mayer 已提交
1796 1797
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1798
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
J
j_mayer 已提交
1799
}
1800
/* rldicl - rldicl. */
1801
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1802
{
J
j_mayer 已提交
1803
    uint32_t sh, mb;
1804

J
j_mayer 已提交
1805 1806
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1807
    gen_rldinm(ctx, mb, 63, sh);
1808
}
J
j_mayer 已提交
1809
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1810
/* rldicr - rldicr. */
1811
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1812
{
J
j_mayer 已提交
1813
    uint32_t sh, me;
1814

J
j_mayer 已提交
1815 1816
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1817
    gen_rldinm(ctx, 0, me, sh);
1818
}
J
j_mayer 已提交
1819
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1820
/* rldic - rldic. */
1821
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1822
{
J
j_mayer 已提交
1823
    uint32_t sh, mb;
1824

J
j_mayer 已提交
1825 1826
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1827 1828 1829 1830
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1831 1832
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1833
{
1834
    TCGv t0;
1835 1836 1837

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
P
pbrook 已提交
1838
    t0 = tcg_temp_new();
1839
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1840
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
J
j_mayer 已提交
1841
    if (unlikely(mb != 0 || me != 63)) {
1842 1843 1844 1845 1846
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
    } else {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    }
    tcg_temp_free(t0);
J
j_mayer 已提交
1847
    if (unlikely(Rc(ctx->opcode) != 0))
1848
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1849
}
J
j_mayer 已提交
1850

1851
/* rldcl - rldcl. */
1852
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1853
{
J
j_mayer 已提交
1854
    uint32_t mb;
1855

J
j_mayer 已提交
1856
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1857
    gen_rldnm(ctx, mb, 63);
1858
}
1859
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1860
/* rldcr - rldcr. */
1861
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1862
{
J
j_mayer 已提交
1863
    uint32_t me;
1864

J
j_mayer 已提交
1865
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1866
    gen_rldnm(ctx, 0, me);
1867
}
1868
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1869
/* rldimi - rldimi. */
1870
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1871
{
1872
    uint32_t sh, mb, me;
1873

J
j_mayer 已提交
1874 1875
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1876
    me = 63 - sh;
1877 1878 1879 1880 1881 1882
    if (unlikely(sh == 0 && mb == 0)) {
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
    } else {
        TCGv t0, t1;
        target_ulong mask;

P
pbrook 已提交
1883
        t0 = tcg_temp_new();
1884
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
P
pbrook 已提交
1885
        t1 = tcg_temp_new();
1886 1887 1888 1889 1890 1891
        mask = MASK(mb, me);
        tcg_gen_andi_tl(t0, t0, mask);
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
J
j_mayer 已提交
1892 1893
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1894
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1895
}
1896
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1897 1898
#endif

B
bellard 已提交
1899 1900
/***                             Integer shift                             ***/
/* slw & slw. */
1901 1902
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
{
1903
    TCGv t0;
1904 1905 1906 1907
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

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

P
pbrook 已提交
1966
    t0 = tcg_temp_local_new();
A
aurel32 已提交
1967 1968
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1969 1970 1971
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
P
pbrook 已提交
1972
    t1 = tcg_temp_new();
1973 1974 1975
    tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
    tcg_temp_free(t1);
1976
    gen_set_label(l2);
1977
    tcg_temp_free(t0);
1978 1979 1980
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
1981 1982
#if defined(TARGET_PPC64)
/* sld & sld. */
1983 1984
GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
{
1985
    TCGv t0;
1986 1987 1988 1989
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

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

P
pbrook 已提交
2053
    t0 = tcg_temp_local_new();
A
aurel32 已提交
2054 2055
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2056 2057 2058
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    tcg_gen_br(l2);
    gen_set_label(l1);
2059
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2060
    gen_set_label(l2);
2061
    tcg_temp_free(t0);
2062 2063 2064
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
}
2065
#endif
B
bellard 已提交
2066 2067

/***                       Floating-Point arithmetic                       ***/
2068
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2069
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2070
{                                                                             \
2071
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2072
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2073 2074
        return;                                                               \
    }                                                                         \
2075 2076
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2077
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2078 2079
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2080
    if (isfloat) {                                                            \
A
aurel32 已提交
2081
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2082
    }                                                                         \
A
aurel32 已提交
2083 2084
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
                     Rc(ctx->opcode) != 0);                                   \
2085 2086
}

2087 2088 2089
#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);
2090

2091 2092
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2093
{                                                                             \
2094
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2095
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2096 2097
        return;                                                               \
    }                                                                         \
2098 2099
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2100
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2101 2102
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rB(ctx->opcode)]);                               \
2103
    if (isfloat) {                                                            \
A
aurel32 已提交
2104
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2105
    }                                                                         \
A
aurel32 已提交
2106 2107
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2108
}
2109 2110 2111
#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);
2112

2113 2114
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2115
{                                                                             \
2116
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2117
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2118 2119
        return;                                                               \
    }                                                                         \
2120 2121
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2122
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2123 2124
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                       cpu_fpr[rC(ctx->opcode)]);                             \
2125
    if (isfloat) {                                                            \
A
aurel32 已提交
2126
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2127
    }                                                                         \
A
aurel32 已提交
2128 2129
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2130
}
2131 2132 2133
#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);
2134

2135
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2136
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2137
{                                                                             \
2138
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2139
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2140 2141
        return;                                                               \
    }                                                                         \
2142 2143
    /* NIP cannot be restored if the memory exception comes from an helper */ \
    gen_update_nip(ctx, ctx->nip - 4);                                        \
2144
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2145 2146 2147
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
2148 2149
}

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

2165
/* fadd - fadds */
2166
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2167
/* fdiv - fdivs */
2168
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2169
/* fmul - fmuls */
2170
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
2171

2172
/* fre */
2173
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2174

2175
/* fres */
2176
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
2177

2178
/* frsqrte */
2179 2180 2181
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);

/* frsqrtes */
A
aurel32 已提交
2182
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2183
{
A
aurel32 已提交
2184
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2185
        gen_exception(ctx, POWERPC_EXCP_FPU);
A
aurel32 已提交
2186 2187
        return;
    }
2188 2189
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
2190 2191 2192 2193
    gen_reset_fpstatus();
    gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2194
}
B
bellard 已提交
2195

2196
/* fsel */
2197
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2198
/* fsub - fsubs */
2199
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
2200 2201
/* Optional: */
/* fsqrt */
2202
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2203
{
2204
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2205
        gen_exception(ctx, POWERPC_EXCP_FPU);
2206 2207
        return;
    }
2208 2209
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2210
    gen_reset_fpstatus();
A
aurel32 已提交
2211 2212
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2213
}
B
bellard 已提交
2214

2215
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
2216
{
2217
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2218
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2219 2220
        return;
    }
2221 2222
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2223
    gen_reset_fpstatus();
A
aurel32 已提交
2224 2225 2226
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
B
bellard 已提交
2227 2228 2229
}

/***                     Floating-Point multiply-and-add                   ***/
2230
/* fmadd - fmadds */
2231
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2232
/* fmsub - fmsubs */
2233
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2234
/* fnmadd - fnmadds */
2235
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2236
/* fnmsub - fnmsubs */
2237
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
B
bellard 已提交
2238 2239 2240

/***                     Floating-Point round & convert                    ***/
/* fctiw */
2241
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2242
/* fctiwz */
2243
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
2244
/* frsp */
2245
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
J
j_mayer 已提交
2246 2247
#if defined(TARGET_PPC64)
/* fcfid */
2248
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
J
j_mayer 已提交
2249
/* fctid */
2250
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2251
/* fctidz */
2252
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
J
j_mayer 已提交
2253
#endif
B
bellard 已提交
2254

2255
/* frin */
2256
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2257
/* friz */
2258
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2259
/* frip */
2260
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2261
/* frim */
2262
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2263

B
bellard 已提交
2264 2265
/***                         Floating-Point compare                        ***/
/* fcmpo */
2266
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2267
{
A
aurel32 已提交
2268
    TCGv_i32 crf;
2269
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2270
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2271 2272
        return;
    }
2273 2274
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2275
    gen_reset_fpstatus();
A
aurel32 已提交
2276 2277
    crf = tcg_const_i32(crfD(ctx->opcode));
    gen_helper_fcmpo(cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)], crf);
A
aurel32 已提交
2278
    tcg_temp_free_i32(crf);
A
aurel32 已提交
2279
    gen_helper_float_check_status();
B
bellard 已提交
2280 2281 2282
}

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

2299 2300
/***                         Floating-point move                           ***/
/* fabs */
2301 2302
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2303 2304

/* fmr  - fmr. */
2305
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2306 2307
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
2308
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2309
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2310 2311
        return;
    }
A
aurel32 已提交
2312 2313
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2314 2315 2316
}

/* fnabs */
2317 2318
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2319
/* fneg */
2320 2321
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2322

B
bellard 已提交
2323 2324 2325 2326
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
2327 2328
    int bfa;

2329
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2330
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2331 2332
        return;
    }
2333
    bfa = 4 * (7 - crfS(ctx->opcode));
2334 2335
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
A
aurel32 已提交
2336
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
B
bellard 已提交
2337 2338 2339 2340 2341
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
2342
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2343
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2344 2345
        return;
    }
2346
    gen_reset_fpstatus();
A
aurel32 已提交
2347 2348
    tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
B
bellard 已提交
2349 2350 2351 2352 2353
}

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

2356
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2357
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2358 2359
        return;
    }
A
aurel32 已提交
2360
    crb = 31 - crbD(ctx->opcode);
2361
    gen_reset_fpstatus();
A
aurel32 已提交
2362
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2363 2364 2365 2366
        TCGv_i32 t0;
        /* NIP cannot be restored if the memory exception comes from an helper */
        gen_update_nip(ctx, ctx->nip - 4);
        t0 = tcg_const_i32(crb);
A
aurel32 已提交
2367 2368 2369
        gen_helper_fpscr_clrbit(t0);
        tcg_temp_free_i32(t0);
    }
2370
    if (unlikely(Rc(ctx->opcode) != 0)) {
2371
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2372
    }
B
bellard 已提交
2373 2374 2375 2376 2377
}

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

2380
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2381
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2382 2383
        return;
    }
A
aurel32 已提交
2384
    crb = 31 - crbD(ctx->opcode);
2385 2386
    gen_reset_fpstatus();
    /* XXX: we pretend we can only do IEEE floating-point computations */
A
aurel32 已提交
2387
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2388 2389 2390 2391
        TCGv_i32 t0;
        /* NIP cannot be restored if the memory exception comes from an helper */
        gen_update_nip(ctx, ctx->nip - 4);
        t0 = tcg_const_i32(crb);
A
aurel32 已提交
2392
        gen_helper_fpscr_setbit(t0);
2393
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2394
    }
2395
    if (unlikely(Rc(ctx->opcode) != 0)) {
2396
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2397 2398
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2399
    gen_helper_float_check_status();
B
bellard 已提交
2400 2401 2402 2403 2404
}

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

2407
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2408
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2409 2410
        return;
    }
2411 2412
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2413
    gen_reset_fpstatus();
A
aurel32 已提交
2414 2415
    t0 = tcg_const_i32(FM(ctx->opcode));
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2416
    tcg_temp_free_i32(t0);
2417
    if (unlikely(Rc(ctx->opcode) != 0)) {
2418
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2419 2420
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2421
    gen_helper_float_check_status();
B
bellard 已提交
2422 2423 2424 2425 2426
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
2427
    int bf, sh;
2428 2429
    TCGv_i64 t0;
    TCGv_i32 t1;
2430

2431
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2432
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2433 2434
        return;
    }
2435 2436
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
2437 2438
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2439
    gen_reset_fpstatus();
2440
    t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
A
aurel32 已提交
2441 2442
    t1 = tcg_const_i32(1 << sh);
    gen_helper_store_fpscr(t0, t1);
2443 2444
    tcg_temp_free_i64(t0);
    tcg_temp_free_i32(t1);
2445
    if (unlikely(Rc(ctx->opcode) != 0)) {
2446
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2447 2448
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2449
    gen_helper_float_check_status();
B
bellard 已提交
2450 2451
}

2452 2453
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
A
aurel32 已提交
2454
static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2455 2456 2457
{
    target_long simm = SIMM(ctx->opcode);

2458
    simm &= ~maskl;
A
aurel32 已提交
2459 2460 2461 2462 2463 2464
    if (rA(ctx->opcode) == 0) {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_movi_tl(EA, (uint32_t)simm);
        } else
#endif
2465
        tcg_gen_movi_tl(EA, simm);
A
aurel32 已提交
2466
    } else if (likely(simm != 0)) {
2467
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
A
aurel32 已提交
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    } else {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
        } else
#endif
2479
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
2480
    }
2481 2482
}

A
aurel32 已提交
2483
static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2484
{
A
aurel32 已提交
2485 2486 2487 2488 2489 2490
    if (rA(ctx->opcode) == 0) {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
        } else
#endif
2491
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2492
    } else {
2493
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2494 2495 2496 2497 2498 2499
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
2500 2501
}

A
aurel32 已提交
2502
static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2503
{
A
aurel32 已提交
2504
    if (rA(ctx->opcode) == 0) {
2505
        tcg_gen_movi_tl(EA, 0);
A
aurel32 已提交
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
    } else {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
        } else
#endif
            tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
    }
}

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

2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
static always_inline void gen_check_align (DisasContext *ctx, TCGv EA, int mask)
{
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1, t2;
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    tcg_gen_andi_tl(t0, EA, mask);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
    t2 = tcg_const_i32(0);
    gen_helper_raise_exception_err(t1, t2);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    gen_set_label(l1);
    tcg_temp_free(t0);
}

2544
/***                             Integer load                              ***/
A
aurel32 已提交
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
static always_inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
{
    tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
}

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

static always_inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
{
    tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
A
aurel32 已提交
2559
#if defined(TARGET_PPC64)
A
aurel32 已提交
2560 2561
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
2562
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2563
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2564
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2565 2566 2567 2568
#else
        tcg_gen_bswap16_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2569 2570
}

A
aurel32 已提交
2571
static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2572
{
A
aurel32 已提交
2573 2574
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2575
        TCGv_i32 t0;
A
aurel32 已提交
2576
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
P
pbrook 已提交
2577
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2578
        tcg_gen_trunc_tl_i32(t0, arg1);
2579
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2580 2581
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_gen_ext16s_tl(arg1, arg1);
P
pbrook 已提交
2582
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2583 2584 2585 2586 2587 2588 2589 2590
#else
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
        tcg_gen_bswap16_i32(arg1, arg1);
        tcg_gen_ext16s_i32(arg1, arg1);
#endif
    } else {
        tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2591 2592
}

A
aurel32 已提交
2593
static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2594
{
A
aurel32 已提交
2595 2596 2597 2598 2599
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
2600
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2601
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2602
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2603 2604 2605 2606
#else
        tcg_gen_bswap_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2607 2608
}

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

A
aurel32 已提交
2625
static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2626
{
A
aurel32 已提交
2627 2628 2629 2630
    tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
        tcg_gen_bswap_i64(arg1, arg1);
    }
A
aurel32 已提交
2631 2632
}

A
aurel32 已提交
2633
static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2634
{
A
aurel32 已提交
2635
    tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2636 2637
}

A
aurel32 已提交
2638
static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2639
{
A
aurel32 已提交
2640 2641
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2642
        TCGv_i32 t0;
A
aurel32 已提交
2643
        TCGv t1;
P
pbrook 已提交
2644
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2645
        tcg_gen_trunc_tl_i32(t0, arg1);
2646 2647
        tcg_gen_ext16u_i32(t0, t0);
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2648
        t1 = tcg_temp_new();
2649
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2650
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext16u_tl(t0, arg1);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2663 2664
}

A
aurel32 已提交
2665
static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2666
{
A
aurel32 已提交
2667 2668
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2669
        TCGv_i32 t0;
A
aurel32 已提交
2670
        TCGv t1;
P
pbrook 已提交
2671
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2672
        tcg_gen_trunc_tl_i32(t0, arg1);
2673
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2674
        t1 = tcg_temp_new();
2675
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2676
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new_i32();
        tcg_gen_bswap_i32(t0, arg1);
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
2688 2689
}

A
aurel32 已提交
2690
static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2691
{
A
aurel32 已提交
2692
    if (unlikely(ctx->le_mode)) {
P
pbrook 已提交
2693
        TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
2694 2695
        tcg_gen_bswap_i64(t0, arg1);
        tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
P
pbrook 已提交
2696
        tcg_temp_free_i64(t0);
A
aurel32 已提交
2697
    } else
A
aurel32 已提交
2698
        tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2699 2700
}

2701 2702
#define GEN_LD(name, ldop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2703
{                                                                             \
A
aurel32 已提交
2704 2705 2706 2707 2708
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2709
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2710 2711
}

2712 2713
#define GEN_LDU(name, ldop, opc, type)                                        \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2714
{                                                                             \
A
aurel32 已提交
2715
    TCGv EA;                                                                  \
2716 2717
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2718
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2719
        return;                                                               \
2720
    }                                                                         \
A
aurel32 已提交
2721
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2722
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2723
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2724
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2725
    else                                                                      \
A
aurel32 已提交
2726 2727
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2728 2729
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2730 2731
}

2732 2733
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2734
{                                                                             \
A
aurel32 已提交
2735
    TCGv EA;                                                                  \
2736 2737
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2738
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2739
        return;                                                               \
2740
    }                                                                         \
A
aurel32 已提交
2741
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2742
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2743 2744
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2745 2746
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2747 2748
}

2749 2750
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2751
{                                                                             \
A
aurel32 已提交
2752 2753 2754 2755 2756
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2757
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2758 2759
}

2760 2761 2762 2763 2764
#define GEN_LDS(name, ldop, op, type)                                         \
GEN_LD(name, ldop, op | 0x20, type);                                          \
GEN_LDU(name, ldop, op | 0x21, type);                                         \
GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
GEN_LDX(name, ldop, 0x17, op | 0x00, type)
B
bellard 已提交
2765 2766

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

    /* Restore CPU state */
A
aurel32 已提交
2817
    if (unlikely(ctx->mem_idx == 0)) {
A
aurel32 已提交
2818
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2819 2820 2821 2822 2823
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
A
aurel32 已提交
2824
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2825 2826
        return;
    }
A
aurel32 已提交
2827
    if (unlikely(ctx->le_mode)) {
2828
        /* Little-endian mode is not handled */
A
aurel32 已提交
2829
        gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2830 2831
        return;
    }
A
aurel32 已提交
2832
    gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2833
    EA = tcg_temp_new();
A
aurel32 已提交
2834 2835 2836 2837
    gen_addr_imm_index(ctx, EA, 0x0F);
    gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
    gen_addr_add(ctx, EA, EA, 8);
    gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
A
aurel32 已提交
2838
    tcg_temp_free(EA);
2839 2840
#endif
}
2841
#endif
B
bellard 已提交
2842 2843

/***                              Integer store                            ***/
2844 2845
#define GEN_ST(name, stop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2846
{                                                                             \
A
aurel32 已提交
2847 2848 2849 2850 2851
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2852
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2853 2854
}

2855 2856
#define GEN_STU(name, stop, opc, type)                                        \
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2857
{                                                                             \
A
aurel32 已提交
2858
    TCGv EA;                                                                  \
2859
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2860
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2861
        return;                                                               \
2862
    }                                                                         \
A
aurel32 已提交
2863
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2864
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2865
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2866
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2867
    else                                                                      \
A
aurel32 已提交
2868 2869
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2870 2871
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2872 2873
}

2874 2875
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2876
{                                                                             \
A
aurel32 已提交
2877
    TCGv EA;                                                                  \
2878
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2879
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2880
        return;                                                               \
2881
    }                                                                         \
A
aurel32 已提交
2882
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2883
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2884 2885
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2886 2887
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2888 2889
}

2890 2891
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2892
{                                                                             \
A
aurel32 已提交
2893 2894 2895 2896 2897
    TCGv EA;                                                                  \
    gen_set_access_type(ctx, ACCESS_INT);                                     \
    EA = tcg_temp_new();                                                      \
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2898
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2899 2900
}

2901 2902 2903 2904 2905
#define GEN_STS(name, stop, op, type)                                         \
GEN_ST(name, stop, op | 0x20, type);                                          \
GEN_STU(name, stop, op | 0x21, type);                                         \
GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
GEN_STX(name, stop, 0x17, op | 0x00, type)
B
bellard 已提交
2906 2907

/* stb stbu stbux stbx */
2908
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
B
bellard 已提交
2909
/* sth sthu sthux sthx */
2910
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
B
bellard 已提交
2911
/* stw stwu stwux stwx */
2912
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2913
#if defined(TARGET_PPC64)
2914 2915
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2916
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2917
{
2918
    int rs;
A
aurel32 已提交
2919
    TCGv EA;
2920 2921 2922 2923

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

B
bellard 已提交
2985
/* lwbrx */
A
aurel32 已提交
2986
static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2987
{
A
aurel32 已提交
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_bswap_i32(t0, t0);
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_temp_free_i32(t0);
#else
        tcg_gen_bswap_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
3000
}
3001
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
A
aurel32 已提交
3002

B
bellard 已提交
3003
/* sthbrx */
A
aurel32 已提交
3004
static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3005
{
A
aurel32 已提交
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0;
        TCGv t1;
        t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_ext16u_i32(t0, t0);
        tcg_gen_bswap16_i32(t0, t0);
        t1 = tcg_temp_new();
        tcg_gen_extu_i32_tl(t1, t0);
        tcg_temp_free_i32(t0);
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext16u_tl(t0, arg1);
        tcg_gen_bswap16_i32(t0, t0);
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
3029
}
3030
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
A
aurel32 已提交
3031

B
bellard 已提交
3032
/* stwbrx */
A
aurel32 已提交
3033
static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3034
{
A
aurel32 已提交
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
    if (likely(!ctx->le_mode)) {
#if defined(TARGET_PPC64)
        TCGv_i32 t0;
        TCGv t1;
        t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
        tcg_gen_bswap_i32(t0, t0);
        t1 = tcg_temp_new();
        tcg_gen_extu_i32_tl(t1, t0);
        tcg_temp_free_i32(t0);
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
        tcg_temp_free(t1);
#else
        TCGv t0 = tcg_temp_new_i32();
        tcg_gen_bswap_i32(t0, arg1);
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
        tcg_temp_free(t0);
#endif
    } else {
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
    }
A
aurel32 已提交
3056
}
3057
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
3058 3059 3060 3061 3062

/***                    Integer load and store multiple                    ***/
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
3063 3064 3065
    TCGv t0;
    TCGv_i32 t1;
    gen_set_access_type(ctx, ACCESS_INT);
3066
    /* NIP cannot be restored if the memory exception comes from an helper */
3067
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3068 3069 3070
    t0 = tcg_temp_new();
    t1 = tcg_const_i32(rD(ctx->opcode));
    gen_addr_imm_index(ctx, t0, 0);
3071 3072 3073
    gen_helper_lmw(t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3074 3075 3076 3077 3078
}

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

/***                    Integer load and store strings                     ***/
/* lswi */
3094
/* PowerPC32 specification says we must generate an exception if
3095 3096 3097 3098
 * 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...
 */
3099
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
B
bellard 已提交
3100
{
3101 3102
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3103 3104
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
3105
    int ra = rA(ctx->opcode);
B
bellard 已提交
3106 3107 3108 3109 3110
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
3111 3112 3113
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
A
aurel32 已提交
3114
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3115
        return;
B
bellard 已提交
3116
    }
A
aurel32 已提交
3117
    gen_set_access_type(ctx, ACCESS_INT);
3118
    /* NIP cannot be restored if the memory exception comes from an helper */
3119
    gen_update_nip(ctx, ctx->nip - 4);
3120
    t0 = tcg_temp_new();
A
aurel32 已提交
3121
    gen_addr_register(ctx, t0);
3122 3123 3124 3125 3126 3127
    t1 = tcg_const_i32(nb);
    t2 = tcg_const_i32(start);
    gen_helper_lsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3128 3129 3130
}

/* lswx */
3131
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
B
bellard 已提交
3132
{
A
aurel32 已提交
3133 3134 3135
    TCGv t0;
    TCGv_i32 t1, t2, t3;
    gen_set_access_type(ctx, ACCESS_INT);
3136
    /* NIP cannot be restored if the memory exception comes from an helper */
3137
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3138 3139 3140 3141 3142
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    t1 = tcg_const_i32(rD(ctx->opcode));
    t2 = tcg_const_i32(rA(ctx->opcode));
    t3 = tcg_const_i32(rB(ctx->opcode));
3143 3144 3145 3146 3147
    gen_helper_lswx(t0, t1, t2, t3);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    tcg_temp_free_i32(t3);
B
bellard 已提交
3148 3149 3150
}

/* stswi */
3151
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
B
bellard 已提交
3152
{
A
aurel32 已提交
3153 3154
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3155
    int nb = NB(ctx->opcode);
A
aurel32 已提交
3156
    gen_set_access_type(ctx, ACCESS_INT);
3157
    /* NIP cannot be restored if the memory exception comes from an helper */
3158
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3159 3160
    t0 = tcg_temp_new();
    gen_addr_register(ctx, t0);
B
bellard 已提交
3161 3162
    if (nb == 0)
        nb = 32;
3163
    t1 = tcg_const_i32(nb);
A
aurel32 已提交
3164
    t2 = tcg_const_i32(rS(ctx->opcode));
3165 3166 3167 3168
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3169 3170 3171
}

/* stswx */
3172
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
B
bellard 已提交
3173
{
A
aurel32 已提交
3174 3175 3176
    TCGv t0;
    TCGv_i32 t1, t2;
    gen_set_access_type(ctx, ACCESS_INT);
3177
    /* NIP cannot be restored if the memory exception comes from an helper */
3178
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3179 3180 3181
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    t1 = tcg_temp_new_i32();
3182 3183
    tcg_gen_trunc_tl_i32(t1, cpu_xer);
    tcg_gen_andi_i32(t1, t1, 0x7F);
A
aurel32 已提交
3184
    t2 = tcg_const_i32(rS(ctx->opcode));
3185 3186 3187 3188
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3189 3190 3191 3192
}

/***                        Memory synchronisation                         ***/
/* eieio */
3193
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
3194 3195 3196 3197
{
}

/* isync */
3198
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
3199
{
A
aurel32 已提交
3200
    gen_stop_exception(ctx);
B
bellard 已提交
3201 3202
}

3203
/* lwarx */
3204
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
3205
{
A
aurel32 已提交
3206 3207 3208 3209
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3210
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
3211
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3212 3213
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
B
bellard 已提交
3214 3215 3216
}

/* stwcx. */
3217
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
3218
{
A
aurel32 已提交
3219 3220 3221 3222 3223
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3224 3225 3226 3227
    gen_check_align(ctx, t0, 0x03);
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
A
aurel32 已提交
3228
    l1 = gen_new_label();
3229 3230
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
A
aurel32 已提交
3231
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3232 3233 3234
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
B
bellard 已提交
3235 3236
}

J
j_mayer 已提交
3237 3238
#if defined(TARGET_PPC64)
/* ldarx */
3239
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
3240
{
A
aurel32 已提交
3241 3242 3243 3244
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3245
    gen_check_align(ctx, t0, 0x07);
A
aurel32 已提交
3246
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3247 3248
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
3249 3250 3251
}

/* stdcx. */
3252
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
3253
{
A
aurel32 已提交
3254 3255 3256 3257 3258
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3259 3260 3261 3262
    gen_check_align(ctx, t0, 0x07);
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
A
aurel32 已提交
3263
    l1 = gen_new_label();
3264 3265
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
A
aurel32 已提交
3266
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3267 3268 3269
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
J
j_mayer 已提交
3270 3271 3272
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
3273
/* sync */
3274
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
3275 3276 3277
{
}

3278 3279 3280
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
3281 3282 3283
    TCGv_i32 t0 = tcg_temp_new_i32();
    tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
    tcg_temp_free_i32(t0);
3284
    /* Stop translation, as the CPU is supposed to sleep from now */
A
aurel32 已提交
3285
    gen_exception_err(ctx, EXCP_HLT, 1);
3286 3287
}

B
bellard 已提交
3288
/***                         Floating-point load                           ***/
3289 3290
#define GEN_LDF(name, ldop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3291
{                                                                             \
3292
    TCGv EA;                                                                  \
3293
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3294
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3295 3296
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3297
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3298
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3299 3300
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3301
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3302 3303
}

3304 3305
#define GEN_LDUF(name, ldop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3306
{                                                                             \
3307
    TCGv EA;                                                                  \
3308
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3309
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3310 3311
        return;                                                               \
    }                                                                         \
3312
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3313
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3314
        return;                                                               \
3315
    }                                                                         \
A
aurel32 已提交
3316
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3317
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3318 3319
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3320 3321
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3322 3323
}

3324 3325
#define GEN_LDUXF(name, ldop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3326
{                                                                             \
3327
    TCGv EA;                                                                  \
3328
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3329
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3330 3331
        return;                                                               \
    }                                                                         \
3332
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3333
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3334
        return;                                                               \
3335
    }                                                                         \
A
aurel32 已提交
3336
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3337
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3338 3339
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3340 3341
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3342 3343
}

3344 3345
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3346
{                                                                             \
3347
    TCGv EA;                                                                  \
3348
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3349
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3350 3351
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3352
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3353
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3354 3355
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3356
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3357 3358
}

3359 3360 3361 3362 3363 3364
#define GEN_LDFS(name, ldop, op, type)                                        \
GEN_LDF(name, ldop, op | 0x20, type);                                         \
GEN_LDUF(name, ldop, op | 0x21, type);                                        \
GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
GEN_LDXF(name, ldop, 0x17, op | 0x00, type)

A
aurel32 已提交
3365
static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3366 3367 3368
{
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1 = tcg_temp_new_i32();
A
aurel32 已提交
3369
    gen_qemu_ld32u(ctx, t0, arg2);
3370 3371 3372 3373 3374
    tcg_gen_trunc_tl_i32(t1, t0);
    tcg_temp_free(t0);
    gen_helper_float32_to_float64(arg1, t1);
    tcg_temp_free_i32(t1);
}
B
bellard 已提交
3375

3376 3377 3378 3379
 /* lfd lfdu lfdux lfdx */
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
 /* lfs lfsu lfsux lfsx */
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
B
bellard 已提交
3380 3381

/***                         Floating-point store                          ***/
3382 3383
#define GEN_STF(name, stop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3384
{                                                                             \
3385
    TCGv EA;                                                                  \
3386
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3387
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3388 3389
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3390
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3391
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3392 3393
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3394
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3395 3396
}

3397 3398
#define GEN_STUF(name, stop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3399
{                                                                             \
3400
    TCGv EA;                                                                  \
3401
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3402
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3403 3404
        return;                                                               \
    }                                                                         \
3405
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3406
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3407
        return;                                                               \
3408
    }                                                                         \
A
aurel32 已提交
3409
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3410
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3411 3412
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3413 3414
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3415 3416
}

3417 3418
#define GEN_STUXF(name, stop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3419
{                                                                             \
3420
    TCGv EA;                                                                  \
3421
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3422
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3423 3424
        return;                                                               \
    }                                                                         \
3425
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3426
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3427
        return;                                                               \
3428
    }                                                                         \
A
aurel32 已提交
3429
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3430
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3431 3432
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3433 3434
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3435 3436
}

3437 3438
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3439
{                                                                             \
3440
    TCGv EA;                                                                  \
3441
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3442
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3443 3444
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3445
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3446
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3447 3448
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3449
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3450 3451
}

3452 3453 3454 3455 3456 3457
#define GEN_STFS(name, stop, op, type)                                        \
GEN_STF(name, stop, op | 0x20, type);                                         \
GEN_STUF(name, stop, op | 0x21, type);                                        \
GEN_STUXF(name, stop, op | 0x01, type);                                       \
GEN_STXF(name, stop, 0x17, op | 0x00, type)

A
aurel32 已提交
3458
static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3459 3460 3461 3462 3463 3464
{
    TCGv_i32 t0 = tcg_temp_new_i32();
    TCGv t1 = tcg_temp_new();
    gen_helper_float64_to_float32(t0, arg1);
    tcg_gen_extu_i32_tl(t1, t0);
    tcg_temp_free_i32(t0);
A
aurel32 已提交
3465
    gen_qemu_st32(ctx, t1, arg2);
3466 3467
    tcg_temp_free(t1);
}
B
bellard 已提交
3468 3469

/* stfd stfdu stfdux stfdx */
3470
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
B
bellard 已提交
3471
/* stfs stfsu stfsux stfsx */
3472
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3473 3474

/* Optional: */
A
aurel32 已提交
3475
static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3476 3477 3478
{
    TCGv t0 = tcg_temp_new();
    tcg_gen_trunc_i64_tl(t0, arg1),
A
aurel32 已提交
3479
    gen_qemu_st32(ctx, t0, arg2);
3480 3481
    tcg_temp_free(t0);
}
B
bellard 已提交
3482
/* stfiwx */
3483
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3484 3485

/***                                Branch                                 ***/
3486 3487
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
3488 3489 3490
{
    TranslationBlock *tb;
    tb = ctx->tb;
3491 3492 3493 3494
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        dest = (uint32_t) dest;
#endif
B
bellard 已提交
3495
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3496
        likely(!ctx->singlestep_enabled)) {
B
bellard 已提交
3497
        tcg_gen_goto_tb(n);
3498
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
B
bellard 已提交
3499
        tcg_gen_exit_tb((long)tb + n);
3500
    } else {
3501
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3502 3503
        if (unlikely(ctx->singlestep_enabled)) {
            if ((ctx->singlestep_enabled &
A
aurel32 已提交
3504
                (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3505 3506 3507
                ctx->exception == POWERPC_EXCP_BRANCH) {
                target_ulong tmp = ctx->nip;
                ctx->nip = dest;
A
aurel32 已提交
3508
                gen_exception(ctx, POWERPC_EXCP_TRACE);
3509 3510 3511
                ctx->nip = tmp;
            }
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
A
aurel32 已提交
3512
                gen_debug_exception(ctx);
3513 3514
            }
        }
B
bellard 已提交
3515
        tcg_gen_exit_tb(0);
3516
    }
B
bellard 已提交
3517 3518
}

3519
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3520 3521
{
#if defined(TARGET_PPC64)
3522 3523
    if (ctx->sf_mode == 0)
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3524 3525
    else
#endif
3526
        tcg_gen_movi_tl(cpu_lr, nip);
3527 3528
}

B
bellard 已提交
3529 3530 3531
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3532
    target_ulong li, target;
B
bellard 已提交
3533

3534
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3535
    /* sign extend LI */
3536
#if defined(TARGET_PPC64)
3537 3538 3539
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3540
#endif
3541
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3542
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3543
        target = ctx->nip + li - 4;
B
bellard 已提交
3544
    else
3545
        target = li;
3546 3547
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3548
    gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3549 3550
}

3551 3552 3553 3554
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3555
static always_inline void gen_bcond (DisasContext *ctx, int type)
3556 3557
{
    uint32_t bo = BO(ctx->opcode);
3558 3559
    int l1 = gen_new_label();
    TCGv target;
3560

3561
    ctx->exception = POWERPC_EXCP_BRANCH;
3562
    if (type == BCOND_LR || type == BCOND_CTR) {
P
pbrook 已提交
3563
        target = tcg_temp_local_new();
3564 3565 3566 3567
        if (type == BCOND_CTR)
            tcg_gen_mov_tl(target, cpu_ctr);
        else
            tcg_gen_mov_tl(target, cpu_lr);
3568
    }
3569 3570
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3571 3572 3573
    l1 = gen_new_label();
    if ((bo & 0x4) == 0) {
        /* Decrement and test CTR */
P
pbrook 已提交
3574
        TCGv temp = tcg_temp_new();
3575
        if (unlikely(type == BCOND_CTR)) {
A
aurel32 已提交
3576
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3577 3578 3579
            return;
        }
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3580
#if defined(TARGET_PPC64)
3581 3582 3583
        if (!ctx->sf_mode)
            tcg_gen_ext32u_tl(temp, cpu_ctr);
        else
3584
#endif
3585 3586 3587 3588 3589
            tcg_gen_mov_tl(temp, cpu_ctr);
        if (bo & 0x2) {
            tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
        } else {
            tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3590
        }
P
pbrook 已提交
3591
        tcg_temp_free(temp);
3592 3593 3594 3595 3596
    }
    if ((bo & 0x10) == 0) {
        /* Test CR */
        uint32_t bi = BI(ctx->opcode);
        uint32_t mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
3597
        TCGv_i32 temp = tcg_temp_new_i32();
3598

3599
        if (bo & 0x8) {
3600 3601
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3602
        } else {
3603 3604
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3605
        }
P
pbrook 已提交
3606
        tcg_temp_free_i32(temp);
3607
    }
3608
    if (type == BCOND_IM) {
3609 3610 3611 3612 3613 3614
        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
            gen_goto_tb(ctx, 0, ctx->nip + li - 4);
        } else {
            gen_goto_tb(ctx, 0, li);
        }
B
bellard 已提交
3615
        gen_set_label(l1);
3616
        gen_goto_tb(ctx, 1, ctx->nip);
3617
    } else {
3618
#if defined(TARGET_PPC64)
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
        if (!(ctx->sf_mode))
            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
        else
#endif
            tcg_gen_andi_tl(cpu_nip, target, ~3);
        tcg_gen_exit_tb(0);
        gen_set_label(l1);
#if defined(TARGET_PPC64)
        if (!(ctx->sf_mode))
            tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3629 3630
        else
#endif
3631
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
B
bellard 已提交
3632
        tcg_gen_exit_tb(0);
J
j_mayer 已提交
3633
    }
3634 3635 3636
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3637
{
3638 3639 3640 3641
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3642
{
3643 3644 3645 3646
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3647
{
3648 3649
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3650 3651

/***                      Condition register logical                       ***/
3652 3653
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
B
bellard 已提交
3654
{                                                                             \
3655 3656
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
P
pbrook 已提交
3657
    TCGv_i32 t0, t1;                                                          \
3658
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
P
pbrook 已提交
3659
    t0 = tcg_temp_new_i32();                                                  \
3660
    if (sh > 0)                                                               \
3661
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3662
    else if (sh < 0)                                                          \
3663
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3664
    else                                                                      \
3665
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
P
pbrook 已提交
3666
    t1 = tcg_temp_new_i32();                                                  \
3667 3668
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
3669
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3670
    else if (sh < 0)                                                          \
3671
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3672
    else                                                                      \
3673 3674
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
    tcg_op(t0, t0, t1);                                                       \
3675
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3676 3677 3678
    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
P
pbrook 已提交
3679 3680
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
B
bellard 已提交
3681 3682 3683
}

/* crand */
3684
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
B
bellard 已提交
3685
/* crandc */
3686
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
B
bellard 已提交
3687
/* creqv */
3688
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
B
bellard 已提交
3689
/* crnand */
3690
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
B
bellard 已提交
3691
/* crnor */
3692
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
B
bellard 已提交
3693
/* cror */
3694
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
B
bellard 已提交
3695
/* crorc */
3696
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
B
bellard 已提交
3697
/* crxor */
3698
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
B
bellard 已提交
3699 3700 3701
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
A
aurel32 已提交
3702
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
B
bellard 已提交
3703 3704 3705
}

/***                           System linkage                              ***/
A
aurel32 已提交
3706
/* rfi (mem_idx only) */
3707
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3708
{
3709
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3710
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3711 3712
#else
    /* Restore CPU state */
A
aurel32 已提交
3713
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3714
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3715
        return;
3716
    }
3717
    gen_helper_rfi();
A
aurel32 已提交
3718
    gen_sync_exception(ctx);
3719
#endif
B
bellard 已提交
3720 3721
}

J
j_mayer 已提交
3722
#if defined(TARGET_PPC64)
3723
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3724 3725
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3726
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3727 3728
#else
    /* Restore CPU state */
A
aurel32 已提交
3729
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3730
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3731 3732
        return;
    }
3733
    gen_helper_rfid();
A
aurel32 已提交
3734
    gen_sync_exception(ctx);
J
j_mayer 已提交
3735 3736 3737
#endif
}

J
j_mayer 已提交
3738
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3739 3740
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3741
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3742 3743
#else
    /* Restore CPU state */
A
aurel32 已提交
3744
    if (unlikely(ctx->mem_idx <= 1)) {
A
aurel32 已提交
3745
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3746 3747
        return;
    }
3748
    gen_helper_hrfid();
A
aurel32 已提交
3749
    gen_sync_exception(ctx);
3750 3751 3752 3753
#endif
}
#endif

B
bellard 已提交
3754
/* sc */
3755 3756 3757 3758 3759
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3760
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3761
{
3762 3763 3764
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
A
aurel32 已提交
3765
    gen_exception_err(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3766 3767 3768 3769
}

/***                                Trap                                   ***/
/* tw */
3770
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3771
{
3772
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3773
    /* Update the nip since this might generate a trap exception */
3774
    gen_update_nip(ctx, ctx->nip);
3775 3776
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
B
bellard 已提交
3777 3778 3779 3780 3781
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3782 3783
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3784 3785
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3786 3787 3788
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3789 3790
}

3791 3792 3793 3794
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
3795
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3796 3797
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3798 3799
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
3800 3801 3802 3803 3804
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
3805 3806
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3807 3808
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3809 3810 3811
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
3812 3813 3814
}
#endif

B
bellard 已提交
3815 3816 3817 3818
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
A
aurel32 已提交
3819 3820
    tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3821
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
B
bellard 已提交
3822 3823 3824
}

/* mfcr */
3825
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3826
{
3827
    uint32_t crm, crn;
3828

3829 3830 3831 3832
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
3833
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3834
        }
3835
    } else {
P
pbrook 已提交
3836
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3837
    }
B
bellard 已提交
3838 3839 3840 3841 3842
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3843
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3844
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3845
#else
A
aurel32 已提交
3846
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3847
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3848
        return;
3849
    }
3850
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3851
#endif
B
bellard 已提交
3852 3853
}

J
j_mayer 已提交
3854
#if 1
3855
#define SPR_NOACCESS ((void *)(-1UL))
3856 3857 3858 3859 3860 3861 3862 3863 3864
#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 已提交
3865
/* mfspr */
3866
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3867
{
A
aurel32 已提交
3868
    void (*read_cb)(void *opaque, int gprn, int sprn);
B
bellard 已提交
3869 3870
    uint32_t sprn = SPR(ctx->opcode);

3871
#if !defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3872
    if (ctx->mem_idx == 2)
3873
        read_cb = ctx->spr_cb[sprn].hea_read;
A
aurel32 已提交
3874
    else if (ctx->mem_idx)
3875 3876
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3877
#endif
3878
        read_cb = ctx->spr_cb[sprn].uea_read;
3879 3880
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
A
aurel32 已提交
3881
            (*read_cb)(ctx, rD(ctx->opcode), sprn);
3882 3883
        } else {
            /* Privilege exception */
3884 3885 3886 3887 3888 3889
            /* 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) {
3890
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
J
j_mayer 已提交
3891
                            ADDRX "\n", sprn, sprn, ctx->nip);
3892
                }
J
j_mayer 已提交
3893 3894
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
                       sprn, sprn, ctx->nip);
3895
            }
A
aurel32 已提交
3896
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
B
bellard 已提交
3897
        }
3898 3899
    } else {
        /* Not defined */
J
j_mayer 已提交
3900
        if (loglevel != 0) {
J
j_mayer 已提交
3901 3902
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3903
        }
J
j_mayer 已提交
3904 3905
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
A
aurel32 已提交
3906
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3907 3908 3909
    }
}

3910
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3911
{
3912
    gen_op_mfspr(ctx);
3913
}
3914 3915

/* mftb */
3916
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3917 3918
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3919 3920 3921
}

/* mtcrf */
3922
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3923
{
3924
    uint32_t crm, crn;
3925

3926 3927
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
P
pbrook 已提交
3928
        TCGv_i32 temp = tcg_temp_new_i32();
3929
        crn = ffs(crm);
P
pbrook 已提交
3930 3931
        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3932
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
P
pbrook 已提交
3933
        tcg_temp_free_i32(temp);
3934
    } else {
P
pbrook 已提交
3935 3936 3937
        TCGv_i32 temp = tcg_const_i32(crm);
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
        tcg_temp_free_i32(temp);
3938
    }
B
bellard 已提交
3939 3940 3941
}

/* mtmsr */
J
j_mayer 已提交
3942
#if defined(TARGET_PPC64)
3943
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3944 3945
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3946
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3947
#else
A
aurel32 已提交
3948
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3949
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3950 3951
        return;
    }
3952 3953
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
3954 3955 3956 3957 3958
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
        tcg_temp_free(t0);
3959
    } else {
3960 3961 3962 3963
        /* 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
         */
3964
        gen_update_nip(ctx, ctx->nip);
3965
        gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3966 3967
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
3968
        gen_stop_exception(ctx);
3969
    }
J
j_mayer 已提交
3970 3971 3972 3973
#endif
}
#endif

B
bellard 已提交
3974 3975
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3976
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3977
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3978
#else
A
aurel32 已提交
3979
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3980
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3981
        return;
3982
    }
3983 3984
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
3985 3986 3987 3988 3989
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
        tcg_temp_free(t0);
3990
    } else {
3991 3992 3993 3994
        /* 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
         */
3995
        gen_update_nip(ctx, ctx->nip);
3996
#if defined(TARGET_PPC64)
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006
        if (!ctx->sf_mode) {
            TCGv t0 = tcg_temp_new();
            TCGv t1 = tcg_temp_new();
            tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
            tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_temp_free(t1);
            gen_helper_store_msr(t0);
            tcg_temp_free(t0);
        } else
4007
#endif
4008
            gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
4009
        /* Must stop the translation as machine state (may have) changed */
4010
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
4011
        gen_stop_exception(ctx);
4012
    }
4013
#endif
B
bellard 已提交
4014 4015 4016 4017 4018
}

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

4022
#if !defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4023
    if (ctx->mem_idx == 2)
4024
        write_cb = ctx->spr_cb[sprn].hea_write;
A
aurel32 已提交
4025
    else if (ctx->mem_idx)
4026 4027
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
4028
#endif
4029
        write_cb = ctx->spr_cb[sprn].uea_write;
4030 4031
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
A
aurel32 已提交
4032
            (*write_cb)(ctx, sprn, rS(ctx->opcode));
4033 4034
        } else {
            /* Privilege exception */
J
j_mayer 已提交
4035
            if (loglevel != 0) {
J
j_mayer 已提交
4036 4037
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
                        ADDRX "\n", sprn, sprn, ctx->nip);
4038
            }
J
j_mayer 已提交
4039 4040
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
                   sprn, sprn, ctx->nip);
A
aurel32 已提交
4041
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4042
        }
4043 4044
    } else {
        /* Not defined */
J
j_mayer 已提交
4045
        if (loglevel != 0) {
J
j_mayer 已提交
4046 4047
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
4048
        }
J
j_mayer 已提交
4049 4050
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
A
aurel32 已提交
4051
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
4052 4053 4054 4055 4056
    }
}

/***                         Cache management                              ***/
/* dcbf */
4057
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
4058
{
J
j_mayer 已提交
4059
    /* XXX: specification says this is treated as a load by the MMU */
A
aurel32 已提交
4060 4061 4062 4063 4064
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld8u(ctx, t0, t0);
4065
    tcg_temp_free(t0);
B
bellard 已提交
4066 4067 4068
}

/* dcbi (Supervisor only) */
4069
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4070
{
4071
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4072
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4073
#else
A
aurel32 已提交
4074
    TCGv EA, val;
A
aurel32 已提交
4075
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4076
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4077
        return;
4078
    }
P
pbrook 已提交
4079
    EA = tcg_temp_new();
A
aurel32 已提交
4080 4081
    gen_set_access_type(ctx, ACCESS_CACHE);
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
4082
    val = tcg_temp_new();
4083
    /* XXX: specification says this should be treated as a store by the MMU */
A
aurel32 已提交
4084 4085
    gen_qemu_ld8u(ctx, val, EA);
    gen_qemu_st8(ctx, val, EA);
A
aurel32 已提交
4086 4087
    tcg_temp_free(val);
    tcg_temp_free(EA);
4088
#endif
B
bellard 已提交
4089 4090 4091
}

/* dcdst */
4092
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4093
{
4094
    /* XXX: specification say this is treated as a load by the MMU */
A
aurel32 已提交
4095 4096 4097 4098 4099
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld8u(ctx, t0, t0);
4100
    tcg_temp_free(t0);
B
bellard 已提交
4101 4102 4103
}

/* dcbt */
4104
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
4105
{
4106
    /* interpreted as no-op */
4107 4108 4109
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4110 4111 4112
}

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

/* dcbz */
4122
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
4123
{
A
aurel32 已提交
4124 4125
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4126 4127
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4128 4129
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4130 4131
    gen_helper_dcbz(t0);
    tcg_temp_free(t0);
4132 4133
}

4134
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4135
{
A
aurel32 已提交
4136 4137
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4138 4139
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4140 4141
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4142
    if (ctx->opcode & 0x00200000)
4143
        gen_helper_dcbz(t0);
4144
    else
4145 4146
        gen_helper_dcbz_970(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4147 4148 4149
}

/* icbi */
4150
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
4151
{
A
aurel32 已提交
4152 4153
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4154 4155
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4156 4157
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4158 4159
    gen_helper_icbi(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4160 4161 4162 4163
}

/* Optional: */
/* dcba */
4164
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
4165
{
4166 4167 4168 4169
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4170 4171 4172 4173 4174 4175 4176
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
4177
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4178
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4179
#else
4180
    TCGv t0;
A
aurel32 已提交
4181
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4182
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4183
        return;
4184
    }
4185 4186 4187
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4188
#endif
B
bellard 已提交
4189 4190 4191
}

/* mfsrin */
4192
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4193
{
4194
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4195
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4196
#else
4197
    TCGv t0;
A
aurel32 已提交
4198
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4199
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4200
        return;
4201
    }
4202 4203 4204 4205 4206
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4207
#endif
B
bellard 已提交
4208 4209 4210
}

/* mtsr */
B
bellard 已提交
4211
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
4212
{
4213
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4214
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4215
#else
4216
    TCGv t0;
A
aurel32 已提交
4217
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4218
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4219
        return;
4220
    }
4221 4222 4223
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4224
#endif
B
bellard 已提交
4225 4226 4227
}

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

4246 4247 4248
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
4249
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4250 4251
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4252
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4253
#else
4254
    TCGv t0;
A
aurel32 已提交
4255
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4256
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4257 4258
        return;
    }
4259 4260 4261
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4262 4263 4264 4265
#endif
}

/* mfsrin */
4266 4267
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
4268 4269
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4270
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4271
#else
4272
    TCGv t0;
A
aurel32 已提交
4273
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4274
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4275 4276
        return;
    }
4277 4278 4279 4280 4281
    t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4282 4283 4284 4285
#endif
}

/* mtsr */
4286
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4287 4288
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4289
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4290
#else
4291
    TCGv t0;
A
aurel32 已提交
4292
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4293
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4294 4295
        return;
    }
4296 4297 4298
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4299 4300 4301 4302
#endif
}

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

B
bellard 已提交
4323
/***                      Lookaside buffer management                      ***/
A
aurel32 已提交
4324
/* Optional & mem_idx only: */
B
bellard 已提交
4325
/* tlbia */
4326
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4327
{
4328
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4329
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4330
#else
A
aurel32 已提交
4331
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4332
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4333
        return;
4334
    }
4335
    gen_helper_tlbia();
4336
#endif
B
bellard 已提交
4337 4338 4339
}

/* tlbie */
4340
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4341
{
4342
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4343
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4344
#else
A
aurel32 已提交
4345
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4346
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4347
        return;
4348
    }
4349
#if defined(TARGET_PPC64)
4350 4351 4352 4353 4354 4355
    if (!ctx->sf_mode) {
        TCGv t0 = tcg_temp_new();
        tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
        gen_helper_tlbie(t0);
        tcg_temp_free(t0);
    } else
4356
#endif
4357
        gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4358
#endif
B
bellard 已提交
4359 4360 4361
}

/* tlbsync */
4362
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4363
{
4364
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4365
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4366
#else
A
aurel32 已提交
4367
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4368
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4369
        return;
4370 4371 4372 4373
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
A
aurel32 已提交
4374
    gen_stop_exception(ctx);
4375
#endif
B
bellard 已提交
4376 4377
}

J
j_mayer 已提交
4378 4379 4380 4381 4382
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4383
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4384
#else
A
aurel32 已提交
4385
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4386
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4387 4388
        return;
    }
4389
    gen_helper_slbia();
J
j_mayer 已提交
4390 4391 4392 4393 4394 4395 4396
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4397
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4398
#else
A
aurel32 已提交
4399
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4400
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4401 4402
        return;
    }
4403
    gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
4404 4405 4406 4407
#endif
}
#endif

B
bellard 已提交
4408 4409
/***                              External control                         ***/
/* Optional: */
4410
/* eciwx */
B
bellard 已提交
4411 4412
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4413
    TCGv t0;
4414
    /* Should check EAR[E] ! */
A
aurel32 已提交
4415 4416 4417
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4418
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4419
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4420
    tcg_temp_free(t0);
4421 4422 4423 4424 4425
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4426
    TCGv t0;
4427
    /* Should check EAR[E] ! */
A
aurel32 已提交
4428 4429 4430
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4431
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4432
    gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4433
    tcg_temp_free(t0);
4434 4435 4436 4437 4438 4439
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
4440 4441 4442 4443 4444 4445 4446 4447
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
4448
    if (unlikely(Rc(ctx->opcode) != 0))
4449
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4450 4451 4452 4453 4454
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    int l3 = gen_new_label();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l3);
    gen_set_label(l2);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l3);
4470
    if (unlikely(Rc(ctx->opcode) != 0))
4471
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4472 4473 4474
}

/* clcs */
4475
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4476
{
4477 4478 4479
    TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
    gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
4480
    /* Rc=1 sets CR0 to an undefined state */
4481 4482 4483 4484 4485
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
4486
    gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4487
    if (unlikely(Rc(ctx->opcode) != 0))
4488
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4489 4490 4491 4492 4493
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
4494
    gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4495
    if (unlikely(Rc(ctx->opcode) != 0))
4496
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4497 4498 4499 4500 4501
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
4502
    gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4503
    if (unlikely(Rc(ctx->opcode) != 0))
4504
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4505 4506 4507 4508 4509
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
4510
    gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4511
    if (unlikely(Rc(ctx->opcode) != 0))
4512
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4513 4514 4515 4516 4517
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
4518 4519 4520 4521 4522 4523 4524 4525
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
    tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
4526
    if (unlikely(Rc(ctx->opcode) != 0))
4527
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4528 4529 4530 4531 4532
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
    tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4555
    if (unlikely(Rc(ctx->opcode) != 0))
4556
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4557 4558 4559 4560 4561
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572
    target_long simm = SIMM(ctx->opcode);
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
    tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
    gen_set_label(l2);
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4573 4574 4575 4576 4577
}

/* lscbx - lscbx. */
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
{
4578 4579 4580 4581
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
    TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
    TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4582

A
aurel32 已提交
4583
    gen_addr_reg_index(ctx, t0);
4584
    /* NIP cannot be restored if the memory exception comes from an helper */
4585
    gen_update_nip(ctx, ctx->nip - 4);
4586 4587 4588 4589
    gen_helper_lscbx(t0, t0, t1, t2, t3);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
    tcg_temp_free_i32(t3);
A
aurel32 已提交
4590
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4591
    tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4592
    if (unlikely(Rc(ctx->opcode) != 0))
4593 4594
        gen_set_Rc0(ctx, t0);
    tcg_temp_free(t0);
4595 4596 4597 4598 4599
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    TCGv t3 = tcg_temp_new();
    tcg_gen_movi_tl(t3, 0xFFFFFFFF);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
    tcg_gen_addi_tl(t2, t0, 1);
    tcg_gen_shr_tl(t2, t3, t2);
    tcg_gen_shr_tl(t3, t3, t1);
    tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
    tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
    tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(t3);
4619
    if (unlikely(Rc(ctx->opcode) != 0))
4620
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4621 4622 4623 4624 4625
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
4626 4627 4628 4629 4630 4631 4632
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4633
    if (unlikely(Rc(ctx->opcode) != 0))
4634
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4635 4636 4637 4638 4639
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652
    TCGv_i64 t0 = tcg_temp_new_i64();
    TCGv_i64 t1 = tcg_temp_new_i64();
    TCGv t2 = tcg_temp_new();
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_trunc_i64_tl(t2, t0);
    gen_store_spr(SPR_MQ, t2);
    tcg_gen_shri_i64(t1, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
    tcg_temp_free(t2);
4653
    if (unlikely(Rc(ctx->opcode) != 0))
4654
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4655 4656 4657 4658 4659
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679
    int l1 = gen_new_label();
    TCGv_i64 t0 = tcg_temp_new_i64();
    TCGv_i64 t1 = tcg_temp_new_i64();
    TCGv t2 = tcg_temp_new();
    /* Start with XER OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mul_i64(t0, t0, t1);
    tcg_gen_trunc_i64_tl(t2, t0);
    gen_store_spr(SPR_MQ, t2);
    tcg_gen_shri_i64(t1, t0, 32);
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
    tcg_gen_ext32s_i64(t1, t0);
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
    gen_set_label(l1);
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
    tcg_temp_free(t2);
4680
    if (unlikely(Rc(ctx->opcode) != 0))
4681
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4682 4683 4684 4685 4686
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
4687 4688 4689 4690 4691 4692 4693 4694
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
4695
    if (unlikely(Rc(ctx->opcode) != 0))
4696
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4697 4698 4699 4700 4701
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
4702 4703 4704 4705 4706 4707 4708 4709 4710 4711
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
    gen_set_label(l2);
    /* nabs never overflows */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4712
    if (unlikely(Rc(ctx->opcode) != 0))
4713
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4714 4715 4716 4717 4718
}

/* rlmi - rlmi. */
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4719 4720 4721 4722 4723 4724 4725 4726 4727
    uint32_t mb = MB(ctx->opcode);
    uint32_t me = ME(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    tcg_gen_andi_tl(t0, t0, MASK(mb, me));
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
    tcg_temp_free(t0);
4728
    if (unlikely(Rc(ctx->opcode) != 0))
4729
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4730 4731 4732 4733 4734
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0x80000000);
    tcg_gen_shr_tl(t1, t1, t0);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    tcg_gen_and_tl(t0, t0, t1);
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4746
    if (unlikely(Rc(ctx->opcode) != 0))
4747
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4748 4749 4750 4751 4752
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4764
    if (unlikely(Rc(ctx->opcode) != 0))
4765
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4766 4767 4768 4769 4770
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t2, 0xFFFFFFFF);
    tcg_gen_shl_tl(t2, t2, t0);
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_and_tl(t0, t0, t2);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4786
    if (unlikely(Rc(ctx->opcode) != 0))
4787
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4788 4789 4790 4791 4792
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4803
    if (unlikely(Rc(ctx->opcode) != 0))
4804
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4805 4806 4807 4808 4809
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4821
    if (unlikely(Rc(ctx->opcode) != 0))
4822
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4823 4824 4825 4826 4827
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_local_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shl_tl(t1, t1, t2);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    gen_load_spr(t0, SPR_MQ);
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    gen_load_spr(t2, SPR_MQ);
    tcg_gen_andc_tl(t1, t2, t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4850
    if (unlikely(Rc(ctx->opcode) != 0))
4851
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4852 4853 4854 4855 4856
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    gen_store_spr(SPR_MQ, t1);
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4873
    if (unlikely(Rc(ctx->opcode) != 0))
4874
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4875 4876
}

4877
/* sraiq - sraiq. */
4878 4879
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
    int sh = SH(ctx->opcode);
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t0, t0, t1);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
    gen_set_label(l1);
    tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4896
    if (unlikely(Rc(ctx->opcode) != 0))
4897
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4898 4899 4900 4901 4902
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_subfi_tl(t2, 32, t2);
    tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_or_tl(t0, t0, t2);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
    tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
    tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
    gen_set_label(l2);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4929
    if (unlikely(Rc(ctx->opcode) != 0))
4930
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4931 4932 4933 4934 4935
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4947
    if (unlikely(Rc(ctx->opcode) != 0))
4948
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4949 4950 4951 4952 4953
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
4954 4955 4956 4957 4958 4959 4960 4961
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
4962
    if (unlikely(Rc(ctx->opcode) != 0))
4963
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4964 4965 4966 4967 4968
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    TCGv t2 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shr_tl(t1, t1, t0);
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
    gen_load_spr(t2, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_and_tl(t0, t0, t1);
    tcg_gen_andc_tl(t2, t2, t1);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
4984
    if (unlikely(Rc(ctx->opcode) != 0))
4985
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4986 4987 4988 4989 4990
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
4991 4992 4993 4994 4995 4996 4997 4998 4999 5000
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
    tcg_gen_or_tl(t1, t0, t1);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    gen_store_spr(SPR_MQ, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5001
    if (unlikely(Rc(ctx->opcode) != 0))
5002
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5003 5004 5005 5006 5007
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018
    int sh = SH(ctx->opcode);
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
    gen_load_spr(t1, SPR_MQ);
    gen_store_spr(SPR_MQ, t0);
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5019
    if (unlikely(Rc(ctx->opcode) != 0))
5020
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5021 5022 5023 5024 5025
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    TCGv t0 = tcg_temp_local_new();
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
    tcg_gen_shr_tl(t2, t1, t2);
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    gen_load_spr(t0, SPR_MQ);
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
    tcg_gen_and_tl(t0, t0, t2);
    gen_load_spr(t1, SPR_MQ);
    tcg_gen_andc_tl(t1, t1, t2);
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
    gen_set_label(l2);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
5049
    if (unlikely(Rc(ctx->opcode) != 0))
5050
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5051 5052 5053 5054 5055
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
    int l1 = gen_new_label();
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_subfi_tl(t1, 32, t1);
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
    tcg_gen_or_tl(t1, t0, t1);
    gen_store_spr(SPR_MQ, t1);
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
    gen_set_label(l1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5072
    if (unlikely(Rc(ctx->opcode) != 0))
5073
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5074 5075 5076 5077 5078 5079 5080
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5081
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5082 5083 5084 5085 5086 5087
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5088
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5089 5090 5091 5092 5093 5094
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5095
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5096
#else
A
aurel32 已提交
5097
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5098
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5099 5100
        return;
    }
5101
    gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5102 5103 5104 5105 5106
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
5107
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5108 5109
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5110
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5111
#else
A
aurel32 已提交
5112
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5113
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5114 5115
        return;
    }
5116
    gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5117 5118 5119 5120
#endif
}

/* tlbli */
5121
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5122 5123
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5124
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5125
#else
A
aurel32 已提交
5126
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5127
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5128 5129
        return;
    }
5130
    gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5131 5132 5133
#endif
}

5134 5135
/* 74xx TLB management */
/* tlbld */
5136
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5137 5138
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5139
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5140
#else
A
aurel32 已提交
5141
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5142
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5143 5144
        return;
    }
5145
    gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5146 5147 5148 5149
#endif
}

/* tlbli */
5150
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5151 5152
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5153
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5154
#else
A
aurel32 已提交
5155
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5156
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5157 5158
        return;
    }
5159
    gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5160 5161 5162
#endif
}

5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
/* 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 已提交
5173
    /* Cache line invalidate: privileged and treated as no-op */
5174
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5175
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5176
#else
A
aurel32 已提交
5177
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5178
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
        return;
    }
#endif
}

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

GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5193
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5194
#else
5195 5196 5197
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);
    TCGv t0;
A
aurel32 已提交
5198
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5199
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5200 5201
        return;
    }
5202
    t0 = tcg_temp_new();
A
aurel32 已提交
5203
    gen_addr_reg_index(ctx, t0);
5204 5205 5206 5207
    tcg_gen_shri_tl(t0, t0, 28);
    tcg_gen_andi_tl(t0, t0, 0xF);
    gen_helper_load_sr(cpu_gpr[rd], t0);
    tcg_temp_free(t0);
5208
    if (ra != 0 && ra != rd)
5209
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5210 5211 5212 5213 5214 5215
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5216
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5217
#else
5218
    TCGv t0;
A
aurel32 已提交
5219
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5220
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5221 5222
        return;
    }
5223
    t0 = tcg_temp_new();
A
aurel32 已提交
5224
    gen_addr_reg_index(ctx, t0);
5225 5226
    gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
5227 5228 5229 5230 5231 5232
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5233
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5234
#else
A
aurel32 已提交
5235
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5236
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5237 5238
        return;
    }
5239
    gen_helper_rfsvc();
A
aurel32 已提交
5240
    gen_sync_exception(ctx);
5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251
#endif
}

/* svc is not implemented for now */

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

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
5252
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5253 5254 5255 5256 5257 5258 5259
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5260
    tcg_temp_free(t0);
5261 5262 5263 5264 5265 5266
}

/* lfqu */
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5267
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5268 5269 5270 5271 5272 5273 5274 5275
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    t1 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5276
    if (ra != 0)
5277 5278 5279
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5280 5281 5282 5283 5284 5285
}

/* lfqux */
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5286
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5287 5288 5289 5290 5291 5292 5293 5294 5295
    gen_set_access_type(ctx, ACCESS_FLOAT);
    TCGv t0, t1;
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5296
    if (ra != 0)
5297 5298
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5299 5300 5301 5302 5303
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
5304
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5305 5306 5307 5308 5309 5310 5311
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5312
    tcg_temp_free(t0);
5313 5314 5315 5316 5317
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
5318
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5319 5320 5321 5322 5323 5324 5325
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5326
    tcg_temp_free(t0);
5327 5328 5329 5330 5331 5332
}

/* stfqu */
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5333
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5334 5335 5336 5337 5338 5339 5340 5341 5342
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_imm_index(ctx, t0, 0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5343
    if (ra != 0)
5344 5345
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5346 5347 5348 5349 5350 5351
}

/* stfqux */
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5352
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5353 5354 5355 5356 5357 5358 5359 5360 5361
    TCGv t0, t1;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    t1 = tcg_temp_new();
    gen_addr_add(ctx, t1, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
    tcg_temp_free(t1);
5362
    if (ra != 0)
5363 5364
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5365 5366 5367 5368 5369
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
5370
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5371 5372 5373 5374 5375 5376 5377
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_FLOAT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
    gen_addr_add(ctx, t0, t0, 8);
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5378
    tcg_temp_free(t0);
5379 5380 5381
}

/* BookE specific instructions */
5382
/* XXX: not implemented on 440 ? */
5383
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5384 5385
{
    /* XXX: TODO */
A
aurel32 已提交
5386
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5387 5388
}

5389
/* XXX: not implemented on 440 ? */
5390
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5391 5392
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5393
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5394
#else
5395
    TCGv t0;
A
aurel32 已提交
5396
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5397
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5398 5399
        return;
    }
5400
    t0 = tcg_temp_new();
A
aurel32 已提交
5401
    gen_addr_reg_index(ctx, t0);
5402 5403
    gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
    tcg_temp_free(t0);
5404 5405 5406 5407
#endif
}

/* All 405 MAC instructions are translated here */
5408 5409 5410
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
5411
{
5412 5413
    TCGv t0, t1;

P
pbrook 已提交
5414 5415
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
5416

5417 5418 5419 5420 5421 5422 5423
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
5424 5425 5426
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16s_tl(t1, t1);
5427 5428 5429 5430 5431
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
5432 5433 5434
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16u_tl(t1, t1);
5435 5436 5437 5438 5439 5440 5441
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
5442 5443 5444 5445
        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
        tcg_gen_ext16s_tl(t0, t0);
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16s_tl(t1, t1);
5446 5447 5448 5449 5450
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
5451 5452 5453 5454
        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
        tcg_gen_ext16u_tl(t0, t0);
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16u_tl(t1, t1);
5455 5456 5457 5458 5459 5460 5461
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
5462 5463
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5464 5465 5466 5467 5468
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
5469 5470
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5471 5472 5473
        break;
    }
    if (opc2 & 0x04) {
5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497
        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
        tcg_gen_mul_tl(t1, t0, t1);
        if (opc2 & 0x02) {
            /* nmultiply-and-accumulate (0x0E) */
            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
        } else {
            /* multiply-and-accumulate (0x0C) */
            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
        }

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

            if (opc3 & 0x10) {
                /* Start with XER OV disabled, the most likely case */
                tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
            }
            if (opc3 & 0x01) {
                /* Signed */
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
A
aurel32 已提交
5498
                if (opc3 & 0x02) {
5499 5500 5501 5502 5503 5504 5505
                    /* Saturate */
                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
                }
            } else {
                /* Unsigned */
                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
A
aurel32 已提交
5506
                if (opc3 & 0x02) {
5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
                    /* Saturate */
                    tcg_gen_movi_tl(t0, UINT32_MAX);
                }
            }
            if (opc3 & 0x10) {
                /* Check overflow */
                tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
            }
            gen_set_label(l1);
            tcg_gen_mov_tl(cpu_gpr[rt], t0);
        }
    } else {
        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5520
    }
5521 5522
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5523 5524
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
5525
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5526 5527 5528
    }
}

5529 5530
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5531 5532 5533 5534 5535 5536
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5537
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5538
/* macchwo   - macchwo.   */
5539
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5540
/* macchws   - macchws.   */
5541
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5542
/* macchwso  - macchwso.  */
5543
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5544
/* macchwsu  - macchwsu.  */
5545
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5546
/* macchwsuo - macchwsuo. */
5547
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5548
/* macchwu   - macchwu.   */
5549
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5550
/* macchwuo  - macchwuo.  */
5551
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5552
/* machhw    - machhw.    */
5553
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5554
/* machhwo   - machhwo.   */
5555
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5556
/* machhws   - machhws.   */
5557
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5558
/* machhwso  - machhwso.  */
5559
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5560
/* machhwsu  - machhwsu.  */
5561
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5562
/* machhwsuo - machhwsuo. */
5563
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5564
/* machhwu   - machhwu.   */
5565
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5566
/* machhwuo  - machhwuo.  */
5567
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5568
/* maclhw    - maclhw.    */
5569
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5570
/* maclhwo   - maclhwo.   */
5571
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5572
/* maclhws   - maclhws.   */
5573
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5574
/* maclhwso  - maclhwso.  */
5575
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5576
/* maclhwu   - maclhwu.   */
5577
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5578
/* maclhwuo  - maclhwuo.  */
5579
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5580
/* maclhwsu  - maclhwsu.  */
5581
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5582
/* maclhwsuo - maclhwsuo. */
5583
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5584
/* nmacchw   - nmacchw.   */
5585
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5586
/* nmacchwo  - nmacchwo.  */
5587
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5588
/* nmacchws  - nmacchws.  */
5589
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5590
/* nmacchwso - nmacchwso. */
5591
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5592
/* nmachhw   - nmachhw.   */
5593
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5594
/* nmachhwo  - nmachhwo.  */
5595
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5596
/* nmachhws  - nmachhws.  */
5597
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5598
/* nmachhwso - nmachhwso. */
5599
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5600
/* nmaclhw   - nmaclhw.   */
5601
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5602
/* nmaclhwo  - nmaclhwo.  */
5603
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5604
/* nmaclhws  - nmaclhws.  */
5605
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5606
/* nmaclhwso - nmaclhwso. */
5607
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5608 5609

/* mulchw  - mulchw.  */
5610
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5611
/* mulchwu - mulchwu. */
5612
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5613
/* mulhhw  - mulhhw.  */
5614
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5615
/* mulhhwu - mulhhwu. */
5616
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5617
/* mullhw  - mullhw.  */
5618
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5619
/* mullhwu - mullhwu. */
5620
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5621 5622

/* mfdcr */
5623
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5624 5625
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5626
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5627
#else
5628
    TCGv dcrn;
A
aurel32 已提交
5629
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5630
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5631 5632
        return;
    }
5633 5634 5635 5636 5637
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    dcrn = tcg_const_tl(SPR(ctx->opcode));
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
    tcg_temp_free(dcrn);
5638 5639 5640 5641
#endif
}

/* mtdcr */
5642
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5643 5644
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5645
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5646
#else
5647
    TCGv dcrn;
A
aurel32 已提交
5648
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5649
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5650 5651
        return;
    }
5652 5653 5654 5655 5656
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    dcrn = tcg_const_tl(SPR(ctx->opcode));
    gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(dcrn);
5657 5658 5659 5660
#endif
}

/* mfdcrx */
5661
/* XXX: not implemented on 440 ? */
5662
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5663 5664
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5665
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5666
#else
A
aurel32 已提交
5667
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5668
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5669 5670
        return;
    }
5671 5672 5673
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5674
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5675 5676 5677 5678
#endif
}

/* mtdcrx */
5679
/* XXX: not implemented on 440 ? */
5680
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5681 5682
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5683
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5684
#else
A
aurel32 已提交
5685
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5686
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5687 5688
        return;
    }
5689 5690 5691
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5692
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5693 5694 5695
#endif
}

5696 5697 5698
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
5699 5700 5701
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5702 5703 5704 5705 5706 5707
    /* 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)
{
5708 5709 5710
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5711 5712 5713
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

5714 5715 5716 5717
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5718
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5719
#else
A
aurel32 已提交
5720
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5721
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5722 5723 5724 5725 5726 5727 5728 5729 5730 5731
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5732
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5733
#else
A
aurel32 已提交
5734
    TCGv EA, val;
A
aurel32 已提交
5735
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5736
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5737 5738
        return;
    }
A
aurel32 已提交
5739
    gen_set_access_type(ctx, ACCESS_CACHE);
P
pbrook 已提交
5740
    EA = tcg_temp_new();
A
aurel32 已提交
5741
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
5742
    val = tcg_temp_new();
A
aurel32 已提交
5743
    gen_qemu_ld32u(ctx, val, EA);
A
aurel32 已提交
5744 5745 5746
    tcg_temp_free(val);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
    tcg_temp_free(EA);
5747 5748 5749 5750
#endif
}

/* icbt */
5751
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
}

/* iccci */
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5763
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5764
#else
A
aurel32 已提交
5765
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5766
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5767 5768 5769 5770 5771 5772 5773 5774 5775 5776
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5777
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5778
#else
A
aurel32 已提交
5779
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5780
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5781 5782 5783 5784 5785 5786
        return;
    }
    /* interpreted as no-op */
#endif
}

A
aurel32 已提交
5787
/* rfci (mem_idx only) */
5788
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5789 5790
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5791
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5792
#else
A
aurel32 已提交
5793
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5794
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5795 5796 5797
        return;
    }
    /* Restore CPU state */
5798
    gen_helper_40x_rfci();
A
aurel32 已提交
5799
    gen_sync_exception(ctx);
5800 5801 5802 5803 5804 5805
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5806
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5807
#else
A
aurel32 已提交
5808
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5809
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5810 5811 5812
        return;
    }
    /* Restore CPU state */
5813
    gen_helper_rfci();
A
aurel32 已提交
5814
    gen_sync_exception(ctx);
5815 5816 5817 5818
#endif
}

/* BookE specific */
5819
/* XXX: not implemented on 440 ? */
5820
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5821 5822
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5823
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5824
#else
A
aurel32 已提交
5825
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5826
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5827 5828 5829
        return;
    }
    /* Restore CPU state */
5830
    gen_helper_rfdi();
A
aurel32 已提交
5831
    gen_sync_exception(ctx);
5832 5833 5834
#endif
}

5835
/* XXX: not implemented on 440 ? */
5836
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5837 5838
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5839
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5840
#else
A
aurel32 已提交
5841
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5842
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5843 5844 5845
        return;
    }
    /* Restore CPU state */
5846
    gen_helper_rfmci();
A
aurel32 已提交
5847
    gen_sync_exception(ctx);
5848 5849
#endif
}
5850

5851
/* TLB management - PowerPC 405 implementation */
5852
/* tlbre */
5853
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5854 5855
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5856
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5857
#else
A
aurel32 已提交
5858
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5859
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5860 5861 5862 5863
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5864
        gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5865 5866
        break;
    case 1:
5867
        gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5868 5869
        break;
    default:
A
aurel32 已提交
5870
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5871
        break;
5872
    }
5873 5874 5875
#endif
}

5876
/* tlbsx - tlbsx. */
5877
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5878 5879
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5880
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5881
#else
5882
    TCGv t0;
A
aurel32 已提交
5883
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5884
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5885 5886
        return;
    }
5887
    t0 = tcg_temp_new();
A
aurel32 已提交
5888
    gen_addr_reg_index(ctx, t0);
5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899
    gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
    if (Rc(ctx->opcode)) {
        int l1 = gen_new_label();
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
        gen_set_label(l1);
    }
5900
#endif
B
bellard 已提交
5901 5902
}

5903
/* tlbwe */
5904
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5905
{
5906
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5907
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5908
#else
A
aurel32 已提交
5909
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5910
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5911 5912 5913 5914
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5915
        gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5916 5917
        break;
    case 1:
5918
        gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5919 5920
        break;
    default:
A
aurel32 已提交
5921
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5922
        break;
5923
    }
5924 5925 5926
#endif
}

5927
/* TLB management - PowerPC 440 implementation */
5928
/* tlbre */
5929
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5930 5931
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5932
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5933
#else
A
aurel32 已提交
5934
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5935
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5936 5937 5938 5939 5940 5941
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
5942 5943 5944 5945 5946
        {
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
            gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
            tcg_temp_free_i32(t0);
        }
5947 5948
        break;
    default:
A
aurel32 已提交
5949
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5950 5951 5952 5953 5954 5955
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5956
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5957 5958
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5959
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5960
#else
5961
    TCGv t0;
A
aurel32 已提交
5962
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5963
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5964 5965
        return;
    }
5966
    t0 = tcg_temp_new();
A
aurel32 已提交
5967
    gen_addr_reg_index(ctx, t0);
5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978
    gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
    if (Rc(ctx->opcode)) {
        int l1 = gen_new_label();
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
        gen_set_label(l1);
    }
5979 5980 5981 5982
#endif
}

/* tlbwe */
5983
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5984 5985
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5986
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5987
#else
A
aurel32 已提交
5988
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5989
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5990 5991 5992 5993 5994 5995
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
5996 5997 5998 5999 6000
        {
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
            gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
            tcg_temp_free_i32(t0);
        }
6001 6002
        break;
    default:
A
aurel32 已提交
6003
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6004 6005 6006 6007 6008
        break;
    }
#endif
}

6009
/* wrtee */
6010
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
6011 6012
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6013
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6014
#else
6015
    TCGv t0;
A
aurel32 已提交
6016
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6017
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6018 6019
        return;
    }
6020 6021 6022 6023 6024
    t0 = tcg_temp_new();
    tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
    tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
    tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
6025 6026 6027
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
A
aurel32 已提交
6028
    gen_stop_exception(ctx);
6029 6030 6031 6032
#endif
}

/* wrteei */
6033
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6034 6035
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6036
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6037
#else
A
aurel32 已提交
6038
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6039
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6040 6041
        return;
    }
6042 6043 6044
    if (ctx->opcode & 0x00010000) {
        tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
        /* Stop translation to have a chance to raise an exception */
A
aurel32 已提交
6045
        gen_stop_exception(ctx);
6046 6047 6048
    } else {
        tcg_gen_andi_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
    }
6049 6050 6051
#endif
}

J
j_mayer 已提交
6052
/* PowerPC 440 specific instructions */
6053 6054 6055
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
6056 6057 6058 6059
    TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
    gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
                     cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
6060 6061 6062
}

/* mbar replaces eieio on 440 */
A
aurel32 已提交
6063
GEN_HANDLER(mbar, 0x1F, 0x16, 0x1a, 0x001FF801, PPC_BOOKE)
6064 6065 6066 6067 6068
{
    /* interpreted as no-op */
}

/* msync replaces sync on 440 */
6069
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6070 6071 6072 6073 6074
{
    /* interpreted as no-op */
}

/* icbt */
6075
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6076 6077 6078 6079 6080
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
6081 6082
}

6083 6084 6085
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */

A
aurel32 已提交
6086 6087
static always_inline TCGv_ptr gen_avr_ptr(int reg)
{
A
aurel32 已提交
6088
    TCGv_ptr r = tcg_temp_new_ptr();
A
aurel32 已提交
6089 6090 6091 6092
    tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
    return r;
}

6093
#define GEN_VR_LDX(name, opc2, opc3)                                          \
6094
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
6095
{                                                                             \
6096
    TCGv EA;                                                                  \
6097
    if (unlikely(!ctx->altivec_enabled)) {                                    \
A
aurel32 已提交
6098
        gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6099 6100
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
6101
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6102
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
6103
    gen_addr_reg_index(ctx, EA);                                              \
6104
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
A
aurel32 已提交
6105 6106
    if (ctx->le_mode) {                                                       \
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6107
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6108
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6109
    } else {                                                                  \
A
aurel32 已提交
6110
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6111
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6112
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6113 6114
    }                                                                         \
    tcg_temp_free(EA);                                                        \
6115 6116 6117 6118 6119
}

#define GEN_VR_STX(name, opc2, opc3)                                          \
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
{                                                                             \
6120
    TCGv EA;                                                                  \
6121
    if (unlikely(!ctx->altivec_enabled)) {                                    \
A
aurel32 已提交
6122
        gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6123 6124
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
6125
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6126
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
6127
    gen_addr_reg_index(ctx, EA);                                              \
6128
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
A
aurel32 已提交
6129 6130
    if (ctx->le_mode) {                                                       \
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6131
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6132
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6133
    } else {                                                                  \
A
aurel32 已提交
6134
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6135
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6136
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6137 6138
    }                                                                         \
    tcg_temp_free(EA);                                                        \
6139 6140
}

6141
GEN_VR_LDX(lvx, 0x07, 0x03);
6142
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6143
GEN_VR_LDX(lvxl, 0x07, 0x0B);
6144

6145
GEN_VR_STX(svx, 0x07, 0x07);
6146
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6147
GEN_VR_STX(svxl, 0x07, 0x0F);
6148

6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
/* Logical operations */
#define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)            \
{                                                                       \
    if (unlikely(!ctx->altivec_enabled)) {                              \
        gen_exception(ctx, POWERPC_EXCP_VPU);                           \
        return;                                                         \
    }                                                                   \
    tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
    tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
}

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

6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183
#define GEN_VXFORM(name, opc2, opc3)                                    \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)            \
{                                                                       \
    TCGv_ptr ra, rb, rd;                                                \
    if (unlikely(!ctx->altivec_enabled)) {                              \
        gen_exception(ctx, POWERPC_EXCP_VPU);                           \
        return;                                                         \
    }                                                                   \
    ra = gen_avr_ptr(rA(ctx->opcode));                                  \
    rb = gen_avr_ptr(rB(ctx->opcode));                                  \
    rd = gen_avr_ptr(rD(ctx->opcode));                                  \
    gen_helper_##name (rd, ra, rb);                                     \
    tcg_temp_free_ptr(ra);                                              \
    tcg_temp_free_ptr(rb);                                              \
    tcg_temp_free_ptr(rd);                                              \
}

A
aurel32 已提交
6184 6185 6186 6187 6188 6189
GEN_VXFORM(vaddubm, 0, 0);
GEN_VXFORM(vadduhm, 0, 1);
GEN_VXFORM(vadduwm, 0, 2);
GEN_VXFORM(vsububm, 0, 16);
GEN_VXFORM(vsubuhm, 0, 17);
GEN_VXFORM(vsubuwm, 0, 18);
6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
GEN_VXFORM(vmaxub, 1, 0);
GEN_VXFORM(vmaxuh, 1, 1);
GEN_VXFORM(vmaxuw, 1, 2);
GEN_VXFORM(vmaxsb, 1, 4);
GEN_VXFORM(vmaxsh, 1, 5);
GEN_VXFORM(vmaxsw, 1, 6);
GEN_VXFORM(vminub, 1, 8);
GEN_VXFORM(vminuh, 1, 9);
GEN_VXFORM(vminuw, 1, 10);
GEN_VXFORM(vminsb, 1, 12);
GEN_VXFORM(vminsh, 1, 13);
GEN_VXFORM(vminsw, 1, 14);
A
aurel32 已提交
6202 6203 6204 6205 6206 6207 6208
GEN_VXFORM(vavgub, 1, 16);
GEN_VXFORM(vavguh, 1, 17);
GEN_VXFORM(vavguw, 1, 18);
GEN_VXFORM(vavgsb, 1, 20);
GEN_VXFORM(vavgsh, 1, 21);
GEN_VXFORM(vavgsw, 1, 22);

6209 6210
/***                           SPE extension                               ***/
/* Register moves */
6211

P
pbrook 已提交
6212
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
A
aurel32 已提交
6213 6214 6215
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
#else
P
pbrook 已提交
6216
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6217
#endif
A
aurel32 已提交
6218
}
6219

P
pbrook 已提交
6220
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
A
aurel32 已提交
6221 6222 6223
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(cpu_gpr[reg], t);
#else
P
pbrook 已提交
6224
    TCGv_i64 tmp = tcg_temp_new_i64();
A
aurel32 已提交
6225 6226 6227
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
    tcg_gen_shri_i64(tmp, t, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
P
pbrook 已提交
6228
    tcg_temp_free_i64(tmp);
6229
#endif
A
aurel32 已提交
6230
}
6231

6232 6233 6234 6235 6236 6237 6238 6239 6240 6241
#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 */
6242
static always_inline void gen_speundef (DisasContext *ctx)
6243
{
A
aurel32 已提交
6244
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6245 6246
}

6247 6248 6249
/* SPE logic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6250
static always_inline void gen_##name (DisasContext *ctx)                      \
6251 6252
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6253
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6254 6255
        return;                                                               \
    }                                                                         \
6256 6257 6258 6259 6260 6261 6262 6263
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
}
#else
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6264
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6265 6266 6267 6268 6269 6270
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
           cpu_gprh[rB(ctx->opcode)]);                                        \
6271
}
6272 6273 6274 6275 6276 6277 6278 6279 6280 6281
#endif

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

6283 6284 6285
/* SPE logic immediate */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6286 6287 6288
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6289
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6290 6291
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6292 6293 6294
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6295 6296 6297 6298
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_opi(t0, t0, rB(ctx->opcode));                                         \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6299
    tcg_temp_free_i64(t2);                                                    \
6300 6301
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6302 6303
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6304
}
6305 6306
#else
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6307
static always_inline void gen_##name (DisasContext *ctx)                      \
6308 6309
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6310
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6311 6312
        return;                                                               \
    }                                                                         \
6313 6314 6315 6316
    tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
            rB(ctx->opcode));                                                 \
    tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
            rB(ctx->opcode));                                                 \
6317
}
6318 6319 6320 6321 6322
#endif
GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6323

6324 6325 6326
/* SPE arithmetic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6327
static always_inline void gen_##name (DisasContext *ctx)                      \
6328 6329
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6330
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6331 6332
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6333 6334 6335
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6336 6337 6338 6339
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_op(t0, t0);                                                           \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6340
    tcg_temp_free_i64(t2);                                                    \
6341 6342
    tcg_op(t1, t1);                                                           \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6343 6344
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6345
}
6346
#else
P
pbrook 已提交
6347
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6348 6349 6350
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6351
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6352 6353 6354 6355 6356 6357
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
}
#endif
6358

P
pbrook 已提交
6359
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6360 6361 6362
{
    int l1 = gen_new_label();
    int l2 = gen_new_label();
6363

6364 6365 6366 6367
    tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
    tcg_gen_neg_i32(ret, arg1);
    tcg_gen_br(l2);
    gen_set_label(l1);
P
pbrook 已提交
6368
    tcg_gen_mov_i32(ret, arg1);
6369 6370 6371 6372 6373 6374
    gen_set_label(l2);
}
GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
P
pbrook 已提交
6375
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6376
{
6377 6378 6379 6380
    tcg_gen_addi_i32(ret, arg1, 0x8000);
    tcg_gen_ext16u_i32(ret, ret);
}
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
P
pbrook 已提交
6381 6382
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6383

6384 6385 6386
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6387 6388
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6389
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6390 6391
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6392 6393 6394
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6395
    TCGv_i64 t3 = tcg_temp_local_new_i64();                                   \
6396 6397 6398 6399 6400 6401 6402
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_op(t0, t0, t2);                                                       \
    tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t3);                                            \
    tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t2, t3);                                            \
P
pbrook 已提交
6403
    tcg_temp_free_i64(t3);                                                    \
6404
    tcg_op(t1, t1, t2);                                                       \
P
pbrook 已提交
6405
    tcg_temp_free_i32(t2);                                                    \
6406
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6407 6408
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6409
}
6410 6411 6412
#else
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6413 6414
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6415
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6416 6417
        return;                                                               \
    }                                                                         \
6418 6419 6420 6421
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
           cpu_gpr[rB(ctx->opcode)]);                                         \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
           cpu_gprh[rB(ctx->opcode)]);                                        \
6422
}
6423
#endif
6424

P
pbrook 已提交
6425
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6426
{
P
pbrook 已提交
6427
    TCGv_i32 t0;
6428
    int l1, l2;
6429

6430 6431
    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6432
    t0 = tcg_temp_local_new_i32();
6433 6434 6435 6436 6437 6438 6439 6440
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_shr_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6441
    tcg_temp_free_i32(t0);
6442 6443
}
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
P
pbrook 已提交
6444
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6445
{
P
pbrook 已提交
6446
    TCGv_i32 t0;
6447 6448 6449 6450
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6451
    t0 = tcg_temp_local_new_i32();
6452 6453 6454 6455 6456 6457 6458 6459
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_sar_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6460
    tcg_temp_free_i32(t0);
6461 6462
}
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
P
pbrook 已提交
6463
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6464
{
P
pbrook 已提交
6465
    TCGv_i32 t0;
6466 6467 6468 6469
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6470
    t0 = tcg_temp_local_new_i32();
6471 6472 6473 6474 6475 6476 6477 6478
    /* No error here: 6 bits are used */
    tcg_gen_andi_i32(t0, arg2, 0x3F);
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
    tcg_gen_shl_i32(ret, arg1, t0);
    tcg_gen_br(l2);
    gen_set_label(l1);
    tcg_gen_movi_i32(ret, 0);
    tcg_gen_br(l2);
P
pbrook 已提交
6479
    tcg_temp_free_i32(t0);
6480 6481
}
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
P
pbrook 已提交
6482
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6483
{
P
pbrook 已提交
6484
    TCGv_i32 t0 = tcg_temp_new_i32();
6485 6486
    tcg_gen_andi_i32(t0, arg2, 0x1F);
    tcg_gen_rotl_i32(ret, arg1, t0);
P
pbrook 已提交
6487
    tcg_temp_free_i32(t0);
6488 6489 6490 6491 6492
}
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
static always_inline void gen_evmergehi (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6493
        gen_exception(ctx, POWERPC_EXCP_APU);
6494 6495 6496
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6497 6498
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
}
GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
P
pbrook 已提交
6510
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6511
{
6512 6513 6514
    tcg_gen_sub_i32(ret, arg2, arg1);
}
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6515

6516 6517 6518 6519 6520 6521
/* SPE arithmetic immediate */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6522
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6523 6524
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6525 6526 6527
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6528 6529 6530 6531
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_op(t0, t0, rA(ctx->opcode));                                          \
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
A
aurel32 已提交
6532
    tcg_temp_free_i64(t2);                                                    \
6533 6534
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6535 6536
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6537 6538 6539 6540 6541 6542
}
#else
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6543
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560
        return;                                                               \
    }                                                                         \
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
           rA(ctx->opcode));                                                  \
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
           rA(ctx->opcode));                                                  \
}
#endif
GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);

/* SPE comparison */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6561
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6562 6563 6564 6565 6566 6567
        return;                                                               \
    }                                                                         \
    int l1 = gen_new_label();                                                 \
    int l2 = gen_new_label();                                                 \
    int l3 = gen_new_label();                                                 \
    int l4 = gen_new_label();                                                 \
P
pbrook 已提交
6568 6569 6570
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6571 6572 6573
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
P
pbrook 已提交
6574
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6575 6576 6577 6578 6579 6580 6581 6582 6583
    tcg_gen_br(l2);                                                           \
    gen_set_label(l1);                                                        \
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
    gen_set_label(l2);                                                        \
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t0, t2);                                            \
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
P
pbrook 已提交
6584
    tcg_temp_free_i64(t2);                                                    \
6585 6586 6587 6588 6589 6590 6591 6592
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
    tcg_gen_br(l4);                                                           \
    gen_set_label(l3);                                                        \
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
                    CRF_CH | CRF_CH_OR_CL);                                   \
    gen_set_label(l4);                                                        \
P
pbrook 已提交
6593 6594
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6595 6596 6597 6598 6599 6600
}
#else
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6601
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
        return;                                                               \
    }                                                                         \
    int l1 = gen_new_label();                                                 \
    int l2 = gen_new_label();                                                 \
    int l3 = gen_new_label();                                                 \
    int l4 = gen_new_label();                                                 \
                                                                              \
    tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
                       cpu_gpr[rB(ctx->opcode)], l1);                         \
    tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
    tcg_gen_br(l2);                                                           \
    gen_set_label(l1);                                                        \
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
    gen_set_label(l2);                                                        \
    tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
                       cpu_gprh[rB(ctx->opcode)], l3);                        \
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
    tcg_gen_br(l4);                                                           \
    gen_set_label(l3);                                                        \
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
                    CRF_CH | CRF_CH_OR_CL);                                   \
    gen_set_label(l4);                                                        \
}
#endif
GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);

/* SPE misc */
static always_inline void gen_brinc (DisasContext *ctx)
{
    /* Note: brinc is usable even if SPE is disabled */
P
pbrook 已提交
6638 6639
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6640
}
6641 6642 6643
static always_inline void gen_evmergelo (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6644
        gen_exception(ctx, POWERPC_EXCP_APU);
6645 6646 6647
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6648 6649
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evmergehilo (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6663
        gen_exception(ctx, POWERPC_EXCP_APU);
6664 6665 6666
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6667 6668
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evmergelohi (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6682
        gen_exception(ctx, POWERPC_EXCP_APU);
6683 6684 6685
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6686 6687
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
#else
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
}
static always_inline void gen_evsplati (DisasContext *ctx)
{
6700
    uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6701

6702
#if defined(TARGET_PPC64)
6703
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6704 6705 6706 6707 6708
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
}
6709
static always_inline void gen_evsplatfi (DisasContext *ctx)
6710
{
6711
    uint64_t imm = rA(ctx->opcode) << 11;
6712

6713
#if defined(TARGET_PPC64)
6714
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6715 6716 6717 6718
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
6719 6720
}

6721 6722 6723 6724 6725 6726
static always_inline void gen_evsel (DisasContext *ctx)
{
    int l1 = gen_new_label();
    int l2 = gen_new_label();
    int l3 = gen_new_label();
    int l4 = gen_new_label();
P
pbrook 已提交
6727
    TCGv_i32 t0 = tcg_temp_local_new_i32();
6728
#if defined(TARGET_PPC64)
P
pbrook 已提交
6729 6730
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761
#endif
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
#endif
    tcg_gen_br(l2);
    gen_set_label(l1);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
#endif
    gen_set_label(l2);
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
#else
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
#endif
    tcg_gen_br(l4);
    gen_set_label(l3);
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
#else
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
#endif
    gen_set_label(l4);
P
pbrook 已提交
6762
    tcg_temp_free_i32(t0);
6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784
#if defined(TARGET_PPC64)
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
#endif
}
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811

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

6812
/* SPE load and stores */
A
aurel32 已提交
6813
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6814 6815 6816
{
    target_ulong uimm = rB(ctx->opcode);

A
aurel32 已提交
6817
    if (rA(ctx->opcode) == 0) {
6818
        tcg_gen_movi_tl(EA, uimm << sh);
A
aurel32 已提交
6819
    } else {
6820
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
A
aurel32 已提交
6821 6822 6823 6824 6825 6826
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
6827
}
6828 6829 6830 6831

static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
6832
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6833 6834
#else
    TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
6835
    gen_qemu_ld64(ctx, t0, addr);
6836 6837 6838 6839 6840
    tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_shri_i64(t0, t0, 32);
    tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_temp_free_i64(t0);
#endif
6841
}
6842 6843 6844

static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
{
6845
#if defined(TARGET_PPC64)
6846
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6847
    gen_qemu_ld32u(ctx, t0, addr);
6848
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
A
aurel32 已提交
6849 6850
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_ld32u(ctx, t0, addr);
6851 6852 6853
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
6854 6855 6856
    gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6857
#endif
6858
}
6859 6860 6861 6862 6863

static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
6864
    gen_qemu_ld16u(ctx, t0, addr);
6865
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
A
aurel32 已提交
6866 6867
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6868 6869
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6870 6871
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6872 6873
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6874 6875
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6876
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6877
#else
A
aurel32 已提交
6878
    gen_qemu_ld16u(ctx, t0, addr);
6879
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6880 6881
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6882
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
A
aurel32 已提交
6883 6884
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6885
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6886 6887
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6888
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6889
#endif
6890
    tcg_temp_free(t0);
6891 6892
}

6893 6894 6895
static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6896
    gen_qemu_ld16u(ctx, t0, addr);
6897 6898 6899 6900 6901 6902 6903 6904 6905 6906
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
6907 6908
}

6909 6910 6911
static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6912
    gen_qemu_ld16u(ctx, t0, addr);
6913 6914 6915 6916 6917 6918 6919 6920
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
6921 6922
}

6923 6924 6925
static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6926
    gen_qemu_ld16s(ctx, t0, addr);
6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941
#if defined(TARGET_PPC64)
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_ext32u_tl(t0, t0);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
6942
    gen_qemu_ld16u(ctx, t0, addr);
6943
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
A
aurel32 已提交
6944 6945
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6946 6947 6948
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
A
aurel32 已提交
6949
    gen_qemu_ld16u(ctx, t0, addr);
6950
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6951 6952
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6953 6954 6955 6956 6957 6958 6959 6960 6961
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6962 6963 6964
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6965 6966 6967 6968
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
6969 6970 6971
    gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6972 6973 6974 6975 6976 6977 6978
#endif
}

static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6979
    gen_qemu_ld16s(ctx, t0, addr);
6980
    tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6981 6982
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16s(ctx, t0, addr);
6983 6984 6985 6986
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
6987 6988 6989
    gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6990 6991 6992 6993 6994 6995
#endif
}

static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6996
    gen_qemu_ld32u(ctx, t0, addr);
6997
#if defined(TARGET_PPC64)
6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
#endif
    tcg_temp_free(t0);
}

static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
7011
    gen_qemu_ld16u(ctx, t0, addr);
7012 7013 7014
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
7015 7016
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7017 7018 7019 7020
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
#else
A
aurel32 已提交
7021
    gen_qemu_ld16u(ctx, t0, addr);
7022 7023
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
A
aurel32 已提交
7024 7025
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
7026 7027
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
7028
#endif
7029 7030 7031 7032 7033 7034
    tcg_temp_free(t0);
}

static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
7035
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7036
#else
7037 7038
    TCGv_i64 t0 = tcg_temp_new_i64();
    tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
A
aurel32 已提交
7039
    gen_qemu_st64(ctx, t0, addr);
7040 7041 7042 7043 7044 7045
    tcg_temp_free_i64(t0);
#endif
}

static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
{
7046
#if defined(TARGET_PPC64)
7047 7048
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7049
    gen_qemu_st32(ctx, t0, addr);
7050 7051
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7052
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7053
#endif
A
aurel32 已提交
7054 7055
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7056 7057 7058 7059 7060 7061 7062 7063 7064 7065
}

static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
#else
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
#endif
A
aurel32 已提交
7066 7067
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
7068 7069
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7070
    gen_qemu_st16(ctx, t0, addr);
7071
#else
A
aurel32 已提交
7072
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7073
#endif
A
aurel32 已提交
7074
    gen_addr_add(ctx, addr, addr, 2);
7075
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
7076
    gen_qemu_st16(ctx, t0, addr);
7077
    tcg_temp_free(t0);
A
aurel32 已提交
7078 7079
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7080 7081 7082 7083 7084 7085 7086 7087 7088 7089
}

static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
#else
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
#endif
A
aurel32 已提交
7090 7091
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
7092
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
7093
    gen_qemu_st16(ctx, t0, addr);
7094 7095 7096 7097 7098 7099 7100 7101
    tcg_temp_free(t0);
}

static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7102
    gen_qemu_st16(ctx, t0, addr);
7103 7104
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7105
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7106
#endif
A
aurel32 已提交
7107 7108
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7109 7110 7111 7112 7113 7114 7115
}

static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
7116
    gen_qemu_st32(ctx, t0, addr);
7117 7118
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7119
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7120 7121 7122 7123 7124
#endif
}

static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
{
A
aurel32 已提交
7125
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7126 7127 7128
}

#define GEN_SPEOP_LDST(name, opc2, sh)                                        \
A
aurel32 已提交
7129
GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                      \
7130 7131 7132
{                                                                             \
    TCGv t0;                                                                  \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7133
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7134 7135
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7136
    gen_set_access_type(ctx, ACCESS_INT);                                     \
7137 7138
    t0 = tcg_temp_new();                                                      \
    if (Rc(ctx->opcode)) {                                                    \
A
aurel32 已提交
7139
        gen_addr_spe_imm_index(ctx, t0, sh);                                  \
7140
    } else {                                                                  \
A
aurel32 已提交
7141
        gen_addr_reg_index(ctx, t0);                                          \
7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165
    }                                                                         \
    gen_op_##name(ctx, t0);                                                   \
    tcg_temp_free(t0);                                                        \
}

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

GEN_SPEOP_LDST(evstdd, 0x10, 3);
GEN_SPEOP_LDST(evstdw, 0x11, 3);
GEN_SPEOP_LDST(evstdh, 0x12, 3);
GEN_SPEOP_LDST(evstwhe, 0x18, 2);
GEN_SPEOP_LDST(evstwho, 0x1A, 2);
GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243

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

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

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

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

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

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

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

/***                      SPE floating-point extension                     ***/
A
aurel32 已提交
7244 7245
#if defined(TARGET_PPC64)
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7246
static always_inline void gen_##name (DisasContext *ctx)                      \
7247
{                                                                             \
A
aurel32 已提交
7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259
    TCGv_i32 t0;                                                              \
    TCGv t1;                                                                  \
    t0 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(t0, t0);                                                \
    t1 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t1, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
    tcg_temp_free(t1);                                                        \
7260
}
A
aurel32 已提交
7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i32 t0;                                                              \
    TCGv t1;                                                                  \
    t0 = tcg_temp_new_i32();                                                  \
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
    t1 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t1, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
    tcg_temp_free(t1);                                                        \
}
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i32 t0 = tcg_temp_new_i32();                                         \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
    tcg_temp_free_i32(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7290 7291
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7292 7293
    TCGv_i32 t0, t1;                                                          \
    TCGv_i64 t2;                                                              \
7294
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7295
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7296 7297
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310
    t0 = tcg_temp_new_i32();                                                  \
    t1 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(t0, t0, t1);                                            \
    tcg_temp_free_i32(t1);                                                    \
    t2 = tcg_temp_new();                                                      \
    tcg_gen_extu_i32_tl(t2, t0);                                              \
    tcg_temp_free_i32(t0);                                                    \
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
                    0xFFFFFFFF00000000ULL);                                   \
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
    tcg_temp_free(t2);                                                        \
7311
}
A
aurel32 已提交
7312
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7313 7314 7315
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7316
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7317 7318
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7319 7320
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
                      cpu_gpr[rB(ctx->opcode)]);                              \
7321
}
A
aurel32 已提交
7322
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7323 7324
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7325
    TCGv_i32 t0, t1;                                                          \
7326
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7327
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7328 7329
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341
    t0 = tcg_temp_new_i32();                                                  \
    t1 = tcg_temp_new_i32();                                                  \
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
}
#define GEN_SPEFPUOP_COMP_64(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7342
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7343 7344 7345 7346 7347 7348 7349 7350 7351 7352
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#else
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7353
}
A
aurel32 已提交
7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
    gen_helper_##name(t0, t0);                                                \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
}
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7383
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7384 7385 7386 7387 7388 7389 7390 7391 7392 7393
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0, t1;                                                          \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7394
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409
        return;                                                               \
    }                                                                         \
    t0 = tcg_temp_new_i64();                                                  \
    t1 = tcg_temp_new_i64();                                                  \
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
    gen_helper_##name(t0, t0, t1);                                            \
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
    tcg_temp_free_i64(t0);                                                    \
    tcg_temp_free_i64(t1);                                                    \
}
#define GEN_SPEFPUOP_COMP_32(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7410
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7411 7412 7413 7414 7415 7416 7417 7418 7419 7420
        return;                                                               \
    }                                                                         \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
}
#define GEN_SPEFPUOP_COMP_64(name)                                            \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    TCGv_i64 t0, t1;                                                          \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7421
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432
        return;                                                               \
    }                                                                         \
    t0 = tcg_temp_new_i64();                                                  \
    t1 = tcg_temp_new_i64();                                                  \
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
    tcg_temp_free_i64(t0);                                                    \
    tcg_temp_free_i64(t1);                                                    \
}
#endif
7433

7434 7435
/* Single precision floating-point vectors operations */
/* Arithmetic */
A
aurel32 已提交
7436 7437 7438 7439 7440 7441 7442
GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
GEN_SPEFPUOP_ARITH2_64_64(evfssub);
GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
static always_inline void gen_evfsabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7443
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
#else
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
#endif
}
static always_inline void gen_evfsnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7456
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
#else
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}
static always_inline void gen_evfsneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7469
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7470 7471 7472 7473 7474 7475 7476 7477 7478 7479
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
#else
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}

7480
/* Conversion */
A
aurel32 已提交
7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491
GEN_SPEFPUOP_CONV_64_64(evfscfui);
GEN_SPEFPUOP_CONV_64_64(evfscfsi);
GEN_SPEFPUOP_CONV_64_64(evfscfuf);
GEN_SPEFPUOP_CONV_64_64(evfscfsf);
GEN_SPEFPUOP_CONV_64_64(evfsctui);
GEN_SPEFPUOP_CONV_64_64(evfsctsi);
GEN_SPEFPUOP_CONV_64_64(evfsctuf);
GEN_SPEFPUOP_CONV_64_64(evfsctsf);
GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
GEN_SPEFPUOP_CONV_64_64(evfsctsiz);

7492
/* Comparison */
A
aurel32 已提交
7493 7494 7495 7496 7497 7498
GEN_SPEFPUOP_COMP_64(evfscmpgt);
GEN_SPEFPUOP_COMP_64(evfscmplt);
GEN_SPEFPUOP_COMP_64(evfscmpeq);
GEN_SPEFPUOP_COMP_64(evfststgt);
GEN_SPEFPUOP_COMP_64(evfststlt);
GEN_SPEFPUOP_COMP_64(evfststeq);
7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517

/* 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 */
A
aurel32 已提交
7518 7519 7520 7521 7522 7523 7524
GEN_SPEFPUOP_ARITH2_32_32(efsadd);
GEN_SPEFPUOP_ARITH2_32_32(efssub);
GEN_SPEFPUOP_ARITH2_32_32(efsmul);
GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
static always_inline void gen_efsabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7525
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7526 7527 7528 7529 7530 7531 7532
        return;
    }
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
}
static always_inline void gen_efsnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7533
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7534 7535 7536 7537 7538 7539 7540
        return;
    }
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
}
static always_inline void gen_efsneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7541
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7542 7543 7544 7545 7546
        return;
    }
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
}

7547
/* Conversion */
A
aurel32 已提交
7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559
GEN_SPEFPUOP_CONV_32_32(efscfui);
GEN_SPEFPUOP_CONV_32_32(efscfsi);
GEN_SPEFPUOP_CONV_32_32(efscfuf);
GEN_SPEFPUOP_CONV_32_32(efscfsf);
GEN_SPEFPUOP_CONV_32_32(efsctui);
GEN_SPEFPUOP_CONV_32_32(efsctsi);
GEN_SPEFPUOP_CONV_32_32(efsctuf);
GEN_SPEFPUOP_CONV_32_32(efsctsf);
GEN_SPEFPUOP_CONV_32_32(efsctuiz);
GEN_SPEFPUOP_CONV_32_32(efsctsiz);
GEN_SPEFPUOP_CONV_32_64(efscfd);

7560
/* Comparison */
A
aurel32 已提交
7561 7562 7563 7564 7565 7566
GEN_SPEFPUOP_COMP_32(efscmpgt);
GEN_SPEFPUOP_COMP_32(efscmplt);
GEN_SPEFPUOP_COMP_32(efscmpeq);
GEN_SPEFPUOP_COMP_32(efststgt);
GEN_SPEFPUOP_COMP_32(efststlt);
GEN_SPEFPUOP_COMP_32(efststeq);
7567 7568

/* Opcodes definitions */
7569
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7570 7571 7572 7573 7574 7575 7576 7577 7578
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); //
7579 7580
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7581 7582 7583 7584 7585
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //

/* Double precision floating-point operations */
/* Arithmetic */
A
aurel32 已提交
7586 7587 7588 7589 7590 7591 7592
GEN_SPEFPUOP_ARITH2_64_64(efdadd);
GEN_SPEFPUOP_ARITH2_64_64(efdsub);
GEN_SPEFPUOP_ARITH2_64_64(efdmul);
GEN_SPEFPUOP_ARITH2_64_64(efddiv);
static always_inline void gen_efdabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7593
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
#else
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
#endif
}
static always_inline void gen_efdnabs (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7605
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
#else
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}
static always_inline void gen_efdneg (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
7617
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7618 7619 7620 7621 7622 7623 7624 7625 7626
        return;
    }
#if defined(TARGET_PPC64)
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
#else
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
#endif
}

7627
/* Conversion */
A
aurel32 已提交
7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642
GEN_SPEFPUOP_CONV_64_32(efdcfui);
GEN_SPEFPUOP_CONV_64_32(efdcfsi);
GEN_SPEFPUOP_CONV_64_32(efdcfuf);
GEN_SPEFPUOP_CONV_64_32(efdcfsf);
GEN_SPEFPUOP_CONV_32_64(efdctui);
GEN_SPEFPUOP_CONV_32_64(efdctsi);
GEN_SPEFPUOP_CONV_32_64(efdctuf);
GEN_SPEFPUOP_CONV_32_64(efdctsf);
GEN_SPEFPUOP_CONV_32_64(efdctuiz);
GEN_SPEFPUOP_CONV_32_64(efdctsiz);
GEN_SPEFPUOP_CONV_64_32(efdcfs);
GEN_SPEFPUOP_CONV_64_64(efdcfuid);
GEN_SPEFPUOP_CONV_64_64(efdcfsid);
GEN_SPEFPUOP_CONV_64_64(efdctuidz);
GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7643 7644

/* Comparison */
A
aurel32 已提交
7645 7646 7647 7648 7649 7650
GEN_SPEFPUOP_COMP_64(efdcmpgt);
GEN_SPEFPUOP_COMP_64(efdcmplt);
GEN_SPEFPUOP_COMP_64(efdcmpeq);
GEN_SPEFPUOP_COMP_64(efdtstgt);
GEN_SPEFPUOP_COMP_64(efdtstlt);
GEN_SPEFPUOP_COMP_64(efdtsteq);
7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669

/* 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 已提交
7670 7671 7672
/* End opcode list */
GEN_OPCODE_MARK(end);

7673
#include "translate_init.c"
7674
#include "helper_regs.h"
B
bellard 已提交
7675

7676
/*****************************************************************************/
7677
/* Misc PowerPC helpers */
7678 7679 7680
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
7681
{
7682 7683 7684
#define RGPL  4
#define RFPL  4

B
bellard 已提交
7685 7686
    int i;

J
j_mayer 已提交
7687
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
A
aurel32 已提交
7688
                env->nip, env->lr, env->ctr, env->xer);
7689 7690
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7691
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
7692
    cpu_fprintf(f, "TB %08x %08x "
7693 7694 7695 7696
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
7697
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7698 7699 7700 7701
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
7702
#endif
7703
    for (i = 0; i < 32; i++) {
7704 7705
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
7706
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7707
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
7708
            cpu_fprintf(f, "\n");
7709
    }
7710
    cpu_fprintf(f, "CR ");
7711
    for (i = 0; i < 8; i++)
B
bellard 已提交
7712 7713
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
7714 7715 7716 7717 7718 7719 7720 7721
    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 已提交
7722
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7723
    }
7724
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7725 7726 7727
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
7728
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7729
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
7730
            cpu_fprintf(f, "\n");
B
bellard 已提交
7731
    }
7732
    cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
7733
#if !defined(CONFIG_USER_ONLY)
7734
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7735
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7736
#endif
B
bellard 已提交
7737

7738 7739
#undef RGPL
#undef RFPL
B
bellard 已提交
7740 7741
}

7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788
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
}

7789
/*****************************************************************************/
7790 7791 7792
static always_inline void gen_intermediate_code_internal (CPUState *env,
                                                          TranslationBlock *tb,
                                                          int search_pc)
B
bellard 已提交
7793
{
7794
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
7795
    opc_handler_t **table, *handler;
B
bellard 已提交
7796
    target_ulong pc_start;
B
bellard 已提交
7797
    uint16_t *gen_opc_end;
7798
    CPUBreakpoint *bp;
B
bellard 已提交
7799
    int j, lj = -1;
P
pbrook 已提交
7800 7801
    int num_insns;
    int max_insns;
B
bellard 已提交
7802 7803 7804

    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7805
    ctx.nip = pc_start;
B
bellard 已提交
7806
    ctx.tb = tb;
7807
    ctx.exception = POWERPC_EXCP_NONE;
7808
    ctx.spr_cb = env->spr_cb;
A
aurel32 已提交
7809 7810 7811
    ctx.mem_idx = env->mmu_idx;
    ctx.access_type = -1;
    ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
7812 7813
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
7814
#endif
B
bellard 已提交
7815
    ctx.fpu_enabled = msr_fp;
7816
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7817 7818 7819
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
7820 7821 7822 7823
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
7824
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7825
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
7826
    else
7827
        ctx.singlestep_enabled = 0;
7828
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7829 7830 7831
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
    if (unlikely(env->singlestep_enabled))
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7832
#if defined (DO_SINGLE_STEP) && 0
7833 7834 7835
    /* Single step trace mode */
    msr_se = 1;
#endif
P
pbrook 已提交
7836 7837 7838 7839 7840 7841
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;

    gen_icount_start();
7842
    /* Set env in case of segfault during code fetch */
7843
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7844 7845
        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7846
                if (bp->pc == ctx.nip) {
A
aurel32 已提交
7847
                    gen_debug_exception(ctxp);
7848 7849 7850 7851
                    break;
                }
            }
        }
7852
        if (unlikely(search_pc)) {
B
bellard 已提交
7853 7854 7855 7856 7857
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
7858
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
7859
                gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
7860
                gen_opc_icount[lj] = num_insns;
B
bellard 已提交
7861 7862
            }
        }
7863 7864
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
7865
            fprintf(logfile, "----------------\n");
7866
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
A
aurel32 已提交
7867
                    ctx.nip, ctx.mem_idx, (int)msr_ir);
7868 7869
        }
#endif
P
pbrook 已提交
7870 7871
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();
A
aurel32 已提交
7872
        if (unlikely(ctx.le_mode)) {
7873 7874 7875
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
7876
        }
7877 7878
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7879
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7880
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7881
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
7882 7883
        }
#endif
B
bellard 已提交
7884
        ctx.nip += 4;
7885
        table = env->opcodes;
P
pbrook 已提交
7886
        num_insns++;
B
bellard 已提交
7887 7888 7889 7890 7891 7892 7893 7894 7895 7896
        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 ? */
7897
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
7898
            if (loglevel != 0) {
7899
                fprintf(logfile, "invalid/unsupported opcode: "
7900
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7901
                        opc1(ctx.opcode), opc2(ctx.opcode),
7902
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
7903 7904
            } else {
                printf("invalid/unsupported opcode: "
7905
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
B
bellard 已提交
7906
                       opc1(ctx.opcode), opc2(ctx.opcode),
7907
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
7908
            }
7909 7910
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
7911
                if (loglevel != 0) {
B
bellard 已提交
7912
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7913
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
B
bellard 已提交
7914 7915
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
7916
                            ctx.opcode, ctx.nip - 4);
7917 7918
                } else {
                    printf("invalid bits: %08x for opcode: "
7919
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
7920 7921
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
7922
                           ctx.opcode, ctx.nip - 4);
7923
                }
A
aurel32 已提交
7924
                gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
B
bellard 已提交
7925
                break;
B
bellard 已提交
7926 7927
            }
        }
B
bellard 已提交
7928
        (*(handler->handler))(&ctx);
7929 7930 7931
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
7932
        /* Check trace mode exceptions */
7933 7934 7935 7936 7937
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
                     ctx.exception != POWERPC_SYSCALL &&
                     ctx.exception != POWERPC_EXCP_TRAP &&
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
A
aurel32 已提交
7938
            gen_exception(ctxp, POWERPC_EXCP_TRACE);
7939
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
P
pbrook 已提交
7940 7941
                            (env->singlestep_enabled) ||
                            num_insns >= max_insns)) {
7942 7943 7944
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
7945
            break;
7946
        }
7947 7948 7949 7950
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
P
pbrook 已提交
7951 7952
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
7953
    if (ctx.exception == POWERPC_EXCP_NONE) {
7954
        gen_goto_tb(&ctx, 0, ctx.nip);
7955
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7956
        if (unlikely(env->singlestep_enabled)) {
A
aurel32 已提交
7957
            gen_debug_exception(ctxp);
7958
        }
7959
        /* Generate the return instruction */
B
bellard 已提交
7960
        tcg_gen_exit_tb(0);
7961
    }
P
pbrook 已提交
7962
    gen_icount_end(tb, num_insns);
B
bellard 已提交
7963
    *gen_opc_ptr = INDEX_op_end;
7964
    if (unlikely(search_pc)) {
7965 7966 7967 7968 7969
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
7970
        tb->size = ctx.nip - pc_start;
P
pbrook 已提交
7971
        tb->icount = num_insns;
7972
    }
7973
#if defined(DEBUG_DISAS)
7974
    if (loglevel & CPU_LOG_TB_CPU) {
7975
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
7976
        cpu_dump_state(env, logfile, fprintf, 0);
7977 7978
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7979
        int flags;
7980
        flags = env->bfd_mach;
A
aurel32 已提交
7981
        flags |= ctx.le_mode << 16;
B
bellard 已提交
7982
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7983
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
7984
        fprintf(logfile, "\n");
7985
    }
B
bellard 已提交
7986 7987 7988
#endif
}

7989
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
7990
{
7991
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
7992 7993
}

7994
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
7995
{
7996
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
7997
}
A
aurel32 已提交
7998 7999 8000 8001 8002 8003

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