translate.c 97.5 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
B
bellard 已提交
3
 * 
4
 *  Copyright (c) 2003-2005 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 30
#include "disas.h"

//#define DO_SINGLE_STEP
31
//#define PPC_DEBUG_DISAS
B
bellard 已提交
32

B
bellard 已提交
33 34 35 36 37 38
#ifdef USE_DIRECT_JUMP
#define TBPARAM(x)
#else
#define TBPARAM(x) (long)(x)
#endif

B
bellard 已提交
39 40 41 42 43 44 45 46 47 48 49
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"
50 51

#define GEN8(func, NAME) \
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static GenOpFunc *NAME ## _table [8] = {                                      \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
};                                                                            \
static inline void func(int n)                                                \
{                                                                             \
    NAME ## _table[n]();                                                      \
}

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

#define GEN32(func, NAME) \
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static GenOpFunc *NAME ## _table [32] = {                                     \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
};                                                                            \
static inline void func(int n)                                                \
{                                                                             \
    NAME ## _table[n]();                                                      \
}

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

B
bellard 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/* 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);
static GenOpFunc1 *gen_op_store_T0_fpscri_fpscr_table[8] = {
    &gen_op_store_T0_fpscri_fpscr0,
    &gen_op_store_T0_fpscri_fpscr1,
    &gen_op_store_T0_fpscri_fpscr2,
    &gen_op_store_T0_fpscri_fpscr3,
    &gen_op_store_T0_fpscri_fpscr4,
    &gen_op_store_T0_fpscri_fpscr5,
    &gen_op_store_T0_fpscri_fpscr6,
    &gen_op_store_T0_fpscri_fpscr7,
};
static inline void gen_op_store_T0_fpscri(int n, uint8_t param)
{
    (*gen_op_store_T0_fpscri_fpscr_table[n])(param);
}

114 115 116
/* Segment register moves */
GEN16(gen_op_load_sr, gen_op_load_sr);
GEN16(gen_op_store_sr, gen_op_store_sr);
117

118 119 120 121 122 123 124 125
/* 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);
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
126

B
bellard 已提交
127 128 129 130 131 132 133
/* 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);
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
B
bellard 已提交
134 135 136 137 138 139

static uint8_t  spr_access[1024 / 2];

/* internal defines */
typedef struct DisasContext {
    struct TranslationBlock *tb;
B
bellard 已提交
140
    target_ulong nip;
B
bellard 已提交
141
    uint32_t opcode;
142
    uint32_t exception;
B
bellard 已提交
143 144 145
    /* Routine used to access memory */
    int mem_idx;
    /* Translation flags */
146
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
147
    int supervisor;
148
#endif
B
bellard 已提交
149
    int fpu_enabled;
150
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
151
    int singlestep_enabled;
B
bellard 已提交
152 153
} DisasContext;

154
struct opc_handler_t {
B
bellard 已提交
155 156
    /* invalid bits */
    uint32_t inval;
157 158
    /* instruction type */
    uint32_t type;
B
bellard 已提交
159 160
    /* handler */
    void (*handler)(DisasContext *ctx);
161
};
B
bellard 已提交
162

163
#define RET_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
164
do {                                                                          \
165 166 167 168 169
    if ((ctx)->exception == EXCP_NONE) {                                      \
        gen_op_update_nip((ctx)->nip);                                        \
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
170 171
} while (0)

172 173 174 175 176
#define RET_INVAL(ctx)                                                        \
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_INVAL)

#define RET_PRIVOPC(ctx)                                                      \
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_OPC)
177

178 179
#define RET_PRIVREG(ctx)                                                      \
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_REG)
180

181
/* Stop translation */
182 183
static inline void RET_STOP (DisasContext *ctx)
{
184 185
    gen_op_update_nip((ctx)->nip);
    ctx->exception = EXCP_MTMSR;
186 187
}

188
/* No need to update nip here, as execution flow will change */
189 190 191 192 193
static inline void RET_CHG_FLOW (DisasContext *ctx)
{
    ctx->exception = EXCP_MTMSR;
}

B
bellard 已提交
194 195 196 197 198 199 200
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
static void gen_##name (DisasContext *ctx)

typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
201 202 203 204 205
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
206
    opc_handler_t handler;
207
    const unsigned char *oname;
B
bellard 已提交
208 209 210 211 212 213 214 215 216 217 218 219
} opcode_t;

/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
static inline uint32_t name (uint32_t opcode)                                 \
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
static inline int32_t name (uint32_t opcode)                                  \
{                                                                             \
220
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

/* 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 */
248 249 250 251 252 253 254
EXTRACT_HELPER(_SPR, 11, 10);
static inline uint32_t SPR (uint32_t opcode)
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268
/***                              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 已提交
269 270
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
271 272 273 274

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

B
bellard 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
static inline uint32_t LI (uint32_t opcode)
{
    return (opcode >> 0) & 0x03FFFFFC;
}

static inline uint32_t BD (uint32_t opcode)
{
    return (opcode >> 0) & 0xFFFC;
}

EXTRACT_HELPER(BO, 21, 5);
EXTRACT_HELPER(BI, 16, 5);
/* Absolute/relative address */
EXTRACT_HELPER(AA, 1, 1);
/* Link */
EXTRACT_HELPER(LK, 0, 1);

/* Create a mask between <start> and <end> bits */
static inline uint32_t MASK (uint32_t start, uint32_t end)
{
    uint32_t ret;

    ret = (((uint32_t)(-1)) >> (start)) ^ (((uint32_t)(-1) >> (end)) >> 1);
    if (start > end)
        return ~ret;

    return ret;
}

310 311 312 313 314
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
315
#if defined(__APPLE__)
B
bellard 已提交
316
#define OPCODES_SECTION \
317
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
318
#else
B
bellard 已提交
319
#define OPCODES_SECTION \
320
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
321 322
#endif

B
bellard 已提交
323
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
324
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
325 326 327
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
328
    .pad  = { 0, },                                                           \
B
bellard 已提交
329 330
    .handler = {                                                              \
        .inval   = invl,                                                      \
331
        .type = _typ,                                                         \
B
bellard 已提交
332 333
        .handler = &gen_##name,                                               \
    },                                                                        \
334
    .oname = stringify(name),                                                 \
B
bellard 已提交
335 336 337
}

#define GEN_OPCODE_MARK(name)                                                 \
338
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
339 340 341
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
342
    .pad  = { 0, },                                                           \
B
bellard 已提交
343 344
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
345
        .type = 0x00,                                                         \
B
bellard 已提交
346 347
        .handler = NULL,                                                      \
    },                                                                        \
348
    .oname = stringify(name),                                                 \
B
bellard 已提交
349 350 351 352 353 354
}

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

/* Invalid instruction */
355 356
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
357
    RET_INVAL(ctx);
358 359
}

B
bellard 已提交
360 361
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
362
    .type    = PPC_NONE,
B
bellard 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval)                       \
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
        gen_op_set_Rc0();                                                     \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
}

#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval)                     \
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
385
        gen_op_set_Rc0();                                                     \
B
bellard 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
}

#define __GEN_INT_ARITH1(name, opc1, opc2, opc3)                              \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
        gen_op_set_Rc0();                                                     \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
}
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3)                            \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
404
        gen_op_set_Rc0();                                                     \
B
bellard 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
}

/* Two operands arithmetic functions */
#define GEN_INT_ARITH2(name, opc1, opc2, opc3)                                \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000)                          \
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000)

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

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

/* add    add.    addo    addo.    */
GEN_INT_ARITH2 (add,    0x1F, 0x0A, 0x08);
/* addc   addc.   addco   addco.   */
GEN_INT_ARITH2 (addc,   0x1F, 0x0A, 0x00);
/* adde   adde.   addeo   addeo.   */
GEN_INT_ARITH2 (adde,   0x1F, 0x0A, 0x04);
/* addme  addme.  addmeo  addmeo.  */
GEN_INT_ARITH1 (addme,  0x1F, 0x0A, 0x07);
/* addze  addze.  addzeo  addzeo.  */
GEN_INT_ARITH1 (addze,  0x1F, 0x0A, 0x06);
/* divw   divw.   divwo   divwo.   */
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F);
/* divwu  divwu.  divwuo  divwuo.  */
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E);
/* mulhw  mulhw.                   */
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02);
/* mulhwu mulhwu.                  */
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00);
/* mullw  mullw.  mullwo  mullwo.  */
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07);
/* neg    neg.    nego    nego.    */
GEN_INT_ARITH1 (neg,    0x1F, 0x08, 0x03);
/* subf   subf.   subfo   subfo.   */
GEN_INT_ARITH2 (subf,   0x1F, 0x08, 0x01);
/* subfc  subfc.  subfco  subfco.  */
GEN_INT_ARITH2 (subfc,  0x1F, 0x08, 0x00);
/* subfe  subfe.  subfeo  subfeo.  */
GEN_INT_ARITH2 (subfe,  0x1F, 0x08, 0x04);
/* subfme subfme. subfmeo subfmeo. */
GEN_INT_ARITH1 (subfme, 0x1F, 0x08, 0x07);
/* subfze subfze. subfzeo subfzeo. */
GEN_INT_ARITH1 (subfze, 0x1F, 0x08, 0x06);
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    int32_t simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        gen_op_set_T0(simm);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_addi(simm);
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_addic(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic. */
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_addic(SIMM(ctx->opcode));
    gen_op_set_Rc0();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    int32_t simm = SIMM(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        gen_op_set_T0(simm << 16);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_addi(simm << 16);
    }
    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));
    gen_op_subfic(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/***                           Integer comparison                          ***/
#define GEN_CMP(name, opc)                                                    \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
{                                                                             \
    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));                                   \
}

/* cmp */
GEN_CMP(cmp, 0x00);
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_cmpi(SIMM(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
}
/* cmpl */
GEN_CMP(cmpl, 0x01);
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_cmpli(UIMM(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

/***                            Integer logical                            ***/
#define __GEN_LOGICAL2(name, opc2, opc3)                                      \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, PPC_INTEGER)                  \
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
        gen_op_set_Rc0();                                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}
#define GEN_LOGICAL2(name, opc)                                               \
__GEN_LOGICAL2(name, 0x1C, opc)

#define GEN_LOGICAL1(name, opc)                                               \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, PPC_INTEGER)                   \
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    if (Rc(ctx->opcode) != 0)                                                 \
        gen_op_set_Rc0();                                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

/* and & and. */
GEN_LOGICAL2(and, 0x00);
/* andc & andc. */
GEN_LOGICAL2(andc, 0x01);
/* andi. */
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_andi_(UIMM(ctx->opcode));
    gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* andis. */
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_andi_(UIMM(ctx->opcode) << 16);
    gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* cntlzw */
GEN_LOGICAL1(cntlzw, 0x00);
/* eqv & eqv. */
GEN_LOGICAL2(eqv, 0x08);
/* extsb & extsb. */
GEN_LOGICAL1(extsb, 0x1D);
/* extsh & extsh. */
GEN_LOGICAL1(extsh, 0x1C);
/* nand & nand. */
GEN_LOGICAL2(nand, 0x0E);
/* nor & nor. */
GEN_LOGICAL2(nor, 0x03);
596

B
bellard 已提交
597
/* or & or. */
598 599 600 601 602 603 604 605 606 607 608 609 610
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    /* Optimisation for mr case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_or();
    }
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

B
bellard 已提交
611 612 613
/* orc & orc. */
GEN_LOGICAL2(orc, 0x0C);
/* xor & xor. */
614 615 616 617 618 619 620 621 622 623 624 625 626 627
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 {
        gen_op_set_T0(0);
    }
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
B
bellard 已提交
628 629 630 631 632
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t uimm = UIMM(ctx->opcode);

633 634 635
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
B
bellard 已提交
636 637
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
638
    if (uimm != 0)
B
bellard 已提交
639 640 641 642 643 644 645 646
        gen_op_ori(uimm);
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t uimm = UIMM(ctx->opcode);

647 648 649
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
B
bellard 已提交
650 651
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
652
    if (uimm != 0)
B
bellard 已提交
653 654 655 656 657 658
        gen_op_ori(uimm << 16);
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
659 660 661 662 663 664
    uint32_t uimm = UIMM(ctx->opcode);

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
665
    gen_op_load_gpr_T0(rS(ctx->opcode));
666
    if (uimm != 0)
B
bellard 已提交
667
    gen_op_xori(uimm);
B
bellard 已提交
668 669 670 671 672 673
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
674 675 676 677 678 679
    uint32_t uimm = UIMM(ctx->opcode);

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
680
    gen_op_load_gpr_T0(rS(ctx->opcode));
681
    if (uimm != 0)
B
bellard 已提交
682
    gen_op_xori(uimm << 16);
B
bellard 已提交
683 684 685 686 687 688 689 690 691 692 693 694
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 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));
B
bellard 已提交
695
    gen_op_load_gpr_T1(rA(ctx->opcode));
B
bellard 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709
    gen_op_rlwimi(SH(ctx->opcode), MASK(mb, me), ~MASK(mb, me));
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
    
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
B
bellard 已提交
710 711 712 713 714 715
#if 1 // TRY
    if (sh == 0) {
        gen_op_andi_(MASK(mb, me));
        goto store;
    }
#endif
B
bellard 已提交
716 717 718 719
    if (mb == 0) {
        if (me == 31) {
            gen_op_rotlwi(sh);
            goto store;
B
bellard 已提交
720
#if 0
B
bellard 已提交
721 722 723
        } else if (me == (31 - sh)) {
            gen_op_slwi(sh);
            goto store;
B
bellard 已提交
724
#endif
B
bellard 已提交
725 726
        }
    } else if (me == 31) {
B
bellard 已提交
727
#if 0
B
bellard 已提交
728 729 730 731
        if (sh == (32 - mb)) {
            gen_op_srwi(mb);
            goto store;
        }
B
bellard 已提交
732
#endif
B
bellard 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    }
    gen_op_rlwinm(sh, MASK(mb, me));
store:
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* 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));
    if (mb == 0 && me == 31) {
        gen_op_rotl();
    } else
    {
        gen_op_rlwnm(MASK(mb, me));
    }
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/***                             Integer shift                             ***/
/* slw & slw. */
__GEN_LOGICAL2(slw, 0x18, 0x00);
/* sraw & sraw. */
__GEN_LOGICAL2(sraw, 0x18, 0x18);
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
769
    if (SH(ctx->opcode) != 0)
B
bellard 已提交
770 771 772 773 774 775 776 777 778
    gen_op_srawi(SH(ctx->opcode), MASK(32 - SH(ctx->opcode), 31));
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
/* srw & srw. */
__GEN_LOGICAL2(srw, 0x18, 0x10);

/***                       Floating-Point arithmetic                       ***/
779
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat)                           \
780 781
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
{                                                                             \
B
bellard 已提交
782 783 784 785
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
786 787 788 789
    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));                                     \
790 791 792 793
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
794 795 796 797 798 799
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
}

#define GEN_FLOAT_ACB(name, op2)                                              \
800 801
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0);                                     \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1);
802

803
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat)                     \
804 805
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
B
bellard 已提交
806 807 808 809
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
810 811 812
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
813 814 815 816
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
817 818 819 820 821
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AB(name, op2, inval)                                        \
822 823
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1);
824

825
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat)                     \
826 827
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
B
bellard 已提交
828 829 830 831
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
832 833 834
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
835 836 837 838
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
839 840 841 842 843
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AC(name, op2, inval)                                        \
844 845
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1);
846 847 848 849

#define GEN_FLOAT_B(name, op2, op3)                                           \
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
{                                                                             \
B
bellard 已提交
850 851 852 853
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
854 855 856 857 858 859
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
860 861
}

862 863
#define GEN_FLOAT_BS(name, op1, op2)                                          \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                   \
864
{                                                                             \
B
bellard 已提交
865 866 867 868
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
869 870 871 872 873 874
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
875 876
}

877 878
/* fadd - fadds */
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
879
/* fdiv - fdivs */
880
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
881
/* fmul - fmuls */
882
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
B
bellard 已提交
883 884

/* fres */
885
GEN_FLOAT_BS(res, 0x3B, 0x18);
B
bellard 已提交
886 887

/* frsqrte */
888
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A);
B
bellard 已提交
889 890

/* fsel */
891 892
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0);
/* fsub - fsubs */
893
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
B
bellard 已提交
894 895
/* Optional: */
/* fsqrt */
896 897 898 899 900 901 902 903 904 905 906 907 908
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
{
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_fsqrt();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
}
B
bellard 已提交
909

910
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
B
bellard 已提交
911
{
B
bellard 已提交
912 913 914 915
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
916 917
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
918 919
    gen_op_fsqrt();
    gen_op_frsp();
920 921 922
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
923 924 925
}

/***                     Floating-Point multiply-and-add                   ***/
926
/* fmadd - fmadds */
927
GEN_FLOAT_ACB(madd, 0x1D);
928
/* fmsub - fmsubs */
929
GEN_FLOAT_ACB(msub, 0x1C);
930
/* fnmadd - fnmadds */
931
GEN_FLOAT_ACB(nmadd, 0x1F);
932
/* fnmsub - fnmsubs */
933
GEN_FLOAT_ACB(nmsub, 0x1E);
B
bellard 已提交
934 935 936

/***                     Floating-Point round & convert                    ***/
/* fctiw */
937
GEN_FLOAT_B(ctiw, 0x0E, 0x00);
B
bellard 已提交
938
/* fctiwz */
939
GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
B
bellard 已提交
940
/* frsp */
941
GEN_FLOAT_B(rsp, 0x0C, 0x00);
B
bellard 已提交
942 943 944 945 946

/***                         Floating-Point compare                        ***/
/* fcmpo */
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
{
B
bellard 已提交
947 948 949 950
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
951 952 953 954 955
    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 已提交
956 957 958 959 960
}

/* fcmpu */
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
{
B
bellard 已提交
961 962 963 964
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
965 966 967 968 969
    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 已提交
970 971
}

972 973 974 975 976 977 978
/***                         Floating-point move                           ***/
/* fabs */
GEN_FLOAT_B(abs, 0x08, 0x08);

/* fmr  - fmr. */
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
B
bellard 已提交
979 980 981 982
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
983 984 985 986 987 988 989 990 991 992 993 994
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
}

/* fnabs */
GEN_FLOAT_B(nabs, 0x08, 0x04);
/* fneg */
GEN_FLOAT_B(neg, 0x08, 0x01);

B
bellard 已提交
995 996 997 998
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
B
bellard 已提交
999 1000 1001 1002
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1003 1004 1005
    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 已提交
1006 1007 1008 1009 1010
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1011 1012 1013 1014
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
1015
    gen_op_load_fpscr();
B
bellard 已提交
1016 1017 1018
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1019 1020 1021 1022 1023
}

/* mtfsb0 */
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1024 1025
    uint8_t crb;
    
B
bellard 已提交
1026 1027 1028 1029
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1030 1031 1032 1033 1034 1035
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
    gen_op_andi_(~(1 << (crbD(ctx->opcode) & 0x03)));
    gen_op_store_T0_fpscr(crb);
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1036 1037 1038 1039 1040
}

/* mtfsb1 */
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1041 1042
    uint8_t crb;
    
B
bellard 已提交
1043 1044 1045 1046
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1047 1048 1049 1050 1051 1052
    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);
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1053 1054 1055 1056 1057
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
B
bellard 已提交
1058 1059 1060 1061
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1062
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1063
    gen_op_store_fpscr(FM(ctx->opcode));
B
bellard 已提交
1064 1065
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1066 1067 1068 1069 1070
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
B
bellard 已提交
1071 1072 1073 1074
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1075 1076 1077
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1078 1079 1080
}

/***                             Integer load                              ***/
1081
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
1082
#if defined(CONFIG_USER_ONLY)
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
#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,                                               \
};
/* Byte access routine are endian safe */
#define gen_op_stb_le_raw gen_op_stb_raw
#define gen_op_lbz_le_raw gen_op_lbz_raw
1096 1097 1098 1099
#else
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
1100
    &gen_op_l##width##_le_user,                                               \
1101
    &gen_op_l##width##_kernel,                                                \
1102 1103
    &gen_op_l##width##_le_kernel,                                             \
};
1104 1105 1106
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
1107
    &gen_op_st##width##_le_user,                                              \
1108
    &gen_op_st##width##_kernel,                                               \
1109 1110 1111 1112 1113 1114 1115
    &gen_op_st##width##_le_kernel,                                            \
};
/* Byte access routine are endian safe */
#define gen_op_stb_le_user gen_op_stb_user
#define gen_op_lbz_le_user gen_op_lbz_user
#define gen_op_stb_le_kernel gen_op_stb_kernel
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
1116 1117 1118
#endif

#define GEN_LD(width, opc)                                                    \
B
bellard 已提交
1119 1120 1121 1122
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
{                                                                             \
    uint32_t simm = SIMM(ctx->opcode);                                        \
    if (rA(ctx->opcode) == 0) {                                               \
1123
        gen_op_set_T0(simm);                                                  \
B
bellard 已提交
1124 1125
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1126 1127
        if (simm != 0)                                                        \
            gen_op_addi(simm);                                                \
B
bellard 已提交
1128
    }                                                                         \
1129
    op_ldst(l##width);                                                        \
B
bellard 已提交
1130 1131 1132
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

1133
#define GEN_LDU(width, opc)                                                   \
B
bellard 已提交
1134 1135
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
{                                                                             \
1136
    uint32_t simm = SIMM(ctx->opcode);                                        \
B
bellard 已提交
1137
    if (rA(ctx->opcode) == 0 ||                                               \
1138
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1139 1140
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1141
    }                                                                         \
B
bellard 已提交
1142
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1143 1144 1145
    if (simm != 0)                                                            \
        gen_op_addi(simm);                                                    \
    op_ldst(l##width);                                                        \
B
bellard 已提交
1146 1147 1148 1149
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1150
#define GEN_LDUX(width, opc)                                                  \
B
bellard 已提交
1151 1152 1153
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
{                                                                             \
    if (rA(ctx->opcode) == 0 ||                                               \
1154
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1155 1156
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1157
    }                                                                         \
B
bellard 已提交
1158 1159
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1160 1161
    gen_op_add();                                                             \
    op_ldst(l##width);                                                        \
B
bellard 已提交
1162 1163 1164 1165
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1166
#define GEN_LDX(width, opc2, opc3)                                            \
B
bellard 已提交
1167 1168 1169 1170 1171 1172 1173
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
{                                                                             \
    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));                                  \
1174
        gen_op_add();                                                         \
B
bellard 已提交
1175
    }                                                                         \
1176
    op_ldst(l##width);                                                        \
B
bellard 已提交
1177 1178 1179
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

1180 1181 1182 1183 1184 1185
#define GEN_LDS(width, op)                                                    \
OP_LD_TABLE(width);                                                           \
GEN_LD(width, op | 0x20);                                                     \
GEN_LDU(width, op | 0x21);                                                    \
GEN_LDUX(width, op | 0x01);                                                   \
GEN_LDX(width, 0x17, op | 0x00)
B
bellard 已提交
1186 1187

/* lbz lbzu lbzux lbzx */
1188
GEN_LDS(bz, 0x02);
B
bellard 已提交
1189
/* lha lhau lhaux lhax */
1190
GEN_LDS(ha, 0x0A);
B
bellard 已提交
1191
/* lhz lhzu lhzux lhzx */
1192
GEN_LDS(hz, 0x08);
B
bellard 已提交
1193
/* lwz lwzu lwzux lwzx */
1194
GEN_LDS(wz, 0x00);
B
bellard 已提交
1195 1196

/***                              Integer store                            ***/
1197
#define GEN_ST(width, opc)                                                    \
B
bellard 已提交
1198 1199 1200 1201
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
{                                                                             \
    uint32_t simm = SIMM(ctx->opcode);                                        \
    if (rA(ctx->opcode) == 0) {                                               \
1202
        gen_op_set_T0(simm);                                                  \
B
bellard 已提交
1203 1204
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1205 1206
        if (simm != 0)                                                        \
            gen_op_addi(simm);                                                \
B
bellard 已提交
1207
    }                                                                         \
1208 1209
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1210 1211
}

1212
#define GEN_STU(width, opc)                                                   \
B
bellard 已提交
1213 1214
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
{                                                                             \
1215 1216
    uint32_t simm = SIMM(ctx->opcode);                                        \
    if (rA(ctx->opcode) == 0) {                                               \
1217 1218
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1219
    }                                                                         \
B
bellard 已提交
1220
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1221 1222
    if (simm != 0)                                                            \
        gen_op_addi(simm);                                                    \
B
bellard 已提交
1223
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1224
    op_ldst(st##width);                                                       \
B
bellard 已提交
1225 1226 1227
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1228
#define GEN_STUX(width, opc)                                                  \
B
bellard 已提交
1229 1230
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
{                                                                             \
1231
    if (rA(ctx->opcode) == 0) {                                               \
1232 1233
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1234
    }                                                                         \
B
bellard 已提交
1235 1236
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1237 1238 1239
    gen_op_add();                                                             \
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1240 1241 1242
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1243
#define GEN_STX(width, opc2, opc3)                                            \
B
bellard 已提交
1244 1245 1246 1247 1248 1249 1250
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
{                                                                             \
    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));                                  \
1251
        gen_op_add();                                                         \
B
bellard 已提交
1252
    }                                                                         \
1253 1254
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1255 1256
}

1257 1258 1259 1260 1261 1262
#define GEN_STS(width, op)                                                    \
OP_ST_TABLE(width);                                                           \
GEN_ST(width, op | 0x20);                                                     \
GEN_STU(width, op | 0x21);                                                    \
GEN_STUX(width, op | 0x01);                                                   \
GEN_STX(width, 0x17, op | 0x00)
B
bellard 已提交
1263 1264

/* stb stbu stbux stbx */
1265
GEN_STS(b, 0x06);
B
bellard 已提交
1266
/* sth sthu sthux sthx */
1267
GEN_STS(h, 0x0C);
B
bellard 已提交
1268
/* stw stwu stwux stwx */
1269
GEN_STS(w, 0x04);
B
bellard 已提交
1270 1271 1272

/***                Integer load and store with byte reverse               ***/
/* lhbrx */
1273 1274
OP_LD_TABLE(hbr);
GEN_LDX(hbr, 0x16, 0x18);
B
bellard 已提交
1275
/* lwbrx */
1276 1277
OP_LD_TABLE(wbr);
GEN_LDX(wbr, 0x16, 0x10);
B
bellard 已提交
1278
/* sthbrx */
1279 1280
OP_ST_TABLE(hbr);
GEN_STX(hbr, 0x16, 0x1C);
B
bellard 已提交
1281
/* stwbrx */
1282 1283
OP_ST_TABLE(wbr);
GEN_STX(wbr, 0x16, 0x14);
B
bellard 已提交
1284 1285

/***                    Integer load and store multiple                    ***/
1286
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1287
#if defined(CONFIG_USER_ONLY)
1288 1289 1290 1291 1292 1293 1294 1295
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_raw,
    &gen_op_lmw_le_raw,
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_raw,
    &gen_op_stmw_le_raw,
};
1296 1297 1298
#else
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
1299
    &gen_op_lmw_le_user,
1300
    &gen_op_lmw_kernel,
1301
    &gen_op_lmw_le_kernel,
1302 1303 1304
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
1305
    &gen_op_stmw_le_user,
1306
    &gen_op_stmw_kernel,
1307
    &gen_op_stmw_le_kernel,
1308 1309 1310
};
#endif

B
bellard 已提交
1311 1312 1313
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1314 1315
    int simm = SIMM(ctx->opcode);

B
bellard 已提交
1316
    if (rA(ctx->opcode) == 0) {
1317
        gen_op_set_T0(simm);
B
bellard 已提交
1318 1319
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
1320 1321
        if (simm != 0)
            gen_op_addi(simm);
B
bellard 已提交
1322
    }
1323
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
1324 1325 1326 1327 1328
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1329 1330
    int simm = SIMM(ctx->opcode);

B
bellard 已提交
1331
    if (rA(ctx->opcode) == 0) {
1332
        gen_op_set_T0(simm);
B
bellard 已提交
1333 1334
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
1335 1336
        if (simm != 0)
            gen_op_addi(simm);
B
bellard 已提交
1337
    }
1338
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
1339 1340 1341
}

/***                    Integer load and store strings                     ***/
1342 1343
#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)
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
#if defined(CONFIG_USER_ONLY)
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
};
#else
1358 1359
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
1360
    &gen_op_lswi_le_user,
1361
    &gen_op_lswi_kernel,
1362
    &gen_op_lswi_le_kernel,
1363 1364 1365
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
1366
    &gen_op_lswx_le_user,
1367
    &gen_op_lswx_kernel,
1368
    &gen_op_lswx_le_kernel,
1369 1370 1371
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
1372
    &gen_op_stsw_le_user,
1373
    &gen_op_stsw_kernel,
1374
    &gen_op_stsw_le_kernel,
1375 1376 1377
};
#endif

B
bellard 已提交
1378
/* lswi */
1379
/* PowerPC32 specification says we must generate an exception if
1380 1381 1382 1383
 * 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 已提交
1384 1385 1386 1387
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
1388
    int ra = rA(ctx->opcode);
B
bellard 已提交
1389 1390 1391 1392 1393
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
B
bellard 已提交
1394 1395
    if (((start + nr) > 32  && start <= ra && (start + nr - 32) > ra) ||
        ((start + nr) <= 32 && start <= ra && (start + nr) > ra)) {
1396 1397
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_LSWX);
        return;
B
bellard 已提交
1398
    }
1399
    if (ra == 0) {
B
bellard 已提交
1400 1401
        gen_op_set_T0(0);
    } else {
1402
        gen_op_load_gpr_T0(ra);
B
bellard 已提交
1403
    }
1404
    gen_op_set_T1(nb);
1405 1406
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_op_update_nip((ctx)->nip - 4); 
1407
    op_ldsts(lswi, start);
B
bellard 已提交
1408 1409 1410 1411 1412
}

/* lswx */
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
{
1413 1414 1415 1416 1417 1418
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

    if (ra == 0) {
        gen_op_load_gpr_T0(rb);
        ra = rb;
B
bellard 已提交
1419
    } else {
1420 1421 1422
        gen_op_load_gpr_T0(ra);
        gen_op_load_gpr_T1(rb);
        gen_op_add();
B
bellard 已提交
1423
    }
1424
    gen_op_load_xer_bc();
1425 1426
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_op_update_nip((ctx)->nip - 4); 
1427
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
1428 1429 1430 1431 1432
}

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

B
bellard 已提交
1435 1436 1437 1438 1439
    if (rA(ctx->opcode) == 0) {
        gen_op_set_T0(0);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
    }
B
bellard 已提交
1440 1441 1442
    if (nb == 0)
        nb = 32;
    gen_op_set_T1(nb);
1443 1444
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_op_update_nip((ctx)->nip - 4); 
1445
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
1446 1447 1448 1449 1450
}

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
1451 1452 1453 1454 1455
    int ra = rA(ctx->opcode);

    if (ra == 0) {
        gen_op_load_gpr_T0(rB(ctx->opcode));
        ra = rB(ctx->opcode);
B
bellard 已提交
1456
    } else {
1457 1458 1459
        gen_op_load_gpr_T0(ra);
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_add();
B
bellard 已提交
1460
    }
1461
    gen_op_load_xer_bc();
1462 1463
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_op_update_nip((ctx)->nip - 4); 
1464
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
}

/***                        Memory synchronisation                         ***/
/* eieio */
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM)
{
}

/* isync */
GEN_HANDLER(isync, 0x13, 0x16, 0xFF, 0x03FF0801, PPC_MEM)
{
}

1478 1479
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1480
#if defined(CONFIG_USER_ONLY)
1481 1482 1483 1484 1485 1486 1487 1488
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_raw,
    &gen_op_lwarx_le_raw,
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_raw,
    &gen_op_stwcx_le_raw,
};
1489
#else
B
bellard 已提交
1490 1491
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
1492
    &gen_op_lwarx_le_user,
B
bellard 已提交
1493
    &gen_op_lwarx_kernel,
1494
    &gen_op_lwarx_le_kernel,
B
bellard 已提交
1495
};
1496 1497
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
1498
    &gen_op_stwcx_le_user,
1499
    &gen_op_stwcx_kernel,
1500
    &gen_op_stwcx_le_kernel,
1501 1502 1503
};
#endif

1504
/* lwarx */
1505
GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
B
bellard 已提交
1506 1507 1508 1509 1510 1511
{
    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));
1512
        gen_op_add();
B
bellard 已提交
1513
    }
B
bellard 已提交
1514
    op_lwarx();
B
bellard 已提交
1515 1516 1517 1518
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stwcx. */
1519
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
1520 1521 1522 1523 1524 1525
{
        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));
1526
        gen_op_add();
B
bellard 已提交
1527
        }
1528 1529
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stwcx();
B
bellard 已提交
1530 1531 1532 1533 1534 1535 1536 1537
}

/* sync */
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_MEM)
{
}

/***                         Floating-point load                           ***/
1538
#define GEN_LDF(width, opc)                                                   \
1539
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)                 \
B
bellard 已提交
1540 1541
{                                                                             \
    uint32_t simm = SIMM(ctx->opcode);                                        \
1542 1543 1544 1545
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1546
    if (rA(ctx->opcode) == 0) {                                               \
1547
        gen_op_set_T0(simm);                                                  \
B
bellard 已提交
1548 1549
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1550 1551
        if (simm != 0)                                                        \
            gen_op_addi(simm);                                                \
B
bellard 已提交
1552
    }                                                                         \
1553 1554
    op_ldst(l##width);                                                        \
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
1555 1556
}

1557
#define GEN_LDUF(width, opc)                                                  \
1558
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)              \
B
bellard 已提交
1559
{                                                                             \
1560
    uint32_t simm = SIMM(ctx->opcode);                                        \
1561 1562 1563 1564
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1565
    if (rA(ctx->opcode) == 0 ||                                               \
1566
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1567 1568
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1569
    }                                                                         \
B
bellard 已提交
1570
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1571 1572 1573 1574
    if (simm != 0)                                                            \
        gen_op_addi(simm);                                                    \
    op_ldst(l##width);                                                        \
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
1575 1576 1577
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1578
#define GEN_LDUXF(width, opc)                                                 \
1579
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_FLOAT)             \
B
bellard 已提交
1580
{                                                                             \
1581 1582 1583 1584
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1585
    if (rA(ctx->opcode) == 0 ||                                               \
1586
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1587 1588
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1589
    }                                                                         \
B
bellard 已提交
1590 1591
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1592 1593 1594
    gen_op_add();                                                             \
    op_ldst(l##width);                                                        \
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
1595 1596 1597
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1598
#define GEN_LDXF(width, opc2, opc3)                                           \
1599
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_FLOAT)             \
B
bellard 已提交
1600
{                                                                             \
1601 1602 1603 1604
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1605 1606 1607 1608 1609
    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));                                  \
1610
        gen_op_add();                                                         \
B
bellard 已提交
1611
    }                                                                         \
1612 1613
    op_ldst(l##width);                                                        \
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
1614 1615
}

1616 1617 1618 1619 1620 1621
#define GEN_LDFS(width, op)                                                   \
OP_LD_TABLE(width);                                                           \
GEN_LDF(width, op | 0x20);                                                    \
GEN_LDUF(width, op | 0x21);                                                   \
GEN_LDUXF(width, op | 0x01);                                                  \
GEN_LDXF(width, 0x17, op | 0x00)
B
bellard 已提交
1622 1623

/* lfd lfdu lfdux lfdx */
1624
GEN_LDFS(fd, 0x12);
B
bellard 已提交
1625
/* lfs lfsu lfsux lfsx */
1626
GEN_LDFS(fs, 0x10);
B
bellard 已提交
1627 1628 1629

/***                         Floating-point store                          ***/
#define GEN_STF(width, opc)                                                   \
1630
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)                \
B
bellard 已提交
1631 1632
{                                                                             \
    uint32_t simm = SIMM(ctx->opcode);                                        \
1633 1634 1635 1636
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1637
    if (rA(ctx->opcode) == 0) {                                               \
1638
        gen_op_set_T0(simm);                                                  \
B
bellard 已提交
1639 1640
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1641 1642
        if (simm != 0)                                                        \
            gen_op_addi(simm);                                                \
B
bellard 已提交
1643
    }                                                                         \
1644 1645
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1646 1647
}

1648
#define GEN_STUF(width, opc)                                                  \
1649
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)             \
B
bellard 已提交
1650
{                                                                             \
1651
    uint32_t simm = SIMM(ctx->opcode);                                        \
1652 1653 1654 1655
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
1656
    if (rA(ctx->opcode) == 0) {                                               \
1657 1658
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1659
    }                                                                         \
B
bellard 已提交
1660
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1661 1662 1663 1664
    if (simm != 0)                                                            \
        gen_op_addi(simm);                                                    \
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1665 1666 1667
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1668
#define GEN_STUXF(width, opc)                                                 \
1669
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_FLOAT)            \
B
bellard 已提交
1670
{                                                                             \
1671 1672 1673 1674
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
1675
    if (rA(ctx->opcode) == 0) {                                               \
1676 1677
        RET_INVAL(ctx);                                                       \
        return;                                                               \
1678
    }                                                                         \
B
bellard 已提交
1679 1680
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1681 1682 1683
    gen_op_add();                                                             \
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1684 1685 1686
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

1687
#define GEN_STXF(width, opc2, opc3)                                           \
1688
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_FLOAT)            \
B
bellard 已提交
1689
{                                                                             \
1690 1691 1692 1693
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
B
bellard 已提交
1694 1695 1696 1697 1698
    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));                                  \
1699
        gen_op_add();                                                         \
B
bellard 已提交
1700
    }                                                                         \
1701 1702
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1703 1704
}

1705 1706 1707 1708 1709 1710
#define GEN_STFS(width, op)                                                   \
OP_ST_TABLE(width);                                                           \
GEN_STF(width, op | 0x20);                                                    \
GEN_STUF(width, op | 0x21);                                                   \
GEN_STUXF(width, op | 0x01);                                                  \
GEN_STXF(width, 0x17, op | 0x00)
B
bellard 已提交
1711 1712

/* stfd stfdu stfdux stfdx */
1713
GEN_STFS(fd, 0x16);
B
bellard 已提交
1714
/* stfs stfsu stfsux stfsx */
1715
GEN_STFS(fs, 0x14);
B
bellard 已提交
1716 1717 1718 1719 1720

/* Optional: */
/* stfiwx */
GEN_HANDLER(stfiwx, 0x1F, 0x17, 0x1E, 0x00000001, PPC_FLOAT)
{
B
bellard 已提交
1721 1722 1723 1724
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
1725
    RET_INVAL(ctx);
B
bellard 已提交
1726 1727 1728 1729
}

/***                                Branch                                 ***/

1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
{
    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));
        gen_op_set_T1(dest);
        gen_op_b_T1();
        gen_op_set_T0((long)tb + n);
1742 1743
        if (ctx->singlestep_enabled)
            gen_op_debug();
1744 1745 1746 1747
        gen_op_exit_tb();
    } else {
        gen_op_set_T1(dest);
        gen_op_b_T1();
1748 1749
        if (ctx->singlestep_enabled)
            gen_op_debug();
1750 1751 1752
        gen_op_set_T0(0);
        gen_op_exit_tb();
    }
B
bellard 已提交
1753 1754
}

B
bellard 已提交
1755 1756 1757
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
B
bellard 已提交
1758 1759 1760 1761
    uint32_t li, target;

    /* sign extend LI */
    li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
B
bellard 已提交
1762 1763

    if (AA(ctx->opcode) == 0)
B
bellard 已提交
1764
        target = ctx->nip + li - 4;
B
bellard 已提交
1765
    else
1766 1767
        target = li;
    if (LK(ctx->opcode)) {
B
bellard 已提交
1768
        gen_op_setlr(ctx->nip);
1769
    }
1770
    gen_goto_tb(ctx, 0, target);
1771
    ctx->exception = EXCP_BRANCH;
B
bellard 已提交
1772 1773
}

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

static inline void gen_bcond(DisasContext *ctx, int type) 
{                                                                             
    uint32_t target = 0;
    uint32_t bo = BO(ctx->opcode);                                            
    uint32_t bi = BI(ctx->opcode);                                            
    uint32_t mask;                                                            
    uint32_t li;

    if ((bo & 0x4) == 0)
        gen_op_dec_ctr();                                                     
    switch(type) {
    case BCOND_IM:
1790
        li = (int32_t)((int16_t)(BD(ctx->opcode)));
1791
        if (AA(ctx->opcode) == 0) {
B
bellard 已提交
1792
            target = ctx->nip + li - 4;
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
        } else {
            target = li;
        }
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
    if (LK(ctx->opcode)) {                                        
B
bellard 已提交
1806
        gen_op_setlr(ctx->nip);
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    }
    if (bo & 0x10) {
        /* No CR condition */                                                 
        switch (bo & 0x6) {                                                   
        case 0:                                                               
            gen_op_test_ctr();
            break;
        case 2:                                                               
            gen_op_test_ctrz();
            break;                                                            
        default:
        case 4:                                                               
        case 6:                                                               
            if (type == BCOND_IM) {
1821
                gen_goto_tb(ctx, 0, target);
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
            } else {
                gen_op_b_T1();
            }
            goto no_test;
        }
    } else {                                                                  
        mask = 1 << (3 - (bi & 0x03));                                        
        gen_op_load_crf_T0(bi >> 2);                                          
        if (bo & 0x8) {                                                       
            switch (bo & 0x6) {                                               
            case 0:                                                           
                gen_op_test_ctr_true(mask);
                break;                                                        
            case 2:                                                           
                gen_op_test_ctrz_true(mask);
                break;                                                        
            default:                                                          
            case 4:                                                           
            case 6:                                                           
                gen_op_test_true(mask);
                break;                                                        
            }                                                                 
        } else {                                                              
            switch (bo & 0x6) {                                               
            case 0:                                                           
                gen_op_test_ctr_false(mask);
                break;                                                        
            case 2:                                                           
                gen_op_test_ctrz_false(mask);
                break;                                                        
            default:
            case 4:                                                           
            case 6:                                                           
                gen_op_test_false(mask);
                break;                                                        
            }                                                                 
        }                                                                     
    }                                                                         
    if (type == BCOND_IM) {
B
bellard 已提交
1861 1862
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
1863
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
1864
        gen_set_label(l1);
1865
        gen_goto_tb(ctx, 1, ctx->nip);
1866
    } else {
B
bellard 已提交
1867
        gen_op_btest_T1(ctx->nip);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
    }
 no_test:
    ctx->exception = EXCP_BRANCH;                                             
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{                                                                             
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
{                                                                             
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
{                                                                             
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929

/***                      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 */
GEN_CRLOGIC(and, 0x08)
/* crandc */
GEN_CRLOGIC(andc, 0x04)
/* creqv */
GEN_CRLOGIC(eqv, 0x09)
/* crnand */
GEN_CRLOGIC(nand, 0x07)
/* crnor */
GEN_CRLOGIC(nor, 0x01)
/* cror */
GEN_CRLOGIC(or, 0x0E)
/* crorc */
GEN_CRLOGIC(orc, 0x0D)
/* crxor */
GEN_CRLOGIC(xor, 0x06)
/* 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) */
GEN_HANDLER(rfi, 0x13, 0x12, 0xFF, 0x03FF8001, PPC_FLOW)
{
1930
#if defined(CONFIG_USER_ONLY)
1931
    RET_PRIVOPC(ctx);
1932 1933 1934
#else
    /* Restore CPU state */
    if (!ctx->supervisor) {
1935 1936
        RET_PRIVOPC(ctx);
        return;
1937 1938
    }
    gen_op_rfi();
1939
    RET_CHG_FLOW(ctx);
1940
#endif
B
bellard 已提交
1941 1942 1943 1944 1945
}

/* sc */
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
{
1946
#if defined(CONFIG_USER_ONLY)
1947
    RET_EXCP(ctx, EXCP_SYSCALL_USER, 0);
1948
#else
1949
    RET_EXCP(ctx, EXCP_SYSCALL, 0);
1950
#endif
B
bellard 已提交
1951 1952 1953 1954 1955 1956
}

/***                                Trap                                   ***/
/* tw */
GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
{
1957 1958 1959
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
1960 1961 1962 1963 1964
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
1965 1966 1967 1968 1969 1970
    gen_op_load_gpr_T0(rA(ctx->opcode));
#if 0
    printf("%s: param=0x%04x T0=0x%04x\n", __func__,
           SIMM(ctx->opcode), TO(ctx->opcode));
#endif
    gen_op_twi(SIMM(ctx->opcode), TO(ctx->opcode));
B
bellard 已提交
1971 1972 1973 1974 1975 1976 1977
}

/***                          Processor control                            ***/
static inline int check_spr_access (int spr, int rw, int supervisor)
{
    uint32_t rights = spr_access[spr >> 1] >> (4 * (spr & 1));

1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
#if 0
    if (spr != LR && spr != CTR) {
    if (loglevel > 0) {
        fprintf(logfile, "%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
                SPR_ENCODE(spr), supervisor, rw, rights,
                (rights >> ((2 * supervisor) + rw)) & 1);
    } else {
        printf("%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
               SPR_ENCODE(spr), supervisor, rw, rights,
               (rights >> ((2 * supervisor) + rw)) & 1);
    }
    }
#endif
    if (rights == 0)
        return -1;
B
bellard 已提交
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
    rights = rights >> (2 * supervisor);
    rights = rights >> rw;

    return rights & 1;
}

/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
    gen_op_load_xer_cr();
    gen_op_store_T0_crf(crfD(ctx->opcode));
    gen_op_clear_xer_cr();
}

/* mfcr */
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x001FF801, PPC_MISC)
{
    gen_op_load_cr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
2017
#if defined(CONFIG_USER_ONLY)
2018
    RET_PRIVREG(ctx);
2019 2020
#else
    if (!ctx->supervisor) {
2021 2022
        RET_PRIVREG(ctx);
        return;
2023
    }
B
bellard 已提交
2024 2025
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
2026
#endif
B
bellard 已提交
2027 2028
}

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
#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 已提交
2040
/* mfspr */
2041
static inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
2042
{
2043
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
2044 2045
    uint32_t sprn = SPR(ctx->opcode);

2046 2047 2048 2049
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
2050
#endif
2051 2052 2053 2054 2055 2056 2057
        read_cb = ctx->spr_cb[sprn].uea_read;
    if (read_cb != NULL) {
        if (read_cb != SPR_NOACCESS) {
            (*read_cb)(ctx, sprn);
            gen_op_store_T0_gpr(rD(ctx->opcode));
        } else {
            /* Privilege exception */
2058 2059 2060 2061
            if (loglevel) {
                fprintf(logfile, "Trying to read priviledged spr %d %03x\n",
                        sprn, sprn);
            }
2062
            printf("Trying to read priviledged spr %d %03x\n", sprn, sprn);
2063
        RET_PRIVREG(ctx);
B
bellard 已提交
2064
        }
2065 2066
    } else {
        /* Not defined */
2067 2068 2069 2070
        if (loglevel) {
            fprintf(logfile, "Trying to read invalid spr %d %03x\n",
                    sprn, sprn);
        }
2071 2072
        printf("Trying to read invalid spr %d %03x\n", sprn, sprn);
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
B
bellard 已提交
2073 2074 2075
    }
}

2076
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
2077
{
2078
    gen_op_mfspr(ctx);
B
bellard 已提交
2079
    }
2080 2081 2082 2083 2084

/* mftb */
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_TB)
{
    gen_op_mfspr(ctx);
B
bellard 已提交
2085 2086 2087
}

/* mtcrf */
2088 2089
/* The mask should be 0x00100801, but Mac OS X 10.4 use an alternate form */
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
2090 2091 2092 2093 2094 2095 2096 2097
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_cr(CRM(ctx->opcode));
}

/* mtmsr */
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
2098
#if defined(CONFIG_USER_ONLY)
2099
    RET_PRIVREG(ctx);
2100 2101
#else
    if (!ctx->supervisor) {
2102 2103
        RET_PRIVREG(ctx);
        return;
2104
    }
B
bellard 已提交
2105
    gen_op_update_nip((ctx)->nip);
B
bellard 已提交
2106 2107 2108
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_msr();
    /* Must stop the translation as machine state (may have) changed */
B
bellard 已提交
2109
    RET_CHG_FLOW(ctx);
2110
#endif
B
bellard 已提交
2111 2112 2113 2114 2115
}

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

2119 2120 2121 2122
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
2123
#endif
2124 2125 2126 2127 2128 2129 2130
        write_cb = ctx->spr_cb[sprn].uea_write;
    if (write_cb != NULL) {
        if (write_cb != SPR_NOACCESS) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
2131 2132 2133 2134
            if (loglevel) {
                fprintf(logfile, "Trying to write priviledged spr %d %03x\n",
                        sprn, sprn);
            }
2135
            printf("Trying to write priviledged spr %d %03x\n", sprn, sprn);
2136
        RET_PRIVREG(ctx);
2137
    }
2138 2139
    } else {
        /* Not defined */
2140 2141 2142 2143
        if (loglevel) {
            fprintf(logfile, "Trying to write invalid spr %d %03x\n",
                    sprn, sprn);
        }
2144 2145
        printf("Trying to write invalid spr %d %03x\n", sprn, sprn);
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
B
bellard 已提交
2146 2147 2148 2149 2150 2151
    }
}

/***                         Cache management                              ***/
/* For now, all those will be implemented as nop:
 * this is valid, regarding the PowerPC specs...
2152
 * We just have to flush tb while invalidating instruction cache lines...
B
bellard 已提交
2153 2154
 */
/* dcbf */
2155
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2156
{
2157 2158 2159 2160 2161 2162 2163 2164
    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();
    }
    op_ldst(lbz);
B
bellard 已提交
2165 2166 2167
}

/* dcbi (Supervisor only) */
2168
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2169
{
2170
#if defined(CONFIG_USER_ONLY)
2171
    RET_PRIVOPC(ctx);
2172 2173
#else
    if (!ctx->supervisor) {
2174 2175
        RET_PRIVOPC(ctx);
        return;
2176
    }
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
    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();
    }
    op_ldst(lbz);
    op_ldst(stb);
#endif
B
bellard 已提交
2187 2188 2189
}

/* dcdst */
2190
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2191
{
2192 2193 2194 2195 2196 2197 2198 2199
    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();
    }
    op_ldst(lbz);
B
bellard 已提交
2200 2201 2202
}

/* dcbt */
2203
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2204 2205 2206 2207
{
}

/* dcbtst */
2208
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2209 2210 2211 2212
{
}

/* dcbz */
2213 2214 2215 2216 2217 2218
#if defined(CONFIG_USER_ONLY)
#define op_dcbz() gen_op_dcbz_raw()
#else
#define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
static GenOpFunc *gen_op_dcbz[] = {
    &gen_op_dcbz_user,
B
bellard 已提交
2219 2220
    &gen_op_dcbz_user,
    &gen_op_dcbz_kernel,
2221 2222 2223 2224 2225
    &gen_op_dcbz_kernel,
};
#endif

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2226
{
B
bellard 已提交
2227 2228 2229 2230 2231
    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));
2232
        gen_op_add();
B
bellard 已提交
2233
    }
2234
    op_dcbz();
B
bellard 已提交
2235
    gen_op_check_reservation();
B
bellard 已提交
2236 2237 2238
}

/* icbi */
2239
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2240
{
B
bellard 已提交
2241 2242 2243 2244 2245
    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));
2246
        gen_op_add();
B
bellard 已提交
2247
    }
2248
    gen_op_icbi();
B
bellard 已提交
2249 2250 2251 2252
}

/* Optional: */
/* dcba */
2253
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_OPT)
B
bellard 已提交
2254 2255 2256 2257 2258 2259 2260 2261
{
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
2262
#if defined(CONFIG_USER_ONLY)
2263
    RET_PRIVREG(ctx);
2264 2265
#else
    if (!ctx->supervisor) {
2266 2267
        RET_PRIVREG(ctx);
        return;
2268 2269 2270 2271
    }
    gen_op_load_sr(SR(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
2272 2273 2274
}

/* mfsrin */
2275
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
2276
{
2277
#if defined(CONFIG_USER_ONLY)
2278
    RET_PRIVREG(ctx);
2279 2280
#else
    if (!ctx->supervisor) {
2281 2282
        RET_PRIVREG(ctx);
        return;
2283 2284 2285 2286 2287
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_load_srin();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
2288 2289 2290
}

/* mtsr */
B
bellard 已提交
2291
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
2292
{
2293
#if defined(CONFIG_USER_ONLY)
2294
    RET_PRIVREG(ctx);
2295 2296
#else
    if (!ctx->supervisor) {
2297 2298
        RET_PRIVREG(ctx);
        return;
2299 2300 2301
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_sr(SR(ctx->opcode));
2302
    RET_STOP(ctx);
2303
#endif
B
bellard 已提交
2304 2305 2306
}

/* mtsrin */
2307
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
2308
{
2309
#if defined(CONFIG_USER_ONLY)
2310
    RET_PRIVREG(ctx);
2311 2312
#else
    if (!ctx->supervisor) {
2313 2314
        RET_PRIVREG(ctx);
        return;
2315 2316 2317 2318
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_store_srin();
2319
    RET_STOP(ctx);
2320
#endif
B
bellard 已提交
2321 2322 2323 2324 2325
}

/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
2326
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
2327
{
2328
#if defined(CONFIG_USER_ONLY)
2329
    RET_PRIVOPC(ctx);
2330 2331
#else
    if (!ctx->supervisor) {
2332 2333 2334 2335
        if (loglevel)
            fprintf(logfile, "%s: ! supervisor\n", __func__);
        RET_PRIVOPC(ctx);
        return;
2336 2337
    }
    gen_op_tlbia();
2338
    RET_STOP(ctx);
2339
#endif
B
bellard 已提交
2340 2341 2342
}

/* tlbie */
2343
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
B
bellard 已提交
2344
{
2345
#if defined(CONFIG_USER_ONLY)
2346
    RET_PRIVOPC(ctx);
2347 2348
#else
    if (!ctx->supervisor) {
2349 2350
        RET_PRIVOPC(ctx);
        return;
2351 2352 2353
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_tlbie();
2354
    RET_STOP(ctx);
2355
#endif
B
bellard 已提交
2356 2357 2358
}

/* tlbsync */
B
bellard 已提交
2359
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2360
{
2361
#if defined(CONFIG_USER_ONLY)
2362
    RET_PRIVOPC(ctx);
2363 2364
#else
    if (!ctx->supervisor) {
2365 2366
        RET_PRIVOPC(ctx);
        return;
2367 2368 2369 2370
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
2371
    RET_STOP(ctx);
2372
#endif
B
bellard 已提交
2373 2374 2375 2376
}

/***                              External control                         ***/
/* Optional: */
2377 2378
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_raw,
    &gen_op_eciwx_le_raw,
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_raw,
    &gen_op_ecowx_le_raw,
};
#else
2389 2390
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
2391
    &gen_op_eciwx_le_user,
2392
    &gen_op_eciwx_kernel,
2393
    &gen_op_eciwx_le_kernel,
2394 2395 2396
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
2397
    &gen_op_ecowx_le_user,
2398
    &gen_op_ecowx_kernel,
2399
    &gen_op_ecowx_le_kernel,
2400 2401 2402
};
#endif

2403
/* eciwx */
B
bellard 已提交
2404 2405
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
    /* Should check EAR[E] & alignment ! */
    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();
    }
    op_eciwx();
    gen_op_store_T0_gpr(rD(ctx->opcode));
B
bellard 已提交
2416 2417 2418 2419 2420
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
    /* Should check EAR[E] & alignment ! */
    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();
    }
    gen_op_load_gpr_T2(rS(ctx->opcode));
    op_ecowx();
B
bellard 已提交
2431 2432 2433 2434 2435
}

/* End opcode list */
GEN_OPCODE_MARK(end);

2436
#include "translate_init.c"
B
bellard 已提交
2437

2438
/*****************************************************************************/
2439
/* Misc PowerPC helpers */
B
bellard 已提交
2440 2441 2442
void cpu_dump_state(CPUState *env, FILE *f, 
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
B
bellard 已提交
2443
{
2444 2445
#if defined(TARGET_PPC64) || 1
#define FILL ""
B
bellard 已提交
2446
#define REGX "%016" PRIx64
2447 2448 2449 2450
#define RGPL  4
#define RFPL  4
#else
#define FILL "        "
B
bellard 已提交
2451
#define REGX "%08" PRIx64
2452 2453 2454 2455
#define RGPL  8
#define RFPL  4
#endif

B
bellard 已提交
2456 2457
    int i;

2458 2459 2460 2461 2462
    cpu_fprintf(f, "NIP " REGX " LR " REGX " CTR " REGX "\n",
                env->nip, env->lr, env->ctr);
    cpu_fprintf(f, "MSR " REGX FILL " XER %08x      TB %08x %08x DECR %08x\n",
                do_load_msr(env), do_load_xer(env), cpu_ppc_load_tbu(env),
                cpu_ppc_load_tbl(env), cpu_ppc_load_decr(env));
B
bellard 已提交
2463
        for (i = 0; i < 32; i++) {
2464 2465 2466 2467
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
        cpu_fprintf(f, " " REGX, env->gpr[i]);
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
2468
            cpu_fprintf(f, "\n");
B
bellard 已提交
2469
        }
2470
    cpu_fprintf(f, "CR ");
B
bellard 已提交
2471
        for (i = 0; i < 8; i++)
B
bellard 已提交
2472 2473
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
B
bellard 已提交
2474 2475 2476 2477 2478 2479 2480 2481
        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 已提交
2482
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
B
bellard 已提交
2483
        }
2484 2485 2486 2487
    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 已提交
2488
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
2489
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
2490
            cpu_fprintf(f, "\n");
B
bellard 已提交
2491
    }
2492 2493 2494
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX "         " FILL FILL FILL
                "SDR1 " REGX "\n",
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
B
bellard 已提交
2495

2496 2497 2498 2499
#undef REGX
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
2500 2501
}

2502
/*****************************************************************************/
B
bellard 已提交
2503 2504 2505
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
                                    int search_pc)
{
2506
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
2507
    opc_handler_t **table, *handler;
B
bellard 已提交
2508
    target_ulong pc_start;
B
bellard 已提交
2509 2510 2511 2512 2513 2514 2515
    uint16_t *gen_opc_end;
    int j, lj = -1;

    pc_start = tb->pc;
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
B
bellard 已提交
2516
    nb_gen_labels = 0;
B
bellard 已提交
2517
    ctx.nip = pc_start;
B
bellard 已提交
2518
    ctx.tb = tb;
2519
    ctx.exception = EXCP_NONE;
2520
    ctx.spr_cb = env->spr_cb;
2521
#if defined(CONFIG_USER_ONLY)
2522
    ctx.mem_idx = msr_le;
2523 2524
#else
    ctx.supervisor = 1 - msr_pr;
2525
    ctx.mem_idx = ((1 - msr_pr) << 1) | msr_le;
2526
#endif
B
bellard 已提交
2527
    ctx.fpu_enabled = msr_fp;
2528
    ctx.singlestep_enabled = env->singlestep_enabled;
2529
#if defined (DO_SINGLE_STEP) && 0
2530 2531 2532 2533 2534
    /* Single step trace mode */
    msr_se = 1;
#endif
    /* Set env in case of segfault during code fetch */
    while (ctx.exception == EXCP_NONE && gen_opc_ptr < gen_opc_end) {
2535 2536 2537 2538 2539 2540 2541 2542 2543
        if (env->nb_breakpoints > 0) {
            for(j = 0; j < env->nb_breakpoints; j++) {
                if (env->breakpoints[j] == ctx.nip) {
                    gen_op_update_nip(ctx.nip); 
                    gen_op_debug();
                    break;
                }
            }
        }
B
bellard 已提交
2544 2545 2546 2547 2548 2549
        if (search_pc) {
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
2550
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
2551 2552 2553
                gen_opc_instr_start[lj] = 1;
            }
        }
2554 2555
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
2556
            fprintf(logfile, "----------------\n");
B
bellard 已提交
2557
            fprintf(logfile, "nip=%08x super=%d ir=%d\n",
2558 2559 2560
                    ctx.nip, 1 - msr_pr, msr_ir);
        }
#endif
B
bellard 已提交
2561
        ctx.opcode = ldl_code(ctx.nip);
2562 2563 2564 2565 2566 2567
        if (msr_le) {
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                ((ctx.opcode & 0x00FF0000) >> 8) |
                ((ctx.opcode & 0x0000FF00) << 8) |
                ((ctx.opcode & 0x000000FF) << 24);
        }
2568 2569
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
2570
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
2571
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
2572
                    opc3(ctx.opcode), msr_le ? "little" : "big");
B
bellard 已提交
2573 2574
        }
#endif
B
bellard 已提交
2575
        ctx.nip += 4;
2576
        table = env->opcodes;
B
bellard 已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
        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 ? */
                if (handler->handler == &gen_invalid) {
B
bellard 已提交
2588
            if (loglevel > 0) {
B
bellard 已提交
2589
                    fprintf(logfile, "invalid/unsupported opcode: "
B
bellard 已提交
2590
                        "%02x - %02x - %02x (%08x) 0x%08x %d\n",
2591
                            opc1(ctx.opcode), opc2(ctx.opcode),
B
bellard 已提交
2592 2593 2594 2595 2596 2597 2598
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            } else {
                printf("invalid/unsupported opcode: "
                       "%02x - %02x - %02x (%08x) 0x%08x %d\n",
                       opc1(ctx.opcode), opc2(ctx.opcode),
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            }
B
bellard 已提交
2599
                } else {
B
bellard 已提交
2600 2601
            if ((ctx.opcode & handler->inval) != 0) {
                if (loglevel > 0) {
B
bellard 已提交
2602
                    fprintf(logfile, "invalid bits: %08x for opcode: "
B
bellard 已提交
2603
                            "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
B
bellard 已提交
2604 2605
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
2606
                            ctx.opcode, ctx.nip - 4);
2607 2608
                } else {
                    printf("invalid bits: %08x for opcode: "
B
bellard 已提交
2609
                           "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
2610 2611
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
2612
                           ctx.opcode, ctx.nip - 4);
2613
            }
B
bellard 已提交
2614 2615
                RET_INVAL(ctxp);
                break;
B
bellard 已提交
2616 2617
            }
        }
B
bellard 已提交
2618
        (*(handler->handler))(&ctx);
2619 2620 2621 2622 2623 2624 2625
        /* Check trace mode exceptions */
        if ((msr_be && ctx.exception == EXCP_BRANCH) ||
            /* Check in single step trace mode
             * we need to stop except if:
             * - rfi, trap or syscall
             * - first instruction of an exception handler
             */
B
bellard 已提交
2626 2627 2628
            (msr_se && (ctx.nip < 0x100 ||
                        ctx.nip > 0xF00 ||
                        (ctx.nip & 0xFC) != 0x04) &&
2629 2630
             ctx.exception != EXCP_SYSCALL &&
             ctx.exception != EXCP_SYSCALL_USER &&
2631
             ctx.exception != EXCP_TRAP)) {
2632
            RET_EXCP(ctxp, EXCP_TRACE, 0);
2633
        }
2634 2635 2636 2637 2638 2639

        /* if we reach a page boundary or are single stepping, stop
         * generation
         */
        if (((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
            (env->singlestep_enabled)) {
2640
            break;
2641
    }
2642 2643 2644 2645
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
2646
    if (ctx.exception == EXCP_NONE) {
2647
        gen_goto_tb(&ctx, 0, ctx.nip);
2648 2649
    } else if (ctx.exception != EXCP_BRANCH) {
        gen_op_set_T0(0);
2650 2651
    }
#if 1
B
bellard 已提交
2652 2653 2654 2655
    /* TO BE FIXED: T0 hasn't got a proper value, which makes tb_add_jump
     *              do bad business and then qemu crashes !
     */
    gen_op_set_T0(0);
2656
#endif
B
bellard 已提交
2657 2658 2659
    /* Generate the return instruction */
    gen_op_exit_tb();
    *gen_opc_ptr = INDEX_op_end;
2660 2661 2662 2663 2664
    if (search_pc) {
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
2665
        tb->size = 0;
B
bellard 已提交
2666
#if 0
2667 2668 2669
        if (loglevel > 0) {
            page_dump(logfile);
        }
B
bellard 已提交
2670
#endif
2671
    } else {
B
bellard 已提交
2672
        tb->size = ctx.nip - pc_start;
2673
    }
B
bellard 已提交
2674
#ifdef DEBUG_DISAS
2675
    if (loglevel & CPU_LOG_TB_CPU) {
2676
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
2677
        cpu_dump_state(env, logfile, fprintf, 0);
2678 2679
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
2680
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
2681
	target_disas(logfile, pc_start, ctx.nip - pc_start, msr_le);
B
bellard 已提交
2682
        fprintf(logfile, "\n");
2683 2684
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
2685 2686 2687 2688 2689 2690 2691 2692
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

2693
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
2694 2695 2696 2697
{
    return gen_intermediate_code_internal(env, tb, 0);
}

2698
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
2699 2700 2701
{
    return gen_intermediate_code_internal(env, tb, 1);
}