Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 2a3ec4b5

History | View | Annotate | Download (285.9 kB)

1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
#include <stdarg.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <inttypes.h>
25

    
26
#include "cpu.h"
27
#include "exec-all.h"
28
#include "disas.h"
29
#include "tcg-op.h"
30
#include "qemu-common.h"
31

    
32
#include "helper.h"
33
#define GEN_HELPER 1
34
#include "helper.h"
35

    
36
#define CPU_SINGLE_STEP 0x1
37
#define CPU_BRANCH_STEP 0x2
38
#define GDBSTUB_SINGLE_STEP 0x4
39

    
40
/* Include definitions for instructions classes and implementations flags */
41
//#define DO_SINGLE_STEP
42
//#define PPC_DEBUG_DISAS
43
//#define DO_PPC_STATISTICS
44
//#define OPTIMIZE_FPRF_UPDATE
45

    
46
/*****************************************************************************/
47
/* Code translation helpers                                                  */
48

    
49
/* global register indexes */
50
static TCGv_ptr cpu_env;
51
static char cpu_reg_names[10*3 + 22*4 /* GPR */
52
#if !defined(TARGET_PPC64)
53
    + 10*4 + 22*5 /* SPE GPRh */
54
#endif
55
    + 10*4 + 22*5 /* FPR */
56
    + 2*(10*6 + 22*7) /* AVRh, AVRl */
57
    + 8*5 /* CRF */];
58
static TCGv cpu_gpr[32];
59
#if !defined(TARGET_PPC64)
60
static TCGv cpu_gprh[32];
61
#endif
62
static TCGv_i64 cpu_fpr[32];
63
static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64
static TCGv_i32 cpu_crf[8];
65
static TCGv cpu_nip;
66
static TCGv cpu_msr;
67
static TCGv cpu_ctr;
68
static TCGv cpu_lr;
69
static TCGv cpu_xer;
70
static TCGv cpu_reserve;
71
static TCGv_i32 cpu_fpscr;
72
static TCGv_i32 cpu_access_type;
73

    
74
#include "gen-icount.h"
75

    
76
void ppc_translate_init(void)
77
{
78
    int i;
79
    char* p;
80
    static int done_init = 0;
81

    
82
    if (done_init)
83
        return;
84

    
85
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
86

    
87
    p = cpu_reg_names;
88

    
89
    for (i = 0; i < 8; i++) {
90
        sprintf(p, "crf%d", i);
91
        cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
92
                                            offsetof(CPUState, crf[i]), p);
93
        p += 5;
94
    }
95

    
96
    for (i = 0; i < 32; i++) {
97
        sprintf(p, "r%d", i);
98
        cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
99
                                        offsetof(CPUState, gpr[i]), p);
100
        p += (i < 10) ? 3 : 4;
101
#if !defined(TARGET_PPC64)
102
        sprintf(p, "r%dH", i);
103
        cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
104
                                             offsetof(CPUState, gprh[i]), p);
105
        p += (i < 10) ? 4 : 5;
106
#endif
107

    
108
        sprintf(p, "fp%d", i);
109
        cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
110
                                            offsetof(CPUState, fpr[i]), p);
111
        p += (i < 10) ? 4 : 5;
112

    
113
        sprintf(p, "avr%dH", i);
114
#ifdef WORDS_BIGENDIAN
115
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
116
                                             offsetof(CPUState, avr[i].u64[0]), p);
117
#else
118
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
119
                                             offsetof(CPUState, avr[i].u64[1]), p);
120
#endif
121
        p += (i < 10) ? 6 : 7;
122

    
123
        sprintf(p, "avr%dL", i);
124
#ifdef WORDS_BIGENDIAN
125
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
126
                                             offsetof(CPUState, avr[i].u64[1]), p);
127
#else
128
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
129
                                             offsetof(CPUState, avr[i].u64[0]), p);
130
#endif
131
        p += (i < 10) ? 6 : 7;
132
    }
133

    
134
    cpu_nip = tcg_global_mem_new(TCG_AREG0,
135
                                 offsetof(CPUState, nip), "nip");
136

    
137
    cpu_msr = tcg_global_mem_new(TCG_AREG0,
138
                                 offsetof(CPUState, msr), "msr");
139

    
140
    cpu_ctr = tcg_global_mem_new(TCG_AREG0,
141
                                 offsetof(CPUState, ctr), "ctr");
142

    
143
    cpu_lr = tcg_global_mem_new(TCG_AREG0,
144
                                offsetof(CPUState, lr), "lr");
145

    
146
    cpu_xer = tcg_global_mem_new(TCG_AREG0,
147
                                 offsetof(CPUState, xer), "xer");
148

    
149
    cpu_reserve = tcg_global_mem_new(TCG_AREG0,
150
                                     offsetof(CPUState, reserve), "reserve");
151

    
152
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
153
                                       offsetof(CPUState, fpscr), "fpscr");
154

    
155
    cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
156
                                             offsetof(CPUState, access_type), "access_type");
157

    
158
    /* register helpers */
159
#define GEN_HELPER 2
160
#include "helper.h"
161

    
162
    done_init = 1;
163
}
164

    
165
#if defined(OPTIMIZE_FPRF_UPDATE)
166
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
167
static uint16_t **gen_fprf_ptr;
168
#endif
169

    
170
/* internal defines */
171
typedef struct DisasContext {
172
    struct TranslationBlock *tb;
173
    target_ulong nip;
174
    uint32_t opcode;
175
    uint32_t exception;
176
    /* Routine used to access memory */
177
    int mem_idx;
178
    int access_type;
179
    /* Translation flags */
180
    int le_mode;
181
#if defined(TARGET_PPC64)
182
    int sf_mode;
183
#endif
184
    int fpu_enabled;
185
    int altivec_enabled;
186
    int spe_enabled;
187
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
188
    int singlestep_enabled;
189
} DisasContext;
190

    
191
struct opc_handler_t {
192
    /* invalid bits */
193
    uint32_t inval;
194
    /* instruction type */
195
    uint64_t type;
196
    /* handler */
197
    void (*handler)(DisasContext *ctx);
198
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
199
    const char *oname;
200
#endif
201
#if defined(DO_PPC_STATISTICS)
202
    uint64_t count;
203
#endif
204
};
205

    
206
static always_inline void gen_reset_fpstatus (void)
207
{
208
#ifdef CONFIG_SOFTFLOAT
209
    gen_op_reset_fpstatus();
210
#endif
211
}
212

    
213
static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
214
{
215
    TCGv_i32 t0 = tcg_temp_new_i32();
216

    
217
    if (set_fprf != 0) {
218
        /* This case might be optimized later */
219
#if defined(OPTIMIZE_FPRF_UPDATE)
220
        *gen_fprf_ptr++ = gen_opc_ptr;
221
#endif
222
        tcg_gen_movi_i32(t0, 1);
223
        gen_helper_compute_fprf(t0, arg, t0);
224
        if (unlikely(set_rc)) {
225
            tcg_gen_mov_i32(cpu_crf[1], t0);
226
        }
227
        gen_helper_float_check_status();
228
    } else if (unlikely(set_rc)) {
229
        /* We always need to compute fpcc */
230
        tcg_gen_movi_i32(t0, 0);
231
        gen_helper_compute_fprf(t0, arg, t0);
232
        tcg_gen_mov_i32(cpu_crf[1], t0);
233
    }
234

    
235
    tcg_temp_free_i32(t0);
236
}
237

    
238
static always_inline void gen_optimize_fprf (void)
239
{
240
#if defined(OPTIMIZE_FPRF_UPDATE)
241
    uint16_t **ptr;
242

    
243
    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
244
        *ptr = INDEX_op_nop1;
245
    gen_fprf_ptr = gen_fprf_buf;
246
#endif
247
}
248

    
249
static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
250
{
251
    if (ctx->access_type != access_type) {
252
        tcg_gen_movi_i32(cpu_access_type, access_type);
253
        ctx->access_type = access_type;
254
    }
255
}
256

    
257
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
258
{
259
#if defined(TARGET_PPC64)
260
    if (ctx->sf_mode)
261
        tcg_gen_movi_tl(cpu_nip, nip);
262
    else
263
#endif
264
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
265
}
266

    
267
#define GEN_EXCP(ctx, excp, error)                                            \
268
do {                                                                          \
269
    TCGv_i32 t0 = tcg_const_i32(excp);                                        \
270
    TCGv_i32 t1 = tcg_const_i32(error);                                       \
271
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
272
        gen_update_nip(ctx, (ctx)->nip);                                      \
273
    }                                                                         \
274
    gen_helper_raise_exception_err(t0, t1);                                   \
275
    tcg_temp_free_i32(t0);                                                    \
276
    tcg_temp_free_i32(t1);                                                    \
277
    ctx->exception = (excp);                                                  \
278
} while (0)
279

    
280
#define GEN_EXCP_INVAL(ctx)                                                   \
281
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
282
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
283

    
284
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
285
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
286
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
287

    
288
#define GEN_EXCP_PRIVREG(ctx)                                                 \
289
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
290
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
291

    
292
#define GEN_EXCP_NO_FP(ctx)                                                   \
293
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
294

    
295
#define GEN_EXCP_NO_AP(ctx)                                                   \
296
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
297

    
298
#define GEN_EXCP_NO_VR(ctx)                                                   \
299
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
300

    
301
/* Stop translation */
302
static always_inline void GEN_STOP (DisasContext *ctx)
303
{
304
    gen_update_nip(ctx, ctx->nip);
305
    ctx->exception = POWERPC_EXCP_STOP;
306
}
307

    
308
/* No need to update nip here, as execution flow will change */
309
static always_inline void GEN_SYNC (DisasContext *ctx)
310
{
311
    ctx->exception = POWERPC_EXCP_SYNC;
312
}
313

    
314
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
315
static void gen_##name (DisasContext *ctx);                                   \
316
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
317
static void gen_##name (DisasContext *ctx)
318

    
319
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
320
static void gen_##name (DisasContext *ctx);                                   \
321
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
322
static void gen_##name (DisasContext *ctx)
323

    
324
typedef struct opcode_t {
325
    unsigned char opc1, opc2, opc3;
326
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
327
    unsigned char pad[5];
328
#else
329
    unsigned char pad[1];
330
#endif
331
    opc_handler_t handler;
332
    const char *oname;
333
} opcode_t;
334

    
335
/*****************************************************************************/
336
/***                           Instruction decoding                        ***/
337
#define EXTRACT_HELPER(name, shift, nb)                                       \
338
static always_inline uint32_t name (uint32_t opcode)                          \
339
{                                                                             \
340
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
341
}
342

    
343
#define EXTRACT_SHELPER(name, shift, nb)                                      \
344
static always_inline int32_t name (uint32_t opcode)                           \
345
{                                                                             \
346
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
347
}
348

    
349
/* Opcode part 1 */
350
EXTRACT_HELPER(opc1, 26, 6);
351
/* Opcode part 2 */
352
EXTRACT_HELPER(opc2, 1, 5);
353
/* Opcode part 3 */
354
EXTRACT_HELPER(opc3, 6, 5);
355
/* Update Cr0 flags */
356
EXTRACT_HELPER(Rc, 0, 1);
357
/* Destination */
358
EXTRACT_HELPER(rD, 21, 5);
359
/* Source */
360
EXTRACT_HELPER(rS, 21, 5);
361
/* First operand */
362
EXTRACT_HELPER(rA, 16, 5);
363
/* Second operand */
364
EXTRACT_HELPER(rB, 11, 5);
365
/* Third operand */
366
EXTRACT_HELPER(rC, 6, 5);
367
/***                               Get CRn                                 ***/
368
EXTRACT_HELPER(crfD, 23, 3);
369
EXTRACT_HELPER(crfS, 18, 3);
370
EXTRACT_HELPER(crbD, 21, 5);
371
EXTRACT_HELPER(crbA, 16, 5);
372
EXTRACT_HELPER(crbB, 11, 5);
373
/* SPR / TBL */
374
EXTRACT_HELPER(_SPR, 11, 10);
375
static always_inline uint32_t SPR (uint32_t opcode)
376
{
377
    uint32_t sprn = _SPR(opcode);
378

    
379
    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
380
}
381
/***                              Get constants                            ***/
382
EXTRACT_HELPER(IMM, 12, 8);
383
/* 16 bits signed immediate value */
384
EXTRACT_SHELPER(SIMM, 0, 16);
385
/* 16 bits unsigned immediate value */
386
EXTRACT_HELPER(UIMM, 0, 16);
387
/* Bit count */
388
EXTRACT_HELPER(NB, 11, 5);
389
/* Shift count */
390
EXTRACT_HELPER(SH, 11, 5);
391
/* Mask start */
392
EXTRACT_HELPER(MB, 6, 5);
393
/* Mask end */
394
EXTRACT_HELPER(ME, 1, 5);
395
/* Trap operand */
396
EXTRACT_HELPER(TO, 21, 5);
397

    
398
EXTRACT_HELPER(CRM, 12, 8);
399
EXTRACT_HELPER(FM, 17, 8);
400
EXTRACT_HELPER(SR, 16, 4);
401
EXTRACT_HELPER(FPIMM, 12, 4);
402

    
403
/***                            Jump target decoding                       ***/
404
/* Displacement */
405
EXTRACT_SHELPER(d, 0, 16);
406
/* Immediate address */
407
static always_inline target_ulong LI (uint32_t opcode)
408
{
409
    return (opcode >> 0) & 0x03FFFFFC;
410
}
411

    
412
static always_inline uint32_t BD (uint32_t opcode)
413
{
414
    return (opcode >> 0) & 0xFFFC;
415
}
416

    
417
EXTRACT_HELPER(BO, 21, 5);
418
EXTRACT_HELPER(BI, 16, 5);
419
/* Absolute/relative address */
420
EXTRACT_HELPER(AA, 1, 1);
421
/* Link */
422
EXTRACT_HELPER(LK, 0, 1);
423

    
424
/* Create a mask between <start> and <end> bits */
425
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
426
{
427
    target_ulong ret;
428

    
429
#if defined(TARGET_PPC64)
430
    if (likely(start == 0)) {
431
        ret = UINT64_MAX << (63 - end);
432
    } else if (likely(end == 63)) {
433
        ret = UINT64_MAX >> start;
434
    }
435
#else
436
    if (likely(start == 0)) {
437
        ret = UINT32_MAX << (31  - end);
438
    } else if (likely(end == 31)) {
439
        ret = UINT32_MAX >> start;
440
    }
441
#endif
442
    else {
443
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
444
            (((target_ulong)(-1ULL) >> (end)) >> 1);
445
        if (unlikely(start > end))
446
            return ~ret;
447
    }
448

    
449
    return ret;
450
}
451

    
452
/*****************************************************************************/
453
/* PowerPC Instructions types definitions                                    */
454
enum {
455
    PPC_NONE           = 0x0000000000000000ULL,
456
    /* PowerPC base instructions set                                         */
457
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
458
    /*   integer operations instructions                                     */
459
#define PPC_INTEGER PPC_INSNS_BASE
460
    /*   flow control instructions                                           */
461
#define PPC_FLOW    PPC_INSNS_BASE
462
    /*   virtual memory instructions                                         */
463
#define PPC_MEM     PPC_INSNS_BASE
464
    /*   ld/st with reservation instructions                                 */
465
#define PPC_RES     PPC_INSNS_BASE
466
    /*   spr/msr access instructions                                         */
467
#define PPC_MISC    PPC_INSNS_BASE
468
    /* Deprecated instruction sets                                           */
469
    /*   Original POWER instruction set                                      */
470
    PPC_POWER          = 0x0000000000000002ULL,
471
    /*   POWER2 instruction set extension                                    */
472
    PPC_POWER2         = 0x0000000000000004ULL,
473
    /*   Power RTC support                                                   */
474
    PPC_POWER_RTC      = 0x0000000000000008ULL,
475
    /*   Power-to-PowerPC bridge (601)                                       */
476
    PPC_POWER_BR       = 0x0000000000000010ULL,
477
    /* 64 bits PowerPC instruction set                                       */
478
    PPC_64B            = 0x0000000000000020ULL,
479
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
480
    PPC_64BX           = 0x0000000000000040ULL,
481
    /*   64 bits hypervisor extensions                                       */
482
    PPC_64H            = 0x0000000000000080ULL,
483
    /*   New wait instruction (PowerPC 2.0x)                                 */
484
    PPC_WAIT           = 0x0000000000000100ULL,
485
    /*   Time base mftb instruction                                          */
486
    PPC_MFTB           = 0x0000000000000200ULL,
487

    
488
    /* Fixed-point unit extensions                                           */
489
    /*   PowerPC 602 specific                                                */
490
    PPC_602_SPEC       = 0x0000000000000400ULL,
491
    /*   isel instruction                                                    */
492
    PPC_ISEL           = 0x0000000000000800ULL,
493
    /*   popcntb instruction                                                 */
494
    PPC_POPCNTB        = 0x0000000000001000ULL,
495
    /*   string load / store                                                 */
496
    PPC_STRING         = 0x0000000000002000ULL,
497

    
498
    /* Floating-point unit extensions                                        */
499
    /*   Optional floating point instructions                                */
500
    PPC_FLOAT          = 0x0000000000010000ULL,
501
    /* New floating-point extensions (PowerPC 2.0x)                          */
502
    PPC_FLOAT_EXT      = 0x0000000000020000ULL,
503
    PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
504
    PPC_FLOAT_FRES     = 0x0000000000080000ULL,
505
    PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
506
    PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
507
    PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
508
    PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,
509

    
510
    /* Vector/SIMD extensions                                                */
511
    /*   Altivec support                                                     */
512
    PPC_ALTIVEC        = 0x0000000001000000ULL,
513
    /*   PowerPC 2.03 SPE extension                                          */
514
    PPC_SPE            = 0x0000000002000000ULL,
515
    /*   PowerPC 2.03 SPE floating-point extension                           */
516
    PPC_SPEFPU         = 0x0000000004000000ULL,
517

    
518
    /* Optional memory control instructions                                  */
519
    PPC_MEM_TLBIA      = 0x0000000010000000ULL,
520
    PPC_MEM_TLBIE      = 0x0000000020000000ULL,
521
    PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
522
    /*   sync instruction                                                    */
523
    PPC_MEM_SYNC       = 0x0000000080000000ULL,
524
    /*   eieio instruction                                                   */
525
    PPC_MEM_EIEIO      = 0x0000000100000000ULL,
526

    
527
    /* Cache control instructions                                            */
528
    PPC_CACHE          = 0x0000000200000000ULL,
529
    /*   icbi instruction                                                    */
530
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
531
    /*   dcbz instruction with fixed cache line size                         */
532
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
533
    /*   dcbz instruction with tunable cache line size                       */
534
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
535
    /*   dcba instruction                                                    */
536
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
537
    /*   Freescale cache locking instructions                                */
538
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
539

    
540
    /* MMU related extensions                                                */
541
    /*   external control instructions                                       */
542
    PPC_EXTERN         = 0x0000010000000000ULL,
543
    /*   segment register access instructions                                */
544
    PPC_SEGMENT        = 0x0000020000000000ULL,
545
    /*   PowerPC 6xx TLB management instructions                             */
546
    PPC_6xx_TLB        = 0x0000040000000000ULL,
547
    /* PowerPC 74xx TLB management instructions                              */
548
    PPC_74xx_TLB       = 0x0000080000000000ULL,
549
    /*   PowerPC 40x TLB management instructions                             */
550
    PPC_40x_TLB        = 0x0000100000000000ULL,
551
    /*   segment register access instructions for PowerPC 64 "bridge"        */
552
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
553
    /*   SLB management                                                      */
554
    PPC_SLBI           = 0x0000400000000000ULL,
555

    
556
    /* Embedded PowerPC dedicated instructions                               */
557
    PPC_WRTEE          = 0x0001000000000000ULL,
558
    /* PowerPC 40x exception model                                           */
559
    PPC_40x_EXCP       = 0x0002000000000000ULL,
560
    /* PowerPC 405 Mac instructions                                          */
561
    PPC_405_MAC        = 0x0004000000000000ULL,
562
    /* PowerPC 440 specific instructions                                     */
563
    PPC_440_SPEC       = 0x0008000000000000ULL,
564
    /* BookE (embedded) PowerPC specification                                */
565
    PPC_BOOKE          = 0x0010000000000000ULL,
566
    /* mfapidi instruction                                                   */
567
    PPC_MFAPIDI        = 0x0020000000000000ULL,
568
    /* tlbiva instruction                                                    */
569
    PPC_TLBIVA         = 0x0040000000000000ULL,
570
    /* tlbivax instruction                                                   */
571
    PPC_TLBIVAX        = 0x0080000000000000ULL,
572
    /* PowerPC 4xx dedicated instructions                                    */
573
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
574
    /* PowerPC 40x ibct instructions                                         */
575
    PPC_40x_ICBT       = 0x0200000000000000ULL,
576
    /* rfmci is not implemented in all BookE PowerPC                         */
577
    PPC_RFMCI          = 0x0400000000000000ULL,
578
    /* rfdi instruction                                                      */
579
    PPC_RFDI           = 0x0800000000000000ULL,
580
    /* DCR accesses                                                          */
581
    PPC_DCR            = 0x1000000000000000ULL,
582
    /* DCR extended accesse                                                  */
583
    PPC_DCRX           = 0x2000000000000000ULL,
584
    /* user-mode DCR access, implemented in PowerPC 460                      */
585
    PPC_DCRUX          = 0x4000000000000000ULL,
586
};
587

    
588
/*****************************************************************************/
589
/* PowerPC instructions table                                                */
590
#if HOST_LONG_BITS == 64
591
#define OPC_ALIGN 8
592
#else
593
#define OPC_ALIGN 4
594
#endif
595
#if defined(__APPLE__)
596
#define OPCODES_SECTION                                                       \
597
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
598
#else
599
#define OPCODES_SECTION                                                       \
600
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
601
#endif
602

    
603
#if defined(DO_PPC_STATISTICS)
604
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
605
OPCODES_SECTION opcode_t opc_##name = {                                       \
606
    .opc1 = op1,                                                              \
607
    .opc2 = op2,                                                              \
608
    .opc3 = op3,                                                              \
609
    .pad  = { 0, },                                                           \
610
    .handler = {                                                              \
611
        .inval   = invl,                                                      \
612
        .type = _typ,                                                         \
613
        .handler = &gen_##name,                                               \
614
        .oname = stringify(name),                                             \
615
    },                                                                        \
616
    .oname = stringify(name),                                                 \
617
}
618
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
619
OPCODES_SECTION opcode_t opc_##name = {                                       \
620
    .opc1 = op1,                                                              \
621
    .opc2 = op2,                                                              \
622
    .opc3 = op3,                                                              \
623
    .pad  = { 0, },                                                           \
624
    .handler = {                                                              \
625
        .inval   = invl,                                                      \
626
        .type = _typ,                                                         \
627
        .handler = &gen_##name,                                               \
628
        .oname = onam,                                                        \
629
    },                                                                        \
630
    .oname = onam,                                                            \
631
}
632
#else
633
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
634
OPCODES_SECTION opcode_t opc_##name = {                                       \
635
    .opc1 = op1,                                                              \
636
    .opc2 = op2,                                                              \
637
    .opc3 = op3,                                                              \
638
    .pad  = { 0, },                                                           \
639
    .handler = {                                                              \
640
        .inval   = invl,                                                      \
641
        .type = _typ,                                                         \
642
        .handler = &gen_##name,                                               \
643
    },                                                                        \
644
    .oname = stringify(name),                                                 \
645
}
646
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
647
OPCODES_SECTION opcode_t opc_##name = {                                       \
648
    .opc1 = op1,                                                              \
649
    .opc2 = op2,                                                              \
650
    .opc3 = op3,                                                              \
651
    .pad  = { 0, },                                                           \
652
    .handler = {                                                              \
653
        .inval   = invl,                                                      \
654
        .type = _typ,                                                         \
655
        .handler = &gen_##name,                                               \
656
    },                                                                        \
657
    .oname = onam,                                                            \
658
}
659
#endif
660

    
661
#define GEN_OPCODE_MARK(name)                                                 \
662
OPCODES_SECTION opcode_t opc_##name = {                                       \
663
    .opc1 = 0xFF,                                                             \
664
    .opc2 = 0xFF,                                                             \
665
    .opc3 = 0xFF,                                                             \
666
    .pad  = { 0, },                                                           \
667
    .handler = {                                                              \
668
        .inval   = 0x00000000,                                                \
669
        .type = 0x00,                                                         \
670
        .handler = NULL,                                                      \
671
    },                                                                        \
672
    .oname = stringify(name),                                                 \
673
}
674

    
675
/* SPR load/store helpers */
676
static always_inline void gen_load_spr(TCGv t, int reg)
677
{
678
    tcg_gen_ld_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
679
}
680

    
681
static always_inline void gen_store_spr(int reg, TCGv t)
682
{
683
    tcg_gen_st_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
684
}
685

    
686
/* Start opcode list */
687
GEN_OPCODE_MARK(start);
688

    
689
/* Invalid instruction */
690
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
691
{
692
    GEN_EXCP_INVAL(ctx);
693
}
694

    
695
static opc_handler_t invalid_handler = {
696
    .inval   = 0xFFFFFFFF,
697
    .type    = PPC_NONE,
698
    .handler = gen_invalid,
699
};
700

    
701
/***                           Integer comparison                          ***/
702

    
703
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
704
{
705
    int l1, l2, l3;
706

    
707
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
708
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
709
    tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
710

    
711
    l1 = gen_new_label();
712
    l2 = gen_new_label();
713
    l3 = gen_new_label();
714
    if (s) {
715
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
716
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
717
    } else {
718
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
719
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
720
    }
721
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
722
    tcg_gen_br(l3);
723
    gen_set_label(l1);
724
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
725
    tcg_gen_br(l3);
726
    gen_set_label(l2);
727
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
728
    gen_set_label(l3);
729
}
730

    
731
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
732
{
733
    TCGv t0 = tcg_const_local_tl(arg1);
734
    gen_op_cmp(arg0, t0, s, crf);
735
    tcg_temp_free(t0);
736
}
737

    
738
#if defined(TARGET_PPC64)
739
static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
740
{
741
    TCGv t0, t1;
742
    t0 = tcg_temp_local_new();
743
    t1 = tcg_temp_local_new();
744
    if (s) {
745
        tcg_gen_ext32s_tl(t0, arg0);
746
        tcg_gen_ext32s_tl(t1, arg1);
747
    } else {
748
        tcg_gen_ext32u_tl(t0, arg0);
749
        tcg_gen_ext32u_tl(t1, arg1);
750
    }
751
    gen_op_cmp(t0, t1, s, crf);
752
    tcg_temp_free(t1);
753
    tcg_temp_free(t0);
754
}
755

    
756
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
757
{
758
    TCGv t0 = tcg_const_local_tl(arg1);
759
    gen_op_cmp32(arg0, t0, s, crf);
760
    tcg_temp_free(t0);
761
}
762
#endif
763

    
764
static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
765
{
766
#if defined(TARGET_PPC64)
767
    if (!(ctx->sf_mode))
768
        gen_op_cmpi32(reg, 0, 1, 0);
769
    else
770
#endif
771
        gen_op_cmpi(reg, 0, 1, 0);
772
}
773

    
774
/* cmp */
775
GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
776
{
777
#if defined(TARGET_PPC64)
778
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
779
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
780
                     1, crfD(ctx->opcode));
781
    else
782
#endif
783
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
784
                   1, crfD(ctx->opcode));
785
}
786

    
787
/* cmpi */
788
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
789
{
790
#if defined(TARGET_PPC64)
791
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
792
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
793
                      1, crfD(ctx->opcode));
794
    else
795
#endif
796
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
797
                    1, crfD(ctx->opcode));
798
}
799

    
800
/* cmpl */
801
GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
802
{
803
#if defined(TARGET_PPC64)
804
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
805
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
806
                     0, crfD(ctx->opcode));
807
    else
808
#endif
809
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
810
                   0, crfD(ctx->opcode));
811
}
812

    
813
/* cmpli */
814
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
815
{
816
#if defined(TARGET_PPC64)
817
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
818
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
819
                      0, crfD(ctx->opcode));
820
    else
821
#endif
822
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
823
                    0, crfD(ctx->opcode));
824
}
825

    
826
/* isel (PowerPC 2.03 specification) */
827
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
828
{
829
    int l1, l2;
830
    uint32_t bi = rC(ctx->opcode);
831
    uint32_t mask;
832
    TCGv_i32 t0;
833

    
834
    l1 = gen_new_label();
835
    l2 = gen_new_label();
836

    
837
    mask = 1 << (3 - (bi & 0x03));
838
    t0 = tcg_temp_new_i32();
839
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
840
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
841
    if (rA(ctx->opcode) == 0)
842
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
843
    else
844
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
845
    tcg_gen_br(l2);
846
    gen_set_label(l1);
847
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
848
    gen_set_label(l2);
849
    tcg_temp_free_i32(t0);
850
}
851

    
852
/***                           Integer arithmetic                          ***/
853

    
854
static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
855
{
856
    int l1;
857
    TCGv t0;
858

    
859
    l1 = gen_new_label();
860
    /* Start with XER OV disabled, the most likely case */
861
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
862
    t0 = tcg_temp_local_new();
863
    tcg_gen_xor_tl(t0, arg0, arg1);
864
#if defined(TARGET_PPC64)
865
    if (!ctx->sf_mode)
866
        tcg_gen_ext32s_tl(t0, t0);
867
#endif
868
    if (sub)
869
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
870
    else
871
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
872
    tcg_gen_xor_tl(t0, arg1, arg2);
873
#if defined(TARGET_PPC64)
874
    if (!ctx->sf_mode)
875
        tcg_gen_ext32s_tl(t0, t0);
876
#endif
877
    if (sub)
878
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
879
    else
880
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
881
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
882
    gen_set_label(l1);
883
    tcg_temp_free(t0);
884
}
885

    
886
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
887
{
888
    int l1 = gen_new_label();
889

    
890
#if defined(TARGET_PPC64)
891
    if (!(ctx->sf_mode)) {
892
        TCGv t0, t1;
893
        t0 = tcg_temp_new();
894
        t1 = tcg_temp_new();
895

    
896
        tcg_gen_ext32u_tl(t0, arg1);
897
        tcg_gen_ext32u_tl(t1, arg2);
898
        if (sub) {
899
            tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
900
        } else {
901
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
902
        }
903
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
904
        gen_set_label(l1);
905
        tcg_temp_free(t0);
906
        tcg_temp_free(t1);
907
    } else
908
#endif
909
    {
910
        if (sub) {
911
            tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
912
        } else {
913
            tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
914
        }
915
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
916
        gen_set_label(l1);
917
    }
918
}
919

    
920
/* Common add function */
921
static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
922
                                           int add_ca, int compute_ca, int compute_ov)
923
{
924
    TCGv t0, t1;
925

    
926
    if ((!compute_ca && !compute_ov) ||
927
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
928
        t0 = ret;
929
    } else {
930
        t0 = tcg_temp_local_new();
931
    }
932

    
933
    if (add_ca) {
934
        t1 = tcg_temp_local_new();
935
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
936
        tcg_gen_shri_tl(t1, t1, XER_CA);
937
    }
938

    
939
    if (compute_ca && compute_ov) {
940
        /* Start with XER CA and OV disabled, the most likely case */
941
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
942
    } else if (compute_ca) {
943
        /* Start with XER CA disabled, the most likely case */
944
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
945
    } else if (compute_ov) {
946
        /* Start with XER OV disabled, the most likely case */
947
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
948
    }
949

    
950
    tcg_gen_add_tl(t0, arg1, arg2);
951

    
952
    if (compute_ca) {
953
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
954
    }
955
    if (add_ca) {
956
        tcg_gen_add_tl(t0, t0, t1);
957
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
958
        tcg_temp_free(t1);
959
    }
960
    if (compute_ov) {
961
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
962
    }
963

    
964
    if (unlikely(Rc(ctx->opcode) != 0))
965
        gen_set_Rc0(ctx, t0);
966

    
967
    if (!TCGV_EQUAL(t0, ret)) {
968
        tcg_gen_mov_tl(ret, t0);
969
        tcg_temp_free(t0);
970
    }
971
}
972
/* Add functions with two operands */
973
#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
974
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER)                  \
975
{                                                                             \
976
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
977
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
978
                     add_ca, compute_ca, compute_ov);                         \
979
}
980
/* Add functions with one operand and one immediate */
981
#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
982
                                add_ca, compute_ca, compute_ov)               \
983
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER)                  \
984
{                                                                             \
985
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
986
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
987
                     cpu_gpr[rA(ctx->opcode)], t0,                            \
988
                     add_ca, compute_ca, compute_ov);                         \
989
    tcg_temp_free(t0);                                                        \
990
}
991

    
992
/* add  add.  addo  addo. */
993
GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
994
GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
995
/* addc  addc.  addco  addco. */
996
GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
997
GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
998
/* adde  adde.  addeo  addeo. */
999
GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1000
GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1001
/* addme  addme.  addmeo  addmeo.  */
1002
GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1003
GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1004
/* addze  addze.  addzeo  addzeo.*/
1005
GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1006
GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1007
/* addi */
1008
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1009
{
1010
    target_long simm = SIMM(ctx->opcode);
1011

    
1012
    if (rA(ctx->opcode) == 0) {
1013
        /* li case */
1014
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1015
    } else {
1016
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1017
    }
1018
}
1019
/* addic  addic.*/
1020
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1021
                                        int compute_Rc0)
1022
{
1023
    target_long simm = SIMM(ctx->opcode);
1024

    
1025
    /* Start with XER CA and OV disabled, the most likely case */
1026
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1027

    
1028
    if (likely(simm != 0)) {
1029
        TCGv t0 = tcg_temp_local_new();
1030
        tcg_gen_addi_tl(t0, arg1, simm);
1031
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1032
        tcg_gen_mov_tl(ret, t0);
1033
        tcg_temp_free(t0);
1034
    } else {
1035
        tcg_gen_mov_tl(ret, arg1);
1036
    }
1037
    if (compute_Rc0) {
1038
        gen_set_Rc0(ctx, ret);
1039
    }
1040
}
1041
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1042
{
1043
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1044
}
1045
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1046
{
1047
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1048
}
1049
/* addis */
1050
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1051
{
1052
    target_long simm = SIMM(ctx->opcode);
1053

    
1054
    if (rA(ctx->opcode) == 0) {
1055
        /* lis case */
1056
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1057
    } else {
1058
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1059
    }
1060
}
1061

    
1062
static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1063
                                             int sign, int compute_ov)
1064
{
1065
    int l1 = gen_new_label();
1066
    int l2 = gen_new_label();
1067
    TCGv_i32 t0 = tcg_temp_local_new_i32();
1068
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1069

    
1070
    tcg_gen_trunc_tl_i32(t0, arg1);
1071
    tcg_gen_trunc_tl_i32(t1, arg2);
1072
    tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1073
    if (sign) {
1074
        int l3 = gen_new_label();
1075
        tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1076
        tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1077
        gen_set_label(l3);
1078
        tcg_gen_div_i32(t0, t0, t1);
1079
    } else {
1080
        tcg_gen_divu_i32(t0, t0, t1);
1081
    }
1082
    if (compute_ov) {
1083
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1084
    }
1085
    tcg_gen_br(l2);
1086
    gen_set_label(l1);
1087
    if (sign) {
1088
        tcg_gen_sari_i32(t0, t0, 31);
1089
    } else {
1090
        tcg_gen_movi_i32(t0, 0);
1091
    }
1092
    if (compute_ov) {
1093
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1094
    }
1095
    gen_set_label(l2);
1096
    tcg_gen_extu_i32_tl(ret, t0);
1097
    tcg_temp_free_i32(t0);
1098
    tcg_temp_free_i32(t1);
1099
    if (unlikely(Rc(ctx->opcode) != 0))
1100
        gen_set_Rc0(ctx, ret);
1101
}
1102
/* Div functions */
1103
#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1104
GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)                  \
1105
{                                                                             \
1106
    gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1107
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1108
                     sign, compute_ov);                                       \
1109
}
1110
/* divwu  divwu.  divwuo  divwuo.   */
1111
GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1112
GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1113
/* divw  divw.  divwo  divwo.   */
1114
GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1115
GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1116
#if defined(TARGET_PPC64)
1117
static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1118
                                             int sign, int compute_ov)
1119
{
1120
    int l1 = gen_new_label();
1121
    int l2 = gen_new_label();
1122

    
1123
    tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1124
    if (sign) {
1125
        int l3 = gen_new_label();
1126
        tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1127
        tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1128
        gen_set_label(l3);
1129
        tcg_gen_div_i64(ret, arg1, arg2);
1130
    } else {
1131
        tcg_gen_divu_i64(ret, arg1, arg2);
1132
    }
1133
    if (compute_ov) {
1134
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1135
    }
1136
    tcg_gen_br(l2);
1137
    gen_set_label(l1);
1138
    if (sign) {
1139
        tcg_gen_sari_i64(ret, arg1, 63);
1140
    } else {
1141
        tcg_gen_movi_i64(ret, 0);
1142
    }
1143
    if (compute_ov) {
1144
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1145
    }
1146
    gen_set_label(l2);
1147
    if (unlikely(Rc(ctx->opcode) != 0))
1148
        gen_set_Rc0(ctx, ret);
1149
}
1150
#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1151
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1152
{                                                                             \
1153
    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1154
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1155
                      sign, compute_ov);                                      \
1156
}
1157
/* divwu  divwu.  divwuo  divwuo.   */
1158
GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1159
GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1160
/* divw  divw.  divwo  divwo.   */
1161
GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1162
GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1163
#endif
1164

    
1165
/* mulhw  mulhw. */
1166
GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1167
{
1168
    TCGv_i64 t0, t1;
1169

    
1170
    t0 = tcg_temp_new_i64();
1171
    t1 = tcg_temp_new_i64();
1172
#if defined(TARGET_PPC64)
1173
    tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1174
    tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1175
    tcg_gen_mul_i64(t0, t0, t1);
1176
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1177
#else
1178
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1179
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1180
    tcg_gen_mul_i64(t0, t0, t1);
1181
    tcg_gen_shri_i64(t0, t0, 32);
1182
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1183
#endif
1184
    tcg_temp_free_i64(t0);
1185
    tcg_temp_free_i64(t1);
1186
    if (unlikely(Rc(ctx->opcode) != 0))
1187
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1188
}
1189
/* mulhwu  mulhwu.  */
1190
GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1191
{
1192
    TCGv_i64 t0, t1;
1193

    
1194
    t0 = tcg_temp_new_i64();
1195
    t1 = tcg_temp_new_i64();
1196
#if defined(TARGET_PPC64)
1197
    tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1198
    tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1199
    tcg_gen_mul_i64(t0, t0, t1);
1200
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1201
#else
1202
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1203
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1204
    tcg_gen_mul_i64(t0, t0, t1);
1205
    tcg_gen_shri_i64(t0, t0, 32);
1206
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1207
#endif
1208
    tcg_temp_free_i64(t0);
1209
    tcg_temp_free_i64(t1);
1210
    if (unlikely(Rc(ctx->opcode) != 0))
1211
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1212
}
1213
/* mullw  mullw. */
1214
GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1215
{
1216
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1217
                   cpu_gpr[rB(ctx->opcode)]);
1218
    tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1219
    if (unlikely(Rc(ctx->opcode) != 0))
1220
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1221
}
1222
/* mullwo  mullwo. */
1223
GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1224
{
1225
    int l1;
1226
    TCGv_i64 t0, t1;
1227

    
1228
    t0 = tcg_temp_new_i64();
1229
    t1 = tcg_temp_new_i64();
1230
    l1 = gen_new_label();
1231
    /* Start with XER OV disabled, the most likely case */
1232
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1233
#if defined(TARGET_PPC64)
1234
    tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1235
    tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1236
#else
1237
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1238
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1239
#endif
1240
    tcg_gen_mul_i64(t0, t0, t1);
1241
#if defined(TARGET_PPC64)
1242
    tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1243
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1244
#else
1245
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1246
    tcg_gen_ext32s_i64(t1, t0);
1247
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1248
#endif
1249
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1250
    gen_set_label(l1);
1251
    tcg_temp_free_i64(t0);
1252
    tcg_temp_free_i64(t1);
1253
    if (unlikely(Rc(ctx->opcode) != 0))
1254
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1255
}
1256
/* mulli */
1257
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1258
{
1259
    tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1260
                    SIMM(ctx->opcode));
1261
}
1262
#if defined(TARGET_PPC64)
1263
#define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
1264
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1265
{                                                                             \
1266
    gen_helper_##name (cpu_gpr[rD(ctx->opcode)],                              \
1267
                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);   \
1268
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1269
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1270
}
1271
/* mulhd  mulhd. */
1272
GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1273
/* mulhdu  mulhdu. */
1274
GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1275
/* mulld  mulld. */
1276
GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1277
{
1278
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1279
                   cpu_gpr[rB(ctx->opcode)]);
1280
    if (unlikely(Rc(ctx->opcode) != 0))
1281
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1282
}
1283
/* mulldo  mulldo. */
1284
GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1285
#endif
1286

    
1287
/* neg neg. nego nego. */
1288
static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1289
{
1290
    int l1 = gen_new_label();
1291
    int l2 = gen_new_label();
1292
    TCGv t0 = tcg_temp_local_new();
1293
#if defined(TARGET_PPC64)
1294
    if (ctx->sf_mode) {
1295
        tcg_gen_mov_tl(t0, arg1);
1296
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1297
    } else
1298
#endif
1299
    {
1300
        tcg_gen_ext32s_tl(t0, arg1);
1301
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1302
    }
1303
    tcg_gen_neg_tl(ret, arg1);
1304
    if (ov_check) {
1305
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1306
    }
1307
    tcg_gen_br(l2);
1308
    gen_set_label(l1);
1309
    tcg_gen_mov_tl(ret, t0);
1310
    if (ov_check) {
1311
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1312
    }
1313
    gen_set_label(l2);
1314
    tcg_temp_free(t0);
1315
    if (unlikely(Rc(ctx->opcode) != 0))
1316
        gen_set_Rc0(ctx, ret);
1317
}
1318
GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1319
{
1320
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1321
}
1322
GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
1323
{
1324
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1325
}
1326

    
1327
/* Common subf function */
1328
static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1329
                                            int add_ca, int compute_ca, int compute_ov)
1330
{
1331
    TCGv t0, t1;
1332

    
1333
    if ((!compute_ca && !compute_ov) ||
1334
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1335
        t0 = ret;
1336
    } else {
1337
        t0 = tcg_temp_local_new();
1338
    }
1339

    
1340
    if (add_ca) {
1341
        t1 = tcg_temp_local_new();
1342
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1343
        tcg_gen_shri_tl(t1, t1, XER_CA);
1344
    }
1345

    
1346
    if (compute_ca && compute_ov) {
1347
        /* Start with XER CA and OV disabled, the most likely case */
1348
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1349
    } else if (compute_ca) {
1350
        /* Start with XER CA disabled, the most likely case */
1351
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1352
    } else if (compute_ov) {
1353
        /* Start with XER OV disabled, the most likely case */
1354
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1355
    }
1356

    
1357
    if (add_ca) {
1358
        tcg_gen_not_tl(t0, arg1);
1359
        tcg_gen_add_tl(t0, t0, arg2);
1360
        gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1361
        tcg_gen_add_tl(t0, t0, t1);
1362
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
1363
        tcg_temp_free(t1);
1364
    } else {
1365
        tcg_gen_sub_tl(t0, arg2, arg1);
1366
        if (compute_ca) {
1367
            gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1368
        }
1369
    }
1370
    if (compute_ov) {
1371
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1372
    }
1373

    
1374
    if (unlikely(Rc(ctx->opcode) != 0))
1375
        gen_set_Rc0(ctx, t0);
1376

    
1377
    if (!TCGV_EQUAL(t0, ret)) {
1378
        tcg_gen_mov_tl(ret, t0);
1379
        tcg_temp_free(t0);
1380
    }
1381
}
1382
/* Sub functions with Two operands functions */
1383
#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1384
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER)                  \
1385
{                                                                             \
1386
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1387
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1388
                      add_ca, compute_ca, compute_ov);                        \
1389
}
1390
/* Sub functions with one operand and one immediate */
1391
#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1392
                                add_ca, compute_ca, compute_ov)               \
1393
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER)                  \
1394
{                                                                             \
1395
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
1396
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1397
                      cpu_gpr[rA(ctx->opcode)], t0,                           \
1398
                      add_ca, compute_ca, compute_ov);                        \
1399
    tcg_temp_free(t0);                                                        \
1400
}
1401
/* subf  subf.  subfo  subfo. */
1402
GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1403
GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1404
/* subfc  subfc.  subfco  subfco. */
1405
GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1406
GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1407
/* subfe  subfe.  subfeo  subfo. */
1408
GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1409
GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1410
/* subfme  subfme.  subfmeo  subfmeo.  */
1411
GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1412
GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1413
/* subfze  subfze.  subfzeo  subfzeo.*/
1414
GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1415
GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1416
/* subfic */
1417
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1418
{
1419
    /* Start with XER CA and OV disabled, the most likely case */
1420
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1421
    TCGv t0 = tcg_temp_local_new();
1422
    TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1423
    tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1424
    gen_op_arith_compute_ca(ctx, t0, t1, 1);
1425
    tcg_temp_free(t1);
1426
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1427
    tcg_temp_free(t0);
1428
}
1429

    
1430
/***                            Integer logical                            ***/
1431
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1432
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
1433
{                                                                             \
1434
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1435
       cpu_gpr[rB(ctx->opcode)]);                                             \
1436
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1437
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1438
}
1439

    
1440
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1441
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1442
{                                                                             \
1443
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1444
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1445
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1446
}
1447

    
1448
/* and & and. */
1449
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1450
/* andc & andc. */
1451
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1452
/* andi. */
1453
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1454
{
1455
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1456
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1457
}
1458
/* andis. */
1459
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1460
{
1461
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1462
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1463
}
1464
/* cntlzw */
1465
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1466
{
1467
    gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1468
    if (unlikely(Rc(ctx->opcode) != 0))
1469
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1470
}
1471
/* eqv & eqv. */
1472
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1473
/* extsb & extsb. */
1474
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1475
/* extsh & extsh. */
1476
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1477
/* nand & nand. */
1478
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1479
/* nor & nor. */
1480
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1481
/* or & or. */
1482
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1483
{
1484
    int rs, ra, rb;
1485

    
1486
    rs = rS(ctx->opcode);
1487
    ra = rA(ctx->opcode);
1488
    rb = rB(ctx->opcode);
1489
    /* Optimisation for mr. ri case */
1490
    if (rs != ra || rs != rb) {
1491
        if (rs != rb)
1492
            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1493
        else
1494
            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1495
        if (unlikely(Rc(ctx->opcode) != 0))
1496
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1497
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1498
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1499
#if defined(TARGET_PPC64)
1500
    } else {
1501
        int prio = 0;
1502

    
1503
        switch (rs) {
1504
        case 1:
1505
            /* Set process priority to low */
1506
            prio = 2;
1507
            break;
1508
        case 6:
1509
            /* Set process priority to medium-low */
1510
            prio = 3;
1511
            break;
1512
        case 2:
1513
            /* Set process priority to normal */
1514
            prio = 4;
1515
            break;
1516
#if !defined(CONFIG_USER_ONLY)
1517
        case 31:
1518
            if (ctx->mem_idx > 0) {
1519
                /* Set process priority to very low */
1520
                prio = 1;
1521
            }
1522
            break;
1523
        case 5:
1524
            if (ctx->mem_idx > 0) {
1525
                /* Set process priority to medium-hight */
1526
                prio = 5;
1527
            }
1528
            break;
1529
        case 3:
1530
            if (ctx->mem_idx > 0) {
1531
                /* Set process priority to high */
1532
                prio = 6;
1533
            }
1534
            break;
1535
        case 7:
1536
            if (ctx->mem_idx > 1) {
1537
                /* Set process priority to very high */
1538
                prio = 7;
1539
            }
1540
            break;
1541
#endif
1542
        default:
1543
            /* nop */
1544
            break;
1545
        }
1546
        if (prio) {
1547
            TCGv t0 = tcg_temp_new();
1548
            gen_load_spr(t0, SPR_PPR);
1549
            tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1550
            tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1551
            gen_store_spr(SPR_PPR, t0);
1552
            tcg_temp_free(t0);
1553
        }
1554
#endif
1555
    }
1556
}
1557
/* orc & orc. */
1558
GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1559
/* xor & xor. */
1560
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1561
{
1562
    /* Optimisation for "set to zero" case */
1563
    if (rS(ctx->opcode) != rB(ctx->opcode))
1564
        tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1565
    else
1566
        tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1567
    if (unlikely(Rc(ctx->opcode) != 0))
1568
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1569
}
1570
/* ori */
1571
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1572
{
1573
    target_ulong uimm = UIMM(ctx->opcode);
1574

    
1575
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1576
        /* NOP */
1577
        /* XXX: should handle special NOPs for POWER series */
1578
        return;
1579
    }
1580
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1581
}
1582
/* oris */
1583
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1584
{
1585
    target_ulong uimm = UIMM(ctx->opcode);
1586

    
1587
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1588
        /* NOP */
1589
        return;
1590
    }
1591
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1592
}
1593
/* xori */
1594
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1595
{
1596
    target_ulong uimm = UIMM(ctx->opcode);
1597

    
1598
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1599
        /* NOP */
1600
        return;
1601
    }
1602
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1603
}
1604
/* xoris */
1605
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1606
{
1607
    target_ulong uimm = UIMM(ctx->opcode);
1608

    
1609
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1610
        /* NOP */
1611
        return;
1612
    }
1613
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1614
}
1615
/* popcntb : PowerPC 2.03 specification */
1616
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1617
{
1618
#if defined(TARGET_PPC64)
1619
    if (ctx->sf_mode)
1620
        gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1621
    else
1622
#endif
1623
        gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1624
}
1625

    
1626
#if defined(TARGET_PPC64)
1627
/* extsw & extsw. */
1628
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1629
/* cntlzd */
1630
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1631
{
1632
    gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1633
    if (unlikely(Rc(ctx->opcode) != 0))
1634
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1635
}
1636
#endif
1637

    
1638
/***                             Integer rotate                            ***/
1639
/* rlwimi & rlwimi. */
1640
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1641
{
1642
    uint32_t mb, me, sh;
1643

    
1644
    mb = MB(ctx->opcode);
1645
    me = ME(ctx->opcode);
1646
    sh = SH(ctx->opcode);
1647
    if (likely(sh == 0 && mb == 0 && me == 31)) {
1648
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1649
    } else {
1650
        target_ulong mask;
1651
        TCGv t1;
1652
        TCGv t0 = tcg_temp_new();
1653
#if defined(TARGET_PPC64)
1654
        TCGv_i32 t2 = tcg_temp_new_i32();
1655
        tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1656
        tcg_gen_rotli_i32(t2, t2, sh);
1657
        tcg_gen_extu_i32_i64(t0, t2);
1658
        tcg_temp_free_i32(t2);
1659
#else
1660
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1661
#endif
1662
#if defined(TARGET_PPC64)
1663
        mb += 32;
1664
        me += 32;
1665
#endif
1666
        mask = MASK(mb, me);
1667
        t1 = tcg_temp_new();
1668
        tcg_gen_andi_tl(t0, t0, mask);
1669
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1670
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1671
        tcg_temp_free(t0);
1672
        tcg_temp_free(t1);
1673
    }
1674
    if (unlikely(Rc(ctx->opcode) != 0))
1675
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1676
}
1677
/* rlwinm & rlwinm. */
1678
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1679
{
1680
    uint32_t mb, me, sh;
1681

    
1682
    sh = SH(ctx->opcode);
1683
    mb = MB(ctx->opcode);
1684
    me = ME(ctx->opcode);
1685

    
1686
    if (likely(mb == 0 && me == (31 - sh))) {
1687
        if (likely(sh == 0)) {
1688
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1689
        } else {
1690
            TCGv t0 = tcg_temp_new();
1691
            tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1692
            tcg_gen_shli_tl(t0, t0, sh);
1693
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1694
            tcg_temp_free(t0);
1695
        }
1696
    } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1697
        TCGv t0 = tcg_temp_new();
1698
        tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1699
        tcg_gen_shri_tl(t0, t0, mb);
1700
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1701
        tcg_temp_free(t0);
1702
    } else {
1703
        TCGv t0 = tcg_temp_new();
1704
#if defined(TARGET_PPC64)
1705
        TCGv_i32 t1 = tcg_temp_new_i32();
1706
        tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1707
        tcg_gen_rotli_i32(t1, t1, sh);
1708
        tcg_gen_extu_i32_i64(t0, t1);
1709
        tcg_temp_free_i32(t1);
1710
#else
1711
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1712
#endif
1713
#if defined(TARGET_PPC64)
1714
        mb += 32;
1715
        me += 32;
1716
#endif
1717
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1718
        tcg_temp_free(t0);
1719
    }
1720
    if (unlikely(Rc(ctx->opcode) != 0))
1721
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1722
}
1723
/* rlwnm & rlwnm. */
1724
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1725
{
1726
    uint32_t mb, me;
1727
    TCGv t0;
1728
#if defined(TARGET_PPC64)
1729
    TCGv_i32 t1, t2;
1730
#endif
1731

    
1732
    mb = MB(ctx->opcode);
1733
    me = ME(ctx->opcode);
1734
    t0 = tcg_temp_new();
1735
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1736
#if defined(TARGET_PPC64)
1737
    t1 = tcg_temp_new_i32();
1738
    t2 = tcg_temp_new_i32();
1739
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1740
    tcg_gen_trunc_i64_i32(t2, t0);
1741
    tcg_gen_rotl_i32(t1, t1, t2);
1742
    tcg_gen_extu_i32_i64(t0, t1);
1743
    tcg_temp_free_i32(t1);
1744
    tcg_temp_free_i32(t2);
1745
#else
1746
    tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1747
#endif
1748
    if (unlikely(mb != 0 || me != 31)) {
1749
#if defined(TARGET_PPC64)
1750
        mb += 32;
1751
        me += 32;
1752
#endif
1753
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1754
    } else {
1755
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1756
    }
1757
    tcg_temp_free(t0);
1758
    if (unlikely(Rc(ctx->opcode) != 0))
1759
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1760
}
1761

    
1762
#if defined(TARGET_PPC64)
1763
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1764
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1765
{                                                                             \
1766
    gen_##name(ctx, 0);                                                       \
1767
}                                                                             \
1768
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1769
             PPC_64B)                                                         \
1770
{                                                                             \
1771
    gen_##name(ctx, 1);                                                       \
1772
}
1773
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1774
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1775
{                                                                             \
1776
    gen_##name(ctx, 0, 0);                                                    \
1777
}                                                                             \
1778
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1779
             PPC_64B)                                                         \
1780
{                                                                             \
1781
    gen_##name(ctx, 0, 1);                                                    \
1782
}                                                                             \
1783
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1784
             PPC_64B)                                                         \
1785
{                                                                             \
1786
    gen_##name(ctx, 1, 0);                                                    \
1787
}                                                                             \
1788
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1789
             PPC_64B)                                                         \
1790
{                                                                             \
1791
    gen_##name(ctx, 1, 1);                                                    \
1792
}
1793

    
1794
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1795
                                      uint32_t me, uint32_t sh)
1796
{
1797
    if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1798
        tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1799
    } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1800
        tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1801
    } else {
1802
        TCGv t0 = tcg_temp_new();
1803
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1804
        if (likely(mb == 0 && me == 63)) {
1805
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1806
        } else {
1807
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1808
        }
1809
        tcg_temp_free(t0);
1810
    }
1811
    if (unlikely(Rc(ctx->opcode) != 0))
1812
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1813
}
1814
/* rldicl - rldicl. */
1815
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1816
{
1817
    uint32_t sh, mb;
1818

    
1819
    sh = SH(ctx->opcode) | (shn << 5);
1820
    mb = MB(ctx->opcode) | (mbn << 5);
1821
    gen_rldinm(ctx, mb, 63, sh);
1822
}
1823
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1824
/* rldicr - rldicr. */
1825
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1826
{
1827
    uint32_t sh, me;
1828

    
1829
    sh = SH(ctx->opcode) | (shn << 5);
1830
    me = MB(ctx->opcode) | (men << 5);
1831
    gen_rldinm(ctx, 0, me, sh);
1832
}
1833
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1834
/* rldic - rldic. */
1835
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1836
{
1837
    uint32_t sh, mb;
1838

    
1839
    sh = SH(ctx->opcode) | (shn << 5);
1840
    mb = MB(ctx->opcode) | (mbn << 5);
1841
    gen_rldinm(ctx, mb, 63 - sh, sh);
1842
}
1843
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1844

    
1845
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1846
                                     uint32_t me)
1847
{
1848
    TCGv t0;
1849

    
1850
    mb = MB(ctx->opcode);
1851
    me = ME(ctx->opcode);
1852
    t0 = tcg_temp_new();
1853
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1854
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1855
    if (unlikely(mb != 0 || me != 63)) {
1856
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1857
    } else {
1858
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1859
    }
1860
    tcg_temp_free(t0);
1861
    if (unlikely(Rc(ctx->opcode) != 0))
1862
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1863
}
1864

    
1865
/* rldcl - rldcl. */
1866
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1867
{
1868
    uint32_t mb;
1869

    
1870
    mb = MB(ctx->opcode) | (mbn << 5);
1871
    gen_rldnm(ctx, mb, 63);
1872
}
1873
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1874
/* rldcr - rldcr. */
1875
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1876
{
1877
    uint32_t me;
1878

    
1879
    me = MB(ctx->opcode) | (men << 5);
1880
    gen_rldnm(ctx, 0, me);
1881
}
1882
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1883
/* rldimi - rldimi. */
1884
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1885
{
1886
    uint32_t sh, mb, me;
1887

    
1888
    sh = SH(ctx->opcode) | (shn << 5);
1889
    mb = MB(ctx->opcode) | (mbn << 5);
1890
    me = 63 - sh;
1891
    if (unlikely(sh == 0 && mb == 0)) {
1892
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1893
    } else {
1894
        TCGv t0, t1;
1895
        target_ulong mask;
1896

    
1897
        t0 = tcg_temp_new();
1898
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1899
        t1 = tcg_temp_new();
1900
        mask = MASK(mb, me);
1901
        tcg_gen_andi_tl(t0, t0, mask);
1902
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1903
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1904
        tcg_temp_free(t0);
1905
        tcg_temp_free(t1);
1906
    }
1907
    if (unlikely(Rc(ctx->opcode) != 0))
1908
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1909
}
1910
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1911
#endif
1912

    
1913
/***                             Integer shift                             ***/
1914
/* slw & slw. */
1915
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1916
{
1917
    TCGv t0;
1918
    int l1, l2;
1919
    l1 = gen_new_label();
1920
    l2 = gen_new_label();
1921

    
1922
    t0 = tcg_temp_local_new();
1923
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1924
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1925
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1926
    tcg_gen_br(l2);
1927
    gen_set_label(l1);
1928
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1929
    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1930
    gen_set_label(l2);
1931
    tcg_temp_free(t0);
1932
    if (unlikely(Rc(ctx->opcode) != 0))
1933
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1934
}
1935
/* sraw & sraw. */
1936
GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1937
{
1938
    gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1939
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1940
    if (unlikely(Rc(ctx->opcode) != 0))
1941
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1942
}
1943
/* srawi & srawi. */
1944
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1945
{
1946
    int sh = SH(ctx->opcode);
1947
    if (sh != 0) {
1948
        int l1, l2;
1949
        TCGv t0;
1950
        l1 = gen_new_label();
1951
        l2 = gen_new_label();
1952
        t0 = tcg_temp_local_new();
1953
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1954
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1955
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1956
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1957
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1958
        tcg_gen_br(l2);
1959
        gen_set_label(l1);
1960
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1961
        gen_set_label(l2);
1962
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1963
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1964
        tcg_temp_free(t0);
1965
    } else {
1966
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1967
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1968
    }
1969
    if (unlikely(Rc(ctx->opcode) != 0))
1970
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1971
}
1972
/* srw & srw. */
1973
GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1974
{
1975
    TCGv t0, t1;
1976
    int l1, l2;
1977
    l1 = gen_new_label();
1978
    l2 = gen_new_label();
1979

    
1980
    t0 = tcg_temp_local_new();
1981
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1982
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1983
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1984
    tcg_gen_br(l2);
1985
    gen_set_label(l1);
1986
    t1 = tcg_temp_new();
1987
    tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
1988
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
1989
    tcg_temp_free(t1);
1990
    gen_set_label(l2);
1991
    tcg_temp_free(t0);
1992
    if (unlikely(Rc(ctx->opcode) != 0))
1993
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1994
}
1995
#if defined(TARGET_PPC64)
1996
/* sld & sld. */
1997
GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
1998
{
1999
    TCGv t0;
2000
    int l1, l2;
2001
    l1 = gen_new_label();
2002
    l2 = gen_new_label();
2003

    
2004
    t0 = tcg_temp_local_new();
2005
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2006
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2007
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2008
    tcg_gen_br(l2);
2009
    gen_set_label(l1);
2010
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2011
    gen_set_label(l2);
2012
    tcg_temp_free(t0);
2013
    if (unlikely(Rc(ctx->opcode) != 0))
2014
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2015
}
2016
/* srad & srad. */
2017
GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2018
{
2019
    gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2020
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2021
    if (unlikely(Rc(ctx->opcode) != 0))
2022
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2023
}
2024
/* sradi & sradi. */
2025
static always_inline void gen_sradi (DisasContext *ctx, int n)
2026
{
2027
    int sh = SH(ctx->opcode) + (n << 5);
2028
    if (sh != 0) {
2029
        int l1, l2;
2030
        TCGv t0;
2031
        l1 = gen_new_label();
2032
        l2 = gen_new_label();
2033
        t0 = tcg_temp_local_new();
2034
        tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2035
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2036
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2037
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2038
        tcg_gen_br(l2);
2039
        gen_set_label(l1);
2040
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2041
        gen_set_label(l2);
2042
        tcg_temp_free(t0);
2043
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2044
    } else {
2045
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2046
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2047
    }
2048
    if (unlikely(Rc(ctx->opcode) != 0))
2049
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2050
}
2051
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2052
{
2053
    gen_sradi(ctx, 0);
2054
}
2055
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2056
{
2057
    gen_sradi(ctx, 1);
2058
}
2059
/* srd & srd. */
2060
GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2061
{
2062
    TCGv t0;
2063
    int l1, l2;
2064
    l1 = gen_new_label();
2065
    l2 = gen_new_label();
2066

    
2067
    t0 = tcg_temp_local_new();
2068
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2069
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2070
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2071
    tcg_gen_br(l2);
2072
    gen_set_label(l1);
2073
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2074
    gen_set_label(l2);
2075
    tcg_temp_free(t0);
2076
    if (unlikely(Rc(ctx->opcode) != 0))
2077
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2078
}
2079
#endif
2080

    
2081
/***                       Floating-Point arithmetic                       ***/
2082
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2083
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2084
{                                                                             \
2085
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2086
        GEN_EXCP_NO_FP(ctx);                                                  \
2087
        return;                                                               \
2088
    }                                                                         \
2089
    gen_reset_fpstatus();                                                     \
2090
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2091
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2092
    if (isfloat) {                                                            \
2093
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2094
    }                                                                         \
2095
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2096
                     Rc(ctx->opcode) != 0);                                   \
2097
}
2098

    
2099
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2100
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2101
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2102

    
2103
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2104
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2105
{                                                                             \
2106
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2107
        GEN_EXCP_NO_FP(ctx);                                                  \
2108
        return;                                                               \
2109
    }                                                                         \
2110
    gen_reset_fpstatus();                                                     \
2111
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2112
                     cpu_fpr[rB(ctx->opcode)]);                               \
2113
    if (isfloat) {                                                            \
2114
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2115
    }                                                                         \
2116
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2117
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2118
}
2119
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2120
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2121
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2122

    
2123
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2124
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2125
{                                                                             \
2126
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2127
        GEN_EXCP_NO_FP(ctx);                                                  \
2128
        return;                                                               \
2129
    }                                                                         \
2130
    gen_reset_fpstatus();                                                     \
2131
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2132
                       cpu_fpr[rC(ctx->opcode)]);                             \
2133
    if (isfloat) {                                                            \
2134
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2135
    }                                                                         \
2136
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2137
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2138
}
2139
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2140
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2141
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2142

    
2143
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2144
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2145
{                                                                             \
2146
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2147
        GEN_EXCP_NO_FP(ctx);                                                  \
2148
        return;                                                               \
2149
    }                                                                         \
2150
    gen_reset_fpstatus();                                                     \
2151
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2152
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2153
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2154
}
2155

    
2156
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2157
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2158
{                                                                             \
2159
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2160
        GEN_EXCP_NO_FP(ctx);                                                  \
2161
        return;                                                               \
2162
    }                                                                         \
2163
    gen_reset_fpstatus();                                                     \
2164
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2165
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2166
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2167
}
2168

    
2169
/* fadd - fadds */
2170
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2171
/* fdiv - fdivs */
2172
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2173
/* fmul - fmuls */
2174
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2175

    
2176
/* fre */
2177
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2178

    
2179
/* fres */
2180
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2181

    
2182
/* frsqrte */
2183
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2184

    
2185
/* frsqrtes */
2186
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2187
{
2188
    if (unlikely(!ctx->fpu_enabled)) {
2189
        GEN_EXCP_NO_FP(ctx);
2190
        return;
2191
    }
2192
    gen_reset_fpstatus();
2193
    gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2194
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2195
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2196
}
2197

    
2198
/* fsel */
2199
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2200
/* fsub - fsubs */
2201
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2202
/* Optional: */
2203
/* fsqrt */
2204
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2205
{
2206
    if (unlikely(!ctx->fpu_enabled)) {
2207
        GEN_EXCP_NO_FP(ctx);
2208
        return;
2209
    }
2210
    gen_reset_fpstatus();
2211
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2212
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2213
}
2214

    
2215
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2216
{
2217
    if (unlikely(!ctx->fpu_enabled)) {
2218
        GEN_EXCP_NO_FP(ctx);
2219
        return;
2220
    }
2221
    gen_reset_fpstatus();
2222
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2223
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2224
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2225
}
2226

    
2227
/***                     Floating-Point multiply-and-add                   ***/
2228
/* fmadd - fmadds */
2229
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2230
/* fmsub - fmsubs */
2231
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2232
/* fnmadd - fnmadds */
2233
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2234
/* fnmsub - fnmsubs */
2235
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2236

    
2237
/***                     Floating-Point round & convert                    ***/
2238
/* fctiw */
2239
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2240
/* fctiwz */
2241
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2242
/* frsp */
2243
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2244
#if defined(TARGET_PPC64)
2245
/* fcfid */
2246
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2247
/* fctid */
2248
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2249
/* fctidz */
2250
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2251
#endif
2252

    
2253
/* frin */
2254
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2255
/* friz */
2256
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2257
/* frip */
2258
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2259
/* frim */
2260
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2261

    
2262
/***                         Floating-Point compare                        ***/
2263
/* fcmpo */
2264
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2265
{
2266
    if (unlikely(!ctx->fpu_enabled)) {
2267
        GEN_EXCP_NO_FP(ctx);
2268
        return;
2269
    }
2270
    gen_reset_fpstatus();
2271
    gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2272
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2273
    gen_helper_float_check_status();
2274
}
2275

    
2276
/* fcmpu */
2277
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2278
{
2279
    if (unlikely(!ctx->fpu_enabled)) {
2280
        GEN_EXCP_NO_FP(ctx);
2281
        return;
2282
    }
2283
    gen_reset_fpstatus();
2284
    gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2285
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2286
    gen_helper_float_check_status();
2287
}
2288

    
2289
/***                         Floating-point move                           ***/
2290
/* fabs */
2291
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2292
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2293

    
2294
/* fmr  - fmr. */
2295
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2296
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2297
{
2298
    if (unlikely(!ctx->fpu_enabled)) {
2299
        GEN_EXCP_NO_FP(ctx);
2300
        return;
2301
    }
2302
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2303
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2304
}
2305

    
2306
/* fnabs */
2307
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2308
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2309
/* fneg */
2310
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2311
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2312

    
2313
/***                  Floating-Point status & ctrl register                ***/
2314
/* mcrfs */
2315
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2316
{
2317
    int bfa;
2318

    
2319
    if (unlikely(!ctx->fpu_enabled)) {
2320
        GEN_EXCP_NO_FP(ctx);
2321
        return;
2322
    }
2323
    gen_optimize_fprf();
2324
    bfa = 4 * (7 - crfS(ctx->opcode));
2325
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2326
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2327
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2328
}
2329

    
2330
/* mffs */
2331
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2332
{
2333
    if (unlikely(!ctx->fpu_enabled)) {
2334
        GEN_EXCP_NO_FP(ctx);
2335
        return;
2336
    }
2337
    gen_optimize_fprf();
2338
    gen_reset_fpstatus();
2339
    tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2340
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2341
}
2342

    
2343
/* mtfsb0 */
2344
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2345
{
2346
    uint8_t crb;
2347

    
2348
    if (unlikely(!ctx->fpu_enabled)) {
2349
        GEN_EXCP_NO_FP(ctx);
2350
        return;
2351
    }
2352
    crb = 32 - (crbD(ctx->opcode) >> 2);
2353
    gen_optimize_fprf();
2354
    gen_reset_fpstatus();
2355
    if (likely(crb != 30 && crb != 29))
2356
        tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
2357
    if (unlikely(Rc(ctx->opcode) != 0)) {
2358
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2359
    }
2360
}
2361

    
2362
/* mtfsb1 */
2363
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2364
{
2365
    uint8_t crb;
2366

    
2367
    if (unlikely(!ctx->fpu_enabled)) {
2368
        GEN_EXCP_NO_FP(ctx);
2369
        return;
2370
    }
2371
    crb = 32 - (crbD(ctx->opcode) >> 2);
2372
    gen_optimize_fprf();
2373
    gen_reset_fpstatus();
2374
    /* XXX: we pretend we can only do IEEE floating-point computations */
2375
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2376
        TCGv_i32 t0 = tcg_const_i32(crb);
2377
        gen_helper_fpscr_setbit(t0);
2378
        tcg_temp_free_i32(t0);
2379
    }
2380
    if (unlikely(Rc(ctx->opcode) != 0)) {
2381
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2382
    }
2383
    /* We can raise a differed exception */
2384
    gen_helper_float_check_status();
2385
}
2386

    
2387
/* mtfsf */
2388
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2389
{
2390
    TCGv_i32 t0;
2391

    
2392
    if (unlikely(!ctx->fpu_enabled)) {
2393
        GEN_EXCP_NO_FP(ctx);
2394
        return;
2395
    }
2396
    gen_optimize_fprf();
2397
    gen_reset_fpstatus();
2398
    t0 = tcg_const_i32(FM(ctx->opcode));
2399
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2400
    tcg_temp_free_i32(t0);
2401
    if (unlikely(Rc(ctx->opcode) != 0)) {
2402
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2403
    }
2404
    /* We can raise a differed exception */
2405
    gen_helper_float_check_status();
2406
}
2407

    
2408
/* mtfsfi */
2409
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2410
{
2411
    int bf, sh;
2412
    TCGv_i64 t0;
2413
    TCGv_i32 t1;
2414

    
2415
    if (unlikely(!ctx->fpu_enabled)) {
2416
        GEN_EXCP_NO_FP(ctx);
2417
        return;
2418
    }
2419
    bf = crbD(ctx->opcode) >> 2;
2420
    sh = 7 - bf;
2421
    gen_optimize_fprf();
2422
    gen_reset_fpstatus();
2423
    t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
2424
    t1 = tcg_const_i32(1 << sh);
2425
    gen_helper_store_fpscr(t0, t1);
2426
    tcg_temp_free_i64(t0);
2427
    tcg_temp_free_i32(t1);
2428
    if (unlikely(Rc(ctx->opcode) != 0)) {
2429
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2430
    }
2431
    /* We can raise a differed exception */
2432
    gen_helper_float_check_status();
2433
}
2434

    
2435
/***                           Addressing modes                            ***/
2436
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2437
static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2438
{
2439
    target_long simm = SIMM(ctx->opcode);
2440

    
2441
    simm &= ~maskl;
2442
    if (rA(ctx->opcode) == 0) {
2443
#if defined(TARGET_PPC64)
2444
        if (!ctx->sf_mode) {
2445
            tcg_gen_movi_tl(EA, (uint32_t)simm);
2446
        } else
2447
#endif
2448
        tcg_gen_movi_tl(EA, simm);
2449
    } else if (likely(simm != 0)) {
2450
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2451
#if defined(TARGET_PPC64)
2452
        if (!ctx->sf_mode) {
2453
            tcg_gen_ext32u_tl(EA, EA);
2454
        }
2455
#endif
2456
    } else {
2457
#if defined(TARGET_PPC64)
2458
        if (!ctx->sf_mode) {
2459
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2460
        } else
2461
#endif
2462
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2463
    }
2464
}
2465

    
2466
static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2467
{
2468
    if (rA(ctx->opcode) == 0) {
2469
#if defined(TARGET_PPC64)
2470
        if (!ctx->sf_mode) {
2471
            tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2472
        } else
2473
#endif
2474
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2475
    } else {
2476
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2477
#if defined(TARGET_PPC64)
2478
        if (!ctx->sf_mode) {
2479
            tcg_gen_ext32u_tl(EA, EA);
2480
        }
2481
#endif
2482
    }
2483
}
2484

    
2485
static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2486
{
2487
    if (rA(ctx->opcode) == 0) {
2488
        tcg_gen_movi_tl(EA, 0);
2489
    } else {
2490
#if defined(TARGET_PPC64)
2491
        if (!ctx->sf_mode) {
2492
            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2493
        } else
2494
#endif
2495
            tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2496
    }
2497
}
2498

    
2499
static always_inline void gen_addr_add (DisasContext *ctx, TCGv ret, TCGv arg1, target_long val)
2500
{
2501
    tcg_gen_addi_tl(ret, arg1, val);
2502
#if defined(TARGET_PPC64)
2503
    if (!ctx->sf_mode) {
2504
        tcg_gen_ext32u_tl(ret, ret);
2505
    }
2506
#endif
2507
}
2508

    
2509
static always_inline void gen_check_align (DisasContext *ctx, TCGv EA, int mask)
2510
{
2511
    int l1 = gen_new_label();
2512
    TCGv t0 = tcg_temp_new();
2513
    TCGv_i32 t1, t2;
2514
    /* NIP cannot be restored if the memory exception comes from an helper */
2515
    gen_update_nip(ctx, ctx->nip - 4);
2516
    tcg_gen_andi_tl(t0, EA, mask);
2517
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2518
    t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2519
    t2 = tcg_const_i32(0);
2520
    gen_helper_raise_exception_err(t1, t2);
2521
    tcg_temp_free_i32(t1);
2522
    tcg_temp_free_i32(t2);
2523
    gen_set_label(l1);
2524
    tcg_temp_free(t0);
2525
}
2526

    
2527
/***                             Integer load                              ***/
2528
static always_inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2529
{
2530
    tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2531
}
2532

    
2533
static always_inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2534
{
2535
    tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2536
}
2537

    
2538
static always_inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2539
{
2540
    tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2541
    if (unlikely(ctx->le_mode)) {
2542
#if defined(TARGET_PPC64)
2543
        TCGv_i32 t0 = tcg_temp_new_i32();
2544
        tcg_gen_trunc_tl_i32(t0, arg1);
2545
        tcg_gen_bswap16_i32(t0, t0);
2546
        tcg_gen_extu_i32_tl(arg1, t0);
2547
        tcg_temp_free_i32(t0);
2548
#else
2549
        tcg_gen_bswap16_i32(arg1, arg1);
2550
#endif
2551
    }
2552
}
2553

    
2554
static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2555
{
2556
    if (unlikely(ctx->le_mode)) {
2557
#if defined(TARGET_PPC64)
2558
        TCGv_i32 t0;
2559
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2560
        t0 = tcg_temp_new_i32();
2561
        tcg_gen_trunc_tl_i32(t0, arg1);
2562
        tcg_gen_bswap16_i32(t0, t0);
2563
        tcg_gen_extu_i32_tl(arg1, t0);
2564
        tcg_gen_ext16s_tl(arg1, arg1);
2565
        tcg_temp_free_i32(t0);
2566
#else
2567
        tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2568
        tcg_gen_bswap16_i32(arg1, arg1);
2569
        tcg_gen_ext16s_i32(arg1, arg1);
2570
#endif
2571
    } else {
2572
        tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2573
    }
2574
}
2575

    
2576
static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2577
{
2578
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2579
    if (unlikely(ctx->le_mode)) {
2580
#if defined(TARGET_PPC64)
2581
        TCGv_i32 t0 = tcg_temp_new_i32();
2582
        tcg_gen_trunc_tl_i32(t0, arg1);
2583
        tcg_gen_bswap_i32(t0, t0);
2584
        tcg_gen_extu_i32_tl(arg1, t0);
2585
        tcg_temp_free_i32(t0);
2586
#else
2587
        tcg_gen_bswap_i32(arg1, arg1);
2588
#endif
2589
    }
2590
}
2591

    
2592
#if defined(TARGET_PPC64)
2593
static always_inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2594
{
2595
    if (unlikely(ctx->mem_idx)) {
2596
        TCGv_i32 t0;
2597
        tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2598
        t0 = tcg_temp_new_i32();
2599
        tcg_gen_trunc_tl_i32(t0, arg1);
2600
        tcg_gen_bswap_i32(t0, t0);
2601
        tcg_gen_ext_i32_tl(arg1, t0);
2602
        tcg_temp_free_i32(t0);
2603
    } else
2604
        tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
2605
}
2606
#endif
2607

    
2608
static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2609
{
2610
    tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2611
    if (unlikely(ctx->le_mode)) {
2612
        tcg_gen_bswap_i64(arg1, arg1);
2613
    }
2614
}
2615

    
2616
static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2617
{
2618
    tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2619
}
2620

    
2621
static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2622
{
2623
    if (unlikely(ctx->le_mode)) {
2624
#if defined(TARGET_PPC64)
2625
        TCGv_i32 t0;
2626
        TCGv t1;
2627
        t0 = tcg_temp_new_i32();
2628
        tcg_gen_trunc_tl_i32(t0, arg1);
2629
        tcg_gen_ext16u_i32(t0, t0);
2630
        tcg_gen_bswap16_i32(t0, t0);
2631
        t1 = tcg_temp_new();
2632
        tcg_gen_extu_i32_tl(t1, t0);
2633
        tcg_temp_free_i32(t0);
2634
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
2635
        tcg_temp_free(t1);
2636
#else
2637
        TCGv t0 = tcg_temp_new();
2638
        tcg_gen_ext16u_tl(t0, arg1);
2639
        tcg_gen_bswap16_i32(t0, t0);
2640
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2641
        tcg_temp_free(t0);
2642
#endif
2643
    } else {
2644
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2645
    }
2646
}
2647

    
2648
static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2649
{
2650
    if (unlikely(ctx->le_mode)) {
2651
#if defined(TARGET_PPC64)
2652
        TCGv_i32 t0;
2653
        TCGv t1;
2654
        t0 = tcg_temp_new_i32();
2655
        tcg_gen_trunc_tl_i32(t0, arg1);
2656
        tcg_gen_bswap_i32(t0, t0);
2657
        t1 = tcg_temp_new();
2658
        tcg_gen_extu_i32_tl(t1, t0);
2659
        tcg_temp_free_i32(t0);
2660
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
2661
        tcg_temp_free(t1);
2662
#else
2663
        TCGv t0 = tcg_temp_new_i32();
2664
        tcg_gen_bswap_i32(t0, arg1);
2665
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2666
        tcg_temp_free(t0);
2667
#endif
2668
    } else {
2669
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2670
    }
2671
}
2672

    
2673
static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2674
{
2675
    if (unlikely(ctx->le_mode)) {
2676
        TCGv_i64 t0 = tcg_temp_new_i64();
2677
        tcg_gen_bswap_i64(t0, arg1);
2678
        tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2679
        tcg_temp_free_i64(t0);
2680
    } else
2681
        tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2682
}
2683

    
2684
#define GEN_LD(name, ldop, opc, type)                                         \
2685
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2686
{                                                                             \
2687
    TCGv EA;                                                                  \
2688
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2689
    EA = tcg_temp_new();                                                      \
2690
    gen_addr_imm_index(ctx, EA, 0);                                           \
2691
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2692
    tcg_temp_free(EA);                                                        \
2693
}
2694

    
2695
#define GEN_LDU(name, ldop, opc, type)                                        \
2696
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2697
{                                                                             \
2698
    TCGv EA;                                                                  \
2699
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2700
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2701
        GEN_EXCP_INVAL(ctx);                                                  \
2702
        return;                                                               \
2703
    }                                                                         \
2704
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2705
    EA = tcg_temp_new();                                                      \
2706
    if (type == PPC_64B)                                                      \
2707
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
2708
    else                                                                      \
2709
        gen_addr_imm_index(ctx, EA, 0);                                       \
2710
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2711
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2712
    tcg_temp_free(EA);                                                        \
2713
}
2714

    
2715
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2716
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2717
{                                                                             \
2718
    TCGv EA;                                                                  \
2719
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2720
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2721
        GEN_EXCP_INVAL(ctx);                                                  \
2722
        return;                                                               \
2723
    }                                                                         \
2724
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2725
    EA = tcg_temp_new();                                                      \
2726
    gen_addr_reg_index(ctx, EA);                                              \
2727
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2728
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2729
    tcg_temp_free(EA);                                                        \
2730
}
2731

    
2732
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2733
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2734
{                                                                             \
2735
    TCGv EA;                                                                  \
2736
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2737
    EA = tcg_temp_new();                                                      \
2738
    gen_addr_reg_index(ctx, EA);                                              \
2739
    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2740
    tcg_temp_free(EA);                                                        \
2741
}
2742

    
2743
#define GEN_LDS(name, ldop, op, type)                                         \
2744
GEN_LD(name, ldop, op | 0x20, type);                                          \
2745
GEN_LDU(name, ldop, op | 0x21, type);                                         \
2746
GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2747
GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2748

    
2749
/* lbz lbzu lbzux lbzx */
2750
GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2751
/* lha lhau lhaux lhax */
2752
GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2753
/* lhz lhzu lhzux lhzx */
2754
GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2755
/* lwz lwzu lwzux lwzx */
2756
GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2757
#if defined(TARGET_PPC64)
2758
/* lwaux */
2759
GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2760
/* lwax */
2761
GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2762
/* ldux */
2763
GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2764
/* ldx */
2765
GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2766
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2767
{
2768
    TCGv EA;
2769
    if (Rc(ctx->opcode)) {
2770
        if (unlikely(rA(ctx->opcode) == 0 ||
2771
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2772
            GEN_EXCP_INVAL(ctx);
2773
            return;
2774
        }
2775
    }
2776
    gen_set_access_type(ctx, ACCESS_INT);
2777
    EA = tcg_temp_new();
2778
    gen_addr_imm_index(ctx, EA, 0x03);
2779
    if (ctx->opcode & 0x02) {
2780
        /* lwa (lwau is undefined) */
2781
        gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2782
    } else {
2783
        /* ld - ldu */
2784
        gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2785
    }
2786
    if (Rc(ctx->opcode))
2787
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2788
    tcg_temp_free(EA);
2789
}
2790
/* lq */
2791
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2792
{
2793
#if defined(CONFIG_USER_ONLY)
2794
    GEN_EXCP_PRIVOPC(ctx);
2795
#else
2796
    int ra, rd;
2797
    TCGv EA;
2798

    
2799
    /* Restore CPU state */
2800
    if (unlikely(ctx->mem_idx == 0)) {
2801
        GEN_EXCP_PRIVOPC(ctx);
2802
        return;
2803
    }
2804
    ra = rA(ctx->opcode);
2805
    rd = rD(ctx->opcode);
2806
    if (unlikely((rd & 1) || rd == ra)) {
2807
        GEN_EXCP_INVAL(ctx);
2808
        return;
2809
    }
2810
    if (unlikely(ctx->le_mode)) {
2811
        /* Little-endian mode is not handled */
2812
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2813
        return;
2814
    }
2815
    gen_set_access_type(ctx, ACCESS_INT);
2816
    EA = tcg_temp_new();
2817
    gen_addr_imm_index(ctx, EA, 0x0F);
2818
    gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2819
    gen_addr_add(ctx, EA, EA, 8);
2820
    gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2821
    tcg_temp_free(EA);
2822
#endif
2823
}
2824
#endif
2825

    
2826
/***                              Integer store                            ***/
2827
#define GEN_ST(name, stop, opc, type)                                         \
2828
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2829
{                                                                             \
2830
    TCGv EA;                                                                  \
2831
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2832
    EA = tcg_temp_new();                                                      \
2833
    gen_addr_imm_index(ctx, EA, 0);                                           \
2834
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2835
    tcg_temp_free(EA);                                                        \
2836
}
2837

    
2838
#define GEN_STU(name, stop, opc, type)                                        \
2839
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2840
{                                                                             \
2841
    TCGv EA;                                                                  \
2842
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2843
        GEN_EXCP_INVAL(ctx);                                                  \
2844
        return;                                                               \
2845
    }                                                                         \
2846
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2847
    EA = tcg_temp_new();                                                      \
2848
    if (type == PPC_64B)                                                      \
2849
        gen_addr_imm_index(ctx, EA, 0x03);                                    \
2850
    else                                                                      \
2851
        gen_addr_imm_index(ctx, EA, 0);                                       \
2852
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2853
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2854
    tcg_temp_free(EA);                                                        \
2855
}
2856

    
2857
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
2858
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2859
{                                                                             \
2860
    TCGv EA;                                                                  \
2861
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2862
        GEN_EXCP_INVAL(ctx);                                                  \
2863
        return;                                                               \
2864
    }                                                                         \
2865
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2866
    EA = tcg_temp_new();                                                      \
2867
    gen_addr_reg_index(ctx, EA);                                              \
2868
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2869
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2870
    tcg_temp_free(EA);                                                        \
2871
}
2872

    
2873
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
2874
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2875
{                                                                             \
2876
    TCGv EA;                                                                  \
2877
    gen_set_access_type(ctx, ACCESS_INT);                                     \
2878
    EA = tcg_temp_new();                                                      \
2879
    gen_addr_reg_index(ctx, EA);                                              \
2880
    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2881
    tcg_temp_free(EA);                                                        \
2882
}
2883

    
2884
#define GEN_STS(name, stop, op, type)                                         \
2885
GEN_ST(name, stop, op | 0x20, type);                                          \
2886
GEN_STU(name, stop, op | 0x21, type);                                         \
2887
GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2888
GEN_STX(name, stop, 0x17, op | 0x00, type)
2889

    
2890
/* stb stbu stbux stbx */
2891
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2892
/* sth sthu sthux sthx */
2893
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2894
/* stw stwu stwux stwx */
2895
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2896
#if defined(TARGET_PPC64)
2897
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2898
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2899
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2900
{
2901
    int rs;
2902
    TCGv EA;
2903

    
2904
    rs = rS(ctx->opcode);
2905
    if ((ctx->opcode & 0x3) == 0x2) {
2906
#if defined(CONFIG_USER_ONLY)
2907
        GEN_EXCP_PRIVOPC(ctx);
2908
#else
2909
        /* stq */
2910
        if (unlikely(ctx->mem_idx == 0)) {
2911
            GEN_EXCP_PRIVOPC(ctx);
2912
            return;
2913
        }
2914
        if (unlikely(rs & 1)) {
2915
            GEN_EXCP_INVAL(ctx);
2916
            return;
2917
        }
2918
        if (unlikely(ctx->le_mode)) {
2919
            /* Little-endian mode is not handled */
2920
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2921
            return;
2922
        }
2923
        gen_set_access_type(ctx, ACCESS_INT);
2924
        EA = tcg_temp_new();
2925
        gen_addr_imm_index(ctx, EA, 0x03);
2926
        gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2927
        gen_addr_add(ctx, EA, EA, 8);
2928
        gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2929
        tcg_temp_free(EA);
2930
#endif
2931
    } else {
2932
        /* std / stdu */
2933
        if (Rc(ctx->opcode)) {
2934
            if (unlikely(rA(ctx->opcode) == 0)) {
2935
                GEN_EXCP_INVAL(ctx);
2936
                return;
2937
            }
2938
        }
2939
        gen_set_access_type(ctx, ACCESS_INT);
2940
        EA = tcg_temp_new();
2941
        gen_addr_imm_index(ctx, EA, 0x03);
2942
        gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2943
        if (Rc(ctx->opcode))
2944
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2945
        tcg_temp_free(EA);
2946
    }
2947
}
2948
#endif
2949
/***                Integer load and store with byte reverse               ***/
2950
/* lhbrx */
2951
static void always_inline gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2952
{
2953
    tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2954
    if (likely(!ctx->le_mode)) {
2955
#if defined(TARGET_PPC64)
2956
        TCGv_i32 t0 = tcg_temp_new_i32();
2957
        tcg_gen_trunc_tl_i32(t0, arg1);
2958
        tcg_gen_bswap16_i32(t0, t0);
2959
        tcg_gen_extu_i32_tl(arg1, t0);
2960
        tcg_temp_free_i32(t0);
2961
#else
2962
        tcg_gen_bswap16_i32(arg1, arg1);
2963
#endif
2964
    }
2965
}
2966
GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2967

    
2968
/* lwbrx */
2969
static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2970
{
2971
    tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2972
    if (likely(!ctx->le_mode)) {
2973
#if defined(TARGET_PPC64)
2974
        TCGv_i32 t0 = tcg_temp_new_i32();
2975
        tcg_gen_trunc_tl_i32(t0, arg1);
2976
        tcg_gen_bswap_i32(t0, t0);
2977
        tcg_gen_extu_i32_tl(arg1, t0);
2978
        tcg_temp_free_i32(t0);
2979
#else
2980
        tcg_gen_bswap_i32(arg1, arg1);
2981
#endif
2982
    }
2983
}
2984
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2985

    
2986
/* sthbrx */
2987
static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2988
{
2989
    if (likely(!ctx->le_mode)) {
2990
#if defined(TARGET_PPC64)
2991
        TCGv_i32 t0;
2992
        TCGv t1;
2993
        t0 = tcg_temp_new_i32();
2994
        tcg_gen_trunc_tl_i32(t0, arg1);
2995
        tcg_gen_ext16u_i32(t0, t0);
2996
        tcg_gen_bswap16_i32(t0, t0);
2997
        t1 = tcg_temp_new();
2998
        tcg_gen_extu_i32_tl(t1, t0);
2999
        tcg_temp_free_i32(t0);
3000
        tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
3001
        tcg_temp_free(t1);
3002
#else
3003
        TCGv t0 = tcg_temp_new();
3004
        tcg_gen_ext16u_tl(t0, arg1);
3005
        tcg_gen_bswap16_i32(t0, t0);
3006
        tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3007
        tcg_temp_free(t0);
3008
#endif
3009
    } else {
3010
        tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3011
    }
3012
}
3013
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3014

    
3015
/* stwbrx */
3016
static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3017
{
3018
    if (likely(!ctx->le_mode)) {
3019
#if defined(TARGET_PPC64)
3020
        TCGv_i32 t0;
3021
        TCGv t1;
3022
        t0 = tcg_temp_new_i32();
3023
        tcg_gen_trunc_tl_i32(t0, arg1);
3024
        tcg_gen_bswap_i32(t0, t0);
3025
        t1 = tcg_temp_new();
3026
        tcg_gen_extu_i32_tl(t1, t0);
3027
        tcg_temp_free_i32(t0);
3028
        tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
3029
        tcg_temp_free(t1);
3030
#else
3031
        TCGv t0 = tcg_temp_new_i32();
3032
        tcg_gen_bswap_i32(t0, arg1);
3033
        tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3034
        tcg_temp_free(t0);
3035
#endif
3036
    } else {
3037
        tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3038
    }
3039
}
3040
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3041

    
3042
/***                    Integer load and store multiple                    ***/
3043
/* lmw */
3044
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3045
{
3046
    TCGv t0;
3047
    TCGv_i32 t1;
3048
    gen_set_access_type(ctx, ACCESS_INT);
3049
    /* NIP cannot be restored if the memory exception comes from an helper */
3050
    gen_update_nip(ctx, ctx->nip - 4);
3051
    t0 = tcg_temp_new();
3052
    t1 = tcg_const_i32(rD(ctx->opcode));
3053
    gen_addr_imm_index(ctx, t0, 0);
3054
    gen_helper_lmw(t0, t1);
3055
    tcg_temp_free(t0);
3056
    tcg_temp_free_i32(t1);
3057
}
3058

    
3059
/* stmw */
3060
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3061
{
3062
    TCGv t0;
3063
    TCGv_i32 t1;
3064
    gen_set_access_type(ctx, ACCESS_INT);
3065
    /* NIP cannot be restored if the memory exception comes from an helper */
3066
    gen_update_nip(ctx, ctx->nip - 4);
3067
    t0 = tcg_temp_new();
3068
    t1 = tcg_const_i32(rS(ctx->opcode));
3069
    gen_addr_imm_index(ctx, t0, 0);
3070
    gen_helper_stmw(t0, t1);
3071
    tcg_temp_free(t0);
3072
    tcg_temp_free_i32(t1);
3073
}
3074

    
3075
/***                    Integer load and store strings                     ***/
3076
/* lswi */
3077
/* PowerPC32 specification says we must generate an exception if
3078
 * rA is in the range of registers to be loaded.
3079
 * In an other hand, IBM says this is valid, but rA won't be loaded.
3080
 * For now, I'll follow the spec...
3081
 */
3082
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3083
{
3084
    TCGv t0;
3085
    TCGv_i32 t1, t2;
3086
    int nb = NB(ctx->opcode);
3087
    int start = rD(ctx->opcode);
3088
    int ra = rA(ctx->opcode);
3089
    int nr;
3090

    
3091
    if (nb == 0)
3092
        nb = 32;
3093
    nr = nb / 4;
3094
    if (unlikely(((start + nr) > 32  &&
3095
                  start <= ra && (start + nr - 32) > ra) ||
3096
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3097
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3098
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
3099
        return;
3100
    }
3101
    gen_set_access_type(ctx, ACCESS_INT);
3102
    /* NIP cannot be restored if the memory exception comes from an helper */
3103
    gen_update_nip(ctx, ctx->nip - 4);
3104
    t0 = tcg_temp_new();
3105
    gen_addr_register(ctx, t0);
3106
    t1 = tcg_const_i32(nb);
3107
    t2 = tcg_const_i32(start);
3108
    gen_helper_lsw(t0, t1, t2);
3109
    tcg_temp_free(t0);
3110
    tcg_temp_free_i32(t1);
3111
    tcg_temp_free_i32(t2);
3112
}
3113

    
3114
/* lswx */
3115
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3116
{
3117
    TCGv t0;
3118
    TCGv_i32 t1, t2, t3;
3119
    gen_set_access_type(ctx, ACCESS_INT);
3120
    /* NIP cannot be restored if the memory exception comes from an helper */
3121
    gen_update_nip(ctx, ctx->nip - 4);
3122
    t0 = tcg_temp_new();
3123
    gen_addr_reg_index(ctx, t0);
3124
    t1 = tcg_const_i32(rD(ctx->opcode));
3125
    t2 = tcg_const_i32(rA(ctx->opcode));
3126
    t3 = tcg_const_i32(rB(ctx->opcode));
3127
    gen_helper_lswx(t0, t1, t2, t3);
3128
    tcg_temp_free(t0);
3129
    tcg_temp_free_i32(t1);
3130
    tcg_temp_free_i32(t2);
3131
    tcg_temp_free_i32(t3);
3132
}
3133

    
3134
/* stswi */
3135
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3136
{
3137
    TCGv t0;
3138
    TCGv_i32 t1, t2;
3139
    int nb = NB(ctx->opcode);
3140
    gen_set_access_type(ctx, ACCESS_INT);
3141
    /* NIP cannot be restored if the memory exception comes from an helper */
3142
    gen_update_nip(ctx, ctx->nip - 4);
3143
    t0 = tcg_temp_new();
3144
    gen_addr_register(ctx, t0);
3145
    if (nb == 0)
3146
        nb = 32;
3147
    t1 = tcg_const_i32(nb);
3148
    t2 = tcg_const_i32(rS(ctx->opcode));
3149
    gen_helper_stsw(t0, t1, t2);
3150
    tcg_temp_free(t0);
3151
    tcg_temp_free_i32(t1);
3152
    tcg_temp_free_i32(t2);
3153
}
3154

    
3155
/* stswx */
3156
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3157
{
3158
    TCGv t0;
3159
    TCGv_i32 t1, t2;
3160
    gen_set_access_type(ctx, ACCESS_INT);
3161
    /* NIP cannot be restored if the memory exception comes from an helper */
3162
    gen_update_nip(ctx, ctx->nip - 4);
3163
    t0 = tcg_temp_new();
3164
    gen_addr_reg_index(ctx, t0);
3165
    t1 = tcg_temp_new_i32();
3166
    tcg_gen_trunc_tl_i32(t1, cpu_xer);
3167
    tcg_gen_andi_i32(t1, t1, 0x7F);
3168
    t2 = tcg_const_i32(rS(ctx->opcode));
3169
    gen_helper_stsw(t0, t1, t2);
3170
    tcg_temp_free(t0);
3171
    tcg_temp_free_i32(t1);
3172
    tcg_temp_free_i32(t2);
3173
}
3174

    
3175
/***                        Memory synchronisation                         ***/
3176
/* eieio */
3177
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3178
{
3179
}
3180

    
3181
/* isync */
3182
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3183
{
3184
    GEN_STOP(ctx);
3185
}
3186

    
3187
/* lwarx */
3188
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3189
{
3190
    TCGv t0;
3191
    gen_set_access_type(ctx, ACCESS_RES);
3192
    t0 = tcg_temp_local_new();
3193
    gen_addr_reg_index(ctx, t0);
3194
    gen_check_align(ctx, t0, 0x03);
3195
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3196
    tcg_gen_mov_tl(cpu_reserve, t0);
3197
    tcg_temp_free(t0);
3198
}
3199

    
3200
/* stwcx. */
3201
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3202
{
3203
    int l1;
3204
    TCGv t0;
3205
    gen_set_access_type(ctx, ACCESS_RES);
3206
    t0 = tcg_temp_local_new();
3207
    gen_addr_reg_index(ctx, t0);
3208
    gen_check_align(ctx, t0, 0x03);
3209
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3210
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3211
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3212
    l1 = gen_new_label();
3213
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3214
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3215
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3216
    gen_set_label(l1);
3217
    tcg_gen_movi_tl(cpu_reserve, -1);
3218
    tcg_temp_free(t0);
3219
}
3220

    
3221
#if defined(TARGET_PPC64)
3222
/* ldarx */
3223
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3224
{
3225
    TCGv t0;
3226
    gen_set_access_type(ctx, ACCESS_RES);
3227
    t0 = tcg_temp_local_new();
3228
    gen_addr_reg_index(ctx, t0);
3229
    gen_check_align(ctx, t0, 0x07);
3230
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3231
    tcg_gen_mov_tl(cpu_reserve, t0);
3232
    tcg_temp_free(t0);
3233
}
3234

    
3235
/* stdcx. */
3236
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3237
{
3238
    int l1;
3239
    TCGv t0;
3240
    gen_set_access_type(ctx, ACCESS_RES);
3241
    t0 = tcg_temp_local_new();
3242
    gen_addr_reg_index(ctx, t0);
3243
    gen_check_align(ctx, t0, 0x07);
3244
    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3245
    tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3246
    tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3247
    l1 = gen_new_label();
3248
    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3249
    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3250
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3251
    gen_set_label(l1);
3252
    tcg_gen_movi_tl(cpu_reserve, -1);
3253
    tcg_temp_free(t0);
3254
}
3255
#endif /* defined(TARGET_PPC64) */
3256

    
3257
/* sync */
3258
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3259
{
3260
}
3261

    
3262
/* wait */
3263
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3264
{
3265
    TCGv_i32 t0 = tcg_temp_new_i32();
3266
    tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
3267
    tcg_temp_free_i32(t0);
3268
    /* Stop translation, as the CPU is supposed to sleep from now */
3269
    GEN_EXCP(ctx, EXCP_HLT, 1);
3270
}
3271

    
3272
/***                         Floating-point load                           ***/
3273
#define GEN_LDF(name, ldop, opc, type)                                        \
3274
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3275
{                                                                             \
3276
    TCGv EA;                                                                  \
3277
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3278
        GEN_EXCP_NO_FP(ctx);                                                  \
3279
        return;                                                               \
3280
    }                                                                         \
3281
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3282
    EA = tcg_temp_new();                                                      \
3283
    gen_addr_imm_index(ctx, EA, 0);                                           \
3284
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3285
    tcg_temp_free(EA);                                                        \
3286
}
3287

    
3288
#define GEN_LDUF(name, ldop, opc, type)                                       \
3289
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3290
{                                                                             \
3291
    TCGv EA;                                                                  \
3292
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3293
        GEN_EXCP_NO_FP(ctx);                                                  \
3294
        return;                                                               \
3295
    }                                                                         \
3296
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3297
        GEN_EXCP_INVAL(ctx);                                                  \
3298
        return;                                                               \
3299
    }                                                                         \
3300
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3301
    EA = tcg_temp_new();                                                      \
3302
    gen_addr_imm_index(ctx, EA, 0);                                           \
3303
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3304
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3305
    tcg_temp_free(EA);                                                        \
3306
}
3307

    
3308
#define GEN_LDUXF(name, ldop, opc, type)                                      \
3309
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3310
{                                                                             \
3311
    TCGv EA;                                                                  \
3312
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3313
        GEN_EXCP_NO_FP(ctx);                                                  \
3314
        return;                                                               \
3315
    }                                                                         \
3316
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3317
        GEN_EXCP_INVAL(ctx);                                                  \
3318
        return;                                                               \
3319
    }                                                                         \
3320
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3321
    EA = tcg_temp_new();                                                      \
3322
    gen_addr_reg_index(ctx, EA);                                              \
3323
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3324
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3325
    tcg_temp_free(EA);                                                        \
3326
}
3327

    
3328
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3329
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3330
{                                                                             \
3331
    TCGv EA;                                                                  \
3332
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3333
        GEN_EXCP_NO_FP(ctx);                                                  \
3334
        return;                                                               \
3335
    }                                                                         \
3336
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3337
    EA = tcg_temp_new();                                                      \
3338
    gen_addr_reg_index(ctx, EA);                                              \
3339
    gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3340
    tcg_temp_free(EA);                                                        \
3341
}
3342

    
3343
#define GEN_LDFS(name, ldop, op, type)                                        \
3344
GEN_LDF(name, ldop, op | 0x20, type);                                         \
3345
GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3346
GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3347
GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3348

    
3349
static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3350
{
3351
    TCGv t0 = tcg_temp_new();
3352
    TCGv_i32 t1 = tcg_temp_new_i32();
3353
    gen_qemu_ld32u(ctx, t0, arg2);
3354
    tcg_gen_trunc_tl_i32(t1, t0);
3355
    tcg_temp_free(t0);
3356
    gen_helper_float32_to_float64(arg1, t1);
3357
    tcg_temp_free_i32(t1);
3358
}
3359

    
3360
 /* lfd lfdu lfdux lfdx */
3361
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3362
 /* lfs lfsu lfsux lfsx */
3363
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3364

    
3365
/***                         Floating-point store                          ***/
3366
#define GEN_STF(name, stop, opc, type)                                        \
3367
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3368
{                                                                             \
3369
    TCGv EA;                                                                  \
3370
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3371
        GEN_EXCP_NO_FP(ctx);                                                  \
3372
        return;                                                               \
3373
    }                                                                         \
3374
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3375
    EA = tcg_temp_new();                                                      \
3376
    gen_addr_imm_index(ctx, EA, 0);                                           \
3377
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3378
    tcg_temp_free(EA);                                                        \
3379
}
3380

    
3381
#define GEN_STUF(name, stop, opc, type)                                       \
3382
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3383
{                                                                             \
3384
    TCGv EA;                                                                  \
3385
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3386
        GEN_EXCP_NO_FP(ctx);                                                  \
3387
        return;                                                               \
3388
    }                                                                         \
3389
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3390
        GEN_EXCP_INVAL(ctx);                                                  \
3391
        return;                                                               \
3392
    }                                                                         \
3393
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3394
    EA = tcg_temp_new();                                                      \
3395
    gen_addr_imm_index(ctx, EA, 0);                                           \
3396
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3397
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3398
    tcg_temp_free(EA);                                                        \
3399
}
3400

    
3401
#define GEN_STUXF(name, stop, opc, type)                                      \
3402
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3403
{                                                                             \
3404
    TCGv EA;                                                                  \
3405
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3406
        GEN_EXCP_NO_FP(ctx);                                                  \
3407
        return;                                                               \
3408
    }                                                                         \
3409
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3410
        GEN_EXCP_INVAL(ctx);                                                  \
3411
        return;                                                               \
3412
    }                                                                         \
3413
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3414
    EA = tcg_temp_new();                                                      \
3415
    gen_addr_reg_index(ctx, EA);                                              \
3416
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3417
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3418
    tcg_temp_free(EA);                                                        \
3419
}
3420

    
3421
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
3422
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3423
{                                                                             \
3424
    TCGv EA;                                                                  \
3425
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3426
        GEN_EXCP_NO_FP(ctx);                                                  \
3427
        return;                                                               \
3428
    }                                                                         \
3429
    gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3430
    EA = tcg_temp_new();                                                      \
3431
    gen_addr_reg_index(ctx, EA);                                              \
3432
    gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3433
    tcg_temp_free(EA);                                                        \
3434
}
3435

    
3436
#define GEN_STFS(name, stop, op, type)                                        \
3437
GEN_STF(name, stop, op | 0x20, type);                                         \
3438
GEN_STUF(name, stop, op | 0x21, type);                                        \
3439
GEN_STUXF(name, stop, op | 0x01, type);                                       \
3440
GEN_STXF(name, stop, 0x17, op | 0x00, type)
3441

    
3442
static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3443
{
3444
    TCGv_i32 t0 = tcg_temp_new_i32();
3445
    TCGv t1 = tcg_temp_new();
3446
    gen_helper_float64_to_float32(t0, arg1);
3447
    tcg_gen_extu_i32_tl(t1, t0);
3448
    tcg_temp_free_i32(t0);
3449
    gen_qemu_st32(ctx, t1, arg2);
3450
    tcg_temp_free(t1);
3451
}
3452

    
3453
/* stfd stfdu stfdux stfdx */
3454
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3455
/* stfs stfsu stfsux stfsx */
3456
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3457

    
3458
/* Optional: */
3459
static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3460
{
3461
    TCGv t0 = tcg_temp_new();
3462
    tcg_gen_trunc_i64_tl(t0, arg1),
3463
    gen_qemu_st32(ctx, t0, arg2);
3464
    tcg_temp_free(t0);
3465
}
3466
/* stfiwx */
3467
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3468

    
3469
/***                                Branch                                 ***/
3470
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3471
                                       target_ulong dest)
3472
{
3473
    TranslationBlock *tb;
3474
    tb = ctx->tb;
3475
#if defined(TARGET_PPC64)
3476
    if (!ctx->sf_mode)
3477
        dest = (uint32_t) dest;
3478
#endif
3479
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3480
        likely(!ctx->singlestep_enabled)) {
3481
        tcg_gen_goto_tb(n);
3482
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3483
        tcg_gen_exit_tb((long)tb + n);
3484
    } else {
3485
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3486
        if (unlikely(ctx->singlestep_enabled)) {
3487
            if ((ctx->singlestep_enabled &
3488
                (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3489
                ctx->exception == POWERPC_EXCP_BRANCH) {
3490
                target_ulong tmp = ctx->nip;
3491
                ctx->nip = dest;
3492
                GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
3493
                ctx->nip = tmp;
3494
            }
3495
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3496
                gen_update_nip(ctx, dest);
3497
                gen_helper_raise_debug();
3498
            }
3499
        }
3500
        tcg_gen_exit_tb(0);
3501
    }
3502
}
3503

    
3504
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3505
{
3506
#if defined(TARGET_PPC64)
3507
    if (ctx->sf_mode == 0)
3508
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3509
    else
3510
#endif
3511
        tcg_gen_movi_tl(cpu_lr, nip);
3512
}
3513

    
3514
/* b ba bl bla */
3515
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3516
{
3517
    target_ulong li, target;
3518

    
3519
    ctx->exception = POWERPC_EXCP_BRANCH;
3520
    /* sign extend LI */
3521
#if defined(TARGET_PPC64)
3522
    if (ctx->sf_mode)
3523
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3524
    else
3525
#endif
3526
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3527
    if (likely(AA(ctx->opcode) == 0))
3528
        target = ctx->nip + li - 4;
3529
    else
3530
        target = li;
3531
    if (LK(ctx->opcode))
3532
        gen_setlr(ctx, ctx->nip);
3533
    gen_goto_tb(ctx, 0, target);
3534
}
3535

    
3536
#define BCOND_IM  0
3537
#define BCOND_LR  1
3538
#define BCOND_CTR 2
3539

    
3540
static always_inline void gen_bcond (DisasContext *ctx, int type)
3541
{
3542
    uint32_t bo = BO(ctx->opcode);
3543
    int l1 = gen_new_label();
3544
    TCGv target;
3545

    
3546
    ctx->exception = POWERPC_EXCP_BRANCH;
3547
    if (type == BCOND_LR || type == BCOND_CTR) {
3548
        target = tcg_temp_local_new();
3549
        if (type == BCOND_CTR)
3550
            tcg_gen_mov_tl(target, cpu_ctr);
3551
        else
3552
            tcg_gen_mov_tl(target, cpu_lr);
3553
    }
3554
    if (LK(ctx->opcode))
3555
        gen_setlr(ctx, ctx->nip);
3556
    l1 = gen_new_label();
3557
    if ((bo & 0x4) == 0) {
3558
        /* Decrement and test CTR */
3559
        TCGv temp = tcg_temp_new();
3560
        if (unlikely(type == BCOND_CTR)) {
3561
            GEN_EXCP_INVAL(ctx);
3562
            return;
3563
        }
3564
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3565
#if defined(TARGET_PPC64)
3566
        if (!ctx->sf_mode)
3567
            tcg_gen_ext32u_tl(temp, cpu_ctr);
3568
        else
3569
#endif
3570
            tcg_gen_mov_tl(temp, cpu_ctr);
3571
        if (bo & 0x2) {
3572
            tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3573
        } else {
3574
            tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3575
        }
3576
        tcg_temp_free(temp);
3577
    }
3578
    if ((bo & 0x10) == 0) {
3579
        /* Test CR */
3580
        uint32_t bi = BI(ctx->opcode);
3581
        uint32_t mask = 1 << (3 - (bi & 0x03));
3582
        TCGv_i32 temp = tcg_temp_new_i32();
3583

    
3584
        if (bo & 0x8) {
3585
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3586
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3587
        } else {
3588
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3589
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3590
        }
3591
        tcg_temp_free_i32(temp);
3592
    }
3593
    if (type == BCOND_IM) {
3594
        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3595
        if (likely(AA(ctx->opcode) == 0)) {
3596
            gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3597
        } else {
3598
            gen_goto_tb(ctx, 0, li);
3599
        }
3600
        gen_set_label(l1);
3601
        gen_goto_tb(ctx, 1, ctx->nip);
3602
    } else {
3603
#if defined(TARGET_PPC64)
3604
        if (!(ctx->sf_mode))
3605
            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3606
        else
3607
#endif
3608
            tcg_gen_andi_tl(cpu_nip, target, ~3);
3609
        tcg_gen_exit_tb(0);
3610
        gen_set_label(l1);
3611
#if defined(TARGET_PPC64)
3612
        if (!(ctx->sf_mode))
3613
            tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3614
        else
3615
#endif
3616
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
3617
        tcg_gen_exit_tb(0);
3618
    }
3619
}
3620

    
3621
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3622
{
3623
    gen_bcond(ctx, BCOND_IM);
3624
}
3625

    
3626
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3627
{
3628
    gen_bcond(ctx, BCOND_CTR);
3629
}
3630

    
3631
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3632
{
3633
    gen_bcond(ctx, BCOND_LR);
3634
}
3635

    
3636
/***                      Condition register logical                       ***/
3637
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3638
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
3639
{                                                                             \
3640
    uint8_t bitmask;                                                          \
3641
    int sh;                                                                   \
3642
    TCGv_i32 t0, t1;                                                          \
3643
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3644
    t0 = tcg_temp_new_i32();                                                  \
3645
    if (sh > 0)                                                               \
3646
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3647
    else if (sh < 0)                                                          \
3648
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3649
    else                                                                      \
3650
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3651
    t1 = tcg_temp_new_i32();                                                  \
3652
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3653
    if (sh > 0)                                                               \
3654
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3655
    else if (sh < 0)                                                          \
3656
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3657
    else                                                                      \
3658
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3659
    tcg_op(t0, t0, t1);                                                       \
3660
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3661
    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3662
    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3663
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3664
    tcg_temp_free_i32(t0);                                                    \
3665
    tcg_temp_free_i32(t1);                                                    \
3666
}
3667

    
3668
/* crand */
3669
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3670
/* crandc */
3671
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3672
/* creqv */
3673
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3674
/* crnand */
3675
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3676
/* crnor */
3677
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3678
/* cror */
3679
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3680
/* crorc */
3681
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3682
/* crxor */
3683
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3684
/* mcrf */
3685
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3686
{
3687
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3688
}
3689

    
3690
/***                           System linkage                              ***/
3691
/* rfi (mem_idx only) */
3692
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3693
{
3694
#if defined(CONFIG_USER_ONLY)
3695
    GEN_EXCP_PRIVOPC(ctx);
3696
#else
3697
    /* Restore CPU state */
3698
    if (unlikely(!ctx->mem_idx)) {
3699
        GEN_EXCP_PRIVOPC(ctx);
3700
        return;
3701
    }
3702
    gen_helper_rfi();
3703
    GEN_SYNC(ctx);
3704
#endif
3705
}
3706

    
3707
#if defined(TARGET_PPC64)
3708
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3709
{
3710
#if defined(CONFIG_USER_ONLY)
3711
    GEN_EXCP_PRIVOPC(ctx);
3712
#else
3713
    /* Restore CPU state */
3714
    if (unlikely(!ctx->mem_idx)) {
3715
        GEN_EXCP_PRIVOPC(ctx);
3716
        return;
3717
    }
3718
    gen_helper_rfid();
3719
    GEN_SYNC(ctx);
3720
#endif
3721
}
3722

    
3723
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3724
{
3725
#if defined(CONFIG_USER_ONLY)
3726
    GEN_EXCP_PRIVOPC(ctx);
3727
#else
3728
    /* Restore CPU state */
3729
    if (unlikely(ctx->mem_idx <= 1)) {
3730
        GEN_EXCP_PRIVOPC(ctx);
3731
        return;
3732
    }
3733
    gen_helper_hrfid();
3734
    GEN_SYNC(ctx);
3735
#endif
3736
}
3737
#endif
3738

    
3739
/* sc */
3740
#if defined(CONFIG_USER_ONLY)
3741
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3742
#else
3743
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3744
#endif
3745
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3746
{
3747
    uint32_t lev;
3748

    
3749
    lev = (ctx->opcode >> 5) & 0x7F;
3750
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3751
}
3752

    
3753
/***                                Trap                                   ***/
3754
/* tw */
3755
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3756
{
3757
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3758
    /* Update the nip since this might generate a trap exception */
3759
    gen_update_nip(ctx, ctx->nip);
3760
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3761
    tcg_temp_free_i32(t0);
3762
}
3763

    
3764
/* twi */
3765
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3766
{
3767
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3768
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3769
    /* Update the nip since this might generate a trap exception */
3770
    gen_update_nip(ctx, ctx->nip);
3771
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3772
    tcg_temp_free(t0);
3773
    tcg_temp_free_i32(t1);
3774
}
3775

    
3776
#if defined(TARGET_PPC64)
3777
/* td */
3778
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3779
{
3780
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3781
    /* Update the nip since this might generate a trap exception */
3782
    gen_update_nip(ctx, ctx->nip);
3783
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3784
    tcg_temp_free_i32(t0);
3785
}
3786

    
3787
/* tdi */
3788
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3789
{
3790
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3791
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3792
    /* Update the nip since this might generate a trap exception */
3793
    gen_update_nip(ctx, ctx->nip);
3794
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3795
    tcg_temp_free(t0);
3796
    tcg_temp_free_i32(t1);
3797
}
3798
#endif
3799

    
3800
/***                          Processor control                            ***/
3801
/* mcrxr */
3802
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3803
{
3804
    tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3805
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3806
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3807
}
3808

    
3809
/* mfcr */
3810
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3811
{
3812
    uint32_t crm, crn;
3813

    
3814
    if (likely(ctx->opcode & 0x00100000)) {
3815
        crm = CRM(ctx->opcode);
3816
        if (likely((crm ^ (crm - 1)) == 0)) {
3817
            crn = ffs(crm);
3818
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3819
        }
3820
    } else {
3821
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3822
    }
3823
}
3824

    
3825
/* mfmsr */
3826
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3827
{
3828
#if defined(CONFIG_USER_ONLY)
3829
    GEN_EXCP_PRIVREG(ctx);
3830
#else
3831
    if (unlikely(!ctx->mem_idx)) {
3832
        GEN_EXCP_PRIVREG(ctx);
3833
        return;
3834
    }
3835
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3836
#endif
3837
}
3838

    
3839
#if 1
3840
#define SPR_NOACCESS ((void *)(-1UL))
3841
#else
3842
static void spr_noaccess (void *opaque, int sprn)
3843
{
3844
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3845
    printf("ERROR: try to access SPR %d !\n", sprn);
3846
}
3847
#define SPR_NOACCESS (&spr_noaccess)
3848
#endif
3849

    
3850
/* mfspr */
3851
static always_inline void gen_op_mfspr (DisasContext *ctx)
3852
{
3853
    void (*read_cb)(void *opaque, int gprn, int sprn);
3854
    uint32_t sprn = SPR(ctx->opcode);
3855

    
3856
#if !defined(CONFIG_USER_ONLY)
3857
    if (ctx->mem_idx == 2)
3858
        read_cb = ctx->spr_cb[sprn].hea_read;
3859
    else if (ctx->mem_idx)
3860
        read_cb = ctx->spr_cb[sprn].oea_read;
3861
    else
3862
#endif
3863
        read_cb = ctx->spr_cb[sprn].uea_read;
3864
    if (likely(read_cb != NULL)) {
3865
        if (likely(read_cb != SPR_NOACCESS)) {
3866
            (*read_cb)(ctx, rD(ctx->opcode), sprn);
3867
        } else {
3868
            /* Privilege exception */
3869
            /* This is a hack to avoid warnings when running Linux:
3870
             * this OS breaks the PowerPC virtualisation model,
3871
             * allowing userland application to read the PVR
3872
             */
3873
            if (sprn != SPR_PVR) {
3874
                if (loglevel != 0) {
3875
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
3876
                            ADDRX "\n", sprn, sprn, ctx->nip);
3877
                }
3878
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3879
                       sprn, sprn, ctx->nip);
3880
            }
3881
            GEN_EXCP_PRIVREG(ctx);
3882
        }
3883
    } else {
3884
        /* Not defined */
3885
        if (loglevel != 0) {
3886
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
3887
                    ADDRX "\n", sprn, sprn, ctx->nip);
3888
        }
3889
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3890
               sprn, sprn, ctx->nip);
3891
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3892
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3893
    }
3894
}
3895

    
3896
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3897
{
3898
    gen_op_mfspr(ctx);
3899
}
3900

    
3901
/* mftb */
3902
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3903
{
3904
    gen_op_mfspr(ctx);
3905
}
3906

    
3907
/* mtcrf */
3908
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3909
{
3910
    uint32_t crm, crn;
3911

    
3912
    crm = CRM(ctx->opcode);
3913
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3914
        TCGv_i32 temp = tcg_temp_new_i32();
3915
        crn = ffs(crm);
3916
        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3917
        tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3918
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3919
        tcg_temp_free_i32(temp);
3920
    } else {
3921
        TCGv_i32 temp = tcg_const_i32(crm);
3922
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3923
        tcg_temp_free_i32(temp);
3924
    }
3925
}
3926

    
3927
/* mtmsr */
3928
#if defined(TARGET_PPC64)
3929
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3930
{
3931
#if defined(CONFIG_USER_ONLY)
3932
    GEN_EXCP_PRIVREG(ctx);
3933
#else
3934
    if (unlikely(!ctx->mem_idx)) {
3935
        GEN_EXCP_PRIVREG(ctx);
3936
        return;
3937
    }
3938
    if (ctx->opcode & 0x00010000) {
3939
        /* Special form that does not need any synchronisation */
3940
        TCGv t0 = tcg_temp_new();
3941
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3942
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3943
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3944
        tcg_temp_free(t0);
3945
    } else {
3946
        /* XXX: we need to update nip before the store
3947
         *      if we enter power saving mode, we will exit the loop
3948
         *      directly from ppc_store_msr
3949
         */
3950
        gen_update_nip(ctx, ctx->nip);
3951
        gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3952
        /* Must stop the translation as machine state (may have) changed */
3953
        /* Note that mtmsr is not always defined as context-synchronizing */
3954
        ctx->exception = POWERPC_EXCP_STOP;
3955
    }
3956
#endif
3957
}
3958
#endif
3959

    
3960
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3961
{
3962
#if defined(CONFIG_USER_ONLY)
3963
    GEN_EXCP_PRIVREG(ctx);
3964
#else
3965
    if (unlikely(!ctx->mem_idx)) {
3966
        GEN_EXCP_PRIVREG(ctx);
3967
        return;
3968
    }
3969
    if (ctx->opcode & 0x00010000) {
3970
        /* Special form that does not need any synchronisation */
3971
        TCGv t0 = tcg_temp_new();
3972
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3973
        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3974
        tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3975
        tcg_temp_free(t0);
3976
    } else {
3977
        /* XXX: we need to update nip before the store
3978
         *      if we enter power saving mode, we will exit the loop
3979
         *      directly from ppc_store_msr
3980
         */
3981
        gen_update_nip(ctx, ctx->nip);
3982
#if defined(TARGET_PPC64)
3983
        if (!ctx->sf_mode) {
3984
            TCGv t0 = tcg_temp_new();
3985
            TCGv t1 = tcg_temp_new();
3986
            tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
3987
            tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
3988
            tcg_gen_or_tl(t0, t0, t1);
3989
            tcg_temp_free(t1);
3990
            gen_helper_store_msr(t0);
3991
            tcg_temp_free(t0);
3992
        } else
3993
#endif
3994
            gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3995
        /* Must stop the translation as machine state (may have) changed */
3996
        /* Note that mtmsr is not always defined as context-synchronizing */
3997
        ctx->exception = POWERPC_EXCP_STOP;
3998
    }
3999
#endif
4000
}
4001

    
4002
/* mtspr */
4003
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4004
{
4005
    void (*write_cb)(void *opaque, int sprn, int gprn);
4006
    uint32_t sprn = SPR(ctx->opcode);
4007

    
4008
#if !defined(CONFIG_USER_ONLY)
4009
    if (ctx->mem_idx == 2)
4010
        write_cb = ctx->spr_cb[sprn].hea_write;
4011
    else if (ctx->mem_idx)
4012
        write_cb = ctx->spr_cb[sprn].oea_write;
4013
    else
4014
#endif
4015
        write_cb = ctx->spr_cb[sprn].uea_write;
4016
    if (likely(write_cb != NULL)) {
4017
        if (likely(write_cb != SPR_NOACCESS)) {
4018
            (*write_cb)(ctx, sprn, rS(ctx->opcode));
4019
        } else {
4020
            /* Privilege exception */
4021
            if (loglevel != 0) {
4022
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
4023
                        ADDRX "\n", sprn, sprn, ctx->nip);
4024
            }
4025
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4026
                   sprn, sprn, ctx->nip);
4027
            GEN_EXCP_PRIVREG(ctx);
4028
        }
4029
    } else {
4030
        /* Not defined */
4031
        if (loglevel != 0) {
4032
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
4033
                    ADDRX "\n", sprn, sprn, ctx->nip);
4034
        }
4035
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4036
               sprn, sprn, ctx->nip);
4037
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
4038
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
4039
    }
4040
}
4041

    
4042
/***                         Cache management                              ***/
4043
/* dcbf */
4044
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4045
{
4046
    /* XXX: specification says this is treated as a load by the MMU */
4047
    TCGv t0;
4048
    gen_set_access_type(ctx, ACCESS_CACHE);
4049
    t0 = tcg_temp_new();
4050
    gen_addr_reg_index(ctx, t0);
4051
    gen_qemu_ld8u(ctx, t0, t0);
4052
    tcg_temp_free(t0);
4053
}
4054

    
4055
/* dcbi (Supervisor only) */
4056
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4057
{
4058
#if defined(CONFIG_USER_ONLY)
4059
    GEN_EXCP_PRIVOPC(ctx);
4060
#else
4061
    TCGv EA, val;
4062
    if (unlikely(!ctx->mem_idx)) {
4063
        GEN_EXCP_PRIVOPC(ctx);
4064
        return;
4065
    }
4066
    EA = tcg_temp_new();
4067
    gen_set_access_type(ctx, ACCESS_CACHE);
4068
    gen_addr_reg_index(ctx, EA);
4069
    val = tcg_temp_new();
4070
    /* XXX: specification says this should be treated as a store by the MMU */
4071
    gen_qemu_ld8u(ctx, val, EA);
4072
    gen_qemu_st8(ctx, val, EA);
4073
    tcg_temp_free(val);
4074
    tcg_temp_free(EA);
4075
#endif
4076
}
4077

    
4078
/* dcdst */
4079
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4080
{
4081
    /* XXX: specification say this is treated as a load by the MMU */
4082
    TCGv t0;
4083
    gen_set_access_type(ctx, ACCESS_CACHE);
4084
    t0 = tcg_temp_new();
4085
    gen_addr_reg_index(ctx, t0);
4086
    gen_qemu_ld8u(ctx, t0, t0);
4087
    tcg_temp_free(t0);
4088
}
4089

    
4090
/* dcbt */
4091
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4092
{
4093
    /* interpreted as no-op */
4094
    /* XXX: specification say this is treated as a load by the MMU
4095
     *      but does not generate any exception
4096
     */
4097
}
4098

    
4099
/* dcbtst */
4100
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4101
{
4102
    /* interpreted as no-op */
4103
    /* XXX: specification say this is treated as a load by the MMU
4104
     *      but does not generate any exception
4105
     */
4106
}
4107

    
4108
/* dcbz */
4109
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4110
{
4111
    TCGv t0;
4112
    gen_set_access_type(ctx, ACCESS_CACHE);
4113
    /* NIP cannot be restored if the memory exception comes from an helper */
4114
    gen_update_nip(ctx, ctx->nip - 4);
4115
    t0 = tcg_temp_new();
4116
    gen_addr_reg_index(ctx, t0);
4117
    gen_helper_dcbz(t0);
4118
    tcg_temp_free(t0);
4119
}
4120

    
4121
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4122
{
4123
    TCGv t0;
4124
    gen_set_access_type(ctx, ACCESS_CACHE);
4125
    /* NIP cannot be restored if the memory exception comes from an helper */
4126
    gen_update_nip(ctx, ctx->nip - 4);
4127
    t0 = tcg_temp_new();
4128
    gen_addr_reg_index(ctx, t0);
4129
    if (ctx->opcode & 0x00200000)
4130
        gen_helper_dcbz(t0);
4131
    else
4132
        gen_helper_dcbz_970(t0);
4133
    tcg_temp_free(t0);
4134
}
4135

    
4136
/* icbi */
4137
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4138
{
4139
    TCGv t0;
4140
    gen_set_access_type(ctx, ACCESS_CACHE);
4141
    /* NIP cannot be restored if the memory exception comes from an helper */
4142
    gen_update_nip(ctx, ctx->nip - 4);
4143
    t0 = tcg_temp_new();
4144
    gen_addr_reg_index(ctx, t0);
4145
    gen_helper_icbi(t0);
4146
    tcg_temp_free(t0);
4147
}
4148

    
4149
/* Optional: */
4150
/* dcba */
4151
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4152
{
4153
    /* interpreted as no-op */
4154
    /* XXX: specification say this is treated as a store by the MMU
4155
     *      but does not generate any exception
4156
     */
4157
}
4158

    
4159
/***                    Segment register manipulation                      ***/
4160
/* Supervisor only: */
4161
/* mfsr */
4162
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4163
{
4164
#if defined(CONFIG_USER_ONLY)
4165
    GEN_EXCP_PRIVREG(ctx);
4166
#else
4167
    TCGv t0;
4168
    if (unlikely(!ctx->mem_idx)) {
4169
        GEN_EXCP_PRIVREG(ctx);
4170
        return;
4171
    }
4172
    t0 = tcg_const_tl(SR(ctx->opcode));
4173
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4174
    tcg_temp_free(t0);
4175
#endif
4176
}
4177

    
4178
/* mfsrin */
4179
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4180
{
4181
#if defined(CONFIG_USER_ONLY)
4182
    GEN_EXCP_PRIVREG(ctx);
4183
#else
4184
    TCGv t0;
4185
    if (unlikely(!ctx->mem_idx)) {
4186
        GEN_EXCP_PRIVREG(ctx);
4187
        return;
4188
    }
4189
    t0 = tcg_temp_new();
4190
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4191
    tcg_gen_andi_tl(t0, t0, 0xF);
4192
    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4193
    tcg_temp_free(t0);
4194
#endif
4195
}
4196

    
4197
/* mtsr */
4198
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4199
{
4200
#if defined(CONFIG_USER_ONLY)
4201
    GEN_EXCP_PRIVREG(ctx);
4202
#else
4203
    TCGv t0;
4204
    if (unlikely(!ctx->mem_idx)) {
4205
        GEN_EXCP_PRIVREG(ctx);
4206
        return;
4207
    }
4208
    t0 = tcg_const_tl(SR(ctx->opcode));
4209
    gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
4210
    tcg_temp_free(t0);
4211
#endif
4212
}
4213

    
4214
/* mtsrin */
4215
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4216
{
4217
#if defined(CONFIG_USER_ONLY)
4218
    GEN_EXCP_PRIVREG(ctx);
4219
#else
4220
    TCGv t0;
4221
    if (unlikely(!ctx->mem_idx)) {
4222
        GEN_EXCP_PRIVREG(ctx);
4223
        return;
4224
    }
4225
    t0 = tcg_temp_new();
4226
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4227
    tcg_gen_andi_tl(t0, t0, 0xF);
4228
    gen_helper_store_sr(t0, cpu_gpr[rD(ctx->opcode)]);
4229
    tcg_temp_free(t0);
4230
#endif
4231
}
4232

    
4233
#if defined(TARGET_PPC64)
4234
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4235
/* mfsr */
4236
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4237
{
4238
#if defined(CONFIG_USER_ONLY)
4239
    GEN_EXCP_PRIVREG(ctx);
4240
#else
4241
    TCGv t0;
4242
    if (unlikely(!ctx->mem_idx)) {
4243
        GEN_EXCP_PRIVREG(ctx);
4244
        return;
4245
    }
4246
    t0 = tcg_const_tl(SR(ctx->opcode));
4247
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4248
    tcg_temp_free(t0);
4249
#endif
4250
}
4251

    
4252
/* mfsrin */
4253
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4254
             PPC_SEGMENT_64B)
4255
{
4256
#if defined(CONFIG_USER_ONLY)
4257
    GEN_EXCP_PRIVREG(ctx);
4258
#else
4259
    TCGv t0;
4260
    if (unlikely(!ctx->mem_idx)) {
4261
        GEN_EXCP_PRIVREG(ctx);
4262
        return;
4263
    }
4264
    t0 = tcg_temp_new();
4265
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4266
    tcg_gen_andi_tl(t0, t0, 0xF);
4267
    gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4268
    tcg_temp_free(t0);
4269
#endif
4270
}
4271

    
4272
/* mtsr */
4273
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4274
{
4275
#if defined(CONFIG_USER_ONLY)
4276
    GEN_EXCP_PRIVREG(ctx);
4277
#else
4278
    TCGv t0;
4279
    if (unlikely(!ctx->mem_idx)) {
4280
        GEN_EXCP_PRIVREG(ctx);
4281
        return;
4282
    }
4283
    t0 = tcg_const_tl(SR(ctx->opcode));
4284
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4285
    tcg_temp_free(t0);
4286
#endif
4287
}
4288

    
4289
/* mtsrin */
4290
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4291
             PPC_SEGMENT_64B)
4292
{
4293
#if defined(CONFIG_USER_ONLY)
4294
    GEN_EXCP_PRIVREG(ctx);
4295
#else
4296
    TCGv t0;
4297
    if (unlikely(!ctx->mem_idx)) {
4298
        GEN_EXCP_PRIVREG(ctx);
4299
        return;
4300
    }
4301
    t0 = tcg_temp_new();
4302
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4303
    tcg_gen_andi_tl(t0, t0, 0xF);
4304
    gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4305
    tcg_temp_free(t0);
4306
#endif
4307
}
4308
#endif /* defined(TARGET_PPC64) */
4309

    
4310
/***                      Lookaside buffer management                      ***/
4311
/* Optional & mem_idx only: */
4312
/* tlbia */
4313
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4314
{
4315
#if defined(CONFIG_USER_ONLY)
4316
    GEN_EXCP_PRIVOPC(ctx);
4317
#else
4318
    if (unlikely(!ctx->mem_idx)) {
4319
        GEN_EXCP_PRIVOPC(ctx);
4320
        return;
4321
    }
4322
    gen_helper_tlbia();
4323
#endif
4324
}
4325

    
4326
/* tlbie */
4327
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4328
{
4329
#if defined(CONFIG_USER_ONLY)
4330
    GEN_EXCP_PRIVOPC(ctx);
4331
#else
4332
    if (unlikely(!ctx->mem_idx)) {
4333
        GEN_EXCP_PRIVOPC(ctx);
4334
        return;
4335
    }
4336
#if defined(TARGET_PPC64)
4337
    if (!ctx->sf_mode) {
4338
        TCGv t0 = tcg_temp_new();
4339
        tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4340
        gen_helper_tlbie(t0);
4341
        tcg_temp_free(t0);
4342
    } else
4343
#endif
4344
        gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4345
#endif
4346
}
4347

    
4348
/* tlbsync */
4349
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4350
{
4351
#if defined(CONFIG_USER_ONLY)
4352
    GEN_EXCP_PRIVOPC(ctx);
4353
#else
4354
    if (unlikely(!ctx->mem_idx)) {
4355
        GEN_EXCP_PRIVOPC(ctx);
4356
        return;
4357
    }
4358
    /* This has no effect: it should ensure that all previous
4359
     * tlbie have completed
4360
     */
4361
    GEN_STOP(ctx);
4362
#endif
4363
}
4364

    
4365
#if defined(TARGET_PPC64)
4366
/* slbia */
4367
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4368
{
4369
#if defined(CONFIG_USER_ONLY)
4370
    GEN_EXCP_PRIVOPC(ctx);
4371
#else
4372
    if (unlikely(!ctx->mem_idx)) {
4373
        GEN_EXCP_PRIVOPC(ctx);
4374
        return;
4375
    }
4376
    gen_helper_slbia();
4377
#endif
4378
}
4379

    
4380
/* slbie */
4381
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4382
{
4383
#if defined(CONFIG_USER_ONLY)
4384
    GEN_EXCP_PRIVOPC(ctx);
4385
#else
4386
    if (unlikely(!ctx->mem_idx)) {
4387
        GEN_EXCP_PRIVOPC(ctx);
4388
        return;
4389
    }
4390
    gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
4391
#endif
4392
}
4393
#endif
4394

    
4395
/***                              External control                         ***/
4396
/* Optional: */
4397
/* eciwx */
4398
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4399
{
4400
    TCGv t0;
4401
    /* Should check EAR[E] ! */
4402
    gen_set_access_type(ctx, ACCESS_EXT);
4403
    t0 = tcg_temp_new();
4404
    gen_addr_reg_index(ctx, t0);
4405
    gen_check_align(ctx, t0, 0x03);
4406
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4407
    tcg_temp_free(t0);
4408
}
4409

    
4410
/* ecowx */
4411
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4412
{
4413
    TCGv t0;
4414
    /* Should check EAR[E] ! */
4415
    gen_set_access_type(ctx, ACCESS_EXT);
4416
    t0 = tcg_temp_new();
4417
    gen_addr_reg_index(ctx, t0);
4418
    gen_check_align(ctx, t0, 0x03);
4419
    gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4420
    tcg_temp_free(t0);
4421
}
4422

    
4423
/* PowerPC 601 specific instructions */
4424
/* abs - abs. */
4425
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4426
{
4427
    int l1 = gen_new_label();
4428
    int l2 = gen_new_label();
4429
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4430
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4431
    tcg_gen_br(l2);
4432
    gen_set_label(l1);
4433
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4434
    gen_set_label(l2);
4435
    if (unlikely(Rc(ctx->opcode) != 0))
4436
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4437
}
4438

    
4439
/* abso - abso. */
4440
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4441
{
4442
    int l1 = gen_new_label();
4443
    int l2 = gen_new_label();
4444
    int l3 = gen_new_label();
4445
    /* Start with XER OV disabled, the most likely case */
4446
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4447
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4448
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4449
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4450
    tcg_gen_br(l2);
4451
    gen_set_label(l1);
4452
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4453
    tcg_gen_br(l3);
4454
    gen_set_label(l2);
4455
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4456
    gen_set_label(l3);
4457
    if (unlikely(Rc(ctx->opcode) != 0))
4458
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4459
}
4460

    
4461
/* clcs */
4462
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4463
{
4464
    TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4465
    gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
4466
    tcg_temp_free_i32(t0);
4467
    /* Rc=1 sets CR0 to an undefined state */
4468
}
4469

    
4470
/* div - div. */
4471
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4472
{
4473
    gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4474
    if (unlikely(Rc(ctx->opcode) != 0))
4475
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4476
}
4477

    
4478
/* divo - divo. */
4479
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4480
{
4481
    gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4482
    if (unlikely(Rc(ctx->opcode) != 0))
4483
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4484
}
4485

    
4486
/* divs - divs. */
4487
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4488
{
4489
    gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4490
    if (unlikely(Rc(ctx->opcode) != 0))
4491
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4492
}
4493

    
4494
/* divso - divso. */
4495
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4496
{
4497
    gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4498
    if (unlikely(Rc(ctx->opcode) != 0))
4499
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4500
}
4501

    
4502
/* doz - doz. */
4503
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4504
{
4505
    int l1 = gen_new_label();
4506
    int l2 = gen_new_label();
4507
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4508
    tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4509
    tcg_gen_br(l2);
4510
    gen_set_label(l1);
4511
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4512
    gen_set_label(l2);
4513
    if (unlikely(Rc(ctx->opcode) != 0))
4514
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4515
}
4516

    
4517
/* dozo - dozo. */
4518
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4519
{
4520
    int l1 = gen_new_label();
4521
    int l2 = gen_new_label();
4522
    TCGv t0 = tcg_temp_new();
4523
    TCGv t1 = tcg_temp_new();
4524
    TCGv t2 = tcg_temp_new();
4525
    /* Start with XER OV disabled, the most likely case */
4526
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4527
    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4528
    tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4529
    tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4530
    tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4531
    tcg_gen_andc_tl(t1, t1, t2);
4532
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4533
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4534
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4535
    tcg_gen_br(l2);
4536
    gen_set_label(l1);
4537
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4538
    gen_set_label(l2);
4539
    tcg_temp_free(t0);
4540
    tcg_temp_free(t1);
4541
    tcg_temp_free(t2);
4542
    if (unlikely(Rc(ctx->opcode) != 0))
4543
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4544
}
4545

    
4546
/* dozi */
4547
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4548
{
4549
    target_long simm = SIMM(ctx->opcode);
4550
    int l1 = gen_new_label();
4551
    int l2 = gen_new_label();
4552
    tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4553
    tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4554
    tcg_gen_br(l2);
4555
    gen_set_label(l1);
4556
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4557
    gen_set_label(l2);
4558
    if (unlikely(Rc(ctx->opcode) != 0))
4559
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4560
}
4561

    
4562
/* lscbx - lscbx. */
4563
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4564
{
4565
    TCGv t0 = tcg_temp_new();
4566
    TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4567
    TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4568
    TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4569

    
4570
    gen_addr_reg_index(ctx, t0);
4571
    /* NIP cannot be restored if the memory exception comes from an helper */
4572
    gen_update_nip(ctx, ctx->nip - 4);
4573
    gen_helper_lscbx(t0, t0, t1, t2, t3);
4574
    tcg_temp_free_i32(t1);
4575
    tcg_temp_free_i32(t2);
4576
    tcg_temp_free_i32(t3);
4577
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4578
    tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4579
    if (unlikely(Rc(ctx->opcode) != 0))
4580
        gen_set_Rc0(ctx, t0);
4581
    tcg_temp_free(t0);
4582
}
4583

    
4584
/* maskg - maskg. */
4585
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4586
{
4587
    int l1 = gen_new_label();
4588
    TCGv t0 = tcg_temp_new();
4589
    TCGv t1 = tcg_temp_new();
4590
    TCGv t2 = tcg_temp_new();
4591
    TCGv t3 = tcg_temp_new();
4592
    tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4593
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4594
    tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4595
    tcg_gen_addi_tl(t2, t0, 1);
4596
    tcg_gen_shr_tl(t2, t3, t2);
4597
    tcg_gen_shr_tl(t3, t3, t1);
4598
    tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4599
    tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4600
    tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4601
    gen_set_label(l1);
4602
    tcg_temp_free(t0);
4603
    tcg_temp_free(t1);
4604
    tcg_temp_free(t2);
4605
    tcg_temp_free(t3);
4606
    if (unlikely(Rc(ctx->opcode) != 0))
4607
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4608
}
4609

    
4610
/* maskir - maskir. */
4611
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4612
{
4613
    TCGv t0 = tcg_temp_new();
4614
    TCGv t1 = tcg_temp_new();
4615
    tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4616
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4617
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4618
    tcg_temp_free(t0);
4619
    tcg_temp_free(t1);
4620
    if (unlikely(Rc(ctx->opcode) != 0))
4621
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4622
}
4623

    
4624
/* mul - mul. */
4625
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4626
{
4627
    TCGv_i64 t0 = tcg_temp_new_i64();
4628
    TCGv_i64 t1 = tcg_temp_new_i64();
4629
    TCGv t2 = tcg_temp_new();
4630
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4631
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4632
    tcg_gen_mul_i64(t0, t0, t1);
4633
    tcg_gen_trunc_i64_tl(t2, t0);
4634
    gen_store_spr(SPR_MQ, t2);
4635
    tcg_gen_shri_i64(t1, t0, 32);
4636
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4637
    tcg_temp_free_i64(t0);
4638
    tcg_temp_free_i64(t1);
4639
    tcg_temp_free(t2);
4640
    if (unlikely(Rc(ctx->opcode) != 0))
4641
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4642
}
4643

    
4644
/* mulo - mulo. */
4645
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4646
{
4647
    int l1 = gen_new_label();
4648
    TCGv_i64 t0 = tcg_temp_new_i64();
4649
    TCGv_i64 t1 = tcg_temp_new_i64();
4650
    TCGv t2 = tcg_temp_new();
4651
    /* Start with XER OV disabled, the most likely case */
4652
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4653
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4654
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4655
    tcg_gen_mul_i64(t0, t0, t1);
4656
    tcg_gen_trunc_i64_tl(t2, t0);
4657
    gen_store_spr(SPR_MQ, t2);
4658
    tcg_gen_shri_i64(t1, t0, 32);
4659
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4660
    tcg_gen_ext32s_i64(t1, t0);
4661
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
4662
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4663
    gen_set_label(l1);
4664
    tcg_temp_free_i64(t0);
4665
    tcg_temp_free_i64(t1);
4666
    tcg_temp_free(t2);
4667
    if (unlikely(Rc(ctx->opcode) != 0))
4668
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4669
}
4670

    
4671
/* nabs - nabs. */
4672
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4673
{
4674
    int l1 = gen_new_label();
4675
    int l2 = gen_new_label();
4676
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4677
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4678
    tcg_gen_br(l2);
4679
    gen_set_label(l1);
4680
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4681
    gen_set_label(l2);
4682
    if (unlikely(Rc(ctx->opcode) != 0))
4683
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4684
}
4685

    
4686
/* nabso - nabso. */
4687
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4688
{
4689
    int l1 = gen_new_label();
4690
    int l2 = gen_new_label();
4691
    tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4692
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4693
    tcg_gen_br(l2);
4694
    gen_set_label(l1);
4695
    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4696
    gen_set_label(l2);
4697
    /* nabs never overflows */
4698
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4699
    if (unlikely(Rc(ctx->opcode) != 0))
4700
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4701
}
4702

    
4703
/* rlmi - rlmi. */
4704
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4705
{
4706
    uint32_t mb = MB(ctx->opcode);
4707
    uint32_t me = ME(ctx->opcode);
4708
    TCGv t0 = tcg_temp_new();
4709
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4710
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4711
    tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4712
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4713
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4714
    tcg_temp_free(t0);
4715
    if (unlikely(Rc(ctx->opcode) != 0))
4716
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4717
}
4718

    
4719
/* rrib - rrib. */
4720
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4721
{
4722
    TCGv t0 = tcg_temp_new();
4723
    TCGv t1 = tcg_temp_new();
4724
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4725
    tcg_gen_movi_tl(t1, 0x80000000);
4726
    tcg_gen_shr_tl(t1, t1, t0);
4727
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4728
    tcg_gen_and_tl(t0, t0, t1);
4729
    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4730
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4731
    tcg_temp_free(t0);
4732
    tcg_temp_free(t1);
4733
    if (unlikely(Rc(ctx->opcode) != 0))
4734
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4735
}
4736

    
4737
/* sle - sle. */
4738
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4739
{
4740
    TCGv t0 = tcg_temp_new();
4741
    TCGv t1 = tcg_temp_new();
4742
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4743
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4744
    tcg_gen_subfi_tl(t1, 32, t1);
4745
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4746
    tcg_gen_or_tl(t1, t0, t1);
4747
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4748
    gen_store_spr(SPR_MQ, t1);
4749
    tcg_temp_free(t0);
4750
    tcg_temp_free(t1);
4751
    if (unlikely(Rc(ctx->opcode) != 0))
4752
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4753
}
4754

    
4755
/* sleq - sleq. */
4756
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4757
{
4758
    TCGv t0 = tcg_temp_new();
4759
    TCGv t1 = tcg_temp_new();
4760
    TCGv t2 = tcg_temp_new();
4761
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4762
    tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4763
    tcg_gen_shl_tl(t2, t2, t0);
4764
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4765
    gen_load_spr(t1, SPR_MQ);
4766
    gen_store_spr(SPR_MQ, t0);
4767
    tcg_gen_and_tl(t0, t0, t2);
4768
    tcg_gen_andc_tl(t1, t1, t2);
4769
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4770
    tcg_temp_free(t0);
4771
    tcg_temp_free(t1);
4772
    tcg_temp_free(t2);
4773
    if (unlikely(Rc(ctx->opcode) != 0))
4774
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4775
}
4776

    
4777
/* sliq - sliq. */
4778
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4779
{
4780
    int sh = SH(ctx->opcode);
4781
    TCGv t0 = tcg_temp_new();
4782
    TCGv t1 = tcg_temp_new();
4783
    tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4784
    tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4785
    tcg_gen_or_tl(t1, t0, t1);
4786
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4787
    gen_store_spr(SPR_MQ, t1);
4788
    tcg_temp_free(t0);
4789
    tcg_temp_free(t1);
4790
    if (unlikely(Rc(ctx->opcode) != 0))
4791
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4792
}
4793

    
4794
/* slliq - slliq. */
4795
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4796
{
4797
    int sh = SH(ctx->opcode);
4798
    TCGv t0 = tcg_temp_new();
4799
    TCGv t1 = tcg_temp_new();
4800
    tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4801
    gen_load_spr(t1, SPR_MQ);
4802
    gen_store_spr(SPR_MQ, t0);
4803
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
4804
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4805
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4806
    tcg_temp_free(t0);
4807
    tcg_temp_free(t1);
4808
    if (unlikely(Rc(ctx->opcode) != 0))
4809
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4810
}
4811

    
4812
/* sllq - sllq. */
4813
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4814
{
4815
    int l1 = gen_new_label();
4816
    int l2 = gen_new_label();
4817
    TCGv t0 = tcg_temp_local_new();
4818
    TCGv t1 = tcg_temp_local_new();
4819
    TCGv t2 = tcg_temp_local_new();
4820
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4821
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4822
    tcg_gen_shl_tl(t1, t1, t2);
4823
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4824
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4825
    gen_load_spr(t0, SPR_MQ);
4826
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4827
    tcg_gen_br(l2);
4828
    gen_set_label(l1);
4829
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4830
    gen_load_spr(t2, SPR_MQ);
4831
    tcg_gen_andc_tl(t1, t2, t1);
4832
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4833
    gen_set_label(l2);
4834
    tcg_temp_free(t0);
4835
    tcg_temp_free(t1);
4836
    tcg_temp_free(t2);
4837
    if (unlikely(Rc(ctx->opcode) != 0))
4838
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4839
}
4840

    
4841
/* slq - slq. */
4842
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4843
{
4844
    int l1 = gen_new_label();
4845
    TCGv t0 = tcg_temp_new();
4846
    TCGv t1 = tcg_temp_new();
4847
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4848
    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4849
    tcg_gen_subfi_tl(t1, 32, t1);
4850
    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4851
    tcg_gen_or_tl(t1, t0, t1);
4852
    gen_store_spr(SPR_MQ, t1);
4853
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4854
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4855
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4856
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4857
    gen_set_label(l1);
4858
    tcg_temp_free(t0);
4859
    tcg_temp_free(t1);
4860
    if (unlikely(Rc(ctx->opcode) != 0))
4861
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4862
}
4863

    
4864
/* sraiq - sraiq. */
4865
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4866
{
4867
    int sh = SH(ctx->opcode);
4868
    int l1 = gen_new_label();
4869
    TCGv t0 = tcg_temp_new();
4870
    TCGv t1 = tcg_temp_new();
4871
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4872
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4873
    tcg_gen_or_tl(t0, t0, t1);
4874
    gen_store_spr(SPR_MQ, t0);
4875
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4876
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4877
    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
4878
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4879
    gen_set_label(l1);
4880
    tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4881
    tcg_temp_free(t0);
4882
    tcg_temp_free(t1);
4883
    if (unlikely(Rc(ctx->opcode) != 0))
4884
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4885
}
4886

    
4887
/* sraq - sraq. */
4888
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4889
{
4890
    int l1 = gen_new_label();
4891
    int l2 = gen_new_label();
4892
    TCGv t0 = tcg_temp_new();
4893
    TCGv t1 = tcg_temp_local_new();
4894
    TCGv t2 = tcg_temp_local_new();
4895
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4896
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4897
    tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4898
    tcg_gen_subfi_tl(t2, 32, t2);
4899
    tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4900
    tcg_gen_or_tl(t0, t0, t2);
4901
    gen_store_spr(SPR_MQ, t0);
4902
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4903
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4904
    tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4905
    tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4906
    gen_set_label(l1);
4907
    tcg_temp_free(t0);
4908
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
4909
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4910
    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4911
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
4912
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4913
    gen_set_label(l2);
4914
    tcg_temp_free(t1);
4915
    tcg_temp_free(t2);
4916
    if (unlikely(Rc(ctx->opcode) != 0))
4917
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4918
}
4919

    
4920
/* sre - sre. */
4921
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4922
{
4923
    TCGv t0 = tcg_temp_new();
4924
    TCGv t1 = tcg_temp_new();
4925
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4926
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4927
    tcg_gen_subfi_tl(t1, 32, t1);
4928
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4929
    tcg_gen_or_tl(t1, t0, t1);
4930
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4931
    gen_store_spr(SPR_MQ, t1);
4932
    tcg_temp_free(t0);
4933
    tcg_temp_free(t1);
4934
    if (unlikely(Rc(ctx->opcode) != 0))
4935
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4936
}
4937

    
4938
/* srea - srea. */
4939
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4940
{
4941
    TCGv t0 = tcg_temp_new();
4942
    TCGv t1 = tcg_temp_new();
4943
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4944
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4945
    gen_store_spr(SPR_MQ, t0);
4946
    tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4947
    tcg_temp_free(t0);
4948
    tcg_temp_free(t1);
4949
    if (unlikely(Rc(ctx->opcode) != 0))
4950
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4951
}
4952

    
4953
/* sreq */
4954
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4955
{
4956
    TCGv t0 = tcg_temp_new();
4957
    TCGv t1 = tcg_temp_new();
4958
    TCGv t2 = tcg_temp_new();
4959
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4960
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4961
    tcg_gen_shr_tl(t1, t1, t0);
4962
    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4963
    gen_load_spr(t2, SPR_MQ);
4964
    gen_store_spr(SPR_MQ, t0);
4965
    tcg_gen_and_tl(t0, t0, t1);
4966
    tcg_gen_andc_tl(t2, t2, t1);
4967
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4968
    tcg_temp_free(t0);
4969
    tcg_temp_free(t1);
4970
    tcg_temp_free(t2);
4971
    if (unlikely(Rc(ctx->opcode) != 0))
4972
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4973
}
4974

    
4975
/* sriq */
4976
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4977
{
4978
    int sh = SH(ctx->opcode);
4979
    TCGv t0 = tcg_temp_new();
4980
    TCGv t1 = tcg_temp_new();
4981
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4982
    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4983
    tcg_gen_or_tl(t1, t0, t1);
4984
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4985
    gen_store_spr(SPR_MQ, t1);
4986
    tcg_temp_free(t0);
4987
    tcg_temp_free(t1);
4988
    if (unlikely(Rc(ctx->opcode) != 0))
4989
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4990
}
4991

    
4992
/* srliq */
4993
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4994
{
4995
    int sh = SH(ctx->opcode);
4996
    TCGv t0 = tcg_temp_new();
4997
    TCGv t1 = tcg_temp_new();
4998
    tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4999
    gen_load_spr(t1, SPR_MQ);
5000
    gen_store_spr(SPR_MQ, t0);
5001
    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5002
    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5003
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5004
    tcg_temp_free(t0);
5005
    tcg_temp_free(t1);
5006
    if (unlikely(Rc(ctx->opcode) != 0))
5007
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5008
}
5009

    
5010
/* srlq */
5011
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
5012
{
5013
    int l1 = gen_new_label();
5014
    int l2 = gen_new_label();
5015
    TCGv t0 = tcg_temp_local_new();
5016
    TCGv t1 = tcg_temp_local_new();
5017
    TCGv t2 = tcg_temp_local_new();
5018
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5019
    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5020
    tcg_gen_shr_tl(t2, t1, t2);
5021
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5022
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5023
    gen_load_spr(t0, SPR_MQ);
5024
    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5025
    tcg_gen_br(l2);
5026
    gen_set_label(l1);
5027
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5028
    tcg_gen_and_tl(t0, t0, t2);
5029
    gen_load_spr(t1, SPR_MQ);
5030
    tcg_gen_andc_tl(t1, t1, t2);
5031
    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5032
    gen_set_label(l2);
5033
    tcg_temp_free(t0);
5034
    tcg_temp_free(t1);
5035
    tcg_temp_free(t2);
5036
    if (unlikely(Rc(ctx->opcode) != 0))
5037
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5038
}
5039

    
5040
/* srq */
5041
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
5042
{
5043
    int l1 = gen_new_label();
5044
    TCGv t0 = tcg_temp_new();
5045
    TCGv t1 = tcg_temp_new();
5046
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5047
    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5048
    tcg_gen_subfi_tl(t1, 32, t1);
5049
    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5050
    tcg_gen_or_tl(t1, t0, t1);
5051
    gen_store_spr(SPR_MQ, t1);
5052
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5053
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5054
    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5055
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5056
    gen_set_label(l1);
5057
    tcg_temp_free(t0);
5058
    tcg_temp_free(t1);
5059
    if (unlikely(Rc(ctx->opcode) != 0))
5060
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5061
}
5062

    
5063
/* PowerPC 602 specific instructions */
5064
/* dsa  */
5065
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
5066
{
5067
    /* XXX: TODO */
5068
    GEN_EXCP_INVAL(ctx);
5069
}
5070

    
5071
/* esa */
5072
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
5073
{
5074
    /* XXX: TODO */
5075
    GEN_EXCP_INVAL(ctx);
5076
}
5077

    
5078
/* mfrom */
5079
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
5080
{
5081
#if defined(CONFIG_USER_ONLY)
5082
    GEN_EXCP_PRIVOPC(ctx);
5083
#else
5084
    if (unlikely(!ctx->mem_idx)) {
5085
        GEN_EXCP_PRIVOPC(ctx);
5086
        return;
5087
    }
5088
    gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5089
#endif
5090
}
5091

    
5092
/* 602 - 603 - G2 TLB management */
5093
/* tlbld */
5094
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5095
{
5096
#if defined(CONFIG_USER_ONLY)
5097
    GEN_EXCP_PRIVOPC(ctx);
5098
#else
5099
    if (unlikely(!ctx->mem_idx)) {
5100
        GEN_EXCP_PRIVOPC(ctx);
5101
        return;
5102
    }
5103
    gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5104
#endif
5105
}
5106

    
5107
/* tlbli */
5108
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5109
{
5110
#if defined(CONFIG_USER_ONLY)
5111
    GEN_EXCP_PRIVOPC(ctx);
5112
#else
5113
    if (unlikely(!ctx->mem_idx)) {
5114
        GEN_EXCP_PRIVOPC(ctx);
5115
        return;
5116
    }
5117
    gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5118
#endif
5119
}
5120

    
5121
/* 74xx TLB management */
5122
/* tlbld */
5123
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5124
{
5125
#if defined(CONFIG_USER_ONLY)
5126
    GEN_EXCP_PRIVOPC(ctx);
5127
#else
5128
    if (unlikely(!ctx->mem_idx)) {
5129
        GEN_EXCP_PRIVOPC(ctx);
5130
        return;
5131
    }
5132
    gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5133
#endif
5134
}
5135

    
5136
/* tlbli */
5137
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5138
{
5139
#if defined(CONFIG_USER_ONLY)
5140
    GEN_EXCP_PRIVOPC(ctx);
5141
#else
5142
    if (unlikely(!ctx->mem_idx)) {
5143
        GEN_EXCP_PRIVOPC(ctx);
5144
        return;
5145
    }
5146
    gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5147
#endif
5148
}
5149

    
5150
/* POWER instructions not in PowerPC 601 */
5151
/* clf */
5152
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5153
{
5154
    /* Cache line flush: implemented as no-op */
5155
}
5156

    
5157
/* cli */
5158
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5159
{
5160
    /* Cache line invalidate: privileged and treated as no-op */
5161
#if defined(CONFIG_USER_ONLY)
5162
    GEN_EXCP_PRIVOPC(ctx);
5163
#else
5164
    if (unlikely(!ctx->mem_idx)) {
5165
        GEN_EXCP_PRIVOPC(ctx);
5166
        return;
5167
    }
5168
#endif
5169
}
5170

    
5171
/* dclst */
5172
GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
5173
{
5174
    /* Data cache line store: treated as no-op */
5175
}
5176

    
5177
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5178
{
5179
#if defined(CONFIG_USER_ONLY)
5180
    GEN_EXCP_PRIVOPC(ctx);
5181
#else
5182
    int ra = rA(ctx->opcode);
5183
    int rd = rD(ctx->opcode);
5184
    TCGv t0;
5185
    if (unlikely(!ctx->mem_idx)) {
5186
        GEN_EXCP_PRIVOPC(ctx);
5187
        return;
5188
    }
5189
    t0 = tcg_temp_new();
5190
    gen_addr_reg_index(ctx, t0);
5191
    tcg_gen_shri_tl(t0, t0, 28);
5192
    tcg_gen_andi_tl(t0, t0, 0xF);
5193
    gen_helper_load_sr(cpu_gpr[rd], t0);
5194
    tcg_temp_free(t0);
5195
    if (ra != 0 && ra != rd)
5196
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5197
#endif
5198
}
5199

    
5200
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5201
{
5202
#if defined(CONFIG_USER_ONLY)
5203
    GEN_EXCP_PRIVOPC(ctx);
5204
#else
5205
    TCGv t0;
5206
    if (unlikely(!ctx->mem_idx)) {
5207
        GEN_EXCP_PRIVOPC(ctx);
5208
        return;
5209
    }
5210
    t0 = tcg_temp_new();
5211
    gen_addr_reg_index(ctx, t0);
5212
    gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
5213
    tcg_temp_free(t0);
5214
#endif
5215
}
5216

    
5217
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5218
{
5219
#if defined(CONFIG_USER_ONLY)
5220
    GEN_EXCP_PRIVOPC(ctx);
5221
#else
5222
    if (unlikely(!ctx->mem_idx)) {
5223
        GEN_EXCP_PRIVOPC(ctx);
5224
        return;
5225
    }
5226
    gen_helper_rfsvc();
5227
    GEN_SYNC(ctx);
5228
#endif
5229
}
5230

    
5231
/* svc is not implemented for now */
5232

    
5233
/* POWER2 specific instructions */
5234
/* Quad manipulation (load/store two floats at a time) */
5235

    
5236
/* lfq */
5237
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5238
{
5239
    int rd = rD(ctx->opcode);
5240
    TCGv t0;
5241
    gen_set_access_type(ctx, ACCESS_FLOAT);
5242
    t0 = tcg_temp_new();
5243
    gen_addr_imm_index(ctx, t0, 0);
5244
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5245
    gen_addr_add(ctx, t0, t0, 8);
5246
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5247
    tcg_temp_free(t0);
5248
}
5249

    
5250
/* lfqu */
5251
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5252
{
5253
    int ra = rA(ctx->opcode);
5254
    int rd = rD(ctx->opcode);
5255
    TCGv t0, t1;
5256
    gen_set_access_type(ctx, ACCESS_FLOAT);
5257
    t0 = tcg_temp_new();
5258
    t1 = tcg_temp_new();
5259
    gen_addr_imm_index(ctx, t0, 0);
5260
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5261
    gen_addr_add(ctx, t1, t0, 8);
5262
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5263
    if (ra != 0)
5264
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
5265
    tcg_temp_free(t0);
5266
    tcg_temp_free(t1);
5267
}
5268

    
5269
/* lfqux */
5270
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5271
{
5272
    int ra = rA(ctx->opcode);
5273
    int rd = rD(ctx->opcode);
5274
    gen_set_access_type(ctx, ACCESS_FLOAT);
5275
    TCGv t0, t1;
5276
    t0 = tcg_temp_new();
5277
    gen_addr_reg_index(ctx, t0);
5278
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5279
    t1 = tcg_temp_new();
5280
    gen_addr_add(ctx, t1, t0, 8);
5281
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5282
    tcg_temp_free(t1);
5283
    if (ra != 0)
5284
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
5285
    tcg_temp_free(t0);
5286
}
5287

    
5288
/* lfqx */
5289
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5290
{
5291
    int rd = rD(ctx->opcode);
5292
    TCGv t0;
5293
    gen_set_access_type(ctx, ACCESS_FLOAT);
5294
    t0 = tcg_temp_new();
5295
    gen_addr_reg_index(ctx, t0);
5296
    gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5297
    gen_addr_add(ctx, t0, t0, 8);
5298
    gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5299
    tcg_temp_free(t0);
5300
}
5301

    
5302
/* stfq */
5303
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5304
{
5305
    int rd = rD(ctx->opcode);
5306
    TCGv t0;
5307
    gen_set_access_type(ctx, ACCESS_FLOAT);
5308
    t0 = tcg_temp_new();
5309
    gen_addr_imm_index(ctx, t0, 0);
5310
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5311
    gen_addr_add(ctx, t0, t0, 8);
5312
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5313
    tcg_temp_free(t0);
5314
}
5315

    
5316
/* stfqu */
5317
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5318
{
5319
    int ra = rA(ctx->opcode);
5320
    int rd = rD(ctx->opcode);
5321
    TCGv t0, t1;
5322
    gen_set_access_type(ctx, ACCESS_FLOAT);
5323
    t0 = tcg_temp_new();
5324
    gen_addr_imm_index(ctx, t0, 0);
5325
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5326
    t1 = tcg_temp_new();
5327
    gen_addr_add(ctx, t1, t0, 8);
5328
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5329
    tcg_temp_free(t1);
5330
    if (ra != 0)
5331
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
5332
    tcg_temp_free(t0);
5333
}
5334

    
5335
/* stfqux */
5336
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5337
{
5338
    int ra = rA(ctx->opcode);
5339
    int rd = rD(ctx->opcode);
5340
    TCGv t0, t1;
5341
    gen_set_access_type(ctx, ACCESS_FLOAT);
5342
    t0 = tcg_temp_new();
5343
    gen_addr_reg_index(ctx, t0);
5344
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5345
    t1 = tcg_temp_new();
5346
    gen_addr_add(ctx, t1, t0, 8);
5347
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5348
    tcg_temp_free(t1);
5349
    if (ra != 0)
5350
        tcg_gen_mov_tl(cpu_gpr[ra], t0);
5351
    tcg_temp_free(t0);
5352
}
5353

    
5354
/* stfqx */
5355
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5356
{
5357
    int rd = rD(ctx->opcode);
5358
    TCGv t0;
5359
    gen_set_access_type(ctx, ACCESS_FLOAT);
5360
    t0 = tcg_temp_new();
5361
    gen_addr_reg_index(ctx, t0);
5362
    gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5363
    gen_addr_add(ctx, t0, t0, 8);
5364
    gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5365
    tcg_temp_free(t0);
5366
}
5367

    
5368
/* BookE specific instructions */
5369
/* XXX: not implemented on 440 ? */
5370
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5371
{
5372
    /* XXX: TODO */
5373
    GEN_EXCP_INVAL(ctx);
5374
}
5375

    
5376
/* XXX: not implemented on 440 ? */
5377
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5378
{
5379
#if defined(CONFIG_USER_ONLY)
5380
    GEN_EXCP_PRIVOPC(ctx);
5381
#else
5382
    TCGv t0;
5383
    if (unlikely(!ctx->mem_idx)) {
5384
        GEN_EXCP_PRIVOPC(ctx);
5385
        return;
5386
    }
5387
    t0 = tcg_temp_new();
5388
    gen_addr_reg_index(ctx, t0);
5389
    gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
5390
    tcg_temp_free(t0);
5391
#endif
5392
}
5393

    
5394
/* All 405 MAC instructions are translated here */
5395
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5396
                                                int opc2, int opc3,
5397
                                                int ra, int rb, int rt, int Rc)
5398
{
5399
    TCGv t0, t1;
5400

    
5401
    t0 = tcg_temp_local_new();
5402
    t1 = tcg_temp_local_new();
5403

    
5404
    switch (opc3 & 0x0D) {
5405
    case 0x05:
5406
        /* macchw    - macchw.    - macchwo   - macchwo.   */
5407
        /* macchws   - macchws.   - macchwso  - macchwso.  */
5408
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5409
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5410
        /* mulchw - mulchw. */
5411
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5412
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5413
        tcg_gen_ext16s_tl(t1, t1);
5414
        break;
5415
    case 0x04:
5416
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5417
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5418
        /* mulchwu - mulchwu. */
5419
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5420
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5421
        tcg_gen_ext16u_tl(t1, t1);
5422
        break;
5423
    case 0x01:
5424
        /* machhw    - machhw.    - machhwo   - machhwo.   */
5425
        /* machhws   - machhws.   - machhwso  - machhwso.  */
5426
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5427
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5428
        /* mulhhw - mulhhw. */
5429
        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5430
        tcg_gen_ext16s_tl(t0, t0);
5431
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5432
        tcg_gen_ext16s_tl(t1, t1);
5433
        break;
5434
    case 0x00:
5435
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5436
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5437
        /* mulhhwu - mulhhwu. */
5438
        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5439
        tcg_gen_ext16u_tl(t0, t0);
5440
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5441
        tcg_gen_ext16u_tl(t1, t1);
5442
        break;
5443
    case 0x0D:
5444
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5445
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5446
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5447
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5448
        /* mullhw - mullhw. */
5449
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5450
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5451
        break;
5452
    case 0x0C:
5453
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5454
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5455
        /* mullhwu - mullhwu. */
5456
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5457
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5458
        break;
5459
    }
5460
    if (opc2 & 0x04) {
5461
        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5462
        tcg_gen_mul_tl(t1, t0, t1);
5463
        if (opc2 & 0x02) {
5464
            /* nmultiply-and-accumulate (0x0E) */
5465
            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5466
        } else {
5467
            /* multiply-and-accumulate (0x0C) */
5468
            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5469
        }
5470

    
5471
        if (opc3 & 0x12) {
5472
            /* Check overflow and/or saturate */
5473
            int l1 = gen_new_label();
5474

    
5475
            if (opc3 & 0x10) {
5476
                /* Start with XER OV disabled, the most likely case */
5477
                tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5478
            }
5479
            if (opc3 & 0x01) {
5480
                /* Signed */
5481
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5482
                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5483
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5484
                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5485
                if (opc3 & 0x02) {
5486
                    /* Saturate */
5487
                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5488
                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5489
                }
5490
            } else {
5491
                /* Unsigned */
5492
                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5493
                if (opc3 & 0x02) {
5494
                    /* Saturate */
5495
                    tcg_gen_movi_tl(t0, UINT32_MAX);
5496
                }
5497
            }
5498
            if (opc3 & 0x10) {
5499
                /* Check overflow */
5500
                tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5501
            }
5502
            gen_set_label(l1);
5503
            tcg_gen_mov_tl(cpu_gpr[rt], t0);
5504
        }
5505
    } else {
5506
        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5507
    }
5508
    tcg_temp_free(t0);
5509
    tcg_temp_free(t1);
5510
    if (unlikely(Rc) != 0) {
5511
        /* Update Rc0 */
5512
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5513
    }
5514
}
5515

    
5516
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5517
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5518
{                                                                             \
5519
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5520
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
5521
}
5522

    
5523
/* macchw    - macchw.    */
5524
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5525
/* macchwo   - macchwo.   */
5526
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5527
/* macchws   - macchws.   */
5528
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5529
/* macchwso  - macchwso.  */
5530
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5531
/* macchwsu  - macchwsu.  */
5532
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5533
/* macchwsuo - macchwsuo. */
5534
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5535
/* macchwu   - macchwu.   */
5536
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5537
/* macchwuo  - macchwuo.  */
5538
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5539
/* machhw    - machhw.    */
5540
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5541
/* machhwo   - machhwo.   */
5542
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5543
/* machhws   - machhws.   */
5544
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5545
/* machhwso  - machhwso.  */
5546
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5547
/* machhwsu  - machhwsu.  */
5548
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5549
/* machhwsuo - machhwsuo. */
5550
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5551
/* machhwu   - machhwu.   */
5552
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5553
/* machhwuo  - machhwuo.  */
5554
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5555
/* maclhw    - maclhw.    */
5556
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5557
/* maclhwo   - maclhwo.   */
5558
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5559
/* maclhws   - maclhws.   */
5560
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5561
/* maclhwso  - maclhwso.  */
5562
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5563
/* maclhwu   - maclhwu.   */
5564
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5565
/* maclhwuo  - maclhwuo.  */
5566
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5567
/* maclhwsu  - maclhwsu.  */
5568
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5569
/* maclhwsuo - maclhwsuo. */
5570
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5571
/* nmacchw   - nmacchw.   */
5572
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5573
/* nmacchwo  - nmacchwo.  */
5574
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5575
/* nmacchws  - nmacchws.  */
5576
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5577
/* nmacchwso - nmacchwso. */
5578
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5579
/* nmachhw   - nmachhw.   */
5580
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5581
/* nmachhwo  - nmachhwo.  */
5582
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5583
/* nmachhws  - nmachhws.  */
5584
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5585
/* nmachhwso - nmachhwso. */
5586
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5587
/* nmaclhw   - nmaclhw.   */
5588
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5589
/* nmaclhwo  - nmaclhwo.  */
5590
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5591
/* nmaclhws  - nmaclhws.  */
5592
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5593
/* nmaclhwso - nmaclhwso. */
5594
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5595

    
5596
/* mulchw  - mulchw.  */
5597
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5598
/* mulchwu - mulchwu. */
5599
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5600
/* mulhhw  - mulhhw.  */
5601
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5602
/* mulhhwu - mulhhwu. */
5603
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5604
/* mullhw  - mullhw.  */
5605
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5606
/* mullhwu - mullhwu. */
5607
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5608

    
5609
/* mfdcr */
5610
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5611
{
5612
#if defined(CONFIG_USER_ONLY)
5613
    GEN_EXCP_PRIVREG(ctx);
5614
#else
5615
    TCGv dcrn;
5616
    if (unlikely(!ctx->mem_idx)) {
5617
        GEN_EXCP_PRIVREG(ctx);
5618
        return;
5619
    }
5620
    /* NIP cannot be restored if the memory exception comes from an helper */
5621
    gen_update_nip(ctx, ctx->nip - 4);
5622
    dcrn = tcg_const_tl(SPR(ctx->opcode));
5623
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
5624
    tcg_temp_free(dcrn);
5625
#endif
5626
}
5627

    
5628
/* mtdcr */
5629
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5630
{
5631
#if defined(CONFIG_USER_ONLY)
5632
    GEN_EXCP_PRIVREG(ctx);
5633
#else
5634
    TCGv dcrn;
5635
    if (unlikely(!ctx->mem_idx)) {
5636
        GEN_EXCP_PRIVREG(ctx);
5637
        return;
5638
    }
5639
    /* NIP cannot be restored if the memory exception comes from an helper */
5640
    gen_update_nip(ctx, ctx->nip - 4);
5641
    dcrn = tcg_const_tl(SPR(ctx->opcode));
5642
    gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
5643
    tcg_temp_free(dcrn);
5644
#endif
5645
}
5646

    
5647
/* mfdcrx */
5648
/* XXX: not implemented on 440 ? */
5649
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5650
{
5651
#if defined(CONFIG_USER_ONLY)
5652
    GEN_EXCP_PRIVREG(ctx);
5653
#else
5654
    if (unlikely(!ctx->mem_idx)) {
5655
        GEN_EXCP_PRIVREG(ctx);
5656
        return;
5657
    }
5658
    /* NIP cannot be restored if the memory exception comes from an helper */
5659
    gen_update_nip(ctx, ctx->nip - 4);
5660
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5661
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5662
#endif
5663
}
5664

    
5665
/* mtdcrx */
5666
/* XXX: not implemented on 440 ? */
5667
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5668
{
5669
#if defined(CONFIG_USER_ONLY)
5670
    GEN_EXCP_PRIVREG(ctx);
5671
#else
5672
    if (unlikely(!ctx->mem_idx)) {
5673
        GEN_EXCP_PRIVREG(ctx);
5674
        return;
5675
    }
5676
    /* NIP cannot be restored if the memory exception comes from an helper */
5677
    gen_update_nip(ctx, ctx->nip - 4);
5678
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5679
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5680
#endif
5681
}
5682

    
5683
/* mfdcrux (PPC 460) : user-mode access to DCR */
5684
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5685
{
5686
    /* NIP cannot be restored if the memory exception comes from an helper */
5687
    gen_update_nip(ctx, ctx->nip - 4);
5688
    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5689
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5690
}
5691

    
5692
/* mtdcrux (PPC 460) : user-mode access to DCR */
5693
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5694
{
5695
    /* NIP cannot be restored if the memory exception comes from an helper */
5696
    gen_update_nip(ctx, ctx->nip - 4);
5697
    gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5698
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5699
}
5700

    
5701
/* dccci */
5702
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5703
{
5704
#if defined(CONFIG_USER_ONLY)
5705
    GEN_EXCP_PRIVOPC(ctx);
5706
#else
5707
    if (unlikely(!ctx->mem_idx)) {
5708
        GEN_EXCP_PRIVOPC(ctx);
5709
        return;
5710
    }
5711
    /* interpreted as no-op */
5712
#endif
5713
}
5714

    
5715
/* dcread */
5716
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5717
{
5718
#if defined(CONFIG_USER_ONLY)
5719
    GEN_EXCP_PRIVOPC(ctx);
5720
#else
5721
    TCGv EA, val;
5722
    if (unlikely(!ctx->mem_idx)) {
5723
        GEN_EXCP_PRIVOPC(ctx);
5724
        return;
5725
    }
5726
    gen_set_access_type(ctx, ACCESS_CACHE);
5727
    EA = tcg_temp_new();
5728
    gen_addr_reg_index(ctx, EA);
5729
    val = tcg_temp_new();
5730
    gen_qemu_ld32u(ctx, val, EA);
5731
    tcg_temp_free(val);
5732
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5733
    tcg_temp_free(EA);
5734
#endif
5735
}
5736

    
5737
/* icbt */
5738
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5739
{
5740
    /* interpreted as no-op */
5741
    /* XXX: specification say this is treated as a load by the MMU
5742
     *      but does not generate any exception
5743
     */
5744
}
5745

    
5746
/* iccci */
5747
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5748
{
5749
#if defined(CONFIG_USER_ONLY)
5750
    GEN_EXCP_PRIVOPC(ctx);
5751
#else
5752
    if (unlikely(!ctx->mem_idx)) {
5753
        GEN_EXCP_PRIVOPC(ctx);
5754
        return;
5755
    }
5756
    /* interpreted as no-op */
5757
#endif
5758
}
5759

    
5760
/* icread */
5761
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5762
{
5763
#if defined(CONFIG_USER_ONLY)
5764
    GEN_EXCP_PRIVOPC(ctx);
5765
#else
5766
    if (unlikely(!ctx->mem_idx)) {
5767
        GEN_EXCP_PRIVOPC(ctx);
5768
        return;
5769
    }
5770
    /* interpreted as no-op */
5771
#endif
5772
}
5773

    
5774
/* rfci (mem_idx only) */
5775
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5776
{
5777
#if defined(CONFIG_USER_ONLY)
5778
    GEN_EXCP_PRIVOPC(ctx);
5779
#else
5780
    if (unlikely(!ctx->mem_idx)) {
5781
        GEN_EXCP_PRIVOPC(ctx);
5782
        return;
5783
    }
5784
    /* Restore CPU state */
5785
    gen_helper_40x_rfci();
5786
    GEN_SYNC(ctx);
5787
#endif
5788
}
5789

    
5790
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5791
{
5792
#if defined(CONFIG_USER_ONLY)
5793
    GEN_EXCP_PRIVOPC(ctx);
5794
#else
5795
    if (unlikely(!ctx->mem_idx)) {
5796
        GEN_EXCP_PRIVOPC(ctx);
5797
        return;
5798
    }
5799
    /* Restore CPU state */
5800
    gen_helper_rfci();
5801
    GEN_SYNC(ctx);
5802
#endif
5803
}
5804

    
5805
/* BookE specific */
5806
/* XXX: not implemented on 440 ? */
5807
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5808
{
5809
#if defined(CONFIG_USER_ONLY)
5810
    GEN_EXCP_PRIVOPC(ctx);
5811
#else
5812
    if (unlikely(!ctx->mem_idx)) {
5813
        GEN_EXCP_PRIVOPC(ctx);
5814
        return;
5815
    }
5816
    /* Restore CPU state */
5817
    gen_helper_rfdi();
5818
    GEN_SYNC(ctx);
5819
#endif
5820
}
5821

    
5822
/* XXX: not implemented on 440 ? */
5823
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5824
{
5825
#if defined(CONFIG_USER_ONLY)
5826
    GEN_EXCP_PRIVOPC(ctx);
5827
#else
5828
    if (unlikely(!ctx->mem_idx)) {
5829
        GEN_EXCP_PRIVOPC(ctx);
5830
        return;
5831
    }
5832
    /* Restore CPU state */
5833
    gen_helper_rfmci();
5834
    GEN_SYNC(ctx);
5835
#endif
5836
}
5837

    
5838
/* TLB management - PowerPC 405 implementation */
5839
/* tlbre */
5840
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5841
{
5842
#if defined(CONFIG_USER_ONLY)
5843
    GEN_EXCP_PRIVOPC(ctx);
5844
#else
5845
    if (unlikely(!ctx->mem_idx)) {
5846
        GEN_EXCP_PRIVOPC(ctx);
5847
        return;
5848
    }
5849
    switch (rB(ctx->opcode)) {
5850
    case 0:
5851
        gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5852
        break;
5853
    case 1:
5854
        gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5855
        break;
5856
    default:
5857
        GEN_EXCP_INVAL(ctx);
5858
        break;
5859
    }
5860
#endif
5861
}
5862

    
5863
/* tlbsx - tlbsx. */
5864
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5865
{
5866
#if defined(CONFIG_USER_ONLY)
5867
    GEN_EXCP_PRIVOPC(ctx);
5868
#else
5869
    TCGv t0;
5870
    if (unlikely(!ctx->mem_idx)) {
5871
        GEN_EXCP_PRIVOPC(ctx);
5872
        return;
5873
    }
5874
    t0 = tcg_temp_new();
5875
    gen_addr_reg_index(ctx, t0);
5876
    gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5877
    tcg_temp_free(t0);
5878
    if (Rc(ctx->opcode)) {
5879
        int l1 = gen_new_label();
5880
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5881
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5882
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5883
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5884
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5885
        gen_set_label(l1);
5886
    }
5887
#endif
5888
}
5889

    
5890
/* tlbwe */
5891
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5892
{
5893
#if defined(CONFIG_USER_ONLY)
5894
    GEN_EXCP_PRIVOPC(ctx);
5895
#else
5896
    if (unlikely(!ctx->mem_idx)) {
5897
        GEN_EXCP_PRIVOPC(ctx);
5898
        return;
5899
    }
5900
    switch (rB(ctx->opcode)) {
5901
    case 0:
5902
        gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5903
        break;
5904
    case 1:
5905
        gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5906
        break;
5907
    default:
5908
        GEN_EXCP_INVAL(ctx);
5909
        break;
5910
    }
5911
#endif
5912
}
5913

    
5914
/* TLB management - PowerPC 440 implementation */
5915
/* tlbre */
5916
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5917
{
5918
#if defined(CONFIG_USER_ONLY)
5919
    GEN_EXCP_PRIVOPC(ctx);
5920
#else
5921
    if (unlikely(!ctx->mem_idx)) {
5922
        GEN_EXCP_PRIVOPC(ctx);
5923
        return;
5924
    }
5925
    switch (rB(ctx->opcode)) {
5926
    case 0:
5927
    case 1:
5928
    case 2:
5929
        {
5930
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5931
            gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5932
            tcg_temp_free_i32(t0);
5933
        }
5934
        break;
5935
    default:
5936
        GEN_EXCP_INVAL(ctx);
5937
        break;
5938
    }
5939
#endif
5940
}
5941

    
5942
/* tlbsx - tlbsx. */
5943
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5944
{
5945
#if defined(CONFIG_USER_ONLY)
5946
    GEN_EXCP_PRIVOPC(ctx);
5947
#else
5948
    TCGv t0;
5949
    if (unlikely(!ctx->mem_idx)) {
5950
        GEN_EXCP_PRIVOPC(ctx);
5951
        return;
5952
    }
5953
    t0 = tcg_temp_new();
5954
    gen_addr_reg_index(ctx, t0);
5955
    gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5956
    tcg_temp_free(t0);
5957
    if (Rc(ctx->opcode)) {
5958
        int l1 = gen_new_label();
5959
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5960
        tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5961
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5962
        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5963
        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5964
        gen_set_label(l1);
5965
    }
5966
#endif
5967
}
5968

    
5969
/* tlbwe */
5970
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5971
{
5972
#if defined(CONFIG_USER_ONLY)
5973
    GEN_EXCP_PRIVOPC(ctx);
5974
#else
5975
    if (unlikely(!ctx->mem_idx)) {
5976
        GEN_EXCP_PRIVOPC(ctx);
5977
        return;
5978
    }
5979
    switch (rB(ctx->opcode)) {
5980
    case 0:
5981
    case 1:
5982
    case 2:
5983
        {
5984
            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5985
            gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5986
            tcg_temp_free_i32(t0);
5987
        }
5988
        break;
5989
    default:
5990
        GEN_EXCP_INVAL(ctx);
5991
        break;
5992
    }
5993
#endif
5994
}
5995

    
5996
/* wrtee */
5997
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5998
{
5999
#if defined(CONFIG_USER_ONLY)
6000
    GEN_EXCP_PRIVOPC(ctx);
6001
#else
6002
    TCGv t0;
6003
    if (unlikely(!ctx->mem_idx)) {
6004
        GEN_EXCP_PRIVOPC(ctx);
6005
        return;
6006
    }
6007
    t0 = tcg_temp_new();
6008
    tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6009
    tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6010
    tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6011
    tcg_temp_free(t0);
6012
    /* Stop translation to have a chance to raise an exception
6013
     * if we just set msr_ee to 1
6014
     */
6015
    GEN_STOP(ctx);
6016
#endif
6017
}
6018

    
6019
/* wrteei */
6020
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6021
{
6022
#if defined(CONFIG_USER_ONLY)
6023
    GEN_EXCP_PRIVOPC(ctx);
6024
#else
6025
    if (unlikely(!ctx->mem_idx)) {
6026
        GEN_EXCP_PRIVOPC(ctx);
6027
        return;
6028
    }
6029
    if (ctx->opcode & 0x00010000) {
6030
        tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6031
        /* Stop translation to have a chance to raise an exception */
6032
        GEN_STOP(ctx);
6033
    } else {
6034
        tcg_gen_andi_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6035
    }
6036
#endif
6037
}
6038

    
6039
/* PowerPC 440 specific instructions */
6040
/* dlmzb */
6041
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
6042
{
6043
    TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6044
    gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
6045
                     cpu_gpr[rB(ctx->opcode)], t0);
6046
    tcg_temp_free_i32(t0);
6047
}
6048

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

    
6055
/* msync replaces sync on 440 */
6056
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6057
{
6058
    /* interpreted as no-op */
6059
}
6060

    
6061
/* icbt */
6062
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6063
{
6064
    /* interpreted as no-op */
6065
    /* XXX: specification say this is treated as a load by the MMU
6066
     *      but does not generate any exception
6067
     */
6068
}
6069

    
6070
/***                      Altivec vector extension                         ***/
6071
/* Altivec registers moves */
6072

    
6073
#define GEN_VR_LDX(name, opc2, opc3)                                          \
6074
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
6075
{                                                                             \
6076
    TCGv EA;                                                                  \
6077
    if (unlikely(!ctx->altivec_enabled)) {                                    \
6078
        GEN_EXCP_NO_VR(ctx);                                                  \
6079
        return;                                                               \
6080
    }                                                                         \
6081
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6082
    EA = tcg_temp_new();                                                      \
6083
    gen_addr_reg_index(ctx, EA);                                              \
6084
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6085
    if (ctx->le_mode) {                                                       \
6086
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6087
        tcg_gen_addi_tl(EA, EA, 8);                                           \
6088
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6089
    } else {                                                                  \
6090
        gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6091
        tcg_gen_addi_tl(EA, EA, 8);                                           \
6092
        gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6093
    }                                                                         \
6094
    tcg_temp_free(EA);                                                        \
6095
}
6096

    
6097
#define GEN_VR_STX(name, opc2, opc3)                                          \
6098
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
6099
{                                                                             \
6100
    TCGv EA;                                                                  \
6101
    if (unlikely(!ctx->altivec_enabled)) {                                    \
6102
        GEN_EXCP_NO_VR(ctx);                                                  \
6103
        return;                                                               \
6104
    }                                                                         \
6105
    gen_set_access_type(ctx, ACCESS_INT);                                     \
6106
    EA = tcg_temp_new();                                                      \
6107
    gen_addr_reg_index(ctx, EA);                                              \
6108
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6109
    if (ctx->le_mode) {                                                       \
6110
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6111
        tcg_gen_addi_tl(EA, EA, 8);                                           \
6112
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6113
    } else {                                                                  \
6114
        gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6115
        tcg_gen_addi_tl(EA, EA, 8);                                           \
6116
        gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6117
    }                                                                         \
6118
    tcg_temp_free(EA);                                                        \
6119
}
6120

    
6121
GEN_VR_LDX(lvx, 0x07, 0x03);
6122
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6123
GEN_VR_LDX(lvxl, 0x07, 0x0B);
6124

    
6125
GEN_VR_STX(svx, 0x07, 0x07);
6126
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6127
GEN_VR_STX(svxl, 0x07, 0x0F);
6128

    
6129
/***                           SPE extension                               ***/
6130
/* Register moves */
6131

    
6132
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
6133
#if defined(TARGET_PPC64)
6134
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
6135
#else
6136
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6137
#endif
6138
}
6139

    
6140
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
6141
#if defined(TARGET_PPC64)
6142
    tcg_gen_mov_i64(cpu_gpr[reg], t);
6143
#else
6144
    TCGv_i64 tmp = tcg_temp_new_i64();
6145
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6146
    tcg_gen_shri_i64(tmp, t, 32);
6147
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6148
    tcg_temp_free_i64(tmp);
6149
#endif
6150
}
6151

    
6152
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
6153
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
6154
{                                                                             \
6155
    if (Rc(ctx->opcode))                                                      \
6156
        gen_##name1(ctx);                                                     \
6157
    else                                                                      \
6158
        gen_##name0(ctx);                                                     \
6159
}
6160

    
6161
/* Handler for undefined SPE opcodes */
6162
static always_inline void gen_speundef (DisasContext *ctx)
6163
{
6164
    GEN_EXCP_INVAL(ctx);
6165
}
6166

    
6167
/* SPE logic */
6168
#if defined(TARGET_PPC64)
6169
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6170
static always_inline void gen_##name (DisasContext *ctx)                      \
6171
{                                                                             \
6172
    if (unlikely(!ctx->spe_enabled)) {                                        \
6173
        GEN_EXCP_NO_AP(ctx);                                                  \
6174
        return;                                                               \
6175
    }                                                                         \
6176
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6177
           cpu_gpr[rB(ctx->opcode)]);                                         \
6178
}
6179
#else
6180
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6181
static always_inline void gen_##name (DisasContext *ctx)                      \
6182
{                                                                             \
6183
    if (unlikely(!ctx->spe_enabled)) {                                        \
6184
        GEN_EXCP_NO_AP(ctx);                                                  \
6185
        return;                                                               \
6186
    }                                                                         \
6187
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6188
           cpu_gpr[rB(ctx->opcode)]);                                         \
6189
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6190
           cpu_gprh[rB(ctx->opcode)]);                                        \
6191
}
6192
#endif
6193

    
6194
GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6195
GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6196
GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6197
GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6198
GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6199
GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6200
GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6201
GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6202

    
6203
/* SPE logic immediate */
6204
#if defined(TARGET_PPC64)
6205
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6206
static always_inline void gen_##name (DisasContext *ctx)                      \
6207
{                                                                             \
6208
    if (unlikely(!ctx->spe_enabled)) {                                        \
6209
        GEN_EXCP_NO_AP(ctx);                                                  \
6210
        return;                                                               \
6211
    }                                                                         \
6212
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6213
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6214
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6215
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6216
    tcg_opi(t0, t0, rB(ctx->opcode));                                         \
6217
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6218
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6219
    tcg_temp_free_i64(t2);                                                    \
6220
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
6221
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6222
    tcg_temp_free_i32(t0);                                                    \
6223
    tcg_temp_free_i32(t1);                                                    \
6224
}
6225
#else
6226
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6227
static always_inline void gen_##name (DisasContext *ctx)                      \
6228
{                                                                             \
6229
    if (unlikely(!ctx->spe_enabled)) {                                        \
6230
        GEN_EXCP_NO_AP(ctx);                                                  \
6231
        return;                                                               \
6232
    }                                                                         \
6233
    tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
6234
            rB(ctx->opcode));                                                 \
6235
    tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
6236
            rB(ctx->opcode));                                                 \
6237
}
6238
#endif
6239
GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6240
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6241
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6242
GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6243

    
6244
/* SPE arithmetic */
6245
#if defined(TARGET_PPC64)
6246
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6247
static always_inline void gen_##name (DisasContext *ctx)                      \
6248
{                                                                             \
6249
    if (unlikely(!ctx->spe_enabled)) {                                        \
6250
        GEN_EXCP_NO_AP(ctx);                                                  \
6251
        return;                                                               \
6252
    }                                                                         \
6253
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6254
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6255
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6256
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6257
    tcg_op(t0, t0);                                                           \
6258
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6259
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6260
    tcg_temp_free_i64(t2);                                                    \
6261
    tcg_op(t1, t1);                                                           \
6262
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6263
    tcg_temp_free_i32(t0);                                                    \
6264
    tcg_temp_free_i32(t1);                                                    \
6265
}
6266
#else
6267
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6268
static always_inline void gen_##name (DisasContext *ctx)                      \
6269
{                                                                             \
6270
    if (unlikely(!ctx->spe_enabled)) {                                        \
6271
        GEN_EXCP_NO_AP(ctx);                                                  \
6272
        return;                                                               \
6273
    }                                                                         \
6274
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
6275
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
6276
}
6277
#endif
6278

    
6279
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6280
{
6281
    int l1 = gen_new_label();
6282
    int l2 = gen_new_label();
6283

    
6284
    tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6285
    tcg_gen_neg_i32(ret, arg1);
6286
    tcg_gen_br(l2);
6287
    gen_set_label(l1);
6288
    tcg_gen_mov_i32(ret, arg1);
6289
    gen_set_label(l2);
6290
}
6291
GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6292
GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6293
GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6294
GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6295
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6296
{
6297
    tcg_gen_addi_i32(ret, arg1, 0x8000);
6298
    tcg_gen_ext16u_i32(ret, ret);
6299
}
6300
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6301
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6302
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6303

    
6304
#if defined(TARGET_PPC64)
6305
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6306
static always_inline void gen_##name (DisasContext *ctx)                      \
6307
{                                                                             \
6308
    if (unlikely(!ctx->spe_enabled)) {                                        \
6309
        GEN_EXCP_NO_AP(ctx);                                                  \
6310
        return;                                                               \
6311
    }                                                                         \
6312
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6313
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6314
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6315
    TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64);                           \
6316
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6317
    tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
6318
    tcg_op(t0, t0, t2);                                                       \
6319
    tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
6320
    tcg_gen_trunc_i64_i32(t1, t3);                                            \
6321
    tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
6322
    tcg_gen_trunc_i64_i32(t2, t3);                                            \
6323
    tcg_temp_free_i64(t3);                                                    \
6324
    tcg_op(t1, t1, t2);                                                       \
6325
    tcg_temp_free_i32(t2);                                                    \
6326
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6327
    tcg_temp_free_i32(t0);                                                    \
6328
    tcg_temp_free_i32(t1);                                                    \
6329
}
6330
#else
6331
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6332
static always_inline void gen_##name (DisasContext *ctx)                      \
6333
{                                                                             \
6334
    if (unlikely(!ctx->spe_enabled)) {                                        \
6335
        GEN_EXCP_NO_AP(ctx);                                                  \
6336
        return;                                                               \
6337
    }                                                                         \
6338
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6339
           cpu_gpr[rB(ctx->opcode)]);                                         \
6340
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6341
           cpu_gprh[rB(ctx->opcode)]);                                        \
6342
}
6343
#endif
6344

    
6345
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6346
{
6347
    TCGv_i32 t0;
6348
    int l1, l2;
6349

    
6350
    l1 = gen_new_label();
6351
    l2 = gen_new_label();
6352
    t0 = tcg_temp_local_new_i32();
6353
    /* No error here: 6 bits are used */
6354
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6355
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6356
    tcg_gen_shr_i32(ret, arg1, t0);
6357
    tcg_gen_br(l2);
6358
    gen_set_label(l1);
6359
    tcg_gen_movi_i32(ret, 0);
6360
    tcg_gen_br(l2);
6361
    tcg_temp_free_i32(t0);
6362
}
6363
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6364
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6365
{
6366
    TCGv_i32 t0;
6367
    int l1, l2;
6368

    
6369
    l1 = gen_new_label();
6370
    l2 = gen_new_label();
6371
    t0 = tcg_temp_local_new_i32();
6372
    /* No error here: 6 bits are used */
6373
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6374
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6375
    tcg_gen_sar_i32(ret, arg1, t0);
6376
    tcg_gen_br(l2);
6377
    gen_set_label(l1);
6378
    tcg_gen_movi_i32(ret, 0);
6379
    tcg_gen_br(l2);
6380
    tcg_temp_free_i32(t0);
6381
}
6382
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6383
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6384
{
6385
    TCGv_i32 t0;
6386
    int l1, l2;
6387

    
6388
    l1 = gen_new_label();
6389
    l2 = gen_new_label();
6390
    t0 = tcg_temp_local_new_i32();
6391
    /* No error here: 6 bits are used */
6392
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6393
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6394
    tcg_gen_shl_i32(ret, arg1, t0);
6395
    tcg_gen_br(l2);
6396
    gen_set_label(l1);
6397
    tcg_gen_movi_i32(ret, 0);
6398
    tcg_gen_br(l2);
6399
    tcg_temp_free_i32(t0);
6400
}
6401
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6402
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6403
{
6404
    TCGv_i32 t0 = tcg_temp_new_i32();
6405
    tcg_gen_andi_i32(t0, arg2, 0x1F);
6406
    tcg_gen_rotl_i32(ret, arg1, t0);
6407
    tcg_temp_free_i32(t0);
6408
}
6409
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6410
static always_inline void gen_evmergehi (DisasContext *ctx)
6411
{
6412
    if (unlikely(!ctx->spe_enabled)) {
6413
        GEN_EXCP_NO_AP(ctx);
6414
        return;
6415
    }
6416
#if defined(TARGET_PPC64)
6417
    TCGv t0 = tcg_temp_new();
6418
    TCGv t1 = tcg_temp_new();
6419
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6420
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6421
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6422
    tcg_temp_free(t0);
6423
    tcg_temp_free(t1);
6424
#else
6425
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6426
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6427
#endif
6428
}
6429
GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6430
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6431
{
6432
    tcg_gen_sub_i32(ret, arg2, arg1);
6433
}
6434
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6435

    
6436
/* SPE arithmetic immediate */
6437
#if defined(TARGET_PPC64)
6438
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6439
static always_inline void gen_##name (DisasContext *ctx)                      \
6440
{                                                                             \
6441
    if (unlikely(!ctx->spe_enabled)) {                                        \
6442
        GEN_EXCP_NO_AP(ctx);                                                  \
6443
        return;                                                               \
6444
    }                                                                         \
6445
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6446
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6447
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6448
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
6449
    tcg_op(t0, t0, rA(ctx->opcode));                                          \
6450
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6451
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6452
    tcg_temp_free_i64(t2);                                                        \
6453
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
6454
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6455
    tcg_temp_free_i32(t0);                                                    \
6456
    tcg_temp_free_i32(t1);                                                    \
6457
}
6458
#else
6459
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6460
static always_inline void gen_##name (DisasContext *ctx)                      \
6461
{                                                                             \
6462
    if (unlikely(!ctx->spe_enabled)) {                                        \
6463
        GEN_EXCP_NO_AP(ctx);                                                  \
6464
        return;                                                               \
6465
    }                                                                         \
6466
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
6467
           rA(ctx->opcode));                                                  \
6468
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
6469
           rA(ctx->opcode));                                                  \
6470
}
6471
#endif
6472
GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6473
GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6474

    
6475
/* SPE comparison */
6476
#if defined(TARGET_PPC64)
6477
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6478
static always_inline void gen_##name (DisasContext *ctx)                      \
6479
{                                                                             \
6480
    if (unlikely(!ctx->spe_enabled)) {                                        \
6481
        GEN_EXCP_NO_AP(ctx);                                                  \
6482
        return;                                                               \
6483
    }                                                                         \
6484
    int l1 = gen_new_label();                                                 \
6485
    int l2 = gen_new_label();                                                 \
6486
    int l3 = gen_new_label();                                                 \
6487
    int l4 = gen_new_label();                                                 \
6488
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6489
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6490
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6491
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6492
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
6493
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
6494
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6495
    tcg_gen_br(l2);                                                           \
6496
    gen_set_label(l1);                                                        \
6497
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6498
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6499
    gen_set_label(l2);                                                        \
6500
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6501
    tcg_gen_trunc_i64_i32(t0, t2);                                            \
6502
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6503
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6504
    tcg_temp_free_i64(t2);                                                    \
6505
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
6506
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6507
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6508
    tcg_gen_br(l4);                                                           \
6509
    gen_set_label(l3);                                                        \
6510
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6511
                    CRF_CH | CRF_CH_OR_CL);                                   \
6512
    gen_set_label(l4);                                                        \
6513
    tcg_temp_free_i32(t0);                                                    \
6514
    tcg_temp_free_i32(t1);                                                    \
6515
}
6516
#else
6517
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6518
static always_inline void gen_##name (DisasContext *ctx)                      \
6519
{                                                                             \
6520
    if (unlikely(!ctx->spe_enabled)) {                                        \
6521
        GEN_EXCP_NO_AP(ctx);                                                  \
6522
        return;                                                               \
6523
    }                                                                         \
6524
    int l1 = gen_new_label();                                                 \
6525
    int l2 = gen_new_label();                                                 \
6526
    int l3 = gen_new_label();                                                 \
6527
    int l4 = gen_new_label();                                                 \
6528
                                                                              \
6529
    tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
6530
                       cpu_gpr[rB(ctx->opcode)], l1);                         \
6531
    tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
6532
    tcg_gen_br(l2);                                                           \
6533
    gen_set_label(l1);                                                        \
6534
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6535
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6536
    gen_set_label(l2);                                                        \
6537
    tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
6538
                       cpu_gprh[rB(ctx->opcode)], l3);                        \
6539
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6540
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6541
    tcg_gen_br(l4);                                                           \
6542
    gen_set_label(l3);                                                        \
6543
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6544
                    CRF_CH | CRF_CH_OR_CL);                                   \
6545
    gen_set_label(l4);                                                        \
6546
}
6547
#endif
6548
GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6549
GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6550
GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6551
GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6552
GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6553

    
6554
/* SPE misc */
6555
static always_inline void gen_brinc (DisasContext *ctx)
6556
{
6557
    /* Note: brinc is usable even if SPE is disabled */
6558
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6559
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6560
}
6561
static always_inline void gen_evmergelo (DisasContext *ctx)
6562
{
6563
    if (unlikely(!ctx->spe_enabled)) {
6564
        GEN_EXCP_NO_AP(ctx);
6565
        return;
6566
    }
6567
#if defined(TARGET_PPC64)
6568
    TCGv t0 = tcg_temp_new();
6569
    TCGv t1 = tcg_temp_new();
6570
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6571
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6572
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6573
    tcg_temp_free(t0);
6574
    tcg_temp_free(t1);
6575
#else
6576
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6577
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6578
#endif
6579
}
6580
static always_inline void gen_evmergehilo (DisasContext *ctx)
6581
{
6582
    if (unlikely(!ctx->spe_enabled)) {
6583
        GEN_EXCP_NO_AP(ctx);
6584
        return;
6585
    }
6586
#if defined(TARGET_PPC64)
6587
    TCGv t0 = tcg_temp_new();
6588
    TCGv t1 = tcg_temp_new();
6589
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6590
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6591
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6592
    tcg_temp_free(t0);
6593
    tcg_temp_free(t1);
6594
#else
6595
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6596
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6597
#endif
6598
}
6599
static always_inline void gen_evmergelohi (DisasContext *ctx)
6600
{
6601
    if (unlikely(!ctx->spe_enabled)) {
6602
        GEN_EXCP_NO_AP(ctx);
6603
        return;
6604
    }
6605
#if defined(TARGET_PPC64)
6606
    TCGv t0 = tcg_temp_new();
6607
    TCGv t1 = tcg_temp_new();
6608
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6609
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6610
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6611
    tcg_temp_free(t0);
6612
    tcg_temp_free(t1);
6613
#else
6614
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6615
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6616
#endif
6617
}
6618
static always_inline void gen_evsplati (DisasContext *ctx)
6619
{
6620
    uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6621

    
6622
#if defined(TARGET_PPC64)
6623
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6624
#else
6625
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6626
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6627
#endif
6628
}
6629
static always_inline void gen_evsplatfi (DisasContext *ctx)
6630
{
6631
    uint64_t imm = rA(ctx->opcode) << 11;
6632

    
6633
#if defined(TARGET_PPC64)
6634
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6635
#else
6636
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6637
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6638
#endif
6639
}
6640

    
6641
static always_inline void gen_evsel (DisasContext *ctx)
6642
{
6643
    int l1 = gen_new_label();
6644
    int l2 = gen_new_label();
6645
    int l3 = gen_new_label();
6646
    int l4 = gen_new_label();
6647
    TCGv_i32 t0 = tcg_temp_local_new_i32();
6648
#if defined(TARGET_PPC64)
6649
    TCGv t1 = tcg_temp_local_new();
6650
    TCGv t2 = tcg_temp_local_new();
6651
#endif
6652
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6653
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6654
#if defined(TARGET_PPC64)
6655
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6656
#else
6657
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6658
#endif
6659
    tcg_gen_br(l2);
6660
    gen_set_label(l1);
6661
#if defined(TARGET_PPC64)
6662
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6663
#else
6664
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6665
#endif
6666
    gen_set_label(l2);
6667
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6668
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6669
#if defined(TARGET_PPC64)
6670
    tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6671
#else
6672
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6673
#endif
6674
    tcg_gen_br(l4);
6675
    gen_set_label(l3);
6676
#if defined(TARGET_PPC64)
6677
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6678
#else
6679
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6680
#endif
6681
    gen_set_label(l4);
6682
    tcg_temp_free_i32(t0);
6683
#if defined(TARGET_PPC64)
6684
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6685
    tcg_temp_free(t1);
6686
    tcg_temp_free(t2);
6687
#endif
6688
}
6689
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6690
{
6691
    gen_evsel(ctx);
6692
}
6693
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6694
{
6695
    gen_evsel(ctx);
6696
}
6697
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6698
{
6699
    gen_evsel(ctx);
6700
}
6701
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6702
{
6703
    gen_evsel(ctx);
6704
}
6705

    
6706
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
6707
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
6708
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
6709
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
6710
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
6711
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
6712
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
6713
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
6714
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
6715
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
6716
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
6717
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
6718
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
6719
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
6720
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
6721
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
6722
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
6723
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
6724
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
6725
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
6726
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
6727
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
6728
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
6729
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
6730
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
6731

    
6732
/* SPE load and stores */
6733
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6734
{
6735
    target_ulong uimm = rB(ctx->opcode);
6736

    
6737
    if (rA(ctx->opcode) == 0) {
6738
        tcg_gen_movi_tl(EA, uimm << sh);
6739
    } else {
6740
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
6741
#if defined(TARGET_PPC64)
6742
        if (!ctx->sf_mode) {
6743
            tcg_gen_ext32u_tl(EA, EA);
6744
        }
6745
#endif
6746
    }
6747
}
6748

    
6749
static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6750
{
6751
#if defined(TARGET_PPC64)
6752
    gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6753
#else
6754
    TCGv_i64 t0 = tcg_temp_new_i64();
6755
    gen_qemu_ld64(ctx, t0, addr);
6756
    tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
6757
    tcg_gen_shri_i64(t0, t0, 32);
6758
    tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
6759
    tcg_temp_free_i64(t0);
6760
#endif
6761
}
6762

    
6763
static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6764
{
6765
#if defined(TARGET_PPC64)
6766
    TCGv t0 = tcg_temp_new();
6767
    gen_qemu_ld32u(ctx, t0, addr);
6768
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6769
    gen_addr_add(ctx, addr, addr, 4);
6770
    gen_qemu_ld32u(ctx, t0, addr);
6771
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6772
    tcg_temp_free(t0);
6773
#else
6774
    gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6775
    gen_addr_add(ctx, addr, addr, 4);
6776
    gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6777
#endif
6778
}
6779

    
6780
static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6781
{
6782
    TCGv t0 = tcg_temp_new();
6783
#if defined(TARGET_PPC64)
6784
    gen_qemu_ld16u(ctx, t0, addr);
6785
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6786
    gen_addr_add(ctx, addr, addr, 2);
6787
    gen_qemu_ld16u(ctx, t0, addr);
6788
    tcg_gen_shli_tl(t0, t0, 32);
6789
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6790
    gen_addr_add(ctx, addr, addr, 2);
6791
    gen_qemu_ld16u(ctx, t0, addr);
6792
    tcg_gen_shli_tl(t0, t0, 16);
6793
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6794
    gen_addr_add(ctx, addr, addr, 2);
6795
    gen_qemu_ld16u(ctx, t0, addr);
6796
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6797
#else
6798
    gen_qemu_ld16u(ctx, t0, addr);
6799
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6800
    gen_addr_add(ctx, addr, addr, 2);
6801
    gen_qemu_ld16u(ctx, t0, addr);
6802
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6803
    gen_addr_add(ctx, addr, addr, 2);
6804
    gen_qemu_ld16u(ctx, t0, addr);
6805
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6806
    gen_addr_add(ctx, addr, addr, 2);
6807
    gen_qemu_ld16u(ctx, t0, addr);
6808
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6809
#endif
6810
    tcg_temp_free(t0);
6811
}
6812

    
6813
static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6814
{
6815
    TCGv t0 = tcg_temp_new();
6816
    gen_qemu_ld16u(ctx, t0, addr);
6817
#if defined(TARGET_PPC64)
6818
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6819
    tcg_gen_shli_tl(t0, t0, 16);
6820
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6821
#else
6822
    tcg_gen_shli_tl(t0, t0, 16);
6823
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6824
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6825
#endif
6826
    tcg_temp_free(t0);
6827
}
6828

    
6829
static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6830
{
6831
    TCGv t0 = tcg_temp_new();
6832
    gen_qemu_ld16u(ctx, t0, addr);
6833
#if defined(TARGET_PPC64)
6834
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6835
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6836
#else
6837
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6838
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6839
#endif
6840
    tcg_temp_free(t0);
6841
}
6842

    
6843
static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6844
{
6845
    TCGv t0 = tcg_temp_new();
6846
    gen_qemu_ld16s(ctx, t0, addr);
6847
#if defined(TARGET_PPC64)
6848
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6849
    tcg_gen_ext32u_tl(t0, t0);
6850
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6851
#else
6852
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6853
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6854
#endif
6855
    tcg_temp_free(t0);
6856
}
6857

    
6858
static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6859
{
6860
    TCGv t0 = tcg_temp_new();
6861
#if defined(TARGET_PPC64)
6862
    gen_qemu_ld16u(ctx, t0, addr);
6863
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6864
    gen_addr_add(ctx, addr, addr, 2);
6865
    gen_qemu_ld16u(ctx, t0, addr);
6866
    tcg_gen_shli_tl(t0, t0, 16);
6867
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6868
#else
6869
    gen_qemu_ld16u(ctx, t0, addr);
6870
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6871
    gen_addr_add(ctx, addr, addr, 2);
6872
    gen_qemu_ld16u(ctx, t0, addr);
6873
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6874
#endif
6875
    tcg_temp_free(t0);
6876
}
6877

    
6878
static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6879
{
6880
#if defined(TARGET_PPC64)
6881
    TCGv t0 = tcg_temp_new();
6882
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6883
    gen_addr_add(ctx, addr, addr, 2);
6884
    gen_qemu_ld16u(ctx, t0, addr);
6885
    tcg_gen_shli_tl(t0, t0, 32);
6886
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6887
    tcg_temp_free(t0);
6888
#else
6889
    gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6890
    gen_addr_add(ctx, addr, addr, 2);
6891
    gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6892
#endif
6893
}
6894

    
6895
static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6896
{
6897
#if defined(TARGET_PPC64)
6898
    TCGv t0 = tcg_temp_new();
6899
    gen_qemu_ld16s(ctx, t0, addr);
6900
    tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
6901
    gen_addr_add(ctx, addr, addr, 2);
6902
    gen_qemu_ld16s(ctx, t0, addr);
6903
    tcg_gen_shli_tl(t0, t0, 32);
6904
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6905
    tcg_temp_free(t0);
6906
#else
6907
    gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6908
    gen_addr_add(ctx, addr, addr, 2);
6909
    gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6910
#endif
6911
}
6912

    
6913
static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6914
{
6915
    TCGv t0 = tcg_temp_new();
6916
    gen_qemu_ld32u(ctx, t0, addr);
6917
#if defined(TARGET_PPC64)
6918
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6919
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6920
#else
6921
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6922
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6923
#endif
6924
    tcg_temp_free(t0);
6925
}
6926

    
6927
static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6928
{
6929
    TCGv t0 = tcg_temp_new();
6930
#if defined(TARGET_PPC64)
6931
    gen_qemu_ld16u(ctx, t0, addr);
6932
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6933
    tcg_gen_shli_tl(t0, t0, 32);
6934
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6935
    gen_addr_add(ctx, addr, addr, 2);
6936
    gen_qemu_ld16u(ctx, t0, addr);
6937
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6938
    tcg_gen_shli_tl(t0, t0, 16);
6939
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6940
#else
6941
    gen_qemu_ld16u(ctx, t0, addr);
6942
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6943
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6944
    gen_addr_add(ctx, addr, addr, 2);
6945
    gen_qemu_ld16u(ctx, t0, addr);
6946
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6947
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6948
#endif
6949
    tcg_temp_free(t0);
6950
}
6951

    
6952
static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6953
{
6954
#if defined(TARGET_PPC64)
6955
    gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6956
#else
6957
    TCGv_i64 t0 = tcg_temp_new_i64();
6958
    tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
6959
    gen_qemu_st64(ctx, t0, addr);
6960
    tcg_temp_free_i64(t0);
6961
#endif
6962
}
6963

    
6964
static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6965
{
6966
#if defined(TARGET_PPC64)
6967
    TCGv t0 = tcg_temp_new();
6968
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6969
    gen_qemu_st32(ctx, t0, addr);
6970
    tcg_temp_free(t0);
6971
#else
6972
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6973
#endif
6974
    gen_addr_add(ctx, addr, addr, 4);
6975
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6976
}
6977

    
6978
static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6979
{
6980
    TCGv t0 = tcg_temp_new();
6981
#if defined(TARGET_PPC64)
6982
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6983
#else
6984
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6985
#endif
6986
    gen_qemu_st16(ctx, t0, addr);
6987
    gen_addr_add(ctx, addr, addr, 2);
6988
#if defined(TARGET_PPC64)
6989
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6990
    gen_qemu_st16(ctx, t0, addr);
6991
#else
6992
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6993
#endif
6994
    gen_addr_add(ctx, addr, addr, 2);
6995
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
6996
    gen_qemu_st16(ctx, t0, addr);
6997
    tcg_temp_free(t0);
6998
    gen_addr_add(ctx, addr, addr, 2);
6999
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7000
}
7001

    
7002
static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
7003
{
7004
    TCGv t0 = tcg_temp_new();
7005
#if defined(TARGET_PPC64)
7006
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7007
#else
7008
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7009
#endif
7010
    gen_qemu_st16(ctx, t0, addr);
7011
    gen_addr_add(ctx, addr, addr, 2);
7012
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
7013
    gen_qemu_st16(ctx, t0, addr);
7014
    tcg_temp_free(t0);
7015
}
7016

    
7017
static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
7018
{
7019
#if defined(TARGET_PPC64)
7020
    TCGv t0 = tcg_temp_new();
7021
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7022
    gen_qemu_st16(ctx, t0, addr);
7023
    tcg_temp_free(t0);
7024
#else
7025
    gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7026
#endif
7027
    gen_addr_add(ctx, addr, addr, 2);
7028
    gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7029
}
7030

    
7031
static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
7032
{
7033
#if defined(TARGET_PPC64)
7034
    TCGv t0 = tcg_temp_new();
7035
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7036
    gen_qemu_st32(ctx, t0, addr);
7037
    tcg_temp_free(t0);
7038
#else
7039
    gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7040
#endif
7041
}
7042

    
7043
static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
7044
{
7045
    gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7046
}
7047

    
7048
#define GEN_SPEOP_LDST(name, opc2, sh)                                        \
7049
GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                      \
7050
{                                                                             \
7051
    TCGv t0;                                                                  \
7052
    if (unlikely(!ctx->spe_enabled)) {                                        \
7053
        GEN_EXCP_NO_AP(ctx);                                                  \
7054
        return;                                                               \
7055
    }                                                                         \
7056
    gen_set_access_type(ctx, ACCESS_INT);                                     \
7057
    t0 = tcg_temp_new();                                                      \
7058
    if (Rc(ctx->opcode)) {                                                    \
7059
        gen_addr_spe_imm_index(ctx, t0, sh);                                  \
7060
    } else {                                                                  \
7061
        gen_addr_reg_index(ctx, t0);                                          \
7062
    }                                                                         \
7063
    gen_op_##name(ctx, t0);                                                   \
7064
    tcg_temp_free(t0);                                                        \
7065
}
7066

    
7067
GEN_SPEOP_LDST(evldd, 0x00, 3);
7068
GEN_SPEOP_LDST(evldw, 0x01, 3);
7069
GEN_SPEOP_LDST(evldh, 0x02, 3);
7070
GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
7071
GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
7072
GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
7073
GEN_SPEOP_LDST(evlwhe, 0x08, 2);
7074
GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
7075
GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
7076
GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
7077
GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
7078

    
7079
GEN_SPEOP_LDST(evstdd, 0x10, 3);
7080
GEN_SPEOP_LDST(evstdw, 0x11, 3);
7081
GEN_SPEOP_LDST(evstdh, 0x12, 3);
7082
GEN_SPEOP_LDST(evstwhe, 0x18, 2);
7083
GEN_SPEOP_LDST(evstwho, 0x1A, 2);
7084
GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
7085
GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
7086

    
7087
/* Multiply and add - TODO */
7088
#if 0
7089
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
7090
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
7091
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
7092
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
7093
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
7094
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
7095
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
7096
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
7097
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
7098
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
7099
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
7100
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
7101

7102
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
7103
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
7104
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
7105
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
7106
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
7107
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
7108
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
7109
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
7110
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
7111
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
7112
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
7113
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
7114
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
7115
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
7116

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

7124
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
7125
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
7126
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
7127
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
7128
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
7129
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
7130
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
7131
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
7132
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
7133
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
7134
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
7135
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
7136

7137
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
7138
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
7139
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
7140
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
7141
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
7142

7143
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
7144
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
7145
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
7146
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
7147
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
7148
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
7149
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
7150
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
7151
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
7152
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
7153
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
7154
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
7155

7156
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
7157
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
7158
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
7159
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
7160
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
7161
#endif
7162

    
7163
/***                      SPE floating-point extension                     ***/
7164
#if defined(TARGET_PPC64)
7165
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7166
static always_inline void gen_##name (DisasContext *ctx)                      \
7167
{                                                                             \
7168
    TCGv_i32 t0;                                                              \
7169
    TCGv t1;                                                                  \
7170
    t0 = tcg_temp_new_i32();                                                  \
7171
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7172
    gen_helper_##name(t0, t0);                                                \
7173
    t1 = tcg_temp_new();                                                      \
7174
    tcg_gen_extu_i32_tl(t1, t0);                                              \
7175
    tcg_temp_free_i32(t0);                                                    \
7176
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7177
                    0xFFFFFFFF00000000ULL);                                   \
7178
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7179
    tcg_temp_free(t1);                                                        \
7180
}
7181
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7182
static always_inline void gen_##name (DisasContext *ctx)                      \
7183
{                                                                             \
7184
    TCGv_i32 t0;                                                              \
7185
    TCGv t1;                                                                  \
7186
    t0 = tcg_temp_new_i32();                                                  \
7187
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7188
    t1 = tcg_temp_new();                                                      \
7189
    tcg_gen_extu_i32_tl(t1, t0);                                              \
7190
    tcg_temp_free_i32(t0);                                                    \
7191
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7192
                    0xFFFFFFFF00000000ULL);                                   \
7193
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7194
    tcg_temp_free(t1);                                                        \
7195
}
7196
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7197
static always_inline void gen_##name (DisasContext *ctx)                      \
7198
{                                                                             \
7199
    TCGv_i32 t0 = tcg_temp_new_i32();                                         \
7200
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7201
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7202
    tcg_temp_free_i32(t0);                                                    \
7203
}
7204
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7205
static always_inline void gen_##name (DisasContext *ctx)                      \
7206
{                                                                             \
7207
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7208
}
7209
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7210
static always_inline void gen_##name (DisasContext *ctx)                      \
7211
{                                                                             \
7212
    TCGv_i32 t0, t1;                                                          \
7213
    TCGv_i64 t2;                                                              \
7214
    if (unlikely(!ctx->spe_enabled)) {                                        \
7215
        GEN_EXCP_NO_AP(ctx);                                                  \
7216
        return;                                                               \
7217
    }                                                                         \
7218
    t0 = tcg_temp_new_i32();                                                  \
7219
    t1 = tcg_temp_new_i32();                                                  \
7220
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7221
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7222
    gen_helper_##name(t0, t0, t1);                                            \
7223
    tcg_temp_free_i32(t1);                                                    \
7224
    t2 = tcg_temp_new();                                                      \
7225
    tcg_gen_extu_i32_tl(t2, t0);                                              \
7226
    tcg_temp_free_i32(t0);                                                    \
7227
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7228
                    0xFFFFFFFF00000000ULL);                                   \
7229
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
7230
    tcg_temp_free(t2);                                                        \
7231
}
7232
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7233
static always_inline void gen_##name (DisasContext *ctx)                      \
7234
{                                                                             \
7235
    if (unlikely(!ctx->spe_enabled)) {                                        \
7236
        GEN_EXCP_NO_AP(ctx);                                                  \
7237
        return;                                                               \
7238
    }                                                                         \
7239
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
7240
                      cpu_gpr[rB(ctx->opcode)]);                              \
7241
}
7242
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7243
static always_inline void gen_##name (DisasContext *ctx)                      \
7244
{                                                                             \
7245
    TCGv_i32 t0, t1;                                                          \
7246
    if (unlikely(!ctx->spe_enabled)) {                                        \
7247
        GEN_EXCP_NO_AP(ctx);                                                  \
7248
        return;                                                               \
7249
    }                                                                         \
7250
    t0 = tcg_temp_new_i32();                                                  \
7251
    t1 = tcg_temp_new_i32();                                                  \
7252
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7253
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7254
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7255
    tcg_temp_free_i32(t0);                                                    \
7256
    tcg_temp_free_i32(t1);                                                    \
7257
}
7258
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7259
static always_inline void gen_##name (DisasContext *ctx)                      \
7260
{                                                                             \
7261
    if (unlikely(!ctx->spe_enabled)) {                                        \
7262
        GEN_EXCP_NO_AP(ctx);                                                  \
7263
        return;                                                               \
7264
    }                                                                         \
7265
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7266
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7267
}
7268
#else
7269
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7270
static always_inline void gen_##name (DisasContext *ctx)                      \
7271
{                                                                             \
7272
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7273
}
7274
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7275
static always_inline void gen_##name (DisasContext *ctx)                      \
7276
{                                                                             \
7277
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7278
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7279
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7280
    tcg_temp_free_i64(t0);                                                    \
7281
}
7282
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7283
static always_inline void gen_##name (DisasContext *ctx)                      \
7284
{                                                                             \
7285
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7286
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7287
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7288
    tcg_temp_free_i64(t0);                                                    \
7289
}
7290
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7291
static always_inline void gen_##name (DisasContext *ctx)                      \
7292
{                                                                             \
7293
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7294
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7295
    gen_helper_##name(t0, t0);                                                \
7296
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7297
    tcg_temp_free_i64(t0);                                                    \
7298
}
7299
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7300
static always_inline void gen_##name (DisasContext *ctx)                      \
7301
{                                                                             \
7302
    if (unlikely(!ctx->spe_enabled)) {                                        \
7303
        GEN_EXCP_NO_AP(ctx);                                                  \
7304
        return;                                                               \
7305
    }                                                                         \
7306
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
7307
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7308
}
7309
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7310
static always_inline void gen_##name (DisasContext *ctx)                      \
7311
{                                                                             \
7312
    TCGv_i64 t0, t1;                                                          \
7313
    if (unlikely(!ctx->spe_enabled)) {                                        \
7314
        GEN_EXCP_NO_AP(ctx);                                                  \
7315
        return;                                                               \
7316
    }                                                                         \
7317
    t0 = tcg_temp_new_i64();                                                  \
7318
    t1 = tcg_temp_new_i64();                                                  \
7319
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7320
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7321
    gen_helper_##name(t0, t0, t1);                                            \
7322
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7323
    tcg_temp_free_i64(t0);                                                    \
7324
    tcg_temp_free_i64(t1);                                                    \
7325
}
7326
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7327
static always_inline void gen_##name (DisasContext *ctx)                      \
7328
{                                                                             \
7329
    if (unlikely(!ctx->spe_enabled)) {                                        \
7330
        GEN_EXCP_NO_AP(ctx);                                                  \
7331
        return;                                                               \
7332
    }                                                                         \
7333
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7334
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7335
}
7336
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7337
static always_inline void gen_##name (DisasContext *ctx)                      \
7338
{                                                                             \
7339
    TCGv_i64 t0, t1;                                                          \
7340
    if (unlikely(!ctx->spe_enabled)) {                                        \
7341
        GEN_EXCP_NO_AP(ctx);                                                  \
7342
        return;                                                               \
7343
    }                                                                         \
7344
    t0 = tcg_temp_new_i64();                                                  \
7345
    t1 = tcg_temp_new_i64();                                                  \
7346
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7347
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7348
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7349
    tcg_temp_free_i64(t0);                                                    \
7350
    tcg_temp_free_i64(t1);                                                    \
7351
}
7352
#endif
7353

    
7354
/* Single precision floating-point vectors operations */
7355
/* Arithmetic */
7356
GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7357
GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7358
GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7359
GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7360
static always_inline void gen_evfsabs (DisasContext *ctx)
7361
{
7362
    if (unlikely(!ctx->spe_enabled)) {
7363
        GEN_EXCP_NO_AP(ctx);
7364
        return;
7365
    }
7366
#if defined(TARGET_PPC64)
7367
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7368
#else
7369
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7370
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7371
#endif
7372
}
7373
static always_inline void gen_evfsnabs (DisasContext *ctx)
7374
{
7375
    if (unlikely(!ctx->spe_enabled)) {
7376
        GEN_EXCP_NO_AP(ctx);
7377
        return;
7378
    }
7379
#if defined(TARGET_PPC64)
7380
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7381
#else
7382
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7383
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7384
#endif
7385
}
7386
static always_inline void gen_evfsneg (DisasContext *ctx)
7387
{
7388
    if (unlikely(!ctx->spe_enabled)) {
7389
        GEN_EXCP_NO_AP(ctx);
7390
        return;
7391
    }
7392
#if defined(TARGET_PPC64)
7393
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7394
#else
7395
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7396
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7397
#endif
7398
}
7399

    
7400
/* Conversion */
7401
GEN_SPEFPUOP_CONV_64_64(evfscfui);
7402
GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7403
GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7404
GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7405
GEN_SPEFPUOP_CONV_64_64(evfsctui);
7406
GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7407
GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7408
GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7409
GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7410
GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7411

    
7412
/* Comparison */
7413
GEN_SPEFPUOP_COMP_64(evfscmpgt);
7414
GEN_SPEFPUOP_COMP_64(evfscmplt);
7415
GEN_SPEFPUOP_COMP_64(evfscmpeq);
7416
GEN_SPEFPUOP_COMP_64(evfststgt);
7417
GEN_SPEFPUOP_COMP_64(evfststlt);
7418
GEN_SPEFPUOP_COMP_64(evfststeq);
7419

    
7420
/* Opcodes definitions */
7421
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7422
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7423
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7424
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7425
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7426
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7427
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7428
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7429
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7430
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7431
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7432
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7433
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7434
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7435

    
7436
/* Single precision floating-point operations */
7437
/* Arithmetic */
7438
GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7439
GEN_SPEFPUOP_ARITH2_32_32(efssub);
7440
GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7441
GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7442
static always_inline void gen_efsabs (DisasContext *ctx)
7443
{
7444
    if (unlikely(!ctx->spe_enabled)) {
7445
        GEN_EXCP_NO_AP(ctx);
7446
        return;
7447
    }
7448
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7449
}
7450
static always_inline void gen_efsnabs (DisasContext *ctx)
7451
{
7452
    if (unlikely(!ctx->spe_enabled)) {
7453
        GEN_EXCP_NO_AP(ctx);
7454
        return;
7455
    }
7456
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7457
}
7458
static always_inline void gen_efsneg (DisasContext *ctx)
7459
{
7460
    if (unlikely(!ctx->spe_enabled)) {
7461
        GEN_EXCP_NO_AP(ctx);
7462
        return;
7463
    }
7464
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7465
}
7466

    
7467
/* Conversion */
7468
GEN_SPEFPUOP_CONV_32_32(efscfui);
7469
GEN_SPEFPUOP_CONV_32_32(efscfsi);
7470
GEN_SPEFPUOP_CONV_32_32(efscfuf);
7471
GEN_SPEFPUOP_CONV_32_32(efscfsf);
7472
GEN_SPEFPUOP_CONV_32_32(efsctui);
7473
GEN_SPEFPUOP_CONV_32_32(efsctsi);
7474
GEN_SPEFPUOP_CONV_32_32(efsctuf);
7475
GEN_SPEFPUOP_CONV_32_32(efsctsf);
7476
GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7477
GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7478
GEN_SPEFPUOP_CONV_32_64(efscfd);
7479

    
7480
/* Comparison */
7481
GEN_SPEFPUOP_COMP_32(efscmpgt);
7482
GEN_SPEFPUOP_COMP_32(efscmplt);
7483
GEN_SPEFPUOP_COMP_32(efscmpeq);
7484
GEN_SPEFPUOP_COMP_32(efststgt);
7485
GEN_SPEFPUOP_COMP_32(efststlt);
7486
GEN_SPEFPUOP_COMP_32(efststeq);
7487

    
7488
/* Opcodes definitions */
7489
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7490
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7491
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7492
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7493
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7494
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7495
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7496
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7497
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7498
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7499
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7500
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7501
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7502
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7503

    
7504
/* Double precision floating-point operations */
7505
/* Arithmetic */
7506
GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7507
GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7508
GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7509
GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7510
static always_inline void gen_efdabs (DisasContext *ctx)
7511
{
7512
    if (unlikely(!ctx->spe_enabled)) {
7513
        GEN_EXCP_NO_AP(ctx);
7514
        return;
7515
    }
7516
#if defined(TARGET_PPC64)
7517
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7518
#else
7519
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7520
#endif
7521
}
7522
static always_inline void gen_efdnabs (DisasContext *ctx)
7523
{
7524
    if (unlikely(!ctx->spe_enabled)) {
7525
        GEN_EXCP_NO_AP(ctx);
7526
        return;
7527
    }
7528
#if defined(TARGET_PPC64)
7529
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7530
#else
7531
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7532
#endif
7533
}
7534
static always_inline void gen_efdneg (DisasContext *ctx)
7535
{
7536
    if (unlikely(!ctx->spe_enabled)) {
7537
        GEN_EXCP_NO_AP(ctx);
7538
        return;
7539
    }
7540
#if defined(TARGET_PPC64)
7541
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7542
#else
7543
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7544
#endif
7545
}
7546

    
7547
/* Conversion */
7548
GEN_SPEFPUOP_CONV_64_32(efdcfui);
7549
GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7550
GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7551
GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7552
GEN_SPEFPUOP_CONV_32_64(efdctui);
7553
GEN_SPEFPUOP_CONV_32_64(efdctsi);
7554
GEN_SPEFPUOP_CONV_32_64(efdctuf);
7555
GEN_SPEFPUOP_CONV_32_64(efdctsf);
7556
GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7557
GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7558
GEN_SPEFPUOP_CONV_64_32(efdcfs);
7559
GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7560
GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7561
GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7562
GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7563

    
7564
/* Comparison */
7565
GEN_SPEFPUOP_COMP_64(efdcmpgt);
7566
GEN_SPEFPUOP_COMP_64(efdcmplt);
7567
GEN_SPEFPUOP_COMP_64(efdcmpeq);
7568
GEN_SPEFPUOP_COMP_64(efdtstgt);
7569
GEN_SPEFPUOP_COMP_64(efdtstlt);
7570
GEN_SPEFPUOP_COMP_64(efdtsteq);
7571

    
7572
/* Opcodes definitions */
7573
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7574
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7575
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7576
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7577
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7578
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7579
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7580
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7581
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7582
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7583
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7584
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7585
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7586
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7587
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7588
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7589

    
7590
/* End opcode list */
7591
GEN_OPCODE_MARK(end);
7592

    
7593
#include "translate_init.c"
7594
#include "helper_regs.h"
7595

    
7596
/*****************************************************************************/
7597
/* Misc PowerPC helpers */
7598
void cpu_dump_state (CPUState *env, FILE *f,
7599
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7600
                     int flags)
7601
{
7602
#define RGPL  4
7603
#define RFPL  4
7604

    
7605
    int i;
7606

    
7607
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
7608
                env->nip, env->lr, env->ctr, env->xer);
7609
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
7610
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7611
#if !defined(NO_TIMER_DUMP)
7612
    cpu_fprintf(f, "TB %08x %08x "
7613
#if !defined(CONFIG_USER_ONLY)
7614
                "DECR %08x"
7615
#endif
7616
                "\n",
7617
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7618
#if !defined(CONFIG_USER_ONLY)
7619
                , cpu_ppc_load_decr(env)
7620
#endif
7621
                );
7622
#endif
7623
    for (i = 0; i < 32; i++) {
7624
        if ((i & (RGPL - 1)) == 0)
7625
            cpu_fprintf(f, "GPR%02d", i);
7626
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7627
        if ((i & (RGPL - 1)) == (RGPL - 1))
7628
            cpu_fprintf(f, "\n");
7629
    }
7630
    cpu_fprintf(f, "CR ");
7631
    for (i = 0; i < 8; i++)
7632
        cpu_fprintf(f, "%01x", env->crf[i]);
7633
    cpu_fprintf(f, "  [");
7634
    for (i = 0; i < 8; i++) {
7635
        char a = '-';
7636
        if (env->crf[i] & 0x08)
7637
            a = 'L';
7638
        else if (env->crf[i] & 0x04)
7639
            a = 'G';
7640
        else if (env->crf[i] & 0x02)
7641
            a = 'E';
7642
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7643
    }
7644
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7645
    for (i = 0; i < 32; i++) {
7646
        if ((i & (RFPL - 1)) == 0)
7647
            cpu_fprintf(f, "FPR%02d", i);
7648
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7649
        if ((i & (RFPL - 1)) == (RFPL - 1))
7650
            cpu_fprintf(f, "\n");
7651
    }
7652
#if !defined(CONFIG_USER_ONLY)
7653
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7654
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7655
#endif
7656

    
7657
#undef RGPL
7658
#undef RFPL
7659
}
7660

    
7661
void cpu_dump_statistics (CPUState *env, FILE*f,
7662
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7663
                          int flags)
7664
{
7665
#if defined(DO_PPC_STATISTICS)
7666
    opc_handler_t **t1, **t2, **t3, *handler;
7667
    int op1, op2, op3;
7668

    
7669
    t1 = env->opcodes;
7670
    for (op1 = 0; op1 < 64; op1++) {
7671
        handler = t1[op1];
7672
        if (is_indirect_opcode(handler)) {
7673
            t2 = ind_table(handler);
7674
            for (op2 = 0; op2 < 32; op2++) {
7675
                handler = t2[op2];
7676
                if (is_indirect_opcode(handler)) {
7677
                    t3 = ind_table(handler);
7678
                    for (op3 = 0; op3 < 32; op3++) {
7679
                        handler = t3[op3];
7680
                        if (handler->count == 0)
7681
                            continue;
7682
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7683
                                    "%016llx %lld\n",
7684
                                    op1, op2, op3, op1, (op3 << 5) | op2,
7685
                                    handler->oname,
7686
                                    handler->count, handler->count);
7687
                    }
7688
                } else {
7689
                    if (handler->count == 0)
7690
                        continue;
7691
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
7692
                                "%016llx %lld\n",
7693
                                op1, op2, op1, op2, handler->oname,
7694
                                handler->count, handler->count);
7695
                }
7696
            }
7697
        } else {
7698
            if (handler->count == 0)
7699
                continue;
7700
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
7701
                        op1, op1, handler->oname,
7702
                        handler->count, handler->count);
7703
        }
7704
    }
7705
#endif
7706
}
7707

    
7708
/*****************************************************************************/
7709
static always_inline void gen_intermediate_code_internal (CPUState *env,
7710
                                                          TranslationBlock *tb,
7711
                                                          int search_pc)
7712
{
7713
    DisasContext ctx, *ctxp = &ctx;
7714
    opc_handler_t **table, *handler;
7715
    target_ulong pc_start;
7716
    uint16_t *gen_opc_end;
7717
    CPUBreakpoint *bp;
7718
    int j, lj = -1;
7719
    int num_insns;
7720
    int max_insns;
7721

    
7722
    pc_start = tb->pc;
7723
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7724
#if defined(OPTIMIZE_FPRF_UPDATE)
7725
    gen_fprf_ptr = gen_fprf_buf;
7726
#endif
7727
    ctx.nip = pc_start;
7728
    ctx.tb = tb;
7729
    ctx.exception = POWERPC_EXCP_NONE;
7730
    ctx.spr_cb = env->spr_cb;
7731
    ctx.mem_idx = env->mmu_idx;
7732
    ctx.access_type = -1;
7733
    ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
7734
#if defined(TARGET_PPC64)
7735
    ctx.sf_mode = msr_sf;
7736
#endif
7737
    ctx.fpu_enabled = msr_fp;
7738
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7739
        ctx.spe_enabled = msr_spe;
7740
    else
7741
        ctx.spe_enabled = 0;
7742
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7743
        ctx.altivec_enabled = msr_vr;
7744
    else
7745
        ctx.altivec_enabled = 0;
7746
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7747
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
7748
    else
7749
        ctx.singlestep_enabled = 0;
7750
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7751
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7752
    if (unlikely(env->singlestep_enabled))
7753
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7754
#if defined (DO_SINGLE_STEP) && 0
7755
    /* Single step trace mode */
7756
    msr_se = 1;
7757
#endif
7758
    num_insns = 0;
7759
    max_insns = tb->cflags & CF_COUNT_MASK;
7760
    if (max_insns == 0)
7761
        max_insns = CF_COUNT_MASK;
7762

    
7763
    gen_icount_start();
7764
    /* Set env in case of segfault during code fetch */
7765
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7766
        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7767
            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7768
                if (bp->pc == ctx.nip) {
7769
                    gen_update_nip(&ctx, ctx.nip);
7770
                    gen_helper_raise_debug();
7771
                    break;
7772
                }
7773
            }
7774
        }
7775
        if (unlikely(search_pc)) {
7776
            j = gen_opc_ptr - gen_opc_buf;
7777
            if (lj < j) {
7778
                lj++;
7779
                while (lj < j)
7780
                    gen_opc_instr_start[lj++] = 0;
7781
                gen_opc_pc[lj] = ctx.nip;
7782
                gen_opc_instr_start[lj] = 1;
7783
                gen_opc_icount[lj] = num_insns;
7784
            }
7785
        }
7786
#if defined PPC_DEBUG_DISAS
7787
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7788
            fprintf(logfile, "----------------\n");
7789
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7790
                    ctx.nip, ctx.mem_idx, (int)msr_ir);
7791
        }
7792
#endif
7793
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7794
            gen_io_start();
7795
        if (unlikely(ctx.le_mode)) {
7796
            ctx.opcode = bswap32(ldl_code(ctx.nip));
7797
        } else {
7798
            ctx.opcode = ldl_code(ctx.nip);
7799
        }
7800
#if defined PPC_DEBUG_DISAS
7801
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7802
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7803
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7804
                    opc3(ctx.opcode), little_endian ? "little" : "big");
7805
        }
7806
#endif
7807
        ctx.nip += 4;
7808
        table = env->opcodes;
7809
        num_insns++;
7810
        handler = table[opc1(ctx.opcode)];
7811
        if (is_indirect_opcode(handler)) {
7812
            table = ind_table(handler);
7813
            handler = table[opc2(ctx.opcode)];
7814
            if (is_indirect_opcode(handler)) {
7815
                table = ind_table(handler);
7816
                handler = table[opc3(ctx.opcode)];
7817
            }
7818
        }
7819
        /* Is opcode *REALLY* valid ? */
7820
        if (unlikely(handler->handler == &gen_invalid)) {
7821
            if (loglevel != 0) {
7822
                fprintf(logfile, "invalid/unsupported opcode: "
7823
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7824
                        opc1(ctx.opcode), opc2(ctx.opcode),
7825
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7826
            } else {
7827
                printf("invalid/unsupported opcode: "
7828
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7829
                       opc1(ctx.opcode), opc2(ctx.opcode),
7830
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7831
            }
7832
        } else {
7833
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
7834
                if (loglevel != 0) {
7835
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7836
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
7837
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
7838
                            opc2(ctx.opcode), opc3(ctx.opcode),
7839
                            ctx.opcode, ctx.nip - 4);
7840
                } else {
7841
                    printf("invalid bits: %08x for opcode: "
7842
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
7843
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
7844
                           opc2(ctx.opcode), opc3(ctx.opcode),
7845
                           ctx.opcode, ctx.nip - 4);
7846
                }
7847
                GEN_EXCP_INVAL(ctxp);
7848
                break;
7849
            }
7850
        }
7851
        (*(handler->handler))(&ctx);
7852
#if defined(DO_PPC_STATISTICS)
7853
        handler->count++;
7854
#endif
7855
        /* Check trace mode exceptions */
7856
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7857
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7858
                     ctx.exception != POWERPC_SYSCALL &&
7859
                     ctx.exception != POWERPC_EXCP_TRAP &&
7860
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
7861
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7862
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7863
                            (env->singlestep_enabled) ||
7864
                            num_insns >= max_insns)) {
7865
            /* if we reach a page boundary or are single stepping, stop
7866
             * generation
7867
             */
7868
            break;
7869
        }
7870
#if defined (DO_SINGLE_STEP)
7871
        break;
7872
#endif
7873
    }
7874
    if (tb->cflags & CF_LAST_IO)
7875
        gen_io_end();
7876
    if (ctx.exception == POWERPC_EXCP_NONE) {
7877
        gen_goto_tb(&ctx, 0, ctx.nip);
7878
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7879
        if (unlikely(env->singlestep_enabled)) {
7880
            gen_update_nip(&ctx, ctx.nip);
7881
            gen_helper_raise_debug();
7882
        }
7883
        /* Generate the return instruction */
7884
        tcg_gen_exit_tb(0);
7885
    }
7886
    gen_icount_end(tb, num_insns);
7887
    *gen_opc_ptr = INDEX_op_end;
7888
    if (unlikely(search_pc)) {
7889
        j = gen_opc_ptr - gen_opc_buf;
7890
        lj++;
7891
        while (lj <= j)
7892
            gen_opc_instr_start[lj++] = 0;
7893
    } else {
7894
        tb->size = ctx.nip - pc_start;
7895
        tb->icount = num_insns;
7896
    }
7897
#if defined(DEBUG_DISAS)
7898
    if (loglevel & CPU_LOG_TB_CPU) {
7899
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7900
        cpu_dump_state(env, logfile, fprintf, 0);
7901
    }
7902
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7903
        int flags;
7904
        flags = env->bfd_mach;
7905
        flags |= ctx.le_mode << 16;
7906
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7907
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7908
        fprintf(logfile, "\n");
7909
    }
7910
#endif
7911
}
7912

    
7913
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7914
{
7915
    gen_intermediate_code_internal(env, tb, 0);
7916
}
7917

    
7918
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7919
{
7920
    gen_intermediate_code_internal(env, tb, 1);
7921
}
7922

    
7923
void gen_pc_load(CPUState *env, TranslationBlock *tb,
7924
                unsigned long searched_pc, int pc_pos, void *puc)
7925
{
7926
    env->nip = gen_opc_pc[pc_pos];
7927
}