translate.c 95.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 33 34 35 36 37 38 39 40 41 42 43

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"
44 45

#define GEN8(func, NAME) \
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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]();                                                      \
65 66 67
}

#define GEN32(func, NAME) \
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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);
88

B
bellard 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* 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);
}

108 109 110
/* Segment register moves */
GEN16(gen_op_load_sr, gen_op_load_sr);
GEN16(gen_op_store_sr, gen_op_store_sr);
111

112 113 114 115 116 117 118 119
/* 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);
120

B
bellard 已提交
121 122 123 124 125 126 127
/* 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 已提交
128 129 130 131 132 133

static uint8_t  spr_access[1024 / 2];

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

147
struct opc_handler_t {
B
bellard 已提交
148 149
    /* invalid bits */
    uint32_t inval;
150 151
    /* instruction type */
    uint32_t type;
B
bellard 已提交
152 153
    /* handler */
    void (*handler)(DisasContext *ctx);
154
};
B
bellard 已提交
155

156
#define RET_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
157
do {                                                                          \
158 159 160 161 162
    if ((ctx)->exception == EXCP_NONE) {                                      \
        gen_op_update_nip((ctx)->nip);                                        \
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
163 164
} while (0)

165 166 167 168 169
#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)
170

171 172
#define RET_PRIVREG(ctx)                                                      \
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_REG)
173

174 175
#define RET_MTMSR(ctx)                                                        \
RET_EXCP((ctx), EXCP_MTMSR, 0)
B
bellard 已提交
176

177 178 179 180 181
static inline void RET_STOP (DisasContext *ctx)
{
    RET_EXCP(ctx, EXCP_MTMSR, 0);
}

182 183 184 185 186 187
static inline void RET_CHG_FLOW (DisasContext *ctx)
{
    gen_op_raise_exception_err(EXCP_MTMSR, 0);
    ctx->exception = EXCP_MTMSR;
}

B
bellard 已提交
188 189 190 191 192 193 194
#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;
195 196 197 198 199
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
200
    opc_handler_t handler;
201
    const unsigned char *oname;
B
bellard 已提交
202 203 204 205 206 207 208 209 210 211 212 213
} 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)                                  \
{                                                                             \
214
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

/* 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 */
242 243 244 245 246 247 248
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 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262
/***                              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 已提交
263 264
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
265 266 267 268

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

B
bellard 已提交
271 272 273 274 275 276 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
/***                            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;
}

304 305 306 307 308
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
309
#if defined(__APPLE__)
B
bellard 已提交
310
#define OPCODES_SECTION \
311
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
312
#else
B
bellard 已提交
313
#define OPCODES_SECTION \
314
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
315 316
#endif

B
bellard 已提交
317
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
318
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
319 320 321
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
322
    .pad  = { 0, },                                                           \
B
bellard 已提交
323 324
    .handler = {                                                              \
        .inval   = invl,                                                      \
325
        .type = _typ,                                                         \
B
bellard 已提交
326 327
        .handler = &gen_##name,                                               \
    },                                                                        \
328
    .oname = stringify(name),                                                 \
B
bellard 已提交
329 330 331
}

#define GEN_OPCODE_MARK(name)                                                 \
332
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
333 334 335
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
336
    .pad  = { 0, },                                                           \
B
bellard 已提交
337 338
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
339
        .type = 0x00,                                                         \
B
bellard 已提交
340 341
        .handler = NULL,                                                      \
    },                                                                        \
342
    .oname = stringify(name),                                                 \
B
bellard 已提交
343 344 345 346 347 348
}

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

/* Invalid instruction */
349 350
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
351
    RET_INVAL(ctx);
352 353
}

B
bellard 已提交
354 355
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
356
    .type    = PPC_NONE,
B
bellard 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    .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)                                                 \
379
        gen_op_set_Rc0();                                                     \
B
bellard 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    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)                                                 \
398
        gen_op_set_Rc0();                                                     \
B
bellard 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 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
    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);
590

B
bellard 已提交
591
/* or & or. */
592 593 594 595 596 597 598 599 600 601 602 603 604
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 已提交
605 606 607
/* orc & orc. */
GEN_LOGICAL2(orc, 0x0C);
/* xor & xor. */
608 609 610 611 612 613 614 615 616 617 618 619 620 621
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 已提交
622 623 624 625 626
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t uimm = UIMM(ctx->opcode);

627 628 629
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
B
bellard 已提交
630 631
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
632
    if (uimm != 0)
B
bellard 已提交
633 634 635 636 637 638 639 640
        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);

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

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
659
    gen_op_load_gpr_T0(rS(ctx->opcode));
660
    if (uimm != 0)
B
bellard 已提交
661
    gen_op_xori(uimm);
B
bellard 已提交
662 663 664 665 666 667
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
668 669 670 671 672 673
    uint32_t uimm = UIMM(ctx->opcode);

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
674
    gen_op_load_gpr_T0(rS(ctx->opcode));
675
    if (uimm != 0)
B
bellard 已提交
676
    gen_op_xori(uimm << 16);
B
bellard 已提交
677 678 679 680 681 682 683 684 685 686 687 688
    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 已提交
689
    gen_op_load_gpr_T1(rA(ctx->opcode));
B
bellard 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703
    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 已提交
704 705 706 707 708 709
#if 1 // TRY
    if (sh == 0) {
        gen_op_andi_(MASK(mb, me));
        goto store;
    }
#endif
B
bellard 已提交
710 711 712 713
    if (mb == 0) {
        if (me == 31) {
            gen_op_rotlwi(sh);
            goto store;
B
bellard 已提交
714
#if 0
B
bellard 已提交
715 716 717
        } else if (me == (31 - sh)) {
            gen_op_slwi(sh);
            goto store;
B
bellard 已提交
718
#endif
B
bellard 已提交
719 720
        }
    } else if (me == 31) {
B
bellard 已提交
721
#if 0
B
bellard 已提交
722 723 724 725
        if (sh == (32 - mb)) {
            gen_op_srwi(mb);
            goto store;
        }
B
bellard 已提交
726
#endif
B
bellard 已提交
727 728 729 730 731 732 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
    }
    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));
763
    if (SH(ctx->opcode) != 0)
B
bellard 已提交
764 765 766 767 768 769 770 771 772
    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                       ***/
773
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat)                           \
774 775
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
{                                                                             \
B
bellard 已提交
776 777 778 779
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
780 781 782 783
    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));                                     \
784 785 786 787
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
788 789 790 791 792 793
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
}

#define GEN_FLOAT_ACB(name, op2)                                              \
794 795
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0);                                     \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1);
796

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

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

#define GEN_FLOAT_B(name, op2, op3)                                           \
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
{                                                                             \
B
bellard 已提交
844 845 846 847
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
848 849 850 851 852 853
    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 已提交
854 855
}

856 857
#define GEN_FLOAT_BS(name, op1, op2)                                          \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                   \
858
{                                                                             \
B
bellard 已提交
859 860 861 862
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
863 864 865 866 867 868
    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 已提交
869 870
}

871 872
/* fadd - fadds */
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
873
/* fdiv - fdivs */
874
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
875
/* fmul - fmuls */
876
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
B
bellard 已提交
877 878

/* fres */
879
GEN_FLOAT_BS(res, 0x3B, 0x18);
B
bellard 已提交
880 881

/* frsqrte */
882
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A);
B
bellard 已提交
883 884

/* fsel */
885 886
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0);
/* fsub - fsubs */
887
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
B
bellard 已提交
888 889
/* Optional: */
/* fsqrt */
890 891 892 893 894 895 896 897 898 899 900 901 902
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 已提交
903

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

/***                     Floating-Point multiply-and-add                   ***/
920
/* fmadd - fmadds */
921
GEN_FLOAT_ACB(madd, 0x1D);
922
/* fmsub - fmsubs */
923
GEN_FLOAT_ACB(msub, 0x1C);
924
/* fnmadd - fnmadds */
925
GEN_FLOAT_ACB(nmadd, 0x1F);
926
/* fnmsub - fnmsubs */
927
GEN_FLOAT_ACB(nmsub, 0x1E);
B
bellard 已提交
928 929 930

/***                     Floating-Point round & convert                    ***/
/* fctiw */
931
GEN_FLOAT_B(ctiw, 0x0E, 0x00);
B
bellard 已提交
932
/* fctiwz */
933
GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
B
bellard 已提交
934
/* frsp */
935
GEN_FLOAT_B(rsp, 0x0C, 0x00);
B
bellard 已提交
936 937 938 939 940

/***                         Floating-Point compare                        ***/
/* fcmpo */
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
{
B
bellard 已提交
941 942 943 944
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
945 946 947 948 949
    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 已提交
950 951 952 953 954
}

/* fcmpu */
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
{
B
bellard 已提交
955 956 957 958
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
959 960 961 962 963
    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 已提交
964 965
}

966 967 968 969 970 971 972
/***                         Floating-point move                           ***/
/* fabs */
GEN_FLOAT_B(abs, 0x08, 0x08);

/* fmr  - fmr. */
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
B
bellard 已提交
973 974 975 976
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
977 978 979 980 981 982 983 984 985 986 987 988
    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 已提交
989 990 991 992
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
B
bellard 已提交
993 994 995 996
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
997 998 999
    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 已提交
1000 1001 1002 1003 1004
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1005 1006 1007 1008
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
1009
    gen_op_load_fpscr();
B
bellard 已提交
1010 1011 1012
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1013 1014 1015 1016 1017
}

/* mtfsb0 */
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1018 1019
    uint8_t crb;
    
B
bellard 已提交
1020 1021 1022 1023
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1024 1025 1026 1027 1028 1029
    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 已提交
1030 1031 1032 1033 1034
}

/* mtfsb1 */
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1035 1036
    uint8_t crb;
    
B
bellard 已提交
1037 1038 1039 1040
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1041 1042 1043 1044 1045 1046
    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 已提交
1047 1048 1049 1050 1051
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
B
bellard 已提交
1052 1053 1054 1055
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1056
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1057
    gen_op_store_fpscr(FM(ctx->opcode));
B
bellard 已提交
1058 1059
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1060 1061 1062 1063 1064
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
B
bellard 已提交
1065 1066 1067 1068
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
B
bellard 已提交
1069 1070 1071
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
B
bellard 已提交
1072 1073 1074
}

/***                             Integer load                              ***/
1075
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
1076
#if defined(CONFIG_USER_ONLY)
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
#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
1090 1091 1092 1093
#else
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
1094
    &gen_op_l##width##_le_user,                                               \
1095
    &gen_op_l##width##_kernel,                                                \
1096 1097
    &gen_op_l##width##_le_kernel,                                             \
};
1098 1099 1100
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
1101
    &gen_op_st##width##_le_user,                                              \
1102
    &gen_op_st##width##_kernel,                                               \
1103 1104 1105 1106 1107 1108 1109
    &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
1110 1111 1112
#endif

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

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

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

1160
#define GEN_LDX(width, opc2, opc3)                                            \
B
bellard 已提交
1161 1162 1163 1164 1165 1166 1167
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));                                  \
1168
        gen_op_add();                                                         \
B
bellard 已提交
1169
    }                                                                         \
1170
    op_ldst(l##width);                                                        \
B
bellard 已提交
1171 1172 1173
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

1174 1175 1176 1177 1178 1179
#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 已提交
1180 1181

/* lbz lbzu lbzux lbzx */
1182
GEN_LDS(bz, 0x02);
B
bellard 已提交
1183
/* lha lhau lhaux lhax */
1184
GEN_LDS(ha, 0x0A);
B
bellard 已提交
1185
/* lhz lhzu lhzux lhzx */
1186
GEN_LDS(hz, 0x08);
B
bellard 已提交
1187
/* lwz lwzu lwzux lwzx */
1188
GEN_LDS(wz, 0x00);
B
bellard 已提交
1189 1190

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

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

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

1237
#define GEN_STX(width, opc2, opc3)                                            \
B
bellard 已提交
1238 1239 1240 1241 1242 1243 1244
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));                                  \
1245
        gen_op_add();                                                         \
B
bellard 已提交
1246
    }                                                                         \
1247 1248
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
1249 1250
}

1251 1252 1253 1254 1255 1256
#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 已提交
1257 1258

/* stb stbu stbux stbx */
1259
GEN_STS(b, 0x06);
B
bellard 已提交
1260
/* sth sthu sthux sthx */
1261
GEN_STS(h, 0x0C);
B
bellard 已提交
1262
/* stw stwu stwux stwx */
1263
GEN_STS(w, 0x04);
B
bellard 已提交
1264 1265 1266

/***                Integer load and store with byte reverse               ***/
/* lhbrx */
1267 1268
OP_LD_TABLE(hbr);
GEN_LDX(hbr, 0x16, 0x18);
B
bellard 已提交
1269
/* lwbrx */
1270 1271
OP_LD_TABLE(wbr);
GEN_LDX(wbr, 0x16, 0x10);
B
bellard 已提交
1272
/* sthbrx */
1273 1274
OP_ST_TABLE(hbr);
GEN_STX(hbr, 0x16, 0x1C);
B
bellard 已提交
1275
/* stwbrx */
1276 1277
OP_ST_TABLE(wbr);
GEN_STX(wbr, 0x16, 0x14);
B
bellard 已提交
1278 1279

/***                    Integer load and store multiple                    ***/
1280
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1281
#if defined(CONFIG_USER_ONLY)
1282 1283 1284 1285 1286 1287 1288 1289
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,
};
1290 1291 1292
#else
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
1293
    &gen_op_lmw_le_user,
1294
    &gen_op_lmw_kernel,
1295
    &gen_op_lmw_le_kernel,
1296 1297 1298
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
1299
    &gen_op_stmw_le_user,
1300
    &gen_op_stmw_kernel,
1301
    &gen_op_stmw_le_kernel,
1302 1303 1304
};
#endif

B
bellard 已提交
1305 1306 1307
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1308 1309
    int simm = SIMM(ctx->opcode);

B
bellard 已提交
1310
    if (rA(ctx->opcode) == 0) {
1311
        gen_op_set_T0(simm);
B
bellard 已提交
1312 1313
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
1314 1315
        if (simm != 0)
            gen_op_addi(simm);
B
bellard 已提交
1316
    }
1317
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
1318 1319 1320 1321 1322
}

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

B
bellard 已提交
1325
    if (rA(ctx->opcode) == 0) {
1326
        gen_op_set_T0(simm);
B
bellard 已提交
1327 1328
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
1329 1330
        if (simm != 0)
            gen_op_addi(simm);
B
bellard 已提交
1331
    }
1332
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
1333 1334 1335
}

/***                    Integer load and store strings                     ***/
1336 1337
#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)
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
#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
1352 1353
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
1354
    &gen_op_lswi_le_user,
1355
    &gen_op_lswi_kernel,
1356
    &gen_op_lswi_le_kernel,
1357 1358 1359
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
1360
    &gen_op_lswx_le_user,
1361
    &gen_op_lswx_kernel,
1362
    &gen_op_lswx_le_kernel,
1363 1364 1365
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
1366
    &gen_op_stsw_le_user,
1367
    &gen_op_stsw_kernel,
1368
    &gen_op_stsw_le_kernel,
1369 1370 1371
};
#endif

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

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

/* lswx */
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
{
1407 1408 1409 1410 1411 1412
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

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

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

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

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
1445 1446 1447 1448 1449
    int ra = rA(ctx->opcode);

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

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

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

1472 1473
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1474
#if defined(CONFIG_USER_ONLY)
1475 1476 1477 1478 1479 1480 1481 1482
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,
};
1483
#else
B
bellard 已提交
1484 1485
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
1486
    &gen_op_lwarx_le_user,
B
bellard 已提交
1487
    &gen_op_lwarx_kernel,
1488
    &gen_op_lwarx_le_kernel,
B
bellard 已提交
1489
};
1490 1491
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
1492
    &gen_op_stwcx_le_user,
1493
    &gen_op_stwcx_kernel,
1494
    &gen_op_stwcx_le_kernel,
1495 1496 1497
};
#endif

1498
/* lwarx */
1499
GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
B
bellard 已提交
1500 1501 1502 1503 1504 1505
{
    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));
1506
        gen_op_add();
B
bellard 已提交
1507
    }
B
bellard 已提交
1508
    op_lwarx();
B
bellard 已提交
1509 1510 1511 1512
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

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

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

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

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

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

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

1610 1611 1612 1613 1614 1615
#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 已提交
1616 1617

/* lfd lfdu lfdux lfdx */
1618
GEN_LDFS(fd, 0x12);
B
bellard 已提交
1619
/* lfs lfsu lfsux lfsx */
1620
GEN_LDFS(fs, 0x10);
B
bellard 已提交
1621 1622 1623

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

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

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

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

1699 1700 1701 1702 1703 1704
#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 已提交
1705 1706

/* stfd stfdu stfdux stfdx */
1707
GEN_STFS(fd, 0x16);
B
bellard 已提交
1708
/* stfs stfsu stfsux stfsx */
1709
GEN_STFS(fs, 0x14);
B
bellard 已提交
1710 1711 1712 1713 1714

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

/***                                Branch                                 ***/

/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
B
bellard 已提交
1727 1728 1729 1730
    uint32_t li, target;

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

    if (AA(ctx->opcode) == 0)
B
bellard 已提交
1733
        target = ctx->nip + li - 4;
B
bellard 已提交
1734
    else
1735 1736
        target = li;
    if (LK(ctx->opcode)) {
B
bellard 已提交
1737
        gen_op_setlr(ctx->nip);
1738
    }
1739
    gen_op_b((long)ctx->tb, target);
1740
    ctx->exception = EXCP_BRANCH;
B
bellard 已提交
1741 1742
}

1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
#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:
1759
        li = (int32_t)((int16_t)(BD(ctx->opcode)));
1760
        if (AA(ctx->opcode) == 0) {
B
bellard 已提交
1761
            target = ctx->nip + li - 4;
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
        } 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 已提交
1775
        gen_op_setlr(ctx->nip);
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
    }
    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) {
                gen_op_b((long)ctx->tb, target);
            } 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 已提交
1830
        gen_op_btest((long)ctx->tb, target, ctx->nip);
1831
    } else {
B
bellard 已提交
1832
        gen_op_btest_T1(ctx->nip);
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
    }
 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 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

/***                      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)
{
1895
#if defined(CONFIG_USER_ONLY)
1896
    RET_PRIVOPC(ctx);
1897 1898 1899
#else
    /* Restore CPU state */
    if (!ctx->supervisor) {
1900 1901
        RET_PRIVOPC(ctx);
        return;
1902 1903
    }
    gen_op_rfi();
1904
    RET_CHG_FLOW(ctx);
1905
#endif
B
bellard 已提交
1906 1907 1908 1909 1910
}

/* sc */
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
{
1911
#if defined(CONFIG_USER_ONLY)
1912
    RET_EXCP(ctx, EXCP_SYSCALL_USER, 0);
1913
#else
1914
    RET_EXCP(ctx, EXCP_SYSCALL, 0);
1915
#endif
B
bellard 已提交
1916 1917 1918 1919 1920 1921
}

/***                                Trap                                   ***/
/* tw */
GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
{
1922 1923 1924
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
1925 1926 1927 1928 1929
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
1930 1931 1932 1933 1934 1935
    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 已提交
1936 1937 1938 1939 1940 1941 1942
}

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

1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
#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 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
    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)
{
1982
#if defined(CONFIG_USER_ONLY)
1983
    RET_PRIVREG(ctx);
1984 1985
#else
    if (!ctx->supervisor) {
1986 1987
        RET_PRIVREG(ctx);
        return;
1988
    }
B
bellard 已提交
1989 1990
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
1991
#endif
B
bellard 已提交
1992 1993
}

1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
#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 已提交
2005
/* mfspr */
2006
static inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
2007
{
2008
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
2009 2010
    uint32_t sprn = SPR(ctx->opcode);

2011 2012 2013 2014
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
2015
#endif
2016 2017 2018 2019 2020 2021 2022 2023
        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 */
            printf("Trying to read priviledged spr %d %03x\n", sprn, sprn);
2024
        RET_PRIVREG(ctx);
B
bellard 已提交
2025
        }
2026 2027 2028 2029
    } else {
        /* Not defined */
        printf("Trying to read invalid spr %d %03x\n", sprn, sprn);
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
B
bellard 已提交
2030 2031 2032
    }
}

2033
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
2034
{
2035
    gen_op_mfspr(ctx);
B
bellard 已提交
2036
    }
2037 2038 2039 2040 2041

/* mftb */
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_TB)
{
    gen_op_mfspr(ctx);
B
bellard 已提交
2042 2043 2044
}

/* mtcrf */
2045 2046
/* 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 已提交
2047 2048 2049 2050 2051 2052 2053 2054
{
    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)
{
2055
#if defined(CONFIG_USER_ONLY)
2056
    RET_PRIVREG(ctx);
2057 2058
#else
    if (!ctx->supervisor) {
2059 2060
        RET_PRIVREG(ctx);
        return;
2061
    }
B
bellard 已提交
2062 2063 2064
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_msr();
    /* Must stop the translation as machine state (may have) changed */
2065
    RET_MTMSR(ctx);
2066
#endif
B
bellard 已提交
2067 2068 2069 2070 2071
}

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

2075 2076 2077 2078
#if !defined(CONFIG_USER_ONLY)
    if (ctx->supervisor)
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
2079
#endif
2080 2081 2082 2083 2084 2085 2086 2087
        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 */
            printf("Trying to write priviledged spr %d %03x\n", sprn, sprn);
2088
        RET_PRIVREG(ctx);
2089
    }
2090 2091 2092 2093
    } else {
        /* Not defined */
        printf("Trying to write invalid spr %d %03x\n", sprn, sprn);
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
B
bellard 已提交
2094 2095 2096 2097 2098 2099
    }
}

/***                         Cache management                              ***/
/* For now, all those will be implemented as nop:
 * this is valid, regarding the PowerPC specs...
2100
 * We just have to flush tb while invalidating instruction cache lines...
B
bellard 已提交
2101 2102
 */
/* dcbf */
2103
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2104
{
2105 2106 2107 2108 2109 2110 2111 2112
    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 已提交
2113 2114 2115
}

/* dcbi (Supervisor only) */
2116
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2117
{
2118
#if defined(CONFIG_USER_ONLY)
2119
    RET_PRIVOPC(ctx);
2120 2121
#else
    if (!ctx->supervisor) {
2122 2123
        RET_PRIVOPC(ctx);
        return;
2124
    }
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
    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 已提交
2135 2136 2137
}

/* dcdst */
2138
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2139
{
2140 2141 2142 2143 2144 2145 2146 2147
    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 已提交
2148 2149 2150
}

/* dcbt */
2151
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2152 2153 2154 2155
{
}

/* dcbtst */
2156
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2157 2158 2159 2160
{
}

/* dcbz */
2161 2162 2163 2164 2165 2166
#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 已提交
2167 2168
    &gen_op_dcbz_user,
    &gen_op_dcbz_kernel,
2169 2170 2171 2172 2173
    &gen_op_dcbz_kernel,
};
#endif

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2174
{
B
bellard 已提交
2175 2176 2177 2178 2179
    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));
2180
        gen_op_add();
B
bellard 已提交
2181
    }
2182
    op_dcbz();
B
bellard 已提交
2183
    gen_op_check_reservation();
B
bellard 已提交
2184 2185 2186
}

/* icbi */
2187
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
2188
{
B
bellard 已提交
2189 2190 2191 2192 2193
    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));
2194
        gen_op_add();
B
bellard 已提交
2195
    }
2196
    gen_op_icbi();
B
bellard 已提交
2197 2198 2199 2200
}

/* Optional: */
/* dcba */
2201
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_OPT)
B
bellard 已提交
2202 2203 2204 2205 2206 2207 2208 2209
{
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
2210
#if defined(CONFIG_USER_ONLY)
2211
    RET_PRIVREG(ctx);
2212 2213
#else
    if (!ctx->supervisor) {
2214 2215
        RET_PRIVREG(ctx);
        return;
2216 2217 2218 2219
    }
    gen_op_load_sr(SR(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
2220 2221 2222
}

/* mfsrin */
2223
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
2224
{
2225
#if defined(CONFIG_USER_ONLY)
2226
    RET_PRIVREG(ctx);
2227 2228
#else
    if (!ctx->supervisor) {
2229 2230
        RET_PRIVREG(ctx);
        return;
2231 2232 2233 2234 2235
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_load_srin();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
2236 2237 2238
}

/* mtsr */
B
bellard 已提交
2239
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
2240
{
2241
#if defined(CONFIG_USER_ONLY)
2242
    RET_PRIVREG(ctx);
2243 2244
#else
    if (!ctx->supervisor) {
2245 2246
        RET_PRIVREG(ctx);
        return;
2247 2248 2249 2250
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_store_sr(SR(ctx->opcode));
#endif
B
bellard 已提交
2251 2252 2253
}

/* mtsrin */
2254
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
2255
{
2256
#if defined(CONFIG_USER_ONLY)
2257
    RET_PRIVREG(ctx);
2258 2259
#else
    if (!ctx->supervisor) {
2260 2261
        RET_PRIVREG(ctx);
        return;
2262 2263 2264 2265 2266
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_store_srin();
#endif
B
bellard 已提交
2267 2268 2269 2270 2271
}

/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
2272
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
2273
{
2274
#if defined(CONFIG_USER_ONLY)
2275
    RET_PRIVOPC(ctx);
2276 2277
#else
    if (!ctx->supervisor) {
2278 2279 2280 2281
        if (loglevel)
            fprintf(logfile, "%s: ! supervisor\n", __func__);
        RET_PRIVOPC(ctx);
        return;
2282 2283
    }
    gen_op_tlbia();
B
bellard 已提交
2284
    RET_MTMSR(ctx);
2285
#endif
B
bellard 已提交
2286 2287 2288
}

/* tlbie */
2289
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
B
bellard 已提交
2290
{
2291
#if defined(CONFIG_USER_ONLY)
2292
    RET_PRIVOPC(ctx);
2293 2294
#else
    if (!ctx->supervisor) {
2295 2296
        RET_PRIVOPC(ctx);
        return;
2297 2298 2299
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_tlbie();
B
bellard 已提交
2300
    RET_MTMSR(ctx);
2301
#endif
B
bellard 已提交
2302 2303 2304
}

/* tlbsync */
B
bellard 已提交
2305
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2306
{
2307
#if defined(CONFIG_USER_ONLY)
2308
    RET_PRIVOPC(ctx);
2309 2310
#else
    if (!ctx->supervisor) {
2311 2312
        RET_PRIVOPC(ctx);
        return;
2313 2314 2315 2316
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
B
bellard 已提交
2317
    RET_MTMSR(ctx);
2318
#endif
B
bellard 已提交
2319 2320 2321 2322
}

/***                              External control                         ***/
/* Optional: */
2323 2324
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
#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
2335 2336
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
2337
    &gen_op_eciwx_le_user,
2338
    &gen_op_eciwx_kernel,
2339
    &gen_op_eciwx_le_kernel,
2340 2341 2342
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
2343
    &gen_op_ecowx_le_user,
2344
    &gen_op_ecowx_kernel,
2345
    &gen_op_ecowx_le_kernel,
2346 2347 2348
};
#endif

2349
/* eciwx */
B
bellard 已提交
2350 2351
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
    /* 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 已提交
2362 2363 2364 2365 2366
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
    /* 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 已提交
2377 2378 2379 2380 2381
}

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

2382
#include "translate_init.c"
B
bellard 已提交
2383

2384
/*****************************************************************************/
2385
/* Misc PowerPC helpers */
B
bellard 已提交
2386 2387 2388
void cpu_dump_state(CPUState *env, FILE *f, 
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
B
bellard 已提交
2389
{
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
#if defined(TARGET_PPC64) || 1
#define FILL ""
#define REGX "%016llx"
#define RGPL  4
#define RFPL  4
#else
#define FILL "        "
#define REGX "%08llx"
#define RGPL  8
#define RFPL  4
#endif

B
bellard 已提交
2402 2403
    int i;

2404 2405 2406 2407 2408
    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 已提交
2409
        for (i = 0; i < 32; i++) {
2410 2411 2412 2413
        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 已提交
2414
            cpu_fprintf(f, "\n");
B
bellard 已提交
2415
        }
2416
    cpu_fprintf(f, "CR ");
B
bellard 已提交
2417
        for (i = 0; i < 8; i++)
B
bellard 已提交
2418 2419
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
B
bellard 已提交
2420 2421 2422 2423 2424 2425 2426 2427
        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 已提交
2428
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
B
bellard 已提交
2429
        }
2430 2431 2432 2433
    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 已提交
2434
        cpu_fprintf(f, " %016llx", *((uint64_t *)&env->fpr[i]));
2435
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
2436
            cpu_fprintf(f, "\n");
B
bellard 已提交
2437
    }
2438 2439 2440
    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 已提交
2441

2442 2443 2444 2445
#undef REGX
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
2446 2447
}

2448
/*****************************************************************************/
B
bellard 已提交
2449 2450 2451
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
                                    int search_pc)
{
2452
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
2453
    opc_handler_t **table, *handler;
B
bellard 已提交
2454
    target_ulong pc_start;
B
bellard 已提交
2455 2456 2457 2458 2459 2460 2461
    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 已提交
2462
    ctx.nip = pc_start;
B
bellard 已提交
2463
    ctx.tb = tb;
2464
    ctx.exception = EXCP_NONE;
2465
    ctx.spr_cb = env->spr_cb;
2466
#if defined(CONFIG_USER_ONLY)
2467
    ctx.mem_idx = msr_le;
2468 2469
#else
    ctx.supervisor = 1 - msr_pr;
2470
    ctx.mem_idx = ((1 - msr_pr) << 1) | msr_le;
2471
#endif
B
bellard 已提交
2472
    ctx.fpu_enabled = msr_fp;
2473
#if defined (DO_SINGLE_STEP) && 0
2474 2475 2476 2477 2478
    /* 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) {
B
bellard 已提交
2479 2480 2481 2482 2483 2484
        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 已提交
2485
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
2486 2487 2488
                gen_opc_instr_start[lj] = 1;
            }
        }
2489 2490
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
2491
            fprintf(logfile, "----------------\n");
B
bellard 已提交
2492
            fprintf(logfile, "nip=%08x super=%d ir=%d\n",
2493 2494 2495
                    ctx.nip, 1 - msr_pr, msr_ir);
        }
#endif
B
bellard 已提交
2496
        ctx.opcode = ldl_code(ctx.nip);
2497 2498 2499 2500 2501 2502
        if (msr_le) {
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                ((ctx.opcode & 0x00FF0000) >> 8) |
                ((ctx.opcode & 0x0000FF00) << 8) |
                ((ctx.opcode & 0x000000FF) << 24);
        }
2503 2504
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
2505
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
2506
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
2507
                    opc3(ctx.opcode), msr_le ? "little" : "big");
B
bellard 已提交
2508 2509
        }
#endif
B
bellard 已提交
2510
        ctx.nip += 4;
2511
        table = env->opcodes;
B
bellard 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
        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 已提交
2523
            if (loglevel > 0) {
B
bellard 已提交
2524
                    fprintf(logfile, "invalid/unsupported opcode: "
B
bellard 已提交
2525
                        "%02x - %02x - %02x (%08x) 0x%08x %d\n",
2526
                            opc1(ctx.opcode), opc2(ctx.opcode),
B
bellard 已提交
2527 2528 2529 2530 2531 2532 2533
                        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 已提交
2534
                } else {
B
bellard 已提交
2535 2536
            if ((ctx.opcode & handler->inval) != 0) {
                if (loglevel > 0) {
B
bellard 已提交
2537
                    fprintf(logfile, "invalid bits: %08x for opcode: "
B
bellard 已提交
2538
                            "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
B
bellard 已提交
2539 2540
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
2541
                            ctx.opcode, ctx.nip - 4);
2542 2543
                } else {
                    printf("invalid bits: %08x for opcode: "
B
bellard 已提交
2544
                           "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
2545 2546
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
2547
                           ctx.opcode, ctx.nip - 4);
2548
            }
B
bellard 已提交
2549 2550
                RET_INVAL(ctxp);
                break;
B
bellard 已提交
2551 2552
            }
        }
B
bellard 已提交
2553
        (*(handler->handler))(&ctx);
2554 2555 2556 2557 2558 2559 2560
        /* 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 已提交
2561 2562 2563
            (msr_se && (ctx.nip < 0x100 ||
                        ctx.nip > 0xF00 ||
                        (ctx.nip & 0xFC) != 0x04) &&
2564 2565
             ctx.exception != EXCP_SYSCALL &&
             ctx.exception != EXCP_SYSCALL_USER &&
2566
             ctx.exception != EXCP_TRAP)) {
2567
            RET_EXCP(ctxp, EXCP_TRACE, 0);
2568
        }
2569
        /* if we reach a page boundary, stop generation */
2570
        if ((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) {
2571
            break;
2572
    }
2573 2574 2575 2576
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
2577 2578 2579 2580
    if (ctx.exception == EXCP_NONE) {
        gen_op_b((unsigned long)ctx.tb, ctx.nip);
    } else if (ctx.exception != EXCP_BRANCH) {
        gen_op_set_T0(0);
2581 2582
    }
#if 1
B
bellard 已提交
2583 2584 2585 2586
    /* 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);
2587
#endif
B
bellard 已提交
2588 2589 2590
    /* Generate the return instruction */
    gen_op_exit_tb();
    *gen_opc_ptr = INDEX_op_end;
2591 2592 2593 2594 2595
    if (search_pc) {
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
2596
        tb->size = 0;
B
bellard 已提交
2597
#if 0
2598 2599 2600
        if (loglevel > 0) {
            page_dump(logfile);
        }
B
bellard 已提交
2601
#endif
2602
    } else {
B
bellard 已提交
2603
        tb->size = ctx.nip - pc_start;
2604
    }
B
bellard 已提交
2605
#ifdef DEBUG_DISAS
2606
    if (loglevel & CPU_LOG_TB_CPU) {
2607
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
2608
        cpu_dump_state(env, logfile, fprintf, 0);
2609 2610
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
2611 2612
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
	target_disas(logfile, pc_start, ctx.nip - pc_start, 0);
B
bellard 已提交
2613
        fprintf(logfile, "\n");
2614 2615
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
2616 2617 2618 2619 2620 2621 2622 2623
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

2624
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
2625 2626 2627 2628
{
    return gen_intermediate_code_internal(env, tb, 0);
}

2629
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
2630 2631 2632
{
    return gen_intermediate_code_internal(env, tb, 1);
}