translate.c 238.0 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
static always_inline void gen_set_T0 (target_ulong val)
57 58 59 60 61 62 63 64 65
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T0_64(val >> 32, val);
    else
#endif
        gen_op_set_T0(val);
}

66
static always_inline void gen_set_T1 (target_ulong val)
67 68 69 70 71 72 73 74 75 76
{
#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
static GenOpFunc *NAME ## _table [8] = {                                      \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
};                                                                            \
81
static always_inline void func (int n)                                        \
82 83 84 85 86 87 88 89 90 91 92
{                                                                             \
    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,                               \
};                                                                            \
93
static always_inline void func (int n)                                        \
94 95
{                                                                             \
    NAME ## _table[n]();                                                      \
96 97
}

98
#define GEN32(func, NAME)                                                     \
99 100 101 102 103 104 105 106 107 108
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,                               \
};                                                                            \
109
static always_inline void func (int n)                                        \
110 111 112 113 114 115 116 117 118
{                                                                             \
    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 always_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
    int altivec_enabled;
168
#if defined(TARGET_PPCEMB)
169 170
    int spe_enabled;
#endif
171
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
172
    int singlestep_enabled;
173
    int dcache_line_size;
B
bellard 已提交
174 175
} DisasContext;

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

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

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

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

221 222 223
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
224

225 226 227
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
228

229 230 231 232 233 234 235 236 237
#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)
238

239 240 241
#define GEN_EXCP_NO_VR(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)

242
/* Stop translation */
243
static always_inline void GEN_STOP (DisasContext *ctx)
244
{
245
    gen_update_nip(ctx, ctx->nip);
246
    ctx->exception = POWERPC_EXCP_STOP;
247 248
}

249
/* No need to update nip here, as execution flow will change */
250
static always_inline void GEN_SYNC (DisasContext *ctx)
251
{
252
    ctx->exception = POWERPC_EXCP_SYNC;
253 254
}

B
bellard 已提交
255 256 257 258 259
#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)

260 261 262 263 264 265
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
static void gen_##name (DisasContext *ctx)


B
bellard 已提交
266 267
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
268 269 270 271 272
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
273
    opc_handler_t handler;
274
    const unsigned char *oname;
B
bellard 已提交
275 276
} opcode_t;

277
/*****************************************************************************/
B
bellard 已提交
278 279
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
280
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
281 282 283 284 285
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
286
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
287
{                                                                             \
288
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

/* 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 */
316
EXTRACT_HELPER(_SPR, 11, 10);
317
static always_inline uint32_t SPR (uint32_t opcode)
318 319 320 321 322
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336
/***                              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 已提交
337 338
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
339 340 341 342

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

B
bellard 已提交
345 346 347 348
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
349
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
350 351 352 353
{
    return (opcode >> 0) & 0x03FFFFFC;
}

354
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
355 356 357 358 359 360 361 362 363 364 365 366
{
    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 */
367
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
368
{
369
    target_ulong ret;
B
bellard 已提交
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
#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 已提交
390 391 392 393

    return ret;
}

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

/*****************************************************************************/
/* PowerPC instructions table                                                */
504 505 506 507 508
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
509
#if defined(__APPLE__)
510
#define OPCODES_SECTION                                                       \
511
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
512
#else
513
#define OPCODES_SECTION                                                       \
514
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
515 516
#endif

517
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
518
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
519
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
520 521 522
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
523
    .pad  = { 0, },                                                           \
B
bellard 已提交
524 525
    .handler = {                                                              \
        .inval   = invl,                                                      \
526
        .type = _typ,                                                         \
B
bellard 已提交
527
        .handler = &gen_##name,                                               \
528
        .oname = stringify(name),                                             \
B
bellard 已提交
529
    },                                                                        \
530
    .oname = stringify(name),                                                 \
B
bellard 已提交
531
}
532 533 534 535 536 537 538 539 540 541 542 543 544 545
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
        .oname = onam,                                                        \
    },                                                                        \
    .oname = onam,                                                            \
}
546 547 548 549 550 551 552 553 554 555 556 557 558 559
#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),                                                 \
}
560 561 562 563 564 565 566 567 568 569 570 571 572
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = onam,                                                            \
}
573
#endif
B
bellard 已提交
574 575

#define GEN_OPCODE_MARK(name)                                                 \
576
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
577 578 579
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
580
    .pad  = { 0, },                                                           \
B
bellard 已提交
581 582
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
583
        .type = 0x00,                                                         \
B
bellard 已提交
584 585
        .handler = NULL,                                                      \
    },                                                                        \
586
    .oname = stringify(name),                                                 \
B
bellard 已提交
587 588 589 590 591 592
}

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

/* Invalid instruction */
593 594
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
595
    GEN_EXCP_INVAL(ctx);
596 597
}

B
bellard 已提交
598 599
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
600
    .type    = PPC_NONE,
B
bellard 已提交
601 602 603 604
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
605 606
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
607 608 609 610 611
{                                                                             \
    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));                                     \
612 613
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
614 615
}

616 617
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
618 619 620 621 622
{                                                                             \
    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));                                     \
623 624
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
625 626
}

627 628
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
629 630 631 632
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
633 634
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
635
}
636 637
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
638 639 640 641
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
642 643
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
644 645 646
}

/* Two operands arithmetic functions */
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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
#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 已提交
718 719

/* Two operands arithmetic functions with no overflow allowed */
720 721
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
722 723

/* One operand arithmetic functions */
724 725 726 727 728 729 730 731
#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 已提交
732 733

/* add    add.    addo    addo.    */
734
static always_inline void gen_op_addo (void)
735 736 737 738 739 740 741
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
742
static always_inline void gen_op_addo_64 (void)
743 744 745 746 747 748 749
{
    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 已提交
750
/* addc   addc.   addco   addco.   */
751
static always_inline void gen_op_addc (void)
752 753 754 755 756
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
}
757
static always_inline void gen_op_addco (void)
758 759 760 761 762 763 764
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
765
static always_inline void gen_op_addc_64 (void)
766 767 768 769 770
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
}
771
static always_inline void gen_op_addco_64 (void)
772 773 774 775 776 777 778 779
{
    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 已提交
780
/* adde   adde.   addeo   addeo.   */
781
static always_inline void gen_op_addeo (void)
782 783 784 785 786 787
{
    gen_op_move_T2_T0();
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
788
static always_inline void gen_op_addeo_64 (void)
789 790 791 792 793 794 795
{
    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 已提交
796
/* addme  addme.  addmeo  addmeo.  */
797
static always_inline void gen_op_addme (void)
798 799 800 801 802
{
    gen_op_move_T1_T0();
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
803
static always_inline void gen_op_addme_64 (void)
804 805 806 807 808 809
{
    gen_op_move_T1_T0();
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
810
/* addze  addze.  addzeo  addzeo.  */
811
static always_inline void gen_op_addze (void)
812 813 814 815 816
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
}
817
static always_inline void gen_op_addzeo (void)
818 819 820 821 822 823 824
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
825
static always_inline void gen_op_addze_64 (void)
826 827 828 829 830
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
}
831
static always_inline void gen_op_addzeo_64 (void)
832 833 834 835 836 837 838 839
{
    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 已提交
840
/* divw   divw.   divwo   divwo.   */
841
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
842
/* divwu  divwu.  divwuo  divwuo.  */
843
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
844
/* mulhw  mulhw.                   */
845
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
846
/* mulhwu mulhwu.                  */
847
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
848
/* mullw  mullw.  mullwo  mullwo.  */
849
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
850
/* neg    neg.    nego    nego.    */
851
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
852
/* subf   subf.   subfo   subfo.   */
853
static always_inline void gen_op_subfo (void)
854 855 856 857 858 859 860
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
861
static always_inline void gen_op_subfo_64 (void)
862 863 864 865 866 867 868
{
    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 已提交
869
/* subfc  subfc.  subfco  subfco.  */
870
static always_inline void gen_op_subfc (void)
871 872 873 874
{
    gen_op_subf();
    gen_op_check_subfc();
}
875
static always_inline void gen_op_subfco (void)
876 877 878 879 880 881 882
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfc();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
883
static always_inline void gen_op_subfc_64 (void)
884 885 886 887
{
    gen_op_subf();
    gen_op_check_subfc_64();
}
888
static always_inline void gen_op_subfco_64 (void)
889 890 891 892 893 894 895 896
{
    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 已提交
897
/* subfe  subfe.  subfeo  subfeo.  */
898
static always_inline void gen_op_subfeo (void)
899 900 901 902 903 904 905
{
    gen_op_move_T2_T0();
    gen_op_subfe();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
906
static always_inline void gen_op_subfeo_64 (void)
907 908 909 910 911 912 913
{
    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 已提交
914
/* subfme subfme. subfmeo subfmeo. */
915
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
916
/* subfze subfze. subfzeo subfzeo. */
917
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
918 919 920
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
921
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
922 923

    if (rA(ctx->opcode) == 0) {
924
        /* li case */
925
        gen_set_T0(simm);
B
bellard 已提交
926 927
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
928 929
        if (likely(simm != 0))
            gen_op_addi(simm);
B
bellard 已提交
930 931 932 933 934 935
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
936 937
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
938
    gen_op_load_gpr_T0(rA(ctx->opcode));
939 940 941 942 943 944 945 946 947
    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 已提交
948 949
    } else {
        gen_op_clear_xer_ca();
950
    }
B
bellard 已提交
951 952 953
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic. */
954
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
955
{
956 957
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
958
    gen_op_load_gpr_T0(rA(ctx->opcode));
959 960 961 962 963 964 965 966 967
    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 已提交
968 969
    } else {
        gen_op_clear_xer_ca();
970
    }
B
bellard 已提交
971
    gen_op_store_T0_gpr(rD(ctx->opcode));
972
    gen_set_Rc0(ctx);
B
bellard 已提交
973 974 975 976
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
977
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
978 979

    if (rA(ctx->opcode) == 0) {
980
        /* lis case */
981
        gen_set_T0(simm << 16);
B
bellard 已提交
982 983
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
984 985
        if (likely(simm != 0))
            gen_op_addi(simm << 16);
B
bellard 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999
    }
    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));
1000 1001 1002 1003 1004 1005
#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 已提交
1006 1007 1008
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

1009 1010
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
1011
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1012
/* mulhdu mulhdu.                  */
1013
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1014
/* mulld  mulld.  mulldo  mulldo.  */
1015
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1016
/* divd   divd.   divdo   divdo.   */
1017
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1018
/* divdu  divdu.  divduo  divduo.  */
1019
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1020 1021
#endif

B
bellard 已提交
1022
/***                           Integer comparison                          ***/
1023 1024 1025 1026 1027 1028
#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));                                      \
1029
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
1030 1031 1032 1033 1034 1035 1036 1037
        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 已提交
1038 1039 1040 1041 1042 1043
{                                                                             \
    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));                                   \
}
1044
#endif
B
bellard 已提交
1045 1046

/* cmp */
1047
GEN_CMP(cmp, 0x00, PPC_INTEGER);
B
bellard 已提交
1048 1049 1050 1051
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1052
#if defined(TARGET_PPC64)
1053
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1054 1055 1056 1057
        gen_op_cmpi_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_cmpi(SIMM(ctx->opcode));
B
bellard 已提交
1058 1059 1060
    gen_op_store_T0_crf(crfD(ctx->opcode));
}
/* cmpl */
1061
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
B
bellard 已提交
1062 1063 1064 1065
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1066
#if defined(TARGET_PPC64)
1067
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1068 1069 1070 1071
        gen_op_cmpli_64(UIMM(ctx->opcode));
    else
#endif
        gen_op_cmpli(UIMM(ctx->opcode));
B
bellard 已提交
1072 1073 1074
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
/* 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 已提交
1094
/***                            Integer logical                            ***/
1095 1096
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
B
bellard 已提交
1097 1098 1099 1100 1101
{                                                                             \
    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));                                     \
1102 1103
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1104
}
1105 1106
#define GEN_LOGICAL2(name, opc, type)                                         \
__GEN_LOGICAL2(name, 0x1C, opc, type)
B
bellard 已提交
1107

1108 1109
#define GEN_LOGICAL1(name, opc, type)                                         \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1110 1111 1112 1113
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1114 1115
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1116 1117 1118
}

/* and & and. */
1119
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
B
bellard 已提交
1120
/* andc & andc. */
1121
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
B
bellard 已提交
1122
/* andi. */
1123
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1124 1125
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1126
    gen_op_andi_T0(UIMM(ctx->opcode));
B
bellard 已提交
1127
    gen_op_store_T0_gpr(rA(ctx->opcode));
1128
    gen_set_Rc0(ctx);
B
bellard 已提交
1129 1130
}
/* andis. */
1131
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1132 1133
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1134
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
B
bellard 已提交
1135
    gen_op_store_T0_gpr(rA(ctx->opcode));
1136
    gen_set_Rc0(ctx);
B
bellard 已提交
1137 1138 1139
}

/* cntlzw */
1140
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
B
bellard 已提交
1141
/* eqv & eqv. */
1142
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
B
bellard 已提交
1143
/* extsb & extsb. */
1144
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
B
bellard 已提交
1145
/* extsh & extsh. */
1146
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
B
bellard 已提交
1147
/* nand & nand. */
1148
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
B
bellard 已提交
1149
/* nor & nor. */
1150
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1151

B
bellard 已提交
1152
/* or & or. */
1153 1154
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    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);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
#if defined(TARGET_PPC64)
    } else {
        switch (rs) {
        case 1:
            /* Set process priority to low */
            gen_op_store_pri(2);
            break;
        case 6:
            /* Set process priority to medium-low */
            gen_op_store_pri(3);
            break;
        case 2:
            /* Set process priority to normal */
            gen_op_store_pri(4);
            break;
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
#if !defined(CONFIG_USER_ONLY)
        case 31:
            if (ctx->supervisor > 0) {
                /* Set process priority to very low */
                gen_op_store_pri(1);
            }
            break;
        case 5:
            if (ctx->supervisor > 0) {
                /* Set process priority to medium-hight */
                gen_op_store_pri(5);
            }
            break;
        case 3:
            if (ctx->supervisor > 0) {
                /* Set process priority to high */
                gen_op_store_pri(6);
            }
            break;
#if defined(TARGET_PPC64H)
        case 7:
            if (ctx->supervisor > 1) {
                /* Set process priority to very high */
                gen_op_store_pri(7);
            }
            break;
#endif
#endif
1216 1217 1218 1219 1220
        default:
            /* nop */
            break;
        }
#endif
1221 1222 1223
    }
}

B
bellard 已提交
1224
/* orc & orc. */
1225
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
B
bellard 已提交
1226
/* xor & xor. */
1227 1228 1229 1230 1231 1232 1233 1234
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 {
1235
        gen_op_reset_T0();
1236 1237
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1238 1239
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1240
}
B
bellard 已提交
1241 1242 1243
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1244
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1245

1246 1247
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1248
        /* XXX: should handle special NOPs for POWER series */
1249
        return;
1250 1251 1252
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1253
        gen_op_ori(uimm);
1254
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1255 1256 1257 1258
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1259
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1260

1261 1262 1263
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1264 1265 1266
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1267
        gen_op_ori(uimm << 16);
1268
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1269 1270 1271 1272
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1273
    target_ulong uimm = UIMM(ctx->opcode);
1274 1275 1276 1277 1278

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1279
    gen_op_load_gpr_T0(rS(ctx->opcode));
1280 1281
    if (likely(uimm != 0))
        gen_op_xori(uimm);
B
bellard 已提交
1282 1283 1284 1285 1286 1287
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1288
    target_ulong uimm = UIMM(ctx->opcode);
1289 1290 1291 1292 1293

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1294
    gen_op_load_gpr_T0(rS(ctx->opcode));
1295 1296
    if (likely(uimm != 0))
        gen_op_xori(uimm << 16);
B
bellard 已提交
1297 1298 1299
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
/* 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 已提交
1320 1321 1322 1323
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1324 1325
    target_ulong mask;
    uint32_t mb, me, sh;
B
bellard 已提交
1326 1327 1328

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    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 已提交
1342
    gen_op_load_gpr_T0(rS(ctx->opcode));
B
bellard 已提交
1343
    gen_op_load_gpr_T1(rA(ctx->opcode));
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    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 已提交
1355
    gen_op_store_T0_gpr(rA(ctx->opcode));
1356 1357
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1358 1359 1360 1361 1362
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1363

B
bellard 已提交
1364 1365 1366 1367
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    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 已提交
1378
        }
1379 1380 1381 1382
    } else if (likely(me == 31)) {
        if (likely(sh == (32 - mb))) {
            gen_op_srli_T0(mb);
            goto do_store;
B
bellard 已提交
1383 1384
        }
    }
1385 1386 1387 1388 1389 1390 1391 1392
    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 已提交
1393
    gen_op_store_T0_gpr(rA(ctx->opcode));
1394 1395
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
}
/* 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));
1406 1407 1408 1409 1410 1411 1412
    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 已提交
1413 1414
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1415 1416
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1417 1418
}

1419 1420
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1421
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1422 1423 1424
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1425 1426
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1427 1428 1429 1430
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1431
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1432 1433 1434
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1435 1436
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1437 1438 1439
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1440 1441
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1442 1443 1444
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1445 1446
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1447 1448 1449
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1450

1451
static always_inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
1452 1453 1454 1455 1456 1457 1458
{
    if (mask >> 32)
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T0(mask);
}

1459
static always_inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
1460 1461 1462 1463 1464 1465 1466
{
    if (mask >> 32)
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T1(mask);
}

1467 1468
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1469 1470 1471 1472 1473 1474 1475
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 63)) {
1476
            gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1477 1478 1479 1480 1481 1482 1483
            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))) {
1484
            gen_op_srli_T0_64(mb);
J
j_mayer 已提交
1485 1486 1487 1488 1489
            goto do_store;
        }
    }
    gen_op_rotli64_T0(sh);
 do_mask:
1490
    gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1491 1492 1493 1494 1495
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1496
/* rldicl - rldicl. */
1497
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1498
{
J
j_mayer 已提交
1499
    uint32_t sh, mb;
1500

J
j_mayer 已提交
1501 1502
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1503
    gen_rldinm(ctx, mb, 63, sh);
1504
}
J
j_mayer 已提交
1505
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1506
/* rldicr - rldicr. */
1507
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1508
{
J
j_mayer 已提交
1509
    uint32_t sh, me;
1510

J
j_mayer 已提交
1511 1512
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1513
    gen_rldinm(ctx, 0, me, sh);
1514
}
J
j_mayer 已提交
1515
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1516
/* rldic - rldic. */
1517
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1518
{
J
j_mayer 已提交
1519
    uint32_t sh, mb;
1520

J
j_mayer 已提交
1521 1522
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1523 1524 1525 1526
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1527 1528
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1529 1530 1531 1532 1533
{
    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)) {
1534
        gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1535 1536 1537 1538
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1539
}
J
j_mayer 已提交
1540

1541
/* rldcl - rldcl. */
1542
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1543
{
J
j_mayer 已提交
1544
    uint32_t mb;
1545

J
j_mayer 已提交
1546
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1547
    gen_rldnm(ctx, mb, 63);
1548
}
1549
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1550
/* rldcr - rldcr. */
1551
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1552
{
J
j_mayer 已提交
1553
    uint32_t me;
1554

J
j_mayer 已提交
1555
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1556
    gen_rldnm(ctx, 0, me);
1557
}
1558
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1559
/* rldimi - rldimi. */
1560
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1561
{
J
j_mayer 已提交
1562 1563
    uint64_t mask;
    uint32_t sh, mb;
1564

J
j_mayer 已提交
1565 1566
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    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));
1581
    gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1582 1583
 do_mask:
    mask = MASK(mb, 63 - sh);
1584 1585
    gen_andi_T0_64(ctx, mask);
    gen_andi_T1_64(ctx, ~mask);
J
j_mayer 已提交
1586 1587 1588 1589 1590
    gen_op_or();
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1591
}
1592
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1593 1594
#endif

B
bellard 已提交
1595 1596
/***                             Integer shift                             ***/
/* slw & slw. */
1597
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
B
bellard 已提交
1598
/* sraw & sraw. */
1599
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
B
bellard 已提交
1600 1601 1602
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1603
    int mb, me;
B
bellard 已提交
1604
    gen_op_load_gpr_T0(rS(ctx->opcode));
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    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 已提交
1615
    gen_op_store_T0_gpr(rA(ctx->opcode));
1616 1617
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1618 1619
}
/* srw & srw. */
1620 1621 1622 1623 1624 1625 1626 1627
__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. */
1628
static always_inline void gen_sradi (DisasContext *ctx, int n)
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
{
    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);
}
1646
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1647 1648 1649
{
    gen_sradi(ctx, 0);
}
1650
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1651 1652 1653 1654 1655 1656
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
#endif
B
bellard 已提交
1657 1658

/***                       Floating-Point arithmetic                       ***/
1659 1660
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, type)                     \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1661
{                                                                             \
1662
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1663
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1664 1665
        return;                                                               \
    }                                                                         \
1666 1667 1668 1669
    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));                                     \
1670 1671 1672 1673
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1674
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1675
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1676 1677 1678
        gen_op_set_Rc1();                                                     \
}

1679 1680 1681
#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);
1682

1683
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat)                     \
1684 1685
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1686
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1687
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1688 1689
        return;                                                               \
    }                                                                         \
1690 1691 1692
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
1693 1694 1695 1696
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1697
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1698
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1699 1700 1701
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AB(name, op2, inval)                                        \
1702 1703
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1);
1704

1705
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat)                     \
1706 1707
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1708
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1709
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1710 1711
        return;                                                               \
    }                                                                         \
1712 1713 1714
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1715 1716 1717 1718
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1719
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1720
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1721 1722 1723
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AC(name, op2, inval)                                        \
1724 1725
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1);
1726

1727 1728
#define GEN_FLOAT_B(name, op2, op3, type)                                     \
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1729
{                                                                             \
1730
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1731
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1732 1733
        return;                                                               \
    }                                                                         \
1734 1735 1736 1737
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1738
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1739
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1740 1741
}

1742 1743
#define GEN_FLOAT_BS(name, op1, op2, type)                                    \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1744
{                                                                             \
1745
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1746
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1747 1748
        return;                                                               \
    }                                                                         \
1749 1750 1751 1752
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1753
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1754
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1755 1756
}

1757 1758
/* fadd - fadds */
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
1759
/* fdiv - fdivs */
1760
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
1761
/* fmul - fmuls */
1762
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
B
bellard 已提交
1763

1764 1765 1766
/* fre */
GEN_FLOAT_BS(re, 0x3F, 0x18, PPC_FLOAT_EXT);

1767 1768
/* fres */
GEN_FLOAT_BS(res, 0x3B, 0x18, PPC_FLOAT_FRES);
B
bellard 已提交
1769

1770 1771
/* frsqrte */
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, PPC_FLOAT_FRSQRTE);
B
bellard 已提交
1772

1773 1774
/* fsel */
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, PPC_FLOAT_FSEL);
1775
/* fsub - fsubs */
1776
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
B
bellard 已提交
1777 1778
/* Optional: */
/* fsqrt */
1779
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1780
{
1781
    if (unlikely(!ctx->fpu_enabled)) {
1782
        GEN_EXCP_NO_FP(ctx);
1783 1784 1785 1786 1787 1788
        return;
    }
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_fsqrt();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1789
    if (unlikely(Rc(ctx->opcode) != 0))
1790 1791
        gen_op_set_Rc1();
}
B
bellard 已提交
1792

1793
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
1794
{
1795
    if (unlikely(!ctx->fpu_enabled)) {
1796
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1797 1798
        return;
    }
1799 1800
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1801 1802
    gen_op_fsqrt();
    gen_op_frsp();
1803
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1804
    if (unlikely(Rc(ctx->opcode) != 0))
1805
        gen_op_set_Rc1();
B
bellard 已提交
1806 1807 1808
}

/***                     Floating-Point multiply-and-add                   ***/
1809
/* fmadd - fmadds */
1810
GEN_FLOAT_ACB(madd, 0x1D, PPC_FLOAT);
1811
/* fmsub - fmsubs */
1812
GEN_FLOAT_ACB(msub, 0x1C, PPC_FLOAT);
1813
/* fnmadd - fnmadds */
1814
GEN_FLOAT_ACB(nmadd, 0x1F, PPC_FLOAT);
1815
/* fnmsub - fnmsubs */
1816
GEN_FLOAT_ACB(nmsub, 0x1E, PPC_FLOAT);
B
bellard 已提交
1817 1818 1819

/***                     Floating-Point round & convert                    ***/
/* fctiw */
1820
GEN_FLOAT_B(ctiw, 0x0E, 0x00, PPC_FLOAT);
B
bellard 已提交
1821
/* fctiwz */
1822
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, PPC_FLOAT);
B
bellard 已提交
1823
/* frsp */
1824
GEN_FLOAT_B(rsp, 0x0C, 0x00, PPC_FLOAT);
J
j_mayer 已提交
1825 1826
#if defined(TARGET_PPC64)
/* fcfid */
1827
GEN_FLOAT_B(cfid, 0x0E, 0x1A, PPC_64B);
J
j_mayer 已提交
1828
/* fctid */
1829
GEN_FLOAT_B(ctid, 0x0E, 0x19, PPC_64B);
J
j_mayer 已提交
1830
/* fctidz */
1831
GEN_FLOAT_B(ctidz, 0x0F, 0x19, PPC_64B);
J
j_mayer 已提交
1832
#endif
B
bellard 已提交
1833

1834 1835 1836 1837 1838 1839 1840 1841 1842
/* 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 已提交
1843 1844
/***                         Floating-Point compare                        ***/
/* fcmpo */
1845
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1846
{
1847
    if (unlikely(!ctx->fpu_enabled)) {
1848
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1849 1850
        return;
    }
1851 1852 1853 1854 1855
    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 已提交
1856 1857 1858
}

/* fcmpu */
1859
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1860
{
1861
    if (unlikely(!ctx->fpu_enabled)) {
1862
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1863 1864
        return;
    }
1865 1866 1867 1868 1869
    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 已提交
1870 1871
}

1872 1873
/***                         Floating-point move                           ***/
/* fabs */
1874
GEN_FLOAT_B(abs, 0x08, 0x08, PPC_FLOAT);
1875 1876 1877 1878

/* fmr  - fmr. */
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
1879
    if (unlikely(!ctx->fpu_enabled)) {
1880
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1881 1882
        return;
    }
1883 1884 1885
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1886
    if (unlikely(Rc(ctx->opcode) != 0))
1887 1888 1889 1890
        gen_op_set_Rc1();
}

/* fnabs */
1891
GEN_FLOAT_B(nabs, 0x08, 0x04, PPC_FLOAT);
1892
/* fneg */
1893
GEN_FLOAT_B(neg, 0x08, 0x01, PPC_FLOAT);
1894

B
bellard 已提交
1895 1896 1897 1898
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
1899
    if (unlikely(!ctx->fpu_enabled)) {
1900
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1901 1902
        return;
    }
B
bellard 已提交
1903 1904 1905
    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 已提交
1906 1907 1908 1909 1910
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
1911
    if (unlikely(!ctx->fpu_enabled)) {
1912
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1913 1914
        return;
    }
1915
    gen_op_load_fpscr();
B
bellard 已提交
1916
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1917
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1918
        gen_op_set_Rc1();
B
bellard 已提交
1919 1920 1921 1922 1923
}

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

1926
    if (unlikely(!ctx->fpu_enabled)) {
1927
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1928 1929
        return;
    }
B
bellard 已提交
1930 1931
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
1932
    gen_op_andi_T0(~(1 << (crbD(ctx->opcode) & 0x03)));
B
bellard 已提交
1933
    gen_op_store_T0_fpscr(crb);
1934
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1935
        gen_op_set_Rc1();
B
bellard 已提交
1936 1937 1938 1939 1940
}

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

1943
    if (unlikely(!ctx->fpu_enabled)) {
1944
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1945 1946
        return;
    }
B
bellard 已提交
1947 1948 1949 1950
    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);
1951
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1952
        gen_op_set_Rc1();
B
bellard 已提交
1953 1954 1955 1956 1957
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
1958
    if (unlikely(!ctx->fpu_enabled)) {
1959
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1960 1961
        return;
    }
B
bellard 已提交
1962
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1963
    gen_op_store_fpscr(FM(ctx->opcode));
1964
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1965
        gen_op_set_Rc1();
B
bellard 已提交
1966 1967 1968 1969 1970
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
1971
    if (unlikely(!ctx->fpu_enabled)) {
1972
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1973 1974
        return;
    }
B
bellard 已提交
1975
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
1976
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1977
        gen_op_set_Rc1();
B
bellard 已提交
1978 1979
}

1980 1981
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
1982 1983
static always_inline void gen_addr_imm_index (DisasContext *ctx,
                                              target_long maskl)
1984 1985 1986
{
    target_long simm = SIMM(ctx->opcode);

1987
    simm &= ~maskl;
1988
    if (rA(ctx->opcode) == 0) {
1989
        gen_set_T0(simm);
1990 1991 1992 1993 1994
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm);
    }
1995 1996 1997
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1998 1999
}

2000
static always_inline void gen_addr_reg_index (DisasContext *ctx)
2001 2002 2003 2004 2005 2006 2007 2008
{
    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();
    }
2009 2010 2011
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
2012 2013
}

2014
static always_inline void gen_addr_register (DisasContext *ctx)
2015 2016 2017 2018 2019 2020
{
    if (rA(ctx->opcode) == 0) {
        gen_op_reset_T0();
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
    }
2021 2022 2023
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
2024 2025
}

B
bellard 已提交
2026
/***                             Integer load                              ***/
2027
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
2028
#if defined(CONFIG_USER_ONLY)
2029
#if defined(TARGET_PPC64)
2030
/* User mode only - 64 bits */
2031 2032 2033 2034
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_raw,                                                   \
    &gen_op_l##width##_le_raw,                                                \
2035 2036
    &gen_op_l##width##_64_raw,                                                \
    &gen_op_l##width##_le_64_raw,                                             \
2037 2038 2039 2040 2041
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_raw,                                                  \
    &gen_op_st##width##_le_raw,                                               \
2042 2043
    &gen_op_st##width##_64_raw,                                               \
    &gen_op_st##width##_le_64_raw,                                            \
2044 2045
};
/* Byte access routine are endian safe */
2046 2047 2048
#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
2049
/* User mode only - 32 bits */
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
#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 */
2062 2063
#define gen_op_stb_le_raw gen_op_stb_raw
#define gen_op_lbz_le_raw gen_op_lbz_raw
2064
#else
2065
#if defined(TARGET_PPC64)
2066 2067
#if defined(TARGET_PPC64H)
/* Full system - 64 bits with hypervisor mode */
2068 2069 2070
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
2071
    &gen_op_l##width##_le_user,                                               \
2072 2073
    &gen_op_l##width##_64_user,                                               \
    &gen_op_l##width##_le_64_user,                                            \
2074 2075
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
2076 2077
    &gen_op_l##width##_64_kernel,                                             \
    &gen_op_l##width##_le_64_kernel,                                          \
2078 2079 2080 2081
    &gen_op_l##width##_hypv,                                                  \
    &gen_op_l##width##_le_hypv,                                               \
    &gen_op_l##width##_64_hypv,                                               \
    &gen_op_l##width##_le_64_hypv,                                            \
2082
};
2083 2084 2085
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
2086
    &gen_op_st##width##_le_user,                                              \
2087 2088
    &gen_op_st##width##_64_user,                                              \
    &gen_op_st##width##_le_64_user,                                           \
2089
    &gen_op_st##width##_kernel,                                               \
2090
    &gen_op_st##width##_le_kernel,                                            \
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
    &gen_op_st##width##_64_kernel,                                            \
    &gen_op_st##width##_le_64_kernel,                                         \
    &gen_op_st##width##_hypv,                                                 \
    &gen_op_st##width##_le_hypv,                                              \
    &gen_op_st##width##_64_hypv,                                              \
    &gen_op_st##width##_le_64_hypv,                                           \
};
/* Byte access routine are endian safe */
#define gen_op_stb_le_hypv      gen_op_stb_64_hypv
#define gen_op_lbz_le_hypv      gen_op_lbz_64_hypv
#define gen_op_stb_le_64_hypv   gen_op_stb_64_hypv
#define gen_op_lbz_le_64_hypv   gen_op_lbz_64_hypv
#else
/* Full system - 64 bits */
#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##_64_user,                                               \
    &gen_op_l##width##_le_64_user,                                            \
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
    &gen_op_l##width##_64_kernel,                                             \
    &gen_op_l##width##_le_64_kernel,                                          \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
    &gen_op_st##width##_le_user,                                              \
2120 2121
    &gen_op_st##width##_64_user,                                              \
    &gen_op_st##width##_le_64_user,                                           \
2122 2123
    &gen_op_st##width##_kernel,                                               \
    &gen_op_st##width##_le_kernel,                                            \
2124 2125
    &gen_op_st##width##_64_kernel,                                            \
    &gen_op_st##width##_le_64_kernel,                                         \
2126
};
2127
#endif
2128
/* Byte access routine are endian safe */
2129 2130
#define gen_op_stb_le_64_user   gen_op_stb_64_user
#define gen_op_lbz_le_64_user   gen_op_lbz_64_user
2131 2132 2133
#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
2134
/* Full system - 32 bits */
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
#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 */
2151 2152
#define gen_op_stb_le_user   gen_op_stb_user
#define gen_op_lbz_le_user   gen_op_lbz_user
2153 2154
#define gen_op_stb_le_kernel gen_op_stb_kernel
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
2155 2156
#endif

2157 2158
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2159
{                                                                             \
J
j_mayer 已提交
2160
    gen_addr_imm_index(ctx, 0);                                               \
2161
    op_ldst(l##width);                                                        \
B
bellard 已提交
2162 2163 2164
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2165 2166
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2167
{                                                                             \
2168 2169
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2170
        GEN_EXCP_INVAL(ctx);                                                  \
2171
        return;                                                               \
2172
    }                                                                         \
J
j_mayer 已提交
2173
    if (type == PPC_64B)                                                      \
2174
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2175 2176
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
2177
    op_ldst(l##width);                                                        \
B
bellard 已提交
2178 2179 2180 2181
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2182 2183
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2184
{                                                                             \
2185 2186
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2187
        GEN_EXCP_INVAL(ctx);                                                  \
2188
        return;                                                               \
2189
    }                                                                         \
2190
    gen_addr_reg_index(ctx);                                                  \
2191
    op_ldst(l##width);                                                        \
B
bellard 已提交
2192 2193 2194 2195
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2196 2197
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2198
{                                                                             \
2199
    gen_addr_reg_index(ctx);                                                  \
2200
    op_ldst(l##width);                                                        \
B
bellard 已提交
2201 2202 2203
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2204
#define GEN_LDS(width, op, type)                                              \
2205
OP_LD_TABLE(width);                                                           \
2206 2207 2208 2209
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 已提交
2210 2211

/* lbz lbzu lbzux lbzx */
2212
GEN_LDS(bz, 0x02, PPC_INTEGER);
B
bellard 已提交
2213
/* lha lhau lhaux lhax */
2214
GEN_LDS(ha, 0x0A, PPC_INTEGER);
B
bellard 已提交
2215
/* lhz lhzu lhzux lhzx */
2216
GEN_LDS(hz, 0x08, PPC_INTEGER);
B
bellard 已提交
2217
/* lwz lwzu lwzux lwzx */
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
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))) {
2235
            GEN_EXCP_INVAL(ctx);
2236 2237 2238
            return;
        }
    }
2239
    gen_addr_imm_index(ctx, 0x03);
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
    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));
}
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
/* lq */
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    int ra, rd;

    /* Restore CPU state */
    if (unlikely(ctx->supervisor == 0)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
        GEN_EXCP_INVAL(ctx);
        return;
    }
    if (unlikely(ctx->mem_idx & 1)) {
        /* Little-endian mode is not handled */
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
        return;
    }
    gen_addr_imm_index(ctx, 0x0F);
    op_ldst(ld);
    gen_op_store_T1_gpr(rd);
    gen_op_addi(8);
    op_ldst(ld);
    gen_op_store_T1_gpr(rd + 1);
#endif
}
2283
#endif
B
bellard 已提交
2284 2285

/***                              Integer store                            ***/
2286 2287
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2288
{                                                                             \
J
j_mayer 已提交
2289
    gen_addr_imm_index(ctx, 0);                                               \
2290 2291
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2292 2293
}

2294 2295
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2296
{                                                                             \
2297
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2298
        GEN_EXCP_INVAL(ctx);                                                  \
2299
        return;                                                               \
2300
    }                                                                         \
J
j_mayer 已提交
2301
    if (type == PPC_64B)                                                      \
2302
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2303 2304
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
B
bellard 已提交
2305
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2306
    op_ldst(st##width);                                                       \
B
bellard 已提交
2307 2308 2309
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2310 2311
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2312
{                                                                             \
2313
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2314
        GEN_EXCP_INVAL(ctx);                                                  \
2315
        return;                                                               \
2316
    }                                                                         \
2317
    gen_addr_reg_index(ctx);                                                  \
2318 2319
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2320 2321 2322
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2323 2324
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2325
{                                                                             \
2326
    gen_addr_reg_index(ctx);                                                  \
2327 2328
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2329 2330
}

2331
#define GEN_STS(width, op, type)                                              \
2332
OP_ST_TABLE(width);                                                           \
2333 2334 2335 2336
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 已提交
2337 2338

/* stb stbu stbux stbx */
2339
GEN_STS(b, 0x06, PPC_INTEGER);
B
bellard 已提交
2340
/* sth sthu sthux sthx */
2341
GEN_STS(h, 0x0C, PPC_INTEGER);
B
bellard 已提交
2342
/* stw stwu stwux stwx */
2343 2344 2345
GEN_STS(w, 0x04, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_ST_TABLE(d);
J
j_mayer 已提交
2346 2347
GEN_STUX(d, 0x15, 0x05, PPC_64B);
GEN_STX(d, 0x15, 0x04, PPC_64B);
2348
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2349
{
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
    int rs;

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
        GEN_EXCP_PRIVOPC(ctx);
#else
        /* stq */
        if (unlikely(ctx->supervisor == 0)) {
            GEN_EXCP_PRIVOPC(ctx);
            return;
        }
        if (unlikely(rs & 1)) {
2363
            GEN_EXCP_INVAL(ctx);
2364 2365
            return;
        }
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
        if (unlikely(ctx->mem_idx & 1)) {
            /* Little-endian mode is not handled */
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
            return;
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        gen_op_addi(8);
        gen_op_load_gpr_T1(rs + 1);
        op_ldst(std);
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
                GEN_EXCP_INVAL(ctx);
                return;
            }
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        if (Rc(ctx->opcode))
            gen_op_store_T0_gpr(rA(ctx->opcode));
2391 2392 2393
    }
}
#endif
B
bellard 已提交
2394 2395
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
2396
OP_LD_TABLE(hbr);
2397
GEN_LDX(hbr, 0x16, 0x18, PPC_INTEGER);
B
bellard 已提交
2398
/* lwbrx */
2399
OP_LD_TABLE(wbr);
2400
GEN_LDX(wbr, 0x16, 0x10, PPC_INTEGER);
B
bellard 已提交
2401
/* sthbrx */
2402
OP_ST_TABLE(hbr);
2403
GEN_STX(hbr, 0x16, 0x1C, PPC_INTEGER);
B
bellard 已提交
2404
/* stwbrx */
2405
OP_ST_TABLE(wbr);
2406
GEN_STX(wbr, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
2407 2408

/***                    Integer load and store multiple                    ***/
2409
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2410
#if defined(CONFIG_USER_ONLY)
2411
/* User-mode only */
2412 2413 2414
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_raw,
    &gen_op_lmw_le_raw,
2415
#if defined(TARGET_PPC64)
2416 2417
    &gen_op_lmw_64_raw,
    &gen_op_lmw_le_64_raw,
2418
#endif
2419 2420
};
static GenOpFunc1 *gen_op_stmw[] = {
2421 2422 2423
    &gen_op_stmw_raw,
    &gen_op_stmw_le_raw,
#if defined(TARGET_PPC64)
2424 2425
    &gen_op_stmw_64_raw,
    &gen_op_stmw_le_64_raw,
2426
#endif
2427 2428
};
#else
2429 2430
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
2431 2432 2433 2434 2435
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
    &gen_op_lmw_le_user,
    &gen_op_lmw_64_user,
    &gen_op_lmw_le_64_user,
2436 2437
    &gen_op_lmw_kernel,
    &gen_op_lmw_le_kernel,
2438 2439
    &gen_op_lmw_64_kernel,
    &gen_op_lmw_le_64_kernel,
2440 2441 2442 2443 2444 2445
#if defined(TARGET_PPC64H)
    &gen_op_lmw_hypv,
    &gen_op_lmw_le_hypv,
    &gen_op_lmw_64_hypv,
    &gen_op_lmw_le_64_hypv,
#endif
2446 2447 2448 2449 2450 2451
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
    &gen_op_stmw_le_user,
    &gen_op_stmw_64_user,
    &gen_op_stmw_le_64_user,
2452 2453
    &gen_op_stmw_kernel,
    &gen_op_stmw_le_kernel,
2454 2455
    &gen_op_stmw_64_kernel,
    &gen_op_stmw_le_64_kernel,
2456 2457 2458 2459 2460
#if defined(TARGET_PPC64H)
    &gen_op_stmw_hypv,
    &gen_op_stmw_le_hypv,
    &gen_op_stmw_64_hypv,
    &gen_op_stmw_le_64_hypv,
2461
#endif
2462
};
2463
#else
2464
/* Full system - 32 bits mode */
2465 2466
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
2467
    &gen_op_lmw_le_user,
2468
    &gen_op_lmw_kernel,
2469
    &gen_op_lmw_le_kernel,
2470 2471 2472
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
2473
    &gen_op_stmw_le_user,
2474
    &gen_op_stmw_kernel,
2475
    &gen_op_stmw_le_kernel,
2476 2477
};
#endif
2478
#endif
2479

B
bellard 已提交
2480 2481 2482
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2483
    /* NIP cannot be restored if the memory exception comes from an helper */
2484
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2485
    gen_addr_imm_index(ctx, 0);
2486
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2487 2488 2489 2490 2491
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2492
    /* NIP cannot be restored if the memory exception comes from an helper */
2493
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2494
    gen_addr_imm_index(ctx, 0);
2495
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2496 2497 2498
}

/***                    Integer load and store strings                     ***/
2499 2500
#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)
2501
#if defined(CONFIG_USER_ONLY)
2502
/* User-mode only */
2503 2504 2505
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
2506
#if defined(TARGET_PPC64)
2507 2508
    &gen_op_lswi_64_raw,
    &gen_op_lswi_le_64_raw,
2509
#endif
2510 2511 2512 2513
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
2514
#if defined(TARGET_PPC64)
2515 2516
    &gen_op_lswx_64_raw,
    &gen_op_lswx_le_64_raw,
2517
#endif
2518 2519 2520 2521
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
2522
#if defined(TARGET_PPC64)
2523 2524
    &gen_op_stsw_64_raw,
    &gen_op_stsw_le_64_raw,
2525
#endif
2526 2527
};
#else
2528 2529
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
2530 2531 2532 2533 2534
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
    &gen_op_lswi_le_user,
    &gen_op_lswi_64_user,
    &gen_op_lswi_le_64_user,
2535 2536
    &gen_op_lswi_kernel,
    &gen_op_lswi_le_kernel,
2537 2538
    &gen_op_lswi_64_kernel,
    &gen_op_lswi_le_64_kernel,
2539 2540 2541 2542 2543 2544
#if defined(TARGET_PPC64H)
    &gen_op_lswi_hypv,
    &gen_op_lswi_le_hypv,
    &gen_op_lswi_64_hypv,
    &gen_op_lswi_le_64_hypv,
#endif
2545 2546 2547 2548 2549 2550
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
    &gen_op_lswx_le_user,
    &gen_op_lswx_64_user,
    &gen_op_lswx_le_64_user,
2551 2552
    &gen_op_lswx_kernel,
    &gen_op_lswx_le_kernel,
2553 2554
    &gen_op_lswx_64_kernel,
    &gen_op_lswx_le_64_kernel,
2555 2556 2557 2558 2559 2560
#if defined(TARGET_PPC64H)
    &gen_op_lswx_hypv,
    &gen_op_lswx_le_hypv,
    &gen_op_lswx_64_hypv,
    &gen_op_lswx_le_64_hypv,
#endif
2561 2562 2563 2564 2565 2566
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
    &gen_op_stsw_le_user,
    &gen_op_stsw_64_user,
    &gen_op_stsw_le_64_user,
2567 2568
    &gen_op_stsw_kernel,
    &gen_op_stsw_le_kernel,
2569 2570
    &gen_op_stsw_64_kernel,
    &gen_op_stsw_le_64_kernel,
2571 2572 2573 2574 2575
#if defined(TARGET_PPC64H)
    &gen_op_stsw_hypv,
    &gen_op_stsw_le_hypv,
    &gen_op_stsw_64_hypv,
    &gen_op_stsw_le_64_hypv,
2576
#endif
2577 2578
};
#else
2579
/* Full system - 32 bits mode */
2580 2581
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
2582
    &gen_op_lswi_le_user,
2583
    &gen_op_lswi_kernel,
2584
    &gen_op_lswi_le_kernel,
2585 2586 2587
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
2588
    &gen_op_lswx_le_user,
2589
    &gen_op_lswx_kernel,
2590
    &gen_op_lswx_le_kernel,
2591 2592 2593
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
2594
    &gen_op_stsw_le_user,
2595
    &gen_op_stsw_kernel,
2596
    &gen_op_stsw_le_kernel,
2597 2598
};
#endif
2599
#endif
2600

B
bellard 已提交
2601
/* lswi */
2602
/* PowerPC32 specification says we must generate an exception if
2603 2604 2605 2606
 * 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 已提交
2607 2608 2609 2610
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
2611
    int ra = rA(ctx->opcode);
B
bellard 已提交
2612 2613 2614 2615 2616
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
2617 2618 2619
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2620 2621
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2622
        return;
B
bellard 已提交
2623
    }
2624
    /* NIP cannot be restored if the memory exception comes from an helper */
2625
    gen_update_nip(ctx, ctx->nip - 4);
2626 2627
    gen_addr_register(ctx);
    gen_op_set_T1(nb);
2628
    op_ldsts(lswi, start);
B
bellard 已提交
2629 2630 2631 2632 2633
}

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

2637
    /* NIP cannot be restored if the memory exception comes from an helper */
2638
    gen_update_nip(ctx, ctx->nip - 4);
2639
    gen_addr_reg_index(ctx);
2640 2641
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
2642
    }
2643 2644
    gen_op_load_xer_bc();
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
2645 2646 2647 2648 2649
}

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

2652
    /* NIP cannot be restored if the memory exception comes from an helper */
2653
    gen_update_nip(ctx, ctx->nip - 4);
2654
    gen_addr_register(ctx);
B
bellard 已提交
2655 2656 2657
    if (nb == 0)
        nb = 32;
    gen_op_set_T1(nb);
2658
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2659 2660 2661 2662 2663
}

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
2664
    /* NIP cannot be restored if the memory exception comes from an helper */
2665
    gen_update_nip(ctx, ctx->nip - 4);
2666 2667
    gen_addr_reg_index(ctx);
    gen_op_load_xer_bc();
2668
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2669 2670 2671 2672
}

/***                        Memory synchronisation                         ***/
/* eieio */
2673
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
2674 2675 2676 2677
{
}

/* isync */
2678
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2679
{
2680
    GEN_STOP(ctx);
B
bellard 已提交
2681 2682
}

2683 2684
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2685
#if defined(CONFIG_USER_ONLY)
2686
/* User-mode only */
2687 2688 2689
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_raw,
    &gen_op_lwarx_le_raw,
2690
#if defined(TARGET_PPC64)
2691 2692
    &gen_op_lwarx_64_raw,
    &gen_op_lwarx_le_64_raw,
2693
#endif
2694 2695 2696 2697
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_raw,
    &gen_op_stwcx_le_raw,
2698
#if defined(TARGET_PPC64)
2699 2700
    &gen_op_stwcx_64_raw,
    &gen_op_stwcx_le_64_raw,
2701
#endif
2702
};
2703
#else
2704 2705
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
B
bellard 已提交
2706 2707
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
2708
    &gen_op_lwarx_le_user,
2709 2710
    &gen_op_lwarx_64_user,
    &gen_op_lwarx_le_64_user,
2711 2712
    &gen_op_lwarx_kernel,
    &gen_op_lwarx_le_kernel,
2713 2714
    &gen_op_lwarx_64_kernel,
    &gen_op_lwarx_le_64_kernel,
2715 2716 2717 2718 2719 2720
#if defined(TARGET_PPC64H)
    &gen_op_lwarx_hypv,
    &gen_op_lwarx_le_hypv,
    &gen_op_lwarx_64_hypv,
    &gen_op_lwarx_le_64_hypv,
#endif
B
bellard 已提交
2721
};
2722 2723
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
2724
    &gen_op_stwcx_le_user,
2725 2726
    &gen_op_stwcx_64_user,
    &gen_op_stwcx_le_64_user,
2727 2728
    &gen_op_stwcx_kernel,
    &gen_op_stwcx_le_kernel,
2729 2730
    &gen_op_stwcx_64_kernel,
    &gen_op_stwcx_le_64_kernel,
2731 2732 2733 2734 2735
#if defined(TARGET_PPC64H)
    &gen_op_stwcx_hypv,
    &gen_op_stwcx_le_hypv,
    &gen_op_stwcx_64_hypv,
    &gen_op_stwcx_le_64_hypv,
2736
#endif
2737 2738
};
#else
2739
/* Full system - 32 bits mode */
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
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
2754

2755
/* lwarx */
2756
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
2757
{
2758 2759
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2760
    gen_addr_reg_index(ctx);
B
bellard 已提交
2761
    op_lwarx();
B
bellard 已提交
2762 2763 2764 2765
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stwcx. */
2766
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
2767
{
2768 2769
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2770
    gen_addr_reg_index(ctx);
2771 2772
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stwcx();
B
bellard 已提交
2773 2774
}

J
j_mayer 已提交
2775 2776 2777 2778
#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)
2779
/* User-mode only */
J
j_mayer 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
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
2793
/* Full system */
J
j_mayer 已提交
2794 2795 2796 2797 2798
static GenOpFunc *gen_op_ldarx[] = {
    &gen_op_ldarx_user,
    &gen_op_ldarx_le_user,
    &gen_op_ldarx_64_user,
    &gen_op_ldarx_le_64_user,
2799 2800
    &gen_op_ldarx_kernel,
    &gen_op_ldarx_le_kernel,
J
j_mayer 已提交
2801 2802
    &gen_op_ldarx_64_kernel,
    &gen_op_ldarx_le_64_kernel,
2803 2804 2805 2806 2807 2808
#if defined(TARGET_PPC64H)
    &gen_op_ldarx_hypv,
    &gen_op_ldarx_le_hypv,
    &gen_op_ldarx_64_hypv,
    &gen_op_ldarx_le_64_hypv,
#endif
J
j_mayer 已提交
2809 2810 2811 2812 2813 2814
};
static GenOpFunc *gen_op_stdcx[] = {
    &gen_op_stdcx_user,
    &gen_op_stdcx_le_user,
    &gen_op_stdcx_64_user,
    &gen_op_stdcx_le_64_user,
2815 2816
    &gen_op_stdcx_kernel,
    &gen_op_stdcx_le_kernel,
J
j_mayer 已提交
2817 2818
    &gen_op_stdcx_64_kernel,
    &gen_op_stdcx_le_64_kernel,
2819 2820 2821 2822 2823 2824
#if defined(TARGET_PPC64H)
    &gen_op_stdcx_hypv,
    &gen_op_stdcx_le_hypv,
    &gen_op_stdcx_64_hypv,
    &gen_op_stdcx_le_64_hypv,
#endif
J
j_mayer 已提交
2825 2826 2827 2828
};
#endif

/* ldarx */
2829
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
2830
{
2831 2832
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2833 2834 2835 2836 2837 2838
    gen_addr_reg_index(ctx);
    op_ldarx();
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stdcx. */
2839
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
2840
{
2841 2842
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2843 2844 2845 2846 2847 2848
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
2849
/* sync */
2850
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
2851 2852 2853
{
}

2854 2855 2856 2857
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
    /* Stop translation, as the CPU is supposed to sleep from now */
2858 2859
    gen_op_wait();
    GEN_EXCP(ctx, EXCP_HLT, 1);
2860 2861
}

B
bellard 已提交
2862
/***                         Floating-point load                           ***/
2863 2864
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2865
{                                                                             \
2866
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2867
        GEN_EXCP_NO_FP(ctx);                                                  \
2868 2869
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2870
    gen_addr_imm_index(ctx, 0);                                               \
2871
    op_ldst(l##width);                                                        \
2872
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2873 2874
}

2875 2876
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2877
{                                                                             \
2878
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2879
        GEN_EXCP_NO_FP(ctx);                                                  \
2880 2881
        return;                                                               \
    }                                                                         \
2882
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2883
        GEN_EXCP_INVAL(ctx);                                                  \
2884
        return;                                                               \
2885
    }                                                                         \
J
j_mayer 已提交
2886
    gen_addr_imm_index(ctx, 0);                                               \
2887
    op_ldst(l##width);                                                        \
2888
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2889 2890 2891
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2892 2893
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
2894
{                                                                             \
2895
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2896
        GEN_EXCP_NO_FP(ctx);                                                  \
2897 2898
        return;                                                               \
    }                                                                         \
2899
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2900
        GEN_EXCP_INVAL(ctx);                                                  \
2901
        return;                                                               \
2902
    }                                                                         \
2903
    gen_addr_reg_index(ctx);                                                  \
2904
    op_ldst(l##width);                                                        \
2905
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2906 2907 2908
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2909 2910
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2911
{                                                                             \
2912
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2913
        GEN_EXCP_NO_FP(ctx);                                                  \
2914 2915
        return;                                                               \
    }                                                                         \
2916
    gen_addr_reg_index(ctx);                                                  \
2917
    op_ldst(l##width);                                                        \
2918
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2919 2920
}

2921
#define GEN_LDFS(width, op, type)                                             \
2922
OP_LD_TABLE(width);                                                           \
2923 2924 2925 2926
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 已提交
2927 2928

/* lfd lfdu lfdux lfdx */
2929
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
2930
/* lfs lfsu lfsux lfsx */
2931
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
2932 2933

/***                         Floating-point store                          ***/
2934 2935
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2936
{                                                                             \
2937
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2938
        GEN_EXCP_NO_FP(ctx);                                                  \
2939 2940
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2941
    gen_addr_imm_index(ctx, 0);                                               \
2942
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2943
    op_ldst(st##width);                                                       \
B
bellard 已提交
2944 2945
}

2946 2947
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2948
{                                                                             \
2949
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2950
        GEN_EXCP_NO_FP(ctx);                                                  \
2951 2952
        return;                                                               \
    }                                                                         \
2953
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2954
        GEN_EXCP_INVAL(ctx);                                                  \
2955
        return;                                                               \
2956
    }                                                                         \
J
j_mayer 已提交
2957
    gen_addr_imm_index(ctx, 0);                                               \
2958
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2959
    op_ldst(st##width);                                                       \
B
bellard 已提交
2960 2961 2962
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2963 2964
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
2965
{                                                                             \
2966
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2967
        GEN_EXCP_NO_FP(ctx);                                                  \
2968 2969
        return;                                                               \
    }                                                                         \
2970
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2971
        GEN_EXCP_INVAL(ctx);                                                  \
2972
        return;                                                               \
2973
    }                                                                         \
2974 2975
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2976
    op_ldst(st##width);                                                       \
B
bellard 已提交
2977 2978 2979
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2980 2981
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2982
{                                                                             \
2983
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2984
        GEN_EXCP_NO_FP(ctx);                                                  \
2985 2986
        return;                                                               \
    }                                                                         \
2987 2988
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2989
    op_ldst(st##width);                                                       \
B
bellard 已提交
2990 2991
}

2992
#define GEN_STFS(width, op, type)                                             \
2993
OP_ST_TABLE(width);                                                           \
2994 2995 2996 2997
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 已提交
2998 2999

/* stfd stfdu stfdux stfdx */
3000
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
3001
/* stfs stfsu stfsux stfsx */
3002
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
3003 3004 3005

/* Optional: */
/* stfiwx */
3006 3007
OP_ST_TABLE(fiwx);
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
3008 3009

/***                                Branch                                 ***/
3010 3011
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
3012 3013 3014 3015 3016 3017 3018 3019
{
    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));
3020 3021 3022 3023 3024 3025 3026
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
3027
        gen_op_set_T0((long)tb + n);
3028 3029
        if (ctx->singlestep_enabled)
            gen_op_debug();
3030 3031
        gen_op_exit_tb();
    } else {
3032 3033 3034 3035 3036 3037 3038
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
3039
        gen_op_reset_T0();
3040 3041
        if (ctx->singlestep_enabled)
            gen_op_debug();
3042 3043
        gen_op_exit_tb();
    }
B
bellard 已提交
3044 3045
}

3046
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3047 3048 3049 3050 3051 3052 3053 3054 3055
{
#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 已提交
3056 3057 3058
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3059
    target_ulong li, target;
B
bellard 已提交
3060 3061

    /* sign extend LI */
3062
#if defined(TARGET_PPC64)
3063 3064 3065
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3066
#endif
3067
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3068
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3069
        target = ctx->nip + li - 4;
B
bellard 已提交
3070
    else
3071
        target = li;
3072
#if defined(TARGET_PPC64)
3073 3074
    if (!ctx->sf_mode)
        target = (uint32_t)target;
3075
#endif
3076 3077
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3078
    gen_goto_tb(ctx, 0, target);
3079
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3080 3081
}

3082 3083 3084 3085
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3086
static always_inline void gen_bcond (DisasContext *ctx, int type)
3087
{
3088 3089
    target_ulong target = 0;
    target_ulong li;
3090 3091 3092
    uint32_t bo = BO(ctx->opcode);
    uint32_t bi = BI(ctx->opcode);
    uint32_t mask;
3093 3094

    if ((bo & 0x4) == 0)
3095
        gen_op_dec_ctr();
3096 3097
    switch(type) {
    case BCOND_IM:
3098 3099
        li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
B
bellard 已提交
3100
            target = ctx->nip + li - 4;
3101 3102 3103
        } else {
            target = li;
        }
3104 3105 3106 3107
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode)
            target = (uint32_t)target;
#endif
3108 3109 3110 3111 3112 3113 3114 3115 3116
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
3117 3118
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3119
    if (bo & 0x10) {
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
        /* 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();
3137 3138
            break;
        default:
3139 3140
        case 4:
        case 6:
3141
            if (type == BCOND_IM) {
3142
                gen_goto_tb(ctx, 0, target);
3143
                goto out;
3144
            } else {
3145 3146 3147 3148 3149 3150
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_b_T1_64();
                else
#endif
                    gen_op_b_T1();
3151
                gen_op_reset_T0();
3152
                goto no_test;
3153
            }
3154
            break;
3155
        }
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
    } 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:
3180
                gen_op_test_true(mask);
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
                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);
3192
                break;
3193 3194 3195 3196 3197 3198 3199 3200
            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;
3201
            default:
3202 3203
            case 4:
            case 6:
3204
                gen_op_test_false(mask);
3205 3206 3207 3208
                break;
            }
        }
    }
3209
    if (type == BCOND_IM) {
B
bellard 已提交
3210 3211
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
3212
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3213
        gen_set_label(l1);
3214
        gen_goto_tb(ctx, 1, ctx->nip);
3215
    } else {
3216 3217 3218 3219 3220 3221
#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);
3222
        gen_op_reset_T0();
3223
    no_test:
J
j_mayer 已提交
3224 3225 3226 3227
        if (ctx->singlestep_enabled)
            gen_op_debug();
        gen_op_exit_tb();
    }
3228
 out:
3229
    ctx->exception = POWERPC_EXCP_BRANCH;
3230 3231 3232
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3233
{
3234 3235 3236 3237
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3238
{
3239 3240 3241 3242
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3243
{
3244 3245
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262

/***                      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 */
3263
GEN_CRLOGIC(and, 0x08);
B
bellard 已提交
3264
/* crandc */
3265
GEN_CRLOGIC(andc, 0x04);
B
bellard 已提交
3266
/* creqv */
3267
GEN_CRLOGIC(eqv, 0x09);
B
bellard 已提交
3268
/* crnand */
3269
GEN_CRLOGIC(nand, 0x07);
B
bellard 已提交
3270
/* crnor */
3271
GEN_CRLOGIC(nor, 0x01);
B
bellard 已提交
3272
/* cror */
3273
GEN_CRLOGIC(or, 0x0E);
B
bellard 已提交
3274
/* crorc */
3275
GEN_CRLOGIC(orc, 0x0D);
B
bellard 已提交
3276
/* crxor */
3277
GEN_CRLOGIC(xor, 0x06);
B
bellard 已提交
3278 3279 3280 3281 3282 3283 3284 3285 3286
/* 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) */
3287
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3288
{
3289
#if defined(CONFIG_USER_ONLY)
3290
    GEN_EXCP_PRIVOPC(ctx);
3291 3292
#else
    /* Restore CPU state */
3293
    if (unlikely(!ctx->supervisor)) {
3294
        GEN_EXCP_PRIVOPC(ctx);
3295
        return;
3296
    }
3297
    gen_op_rfi();
3298
    GEN_SYNC(ctx);
3299
#endif
B
bellard 已提交
3300 3301
}

J
j_mayer 已提交
3302
#if defined(TARGET_PPC64)
3303
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3304 3305
{
#if defined(CONFIG_USER_ONLY)
3306
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3307 3308 3309
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3310
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3311 3312
        return;
    }
3313
    gen_op_rfid();
3314
    GEN_SYNC(ctx);
J
j_mayer 已提交
3315 3316 3317 3318
#endif
}
#endif

3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
#if defined(TARGET_PPC64H)
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    /* Restore CPU state */
    if (unlikely(ctx->supervisor <= 1)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_hrfid();
    GEN_SYNC(ctx);
#endif
}
#endif

B
bellard 已提交
3336
/* sc */
3337 3338 3339 3340 3341
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3342
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3343
{
3344 3345 3346
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3347
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3348 3349 3350 3351
}

/***                                Trap                                   ***/
/* tw */
3352
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3353
{
3354 3355
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3356
    /* Update the nip since this might generate a trap exception */
3357
    gen_update_nip(ctx, ctx->nip);
3358
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3359 3360 3361 3362 3363
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3364
    gen_op_load_gpr_T0(rA(ctx->opcode));
3365 3366 3367
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3368
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3369 3370
}

3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
#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 已提交
3393 3394 3395 3396 3397 3398
/***                          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 已提交
3399 3400
    gen_op_clear_xer_ov();
    gen_op_clear_xer_ca();
B
bellard 已提交
3401 3402 3403
}

/* mfcr */
3404
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3405
{
3406
    uint32_t crm, crn;
3407

3408 3409 3410 3411 3412 3413
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
            gen_op_load_cro(7 - crn);
        }
3414 3415 3416
    } else {
        gen_op_load_cr();
    }
B
bellard 已提交
3417 3418 3419 3420 3421 3422
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3423
#if defined(CONFIG_USER_ONLY)
3424
    GEN_EXCP_PRIVREG(ctx);
3425
#else
3426
    if (unlikely(!ctx->supervisor)) {
3427
        GEN_EXCP_PRIVREG(ctx);
3428
        return;
3429
    }
B
bellard 已提交
3430 3431
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
3432
#endif
B
bellard 已提交
3433 3434
}

3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
#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 已提交
3446
/* mfspr */
3447
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3448
{
3449
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3450 3451
    uint32_t sprn = SPR(ctx->opcode);

3452
#if !defined(CONFIG_USER_ONLY)
3453 3454 3455 3456 3457
#if defined(TARGET_PPC64H)
    if (ctx->supervisor == 2)
        read_cb = ctx->spr_cb[sprn].hea_read;
    else
#endif
3458 3459 3460
    if (ctx->supervisor)
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3461
#endif
3462
        read_cb = ctx->spr_cb[sprn].uea_read;
3463 3464
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3465 3466 3467 3468
            (*read_cb)(ctx, sprn);
            gen_op_store_T0_gpr(rD(ctx->opcode));
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3469
            if (loglevel != 0) {
B
blueswir1 已提交
3470
                fprintf(logfile, "Trying to read privileged spr %d %03x\n",
3471 3472
                        sprn, sprn);
            }
B
blueswir1 已提交
3473
            printf("Trying to read privileged spr %d %03x\n", sprn, sprn);
3474
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3475
        }
3476 3477
    } else {
        /* Not defined */
J
j_mayer 已提交
3478
        if (loglevel != 0) {
3479 3480 3481
            fprintf(logfile, "Trying to read invalid spr %d %03x\n",
                    sprn, sprn);
        }
3482
        printf("Trying to read invalid spr %d %03x\n", sprn, sprn);
3483 3484
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3485 3486 3487
    }
}

3488
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3489
{
3490
    gen_op_mfspr(ctx);
3491
}
3492 3493

/* mftb */
3494
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3495 3496
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3497 3498 3499
}

/* mtcrf */
3500
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3501
{
3502
    uint32_t crm, crn;
3503

B
bellard 已提交
3504
    gen_op_load_gpr_T0(rS(ctx->opcode));
3505 3506 3507 3508 3509 3510 3511 3512 3513
    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 已提交
3514 3515 3516
}

/* mtmsr */
J
j_mayer 已提交
3517
#if defined(TARGET_PPC64)
3518
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3519 3520
{
#if defined(CONFIG_USER_ONLY)
3521
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3522 3523
#else
    if (unlikely(!ctx->supervisor)) {
3524
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3525 3526 3527
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3528 3529 3530 3531
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3532 3533 3534 3535
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3536 3537 3538 3539
        gen_update_nip(ctx, ctx->nip);
        gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
3540
        ctx->exception = POWERPC_EXCP_STOP;
3541
    }
J
j_mayer 已提交
3542 3543 3544 3545
#endif
}
#endif

B
bellard 已提交
3546 3547
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3548
#if defined(CONFIG_USER_ONLY)
3549
    GEN_EXCP_PRIVREG(ctx);
3550
#else
3551
    if (unlikely(!ctx->supervisor)) {
3552
        GEN_EXCP_PRIVREG(ctx);
3553
        return;
3554
    }
B
bellard 已提交
3555
    gen_op_load_gpr_T0(rS(ctx->opcode));
3556 3557 3558 3559
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3560 3561 3562 3563
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3564
        gen_update_nip(ctx, ctx->nip);
3565
#if defined(TARGET_PPC64)
3566 3567 3568
        if (!ctx->sf_mode)
            gen_op_store_msr_32();
        else
3569
#endif
3570 3571 3572
            gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsrd is not always defined as context-synchronizing */
3573
        ctx->exception = POWERPC_EXCP_STOP;
3574
    }
3575
#endif
B
bellard 已提交
3576 3577 3578 3579 3580
}

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

3584
#if !defined(CONFIG_USER_ONLY)
3585 3586 3587 3588 3589
#if defined(TARGET_PPC64H)
    if (ctx->supervisor == 2)
        write_cb = ctx->spr_cb[sprn].hea_write;
    else
#endif
3590 3591 3592
    if (ctx->supervisor)
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3593
#endif
3594
        write_cb = ctx->spr_cb[sprn].uea_write;
3595 3596
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
3597 3598 3599 3600
            gen_op_load_gpr_T0(rS(ctx->opcode));
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3601
            if (loglevel != 0) {
B
blueswir1 已提交
3602
                fprintf(logfile, "Trying to write privileged spr %d %03x\n",
3603 3604
                        sprn, sprn);
            }
B
blueswir1 已提交
3605
            printf("Trying to write privileged spr %d %03x\n", sprn, sprn);
3606
            GEN_EXCP_PRIVREG(ctx);
3607
        }
3608 3609
    } else {
        /* Not defined */
J
j_mayer 已提交
3610
        if (loglevel != 0) {
3611 3612 3613
            fprintf(logfile, "Trying to write invalid spr %d %03x\n",
                    sprn, sprn);
        }
3614
        printf("Trying to write invalid spr %d %03x\n", sprn, sprn);
3615 3616
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3617 3618 3619 3620 3621
    }
}

/***                         Cache management                              ***/
/* dcbf */
3622
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
3623
{
J
j_mayer 已提交
3624
    /* XXX: specification says this is treated as a load by the MMU */
3625
    gen_addr_reg_index(ctx);
3626
    op_ldst(lbz);
B
bellard 已提交
3627 3628 3629
}

/* dcbi (Supervisor only) */
3630
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3631
{
3632
#if defined(CONFIG_USER_ONLY)
3633
    GEN_EXCP_PRIVOPC(ctx);
3634
#else
3635
    if (unlikely(!ctx->supervisor)) {
3636
        GEN_EXCP_PRIVOPC(ctx);
3637
        return;
3638
    }
3639 3640
    gen_addr_reg_index(ctx);
    /* XXX: specification says this should be treated as a store by the MMU */
J
j_mayer 已提交
3641
    op_ldst(lbz);
3642 3643
    op_ldst(stb);
#endif
B
bellard 已提交
3644 3645 3646
}

/* dcdst */
3647
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3648
{
3649 3650
    /* XXX: specification say this is treated as a load by the MMU */
    gen_addr_reg_index(ctx);
3651
    op_ldst(lbz);
B
bellard 已提交
3652 3653 3654
}

/* dcbt */
3655
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
3656
{
3657
    /* interpreted as no-op */
3658 3659 3660
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3661 3662 3663
}

/* dcbtst */
3664
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
3665
{
3666
    /* interpreted as no-op */
3667 3668 3669
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3670 3671 3672
}

/* dcbz */
3673
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3674
#if defined(CONFIG_USER_ONLY)
3675
/* User-mode only */
3676 3677 3678 3679
static GenOpFunc *gen_op_dcbz[4][4] = {
    {
        &gen_op_dcbz_l32_raw,
        &gen_op_dcbz_l32_raw,
3680
#if defined(TARGET_PPC64)
3681 3682
        &gen_op_dcbz_l32_64_raw,
        &gen_op_dcbz_l32_64_raw,
3683
#endif
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
    },
    {
        &gen_op_dcbz_l64_raw,
        &gen_op_dcbz_l64_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_l64_64_raw,
        &gen_op_dcbz_l64_64_raw,
#endif
    },
    {
        &gen_op_dcbz_l128_raw,
        &gen_op_dcbz_l128_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_l128_64_raw,
        &gen_op_dcbz_l128_64_raw,
#endif
    },
    {
        &gen_op_dcbz_raw,
        &gen_op_dcbz_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_64_raw,
        &gen_op_dcbz_64_raw,
#endif
    },
3709 3710
};
#else
3711 3712
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
static GenOpFunc *gen_op_dcbz[4][12] = {
    {
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_64_user,
        &gen_op_dcbz_l32_64_user,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_64_kernel,
        &gen_op_dcbz_l32_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_l32_hypv,
        &gen_op_dcbz_l32_hypv,
        &gen_op_dcbz_l32_64_hypv,
        &gen_op_dcbz_l32_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_64_user,
        &gen_op_dcbz_l64_64_user,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_64_kernel,
        &gen_op_dcbz_l64_64_kernel,
3739
#if defined(TARGET_PPC64H)
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
        &gen_op_dcbz_l64_hypv,
        &gen_op_dcbz_l64_hypv,
        &gen_op_dcbz_l64_64_hypv,
        &gen_op_dcbz_l64_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_64_user,
        &gen_op_dcbz_l128_64_user,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_64_kernel,
        &gen_op_dcbz_l128_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_l128_hypv,
        &gen_op_dcbz_l128_hypv,
        &gen_op_dcbz_l128_64_hypv,
        &gen_op_dcbz_l128_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_user,
        &gen_op_dcbz_user,
        &gen_op_dcbz_64_user,
        &gen_op_dcbz_64_user,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_64_kernel,
        &gen_op_dcbz_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_hypv,
        &gen_op_dcbz_hypv,
        &gen_op_dcbz_64_hypv,
        &gen_op_dcbz_64_hypv,
3776
#endif
3777
    },
3778
};
3779
#else
3780
/* Full system - 32 bits mode */
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
static GenOpFunc *gen_op_dcbz[4][4] = {
    {
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_kernel,
    },
    {
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_kernel,
    },
    {
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_kernel,
    },
    {
        &gen_op_dcbz_user,
        &gen_op_dcbz_user,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_kernel,
    },
3806 3807
};
#endif
3808
#endif
3809

3810 3811
static always_inline void handler_dcbz (DisasContext *ctx,
                                        int dcache_line_size)
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832
{
    int n;

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

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
3833
{
3834
    gen_addr_reg_index(ctx);
3835 3836 3837 3838
    handler_dcbz(ctx, ctx->dcache_line_size);
    gen_op_check_reservation();
}

3839
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3840 3841 3842 3843 3844 3845
{
    gen_addr_reg_index(ctx);
    if (ctx->opcode & 0x00200000)
        handler_dcbz(ctx, ctx->dcache_line_size);
    else
        handler_dcbz(ctx, -1);
B
bellard 已提交
3846
    gen_op_check_reservation();
B
bellard 已提交
3847 3848 3849
}

/* icbi */
3850 3851
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
3852
/* User-mode only */
3853 3854 3855
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_raw,
    &gen_op_icbi_raw,
3856
#if defined(TARGET_PPC64)
3857 3858
    &gen_op_icbi_64_raw,
    &gen_op_icbi_64_raw,
3859
#endif
3860 3861
};
#else
3862 3863
/* Full system - 64 bits mode */
#if defined(TARGET_PPC64)
3864 3865 3866 3867 3868
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_64_user,
    &gen_op_icbi_64_user,
3869 3870
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
3871 3872
    &gen_op_icbi_64_kernel,
    &gen_op_icbi_64_kernel,
3873 3874 3875 3876 3877
#if defined(TARGET_PPC64H)
    &gen_op_icbi_hypv,
    &gen_op_icbi_hypv,
    &gen_op_icbi_64_hypv,
    &gen_op_icbi_64_hypv,
3878 3879 3880
#endif
};
#else
3881
/* Full system - 32 bits mode */
3882 3883 3884 3885 3886 3887 3888 3889
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
};
#endif
#endif
3890

3891
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3892
{
3893 3894
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3895
    gen_addr_reg_index(ctx);
3896
    op_icbi();
B
bellard 已提交
3897 3898 3899 3900
}

/* Optional: */
/* dcba */
3901
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
3902
{
3903 3904 3905 3906
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3907 3908 3909 3910 3911 3912 3913
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
3914
#if defined(CONFIG_USER_ONLY)
3915
    GEN_EXCP_PRIVREG(ctx);
3916
#else
3917
    if (unlikely(!ctx->supervisor)) {
3918
        GEN_EXCP_PRIVREG(ctx);
3919
        return;
3920
    }
3921 3922
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_sr();
3923 3924
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3925 3926 3927
}

/* mfsrin */
3928
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3929
{
3930
#if defined(CONFIG_USER_ONLY)
3931
    GEN_EXCP_PRIVREG(ctx);
3932
#else
3933
    if (unlikely(!ctx->supervisor)) {
3934
        GEN_EXCP_PRIVREG(ctx);
3935
        return;
3936 3937
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
3938 3939
    gen_op_srli_T1(28);
    gen_op_load_sr();
3940 3941
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3942 3943 3944
}

/* mtsr */
B
bellard 已提交
3945
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
3946
{
3947
#if defined(CONFIG_USER_ONLY)
3948
    GEN_EXCP_PRIVREG(ctx);
3949
#else
3950
    if (unlikely(!ctx->supervisor)) {
3951
        GEN_EXCP_PRIVREG(ctx);
3952
        return;
3953 3954
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3955 3956
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_sr();
3957
#endif
B
bellard 已提交
3958 3959 3960
}

/* mtsrin */
3961
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3962
{
3963
#if defined(CONFIG_USER_ONLY)
3964
    GEN_EXCP_PRIVREG(ctx);
3965
#else
3966
    if (unlikely(!ctx->supervisor)) {
3967
        GEN_EXCP_PRIVREG(ctx);
3968
        return;
3969 3970 3971
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3972 3973
    gen_op_srli_T1(28);
    gen_op_store_sr();
3974
#endif
B
bellard 已提交
3975 3976
}

3977 3978 3979
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
3980
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mfsrin */
3996 3997
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtsr */
4014
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_slb();
#endif
}

/* mtsrin */
4030 4031
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_store_slb();
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4048 4049 4050
/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
4051
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4052
{
4053
#if defined(CONFIG_USER_ONLY)
4054
    GEN_EXCP_PRIVOPC(ctx);
4055
#else
4056
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
4057
        if (loglevel != 0)
4058
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4059
        GEN_EXCP_PRIVOPC(ctx);
4060
        return;
4061 4062 4063
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
4064 4065 4066
}

/* tlbie */
4067
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4068
{
4069
#if defined(CONFIG_USER_ONLY)
4070
    GEN_EXCP_PRIVOPC(ctx);
4071
#else
4072
    if (unlikely(!ctx->supervisor)) {
4073
        GEN_EXCP_PRIVOPC(ctx);
4074
        return;
4075 4076
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
4077 4078 4079 4080 4081 4082
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4083
#endif
B
bellard 已提交
4084 4085 4086
}

/* tlbsync */
4087
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4088
{
4089
#if defined(CONFIG_USER_ONLY)
4090
    GEN_EXCP_PRIVOPC(ctx);
4091
#else
4092
    if (unlikely(!ctx->supervisor)) {
4093
        GEN_EXCP_PRIVOPC(ctx);
4094
        return;
4095 4096 4097 4098
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
4099
    GEN_STOP(ctx);
4100
#endif
B
bellard 已提交
4101 4102
}

J
j_mayer 已提交
4103 4104 4105 4106 4107
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4108
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4109 4110
#else
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
4111
        if (loglevel != 0)
J
j_mayer 已提交
4112
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4113
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4114 4115 4116 4117 4118 4119 4120 4121 4122 4123
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4124
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4125 4126
#else
    if (unlikely(!ctx->supervisor)) {
4127
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4128 4129 4130 4131 4132 4133 4134 4135
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
4136 4137
/***                              External control                         ***/
/* Optional: */
4138 4139
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4140
#if defined(CONFIG_USER_ONLY)
4141
/* User-mode only */
4142 4143 4144
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_raw,
    &gen_op_eciwx_le_raw,
4145
#if defined(TARGET_PPC64)
4146 4147
    &gen_op_eciwx_64_raw,
    &gen_op_eciwx_le_64_raw,
4148
#endif
4149 4150 4151 4152
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_raw,
    &gen_op_ecowx_le_raw,
4153
#if defined(TARGET_PPC64)
4154 4155
    &gen_op_ecowx_64_raw,
    &gen_op_ecowx_le_64_raw,
4156
#endif
4157 4158
};
#else
4159 4160
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
4161 4162
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
4163
    &gen_op_eciwx_le_user,
4164 4165
    &gen_op_eciwx_64_user,
    &gen_op_eciwx_le_64_user,
4166 4167
    &gen_op_eciwx_kernel,
    &gen_op_eciwx_le_kernel,
4168 4169
    &gen_op_eciwx_64_kernel,
    &gen_op_eciwx_le_64_kernel,
4170 4171 4172 4173 4174 4175
#if defined(TARGET_PPC64H)
    &gen_op_eciwx_hypv,
    &gen_op_eciwx_le_hypv,
    &gen_op_eciwx_64_hypv,
    &gen_op_eciwx_le_64_hypv,
#endif
4176 4177 4178
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
4179
    &gen_op_ecowx_le_user,
4180 4181
    &gen_op_ecowx_64_user,
    &gen_op_ecowx_le_64_user,
4182 4183
    &gen_op_ecowx_kernel,
    &gen_op_ecowx_le_kernel,
4184 4185
    &gen_op_ecowx_64_kernel,
    &gen_op_ecowx_le_64_kernel,
4186 4187 4188 4189 4190
#if defined(TARGET_PPC64H)
    &gen_op_ecowx_hypv,
    &gen_op_ecowx_le_hypv,
    &gen_op_ecowx_64_hypv,
    &gen_op_ecowx_le_64_hypv,
4191
#endif
4192 4193
};
#else
4194
/* Full system - 32 bits mode */
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
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
4209

4210
/* eciwx */
B
bellard 已提交
4211 4212
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
4213
    /* Should check EAR[E] & alignment ! */
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249
    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 */
4250
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4251 4252 4253
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_clcs();
4254
    /* Rc=1 sets CR0 to an undefined state */
4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333
    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 */
4334
#define op_POWER_lscbx(start, ra, rb)                                         \
4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
(*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 */
4361
    gen_update_nip(ctx, ctx->nip - 4);
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 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 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528
    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);
}

4529
/* sraiq - sraiq. */
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633
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 */
4634
    GEN_EXCP_INVAL(ctx);
4635 4636 4637 4638 4639 4640
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4641
    GEN_EXCP_INVAL(ctx);
4642 4643 4644 4645 4646 4647
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4648
    GEN_EXCP_PRIVOPC(ctx);
4649 4650
#else
    if (unlikely(!ctx->supervisor)) {
4651
        GEN_EXCP_PRIVOPC(ctx);
4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
        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 */
4662
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4663 4664
{
#if defined(CONFIG_USER_ONLY)
4665
    GEN_EXCP_PRIVOPC(ctx);
4666 4667
#else
    if (unlikely(!ctx->supervisor)) {
4668
        GEN_EXCP_PRIVOPC(ctx);
4669 4670 4671 4672 4673 4674 4675 4676
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
4677
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4678 4679
{
#if defined(CONFIG_USER_ONLY)
4680
    GEN_EXCP_PRIVOPC(ctx);
4681 4682
#else
    if (unlikely(!ctx->supervisor)) {
4683
        GEN_EXCP_PRIVOPC(ctx);
4684 4685 4686 4687 4688 4689 4690
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbli();
#endif
}

4691 4692
/* 74xx TLB management */
/* tlbld */
4693
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbld();
#endif
}

/* tlbli */
4708
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbli();
#endif
}

4722 4723 4724 4725 4726 4727 4728 4729 4730 4731
/* 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 已提交
4732
    /* Cache line invalidate: privileged and treated as no-op */
4733
#if defined(CONFIG_USER_ONLY)
4734
    GEN_EXCP_PRIVOPC(ctx);
4735 4736
#else
    if (unlikely(!ctx->supervisor)) {
4737
        GEN_EXCP_PRIVOPC(ctx);
4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751
        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)
4752
    GEN_EXCP_PRIVOPC(ctx);
4753 4754
#else
    if (unlikely(!ctx->supervisor)) {
4755
        GEN_EXCP_PRIVOPC(ctx);
4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
        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)
4772
    GEN_EXCP_PRIVOPC(ctx);
4773 4774
#else
    if (unlikely(!ctx->supervisor)) {
4775
        GEN_EXCP_PRIVOPC(ctx);
4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
        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)
4787
    GEN_EXCP_PRIVOPC(ctx);
4788 4789
#else
    if (unlikely(!ctx->supervisor)) {
4790
        GEN_EXCP_PRIVOPC(ctx);
4791 4792 4793
        return;
    }
    gen_op_POWER_rfsvc();
4794
    GEN_SYNC(ctx);
4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831
#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 */
4832
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4833
    gen_addr_imm_index(ctx, 0);
4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
    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 */
4845
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4846
    gen_addr_imm_index(ctx, 0);
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
    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 */
4860
    gen_update_nip(ctx, ctx->nip - 4);
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
    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 */
4873
    gen_update_nip(ctx, ctx->nip - 4);
4874 4875 4876 4877 4878 4879 4880 4881 4882 4883
    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 */
4884
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4885
    gen_addr_imm_index(ctx, 0);
4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896
    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 */
4897
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4898
    gen_addr_imm_index(ctx, 0);
4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911
    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 */
4912
    gen_update_nip(ctx, ctx->nip - 4);
4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924
    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 */
4925
    gen_update_nip(ctx, ctx->nip - 4);
4926 4927 4928 4929 4930 4931 4932
    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 */
4933
/* XXX: not implemented on 440 ? */
4934
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
4935 4936
{
    /* XXX: TODO */
4937
    GEN_EXCP_INVAL(ctx);
4938 4939
}

4940
/* XXX: not implemented on 440 ? */
4941
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
4942 4943
{
#if defined(CONFIG_USER_ONLY)
4944
    GEN_EXCP_PRIVOPC(ctx);
4945 4946
#else
    if (unlikely(!ctx->supervisor)) {
4947
        GEN_EXCP_PRIVOPC(ctx);
4948 4949 4950 4951
        return;
    }
    gen_addr_reg_index(ctx);
    /* Use the same micro-ops as for tlbie */
4952 4953 4954 4955 4956 4957
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4958 4959 4960 4961
#endif
}

/* All 405 MAC instructions are translated here */
4962 4963 4964
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
4965 4966 4967 4968 4969 4970 4971 4972 4973 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 5007 5008 5009 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 5037 5038 5039 5040 5041 5042
{
    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);
    }
}

5043 5044
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5045 5046 5047 5048 5049 5050
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5051
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5052
/* macchwo   - macchwo.   */
5053
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5054
/* macchws   - macchws.   */
5055
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5056
/* macchwso  - macchwso.  */
5057
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5058
/* macchwsu  - macchwsu.  */
5059
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5060
/* macchwsuo - macchwsuo. */
5061
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5062
/* macchwu   - macchwu.   */
5063
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5064
/* macchwuo  - macchwuo.  */
5065
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5066
/* machhw    - machhw.    */
5067
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5068
/* machhwo   - machhwo.   */
5069
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5070
/* machhws   - machhws.   */
5071
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5072
/* machhwso  - machhwso.  */
5073
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5074
/* machhwsu  - machhwsu.  */
5075
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5076
/* machhwsuo - machhwsuo. */
5077
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5078
/* machhwu   - machhwu.   */
5079
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5080
/* machhwuo  - machhwuo.  */
5081
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5082
/* maclhw    - maclhw.    */
5083
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5084
/* maclhwo   - maclhwo.   */
5085
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5086
/* maclhws   - maclhws.   */
5087
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5088
/* maclhwso  - maclhwso.  */
5089
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5090
/* maclhwu   - maclhwu.   */
5091
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5092
/* maclhwuo  - maclhwuo.  */
5093
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5094
/* maclhwsu  - maclhwsu.  */
5095
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5096
/* maclhwsuo - maclhwsuo. */
5097
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5098
/* nmacchw   - nmacchw.   */
5099
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5100
/* nmacchwo  - nmacchwo.  */
5101
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5102
/* nmacchws  - nmacchws.  */
5103
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5104
/* nmacchwso - nmacchwso. */
5105
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5106
/* nmachhw   - nmachhw.   */
5107
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5108
/* nmachhwo  - nmachhwo.  */
5109
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5110
/* nmachhws  - nmachhws.  */
5111
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5112
/* nmachhwso - nmachhwso. */
5113
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5114
/* nmaclhw   - nmaclhw.   */
5115
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5116
/* nmaclhwo  - nmaclhwo.  */
5117
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5118
/* nmaclhws  - nmaclhws.  */
5119
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5120
/* nmaclhwso - nmaclhwso. */
5121
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5122 5123

/* mulchw  - mulchw.  */
5124
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5125
/* mulchwu - mulchwu. */
5126
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5127
/* mulhhw  - mulhhw.  */
5128
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5129
/* mulhhwu - mulhhwu. */
5130
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5131
/* mullhw  - mullhw.  */
5132
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5133
/* mullhwu - mullhwu. */
5134
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5135 5136 5137 5138 5139

/* mfdcr */
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5140
    GEN_EXCP_PRIVREG(ctx);
5141 5142 5143 5144
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5145
        GEN_EXCP_PRIVREG(ctx);
5146 5147
        return;
    }
5148 5149
    gen_op_set_T0(dcrn);
    gen_op_load_dcr();
5150 5151 5152 5153 5154 5155 5156 5157
    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)
5158
    GEN_EXCP_PRIVREG(ctx);
5159 5160 5161 5162
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5163
        GEN_EXCP_PRIVREG(ctx);
5164 5165
        return;
    }
5166 5167 5168 5169 5170 5171 5172
    gen_op_set_T0(dcrn);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
5173
/* XXX: not implemented on 440 ? */
5174
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
5175 5176
{
#if defined(CONFIG_USER_ONLY)
5177
    GEN_EXCP_PRIVREG(ctx);
5178 5179
#else
    if (unlikely(!ctx->supervisor)) {
5180
        GEN_EXCP_PRIVREG(ctx);
5181 5182 5183 5184 5185
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
5186
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5187 5188 5189 5190
#endif
}

/* mtdcrx */
5191
/* XXX: not implemented on 440 ? */
5192
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
5193 5194
{
#if defined(CONFIG_USER_ONLY)
5195
    GEN_EXCP_PRIVREG(ctx);
5196 5197
#else
    if (unlikely(!ctx->supervisor)) {
5198
        GEN_EXCP_PRIVREG(ctx);
5199 5200 5201 5202 5203
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
5204
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5205 5206 5207
#endif
}

5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225
/* 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 */
}

5226 5227 5228 5229
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5230
    GEN_EXCP_PRIVOPC(ctx);
5231 5232
#else
    if (unlikely(!ctx->supervisor)) {
5233
        GEN_EXCP_PRIVOPC(ctx);
5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5244
    GEN_EXCP_PRIVOPC(ctx);
5245 5246
#else
    if (unlikely(!ctx->supervisor)) {
5247
        GEN_EXCP_PRIVOPC(ctx);
5248 5249 5250 5251 5252 5253 5254 5255 5256
        return;
    }
    gen_addr_reg_index(ctx);
    op_ldst(lwz);
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* icbt */
5257
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268
{
    /* 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)
5269
    GEN_EXCP_PRIVOPC(ctx);
5270 5271
#else
    if (unlikely(!ctx->supervisor)) {
5272
        GEN_EXCP_PRIVOPC(ctx);
5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5283
    GEN_EXCP_PRIVOPC(ctx);
5284 5285
#else
    if (unlikely(!ctx->supervisor)) {
5286
        GEN_EXCP_PRIVOPC(ctx);
5287 5288 5289 5290 5291 5292 5293
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
5294
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5295 5296
{
#if defined(CONFIG_USER_ONLY)
5297
    GEN_EXCP_PRIVOPC(ctx);
5298 5299
#else
    if (unlikely(!ctx->supervisor)) {
5300
        GEN_EXCP_PRIVOPC(ctx);
5301 5302 5303 5304
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
5305
    GEN_SYNC(ctx);
5306 5307 5308 5309 5310 5311
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
5312
    GEN_EXCP_PRIVOPC(ctx);
5313 5314
#else
    if (unlikely(!ctx->supervisor)) {
5315
        GEN_EXCP_PRIVOPC(ctx);
5316 5317 5318 5319
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
5320
    GEN_SYNC(ctx);
5321 5322 5323 5324
#endif
}

/* BookE specific */
5325
/* XXX: not implemented on 440 ? */
5326
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
5327 5328
{
#if defined(CONFIG_USER_ONLY)
5329
    GEN_EXCP_PRIVOPC(ctx);
5330 5331
#else
    if (unlikely(!ctx->supervisor)) {
5332
        GEN_EXCP_PRIVOPC(ctx);
5333 5334 5335
        return;
    }
    /* Restore CPU state */
5336
    gen_op_rfdi();
5337
    GEN_SYNC(ctx);
5338 5339 5340
#endif
}

5341
/* XXX: not implemented on 440 ? */
5342
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5343 5344
{
#if defined(CONFIG_USER_ONLY)
5345
    GEN_EXCP_PRIVOPC(ctx);
5346 5347
#else
    if (unlikely(!ctx->supervisor)) {
5348
        GEN_EXCP_PRIVOPC(ctx);
5349 5350 5351 5352
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
5353
    GEN_SYNC(ctx);
5354 5355
#endif
}
5356

5357
/* TLB management - PowerPC 405 implementation */
5358
/* tlbre */
5359
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5360 5361
{
#if defined(CONFIG_USER_ONLY)
5362
    GEN_EXCP_PRIVOPC(ctx);
5363 5364
#else
    if (unlikely(!ctx->supervisor)) {
5365
        GEN_EXCP_PRIVOPC(ctx);
5366 5367 5368 5369
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5370
        gen_op_load_gpr_T0(rA(ctx->opcode));
5371 5372 5373 5374 5375 5376 5377 5378 5379
        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:
5380
        GEN_EXCP_INVAL(ctx);
5381
        break;
5382
    }
5383 5384 5385
#endif
}

5386
/* tlbsx - tlbsx. */
5387
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5388 5389
{
#if defined(CONFIG_USER_ONLY)
5390
    GEN_EXCP_PRIVOPC(ctx);
5391 5392
#else
    if (unlikely(!ctx->supervisor)) {
5393
        GEN_EXCP_PRIVOPC(ctx);
5394 5395 5396
        return;
    }
    gen_addr_reg_index(ctx);
5397
    gen_op_4xx_tlbsx();
5398
    if (Rc(ctx->opcode))
5399
        gen_op_4xx_tlbsx_check();
5400
    gen_op_store_T0_gpr(rD(ctx->opcode));
5401
#endif
B
bellard 已提交
5402 5403
}

5404
/* tlbwe */
5405
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5406
{
5407
#if defined(CONFIG_USER_ONLY)
5408
    GEN_EXCP_PRIVOPC(ctx);
5409 5410
#else
    if (unlikely(!ctx->supervisor)) {
5411
        GEN_EXCP_PRIVOPC(ctx);
5412 5413 5414 5415
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5416
        gen_op_load_gpr_T0(rA(ctx->opcode));
5417 5418 5419 5420 5421 5422 5423 5424 5425
        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:
5426
        GEN_EXCP_INVAL(ctx);
5427
        break;
5428
    }
5429 5430 5431
#endif
}

5432
/* TLB management - PowerPC 440 implementation */
5433
/* tlbre */
5434
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5435 5436
{
#if defined(CONFIG_USER_ONLY)
5437
    GEN_EXCP_PRIVOPC(ctx);
5438 5439
#else
    if (unlikely(!ctx->supervisor)) {
5440
        GEN_EXCP_PRIVOPC(ctx);
5441 5442 5443 5444 5445 5446 5447
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
5448
        gen_op_440_tlbre(rB(ctx->opcode));
5449 5450 5451
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
5452
        GEN_EXCP_INVAL(ctx);
5453 5454 5455 5456 5457 5458
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5459
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5460 5461
{
#if defined(CONFIG_USER_ONLY)
5462
    GEN_EXCP_PRIVOPC(ctx);
5463 5464
#else
    if (unlikely(!ctx->supervisor)) {
5465
        GEN_EXCP_PRIVOPC(ctx);
5466 5467 5468
        return;
    }
    gen_addr_reg_index(ctx);
5469
    gen_op_440_tlbsx();
5470
    if (Rc(ctx->opcode))
5471
        gen_op_4xx_tlbsx_check();
5472 5473 5474 5475 5476
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* tlbwe */
5477
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5478 5479
{
#if defined(CONFIG_USER_ONLY)
5480
    GEN_EXCP_PRIVOPC(ctx);
5481 5482
#else
    if (unlikely(!ctx->supervisor)) {
5483
        GEN_EXCP_PRIVOPC(ctx);
5484 5485 5486 5487 5488 5489 5490 5491
        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));
5492
        gen_op_440_tlbwe(rB(ctx->opcode));
5493 5494
        break;
    default:
5495
        GEN_EXCP_INVAL(ctx);
5496 5497 5498 5499 5500
        break;
    }
#endif
}

5501 5502 5503 5504
/* wrtee */
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5505
    GEN_EXCP_PRIVOPC(ctx);
5506 5507
#else
    if (unlikely(!ctx->supervisor)) {
5508
        GEN_EXCP_PRIVOPC(ctx);
5509 5510 5511
        return;
    }
    gen_op_load_gpr_T0(rD(ctx->opcode));
5512
    gen_op_wrte();
J
j_mayer 已提交
5513 5514 5515
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5516
    GEN_STOP(ctx);
5517 5518 5519 5520 5521 5522 5523
#endif
}

/* wrteei */
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5524
    GEN_EXCP_PRIVOPC(ctx);
5525 5526
#else
    if (unlikely(!ctx->supervisor)) {
5527
        GEN_EXCP_PRIVOPC(ctx);
5528 5529 5530
        return;
    }
    gen_op_set_T0(ctx->opcode & 0x00010000);
5531
    gen_op_wrte();
J
j_mayer 已提交
5532 5533 5534
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5535
    GEN_STOP(ctx);
5536 5537 5538
#endif
}

J
j_mayer 已提交
5539
/* PowerPC 440 specific instructions */
5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560
/* 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 */
5561
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5562 5563 5564 5565 5566
{
    /* interpreted as no-op */
}

/* icbt */
5567
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5568 5569 5570 5571 5572
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
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
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */
GEN32(gen_op_load_avr_A0, gen_op_load_avr_A0_avr);
GEN32(gen_op_load_avr_A1, gen_op_load_avr_A1_avr);
GEN32(gen_op_load_avr_A2, gen_op_load_avr_A2_avr);

GEN32(gen_op_store_A0_avr, gen_op_store_A0_avr_avr);
GEN32(gen_op_store_A1_avr, gen_op_store_A1_avr_avr);
#if 0 // unused
GEN32(gen_op_store_A2_avr, gen_op_store_A2_avr_avr);
#endif

#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
#if defined(TARGET_PPC64)
/* User-mode only - 64 bits mode */
#define OP_VR_LD_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
    &gen_op_vr_l##name##_raw,                                                 \
    &gen_op_vr_l##name##_le_raw,                                              \
    &gen_op_vr_l##name##_64_raw,                                              \
    &gen_op_vr_l##name##_le_64_raw,                                           \
};
#define OP_VR_ST_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
    &gen_op_vr_st##name##_raw,                                                \
    &gen_op_vr_st##name##_le_raw,                                             \
    &gen_op_vr_st##name##_64_raw,                                             \
    &gen_op_vr_st##name##_le_64_raw,                                          \
};
#else /* defined(TARGET_PPC64) */
/* User-mode only - 32 bits mode */
#define OP_VR_LD_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
    &gen_op_vr_l##name##_raw,                                                 \
    &gen_op_vr_l##name##_le_raw,                                              \
};
#define OP_VR_ST_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
    &gen_op_vr_st##name##_raw,                                                \
    &gen_op_vr_st##name##_le_raw,                                             \
};
#endif /* defined(TARGET_PPC64) */
#else /* defined(CONFIG_USER_ONLY) */
#if defined(TARGET_PPC64H)
/* Full system with hypervisor mode */
#define OP_VR_LD_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
    &gen_op_vr_l##name##_user,                                                \
    &gen_op_vr_l##name##_le_user,                                             \
    &gen_op_vr_l##name##_64_user,                                             \
    &gen_op_vr_l##name##_le_64_user,                                          \
    &gen_op_vr_l##name##_kernel,                                              \
    &gen_op_vr_l##name##_le_kernel,                                           \
    &gen_op_vr_l##name##_64_kernel,                                           \
    &gen_op_vr_l##name##_le_64_kernel,                                        \
    &gen_op_vr_l##name##_hypv,                                                \
    &gen_op_vr_l##name##_le_hypv,                                             \
    &gen_op_vr_l##name##_64_hypv,                                             \
    &gen_op_vr_l##name##_le_64_hypv,                                          \
};
#define OP_VR_ST_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
    &gen_op_vr_st##name##_user,                                               \
    &gen_op_vr_st##name##_le_user,                                            \
    &gen_op_vr_st##name##_64_user,                                            \
    &gen_op_vr_st##name##_le_64_user,                                         \
    &gen_op_vr_st##name##_kernel,                                             \
    &gen_op_vr_st##name##_le_kernel,                                          \
    &gen_op_vr_st##name##_64_kernel,                                          \
    &gen_op_vr_st##name##_le_64_kernel,                                       \
    &gen_op_vr_st##name##_hypv,                                               \
    &gen_op_vr_st##name##_le_hypv,                                            \
    &gen_op_vr_st##name##_64_hypv,                                            \
    &gen_op_vr_st##name##_le_64_hypv,                                         \
};
#elif defined(TARGET_PPC64)
/* Full system - 64 bits mode */
#define OP_VR_LD_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
    &gen_op_vr_l##name##_user,                                                \
    &gen_op_vr_l##name##_le_user,                                             \
    &gen_op_vr_l##name##_64_user,                                             \
    &gen_op_vr_l##name##_le_64_user,                                          \
    &gen_op_vr_l##name##_kernel,                                              \
    &gen_op_vr_l##name##_le_kernel,                                           \
    &gen_op_vr_l##name##_64_kernel,                                           \
    &gen_op_vr_l##name##_le_64_kernel,                                        \
};
#define OP_VR_ST_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
    &gen_op_vr_st##name##_user,                                               \
    &gen_op_vr_st##name##_le_user,                                            \
    &gen_op_vr_st##name##_64_user,                                            \
    &gen_op_vr_st##name##_le_64_user,                                         \
    &gen_op_vr_st##name##_kernel,                                             \
    &gen_op_vr_st##name##_le_kernel,                                          \
    &gen_op_vr_st##name##_64_kernel,                                          \
    &gen_op_vr_st##name##_le_64_kernel,                                       \
};
#else /* defined(TARGET_PPC64) */
/* Full system - 32 bits mode */
#define OP_VR_LD_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
    &gen_op_vr_l##name##_user,                                                \
    &gen_op_vr_l##name##_le_user,                                             \
    &gen_op_vr_l##name##_kernel,                                              \
    &gen_op_vr_l##name##_le_kernel,                                           \
};
#define OP_VR_ST_TABLE(name)                                                  \
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
    &gen_op_vr_st##name##_user,                                               \
    &gen_op_vr_st##name##_le_user,                                            \
    &gen_op_vr_st##name##_kernel,                                             \
    &gen_op_vr_st##name##_le_kernel,                                          \
};
#endif /* defined(TARGET_PPC64) */
#endif /* defined(CONFIG_USER_ONLY) */

#define GEN_VR_LDX(name, opc2, opc3)                                          \
GEN_HANDLER(l##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)               \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    op_vr_ldst(vr_l##name);                                                   \
    gen_op_store_A0_avr(rD(ctx->opcode));                                     \
}

#define GEN_VR_STX(name, opc2, opc3)                                          \
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_avr_A0(rS(ctx->opcode));                                      \
    op_vr_ldst(vr_st##name);                                                  \
}

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

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

5730
#if defined(TARGET_PPCEMB)
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
/***                           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 */
5756
static always_inline void gen_speundef (DisasContext *ctx)
5757
{
5758
    GEN_EXCP_INVAL(ctx);
5759 5760 5761
}

/* SPE load and stores */
5762
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777
{
    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)
5778
/* User-mode only - 64 bits mode */
5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793
#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) */
5794
/* User-mode only - 32 bits mode */
5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806
#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) */
5807 5808
#if defined(TARGET_PPC64H)
/* Full system with hypervisor mode */
5809 5810 5811 5812 5813 5814
#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##_64_user,                                            \
    &gen_op_spe_l##name##_le_64_user,                                         \
5815 5816
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
5817 5818
    &gen_op_spe_l##name##_64_kernel,                                          \
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5819 5820 5821 5822
    &gen_op_spe_l##name##_hypv,                                               \
    &gen_op_spe_l##name##_le_hypv,                                            \
    &gen_op_spe_l##name##_64_hypv,                                            \
    &gen_op_spe_l##name##_le_64_hypv,                                         \
5823 5824 5825 5826 5827
};
#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,                                           \
5828 5829
    &gen_op_spe_st##name##_64_user,                                           \
    &gen_op_spe_st##name##_le_64_user,                                        \
5830 5831
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855
    &gen_op_spe_st##name##_64_kernel,                                         \
    &gen_op_spe_st##name##_le_64_kernel,                                      \
    &gen_op_spe_st##name##_hypv,                                              \
    &gen_op_spe_st##name##_le_hypv,                                           \
    &gen_op_spe_st##name##_64_hypv,                                           \
    &gen_op_spe_st##name##_le_64_hypv,                                        \
};
#elif defined(TARGET_PPC64)
/* Full system - 64 bits mode */
#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##_64_user,                                            \
    &gen_op_spe_l##name##_le_64_user,                                         \
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
    &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,                                           \
5856 5857
    &gen_op_spe_st##name##_64_user,                                           \
    &gen_op_spe_st##name##_le_64_user,                                        \
5858 5859
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
5860 5861 5862 5863
    &gen_op_spe_st##name##_64_kernel,                                         \
    &gen_op_spe_st##name##_le_64_kernel,                                      \
};
#else /* defined(TARGET_PPC64) */
5864
/* Full system - 32 bits mode */
5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882
#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)                                                  \
5883
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5884 5885
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5886
        GEN_EXCP_NO_AP(ctx);                                                  \
5887 5888 5889 5890 5891 5892 5893 5894
        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)                                                     \
5895
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
5896 5897
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5898
        GEN_EXCP_NO_AP(ctx);                                                  \
5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911
        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)                                                  \
5912
static always_inline void gen_evst##name (DisasContext *ctx)                  \
5913 5914
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5915
        GEN_EXCP_NO_AP(ctx);                                                  \
5916 5917 5918 5919 5920 5921 5922 5923
        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)                                                     \
5924
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
5925 5926
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5927
        GEN_EXCP_NO_AP(ctx);                                                  \
5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945
        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)                                                \
5946
static always_inline void gen_##name (DisasContext *ctx)                      \
5947 5948
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5949
        GEN_EXCP_NO_AP(ctx);                                                  \
5950 5951 5952 5953 5954 5955 5956 5957 5958
        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)                                                \
5959
static always_inline void gen_##name (DisasContext *ctx)                      \
5960 5961
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5962
        GEN_EXCP_NO_AP(ctx);                                                  \
5963 5964 5965 5966 5967 5968 5969 5970
        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)                                                  \
5971
static always_inline void gen_##name (DisasContext *ctx)                      \
5972 5973
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5974
        GEN_EXCP_NO_AP(ctx);                                                  \
5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010
        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);
6011
static always_inline void gen_brinc (DisasContext *ctx)
6012 6013 6014 6015 6016 6017 6018 6019 6020
{
    /* 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)                                            \
6021
static always_inline void gen_##name##i (DisasContext *ctx)                   \
6022 6023
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
6024
        GEN_EXCP_NO_AP(ctx);                                                  \
6025 6026 6027 6028 6029 6030 6031 6032 6033
        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)                                            \
6034
static always_inline void gen_##name##i (DisasContext *ctx)                   \
6035 6036
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
6037
        GEN_EXCP_NO_AP(ctx);                                                  \
6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056
        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);

6057
static always_inline void gen_evsplati (DisasContext *ctx)
6058 6059 6060 6061 6062 6063 6064
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

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

6065
static always_inline void gen_evsplatfi (DisasContext *ctx)
6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105
{
    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); ////

6106
static always_inline void gen_evsel (DisasContext *ctx)
6107 6108
{
    if (unlikely(!ctx->spe_enabled)) {
6109
        GEN_EXCP_NO_AP(ctx);
6110 6111 6112 6113 6114 6115 6116 6117 6118
        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));
}

6119
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6120 6121 6122
{
    gen_evsel(ctx);
}
6123
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6124 6125 6126
{
    gen_evsel(ctx);
}
6127
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6128 6129 6130
{
    gen_evsel(ctx);
}
6131
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195
{
    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)                                             \
6196
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
6197 6198 6199 6200 6201
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
6202
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
6203 6204 6205 6206 6207 6208 6209 6210
{                                                                             \
    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);                                                 \
6211
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
6212 6213 6214 6215
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
6216
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235
{                                                                             \
    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)                                     \
6236
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
6237 6238 6239 6240 6241 6242
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
6243
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
6244 6245 6246 6247 6248 6249
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
6250
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425
{                                                                             \
    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)                                               \
6426
static always_inline void gen_##name (DisasContext *ctx)                      \
6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573
{                                                                             \
    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 已提交
6574 6575 6576
/* End opcode list */
GEN_OPCODE_MARK(end);

6577
#include "translate_init.c"
6578
#include "helper_regs.h"
B
bellard 已提交
6579

6580
/*****************************************************************************/
6581
/* Misc PowerPC helpers */
6582 6583 6584
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
6585
{
6586 6587 6588 6589 6590 6591 6592 6593 6594 6595
#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 已提交
6596 6597
    int i;

6598 6599
    cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX " idx %d\n",
                env->nip, env->lr, env->ctr, env->mmu_idx);
6600 6601 6602
    cpu_fprintf(f, "MSR " REGX FILL " XER %08x      "
#if !defined(NO_TIMER_DUMP)
                "TB %08x %08x "
6603 6604
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
6605
#endif
6606 6607
#endif
                "\n",
6608
                env->msr, hreg_load_xer(env)
6609 6610
#if !defined(NO_TIMER_DUMP)
                , cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6611 6612
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
6613
#endif
6614 6615 6616
#endif
                );
    for (i = 0; i < 32; i++) {
6617 6618
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
6619
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
6620
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
6621
            cpu_fprintf(f, "\n");
6622
    }
6623
    cpu_fprintf(f, "CR ");
6624
    for (i = 0; i < 8; i++)
B
bellard 已提交
6625 6626
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
6627 6628 6629 6630 6631 6632 6633 6634
    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 已提交
6635
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6636
    }
6637 6638 6639 6640
    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 已提交
6641
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6642
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
6643
            cpu_fprintf(f, "\n");
B
bellard 已提交
6644
    }
6645
#if !defined(CONFIG_USER_ONLY)
6646 6647 6648
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX "         " FILL FILL FILL
                "SDR1 " REGX "\n",
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6649
#endif
B
bellard 已提交
6650

6651 6652 6653
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
6654 6655
}

6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702
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
}

6703
/*****************************************************************************/
6704 6705 6706
static always_inline int gen_intermediate_code_internal (CPUState *env,
                                                         TranslationBlock *tb,
                                                         int search_pc)
B
bellard 已提交
6707
{
6708
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
6709
    opc_handler_t **table, *handler;
B
bellard 已提交
6710
    target_ulong pc_start;
B
bellard 已提交
6711
    uint16_t *gen_opc_end;
6712
    int supervisor;
6713
    int single_step, branch_step;
B
bellard 已提交
6714 6715 6716 6717 6718 6719
    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 已提交
6720
    nb_gen_labels = 0;
B
bellard 已提交
6721
    ctx.nip = pc_start;
B
bellard 已提交
6722
    ctx.tb = tb;
6723
    ctx.exception = POWERPC_EXCP_NONE;
6724
    ctx.spr_cb = env->spr_cb;
6725 6726
    supervisor = env->mmu_idx;
#if !defined(CONFIG_USER_ONLY)
6727
    ctx.supervisor = supervisor;
6728 6729 6730
#endif
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
6731 6732 6733
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | msr_le;
#else
    ctx.mem_idx = (supervisor << 1) | msr_le;
6734
#endif
6735
    ctx.dcache_line_size = env->dcache_line_size;
B
bellard 已提交
6736
    ctx.fpu_enabled = msr_fp;
6737
#if defined(TARGET_PPCEMB)
6738
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6739 6740 6741
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
6742
#endif
6743 6744 6745 6746
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
6747 6748 6749 6750 6751 6752 6753 6754
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
        single_step = 1;
    else
        single_step = 0;
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
        branch_step = 1;
    else
        branch_step = 0;
J
j_mayer 已提交
6755
    ctx.singlestep_enabled = env->singlestep_enabled || single_step == 1;
6756
#if defined (DO_SINGLE_STEP) && 0
6757 6758 6759 6760
    /* Single step trace mode */
    msr_se = 1;
#endif
    /* Set env in case of segfault during code fetch */
6761
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6762 6763
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6764
                if (env->breakpoints[j] == ctx.nip) {
6765
                    gen_update_nip(&ctx, ctx.nip);
6766 6767 6768 6769 6770
                    gen_op_debug();
                    break;
                }
            }
        }
6771
        if (unlikely(search_pc)) {
B
bellard 已提交
6772 6773 6774 6775 6776
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6777
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6778 6779 6780
                gen_opc_instr_start[lj] = 1;
            }
        }
6781 6782
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6783
            fprintf(logfile, "----------------\n");
6784
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6785
                    ctx.nip, supervisor, (int)msr_ir);
6786 6787
        }
#endif
B
bellard 已提交
6788
        ctx.opcode = ldl_code(ctx.nip);
6789 6790 6791 6792 6793 6794
        if (msr_le) {
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                ((ctx.opcode & 0x00FF0000) >> 8) |
                ((ctx.opcode & 0x0000FF00) << 8) |
                ((ctx.opcode & 0x000000FF) << 24);
        }
6795 6796
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6797
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6798
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6799
                    opc3(ctx.opcode), msr_le ? "little" : "big");
B
bellard 已提交
6800 6801
        }
#endif
B
bellard 已提交
6802
        ctx.nip += 4;
6803
        table = env->opcodes;
B
bellard 已提交
6804 6805 6806 6807 6808 6809 6810 6811 6812 6813
        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 ? */
6814
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6815
            if (loglevel != 0) {
6816
                fprintf(logfile, "invalid/unsupported opcode: "
6817
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6818
                        opc1(ctx.opcode), opc2(ctx.opcode),
6819
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6820 6821
            } else {
                printf("invalid/unsupported opcode: "
6822
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
B
bellard 已提交
6823
                       opc1(ctx.opcode), opc2(ctx.opcode),
6824
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6825
            }
6826 6827
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6828
                if (loglevel != 0) {
B
bellard 已提交
6829
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6830
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
B
bellard 已提交
6831 6832
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6833
                            ctx.opcode, ctx.nip - 4);
6834 6835
                } else {
                    printf("invalid bits: %08x for opcode: "
6836
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6837 6838
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6839
                           ctx.opcode, ctx.nip - 4);
6840
                }
6841
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6842
                break;
B
bellard 已提交
6843 6844
            }
        }
B
bellard 已提交
6845
        (*(handler->handler))(&ctx);
6846 6847 6848
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6849
        /* Check trace mode exceptions */
6850 6851 6852 6853 6854 6855
        if (unlikely(branch_step != 0 &&
                     ctx.exception == POWERPC_EXCP_BRANCH)) {
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
        } else if (unlikely(single_step != 0 &&
                            (ctx.nip <= 0x100 || ctx.nip > 0xF00 ||
                             (ctx.nip & 0xFC) != 0x04) &&
6856
                            ctx.exception != POWERPC_SYSCALL &&
6857
                            ctx.exception != POWERPC_EXCP_TRAP)) {
6858
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6859 6860 6861 6862 6863
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
                            (env->singlestep_enabled))) {
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
6864
            break;
6865
        }
6866 6867 6868 6869
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
6870
    if (ctx.exception == POWERPC_EXCP_NONE) {
6871
        gen_goto_tb(&ctx, 0, ctx.nip);
6872
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6873 6874 6875
        gen_op_reset_T0();
        /* Generate the return instruction */
        gen_op_exit_tb();
6876
    }
B
bellard 已提交
6877
    *gen_opc_ptr = INDEX_op_end;
6878
    if (unlikely(search_pc)) {
6879 6880 6881 6882 6883
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6884
        tb->size = ctx.nip - pc_start;
6885
    }
6886
#if defined(DEBUG_DISAS)
6887
    if (loglevel & CPU_LOG_TB_CPU) {
6888
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6889
        cpu_dump_state(env, logfile, fprintf, 0);
6890 6891
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6892
        int flags;
6893 6894
        flags = env->bfd_mach;
        flags |= msr_le << 16;
B
bellard 已提交
6895
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6896
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6897
        fprintf(logfile, "\n");
6898 6899
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
6900 6901 6902 6903 6904 6905 6906 6907
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

6908
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6909 6910 6911 6912
{
    return gen_intermediate_code_internal(env, tb, 0);
}

6913
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6914 6915 6916
{
    return gen_intermediate_code_internal(env, tb, 1);
}