translate.c 288.6 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
B
bellard 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 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
44
//#define OPTIMIZE_FPRF_UPDATE
B
bellard 已提交
45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

165 166 167 168
#if defined(OPTIMIZE_FPRF_UPDATE)
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
static uint16_t **gen_fprf_ptr;
#endif
B
bellard 已提交
169 170 171 172

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

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

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

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

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

235
    tcg_temp_free_i32(t0);
236 237 238 239 240 241 242 243 244 245 246 247 248
}

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

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

A
aurel32 已提交
249
static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
A
aurel32 已提交
250
{
A
aurel32 已提交
251 252 253 254
    if (ctx->access_type != access_type) {
        tcg_gen_movi_i32(cpu_access_type, access_type);
        ctx->access_type = access_type;
    }
A
aurel32 已提交
255 256
}

257
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
258 259 260
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
A
aurel32 已提交
261
        tcg_gen_movi_tl(cpu_nip, nip);
262 263
    else
#endif
A
aurel32 已提交
264
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
265 266
}

A
aurel32 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
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);
}
280

A
aurel32 已提交
281 282 283 284 285 286 287 288 289 290 291
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);
}
292

A
aurel32 已提交
293 294 295 296 297 298 299 300
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);
}
301

A
aurel32 已提交
302 303 304 305
static always_inline void gen_inval_exception (DisasContext *ctx, uint32_t error)
{
    gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
}
306

307
/* Stop translation */
A
aurel32 已提交
308
static always_inline void gen_stop_exception (DisasContext *ctx)
309
{
310
    gen_update_nip(ctx, ctx->nip);
311
    ctx->exception = POWERPC_EXCP_STOP;
312 313
}

314
/* No need to update nip here, as execution flow will change */
A
aurel32 已提交
315
static always_inline void gen_sync_exception (DisasContext *ctx)
316
{
317
    ctx->exception = POWERPC_EXCP_SYNC;
318 319
}

B
bellard 已提交
320 321 322 323 324
#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)

325 326 327 328 329
#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 已提交
330 331
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
T
ths 已提交
332
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
333 334 335 336
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
337
    opc_handler_t handler;
338
    const char *oname;
B
bellard 已提交
339 340
} opcode_t;

341
/*****************************************************************************/
B
bellard 已提交
342 343
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
344
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
345 346 347 348 349
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
350
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
351
{                                                                             \
352
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}

/* 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 */
380
EXTRACT_HELPER(_SPR, 11, 10);
381
static always_inline uint32_t SPR (uint32_t opcode)
382 383 384 385 386
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
/***                              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 已提交
401 402
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
403 404 405 406

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

B
bellard 已提交
409 410 411 412
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
413
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
414 415 416 417
{
    return (opcode >> 0) & 0x03FFFFFC;
}

418
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
419 420 421 422 423 424 425 426 427 428 429 430
{
    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 */
431
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
432
{
433
    target_ulong ret;
B
bellard 已提交
434

435 436
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
437
        ret = UINT64_MAX << (63 - end);
438
    } else if (likely(end == 63)) {
439
        ret = UINT64_MAX >> start;
440 441 442
    }
#else
    if (likely(start == 0)) {
443
        ret = UINT32_MAX << (31  - end);
444
    } else if (likely(end == 31)) {
445
        ret = UINT32_MAX >> start;
446 447 448 449 450 451 452 453
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
454 455 456 457

    return ret;
}

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

    /* Fixed-point unit extensions                                           */
    /*   PowerPC 602 specific                                                */
496
    PPC_602_SPEC       = 0x0000000000000400ULL,
497 498 499 500 501 502
    /*   isel instruction                                                    */
    PPC_ISEL           = 0x0000000000000800ULL,
    /*   popcntb instruction                                                 */
    PPC_POPCNTB        = 0x0000000000001000ULL,
    /*   string load / store                                                 */
    PPC_STRING         = 0x0000000000002000ULL,
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

    /* 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                                          */
520
    PPC_SPE            = 0x0000000002000000ULL,
521
    /*   PowerPC 2.03 SPE floating-point extension                           */
522
    PPC_SPEFPU         = 0x0000000004000000ULL,
523

524
    /* Optional memory control instructions                                  */
525 526 527 528 529 530 531 532 533
    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                                            */
534
    PPC_CACHE          = 0x0000000200000000ULL,
535
    /*   icbi instruction                                                    */
536
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
537
    /*   dcbz instruction with fixed cache line size                         */
538
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
539
    /*   dcbz instruction with tunable cache line size                       */
540
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
541
    /*   dcba instruction                                                    */
542 543 544
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
    /*   Freescale cache locking instructions                                */
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
545 546 547

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

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

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

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

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

681 682 683 684 685 686 687 688 689 690 691
/* 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 已提交
692 693 694 695
/* Start opcode list */
GEN_OPCODE_MARK(start);

/* Invalid instruction */
696 697
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
A
aurel32 已提交
698
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
699 700
}

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

707 708
/***                           Integer comparison                          ***/

709
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
710 711 712
{
    int l1, l2, l3;

713 714
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
715 716 717 718 719 720
    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) {
721 722
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
723
    } else {
724 725
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
726 727 728 729 730 731 732 733 734 735 736
    }
    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);
}

737
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
738
{
739 740 741
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp(arg0, t0, s, crf);
    tcg_temp_free(t0);
742 743 744
}

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

762
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
763
{
764 765 766
    TCGv t0 = tcg_const_local_tl(arg1);
    gen_op_cmp32(arg0, t0, s, crf);
    tcg_temp_free(t0);
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
}
#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 已提交
838
    TCGv_i32 t0;
839 840 841 842 843

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

    mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
844
    t0 = tcg_temp_new_i32();
845 846
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
847 848 849 850 851 852 853 854
    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 已提交
855
    tcg_temp_free_i32(t0);
856 857
}

B
bellard 已提交
858 859
/***                           Integer arithmetic                          ***/

860 861 862 863
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 已提交
864

865 866 867
    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 已提交
868
    t0 = tcg_temp_local_new();
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    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 已提交
890 891
}

892 893 894
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
{
    int l1 = gen_new_label();
895 896

#if defined(TARGET_PPC64)
897 898
    if (!(ctx->sf_mode)) {
        TCGv t0, t1;
P
pbrook 已提交
899 900
        t0 = tcg_temp_new();
        t1 = tcg_temp_new();
901

902 903 904 905
        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 已提交
906
        } else {
907 908
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
        }
909 910 911 912
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
        gen_set_label(l1);
        tcg_temp_free(t0);
        tcg_temp_free(t1);
913 914
    } else
#endif
915 916 917 918 919 920 921 922
    {
        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);
923
    }
924 925
}

926 927 928 929 930
/* 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;
931

932
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
933
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
934 935
        t0 = ret;
    } else {
P
pbrook 已提交
936
        t0 = tcg_temp_local_new();
937
    }
B
bellard 已提交
938

939
    if (add_ca) {
P
pbrook 已提交
940
        t1 = tcg_temp_local_new();
941 942 943
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
    }
B
bellard 已提交
944

945 946 947 948 949 950 951 952 953 954
    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 已提交
955

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    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 已提交
973
    if (!TCGV_EQUAL(t0, ret)) {
974 975 976
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
    }
A
aurel32 已提交
977
}
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
/* 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)
1015
{
1016 1017 1018 1019 1020 1021 1022 1023
    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);
    }
1024
}
1025 1026 1027
/* addic  addic.*/
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
                                        int compute_Rc0)
1028
{
1029 1030 1031 1032 1033 1034
    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 已提交
1035
        TCGv t0 = tcg_temp_local_new();
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        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);
    }
1046
}
1047
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1048
{
1049
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1050
}
1051
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1052
{
1053
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1054
}
1055 1056
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1057
{
1058 1059 1060 1061 1062 1063 1064 1065
    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);
    }
1066
}
1067 1068 1069

static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                             int sign, int compute_ov)
1070
{
1071 1072
    int l1 = gen_new_label();
    int l2 = gen_new_label();
P
pbrook 已提交
1073 1074
    TCGv_i32 t0 = tcg_temp_local_new_i32();
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1075

1076 1077 1078
    tcg_gen_trunc_tl_i32(t0, arg1);
    tcg_gen_trunc_tl_i32(t1, arg2);
    tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1079
    if (sign) {
1080 1081 1082
        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);
1083
        gen_set_label(l3);
1084
        tcg_gen_div_i32(t0, t0, t1);
1085
    } else {
1086
        tcg_gen_divu_i32(t0, t0, t1);
1087 1088 1089 1090 1091 1092 1093
    }
    if (compute_ov) {
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
    }
    tcg_gen_br(l2);
    gen_set_label(l1);
    if (sign) {
1094
        tcg_gen_sari_i32(t0, t0, 31);
1095 1096 1097 1098 1099 1100 1101
    } 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);
1102
    tcg_gen_extu_i32_tl(ret, t0);
P
pbrook 已提交
1103 1104
    tcg_temp_free_i32(t0);
    tcg_temp_free_i32(t1);
1105 1106
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, ret);
1107
}
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/* 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);
1122
#if defined(TARGET_PPC64)
1123 1124
static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
                                             int sign, int compute_ov)
1125
{
1126 1127
    int l1 = gen_new_label();
    int l2 = gen_new_label();
1128 1129 1130

    tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
    if (sign) {
1131
        int l3 = gen_new_label();
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        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);
1155
}
1156 1157 1158
#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
{                                                                             \
1159 1160 1161
    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
                      sign, compute_ov);                                      \
1162 1163 1164 1165 1166 1167 1168
}
/* 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);
1169
#endif
1170 1171 1172

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

P
pbrook 已提交
1176 1177
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
#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 已提交
1190 1191
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1192 1193
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1194
}
1195 1196
/* mulhwu  mulhwu.  */
GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1197
{
P
pbrook 已提交
1198
    TCGv_i64 t0, t1;
1199

P
pbrook 已提交
1200 1201
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1202
#if defined(TARGET_PPC64)
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    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 已提交
1214 1215
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1216 1217
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1218
}
1219 1220
/* mullw  mullw. */
GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1221
{
1222 1223
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                   cpu_gpr[rB(ctx->opcode)]);
1224
    tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1225 1226
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1227
}
1228 1229
/* mullwo  mullwo. */
GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1230
{
1231
    int l1;
P
pbrook 已提交
1232
    TCGv_i64 t0, t1;
1233

P
pbrook 已提交
1234 1235
    t0 = tcg_temp_new_i64();
    t1 = tcg_temp_new_i64();
1236 1237 1238 1239 1240 1241 1242 1243 1244
    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)]);
1245
#endif
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    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 已提交
1257 1258
    tcg_temp_free_i64(t0);
    tcg_temp_free_i64(t1);
1259 1260
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1261
}
1262 1263
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1264
{
1265 1266
    tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
                    SIMM(ctx->opcode));
1267 1268
}
#if defined(TARGET_PPC64)
1269 1270 1271
#define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
{                                                                             \
P
pbrook 已提交
1272
    gen_helper_##name (cpu_gpr[rD(ctx->opcode)],                              \
1273 1274 1275
                       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)]);                           \
1276
}
1277 1278 1279 1280 1281 1282
/* 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)
1283
{
1284 1285 1286 1287
    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)]);
1288
}
1289 1290
/* mulldo  mulldo. */
GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1291
#endif
1292 1293

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

/* 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 已提交
1336
{
1337
    TCGv t0, t1;
1338

1339
    if ((!compute_ca && !compute_ov) ||
P
pbrook 已提交
1340
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1341
        t0 = ret;
J
j_mayer 已提交
1342
    } else {
P
pbrook 已提交
1343
        t0 = tcg_temp_local_new();
1344
    }
1345

1346
    if (add_ca) {
P
pbrook 已提交
1347
        t1 = tcg_temp_local_new();
1348 1349
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
        tcg_gen_shri_tl(t1, t1, XER_CA);
1350
    }
B
bellard 已提交
1351

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    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 已提交
1370
    } else {
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
        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 已提交
1383
    if (!TCGV_EQUAL(t0, ret)) {
1384 1385
        tcg_gen_mov_tl(ret, t0);
        tcg_temp_free(t0);
B
bellard 已提交
1386 1387
    }
}
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
/* 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 已提交
1422 1423 1424
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1425 1426
    /* Start with XER CA and OV disabled, the most likely case */
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
P
pbrook 已提交
1427
    TCGv t0 = tcg_temp_local_new();
1428 1429 1430 1431 1432 1433
    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 已提交
1434 1435 1436
}

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

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

/* and & and. */
1455
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
B
bellard 已提交
1456
/* andc & andc. */
1457
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
B
bellard 已提交
1458
/* andi. */
1459
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1460
{
1461 1462
    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 已提交
1463 1464
}
/* andis. */
1465
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1466
{
1467 1468
    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 已提交
1469 1470
}
/* cntlzw */
1471 1472
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
{
P
pbrook 已提交
1473
    gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1474
    if (unlikely(Rc(ctx->opcode) != 0))
P
pbrook 已提交
1475
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1476
}
B
bellard 已提交
1477
/* eqv & eqv. */
1478
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
B
bellard 已提交
1479
/* extsb & extsb. */
1480
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
B
bellard 已提交
1481
/* extsh & extsh. */
1482
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
B
bellard 已提交
1483
/* nand & nand. */
1484
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
B
bellard 已提交
1485
/* nor & nor. */
1486
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
B
bellard 已提交
1487
/* or & or. */
1488 1489
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1490 1491 1492 1493 1494 1495 1496
    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) {
1497 1498 1499 1500
        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]);
1501
        if (unlikely(Rc(ctx->opcode) != 0))
1502
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1503
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1504
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1505 1506
#if defined(TARGET_PPC64)
    } else {
1507 1508
        int prio = 0;

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

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

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

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

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

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

B
bellard 已提交
1644 1645 1646 1647
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1648
    uint32_t mb, me, sh;
B
bellard 已提交
1649 1650 1651

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1652
    sh = SH(ctx->opcode);
1653 1654 1655 1656
    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 已提交
1657 1658
        TCGv t1;
        TCGv t0 = tcg_temp_new();
1659
#if defined(TARGET_PPC64)
P
pbrook 已提交
1660 1661 1662 1663 1664
        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);
1665 1666 1667
#else
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
#endif
1668
#if defined(TARGET_PPC64)
1669 1670
        mb += 32;
        me += 32;
1671
#endif
1672
        mask = MASK(mb, me);
P
pbrook 已提交
1673
        t1 = tcg_temp_new();
1674 1675 1676 1677 1678 1679
        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);
    }
1680
    if (unlikely(Rc(ctx->opcode) != 0))
1681
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
B
bellard 已提交
1682 1683 1684 1685 1686
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1687

B
bellard 已提交
1688 1689 1690
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1691 1692 1693 1694 1695

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

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

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

1800 1801
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1802
{
1803 1804 1805 1806 1807
    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 已提交
1808
        TCGv t0 = tcg_temp_new();
1809
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1810
        if (likely(mb == 0 && me == 63)) {
1811
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1812 1813
        } else {
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
J
j_mayer 已提交
1814
        }
1815
        tcg_temp_free(t0);
J
j_mayer 已提交
1816 1817
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1818
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
J
j_mayer 已提交
1819
}
1820
/* rldicl - rldicl. */
1821
static always_inline void gen_rldicl (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
    gen_rldinm(ctx, mb, 63, sh);
1828
}
J
j_mayer 已提交
1829
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1830
/* rldicr - rldicr. */
1831
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1832
{
J
j_mayer 已提交
1833
    uint32_t sh, me;
1834

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

J
j_mayer 已提交
1845 1846
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1847 1848 1849 1850
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1851 1852
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1853
{
1854
    TCGv t0;
1855 1856 1857

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
P
pbrook 已提交
1858
    t0 = tcg_temp_new();
1859
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1860
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
J
j_mayer 已提交
1861
    if (unlikely(mb != 0 || me != 63)) {
1862 1863 1864 1865 1866
        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 已提交
1867
    if (unlikely(Rc(ctx->opcode) != 0))
1868
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1869
}
J
j_mayer 已提交
1870

1871
/* rldcl - rldcl. */
1872
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1873
{
J
j_mayer 已提交
1874
    uint32_t mb;
1875

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

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

J
j_mayer 已提交
1894 1895
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1896
    me = 63 - sh;
1897 1898 1899 1900 1901 1902
    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 已提交
1903
        t0 = tcg_temp_new();
1904
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
P
pbrook 已提交
1905
        t1 = tcg_temp_new();
1906 1907 1908 1909 1910 1911
        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 已提交
1912 1913
    }
    if (unlikely(Rc(ctx->opcode) != 0))
1914
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1915
}
1916
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1917 1918
#endif

B
bellard 已提交
1919 1920
/***                             Integer shift                             ***/
/* slw & slw. */
1921 1922
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
{
1923
    TCGv t0;
1924 1925 1926 1927
    int l1, l2;
    l1 = gen_new_label();
    l2 = gen_new_label();

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

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

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

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

/***                       Floating-Point arithmetic                       ***/
2088
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2089
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2090
{                                                                             \
2091
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2092
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2093 2094
        return;                                                               \
    }                                                                         \
2095
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2096 2097
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2098
    if (isfloat) {                                                            \
A
aurel32 已提交
2099
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2100
    }                                                                         \
A
aurel32 已提交
2101 2102
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
                     Rc(ctx->opcode) != 0);                                   \
2103 2104
}

2105 2106 2107
#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);
2108

2109 2110
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2111
{                                                                             \
2112
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2113
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2114 2115
        return;                                                               \
    }                                                                         \
2116
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2117 2118
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                     cpu_fpr[rB(ctx->opcode)]);                               \
2119
    if (isfloat) {                                                            \
A
aurel32 已提交
2120
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2121
    }                                                                         \
A
aurel32 已提交
2122 2123
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2124
}
2125 2126 2127
#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);
2128

2129 2130
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2131
{                                                                             \
2132
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2133
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2134 2135
        return;                                                               \
    }                                                                         \
2136
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2137 2138
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
                       cpu_fpr[rC(ctx->opcode)]);                             \
2139
    if (isfloat) {                                                            \
A
aurel32 已提交
2140
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2141
    }                                                                         \
A
aurel32 已提交
2142 2143
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2144
}
2145 2146 2147
#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);
2148

2149
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2150
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2151
{                                                                             \
2152
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2153
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2154 2155
        return;                                                               \
    }                                                                         \
2156
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2157 2158 2159
    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 已提交
2160 2161
}

2162
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2163
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2164
{                                                                             \
2165
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
2166
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
B
bellard 已提交
2167 2168
        return;                                                               \
    }                                                                         \
2169
    gen_reset_fpstatus();                                                     \
A
aurel32 已提交
2170 2171 2172
    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 已提交
2173 2174
}

2175
/* fadd - fadds */
2176
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2177
/* fdiv - fdivs */
2178
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2179
/* fmul - fmuls */
2180
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
2181

2182
/* fre */
2183
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2184

2185
/* fres */
2186
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
2187

2188
/* frsqrte */
2189 2190 2191
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);

/* frsqrtes */
A
aurel32 已提交
2192
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2193
{
A
aurel32 已提交
2194
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2195
        gen_exception(ctx, POWERPC_EXCP_FPU);
A
aurel32 已提交
2196 2197 2198 2199 2200 2201
        return;
    }
    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);
2202
}
B
bellard 已提交
2203

2204
/* fsel */
2205
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2206
/* fsub - fsubs */
2207
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
2208 2209
/* Optional: */
/* fsqrt */
2210
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2211
{
2212
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2213
        gen_exception(ctx, POWERPC_EXCP_FPU);
2214 2215
        return;
    }
2216
    gen_reset_fpstatus();
A
aurel32 已提交
2217 2218
    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);
2219
}
B
bellard 已提交
2220

2221
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
2222
{
2223
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2224
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2225 2226
        return;
    }
2227
    gen_reset_fpstatus();
A
aurel32 已提交
2228 2229 2230
    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 已提交
2231 2232 2233
}

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

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

2259
/* frin */
2260
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2261
/* friz */
2262
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2263
/* frip */
2264
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2265
/* frim */
2266
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2267

B
bellard 已提交
2268 2269
/***                         Floating-Point compare                        ***/
/* fcmpo */
2270
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2271
{
2272
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2273
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2274 2275
        return;
    }
2276
    gen_reset_fpstatus();
A
aurel32 已提交
2277 2278 2279
    gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_float_check_status();
B
bellard 已提交
2280 2281 2282
}

/* fcmpu */
2283
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
2284
{
2285
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2286
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2287 2288
        return;
    }
2289
    gen_reset_fpstatus();
A
aurel32 已提交
2290 2291 2292
    gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
    gen_helper_float_check_status();
B
bellard 已提交
2293 2294
}

2295 2296
/***                         Floating-point move                           ***/
/* fabs */
2297 2298
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2299 2300

/* fmr  - fmr. */
2301
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2302 2303
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
2304
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2305
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2306 2307
        return;
    }
A
aurel32 已提交
2308 2309
    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);
2310 2311 2312
}

/* fnabs */
2313 2314
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2315
/* fneg */
2316 2317
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2318

B
bellard 已提交
2319 2320 2321 2322
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
2323 2324
    int bfa;

2325
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2326
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2327 2328
        return;
    }
2329 2330
    gen_optimize_fprf();
    bfa = 4 * (7 - crfS(ctx->opcode));
2331 2332
    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 已提交
2333
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
B
bellard 已提交
2334 2335 2336 2337 2338
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
2339
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2340
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2341 2342
        return;
    }
2343 2344
    gen_optimize_fprf();
    gen_reset_fpstatus();
A
aurel32 已提交
2345 2346
    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 已提交
2347 2348 2349 2350 2351
}

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

2354
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2355
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2356 2357
        return;
    }
2358 2359 2360 2361
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    if (likely(crb != 30 && crb != 29))
A
aurel32 已提交
2362
        tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
2363
    if (unlikely(Rc(ctx->opcode) != 0)) {
2364
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2365
    }
B
bellard 已提交
2366 2367 2368 2369 2370
}

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

2373
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2374
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2375 2376
        return;
    }
2377 2378 2379 2380
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    /* XXX: we pretend we can only do IEEE floating-point computations */
A
aurel32 已提交
2381
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2382
        TCGv_i32 t0 = tcg_const_i32(crb);
A
aurel32 已提交
2383
        gen_helper_fpscr_setbit(t0);
2384
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2385
    }
2386
    if (unlikely(Rc(ctx->opcode) != 0)) {
2387
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2388 2389
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2390
    gen_helper_float_check_status();
B
bellard 已提交
2391 2392 2393 2394 2395
}

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

2398
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2399
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2400 2401
        return;
    }
2402 2403
    gen_optimize_fprf();
    gen_reset_fpstatus();
A
aurel32 已提交
2404 2405
    t0 = tcg_const_i32(FM(ctx->opcode));
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2406
    tcg_temp_free_i32(t0);
2407
    if (unlikely(Rc(ctx->opcode) != 0)) {
2408
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2409 2410
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2411
    gen_helper_float_check_status();
B
bellard 已提交
2412 2413 2414 2415 2416
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
2417
    int bf, sh;
2418 2419
    TCGv_i64 t0;
    TCGv_i32 t1;
2420

2421
    if (unlikely(!ctx->fpu_enabled)) {
A
aurel32 已提交
2422
        gen_exception(ctx, POWERPC_EXCP_FPU);
B
bellard 已提交
2423 2424
        return;
    }
2425 2426 2427 2428
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
    gen_optimize_fprf();
    gen_reset_fpstatus();
2429
    t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
A
aurel32 已提交
2430 2431
    t1 = tcg_const_i32(1 << sh);
    gen_helper_store_fpscr(t0, t1);
2432 2433
    tcg_temp_free_i64(t0);
    tcg_temp_free_i32(t1);
2434
    if (unlikely(Rc(ctx->opcode) != 0)) {
2435
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2436 2437
    }
    /* We can raise a differed exception */
A
aurel32 已提交
2438
    gen_helper_float_check_status();
B
bellard 已提交
2439 2440
}

2441 2442
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
A
aurel32 已提交
2443
static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2444 2445 2446
{
    target_long simm = SIMM(ctx->opcode);

2447
    simm &= ~maskl;
A
aurel32 已提交
2448 2449 2450 2451 2452 2453
    if (rA(ctx->opcode) == 0) {
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_movi_tl(EA, (uint32_t)simm);
        } else
#endif
2454
        tcg_gen_movi_tl(EA, simm);
A
aurel32 已提交
2455
    } else if (likely(simm != 0)) {
2456
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
A
aurel32 已提交
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
#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
2468
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
A
aurel32 已提交
2469
    }
2470 2471
}

A
aurel32 已提交
2472
static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2473
{
A
aurel32 已提交
2474 2475 2476 2477 2478 2479
    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
2480
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2481
    } else {
2482
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
A
aurel32 已提交
2483 2484 2485 2486 2487 2488
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
2489 2490
}

A
aurel32 已提交
2491
static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2492
{
A
aurel32 已提交
2493
    if (rA(ctx->opcode) == 0) {
2494
        tcg_gen_movi_tl(EA, 0);
A
aurel32 已提交
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
    } 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
2513 2514
}

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
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);
}

2533
/***                             Integer load                              ***/
A
aurel32 已提交
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
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 已提交
2548
#if defined(TARGET_PPC64)
A
aurel32 已提交
2549 2550
        TCGv_i32 t0 = tcg_temp_new_i32();
        tcg_gen_trunc_tl_i32(t0, arg1);
2551
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2552
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2553
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2554 2555 2556 2557
#else
        tcg_gen_bswap16_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2558 2559
}

A
aurel32 已提交
2560
static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2561
{
A
aurel32 已提交
2562 2563
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2564
        TCGv_i32 t0;
A
aurel32 已提交
2565
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
P
pbrook 已提交
2566
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2567
        tcg_gen_trunc_tl_i32(t0, arg1);
2568
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2569 2570
        tcg_gen_extu_i32_tl(arg1, t0);
        tcg_gen_ext16s_tl(arg1, arg1);
P
pbrook 已提交
2571
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2572 2573 2574 2575 2576 2577 2578 2579
#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 已提交
2580 2581
}

A
aurel32 已提交
2582
static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2583
{
A
aurel32 已提交
2584 2585 2586 2587 2588
    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);
2589
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2590
        tcg_gen_extu_i32_tl(arg1, t0);
P
pbrook 已提交
2591
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2592 2593 2594 2595
#else
        tcg_gen_bswap_i32(arg1, arg1);
#endif
    }
A
aurel32 已提交
2596 2597
}

A
aurel32 已提交
2598 2599
#if defined(TARGET_PPC64)
static always_inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2600
{
A
aurel32 已提交
2601
    if (unlikely(ctx->mem_idx)) {
P
pbrook 已提交
2602
        TCGv_i32 t0;
A
aurel32 已提交
2603
        tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
P
pbrook 已提交
2604
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2605
        tcg_gen_trunc_tl_i32(t0, arg1);
2606
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2607
        tcg_gen_ext_i32_tl(arg1, t0);
P
pbrook 已提交
2608
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2609
    } else
A
aurel32 已提交
2610
        tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2611
}
A
aurel32 已提交
2612
#endif
A
aurel32 已提交
2613

A
aurel32 已提交
2614
static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2615
{
A
aurel32 已提交
2616 2617 2618 2619
    tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
    if (unlikely(ctx->le_mode)) {
        tcg_gen_bswap_i64(arg1, arg1);
    }
A
aurel32 已提交
2620 2621
}

A
aurel32 已提交
2622
static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2623
{
A
aurel32 已提交
2624
    tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2625 2626
}

A
aurel32 已提交
2627
static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2628
{
A
aurel32 已提交
2629 2630
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2631
        TCGv_i32 t0;
A
aurel32 已提交
2632
        TCGv t1;
P
pbrook 已提交
2633
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2634
        tcg_gen_trunc_tl_i32(t0, arg1);
2635 2636
        tcg_gen_ext16u_i32(t0, t0);
        tcg_gen_bswap16_i32(t0, t0);
A
aurel32 已提交
2637
        t1 = tcg_temp_new();
2638
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2639
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
        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 已提交
2652 2653
}

A
aurel32 已提交
2654
static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2655
{
A
aurel32 已提交
2656 2657
    if (unlikely(ctx->le_mode)) {
#if defined(TARGET_PPC64)
P
pbrook 已提交
2658
        TCGv_i32 t0;
A
aurel32 已提交
2659
        TCGv t1;
P
pbrook 已提交
2660
        t0 = tcg_temp_new_i32();
A
aurel32 已提交
2661
        tcg_gen_trunc_tl_i32(t0, arg1);
2662
        tcg_gen_bswap_i32(t0, t0);
A
aurel32 已提交
2663
        t1 = tcg_temp_new();
2664
        tcg_gen_extu_i32_tl(t1, t0);
P
pbrook 已提交
2665
        tcg_temp_free_i32(t0);
A
aurel32 已提交
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
        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 已提交
2677 2678
}

A
aurel32 已提交
2679
static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
A
aurel32 已提交
2680
{
A
aurel32 已提交
2681
    if (unlikely(ctx->le_mode)) {
P
pbrook 已提交
2682
        TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
2683 2684
        tcg_gen_bswap_i64(t0, arg1);
        tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
P
pbrook 已提交
2685
        tcg_temp_free_i64(t0);
A
aurel32 已提交
2686
    } else
A
aurel32 已提交
2687
        tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
A
aurel32 已提交
2688 2689
}

2690 2691
#define GEN_LD(name, ldop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2692
{                                                                             \
A
aurel32 已提交
2693 2694 2695 2696 2697
    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 已提交
2698
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2699 2700
}

2701 2702
#define GEN_LDU(name, ldop, opc, type)                                        \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2703
{                                                                             \
A
aurel32 已提交
2704
    TCGv EA;                                                                  \
2705 2706
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2707
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2708
        return;                                                               \
2709
    }                                                                         \
A
aurel32 已提交
2710
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2711
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2712
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2713
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2714
    else                                                                      \
A
aurel32 已提交
2715 2716
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2717 2718
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2719 2720
}

2721 2722
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2723
{                                                                             \
A
aurel32 已提交
2724
    TCGv EA;                                                                  \
2725 2726
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
A
aurel32 已提交
2727
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2728
        return;                                                               \
2729
    }                                                                         \
A
aurel32 已提交
2730
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2731
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2732 2733
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
A
aurel32 已提交
2734 2735
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2736 2737
}

2738 2739
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2740
{                                                                             \
A
aurel32 已提交
2741 2742 2743 2744 2745
    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 已提交
2746
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2747 2748
}

2749 2750 2751 2752 2753
#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 已提交
2754 2755

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

    /* Restore CPU state */
A
aurel32 已提交
2806
    if (unlikely(ctx->mem_idx == 0)) {
A
aurel32 已提交
2807
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2808 2809 2810 2811 2812
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
A
aurel32 已提交
2813
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2814 2815
        return;
    }
A
aurel32 已提交
2816
    if (unlikely(ctx->le_mode)) {
2817
        /* Little-endian mode is not handled */
A
aurel32 已提交
2818
        gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2819 2820
        return;
    }
A
aurel32 已提交
2821
    gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2822
    EA = tcg_temp_new();
A
aurel32 已提交
2823 2824 2825 2826
    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 已提交
2827
    tcg_temp_free(EA);
2828 2829
#endif
}
2830
#endif
B
bellard 已提交
2831 2832

/***                              Integer store                            ***/
2833 2834
#define GEN_ST(name, stop, opc, type)                                         \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
2835
{                                                                             \
A
aurel32 已提交
2836 2837 2838 2839 2840
    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 已提交
2841
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2842 2843
}

2844 2845
#define GEN_STU(name, stop, opc, type)                                        \
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
2846
{                                                                             \
A
aurel32 已提交
2847
    TCGv EA;                                                                  \
2848
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2849
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2850
        return;                                                               \
2851
    }                                                                         \
A
aurel32 已提交
2852
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2853
    EA = tcg_temp_new();                                                      \
J
j_mayer 已提交
2854
    if (type == PPC_64B)                                                      \
A
aurel32 已提交
2855
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
J
j_mayer 已提交
2856
    else                                                                      \
A
aurel32 已提交
2857 2858
        gen_addr_imm_index(ctx, EA, 0);                                       \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2859 2860
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2861 2862
}

2863 2864
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
B
bellard 已提交
2865
{                                                                             \
A
aurel32 已提交
2866
    TCGv EA;                                                                  \
2867
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
2868
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2869
        return;                                                               \
2870
    }                                                                         \
A
aurel32 已提交
2871
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2872
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
2873 2874
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
A
aurel32 已提交
2875 2876
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2877 2878
}

2879 2880
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
2881
{                                                                             \
A
aurel32 已提交
2882 2883 2884 2885 2886
    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 已提交
2887
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
2888 2889
}

2890 2891 2892 2893 2894
#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 已提交
2895 2896

/* stb stbu stbux stbx */
2897
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
B
bellard 已提交
2898
/* sth sthu sthux sthx */
2899
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
B
bellard 已提交
2900
/* stw stwu stwux stwx */
2901
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2902
#if defined(TARGET_PPC64)
2903 2904
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2905
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2906
{
2907
    int rs;
A
aurel32 已提交
2908
    TCGv EA;
2909 2910 2911 2912

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
2913
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2914 2915
#else
        /* stq */
A
aurel32 已提交
2916
        if (unlikely(ctx->mem_idx == 0)) {
A
aurel32 已提交
2917
            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2918 2919 2920
            return;
        }
        if (unlikely(rs & 1)) {
A
aurel32 已提交
2921
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2922 2923
            return;
        }
A
aurel32 已提交
2924
        if (unlikely(ctx->le_mode)) {
2925
            /* Little-endian mode is not handled */
A
aurel32 已提交
2926
            gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2927 2928
            return;
        }
A
aurel32 已提交
2929
        gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2930
        EA = tcg_temp_new();
A
aurel32 已提交
2931 2932 2933 2934
        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 已提交
2935
        tcg_temp_free(EA);
2936 2937 2938 2939 2940
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
A
aurel32 已提交
2941
                gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2942 2943 2944
                return;
            }
        }
A
aurel32 已提交
2945
        gen_set_access_type(ctx, ACCESS_INT);
P
pbrook 已提交
2946
        EA = tcg_temp_new();
A
aurel32 已提交
2947 2948
        gen_addr_imm_index(ctx, EA, 0x03);
        gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2949
        if (Rc(ctx->opcode))
A
aurel32 已提交
2950 2951
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
        tcg_temp_free(EA);
2952 2953 2954
    }
}
#endif
B
bellard 已提交
2955 2956
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
A
aurel32 已提交
2957
static void always_inline gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2958
{
A
aurel32 已提交
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
    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 已提交
2971
}
2972
GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
A
aurel32 已提交
2973

B
bellard 已提交
2974
/* lwbrx */
A
aurel32 已提交
2975
static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2976
{
A
aurel32 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
    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 已提交
2989
}
2990
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
A
aurel32 已提交
2991

B
bellard 已提交
2992
/* sthbrx */
A
aurel32 已提交
2993
static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
2994
{
A
aurel32 已提交
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
    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 已提交
3018
}
3019
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
A
aurel32 已提交
3020

B
bellard 已提交
3021
/* stwbrx */
A
aurel32 已提交
3022
static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
A
aurel32 已提交
3023
{
A
aurel32 已提交
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
    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 已提交
3045
}
3046
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
3047 3048 3049 3050 3051

/***                    Integer load and store multiple                    ***/
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
3052 3053 3054
    TCGv t0;
    TCGv_i32 t1;
    gen_set_access_type(ctx, ACCESS_INT);
3055
    /* NIP cannot be restored if the memory exception comes from an helper */
3056
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3057 3058 3059
    t0 = tcg_temp_new();
    t1 = tcg_const_i32(rD(ctx->opcode));
    gen_addr_imm_index(ctx, t0, 0);
3060 3061 3062
    gen_helper_lmw(t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3063 3064 3065 3066 3067
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
A
aurel32 已提交
3068 3069 3070
    TCGv t0;
    TCGv_i32 t1;
    gen_set_access_type(ctx, ACCESS_INT);
3071
    /* NIP cannot be restored if the memory exception comes from an helper */
3072
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3073 3074 3075
    t0 = tcg_temp_new();
    t1 = tcg_const_i32(rS(ctx->opcode));
    gen_addr_imm_index(ctx, t0, 0);
3076 3077 3078
    gen_helper_stmw(t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3079 3080 3081 3082
}

/***                    Integer load and store strings                     ***/
/* lswi */
3083
/* PowerPC32 specification says we must generate an exception if
3084 3085 3086 3087
 * 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...
 */
3088
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
B
bellard 已提交
3089
{
3090 3091
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3092 3093
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
3094
    int ra = rA(ctx->opcode);
B
bellard 已提交
3095 3096 3097 3098 3099
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
3100 3101 3102
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
A
aurel32 已提交
3103
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3104
        return;
B
bellard 已提交
3105
    }
A
aurel32 已提交
3106
    gen_set_access_type(ctx, ACCESS_INT);
3107
    /* NIP cannot be restored if the memory exception comes from an helper */
3108
    gen_update_nip(ctx, ctx->nip - 4);
3109
    t0 = tcg_temp_new();
A
aurel32 已提交
3110
    gen_addr_register(ctx, t0);
3111 3112 3113 3114 3115 3116
    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 已提交
3117 3118 3119
}

/* lswx */
3120
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
B
bellard 已提交
3121
{
A
aurel32 已提交
3122 3123 3124
    TCGv t0;
    TCGv_i32 t1, t2, t3;
    gen_set_access_type(ctx, ACCESS_INT);
3125
    /* NIP cannot be restored if the memory exception comes from an helper */
3126
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3127 3128 3129 3130 3131
    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));
3132 3133 3134 3135 3136
    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 已提交
3137 3138 3139
}

/* stswi */
3140
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
B
bellard 已提交
3141
{
A
aurel32 已提交
3142 3143
    TCGv t0;
    TCGv_i32 t1, t2;
B
bellard 已提交
3144
    int nb = NB(ctx->opcode);
A
aurel32 已提交
3145
    gen_set_access_type(ctx, ACCESS_INT);
3146
    /* NIP cannot be restored if the memory exception comes from an helper */
3147
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3148 3149
    t0 = tcg_temp_new();
    gen_addr_register(ctx, t0);
B
bellard 已提交
3150 3151
    if (nb == 0)
        nb = 32;
3152
    t1 = tcg_const_i32(nb);
A
aurel32 已提交
3153
    t2 = tcg_const_i32(rS(ctx->opcode));
3154 3155 3156 3157
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3158 3159 3160
}

/* stswx */
3161
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
B
bellard 已提交
3162
{
A
aurel32 已提交
3163 3164 3165
    TCGv t0;
    TCGv_i32 t1, t2;
    gen_set_access_type(ctx, ACCESS_INT);
3166
    /* NIP cannot be restored if the memory exception comes from an helper */
3167
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
3168 3169 3170
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
    t1 = tcg_temp_new_i32();
3171 3172
    tcg_gen_trunc_tl_i32(t1, cpu_xer);
    tcg_gen_andi_i32(t1, t1, 0x7F);
A
aurel32 已提交
3173
    t2 = tcg_const_i32(rS(ctx->opcode));
3174 3175 3176 3177
    gen_helper_stsw(t0, t1, t2);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
    tcg_temp_free_i32(t2);
B
bellard 已提交
3178 3179 3180 3181
}

/***                        Memory synchronisation                         ***/
/* eieio */
3182
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
3183 3184 3185 3186
{
}

/* isync */
3187
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
3188
{
A
aurel32 已提交
3189
    gen_stop_exception(ctx);
B
bellard 已提交
3190 3191
}

3192
/* lwarx */
3193
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
3194
{
A
aurel32 已提交
3195 3196 3197 3198
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3199
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
3200
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3201 3202
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
B
bellard 已提交
3203 3204 3205
}

/* stwcx. */
3206
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
3207
{
A
aurel32 已提交
3208 3209 3210 3211 3212
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3213 3214 3215 3216
    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 已提交
3217
    l1 = gen_new_label();
3218 3219
    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 已提交
3220
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3221 3222 3223
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
B
bellard 已提交
3224 3225
}

J
j_mayer 已提交
3226 3227
#if defined(TARGET_PPC64)
/* ldarx */
3228
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
3229
{
A
aurel32 已提交
3230 3231 3232 3233
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3234
    gen_check_align(ctx, t0, 0x07);
A
aurel32 已提交
3235
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3236 3237
    tcg_gen_mov_tl(cpu_reserve, t0);
    tcg_temp_free(t0);
J
j_mayer 已提交
3238 3239 3240
}

/* stdcx. */
3241
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
3242
{
A
aurel32 已提交
3243 3244 3245 3246 3247
    int l1;
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_RES);
    t0 = tcg_temp_local_new();
    gen_addr_reg_index(ctx, t0);
3248 3249 3250 3251
    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 已提交
3252
    l1 = gen_new_label();
3253 3254
    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 已提交
3255
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3256 3257 3258
    gen_set_label(l1);
    tcg_gen_movi_tl(cpu_reserve, -1);
    tcg_temp_free(t0);
J
j_mayer 已提交
3259 3260 3261
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
3262
/* sync */
3263
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
3264 3265 3266
{
}

3267 3268 3269
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
3270 3271 3272
    TCGv_i32 t0 = tcg_temp_new_i32();
    tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
    tcg_temp_free_i32(t0);
3273
    /* Stop translation, as the CPU is supposed to sleep from now */
A
aurel32 已提交
3274
    gen_exception_err(ctx, EXCP_HLT, 1);
3275 3276
}

B
bellard 已提交
3277
/***                         Floating-point load                           ***/
3278 3279
#define GEN_LDF(name, ldop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3280
{                                                                             \
3281
    TCGv EA;                                                                  \
3282
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3283
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3284 3285
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3286
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3287
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3288 3289
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3290
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3291 3292
}

3293 3294
#define GEN_LDUF(name, ldop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3295
{                                                                             \
3296
    TCGv EA;                                                                  \
3297
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3298
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3299 3300
        return;                                                               \
    }                                                                         \
3301
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3302
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3303
        return;                                                               \
3304
    }                                                                         \
A
aurel32 已提交
3305
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3306
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3307 3308
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3309 3310
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3311 3312
}

3313 3314
#define GEN_LDUXF(name, ldop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3315
{                                                                             \
3316
    TCGv EA;                                                                  \
3317
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3318
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3319 3320
        return;                                                               \
    }                                                                         \
3321
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3322
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3323
        return;                                                               \
3324
    }                                                                         \
A
aurel32 已提交
3325
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3326
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3327 3328
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3329 3330
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3331 3332
}

3333 3334
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3335
{                                                                             \
3336
    TCGv EA;                                                                  \
3337
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3338
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3339 3340
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3341
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3342
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3343 3344
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3345
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3346 3347
}

3348 3349 3350 3351 3352 3353
#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 已提交
3354
static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3355 3356 3357
{
    TCGv t0 = tcg_temp_new();
    TCGv_i32 t1 = tcg_temp_new_i32();
A
aurel32 已提交
3358
    gen_qemu_ld32u(ctx, t0, arg2);
3359 3360 3361 3362 3363
    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 已提交
3364

3365 3366 3367 3368
 /* lfd lfdu lfdux lfdx */
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
 /* lfs lfsu lfsux lfsx */
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
B
bellard 已提交
3369 3370

/***                         Floating-point store                          ***/
3371 3372
#define GEN_STF(name, stop, opc, type)                                        \
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
B
bellard 已提交
3373
{                                                                             \
3374
    TCGv EA;                                                                  \
3375
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3376
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3377 3378
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3379
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3380
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3381 3382
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3383
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3384 3385
}

3386 3387
#define GEN_STUF(name, stop, opc, type)                                       \
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
B
bellard 已提交
3388
{                                                                             \
3389
    TCGv EA;                                                                  \
3390
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3391
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3392 3393
        return;                                                               \
    }                                                                         \
3394
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3395
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3396
        return;                                                               \
3397
    }                                                                         \
A
aurel32 已提交
3398
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3399
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3400 3401
    gen_addr_imm_index(ctx, EA, 0);                                           \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3402 3403
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3404 3405
}

3406 3407
#define GEN_STUXF(name, stop, opc, type)                                      \
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
B
bellard 已提交
3408
{                                                                             \
3409
    TCGv EA;                                                                  \
3410
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3411
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3412 3413
        return;                                                               \
    }                                                                         \
3414
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
A
aurel32 已提交
3415
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3416
        return;                                                               \
3417
    }                                                                         \
A
aurel32 已提交
3418
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3419
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3420 3421
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3422 3423
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3424 3425
}

3426 3427
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
B
bellard 已提交
3428
{                                                                             \
3429
    TCGv EA;                                                                  \
3430
    if (unlikely(!ctx->fpu_enabled)) {                                        \
A
aurel32 已提交
3431
        gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3432 3433
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
3434
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3435
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
3436 3437
    gen_addr_reg_index(ctx, EA);                                              \
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3438
    tcg_temp_free(EA);                                                        \
B
bellard 已提交
3439 3440
}

3441 3442 3443 3444 3445 3446
#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 已提交
3447
static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3448 3449 3450 3451 3452 3453
{
    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 已提交
3454
    gen_qemu_st32(ctx, t1, arg2);
3455 3456
    tcg_temp_free(t1);
}
B
bellard 已提交
3457 3458

/* stfd stfdu stfdux stfdx */
3459
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
B
bellard 已提交
3460
/* stfs stfsu stfsux stfsx */
3461
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3462 3463

/* Optional: */
A
aurel32 已提交
3464
static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3465 3466 3467
{
    TCGv t0 = tcg_temp_new();
    tcg_gen_trunc_i64_tl(t0, arg1),
A
aurel32 已提交
3468
    gen_qemu_st32(ctx, t0, arg2);
3469 3470
    tcg_temp_free(t0);
}
B
bellard 已提交
3471
/* stfiwx */
3472
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3473 3474

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

3508
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3509 3510
{
#if defined(TARGET_PPC64)
3511 3512
    if (ctx->sf_mode == 0)
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3513 3514
    else
#endif
3515
        tcg_gen_movi_tl(cpu_lr, nip);
3516 3517
}

B
bellard 已提交
3518 3519 3520
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3521
    target_ulong li, target;
B
bellard 已提交
3522

3523
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3524
    /* sign extend LI */
3525
#if defined(TARGET_PPC64)
3526 3527 3528
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3529
#endif
3530
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3531
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3532
        target = ctx->nip + li - 4;
B
bellard 已提交
3533
    else
3534
        target = li;
3535 3536
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3537
    gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3538 3539
}

3540 3541 3542 3543
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3544
static always_inline void gen_bcond (DisasContext *ctx, int type)
3545 3546
{
    uint32_t bo = BO(ctx->opcode);
3547 3548
    int l1 = gen_new_label();
    TCGv target;
3549

3550
    ctx->exception = POWERPC_EXCP_BRANCH;
3551
    if (type == BCOND_LR || type == BCOND_CTR) {
P
pbrook 已提交
3552
        target = tcg_temp_local_new();
3553 3554 3555 3556
        if (type == BCOND_CTR)
            tcg_gen_mov_tl(target, cpu_ctr);
        else
            tcg_gen_mov_tl(target, cpu_lr);
3557
    }
3558 3559
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3560 3561 3562
    l1 = gen_new_label();
    if ((bo & 0x4) == 0) {
        /* Decrement and test CTR */
P
pbrook 已提交
3563
        TCGv temp = tcg_temp_new();
3564
        if (unlikely(type == BCOND_CTR)) {
A
aurel32 已提交
3565
            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3566 3567 3568
            return;
        }
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3569
#if defined(TARGET_PPC64)
3570 3571 3572
        if (!ctx->sf_mode)
            tcg_gen_ext32u_tl(temp, cpu_ctr);
        else
3573
#endif
3574 3575 3576 3577 3578
            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);
3579
        }
P
pbrook 已提交
3580
        tcg_temp_free(temp);
3581 3582 3583 3584 3585
    }
    if ((bo & 0x10) == 0) {
        /* Test CR */
        uint32_t bi = BI(ctx->opcode);
        uint32_t mask = 1 << (3 - (bi & 0x03));
P
pbrook 已提交
3586
        TCGv_i32 temp = tcg_temp_new_i32();
3587

3588
        if (bo & 0x8) {
3589 3590
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3591
        } else {
3592 3593
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3594
        }
P
pbrook 已提交
3595
        tcg_temp_free_i32(temp);
3596
    }
3597
    if (type == BCOND_IM) {
3598 3599 3600 3601 3602 3603
        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 已提交
3604
        gen_set_label(l1);
3605
        gen_goto_tb(ctx, 1, ctx->nip);
3606
    } else {
3607
#if defined(TARGET_PPC64)
3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
        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);
3618 3619
        else
#endif
3620
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
B
bellard 已提交
3621
        tcg_gen_exit_tb(0);
J
j_mayer 已提交
3622
    }
3623 3624 3625
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3626
{
3627 3628 3629 3630
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3631
{
3632 3633 3634 3635
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3636
{
3637 3638
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3639 3640

/***                      Condition register logical                       ***/
3641 3642
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
B
bellard 已提交
3643
{                                                                             \
3644 3645
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
P
pbrook 已提交
3646
    TCGv_i32 t0, t1;                                                          \
3647
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
P
pbrook 已提交
3648
    t0 = tcg_temp_new_i32();                                                  \
3649
    if (sh > 0)                                                               \
3650
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3651
    else if (sh < 0)                                                          \
3652
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3653
    else                                                                      \
3654
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
P
pbrook 已提交
3655
    t1 = tcg_temp_new_i32();                                                  \
3656 3657
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
3658
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3659
    else if (sh < 0)                                                          \
3660
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3661
    else                                                                      \
3662 3663
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
    tcg_op(t0, t0, t1);                                                       \
3664
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3665 3666 3667
    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 已提交
3668 3669
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
B
bellard 已提交
3670 3671 3672
}

/* crand */
3673
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
B
bellard 已提交
3674
/* crandc */
3675
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
B
bellard 已提交
3676
/* creqv */
3677
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
B
bellard 已提交
3678
/* crnand */
3679
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
B
bellard 已提交
3680
/* crnor */
3681
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
B
bellard 已提交
3682
/* cror */
3683
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
B
bellard 已提交
3684
/* crorc */
3685
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
B
bellard 已提交
3686
/* crxor */
3687
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
B
bellard 已提交
3688 3689 3690
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
A
aurel32 已提交
3691
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
B
bellard 已提交
3692 3693 3694
}

/***                           System linkage                              ***/
A
aurel32 已提交
3695
/* rfi (mem_idx only) */
3696
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3697
{
3698
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3699
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3700 3701
#else
    /* Restore CPU state */
A
aurel32 已提交
3702
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3703
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3704
        return;
3705
    }
3706
    gen_helper_rfi();
A
aurel32 已提交
3707
    gen_sync_exception(ctx);
3708
#endif
B
bellard 已提交
3709 3710
}

J
j_mayer 已提交
3711
#if defined(TARGET_PPC64)
3712
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3713 3714
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3715
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3716 3717
#else
    /* Restore CPU state */
A
aurel32 已提交
3718
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3719
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
3720 3721
        return;
    }
3722
    gen_helper_rfid();
A
aurel32 已提交
3723
    gen_sync_exception(ctx);
J
j_mayer 已提交
3724 3725 3726
#endif
}

J
j_mayer 已提交
3727
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3728 3729
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3730
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3731 3732
#else
    /* Restore CPU state */
A
aurel32 已提交
3733
    if (unlikely(ctx->mem_idx <= 1)) {
A
aurel32 已提交
3734
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3735 3736
        return;
    }
3737
    gen_helper_hrfid();
A
aurel32 已提交
3738
    gen_sync_exception(ctx);
3739 3740 3741 3742
#endif
}
#endif

B
bellard 已提交
3743
/* sc */
3744 3745 3746 3747 3748
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3749
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3750
{
3751 3752 3753
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
A
aurel32 已提交
3754
    gen_exception_err(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3755 3756 3757 3758
}

/***                                Trap                                   ***/
/* tw */
3759
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3760
{
3761
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3762
    /* Update the nip since this might generate a trap exception */
3763
    gen_update_nip(ctx, ctx->nip);
3764 3765
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
B
bellard 已提交
3766 3767 3768 3769 3770
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3771 3772
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3773 3774
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3775 3776 3777
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
B
bellard 已提交
3778 3779
}

3780 3781 3782 3783
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
3784
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3785 3786
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3787 3788
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
3789 3790 3791 3792 3793
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
3794 3795
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
    TCGv_i32 t1 = 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 3800
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
    tcg_temp_free(t0);
    tcg_temp_free_i32(t1);
3801 3802 3803
}
#endif

B
bellard 已提交
3804 3805 3806 3807
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
A
aurel32 已提交
3808 3809
    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);
3810
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
B
bellard 已提交
3811 3812 3813
}

/* mfcr */
3814
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3815
{
3816
    uint32_t crm, crn;
3817

3818 3819 3820 3821
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
3822
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3823
        }
3824
    } else {
P
pbrook 已提交
3825
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3826
    }
B
bellard 已提交
3827 3828 3829 3830 3831
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3832
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3833
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3834
#else
A
aurel32 已提交
3835
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3836
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3837
        return;
3838
    }
3839
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3840
#endif
B
bellard 已提交
3841 3842
}

J
j_mayer 已提交
3843
#if 1
3844
#define SPR_NOACCESS ((void *)(-1UL))
3845 3846 3847 3848 3849 3850 3851 3852 3853
#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 已提交
3854
/* mfspr */
3855
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3856
{
A
aurel32 已提交
3857
    void (*read_cb)(void *opaque, int gprn, int sprn);
B
bellard 已提交
3858 3859
    uint32_t sprn = SPR(ctx->opcode);

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

3899
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3900
{
3901
    gen_op_mfspr(ctx);
3902
}
3903 3904

/* mftb */
3905
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3906 3907
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3908 3909 3910
}

/* mtcrf */
3911
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3912
{
3913
    uint32_t crm, crn;
3914

3915 3916
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
P
pbrook 已提交
3917
        TCGv_i32 temp = tcg_temp_new_i32();
3918
        crn = ffs(crm);
P
pbrook 已提交
3919 3920
        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
        tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3921
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
P
pbrook 已提交
3922
        tcg_temp_free_i32(temp);
3923
    } else {
P
pbrook 已提交
3924 3925 3926
        TCGv_i32 temp = tcg_const_i32(crm);
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
        tcg_temp_free_i32(temp);
3927
    }
B
bellard 已提交
3928 3929 3930
}

/* mtmsr */
J
j_mayer 已提交
3931
#if defined(TARGET_PPC64)
3932
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3933 3934
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3935
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3936
#else
A
aurel32 已提交
3937
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3938
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
J
j_mayer 已提交
3939 3940
        return;
    }
3941 3942
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
3943 3944 3945 3946 3947
        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);
3948
    } else {
3949 3950 3951 3952
        /* 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
         */
3953
        gen_update_nip(ctx, ctx->nip);
3954
        gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3955 3956
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
3957
        gen_stop_exception(ctx);
3958
    }
J
j_mayer 已提交
3959 3960 3961 3962
#endif
}
#endif

B
bellard 已提交
3963 3964
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3965
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
3966
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3967
#else
A
aurel32 已提交
3968
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
3969
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3970
        return;
3971
    }
3972 3973
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
3974 3975 3976 3977 3978
        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);
3979
    } else {
3980 3981 3982 3983
        /* 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
         */
3984
        gen_update_nip(ctx, ctx->nip);
3985
#if defined(TARGET_PPC64)
3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
        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
3996
#endif
3997
            gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3998
        /* Must stop the translation as machine state (may have) changed */
3999
        /* Note that mtmsr is not always defined as context-synchronizing */
A
aurel32 已提交
4000
        gen_stop_exception(ctx);
4001
    }
4002
#endif
B
bellard 已提交
4003 4004 4005 4006 4007
}

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

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

/***                         Cache management                              ***/
/* dcbf */
4046
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
4047
{
J
j_mayer 已提交
4048
    /* XXX: specification says this is treated as a load by the MMU */
A
aurel32 已提交
4049 4050 4051 4052 4053
    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);
4054
    tcg_temp_free(t0);
B
bellard 已提交
4055 4056 4057
}

/* dcbi (Supervisor only) */
4058
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4059
{
4060
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4061
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4062
#else
A
aurel32 已提交
4063
    TCGv EA, val;
A
aurel32 已提交
4064
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4065
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4066
        return;
4067
    }
P
pbrook 已提交
4068
    EA = tcg_temp_new();
A
aurel32 已提交
4069 4070
    gen_set_access_type(ctx, ACCESS_CACHE);
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
4071
    val = tcg_temp_new();
4072
    /* XXX: specification says this should be treated as a store by the MMU */
A
aurel32 已提交
4073 4074
    gen_qemu_ld8u(ctx, val, EA);
    gen_qemu_st8(ctx, val, EA);
A
aurel32 已提交
4075 4076
    tcg_temp_free(val);
    tcg_temp_free(EA);
4077
#endif
B
bellard 已提交
4078 4079 4080
}

/* dcdst */
4081
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
4082
{
4083
    /* XXX: specification say this is treated as a load by the MMU */
A
aurel32 已提交
4084 4085 4086 4087 4088
    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);
4089
    tcg_temp_free(t0);
B
bellard 已提交
4090 4091 4092
}

/* dcbt */
4093
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
4094
{
4095
    /* interpreted as no-op */
4096 4097 4098
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4099 4100 4101
}

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

/* dcbz */
4111
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
4112
{
A
aurel32 已提交
4113 4114
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4115 4116
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4117 4118
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4119 4120
    gen_helper_dcbz(t0);
    tcg_temp_free(t0);
4121 4122
}

4123
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4124
{
A
aurel32 已提交
4125 4126
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4127 4128
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4129 4130
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4131
    if (ctx->opcode & 0x00200000)
4132
        gen_helper_dcbz(t0);
4133
    else
4134 4135
        gen_helper_dcbz_970(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4136 4137 4138
}

/* icbi */
4139
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
4140
{
A
aurel32 已提交
4141 4142
    TCGv t0;
    gen_set_access_type(ctx, ACCESS_CACHE);
4143 4144
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
A
aurel32 已提交
4145 4146
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4147 4148
    gen_helper_icbi(t0);
    tcg_temp_free(t0);
B
bellard 已提交
4149 4150 4151 4152
}

/* Optional: */
/* dcba */
4153
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
4154
{
4155 4156 4157 4158
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
4159 4160 4161 4162 4163 4164 4165
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
4166
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4167
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4168
#else
4169
    TCGv t0;
A
aurel32 已提交
4170
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4171
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4172
        return;
4173
    }
4174 4175 4176
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4177
#endif
B
bellard 已提交
4178 4179 4180
}

/* mfsrin */
4181
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4182
{
4183
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4184
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4185
#else
4186
    TCGv t0;
A
aurel32 已提交
4187
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4188
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4189
        return;
4190
    }
4191 4192 4193 4194 4195
    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);
4196
#endif
B
bellard 已提交
4197 4198 4199
}

/* mtsr */
B
bellard 已提交
4200
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
4201
{
4202
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4203
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4204
#else
4205
    TCGv t0;
A
aurel32 已提交
4206
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4207
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4208
        return;
4209
    }
4210 4211 4212
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4213
#endif
B
bellard 已提交
4214 4215 4216
}

/* mtsrin */
4217
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
4218
{
4219
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4220
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4221
#else
4222
    TCGv t0;
A
aurel32 已提交
4223
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4224
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4225
        return;
4226
    }
4227 4228 4229 4230 4231
    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);
4232
#endif
B
bellard 已提交
4233 4234
}

4235 4236 4237
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
4238
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4239 4240
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4241
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4242
#else
4243
    TCGv t0;
A
aurel32 已提交
4244
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4245
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4246 4247
        return;
    }
4248 4249 4250
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
4251 4252 4253 4254
#endif
}

/* mfsrin */
4255 4256
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
4257 4258
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4259
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4260
#else
4261
    TCGv t0;
A
aurel32 已提交
4262
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4263
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4264 4265
        return;
    }
4266 4267 4268 4269 4270
    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);
4271 4272 4273 4274
#endif
}

/* mtsr */
4275
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4276 4277
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4278
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4279
#else
4280
    TCGv t0;
A
aurel32 已提交
4281
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4282
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4283 4284
        return;
    }
4285 4286 4287
    t0 = tcg_const_tl(SR(ctx->opcode));
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
    tcg_temp_free(t0);
4288 4289 4290 4291
#endif
}

/* mtsrin */
4292 4293
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
4294 4295
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4296
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4297
#else
4298
    TCGv t0;
A
aurel32 已提交
4299
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4300
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4301 4302
        return;
    }
4303 4304 4305 4306 4307
    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);
4308 4309 4310 4311
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4312
/***                      Lookaside buffer management                      ***/
A
aurel32 已提交
4313
/* Optional & mem_idx only: */
B
bellard 已提交
4314
/* tlbia */
4315
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4316
{
4317
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4318
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4319
#else
A
aurel32 已提交
4320
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4321
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4322
        return;
4323
    }
4324
    gen_helper_tlbia();
4325
#endif
B
bellard 已提交
4326 4327 4328
}

/* tlbie */
4329
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4330
{
4331
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4332
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4333
#else
A
aurel32 已提交
4334
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4335
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4336
        return;
4337
    }
4338
#if defined(TARGET_PPC64)
4339 4340 4341 4342 4343 4344
    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
4345
#endif
4346
        gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4347
#endif
B
bellard 已提交
4348 4349 4350
}

/* tlbsync */
4351
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4352
{
4353
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4354
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4355
#else
A
aurel32 已提交
4356
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4357
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4358
        return;
4359 4360 4361 4362
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
A
aurel32 已提交
4363
    gen_stop_exception(ctx);
4364
#endif
B
bellard 已提交
4365 4366
}

J
j_mayer 已提交
4367 4368 4369 4370 4371
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4372
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4373
#else
A
aurel32 已提交
4374
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4375
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4376 4377
        return;
    }
4378
    gen_helper_slbia();
J
j_mayer 已提交
4379 4380 4381 4382 4383 4384 4385
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
4386
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4387
#else
A
aurel32 已提交
4388
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
4389
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
J
j_mayer 已提交
4390 4391
        return;
    }
4392
    gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
J
j_mayer 已提交
4393 4394 4395 4396
#endif
}
#endif

B
bellard 已提交
4397 4398
/***                              External control                         ***/
/* Optional: */
4399
/* eciwx */
B
bellard 已提交
4400 4401
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4402
    TCGv t0;
4403
    /* Should check EAR[E] ! */
A
aurel32 已提交
4404 4405 4406
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4407
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4408
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4409
    tcg_temp_free(t0);
4410 4411 4412 4413 4414
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
A
aurel32 已提交
4415
    TCGv t0;
4416
    /* Should check EAR[E] ! */
A
aurel32 已提交
4417 4418 4419
    gen_set_access_type(ctx, ACCESS_EXT);
    t0 = tcg_temp_new();
    gen_addr_reg_index(ctx, t0);
4420
    gen_check_align(ctx, t0, 0x03);
A
aurel32 已提交
4421
    gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4422
    tcg_temp_free(t0);
4423 4424 4425 4426 4427 4428
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
4429 4430 4431 4432 4433 4434 4435 4436
    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);
4437
    if (unlikely(Rc(ctx->opcode) != 0))
4438
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4439 4440 4441 4442 4443
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458
    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);
4459
    if (unlikely(Rc(ctx->opcode) != 0))
4460
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4461 4462 4463
}

/* clcs */
4464
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4465
{
4466 4467 4468
    TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
    gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free_i32(t0);
4469
    /* Rc=1 sets CR0 to an undefined state */
4470 4471 4472 4473 4474
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
4475
    gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4476
    if (unlikely(Rc(ctx->opcode) != 0))
4477
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4478 4479 4480 4481 4482
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
4483
    gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4484
    if (unlikely(Rc(ctx->opcode) != 0))
4485
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4486 4487 4488 4489 4490
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
4491
    gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4492
    if (unlikely(Rc(ctx->opcode) != 0))
4493
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4494 4495 4496 4497 4498
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
4499
    gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4500
    if (unlikely(Rc(ctx->opcode) != 0))
4501
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4502 4503 4504 4505 4506
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
4507 4508 4509 4510 4511 4512 4513 4514
    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);
4515
    if (unlikely(Rc(ctx->opcode) != 0))
4516
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4517 4518 4519 4520 4521
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543
    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);
4544
    if (unlikely(Rc(ctx->opcode) != 0))
4545
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4546 4547 4548 4549 4550
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561
    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)]);
4562 4563 4564 4565 4566
}

/* lscbx - lscbx. */
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
{
4567 4568 4569 4570
    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));
4571

A
aurel32 已提交
4572
    gen_addr_reg_index(ctx, t0);
4573
    /* NIP cannot be restored if the memory exception comes from an helper */
4574
    gen_update_nip(ctx, ctx->nip - 4);
4575 4576 4577 4578
    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 已提交
4579
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4580
    tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4581
    if (unlikely(Rc(ctx->opcode) != 0))
4582 4583
        gen_set_Rc0(ctx, t0);
    tcg_temp_free(t0);
4584 4585 4586 4587 4588
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
    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);
4608
    if (unlikely(Rc(ctx->opcode) != 0))
4609
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4610 4611 4612 4613 4614
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
4615 4616 4617 4618 4619 4620 4621
    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);
4622
    if (unlikely(Rc(ctx->opcode) != 0))
4623
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4624 4625 4626 4627 4628
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641
    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);
4642
    if (unlikely(Rc(ctx->opcode) != 0))
4643
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4644 4645 4646 4647 4648
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668
    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);
4669
    if (unlikely(Rc(ctx->opcode) != 0))
4670
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4671 4672 4673 4674 4675
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
4676 4677 4678 4679 4680 4681 4682 4683
    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);
4684
    if (unlikely(Rc(ctx->opcode) != 0))
4685
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4686 4687 4688 4689 4690
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700
    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));
4701
    if (unlikely(Rc(ctx->opcode) != 0))
4702
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4703 4704 4705 4706 4707
}

/* rlmi - rlmi. */
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
4708 4709 4710 4711 4712 4713 4714 4715 4716
    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);
4717
    if (unlikely(Rc(ctx->opcode) != 0))
4718
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4719 4720 4721 4722 4723
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
    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);
4735
    if (unlikely(Rc(ctx->opcode) != 0))
4736
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4737 4738 4739 4740 4741
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752
    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);
4753
    if (unlikely(Rc(ctx->opcode) != 0))
4754
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4755 4756 4757 4758 4759
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774
    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);
4775
    if (unlikely(Rc(ctx->opcode) != 0))
4776
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4777 4778 4779 4780 4781
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
    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);
4792
    if (unlikely(Rc(ctx->opcode) != 0))
4793
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4794 4795 4796 4797 4798
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809
    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);
4810
    if (unlikely(Rc(ctx->opcode) != 0))
4811
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4812 4813 4814 4815 4816
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838
    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);
4839
    if (unlikely(Rc(ctx->opcode) != 0))
4840
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4841 4842 4843 4844 4845
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861
    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);
4862
    if (unlikely(Rc(ctx->opcode) != 0))
4863
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4864 4865
}

4866
/* sraiq - sraiq. */
4867 4868
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884
    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);
4885
    if (unlikely(Rc(ctx->opcode) != 0))
4886
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4887 4888 4889 4890 4891
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917
    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);
4918
    if (unlikely(Rc(ctx->opcode) != 0))
4919
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4920 4921 4922 4923 4924
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935
    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);
4936
    if (unlikely(Rc(ctx->opcode) != 0))
4937
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4938 4939 4940 4941 4942
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
4943 4944 4945 4946 4947 4948 4949 4950
    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);
4951
    if (unlikely(Rc(ctx->opcode) != 0))
4952
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4953 4954 4955 4956 4957
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972
    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);
4973
    if (unlikely(Rc(ctx->opcode) != 0))
4974
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4975 4976 4977 4978 4979
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
    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);
4990
    if (unlikely(Rc(ctx->opcode) != 0))
4991
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4992 4993 4994 4995 4996
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007
    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);
5008
    if (unlikely(Rc(ctx->opcode) != 0))
5009
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5010 5011 5012 5013 5014
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037
    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);
5038
    if (unlikely(Rc(ctx->opcode) != 0))
5039
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5040 5041 5042 5043 5044
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060
    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);
5061
    if (unlikely(Rc(ctx->opcode) != 0))
5062
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5063 5064 5065 5066 5067 5068 5069
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5070
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5071 5072 5073 5074 5075 5076
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
A
aurel32 已提交
5077
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5078 5079 5080 5081 5082 5083
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5084
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5085
#else
A
aurel32 已提交
5086
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5087
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5088 5089
        return;
    }
5090
    gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5091 5092 5093 5094 5095
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
5096
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5097 5098
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5099
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5100
#else
A
aurel32 已提交
5101
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5102
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5103 5104
        return;
    }
5105
    gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5106 5107 5108 5109
#endif
}

/* tlbli */
5110
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5111 5112
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5113
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5114
#else
A
aurel32 已提交
5115
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5116
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5117 5118
        return;
    }
5119
    gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5120 5121 5122
#endif
}

5123 5124
/* 74xx TLB management */
/* tlbld */
5125
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5126 5127
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5128
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5129
#else
A
aurel32 已提交
5130
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5131
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5132 5133
        return;
    }
5134
    gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5135 5136 5137 5138
#endif
}

/* tlbli */
5139
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5140 5141
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5142
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5143
#else
A
aurel32 已提交
5144
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5145
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5146 5147
        return;
    }
5148
    gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5149 5150 5151
#endif
}

5152 5153 5154 5155 5156 5157 5158 5159 5160 5161
/* 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 已提交
5162
    /* Cache line invalidate: privileged and treated as no-op */
5163
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5164
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5165
#else
A
aurel32 已提交
5166
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5167
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181
        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 已提交
5182
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5183
#else
5184 5185 5186
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);
    TCGv t0;
A
aurel32 已提交
5187
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5188
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5189 5190
        return;
    }
5191
    t0 = tcg_temp_new();
A
aurel32 已提交
5192
    gen_addr_reg_index(ctx, t0);
5193 5194 5195 5196
    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);
5197
    if (ra != 0 && ra != rd)
5198
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5199 5200 5201 5202 5203 5204
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5205
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5206
#else
5207
    TCGv t0;
A
aurel32 已提交
5208
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5209
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5210 5211
        return;
    }
5212
    t0 = tcg_temp_new();
A
aurel32 已提交
5213
    gen_addr_reg_index(ctx, t0);
5214 5215
    gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
5216 5217 5218 5219 5220 5221
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5222
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5223
#else
A
aurel32 已提交
5224
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5225
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5226 5227
        return;
    }
5228
    gen_helper_rfsvc();
A
aurel32 已提交
5229
    gen_sync_exception(ctx);
5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240
#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)
{
5241
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5242 5243 5244 5245 5246 5247 5248
    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);
5249
    tcg_temp_free(t0);
5250 5251 5252 5253 5254 5255
}

/* lfqu */
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5256
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5257 5258 5259 5260 5261 5262 5263 5264
    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);
5265
    if (ra != 0)
5266 5267 5268
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5269 5270 5271 5272 5273 5274
}

/* lfqux */
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5275
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5276 5277 5278 5279 5280 5281 5282 5283 5284
    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);
5285
    if (ra != 0)
5286 5287
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5288 5289 5290 5291 5292
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
5293
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5294 5295 5296 5297 5298 5299 5300
    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);
5301
    tcg_temp_free(t0);
5302 5303 5304 5305 5306
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
5307
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5308 5309 5310 5311 5312 5313 5314
    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);
5315
    tcg_temp_free(t0);
5316 5317 5318 5319 5320 5321
}

/* stfqu */
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5322
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5323 5324 5325 5326 5327 5328 5329 5330 5331
    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);
5332
    if (ra != 0)
5333 5334
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5335 5336 5337 5338 5339 5340
}

/* stfqux */
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);
5341
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5342 5343 5344 5345 5346 5347 5348 5349 5350
    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);
5351
    if (ra != 0)
5352 5353
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
    tcg_temp_free(t0);
5354 5355 5356 5357 5358
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
5359
    int rd = rD(ctx->opcode);
A
aurel32 已提交
5360 5361 5362 5363 5364 5365 5366
    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);
5367
    tcg_temp_free(t0);
5368 5369 5370
}

/* BookE specific instructions */
5371
/* XXX: not implemented on 440 ? */
5372
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5373 5374
{
    /* XXX: TODO */
A
aurel32 已提交
5375
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5376 5377
}

5378
/* XXX: not implemented on 440 ? */
5379
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5380 5381
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5382
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5383
#else
5384
    TCGv t0;
A
aurel32 已提交
5385
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5386
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5387 5388
        return;
    }
5389
    t0 = tcg_temp_new();
A
aurel32 已提交
5390
    gen_addr_reg_index(ctx, t0);
5391 5392
    gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
    tcg_temp_free(t0);
5393 5394 5395 5396
#endif
}

/* All 405 MAC instructions are translated here */
5397 5398 5399
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
5400
{
5401 5402
    TCGv t0, t1;

P
pbrook 已提交
5403 5404
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
5405

5406 5407 5408 5409 5410 5411 5412
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
5413 5414 5415
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16s_tl(t1, t1);
5416 5417 5418 5419 5420
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
5421 5422 5423
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
        tcg_gen_ext16u_tl(t1, t1);
5424 5425 5426 5427 5428 5429 5430
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
5431 5432 5433 5434
        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);
5435 5436 5437 5438 5439
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
5440 5441 5442 5443
        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);
5444 5445 5446 5447 5448 5449 5450
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
5451 5452
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5453 5454 5455 5456 5457
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
5458 5459
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5460 5461 5462
        break;
    }
    if (opc2 & 0x04) {
5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486
        /* (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 已提交
5487
                if (opc3 & 0x02) {
5488 5489 5490 5491 5492 5493 5494
                    /* 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 已提交
5495
                if (opc3 & 0x02) {
5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508
                    /* 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);
5509
    }
5510 5511
    tcg_temp_free(t0);
    tcg_temp_free(t1);
5512 5513
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
5514
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5515 5516 5517
    }
}

5518 5519
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5520 5521 5522 5523 5524 5525
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

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

/* mulchw  - mulchw.  */
5599
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5600
/* mulchwu - mulchwu. */
5601
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5602
/* mulhhw  - mulhhw.  */
5603
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5604
/* mulhhwu - mulhhwu. */
5605
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5606
/* mullhw  - mullhw.  */
5607
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5608
/* mullhwu - mullhwu. */
5609
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5610 5611

/* mfdcr */
5612
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5613 5614
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5615
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5616
#else
5617
    TCGv dcrn;
A
aurel32 已提交
5618
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5619
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5620 5621
        return;
    }
5622 5623 5624 5625 5626
    /* 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);
5627 5628 5629 5630
#endif
}

/* mtdcr */
5631
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5632 5633
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5634
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5635
#else
5636
    TCGv dcrn;
A
aurel32 已提交
5637
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5638
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5639 5640
        return;
    }
5641 5642 5643 5644 5645
    /* 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);
5646 5647 5648 5649
#endif
}

/* mfdcrx */
5650
/* XXX: not implemented on 440 ? */
5651
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5652 5653
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5654
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5655
#else
A
aurel32 已提交
5656
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5657
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5658 5659
        return;
    }
5660 5661 5662
    /* 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)]);
5663
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5664 5665 5666 5667
#endif
}

/* mtdcrx */
5668
/* XXX: not implemented on 440 ? */
5669
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5670 5671
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5672
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5673
#else
A
aurel32 已提交
5674
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5675
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5676 5677
        return;
    }
5678 5679 5680
    /* 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)]);
5681
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5682 5683 5684
#endif
}

5685 5686 5687
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
5688 5689 5690
    /* 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)]);
5691 5692 5693 5694 5695 5696
    /* 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)
{
5697 5698 5699
    /* 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)]);
5700 5701 5702
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

5703 5704 5705 5706
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5707
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5708
#else
A
aurel32 已提交
5709
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5710
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5711 5712 5713 5714 5715 5716 5717 5718 5719 5720
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5721
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5722
#else
A
aurel32 已提交
5723
    TCGv EA, val;
A
aurel32 已提交
5724
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5725
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5726 5727
        return;
    }
A
aurel32 已提交
5728
    gen_set_access_type(ctx, ACCESS_CACHE);
P
pbrook 已提交
5729
    EA = tcg_temp_new();
A
aurel32 已提交
5730
    gen_addr_reg_index(ctx, EA);
P
pbrook 已提交
5731
    val = tcg_temp_new();
A
aurel32 已提交
5732
    gen_qemu_ld32u(ctx, val, EA);
A
aurel32 已提交
5733 5734 5735
    tcg_temp_free(val);
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
    tcg_temp_free(EA);
5736 5737 5738 5739
#endif
}

/* icbt */
5740
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751
{
    /* 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 已提交
5752
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5753
#else
A
aurel32 已提交
5754
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5755
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5756 5757 5758 5759 5760 5761 5762 5763 5764 5765
        return;
    }
    /* interpreted as no-op */
#endif
}

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

A
aurel32 已提交
5776
/* rfci (mem_idx only) */
5777
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5778 5779
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5780
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5781
#else
A
aurel32 已提交
5782
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5783
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5784 5785 5786
        return;
    }
    /* Restore CPU state */
5787
    gen_helper_40x_rfci();
A
aurel32 已提交
5788
    gen_sync_exception(ctx);
5789 5790 5791 5792 5793 5794
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5795
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5796
#else
A
aurel32 已提交
5797
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5798
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5799 5800 5801
        return;
    }
    /* Restore CPU state */
5802
    gen_helper_rfci();
A
aurel32 已提交
5803
    gen_sync_exception(ctx);
5804 5805 5806 5807
#endif
}

/* BookE specific */
5808
/* XXX: not implemented on 440 ? */
5809
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5810 5811
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5812
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5813
#else
A
aurel32 已提交
5814
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5815
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5816 5817 5818
        return;
    }
    /* Restore CPU state */
5819
    gen_helper_rfdi();
A
aurel32 已提交
5820
    gen_sync_exception(ctx);
5821 5822 5823
#endif
}

5824
/* XXX: not implemented on 440 ? */
5825
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5826 5827
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5828
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5829
#else
A
aurel32 已提交
5830
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5831
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5832 5833 5834
        return;
    }
    /* Restore CPU state */
5835
    gen_helper_rfmci();
A
aurel32 已提交
5836
    gen_sync_exception(ctx);
5837 5838
#endif
}
5839

5840
/* TLB management - PowerPC 405 implementation */
5841
/* tlbre */
5842
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5843 5844
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5845
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5846
#else
A
aurel32 已提交
5847
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5848
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5849 5850 5851 5852
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5853
        gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5854 5855
        break;
    case 1:
5856
        gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5857 5858
        break;
    default:
A
aurel32 已提交
5859
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5860
        break;
5861
    }
5862 5863 5864
#endif
}

5865
/* tlbsx - tlbsx. */
5866
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5867 5868
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5869
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5870
#else
5871
    TCGv t0;
A
aurel32 已提交
5872
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5873
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5874 5875
        return;
    }
5876
    t0 = tcg_temp_new();
A
aurel32 已提交
5877
    gen_addr_reg_index(ctx, t0);
5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888
    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);
    }
5889
#endif
B
bellard 已提交
5890 5891
}

5892
/* tlbwe */
5893
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5894
{
5895
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5896
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5897
#else
A
aurel32 已提交
5898
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5899
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5900 5901 5902 5903
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5904
        gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5905 5906
        break;
    case 1:
5907
        gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5908 5909
        break;
    default:
A
aurel32 已提交
5910
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5911
        break;
5912
    }
5913 5914 5915
#endif
}

5916
/* TLB management - PowerPC 440 implementation */
5917
/* tlbre */
5918
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5919 5920
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5921
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5922
#else
A
aurel32 已提交
5923
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5924
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5925 5926 5927 5928 5929 5930
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
5931 5932 5933 5934 5935
        {
            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);
        }
5936 5937
        break;
    default:
A
aurel32 已提交
5938
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5939 5940 5941 5942 5943 5944
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5945
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5946 5947
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5948
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5949
#else
5950
    TCGv t0;
A
aurel32 已提交
5951
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5952
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5953 5954
        return;
    }
5955
    t0 = tcg_temp_new();
A
aurel32 已提交
5956
    gen_addr_reg_index(ctx, t0);
5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967
    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);
    }
5968 5969 5970 5971
#endif
}

/* tlbwe */
5972
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5973 5974
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
5975
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5976
#else
A
aurel32 已提交
5977
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
5978
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5979 5980 5981 5982 5983 5984
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
5985 5986 5987 5988 5989
        {
            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);
        }
5990 5991
        break;
    default:
A
aurel32 已提交
5992
        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5993 5994 5995 5996 5997
        break;
    }
#endif
}

5998
/* wrtee */
5999
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
6000 6001
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6002
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6003
#else
6004
    TCGv t0;
A
aurel32 已提交
6005
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6006
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6007 6008
        return;
    }
6009 6010 6011 6012 6013
    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 已提交
6014 6015 6016
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
A
aurel32 已提交
6017
    gen_stop_exception(ctx);
6018 6019 6020 6021
#endif
}

/* wrteei */
6022
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6023 6024
{
#if defined(CONFIG_USER_ONLY)
A
aurel32 已提交
6025
    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6026
#else
A
aurel32 已提交
6027
    if (unlikely(!ctx->mem_idx)) {
A
aurel32 已提交
6028
        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6029 6030
        return;
    }
6031 6032 6033
    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 已提交
6034
        gen_stop_exception(ctx);
6035 6036 6037
    } else {
        tcg_gen_andi_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
    }
6038 6039 6040
#endif
}

J
j_mayer 已提交
6041
/* PowerPC 440 specific instructions */
6042 6043 6044
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
6045 6046 6047 6048
    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);
6049 6050 6051 6052 6053 6054 6055 6056 6057
}

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

/* msync replaces sync on 440 */
6058
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6059 6060 6061 6062 6063
{
    /* interpreted as no-op */
}

/* icbt */
6064
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6065 6066 6067 6068 6069
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
6070 6071
}

6072 6073 6074 6075
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */

#define GEN_VR_LDX(name, opc2, opc3)                                          \
6076
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
6077
{                                                                             \
6078
    TCGv EA;                                                                  \
6079
    if (unlikely(!ctx->altivec_enabled)) {                                    \
A
aurel32 已提交
6080
        gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6081 6082
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
6083
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6084
    EA = tcg_temp_new();                                                      \
A
aurel32 已提交
6085
    gen_addr_reg_index(ctx, EA);                                              \
6086
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
A
aurel32 已提交
6087 6088
    if (ctx->le_mode) {                                                       \
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6089
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6090
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6091
    } else {                                                                  \
A
aurel32 已提交
6092
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6093
        tcg_gen_addi_tl(EA, EA, 8);                                           \
A
aurel32 已提交
6094
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6095 6096
    }                                                                         \
    tcg_temp_free(EA);                                                        \
6097 6098 6099 6100 6101
}

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

6123
GEN_VR_LDX(lvx, 0x07, 0x03);
6124
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6125
GEN_VR_LDX(lvxl, 0x07, 0x0B);
6126

6127
GEN_VR_STX(svx, 0x07, 0x07);
6128
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6129
GEN_VR_STX(svxl, 0x07, 0x0F);
6130

6131 6132
/***                           SPE extension                               ***/
/* Register moves */
6133

P
pbrook 已提交
6134
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
A
aurel32 已提交
6135 6136 6137
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
#else
P
pbrook 已提交
6138
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6139
#endif
A
aurel32 已提交
6140
}
6141

P
pbrook 已提交
6142
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
A
aurel32 已提交
6143 6144 6145
#if defined(TARGET_PPC64)
    tcg_gen_mov_i64(cpu_gpr[reg], t);
#else
P
pbrook 已提交
6146
    TCGv_i64 tmp = tcg_temp_new_i64();
A
aurel32 已提交
6147 6148 6149
    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 已提交
6150
    tcg_temp_free_i64(tmp);
6151
#endif
A
aurel32 已提交
6152
}
6153

6154 6155 6156 6157 6158 6159 6160 6161 6162 6163
#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 */
6164
static always_inline void gen_speundef (DisasContext *ctx)
6165
{
A
aurel32 已提交
6166
    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6167 6168
}

6169 6170 6171
/* SPE logic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6172
static always_inline void gen_##name (DisasContext *ctx)                      \
6173 6174
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6175
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6176 6177
        return;                                                               \
    }                                                                         \
6178 6179 6180 6181 6182 6183 6184 6185
    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 已提交
6186
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6187 6188 6189 6190 6191 6192
        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)]);                                        \
6193
}
6194 6195 6196 6197 6198 6199 6200 6201 6202 6203
#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);
6204

6205 6206 6207
/* SPE logic immediate */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6208 6209 6210
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6211
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6212 6213
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6214 6215 6216
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6217 6218 6219 6220
    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 已提交
6221
    tcg_temp_free_i64(t2);                                                    \
6222 6223
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6224 6225
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6226
}
6227 6228
#else
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6229
static always_inline void gen_##name (DisasContext *ctx)                      \
6230 6231
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6232
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6233 6234
        return;                                                               \
    }                                                                         \
6235 6236 6237 6238
    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));                                                 \
6239
}
6240 6241 6242 6243 6244
#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);
6245

6246 6247 6248
/* SPE arithmetic */
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6249
static always_inline void gen_##name (DisasContext *ctx)                      \
6250 6251
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6252
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6253 6254
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6255 6256 6257
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6258 6259 6260 6261
    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 已提交
6262
    tcg_temp_free_i64(t2);                                                    \
6263 6264
    tcg_op(t1, t1);                                                           \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6265 6266
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6267
}
6268
#else
P
pbrook 已提交
6269
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6270 6271 6272
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6273
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6274 6275 6276 6277 6278 6279
        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
6280

P
pbrook 已提交
6281
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6282 6283 6284
{
    int l1 = gen_new_label();
    int l2 = gen_new_label();
6285

6286 6287 6288 6289
    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 已提交
6290
    tcg_gen_mov_i32(ret, arg1);
6291 6292 6293 6294 6295 6296
    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 已提交
6297
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6298
{
6299 6300 6301 6302
    tcg_gen_addi_i32(ret, arg1, 0x8000);
    tcg_gen_ext16u_i32(ret, ret);
}
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
P
pbrook 已提交
6303 6304
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6305

6306 6307 6308
#if defined(TARGET_PPC64)
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6309 6310
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6311
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6312 6313
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6314 6315 6316 6317
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64);                           \
6318 6319 6320 6321 6322 6323 6324
    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 已提交
6325
    tcg_temp_free_i64(t3);                                                    \
6326
    tcg_op(t1, t1, t2);                                                       \
P
pbrook 已提交
6327
    tcg_temp_free_i32(t2);                                                    \
6328
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6329 6330
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6331
}
6332 6333 6334
#else
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
6335 6336
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6337
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6338 6339
        return;                                                               \
    }                                                                         \
6340 6341 6342 6343
    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)]);                                        \
6344
}
6345
#endif
6346

P
pbrook 已提交
6347
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6348
{
P
pbrook 已提交
6349
    TCGv_i32 t0;
6350
    int l1, l2;
6351

6352 6353
    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6354
    t0 = tcg_temp_local_new_i32();
6355 6356 6357 6358 6359 6360 6361 6362
    /* 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 已提交
6363
    tcg_temp_free_i32(t0);
6364 6365
}
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
P
pbrook 已提交
6366
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6367
{
P
pbrook 已提交
6368
    TCGv_i32 t0;
6369 6370 6371 6372
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6373
    t0 = tcg_temp_local_new_i32();
6374 6375 6376 6377 6378 6379 6380 6381
    /* 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 已提交
6382
    tcg_temp_free_i32(t0);
6383 6384
}
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
P
pbrook 已提交
6385
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6386
{
P
pbrook 已提交
6387
    TCGv_i32 t0;
6388 6389 6390 6391
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
P
pbrook 已提交
6392
    t0 = tcg_temp_local_new_i32();
6393 6394 6395 6396 6397 6398 6399 6400
    /* 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 已提交
6401
    tcg_temp_free_i32(t0);
6402 6403
}
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
P
pbrook 已提交
6404
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6405
{
P
pbrook 已提交
6406
    TCGv_i32 t0 = tcg_temp_new_i32();
6407 6408
    tcg_gen_andi_i32(t0, arg2, 0x1F);
    tcg_gen_rotl_i32(ret, arg1, t0);
P
pbrook 已提交
6409
    tcg_temp_free_i32(t0);
6410 6411 6412 6413 6414
}
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
static always_inline void gen_evmergehi (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6415
        gen_exception(ctx, POWERPC_EXCP_APU);
6416 6417 6418
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6419 6420
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431
    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 已提交
6432
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6433
{
6434 6435 6436
    tcg_gen_sub_i32(ret, arg2, arg1);
}
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6437

6438 6439 6440 6441 6442 6443
/* 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 已提交
6444
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6445 6446
        return;                                                               \
    }                                                                         \
P
pbrook 已提交
6447 6448 6449
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6450 6451 6452 6453
    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 已提交
6454
    tcg_temp_free_i64(t2);                                                    \
6455 6456
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
P
pbrook 已提交
6457 6458
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6459 6460 6461 6462 6463 6464
}
#else
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6465
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482
        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 已提交
6483
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6484 6485 6486 6487 6488 6489
        return;                                                               \
    }                                                                         \
    int l1 = gen_new_label();                                                 \
    int l2 = gen_new_label();                                                 \
    int l3 = gen_new_label();                                                 \
    int l4 = gen_new_label();                                                 \
P
pbrook 已提交
6490 6491 6492
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6493 6494 6495
    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 已提交
6496
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6497 6498 6499 6500 6501 6502 6503 6504 6505
    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 已提交
6506
    tcg_temp_free_i64(t2);                                                    \
6507 6508 6509 6510 6511 6512 6513 6514
    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 已提交
6515 6516
    tcg_temp_free_i32(t0);                                                    \
    tcg_temp_free_i32(t1);                                                    \
6517 6518 6519 6520 6521 6522
}
#else
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
6523
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559
        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 已提交
6560 6561
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6562
}
6563 6564 6565
static always_inline void gen_evmergelo (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
A
aurel32 已提交
6566
        gen_exception(ctx, POWERPC_EXCP_APU);
6567 6568 6569
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6570 6571
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584
    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 已提交
6585
        gen_exception(ctx, POWERPC_EXCP_APU);
6586 6587 6588
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6589 6590
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603
    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 已提交
6604
        gen_exception(ctx, POWERPC_EXCP_APU);
6605 6606 6607
        return;
    }
#if defined(TARGET_PPC64)
P
pbrook 已提交
6608 6609
    TCGv t0 = tcg_temp_new();
    TCGv t1 = tcg_temp_new();
6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621
    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)
{
6622
    uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6623

6624
#if defined(TARGET_PPC64)
6625
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6626 6627 6628 6629 6630
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
}
6631
static always_inline void gen_evsplatfi (DisasContext *ctx)
6632
{
6633
    uint64_t imm = rA(ctx->opcode) << 11;
6634

6635
#if defined(TARGET_PPC64)
6636
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6637 6638 6639 6640
#else
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
#endif
6641 6642
}

6643 6644 6645 6646 6647 6648
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 已提交
6649
    TCGv_i32 t0 = tcg_temp_local_new_i32();
6650
#if defined(TARGET_PPC64)
P
pbrook 已提交
6651 6652
    TCGv t1 = tcg_temp_local_new();
    TCGv t2 = tcg_temp_local_new();
6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683
#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 已提交
6684
    tcg_temp_free_i32(t0);
6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706
#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);
}
6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733

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

6734
/* SPE load and stores */
A
aurel32 已提交
6735
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6736 6737 6738
{
    target_ulong uimm = rB(ctx->opcode);

A
aurel32 已提交
6739
    if (rA(ctx->opcode) == 0) {
6740
        tcg_gen_movi_tl(EA, uimm << sh);
A
aurel32 已提交
6741
    } else {
6742
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
A
aurel32 已提交
6743 6744 6745 6746 6747 6748
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode) {
            tcg_gen_ext32u_tl(EA, EA);
        }
#endif
    }
6749
}
6750 6751 6752 6753

static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
6754
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6755 6756
#else
    TCGv_i64 t0 = tcg_temp_new_i64();
A
aurel32 已提交
6757
    gen_qemu_ld64(ctx, t0, addr);
6758 6759 6760 6761 6762
    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
6763
}
6764 6765 6766

static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
{
6767
#if defined(TARGET_PPC64)
6768
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6769
    gen_qemu_ld32u(ctx, t0, addr);
6770
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
A
aurel32 已提交
6771 6772
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_ld32u(ctx, t0, addr);
6773 6774 6775
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
    tcg_temp_free(t0);
#else
A
aurel32 已提交
6776 6777 6778
    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);
6779
#endif
6780
}
6781 6782 6783 6784 6785

static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
#if defined(TARGET_PPC64)
A
aurel32 已提交
6786
    gen_qemu_ld16u(ctx, t0, addr);
6787
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
A
aurel32 已提交
6788 6789
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6790 6791
    tcg_gen_shli_tl(t0, t0, 32);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6792 6793
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6794 6795
    tcg_gen_shli_tl(t0, t0, 16);
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6796 6797
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6798
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6799
#else
A
aurel32 已提交
6800
    gen_qemu_ld16u(ctx, t0, addr);
6801
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6802 6803
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6804
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
A
aurel32 已提交
6805 6806
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6807
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6808 6809
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6810
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6811
#endif
6812
    tcg_temp_free(t0);
6813 6814
}

6815 6816 6817
static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6818
    gen_qemu_ld16u(ctx, t0, addr);
6819 6820 6821 6822 6823 6824 6825 6826 6827 6828
#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);
6829 6830
}

6831 6832 6833
static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6834
    gen_qemu_ld16u(ctx, t0, addr);
6835 6836 6837 6838 6839 6840 6841 6842
#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);
6843 6844
}

6845 6846 6847
static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6848
    gen_qemu_ld16s(ctx, t0, addr);
6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863
#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 已提交
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 6870
    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 已提交
6871
    gen_qemu_ld16u(ctx, t0, addr);
6872
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
A
aurel32 已提交
6873 6874
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6875 6876 6877 6878 6879 6880 6881 6882 6883
    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 已提交
6884 6885 6886
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6887 6888 6889 6890
    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 已提交
6891 6892 6893
    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);
6894 6895 6896 6897 6898 6899 6900
#endif
}

static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6901
    gen_qemu_ld16s(ctx, t0, addr);
6902
    tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
A
aurel32 已提交
6903 6904
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16s(ctx, t0, addr);
6905 6906 6907 6908
    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 已提交
6909 6910 6911
    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);
6912 6913 6914 6915 6916 6917
#endif
}

static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
{
    TCGv t0 = tcg_temp_new();
A
aurel32 已提交
6918
    gen_qemu_ld32u(ctx, t0, addr);
6919
#if defined(TARGET_PPC64)
6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932
    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 已提交
6933
    gen_qemu_ld16u(ctx, t0, addr);
6934 6935 6936
    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 已提交
6937 6938
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6939 6940 6941 6942
    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 已提交
6943
    gen_qemu_ld16u(ctx, t0, addr);
6944 6945
    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 已提交
6946 6947
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_ld16u(ctx, t0, addr);
6948 6949
    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);
6950
#endif
6951 6952 6953 6954 6955 6956
    tcg_temp_free(t0);
}

static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
{
#if defined(TARGET_PPC64)
A
aurel32 已提交
6957
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6958
#else
6959 6960
    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 已提交
6961
    gen_qemu_st64(ctx, t0, addr);
6962 6963 6964 6965 6966 6967
    tcg_temp_free_i64(t0);
#endif
}

static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
{
6968
#if defined(TARGET_PPC64)
6969 6970
    TCGv t0 = tcg_temp_new();
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
6971
    gen_qemu_st32(ctx, t0, addr);
6972 6973
    tcg_temp_free(t0);
#else
A
aurel32 已提交
6974
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6975
#endif
A
aurel32 已提交
6976 6977
    gen_addr_add(ctx, addr, addr, 4);
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6978 6979 6980 6981 6982 6983 6984 6985 6986 6987
}

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 已提交
6988 6989
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
6990 6991
#if defined(TARGET_PPC64)
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
A
aurel32 已提交
6992
    gen_qemu_st16(ctx, t0, addr);
6993
#else
A
aurel32 已提交
6994
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6995
#endif
A
aurel32 已提交
6996
    gen_addr_add(ctx, addr, addr, 2);
6997
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
6998
    gen_qemu_st16(ctx, t0, addr);
6999
    tcg_temp_free(t0);
A
aurel32 已提交
7000 7001
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7002 7003 7004 7005 7006 7007 7008 7009 7010 7011
}

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 已提交
7012 7013
    gen_qemu_st16(ctx, t0, addr);
    gen_addr_add(ctx, addr, addr, 2);
7014
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
A
aurel32 已提交
7015
    gen_qemu_st16(ctx, t0, addr);
7016 7017 7018 7019 7020 7021 7022 7023
    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 已提交
7024
    gen_qemu_st16(ctx, t0, addr);
7025 7026
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7027
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7028
#endif
A
aurel32 已提交
7029 7030
    gen_addr_add(ctx, addr, addr, 2);
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7031 7032 7033 7034 7035 7036 7037
}

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 已提交
7038
    gen_qemu_st32(ctx, t0, addr);
7039 7040
    tcg_temp_free(t0);
#else
A
aurel32 已提交
7041
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7042 7043 7044 7045 7046
#endif
}

static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
{
A
aurel32 已提交
7047
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7048 7049 7050
}

#define GEN_SPEOP_LDST(name, opc2, sh)                                        \
A
aurel32 已提交
7051
GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                      \
7052 7053 7054
{                                                                             \
    TCGv t0;                                                                  \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7055
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7056 7057
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7058
    gen_set_access_type(ctx, ACCESS_INT);                                     \
7059 7060
    t0 = tcg_temp_new();                                                      \
    if (Rc(ctx->opcode)) {                                                    \
A
aurel32 已提交
7061
        gen_addr_spe_imm_index(ctx, t0, sh);                                  \
7062
    } else {                                                                  \
A
aurel32 已提交
7063
        gen_addr_reg_index(ctx, t0);                                          \
7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087
    }                                                                         \
    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);
7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165

/* 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 已提交
7166 7167
#if defined(TARGET_PPC64)
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7168
static always_inline void gen_##name (DisasContext *ctx)                      \
7169
{                                                                             \
A
aurel32 已提交
7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181
    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);                                                        \
7182
}
A
aurel32 已提交
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
#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)                                       \
7212 7213
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7214 7215
    TCGv_i32 t0, t1;                                                          \
    TCGv_i64 t2;                                                              \
7216
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7217
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7218 7219
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232
    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);                                                        \
7233
}
A
aurel32 已提交
7234
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7235 7236 7237
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7238
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7239 7240
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7241 7242
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
                      cpu_gpr[rB(ctx->opcode)]);                              \
7243
}
A
aurel32 已提交
7244
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7245 7246
static always_inline void gen_##name (DisasContext *ctx)                      \
{                                                                             \
A
aurel32 已提交
7247
    TCGv_i32 t0, t1;                                                          \
7248
    if (unlikely(!ctx->spe_enabled)) {                                        \
A
aurel32 已提交
7249
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7250 7251
        return;                                                               \
    }                                                                         \
A
aurel32 已提交
7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263
    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 已提交
7264
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7265 7266 7267 7268 7269 7270 7271 7272 7273 7274
        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)]);    \
7275
}
A
aurel32 已提交
7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304
#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 已提交
7305
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7306 7307 7308 7309 7310 7311 7312 7313 7314 7315
        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 已提交
7316
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331
        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 已提交
7332
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7333 7334 7335 7336 7337 7338 7339 7340 7341 7342
        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 已提交
7343
        gen_exception(ctx, POWERPC_EXCP_APU);                                 \
A
aurel32 已提交
7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354
        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
7355

7356 7357
/* Single precision floating-point vectors operations */
/* Arithmetic */
A
aurel32 已提交
7358 7359 7360 7361 7362 7363 7364
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 已提交
7365
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377
        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 已提交
7378
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390
        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 已提交
7391
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7392 7393 7394 7395 7396 7397 7398 7399 7400 7401
        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
}

7402
/* Conversion */
A
aurel32 已提交
7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413
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);

7414
/* Comparison */
A
aurel32 已提交
7415 7416 7417 7418 7419 7420
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);
7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439

/* 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 已提交
7440 7441 7442 7443 7444 7445 7446
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 已提交
7447
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7448 7449 7450 7451 7452 7453 7454
        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 已提交
7455
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7456 7457 7458 7459 7460 7461 7462
        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 已提交
7463
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7464 7465 7466 7467 7468
        return;
    }
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
}

7469
/* Conversion */
A
aurel32 已提交
7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481
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);

7482
/* Comparison */
A
aurel32 已提交
7483 7484 7485 7486 7487 7488
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);
7489 7490

/* Opcodes definitions */
7491
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7492 7493 7494 7495 7496 7497 7498 7499 7500
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); //
7501 7502
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7503 7504 7505 7506 7507
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 已提交
7508 7509 7510 7511 7512 7513 7514
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 已提交
7515
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526
        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 已提交
7527
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538
        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 已提交
7539
        gen_exception(ctx, POWERPC_EXCP_APU);
A
aurel32 已提交
7540 7541 7542 7543 7544 7545 7546 7547 7548
        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
}

7549
/* Conversion */
A
aurel32 已提交
7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564
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);
7565 7566

/* Comparison */
A
aurel32 已提交
7567 7568 7569 7570 7571 7572
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);
7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591

/* 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 已提交
7592 7593 7594
/* End opcode list */
GEN_OPCODE_MARK(end);

7595
#include "translate_init.c"
7596
#include "helper_regs.h"
B
bellard 已提交
7597

7598
/*****************************************************************************/
7599
/* Misc PowerPC helpers */
7600 7601 7602
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
7603
{
7604 7605 7606
#define RGPL  4
#define RFPL  4

B
bellard 已提交
7607 7608
    int i;

J
j_mayer 已提交
7609
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
A
aurel32 已提交
7610
                env->nip, env->lr, env->ctr, env->xer);
7611 7612
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7613
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
7614
    cpu_fprintf(f, "TB %08x %08x "
7615 7616 7617 7618
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
7619
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7620 7621 7622 7623
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
7624
#endif
7625
    for (i = 0; i < 32; i++) {
7626 7627
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
7628
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7629
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
7630
            cpu_fprintf(f, "\n");
7631
    }
7632
    cpu_fprintf(f, "CR ");
7633
    for (i = 0; i < 8; i++)
B
bellard 已提交
7634 7635
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
7636 7637 7638 7639 7640 7641 7642 7643
    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 已提交
7644
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7645
    }
7646
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7647 7648 7649
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
7650
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7651
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
7652
            cpu_fprintf(f, "\n");
B
bellard 已提交
7653
    }
7654
#if !defined(CONFIG_USER_ONLY)
7655
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7656
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7657
#endif
B
bellard 已提交
7658

7659 7660
#undef RGPL
#undef RFPL
B
bellard 已提交
7661 7662
}

7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709
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
}

7710
/*****************************************************************************/
7711 7712 7713
static always_inline void gen_intermediate_code_internal (CPUState *env,
                                                          TranslationBlock *tb,
                                                          int search_pc)
B
bellard 已提交
7714
{
7715
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
7716
    opc_handler_t **table, *handler;
B
bellard 已提交
7717
    target_ulong pc_start;
B
bellard 已提交
7718
    uint16_t *gen_opc_end;
7719
    CPUBreakpoint *bp;
B
bellard 已提交
7720
    int j, lj = -1;
P
pbrook 已提交
7721 7722
    int num_insns;
    int max_insns;
B
bellard 已提交
7723 7724 7725

    pc_start = tb->pc;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7726 7727 7728
#if defined(OPTIMIZE_FPRF_UPDATE)
    gen_fprf_ptr = gen_fprf_buf;
#endif
B
bellard 已提交
7729
    ctx.nip = pc_start;
B
bellard 已提交
7730
    ctx.tb = tb;
7731
    ctx.exception = POWERPC_EXCP_NONE;
7732
    ctx.spr_cb = env->spr_cb;
A
aurel32 已提交
7733 7734 7735
    ctx.mem_idx = env->mmu_idx;
    ctx.access_type = -1;
    ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
7736 7737
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
7738
#endif
B
bellard 已提交
7739
    ctx.fpu_enabled = msr_fp;
7740
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7741 7742 7743
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
7744 7745 7746 7747
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
7748
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7749
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
7750
    else
7751
        ctx.singlestep_enabled = 0;
7752
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7753 7754 7755
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
    if (unlikely(env->singlestep_enabled))
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7756
#if defined (DO_SINGLE_STEP) && 0
7757 7758 7759
    /* Single step trace mode */
    msr_se = 1;
#endif
P
pbrook 已提交
7760 7761 7762 7763 7764 7765
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;

    gen_icount_start();
7766
    /* Set env in case of segfault during code fetch */
7767
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7768 7769
        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7770
                if (bp->pc == ctx.nip) {
A
aurel32 已提交
7771
                    gen_debug_exception(ctxp);
7772 7773 7774 7775
                    break;
                }
            }
        }
7776
        if (unlikely(search_pc)) {
B
bellard 已提交
7777 7778 7779 7780 7781
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
7782
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
7783
                gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
7784
                gen_opc_icount[lj] = num_insns;
B
bellard 已提交
7785 7786
            }
        }
7787 7788
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
7789
            fprintf(logfile, "----------------\n");
7790
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
A
aurel32 已提交
7791
                    ctx.nip, ctx.mem_idx, (int)msr_ir);
7792 7793
        }
#endif
P
pbrook 已提交
7794 7795
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();
A
aurel32 已提交
7796
        if (unlikely(ctx.le_mode)) {
7797 7798 7799
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
7800
        }
7801 7802
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7803
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7804
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7805
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
7806 7807
        }
#endif
B
bellard 已提交
7808
        ctx.nip += 4;
7809
        table = env->opcodes;
P
pbrook 已提交
7810
        num_insns++;
B
bellard 已提交
7811 7812 7813 7814 7815 7816 7817 7818 7819 7820
        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 ? */
7821
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
7822
            if (loglevel != 0) {
7823
                fprintf(logfile, "invalid/unsupported opcode: "
7824
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7825
                        opc1(ctx.opcode), opc2(ctx.opcode),
7826
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
7827 7828
            } else {
                printf("invalid/unsupported opcode: "
7829
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
B
bellard 已提交
7830
                       opc1(ctx.opcode), opc2(ctx.opcode),
7831
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
7832
            }
7833 7834
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
7835
                if (loglevel != 0) {
B
bellard 已提交
7836
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7837
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
B
bellard 已提交
7838 7839
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
7840
                            ctx.opcode, ctx.nip - 4);
7841 7842
                } else {
                    printf("invalid bits: %08x for opcode: "
7843
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
7844 7845
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
7846
                           ctx.opcode, ctx.nip - 4);
7847
                }
A
aurel32 已提交
7848
                gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
B
bellard 已提交
7849
                break;
B
bellard 已提交
7850 7851
            }
        }
B
bellard 已提交
7852
        (*(handler->handler))(&ctx);
7853 7854 7855
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
7856
        /* Check trace mode exceptions */
7857 7858 7859 7860 7861
        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 已提交
7862
            gen_exception(ctxp, POWERPC_EXCP_TRACE);
7863
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
P
pbrook 已提交
7864 7865
                            (env->singlestep_enabled) ||
                            num_insns >= max_insns)) {
7866 7867 7868
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
7869
            break;
7870
        }
7871 7872 7873 7874
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
P
pbrook 已提交
7875 7876
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
7877
    if (ctx.exception == POWERPC_EXCP_NONE) {
7878
        gen_goto_tb(&ctx, 0, ctx.nip);
7879
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7880
        if (unlikely(env->singlestep_enabled)) {
A
aurel32 已提交
7881
            gen_debug_exception(ctxp);
7882
        }
7883
        /* Generate the return instruction */
B
bellard 已提交
7884
        tcg_gen_exit_tb(0);
7885
    }
P
pbrook 已提交
7886
    gen_icount_end(tb, num_insns);
B
bellard 已提交
7887
    *gen_opc_ptr = INDEX_op_end;
7888
    if (unlikely(search_pc)) {
7889 7890 7891 7892 7893
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
7894
        tb->size = ctx.nip - pc_start;
P
pbrook 已提交
7895
        tb->icount = num_insns;
7896
    }
7897
#if defined(DEBUG_DISAS)
7898
    if (loglevel & CPU_LOG_TB_CPU) {
7899
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
7900
        cpu_dump_state(env, logfile, fprintf, 0);
7901 7902
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7903
        int flags;
7904
        flags = env->bfd_mach;
A
aurel32 已提交
7905
        flags |= ctx.le_mode << 16;
B
bellard 已提交
7906
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7907
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
7908
        fprintf(logfile, "\n");
7909
    }
B
bellard 已提交
7910 7911 7912
#endif
}

7913
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
7914
{
7915
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
7916 7917
}

7918
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
7919
{
7920
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
7921
}
A
aurel32 已提交
7922 7923 7924 7925 7926 7927

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