Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 6a6ae23f

History | View | Annotate | Download (284.3 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_ctr;
67
static TCGv cpu_lr;
68
static TCGv cpu_xer;
69
static TCGv_i32 cpu_fpscr;
70
static TCGv_i32 cpu_access_type;
71

    
72
/* dyngen register indexes */
73
static TCGv cpu_T[3];
74
static TCGv_i64 cpu_FT[2];
75

    
76
#include "gen-icount.h"
77

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

    
84
    if (done_init)
85
        return;
86

    
87
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
88
#if TARGET_LONG_BITS > HOST_LONG_BITS
89
    cpu_T[0] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t0), "T0");
90
    cpu_T[1] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t1), "T1");
91
    cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
92
#else
93
    cpu_T[0] = tcg_global_reg_new(TCG_AREG1, "T0");
94
    cpu_T[1] = tcg_global_reg_new(TCG_AREG2, "T1");
95
#ifdef HOST_I386
96
    /* XXX: This is a temporary workaround for i386.
97
     *      On i386 qemu_st32 runs out of registers.
98
     *      The proper fix is to remove cpu_T.
99
     */
100
    cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
101
#else
102
    cpu_T[2] = tcg_global_reg_new(TCG_AREG3, "T2");
103
#endif
104
#endif
105

    
106
    cpu_FT[0] = tcg_global_mem_new_i64(TCG_AREG0,
107
                                       offsetof(CPUState, ft0), "FT0");
108
    cpu_FT[1] = tcg_global_mem_new_i64(TCG_AREG0,
109
                                       offsetof(CPUState, ft1), "FT1");
110

    
111
    p = cpu_reg_names;
112

    
113
    for (i = 0; i < 8; i++) {
114
        sprintf(p, "crf%d", i);
115
        cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
116
                                            offsetof(CPUState, crf[i]), p);
117
        p += 5;
118
    }
119

    
120
    for (i = 0; i < 32; i++) {
121
        sprintf(p, "r%d", i);
122
        cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
123
                                        offsetof(CPUState, gpr[i]), p);
124
        p += (i < 10) ? 3 : 4;
125
#if !defined(TARGET_PPC64)
126
        sprintf(p, "r%dH", i);
127
        cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
128
                                             offsetof(CPUState, gprh[i]), p);
129
        p += (i < 10) ? 4 : 5;
130
#endif
131

    
132
        sprintf(p, "fp%d", i);
133
        cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
134
                                            offsetof(CPUState, fpr[i]), p);
135
        p += (i < 10) ? 4 : 5;
136

    
137
        sprintf(p, "avr%dH", i);
138
#ifdef WORDS_BIGENDIAN
139
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
140
                                             offsetof(CPUState, avr[i].u64[0]), p);
141
#else
142
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
143
                                             offsetof(CPUState, avr[i].u64[1]), p);
144
#endif
145
        p += (i < 10) ? 6 : 7;
146

    
147
        sprintf(p, "avr%dL", i);
148
#ifdef WORDS_BIGENDIAN
149
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
150
                                             offsetof(CPUState, avr[i].u64[1]), p);
151
#else
152
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
153
                                             offsetof(CPUState, avr[i].u64[0]), p);
154
#endif
155
        p += (i < 10) ? 6 : 7;
156
    }
157

    
158
    cpu_nip = tcg_global_mem_new(TCG_AREG0,
159
                                 offsetof(CPUState, nip), "nip");
160

    
161
    cpu_ctr = tcg_global_mem_new(TCG_AREG0,
162
                                 offsetof(CPUState, ctr), "ctr");
163

    
164
    cpu_lr = tcg_global_mem_new(TCG_AREG0,
165
                                offsetof(CPUState, lr), "lr");
166

    
167
    cpu_xer = tcg_global_mem_new(TCG_AREG0,
168
                                 offsetof(CPUState, xer), "xer");
169

    
170
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
171
                                       offsetof(CPUState, fpscr), "fpscr");
172

    
173
    cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
174
                                             offsetof(CPUState, access_type), "access_type");
175

    
176
    /* register helpers */
177
#define GEN_HELPER 2
178
#include "helper.h"
179

    
180
    done_init = 1;
181
}
182

    
183
#if defined(OPTIMIZE_FPRF_UPDATE)
184
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
185
static uint16_t **gen_fprf_ptr;
186
#endif
187

    
188
/* internal defines */
189
typedef struct DisasContext {
190
    struct TranslationBlock *tb;
191
    target_ulong nip;
192
    uint32_t opcode;
193
    uint32_t exception;
194
    /* Routine used to access memory */
195
    int mem_idx;
196
    /* Translation flags */
197
#if !defined(CONFIG_USER_ONLY)
198
    int supervisor;
199
#endif
200
#if defined(TARGET_PPC64)
201
    int sf_mode;
202
#endif
203
    int fpu_enabled;
204
    int altivec_enabled;
205
    int spe_enabled;
206
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
207
    int singlestep_enabled;
208
    int dcache_line_size;
209
} DisasContext;
210

    
211
struct opc_handler_t {
212
    /* invalid bits */
213
    uint32_t inval;
214
    /* instruction type */
215
    uint64_t type;
216
    /* handler */
217
    void (*handler)(DisasContext *ctx);
218
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
219
    const char *oname;
220
#endif
221
#if defined(DO_PPC_STATISTICS)
222
    uint64_t count;
223
#endif
224
};
225

    
226
static always_inline void gen_reset_fpstatus (void)
227
{
228
#ifdef CONFIG_SOFTFLOAT
229
    gen_op_reset_fpstatus();
230
#endif
231
}
232

    
233
static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
234
{
235
    TCGv_i32 t0 = tcg_temp_new_i32();
236

    
237
    if (set_fprf != 0) {
238
        /* This case might be optimized later */
239
#if defined(OPTIMIZE_FPRF_UPDATE)
240
        *gen_fprf_ptr++ = gen_opc_ptr;
241
#endif
242
        tcg_gen_movi_i32(t0, 1);
243
        gen_helper_compute_fprf(t0, arg, t0);
244
        if (unlikely(set_rc)) {
245
            tcg_gen_mov_i32(cpu_crf[1], t0);
246
        }
247
        gen_helper_float_check_status();
248
    } else if (unlikely(set_rc)) {
249
        /* We always need to compute fpcc */
250
        tcg_gen_movi_i32(t0, 0);
251
        gen_helper_compute_fprf(t0, arg, t0);
252
        tcg_gen_mov_i32(cpu_crf[1], t0);
253
        if (set_fprf)
254
            gen_helper_float_check_status();
255
    }
256

    
257
    tcg_temp_free_i32(t0);
258
}
259

    
260
static always_inline void gen_optimize_fprf (void)
261
{
262
#if defined(OPTIMIZE_FPRF_UPDATE)
263
    uint16_t **ptr;
264

    
265
    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
266
        *ptr = INDEX_op_nop1;
267
    gen_fprf_ptr = gen_fprf_buf;
268
#endif
269
}
270

    
271
static always_inline void gen_set_access_type(int access_type)
272
{
273
    tcg_gen_movi_i32(cpu_access_type, access_type);
274
}
275

    
276
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
277
{
278
#if defined(TARGET_PPC64)
279
    if (ctx->sf_mode)
280
        tcg_gen_movi_tl(cpu_nip, nip);
281
    else
282
#endif
283
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
284
}
285

    
286
#define GEN_EXCP(ctx, excp, error)                                            \
287
do {                                                                          \
288
    TCGv_i32 t0 = tcg_const_i32(excp);                                        \
289
    TCGv_i32 t1 = tcg_const_i32(error);                                       \
290
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
291
        gen_update_nip(ctx, (ctx)->nip);                                      \
292
    }                                                                         \
293
    gen_helper_raise_exception_err(t0, t1);                                   \
294
    tcg_temp_free_i32(t0);                                                    \
295
    tcg_temp_free_i32(t1);                                                    \
296
    ctx->exception = (excp);                                                  \
297
} while (0)
298

    
299
#define GEN_EXCP_INVAL(ctx)                                                   \
300
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
301
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
302

    
303
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
304
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
305
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
306

    
307
#define GEN_EXCP_PRIVREG(ctx)                                                 \
308
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
309
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
310

    
311
#define GEN_EXCP_NO_FP(ctx)                                                   \
312
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
313

    
314
#define GEN_EXCP_NO_AP(ctx)                                                   \
315
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
316

    
317
#define GEN_EXCP_NO_VR(ctx)                                                   \
318
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
319

    
320
/* Stop translation */
321
static always_inline void GEN_STOP (DisasContext *ctx)
322
{
323
    gen_update_nip(ctx, ctx->nip);
324
    ctx->exception = POWERPC_EXCP_STOP;
325
}
326

    
327
/* No need to update nip here, as execution flow will change */
328
static always_inline void GEN_SYNC (DisasContext *ctx)
329
{
330
    ctx->exception = POWERPC_EXCP_SYNC;
331
}
332

    
333
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
334
static void gen_##name (DisasContext *ctx);                                   \
335
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
336
static void gen_##name (DisasContext *ctx)
337

    
338
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
339
static void gen_##name (DisasContext *ctx);                                   \
340
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
341
static void gen_##name (DisasContext *ctx)
342

    
343
typedef struct opcode_t {
344
    unsigned char opc1, opc2, opc3;
345
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
346
    unsigned char pad[5];
347
#else
348
    unsigned char pad[1];
349
#endif
350
    opc_handler_t handler;
351
    const char *oname;
352
} opcode_t;
353

    
354
/*****************************************************************************/
355
/***                           Instruction decoding                        ***/
356
#define EXTRACT_HELPER(name, shift, nb)                                       \
357
static always_inline uint32_t name (uint32_t opcode)                          \
358
{                                                                             \
359
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
360
}
361

    
362
#define EXTRACT_SHELPER(name, shift, nb)                                      \
363
static always_inline int32_t name (uint32_t opcode)                           \
364
{                                                                             \
365
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
366
}
367

    
368
/* Opcode part 1 */
369
EXTRACT_HELPER(opc1, 26, 6);
370
/* Opcode part 2 */
371
EXTRACT_HELPER(opc2, 1, 5);
372
/* Opcode part 3 */
373
EXTRACT_HELPER(opc3, 6, 5);
374
/* Update Cr0 flags */
375
EXTRACT_HELPER(Rc, 0, 1);
376
/* Destination */
377
EXTRACT_HELPER(rD, 21, 5);
378
/* Source */
379
EXTRACT_HELPER(rS, 21, 5);
380
/* First operand */
381
EXTRACT_HELPER(rA, 16, 5);
382
/* Second operand */
383
EXTRACT_HELPER(rB, 11, 5);
384
/* Third operand */
385
EXTRACT_HELPER(rC, 6, 5);
386
/***                               Get CRn                                 ***/
387
EXTRACT_HELPER(crfD, 23, 3);
388
EXTRACT_HELPER(crfS, 18, 3);
389
EXTRACT_HELPER(crbD, 21, 5);
390
EXTRACT_HELPER(crbA, 16, 5);
391
EXTRACT_HELPER(crbB, 11, 5);
392
/* SPR / TBL */
393
EXTRACT_HELPER(_SPR, 11, 10);
394
static always_inline uint32_t SPR (uint32_t opcode)
395
{
396
    uint32_t sprn = _SPR(opcode);
397

    
398
    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
399
}
400
/***                              Get constants                            ***/
401
EXTRACT_HELPER(IMM, 12, 8);
402
/* 16 bits signed immediate value */
403
EXTRACT_SHELPER(SIMM, 0, 16);
404
/* 16 bits unsigned immediate value */
405
EXTRACT_HELPER(UIMM, 0, 16);
406
/* Bit count */
407
EXTRACT_HELPER(NB, 11, 5);
408
/* Shift count */
409
EXTRACT_HELPER(SH, 11, 5);
410
/* Mask start */
411
EXTRACT_HELPER(MB, 6, 5);
412
/* Mask end */
413
EXTRACT_HELPER(ME, 1, 5);
414
/* Trap operand */
415
EXTRACT_HELPER(TO, 21, 5);
416

    
417
EXTRACT_HELPER(CRM, 12, 8);
418
EXTRACT_HELPER(FM, 17, 8);
419
EXTRACT_HELPER(SR, 16, 4);
420
EXTRACT_HELPER(FPIMM, 12, 4);
421

    
422
/***                            Jump target decoding                       ***/
423
/* Displacement */
424
EXTRACT_SHELPER(d, 0, 16);
425
/* Immediate address */
426
static always_inline target_ulong LI (uint32_t opcode)
427
{
428
    return (opcode >> 0) & 0x03FFFFFC;
429
}
430

    
431
static always_inline uint32_t BD (uint32_t opcode)
432
{
433
    return (opcode >> 0) & 0xFFFC;
434
}
435

    
436
EXTRACT_HELPER(BO, 21, 5);
437
EXTRACT_HELPER(BI, 16, 5);
438
/* Absolute/relative address */
439
EXTRACT_HELPER(AA, 1, 1);
440
/* Link */
441
EXTRACT_HELPER(LK, 0, 1);
442

    
443
/* Create a mask between <start> and <end> bits */
444
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
445
{
446
    target_ulong ret;
447

    
448
#if defined(TARGET_PPC64)
449
    if (likely(start == 0)) {
450
        ret = UINT64_MAX << (63 - end);
451
    } else if (likely(end == 63)) {
452
        ret = UINT64_MAX >> start;
453
    }
454
#else
455
    if (likely(start == 0)) {
456
        ret = UINT32_MAX << (31  - end);
457
    } else if (likely(end == 31)) {
458
        ret = UINT32_MAX >> start;
459
    }
460
#endif
461
    else {
462
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
463
            (((target_ulong)(-1ULL) >> (end)) >> 1);
464
        if (unlikely(start > end))
465
            return ~ret;
466
    }
467

    
468
    return ret;
469
}
470

    
471
/*****************************************************************************/
472
/* PowerPC Instructions types definitions                                    */
473
enum {
474
    PPC_NONE           = 0x0000000000000000ULL,
475
    /* PowerPC base instructions set                                         */
476
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
477
    /*   integer operations instructions                                     */
478
#define PPC_INTEGER PPC_INSNS_BASE
479
    /*   flow control instructions                                           */
480
#define PPC_FLOW    PPC_INSNS_BASE
481
    /*   virtual memory instructions                                         */
482
#define PPC_MEM     PPC_INSNS_BASE
483
    /*   ld/st with reservation instructions                                 */
484
#define PPC_RES     PPC_INSNS_BASE
485
    /*   spr/msr access instructions                                         */
486
#define PPC_MISC    PPC_INSNS_BASE
487
    /* Deprecated instruction sets                                           */
488
    /*   Original POWER instruction set                                      */
489
    PPC_POWER          = 0x0000000000000002ULL,
490
    /*   POWER2 instruction set extension                                    */
491
    PPC_POWER2         = 0x0000000000000004ULL,
492
    /*   Power RTC support                                                   */
493
    PPC_POWER_RTC      = 0x0000000000000008ULL,
494
    /*   Power-to-PowerPC bridge (601)                                       */
495
    PPC_POWER_BR       = 0x0000000000000010ULL,
496
    /* 64 bits PowerPC instruction set                                       */
497
    PPC_64B            = 0x0000000000000020ULL,
498
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
499
    PPC_64BX           = 0x0000000000000040ULL,
500
    /*   64 bits hypervisor extensions                                       */
501
    PPC_64H            = 0x0000000000000080ULL,
502
    /*   New wait instruction (PowerPC 2.0x)                                 */
503
    PPC_WAIT           = 0x0000000000000100ULL,
504
    /*   Time base mftb instruction                                          */
505
    PPC_MFTB           = 0x0000000000000200ULL,
506

    
507
    /* Fixed-point unit extensions                                           */
508
    /*   PowerPC 602 specific                                                */
509
    PPC_602_SPEC       = 0x0000000000000400ULL,
510
    /*   isel instruction                                                    */
511
    PPC_ISEL           = 0x0000000000000800ULL,
512
    /*   popcntb instruction                                                 */
513
    PPC_POPCNTB        = 0x0000000000001000ULL,
514
    /*   string load / store                                                 */
515
    PPC_STRING         = 0x0000000000002000ULL,
516

    
517
    /* Floating-point unit extensions                                        */
518
    /*   Optional floating point instructions                                */
519
    PPC_FLOAT          = 0x0000000000010000ULL,
520
    /* New floating-point extensions (PowerPC 2.0x)                          */
521
    PPC_FLOAT_EXT      = 0x0000000000020000ULL,
522
    PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
523
    PPC_FLOAT_FRES     = 0x0000000000080000ULL,
524
    PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
525
    PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
526
    PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
527
    PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,
528

    
529
    /* Vector/SIMD extensions                                                */
530
    /*   Altivec support                                                     */
531
    PPC_ALTIVEC        = 0x0000000001000000ULL,
532
    /*   PowerPC 2.03 SPE extension                                          */
533
    PPC_SPE            = 0x0000000002000000ULL,
534
    /*   PowerPC 2.03 SPE floating-point extension                           */
535
    PPC_SPEFPU         = 0x0000000004000000ULL,
536

    
537
    /* Optional memory control instructions                                  */
538
    PPC_MEM_TLBIA      = 0x0000000010000000ULL,
539
    PPC_MEM_TLBIE      = 0x0000000020000000ULL,
540
    PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
541
    /*   sync instruction                                                    */
542
    PPC_MEM_SYNC       = 0x0000000080000000ULL,
543
    /*   eieio instruction                                                   */
544
    PPC_MEM_EIEIO      = 0x0000000100000000ULL,
545

    
546
    /* Cache control instructions                                            */
547
    PPC_CACHE          = 0x0000000200000000ULL,
548
    /*   icbi instruction                                                    */
549
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
550
    /*   dcbz instruction with fixed cache line size                         */
551
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
552
    /*   dcbz instruction with tunable cache line size                       */
553
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
554
    /*   dcba instruction                                                    */
555
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
556
    /*   Freescale cache locking instructions                                */
557
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
558

    
559
    /* MMU related extensions                                                */
560
    /*   external control instructions                                       */
561
    PPC_EXTERN         = 0x0000010000000000ULL,
562
    /*   segment register access instructions                                */
563
    PPC_SEGMENT        = 0x0000020000000000ULL,
564
    /*   PowerPC 6xx TLB management instructions                             */
565
    PPC_6xx_TLB        = 0x0000040000000000ULL,
566
    /* PowerPC 74xx TLB management instructions                              */
567
    PPC_74xx_TLB       = 0x0000080000000000ULL,
568
    /*   PowerPC 40x TLB management instructions                             */
569
    PPC_40x_TLB        = 0x0000100000000000ULL,
570
    /*   segment register access instructions for PowerPC 64 "bridge"        */
571
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
572
    /*   SLB management                                                      */
573
    PPC_SLBI           = 0x0000400000000000ULL,
574

    
575
    /* Embedded PowerPC dedicated instructions                               */
576
    PPC_WRTEE          = 0x0001000000000000ULL,
577
    /* PowerPC 40x exception model                                           */
578
    PPC_40x_EXCP       = 0x0002000000000000ULL,
579
    /* PowerPC 405 Mac instructions                                          */
580
    PPC_405_MAC        = 0x0004000000000000ULL,
581
    /* PowerPC 440 specific instructions                                     */
582
    PPC_440_SPEC       = 0x0008000000000000ULL,
583
    /* BookE (embedded) PowerPC specification                                */
584
    PPC_BOOKE          = 0x0010000000000000ULL,
585
    /* mfapidi instruction                                                   */
586
    PPC_MFAPIDI        = 0x0020000000000000ULL,
587
    /* tlbiva instruction                                                    */
588
    PPC_TLBIVA         = 0x0040000000000000ULL,
589
    /* tlbivax instruction                                                   */
590
    PPC_TLBIVAX        = 0x0080000000000000ULL,
591
    /* PowerPC 4xx dedicated instructions                                    */
592
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
593
    /* PowerPC 40x ibct instructions                                         */
594
    PPC_40x_ICBT       = 0x0200000000000000ULL,
595
    /* rfmci is not implemented in all BookE PowerPC                         */
596
    PPC_RFMCI          = 0x0400000000000000ULL,
597
    /* rfdi instruction                                                      */
598
    PPC_RFDI           = 0x0800000000000000ULL,
599
    /* DCR accesses                                                          */
600
    PPC_DCR            = 0x1000000000000000ULL,
601
    /* DCR extended accesse                                                  */
602
    PPC_DCRX           = 0x2000000000000000ULL,
603
    /* user-mode DCR access, implemented in PowerPC 460                      */
604
    PPC_DCRUX          = 0x4000000000000000ULL,
605
};
606

    
607
/*****************************************************************************/
608
/* PowerPC instructions table                                                */
609
#if HOST_LONG_BITS == 64
610
#define OPC_ALIGN 8
611
#else
612
#define OPC_ALIGN 4
613
#endif
614
#if defined(__APPLE__)
615
#define OPCODES_SECTION                                                       \
616
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
617
#else
618
#define OPCODES_SECTION                                                       \
619
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
620
#endif
621

    
622
#if defined(DO_PPC_STATISTICS)
623
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
624
OPCODES_SECTION opcode_t opc_##name = {                                       \
625
    .opc1 = op1,                                                              \
626
    .opc2 = op2,                                                              \
627
    .opc3 = op3,                                                              \
628
    .pad  = { 0, },                                                           \
629
    .handler = {                                                              \
630
        .inval   = invl,                                                      \
631
        .type = _typ,                                                         \
632
        .handler = &gen_##name,                                               \
633
        .oname = stringify(name),                                             \
634
    },                                                                        \
635
    .oname = stringify(name),                                                 \
636
}
637
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
638
OPCODES_SECTION opcode_t opc_##name = {                                       \
639
    .opc1 = op1,                                                              \
640
    .opc2 = op2,                                                              \
641
    .opc3 = op3,                                                              \
642
    .pad  = { 0, },                                                           \
643
    .handler = {                                                              \
644
        .inval   = invl,                                                      \
645
        .type = _typ,                                                         \
646
        .handler = &gen_##name,                                               \
647
        .oname = onam,                                                        \
648
    },                                                                        \
649
    .oname = onam,                                                            \
650
}
651
#else
652
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
653
OPCODES_SECTION opcode_t opc_##name = {                                       \
654
    .opc1 = op1,                                                              \
655
    .opc2 = op2,                                                              \
656
    .opc3 = op3,                                                              \
657
    .pad  = { 0, },                                                           \
658
    .handler = {                                                              \
659
        .inval   = invl,                                                      \
660
        .type = _typ,                                                         \
661
        .handler = &gen_##name,                                               \
662
    },                                                                        \
663
    .oname = stringify(name),                                                 \
664
}
665
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
666
OPCODES_SECTION opcode_t opc_##name = {                                       \
667
    .opc1 = op1,                                                              \
668
    .opc2 = op2,                                                              \
669
    .opc3 = op3,                                                              \
670
    .pad  = { 0, },                                                           \
671
    .handler = {                                                              \
672
        .inval   = invl,                                                      \
673
        .type = _typ,                                                         \
674
        .handler = &gen_##name,                                               \
675
    },                                                                        \
676
    .oname = onam,                                                            \
677
}
678
#endif
679

    
680
#define GEN_OPCODE_MARK(name)                                                 \
681
OPCODES_SECTION opcode_t opc_##name = {                                       \
682
    .opc1 = 0xFF,                                                             \
683
    .opc2 = 0xFF,                                                             \
684
    .opc3 = 0xFF,                                                             \
685
    .pad  = { 0, },                                                           \
686
    .handler = {                                                              \
687
        .inval   = 0x00000000,                                                \
688
        .type = 0x00,                                                         \
689
        .handler = NULL,                                                      \
690
    },                                                                        \
691
    .oname = stringify(name),                                                 \
692
}
693

    
694
/* Start opcode list */
695
GEN_OPCODE_MARK(start);
696

    
697
/* Invalid instruction */
698
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
699
{
700
    GEN_EXCP_INVAL(ctx);
701
}
702

    
703
static opc_handler_t invalid_handler = {
704
    .inval   = 0xFFFFFFFF,
705
    .type    = PPC_NONE,
706
    .handler = gen_invalid,
707
};
708

    
709
/***                           Integer comparison                          ***/
710

    
711
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
712
{
713
    int l1, l2, l3;
714

    
715
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
716
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
717
    tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
718

    
719
    l1 = gen_new_label();
720
    l2 = gen_new_label();
721
    l3 = gen_new_label();
722
    if (s) {
723
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
724
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
725
    } else {
726
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
727
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
728
    }
729
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
730
    tcg_gen_br(l3);
731
    gen_set_label(l1);
732
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
733
    tcg_gen_br(l3);
734
    gen_set_label(l2);
735
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
736
    gen_set_label(l3);
737
}
738

    
739
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
740
{
741
    TCGv t0 = tcg_const_local_tl(arg1);
742
    gen_op_cmp(arg0, t0, s, crf);
743
    tcg_temp_free(t0);
744
}
745

    
746
#if defined(TARGET_PPC64)
747
static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
748
{
749
    TCGv t0, t1;
750
    t0 = tcg_temp_local_new();
751
    t1 = tcg_temp_local_new();
752
    if (s) {
753
        tcg_gen_ext32s_tl(t0, arg0);
754
        tcg_gen_ext32s_tl(t1, arg1);
755
    } else {
756
        tcg_gen_ext32u_tl(t0, arg0);
757
        tcg_gen_ext32u_tl(t1, arg1);
758
    }
759
    gen_op_cmp(t0, t1, s, crf);
760
    tcg_temp_free(t1);
761
    tcg_temp_free(t0);
762
}
763

    
764
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
765
{
766
    TCGv t0 = tcg_const_local_tl(arg1);
767
    gen_op_cmp32(arg0, t0, s, crf);
768
    tcg_temp_free(t0);
769
}
770
#endif
771

    
772
static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
773
{
774
#if defined(TARGET_PPC64)
775
    if (!(ctx->sf_mode))
776
        gen_op_cmpi32(reg, 0, 1, 0);
777
    else
778
#endif
779
        gen_op_cmpi(reg, 0, 1, 0);
780
}
781

    
782
/* cmp */
783
GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
784
{
785
#if defined(TARGET_PPC64)
786
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
787
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
788
                     1, crfD(ctx->opcode));
789
    else
790
#endif
791
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
792
                   1, crfD(ctx->opcode));
793
}
794

    
795
/* cmpi */
796
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
797
{
798
#if defined(TARGET_PPC64)
799
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
800
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
801
                      1, crfD(ctx->opcode));
802
    else
803
#endif
804
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
805
                    1, crfD(ctx->opcode));
806
}
807

    
808
/* cmpl */
809
GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
810
{
811
#if defined(TARGET_PPC64)
812
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
813
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
814
                     0, crfD(ctx->opcode));
815
    else
816
#endif
817
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
818
                   0, crfD(ctx->opcode));
819
}
820

    
821
/* cmpli */
822
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
823
{
824
#if defined(TARGET_PPC64)
825
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
826
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
827
                      0, crfD(ctx->opcode));
828
    else
829
#endif
830
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
831
                    0, crfD(ctx->opcode));
832
}
833

    
834
/* isel (PowerPC 2.03 specification) */
835
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
836
{
837
    int l1, l2;
838
    uint32_t bi = rC(ctx->opcode);
839
    uint32_t mask;
840
    TCGv_i32 t0;
841

    
842
    l1 = gen_new_label();
843
    l2 = gen_new_label();
844

    
845
    mask = 1 << (3 - (bi & 0x03));
846
    t0 = tcg_temp_new_i32();
847
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
848
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
849
    if (rA(ctx->opcode) == 0)
850
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
851
    else
852
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
853
    tcg_gen_br(l2);
854
    gen_set_label(l1);
855
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
856
    gen_set_label(l2);
857
    tcg_temp_free_i32(t0);
858
}
859

    
860
/***                           Integer arithmetic                          ***/
861

    
862
static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
863
{
864
    int l1;
865
    TCGv t0;
866

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

    
894
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
895
{
896
    int l1 = gen_new_label();
897

    
898
#if defined(TARGET_PPC64)
899
    if (!(ctx->sf_mode)) {
900
        TCGv t0, t1;
901
        t0 = tcg_temp_new();
902
        t1 = tcg_temp_new();
903

    
904
        tcg_gen_ext32u_tl(t0, arg1);
905
        tcg_gen_ext32u_tl(t1, arg2);
906
        if (sub) {
907
            tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
908
        } else {
909
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
910
        }
911
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
912
        gen_set_label(l1);
913
        tcg_temp_free(t0);
914
        tcg_temp_free(t1);
915
    } else
916
#endif
917
    {
918
        if (sub) {
919
            tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
920
        } else {
921
            tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
922
        }
923
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
924
        gen_set_label(l1);
925
    }
926
}
927

    
928
/* Common add function */
929
static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
930
                                           int add_ca, int compute_ca, int compute_ov)
931
{
932
    TCGv t0, t1;
933

    
934
    if ((!compute_ca && !compute_ov) ||
935
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
936
        t0 = ret;
937
    } else {
938
        t0 = tcg_temp_local_new();
939
    }
940

    
941
    if (add_ca) {
942
        t1 = tcg_temp_local_new();
943
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
944
        tcg_gen_shri_tl(t1, t1, XER_CA);
945
    }
946

    
947
    if (compute_ca && compute_ov) {
948
        /* Start with XER CA and OV disabled, the most likely case */
949
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
950
    } else if (compute_ca) {
951
        /* Start with XER CA disabled, the most likely case */
952
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
953
    } else if (compute_ov) {
954
        /* Start with XER OV disabled, the most likely case */
955
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
956
    }
957

    
958
    tcg_gen_add_tl(t0, arg1, arg2);
959

    
960
    if (compute_ca) {
961
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
962
    }
963
    if (add_ca) {
964
        tcg_gen_add_tl(t0, t0, t1);
965
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
966
        tcg_temp_free(t1);
967
    }
968
    if (compute_ov) {
969
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
970
    }
971

    
972
    if (unlikely(Rc(ctx->opcode) != 0))
973
        gen_set_Rc0(ctx, t0);
974

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

    
1000
/* add  add.  addo  addo. */
1001
GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1002
GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1003
/* addc  addc.  addco  addco. */
1004
GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1005
GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1006
/* adde  adde.  addeo  addeo. */
1007
GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1008
GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1009
/* addme  addme.  addmeo  addmeo.  */
1010
GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1011
GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1012
/* addze  addze.  addzeo  addzeo.*/
1013
GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1014
GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1015
/* addi */
1016
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1017
{
1018
    target_long simm = SIMM(ctx->opcode);
1019

    
1020
    if (rA(ctx->opcode) == 0) {
1021
        /* li case */
1022
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1023
    } else {
1024
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1025
    }
1026
}
1027
/* addic  addic.*/
1028
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1029
                                        int compute_Rc0)
1030
{
1031
    target_long simm = SIMM(ctx->opcode);
1032

    
1033
    /* Start with XER CA and OV disabled, the most likely case */
1034
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1035

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

    
1062
    if (rA(ctx->opcode) == 0) {
1063
        /* lis case */
1064
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1065
    } else {
1066
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1067
    }
1068
}
1069

    
1070
static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1071
                                             int sign, int compute_ov)
1072
{
1073
    int l1 = gen_new_label();
1074
    int l2 = gen_new_label();
1075
    TCGv_i32 t0 = tcg_temp_local_new_i32();
1076
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1077

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

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

    
1173
/* mulhw  mulhw. */
1174
GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1175
{
1176
    TCGv_i64 t0, t1;
1177

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

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

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

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

    
1335
/* Common subf function */
1336
static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1337
                                            int add_ca, int compute_ca, int compute_ov)
1338
{
1339
    TCGv t0, t1;
1340

    
1341
    if ((!compute_ca && !compute_ov) ||
1342
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1343
        t0 = ret;
1344
    } else {
1345
        t0 = tcg_temp_local_new();
1346
    }
1347

    
1348
    if (add_ca) {
1349
        t1 = tcg_temp_local_new();
1350
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1351
        tcg_gen_shri_tl(t1, t1, XER_CA);
1352
    }
1353

    
1354
    if (compute_ca && compute_ov) {
1355
        /* Start with XER CA and OV disabled, the most likely case */
1356
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1357
    } else if (compute_ca) {
1358
        /* Start with XER CA disabled, the most likely case */
1359
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1360
    } else if (compute_ov) {
1361
        /* Start with XER OV disabled, the most likely case */
1362
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1363
    }
1364

    
1365
    if (add_ca) {
1366
        tcg_gen_not_tl(t0, arg1);
1367
        tcg_gen_add_tl(t0, t0, arg2);
1368
        gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1369
        tcg_gen_add_tl(t0, t0, t1);
1370
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
1371
        tcg_temp_free(t1);
1372
    } else {
1373
        tcg_gen_sub_tl(t0, arg2, arg1);
1374
        if (compute_ca) {
1375
            gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1376
        }
1377
    }
1378
    if (compute_ov) {
1379
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1380
    }
1381

    
1382
    if (unlikely(Rc(ctx->opcode) != 0))
1383
        gen_set_Rc0(ctx, t0);
1384

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

    
1438
/***                            Integer logical                            ***/
1439
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1440
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
1441
{                                                                             \
1442
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1443
       cpu_gpr[rB(ctx->opcode)]);                                             \
1444
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1445
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1446
}
1447

    
1448
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1449
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1450
{                                                                             \
1451
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1452
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1453
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1454
}
1455

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

    
1494
    rs = rS(ctx->opcode);
1495
    ra = rA(ctx->opcode);
1496
    rb = rB(ctx->opcode);
1497
    /* Optimisation for mr. ri case */
1498
    if (rs != ra || rs != rb) {
1499
        if (rs != rb)
1500
            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1501
        else
1502
            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1503
        if (unlikely(Rc(ctx->opcode) != 0))
1504
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1505
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1506
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1507
#if defined(TARGET_PPC64)
1508
    } else {
1509
        int prio = 0;
1510

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

    
1583
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1584
        /* NOP */
1585
        /* XXX: should handle special NOPs for POWER series */
1586
        return;
1587
    }
1588
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1589
}
1590
/* oris */
1591
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1592
{
1593
    target_ulong uimm = UIMM(ctx->opcode);
1594

    
1595
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1596
        /* NOP */
1597
        return;
1598
    }
1599
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1600
}
1601
/* xori */
1602
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1603
{
1604
    target_ulong uimm = UIMM(ctx->opcode);
1605

    
1606
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1607
        /* NOP */
1608
        return;
1609
    }
1610
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1611
}
1612
/* xoris */
1613
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1614
{
1615
    target_ulong uimm = UIMM(ctx->opcode);
1616

    
1617
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1618
        /* NOP */
1619
        return;
1620
    }
1621
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1622
}
1623
/* popcntb : PowerPC 2.03 specification */
1624
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1625
{
1626
#if defined(TARGET_PPC64)
1627
    if (ctx->sf_mode)
1628
        gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1629
    else
1630
#endif
1631
        gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1632
}
1633

    
1634
#if defined(TARGET_PPC64)
1635
/* extsw & extsw. */
1636
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1637
/* cntlzd */
1638
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1639
{
1640
    gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1641
    if (unlikely(Rc(ctx->opcode) != 0))
1642
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1643
}
1644
#endif
1645

    
1646
/***                             Integer rotate                            ***/
1647
/* rlwimi & rlwimi. */
1648
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1649
{
1650
    uint32_t mb, me, sh;
1651

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

    
1690
    sh = SH(ctx->opcode);
1691
    mb = MB(ctx->opcode);
1692
    me = ME(ctx->opcode);
1693

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

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

    
1770
#if defined(TARGET_PPC64)
1771
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1772
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1773
{                                                                             \
1774
    gen_##name(ctx, 0);                                                       \
1775
}                                                                             \
1776
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1777
             PPC_64B)                                                         \
1778
{                                                                             \
1779
    gen_##name(ctx, 1);                                                       \
1780
}
1781
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1782
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1783
{                                                                             \
1784
    gen_##name(ctx, 0, 0);                                                    \
1785
}                                                                             \
1786
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1787
             PPC_64B)                                                         \
1788
{                                                                             \
1789
    gen_##name(ctx, 0, 1);                                                    \
1790
}                                                                             \
1791
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1792
             PPC_64B)                                                         \
1793
{                                                                             \
1794
    gen_##name(ctx, 1, 0);                                                    \
1795
}                                                                             \
1796
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1797
             PPC_64B)                                                         \
1798
{                                                                             \
1799
    gen_##name(ctx, 1, 1);                                                    \
1800
}
1801

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

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

    
1837
    sh = SH(ctx->opcode) | (shn << 5);
1838
    me = MB(ctx->opcode) | (men << 5);
1839
    gen_rldinm(ctx, 0, me, sh);
1840
}
1841
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1842
/* rldic - rldic. */
1843
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1844
{
1845
    uint32_t sh, mb;
1846

    
1847
    sh = SH(ctx->opcode) | (shn << 5);
1848
    mb = MB(ctx->opcode) | (mbn << 5);
1849
    gen_rldinm(ctx, mb, 63 - sh, sh);
1850
}
1851
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1852

    
1853
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1854
                                     uint32_t me)
1855
{
1856
    TCGv t0;
1857

    
1858
    mb = MB(ctx->opcode);
1859
    me = ME(ctx->opcode);
1860
    t0 = tcg_temp_new();
1861
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1862
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1863
    if (unlikely(mb != 0 || me != 63)) {
1864
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1865
    } else {
1866
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1867
    }
1868
    tcg_temp_free(t0);
1869
    if (unlikely(Rc(ctx->opcode) != 0))
1870
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1871
}
1872

    
1873
/* rldcl - rldcl. */
1874
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1875
{
1876
    uint32_t mb;
1877

    
1878
    mb = MB(ctx->opcode) | (mbn << 5);
1879
    gen_rldnm(ctx, mb, 63);
1880
}
1881
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1882
/* rldcr - rldcr. */
1883
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1884
{
1885
    uint32_t me;
1886

    
1887
    me = MB(ctx->opcode) | (men << 5);
1888
    gen_rldnm(ctx, 0, me);
1889
}
1890
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1891
/* rldimi - rldimi. */
1892
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1893
{
1894
    uint32_t sh, mb, me;
1895

    
1896
    sh = SH(ctx->opcode) | (shn << 5);
1897
    mb = MB(ctx->opcode) | (mbn << 5);
1898
    me = 63 - sh;
1899
    if (unlikely(sh == 0 && mb == 0)) {
1900
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1901
    } else {
1902
        TCGv t0, t1;
1903
        target_ulong mask;
1904

    
1905
        t0 = tcg_temp_new();
1906
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1907
        t1 = tcg_temp_new();
1908
        mask = MASK(mb, me);
1909
        tcg_gen_andi_tl(t0, t0, mask);
1910
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1911
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1912
        tcg_temp_free(t0);
1913
        tcg_temp_free(t1);
1914
    }
1915
    if (unlikely(Rc(ctx->opcode) != 0))
1916
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1917
}
1918
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1919
#endif
1920

    
1921
/***                             Integer shift                             ***/
1922
/* slw & slw. */
1923
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1924
{
1925
    TCGv t0;
1926
    int l1, l2;
1927
    l1 = gen_new_label();
1928
    l2 = gen_new_label();
1929

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

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

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

    
2075
    t0 = tcg_temp_local_new();
2076
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2077
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2078
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2079
    tcg_gen_br(l2);
2080
    gen_set_label(l1);
2081
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2082
    gen_set_label(l2);
2083
    tcg_temp_free(t0);
2084
    if (unlikely(Rc(ctx->opcode) != 0))
2085
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2086
}
2087
#endif
2088

    
2089
/***                       Floating-Point arithmetic                       ***/
2090
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2091
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2092
{                                                                             \
2093
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2094
        GEN_EXCP_NO_FP(ctx);                                                  \
2095
        return;                                                               \
2096
    }                                                                         \
2097
    gen_reset_fpstatus();                                                     \
2098
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2099
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2100
    if (isfloat) {                                                            \
2101
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2102
    }                                                                         \
2103
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2104
                     Rc(ctx->opcode) != 0);                                   \
2105
}
2106

    
2107
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2108
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2109
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2110

    
2111
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2112
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2113
{                                                                             \
2114
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2115
        GEN_EXCP_NO_FP(ctx);                                                  \
2116
        return;                                                               \
2117
    }                                                                         \
2118
    gen_reset_fpstatus();                                                     \
2119
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2120
                     cpu_fpr[rB(ctx->opcode)]);                               \
2121
    if (isfloat) {                                                            \
2122
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2123
    }                                                                         \
2124
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2125
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2126
}
2127
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2128
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2129
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2130

    
2131
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2132
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2133
{                                                                             \
2134
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2135
        GEN_EXCP_NO_FP(ctx);                                                  \
2136
        return;                                                               \
2137
    }                                                                         \
2138
    gen_reset_fpstatus();                                                     \
2139
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2140
                       cpu_fpr[rC(ctx->opcode)]);                             \
2141
    if (isfloat) {                                                            \
2142
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2143
    }                                                                         \
2144
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2145
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2146
}
2147
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2148
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2149
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2150

    
2151
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2152
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2153
{                                                                             \
2154
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2155
        GEN_EXCP_NO_FP(ctx);                                                  \
2156
        return;                                                               \
2157
    }                                                                         \
2158
    gen_reset_fpstatus();                                                     \
2159
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2160
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2161
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2162
}
2163

    
2164
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2165
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2166
{                                                                             \
2167
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2168
        GEN_EXCP_NO_FP(ctx);                                                  \
2169
        return;                                                               \
2170
    }                                                                         \
2171
    gen_reset_fpstatus();                                                     \
2172
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2173
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2174
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2175
}
2176

    
2177
/* fadd - fadds */
2178
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2179
/* fdiv - fdivs */
2180
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2181
/* fmul - fmuls */
2182
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2183

    
2184
/* fre */
2185
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2186

    
2187
/* fres */
2188
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2189

    
2190
/* frsqrte */
2191
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2192

    
2193
/* frsqrtes */
2194
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2195
{
2196
    if (unlikely(!ctx->fpu_enabled)) {
2197
        GEN_EXCP_NO_FP(ctx);
2198
        return;
2199
    }
2200
    gen_reset_fpstatus();
2201
    gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2202
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2203
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2204
}
2205

    
2206
/* fsel */
2207
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2208
/* fsub - fsubs */
2209
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2210
/* Optional: */
2211
/* fsqrt */
2212
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2213
{
2214
    if (unlikely(!ctx->fpu_enabled)) {
2215
        GEN_EXCP_NO_FP(ctx);
2216
        return;
2217
    }
2218
    gen_reset_fpstatus();
2219
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2220
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2221
}
2222

    
2223
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2224
{
2225
    if (unlikely(!ctx->fpu_enabled)) {
2226
        GEN_EXCP_NO_FP(ctx);
2227
        return;
2228
    }
2229
    gen_reset_fpstatus();
2230
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2231
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2232
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2233
}
2234

    
2235
/***                     Floating-Point multiply-and-add                   ***/
2236
/* fmadd - fmadds */
2237
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2238
/* fmsub - fmsubs */
2239
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2240
/* fnmadd - fnmadds */
2241
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2242
/* fnmsub - fnmsubs */
2243
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2244

    
2245
/***                     Floating-Point round & convert                    ***/
2246
/* fctiw */
2247
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2248
/* fctiwz */
2249
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2250
/* frsp */
2251
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2252
#if defined(TARGET_PPC64)
2253
/* fcfid */
2254
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2255
/* fctid */
2256
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2257
/* fctidz */
2258
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2259
#endif
2260

    
2261
/* frin */
2262
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2263
/* friz */
2264
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2265
/* frip */
2266
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2267
/* frim */
2268
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2269

    
2270
/***                         Floating-Point compare                        ***/
2271
/* fcmpo */
2272
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2273
{
2274
    if (unlikely(!ctx->fpu_enabled)) {
2275
        GEN_EXCP_NO_FP(ctx);
2276
        return;
2277
    }
2278
    gen_reset_fpstatus();
2279
    gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2280
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2281
    gen_helper_float_check_status();
2282
}
2283

    
2284
/* fcmpu */
2285
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2286
{
2287
    if (unlikely(!ctx->fpu_enabled)) {
2288
        GEN_EXCP_NO_FP(ctx);
2289
        return;
2290
    }
2291
    gen_reset_fpstatus();
2292
    gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2293
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2294
    gen_helper_float_check_status();
2295
}
2296

    
2297
/***                         Floating-point move                           ***/
2298
/* fabs */
2299
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2300
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2301

    
2302
/* fmr  - fmr. */
2303
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2304
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2305
{
2306
    if (unlikely(!ctx->fpu_enabled)) {
2307
        GEN_EXCP_NO_FP(ctx);
2308
        return;
2309
    }
2310
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2311
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2312
}
2313

    
2314
/* fnabs */
2315
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2316
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2317
/* fneg */
2318
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2319
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2320

    
2321
/***                  Floating-Point status & ctrl register                ***/
2322
/* mcrfs */
2323
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2324
{
2325
    int bfa;
2326

    
2327
    if (unlikely(!ctx->fpu_enabled)) {
2328
        GEN_EXCP_NO_FP(ctx);
2329
        return;
2330
    }
2331
    gen_optimize_fprf();
2332
    bfa = 4 * (7 - crfS(ctx->opcode));
2333
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2334
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2335
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2336
}
2337

    
2338
/* mffs */
2339
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2340
{
2341
    if (unlikely(!ctx->fpu_enabled)) {
2342
        GEN_EXCP_NO_FP(ctx);
2343
        return;
2344
    }
2345
    gen_optimize_fprf();
2346
    gen_reset_fpstatus();
2347
    tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2348
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2349
}
2350

    
2351
/* mtfsb0 */
2352
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2353
{
2354
    uint8_t crb;
2355

    
2356
    if (unlikely(!ctx->fpu_enabled)) {
2357
        GEN_EXCP_NO_FP(ctx);
2358
        return;
2359
    }
2360
    crb = 32 - (crbD(ctx->opcode) >> 2);
2361
    gen_optimize_fprf();
2362
    gen_reset_fpstatus();
2363
    if (likely(crb != 30 && crb != 29))
2364
        tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
2365
    if (unlikely(Rc(ctx->opcode) != 0)) {
2366
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2367
    }
2368
}
2369

    
2370
/* mtfsb1 */
2371
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2372
{
2373
    uint8_t crb;
2374

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

    
2395
/* mtfsf */
2396
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2397
{
2398
    TCGv_i32 t0;
2399

    
2400
    if (unlikely(!ctx->fpu_enabled)) {
2401
        GEN_EXCP_NO_FP(ctx);
2402
        return;
2403
    }
2404
    gen_optimize_fprf();
2405
    gen_reset_fpstatus();
2406
    t0 = tcg_const_i32(FM(ctx->opcode));
2407
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2408
    tcg_temp_free_i32(t0);
2409
    if (unlikely(Rc(ctx->opcode) != 0)) {
2410
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2411
    }
2412
    /* We can raise a differed exception */
2413
    gen_helper_float_check_status();
2414
}
2415

    
2416
/* mtfsfi */
2417
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2418
{
2419
    int bf, sh;
2420
    TCGv_i64 t0;
2421
    TCGv_i32 t1;
2422

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

    
2443
/***                           Addressing modes                            ***/
2444
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2445
static always_inline void gen_addr_imm_index (TCGv EA,
2446
                                              DisasContext *ctx,
2447
                                              target_long maskl)
2448
{
2449
    target_long simm = SIMM(ctx->opcode);
2450

    
2451
    simm &= ~maskl;
2452
    if (rA(ctx->opcode) == 0)
2453
        tcg_gen_movi_tl(EA, simm);
2454
    else if (likely(simm != 0))
2455
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2456
    else
2457
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2458
}
2459

    
2460
static always_inline void gen_addr_reg_index (TCGv EA,
2461
                                              DisasContext *ctx)
2462
{
2463
    if (rA(ctx->opcode) == 0)
2464
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2465
    else
2466
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2467
}
2468

    
2469
static always_inline void gen_addr_register (TCGv EA,
2470
                                             DisasContext *ctx)
2471
{
2472
    if (rA(ctx->opcode) == 0)
2473
        tcg_gen_movi_tl(EA, 0);
2474
    else
2475
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2476
}
2477

    
2478
#if defined(TARGET_PPC64)
2479
#define _GEN_MEM_FUNCS(name, mode)                                            \
2480
    &gen_op_##name##_##mode,                                                  \
2481
    &gen_op_##name##_le_##mode,                                               \
2482
    &gen_op_##name##_64_##mode,                                               \
2483
    &gen_op_##name##_le_64_##mode
2484
#else
2485
#define _GEN_MEM_FUNCS(name, mode)                                            \
2486
    &gen_op_##name##_##mode,                                                  \
2487
    &gen_op_##name##_le_##mode
2488
#endif
2489
#if defined(CONFIG_USER_ONLY)
2490
#if defined(TARGET_PPC64)
2491
#define NB_MEM_FUNCS 4
2492
#else
2493
#define NB_MEM_FUNCS 2
2494
#endif
2495
#define GEN_MEM_FUNCS(name)                                                   \
2496
    _GEN_MEM_FUNCS(name, raw)
2497
#else
2498
#if defined(TARGET_PPC64)
2499
#define NB_MEM_FUNCS 12
2500
#else
2501
#define NB_MEM_FUNCS 6
2502
#endif
2503
#define GEN_MEM_FUNCS(name)                                                   \
2504
    _GEN_MEM_FUNCS(name, user),                                               \
2505
    _GEN_MEM_FUNCS(name, kernel),                                             \
2506
    _GEN_MEM_FUNCS(name, hypv)
2507
#endif
2508

    
2509
/***                             Integer load                              ***/
2510
#if defined(TARGET_PPC64)
2511
#define GEN_QEMU_LD_PPC64(width)                                                 \
2512
static always_inline void gen_qemu_ld##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2513
{                                                                                \
2514
    if (likely(flags & 2))                                                       \
2515
        tcg_gen_qemu_ld##width(t0, t1, flags >> 2);                              \
2516
    else {                                                                       \
2517
        TCGv addr = tcg_temp_new();                                   \
2518
        tcg_gen_ext32u_tl(addr, t1);                                             \
2519
        tcg_gen_qemu_ld##width(t0, addr, flags >> 2);                            \
2520
        tcg_temp_free(addr);                                                     \
2521
    }                                                                            \
2522
}
2523
GEN_QEMU_LD_PPC64(8u)
2524
GEN_QEMU_LD_PPC64(8s)
2525
GEN_QEMU_LD_PPC64(16u)
2526
GEN_QEMU_LD_PPC64(16s)
2527
GEN_QEMU_LD_PPC64(32u)
2528
GEN_QEMU_LD_PPC64(32s)
2529
GEN_QEMU_LD_PPC64(64)
2530

    
2531
#define GEN_QEMU_ST_PPC64(width)                                                 \
2532
static always_inline void gen_qemu_st##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2533
{                                                                                \
2534
    if (likely(flags & 2))                                                       \
2535
        tcg_gen_qemu_st##width(t0, t1, flags >> 2);                              \
2536
    else {                                                                       \
2537
        TCGv addr = tcg_temp_new();                                   \
2538
        tcg_gen_ext32u_tl(addr, t1);                                             \
2539
        tcg_gen_qemu_st##width(t0, addr, flags >> 2);                            \
2540
        tcg_temp_free(addr);                                                     \
2541
    }                                                                            \
2542
}
2543
GEN_QEMU_ST_PPC64(8)
2544
GEN_QEMU_ST_PPC64(16)
2545
GEN_QEMU_ST_PPC64(32)
2546
GEN_QEMU_ST_PPC64(64)
2547

    
2548
static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2549
{
2550
    gen_qemu_ld8u_ppc64(arg0, arg1, flags);
2551
}
2552

    
2553
static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2554
{
2555
    gen_qemu_ld8s_ppc64(arg0, arg1, flags);
2556
}
2557

    
2558
static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2559
{
2560
    if (unlikely(flags & 1)) {
2561
        TCGv_i32 t0;
2562
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2563
        t0 = tcg_temp_new_i32();
2564
        tcg_gen_trunc_tl_i32(t0, arg0);
2565
        tcg_gen_bswap16_i32(t0, t0);
2566
        tcg_gen_extu_i32_tl(arg0, t0);
2567
        tcg_temp_free_i32(t0);
2568
    } else
2569
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2570
}
2571

    
2572
static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2573
{
2574
    if (unlikely(flags & 1)) {
2575
        TCGv_i32 t0;
2576
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2577
        t0 = tcg_temp_new_i32();
2578
        tcg_gen_trunc_tl_i32(t0, arg0);
2579
        tcg_gen_bswap16_i32(t0, t0);
2580
        tcg_gen_extu_i32_tl(arg0, t0);
2581
        tcg_gen_ext16s_tl(arg0, arg0);
2582
        tcg_temp_free_i32(t0);
2583
    } else
2584
        gen_qemu_ld16s_ppc64(arg0, arg1, flags);
2585
}
2586

    
2587
static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2588
{
2589
    if (unlikely(flags & 1)) {
2590
        TCGv_i32 t0;
2591
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2592
        t0 = tcg_temp_new_i32();
2593
        tcg_gen_trunc_tl_i32(t0, arg0);
2594
        tcg_gen_bswap_i32(t0, t0);
2595
        tcg_gen_extu_i32_tl(arg0, t0);
2596
        tcg_temp_free_i32(t0);
2597
    } else
2598
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2599
}
2600

    
2601
static always_inline void gen_qemu_ld32s(TCGv arg0, TCGv arg1, int flags)
2602
{
2603
    if (unlikely(flags & 1)) {
2604
        TCGv_i32 t0;
2605
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2606
        t0 = tcg_temp_new_i32();
2607
        tcg_gen_trunc_tl_i32(t0, arg0);
2608
        tcg_gen_bswap_i32(t0, t0);
2609
        tcg_gen_ext_i32_tl(arg0, t0);
2610
        tcg_temp_free_i32(t0);
2611
    } else
2612
        gen_qemu_ld32s_ppc64(arg0, arg1, flags);
2613
}
2614

    
2615
static always_inline void gen_qemu_ld64(TCGv arg0, TCGv arg1, int flags)
2616
{
2617
    gen_qemu_ld64_ppc64(arg0, arg1, flags);
2618
    if (unlikely(flags & 1))
2619
        tcg_gen_bswap_i64(arg0, arg0);
2620
}
2621

    
2622
static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2623
{
2624
    gen_qemu_st8_ppc64(arg0, arg1, flags);
2625
}
2626

    
2627
static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2628
{
2629
    if (unlikely(flags & 1)) {
2630
        TCGv_i32 t0;
2631
        TCGv_i64 t1;
2632
        t0 = tcg_temp_new_i32();
2633
        tcg_gen_trunc_tl_i32(t0, arg0);
2634
        tcg_gen_ext16u_i32(t0, t0);
2635
        tcg_gen_bswap16_i32(t0, t0);
2636
        t1 = tcg_temp_new_i64();
2637
        tcg_gen_extu_i32_tl(t1, t0);
2638
        tcg_temp_free_i32(t0);
2639
        gen_qemu_st16_ppc64(t1, arg1, flags);
2640
        tcg_temp_free_i64(t1);
2641
    } else
2642
        gen_qemu_st16_ppc64(arg0, arg1, flags);
2643
}
2644

    
2645
static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2646
{
2647
    if (unlikely(flags & 1)) {
2648
        TCGv_i32 t0;
2649
        TCGv_i64 t1;
2650
        t0 = tcg_temp_new_i32();
2651
        tcg_gen_trunc_tl_i32(t0, arg0);
2652
        tcg_gen_bswap_i32(t0, t0);
2653
        t1 = tcg_temp_new_i64();
2654
        tcg_gen_extu_i32_tl(t1, t0);
2655
        tcg_temp_free_i32(t0);
2656
        gen_qemu_st32_ppc64(t1, arg1, flags);
2657
        tcg_temp_free_i64(t1);
2658
    } else
2659
        gen_qemu_st32_ppc64(arg0, arg1, flags);
2660
}
2661

    
2662
static always_inline void gen_qemu_st64(TCGv arg0, TCGv arg1, int flags)
2663
{
2664
    if (unlikely(flags & 1)) {
2665
        TCGv_i64 t0 = tcg_temp_new_i64();
2666
        tcg_gen_bswap_i64(t0, arg0);
2667
        gen_qemu_st64_ppc64(t0, arg1, flags);
2668
        tcg_temp_free_i64(t0);
2669
    } else
2670
        gen_qemu_st64_ppc64(arg0, arg1, flags);
2671
}
2672

    
2673

    
2674
#else /* defined(TARGET_PPC64) */
2675
#define GEN_QEMU_LD_PPC32(width)                                                      \
2676
static always_inline void gen_qemu_ld##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2677
{                                                                                     \
2678
    tcg_gen_qemu_ld##width(arg0, arg1, flags >> 1);                                   \
2679
}
2680
GEN_QEMU_LD_PPC32(8u)
2681
GEN_QEMU_LD_PPC32(8s)
2682
GEN_QEMU_LD_PPC32(16u)
2683
GEN_QEMU_LD_PPC32(16s)
2684
GEN_QEMU_LD_PPC32(32u)
2685
GEN_QEMU_LD_PPC32(32s)
2686
static always_inline void gen_qemu_ld64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2687
{
2688
    tcg_gen_qemu_ld64(arg0, arg1, flags >> 1);
2689
}
2690

    
2691
#define GEN_QEMU_ST_PPC32(width)                                                      \
2692
static always_inline void gen_qemu_st##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2693
{                                                                                     \
2694
    tcg_gen_qemu_st##width(arg0, arg1, flags >> 1);                                   \
2695
}
2696
GEN_QEMU_ST_PPC32(8)
2697
GEN_QEMU_ST_PPC32(16)
2698
GEN_QEMU_ST_PPC32(32)
2699
static always_inline void gen_qemu_st64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2700
{
2701
    tcg_gen_qemu_st64(arg0, arg1, flags >> 1);
2702
}
2703

    
2704
static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2705
{
2706
    gen_qemu_ld8u_ppc32(arg0, arg1, flags >> 1);
2707
}
2708

    
2709
static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2710
{
2711
    gen_qemu_ld8s_ppc32(arg0, arg1, flags >> 1);
2712
}
2713

    
2714
static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2715
{
2716
    gen_qemu_ld16u_ppc32(arg0, arg1, flags >> 1);
2717
    if (unlikely(flags & 1))
2718
        tcg_gen_bswap16_i32(arg0, arg0);
2719
}
2720

    
2721
static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2722
{
2723
    if (unlikely(flags & 1)) {
2724
        gen_qemu_ld16u_ppc32(arg0, arg1, flags);
2725
        tcg_gen_bswap16_i32(arg0, arg0);
2726
        tcg_gen_ext16s_i32(arg0, arg0);
2727
    } else
2728
        gen_qemu_ld16s_ppc32(arg0, arg1, flags);
2729
}
2730

    
2731
static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2732
{
2733
    gen_qemu_ld32u_ppc32(arg0, arg1, flags);
2734
    if (unlikely(flags & 1))
2735
        tcg_gen_bswap_i32(arg0, arg0);
2736
}
2737

    
2738
static always_inline void gen_qemu_ld64(TCGv_i64 arg0, TCGv arg1, int flags)
2739
{
2740
    gen_qemu_ld64_ppc32(arg0, arg1, flags);
2741
    if (unlikely(flags & 1))
2742
        tcg_gen_bswap_i64(arg0, arg0);
2743
}
2744

    
2745
static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2746
{
2747
    gen_qemu_st8_ppc32(arg0, arg1, flags);
2748
}
2749

    
2750
static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2751
{
2752
    if (unlikely(flags & 1)) {
2753
        TCGv_i32 temp = tcg_temp_new_i32();
2754
        tcg_gen_ext16u_i32(temp, arg0);
2755
        tcg_gen_bswap16_i32(temp, temp);
2756
        gen_qemu_st16_ppc32(temp, arg1, flags);
2757
        tcg_temp_free_i32(temp);
2758
    } else
2759
        gen_qemu_st16_ppc32(arg0, arg1, flags);
2760
}
2761

    
2762
static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2763
{
2764
    if (unlikely(flags & 1)) {
2765
        TCGv_i32 temp = tcg_temp_new_i32();
2766
        tcg_gen_bswap_i32(temp, arg0);
2767
        gen_qemu_st32_ppc32(temp, arg1, flags);
2768
        tcg_temp_free_i32(temp);
2769
    } else
2770
        gen_qemu_st32_ppc32(arg0, arg1, flags);
2771
}
2772

    
2773
static always_inline void gen_qemu_st64(TCGv_i64 arg0, TCGv arg1, int flags)
2774
{
2775
    if (unlikely(flags & 1)) {
2776
        TCGv_i64 temp = tcg_temp_new_i64();
2777
        tcg_gen_bswap_i64(temp, arg0);
2778
        gen_qemu_st64_ppc32(temp, arg1, flags);
2779
        tcg_temp_free_i64(temp);
2780
    } else
2781
        gen_qemu_st64_ppc32(arg0, arg1, flags);
2782
}
2783
#endif
2784

    
2785
#define GEN_LD(name, ldop, opc, type)                                         \
2786
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2787
{                                                                             \
2788
    TCGv EA = tcg_temp_new();                                                 \
2789
    gen_set_access_type(ACCESS_INT);                                          \
2790
    gen_addr_imm_index(EA, ctx, 0);                                           \
2791
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2792
    tcg_temp_free(EA);                                                        \
2793
}
2794

    
2795
#define GEN_LDU(name, ldop, opc, type)                                        \
2796
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2797
{                                                                             \
2798
    TCGv EA;                                                                  \
2799
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2800
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2801
        GEN_EXCP_INVAL(ctx);                                                  \
2802
        return;                                                               \
2803
    }                                                                         \
2804
    EA = tcg_temp_new();                                                      \
2805
    gen_set_access_type(ACCESS_INT);                                          \
2806
    if (type == PPC_64B)                                                      \
2807
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
2808
    else                                                                      \
2809
        gen_addr_imm_index(EA, ctx, 0);                                       \
2810
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2811
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2812
    tcg_temp_free(EA);                                                        \
2813
}
2814

    
2815
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2816
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2817
{                                                                             \
2818
    TCGv EA;                                                                  \
2819
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2820
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2821
        GEN_EXCP_INVAL(ctx);                                                  \
2822
        return;                                                               \
2823
    }                                                                         \
2824
    EA = tcg_temp_new();                                                      \
2825
    gen_set_access_type(ACCESS_INT);                                          \
2826
    gen_addr_reg_index(EA, ctx);                                              \
2827
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2828
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2829
    tcg_temp_free(EA);                                                        \
2830
}
2831

    
2832
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2833
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2834
{                                                                             \
2835
    TCGv EA = tcg_temp_new();                                                 \
2836
    gen_set_access_type(ACCESS_INT);                                          \
2837
    gen_addr_reg_index(EA, ctx);                                              \
2838
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2839
    tcg_temp_free(EA);                                                        \
2840
}
2841

    
2842
#define GEN_LDS(name, ldop, op, type)                                         \
2843
GEN_LD(name, ldop, op | 0x20, type);                                          \
2844
GEN_LDU(name, ldop, op | 0x21, type);                                         \
2845
GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2846
GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2847

    
2848
/* lbz lbzu lbzux lbzx */
2849
GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2850
/* lha lhau lhaux lhax */
2851
GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2852
/* lhz lhzu lhzux lhzx */
2853
GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2854
/* lwz lwzu lwzux lwzx */
2855
GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2856
#if defined(TARGET_PPC64)
2857
/* lwaux */
2858
GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2859
/* lwax */
2860
GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2861
/* ldux */
2862
GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2863
/* ldx */
2864
GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2865
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2866
{
2867
    TCGv EA;
2868
    if (Rc(ctx->opcode)) {
2869
        if (unlikely(rA(ctx->opcode) == 0 ||
2870
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2871
            GEN_EXCP_INVAL(ctx);
2872
            return;
2873
        }
2874
    }
2875
    EA = tcg_temp_new();
2876
    gen_set_access_type(ACCESS_INT);
2877
    gen_addr_imm_index(EA, ctx, 0x03);
2878
    if (ctx->opcode & 0x02) {
2879
        /* lwa (lwau is undefined) */
2880
        gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2881
    } else {
2882
        /* ld - ldu */
2883
        gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2884
    }
2885
    if (Rc(ctx->opcode))
2886
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2887
    tcg_temp_free(EA);
2888
}
2889
/* lq */
2890
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2891
{
2892
#if defined(CONFIG_USER_ONLY)
2893
    GEN_EXCP_PRIVOPC(ctx);
2894
#else
2895
    int ra, rd;
2896
    TCGv EA;
2897

    
2898
    /* Restore CPU state */
2899
    if (unlikely(ctx->supervisor == 0)) {
2900
        GEN_EXCP_PRIVOPC(ctx);
2901
        return;
2902
    }
2903
    ra = rA(ctx->opcode);
2904
    rd = rD(ctx->opcode);
2905
    if (unlikely((rd & 1) || rd == ra)) {
2906
        GEN_EXCP_INVAL(ctx);
2907
        return;
2908
    }
2909
    if (unlikely(ctx->mem_idx & 1)) {
2910
        /* Little-endian mode is not handled */
2911
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2912
        return;
2913
    }
2914
    EA = tcg_temp_new();
2915
    gen_set_access_type(ACCESS_INT);
2916
    gen_addr_imm_index(EA, ctx, 0x0F);
2917
    gen_qemu_ld64(cpu_gpr[rd], EA, ctx->mem_idx);
2918
    tcg_gen_addi_tl(EA, EA, 8);
2919
    gen_qemu_ld64(cpu_gpr[rd+1], EA, ctx->mem_idx);
2920
    tcg_temp_free(EA);
2921
#endif
2922
}
2923
#endif
2924

    
2925
/***                              Integer store                            ***/
2926
#define GEN_ST(name, stop, opc, type)                                         \
2927
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2928
{                                                                             \
2929
    TCGv EA = tcg_temp_new();                                                 \
2930
    gen_set_access_type(ACCESS_INT);                                          \
2931
    gen_addr_imm_index(EA, ctx, 0);                                           \
2932
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2933
    tcg_temp_free(EA);                                                        \
2934
}
2935

    
2936
#define GEN_STU(name, stop, opc, type)                                        \
2937
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2938
{                                                                             \
2939
    TCGv EA;                                                                  \
2940
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2941
        GEN_EXCP_INVAL(ctx);                                                  \
2942
        return;                                                               \
2943
    }                                                                         \
2944
    EA = tcg_temp_new();                                                      \
2945
    gen_set_access_type(ACCESS_INT);                                          \
2946
    if (type == PPC_64B)                                                      \
2947
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
2948
    else                                                                      \
2949
        gen_addr_imm_index(EA, ctx, 0);                                       \
2950
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2951
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2952
    tcg_temp_free(EA);                                                        \
2953
}
2954

    
2955
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
2956
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2957
{                                                                             \
2958
    TCGv EA;                                                                  \
2959
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2960
        GEN_EXCP_INVAL(ctx);                                                  \
2961
        return;                                                               \
2962
    }                                                                         \
2963
    EA = tcg_temp_new();                                                      \
2964
    gen_set_access_type(ACCESS_INT);                                          \
2965
    gen_addr_reg_index(EA, ctx);                                              \
2966
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2967
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2968
    tcg_temp_free(EA);                                                        \
2969
}
2970

    
2971
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
2972
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2973
{                                                                             \
2974
    TCGv EA = tcg_temp_new();                                                 \
2975
    gen_set_access_type(ACCESS_INT);                                          \
2976
    gen_addr_reg_index(EA, ctx);                                              \
2977
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2978
    tcg_temp_free(EA);                                                        \
2979
}
2980

    
2981
#define GEN_STS(name, stop, op, type)                                         \
2982
GEN_ST(name, stop, op | 0x20, type);                                          \
2983
GEN_STU(name, stop, op | 0x21, type);                                         \
2984
GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2985
GEN_STX(name, stop, 0x17, op | 0x00, type)
2986

    
2987
/* stb stbu stbux stbx */
2988
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2989
/* sth sthu sthux sthx */
2990
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2991
/* stw stwu stwux stwx */
2992
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2993
#if defined(TARGET_PPC64)
2994
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2995
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2996
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2997
{
2998
    int rs;
2999
    TCGv EA;
3000

    
3001
    rs = rS(ctx->opcode);
3002
    if ((ctx->opcode & 0x3) == 0x2) {
3003
#if defined(CONFIG_USER_ONLY)
3004
        GEN_EXCP_PRIVOPC(ctx);
3005
#else
3006
        /* stq */
3007
        if (unlikely(ctx->supervisor == 0)) {
3008
            GEN_EXCP_PRIVOPC(ctx);
3009
            return;
3010
        }
3011
        if (unlikely(rs & 1)) {
3012
            GEN_EXCP_INVAL(ctx);
3013
            return;
3014
        }
3015
        if (unlikely(ctx->mem_idx & 1)) {
3016
            /* Little-endian mode is not handled */
3017
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3018
            return;
3019
        }
3020
        EA = tcg_temp_new();
3021
        gen_set_access_type(ACCESS_INT);
3022
        gen_addr_imm_index(EA, ctx, 0x03);
3023
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3024
        tcg_gen_addi_tl(EA, EA, 8);
3025
        gen_qemu_st64(cpu_gpr[rs+1], EA, ctx->mem_idx);
3026
        tcg_temp_free(EA);
3027
#endif
3028
    } else {
3029
        /* std / stdu */
3030
        if (Rc(ctx->opcode)) {
3031
            if (unlikely(rA(ctx->opcode) == 0)) {
3032
                GEN_EXCP_INVAL(ctx);
3033
                return;
3034
            }
3035
        }
3036
        EA = tcg_temp_new();
3037
        gen_set_access_type(ACCESS_INT);
3038
        gen_addr_imm_index(EA, ctx, 0x03);
3039
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3040
        if (Rc(ctx->opcode))
3041
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3042
        tcg_temp_free(EA);
3043
    }
3044
}
3045
#endif
3046
/***                Integer load and store with byte reverse               ***/
3047
/* lhbrx */
3048
void always_inline gen_qemu_ld16ur(TCGv t0, TCGv t1, int flags)
3049
{
3050
    TCGv_i32 temp = tcg_temp_new_i32();
3051
    gen_qemu_ld16u(t0, t1, flags);
3052
    tcg_gen_trunc_tl_i32(temp, t0);
3053
    tcg_gen_bswap16_i32(temp, temp);
3054
    tcg_gen_extu_i32_tl(t0, temp);
3055
    tcg_temp_free_i32(temp);
3056
}
3057
GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3058

    
3059
/* lwbrx */
3060
void always_inline gen_qemu_ld32ur(TCGv t0, TCGv t1, int flags)
3061
{
3062
    TCGv_i32 temp = tcg_temp_new_i32();
3063
    gen_qemu_ld32u(t0, t1, flags);
3064
    tcg_gen_trunc_tl_i32(temp, t0);
3065
    tcg_gen_bswap_i32(temp, temp);
3066
    tcg_gen_extu_i32_tl(t0, temp);
3067
    tcg_temp_free_i32(temp);
3068
}
3069
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3070

    
3071
/* sthbrx */
3072
void always_inline gen_qemu_st16r(TCGv t0, TCGv t1, int flags)
3073
{
3074
    TCGv_i32 temp = tcg_temp_new_i32();
3075
    TCGv t2 = tcg_temp_new();
3076
    tcg_gen_trunc_tl_i32(temp, t0);
3077
    tcg_gen_ext16u_i32(temp, temp);
3078
    tcg_gen_bswap16_i32(temp, temp);
3079
    tcg_gen_extu_i32_tl(t2, temp);
3080
    tcg_temp_free_i32(temp);
3081
    gen_qemu_st16(t2, t1, flags);
3082
    tcg_temp_free(t2);
3083
}
3084
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3085

    
3086
/* stwbrx */
3087
void always_inline gen_qemu_st32r(TCGv t0, TCGv t1, int flags)
3088
{
3089
    TCGv_i32 temp = tcg_temp_new_i32();
3090
    TCGv t2 = tcg_temp_new();
3091
    tcg_gen_trunc_tl_i32(temp, t0);
3092
    tcg_gen_bswap_i32(temp, temp);
3093
    tcg_gen_extu_i32_tl(t2, temp);
3094
    tcg_temp_free_i32(temp);
3095
    gen_qemu_st32(t2, t1, flags);
3096
    tcg_temp_free(t2);
3097
}
3098
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3099

    
3100
/***                    Integer load and store multiple                    ***/
3101
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
3102
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
3103
    GEN_MEM_FUNCS(lmw),
3104
};
3105
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
3106
    GEN_MEM_FUNCS(stmw),
3107
};
3108

    
3109
/* lmw */
3110
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3111
{
3112
    /* NIP cannot be restored if the memory exception comes from an helper */
3113
    gen_update_nip(ctx, ctx->nip - 4);
3114
    gen_addr_imm_index(cpu_T[0], ctx, 0);
3115
    op_ldstm(lmw, rD(ctx->opcode));
3116
}
3117

    
3118
/* stmw */
3119
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3120
{
3121
    /* NIP cannot be restored if the memory exception comes from an helper */
3122
    gen_update_nip(ctx, ctx->nip - 4);
3123
    gen_addr_imm_index(cpu_T[0], ctx, 0);
3124
    op_ldstm(stmw, rS(ctx->opcode));
3125
}
3126

    
3127
/***                    Integer load and store strings                     ***/
3128
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
3129
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
3130
/* string load & stores are by definition endian-safe */
3131
#define gen_op_lswi_le_raw       gen_op_lswi_raw
3132
#define gen_op_lswi_le_user      gen_op_lswi_user
3133
#define gen_op_lswi_le_kernel    gen_op_lswi_kernel
3134
#define gen_op_lswi_le_hypv      gen_op_lswi_hypv
3135
#define gen_op_lswi_le_64_raw    gen_op_lswi_raw
3136
#define gen_op_lswi_le_64_user   gen_op_lswi_user
3137
#define gen_op_lswi_le_64_kernel gen_op_lswi_kernel
3138
#define gen_op_lswi_le_64_hypv   gen_op_lswi_hypv
3139
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
3140
    GEN_MEM_FUNCS(lswi),
3141
};
3142
#define gen_op_lswx_le_raw       gen_op_lswx_raw
3143
#define gen_op_lswx_le_user      gen_op_lswx_user
3144
#define gen_op_lswx_le_kernel    gen_op_lswx_kernel
3145
#define gen_op_lswx_le_hypv      gen_op_lswx_hypv
3146
#define gen_op_lswx_le_64_raw    gen_op_lswx_raw
3147
#define gen_op_lswx_le_64_user   gen_op_lswx_user
3148
#define gen_op_lswx_le_64_kernel gen_op_lswx_kernel
3149
#define gen_op_lswx_le_64_hypv   gen_op_lswx_hypv
3150
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
3151
    GEN_MEM_FUNCS(lswx),
3152
};
3153
#define gen_op_stsw_le_raw       gen_op_stsw_raw
3154
#define gen_op_stsw_le_user      gen_op_stsw_user
3155
#define gen_op_stsw_le_kernel    gen_op_stsw_kernel
3156
#define gen_op_stsw_le_hypv      gen_op_stsw_hypv
3157
#define gen_op_stsw_le_64_raw    gen_op_stsw_raw
3158
#define gen_op_stsw_le_64_user   gen_op_stsw_user
3159
#define gen_op_stsw_le_64_kernel gen_op_stsw_kernel
3160
#define gen_op_stsw_le_64_hypv   gen_op_stsw_hypv
3161
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
3162
    GEN_MEM_FUNCS(stsw),
3163
};
3164

    
3165
/* lswi */
3166
/* PowerPC32 specification says we must generate an exception if
3167
 * rA is in the range of registers to be loaded.
3168
 * In an other hand, IBM says this is valid, but rA won't be loaded.
3169
 * For now, I'll follow the spec...
3170
 */
3171
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3172
{
3173
    int nb = NB(ctx->opcode);
3174
    int start = rD(ctx->opcode);
3175
    int ra = rA(ctx->opcode);
3176
    int nr;
3177

    
3178
    if (nb == 0)
3179
        nb = 32;
3180
    nr = nb / 4;
3181
    if (unlikely(((start + nr) > 32  &&
3182
                  start <= ra && (start + nr - 32) > ra) ||
3183
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3184
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3185
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
3186
        return;
3187
    }
3188
    /* NIP cannot be restored if the memory exception comes from an helper */
3189
    gen_update_nip(ctx, ctx->nip - 4);
3190
    gen_addr_register(cpu_T[0], ctx);
3191
    tcg_gen_movi_tl(cpu_T[1], nb);
3192
    op_ldsts(lswi, start);
3193
}
3194

    
3195
/* lswx */
3196
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3197
{
3198
    int ra = rA(ctx->opcode);
3199
    int rb = rB(ctx->opcode);
3200

    
3201
    /* NIP cannot be restored if the memory exception comes from an helper */
3202
    gen_update_nip(ctx, ctx->nip - 4);
3203
    gen_addr_reg_index(cpu_T[0], ctx);
3204
    if (ra == 0) {
3205
        ra = rb;
3206
    }
3207
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3208
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
3209
}
3210

    
3211
/* stswi */
3212
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3213
{
3214
    int nb = NB(ctx->opcode);
3215

    
3216
    /* NIP cannot be restored if the memory exception comes from an helper */
3217
    gen_update_nip(ctx, ctx->nip - 4);
3218
    gen_addr_register(cpu_T[0], ctx);
3219
    if (nb == 0)
3220
        nb = 32;
3221
    tcg_gen_movi_tl(cpu_T[1], nb);
3222
    op_ldsts(stsw, rS(ctx->opcode));
3223
}
3224

    
3225
/* stswx */
3226
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3227
{
3228
    /* NIP cannot be restored if the memory exception comes from an helper */
3229
    gen_update_nip(ctx, ctx->nip - 4);
3230
    gen_addr_reg_index(cpu_T[0], ctx);
3231
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3232
    op_ldsts(stsw, rS(ctx->opcode));
3233
}
3234

    
3235
/***                        Memory synchronisation                         ***/
3236
/* eieio */
3237
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3238
{
3239
}
3240

    
3241
/* isync */
3242
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3243
{
3244
    GEN_STOP(ctx);
3245
}
3246

    
3247
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
3248
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
3249
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
3250
    GEN_MEM_FUNCS(lwarx),
3251
};
3252
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
3253
    GEN_MEM_FUNCS(stwcx),
3254
};
3255

    
3256
/* lwarx */
3257
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3258
{
3259
    /* NIP cannot be restored if the memory exception comes from an helper */
3260
    gen_update_nip(ctx, ctx->nip - 4);
3261
    gen_set_access_type(ACCESS_RES);
3262
    gen_addr_reg_index(cpu_T[0], ctx);
3263
    op_lwarx();
3264
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3265
}
3266

    
3267
/* stwcx. */
3268
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3269
{
3270
    /* NIP cannot be restored if the memory exception comes from an helper */
3271
    gen_update_nip(ctx, ctx->nip - 4);
3272
    gen_set_access_type(ACCESS_RES);
3273
    gen_addr_reg_index(cpu_T[0], ctx);
3274
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3275
    op_stwcx();
3276
}
3277

    
3278
#if defined(TARGET_PPC64)
3279
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
3280
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
3281
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
3282
    GEN_MEM_FUNCS(ldarx),
3283
};
3284
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
3285
    GEN_MEM_FUNCS(stdcx),
3286
};
3287

    
3288
/* ldarx */
3289
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3290
{
3291
    /* NIP cannot be restored if the memory exception comes from an helper */
3292
    gen_update_nip(ctx, ctx->nip - 4);
3293
    gen_set_access_type(ACCESS_RES);
3294
    gen_addr_reg_index(cpu_T[0], ctx);
3295
    op_ldarx();
3296
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3297
}
3298

    
3299
/* stdcx. */
3300
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3301
{
3302
    /* NIP cannot be restored if the memory exception comes from an helper */
3303
    gen_update_nip(ctx, ctx->nip - 4);
3304
    gen_set_access_type(ACCESS_RES);
3305
    gen_addr_reg_index(cpu_T[0], ctx);
3306
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3307
    op_stdcx();
3308
}
3309
#endif /* defined(TARGET_PPC64) */
3310

    
3311
/* sync */
3312
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3313
{
3314
}
3315

    
3316
/* wait */
3317
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3318
{
3319
    /* Stop translation, as the CPU is supposed to sleep from now */
3320
    gen_op_wait();
3321
    GEN_EXCP(ctx, EXCP_HLT, 1);
3322
}
3323

    
3324
/***                         Floating-point load                           ***/
3325
#define GEN_LDF(name, ldop, opc, type)                                        \
3326
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3327
{                                                                             \
3328
    TCGv EA;                                                                  \
3329
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3330
        GEN_EXCP_NO_FP(ctx);                                                  \
3331
        return;                                                               \
3332
    }                                                                         \
3333
    gen_set_access_type(ACCESS_FLOAT);                                        \
3334
    EA = tcg_temp_new();                                                      \
3335
    gen_addr_imm_index(EA, ctx, 0);                                           \
3336
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3337
    tcg_temp_free(EA);                                                        \
3338
}
3339

    
3340
#define GEN_LDUF(name, ldop, opc, type)                                       \
3341
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3342
{                                                                             \
3343
    TCGv EA;                                                                  \
3344
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3345
        GEN_EXCP_NO_FP(ctx);                                                  \
3346
        return;                                                               \
3347
    }                                                                         \
3348
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3349
        GEN_EXCP_INVAL(ctx);                                                  \
3350
        return;                                                               \
3351
    }                                                                         \
3352
    gen_set_access_type(ACCESS_FLOAT);                                        \
3353
    EA = tcg_temp_new();                                                      \
3354
    gen_addr_imm_index(EA, ctx, 0);                                           \
3355
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3356
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3357
    tcg_temp_free(EA);                                                        \
3358
}
3359

    
3360
#define GEN_LDUXF(name, ldop, opc, type)                                      \
3361
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3362
{                                                                             \
3363
    TCGv EA;                                                                  \
3364
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3365
        GEN_EXCP_NO_FP(ctx);                                                  \
3366
        return;                                                               \
3367
    }                                                                         \
3368
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3369
        GEN_EXCP_INVAL(ctx);                                                  \
3370
        return;                                                               \
3371
    }                                                                         \
3372
    gen_set_access_type(ACCESS_FLOAT);                                        \
3373
    EA = tcg_temp_new();                                                      \
3374
    gen_addr_reg_index(EA, ctx);                                              \
3375
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3376
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3377
    tcg_temp_free(EA);                                                        \
3378
}
3379

    
3380
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3381
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3382
{                                                                             \
3383
    TCGv EA;                                                                  \
3384
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3385
        GEN_EXCP_NO_FP(ctx);                                                  \
3386
        return;                                                               \
3387
    }                                                                         \
3388
    gen_set_access_type(ACCESS_FLOAT);                                        \
3389
    EA = tcg_temp_new();                                                      \
3390
    gen_addr_reg_index(EA, ctx);                                              \
3391
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3392
    tcg_temp_free(EA);                                                        \
3393
}
3394

    
3395
#define GEN_LDFS(name, ldop, op, type)                                        \
3396
GEN_LDF(name, ldop, op | 0x20, type);                                         \
3397
GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3398
GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3399
GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3400

    
3401
static always_inline void gen_qemu_ld32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3402
{
3403
    TCGv t0 = tcg_temp_new();
3404
    TCGv_i32 t1 = tcg_temp_new_i32();
3405
    gen_qemu_ld32u(t0, arg2, flags);
3406
    tcg_gen_trunc_tl_i32(t1, t0);
3407
    tcg_temp_free(t0);
3408
    gen_helper_float32_to_float64(arg1, t1);
3409
    tcg_temp_free_i32(t1);
3410
}
3411

    
3412
 /* lfd lfdu lfdux lfdx */
3413
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3414
 /* lfs lfsu lfsux lfsx */
3415
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3416

    
3417
/***                         Floating-point store                          ***/
3418
#define GEN_STF(name, stop, opc, type)                                        \
3419
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3420
{                                                                             \
3421
    TCGv EA;                                                                  \
3422
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3423
        GEN_EXCP_NO_FP(ctx);                                                  \
3424
        return;                                                               \
3425
    }                                                                         \
3426
    gen_set_access_type(ACCESS_FLOAT);                                        \
3427
    EA = tcg_temp_new();                                                      \
3428
    gen_addr_imm_index(EA, ctx, 0);                                           \
3429
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3430
    tcg_temp_free(EA);                                                        \
3431
}
3432

    
3433
#define GEN_STUF(name, stop, opc, type)                                       \
3434
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3435
{                                                                             \
3436
    TCGv EA;                                                                  \
3437
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3438
        GEN_EXCP_NO_FP(ctx);                                                  \
3439
        return;                                                               \
3440
    }                                                                         \
3441
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3442
        GEN_EXCP_INVAL(ctx);                                                  \
3443
        return;                                                               \
3444
    }                                                                         \
3445
    gen_set_access_type(ACCESS_FLOAT);                                        \
3446
    EA = tcg_temp_new();                                                      \
3447
    gen_addr_imm_index(EA, ctx, 0);                                           \
3448
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3449
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3450
    tcg_temp_free(EA);                                                        \
3451
}
3452

    
3453
#define GEN_STUXF(name, stop, opc, type)                                      \
3454
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3455
{                                                                             \
3456
    TCGv EA;                                                                  \
3457
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3458
        GEN_EXCP_NO_FP(ctx);                                                  \
3459
        return;                                                               \
3460
    }                                                                         \
3461
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3462
        GEN_EXCP_INVAL(ctx);                                                  \
3463
        return;                                                               \
3464
    }                                                                         \
3465
    gen_set_access_type(ACCESS_FLOAT);                                        \
3466
    EA = tcg_temp_new();                                                      \
3467
    gen_addr_reg_index(EA, ctx);                                              \
3468
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3469
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3470
    tcg_temp_free(EA);                                                        \
3471
}
3472

    
3473
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
3474
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3475
{                                                                             \
3476
    TCGv EA;                                                                  \
3477
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3478
        GEN_EXCP_NO_FP(ctx);                                                  \
3479
        return;                                                               \
3480
    }                                                                         \
3481
    gen_set_access_type(ACCESS_FLOAT);                                        \
3482
    EA = tcg_temp_new();                                                      \
3483
    gen_addr_reg_index(EA, ctx);                                              \
3484
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3485
    tcg_temp_free(EA);                                                        \
3486
}
3487

    
3488
#define GEN_STFS(name, stop, op, type)                                        \
3489
GEN_STF(name, stop, op | 0x20, type);                                         \
3490
GEN_STUF(name, stop, op | 0x21, type);                                        \
3491
GEN_STUXF(name, stop, op | 0x01, type);                                       \
3492
GEN_STXF(name, stop, 0x17, op | 0x00, type)
3493

    
3494
static always_inline void gen_qemu_st32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3495
{
3496
    TCGv_i32 t0 = tcg_temp_new_i32();
3497
    TCGv t1 = tcg_temp_new();
3498
    gen_helper_float64_to_float32(t0, arg1);
3499
    tcg_gen_extu_i32_tl(t1, t0);
3500
    tcg_temp_free_i32(t0);
3501
    gen_qemu_st32(t1, arg2, flags);
3502
    tcg_temp_free(t1);
3503
}
3504

    
3505
/* stfd stfdu stfdux stfdx */
3506
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3507
/* stfs stfsu stfsux stfsx */
3508
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3509

    
3510
/* Optional: */
3511
static always_inline void gen_qemu_st32fiw(TCGv_i64 arg1, TCGv arg2, int flags)
3512
{
3513
    TCGv t0 = tcg_temp_new();
3514
    tcg_gen_trunc_i64_tl(t0, arg1),
3515
    gen_qemu_st32(t0, arg2, flags);
3516
    tcg_temp_free(t0);
3517
}
3518
/* stfiwx */
3519
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3520

    
3521
/***                                Branch                                 ***/
3522
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3523
                                       target_ulong dest)
3524
{
3525
    TranslationBlock *tb;
3526
    tb = ctx->tb;
3527
#if defined(TARGET_PPC64)
3528
    if (!ctx->sf_mode)
3529
        dest = (uint32_t) dest;
3530
#endif
3531
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3532
        likely(!ctx->singlestep_enabled)) {
3533
        tcg_gen_goto_tb(n);
3534
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3535
        tcg_gen_exit_tb((long)tb + n);
3536
    } else {
3537
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3538
        if (unlikely(ctx->singlestep_enabled)) {
3539
            if ((ctx->singlestep_enabled &
3540
                (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3541
                ctx->exception == POWERPC_EXCP_BRANCH) {
3542
                target_ulong tmp = ctx->nip;
3543
                ctx->nip = dest;
3544
                GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
3545
                ctx->nip = tmp;
3546
            }
3547
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3548
                gen_update_nip(ctx, dest);
3549
                gen_helper_raise_debug();
3550
            }
3551
        }
3552
        tcg_gen_exit_tb(0);
3553
    }
3554
}
3555

    
3556
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3557
{
3558
#if defined(TARGET_PPC64)
3559
    if (ctx->sf_mode == 0)
3560
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3561
    else
3562
#endif
3563
        tcg_gen_movi_tl(cpu_lr, nip);
3564
}
3565

    
3566
/* b ba bl bla */
3567
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3568
{
3569
    target_ulong li, target;
3570

    
3571
    ctx->exception = POWERPC_EXCP_BRANCH;
3572
    /* sign extend LI */
3573
#if defined(TARGET_PPC64)
3574
    if (ctx->sf_mode)
3575
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3576
    else
3577
#endif
3578
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3579
    if (likely(AA(ctx->opcode) == 0))
3580
        target = ctx->nip + li - 4;
3581
    else
3582
        target = li;
3583
    if (LK(ctx->opcode))
3584
        gen_setlr(ctx, ctx->nip);
3585
    gen_goto_tb(ctx, 0, target);
3586
}
3587

    
3588
#define BCOND_IM  0
3589
#define BCOND_LR  1
3590
#define BCOND_CTR 2
3591

    
3592
static always_inline void gen_bcond (DisasContext *ctx, int type)
3593
{
3594
    uint32_t bo = BO(ctx->opcode);
3595
    int l1 = gen_new_label();
3596
    TCGv target;
3597

    
3598
    ctx->exception = POWERPC_EXCP_BRANCH;
3599
    if (type == BCOND_LR || type == BCOND_CTR) {
3600
        target = tcg_temp_local_new();
3601
        if (type == BCOND_CTR)
3602
            tcg_gen_mov_tl(target, cpu_ctr);
3603
        else
3604
            tcg_gen_mov_tl(target, cpu_lr);
3605
    }
3606
    if (LK(ctx->opcode))
3607
        gen_setlr(ctx, ctx->nip);
3608
    l1 = gen_new_label();
3609
    if ((bo & 0x4) == 0) {
3610
        /* Decrement and test CTR */
3611
        TCGv temp = tcg_temp_new();
3612
        if (unlikely(type == BCOND_CTR)) {
3613
            GEN_EXCP_INVAL(ctx);
3614
            return;
3615
        }
3616
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3617
#if defined(TARGET_PPC64)
3618
        if (!ctx->sf_mode)
3619
            tcg_gen_ext32u_tl(temp, cpu_ctr);
3620
        else
3621
#endif
3622
            tcg_gen_mov_tl(temp, cpu_ctr);
3623
        if (bo & 0x2) {
3624
            tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3625
        } else {
3626
            tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3627
        }
3628
        tcg_temp_free(temp);
3629
    }
3630
    if ((bo & 0x10) == 0) {
3631
        /* Test CR */
3632
        uint32_t bi = BI(ctx->opcode);
3633
        uint32_t mask = 1 << (3 - (bi & 0x03));
3634
        TCGv_i32 temp = tcg_temp_new_i32();
3635

    
3636
        if (bo & 0x8) {
3637
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3638
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3639
        } else {
3640
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3641
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3642
        }
3643
        tcg_temp_free_i32(temp);
3644
    }
3645
    if (type == BCOND_IM) {
3646
        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3647
        if (likely(AA(ctx->opcode) == 0)) {
3648
            gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3649
        } else {
3650
            gen_goto_tb(ctx, 0, li);
3651
        }
3652
        gen_set_label(l1);
3653
        gen_goto_tb(ctx, 1, ctx->nip);
3654
    } else {
3655
#if defined(TARGET_PPC64)
3656
        if (!(ctx->sf_mode))
3657
            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3658
        else
3659
#endif
3660
            tcg_gen_andi_tl(cpu_nip, target, ~3);
3661
        tcg_gen_exit_tb(0);
3662
        gen_set_label(l1);
3663
#if defined(TARGET_PPC64)
3664
        if (!(ctx->sf_mode))
3665
            tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3666
        else
3667
#endif
3668
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
3669
        tcg_gen_exit_tb(0);
3670
    }
3671
}
3672

    
3673
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3674
{
3675
    gen_bcond(ctx, BCOND_IM);
3676
}
3677

    
3678
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3679
{
3680
    gen_bcond(ctx, BCOND_CTR);
3681
}
3682

    
3683
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3684
{
3685
    gen_bcond(ctx, BCOND_LR);
3686
}
3687

    
3688
/***                      Condition register logical                       ***/
3689
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3690
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
3691
{                                                                             \
3692
    uint8_t bitmask;                                                          \
3693
    int sh;                                                                   \
3694
    TCGv_i32 t0, t1;                                                          \
3695
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3696
    t0 = tcg_temp_new_i32();                                                  \
3697
    if (sh > 0)                                                               \
3698
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3699
    else if (sh < 0)                                                          \
3700
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3701
    else                                                                      \
3702
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3703
    t1 = tcg_temp_new_i32();                                                  \
3704
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3705
    if (sh > 0)                                                               \
3706
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3707
    else if (sh < 0)                                                          \
3708
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3709
    else                                                                      \
3710
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3711
    tcg_op(t0, t0, t1);                                                       \
3712
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3713
    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3714
    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3715
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3716
    tcg_temp_free_i32(t0);                                                    \
3717
    tcg_temp_free_i32(t1);                                                    \
3718
}
3719

    
3720
/* crand */
3721
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3722
/* crandc */
3723
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3724
/* creqv */
3725
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3726
/* crnand */
3727
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3728
/* crnor */
3729
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3730
/* cror */
3731
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3732
/* crorc */
3733
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3734
/* crxor */
3735
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3736
/* mcrf */
3737
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3738
{
3739
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3740
}
3741

    
3742
/***                           System linkage                              ***/
3743
/* rfi (supervisor only) */
3744
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3745
{
3746
#if defined(CONFIG_USER_ONLY)
3747
    GEN_EXCP_PRIVOPC(ctx);
3748
#else
3749
    /* Restore CPU state */
3750
    if (unlikely(!ctx->supervisor)) {
3751
        GEN_EXCP_PRIVOPC(ctx);
3752
        return;
3753
    }
3754
    gen_op_rfi();
3755
    GEN_SYNC(ctx);
3756
#endif
3757
}
3758

    
3759
#if defined(TARGET_PPC64)
3760
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3761
{
3762
#if defined(CONFIG_USER_ONLY)
3763
    GEN_EXCP_PRIVOPC(ctx);
3764
#else
3765
    /* Restore CPU state */
3766
    if (unlikely(!ctx->supervisor)) {
3767
        GEN_EXCP_PRIVOPC(ctx);
3768
        return;
3769
    }
3770
    gen_op_rfid();
3771
    GEN_SYNC(ctx);
3772
#endif
3773
}
3774

    
3775
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3776
{
3777
#if defined(CONFIG_USER_ONLY)
3778
    GEN_EXCP_PRIVOPC(ctx);
3779
#else
3780
    /* Restore CPU state */
3781
    if (unlikely(ctx->supervisor <= 1)) {
3782
        GEN_EXCP_PRIVOPC(ctx);
3783
        return;
3784
    }
3785
    gen_op_hrfid();
3786
    GEN_SYNC(ctx);
3787
#endif
3788
}
3789
#endif
3790

    
3791
/* sc */
3792
#if defined(CONFIG_USER_ONLY)
3793
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3794
#else
3795
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3796
#endif
3797
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3798
{
3799
    uint32_t lev;
3800

    
3801
    lev = (ctx->opcode >> 5) & 0x7F;
3802
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3803
}
3804

    
3805
/***                                Trap                                   ***/
3806
/* tw */
3807
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3808
{
3809
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3810
    /* Update the nip since this might generate a trap exception */
3811
    gen_update_nip(ctx, ctx->nip);
3812
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3813
    tcg_temp_free_i32(t0);
3814
}
3815

    
3816
/* twi */
3817
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3818
{
3819
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3820
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3821
    /* Update the nip since this might generate a trap exception */
3822
    gen_update_nip(ctx, ctx->nip);
3823
    gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3824
    tcg_temp_free(t0);
3825
    tcg_temp_free_i32(t1);
3826
}
3827

    
3828
#if defined(TARGET_PPC64)
3829
/* td */
3830
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3831
{
3832
    TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3833
    /* Update the nip since this might generate a trap exception */
3834
    gen_update_nip(ctx, ctx->nip);
3835
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3836
    tcg_temp_free_i32(t0);
3837
}
3838

    
3839
/* tdi */
3840
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3841
{
3842
    TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3843
    TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3844
    /* Update the nip since this might generate a trap exception */
3845
    gen_update_nip(ctx, ctx->nip);
3846
    gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3847
    tcg_temp_free(t0);
3848
    tcg_temp_free_i32(t1);
3849
}
3850
#endif
3851

    
3852
/***                          Processor control                            ***/
3853
/* mcrxr */
3854
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3855
{
3856
    tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3857
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3858
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3859
}
3860

    
3861
/* mfcr */
3862
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3863
{
3864
    uint32_t crm, crn;
3865

    
3866
    if (likely(ctx->opcode & 0x00100000)) {
3867
        crm = CRM(ctx->opcode);
3868
        if (likely((crm ^ (crm - 1)) == 0)) {
3869
            crn = ffs(crm);
3870
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3871
        }
3872
    } else {
3873
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3874
    }
3875
}
3876

    
3877
/* mfmsr */
3878
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3879
{
3880
#if defined(CONFIG_USER_ONLY)
3881
    GEN_EXCP_PRIVREG(ctx);
3882
#else
3883
    if (unlikely(!ctx->supervisor)) {
3884
        GEN_EXCP_PRIVREG(ctx);
3885
        return;
3886
    }
3887
    gen_op_load_msr();
3888
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3889
#endif
3890
}
3891

    
3892
#if 1
3893
#define SPR_NOACCESS ((void *)(-1UL))
3894
#else
3895
static void spr_noaccess (void *opaque, int sprn)
3896
{
3897
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3898
    printf("ERROR: try to access SPR %d !\n", sprn);
3899
}
3900
#define SPR_NOACCESS (&spr_noaccess)
3901
#endif
3902

    
3903
/* mfspr */
3904
static always_inline void gen_op_mfspr (DisasContext *ctx)
3905
{
3906
    void (*read_cb)(void *opaque, int sprn);
3907
    uint32_t sprn = SPR(ctx->opcode);
3908

    
3909
#if !defined(CONFIG_USER_ONLY)
3910
    if (ctx->supervisor == 2)
3911
        read_cb = ctx->spr_cb[sprn].hea_read;
3912
    else if (ctx->supervisor)
3913
        read_cb = ctx->spr_cb[sprn].oea_read;
3914
    else
3915
#endif
3916
        read_cb = ctx->spr_cb[sprn].uea_read;
3917
    if (likely(read_cb != NULL)) {
3918
        if (likely(read_cb != SPR_NOACCESS)) {
3919
            (*read_cb)(ctx, sprn);
3920
            tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3921
        } else {
3922
            /* Privilege exception */
3923
            /* This is a hack to avoid warnings when running Linux:
3924
             * this OS breaks the PowerPC virtualisation model,
3925
             * allowing userland application to read the PVR
3926
             */
3927
            if (sprn != SPR_PVR) {
3928
                if (loglevel != 0) {
3929
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
3930
                            ADDRX "\n", sprn, sprn, ctx->nip);
3931
                }
3932
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3933
                       sprn, sprn, ctx->nip);
3934
            }
3935
            GEN_EXCP_PRIVREG(ctx);
3936
        }
3937
    } else {
3938
        /* Not defined */
3939
        if (loglevel != 0) {
3940
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
3941
                    ADDRX "\n", sprn, sprn, ctx->nip);
3942
        }
3943
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3944
               sprn, sprn, ctx->nip);
3945
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3946
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3947
    }
3948
}
3949

    
3950
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3951
{
3952
    gen_op_mfspr(ctx);
3953
}
3954

    
3955
/* mftb */
3956
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3957
{
3958
    gen_op_mfspr(ctx);
3959
}
3960

    
3961
/* mtcrf */
3962
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3963
{
3964
    uint32_t crm, crn;
3965

    
3966
    crm = CRM(ctx->opcode);
3967
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3968
        TCGv_i32 temp = tcg_temp_new_i32();
3969
        crn = ffs(crm);
3970
        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3971
        tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3972
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3973
        tcg_temp_free_i32(temp);
3974
    } else {
3975
        TCGv_i32 temp = tcg_const_i32(crm);
3976
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3977
        tcg_temp_free_i32(temp);
3978
    }
3979
}
3980

    
3981
/* mtmsr */
3982
#if defined(TARGET_PPC64)
3983
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3984
{
3985
#if defined(CONFIG_USER_ONLY)
3986
    GEN_EXCP_PRIVREG(ctx);
3987
#else
3988
    if (unlikely(!ctx->supervisor)) {
3989
        GEN_EXCP_PRIVREG(ctx);
3990
        return;
3991
    }
3992
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3993
    if (ctx->opcode & 0x00010000) {
3994
        /* Special form that does not need any synchronisation */
3995
        gen_op_update_riee();
3996
    } else {
3997
        /* XXX: we need to update nip before the store
3998
         *      if we enter power saving mode, we will exit the loop
3999
         *      directly from ppc_store_msr
4000
         */
4001
        gen_update_nip(ctx, ctx->nip);
4002
        gen_op_store_msr();
4003
        /* Must stop the translation as machine state (may have) changed */
4004
        /* Note that mtmsr is not always defined as context-synchronizing */
4005
        ctx->exception = POWERPC_EXCP_STOP;
4006
    }
4007
#endif
4008
}
4009
#endif
4010

    
4011
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
4012
{
4013
#if defined(CONFIG_USER_ONLY)
4014
    GEN_EXCP_PRIVREG(ctx);
4015
#else
4016
    if (unlikely(!ctx->supervisor)) {
4017
        GEN_EXCP_PRIVREG(ctx);
4018
        return;
4019
    }
4020
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4021
    if (ctx->opcode & 0x00010000) {
4022
        /* Special form that does not need any synchronisation */
4023
        gen_op_update_riee();
4024
    } else {
4025
        /* XXX: we need to update nip before the store
4026
         *      if we enter power saving mode, we will exit the loop
4027
         *      directly from ppc_store_msr
4028
         */
4029
        gen_update_nip(ctx, ctx->nip);
4030
#if defined(TARGET_PPC64)
4031
        if (!ctx->sf_mode)
4032
            gen_op_store_msr_32();
4033
        else
4034
#endif
4035
            gen_op_store_msr();
4036
        /* Must stop the translation as machine state (may have) changed */
4037
        /* Note that mtmsrd is not always defined as context-synchronizing */
4038
        ctx->exception = POWERPC_EXCP_STOP;
4039
    }
4040
#endif
4041
}
4042

    
4043
/* mtspr */
4044
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4045
{
4046
    void (*write_cb)(void *opaque, int sprn);
4047
    uint32_t sprn = SPR(ctx->opcode);
4048

    
4049
#if !defined(CONFIG_USER_ONLY)
4050
    if (ctx->supervisor == 2)
4051
        write_cb = ctx->spr_cb[sprn].hea_write;
4052
    else if (ctx->supervisor)
4053
        write_cb = ctx->spr_cb[sprn].oea_write;
4054
    else
4055
#endif
4056
        write_cb = ctx->spr_cb[sprn].uea_write;
4057
    if (likely(write_cb != NULL)) {
4058
        if (likely(write_cb != SPR_NOACCESS)) {
4059
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4060
            (*write_cb)(ctx, sprn);
4061
        } else {
4062
            /* Privilege exception */
4063
            if (loglevel != 0) {
4064
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
4065
                        ADDRX "\n", sprn, sprn, ctx->nip);
4066
            }
4067
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4068
                   sprn, sprn, ctx->nip);
4069
            GEN_EXCP_PRIVREG(ctx);
4070
        }
4071
    } else {
4072
        /* Not defined */
4073
        if (loglevel != 0) {
4074
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
4075
                    ADDRX "\n", sprn, sprn, ctx->nip);
4076
        }
4077
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4078
               sprn, sprn, ctx->nip);
4079
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
4080
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
4081
    }
4082
}
4083

    
4084
/***                         Cache management                              ***/
4085
/* dcbf */
4086
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4087
{
4088
    /* XXX: specification says this is treated as a load by the MMU */
4089
    TCGv t0 = tcg_temp_new();
4090
    gen_set_access_type(ACCESS_CACHE);
4091
    gen_addr_reg_index(t0, ctx);
4092
    gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4093
    tcg_temp_free(t0);
4094
}
4095

    
4096
/* dcbi (Supervisor only) */
4097
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4098
{
4099
#if defined(CONFIG_USER_ONLY)
4100
    GEN_EXCP_PRIVOPC(ctx);
4101
#else
4102
    TCGv EA, val;
4103
    if (unlikely(!ctx->supervisor)) {
4104
        GEN_EXCP_PRIVOPC(ctx);
4105
        return;
4106
    }
4107
    EA = tcg_temp_new();
4108
    gen_set_access_type(ACCESS_CACHE);
4109
    gen_addr_reg_index(EA, ctx);
4110
    val = tcg_temp_new();
4111
    /* XXX: specification says this should be treated as a store by the MMU */
4112
    gen_qemu_ld8u(val, EA, ctx->mem_idx);
4113
    gen_qemu_st8(val, EA, ctx->mem_idx);
4114
    tcg_temp_free(val);
4115
    tcg_temp_free(EA);
4116
#endif
4117
}
4118

    
4119
/* dcdst */
4120
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4121
{
4122
    /* XXX: specification say this is treated as a load by the MMU */
4123
    TCGv t0 = tcg_temp_new();
4124
    gen_set_access_type(ACCESS_CACHE);
4125
    gen_addr_reg_index(t0, ctx);
4126
    gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4127
    tcg_temp_free(t0);
4128
}
4129

    
4130
/* dcbt */
4131
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4132
{
4133
    /* interpreted as no-op */
4134
    /* XXX: specification say this is treated as a load by the MMU
4135
     *      but does not generate any exception
4136
     */
4137
}
4138

    
4139
/* dcbtst */
4140
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4141
{
4142
    /* interpreted as no-op */
4143
    /* XXX: specification say this is treated as a load by the MMU
4144
     *      but does not generate any exception
4145
     */
4146
}
4147

    
4148
/* dcbz */
4149
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
4150
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
4151
    /* 32 bytes cache line size */
4152
    {
4153
#define gen_op_dcbz_l32_le_raw        gen_op_dcbz_l32_raw
4154
#define gen_op_dcbz_l32_le_user       gen_op_dcbz_l32_user
4155
#define gen_op_dcbz_l32_le_kernel     gen_op_dcbz_l32_kernel
4156
#define gen_op_dcbz_l32_le_hypv       gen_op_dcbz_l32_hypv
4157
#define gen_op_dcbz_l32_le_64_raw     gen_op_dcbz_l32_64_raw
4158
#define gen_op_dcbz_l32_le_64_user    gen_op_dcbz_l32_64_user
4159
#define gen_op_dcbz_l32_le_64_kernel  gen_op_dcbz_l32_64_kernel
4160
#define gen_op_dcbz_l32_le_64_hypv    gen_op_dcbz_l32_64_hypv
4161
        GEN_MEM_FUNCS(dcbz_l32),
4162
    },
4163
    /* 64 bytes cache line size */
4164
    {
4165
#define gen_op_dcbz_l64_le_raw        gen_op_dcbz_l64_raw
4166
#define gen_op_dcbz_l64_le_user       gen_op_dcbz_l64_user
4167
#define gen_op_dcbz_l64_le_kernel     gen_op_dcbz_l64_kernel
4168
#define gen_op_dcbz_l64_le_hypv       gen_op_dcbz_l64_hypv
4169
#define gen_op_dcbz_l64_le_64_raw     gen_op_dcbz_l64_64_raw
4170
#define gen_op_dcbz_l64_le_64_user    gen_op_dcbz_l64_64_user
4171
#define gen_op_dcbz_l64_le_64_kernel  gen_op_dcbz_l64_64_kernel
4172
#define gen_op_dcbz_l64_le_64_hypv    gen_op_dcbz_l64_64_hypv
4173
        GEN_MEM_FUNCS(dcbz_l64),
4174
    },
4175
    /* 128 bytes cache line size */
4176
    {
4177
#define gen_op_dcbz_l128_le_raw       gen_op_dcbz_l128_raw
4178
#define gen_op_dcbz_l128_le_user      gen_op_dcbz_l128_user
4179
#define gen_op_dcbz_l128_le_kernel    gen_op_dcbz_l128_kernel
4180
#define gen_op_dcbz_l128_le_hypv      gen_op_dcbz_l128_hypv
4181
#define gen_op_dcbz_l128_le_64_raw    gen_op_dcbz_l128_64_raw
4182
#define gen_op_dcbz_l128_le_64_user   gen_op_dcbz_l128_64_user
4183
#define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
4184
#define gen_op_dcbz_l128_le_64_hypv   gen_op_dcbz_l128_64_hypv
4185
        GEN_MEM_FUNCS(dcbz_l128),
4186
    },
4187
    /* tunable cache line size */
4188
    {
4189
#define gen_op_dcbz_le_raw            gen_op_dcbz_raw
4190
#define gen_op_dcbz_le_user           gen_op_dcbz_user
4191
#define gen_op_dcbz_le_kernel         gen_op_dcbz_kernel
4192
#define gen_op_dcbz_le_hypv           gen_op_dcbz_hypv
4193
#define gen_op_dcbz_le_64_raw         gen_op_dcbz_64_raw
4194
#define gen_op_dcbz_le_64_user        gen_op_dcbz_64_user
4195
#define gen_op_dcbz_le_64_kernel      gen_op_dcbz_64_kernel
4196
#define gen_op_dcbz_le_64_hypv        gen_op_dcbz_64_hypv
4197
        GEN_MEM_FUNCS(dcbz),
4198
    },
4199
};
4200

    
4201
static always_inline void handler_dcbz (DisasContext *ctx,
4202
                                        int dcache_line_size)
4203
{
4204
    int n;
4205

    
4206
    switch (dcache_line_size) {
4207
    case 32:
4208
        n = 0;
4209
        break;
4210
    case 64:
4211
        n = 1;
4212
        break;
4213
    case 128:
4214
        n = 2;
4215
        break;
4216
    default:
4217
        n = 3;
4218
        break;
4219
    }
4220
    op_dcbz(n);
4221
}
4222

    
4223
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4224
{
4225
    gen_addr_reg_index(cpu_T[0], ctx);
4226
    handler_dcbz(ctx, ctx->dcache_line_size);
4227
    gen_op_check_reservation();
4228
}
4229

    
4230
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4231
{
4232
    gen_addr_reg_index(cpu_T[0], ctx);
4233
    if (ctx->opcode & 0x00200000)
4234
        handler_dcbz(ctx, ctx->dcache_line_size);
4235
    else
4236
        handler_dcbz(ctx, -1);
4237
    gen_op_check_reservation();
4238
}
4239

    
4240
/* icbi */
4241
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
4242
#define gen_op_icbi_le_raw       gen_op_icbi_raw
4243
#define gen_op_icbi_le_user      gen_op_icbi_user
4244
#define gen_op_icbi_le_kernel    gen_op_icbi_kernel
4245
#define gen_op_icbi_le_hypv      gen_op_icbi_hypv
4246
#define gen_op_icbi_le_64_raw    gen_op_icbi_64_raw
4247
#define gen_op_icbi_le_64_user   gen_op_icbi_64_user
4248
#define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
4249
#define gen_op_icbi_le_64_hypv   gen_op_icbi_64_hypv
4250
static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
4251
    GEN_MEM_FUNCS(icbi),
4252
};
4253

    
4254
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4255
{
4256
    /* NIP cannot be restored if the memory exception comes from an helper */
4257
    gen_update_nip(ctx, ctx->nip - 4);
4258
    gen_addr_reg_index(cpu_T[0], ctx);
4259
    op_icbi();
4260
}
4261

    
4262
/* Optional: */
4263
/* dcba */
4264
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4265
{
4266
    /* interpreted as no-op */
4267
    /* XXX: specification say this is treated as a store by the MMU
4268
     *      but does not generate any exception
4269
     */
4270
}
4271

    
4272
/***                    Segment register manipulation                      ***/
4273
/* Supervisor only: */
4274
/* mfsr */
4275
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4276
{
4277
#if defined(CONFIG_USER_ONLY)
4278
    GEN_EXCP_PRIVREG(ctx);
4279
#else
4280
    if (unlikely(!ctx->supervisor)) {
4281
        GEN_EXCP_PRIVREG(ctx);
4282
        return;
4283
    }
4284
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4285
    gen_op_load_sr();
4286
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4287
#endif
4288
}
4289

    
4290
/* mfsrin */
4291
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4292
{
4293
#if defined(CONFIG_USER_ONLY)
4294
    GEN_EXCP_PRIVREG(ctx);
4295
#else
4296
    if (unlikely(!ctx->supervisor)) {
4297
        GEN_EXCP_PRIVREG(ctx);
4298
        return;
4299
    }
4300
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4301
    gen_op_srli_T1(28);
4302
    gen_op_load_sr();
4303
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4304
#endif
4305
}
4306

    
4307
/* mtsr */
4308
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4309
{
4310
#if defined(CONFIG_USER_ONLY)
4311
    GEN_EXCP_PRIVREG(ctx);
4312
#else
4313
    if (unlikely(!ctx->supervisor)) {
4314
        GEN_EXCP_PRIVREG(ctx);
4315
        return;
4316
    }
4317
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4318
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4319
    gen_op_store_sr();
4320
#endif
4321
}
4322

    
4323
/* mtsrin */
4324
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4325
{
4326
#if defined(CONFIG_USER_ONLY)
4327
    GEN_EXCP_PRIVREG(ctx);
4328
#else
4329
    if (unlikely(!ctx->supervisor)) {
4330
        GEN_EXCP_PRIVREG(ctx);
4331
        return;
4332
    }
4333
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4334
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4335
    gen_op_srli_T1(28);
4336
    gen_op_store_sr();
4337
#endif
4338
}
4339

    
4340
#if defined(TARGET_PPC64)
4341
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4342
/* mfsr */
4343
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4344
{
4345
#if defined(CONFIG_USER_ONLY)
4346
    GEN_EXCP_PRIVREG(ctx);
4347
#else
4348
    if (unlikely(!ctx->supervisor)) {
4349
        GEN_EXCP_PRIVREG(ctx);
4350
        return;
4351
    }
4352
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4353
    gen_op_load_slb();
4354
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4355
#endif
4356
}
4357

    
4358
/* mfsrin */
4359
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4360
             PPC_SEGMENT_64B)
4361
{
4362
#if defined(CONFIG_USER_ONLY)
4363
    GEN_EXCP_PRIVREG(ctx);
4364
#else
4365
    if (unlikely(!ctx->supervisor)) {
4366
        GEN_EXCP_PRIVREG(ctx);
4367
        return;
4368
    }
4369
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4370
    gen_op_srli_T1(28);
4371
    gen_op_load_slb();
4372
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4373
#endif
4374
}
4375

    
4376
/* mtsr */
4377
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4378
{
4379
#if defined(CONFIG_USER_ONLY)
4380
    GEN_EXCP_PRIVREG(ctx);
4381
#else
4382
    if (unlikely(!ctx->supervisor)) {
4383
        GEN_EXCP_PRIVREG(ctx);
4384
        return;
4385
    }
4386
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4387
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4388
    gen_op_store_slb();
4389
#endif
4390
}
4391

    
4392
/* mtsrin */
4393
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4394
             PPC_SEGMENT_64B)
4395
{
4396
#if defined(CONFIG_USER_ONLY)
4397
    GEN_EXCP_PRIVREG(ctx);
4398
#else
4399
    if (unlikely(!ctx->supervisor)) {
4400
        GEN_EXCP_PRIVREG(ctx);
4401
        return;
4402
    }
4403
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4404
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4405
    gen_op_srli_T1(28);
4406
    gen_op_store_slb();
4407
#endif
4408
}
4409
#endif /* defined(TARGET_PPC64) */
4410

    
4411
/***                      Lookaside buffer management                      ***/
4412
/* Optional & supervisor only: */
4413
/* tlbia */
4414
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4415
{
4416
#if defined(CONFIG_USER_ONLY)
4417
    GEN_EXCP_PRIVOPC(ctx);
4418
#else
4419
    if (unlikely(!ctx->supervisor)) {
4420
        GEN_EXCP_PRIVOPC(ctx);
4421
        return;
4422
    }
4423
    gen_op_tlbia();
4424
#endif
4425
}
4426

    
4427
/* tlbie */
4428
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4429
{
4430
#if defined(CONFIG_USER_ONLY)
4431
    GEN_EXCP_PRIVOPC(ctx);
4432
#else
4433
    if (unlikely(!ctx->supervisor)) {
4434
        GEN_EXCP_PRIVOPC(ctx);
4435
        return;
4436
    }
4437
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4438
#if defined(TARGET_PPC64)
4439
    if (ctx->sf_mode)
4440
        gen_op_tlbie_64();
4441
    else
4442
#endif
4443
        gen_op_tlbie();
4444
#endif
4445
}
4446

    
4447
/* tlbsync */
4448
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4449
{
4450
#if defined(CONFIG_USER_ONLY)
4451
    GEN_EXCP_PRIVOPC(ctx);
4452
#else
4453
    if (unlikely(!ctx->supervisor)) {
4454
        GEN_EXCP_PRIVOPC(ctx);
4455
        return;
4456
    }
4457
    /* This has no effect: it should ensure that all previous
4458
     * tlbie have completed
4459
     */
4460
    GEN_STOP(ctx);
4461
#endif
4462
}
4463

    
4464
#if defined(TARGET_PPC64)
4465
/* slbia */
4466
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4467
{
4468
#if defined(CONFIG_USER_ONLY)
4469
    GEN_EXCP_PRIVOPC(ctx);
4470
#else
4471
    if (unlikely(!ctx->supervisor)) {
4472
        GEN_EXCP_PRIVOPC(ctx);
4473
        return;
4474
    }
4475
    gen_op_slbia();
4476
#endif
4477
}
4478

    
4479
/* slbie */
4480
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4481
{
4482
#if defined(CONFIG_USER_ONLY)
4483
    GEN_EXCP_PRIVOPC(ctx);
4484
#else
4485
    if (unlikely(!ctx->supervisor)) {
4486
        GEN_EXCP_PRIVOPC(ctx);
4487
        return;
4488
    }
4489
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4490
    gen_op_slbie();
4491
#endif
4492
}
4493
#endif
4494

    
4495
/***                              External control                         ***/
4496
/* Optional: */
4497
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4498
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4499
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
4500
    GEN_MEM_FUNCS(eciwx),
4501
};
4502
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
4503
    GEN_MEM_FUNCS(ecowx),
4504
};
4505

    
4506
/* eciwx */
4507
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4508
{
4509
    /* Should check EAR[E] & alignment ! */
4510
    gen_set_access_type(ACCESS_RES);
4511
    gen_addr_reg_index(cpu_T[0], ctx);
4512
    op_eciwx();
4513
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4514
}
4515

    
4516
/* ecowx */
4517
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4518
{
4519
    /* Should check EAR[E] & alignment ! */
4520
    gen_addr_reg_index(cpu_T[0], ctx);
4521
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4522
    op_ecowx();
4523
}
4524

    
4525
/* PowerPC 601 specific instructions */
4526
/* abs - abs. */
4527
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4528
{
4529
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4530
    gen_op_POWER_abs();
4531
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4532
    if (unlikely(Rc(ctx->opcode) != 0))
4533
        gen_set_Rc0(ctx, cpu_T[0]);
4534
}
4535

    
4536
/* abso - abso. */
4537
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4538
{
4539
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4540
    gen_op_POWER_abso();
4541
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4542
    if (unlikely(Rc(ctx->opcode) != 0))
4543
        gen_set_Rc0(ctx, cpu_T[0]);
4544
}
4545

    
4546
/* clcs */
4547
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4548
{
4549
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4550
    gen_op_POWER_clcs();
4551
    /* Rc=1 sets CR0 to an undefined state */
4552
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4553
}
4554

    
4555
/* div - div. */
4556
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4557
{
4558
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4559
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4560
    gen_op_POWER_div();
4561
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4562
    if (unlikely(Rc(ctx->opcode) != 0))
4563
        gen_set_Rc0(ctx, cpu_T[0]);
4564
}
4565

    
4566
/* divo - divo. */
4567
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4568
{
4569
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4570
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4571
    gen_op_POWER_divo();
4572
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4573
    if (unlikely(Rc(ctx->opcode) != 0))
4574
        gen_set_Rc0(ctx, cpu_T[0]);
4575
}
4576

    
4577
/* divs - divs. */
4578
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4579
{
4580
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4581
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4582
    gen_op_POWER_divs();
4583
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4584
    if (unlikely(Rc(ctx->opcode) != 0))
4585
        gen_set_Rc0(ctx, cpu_T[0]);
4586
}
4587

    
4588
/* divso - divso. */
4589
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4590
{
4591
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4592
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4593
    gen_op_POWER_divso();
4594
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4595
    if (unlikely(Rc(ctx->opcode) != 0))
4596
        gen_set_Rc0(ctx, cpu_T[0]);
4597
}
4598

    
4599
/* doz - doz. */
4600
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4601
{
4602
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4603
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4604
    gen_op_POWER_doz();
4605
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4606
    if (unlikely(Rc(ctx->opcode) != 0))
4607
        gen_set_Rc0(ctx, cpu_T[0]);
4608
}
4609

    
4610
/* dozo - dozo. */
4611
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4612
{
4613
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4614
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4615
    gen_op_POWER_dozo();
4616
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4617
    if (unlikely(Rc(ctx->opcode) != 0))
4618
        gen_set_Rc0(ctx, cpu_T[0]);
4619
}
4620

    
4621
/* dozi */
4622
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4623
{
4624
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4625
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4626
    gen_op_POWER_doz();
4627
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4628
}
4629

    
4630
/* As lscbx load from memory byte after byte, it's always endian safe.
4631
 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
4632
 */
4633
#define op_POWER_lscbx(start, ra, rb)                                         \
4634
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4635
#define gen_op_POWER_lscbx_64_raw       gen_op_POWER_lscbx_raw
4636
#define gen_op_POWER_lscbx_64_user      gen_op_POWER_lscbx_user
4637
#define gen_op_POWER_lscbx_64_kernel    gen_op_POWER_lscbx_kernel
4638
#define gen_op_POWER_lscbx_64_hypv      gen_op_POWER_lscbx_hypv
4639
#define gen_op_POWER_lscbx_le_raw       gen_op_POWER_lscbx_raw
4640
#define gen_op_POWER_lscbx_le_user      gen_op_POWER_lscbx_user
4641
#define gen_op_POWER_lscbx_le_kernel    gen_op_POWER_lscbx_kernel
4642
#define gen_op_POWER_lscbx_le_hypv      gen_op_POWER_lscbx_hypv
4643
#define gen_op_POWER_lscbx_le_64_raw    gen_op_POWER_lscbx_raw
4644
#define gen_op_POWER_lscbx_le_64_user   gen_op_POWER_lscbx_user
4645
#define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
4646
#define gen_op_POWER_lscbx_le_64_hypv   gen_op_POWER_lscbx_hypv
4647
static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
4648
    GEN_MEM_FUNCS(POWER_lscbx),
4649
};
4650

    
4651
/* lscbx - lscbx. */
4652
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4653
{
4654
    int ra = rA(ctx->opcode);
4655
    int rb = rB(ctx->opcode);
4656

    
4657
    gen_addr_reg_index(cpu_T[0], ctx);
4658
    if (ra == 0) {
4659
        ra = rb;
4660
    }
4661
    /* NIP cannot be restored if the memory exception comes from an helper */
4662
    gen_update_nip(ctx, ctx->nip - 4);
4663
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
4664
    tcg_gen_shri_tl(cpu_T[2], cpu_xer, XER_CMP);
4665
    tcg_gen_andi_tl(cpu_T[2], cpu_T[2], 0xFF);
4666
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4667
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4668
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
4669
    if (unlikely(Rc(ctx->opcode) != 0))
4670
        gen_set_Rc0(ctx, cpu_T[0]);
4671
}
4672

    
4673
/* maskg - maskg. */
4674
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4675
{
4676
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4677
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4678
    gen_op_POWER_maskg();
4679
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4680
    if (unlikely(Rc(ctx->opcode) != 0))
4681
        gen_set_Rc0(ctx, cpu_T[0]);
4682
}
4683

    
4684
/* maskir - maskir. */
4685
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4686
{
4687
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4688
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4689
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4690
    gen_op_POWER_maskir();
4691
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4692
    if (unlikely(Rc(ctx->opcode) != 0))
4693
        gen_set_Rc0(ctx, cpu_T[0]);
4694
}
4695

    
4696
/* mul - mul. */
4697
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4698
{
4699
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4700
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4701
    gen_op_POWER_mul();
4702
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4703
    if (unlikely(Rc(ctx->opcode) != 0))
4704
        gen_set_Rc0(ctx, cpu_T[0]);
4705
}
4706

    
4707
/* mulo - mulo. */
4708
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4709
{
4710
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4711
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4712
    gen_op_POWER_mulo();
4713
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4714
    if (unlikely(Rc(ctx->opcode) != 0))
4715
        gen_set_Rc0(ctx, cpu_T[0]);
4716
}
4717

    
4718
/* nabs - nabs. */
4719
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4720
{
4721
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4722
    gen_op_POWER_nabs();
4723
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4724
    if (unlikely(Rc(ctx->opcode) != 0))
4725
        gen_set_Rc0(ctx, cpu_T[0]);
4726
}
4727

    
4728
/* nabso - nabso. */
4729
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4730
{
4731
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4732
    gen_op_POWER_nabso();
4733
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4734
    if (unlikely(Rc(ctx->opcode) != 0))
4735
        gen_set_Rc0(ctx, cpu_T[0]);
4736
}
4737

    
4738
/* rlmi - rlmi. */
4739
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4740
{
4741
    uint32_t mb, me;
4742

    
4743
    mb = MB(ctx->opcode);
4744
    me = ME(ctx->opcode);
4745
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4746
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4747
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4748
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4749
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4750
    if (unlikely(Rc(ctx->opcode) != 0))
4751
        gen_set_Rc0(ctx, cpu_T[0]);
4752
}
4753

    
4754
/* rrib - rrib. */
4755
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4756
{
4757
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4758
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4759
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4760
    gen_op_POWER_rrib();
4761
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4762
    if (unlikely(Rc(ctx->opcode) != 0))
4763
        gen_set_Rc0(ctx, cpu_T[0]);
4764
}
4765

    
4766
/* sle - sle. */
4767
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4768
{
4769
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4770
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4771
    gen_op_POWER_sle();
4772
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4773
    if (unlikely(Rc(ctx->opcode) != 0))
4774
        gen_set_Rc0(ctx, cpu_T[0]);
4775
}
4776

    
4777
/* sleq - sleq. */
4778
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4779
{
4780
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4781
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4782
    gen_op_POWER_sleq();
4783
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4784
    if (unlikely(Rc(ctx->opcode) != 0))
4785
        gen_set_Rc0(ctx, cpu_T[0]);
4786
}
4787

    
4788
/* sliq - sliq. */
4789
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4790
{
4791
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4792
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4793
    gen_op_POWER_sle();
4794
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4795
    if (unlikely(Rc(ctx->opcode) != 0))
4796
        gen_set_Rc0(ctx, cpu_T[0]);
4797
}
4798

    
4799
/* slliq - slliq. */
4800
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4801
{
4802
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4803
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4804
    gen_op_POWER_sleq();
4805
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4806
    if (unlikely(Rc(ctx->opcode) != 0))
4807
        gen_set_Rc0(ctx, cpu_T[0]);
4808
}
4809

    
4810
/* sllq - sllq. */
4811
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4812
{
4813
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4814
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4815
    gen_op_POWER_sllq();
4816
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4817
    if (unlikely(Rc(ctx->opcode) != 0))
4818
        gen_set_Rc0(ctx, cpu_T[0]);
4819
}
4820

    
4821
/* slq - slq. */
4822
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4823
{
4824
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4825
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4826
    gen_op_POWER_slq();
4827
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4828
    if (unlikely(Rc(ctx->opcode) != 0))
4829
        gen_set_Rc0(ctx, cpu_T[0]);
4830
}
4831

    
4832
/* sraiq - sraiq. */
4833
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4834
{
4835
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4836
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4837
    gen_op_POWER_sraq();
4838
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4839
    if (unlikely(Rc(ctx->opcode) != 0))
4840
        gen_set_Rc0(ctx, cpu_T[0]);
4841
}
4842

    
4843
/* sraq - sraq. */
4844
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4845
{
4846
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4847
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4848
    gen_op_POWER_sraq();
4849
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4850
    if (unlikely(Rc(ctx->opcode) != 0))
4851
        gen_set_Rc0(ctx, cpu_T[0]);
4852
}
4853

    
4854
/* sre - sre. */
4855
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4856
{
4857
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4858
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4859
    gen_op_POWER_sre();
4860
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4861
    if (unlikely(Rc(ctx->opcode) != 0))
4862
        gen_set_Rc0(ctx, cpu_T[0]);
4863
}
4864

    
4865
/* srea - srea. */
4866
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4867
{
4868
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4869
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4870
    gen_op_POWER_srea();
4871
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4872
    if (unlikely(Rc(ctx->opcode) != 0))
4873
        gen_set_Rc0(ctx, cpu_T[0]);
4874
}
4875

    
4876
/* sreq */
4877
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4878
{
4879
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4880
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4881
    gen_op_POWER_sreq();
4882
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4883
    if (unlikely(Rc(ctx->opcode) != 0))
4884
        gen_set_Rc0(ctx, cpu_T[0]);
4885
}
4886

    
4887
/* sriq */
4888
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4889
{
4890
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4891
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4892
    gen_op_POWER_srq();
4893
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4894
    if (unlikely(Rc(ctx->opcode) != 0))
4895
        gen_set_Rc0(ctx, cpu_T[0]);
4896
}
4897

    
4898
/* srliq */
4899
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4900
{
4901
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4902
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4903
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4904
    gen_op_POWER_srlq();
4905
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4906
    if (unlikely(Rc(ctx->opcode) != 0))
4907
        gen_set_Rc0(ctx, cpu_T[0]);
4908
}
4909

    
4910
/* srlq */
4911
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4912
{
4913
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4914
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4915
    gen_op_POWER_srlq();
4916
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4917
    if (unlikely(Rc(ctx->opcode) != 0))
4918
        gen_set_Rc0(ctx, cpu_T[0]);
4919
}
4920

    
4921
/* srq */
4922
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4923
{
4924
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4925
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4926
    gen_op_POWER_srq();
4927
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4928
    if (unlikely(Rc(ctx->opcode) != 0))
4929
        gen_set_Rc0(ctx, cpu_T[0]);
4930
}
4931

    
4932
/* PowerPC 602 specific instructions */
4933
/* dsa  */
4934
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4935
{
4936
    /* XXX: TODO */
4937
    GEN_EXCP_INVAL(ctx);
4938
}
4939

    
4940
/* esa */
4941
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4942
{
4943
    /* XXX: TODO */
4944
    GEN_EXCP_INVAL(ctx);
4945
}
4946

    
4947
/* mfrom */
4948
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4949
{
4950
#if defined(CONFIG_USER_ONLY)
4951
    GEN_EXCP_PRIVOPC(ctx);
4952
#else
4953
    if (unlikely(!ctx->supervisor)) {
4954
        GEN_EXCP_PRIVOPC(ctx);
4955
        return;
4956
    }
4957
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4958
    gen_op_602_mfrom();
4959
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4960
#endif
4961
}
4962

    
4963
/* 602 - 603 - G2 TLB management */
4964
/* tlbld */
4965
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4966
{
4967
#if defined(CONFIG_USER_ONLY)
4968
    GEN_EXCP_PRIVOPC(ctx);
4969
#else
4970
    if (unlikely(!ctx->supervisor)) {
4971
        GEN_EXCP_PRIVOPC(ctx);
4972
        return;
4973
    }
4974
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4975
    gen_op_6xx_tlbld();
4976
#endif
4977
}
4978

    
4979
/* tlbli */
4980
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4981
{
4982
#if defined(CONFIG_USER_ONLY)
4983
    GEN_EXCP_PRIVOPC(ctx);
4984
#else
4985
    if (unlikely(!ctx->supervisor)) {
4986
        GEN_EXCP_PRIVOPC(ctx);
4987
        return;
4988
    }
4989
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4990
    gen_op_6xx_tlbli();
4991
#endif
4992
}
4993

    
4994
/* 74xx TLB management */
4995
/* tlbld */
4996
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4997
{
4998
#if defined(CONFIG_USER_ONLY)
4999
    GEN_EXCP_PRIVOPC(ctx);
5000
#else
5001
    if (unlikely(!ctx->supervisor)) {
5002
        GEN_EXCP_PRIVOPC(ctx);
5003
        return;
5004
    }
5005
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5006
    gen_op_74xx_tlbld();
5007
#endif
5008
}
5009

    
5010
/* tlbli */
5011
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5012
{
5013
#if defined(CONFIG_USER_ONLY)
5014
    GEN_EXCP_PRIVOPC(ctx);
5015
#else
5016
    if (unlikely(!ctx->supervisor)) {
5017
        GEN_EXCP_PRIVOPC(ctx);
5018
        return;
5019
    }
5020
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5021
    gen_op_74xx_tlbli();
5022
#endif
5023
}
5024

    
5025
/* POWER instructions not in PowerPC 601 */
5026
/* clf */
5027
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5028
{
5029
    /* Cache line flush: implemented as no-op */
5030
}
5031

    
5032
/* cli */
5033
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5034
{
5035
    /* Cache line invalidate: privileged and treated as no-op */
5036
#if defined(CONFIG_USER_ONLY)
5037
    GEN_EXCP_PRIVOPC(ctx);
5038
#else
5039
    if (unlikely(!ctx->supervisor)) {
5040
        GEN_EXCP_PRIVOPC(ctx);
5041
        return;
5042
    }
5043
#endif
5044
}
5045

    
5046
/* dclst */
5047
GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
5048
{
5049
    /* Data cache line store: treated as no-op */
5050
}
5051

    
5052
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5053
{
5054
#if defined(CONFIG_USER_ONLY)
5055
    GEN_EXCP_PRIVOPC(ctx);
5056
#else
5057
    if (unlikely(!ctx->supervisor)) {
5058
        GEN_EXCP_PRIVOPC(ctx);
5059
        return;
5060
    }
5061
    int ra = rA(ctx->opcode);
5062
    int rd = rD(ctx->opcode);
5063

    
5064
    gen_addr_reg_index(cpu_T[0], ctx);
5065
    gen_op_POWER_mfsri();
5066
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
5067
    if (ra != 0 && ra != rd)
5068
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
5069
#endif
5070
}
5071

    
5072
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5073
{
5074
#if defined(CONFIG_USER_ONLY)
5075
    GEN_EXCP_PRIVOPC(ctx);
5076
#else
5077
    if (unlikely(!ctx->supervisor)) {
5078
        GEN_EXCP_PRIVOPC(ctx);
5079
        return;
5080
    }
5081
    gen_addr_reg_index(cpu_T[0], ctx);
5082
    gen_op_POWER_rac();
5083
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5084
#endif
5085
}
5086

    
5087
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5088
{
5089
#if defined(CONFIG_USER_ONLY)
5090
    GEN_EXCP_PRIVOPC(ctx);
5091
#else
5092
    if (unlikely(!ctx->supervisor)) {
5093
        GEN_EXCP_PRIVOPC(ctx);
5094
        return;
5095
    }
5096
    gen_op_POWER_rfsvc();
5097
    GEN_SYNC(ctx);
5098
#endif
5099
}
5100

    
5101
/* svc is not implemented for now */
5102

    
5103
/* POWER2 specific instructions */
5104
/* Quad manipulation (load/store two floats at a time) */
5105
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
5106
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
5107
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
5108
#define gen_op_POWER2_lfq_64_raw        gen_op_POWER2_lfq_raw
5109
#define gen_op_POWER2_lfq_64_user       gen_op_POWER2_lfq_user
5110
#define gen_op_POWER2_lfq_64_kernel     gen_op_POWER2_lfq_kernel
5111
#define gen_op_POWER2_lfq_64_hypv       gen_op_POWER2_lfq_hypv
5112
#define gen_op_POWER2_lfq_le_64_raw     gen_op_POWER2_lfq_le_raw
5113
#define gen_op_POWER2_lfq_le_64_user    gen_op_POWER2_lfq_le_user
5114
#define gen_op_POWER2_lfq_le_64_kernel  gen_op_POWER2_lfq_le_kernel
5115
#define gen_op_POWER2_lfq_le_64_hypv    gen_op_POWER2_lfq_le_hypv
5116
#define gen_op_POWER2_stfq_64_raw       gen_op_POWER2_stfq_raw
5117
#define gen_op_POWER2_stfq_64_user      gen_op_POWER2_stfq_user
5118
#define gen_op_POWER2_stfq_64_kernel    gen_op_POWER2_stfq_kernel
5119
#define gen_op_POWER2_stfq_64_hypv      gen_op_POWER2_stfq_hypv
5120
#define gen_op_POWER2_stfq_le_64_raw    gen_op_POWER2_stfq_le_raw
5121
#define gen_op_POWER2_stfq_le_64_user   gen_op_POWER2_stfq_le_user
5122
#define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
5123
#define gen_op_POWER2_stfq_le_64_hypv   gen_op_POWER2_stfq_le_hypv
5124
static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
5125
    GEN_MEM_FUNCS(POWER2_lfq),
5126
};
5127
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
5128
    GEN_MEM_FUNCS(POWER2_stfq),
5129
};
5130

    
5131
/* lfq */
5132
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5133
{
5134
    /* NIP cannot be restored if the memory exception comes from an helper */
5135
    gen_update_nip(ctx, ctx->nip - 4);
5136
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5137
    op_POWER2_lfq();
5138
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5139
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5140
}
5141

    
5142
/* lfqu */
5143
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5144
{
5145
    int ra = rA(ctx->opcode);
5146

    
5147
    /* NIP cannot be restored if the memory exception comes from an helper */
5148
    gen_update_nip(ctx, ctx->nip - 4);
5149
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5150
    op_POWER2_lfq();
5151
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5152
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5153
    if (ra != 0)
5154
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5155
}
5156

    
5157
/* lfqux */
5158
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5159
{
5160
    int ra = rA(ctx->opcode);
5161

    
5162
    /* NIP cannot be restored if the memory exception comes from an helper */
5163
    gen_update_nip(ctx, ctx->nip - 4);
5164
    gen_addr_reg_index(cpu_T[0], ctx);
5165
    op_POWER2_lfq();
5166
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5167
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5168
    if (ra != 0)
5169
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5170
}
5171

    
5172
/* lfqx */
5173
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5174
{
5175
    /* NIP cannot be restored if the memory exception comes from an helper */
5176
    gen_update_nip(ctx, ctx->nip - 4);
5177
    gen_addr_reg_index(cpu_T[0], ctx);
5178
    op_POWER2_lfq();
5179
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5180
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5181
}
5182

    
5183
/* stfq */
5184
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5185
{
5186
    /* NIP cannot be restored if the memory exception comes from an helper */
5187
    gen_update_nip(ctx, ctx->nip - 4);
5188
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5189
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5190
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5191
    op_POWER2_stfq();
5192
}
5193

    
5194
/* stfqu */
5195
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5196
{
5197
    int ra = rA(ctx->opcode);
5198

    
5199
    /* NIP cannot be restored if the memory exception comes from an helper */
5200
    gen_update_nip(ctx, ctx->nip - 4);
5201
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5202
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5203
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5204
    op_POWER2_stfq();
5205
    if (ra != 0)
5206
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5207
}
5208

    
5209
/* stfqux */
5210
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5211
{
5212
    int ra = rA(ctx->opcode);
5213

    
5214
    /* NIP cannot be restored if the memory exception comes from an helper */
5215
    gen_update_nip(ctx, ctx->nip - 4);
5216
    gen_addr_reg_index(cpu_T[0], ctx);
5217
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5218
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5219
    op_POWER2_stfq();
5220
    if (ra != 0)
5221
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5222
}
5223

    
5224
/* stfqx */
5225
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5226
{
5227
    /* NIP cannot be restored if the memory exception comes from an helper */
5228
    gen_update_nip(ctx, ctx->nip - 4);
5229
    gen_addr_reg_index(cpu_T[0], ctx);
5230
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5231
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5232
    op_POWER2_stfq();
5233
}
5234

    
5235
/* BookE specific instructions */
5236
/* XXX: not implemented on 440 ? */
5237
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5238
{
5239
    /* XXX: TODO */
5240
    GEN_EXCP_INVAL(ctx);
5241
}
5242

    
5243
/* XXX: not implemented on 440 ? */
5244
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5245
{
5246
#if defined(CONFIG_USER_ONLY)
5247
    GEN_EXCP_PRIVOPC(ctx);
5248
#else
5249
    if (unlikely(!ctx->supervisor)) {
5250
        GEN_EXCP_PRIVOPC(ctx);
5251
        return;
5252
    }
5253
    gen_addr_reg_index(cpu_T[0], ctx);
5254
    /* Use the same micro-ops as for tlbie */
5255
#if defined(TARGET_PPC64)
5256
    if (ctx->sf_mode)
5257
        gen_op_tlbie_64();
5258
    else
5259
#endif
5260
        gen_op_tlbie();
5261
#endif
5262
}
5263

    
5264
/* All 405 MAC instructions are translated here */
5265
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5266
                                                int opc2, int opc3,
5267
                                                int ra, int rb, int rt, int Rc)
5268
{
5269
    TCGv t0, t1;
5270

    
5271
    t0 = tcg_temp_local_new();
5272
    t1 = tcg_temp_local_new();
5273

    
5274
    switch (opc3 & 0x0D) {
5275
    case 0x05:
5276
        /* macchw    - macchw.    - macchwo   - macchwo.   */
5277
        /* macchws   - macchws.   - macchwso  - macchwso.  */
5278
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5279
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5280
        /* mulchw - mulchw. */
5281
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5282
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5283
        tcg_gen_ext16s_tl(t1, t1);
5284
        break;
5285
    case 0x04:
5286
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5287
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5288
        /* mulchwu - mulchwu. */
5289
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5290
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5291
        tcg_gen_ext16u_tl(t1, t1);
5292
        break;
5293
    case 0x01:
5294
        /* machhw    - machhw.    - machhwo   - machhwo.   */
5295
        /* machhws   - machhws.   - machhwso  - machhwso.  */
5296
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5297
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5298
        /* mulhhw - mulhhw. */
5299
        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5300
        tcg_gen_ext16s_tl(t0, t0);
5301
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5302
        tcg_gen_ext16s_tl(t1, t1);
5303
        break;
5304
    case 0x00:
5305
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5306
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5307
        /* mulhhwu - mulhhwu. */
5308
        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5309
        tcg_gen_ext16u_tl(t0, t0);
5310
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5311
        tcg_gen_ext16u_tl(t1, t1);
5312
        break;
5313
    case 0x0D:
5314
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5315
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5316
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5317
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5318
        /* mullhw - mullhw. */
5319
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5320
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5321
        break;
5322
    case 0x0C:
5323
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5324
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5325
        /* mullhwu - mullhwu. */
5326
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5327
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5328
        break;
5329
    }
5330
    if (opc2 & 0x04) {
5331
        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5332
        tcg_gen_mul_tl(t1, t0, t1);
5333
        if (opc2 & 0x02) {
5334
            /* nmultiply-and-accumulate (0x0E) */
5335
            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5336
        } else {
5337
            /* multiply-and-accumulate (0x0C) */
5338
            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5339
        }
5340

    
5341
        if (opc3 & 0x12) {
5342
            /* Check overflow and/or saturate */
5343
            int l1 = gen_new_label();
5344

    
5345
            if (opc3 & 0x10) {
5346
                /* Start with XER OV disabled, the most likely case */
5347
                tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5348
            }
5349
            if (opc3 & 0x01) {
5350
                /* Signed */
5351
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5352
                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5353
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5354
                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5355
                if (opc3 & 0x02) {
5356
                    /* Saturate */
5357
                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5358
                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5359
                }
5360
            } else {
5361
                /* Unsigned */
5362
                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5363
                if (opc3 & 0x02) {
5364
                    /* Saturate */
5365
                    tcg_gen_movi_tl(t0, UINT32_MAX);
5366
                }
5367
            }
5368
            if (opc3 & 0x10) {
5369
                /* Check overflow */
5370
                tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5371
            }
5372
            gen_set_label(l1);
5373
            tcg_gen_mov_tl(cpu_gpr[rt], t0);
5374
        }
5375
    } else {
5376
        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5377
    }
5378
    tcg_temp_free(t0);
5379
    tcg_temp_free(t1);
5380
    if (unlikely(Rc) != 0) {
5381
        /* Update Rc0 */
5382
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5383
    }
5384
}
5385

    
5386
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5387
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5388
{                                                                             \
5389
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5390
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
5391
}
5392

    
5393
/* macchw    - macchw.    */
5394
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5395
/* macchwo   - macchwo.   */
5396
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5397
/* macchws   - macchws.   */
5398
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5399
/* macchwso  - macchwso.  */
5400
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5401
/* macchwsu  - macchwsu.  */
5402
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5403
/* macchwsuo - macchwsuo. */
5404
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5405
/* macchwu   - macchwu.   */
5406
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5407
/* macchwuo  - macchwuo.  */
5408
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5409
/* machhw    - machhw.    */
5410
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5411
/* machhwo   - machhwo.   */
5412
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5413
/* machhws   - machhws.   */
5414
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5415
/* machhwso  - machhwso.  */
5416
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5417
/* machhwsu  - machhwsu.  */
5418
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5419
/* machhwsuo - machhwsuo. */
5420
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5421
/* machhwu   - machhwu.   */
5422
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5423
/* machhwuo  - machhwuo.  */
5424
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5425
/* maclhw    - maclhw.    */
5426
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5427
/* maclhwo   - maclhwo.   */
5428
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5429
/* maclhws   - maclhws.   */
5430
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5431
/* maclhwso  - maclhwso.  */
5432
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5433
/* maclhwu   - maclhwu.   */
5434
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5435
/* maclhwuo  - maclhwuo.  */
5436
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5437
/* maclhwsu  - maclhwsu.  */
5438
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5439
/* maclhwsuo - maclhwsuo. */
5440
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5441
/* nmacchw   - nmacchw.   */
5442
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5443
/* nmacchwo  - nmacchwo.  */
5444
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5445
/* nmacchws  - nmacchws.  */
5446
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5447
/* nmacchwso - nmacchwso. */
5448
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5449
/* nmachhw   - nmachhw.   */
5450
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5451
/* nmachhwo  - nmachhwo.  */
5452
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5453
/* nmachhws  - nmachhws.  */
5454
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5455
/* nmachhwso - nmachhwso. */
5456
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5457
/* nmaclhw   - nmaclhw.   */
5458
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5459
/* nmaclhwo  - nmaclhwo.  */
5460
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5461
/* nmaclhws  - nmaclhws.  */
5462
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5463
/* nmaclhwso - nmaclhwso. */
5464
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5465

    
5466
/* mulchw  - mulchw.  */
5467
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5468
/* mulchwu - mulchwu. */
5469
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5470
/* mulhhw  - mulhhw.  */
5471
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5472
/* mulhhwu - mulhhwu. */
5473
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5474
/* mullhw  - mullhw.  */
5475
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5476
/* mullhwu - mullhwu. */
5477
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5478

    
5479
/* mfdcr */
5480
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5481
{
5482
#if defined(CONFIG_USER_ONLY)
5483
    GEN_EXCP_PRIVREG(ctx);
5484
#else
5485
    uint32_t dcrn = SPR(ctx->opcode);
5486

    
5487
    if (unlikely(!ctx->supervisor)) {
5488
        GEN_EXCP_PRIVREG(ctx);
5489
        return;
5490
    }
5491
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5492
    gen_op_load_dcr();
5493
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5494
#endif
5495
}
5496

    
5497
/* mtdcr */
5498
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5499
{
5500
#if defined(CONFIG_USER_ONLY)
5501
    GEN_EXCP_PRIVREG(ctx);
5502
#else
5503
    uint32_t dcrn = SPR(ctx->opcode);
5504

    
5505
    if (unlikely(!ctx->supervisor)) {
5506
        GEN_EXCP_PRIVREG(ctx);
5507
        return;
5508
    }
5509
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5510
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5511
    gen_op_store_dcr();
5512
#endif
5513
}
5514

    
5515
/* mfdcrx */
5516
/* XXX: not implemented on 440 ? */
5517
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5518
{
5519
#if defined(CONFIG_USER_ONLY)
5520
    GEN_EXCP_PRIVREG(ctx);
5521
#else
5522
    if (unlikely(!ctx->supervisor)) {
5523
        GEN_EXCP_PRIVREG(ctx);
5524
        return;
5525
    }
5526
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5527
    gen_op_load_dcr();
5528
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5529
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5530
#endif
5531
}
5532

    
5533
/* mtdcrx */
5534
/* XXX: not implemented on 440 ? */
5535
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5536
{
5537
#if defined(CONFIG_USER_ONLY)
5538
    GEN_EXCP_PRIVREG(ctx);
5539
#else
5540
    if (unlikely(!ctx->supervisor)) {
5541
        GEN_EXCP_PRIVREG(ctx);
5542
        return;
5543
    }
5544
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5545
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5546
    gen_op_store_dcr();
5547
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5548
#endif
5549
}
5550

    
5551
/* mfdcrux (PPC 460) : user-mode access to DCR */
5552
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5553
{
5554
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5555
    gen_op_load_dcr();
5556
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5557
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5558
}
5559

    
5560
/* mtdcrux (PPC 460) : user-mode access to DCR */
5561
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5562
{
5563
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5564
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5565
    gen_op_store_dcr();
5566
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5567
}
5568

    
5569
/* dccci */
5570
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5571
{
5572
#if defined(CONFIG_USER_ONLY)
5573
    GEN_EXCP_PRIVOPC(ctx);
5574
#else
5575
    if (unlikely(!ctx->supervisor)) {
5576
        GEN_EXCP_PRIVOPC(ctx);
5577
        return;
5578
    }
5579
    /* interpreted as no-op */
5580
#endif
5581
}
5582

    
5583
/* dcread */
5584
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5585
{
5586
#if defined(CONFIG_USER_ONLY)
5587
    GEN_EXCP_PRIVOPC(ctx);
5588
#else
5589
    TCGv EA, val;
5590
    if (unlikely(!ctx->supervisor)) {
5591
        GEN_EXCP_PRIVOPC(ctx);
5592
        return;
5593
    }
5594
    EA = tcg_temp_new();
5595
    gen_set_access_type(ACCESS_CACHE);
5596
    gen_addr_reg_index(EA, ctx);
5597
    val = tcg_temp_new();
5598
    gen_qemu_ld32u(val, EA, ctx->mem_idx);
5599
    tcg_temp_free(val);
5600
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5601
    tcg_temp_free(EA);
5602
#endif
5603
}
5604

    
5605
/* icbt */
5606
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5607
{
5608
    /* interpreted as no-op */
5609
    /* XXX: specification say this is treated as a load by the MMU
5610
     *      but does not generate any exception
5611
     */
5612
}
5613

    
5614
/* iccci */
5615
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5616
{
5617
#if defined(CONFIG_USER_ONLY)
5618
    GEN_EXCP_PRIVOPC(ctx);
5619
#else
5620
    if (unlikely(!ctx->supervisor)) {
5621
        GEN_EXCP_PRIVOPC(ctx);
5622
        return;
5623
    }
5624
    /* interpreted as no-op */
5625
#endif
5626
}
5627

    
5628
/* icread */
5629
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5630
{
5631
#if defined(CONFIG_USER_ONLY)
5632
    GEN_EXCP_PRIVOPC(ctx);
5633
#else
5634
    if (unlikely(!ctx->supervisor)) {
5635
        GEN_EXCP_PRIVOPC(ctx);
5636
        return;
5637
    }
5638
    /* interpreted as no-op */
5639
#endif
5640
}
5641

    
5642
/* rfci (supervisor only) */
5643
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5644
{
5645
#if defined(CONFIG_USER_ONLY)
5646
    GEN_EXCP_PRIVOPC(ctx);
5647
#else
5648
    if (unlikely(!ctx->supervisor)) {
5649
        GEN_EXCP_PRIVOPC(ctx);
5650
        return;
5651
    }
5652
    /* Restore CPU state */
5653
    gen_op_40x_rfci();
5654
    GEN_SYNC(ctx);
5655
#endif
5656
}
5657

    
5658
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5659
{
5660
#if defined(CONFIG_USER_ONLY)
5661
    GEN_EXCP_PRIVOPC(ctx);
5662
#else
5663
    if (unlikely(!ctx->supervisor)) {
5664
        GEN_EXCP_PRIVOPC(ctx);
5665
        return;
5666
    }
5667
    /* Restore CPU state */
5668
    gen_op_rfci();
5669
    GEN_SYNC(ctx);
5670
#endif
5671
}
5672

    
5673
/* BookE specific */
5674
/* XXX: not implemented on 440 ? */
5675
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5676
{
5677
#if defined(CONFIG_USER_ONLY)
5678
    GEN_EXCP_PRIVOPC(ctx);
5679
#else
5680
    if (unlikely(!ctx->supervisor)) {
5681
        GEN_EXCP_PRIVOPC(ctx);
5682
        return;
5683
    }
5684
    /* Restore CPU state */
5685
    gen_op_rfdi();
5686
    GEN_SYNC(ctx);
5687
#endif
5688
}
5689

    
5690
/* XXX: not implemented on 440 ? */
5691
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5692
{
5693
#if defined(CONFIG_USER_ONLY)
5694
    GEN_EXCP_PRIVOPC(ctx);
5695
#else
5696
    if (unlikely(!ctx->supervisor)) {
5697
        GEN_EXCP_PRIVOPC(ctx);
5698
        return;
5699
    }
5700
    /* Restore CPU state */
5701
    gen_op_rfmci();
5702
    GEN_SYNC(ctx);
5703
#endif
5704
}
5705

    
5706
/* TLB management - PowerPC 405 implementation */
5707
/* tlbre */
5708
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5709
{
5710
#if defined(CONFIG_USER_ONLY)
5711
    GEN_EXCP_PRIVOPC(ctx);
5712
#else
5713
    if (unlikely(!ctx->supervisor)) {
5714
        GEN_EXCP_PRIVOPC(ctx);
5715
        return;
5716
    }
5717
    switch (rB(ctx->opcode)) {
5718
    case 0:
5719
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5720
        gen_op_4xx_tlbre_hi();
5721
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5722
        break;
5723
    case 1:
5724
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5725
        gen_op_4xx_tlbre_lo();
5726
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5727
        break;
5728
    default:
5729
        GEN_EXCP_INVAL(ctx);
5730
        break;
5731
    }
5732
#endif
5733
}
5734

    
5735
/* tlbsx - tlbsx. */
5736
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5737
{
5738
#if defined(CONFIG_USER_ONLY)
5739
    GEN_EXCP_PRIVOPC(ctx);
5740
#else
5741
    if (unlikely(!ctx->supervisor)) {
5742
        GEN_EXCP_PRIVOPC(ctx);
5743
        return;
5744
    }
5745
    gen_addr_reg_index(cpu_T[0], ctx);
5746
    gen_op_4xx_tlbsx();
5747
    if (Rc(ctx->opcode))
5748
        gen_op_4xx_tlbsx_check();
5749
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5750
#endif
5751
}
5752

    
5753
/* tlbwe */
5754
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5755
{
5756
#if defined(CONFIG_USER_ONLY)
5757
    GEN_EXCP_PRIVOPC(ctx);
5758
#else
5759
    if (unlikely(!ctx->supervisor)) {
5760
        GEN_EXCP_PRIVOPC(ctx);
5761
        return;
5762
    }
5763
    switch (rB(ctx->opcode)) {
5764
    case 0:
5765
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5766
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5767
        gen_op_4xx_tlbwe_hi();
5768
        break;
5769
    case 1:
5770
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5771
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5772
        gen_op_4xx_tlbwe_lo();
5773
        break;
5774
    default:
5775
        GEN_EXCP_INVAL(ctx);
5776
        break;
5777
    }
5778
#endif
5779
}
5780

    
5781
/* TLB management - PowerPC 440 implementation */
5782
/* tlbre */
5783
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5784
{
5785
#if defined(CONFIG_USER_ONLY)
5786
    GEN_EXCP_PRIVOPC(ctx);
5787
#else
5788
    if (unlikely(!ctx->supervisor)) {
5789
        GEN_EXCP_PRIVOPC(ctx);
5790
        return;
5791
    }
5792
    switch (rB(ctx->opcode)) {
5793
    case 0:
5794
    case 1:
5795
    case 2:
5796
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5797
        gen_op_440_tlbre(rB(ctx->opcode));
5798
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5799
        break;
5800
    default:
5801
        GEN_EXCP_INVAL(ctx);
5802
        break;
5803
    }
5804
#endif
5805
}
5806

    
5807
/* tlbsx - tlbsx. */
5808
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5809
{
5810
#if defined(CONFIG_USER_ONLY)
5811
    GEN_EXCP_PRIVOPC(ctx);
5812
#else
5813
    if (unlikely(!ctx->supervisor)) {
5814
        GEN_EXCP_PRIVOPC(ctx);
5815
        return;
5816
    }
5817
    gen_addr_reg_index(cpu_T[0], ctx);
5818
    gen_op_440_tlbsx();
5819
    if (Rc(ctx->opcode))
5820
        gen_op_4xx_tlbsx_check();
5821
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5822
#endif
5823
}
5824

    
5825
/* tlbwe */
5826
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5827
{
5828
#if defined(CONFIG_USER_ONLY)
5829
    GEN_EXCP_PRIVOPC(ctx);
5830
#else
5831
    if (unlikely(!ctx->supervisor)) {
5832
        GEN_EXCP_PRIVOPC(ctx);
5833
        return;
5834
    }
5835
    switch (rB(ctx->opcode)) {
5836
    case 0:
5837
    case 1:
5838
    case 2:
5839
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5840
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5841
        gen_op_440_tlbwe(rB(ctx->opcode));
5842
        break;
5843
    default:
5844
        GEN_EXCP_INVAL(ctx);
5845
        break;
5846
    }
5847
#endif
5848
}
5849

    
5850
/* wrtee */
5851
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5852
{
5853
#if defined(CONFIG_USER_ONLY)
5854
    GEN_EXCP_PRIVOPC(ctx);
5855
#else
5856
    if (unlikely(!ctx->supervisor)) {
5857
        GEN_EXCP_PRIVOPC(ctx);
5858
        return;
5859
    }
5860
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5861
    gen_op_wrte();
5862
    /* Stop translation to have a chance to raise an exception
5863
     * if we just set msr_ee to 1
5864
     */
5865
    GEN_STOP(ctx);
5866
#endif
5867
}
5868

    
5869
/* wrteei */
5870
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5871
{
5872
#if defined(CONFIG_USER_ONLY)
5873
    GEN_EXCP_PRIVOPC(ctx);
5874
#else
5875
    if (unlikely(!ctx->supervisor)) {
5876
        GEN_EXCP_PRIVOPC(ctx);
5877
        return;
5878
    }
5879
    tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5880
    gen_op_wrte();
5881
    /* Stop translation to have a chance to raise an exception
5882
     * if we just set msr_ee to 1
5883
     */
5884
    GEN_STOP(ctx);
5885
#endif
5886
}
5887

    
5888
/* PowerPC 440 specific instructions */
5889
/* dlmzb */
5890
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5891
{
5892
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
5893
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5894
    gen_op_440_dlmzb();
5895
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
5896
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5897
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
5898
    if (Rc(ctx->opcode)) {
5899
        gen_op_440_dlmzb_update_Rc();
5900
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_T[0]);
5901
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 0xf);
5902
    }
5903
}
5904

    
5905
/* mbar replaces eieio on 440 */
5906
GEN_HANDLER(mbar, 0x1F, 0x16, 0x13, 0x001FF801, PPC_BOOKE)
5907
{
5908
    /* interpreted as no-op */
5909
}
5910

    
5911
/* msync replaces sync on 440 */
5912
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5913
{
5914
    /* interpreted as no-op */
5915
}
5916

    
5917
/* icbt */
5918
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5919
{
5920
    /* interpreted as no-op */
5921
    /* XXX: specification say this is treated as a load by the MMU
5922
     *      but does not generate any exception
5923
     */
5924
}
5925

    
5926
/***                      Altivec vector extension                         ***/
5927
/* Altivec registers moves */
5928

    
5929
#define GEN_VR_LDX(name, opc2, opc3)                                          \
5930
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
5931
{                                                                             \
5932
    TCGv EA;                                                                  \
5933
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5934
        GEN_EXCP_NO_VR(ctx);                                                  \
5935
        return;                                                               \
5936
    }                                                                         \
5937
    EA = tcg_temp_new();                                                      \
5938
    gen_addr_reg_index(EA, ctx);                                              \
5939
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
5940
    if (ctx->mem_idx & 1) {                                                   \
5941
        gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5942
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5943
        gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5944
    } else {                                                                  \
5945
        gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5946
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5947
        gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5948
    }                                                                         \
5949
    tcg_temp_free(EA);                                                        \
5950
}
5951

    
5952
#define GEN_VR_STX(name, opc2, opc3)                                          \
5953
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
5954
{                                                                             \
5955
    TCGv EA;                                                                  \
5956
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5957
        GEN_EXCP_NO_VR(ctx);                                                  \
5958
        return;                                                               \
5959
    }                                                                         \
5960
    EA = tcg_temp_new();                                                      \
5961
    gen_addr_reg_index(EA, ctx);                                              \
5962
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
5963
    if (ctx->mem_idx & 1) {                                                   \
5964
        gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5965
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5966
        gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5967
    } else {                                                                  \
5968
        gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5969
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5970
        gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5971
    }                                                                         \
5972
    tcg_temp_free(EA);                                                        \
5973
}
5974

    
5975
GEN_VR_LDX(lvx, 0x07, 0x03);
5976
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
5977
GEN_VR_LDX(lvxl, 0x07, 0x0B);
5978

    
5979
GEN_VR_STX(svx, 0x07, 0x07);
5980
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
5981
GEN_VR_STX(svxl, 0x07, 0x0F);
5982

    
5983
/***                           SPE extension                               ***/
5984
/* Register moves */
5985

    
5986
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
5987
#if defined(TARGET_PPC64)
5988
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
5989
#else
5990
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
5991
#endif
5992
}
5993

    
5994
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
5995
#if defined(TARGET_PPC64)
5996
    tcg_gen_mov_i64(cpu_gpr[reg], t);
5997
#else
5998
    TCGv_i64 tmp = tcg_temp_new_i64();
5999
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6000
    tcg_gen_shri_i64(tmp, t, 32);
6001
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6002
    tcg_temp_free_i64(tmp);
6003
#endif
6004
}
6005

    
6006
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
6007
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
6008
{                                                                             \
6009
    if (Rc(ctx->opcode))                                                      \
6010
        gen_##name1(ctx);                                                     \
6011
    else                                                                      \
6012
        gen_##name0(ctx);                                                     \
6013
}
6014

    
6015
/* Handler for undefined SPE opcodes */
6016
static always_inline void gen_speundef (DisasContext *ctx)
6017
{
6018
    GEN_EXCP_INVAL(ctx);
6019
}
6020

    
6021
/* SPE logic */
6022
#if defined(TARGET_PPC64)
6023
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6024
static always_inline void gen_##name (DisasContext *ctx)                      \
6025
{                                                                             \
6026
    if (unlikely(!ctx->spe_enabled)) {                                        \
6027
        GEN_EXCP_NO_AP(ctx);                                                  \
6028
        return;                                                               \
6029
    }                                                                         \
6030
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6031
           cpu_gpr[rB(ctx->opcode)]);                                         \
6032
}
6033
#else
6034
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6035
static always_inline void gen_##name (DisasContext *ctx)                      \
6036
{                                                                             \
6037
    if (unlikely(!ctx->spe_enabled)) {                                        \
6038
        GEN_EXCP_NO_AP(ctx);                                                  \
6039
        return;                                                               \
6040
    }                                                                         \
6041
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6042
           cpu_gpr[rB(ctx->opcode)]);                                         \
6043
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6044
           cpu_gprh[rB(ctx->opcode)]);                                        \
6045
}
6046
#endif
6047

    
6048
GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6049
GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6050
GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6051
GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6052
GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6053
GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6054
GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6055
GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6056

    
6057
/* SPE logic immediate */
6058
#if defined(TARGET_PPC64)
6059
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6060
static always_inline void gen_##name (DisasContext *ctx)                      \
6061
{                                                                             \
6062
    if (unlikely(!ctx->spe_enabled)) {                                        \
6063
        GEN_EXCP_NO_AP(ctx);                                                  \
6064
        return;                                                               \
6065
    }                                                                         \
6066
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6067
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6068
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6069
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6070
    tcg_opi(t0, t0, rB(ctx->opcode));                                         \
6071
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6072
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6073
    tcg_temp_free_i64(t2);                                                    \
6074
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
6075
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6076
    tcg_temp_free_i32(t0);                                                    \
6077
    tcg_temp_free_i32(t1);                                                    \
6078
}
6079
#else
6080
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6081
static always_inline void gen_##name (DisasContext *ctx)                      \
6082
{                                                                             \
6083
    if (unlikely(!ctx->spe_enabled)) {                                        \
6084
        GEN_EXCP_NO_AP(ctx);                                                  \
6085
        return;                                                               \
6086
    }                                                                         \
6087
    tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
6088
            rB(ctx->opcode));                                                 \
6089
    tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
6090
            rB(ctx->opcode));                                                 \
6091
}
6092
#endif
6093
GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6094
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6095
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6096
GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6097

    
6098
/* SPE arithmetic */
6099
#if defined(TARGET_PPC64)
6100
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6101
static always_inline void gen_##name (DisasContext *ctx)                      \
6102
{                                                                             \
6103
    if (unlikely(!ctx->spe_enabled)) {                                        \
6104
        GEN_EXCP_NO_AP(ctx);                                                  \
6105
        return;                                                               \
6106
    }                                                                         \
6107
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6108
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6109
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6110
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6111
    tcg_op(t0, t0);                                                           \
6112
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6113
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6114
    tcg_temp_free_i64(t2);                                                    \
6115
    tcg_op(t1, t1);                                                           \
6116
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6117
    tcg_temp_free_i32(t0);                                                    \
6118
    tcg_temp_free_i32(t1);                                                    \
6119
}
6120
#else
6121
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6122
static always_inline void gen_##name (DisasContext *ctx)                      \
6123
{                                                                             \
6124
    if (unlikely(!ctx->spe_enabled)) {                                        \
6125
        GEN_EXCP_NO_AP(ctx);                                                  \
6126
        return;                                                               \
6127
    }                                                                         \
6128
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
6129
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
6130
}
6131
#endif
6132

    
6133
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6134
{
6135
    int l1 = gen_new_label();
6136
    int l2 = gen_new_label();
6137

    
6138
    tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6139
    tcg_gen_neg_i32(ret, arg1);
6140
    tcg_gen_br(l2);
6141
    gen_set_label(l1);
6142
    tcg_gen_mov_i32(ret, arg1);
6143
    gen_set_label(l2);
6144
}
6145
GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6146
GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6147
GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6148
GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6149
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6150
{
6151
    tcg_gen_addi_i32(ret, arg1, 0x8000);
6152
    tcg_gen_ext16u_i32(ret, ret);
6153
}
6154
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6155
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6156
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6157

    
6158
#if defined(TARGET_PPC64)
6159
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6160
static always_inline void gen_##name (DisasContext *ctx)                      \
6161
{                                                                             \
6162
    if (unlikely(!ctx->spe_enabled)) {                                        \
6163
        GEN_EXCP_NO_AP(ctx);                                                  \
6164
        return;                                                               \
6165
    }                                                                         \
6166
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6167
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6168
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6169
    TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64);                           \
6170
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6171
    tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
6172
    tcg_op(t0, t0, t2);                                                       \
6173
    tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
6174
    tcg_gen_trunc_i64_i32(t1, t3);                                            \
6175
    tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
6176
    tcg_gen_trunc_i64_i32(t2, t3);                                            \
6177
    tcg_temp_free_i64(t3);                                                    \
6178
    tcg_op(t1, t1, t2);                                                       \
6179
    tcg_temp_free_i32(t2);                                                    \
6180
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6181
    tcg_temp_free_i32(t0);                                                    \
6182
    tcg_temp_free_i32(t1);                                                    \
6183
}
6184
#else
6185
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6186
static always_inline void gen_##name (DisasContext *ctx)                      \
6187
{                                                                             \
6188
    if (unlikely(!ctx->spe_enabled)) {                                        \
6189
        GEN_EXCP_NO_AP(ctx);                                                  \
6190
        return;                                                               \
6191
    }                                                                         \
6192
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6193
           cpu_gpr[rB(ctx->opcode)]);                                         \
6194
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6195
           cpu_gprh[rB(ctx->opcode)]);                                        \
6196
}
6197
#endif
6198

    
6199
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6200
{
6201
    TCGv_i32 t0;
6202
    int l1, l2;
6203

    
6204
    l1 = gen_new_label();
6205
    l2 = gen_new_label();
6206
    t0 = tcg_temp_local_new_i32();
6207
    /* No error here: 6 bits are used */
6208
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6209
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6210
    tcg_gen_shr_i32(ret, arg1, t0);
6211
    tcg_gen_br(l2);
6212
    gen_set_label(l1);
6213
    tcg_gen_movi_i32(ret, 0);
6214
    tcg_gen_br(l2);
6215
    tcg_temp_free_i32(t0);
6216
}
6217
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6218
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6219
{
6220
    TCGv_i32 t0;
6221
    int l1, l2;
6222

    
6223
    l1 = gen_new_label();
6224
    l2 = gen_new_label();
6225
    t0 = tcg_temp_local_new_i32();
6226
    /* No error here: 6 bits are used */
6227
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6228
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6229
    tcg_gen_sar_i32(ret, arg1, t0);
6230
    tcg_gen_br(l2);
6231
    gen_set_label(l1);
6232
    tcg_gen_movi_i32(ret, 0);
6233
    tcg_gen_br(l2);
6234
    tcg_temp_free_i32(t0);
6235
}
6236
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6237
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6238
{
6239
    TCGv_i32 t0;
6240
    int l1, l2;
6241

    
6242
    l1 = gen_new_label();
6243
    l2 = gen_new_label();
6244
    t0 = tcg_temp_local_new_i32();
6245
    /* No error here: 6 bits are used */
6246
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6247
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6248
    tcg_gen_shl_i32(ret, arg1, t0);
6249
    tcg_gen_br(l2);
6250
    gen_set_label(l1);
6251
    tcg_gen_movi_i32(ret, 0);
6252
    tcg_gen_br(l2);
6253
    tcg_temp_free_i32(t0);
6254
}
6255
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6256
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6257
{
6258
    TCGv_i32 t0 = tcg_temp_new_i32();
6259
    tcg_gen_andi_i32(t0, arg2, 0x1F);
6260
    tcg_gen_rotl_i32(ret, arg1, t0);
6261
    tcg_temp_free_i32(t0);
6262
}
6263
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6264
static always_inline void gen_evmergehi (DisasContext *ctx)
6265
{
6266
    if (unlikely(!ctx->spe_enabled)) {
6267
        GEN_EXCP_NO_AP(ctx);
6268
        return;
6269
    }
6270
#if defined(TARGET_PPC64)
6271
    TCGv t0 = tcg_temp_new();
6272
    TCGv t1 = tcg_temp_new();
6273
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6274
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6275
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6276
    tcg_temp_free(t0);
6277
    tcg_temp_free(t1);
6278
#else
6279
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6280
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6281
#endif
6282
}
6283
GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6284
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6285
{
6286
    tcg_gen_sub_i32(ret, arg2, arg1);
6287
}
6288
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6289

    
6290
/* SPE arithmetic immediate */
6291
#if defined(TARGET_PPC64)
6292
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6293
static always_inline void gen_##name (DisasContext *ctx)                      \
6294
{                                                                             \
6295
    if (unlikely(!ctx->spe_enabled)) {                                        \
6296
        GEN_EXCP_NO_AP(ctx);                                                  \
6297
        return;                                                               \
6298
    }                                                                         \
6299
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6300
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6301
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6302
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
6303
    tcg_op(t0, t0, rA(ctx->opcode));                                          \
6304
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6305
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6306
    tcg_temp_free_i64(t2);                                                        \
6307
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
6308
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6309
    tcg_temp_free_i32(t0);                                                    \
6310
    tcg_temp_free_i32(t1);                                                    \
6311
}
6312
#else
6313
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6314
static always_inline void gen_##name (DisasContext *ctx)                      \
6315
{                                                                             \
6316
    if (unlikely(!ctx->spe_enabled)) {                                        \
6317
        GEN_EXCP_NO_AP(ctx);                                                  \
6318
        return;                                                               \
6319
    }                                                                         \
6320
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
6321
           rA(ctx->opcode));                                                  \
6322
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
6323
           rA(ctx->opcode));                                                  \
6324
}
6325
#endif
6326
GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6327
GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6328

    
6329
/* SPE comparison */
6330
#if defined(TARGET_PPC64)
6331
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
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
    int l1 = gen_new_label();                                                 \
6339
    int l2 = gen_new_label();                                                 \
6340
    int l3 = gen_new_label();                                                 \
6341
    int l4 = gen_new_label();                                                 \
6342
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6343
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6344
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6345
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6346
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
6347
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
6348
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6349
    tcg_gen_br(l2);                                                           \
6350
    gen_set_label(l1);                                                        \
6351
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6352
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6353
    gen_set_label(l2);                                                        \
6354
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6355
    tcg_gen_trunc_i64_i32(t0, t2);                                            \
6356
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6357
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6358
    tcg_temp_free_i64(t2);                                                    \
6359
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
6360
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6361
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6362
    tcg_gen_br(l4);                                                           \
6363
    gen_set_label(l3);                                                        \
6364
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6365
                    CRF_CH | CRF_CH_OR_CL);                                   \
6366
    gen_set_label(l4);                                                        \
6367
    tcg_temp_free_i32(t0);                                                    \
6368
    tcg_temp_free_i32(t1);                                                    \
6369
}
6370
#else
6371
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6372
static always_inline void gen_##name (DisasContext *ctx)                      \
6373
{                                                                             \
6374
    if (unlikely(!ctx->spe_enabled)) {                                        \
6375
        GEN_EXCP_NO_AP(ctx);                                                  \
6376
        return;                                                               \
6377
    }                                                                         \
6378
    int l1 = gen_new_label();                                                 \
6379
    int l2 = gen_new_label();                                                 \
6380
    int l3 = gen_new_label();                                                 \
6381
    int l4 = gen_new_label();                                                 \
6382
                                                                              \
6383
    tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
6384
                       cpu_gpr[rB(ctx->opcode)], l1);                         \
6385
    tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
6386
    tcg_gen_br(l2);                                                           \
6387
    gen_set_label(l1);                                                        \
6388
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6389
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6390
    gen_set_label(l2);                                                        \
6391
    tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
6392
                       cpu_gprh[rB(ctx->opcode)], l3);                        \
6393
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6394
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6395
    tcg_gen_br(l4);                                                           \
6396
    gen_set_label(l3);                                                        \
6397
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6398
                    CRF_CH | CRF_CH_OR_CL);                                   \
6399
    gen_set_label(l4);                                                        \
6400
}
6401
#endif
6402
GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6403
GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6404
GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6405
GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6406
GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6407

    
6408
/* SPE misc */
6409
static always_inline void gen_brinc (DisasContext *ctx)
6410
{
6411
    /* Note: brinc is usable even if SPE is disabled */
6412
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6413
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6414
}
6415
static always_inline void gen_evmergelo (DisasContext *ctx)
6416
{
6417
    if (unlikely(!ctx->spe_enabled)) {
6418
        GEN_EXCP_NO_AP(ctx);
6419
        return;
6420
    }
6421
#if defined(TARGET_PPC64)
6422
    TCGv t0 = tcg_temp_new();
6423
    TCGv t1 = tcg_temp_new();
6424
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6425
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6426
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6427
    tcg_temp_free(t0);
6428
    tcg_temp_free(t1);
6429
#else
6430
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6431
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6432
#endif
6433
}
6434
static always_inline void gen_evmergehilo (DisasContext *ctx)
6435
{
6436
    if (unlikely(!ctx->spe_enabled)) {
6437
        GEN_EXCP_NO_AP(ctx);
6438
        return;
6439
    }
6440
#if defined(TARGET_PPC64)
6441
    TCGv t0 = tcg_temp_new();
6442
    TCGv t1 = tcg_temp_new();
6443
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6444
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6445
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6446
    tcg_temp_free(t0);
6447
    tcg_temp_free(t1);
6448
#else
6449
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6450
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6451
#endif
6452
}
6453
static always_inline void gen_evmergelohi (DisasContext *ctx)
6454
{
6455
    if (unlikely(!ctx->spe_enabled)) {
6456
        GEN_EXCP_NO_AP(ctx);
6457
        return;
6458
    }
6459
#if defined(TARGET_PPC64)
6460
    TCGv t0 = tcg_temp_new();
6461
    TCGv t1 = tcg_temp_new();
6462
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6463
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6464
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6465
    tcg_temp_free(t0);
6466
    tcg_temp_free(t1);
6467
#else
6468
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6469
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6470
#endif
6471
}
6472
static always_inline void gen_evsplati (DisasContext *ctx)
6473
{
6474
    uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6475

    
6476
#if defined(TARGET_PPC64)
6477
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6478
#else
6479
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6480
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6481
#endif
6482
}
6483
static always_inline void gen_evsplatfi (DisasContext *ctx)
6484
{
6485
    uint64_t imm = rA(ctx->opcode) << 11;
6486

    
6487
#if defined(TARGET_PPC64)
6488
    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6489
#else
6490
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6491
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6492
#endif
6493
}
6494

    
6495
static always_inline void gen_evsel (DisasContext *ctx)
6496
{
6497
    int l1 = gen_new_label();
6498
    int l2 = gen_new_label();
6499
    int l3 = gen_new_label();
6500
    int l4 = gen_new_label();
6501
    TCGv_i32 t0 = tcg_temp_local_new_i32();
6502
#if defined(TARGET_PPC64)
6503
    TCGv t1 = tcg_temp_local_new();
6504
    TCGv t2 = tcg_temp_local_new();
6505
#endif
6506
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6507
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6508
#if defined(TARGET_PPC64)
6509
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6510
#else
6511
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6512
#endif
6513
    tcg_gen_br(l2);
6514
    gen_set_label(l1);
6515
#if defined(TARGET_PPC64)
6516
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6517
#else
6518
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6519
#endif
6520
    gen_set_label(l2);
6521
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6522
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6523
#if defined(TARGET_PPC64)
6524
    tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6525
#else
6526
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6527
#endif
6528
    tcg_gen_br(l4);
6529
    gen_set_label(l3);
6530
#if defined(TARGET_PPC64)
6531
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6532
#else
6533
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6534
#endif
6535
    gen_set_label(l4);
6536
    tcg_temp_free_i32(t0);
6537
#if defined(TARGET_PPC64)
6538
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6539
    tcg_temp_free(t1);
6540
    tcg_temp_free(t2);
6541
#endif
6542
}
6543
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6544
{
6545
    gen_evsel(ctx);
6546
}
6547
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6548
{
6549
    gen_evsel(ctx);
6550
}
6551
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6552
{
6553
    gen_evsel(ctx);
6554
}
6555
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6556
{
6557
    gen_evsel(ctx);
6558
}
6559

    
6560
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
6561
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
6562
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
6563
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
6564
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
6565
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
6566
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
6567
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
6568
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
6569
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
6570
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
6571
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
6572
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
6573
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
6574
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
6575
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
6576
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
6577
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
6578
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
6579
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
6580
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
6581
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
6582
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
6583
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
6584
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
6585

    
6586
/* SPE load and stores */
6587
static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
6588
{
6589
    target_ulong uimm = rB(ctx->opcode);
6590

    
6591
    if (rA(ctx->opcode) == 0)
6592
        tcg_gen_movi_tl(EA, uimm << sh);
6593
    else
6594
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
6595
}
6596

    
6597
static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6598
{
6599
#if defined(TARGET_PPC64)
6600
    gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6601
#else
6602
    TCGv_i64 t0 = tcg_temp_new_i64();
6603
    gen_qemu_ld64(t0, addr, ctx->mem_idx);
6604
    tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
6605
    tcg_gen_shri_i64(t0, t0, 32);
6606
    tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
6607
    tcg_temp_free_i64(t0);
6608
#endif
6609
}
6610

    
6611
static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6612
{
6613
#if defined(TARGET_PPC64)
6614
    TCGv t0 = tcg_temp_new();
6615
    gen_qemu_ld32u(t0, addr, ctx->mem_idx);
6616
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6617
    tcg_gen_addi_tl(addr, addr, 4);
6618
    gen_qemu_ld32u(t0, addr, ctx->mem_idx);
6619
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6620
    tcg_temp_free(t0);
6621
#else
6622
    gen_qemu_ld32u(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6623
    tcg_gen_addi_tl(addr, addr, 4);
6624
    gen_qemu_ld32u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6625
#endif
6626
}
6627

    
6628
static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6629
{
6630
    TCGv t0 = tcg_temp_new();
6631
#if defined(TARGET_PPC64)
6632
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6633
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6634
    tcg_gen_addi_tl(addr, addr, 2);
6635
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6636
    tcg_gen_shli_tl(t0, t0, 32);
6637
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6638
    tcg_gen_addi_tl(addr, addr, 2);
6639
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6640
    tcg_gen_shli_tl(t0, t0, 16);
6641
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6642
    tcg_gen_addi_tl(addr, addr, 2);
6643
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6644
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6645
#else
6646
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6647
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6648
    tcg_gen_addi_tl(addr, addr, 2);
6649
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6650
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6651
    tcg_gen_addi_tl(addr, addr, 2);
6652
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6653
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6654
    tcg_gen_addi_tl(addr, addr, 2);
6655
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6656
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6657
#endif
6658
    tcg_temp_free(t0);
6659
}
6660

    
6661
static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6662
{
6663
    TCGv t0 = tcg_temp_new();
6664
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6665
#if defined(TARGET_PPC64)
6666
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6667
    tcg_gen_shli_tl(t0, t0, 16);
6668
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6669
#else
6670
    tcg_gen_shli_tl(t0, t0, 16);
6671
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6672
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6673
#endif
6674
    tcg_temp_free(t0);
6675
}
6676

    
6677
static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6678
{
6679
    TCGv t0 = tcg_temp_new();
6680
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6681
#if defined(TARGET_PPC64)
6682
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6683
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6684
#else
6685
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6686
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6687
#endif
6688
    tcg_temp_free(t0);
6689
}
6690

    
6691
static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6692
{
6693
    TCGv t0 = tcg_temp_new();
6694
    gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6695
#if defined(TARGET_PPC64)
6696
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6697
    tcg_gen_ext32u_tl(t0, t0);
6698
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6699
#else
6700
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6701
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6702
#endif
6703
    tcg_temp_free(t0);
6704
}
6705

    
6706
static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6707
{
6708
    TCGv t0 = tcg_temp_new();
6709
#if defined(TARGET_PPC64)
6710
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6711
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6712
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6713
    tcg_gen_shli_tl(t0, t0, 16);
6714
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6715
#else
6716
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6717
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6718
    tcg_gen_addi_tl(addr, addr, 2);
6719
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6720
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6721
#endif
6722
    tcg_temp_free(t0);
6723
}
6724

    
6725
static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6726
{
6727
#if defined(TARGET_PPC64)
6728
    TCGv t0 = tcg_temp_new();
6729
    gen_qemu_ld16u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6730
    tcg_gen_addi_tl(addr, addr, 2);
6731
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6732
    tcg_gen_shli_tl(t0, t0, 32);
6733
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6734
    tcg_temp_free(t0);
6735
#else
6736
    gen_qemu_ld16u(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6737
    tcg_gen_addi_tl(addr, addr, 2);
6738
    gen_qemu_ld16u(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6739
#endif
6740
}
6741

    
6742
static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6743
{
6744
#if defined(TARGET_PPC64)
6745
    TCGv t0 = tcg_temp_new();
6746
    gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6747
    tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
6748
    tcg_gen_addi_tl(addr, addr, 2);
6749
    gen_qemu_ld16s(t0, addr, ctx->mem_idx);
6750
    tcg_gen_shli_tl(t0, t0, 32);
6751
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6752
    tcg_temp_free(t0);
6753
#else
6754
    gen_qemu_ld16s(cpu_gprh[rD(ctx->opcode)], addr, ctx->mem_idx);
6755
    tcg_gen_addi_tl(addr, addr, 2);
6756
    gen_qemu_ld16s(cpu_gpr[rD(ctx->opcode)], addr, ctx->mem_idx);
6757
#endif
6758
}
6759

    
6760
static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6761
{
6762
    TCGv t0 = tcg_temp_new();
6763
    gen_qemu_ld32u(t0, addr, ctx->mem_idx);
6764
#if defined(TARGET_PPC64)
6765
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6766
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6767
#else
6768
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6769
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6770
#endif
6771
    tcg_temp_free(t0);
6772
}
6773

    
6774
static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6775
{
6776
    TCGv t0 = tcg_temp_new();
6777
#if defined(TARGET_PPC64)
6778
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6779
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6780
    tcg_gen_shli_tl(t0, t0, 32);
6781
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6782
    tcg_gen_addi_tl(addr, addr, 2);
6783
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6784
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6785
    tcg_gen_shli_tl(t0, t0, 16);
6786
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6787
#else
6788
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6789
    tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6790
    tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6791
    tcg_gen_addi_tl(addr, addr, 2);
6792
    gen_qemu_ld16u(t0, addr, ctx->mem_idx);
6793
    tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6794
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6795
#endif
6796
    tcg_temp_free(t0);
6797
}
6798

    
6799
static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6800
{
6801
#if defined(TARGET_PPC64)
6802
    gen_qemu_st64(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6803
#else
6804
    TCGv_i64 t0 = tcg_temp_new_i64();
6805
    tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
6806
    gen_qemu_st64(t0, addr, ctx->mem_idx);
6807
    tcg_temp_free_i64(t0);
6808
#endif
6809
}
6810

    
6811
static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6812
{
6813
#if defined(TARGET_PPC64)
6814
    TCGv t0 = tcg_temp_new();
6815
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6816
    gen_qemu_st32(t0, addr, ctx->mem_idx);
6817
    tcg_temp_free(t0);
6818
#else
6819
    gen_qemu_st32(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6820
#endif
6821
    tcg_gen_addi_tl(addr, addr, 4);
6822
    gen_qemu_st32(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6823
}
6824

    
6825
static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6826
{
6827
    TCGv t0 = tcg_temp_new();
6828
#if defined(TARGET_PPC64)
6829
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6830
#else
6831
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6832
#endif
6833
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6834
    tcg_gen_addi_tl(addr, addr, 2);
6835
#if defined(TARGET_PPC64)
6836
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6837
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6838
#else
6839
    gen_qemu_st16(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6840
#endif
6841
    tcg_gen_addi_tl(addr, addr, 2);
6842
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
6843
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6844
    tcg_temp_free(t0);
6845
    tcg_gen_addi_tl(addr, addr, 2);
6846
    gen_qemu_st16(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6847
}
6848

    
6849
static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
6850
{
6851
    TCGv t0 = tcg_temp_new();
6852
#if defined(TARGET_PPC64)
6853
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6854
#else
6855
    tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6856
#endif
6857
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6858
    tcg_gen_addi_tl(addr, addr, 2);
6859
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
6860
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6861
    tcg_temp_free(t0);
6862
}
6863

    
6864
static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
6865
{
6866
#if defined(TARGET_PPC64)
6867
    TCGv t0 = tcg_temp_new();
6868
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6869
    gen_qemu_st16(t0, addr, ctx->mem_idx);
6870
    tcg_temp_free(t0);
6871
#else
6872
    gen_qemu_st16(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6873
#endif
6874
    tcg_gen_addi_tl(addr, addr, 2);
6875
    gen_qemu_st16(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6876
}
6877

    
6878
static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
6879
{
6880
#if defined(TARGET_PPC64)
6881
    TCGv t0 = tcg_temp_new();
6882
    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6883
    gen_qemu_st32(t0, addr, ctx->mem_idx);
6884
    tcg_temp_free(t0);
6885
#else
6886
    gen_qemu_st32(cpu_gprh[rS(ctx->opcode)], addr, ctx->mem_idx);
6887
#endif
6888
}
6889

    
6890
static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
6891
{
6892
    gen_qemu_st32(cpu_gpr[rS(ctx->opcode)], addr, ctx->mem_idx);
6893
}
6894

    
6895
#define GEN_SPEOP_LDST(name, opc2, sh)                                        \
6896
GEN_HANDLER(gen_##name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                \
6897
{                                                                             \
6898
    TCGv t0;                                                                  \
6899
    if (unlikely(!ctx->spe_enabled)) {                                        \
6900
        GEN_EXCP_NO_AP(ctx);                                                  \
6901
        return;                                                               \
6902
    }                                                                         \
6903
    t0 = tcg_temp_new();                                                      \
6904
    if (Rc(ctx->opcode)) {                                                    \
6905
        gen_addr_spe_imm_index(t0, ctx, sh);                                  \
6906
    } else {                                                                  \
6907
        gen_addr_reg_index(t0, ctx);                                          \
6908
    }                                                                         \
6909
    gen_op_##name(ctx, t0);                                                   \
6910
    tcg_temp_free(t0);                                                        \
6911
}
6912

    
6913
GEN_SPEOP_LDST(evldd, 0x00, 3);
6914
GEN_SPEOP_LDST(evldw, 0x01, 3);
6915
GEN_SPEOP_LDST(evldh, 0x02, 3);
6916
GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
6917
GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
6918
GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
6919
GEN_SPEOP_LDST(evlwhe, 0x08, 2);
6920
GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
6921
GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
6922
GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
6923
GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
6924

    
6925
GEN_SPEOP_LDST(evstdd, 0x10, 3);
6926
GEN_SPEOP_LDST(evstdw, 0x11, 3);
6927
GEN_SPEOP_LDST(evstdh, 0x12, 3);
6928
GEN_SPEOP_LDST(evstwhe, 0x18, 2);
6929
GEN_SPEOP_LDST(evstwho, 0x1A, 2);
6930
GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
6931
GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
6932

    
6933
/* Multiply and add - TODO */
6934
#if 0
6935
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
6936
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
6937
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
6938
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
6939
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
6940
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
6941
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
6942
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
6943
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
6944
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
6945
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
6946
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
6947

6948
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
6949
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
6950
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
6951
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
6952
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
6953
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
6954
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
6955
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
6956
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
6957
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
6958
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
6959
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
6960
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
6961
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
6962

6963
GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, PPC_SPE);
6964
GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, PPC_SPE);
6965
GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, PPC_SPE);
6966
GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, PPC_SPE);
6967
GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, PPC_SPE);
6968
GEN_SPE(evmra,          speundef,      0x07, 0x13, 0x0000F800, PPC_SPE);
6969

6970
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
6971
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
6972
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
6973
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
6974
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
6975
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
6976
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
6977
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
6978
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
6979
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
6980
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
6981
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
6982

6983
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
6984
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
6985
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
6986
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
6987
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
6988

6989
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
6990
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
6991
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
6992
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
6993
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
6994
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
6995
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
6996
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
6997
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
6998
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
6999
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
7000
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
7001

7002
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
7003
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
7004
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
7005
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
7006
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
7007
#endif
7008

    
7009
/***                      SPE floating-point extension                     ***/
7010
#if defined(TARGET_PPC64)
7011
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7012
static always_inline void gen_##name (DisasContext *ctx)                      \
7013
{                                                                             \
7014
    TCGv_i32 t0;                                                              \
7015
    TCGv t1;                                                                  \
7016
    t0 = tcg_temp_new_i32();                                                  \
7017
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7018
    gen_helper_##name(t0, t0);                                                \
7019
    t1 = tcg_temp_new();                                                      \
7020
    tcg_gen_extu_i32_tl(t1, t0);                                              \
7021
    tcg_temp_free_i32(t0);                                                    \
7022
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7023
                    0xFFFFFFFF00000000ULL);                                   \
7024
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7025
    tcg_temp_free(t1);                                                        \
7026
}
7027
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7028
static always_inline void gen_##name (DisasContext *ctx)                      \
7029
{                                                                             \
7030
    TCGv_i32 t0;                                                              \
7031
    TCGv t1;                                                                  \
7032
    t0 = tcg_temp_new_i32();                                                  \
7033
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7034
    t1 = tcg_temp_new();                                                      \
7035
    tcg_gen_extu_i32_tl(t1, t0);                                              \
7036
    tcg_temp_free_i32(t0);                                                    \
7037
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7038
                    0xFFFFFFFF00000000ULL);                                   \
7039
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7040
    tcg_temp_free(t1);                                                        \
7041
}
7042
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7043
static always_inline void gen_##name (DisasContext *ctx)                      \
7044
{                                                                             \
7045
    TCGv_i32 t0 = tcg_temp_new_i32();                                         \
7046
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7047
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7048
    tcg_temp_free_i32(t0);                                                    \
7049
}
7050
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7051
static always_inline void gen_##name (DisasContext *ctx)                      \
7052
{                                                                             \
7053
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7054
}
7055
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7056
static always_inline void gen_##name (DisasContext *ctx)                      \
7057
{                                                                             \
7058
    TCGv_i32 t0, t1;                                                          \
7059
    TCGv_i64 t2;                                                              \
7060
    if (unlikely(!ctx->spe_enabled)) {                                        \
7061
        GEN_EXCP_NO_AP(ctx);                                                  \
7062
        return;                                                               \
7063
    }                                                                         \
7064
    t0 = tcg_temp_new_i32();                                                  \
7065
    t1 = tcg_temp_new_i32();                                                  \
7066
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7067
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7068
    gen_helper_##name(t0, t0, t1);                                            \
7069
    tcg_temp_free_i32(t1);                                                    \
7070
    t2 = tcg_temp_new();                                                      \
7071
    tcg_gen_extu_i32_tl(t2, t0);                                              \
7072
    tcg_temp_free_i32(t0);                                                    \
7073
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7074
                    0xFFFFFFFF00000000ULL);                                   \
7075
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
7076
    tcg_temp_free(t2);                                                        \
7077
}
7078
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7079
static always_inline void gen_##name (DisasContext *ctx)                      \
7080
{                                                                             \
7081
    if (unlikely(!ctx->spe_enabled)) {                                        \
7082
        GEN_EXCP_NO_AP(ctx);                                                  \
7083
        return;                                                               \
7084
    }                                                                         \
7085
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
7086
                      cpu_gpr[rB(ctx->opcode)]);                              \
7087
}
7088
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7089
static always_inline void gen_##name (DisasContext *ctx)                      \
7090
{                                                                             \
7091
    TCGv_i32 t0, t1;                                                          \
7092
    if (unlikely(!ctx->spe_enabled)) {                                        \
7093
        GEN_EXCP_NO_AP(ctx);                                                  \
7094
        return;                                                               \
7095
    }                                                                         \
7096
    t0 = tcg_temp_new_i32();                                                  \
7097
    t1 = tcg_temp_new_i32();                                                  \
7098
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7099
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7100
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7101
    tcg_temp_free_i32(t0);                                                    \
7102
    tcg_temp_free_i32(t1);                                                    \
7103
}
7104
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7105
static always_inline void gen_##name (DisasContext *ctx)                      \
7106
{                                                                             \
7107
    if (unlikely(!ctx->spe_enabled)) {                                        \
7108
        GEN_EXCP_NO_AP(ctx);                                                  \
7109
        return;                                                               \
7110
    }                                                                         \
7111
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7112
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7113
}
7114
#else
7115
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7116
static always_inline void gen_##name (DisasContext *ctx)                      \
7117
{                                                                             \
7118
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7119
}
7120
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7121
static always_inline void gen_##name (DisasContext *ctx)                      \
7122
{                                                                             \
7123
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7124
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7125
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7126
    tcg_temp_free_i64(t0);                                                    \
7127
}
7128
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7129
static always_inline void gen_##name (DisasContext *ctx)                      \
7130
{                                                                             \
7131
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7132
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7133
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7134
    tcg_temp_free_i64(t0);                                                    \
7135
}
7136
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7137
static always_inline void gen_##name (DisasContext *ctx)                      \
7138
{                                                                             \
7139
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7140
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7141
    gen_helper_##name(t0, t0);                                                \
7142
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7143
    tcg_temp_free_i64(t0);                                                    \
7144
}
7145
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7146
static always_inline void gen_##name (DisasContext *ctx)                      \
7147
{                                                                             \
7148
    if (unlikely(!ctx->spe_enabled)) {                                        \
7149
        GEN_EXCP_NO_AP(ctx);                                                  \
7150
        return;                                                               \
7151
    }                                                                         \
7152
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
7153
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7154
}
7155
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7156
static always_inline void gen_##name (DisasContext *ctx)                      \
7157
{                                                                             \
7158
    TCGv_i64 t0, t1;                                                          \
7159
    if (unlikely(!ctx->spe_enabled)) {                                        \
7160
        GEN_EXCP_NO_AP(ctx);                                                  \
7161
        return;                                                               \
7162
    }                                                                         \
7163
    t0 = tcg_temp_new_i64();                                                  \
7164
    t1 = tcg_temp_new_i64();                                                  \
7165
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7166
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7167
    gen_helper_##name(t0, t0, t1);                                            \
7168
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7169
    tcg_temp_free_i64(t0);                                                    \
7170
    tcg_temp_free_i64(t1);                                                    \
7171
}
7172
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7173
static always_inline void gen_##name (DisasContext *ctx)                      \
7174
{                                                                             \
7175
    if (unlikely(!ctx->spe_enabled)) {                                        \
7176
        GEN_EXCP_NO_AP(ctx);                                                  \
7177
        return;                                                               \
7178
    }                                                                         \
7179
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7180
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7181
}
7182
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7183
static always_inline void gen_##name (DisasContext *ctx)                      \
7184
{                                                                             \
7185
    TCGv_i64 t0, t1;                                                          \
7186
    if (unlikely(!ctx->spe_enabled)) {                                        \
7187
        GEN_EXCP_NO_AP(ctx);                                                  \
7188
        return;                                                               \
7189
    }                                                                         \
7190
    t0 = tcg_temp_new_i64();                                                  \
7191
    t1 = tcg_temp_new_i64();                                                  \
7192
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7193
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7194
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7195
    tcg_temp_free_i64(t0);                                                    \
7196
    tcg_temp_free_i64(t1);                                                    \
7197
}
7198
#endif
7199

    
7200
/* Single precision floating-point vectors operations */
7201
/* Arithmetic */
7202
GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7203
GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7204
GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7205
GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7206
static always_inline void gen_evfsabs (DisasContext *ctx)
7207
{
7208
    if (unlikely(!ctx->spe_enabled)) {
7209
        GEN_EXCP_NO_AP(ctx);
7210
        return;
7211
    }
7212
#if defined(TARGET_PPC64)
7213
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7214
#else
7215
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7216
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7217
#endif
7218
}
7219
static always_inline void gen_evfsnabs (DisasContext *ctx)
7220
{
7221
    if (unlikely(!ctx->spe_enabled)) {
7222
        GEN_EXCP_NO_AP(ctx);
7223
        return;
7224
    }
7225
#if defined(TARGET_PPC64)
7226
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7227
#else
7228
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7229
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7230
#endif
7231
}
7232
static always_inline void gen_evfsneg (DisasContext *ctx)
7233
{
7234
    if (unlikely(!ctx->spe_enabled)) {
7235
        GEN_EXCP_NO_AP(ctx);
7236
        return;
7237
    }
7238
#if defined(TARGET_PPC64)
7239
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7240
#else
7241
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7242
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7243
#endif
7244
}
7245

    
7246
/* Conversion */
7247
GEN_SPEFPUOP_CONV_64_64(evfscfui);
7248
GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7249
GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7250
GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7251
GEN_SPEFPUOP_CONV_64_64(evfsctui);
7252
GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7253
GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7254
GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7255
GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7256
GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7257

    
7258
/* Comparison */
7259
GEN_SPEFPUOP_COMP_64(evfscmpgt);
7260
GEN_SPEFPUOP_COMP_64(evfscmplt);
7261
GEN_SPEFPUOP_COMP_64(evfscmpeq);
7262
GEN_SPEFPUOP_COMP_64(evfststgt);
7263
GEN_SPEFPUOP_COMP_64(evfststlt);
7264
GEN_SPEFPUOP_COMP_64(evfststeq);
7265

    
7266
/* Opcodes definitions */
7267
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7268
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7269
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7270
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7271
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7272
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7273
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7274
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7275
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7276
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7277
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7278
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7279
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7280
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7281

    
7282
/* Single precision floating-point operations */
7283
/* Arithmetic */
7284
GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7285
GEN_SPEFPUOP_ARITH2_32_32(efssub);
7286
GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7287
GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7288
static always_inline void gen_efsabs (DisasContext *ctx)
7289
{
7290
    if (unlikely(!ctx->spe_enabled)) {
7291
        GEN_EXCP_NO_AP(ctx);
7292
        return;
7293
    }
7294
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7295
}
7296
static always_inline void gen_efsnabs (DisasContext *ctx)
7297
{
7298
    if (unlikely(!ctx->spe_enabled)) {
7299
        GEN_EXCP_NO_AP(ctx);
7300
        return;
7301
    }
7302
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7303
}
7304
static always_inline void gen_efsneg (DisasContext *ctx)
7305
{
7306
    if (unlikely(!ctx->spe_enabled)) {
7307
        GEN_EXCP_NO_AP(ctx);
7308
        return;
7309
    }
7310
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7311
}
7312

    
7313
/* Conversion */
7314
GEN_SPEFPUOP_CONV_32_32(efscfui);
7315
GEN_SPEFPUOP_CONV_32_32(efscfsi);
7316
GEN_SPEFPUOP_CONV_32_32(efscfuf);
7317
GEN_SPEFPUOP_CONV_32_32(efscfsf);
7318
GEN_SPEFPUOP_CONV_32_32(efsctui);
7319
GEN_SPEFPUOP_CONV_32_32(efsctsi);
7320
GEN_SPEFPUOP_CONV_32_32(efsctuf);
7321
GEN_SPEFPUOP_CONV_32_32(efsctsf);
7322
GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7323
GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7324
GEN_SPEFPUOP_CONV_32_64(efscfd);
7325

    
7326
/* Comparison */
7327
GEN_SPEFPUOP_COMP_32(efscmpgt);
7328
GEN_SPEFPUOP_COMP_32(efscmplt);
7329
GEN_SPEFPUOP_COMP_32(efscmpeq);
7330
GEN_SPEFPUOP_COMP_32(efststgt);
7331
GEN_SPEFPUOP_COMP_32(efststlt);
7332
GEN_SPEFPUOP_COMP_32(efststeq);
7333

    
7334
/* Opcodes definitions */
7335
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7336
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7337
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7338
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7339
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7340
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7341
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7342
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7343
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7344
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7345
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7346
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7347
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7348
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7349

    
7350
/* Double precision floating-point operations */
7351
/* Arithmetic */
7352
GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7353
GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7354
GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7355
GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7356
static always_inline void gen_efdabs (DisasContext *ctx)
7357
{
7358
    if (unlikely(!ctx->spe_enabled)) {
7359
        GEN_EXCP_NO_AP(ctx);
7360
        return;
7361
    }
7362
#if defined(TARGET_PPC64)
7363
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7364
#else
7365
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7366
#endif
7367
}
7368
static always_inline void gen_efdnabs (DisasContext *ctx)
7369
{
7370
    if (unlikely(!ctx->spe_enabled)) {
7371
        GEN_EXCP_NO_AP(ctx);
7372
        return;
7373
    }
7374
#if defined(TARGET_PPC64)
7375
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7376
#else
7377
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7378
#endif
7379
}
7380
static always_inline void gen_efdneg (DisasContext *ctx)
7381
{
7382
    if (unlikely(!ctx->spe_enabled)) {
7383
        GEN_EXCP_NO_AP(ctx);
7384
        return;
7385
    }
7386
#if defined(TARGET_PPC64)
7387
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7388
#else
7389
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7390
#endif
7391
}
7392

    
7393
/* Conversion */
7394
GEN_SPEFPUOP_CONV_64_32(efdcfui);
7395
GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7396
GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7397
GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7398
GEN_SPEFPUOP_CONV_32_64(efdctui);
7399
GEN_SPEFPUOP_CONV_32_64(efdctsi);
7400
GEN_SPEFPUOP_CONV_32_64(efdctuf);
7401
GEN_SPEFPUOP_CONV_32_64(efdctsf);
7402
GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7403
GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7404
GEN_SPEFPUOP_CONV_64_32(efdcfs);
7405
GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7406
GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7407
GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7408
GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7409

    
7410
/* Comparison */
7411
GEN_SPEFPUOP_COMP_64(efdcmpgt);
7412
GEN_SPEFPUOP_COMP_64(efdcmplt);
7413
GEN_SPEFPUOP_COMP_64(efdcmpeq);
7414
GEN_SPEFPUOP_COMP_64(efdtstgt);
7415
GEN_SPEFPUOP_COMP_64(efdtstlt);
7416
GEN_SPEFPUOP_COMP_64(efdtsteq);
7417

    
7418
/* Opcodes definitions */
7419
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7420
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7421
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7422
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7423
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7424
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7425
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7426
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7427
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7428
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7429
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7430
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7431
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7432
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7433
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7434
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7435

    
7436
/* End opcode list */
7437
GEN_OPCODE_MARK(end);
7438

    
7439
#include "translate_init.c"
7440
#include "helper_regs.h"
7441

    
7442
/*****************************************************************************/
7443
/* Misc PowerPC helpers */
7444
void cpu_dump_state (CPUState *env, FILE *f,
7445
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7446
                     int flags)
7447
{
7448
#define RGPL  4
7449
#define RFPL  4
7450

    
7451
    int i;
7452

    
7453
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
7454
                env->nip, env->lr, env->ctr, env->xer);
7455
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
7456
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7457
#if !defined(NO_TIMER_DUMP)
7458
    cpu_fprintf(f, "TB %08x %08x "
7459
#if !defined(CONFIG_USER_ONLY)
7460
                "DECR %08x"
7461
#endif
7462
                "\n",
7463
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7464
#if !defined(CONFIG_USER_ONLY)
7465
                , cpu_ppc_load_decr(env)
7466
#endif
7467
                );
7468
#endif
7469
    for (i = 0; i < 32; i++) {
7470
        if ((i & (RGPL - 1)) == 0)
7471
            cpu_fprintf(f, "GPR%02d", i);
7472
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7473
        if ((i & (RGPL - 1)) == (RGPL - 1))
7474
            cpu_fprintf(f, "\n");
7475
    }
7476
    cpu_fprintf(f, "CR ");
7477
    for (i = 0; i < 8; i++)
7478
        cpu_fprintf(f, "%01x", env->crf[i]);
7479
    cpu_fprintf(f, "  [");
7480
    for (i = 0; i < 8; i++) {
7481
        char a = '-';
7482
        if (env->crf[i] & 0x08)
7483
            a = 'L';
7484
        else if (env->crf[i] & 0x04)
7485
            a = 'G';
7486
        else if (env->crf[i] & 0x02)
7487
            a = 'E';
7488
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7489
    }
7490
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7491
    for (i = 0; i < 32; i++) {
7492
        if ((i & (RFPL - 1)) == 0)
7493
            cpu_fprintf(f, "FPR%02d", i);
7494
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7495
        if ((i & (RFPL - 1)) == (RFPL - 1))
7496
            cpu_fprintf(f, "\n");
7497
    }
7498
#if !defined(CONFIG_USER_ONLY)
7499
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7500
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7501
#endif
7502

    
7503
#undef RGPL
7504
#undef RFPL
7505
}
7506

    
7507
void cpu_dump_statistics (CPUState *env, FILE*f,
7508
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7509
                          int flags)
7510
{
7511
#if defined(DO_PPC_STATISTICS)
7512
    opc_handler_t **t1, **t2, **t3, *handler;
7513
    int op1, op2, op3;
7514

    
7515
    t1 = env->opcodes;
7516
    for (op1 = 0; op1 < 64; op1++) {
7517
        handler = t1[op1];
7518
        if (is_indirect_opcode(handler)) {
7519
            t2 = ind_table(handler);
7520
            for (op2 = 0; op2 < 32; op2++) {
7521
                handler = t2[op2];
7522
                if (is_indirect_opcode(handler)) {
7523
                    t3 = ind_table(handler);
7524
                    for (op3 = 0; op3 < 32; op3++) {
7525
                        handler = t3[op3];
7526
                        if (handler->count == 0)
7527
                            continue;
7528
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7529
                                    "%016llx %lld\n",
7530
                                    op1, op2, op3, op1, (op3 << 5) | op2,
7531
                                    handler->oname,
7532
                                    handler->count, handler->count);
7533
                    }
7534
                } else {
7535
                    if (handler->count == 0)
7536
                        continue;
7537
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
7538
                                "%016llx %lld\n",
7539
                                op1, op2, op1, op2, handler->oname,
7540
                                handler->count, handler->count);
7541
                }
7542
            }
7543
        } else {
7544
            if (handler->count == 0)
7545
                continue;
7546
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
7547
                        op1, op1, handler->oname,
7548
                        handler->count, handler->count);
7549
        }
7550
    }
7551
#endif
7552
}
7553

    
7554
/*****************************************************************************/
7555
static always_inline void gen_intermediate_code_internal (CPUState *env,
7556
                                                          TranslationBlock *tb,
7557
                                                          int search_pc)
7558
{
7559
    DisasContext ctx, *ctxp = &ctx;
7560
    opc_handler_t **table, *handler;
7561
    target_ulong pc_start;
7562
    uint16_t *gen_opc_end;
7563
    int supervisor, little_endian;
7564
    CPUBreakpoint *bp;
7565
    int j, lj = -1;
7566
    int num_insns;
7567
    int max_insns;
7568

    
7569
    pc_start = tb->pc;
7570
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7571
#if defined(OPTIMIZE_FPRF_UPDATE)
7572
    gen_fprf_ptr = gen_fprf_buf;
7573
#endif
7574
    ctx.nip = pc_start;
7575
    ctx.tb = tb;
7576
    ctx.exception = POWERPC_EXCP_NONE;
7577
    ctx.spr_cb = env->spr_cb;
7578
    supervisor = env->mmu_idx;
7579
#if !defined(CONFIG_USER_ONLY)
7580
    ctx.supervisor = supervisor;
7581
#endif
7582
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
7583
#if defined(TARGET_PPC64)
7584
    ctx.sf_mode = msr_sf;
7585
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
7586
#else
7587
    ctx.mem_idx = (supervisor << 1) | little_endian;
7588
#endif
7589
    ctx.dcache_line_size = env->dcache_line_size;
7590
    ctx.fpu_enabled = msr_fp;
7591
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7592
        ctx.spe_enabled = msr_spe;
7593
    else
7594
        ctx.spe_enabled = 0;
7595
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7596
        ctx.altivec_enabled = msr_vr;
7597
    else
7598
        ctx.altivec_enabled = 0;
7599
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7600
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
7601
    else
7602
        ctx.singlestep_enabled = 0;
7603
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7604
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7605
    if (unlikely(env->singlestep_enabled))
7606
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7607
#if defined (DO_SINGLE_STEP) && 0
7608
    /* Single step trace mode */
7609
    msr_se = 1;
7610
#endif
7611
    num_insns = 0;
7612
    max_insns = tb->cflags & CF_COUNT_MASK;
7613
    if (max_insns == 0)
7614
        max_insns = CF_COUNT_MASK;
7615

    
7616
    gen_icount_start();
7617
    /* Set env in case of segfault during code fetch */
7618
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7619
        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7620
            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7621
                if (bp->pc == ctx.nip) {
7622
                    gen_update_nip(&ctx, ctx.nip);
7623
                    gen_helper_raise_debug();
7624
                    break;
7625
                }
7626
            }
7627
        }
7628
        if (unlikely(search_pc)) {
7629
            j = gen_opc_ptr - gen_opc_buf;
7630
            if (lj < j) {
7631
                lj++;
7632
                while (lj < j)
7633
                    gen_opc_instr_start[lj++] = 0;
7634
                gen_opc_pc[lj] = ctx.nip;
7635
                gen_opc_instr_start[lj] = 1;
7636
                gen_opc_icount[lj] = num_insns;
7637
            }
7638
        }
7639
#if defined PPC_DEBUG_DISAS
7640
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7641
            fprintf(logfile, "----------------\n");
7642
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7643
                    ctx.nip, supervisor, (int)msr_ir);
7644
        }
7645
#endif
7646
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7647
            gen_io_start();
7648
        if (unlikely(little_endian)) {
7649
            ctx.opcode = bswap32(ldl_code(ctx.nip));
7650
        } else {
7651
            ctx.opcode = ldl_code(ctx.nip);
7652
        }
7653
#if defined PPC_DEBUG_DISAS
7654
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7655
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7656
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7657
                    opc3(ctx.opcode), little_endian ? "little" : "big");
7658
        }
7659
#endif
7660
        ctx.nip += 4;
7661
        table = env->opcodes;
7662
        num_insns++;
7663
        handler = table[opc1(ctx.opcode)];
7664
        if (is_indirect_opcode(handler)) {
7665
            table = ind_table(handler);
7666
            handler = table[opc2(ctx.opcode)];
7667
            if (is_indirect_opcode(handler)) {
7668
                table = ind_table(handler);
7669
                handler = table[opc3(ctx.opcode)];
7670
            }
7671
        }
7672
        /* Is opcode *REALLY* valid ? */
7673
        if (unlikely(handler->handler == &gen_invalid)) {
7674
            if (loglevel != 0) {
7675
                fprintf(logfile, "invalid/unsupported opcode: "
7676
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7677
                        opc1(ctx.opcode), opc2(ctx.opcode),
7678
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7679
            } else {
7680
                printf("invalid/unsupported opcode: "
7681
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7682
                       opc1(ctx.opcode), opc2(ctx.opcode),
7683
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7684
            }
7685
        } else {
7686
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
7687
                if (loglevel != 0) {
7688
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7689
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
7690
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
7691
                            opc2(ctx.opcode), opc3(ctx.opcode),
7692
                            ctx.opcode, ctx.nip - 4);
7693
                } else {
7694
                    printf("invalid bits: %08x for opcode: "
7695
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
7696
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
7697
                           opc2(ctx.opcode), opc3(ctx.opcode),
7698
                           ctx.opcode, ctx.nip - 4);
7699
                }
7700
                GEN_EXCP_INVAL(ctxp);
7701
                break;
7702
            }
7703
        }
7704
        (*(handler->handler))(&ctx);
7705
#if defined(DO_PPC_STATISTICS)
7706
        handler->count++;
7707
#endif
7708
        /* Check trace mode exceptions */
7709
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7710
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7711
                     ctx.exception != POWERPC_SYSCALL &&
7712
                     ctx.exception != POWERPC_EXCP_TRAP &&
7713
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
7714
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7715
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7716
                            (env->singlestep_enabled) ||
7717
                            num_insns >= max_insns)) {
7718
            /* if we reach a page boundary or are single stepping, stop
7719
             * generation
7720
             */
7721
            break;
7722
        }
7723
#if defined (DO_SINGLE_STEP)
7724
        break;
7725
#endif
7726
    }
7727
    if (tb->cflags & CF_LAST_IO)
7728
        gen_io_end();
7729
    if (ctx.exception == POWERPC_EXCP_NONE) {
7730
        gen_goto_tb(&ctx, 0, ctx.nip);
7731
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7732
        if (unlikely(env->singlestep_enabled)) {
7733
            gen_update_nip(&ctx, ctx.nip);
7734
            gen_helper_raise_debug();
7735
        }
7736
        /* Generate the return instruction */
7737
        tcg_gen_exit_tb(0);
7738
    }
7739
    gen_icount_end(tb, num_insns);
7740
    *gen_opc_ptr = INDEX_op_end;
7741
    if (unlikely(search_pc)) {
7742
        j = gen_opc_ptr - gen_opc_buf;
7743
        lj++;
7744
        while (lj <= j)
7745
            gen_opc_instr_start[lj++] = 0;
7746
    } else {
7747
        tb->size = ctx.nip - pc_start;
7748
        tb->icount = num_insns;
7749
    }
7750
#if defined(DEBUG_DISAS)
7751
    if (loglevel & CPU_LOG_TB_CPU) {
7752
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7753
        cpu_dump_state(env, logfile, fprintf, 0);
7754
    }
7755
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7756
        int flags;
7757
        flags = env->bfd_mach;
7758
        flags |= little_endian << 16;
7759
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7760
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7761
        fprintf(logfile, "\n");
7762
    }
7763
#endif
7764
}
7765

    
7766
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7767
{
7768
    gen_intermediate_code_internal(env, tb, 0);
7769
}
7770

    
7771
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7772
{
7773
    gen_intermediate_code_internal(env, tb, 1);
7774
}
7775

    
7776
void gen_pc_load(CPUState *env, TranslationBlock *tb,
7777
                unsigned long searched_pc, int pc_pos, void *puc)
7778
{
7779
    env->nip = gen_opc_pc[pc_pos];
7780
}