translate.c 206.8 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 29
#include "disas.h"

30
/* Include definitions for instructions classes and implementations flags */
B
bellard 已提交
31
//#define DO_SINGLE_STEP
32
//#define PPC_DEBUG_DISAS
33
//#define DEBUG_MEMORY_ACCESSES
34
//#define DO_PPC_STATISTICS
B
bellard 已提交
35

36 37
/*****************************************************************************/
/* Code translation helpers                                                  */
38
#if defined(USE_DIRECT_JUMP)
B
bellard 已提交
39 40 41 42 43
#define TBPARAM(x)
#else
#define TBPARAM(x) (long)(x)
#endif

B
bellard 已提交
44 45 46 47 48 49 50 51 52 53 54
enum {
#define DEF(s, n, copy_size) INDEX_op_ ## s,
#include "opc.h"
#undef DEF
    NB_OPS,
};

static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;

#include "gen-op.h"
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static inline void gen_set_T0 (target_ulong val)
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T0_64(val >> 32, val);
    else
#endif
        gen_op_set_T0(val);
}

static inline void gen_set_T1 (target_ulong val)
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T1_64(val >> 32, val);
    else
#endif
        gen_op_set_T1(val);
}

#define GEN8(func, NAME)                                                      \
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static GenOpFunc *NAME ## _table [8] = {                                      \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
};                                                                            \
static inline void func(int n)                                                \
{                                                                             \
    NAME ## _table[n]();                                                      \
}

#define GEN16(func, NAME)                                                     \
static GenOpFunc *NAME ## _table [16] = {                                     \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
};                                                                            \
static inline void func(int n)                                                \
{                                                                             \
    NAME ## _table[n]();                                                      \
96 97
}

98
#define GEN32(func, NAME)                                                     \
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static GenOpFunc *NAME ## _table [32] = {                                     \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
};                                                                            \
static inline void func(int n)                                                \
{                                                                             \
    NAME ## _table[n]();                                                      \
}

/* Condition register moves */
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
119

B
bellard 已提交
120 121 122 123
/* Floating point condition and status register moves */
GEN8(gen_op_load_fpscr_T0, gen_op_load_fpscr_T0_fpscr);
GEN8(gen_op_store_T0_fpscr, gen_op_store_T0_fpscr_fpscr);
GEN8(gen_op_clear_fpscr, gen_op_clear_fpscr_fpscr);
124
static inline void gen_op_store_T0_fpscri (int n, uint8_t param)
B
bellard 已提交
125
{
126 127
    gen_op_set_T0(param);
    gen_op_store_T0_fpscr(n);
B
bellard 已提交
128 129
}

130 131 132 133 134 135 136
/* General purpose registers moves */
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);

GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
137
#if 0 // unused
138
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
139
#endif
140

B
bellard 已提交
141 142 143 144 145 146
/* floating point registers moves */
GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);
GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);
GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);
GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);
GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);
147
#if 0 // unused
B
bellard 已提交
148
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
149
#endif
B
bellard 已提交
150 151 152 153

/* internal defines */
typedef struct DisasContext {
    struct TranslationBlock *tb;
B
bellard 已提交
154
    target_ulong nip;
B
bellard 已提交
155
    uint32_t opcode;
156
    uint32_t exception;
B
bellard 已提交
157 158 159
    /* Routine used to access memory */
    int mem_idx;
    /* Translation flags */
160
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
161
    int supervisor;
162 163 164
#endif
#if defined(TARGET_PPC64)
    int sf_mode;
165
#endif
B
bellard 已提交
166
    int fpu_enabled;
167
#if defined(TARGET_PPCEMB)
168 169
    int spe_enabled;
#endif
170
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
171
    int singlestep_enabled;
B
bellard 已提交
172 173
} DisasContext;

174
struct opc_handler_t {
B
bellard 已提交
175 176
    /* invalid bits */
    uint32_t inval;
177
    /* instruction type */
178
    uint64_t type;
B
bellard 已提交
179 180
    /* handler */
    void (*handler)(DisasContext *ctx);
181
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
182
    const unsigned char *oname;
183 184
#endif
#if defined(DO_PPC_STATISTICS)
185 186
    uint64_t count;
#endif
187
};
B
bellard 已提交
188

189 190
static inline void gen_set_Rc0 (DisasContext *ctx)
{
191 192 193 194 195 196
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_cmpi_64(0);
    else
#endif
        gen_op_cmpi(0);
197 198 199
    gen_op_set_Rc0();
}

200 201 202 203 204 205 206 207 208 209
static inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_update_nip_64(nip >> 32, nip);
    else
#endif
        gen_op_update_nip(nip);
}

210
#define GEN_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
211
do {                                                                          \
212
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
213
        gen_update_nip(ctx, (ctx)->nip);                                      \
214 215 216
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
217 218
} while (0)

219 220 221
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
222

223 224 225
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
226

227 228 229 230 231 232 233 234 235
#define GEN_EXCP_PRIVREG(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)

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

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

237
/* Stop translation */
238
static inline void GEN_STOP (DisasContext *ctx)
239
{
240
    gen_update_nip(ctx, ctx->nip);
241
    ctx->exception = POWERPC_EXCP_STOP;
242 243
}

244
/* No need to update nip here, as execution flow will change */
245
static inline void GEN_SYNC (DisasContext *ctx)
246
{
247
    ctx->exception = POWERPC_EXCP_SYNC;
248 249
}

B
bellard 已提交
250 251 252 253 254 255 256
#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)

typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
257 258 259 260 261
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
262
    opc_handler_t handler;
263
    const unsigned char *oname;
B
bellard 已提交
264 265
} opcode_t;

266
/*****************************************************************************/
B
bellard 已提交
267 268
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
269
static inline uint32_t name (uint32_t opcode)                                 \
B
bellard 已提交
270 271 272 273 274
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
275
static inline int32_t name (uint32_t opcode)                                  \
B
bellard 已提交
276
{                                                                             \
277
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

/* 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 */
305 306 307 308 309 310 311
EXTRACT_HELPER(_SPR, 11, 10);
static inline uint32_t SPR (uint32_t opcode)
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325
/***                              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 已提交
326 327
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
328 329 330 331

EXTRACT_HELPER(CRM, 12, 8);
EXTRACT_HELPER(FM, 17, 8);
EXTRACT_HELPER(SR, 16, 4);
B
bellard 已提交
332 333
EXTRACT_HELPER(FPIMM, 20, 4);

B
bellard 已提交
334 335 336 337
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
338
static inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
{
    return (opcode >> 0) & 0x03FFFFFC;
}

static inline uint32_t BD (uint32_t opcode)
{
    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 */
356
static inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
357
{
358
    target_ulong ret;
B
bellard 已提交
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
        ret = (uint64_t)(-1ULL) << (63 - end);
    } else if (likely(end == 63)) {
        ret = (uint64_t)(-1ULL) >> start;
    }
#else
    if (likely(start == 0)) {
        ret = (uint32_t)(-1ULL) << (31  - end);
    } else if (likely(end == 31)) {
        ret = (uint32_t)(-1ULL) >> start;
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
379 380 381 382

    return ret;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/*****************************************************************************/
/* PowerPC Instructions types definitions                                    */
enum {
    PPC_NONE          = 0x0000000000000000ULL,
    /* integer operations instructions                  */
    /* flow control instructions                        */
    /* virtual memory instructions                      */
    /* ld/st with reservation instructions              */
    /* cache control instructions                       */
    /* spr/msr access instructions                      */
    PPC_INSNS_BASE    = 0x0000000000000001ULL,
#define PPC_INTEGER PPC_INSNS_BASE
#define PPC_FLOW    PPC_INSNS_BASE
#define PPC_MEM     PPC_INSNS_BASE
#define PPC_RES     PPC_INSNS_BASE
#define PPC_CACHE   PPC_INSNS_BASE
#define PPC_MISC    PPC_INSNS_BASE
    /* Optional floating point instructions             */
    PPC_FLOAT         = 0x0000000000000002ULL,
    PPC_FLOAT_FSQRT   = 0x0000000000000004ULL,
    PPC_FLOAT_FRES    = 0x0000000000000008ULL,
    PPC_FLOAT_FRSQRTE = 0x0000000000000010ULL,
    PPC_FLOAT_FSEL    = 0x0000000000000020ULL,
    PPC_FLOAT_STFIWX  = 0x0000000000000040ULL,
    /* external control instructions                    */
    PPC_EXTERN        = 0x0000000000000080ULL,
    /* segment register access instructions             */
    PPC_SEGMENT       = 0x0000000000000100ULL,
    /* Optional cache control instruction               */
    PPC_CACHE_DCBA    = 0x0000000000000200ULL,
    /* Optional memory control instructions             */
    PPC_MEM_TLBIA     = 0x0000000000000400ULL,
    PPC_MEM_TLBIE     = 0x0000000000000800ULL,
    PPC_MEM_TLBSYNC   = 0x0000000000001000ULL,
    /* eieio & sync                                     */
    PPC_MEM_SYNC      = 0x0000000000002000ULL,
    /* PowerPC 6xx TLB management instructions          */
    PPC_6xx_TLB       = 0x0000000000004000ULL,
    /* Altivec support                                  */
    PPC_ALTIVEC       = 0x0000000000008000ULL,
    /* Time base mftb instruction                       */
    PPC_MFTB          = 0x0000000000010000ULL,
    /* Embedded PowerPC dedicated instructions          */
    PPC_EMB_COMMON    = 0x0000000000020000ULL,
    /* PowerPC 40x exception model                      */
    PPC_40x_EXCP      = 0x0000000000040000ULL,
    /* PowerPC 40x TLB management instructions          */
    PPC_40x_TLB       = 0x0000000000080000ULL,
    /* PowerPC 405 Mac instructions                     */
    PPC_405_MAC       = 0x0000000000100000ULL,
    /* PowerPC 440 specific instructions                */
    PPC_440_SPEC      = 0x0000000000200000ULL,
    /* Power-to-PowerPC bridge (601)                    */
    PPC_POWER_BR      = 0x0000000000400000ULL,
    /* PowerPC 602 specific */
    PPC_602_SPEC      = 0x0000000000800000ULL,
    /* Deprecated instructions                          */
    /* Original POWER instruction set                   */
    PPC_POWER         = 0x0000000001000000ULL,
    /* POWER2 instruction set extension                 */
    PPC_POWER2        = 0x0000000002000000ULL,
    /* Power RTC support */
    PPC_POWER_RTC     = 0x0000000004000000ULL,
    /* 64 bits PowerPC instructions                     */
    /* 64 bits PowerPC instruction set                  */
    PPC_64B           = 0x0000000008000000ULL,
    /* 64 bits hypervisor extensions                    */
    PPC_64H           = 0x0000000010000000ULL,
    /* 64 bits PowerPC "bridge" features                */
    PPC_64_BRIDGE     = 0x0000000020000000ULL,
    /* BookE (embedded) PowerPC specification           */
    PPC_BOOKE         = 0x0000000040000000ULL,
    /* eieio                                            */
    PPC_MEM_EIEIO     = 0x0000000080000000ULL,
    /* e500 vector instructions                         */
    PPC_E500_VECTOR   = 0x0000000100000000ULL,
    /* PowerPC 4xx dedicated instructions               */
    PPC_4xx_COMMON    = 0x0000000200000000ULL,
    /* PowerPC 2.03 specification extensions            */
    PPC_203           = 0x0000000400000000ULL,
    /* PowerPC 2.03 SPE extension                       */
    PPC_SPE           = 0x0000000800000000ULL,
    /* PowerPC 2.03 SPE floating-point extension        */
    PPC_SPEFPU        = 0x0000001000000000ULL,
    /* SLB management                                   */
    PPC_SLBI          = 0x0000002000000000ULL,
    /* PowerPC 40x ibct instructions                    */
    PPC_40x_ICBT      = 0x0000004000000000ULL,
    /* PowerPC 74xx TLB management instructions         */
    PPC_74xx_TLB      = 0x0000008000000000ULL,
    /* More BookE (embedded) instructions...            */
    PPC_BOOKE_EXT     = 0x0000010000000000ULL,
    /* rfmci is not implemented in all BookE PowerPC    */
    PPC_RFMCI         = 0x0000020000000000ULL,
    /* user-mode DCR access, implemented in PowerPC 460 */
    PPC_DCRUX         = 0x0000040000000000ULL,
479 480
    /* New floating-point extensions (PowerPC 2.0x)     */
    PPC_FLOAT_EXT     = 0x0000080000000000ULL,
481 482 483 484
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
485 486 487 488 489
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
490
#if defined(__APPLE__)
491
#define OPCODES_SECTION                                                       \
492
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
493
#else
494
#define OPCODES_SECTION                                                       \
495
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
496 497
#endif

498
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
499
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
500
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
501 502 503
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
504
    .pad  = { 0, },                                                           \
B
bellard 已提交
505 506
    .handler = {                                                              \
        .inval   = invl,                                                      \
507
        .type = _typ,                                                         \
B
bellard 已提交
508
        .handler = &gen_##name,                                               \
509
        .oname = stringify(name),                                             \
B
bellard 已提交
510
    },                                                                        \
511
    .oname = stringify(name),                                                 \
B
bellard 已提交
512
}
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
#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),                                                 \
}
#endif
B
bellard 已提交
528 529

#define GEN_OPCODE_MARK(name)                                                 \
530
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
531 532 533
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
534
    .pad  = { 0, },                                                           \
B
bellard 已提交
535 536
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
537
        .type = 0x00,                                                         \
B
bellard 已提交
538 539
        .handler = NULL,                                                      \
    },                                                                        \
540
    .oname = stringify(name),                                                 \
B
bellard 已提交
541 542 543 544 545 546
}

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

/* Invalid instruction */
547 548
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
549
    GEN_EXCP_INVAL(ctx);
550 551
}

B
bellard 已提交
552 553
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
554
    .type    = PPC_NONE,
B
bellard 已提交
555 556 557 558
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
559 560
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
561 562 563 564 565
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
566 567
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
568 569
}

570 571
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
572 573 574 575 576
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
577 578
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
579 580
}

581 582
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
583 584 585 586
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
587 588
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
589
}
590 591
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
592 593 594 595
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
596 597
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
598 599 600
}

/* Two operands arithmetic functions */
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)

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

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

#if defined(TARGET_PPC64)
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

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

/* Two operands arithmetic functions with no overflow allowed */
674 675
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
676 677

/* One operand arithmetic functions */
678 679 680 681 682 683 684 685
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
#else
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
#endif
B
bellard 已提交
686 687

/* add    add.    addo    addo.    */
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
static inline void gen_op_addo (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
static inline void gen_op_addo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
B
bellard 已提交
704
/* addc   addc.   addco   addco.   */
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
static inline void gen_op_addc (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
}
static inline void gen_op_addco (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addc_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
}
static inline void gen_op_addco_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
B
bellard 已提交
734
/* adde   adde.   addeo   addeo.   */
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
static inline void gen_op_addeo (void)
{
    gen_op_move_T2_T0();
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_adde_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
B
bellard 已提交
750
/* addme  addme.  addmeo  addmeo.  */
751 752 753 754 755 756 757 758 759 760 761 762 763
static inline void gen_op_addme (void)
{
    gen_op_move_T1_T0();
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addme_64 (void)
{
    gen_op_move_T1_T0();
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
764
/* addze  addze.  addzeo  addzeo.  */
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
static inline void gen_op_addze (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
}
static inline void gen_op_addzeo (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addze_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
}
static inline void gen_op_addzeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
B
bellard 已提交
794
/* divw   divw.   divwo   divwo.   */
795
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
796
/* divwu  divwu.  divwuo  divwuo.  */
797
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
798
/* mulhw  mulhw.                   */
799
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
800
/* mulhwu mulhwu.                  */
801
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
802
/* mullw  mullw.  mullwo  mullwo.  */
803
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
804
/* neg    neg.    nego    nego.    */
805
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
806
/* subf   subf.   subfo   subfo.   */
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
static inline void gen_op_subfo (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
static inline void gen_op_subfo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
B
bellard 已提交
823
/* subfc  subfc.  subfco  subfco.  */
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
static inline void gen_op_subfc (void)
{
    gen_op_subf();
    gen_op_check_subfc();
}
static inline void gen_op_subfco (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfc();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_subfc_64 (void)
{
    gen_op_subf();
    gen_op_check_subfc_64();
}
static inline void gen_op_subfco_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfc_64();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
B
bellard 已提交
851
/* subfe  subfe.  subfeo  subfeo.  */
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
static inline void gen_op_subfeo (void)
{
    gen_op_move_T2_T0();
    gen_op_subfe();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
static inline void gen_op_subfeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subfe_64();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
B
bellard 已提交
868
/* subfme subfme. subfmeo subfmeo. */
869
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
870
/* subfze subfze. subfzeo subfzeo. */
871
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
872 873 874
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
875
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
876 877

    if (rA(ctx->opcode) == 0) {
878
        /* li case */
879
        gen_set_T0(simm);
B
bellard 已提交
880 881
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
882 883
        if (likely(simm != 0))
            gen_op_addi(simm);
B
bellard 已提交
884 885 886 887 888 889
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
890 891
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
892
    gen_op_load_gpr_T0(rA(ctx->opcode));
893 894 895 896 897 898 899 900 901
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
902 903
    } else {
        gen_op_clear_xer_ca();
904
    }
B
bellard 已提交
905 906 907 908 909
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic. */
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
910 911
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
912
    gen_op_load_gpr_T0(rA(ctx->opcode));
913 914 915 916 917 918 919 920 921
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
922 923
    } else {
        gen_op_clear_xer_ca();
924
    }
B
bellard 已提交
925
    gen_op_store_T0_gpr(rD(ctx->opcode));
926
    gen_set_Rc0(ctx);
B
bellard 已提交
927 928 929 930
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
931
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
932 933

    if (rA(ctx->opcode) == 0) {
934
        /* lis case */
935
        gen_set_T0(simm << 16);
B
bellard 已提交
936 937
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
938 939
        if (likely(simm != 0))
            gen_op_addi(simm << 16);
B
bellard 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_mulli(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
954 955 956 957 958 959
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_subfic_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_subfic(SIMM(ctx->opcode));
B
bellard 已提交
960 961 962
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

963 964
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
965
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
966
/* mulhdu mulhdu.                  */
967
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
968
/* mulld  mulld.  mulldo  mulldo.  */
969
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
970
/* divd   divd.   divdo   divdo.   */
971
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
972
/* divdu  divdu.  divduo  divduo.  */
973
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
974 975
#endif

B
bellard 已提交
976
/***                           Integer comparison                          ***/
977 978 979 980 981 982
#if defined(TARGET_PPC64)
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
983
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
984 985 986 987 988 989 990 991
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
#else
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
B
bellard 已提交
992 993 994 995 996 997
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
998
#endif
B
bellard 已提交
999 1000

/* cmp */
1001
GEN_CMP(cmp, 0x00, PPC_INTEGER);
B
bellard 已提交
1002 1003 1004 1005
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1006
#if defined(TARGET_PPC64)
1007
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1008 1009 1010 1011
        gen_op_cmpi_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_cmpi(SIMM(ctx->opcode));
B
bellard 已提交
1012 1013 1014
    gen_op_store_T0_crf(crfD(ctx->opcode));
}
/* cmpl */
1015
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
B
bellard 已提交
1016 1017 1018 1019
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1020
#if defined(TARGET_PPC64)
1021
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1022 1023 1024 1025
        gen_op_cmpli_64(UIMM(ctx->opcode));
    else
#endif
        gen_op_cmpli(UIMM(ctx->opcode));
B
bellard 已提交
1026 1027 1028
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
/* isel (PowerPC 2.03 specification) */
GEN_HANDLER(isel, 0x1F, 0x0F, 0x00, 0x00000001, PPC_203)
{
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;

    if (rA(ctx->opcode) == 0) {
        gen_set_T0(0);
    } else {
        gen_op_load_gpr_T1(rA(ctx->opcode));
    }
    gen_op_load_gpr_T2(rB(ctx->opcode));
    mask = 1 << (3 - (bi & 0x03));
    gen_op_load_crf_T0(bi >> 2);
    gen_op_test_true(mask);
    gen_op_isel();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

B
bellard 已提交
1048
/***                            Integer logical                            ***/
1049 1050
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
B
bellard 已提交
1051 1052 1053 1054 1055
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1056 1057
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1058
}
1059 1060
#define GEN_LOGICAL2(name, opc, type)                                         \
__GEN_LOGICAL2(name, 0x1C, opc, type)
B
bellard 已提交
1061

1062 1063
#define GEN_LOGICAL1(name, opc, type)                                         \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1064 1065 1066 1067
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1068 1069
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1070 1071 1072
}

/* and & and. */
1073
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
B
bellard 已提交
1074
/* andc & andc. */
1075
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
B
bellard 已提交
1076 1077 1078 1079
/* andi. */
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1080
    gen_op_andi_T0(UIMM(ctx->opcode));
B
bellard 已提交
1081
    gen_op_store_T0_gpr(rA(ctx->opcode));
1082
    gen_set_Rc0(ctx);
B
bellard 已提交
1083 1084 1085 1086 1087
}
/* andis. */
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1088
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
B
bellard 已提交
1089
    gen_op_store_T0_gpr(rA(ctx->opcode));
1090
    gen_set_Rc0(ctx);
B
bellard 已提交
1091 1092 1093
}

/* cntlzw */
1094
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
B
bellard 已提交
1095
/* eqv & eqv. */
1096
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
B
bellard 已提交
1097
/* extsb & extsb. */
1098
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
B
bellard 已提交
1099
/* extsh & extsh. */
1100
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
B
bellard 已提交
1101
/* nand & nand. */
1102
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
B
bellard 已提交
1103
/* nor & nor. */
1104
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1105

B
bellard 已提交
1106
/* or & or. */
1107 1108
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    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) {
        gen_op_load_gpr_T0(rs);
        if (rs != rb) {
            gen_op_load_gpr_T1(rb);
            gen_op_or();
        }
        gen_op_store_T0_gpr(ra);
        if (unlikely(Rc(ctx->opcode) != 0))
            gen_set_Rc0(ctx);
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_gpr_T0(rs);
        gen_set_Rc0(ctx);
1127 1128 1129
    }
}

B
bellard 已提交
1130
/* orc & orc. */
1131
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
B
bellard 已提交
1132
/* xor & xor. */
1133 1134 1135 1136 1137 1138 1139 1140
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    /* Optimisation for "set to zero" case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_xor();
    } else {
1141
        gen_op_reset_T0();
1142 1143
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1144 1145
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1146
}
B
bellard 已提交
1147 1148 1149
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1150
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1151

1152 1153
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1154
        /* XXX: should handle special NOPs for POWER series */
1155
        return;
1156 1157 1158
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1159
        gen_op_ori(uimm);
1160
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1161 1162 1163 1164
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1165
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1166

1167 1168 1169
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1170 1171 1172
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1173
        gen_op_ori(uimm << 16);
1174
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1175 1176 1177 1178
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1179
    target_ulong uimm = UIMM(ctx->opcode);
1180 1181 1182 1183 1184

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1185
    gen_op_load_gpr_T0(rS(ctx->opcode));
1186 1187
    if (likely(uimm != 0))
        gen_op_xori(uimm);
B
bellard 已提交
1188 1189 1190 1191 1192 1193
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1194
    target_ulong uimm = UIMM(ctx->opcode);
1195 1196 1197 1198 1199

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1200
    gen_op_load_gpr_T0(rS(ctx->opcode));
1201 1202
    if (likely(uimm != 0))
        gen_op_xori(uimm << 16);
B
bellard 已提交
1203 1204 1205
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/* popcntb : PowerPC 2.03 specification */
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_203)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_popcntb_64();
    else
#endif
        gen_op_popcntb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

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

B
bellard 已提交
1226 1227 1228 1229
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1230 1231
    target_ulong mask;
    uint32_t mb, me, sh;
B
bellard 已提交
1232 1233 1234

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    sh = SH(ctx->opcode);
    if (likely(sh == 0)) {
        if (likely(mb == 0 && me == 31)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        } else if (likely(mb == 31 && me == 0)) {
            gen_op_load_gpr_T0(rA(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
B
bellard 已提交
1248
    gen_op_load_gpr_T0(rS(ctx->opcode));
B
bellard 已提交
1249
    gen_op_load_gpr_T1(rA(ctx->opcode));
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    gen_op_rotli32_T0(SH(ctx->opcode));
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    mask = MASK(mb, me);
    gen_op_andi_T0(mask);
    gen_op_andi_T1(~mask);
    gen_op_or();
 do_store:
B
bellard 已提交
1261
    gen_op_store_T0_gpr(rA(ctx->opcode));
1262 1263
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1264 1265 1266 1267 1268
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1269

B
bellard 已提交
1270 1271 1272 1273
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 31)) {
            gen_op_rotli32_T0(sh);
            goto do_store;
        } else if (likely(me == (31 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
B
bellard 已提交
1284
        }
1285 1286 1287 1288
    } else if (likely(me == 31)) {
        if (likely(sh == (32 - mb))) {
            gen_op_srli_T0(mb);
            goto do_store;
B
bellard 已提交
1289 1290
        }
    }
1291 1292 1293 1294 1295 1296 1297 1298
    gen_op_rotli32_T0(sh);
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    gen_op_andi_T0(MASK(mb, me));
 do_store:
B
bellard 已提交
1299
    gen_op_store_T0_gpr(rA(ctx->opcode));
1300 1301
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
1312 1313 1314 1315 1316 1317 1318
    gen_op_rotl32_T0_T1();
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_andi_T0(MASK(mb, me));
B
bellard 已提交
1319 1320
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1321 1322
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1323 1324
}

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
GEN_HANDLER(name##0, opc1, opc2, 0xFF, 0x00000000, PPC_64B)                   \
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
GEN_HANDLER(name##1, opc1, opc2 | 0x10, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
GEN_HANDLER(name##0, opc1, opc2, 0xFF, 0x00000000, PPC_64B)                   \
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
GEN_HANDLER(name##1, opc1, opc2 | 0x01, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
GEN_HANDLER(name##2, opc1, opc2 | 0x10, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
GEN_HANDLER(name##3, opc1, opc2 | 0x11, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1352

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
static inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
{
    if (mask >> 32)
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T0(mask);
}

static inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
{
    if (mask >> 32)
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T1(mask);
}

J
j_mayer 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377
static inline void gen_rldinm (DisasContext *ctx, uint32_t mb, uint32_t me,
                               uint32_t sh)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 63)) {
1378
            gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1379 1380 1381 1382 1383 1384 1385
            goto do_store;
        } else if (likely(me == (63 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
        }
    } else if (likely(me == 63)) {
        if (likely(sh == (64 - mb))) {
1386
            gen_op_srli_T0_64(mb);
J
j_mayer 已提交
1387 1388 1389 1390 1391
            goto do_store;
        }
    }
    gen_op_rotli64_T0(sh);
 do_mask:
1392
    gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1393 1394 1395 1396 1397
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1398 1399 1400
/* rldicl - rldicl. */
static inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1401
    uint32_t sh, mb;
1402

J
j_mayer 已提交
1403 1404
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1405
    gen_rldinm(ctx, mb, 63, sh);
1406
}
J
j_mayer 已提交
1407
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1408 1409 1410
/* rldicr - rldicr. */
static inline void gen_rldicr (DisasContext *ctx, int men, int shn)
{
J
j_mayer 已提交
1411
    uint32_t sh, me;
1412

J
j_mayer 已提交
1413 1414
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1415
    gen_rldinm(ctx, 0, me, sh);
1416
}
J
j_mayer 已提交
1417
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1418 1419 1420
/* rldic - rldic. */
static inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1421
    uint32_t sh, mb;
1422

J
j_mayer 已提交
1423 1424
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

static inline void gen_rldnm (DisasContext *ctx, uint32_t mb, uint32_t me)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_rotl64_T0_T1();
    if (unlikely(mb != 0 || me != 63)) {
1435
        gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1436 1437 1438 1439
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1440
}
J
j_mayer 已提交
1441

1442 1443 1444
/* rldcl - rldcl. */
static inline void gen_rldcl (DisasContext *ctx, int mbn)
{
J
j_mayer 已提交
1445
    uint32_t mb;
1446

J
j_mayer 已提交
1447
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1448
    gen_rldnm(ctx, mb, 63);
1449
}
1450
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1451 1452 1453
/* rldcr - rldcr. */
static inline void gen_rldcr (DisasContext *ctx, int men)
{
J
j_mayer 已提交
1454
    uint32_t me;
1455

J
j_mayer 已提交
1456
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1457
    gen_rldnm(ctx, 0, me);
1458
}
1459
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1460 1461 1462
/* rldimi - rldimi. */
static inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1463 1464
    uint64_t mask;
    uint32_t sh, mb;
1465

J
j_mayer 已提交
1466 1467
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    if (likely(sh == 0)) {
        if (likely(mb == 0)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        } else if (likely(mb == 63)) {
            gen_op_load_gpr_T0(rA(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
1482
    gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1483 1484
 do_mask:
    mask = MASK(mb, 63 - sh);
1485 1486
    gen_andi_T0_64(ctx, mask);
    gen_andi_T1_64(ctx, ~mask);
J
j_mayer 已提交
1487 1488 1489 1490 1491
    gen_op_or();
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1492
}
1493
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1494 1495
#endif

B
bellard 已提交
1496 1497
/***                             Integer shift                             ***/
/* slw & slw. */
1498
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
B
bellard 已提交
1499
/* sraw & sraw. */
1500
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
B
bellard 已提交
1501 1502 1503
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1504
    int mb, me;
B
bellard 已提交
1505
    gen_op_load_gpr_T0(rS(ctx->opcode));
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
    if (SH(ctx->opcode) != 0) {
        gen_op_move_T1_T0();
        mb = 32 - SH(ctx->opcode);
        me = 31;
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
    }
B
bellard 已提交
1516
    gen_op_store_T0_gpr(rA(ctx->opcode));
1517 1518
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1519 1520
}
/* srw & srw. */
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);

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

    gen_op_load_gpr_T0(rS(ctx->opcode));
    sh = SH(ctx->opcode) + (n << 5);
    if (sh != 0) {
        gen_op_move_T1_T0();
        mb = 64 - SH(ctx->opcode);
        me = 63;
        mask = MASK(mb, me);
        gen_op_sradi(sh, mask >> 32, mask);
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
GEN_HANDLER(sradi0, 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
{
    gen_sradi(ctx, 0);
}
GEN_HANDLER(sradi1, 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
#endif
B
bellard 已提交
1558 1559

/***                       Floating-Point arithmetic                       ***/
1560 1561
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, type)                     \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1562
{                                                                             \
1563
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1564
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1565 1566
        return;                                                               \
    }                                                                         \
1567 1568 1569 1570
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
1571 1572 1573 1574
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1575
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1576
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1577 1578 1579
        gen_op_set_Rc1();                                                     \
}

1580 1581 1582
#define GEN_FLOAT_ACB(name, op2, type)                                        \
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, type);                               \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, type);
1583

1584
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat)                     \
1585 1586
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1587
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1588
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1589 1590
        return;                                                               \
    }                                                                         \
1591 1592 1593
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
1594 1595 1596 1597
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1598
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1599
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1600 1601 1602
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AB(name, op2, inval)                                        \
1603 1604
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1);
1605

1606
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat)                     \
1607 1608
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1609
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1610
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1611 1612
        return;                                                               \
    }                                                                         \
1613 1614 1615
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1616 1617 1618 1619
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1620
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1621
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1622 1623 1624
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AC(name, op2, inval)                                        \
1625 1626
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1);
1627

1628 1629
#define GEN_FLOAT_B(name, op2, op3, type)                                     \
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1630
{                                                                             \
1631
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1632
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1633 1634
        return;                                                               \
    }                                                                         \
1635 1636 1637 1638
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1639
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1640
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1641 1642
}

1643 1644
#define GEN_FLOAT_BS(name, op1, op2, type)                                    \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1645
{                                                                             \
1646
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1647
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1648 1649
        return;                                                               \
    }                                                                         \
1650 1651 1652 1653
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1654
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1655
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1656 1657
}

1658 1659
/* fadd - fadds */
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
1660
/* fdiv - fdivs */
1661
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
1662
/* fmul - fmuls */
1663
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
B
bellard 已提交
1664

1665 1666 1667
/* fre */
GEN_FLOAT_BS(re, 0x3F, 0x18, PPC_FLOAT_EXT);

1668 1669
/* fres */
GEN_FLOAT_BS(res, 0x3B, 0x18, PPC_FLOAT_FRES);
B
bellard 已提交
1670

1671 1672
/* frsqrte */
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, PPC_FLOAT_FRSQRTE);
B
bellard 已提交
1673

1674 1675
/* fsel */
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, PPC_FLOAT_FSEL);
1676
/* fsub - fsubs */
1677
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
B
bellard 已提交
1678 1679
/* Optional: */
/* fsqrt */
1680
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1681
{
1682
    if (unlikely(!ctx->fpu_enabled)) {
1683
        GEN_EXCP_NO_FP(ctx);
1684 1685 1686 1687 1688 1689
        return;
    }
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_fsqrt();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1690
    if (unlikely(Rc(ctx->opcode) != 0))
1691 1692
        gen_op_set_Rc1();
}
B
bellard 已提交
1693

1694
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
1695
{
1696
    if (unlikely(!ctx->fpu_enabled)) {
1697
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1698 1699
        return;
    }
1700 1701
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1702 1703
    gen_op_fsqrt();
    gen_op_frsp();
1704
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1705
    if (unlikely(Rc(ctx->opcode) != 0))
1706
        gen_op_set_Rc1();
B
bellard 已提交
1707 1708 1709
}

/***                     Floating-Point multiply-and-add                   ***/
1710
/* fmadd - fmadds */
1711
GEN_FLOAT_ACB(madd, 0x1D, PPC_FLOAT);
1712
/* fmsub - fmsubs */
1713
GEN_FLOAT_ACB(msub, 0x1C, PPC_FLOAT);
1714
/* fnmadd - fnmadds */
1715
GEN_FLOAT_ACB(nmadd, 0x1F, PPC_FLOAT);
1716
/* fnmsub - fnmsubs */
1717
GEN_FLOAT_ACB(nmsub, 0x1E, PPC_FLOAT);
B
bellard 已提交
1718 1719 1720

/***                     Floating-Point round & convert                    ***/
/* fctiw */
1721
GEN_FLOAT_B(ctiw, 0x0E, 0x00, PPC_FLOAT);
B
bellard 已提交
1722
/* fctiwz */
1723
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, PPC_FLOAT);
B
bellard 已提交
1724
/* frsp */
1725
GEN_FLOAT_B(rsp, 0x0C, 0x00, PPC_FLOAT);
J
j_mayer 已提交
1726 1727
#if defined(TARGET_PPC64)
/* fcfid */
1728
GEN_FLOAT_B(cfid, 0x0E, 0x1A, PPC_64B);
J
j_mayer 已提交
1729
/* fctid */
1730
GEN_FLOAT_B(ctid, 0x0E, 0x19, PPC_64B);
J
j_mayer 已提交
1731
/* fctidz */
1732
GEN_FLOAT_B(ctidz, 0x0F, 0x19, PPC_64B);
J
j_mayer 已提交
1733
#endif
B
bellard 已提交
1734

1735 1736 1737 1738 1739 1740 1741 1742 1743
/* frin */
GEN_FLOAT_B(rin, 0x08, 0x0C, PPC_FLOAT_EXT);
/* friz */
GEN_FLOAT_B(riz, 0x08, 0x0D, PPC_FLOAT_EXT);
/* frip */
GEN_FLOAT_B(rip, 0x08, 0x0E, PPC_FLOAT_EXT);
/* frim */
GEN_FLOAT_B(rim, 0x08, 0x0F, PPC_FLOAT_EXT);

B
bellard 已提交
1744 1745
/***                         Floating-Point compare                        ***/
/* fcmpo */
1746
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1747
{
1748
    if (unlikely(!ctx->fpu_enabled)) {
1749
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1750 1751
        return;
    }
1752 1753 1754 1755 1756
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpo();
    gen_op_store_T0_crf(crfD(ctx->opcode));
B
bellard 已提交
1757 1758 1759
}

/* fcmpu */
1760
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1761
{
1762
    if (unlikely(!ctx->fpu_enabled)) {
1763
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1764 1765
        return;
    }
1766 1767 1768 1769 1770
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpu();
    gen_op_store_T0_crf(crfD(ctx->opcode));
B
bellard 已提交
1771 1772
}

1773 1774
/***                         Floating-point move                           ***/
/* fabs */
1775
GEN_FLOAT_B(abs, 0x08, 0x08, PPC_FLOAT);
1776 1777 1778 1779

/* fmr  - fmr. */
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
1780
    if (unlikely(!ctx->fpu_enabled)) {
1781
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1782 1783
        return;
    }
1784 1785 1786
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1787
    if (unlikely(Rc(ctx->opcode) != 0))
1788 1789 1790 1791
        gen_op_set_Rc1();
}

/* fnabs */
1792
GEN_FLOAT_B(nabs, 0x08, 0x04, PPC_FLOAT);
1793
/* fneg */
1794
GEN_FLOAT_B(neg, 0x08, 0x01, PPC_FLOAT);
1795

B
bellard 已提交
1796 1797 1798 1799
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
1800
    if (unlikely(!ctx->fpu_enabled)) {
1801
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1802 1803
        return;
    }
B
bellard 已提交
1804 1805 1806
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
    gen_op_clear_fpscr(crfS(ctx->opcode));
B
bellard 已提交
1807 1808 1809 1810 1811
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
1812
    if (unlikely(!ctx->fpu_enabled)) {
1813
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1814 1815
        return;
    }
1816
    gen_op_load_fpscr();
B
bellard 已提交
1817
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1818
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1819
        gen_op_set_Rc1();
B
bellard 已提交
1820 1821 1822 1823 1824
}

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

1827
    if (unlikely(!ctx->fpu_enabled)) {
1828
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1829 1830
        return;
    }
B
bellard 已提交
1831 1832
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
1833
    gen_op_andi_T0(~(1 << (crbD(ctx->opcode) & 0x03)));
B
bellard 已提交
1834
    gen_op_store_T0_fpscr(crb);
1835
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1836
        gen_op_set_Rc1();
B
bellard 已提交
1837 1838 1839 1840 1841
}

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

1844
    if (unlikely(!ctx->fpu_enabled)) {
1845
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1846 1847
        return;
    }
B
bellard 已提交
1848 1849 1850 1851
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
    gen_op_store_T0_fpscr(crb);
1852
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1853
        gen_op_set_Rc1();
B
bellard 已提交
1854 1855 1856 1857 1858
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
1859
    if (unlikely(!ctx->fpu_enabled)) {
1860
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1861 1862
        return;
    }
B
bellard 已提交
1863
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1864
    gen_op_store_fpscr(FM(ctx->opcode));
1865
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1866
        gen_op_set_Rc1();
B
bellard 已提交
1867 1868 1869 1870 1871
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
1872
    if (unlikely(!ctx->fpu_enabled)) {
1873
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1874 1875
        return;
    }
B
bellard 已提交
1876
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
1877
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1878
        gen_op_set_Rc1();
B
bellard 已提交
1879 1880
}

1881 1882
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
J
j_mayer 已提交
1883
static inline void gen_addr_imm_index (DisasContext *ctx, int maskl)
1884 1885 1886
{
    target_long simm = SIMM(ctx->opcode);

J
j_mayer 已提交
1887 1888
    if (maskl)
        simm &= ~0x03;
1889
    if (rA(ctx->opcode) == 0) {
1890
        gen_set_T0(simm);
1891 1892 1893 1894 1895
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm);
    }
1896 1897 1898
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
}

static inline void gen_addr_reg_index (DisasContext *ctx)
{
    if (rA(ctx->opcode) == 0) {
        gen_op_load_gpr_T0(rB(ctx->opcode));
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_add();
    }
1910 1911 1912
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1913 1914 1915 1916 1917 1918 1919 1920 1921
}

static inline void gen_addr_register (DisasContext *ctx)
{
    if (rA(ctx->opcode) == 0) {
        gen_op_reset_T0();
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
    }
1922 1923 1924
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1925 1926
}

B
bellard 已提交
1927
/***                             Integer load                              ***/
1928
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
1929
#if defined(CONFIG_USER_ONLY)
1930
#if defined(TARGET_PPC64)
1931 1932 1933 1934
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_raw,                                                   \
    &gen_op_l##width##_le_raw,                                                \
1935 1936
    &gen_op_l##width##_64_raw,                                                \
    &gen_op_l##width##_le_64_raw,                                             \
1937 1938 1939 1940 1941
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_raw,                                                  \
    &gen_op_st##width##_le_raw,                                               \
1942 1943
    &gen_op_st##width##_64_raw,                                               \
    &gen_op_st##width##_le_64_raw,                                            \
1944 1945
};
/* Byte access routine are endian safe */
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
#define gen_op_stb_le_64_raw gen_op_stb_64_raw
#define gen_op_lbz_le_64_raw gen_op_lbz_64_raw
#else
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_raw,                                                   \
    &gen_op_l##width##_le_raw,                                                \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_raw,                                                  \
    &gen_op_st##width##_le_raw,                                               \
};
#endif
/* Byte access routine are endian safe */
1961 1962
#define gen_op_stb_le_raw gen_op_stb_raw
#define gen_op_lbz_le_raw gen_op_lbz_raw
1963
#else
1964
#if defined(TARGET_PPC64)
1965 1966 1967
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
1968
    &gen_op_l##width##_le_user,                                               \
1969
    &gen_op_l##width##_kernel,                                                \
1970
    &gen_op_l##width##_le_kernel,                                             \
1971 1972 1973 1974
    &gen_op_l##width##_64_user,                                               \
    &gen_op_l##width##_le_64_user,                                            \
    &gen_op_l##width##_64_kernel,                                             \
    &gen_op_l##width##_le_64_kernel,                                          \
1975
};
1976 1977 1978
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
1979
    &gen_op_st##width##_le_user,                                              \
1980
    &gen_op_st##width##_kernel,                                               \
1981
    &gen_op_st##width##_le_kernel,                                            \
1982 1983 1984 1985
    &gen_op_st##width##_64_user,                                              \
    &gen_op_st##width##_le_64_user,                                           \
    &gen_op_st##width##_64_kernel,                                            \
    &gen_op_st##width##_le_64_kernel,                                         \
1986 1987
};
/* Byte access routine are endian safe */
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
#define gen_op_stb_le_64_user gen_op_stb_64_user
#define gen_op_lbz_le_64_user gen_op_lbz_64_user
#define gen_op_stb_le_64_kernel gen_op_stb_64_kernel
#define gen_op_lbz_le_64_kernel gen_op_lbz_64_kernel
#else
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
    &gen_op_l##width##_le_user,                                               \
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
    &gen_op_st##width##_le_user,                                              \
    &gen_op_st##width##_kernel,                                               \
    &gen_op_st##width##_le_kernel,                                            \
};
#endif
/* Byte access routine are endian safe */
2009 2010 2011 2012
#define gen_op_stb_le_user gen_op_stb_user
#define gen_op_lbz_le_user gen_op_lbz_user
#define gen_op_stb_le_kernel gen_op_stb_kernel
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
2013 2014
#endif

2015 2016
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2017
{                                                                             \
J
j_mayer 已提交
2018
    gen_addr_imm_index(ctx, 0);                                               \
2019
    op_ldst(l##width);                                                        \
B
bellard 已提交
2020 2021 2022
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2023 2024
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2025
{                                                                             \
2026 2027
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2028
        GEN_EXCP_INVAL(ctx);                                                  \
2029
        return;                                                               \
2030
    }                                                                         \
J
j_mayer 已提交
2031 2032 2033 2034
    if (type == PPC_64B)                                                      \
        gen_addr_imm_index(ctx, 1);                                           \
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
2035
    op_ldst(l##width);                                                        \
B
bellard 已提交
2036 2037 2038 2039
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2040 2041
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2042
{                                                                             \
2043 2044
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2045
        GEN_EXCP_INVAL(ctx);                                                  \
2046
        return;                                                               \
2047
    }                                                                         \
2048
    gen_addr_reg_index(ctx);                                                  \
2049
    op_ldst(l##width);                                                        \
B
bellard 已提交
2050 2051 2052 2053
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2054 2055
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2056
{                                                                             \
2057
    gen_addr_reg_index(ctx);                                                  \
2058
    op_ldst(l##width);                                                        \
B
bellard 已提交
2059 2060 2061
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2062
#define GEN_LDS(width, op, type)                                              \
2063
OP_LD_TABLE(width);                                                           \
2064 2065 2066 2067
GEN_LD(width, op | 0x20, type);                                               \
GEN_LDU(width, op | 0x21, type);                                              \
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
GEN_LDX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2068 2069

/* lbz lbzu lbzux lbzx */
2070
GEN_LDS(bz, 0x02, PPC_INTEGER);
B
bellard 已提交
2071
/* lha lhau lhaux lhax */
2072
GEN_LDS(ha, 0x0A, PPC_INTEGER);
B
bellard 已提交
2073
/* lhz lhzu lhzux lhzx */
2074
GEN_LDS(hz, 0x08, PPC_INTEGER);
B
bellard 已提交
2075
/* lwz lwzu lwzux lwzx */
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
GEN_LDS(wz, 0x00, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_LD_TABLE(wa);
OP_LD_TABLE(d);
/* lwaux */
GEN_LDUX(wa, 0x15, 0x0B, PPC_64B);
/* lwax */
GEN_LDX(wa, 0x15, 0x0A, PPC_64B);
/* ldux */
GEN_LDUX(d, 0x15, 0x01, PPC_64B);
/* ldx */
GEN_LDX(d, 0x15, 0x00, PPC_64B);
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2093
            GEN_EXCP_INVAL(ctx);
2094 2095 2096
            return;
        }
    }
J
j_mayer 已提交
2097
    gen_addr_imm_index(ctx, 1);
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
        op_ldst(lwa);
    } else {
        /* ld - ldu */
        op_ldst(ld);
    }
    gen_op_store_T1_gpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
#endif
B
bellard 已提交
2110 2111

/***                              Integer store                            ***/
2112 2113
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2114
{                                                                             \
J
j_mayer 已提交
2115
    gen_addr_imm_index(ctx, 0);                                               \
2116 2117
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2118 2119
}

2120 2121
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2122
{                                                                             \
2123
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2124
        GEN_EXCP_INVAL(ctx);                                                  \
2125
        return;                                                               \
2126
    }                                                                         \
J
j_mayer 已提交
2127 2128 2129 2130
    if (type == PPC_64B)                                                      \
        gen_addr_imm_index(ctx, 1);                                           \
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
B
bellard 已提交
2131
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2132
    op_ldst(st##width);                                                       \
B
bellard 已提交
2133 2134 2135
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2136 2137
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2138
{                                                                             \
2139
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2140
        GEN_EXCP_INVAL(ctx);                                                  \
2141
        return;                                                               \
2142
    }                                                                         \
2143
    gen_addr_reg_index(ctx);                                                  \
2144 2145
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2146 2147 2148
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2149 2150
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2151
{                                                                             \
2152
    gen_addr_reg_index(ctx);                                                  \
2153 2154
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2155 2156
}

2157
#define GEN_STS(width, op, type)                                              \
2158
OP_ST_TABLE(width);                                                           \
2159 2160 2161 2162
GEN_ST(width, op | 0x20, type);                                               \
GEN_STU(width, op | 0x21, type);                                              \
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
GEN_STX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2163 2164

/* stb stbu stbux stbx */
2165
GEN_STS(b, 0x06, PPC_INTEGER);
B
bellard 已提交
2166
/* sth sthu sthux sthx */
2167
GEN_STS(h, 0x0C, PPC_INTEGER);
B
bellard 已提交
2168
/* stw stwu stwux stwx */
2169 2170 2171
GEN_STS(w, 0x04, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_ST_TABLE(d);
J
j_mayer 已提交
2172 2173
GEN_STUX(d, 0x15, 0x05, PPC_64B);
GEN_STX(d, 0x15, 0x04, PPC_64B);
2174 2175 2176 2177
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000002, PPC_64B)
{
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0)) {
2178
            GEN_EXCP_INVAL(ctx);
2179 2180 2181
            return;
        }
    }
J
j_mayer 已提交
2182
    gen_addr_imm_index(ctx, 1);
2183 2184 2185 2186 2187 2188
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_ldst(std);
    if (Rc(ctx->opcode))
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
#endif
B
bellard 已提交
2189 2190
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
2191
OP_LD_TABLE(hbr);
2192
GEN_LDX(hbr, 0x16, 0x18, PPC_INTEGER);
B
bellard 已提交
2193
/* lwbrx */
2194
OP_LD_TABLE(wbr);
2195
GEN_LDX(wbr, 0x16, 0x10, PPC_INTEGER);
B
bellard 已提交
2196
/* sthbrx */
2197
OP_ST_TABLE(hbr);
2198
GEN_STX(hbr, 0x16, 0x1C, PPC_INTEGER);
B
bellard 已提交
2199
/* stwbrx */
2200
OP_ST_TABLE(wbr);
2201
GEN_STX(wbr, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
2202 2203

/***                    Integer load and store multiple                    ***/
2204
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
#if defined(TARGET_PPC64)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_raw,
    &gen_op_lmw_le_raw,
    &gen_op_lmw_64_raw,
    &gen_op_lmw_le_64_raw,
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_64_raw,
    &gen_op_stmw_le_64_raw,
};
#else
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
    &gen_op_lmw_le_user,
    &gen_op_lmw_kernel,
    &gen_op_lmw_le_kernel,
    &gen_op_lmw_64_user,
    &gen_op_lmw_le_64_user,
    &gen_op_lmw_64_kernel,
    &gen_op_lmw_le_64_kernel,
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
    &gen_op_stmw_le_user,
    &gen_op_stmw_kernel,
    &gen_op_stmw_le_kernel,
    &gen_op_stmw_64_user,
    &gen_op_stmw_le_64_user,
    &gen_op_stmw_64_kernel,
    &gen_op_stmw_le_64_kernel,
};
#endif
#else
2240
#if defined(CONFIG_USER_ONLY)
2241 2242 2243 2244 2245 2246 2247 2248
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_raw,
    &gen_op_lmw_le_raw,
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_raw,
    &gen_op_stmw_le_raw,
};
2249 2250 2251
#else
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
2252
    &gen_op_lmw_le_user,
2253
    &gen_op_lmw_kernel,
2254
    &gen_op_lmw_le_kernel,
2255 2256 2257
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
2258
    &gen_op_stmw_le_user,
2259
    &gen_op_stmw_kernel,
2260
    &gen_op_stmw_le_kernel,
2261 2262
};
#endif
2263
#endif
2264

B
bellard 已提交
2265 2266 2267
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2268
    /* NIP cannot be restored if the memory exception comes from an helper */
2269
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2270
    gen_addr_imm_index(ctx, 0);
2271
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2272 2273 2274 2275 2276
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2277
    /* NIP cannot be restored if the memory exception comes from an helper */
2278
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2279
    gen_addr_imm_index(ctx, 0);
2280
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2281 2282 2283
}

/***                    Integer load and store strings                     ***/
2284 2285
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
#if defined(TARGET_PPC64)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
    &gen_op_lswi_64_raw,
    &gen_op_lswi_le_64_raw,
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
    &gen_op_lswx_64_raw,
    &gen_op_lswx_le_64_raw,
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
    &gen_op_stsw_64_raw,
    &gen_op_stsw_le_64_raw,
};
#else
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
    &gen_op_lswi_le_user,
    &gen_op_lswi_kernel,
    &gen_op_lswi_le_kernel,
    &gen_op_lswi_64_user,
    &gen_op_lswi_le_64_user,
    &gen_op_lswi_64_kernel,
    &gen_op_lswi_le_64_kernel,
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
    &gen_op_lswx_le_user,
    &gen_op_lswx_kernel,
    &gen_op_lswx_le_kernel,
    &gen_op_lswx_64_user,
    &gen_op_lswx_le_64_user,
    &gen_op_lswx_64_kernel,
    &gen_op_lswx_le_64_kernel,
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
    &gen_op_stsw_le_user,
    &gen_op_stsw_kernel,
    &gen_op_stsw_le_kernel,
    &gen_op_stsw_64_user,
    &gen_op_stsw_le_64_user,
    &gen_op_stsw_64_kernel,
    &gen_op_stsw_le_64_kernel,
};
#endif
#else
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
#if defined(CONFIG_USER_ONLY)
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
};
#else
2353 2354
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
2355
    &gen_op_lswi_le_user,
2356
    &gen_op_lswi_kernel,
2357
    &gen_op_lswi_le_kernel,
2358 2359 2360
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
2361
    &gen_op_lswx_le_user,
2362
    &gen_op_lswx_kernel,
2363
    &gen_op_lswx_le_kernel,
2364 2365 2366
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
2367
    &gen_op_stsw_le_user,
2368
    &gen_op_stsw_kernel,
2369
    &gen_op_stsw_le_kernel,
2370 2371
};
#endif
2372
#endif
2373

B
bellard 已提交
2374
/* lswi */
2375
/* PowerPC32 specification says we must generate an exception if
2376 2377 2378 2379
 * 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...
 */
B
bellard 已提交
2380 2381 2382 2383
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
2384
    int ra = rA(ctx->opcode);
B
bellard 已提交
2385 2386 2387 2388 2389
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
2390 2391 2392
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2393 2394
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2395
        return;
B
bellard 已提交
2396
    }
2397
    /* NIP cannot be restored if the memory exception comes from an helper */
2398
    gen_update_nip(ctx, ctx->nip - 4);
2399 2400
    gen_addr_register(ctx);
    gen_op_set_T1(nb);
2401
    op_ldsts(lswi, start);
B
bellard 已提交
2402 2403 2404 2405 2406
}

/* lswx */
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
{
2407 2408 2409
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

2410
    /* NIP cannot be restored if the memory exception comes from an helper */
2411
    gen_update_nip(ctx, ctx->nip - 4);
2412
    gen_addr_reg_index(ctx);
2413 2414
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
2415
    }
2416 2417
    gen_op_load_xer_bc();
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
2418 2419 2420 2421 2422
}

/* stswi */
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
{
B
bellard 已提交
2423 2424
    int nb = NB(ctx->opcode);

2425
    /* NIP cannot be restored if the memory exception comes from an helper */
2426
    gen_update_nip(ctx, ctx->nip - 4);
2427
    gen_addr_register(ctx);
B
bellard 已提交
2428 2429 2430
    if (nb == 0)
        nb = 32;
    gen_op_set_T1(nb);
2431
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2432 2433 2434 2435 2436
}

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
2437
    /* NIP cannot be restored if the memory exception comes from an helper */
2438
    gen_update_nip(ctx, ctx->nip - 4);
2439 2440
    gen_addr_reg_index(ctx);
    gen_op_load_xer_bc();
2441
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2442 2443 2444 2445
}

/***                        Memory synchronisation                         ***/
/* eieio */
2446
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM_EIEIO)
B
bellard 已提交
2447 2448 2449 2450
{
}

/* isync */
2451
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FF0801, PPC_MEM)
B
bellard 已提交
2452
{
2453
    GEN_STOP(ctx);
B
bellard 已提交
2454 2455
}

2456 2457
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2458
#if defined(TARGET_PPC64)
2459
#if defined(CONFIG_USER_ONLY)
2460 2461 2462
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_raw,
    &gen_op_lwarx_le_raw,
2463 2464
    &gen_op_lwarx_64_raw,
    &gen_op_lwarx_le_64_raw,
2465 2466 2467 2468
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_raw,
    &gen_op_stwcx_le_raw,
2469 2470
    &gen_op_stwcx_64_raw,
    &gen_op_stwcx_le_64_raw,
2471
};
2472
#else
B
bellard 已提交
2473 2474
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
2475
    &gen_op_lwarx_le_user,
B
bellard 已提交
2476
    &gen_op_lwarx_kernel,
2477
    &gen_op_lwarx_le_kernel,
2478 2479 2480 2481
    &gen_op_lwarx_64_user,
    &gen_op_lwarx_le_64_user,
    &gen_op_lwarx_64_kernel,
    &gen_op_lwarx_le_64_kernel,
B
bellard 已提交
2482
};
2483 2484
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
2485
    &gen_op_stwcx_le_user,
2486
    &gen_op_stwcx_kernel,
2487
    &gen_op_stwcx_le_kernel,
2488 2489 2490 2491
    &gen_op_stwcx_64_user,
    &gen_op_stwcx_le_64_user,
    &gen_op_stwcx_64_kernel,
    &gen_op_stwcx_le_64_kernel,
2492 2493
};
#endif
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
#else
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_raw,
    &gen_op_lwarx_le_raw,
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_raw,
    &gen_op_stwcx_le_raw,
};
#else
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
    &gen_op_lwarx_le_user,
    &gen_op_lwarx_kernel,
    &gen_op_lwarx_le_kernel,
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
    &gen_op_stwcx_le_user,
    &gen_op_stwcx_kernel,
    &gen_op_stwcx_le_kernel,
};
#endif
#endif
2519

2520
/* lwarx */
2521
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
2522
{
2523
    gen_addr_reg_index(ctx);
B
bellard 已提交
2524
    op_lwarx();
B
bellard 已提交
2525 2526 2527 2528
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stwcx. */
2529
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
2530
{
2531
    gen_addr_reg_index(ctx);
2532 2533
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stwcx();
B
bellard 已提交
2534 2535
}

J
j_mayer 已提交
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
#if defined(TARGET_PPC64)
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_ldarx[] = {
    &gen_op_ldarx_raw,
    &gen_op_ldarx_le_raw,
    &gen_op_ldarx_64_raw,
    &gen_op_ldarx_le_64_raw,
};
static GenOpFunc *gen_op_stdcx[] = {
    &gen_op_stdcx_raw,
    &gen_op_stdcx_le_raw,
    &gen_op_stdcx_64_raw,
    &gen_op_stdcx_le_64_raw,
};
#else
static GenOpFunc *gen_op_ldarx[] = {
    &gen_op_ldarx_user,
    &gen_op_ldarx_le_user,
    &gen_op_ldarx_kernel,
    &gen_op_ldarx_le_kernel,
    &gen_op_ldarx_64_user,
    &gen_op_ldarx_le_64_user,
    &gen_op_ldarx_64_kernel,
    &gen_op_ldarx_le_64_kernel,
};
static GenOpFunc *gen_op_stdcx[] = {
    &gen_op_stdcx_user,
    &gen_op_stdcx_le_user,
    &gen_op_stdcx_kernel,
    &gen_op_stdcx_le_kernel,
    &gen_op_stdcx_64_user,
    &gen_op_stdcx_le_64_user,
    &gen_op_stdcx_64_kernel,
    &gen_op_stdcx_le_64_kernel,
};
#endif

/* ldarx */
2576
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
2577 2578 2579 2580 2581 2582 2583
{
    gen_addr_reg_index(ctx);
    op_ldarx();
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stdcx. */
2584
GEN_HANDLER(stdcx_, 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
2585 2586 2587 2588 2589 2590 2591
{
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
2592
/* sync */
2593
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03CF0801, PPC_MEM_SYNC)
B
bellard 已提交
2594 2595 2596 2597
{
}

/***                         Floating-point load                           ***/
2598 2599
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2600
{                                                                             \
2601
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2602
        GEN_EXCP_NO_FP(ctx);                                                  \
2603 2604
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2605
    gen_addr_imm_index(ctx, 0);                                               \
2606
    op_ldst(l##width);                                                        \
2607
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2608 2609
}

2610 2611
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2612
{                                                                             \
2613
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2614
        GEN_EXCP_NO_FP(ctx);                                                  \
2615 2616
        return;                                                               \
    }                                                                         \
2617
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2618
        GEN_EXCP_INVAL(ctx);                                                  \
2619
        return;                                                               \
2620
    }                                                                         \
J
j_mayer 已提交
2621
    gen_addr_imm_index(ctx, 0);                                               \
2622
    op_ldst(l##width);                                                        \
2623
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2624 2625 2626
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2627 2628
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
2629
{                                                                             \
2630
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2631
        GEN_EXCP_NO_FP(ctx);                                                  \
2632 2633
        return;                                                               \
    }                                                                         \
2634
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2635
        GEN_EXCP_INVAL(ctx);                                                  \
2636
        return;                                                               \
2637
    }                                                                         \
2638
    gen_addr_reg_index(ctx);                                                  \
2639
    op_ldst(l##width);                                                        \
2640
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2641 2642 2643
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2644 2645
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2646
{                                                                             \
2647
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2648
        GEN_EXCP_NO_FP(ctx);                                                  \
2649 2650
        return;                                                               \
    }                                                                         \
2651
    gen_addr_reg_index(ctx);                                                  \
2652
    op_ldst(l##width);                                                        \
2653
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2654 2655
}

2656
#define GEN_LDFS(width, op, type)                                             \
2657
OP_LD_TABLE(width);                                                           \
2658 2659 2660 2661
GEN_LDF(width, op | 0x20, type);                                              \
GEN_LDUF(width, op | 0x21, type);                                             \
GEN_LDUXF(width, op | 0x01, type);                                            \
GEN_LDXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2662 2663

/* lfd lfdu lfdux lfdx */
2664
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
2665
/* lfs lfsu lfsux lfsx */
2666
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
2667 2668

/***                         Floating-point store                          ***/
2669 2670
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2671
{                                                                             \
2672
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2673
        GEN_EXCP_NO_FP(ctx);                                                  \
2674 2675
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2676
    gen_addr_imm_index(ctx, 0);                                               \
2677
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2678
    op_ldst(st##width);                                                       \
B
bellard 已提交
2679 2680
}

2681 2682
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2683
{                                                                             \
2684
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2685
        GEN_EXCP_NO_FP(ctx);                                                  \
2686 2687
        return;                                                               \
    }                                                                         \
2688
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2689
        GEN_EXCP_INVAL(ctx);                                                  \
2690
        return;                                                               \
2691
    }                                                                         \
J
j_mayer 已提交
2692
    gen_addr_imm_index(ctx, 0);                                               \
2693
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2694
    op_ldst(st##width);                                                       \
B
bellard 已提交
2695 2696 2697
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2698 2699
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
2700
{                                                                             \
2701
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2702
        GEN_EXCP_NO_FP(ctx);                                                  \
2703 2704
        return;                                                               \
    }                                                                         \
2705
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2706
        GEN_EXCP_INVAL(ctx);                                                  \
2707
        return;                                                               \
2708
    }                                                                         \
2709 2710
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2711
    op_ldst(st##width);                                                       \
B
bellard 已提交
2712 2713 2714
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2715 2716
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2717
{                                                                             \
2718
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2719
        GEN_EXCP_NO_FP(ctx);                                                  \
2720 2721
        return;                                                               \
    }                                                                         \
2722 2723
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2724
    op_ldst(st##width);                                                       \
B
bellard 已提交
2725 2726
}

2727
#define GEN_STFS(width, op, type)                                             \
2728
OP_ST_TABLE(width);                                                           \
2729 2730 2731 2732
GEN_STF(width, op | 0x20, type);                                              \
GEN_STUF(width, op | 0x21, type);                                             \
GEN_STUXF(width, op | 0x01, type);                                            \
GEN_STXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2733 2734

/* stfd stfdu stfdux stfdx */
2735
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
2736
/* stfs stfsu stfsux stfsx */
2737
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
2738 2739 2740

/* Optional: */
/* stfiwx */
2741 2742
OP_ST_TABLE(fiwx);
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
2743 2744

/***                                Branch                                 ***/
2745
static inline void gen_goto_tb (DisasContext *ctx, int n, target_ulong dest)
2746 2747 2748 2749 2750 2751 2752 2753
{
    TranslationBlock *tb;
    tb = ctx->tb;
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
        if (n == 0)
            gen_op_goto_tb0(TBPARAM(tb));
        else
            gen_op_goto_tb1(TBPARAM(tb));
2754 2755 2756 2757 2758 2759 2760
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2761
        gen_op_set_T0((long)tb + n);
2762 2763
        if (ctx->singlestep_enabled)
            gen_op_debug();
2764 2765
        gen_op_exit_tb();
    } else {
2766 2767 2768 2769 2770 2771 2772
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2773
        gen_op_reset_T0();
2774 2775
        if (ctx->singlestep_enabled)
            gen_op_debug();
2776 2777
        gen_op_exit_tb();
    }
B
bellard 已提交
2778 2779
}

2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
static inline void gen_setlr (DisasContext *ctx, target_ulong nip)
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode != 0 && (nip >> 32))
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
    else
#endif
        gen_op_setlr(ctx->nip);
}

B
bellard 已提交
2790 2791 2792
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
2793
    target_ulong li, target;
B
bellard 已提交
2794 2795

    /* sign extend LI */
2796
#if defined(TARGET_PPC64)
2797 2798 2799
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
2800
#endif
2801
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
2802
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
2803
        target = ctx->nip + li - 4;
B
bellard 已提交
2804
    else
2805
        target = li;
2806
#if defined(TARGET_PPC64)
2807 2808
    if (!ctx->sf_mode)
        target = (uint32_t)target;
2809
#endif
2810 2811
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
2812
    gen_goto_tb(ctx, 0, target);
2813
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
2814 2815
}

2816 2817 2818 2819
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

2820
static inline void gen_bcond (DisasContext *ctx, int type)
2821
{
2822 2823
    target_ulong target = 0;
    target_ulong li;
2824 2825 2826
    uint32_t bo = BO(ctx->opcode);
    uint32_t bi = BI(ctx->opcode);
    uint32_t mask;
2827 2828

    if ((bo & 0x4) == 0)
2829
        gen_op_dec_ctr();
2830 2831
    switch(type) {
    case BCOND_IM:
2832 2833
        li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
B
bellard 已提交
2834
            target = ctx->nip + li - 4;
2835 2836 2837
        } else {
            target = li;
        }
2838 2839 2840 2841
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode)
            target = (uint32_t)target;
#endif
2842 2843 2844 2845 2846 2847 2848 2849 2850
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
2851 2852
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
2853
    if (bo & 0x10) {
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
        /* No CR condition */
        switch (bo & 0x6) {
        case 0:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctr_64();
            else
#endif
                gen_op_test_ctr();
            break;
        case 2:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctrz_64();
            else
#endif
                gen_op_test_ctrz();
2871 2872
            break;
        default:
2873 2874
        case 4:
        case 6:
2875
            if (type == BCOND_IM) {
2876
                gen_goto_tb(ctx, 0, target);
2877
            } else {
2878 2879 2880 2881 2882 2883
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_b_T1_64();
                else
#endif
                    gen_op_b_T1();
2884
                gen_op_reset_T0();
2885 2886 2887
            }
            goto no_test;
        }
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
    } else {
        mask = 1 << (3 - (bi & 0x03));
        gen_op_load_crf_T0(bi >> 2);
        if (bo & 0x8) {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_true_64(mask);
                else
#endif
                    gen_op_test_ctr_true(mask);
                break;
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_true_64(mask);
                else
#endif
                    gen_op_test_ctrz_true(mask);
                break;
            default:
            case 4:
            case 6:
2912
                gen_op_test_true(mask);
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
                break;
            }
        } else {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_false_64(mask);
                else
#endif
                    gen_op_test_ctr_false(mask);
2924
                break;
2925 2926 2927 2928 2929 2930 2931 2932
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_false_64(mask);
                else
#endif
                    gen_op_test_ctrz_false(mask);
                break;
2933
            default:
2934 2935
            case 4:
            case 6:
2936
                gen_op_test_false(mask);
2937 2938 2939 2940
                break;
            }
        }
    }
2941
    if (type == BCOND_IM) {
B
bellard 已提交
2942 2943
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
2944
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
2945
        gen_set_label(l1);
2946
        gen_goto_tb(ctx, 1, ctx->nip);
2947
    } else {
2948 2949 2950 2951 2952 2953
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
        else
#endif
            gen_op_btest_T1(ctx->nip);
2954
        gen_op_reset_T0();
2955
    no_test:
J
j_mayer 已提交
2956 2957 2958 2959
        if (ctx->singlestep_enabled)
            gen_op_debug();
        gen_op_exit_tb();
    }
2960
    ctx->exception = POWERPC_EXCP_BRANCH;
2961 2962 2963
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
2964
{
2965 2966 2967 2968
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
2969
{
2970 2971 2972 2973
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
2974
{
2975 2976
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993

/***                      Condition register logical                       ***/
#define GEN_CRLOGIC(op, opc)                                                  \
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
{                                                                             \
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
    gen_op_##op();                                                            \
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
                     3 - (crbD(ctx->opcode) & 0x03));                         \
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
}

/* crand */
2994
GEN_CRLOGIC(and, 0x08);
B
bellard 已提交
2995
/* crandc */
2996
GEN_CRLOGIC(andc, 0x04);
B
bellard 已提交
2997
/* creqv */
2998
GEN_CRLOGIC(eqv, 0x09);
B
bellard 已提交
2999
/* crnand */
3000
GEN_CRLOGIC(nand, 0x07);
B
bellard 已提交
3001
/* crnor */
3002
GEN_CRLOGIC(nor, 0x01);
B
bellard 已提交
3003
/* cror */
3004
GEN_CRLOGIC(or, 0x0E);
B
bellard 已提交
3005
/* crorc */
3006
GEN_CRLOGIC(orc, 0x0D);
B
bellard 已提交
3007
/* crxor */
3008
GEN_CRLOGIC(xor, 0x06);
B
bellard 已提交
3009 3010 3011 3012 3013 3014 3015 3016 3017
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
    gen_op_load_crf_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

/***                           System linkage                              ***/
/* rfi (supervisor only) */
3018
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3019
{
3020
#if defined(CONFIG_USER_ONLY)
3021
    GEN_EXCP_PRIVOPC(ctx);
3022 3023
#else
    /* Restore CPU state */
3024
    if (unlikely(!ctx->supervisor)) {
3025
        GEN_EXCP_PRIVOPC(ctx);
3026
        return;
3027
    }
3028
    gen_op_rfi();
3029
    GEN_SYNC(ctx);
3030
#endif
B
bellard 已提交
3031 3032
}

J
j_mayer 已提交
3033
#if defined(TARGET_PPC64)
3034
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3035 3036
{
#if defined(CONFIG_USER_ONLY)
3037
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3038 3039 3040
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3041
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3042 3043
        return;
    }
3044
    gen_op_rfid();
3045
    GEN_SYNC(ctx);
J
j_mayer 已提交
3046 3047 3048 3049
#endif
}
#endif

B
bellard 已提交
3050
/* sc */
3051
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3052
{
3053 3054 3055
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3056
#if defined(CONFIG_USER_ONLY)
3057
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL_USER, lev);
3058
#else
3059
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL, lev);
3060
#endif
B
bellard 已提交
3061 3062 3063 3064
}

/***                                Trap                                   ***/
/* tw */
3065
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3066
{
3067 3068
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3069
    /* Update the nip since this might generate a trap exception */
3070
    gen_update_nip(ctx, ctx->nip);
3071
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3072 3073 3074 3075 3076
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3077
    gen_op_load_gpr_T0(rA(ctx->opcode));
3078 3079 3080
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3081
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3082 3083
}

3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}
#endif

B
bellard 已提交
3106 3107 3108 3109 3110 3111
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
    gen_op_load_xer_cr();
    gen_op_store_T0_crf(crfD(ctx->opcode));
J
j_mayer 已提交
3112 3113
    gen_op_clear_xer_ov();
    gen_op_clear_xer_ca();
B
bellard 已提交
3114 3115 3116
}

/* mfcr */
3117
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3118
{
3119
    uint32_t crm, crn;
3120

3121 3122 3123 3124 3125 3126
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
            gen_op_load_cro(7 - crn);
        }
3127 3128 3129
    } else {
        gen_op_load_cr();
    }
B
bellard 已提交
3130 3131 3132 3133 3134 3135
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3136
#if defined(CONFIG_USER_ONLY)
3137
    GEN_EXCP_PRIVREG(ctx);
3138
#else
3139
    if (unlikely(!ctx->supervisor)) {
3140
        GEN_EXCP_PRIVREG(ctx);
3141
        return;
3142
    }
B
bellard 已提交
3143 3144
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
3145
#endif
B
bellard 已提交
3146 3147
}

3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
#if 0
#define SPR_NOACCESS ((void *)(-1))
#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 已提交
3159
/* mfspr */
3160
static inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3161
{
3162
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3163 3164
    uint32_t sprn = SPR(ctx->opcode);

3165 3166 3167 3168
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3169
#endif
3170
        read_cb = ctx->spr_cb[sprn].uea_read;
3171 3172
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3173 3174 3175 3176
            (*read_cb)(ctx, sprn);
            gen_op_store_T0_gpr(rD(ctx->opcode));
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3177
            if (loglevel != 0) {
B
blueswir1 已提交
3178
                fprintf(logfile, "Trying to read privileged spr %d %03x\n",
3179 3180
                        sprn, sprn);
            }
B
blueswir1 已提交
3181
            printf("Trying to read privileged spr %d %03x\n", sprn, sprn);
3182
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3183
        }
3184 3185
    } else {
        /* Not defined */
J
j_mayer 已提交
3186
        if (loglevel != 0) {
3187 3188 3189
            fprintf(logfile, "Trying to read invalid spr %d %03x\n",
                    sprn, sprn);
        }
3190
        printf("Trying to read invalid spr %d %03x\n", sprn, sprn);
3191 3192
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3193 3194 3195
    }
}

3196
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3197
{
3198
    gen_op_mfspr(ctx);
3199
}
3200 3201

/* mftb */
3202
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3203 3204
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3205 3206 3207
}

/* mtcrf */
3208
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3209
{
3210
    uint32_t crm, crn;
3211

B
bellard 已提交
3212
    gen_op_load_gpr_T0(rS(ctx->opcode));
3213 3214 3215 3216 3217 3218 3219 3220 3221
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
        crn = ffs(crm);
        gen_op_srli_T0(crn * 4);
        gen_op_andi_T0(0xF);
        gen_op_store_cro(7 - crn);
    } else {
        gen_op_store_cr(crm);
    }
B
bellard 已提交
3222 3223 3224
}

/* mtmsr */
J
j_mayer 已提交
3225
#if defined(TARGET_PPC64)
3226
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001FF801, PPC_64B)
J
j_mayer 已提交
3227 3228
{
#if defined(CONFIG_USER_ONLY)
3229
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3230 3231
#else
    if (unlikely(!ctx->supervisor)) {
3232
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3233 3234 3235 3236 3237 3238
        return;
    }
    gen_update_nip(ctx, ctx->nip);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_msr();
    /* Must stop the translation as machine state (may have) changed */
J
j_mayer 已提交
3239 3240
    /* Note that mtmsr is not always defined as context-synchronizing */
    GEN_STOP(ctx);
J
j_mayer 已提交
3241 3242 3243 3244
#endif
}
#endif

B
bellard 已提交
3245 3246
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3247
#if defined(CONFIG_USER_ONLY)
3248
    GEN_EXCP_PRIVREG(ctx);
3249
#else
3250
    if (unlikely(!ctx->supervisor)) {
3251
        GEN_EXCP_PRIVREG(ctx);
3252
        return;
3253
    }
3254
    gen_update_nip(ctx, ctx->nip);
B
bellard 已提交
3255
    gen_op_load_gpr_T0(rS(ctx->opcode));
3256 3257 3258 3259 3260 3261
#if defined(TARGET_PPC64)
    if (!ctx->sf_mode)
        gen_op_store_msr_32();
    else
#endif
        gen_op_store_msr();
B
bellard 已提交
3262
    /* Must stop the translation as machine state (may have) changed */
J
j_mayer 已提交
3263 3264
    /* Note that mtmsrd is not always defined as context-synchronizing */
    GEN_STOP(ctx);
3265
#endif
B
bellard 已提交
3266 3267 3268 3269 3270
}

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

3274 3275 3276 3277
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3278
#endif
3279
        write_cb = ctx->spr_cb[sprn].uea_write;
3280 3281
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
3282 3283 3284 3285
            gen_op_load_gpr_T0(rS(ctx->opcode));
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3286
            if (loglevel != 0) {
B
blueswir1 已提交
3287
                fprintf(logfile, "Trying to write privileged spr %d %03x\n",
3288 3289
                        sprn, sprn);
            }
B
blueswir1 已提交
3290
            printf("Trying to write privileged spr %d %03x\n", sprn, sprn);
3291
            GEN_EXCP_PRIVREG(ctx);
3292
        }
3293 3294
    } else {
        /* Not defined */
J
j_mayer 已提交
3295
        if (loglevel != 0) {
3296 3297 3298
            fprintf(logfile, "Trying to write invalid spr %d %03x\n",
                    sprn, sprn);
        }
3299
        printf("Trying to write invalid spr %d %03x\n", sprn, sprn);
3300 3301
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3302 3303 3304 3305 3306 3307
    }
}

/***                         Cache management                              ***/
/* For now, all those will be implemented as nop:
 * this is valid, regarding the PowerPC specs...
3308
 * We just have to flush tb while invalidating instruction cache lines...
B
bellard 已提交
3309 3310
 */
/* dcbf */
3311
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3312
{
3313
    gen_addr_reg_index(ctx);
3314
    op_ldst(lbz);
B
bellard 已提交
3315 3316 3317
}

/* dcbi (Supervisor only) */
3318
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3319
{
3320
#if defined(CONFIG_USER_ONLY)
3321
    GEN_EXCP_PRIVOPC(ctx);
3322
#else
3323
    if (unlikely(!ctx->supervisor)) {
3324
        GEN_EXCP_PRIVOPC(ctx);
3325
        return;
3326
    }
3327 3328 3329
    gen_addr_reg_index(ctx);
    /* XXX: specification says this should be treated as a store by the MMU */
    //op_ldst(lbz);
3330 3331
    op_ldst(stb);
#endif
B
bellard 已提交
3332 3333 3334
}

/* dcdst */
3335
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3336
{
3337 3338
    /* XXX: specification say this is treated as a load by the MMU */
    gen_addr_reg_index(ctx);
3339
    op_ldst(lbz);
B
bellard 已提交
3340 3341 3342
}

/* dcbt */
3343
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3344
{
3345 3346 3347
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3348 3349 3350
}

/* dcbtst */
3351
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3352
{
3353 3354 3355
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3356 3357 3358
}

/* dcbz */
3359
#define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
#if defined(TARGET_PPC64)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_dcbz[] = {
    &gen_op_dcbz_raw,
    &gen_op_dcbz_raw,
    &gen_op_dcbz_64_raw,
    &gen_op_dcbz_64_raw,
};
#else
static GenOpFunc *gen_op_dcbz[] = {
    &gen_op_dcbz_user,
    &gen_op_dcbz_user,
    &gen_op_dcbz_kernel,
    &gen_op_dcbz_kernel,
    &gen_op_dcbz_64_user,
    &gen_op_dcbz_64_user,
    &gen_op_dcbz_64_kernel,
    &gen_op_dcbz_64_kernel,
};
#endif
#else
3381
#if defined(CONFIG_USER_ONLY)
3382 3383 3384 3385
static GenOpFunc *gen_op_dcbz[] = {
    &gen_op_dcbz_raw,
    &gen_op_dcbz_raw,
};
3386 3387 3388
#else
static GenOpFunc *gen_op_dcbz[] = {
    &gen_op_dcbz_user,
B
bellard 已提交
3389 3390
    &gen_op_dcbz_user,
    &gen_op_dcbz_kernel,
3391 3392 3393
    &gen_op_dcbz_kernel,
};
#endif
3394
#endif
3395 3396

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3397
{
3398
    gen_addr_reg_index(ctx);
3399
    op_dcbz();
B
bellard 已提交
3400
    gen_op_check_reservation();
B
bellard 已提交
3401 3402 3403
}

/* icbi */
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
#if defined(TARGET_PPC64)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_raw,
    &gen_op_icbi_raw,
    &gen_op_icbi_64_raw,
    &gen_op_icbi_64_raw,
};
#else
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
    &gen_op_icbi_64_user,
    &gen_op_icbi_64_user,
    &gen_op_icbi_64_kernel,
    &gen_op_icbi_64_kernel,
};
#endif
#else
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_raw,
    &gen_op_icbi_raw,
};
#else
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
};
#endif
#endif
3440

3441
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3442
{
3443
    gen_addr_reg_index(ctx);
3444
    op_icbi();
B
bellard 已提交
3445 3446 3447 3448
}

/* Optional: */
/* dcba */
3449
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
3450 3451 3452 3453 3454 3455 3456 3457
{
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
3458
#if defined(CONFIG_USER_ONLY)
3459
    GEN_EXCP_PRIVREG(ctx);
3460
#else
3461
    if (unlikely(!ctx->supervisor)) {
3462
        GEN_EXCP_PRIVREG(ctx);
3463
        return;
3464
    }
3465 3466
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_sr();
3467 3468
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3469 3470 3471
}

/* mfsrin */
3472
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3473
{
3474
#if defined(CONFIG_USER_ONLY)
3475
    GEN_EXCP_PRIVREG(ctx);
3476
#else
3477
    if (unlikely(!ctx->supervisor)) {
3478
        GEN_EXCP_PRIVREG(ctx);
3479
        return;
3480 3481
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
3482 3483
    gen_op_srli_T1(28);
    gen_op_load_sr();
3484 3485
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3486 3487 3488
}

/* mtsr */
B
bellard 已提交
3489
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
3490
{
3491
#if defined(CONFIG_USER_ONLY)
3492
    GEN_EXCP_PRIVREG(ctx);
3493
#else
3494
    if (unlikely(!ctx->supervisor)) {
3495
        GEN_EXCP_PRIVREG(ctx);
3496
        return;
3497 3498
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3499 3500
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_sr();
3501
#endif
B
bellard 已提交
3502 3503 3504
}

/* mtsrin */
3505
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3506
{
3507
#if defined(CONFIG_USER_ONLY)
3508
    GEN_EXCP_PRIVREG(ctx);
3509
#else
3510
    if (unlikely(!ctx->supervisor)) {
3511
        GEN_EXCP_PRIVREG(ctx);
3512
        return;
3513 3514 3515
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3516 3517
    gen_op_srli_T1(28);
    gen_op_store_sr();
3518
#endif
B
bellard 已提交
3519 3520 3521 3522 3523
}

/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
3524
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
3525
{
3526
#if defined(CONFIG_USER_ONLY)
3527
    GEN_EXCP_PRIVOPC(ctx);
3528
#else
3529
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
3530
        if (loglevel != 0)
3531
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3532
        GEN_EXCP_PRIVOPC(ctx);
3533
        return;
3534 3535 3536
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
3537 3538 3539
}

/* tlbie */
3540
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
3541
{
3542
#if defined(CONFIG_USER_ONLY)
3543
    GEN_EXCP_PRIVOPC(ctx);
3544
#else
3545
    if (unlikely(!ctx->supervisor)) {
3546
        GEN_EXCP_PRIVOPC(ctx);
3547
        return;
3548 3549
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
3550 3551 3552 3553 3554 3555
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
3556
#endif
B
bellard 已提交
3557 3558 3559
}

/* tlbsync */
3560
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
3561
{
3562
#if defined(CONFIG_USER_ONLY)
3563
    GEN_EXCP_PRIVOPC(ctx);
3564
#else
3565
    if (unlikely(!ctx->supervisor)) {
3566
        GEN_EXCP_PRIVOPC(ctx);
3567
        return;
3568 3569 3570 3571
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
3572
    GEN_STOP(ctx);
3573
#endif
B
bellard 已提交
3574 3575
}

J
j_mayer 已提交
3576 3577 3578 3579 3580
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
3581
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3582 3583
#else
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
3584
        if (loglevel != 0)
J
j_mayer 已提交
3585
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3586
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
3597
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3598 3599
#else
    if (unlikely(!ctx->supervisor)) {
3600
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3601 3602 3603 3604 3605 3606 3607 3608
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
3609 3610
/***                              External control                         ***/
/* Optional: */
3611 3612
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
3613
#if defined(TARGET_PPC64)
3614 3615 3616 3617
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_raw,
    &gen_op_eciwx_le_raw,
3618 3619
    &gen_op_eciwx_64_raw,
    &gen_op_eciwx_le_64_raw,
3620 3621 3622 3623
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_raw,
    &gen_op_ecowx_le_raw,
3624 3625
    &gen_op_ecowx_64_raw,
    &gen_op_ecowx_le_64_raw,
3626 3627
};
#else
3628 3629
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
3630
    &gen_op_eciwx_le_user,
3631
    &gen_op_eciwx_kernel,
3632
    &gen_op_eciwx_le_kernel,
3633 3634 3635 3636
    &gen_op_eciwx_64_user,
    &gen_op_eciwx_le_64_user,
    &gen_op_eciwx_64_kernel,
    &gen_op_eciwx_le_64_kernel,
3637 3638 3639
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
3640
    &gen_op_ecowx_le_user,
3641
    &gen_op_ecowx_kernel,
3642
    &gen_op_ecowx_le_kernel,
3643 3644 3645 3646
    &gen_op_ecowx_64_user,
    &gen_op_ecowx_le_64_user,
    &gen_op_ecowx_64_kernel,
    &gen_op_ecowx_le_64_kernel,
3647 3648
};
#endif
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673
#else
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_raw,
    &gen_op_eciwx_le_raw,
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_raw,
    &gen_op_ecowx_le_raw,
};
#else
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
    &gen_op_eciwx_le_user,
    &gen_op_eciwx_kernel,
    &gen_op_eciwx_le_kernel,
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
    &gen_op_ecowx_le_user,
    &gen_op_ecowx_kernel,
    &gen_op_ecowx_le_kernel,
};
#endif
#endif
3674

3675
/* eciwx */
B
bellard 已提交
3676 3677
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
3678
    /* Should check EAR[E] & alignment ! */
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
    gen_addr_reg_index(ctx);
    op_eciwx();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
    /* Should check EAR[E] & alignment ! */
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_ecowx();
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_abs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_abso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* clcs */
3715
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_clcs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_div();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_doz();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_dozo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_set_T1(SIMM(ctx->opcode));
    gen_op_POWER_doz();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* As lscbx load from memory byte after byte, it's always endian safe */
#define op_POWER_lscbx(start, ra, rb) \
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
    &gen_op_POWER_lscbx_raw,
    &gen_op_POWER_lscbx_raw,
};
#else
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
    &gen_op_POWER_lscbx_user,
    &gen_op_POWER_lscbx_user,
    &gen_op_POWER_lscbx_kernel,
    &gen_op_POWER_lscbx_kernel,
};
#endif

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

    gen_addr_reg_index(ctx);
    if (ra == 0) {
        ra = rb;
    }
    /* NIP cannot be restored if the memory exception comes from an helper */
3825
    gen_update_nip(ctx, ctx->nip - 4);
3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
    gen_op_load_xer_bc();
    gen_op_load_xer_cmp();
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
    gen_op_store_xer_bc();
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_maskg();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_maskir();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_mul();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_mulo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_nabs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_nabso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

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

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_rrib();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sle();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sleq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sle();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sleq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sllq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_slq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

3993
/* sraiq - sraiq. */
3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sraq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sraq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sre();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srea();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sreq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_srq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_srlq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srlq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4098
    GEN_EXCP_INVAL(ctx);
4099 4100 4101 4102 4103 4104
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4105
    GEN_EXCP_INVAL(ctx);
4106 4107 4108 4109 4110 4111
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4112
    GEN_EXCP_PRIVOPC(ctx);
4113 4114
#else
    if (unlikely(!ctx->supervisor)) {
4115
        GEN_EXCP_PRIVOPC(ctx);
4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_602_mfrom();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
GEN_HANDLER(tlbld, 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
{
#if defined(CONFIG_USER_ONLY)
4129
    GEN_EXCP_PRIVOPC(ctx);
4130 4131
#else
    if (unlikely(!ctx->supervisor)) {
4132
        GEN_EXCP_PRIVOPC(ctx);
4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
GEN_HANDLER(tlbli, 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
{
#if defined(CONFIG_USER_ONLY)
4144
    GEN_EXCP_PRIVOPC(ctx);
4145 4146
#else
    if (unlikely(!ctx->supervisor)) {
4147
        GEN_EXCP_PRIVOPC(ctx);
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbli();
#endif
}

/* 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 已提交
4165
    /* Cache line invalidate: privileged and treated as no-op */
4166
#if defined(CONFIG_USER_ONLY)
4167
    GEN_EXCP_PRIVOPC(ctx);
4168 4169
#else
    if (unlikely(!ctx->supervisor)) {
4170
        GEN_EXCP_PRIVOPC(ctx);
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184
        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)
4185
    GEN_EXCP_PRIVOPC(ctx);
4186 4187
#else
    if (unlikely(!ctx->supervisor)) {
4188
        GEN_EXCP_PRIVOPC(ctx);
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204
        return;
    }
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);

    gen_addr_reg_index(ctx);
    gen_op_POWER_mfsri();
    gen_op_store_T0_gpr(rd);
    if (ra != 0 && ra != rd)
        gen_op_store_T1_gpr(ra);
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4205
    GEN_EXCP_PRIVOPC(ctx);
4206 4207
#else
    if (unlikely(!ctx->supervisor)) {
4208
        GEN_EXCP_PRIVOPC(ctx);
4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219
        return;
    }
    gen_addr_reg_index(ctx);
    gen_op_POWER_rac();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4220
    GEN_EXCP_PRIVOPC(ctx);
4221 4222
#else
    if (unlikely(!ctx->supervisor)) {
4223
        GEN_EXCP_PRIVOPC(ctx);
4224 4225 4226
        return;
    }
    gen_op_POWER_rfsvc();
4227
    GEN_SYNC(ctx);
4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_POWER2_lfq[] = {
    &gen_op_POWER2_lfq_le_raw,
    &gen_op_POWER2_lfq_raw,
};
static GenOpFunc *gen_op_POWER2_stfq[] = {
    &gen_op_POWER2_stfq_le_raw,
    &gen_op_POWER2_stfq_raw,
};
#else
static GenOpFunc *gen_op_POWER2_lfq[] = {
    &gen_op_POWER2_lfq_le_user,
    &gen_op_POWER2_lfq_user,
    &gen_op_POWER2_lfq_le_kernel,
    &gen_op_POWER2_lfq_kernel,
};
static GenOpFunc *gen_op_POWER2_stfq[] = {
    &gen_op_POWER2_stfq_le_user,
    &gen_op_POWER2_stfq_user,
    &gen_op_POWER2_stfq_le_kernel,
    &gen_op_POWER2_stfq_kernel,
};
#endif

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4265
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4266
    gen_addr_imm_index(ctx, 0);
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4278
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4279
    gen_addr_imm_index(ctx, 0);
4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4293
    gen_update_nip(ctx, ctx->nip - 4);
4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4306
    gen_update_nip(ctx, ctx->nip - 4);
4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4317
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4318
    gen_addr_imm_index(ctx, 0);
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4330
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4331
    gen_addr_imm_index(ctx, 0);
4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4345
    gen_update_nip(ctx, ctx->nip - 4);
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4358
    gen_update_nip(ctx, ctx->nip - 4);
4359 4360 4361 4362 4363 4364 4365
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

/* BookE specific instructions */
4366
/* XXX: not implemented on 440 ? */
4367
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
4368 4369
{
    /* XXX: TODO */
4370
    GEN_EXCP_INVAL(ctx);
4371 4372
}

4373
/* XXX: not implemented on 440 ? */
4374
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
4375 4376
{
#if defined(CONFIG_USER_ONLY)
4377
    GEN_EXCP_PRIVOPC(ctx);
4378 4379
#else
    if (unlikely(!ctx->supervisor)) {
4380
        GEN_EXCP_PRIVOPC(ctx);
4381 4382 4383 4384
        return;
    }
    gen_addr_reg_index(ctx);
    /* Use the same micro-ops as for tlbie */
4385 4386 4387 4388 4389 4390
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474
#endif
}

/* All 405 MAC instructions are translated here */
static inline void gen_405_mulladd_insn (DisasContext *ctx, int opc2, int opc3,
                                         int ra, int rb, int rt, int Rc)
{
    gen_op_load_gpr_T0(ra);
    gen_op_load_gpr_T1(rb);
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
        gen_op_405_mulchw();
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
        gen_op_405_mulchwu();
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
        gen_op_405_mulhhw();
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
        gen_op_405_mulhhwu();
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
        gen_op_405_mullhw();
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
        gen_op_405_mullhwu();
        break;
    }
    if (opc2 & 0x02) {
        /* nmultiply-and-accumulate (0x0E) */
        gen_op_neg();
    }
    if (opc2 & 0x04) {
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
        gen_op_load_gpr_T2(rt);
        gen_op_move_T1_T0();
        gen_op_405_add_T0_T2();
    }
    if (opc3 & 0x10) {
        /* Check overflow */
        if (opc3 & 0x01)
            gen_op_405_check_ov();
        else
            gen_op_405_check_ovu();
    }
    if (opc3 & 0x02) {
        /* Saturate */
        if (opc3 & 0x01)
            gen_op_405_check_sat();
        else
            gen_op_405_check_satu();
    }
    gen_op_store_T0_gpr(rt);
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
        gen_set_Rc0(ctx);
    }
}

4475 4476
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4477 4478 4479 4480 4481 4482
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
4483
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
4484
/* macchwo   - macchwo.   */
4485
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
4486
/* macchws   - macchws.   */
4487
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
4488
/* macchwso  - macchwso.  */
4489
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
4490
/* macchwsu  - macchwsu.  */
4491
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
4492
/* macchwsuo - macchwsuo. */
4493
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
4494
/* macchwu   - macchwu.   */
4495
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
4496
/* macchwuo  - macchwuo.  */
4497
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
4498
/* machhw    - machhw.    */
4499
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
4500
/* machhwo   - machhwo.   */
4501
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
4502
/* machhws   - machhws.   */
4503
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
4504
/* machhwso  - machhwso.  */
4505
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
4506
/* machhwsu  - machhwsu.  */
4507
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
4508
/* machhwsuo - machhwsuo. */
4509
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
4510
/* machhwu   - machhwu.   */
4511
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
4512
/* machhwuo  - machhwuo.  */
4513
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
4514
/* maclhw    - maclhw.    */
4515
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
4516
/* maclhwo   - maclhwo.   */
4517
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
4518
/* maclhws   - maclhws.   */
4519
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
4520
/* maclhwso  - maclhwso.  */
4521
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
4522
/* maclhwu   - maclhwu.   */
4523
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
4524
/* maclhwuo  - maclhwuo.  */
4525
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
4526
/* maclhwsu  - maclhwsu.  */
4527
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
4528
/* maclhwsuo - maclhwsuo. */
4529
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
4530
/* nmacchw   - nmacchw.   */
4531
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
4532
/* nmacchwo  - nmacchwo.  */
4533
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
4534
/* nmacchws  - nmacchws.  */
4535
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
4536
/* nmacchwso - nmacchwso. */
4537
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
4538
/* nmachhw   - nmachhw.   */
4539
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
4540
/* nmachhwo  - nmachhwo.  */
4541
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
4542
/* nmachhws  - nmachhws.  */
4543
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
4544
/* nmachhwso - nmachhwso. */
4545
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
4546
/* nmaclhw   - nmaclhw.   */
4547
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
4548
/* nmaclhwo  - nmaclhwo.  */
4549
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
4550
/* nmaclhws  - nmaclhws.  */
4551
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
4552
/* nmaclhwso - nmaclhwso. */
4553
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
4554 4555

/* mulchw  - mulchw.  */
4556
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
4557
/* mulchwu - mulchwu. */
4558
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
4559
/* mulhhw  - mulhhw.  */
4560
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
4561
/* mulhhwu - mulhhwu. */
4562
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
4563
/* mullhw  - mullhw.  */
4564
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
4565
/* mullhwu - mullhwu. */
4566
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
4567 4568 4569 4570 4571

/* mfdcr */
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4572
    GEN_EXCP_PRIVREG(ctx);
4573 4574 4575 4576
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
4577
        GEN_EXCP_PRIVREG(ctx);
4578 4579
        return;
    }
4580 4581
    gen_op_set_T0(dcrn);
    gen_op_load_dcr();
4582 4583 4584 4585 4586 4587 4588 4589
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtdcr */
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4590
    GEN_EXCP_PRIVREG(ctx);
4591 4592 4593 4594
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
4595
        GEN_EXCP_PRIVREG(ctx);
4596 4597
        return;
    }
4598 4599 4600 4601 4602 4603 4604
    gen_op_set_T0(dcrn);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
4605
/* XXX: not implemented on 440 ? */
4606
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
4607 4608
{
#if defined(CONFIG_USER_ONLY)
4609
    GEN_EXCP_PRIVREG(ctx);
4610 4611
#else
    if (unlikely(!ctx->supervisor)) {
4612
        GEN_EXCP_PRIVREG(ctx);
4613 4614 4615 4616 4617
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
4618
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4619 4620 4621 4622
#endif
}

/* mtdcrx */
4623
/* XXX: not implemented on 440 ? */
4624
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
4625 4626
{
#if defined(CONFIG_USER_ONLY)
4627
    GEN_EXCP_PRIVREG(ctx);
4628 4629
#else
    if (unlikely(!ctx->supervisor)) {
4630
        GEN_EXCP_PRIVREG(ctx);
4631 4632 4633 4634 4635
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
4636
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4637 4638 4639
#endif
}

4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    /* 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)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

4658 4659 4660 4661
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4662
    GEN_EXCP_PRIVOPC(ctx);
4663 4664
#else
    if (unlikely(!ctx->supervisor)) {
4665
        GEN_EXCP_PRIVOPC(ctx);
4666 4667 4668 4669 4670 4671 4672 4673 4674 4675
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4676
    GEN_EXCP_PRIVOPC(ctx);
4677 4678
#else
    if (unlikely(!ctx->supervisor)) {
4679
        GEN_EXCP_PRIVOPC(ctx);
4680 4681 4682 4683 4684 4685 4686 4687 4688
        return;
    }
    gen_addr_reg_index(ctx);
    op_ldst(lwz);
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* icbt */
4689
GEN_HANDLER(icbt_40x, 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700
{
    /* 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)
4701
    GEN_EXCP_PRIVOPC(ctx);
4702 4703
#else
    if (unlikely(!ctx->supervisor)) {
4704
        GEN_EXCP_PRIVOPC(ctx);
4705 4706 4707 4708 4709 4710 4711 4712 4713 4714
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4715
    GEN_EXCP_PRIVOPC(ctx);
4716 4717
#else
    if (unlikely(!ctx->supervisor)) {
4718
        GEN_EXCP_PRIVOPC(ctx);
4719 4720 4721 4722 4723 4724 4725
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
4726 4727 4728
GEN_HANDLER(rfci_40x, 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
{
#if defined(CONFIG_USER_ONLY)
4729
    GEN_EXCP_PRIVOPC(ctx);
4730 4731
#else
    if (unlikely(!ctx->supervisor)) {
4732
        GEN_EXCP_PRIVOPC(ctx);
4733 4734 4735 4736
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
4737
    GEN_SYNC(ctx);
4738 4739 4740 4741 4742 4743
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
4744
    GEN_EXCP_PRIVOPC(ctx);
4745 4746
#else
    if (unlikely(!ctx->supervisor)) {
4747
        GEN_EXCP_PRIVOPC(ctx);
4748 4749 4750 4751
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
4752
    GEN_SYNC(ctx);
4753 4754 4755 4756
#endif
}

/* BookE specific */
4757
/* XXX: not implemented on 440 ? */
4758
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
4759 4760
{
#if defined(CONFIG_USER_ONLY)
4761
    GEN_EXCP_PRIVOPC(ctx);
4762 4763
#else
    if (unlikely(!ctx->supervisor)) {
4764
        GEN_EXCP_PRIVOPC(ctx);
4765 4766 4767
        return;
    }
    /* Restore CPU state */
4768
    gen_op_rfdi();
4769
    GEN_SYNC(ctx);
4770 4771 4772
#endif
}

4773
/* XXX: not implemented on 440 ? */
4774
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
4775 4776
{
#if defined(CONFIG_USER_ONLY)
4777
    GEN_EXCP_PRIVOPC(ctx);
4778 4779
#else
    if (unlikely(!ctx->supervisor)) {
4780
        GEN_EXCP_PRIVOPC(ctx);
4781 4782 4783 4784
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
4785
    GEN_SYNC(ctx);
4786 4787
#endif
}
4788

4789
/* TLB management - PowerPC 405 implementation */
4790
/* tlbre */
4791
GEN_HANDLER(tlbre_40x, 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
4792 4793
{
#if defined(CONFIG_USER_ONLY)
4794
    GEN_EXCP_PRIVOPC(ctx);
4795 4796
#else
    if (unlikely(!ctx->supervisor)) {
4797
        GEN_EXCP_PRIVOPC(ctx);
4798 4799 4800 4801
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
4802
        gen_op_load_gpr_T0(rA(ctx->opcode));
4803 4804 4805 4806 4807 4808 4809 4810 4811
        gen_op_4xx_tlbre_hi();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_4xx_tlbre_lo();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
4812
        GEN_EXCP_INVAL(ctx);
4813
        break;
4814
    }
4815 4816 4817
#endif
}

4818
/* tlbsx - tlbsx. */
4819
GEN_HANDLER(tlbsx_40x, 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
4820 4821
{
#if defined(CONFIG_USER_ONLY)
4822
    GEN_EXCP_PRIVOPC(ctx);
4823 4824
#else
    if (unlikely(!ctx->supervisor)) {
4825
        GEN_EXCP_PRIVOPC(ctx);
4826 4827 4828 4829 4830 4831 4832
        return;
    }
    gen_addr_reg_index(ctx);
    if (Rc(ctx->opcode))
        gen_op_4xx_tlbsx_();
    else
        gen_op_4xx_tlbsx();
4833
    gen_op_store_T0_gpr(rD(ctx->opcode));
4834
#endif
B
bellard 已提交
4835 4836
}

4837
/* tlbwe */
4838
GEN_HANDLER(tlbwe_40x, 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
4839
{
4840
#if defined(CONFIG_USER_ONLY)
4841
    GEN_EXCP_PRIVOPC(ctx);
4842 4843
#else
    if (unlikely(!ctx->supervisor)) {
4844
        GEN_EXCP_PRIVOPC(ctx);
4845 4846 4847 4848
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
4849
        gen_op_load_gpr_T0(rA(ctx->opcode));
4850 4851 4852 4853 4854 4855 4856 4857 4858
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_hi();
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_lo();
        break;
    default:
4859
        GEN_EXCP_INVAL(ctx);
4860
        break;
4861
    }
4862 4863 4864
#endif
}

4865
/* TLB management - PowerPC 440 implementation */
4866
/* tlbre */
4867
GEN_HANDLER(tlbre_440, 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
4868 4869
{
#if defined(CONFIG_USER_ONLY)
4870
    GEN_EXCP_PRIVOPC(ctx);
4871 4872
#else
    if (unlikely(!ctx->supervisor)) {
4873
        GEN_EXCP_PRIVOPC(ctx);
4874 4875 4876 4877 4878 4879 4880
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
4881
        gen_op_440_tlbre(rB(ctx->opcode));
4882 4883 4884
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
4885
        GEN_EXCP_INVAL(ctx);
4886 4887 4888 4889 4890 4891
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
4892
GEN_HANDLER(tlbsx_440, 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
4893 4894
{
#if defined(CONFIG_USER_ONLY)
4895
    GEN_EXCP_PRIVOPC(ctx);
4896 4897
#else
    if (unlikely(!ctx->supervisor)) {
4898
        GEN_EXCP_PRIVOPC(ctx);
4899 4900 4901 4902
        return;
    }
    gen_addr_reg_index(ctx);
    if (Rc(ctx->opcode))
4903
        gen_op_440_tlbsx_();
4904
    else
4905
        gen_op_440_tlbsx();
4906 4907 4908 4909 4910
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* tlbwe */
4911
GEN_HANDLER(tlbwe_440, 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
4912 4913
{
#if defined(CONFIG_USER_ONLY)
4914
    GEN_EXCP_PRIVOPC(ctx);
4915 4916
#else
    if (unlikely(!ctx->supervisor)) {
4917
        GEN_EXCP_PRIVOPC(ctx);
4918 4919 4920 4921 4922 4923 4924 4925
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
4926
        gen_op_440_tlbwe(rB(ctx->opcode));
4927 4928
        break;
    default:
4929
        GEN_EXCP_INVAL(ctx);
4930 4931 4932 4933 4934
        break;
    }
#endif
}

4935 4936 4937 4938
/* wrtee */
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4939
    GEN_EXCP_PRIVOPC(ctx);
4940 4941
#else
    if (unlikely(!ctx->supervisor)) {
4942
        GEN_EXCP_PRIVOPC(ctx);
4943 4944 4945
        return;
    }
    gen_op_load_gpr_T0(rD(ctx->opcode));
4946
    gen_op_wrte();
J
j_mayer 已提交
4947 4948 4949
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
4950
    GEN_STOP(ctx);
4951 4952 4953 4954 4955 4956 4957
#endif
}

/* wrteei */
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4958
    GEN_EXCP_PRIVOPC(ctx);
4959 4960
#else
    if (unlikely(!ctx->supervisor)) {
4961
        GEN_EXCP_PRIVOPC(ctx);
4962 4963 4964
        return;
    }
    gen_op_set_T0(ctx->opcode & 0x00010000);
4965
    gen_op_wrte();
J
j_mayer 已提交
4966 4967 4968
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
4969
    GEN_STOP(ctx);
4970 4971 4972
#endif
}

J
j_mayer 已提交
4973
/* PowerPC 440 specific instructions */
4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_440_dlmzb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    gen_op_store_xer_bc();
    if (Rc(ctx->opcode)) {
        gen_op_440_dlmzb_update_Rc();
        gen_op_store_T0_crf(0);
    }
}

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

/* msync replaces sync on 440 */
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_BOOKE)
{
    /* interpreted as no-op */
}

/* icbt */
GEN_HANDLER(icbt_440, 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
5007 5008
}

5009
#if defined(TARGET_PPCEMB)
5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036
/***                           SPE extension                               ***/

/* Register moves */
GEN32(gen_op_load_gpr64_T0, gen_op_load_gpr64_T0_gpr);
GEN32(gen_op_load_gpr64_T1, gen_op_load_gpr64_T1_gpr);
#if 0 // unused
GEN32(gen_op_load_gpr64_T2, gen_op_load_gpr64_T2_gpr);
#endif

GEN32(gen_op_store_T0_gpr64, gen_op_store_T0_gpr64_gpr);
GEN32(gen_op_store_T1_gpr64, gen_op_store_T1_gpr64_gpr);
#if 0 // unused
GEN32(gen_op_store_T2_gpr64, gen_op_store_T2_gpr64_gpr);
#endif

#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 */
static inline void gen_speundef (DisasContext *ctx)
{
5037
    GEN_EXCP_INVAL(ctx);
5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128
}

/* SPE load and stores */
static inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
{
    target_long simm = rB(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        gen_set_T0(simm << sh);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm << sh);
    }
}

#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
#if defined(TARGET_PPC64)
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_raw,                                                \
    &gen_op_spe_l##name##_le_raw,                                             \
    &gen_op_spe_l##name##_64_raw,                                             \
    &gen_op_spe_l##name##_le_64_raw,                                          \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_raw,                                               \
    &gen_op_spe_st##name##_le_raw,                                            \
    &gen_op_spe_st##name##_64_raw,                                            \
    &gen_op_spe_st##name##_le_64_raw,                                         \
};
#else /* defined(TARGET_PPC64) */
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_raw,                                                \
    &gen_op_spe_l##name##_le_raw,                                             \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_raw,                                               \
    &gen_op_spe_st##name##_le_raw,                                            \
};
#endif /* defined(TARGET_PPC64) */
#else /* defined(CONFIG_USER_ONLY) */
#if defined(TARGET_PPC64)
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_user,                                               \
    &gen_op_spe_l##name##_le_user,                                            \
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
    &gen_op_spe_l##name##_64_user,                                            \
    &gen_op_spe_l##name##_le_64_user,                                         \
    &gen_op_spe_l##name##_64_kernel,                                          \
    &gen_op_spe_l##name##_le_64_kernel,                                       \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_user,                                              \
    &gen_op_spe_st##name##_le_user,                                           \
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
    &gen_op_spe_st##name##_64_user,                                           \
    &gen_op_spe_st##name##_le_64_user,                                        \
    &gen_op_spe_st##name##_64_kernel,                                         \
    &gen_op_spe_st##name##_le_64_kernel,                                      \
};
#else /* defined(TARGET_PPC64) */
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_user,                                               \
    &gen_op_spe_l##name##_le_user,                                            \
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_user,                                              \
    &gen_op_spe_st##name##_le_user,                                           \
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
};
#endif /* defined(TARGET_PPC64) */
#endif /* defined(CONFIG_USER_ONLY) */

#define GEN_SPE_LD(name, sh)                                                  \
static inline void gen_evl##name (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5129
        GEN_EXCP_NO_AP(ctx);                                                  \
5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPE_LDX(name)                                                     \
static inline void gen_evl##name##x (DisasContext *ctx)                       \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5141
        GEN_EXCP_NO_AP(ctx);                                                  \
5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

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

#define GEN_SPE_ST(name, sh)                                                  \
static inline void gen_evst##name (DisasContext *ctx)                         \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5158
        GEN_EXCP_NO_AP(ctx);                                                  \
5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPE_STX(name)                                                     \
static inline void gen_evst##name##x (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5170
        GEN_EXCP_NO_AP(ctx);                                                  \
5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

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

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

/* SPE arithmetic and logic */
#define GEN_SPEOP_ARITH2(name)                                                \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5192
        GEN_EXCP_NO_AP(ctx);                                                  \
5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_ARITH1(name)                                                \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5205
        GEN_EXCP_NO_AP(ctx);                                                  \
5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_COMP(name)                                                  \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5217
        GEN_EXCP_NO_AP(ctx);                                                  \
5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}

/* Logical */
GEN_SPEOP_ARITH2(evand);
GEN_SPEOP_ARITH2(evandc);
GEN_SPEOP_ARITH2(evxor);
GEN_SPEOP_ARITH2(evor);
GEN_SPEOP_ARITH2(evnor);
GEN_SPEOP_ARITH2(eveqv);
GEN_SPEOP_ARITH2(evorc);
GEN_SPEOP_ARITH2(evnand);
GEN_SPEOP_ARITH2(evsrwu);
GEN_SPEOP_ARITH2(evsrws);
GEN_SPEOP_ARITH2(evslw);
GEN_SPEOP_ARITH2(evrlw);
GEN_SPEOP_ARITH2(evmergehi);
GEN_SPEOP_ARITH2(evmergelo);
GEN_SPEOP_ARITH2(evmergehilo);
GEN_SPEOP_ARITH2(evmergelohi);

/* Arithmetic */
GEN_SPEOP_ARITH2(evaddw);
GEN_SPEOP_ARITH2(evsubfw);
GEN_SPEOP_ARITH1(evabs);
GEN_SPEOP_ARITH1(evneg);
GEN_SPEOP_ARITH1(evextsb);
GEN_SPEOP_ARITH1(evextsh);
GEN_SPEOP_ARITH1(evrndw);
GEN_SPEOP_ARITH1(evcntlzw);
GEN_SPEOP_ARITH1(evcntlsw);
static inline void gen_brinc (DisasContext *ctx)
{
    /* Note: brinc is usable even if SPE is disabled */
    gen_op_load_gpr64_T0(rA(ctx->opcode));
    gen_op_load_gpr64_T1(rB(ctx->opcode));
    gen_op_brinc();
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

#define GEN_SPEOP_ARITH_IMM2(name)                                            \
static inline void gen_##name##i (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5267
        GEN_EXCP_NO_AP(ctx);                                                  \
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
static inline void gen_##name##i (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5280
        GEN_EXCP_NO_AP(ctx);                                                  \
5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

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

static inline void gen_evsplati (DisasContext *ctx)
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

    gen_op_splatwi_T0_64(imm);
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

static inline void gen_evsplatfi (DisasContext *ctx)
{
    uint32_t imm = rA(ctx->opcode) << 27;

    gen_op_splatwi_T0_64(imm);
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

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

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

static inline void gen_evsel (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
5352
        GEN_EXCP_NO_AP(ctx);
5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816
        return;
    }
    gen_op_load_crf_T0(ctx->opcode & 0x7);
    gen_op_load_gpr64_T0(rA(ctx->opcode));
    gen_op_load_gpr64_T1(rB(ctx->opcode));
    gen_op_evsel();
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

GEN_HANDLER(evsel0, 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel1, 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel2, 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel3, 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}

/* Load and stores */
#if defined(TARGET_PPC64)
/* In that case, we already have 64 bits load & stores
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
 */
#if defined(CONFIG_USER_ONLY)
#define gen_op_spe_ldd_raw gen_op_ld_raw
#define gen_op_spe_ldd_64_raw gen_op_ld_64_raw
#define gen_op_spe_ldd_le_raw gen_op_ld_le_raw
#define gen_op_spe_ldd_le_64_raw gen_op_ld_le_64_raw
#define gen_op_spe_stdd_raw gen_op_ld_raw
#define gen_op_spe_stdd_64_raw gen_op_std_64_raw
#define gen_op_spe_stdd_le_raw gen_op_std_le_raw
#define gen_op_spe_stdd_le_64_raw gen_op_std_le_64_raw
#else /* defined(CONFIG_USER_ONLY) */
#define gen_op_spe_ldd_kernel gen_op_ld_kernel
#define gen_op_spe_ldd_64_kernel gen_op_ld_64_kernel
#define gen_op_spe_ldd_le_kernel gen_op_ld_kernel
#define gen_op_spe_ldd_le_64_kernel gen_op_ld_64_kernel
#define gen_op_spe_ldd_user gen_op_ld_user
#define gen_op_spe_ldd_64_user gen_op_ld_64_user
#define gen_op_spe_ldd_le_user gen_op_ld_le_user
#define gen_op_spe_ldd_le_64_user gen_op_ld_le_64_user
#define gen_op_spe_stdd_kernel gen_op_std_kernel
#define gen_op_spe_stdd_64_kernel gen_op_std_64_kernel
#define gen_op_spe_stdd_le_kernel gen_op_std_kernel
#define gen_op_spe_stdd_le_64_kernel gen_op_std_64_kernel
#define gen_op_spe_stdd_user gen_op_std_user
#define gen_op_spe_stdd_64_user gen_op_std_64_user
#define gen_op_spe_stdd_le_user gen_op_std_le_user
#define gen_op_spe_stdd_le_64_user gen_op_std_le_64_user
#endif /* defined(CONFIG_USER_ONLY) */
#endif /* defined(TARGET_PPC64) */
GEN_SPEOP_LDST(dd, 3);
GEN_SPEOP_LDST(dw, 3);
GEN_SPEOP_LDST(dh, 3);
GEN_SPEOP_LDST(whe, 2);
GEN_SPEOP_LD(whou, 2);
GEN_SPEOP_LD(whos, 2);
GEN_SPEOP_ST(who, 2);

#if defined(TARGET_PPC64)
/* In that case, spe_stwwo is equivalent to stw */
#if defined(CONFIG_USER_ONLY)
#define gen_op_spe_stwwo_raw gen_op_stw_raw
#define gen_op_spe_stwwo_le_raw gen_op_stw_le_raw
#define gen_op_spe_stwwo_64_raw gen_op_stw_64_raw
#define gen_op_spe_stwwo_le_64_raw gen_op_stw_le_64_raw
#else
#define gen_op_spe_stwwo_user gen_op_stw_user
#define gen_op_spe_stwwo_le_user gen_op_stw_le_user
#define gen_op_spe_stwwo_64_user gen_op_stw_64_user
#define gen_op_spe_stwwo_le_64_user gen_op_stw_le_64_user
#define gen_op_spe_stwwo_kernel gen_op_stw_kernel
#define gen_op_spe_stwwo_le_kernel gen_op_stw_le_kernel
#define gen_op_spe_stwwo_64_kernel gen_op_stw_64_kernel
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
#endif
#endif
#define _GEN_OP_SPE_STWWE(suffix)                                             \
static inline void gen_op_spe_stwwe_##suffix (void)                           \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
static inline void gen_op_spe_stwwe_le_##suffix (void)                        \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_##suffix();                                           \
}
#if defined(TARGET_PPC64)
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
static inline void gen_op_spe_stwwe_64_##suffix (void)                        \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
static inline void gen_op_spe_stwwe_le_64_##suffix (void)                     \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_64_##suffix();                                        \
}
#else
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix)
#endif
#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_STWWE(raw);
#else /* defined(CONFIG_USER_ONLY) */
GEN_OP_SPE_STWWE(kernel);
GEN_OP_SPE_STWWE(user);
#endif /* defined(CONFIG_USER_ONLY) */
GEN_SPEOP_ST(wwe, 2);
GEN_SPEOP_ST(wwo, 2);

#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
static inline void gen_op_spe_l##name##_##suffix (void)                       \
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
static inline void gen_op_spe_lhe_##suffix (void)                             \
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
static inline void gen_op_spe_lhx_##suffix (void)                             \
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_extsh_T1_64();                                                     \
}

#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_LHE(raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
GEN_OP_SPE_LHE(le_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
GEN_OP_SPE_LHX(raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
GEN_OP_SPE_LHX(le_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
GEN_OP_SPE_LHE(le_64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
GEN_OP_SPE_LHX(64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
GEN_OP_SPE_LHX(le_64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
#endif
#else
GEN_OP_SPE_LHE(kernel);
GEN_OP_SPE_LHE(user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
GEN_OP_SPE_LHE(le_kernel);
GEN_OP_SPE_LHE(le_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
GEN_OP_SPE_LHX(kernel);
GEN_OP_SPE_LHX(user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
GEN_OP_SPE_LHX(le_kernel);
GEN_OP_SPE_LHX(le_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_kernel);
GEN_OP_SPE_LHE(64_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
GEN_OP_SPE_LHE(le_64_kernel);
GEN_OP_SPE_LHE(le_64_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
GEN_OP_SPE_LHX(64_kernel);
GEN_OP_SPE_LHX(64_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
GEN_OP_SPE_LHX(le_64_kernel);
GEN_OP_SPE_LHX(le_64_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
#endif
#endif
GEN_SPEOP_LD(hhesplat, 1);
GEN_SPEOP_LD(hhousplat, 1);
GEN_SPEOP_LD(hhossplat, 1);
GEN_SPEOP_LD(wwsplat, 2);
GEN_SPEOP_LD(whsplat, 2);

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

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

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

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

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

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

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

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

/***                      SPE floating-point extension                     ***/
#define GEN_SPEFPUOP_CONV(name)                                               \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

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

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

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

/* Opcodes definitions */
GEN_SPE(efsadd,         efssub,        0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
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); //
GEN_SPE(efsctuiz,       efsctsiz,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //

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

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

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

B
bellard 已提交
5817 5818 5819
/* End opcode list */
GEN_OPCODE_MARK(end);

5820
#include "translate_init.c"
B
bellard 已提交
5821

5822
/*****************************************************************************/
5823
/* Misc PowerPC helpers */
5824 5825 5826 5827 5828 5829 5830 5831 5832
static inline uint32_t load_xer (CPUState *env)
{
    return (xer_so << XER_SO) |
        (xer_ov << XER_OV) |
        (xer_ca << XER_CA) |
        (xer_bc << XER_BC) |
        (xer_cmp << XER_CMP);
}

5833 5834 5835
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
5836
{
5837 5838 5839 5840 5841 5842 5843 5844 5845 5846
#if defined(TARGET_PPC64) || 1
#define FILL ""
#define RGPL  4
#define RFPL  4
#else
#define FILL "        "
#define RGPL  8
#define RFPL  4
#endif

B
bellard 已提交
5847 5848
    int i;

5849
    cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX "\n",
5850
                env->nip, env->lr, env->ctr);
5851 5852 5853
    cpu_fprintf(f, "MSR " REGX FILL " XER %08x      "
#if !defined(NO_TIMER_DUMP)
                "TB %08x %08x "
5854 5855
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
5856
#endif
5857 5858
#endif
                "\n",
5859 5860 5861
                do_load_msr(env), load_xer(env)
#if !defined(NO_TIMER_DUMP)
                , cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
5862 5863
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
5864
#endif
5865 5866 5867
#endif
                );
    for (i = 0; i < 32; i++) {
5868 5869
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
5870
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
5871
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
5872
            cpu_fprintf(f, "\n");
5873
    }
5874
    cpu_fprintf(f, "CR ");
5875
    for (i = 0; i < 8; i++)
B
bellard 已提交
5876 5877
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
5878 5879 5880 5881 5882 5883 5884 5885
    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 已提交
5886
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
5887
    }
5888 5889 5890 5891
    cpu_fprintf(f, " ]             " FILL "RES " REGX "\n", env->reserve);
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
5892
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
5893
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
5894
            cpu_fprintf(f, "\n");
B
bellard 已提交
5895
    }
5896 5897 5898
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX "         " FILL FILL FILL
                "SDR1 " REGX "\n",
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
B
bellard 已提交
5899

5900 5901 5902
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
5903 5904
}

5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951
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
}

5952
/*****************************************************************************/
5953 5954 5955
static inline int gen_intermediate_code_internal (CPUState *env,
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
5956
{
5957
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
5958
    opc_handler_t **table, *handler;
B
bellard 已提交
5959
    target_ulong pc_start;
B
bellard 已提交
5960 5961 5962 5963 5964 5965 5966
    uint16_t *gen_opc_end;
    int j, lj = -1;

    pc_start = tb->pc;
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
B
bellard 已提交
5967
    nb_gen_labels = 0;
B
bellard 已提交
5968
    ctx.nip = pc_start;
B
bellard 已提交
5969
    ctx.tb = tb;
5970
    ctx.exception = POWERPC_EXCP_NONE;
5971
    ctx.spr_cb = env->spr_cb;
5972
#if defined(CONFIG_USER_ONLY)
5973
    ctx.mem_idx = msr_le;
5974 5975 5976
#if defined(TARGET_PPC64)
    ctx.mem_idx |= msr_sf << 1;
#endif
5977 5978
#else
    ctx.supervisor = 1 - msr_pr;
5979
    ctx.mem_idx = ((1 - msr_pr) << 1) | msr_le;
5980 5981 5982 5983 5984 5985
#if defined(TARGET_PPC64)
    ctx.mem_idx |= msr_sf << 2;
#endif
#endif
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
5986
#endif
B
bellard 已提交
5987
    ctx.fpu_enabled = msr_fp;
5988
#if defined(TARGET_PPCEMB)
5989 5990
    ctx.spe_enabled = msr_spe;
#endif
5991
    ctx.singlestep_enabled = env->singlestep_enabled;
5992
#if defined (DO_SINGLE_STEP) && 0
5993 5994 5995 5996
    /* Single step trace mode */
    msr_se = 1;
#endif
    /* Set env in case of segfault during code fetch */
5997
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
5998 5999
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6000
                if (env->breakpoints[j] == ctx.nip) {
6001
                    gen_update_nip(&ctx, ctx.nip);
6002 6003 6004 6005 6006
                    gen_op_debug();
                    break;
                }
            }
        }
6007
        if (unlikely(search_pc)) {
B
bellard 已提交
6008 6009 6010 6011 6012
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6013
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6014 6015 6016
                gen_opc_instr_start[lj] = 1;
            }
        }
6017 6018
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6019
            fprintf(logfile, "----------------\n");
6020
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6021 6022 6023
                    ctx.nip, 1 - msr_pr, msr_ir);
        }
#endif
B
bellard 已提交
6024
        ctx.opcode = ldl_code(ctx.nip);
6025 6026 6027 6028 6029 6030
        if (msr_le) {
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                ((ctx.opcode & 0x00FF0000) >> 8) |
                ((ctx.opcode & 0x0000FF00) << 8) |
                ((ctx.opcode & 0x000000FF) << 24);
        }
6031 6032
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6033
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6034
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6035
                    opc3(ctx.opcode), msr_le ? "little" : "big");
B
bellard 已提交
6036 6037
        }
#endif
B
bellard 已提交
6038
        ctx.nip += 4;
6039
        table = env->opcodes;
B
bellard 已提交
6040 6041 6042 6043 6044 6045 6046 6047 6048 6049
        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 ? */
6050
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6051
            if (loglevel != 0) {
6052
                fprintf(logfile, "invalid/unsupported opcode: "
6053
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6054
                        opc1(ctx.opcode), opc2(ctx.opcode),
B
bellard 已提交
6055 6056 6057
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            } else {
                printf("invalid/unsupported opcode: "
6058
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
B
bellard 已提交
6059 6060 6061
                       opc1(ctx.opcode), opc2(ctx.opcode),
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            }
6062 6063
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6064
                if (loglevel != 0) {
B
bellard 已提交
6065
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6066
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
B
bellard 已提交
6067 6068
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6069
                            ctx.opcode, ctx.nip - 4);
6070 6071
                } else {
                    printf("invalid bits: %08x for opcode: "
6072
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6073 6074
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6075
                           ctx.opcode, ctx.nip - 4);
6076
                }
6077
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6078
                break;
B
bellard 已提交
6079 6080
            }
        }
B
bellard 已提交
6081
        (*(handler->handler))(&ctx);
6082 6083 6084
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6085
        /* Check trace mode exceptions */
J
j_mayer 已提交
6086
#if 0 // XXX: buggy on embedded PowerPC
6087
        if (unlikely((msr_be && ctx.exception == POWERPC_EXCP_BRANCH) ||
6088 6089 6090 6091 6092 6093 6094 6095
                     /* Check in single step trace mode
                      * we need to stop except if:
                      * - rfi, trap or syscall
                      * - first instruction of an exception handler
                      */
                     (msr_se && (ctx.nip < 0x100 ||
                                 ctx.nip > 0xF00 ||
                                 (ctx.nip & 0xFC) != 0x04) &&
6096 6097 6098 6099 6100 6101 6102
#if defined(CONFIG_USER_ONLY)
                      ctx.exception != POWERPC_EXCP_SYSCALL_USER &&
#else
                      ctx.exception != POWERPC_EXCP_SYSCALL &&
#endif
                      ctx.exception != POWERPC_EXCP_TRAP))) {
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6103
        }
J
j_mayer 已提交
6104
#endif
6105 6106 6107
        /* if we reach a page boundary or are single stepping, stop
         * generation
         */
6108 6109
        if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
                     (env->singlestep_enabled))) {
6110
            break;
6111
        }
6112 6113 6114 6115
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
6116
    if (ctx.exception == POWERPC_EXCP_NONE) {
6117
        gen_goto_tb(&ctx, 0, ctx.nip);
6118
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6119 6120 6121
        gen_op_reset_T0();
        /* Generate the return instruction */
        gen_op_exit_tb();
6122
    }
B
bellard 已提交
6123
    *gen_opc_ptr = INDEX_op_end;
6124
    if (unlikely(search_pc)) {
6125 6126 6127 6128 6129
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6130
        tb->size = ctx.nip - pc_start;
6131
    }
6132
#if defined(DEBUG_DISAS)
6133
    if (loglevel & CPU_LOG_TB_CPU) {
6134
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6135
        cpu_dump_state(env, logfile, fprintf, 0);
6136 6137
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6138
        int flags;
6139 6140
        flags = env->bfd_mach;
        flags |= msr_le << 16;
B
bellard 已提交
6141
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6142
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6143
        fprintf(logfile, "\n");
6144 6145
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
6146 6147 6148 6149 6150 6151 6152 6153
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

6154
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6155 6156 6157 6158
{
    return gen_intermediate_code_internal(env, tb, 0);
}

6159
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6160 6161 6162
{
    return gen_intermediate_code_internal(env, tb, 1);
}