translate.c 97.6 KB
Newer Older
bellard's avatar
bellard committed
1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
bellard's avatar
bellard committed
3
 * 
4
 *  Copyright (c) 2003-2005 Jocelyn Mayer
bellard's avatar
bellard committed
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
 */
bellard's avatar
bellard committed
20
21
22
23
24
25
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

bellard's avatar
bellard committed
26
#include "cpu.h"
bellard's avatar
bellard committed
27
#include "exec-all.h"
bellard's avatar
bellard committed
28
29
30
#include "disas.h"

//#define DO_SINGLE_STEP
31
//#define PPC_DEBUG_DISAS
bellard's avatar
bellard committed
32

bellard's avatar
bellard committed
33
34
35
36
37
38
#ifdef USE_DIRECT_JUMP
#define TBPARAM(x)
#else
#define TBPARAM(x) (long)(x)
#endif

bellard's avatar
bellard committed
39
40
41
42
43
44
45
46
47
48
49
enum {
#define DEF(s, n, copy_size) INDEX_op_ ## s,
#include "opc.h"
#undef DEF
    NB_OPS,
};

static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;

#include "gen-op.h"
50
51

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

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

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

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

bellard's avatar
bellard committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Floating point condition and status register moves */
GEN8(gen_op_load_fpscr_T0, gen_op_load_fpscr_T0_fpscr);
GEN8(gen_op_store_T0_fpscr, gen_op_store_T0_fpscr_fpscr);
GEN8(gen_op_clear_fpscr, gen_op_clear_fpscr_fpscr);
static GenOpFunc1 *gen_op_store_T0_fpscri_fpscr_table[8] = {
    &gen_op_store_T0_fpscri_fpscr0,
    &gen_op_store_T0_fpscri_fpscr1,
    &gen_op_store_T0_fpscri_fpscr2,
    &gen_op_store_T0_fpscri_fpscr3,
    &gen_op_store_T0_fpscri_fpscr4,
    &gen_op_store_T0_fpscri_fpscr5,
    &gen_op_store_T0_fpscri_fpscr6,
    &gen_op_store_T0_fpscri_fpscr7,
};
static inline void gen_op_store_T0_fpscri(int n, uint8_t param)
{
    (*gen_op_store_T0_fpscri_fpscr_table[n])(param);
}

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

118
119
120
121
122
123
124
125
/* General purpose registers moves */
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);

GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
126

bellard's avatar
bellard committed
127
128
129
130
131
132
133
/* floating point registers moves */
GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);
GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);
GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);
GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);
GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
bellard's avatar
bellard committed
134
135
136
137
138
139

static uint8_t  spr_access[1024 / 2];

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

154
struct opc_handler_t {
bellard's avatar
bellard committed
155
156
    /* invalid bits */
    uint32_t inval;
157
158
    /* instruction type */
    uint32_t type;
bellard's avatar
bellard committed
159
160
    /* handler */
    void (*handler)(DisasContext *ctx);
161
};
bellard's avatar
bellard committed
162

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

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

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

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

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

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

bellard's avatar
bellard committed
194
195
196
197
198
199
200
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
static void gen_##name (DisasContext *ctx)

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

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

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

/* Opcode part 1 */
EXTRACT_HELPER(opc1, 26, 6);
/* Opcode part 2 */
EXTRACT_HELPER(opc2, 1, 5);
/* Opcode part 3 */
EXTRACT_HELPER(opc3, 6, 5);
/* Update Cr0 flags */
EXTRACT_HELPER(Rc, 0, 1);
/* Destination */
EXTRACT_HELPER(rD, 21, 5);
/* Source */
EXTRACT_HELPER(rS, 21, 5);
/* First operand */
EXTRACT_HELPER(rA, 16, 5);
/* Second operand */
EXTRACT_HELPER(rB, 11, 5);
/* Third operand */
EXTRACT_HELPER(rC, 6, 5);
/***                               Get CRn                                 ***/
EXTRACT_HELPER(crfD, 23, 3);
EXTRACT_HELPER(crfS, 18, 3);
EXTRACT_HELPER(crbD, 21, 5);
EXTRACT_HELPER(crbA, 16, 5);
EXTRACT_HELPER(crbB, 11, 5);
/* SPR / TBL */
248
249
250
251
252
253
254
EXTRACT_HELPER(_SPR, 11, 10);
static inline uint32_t SPR (uint32_t opcode)
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
bellard's avatar
bellard committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***                              Get constants                            ***/
EXTRACT_HELPER(IMM, 12, 8);
/* 16 bits signed immediate value */
EXTRACT_SHELPER(SIMM, 0, 16);
/* 16 bits unsigned immediate value */
EXTRACT_HELPER(UIMM, 0, 16);
/* Bit count */
EXTRACT_HELPER(NB, 11, 5);
/* Shift count */
EXTRACT_HELPER(SH, 11, 5);
/* Mask start */
EXTRACT_HELPER(MB, 6, 5);
/* Mask end */
EXTRACT_HELPER(ME, 1, 5);
bellard's avatar
bellard committed
269
270
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
bellard's avatar
bellard committed
271
272
273
274

EXTRACT_HELPER(CRM, 12, 8);
EXTRACT_HELPER(FM, 17, 8);
EXTRACT_HELPER(SR, 16, 4);
bellard's avatar
bellard committed
275
276
EXTRACT_HELPER(FPIMM, 20, 4);

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

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

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

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

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

    return ret;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if (rA(ctx->opcode) == 0) {
        gen_op_set_T0(simm << 16);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_addi(simm << 16);
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_mulli(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_subfic(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/***                           Integer comparison                          ***/
#define GEN_CMP(name, opc)                                                    \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}

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

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

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

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

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

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

bellard's avatar
bellard committed
611
612
613
/* orc & orc. */
GEN_LOGICAL2(orc, 0x0C);
/* xor & xor. */
614
615
616
617
618
619
620
621
622
623
624
625
626
627
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    /* Optimisation for "set to zero" case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_xor();
    } else {
        gen_op_set_T0(0);
    }
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}
bellard's avatar
bellard committed
628
629
630
631
632
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t uimm = UIMM(ctx->opcode);

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

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

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

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

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

/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;

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

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    if (mb == 0 && me == 31) {
        gen_op_rotl();
    } else
    {
        gen_op_rlwnm(MASK(mb, me));
    }
    if (Rc(ctx->opcode) != 0)
        gen_op_set_Rc0();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

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

/***                       Floating-Point arithmetic                       ***/
779
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat)                           \
780
781
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
{                                                                             \
bellard's avatar
bellard committed
782
783
784
785
    if (!ctx->fpu_enabled) {                                                  \
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
        return;                                                               \
    }                                                                         \
786
787
788
789
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
790
791
792
793
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
794
795
796
797
798
799
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
    if (Rc(ctx->opcode))                                                      \
        gen_op_set_Rc1();                                                     \
}

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

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

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

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

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

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

/* fres */
885
GEN_FLOAT_BS(res, 0x3B, 0x18);
bellard's avatar
bellard committed
886
887

/* frsqrte */
888
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A);
bellard's avatar
bellard committed
889
890

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

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

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

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

/***                         Floating-Point compare                        ***/
/* fcmpo */
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
{
bellard's avatar
bellard committed
947
948
949
950
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
951
952
953
954
955
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpo();
    gen_op_store_T0_crf(crfD(ctx->opcode));
bellard's avatar
bellard committed
956
957
958
959
960
}

/* fcmpu */
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
{
bellard's avatar
bellard committed
961
962
963
964
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
965
966
967
968
969
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpu();
    gen_op_store_T0_crf(crfD(ctx->opcode));
bellard's avatar
bellard committed
970
971
}

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

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

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

bellard's avatar
bellard committed
995
996
997
998
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
bellard's avatar
bellard committed
999
1000
1001
1002
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
bellard's avatar
bellard committed
1003
1004
1005
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
    gen_op_clear_fpscr(crfS(ctx->opcode));
bellard's avatar
bellard committed
1006
1007
1008
1009
1010
}

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

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

/* mtfsb1 */
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
{
bellard's avatar
bellard committed
1041
1042
    uint8_t crb;
    
bellard's avatar
bellard committed
1043
1044
1045
1046
    if (!ctx->fpu_enabled) {
        RET_EXCP(ctx, EXCP_NO_FP, 0);
        return;
    }
bellard's avatar
bellard committed
1047
1048
1049
1050
1051
1052
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
    gen_op_store_T0_fpscr(crb);
    if (Rc(ctx->opcode))
        gen_op_set_Rc1();
bellard's avatar
bellard committed
1053
1054
1055
1056
1057
}

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

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

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

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

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

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

1166
#define GEN_LDX(width, opc2, opc3)                                            \
bellard's avatar
bellard committed
1167
1168
1169
1170
1171
1172
1173
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
{                                                                             \
    if (rA(ctx->opcode) == 0) {                                               \
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1174
        gen_op_add();                                                         \
bellard's avatar
bellard committed
1175
    }                                                                         \
1176
    op_ldst(l##width);                                                        \
bellard's avatar
bellard committed
1177
1178
1179
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

1180
1181
1182
1183
1184
1185
#define GEN_LDS(width, op)                                                    \
OP_LD_TABLE(width);                                                           \
GEN_LD(width, op | 0x20);                                                     \
GEN_LDU(width, op | 0x21);                                                    \
GEN_LDUX(width, op | 0x01);                                                   \
GEN_LDX(width, 0x17, op | 0x00)
bellard's avatar
bellard committed
1186
1187

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

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

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

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

1243
#define GEN_STX(width, opc2, opc3)                                            \
bellard's avatar
bellard committed
1244
1245
1246
1247
1248
1249
1250
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
{                                                                             \
    if (rA(ctx->opcode) == 0) {                                               \
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
    } else {                                                                  \
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1251
        gen_op_add();                                                         \
bellard's avatar
bellard committed
1252
    }                                                                         \
1253
1254
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
bellard's avatar
bellard committed
1255
1256
}

1257
1258
1259
1260
1261
1262
#define GEN_STS(width, op)                                                    \
OP_ST_TABLE(width);                                                           \
GEN_ST(width, op | 0x20);                                                     \
GEN_STU(width, op | 0x21);                                                    \
GEN_STUX(width, op | 0x01);                                                   \
GEN_STX(width, 0x17, op | 0x00)
bellard's avatar
bellard committed
1263
1264

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

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

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

bellard's avatar
bellard committed
1311
1312
1313
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1314
1315
    int simm = SIMM(ctx->opcode);

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

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

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

/***                    Integer load and store strings                     ***/
1342
1343
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
#if defined(CONFIG_USER_ONLY)
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
};
#else
1358
1359
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
1360
    &gen_op_lswi_le_user,
1361
    &gen_op_lswi_kernel,
1362