Statistics
| Branch: | Revision:

root / tcg / ppc64 / tcg-target.c @ c82e5848

History | View | Annotate | Download (48.9 kB)

1
/*
2
 * Tiny Code Generator for QEMU
3
 *
4
 * Copyright (c) 2008 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#define TCG_CT_CONST_U32 0x100
26

    
27
static uint8_t *tb_ret_addr;
28

    
29
#define FAST_PATH
30

    
31
#if TARGET_LONG_BITS == 32
32
#define LD_ADDR LWZU
33
#define CMP_L 0
34
#else
35
#define LD_ADDR LDU
36
#define CMP_L (1<<21)
37
#endif
38

    
39
#ifndef GUEST_BASE
40
#define GUEST_BASE 0
41
#endif
42

    
43
#ifdef CONFIG_USE_GUEST_BASE
44
#define TCG_GUEST_BASE_REG 30
45
#else
46
#define TCG_GUEST_BASE_REG 0
47
#endif
48

    
49
#ifndef NDEBUG
50
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
51
    "r0",
52
    "r1",
53
    "r2",
54
    "r3",
55
    "r4",
56
    "r5",
57
    "r6",
58
    "r7",
59
    "r8",
60
    "r9",
61
    "r10",
62
    "r11",
63
    "r12",
64
    "r13",
65
    "r14",
66
    "r15",
67
    "r16",
68
    "r17",
69
    "r18",
70
    "r19",
71
    "r20",
72
    "r21",
73
    "r22",
74
    "r23",
75
    "r24",
76
    "r25",
77
    "r26",
78
    "r27",
79
    "r28",
80
    "r29",
81
    "r30",
82
    "r31"
83
};
84
#endif
85

    
86
static const int tcg_target_reg_alloc_order[] = {
87
    TCG_REG_R14,
88
    TCG_REG_R15,
89
    TCG_REG_R16,
90
    TCG_REG_R17,
91
    TCG_REG_R18,
92
    TCG_REG_R19,
93
    TCG_REG_R20,
94
    TCG_REG_R21,
95
    TCG_REG_R22,
96
    TCG_REG_R23,
97
    TCG_REG_R28,
98
    TCG_REG_R29,
99
    TCG_REG_R30,
100
    TCG_REG_R31,
101
#ifdef __APPLE__
102
    TCG_REG_R2,
103
#endif
104
    TCG_REG_R3,
105
    TCG_REG_R4,
106
    TCG_REG_R5,
107
    TCG_REG_R6,
108
    TCG_REG_R7,
109
    TCG_REG_R8,
110
    TCG_REG_R9,
111
    TCG_REG_R10,
112
#ifndef __APPLE__
113
    TCG_REG_R11,
114
#endif
115
    TCG_REG_R12,
116
    TCG_REG_R24,
117
    TCG_REG_R25,
118
    TCG_REG_R26,
119
    TCG_REG_R27
120
};
121

    
122
static const int tcg_target_call_iarg_regs[] = {
123
    TCG_REG_R3,
124
    TCG_REG_R4,
125
    TCG_REG_R5,
126
    TCG_REG_R6,
127
    TCG_REG_R7,
128
    TCG_REG_R8,
129
    TCG_REG_R9,
130
    TCG_REG_R10
131
};
132

    
133
static const int tcg_target_call_oarg_regs[] = {
134
    TCG_REG_R3
135
};
136

    
137
static const int tcg_target_callee_save_regs[] = {
138
#ifdef __APPLE__
139
    TCG_REG_R11,
140
#endif
141
    TCG_REG_R14,
142
    TCG_REG_R15,
143
    TCG_REG_R16,
144
    TCG_REG_R17,
145
    TCG_REG_R18,
146
    TCG_REG_R19,
147
    TCG_REG_R20,
148
    TCG_REG_R21,
149
    TCG_REG_R22,
150
    TCG_REG_R23,
151
    TCG_REG_R24,
152
    TCG_REG_R25,
153
    TCG_REG_R26,
154
    TCG_REG_R27, /* currently used for the global env */
155
    TCG_REG_R28,
156
    TCG_REG_R29,
157
    TCG_REG_R30,
158
    TCG_REG_R31
159
};
160

    
161
static uint32_t reloc_pc24_val (void *pc, tcg_target_long target)
162
{
163
    tcg_target_long disp;
164

    
165
    disp = target - (tcg_target_long) pc;
166
    if ((disp << 38) >> 38 != disp)
167
        tcg_abort ();
168

    
169
    return disp & 0x3fffffc;
170
}
171

    
172
static void reloc_pc24 (void *pc, tcg_target_long target)
173
{
174
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0x3fffffc)
175
        | reloc_pc24_val (pc, target);
176
}
177

    
178
static uint16_t reloc_pc14_val (void *pc, tcg_target_long target)
179
{
180
    tcg_target_long disp;
181

    
182
    disp = target - (tcg_target_long) pc;
183
    if (disp != (int16_t) disp)
184
        tcg_abort ();
185

    
186
    return disp & 0xfffc;
187
}
188

    
189
static void reloc_pc14 (void *pc, tcg_target_long target)
190
{
191
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0xfffc)
192
        | reloc_pc14_val (pc, target);
193
}
194

    
195
static void patch_reloc (uint8_t *code_ptr, int type,
196
                         tcg_target_long value, tcg_target_long addend)
197
{
198
    value += addend;
199
    switch (type) {
200
    case R_PPC_REL14:
201
        reloc_pc14 (code_ptr, value);
202
        break;
203
    case R_PPC_REL24:
204
        reloc_pc24 (code_ptr, value);
205
        break;
206
    default:
207
        tcg_abort ();
208
    }
209
}
210

    
211
/* maximum number of register used for input function arguments */
212
static int tcg_target_get_call_iarg_regs_count (int flags)
213
{
214
    return ARRAY_SIZE (tcg_target_call_iarg_regs);
215
}
216

    
217
/* parse target specific constraints */
218
static int target_parse_constraint (TCGArgConstraint *ct, const char **pct_str)
219
{
220
    const char *ct_str;
221

    
222
    ct_str = *pct_str;
223
    switch (ct_str[0]) {
224
    case 'A': case 'B': case 'C': case 'D':
225
        ct->ct |= TCG_CT_REG;
226
        tcg_regset_set_reg (ct->u.regs, 3 + ct_str[0] - 'A');
227
        break;
228
    case 'r':
229
        ct->ct |= TCG_CT_REG;
230
        tcg_regset_set32 (ct->u.regs, 0, 0xffffffff);
231
        break;
232
    case 'L':                   /* qemu_ld constraint */
233
        ct->ct |= TCG_CT_REG;
234
        tcg_regset_set32 (ct->u.regs, 0, 0xffffffff);
235
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
236
#ifdef CONFIG_SOFTMMU
237
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
238
#endif
239
        break;
240
    case 'S':                   /* qemu_st constraint */
241
        ct->ct |= TCG_CT_REG;
242
        tcg_regset_set32 (ct->u.regs, 0, 0xffffffff);
243
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
244
#ifdef CONFIG_SOFTMMU
245
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
246
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R5);
247
#endif
248
        break;
249
    case 'Z':
250
        ct->ct |= TCG_CT_CONST_U32;
251
        break;
252
    default:
253
        return -1;
254
    }
255
    ct_str++;
256
    *pct_str = ct_str;
257
    return 0;
258
}
259

    
260
/* test if a constant matches the constraint */
261
static int tcg_target_const_match (tcg_target_long val,
262
                                   const TCGArgConstraint *arg_ct)
263
{
264
    int ct;
265

    
266
    ct = arg_ct->ct;
267
    if (ct & TCG_CT_CONST)
268
        return 1;
269
    else if ((ct & TCG_CT_CONST_U32) && (val == (uint32_t) val))
270
        return 1;
271
    return 0;
272
}
273

    
274
#define OPCD(opc) ((opc)<<26)
275
#define XO19(opc) (OPCD(19)|((opc)<<1))
276
#define XO30(opc) (OPCD(30)|((opc)<<2))
277
#define XO31(opc) (OPCD(31)|((opc)<<1))
278
#define XO58(opc) (OPCD(58)|(opc))
279
#define XO62(opc) (OPCD(62)|(opc))
280

    
281
#define B      OPCD( 18)
282
#define BC     OPCD( 16)
283
#define LBZ    OPCD( 34)
284
#define LHZ    OPCD( 40)
285
#define LHA    OPCD( 42)
286
#define LWZ    OPCD( 32)
287
#define STB    OPCD( 38)
288
#define STH    OPCD( 44)
289
#define STW    OPCD( 36)
290

    
291
#define STD    XO62(  0)
292
#define STDU   XO62(  1)
293
#define STDX   XO31(149)
294

    
295
#define LD     XO58(  0)
296
#define LDX    XO31( 21)
297
#define LDU    XO58(  1)
298
#define LWA    XO58(  2)
299
#define LWAX   XO31(341)
300

    
301
#define ADDIC  OPCD( 12)
302
#define ADDI   OPCD( 14)
303
#define ADDIS  OPCD( 15)
304
#define ORI    OPCD( 24)
305
#define ORIS   OPCD( 25)
306
#define XORI   OPCD( 26)
307
#define XORIS  OPCD( 27)
308
#define ANDI   OPCD( 28)
309
#define ANDIS  OPCD( 29)
310
#define MULLI  OPCD(  7)
311
#define CMPLI  OPCD( 10)
312
#define CMPI   OPCD( 11)
313

    
314
#define LWZU   OPCD( 33)
315
#define STWU   OPCD( 37)
316

    
317
#define RLWINM OPCD( 21)
318

    
319
#define RLDICL XO30(  0)
320
#define RLDICR XO30(  1)
321
#define RLDIMI XO30(  3)
322

    
323
#define BCLR   XO19( 16)
324
#define BCCTR  XO19(528)
325
#define CRAND  XO19(257)
326
#define CRANDC XO19(129)
327
#define CRNAND XO19(225)
328
#define CROR   XO19(449)
329
#define CRNOR  XO19( 33)
330

    
331
#define EXTSB  XO31(954)
332
#define EXTSH  XO31(922)
333
#define EXTSW  XO31(986)
334
#define ADD    XO31(266)
335
#define ADDE   XO31(138)
336
#define ADDC   XO31( 10)
337
#define AND    XO31( 28)
338
#define SUBF   XO31( 40)
339
#define SUBFC  XO31(  8)
340
#define SUBFE  XO31(136)
341
#define OR     XO31(444)
342
#define XOR    XO31(316)
343
#define MULLW  XO31(235)
344
#define MULHWU XO31( 11)
345
#define DIVW   XO31(491)
346
#define DIVWU  XO31(459)
347
#define CMP    XO31(  0)
348
#define CMPL   XO31( 32)
349
#define LHBRX  XO31(790)
350
#define LWBRX  XO31(534)
351
#define STHBRX XO31(918)
352
#define STWBRX XO31(662)
353
#define MFSPR  XO31(339)
354
#define MTSPR  XO31(467)
355
#define SRAWI  XO31(824)
356
#define NEG    XO31(104)
357
#define MFCR   XO31( 19)
358
#define NOR    XO31(124)
359
#define CNTLZW XO31( 26)
360
#define CNTLZD XO31( 58)
361

    
362
#define MULLD  XO31(233)
363
#define MULHD  XO31( 73)
364
#define MULHDU XO31(  9)
365
#define DIVD   XO31(489)
366
#define DIVDU  XO31(457)
367

    
368
#define LBZX   XO31( 87)
369
#define LHZX   XO31(279)
370
#define LHAX   XO31(343)
371
#define LWZX   XO31( 23)
372
#define STBX   XO31(215)
373
#define STHX   XO31(407)
374
#define STWX   XO31(151)
375

    
376
#define SPR(a,b) ((((a)<<5)|(b))<<11)
377
#define LR     SPR(8, 0)
378
#define CTR    SPR(9, 0)
379

    
380
#define SLW    XO31( 24)
381
#define SRW    XO31(536)
382
#define SRAW   XO31(792)
383

    
384
#define SLD    XO31( 27)
385
#define SRD    XO31(539)
386
#define SRAD   XO31(794)
387
#define SRADI  XO31(413<<1)
388

    
389
#define TW     XO31( 4)
390
#define TRAP   (TW | TO (31))
391

    
392
#define RT(r) ((r)<<21)
393
#define RS(r) ((r)<<21)
394
#define RA(r) ((r)<<16)
395
#define RB(r) ((r)<<11)
396
#define TO(t) ((t)<<21)
397
#define SH(s) ((s)<<11)
398
#define MB(b) ((b)<<6)
399
#define ME(e) ((e)<<1)
400
#define BO(o) ((o)<<21)
401
#define MB64(b) ((b)<<5)
402

    
403
#define LK    1
404

    
405
#define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
406
#define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
407

    
408
#define BF(n)    ((n)<<23)
409
#define BI(n, c) (((c)+((n)*4))<<16)
410
#define BT(n, c) (((c)+((n)*4))<<21)
411
#define BA(n, c) (((c)+((n)*4))<<16)
412
#define BB(n, c) (((c)+((n)*4))<<11)
413

    
414
#define BO_COND_TRUE  BO (12)
415
#define BO_COND_FALSE BO ( 4)
416
#define BO_ALWAYS     BO (20)
417

    
418
enum {
419
    CR_LT,
420
    CR_GT,
421
    CR_EQ,
422
    CR_SO
423
};
424

    
425
static const uint32_t tcg_to_bc[10] = {
426
    [TCG_COND_EQ]  = BC | BI (7, CR_EQ) | BO_COND_TRUE,
427
    [TCG_COND_NE]  = BC | BI (7, CR_EQ) | BO_COND_FALSE,
428
    [TCG_COND_LT]  = BC | BI (7, CR_LT) | BO_COND_TRUE,
429
    [TCG_COND_GE]  = BC | BI (7, CR_LT) | BO_COND_FALSE,
430
    [TCG_COND_LE]  = BC | BI (7, CR_GT) | BO_COND_FALSE,
431
    [TCG_COND_GT]  = BC | BI (7, CR_GT) | BO_COND_TRUE,
432
    [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
433
    [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
434
    [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
435
    [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
436
};
437

    
438
static void tcg_out_mov (TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
439
{
440
    tcg_out32 (s, OR | SAB (arg, ret, arg));
441
}
442

    
443
static void tcg_out_rld (TCGContext *s, int op, int ra, int rs, int sh, int mb)
444
{
445
    sh = SH (sh & 0x1f) | (((sh >> 5) & 1) << 1);
446
    mb = MB64 ((mb >> 5) | ((mb << 1) & 0x3f));
447
    tcg_out32 (s, op | RA (ra) | RS (rs) | sh | mb);
448
}
449

    
450
static void tcg_out_movi32 (TCGContext *s, int ret, int32_t arg)
451
{
452
    if (arg == (int16_t) arg)
453
        tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
454
    else {
455
        tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
456
        if (arg & 0xffff)
457
            tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
458
    }
459
}
460

    
461
static void tcg_out_movi (TCGContext *s, TCGType type,
462
                          TCGReg ret, tcg_target_long arg)
463
{
464
    int32_t arg32 = arg;
465
    arg = type == TCG_TYPE_I32 ? arg & 0xffffffff : arg;
466

    
467
    if (arg == arg32) {
468
        tcg_out_movi32 (s, ret, arg32);
469
    }
470
    else {
471
        if ((uint64_t) arg >> 32) {
472
            uint16_t h16 = arg >> 16;
473
            uint16_t l16 = arg;
474

    
475
            tcg_out_movi32 (s, ret, arg >> 32);
476
            tcg_out_rld (s, RLDICR, ret, ret, 32, 31);
477
            if (h16) tcg_out32 (s, ORIS | RS (ret) | RA (ret) | h16);
478
            if (l16) tcg_out32 (s, ORI | RS (ret) | RA (ret) | l16);
479
        }
480
        else {
481
            tcg_out_movi32 (s, ret, arg32);
482
            if (arg32 < 0)
483
                tcg_out_rld (s, RLDICL, ret, ret, 0, 32);
484
        }
485
    }
486
}
487

    
488
static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
489
{
490
    tcg_target_long disp;
491

    
492
    disp = target - (tcg_target_long) s->code_ptr;
493
    if ((disp << 38) >> 38 == disp)
494
        tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
495
    else {
496
        tcg_out_movi (s, TCG_TYPE_I64, 0, (tcg_target_long) target);
497
        tcg_out32 (s, MTSPR | RS (0) | CTR);
498
        tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
499
    }
500
}
501

    
502
static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
503
{
504
#ifdef __APPLE__
505
    if (const_arg) {
506
        tcg_out_b (s, LK, arg);
507
    }
508
    else {
509
        tcg_out32 (s, MTSPR | RS (arg) | LR);
510
        tcg_out32 (s, BCLR | BO_ALWAYS | LK);
511
    }
512
#else
513
    int reg;
514

    
515
    if (const_arg) {
516
        reg = 2;
517
        tcg_out_movi (s, TCG_TYPE_I64, reg, arg);
518
    }
519
    else reg = arg;
520

    
521
    tcg_out32 (s, LD | RT (0) | RA (reg));
522
    tcg_out32 (s, MTSPR | RA (0) | CTR);
523
    tcg_out32 (s, LD | RT (11) | RA (reg) | 16);
524
    tcg_out32 (s, LD | RT (2) | RA (reg) | 8);
525
    tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
526
#endif
527
}
528

    
529
static void tcg_out_ldst (TCGContext *s, int ret, int addr,
530
                          int offset, int op1, int op2)
531
{
532
    if (offset == (int16_t) offset)
533
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
534
    else {
535
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
536
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
537
    }
538
}
539

    
540
static void tcg_out_ldsta (TCGContext *s, int ret, int addr,
541
                           int offset, int op1, int op2)
542
{
543
    if (offset == (int16_t) (offset & ~3))
544
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
545
    else {
546
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
547
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
548
    }
549
}
550

    
551
#if defined (CONFIG_SOFTMMU)
552

    
553
#include "../../softmmu_defs.h"
554

    
555
#ifdef CONFIG_TCG_PASS_AREG0
556
/* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
557
   int mmu_idx) */
558
static const void * const qemu_ld_helpers[4] = {
559
    helper_ldb_mmu,
560
    helper_ldw_mmu,
561
    helper_ldl_mmu,
562
    helper_ldq_mmu,
563
};
564

    
565
/* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
566
   uintxx_t val, int mmu_idx) */
567
static const void * const qemu_st_helpers[4] = {
568
    helper_stb_mmu,
569
    helper_stw_mmu,
570
    helper_stl_mmu,
571
    helper_stq_mmu,
572
};
573
#else
574
/* legacy helper signature: __ld_mmu(target_ulong addr, int
575
   mmu_idx) */
576
static void *qemu_ld_helpers[4] = {
577
    __ldb_mmu,
578
    __ldw_mmu,
579
    __ldl_mmu,
580
    __ldq_mmu,
581
};
582

    
583
/* legacy helper signature: __st_mmu(target_ulong addr, uintxx_t val,
584
   int mmu_idx) */
585
static void *qemu_st_helpers[4] = {
586
    __stb_mmu,
587
    __stw_mmu,
588
    __stl_mmu,
589
    __stq_mmu,
590
};
591
#endif
592

    
593
static void tcg_out_tlb_read (TCGContext *s, int r0, int r1, int r2,
594
                              int addr_reg, int s_bits, int offset)
595
{
596
#if TARGET_LONG_BITS == 32
597
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
598

    
599
    tcg_out32 (s, (RLWINM
600
                   | RA (r0)
601
                   | RS (addr_reg)
602
                   | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
603
                   | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
604
                   | ME (31 - CPU_TLB_ENTRY_BITS)
605
                   )
606
        );
607
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
608
    tcg_out32 (s, (LWZU | RT (r1) | RA (r0) | offset));
609
    tcg_out32 (s, (RLWINM
610
                   | RA (r2)
611
                   | RS (addr_reg)
612
                   | SH (0)
613
                   | MB ((32 - s_bits) & 31)
614
                   | ME (31 - TARGET_PAGE_BITS)
615
                   )
616
        );
617
#else
618
    tcg_out_rld (s, RLDICL, r0, addr_reg,
619
                 64 - TARGET_PAGE_BITS,
620
                 64 - CPU_TLB_BITS);
621
    tcg_out_rld (s, RLDICR, r0, r0,
622
                 CPU_TLB_ENTRY_BITS,
623
                 63 - CPU_TLB_ENTRY_BITS);
624

    
625
    tcg_out32 (s, ADD | TAB (r0, r0, TCG_AREG0));
626
    tcg_out32 (s, LD_ADDR | RT (r1) | RA (r0) | offset);
627

    
628
    if (!s_bits) {
629
        tcg_out_rld (s, RLDICR, r2, addr_reg, 0, 63 - TARGET_PAGE_BITS);
630
    }
631
    else {
632
        tcg_out_rld (s, RLDICL, r2, addr_reg,
633
                     64 - TARGET_PAGE_BITS,
634
                     TARGET_PAGE_BITS - s_bits);
635
        tcg_out_rld (s, RLDICL, r2, r2, TARGET_PAGE_BITS, 0);
636
    }
637
#endif
638
}
639
#endif
640

    
641
static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
642
{
643
    int addr_reg, data_reg, r0, r1, rbase, bswap;
644
#ifdef CONFIG_SOFTMMU
645
    int r2, mem_index, s_bits, ir;
646
    void *label1_ptr, *label2_ptr;
647
#endif
648

    
649
    data_reg = *args++;
650
    addr_reg = *args++;
651

    
652
#ifdef CONFIG_SOFTMMU
653
    mem_index = *args;
654
    s_bits = opc & 3;
655

    
656
    r0 = 3;
657
    r1 = 4;
658
    r2 = 0;
659
    rbase = 0;
660

    
661
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, s_bits,
662
                      offsetof (CPUArchState, tlb_table[mem_index][0].addr_read));
663

    
664
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
665

    
666
    label1_ptr = s->code_ptr;
667
#ifdef FAST_PATH
668
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
669
#endif
670

    
671
    /* slow path */
672
    ir = 3;
673
    tcg_out_mov (s, TCG_TYPE_I64, ir++, addr_reg);
674
    tcg_out_movi (s, TCG_TYPE_I64, ir++, mem_index);
675

    
676
#ifdef CONFIG_TCG_PASS_AREG0
677
    /* XXX/FIXME: suboptimal */
678
    tcg_out_mov (s, TCG_TYPE_I32, tcg_target_call_iarg_regs[2],
679
                 tcg_target_call_iarg_regs[1]);
680
    tcg_out_mov (s, TCG_TYPE_TL, tcg_target_call_iarg_regs[1],
681
                 tcg_target_call_iarg_regs[0]);
682
    tcg_out_mov (s, TCG_TYPE_PTR, tcg_target_call_iarg_regs[0],
683
                 TCG_AREG0);
684
#endif
685
    tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
686

    
687
    switch (opc) {
688
    case 0|4:
689
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
690
        break;
691
    case 1|4:
692
        tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
693
        break;
694
    case 2|4:
695
        tcg_out32 (s, EXTSW | RA (data_reg) | RS (3));
696
        break;
697
    case 0:
698
    case 1:
699
    case 2:
700
    case 3:
701
        if (data_reg != 3)
702
            tcg_out_mov (s, TCG_TYPE_I64, data_reg, 3);
703
        break;
704
    }
705
    label2_ptr = s->code_ptr;
706
    tcg_out32 (s, B);
707

    
708
    /* label1: fast path */
709
#ifdef FAST_PATH
710
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
711
#endif
712

    
713
    /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
714
    tcg_out32 (s, (LD
715
                   | RT (r0)
716
                   | RA (r0)
717
                   | (offsetof (CPUTLBEntry, addend)
718
                      - offsetof (CPUTLBEntry, addr_read))
719
                   ));
720
    /* r0 = env->tlb_table[mem_index][index].addend */
721
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
722
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
723

    
724
#else  /* !CONFIG_SOFTMMU */
725
#if TARGET_LONG_BITS == 32
726
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
727
#endif
728
    r0 = addr_reg;
729
    r1 = 3;
730
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
731
#endif
732

    
733
#ifdef TARGET_WORDS_BIGENDIAN
734
    bswap = 0;
735
#else
736
    bswap = 1;
737
#endif
738
    switch (opc) {
739
    default:
740
    case 0:
741
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
742
        break;
743
    case 0|4:
744
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
745
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
746
        break;
747
    case 1:
748
        if (bswap)
749
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
750
        else
751
            tcg_out32 (s, LHZX | TAB (data_reg, rbase, r0));
752
        break;
753
    case 1|4:
754
        if (bswap) {
755
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
756
            tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
757
        }
758
        else tcg_out32 (s, LHAX | TAB (data_reg, rbase, r0));
759
        break;
760
    case 2:
761
        if (bswap)
762
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
763
        else
764
            tcg_out32 (s, LWZX | TAB (data_reg, rbase, r0));
765
        break;
766
    case 2|4:
767
        if (bswap) {
768
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
769
            tcg_out32 (s, EXTSW | RA (data_reg) | RS (data_reg));
770
        }
771
        else tcg_out32 (s, LWAX | TAB (data_reg, rbase, r0));
772
        break;
773
    case 3:
774
#ifdef CONFIG_USE_GUEST_BASE
775
        if (bswap) {
776
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
777
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
778
            tcg_out32 (s, LWBRX | TAB (      r1, rbase, r1));
779
            tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
780
        }
781
        else tcg_out32 (s, LDX | TAB (data_reg, rbase, r0));
782
#else
783
        if (bswap) {
784
            tcg_out_movi32 (s, 0, 4);
785
            tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
786
            tcg_out32 (s, LWBRX | RT (      r1) | RA (r0));
787
            tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
788
        }
789
        else tcg_out32 (s, LD | RT (data_reg) | RA (r0));
790
#endif
791
        break;
792
    }
793

    
794
#ifdef CONFIG_SOFTMMU
795
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
796
#endif
797
}
798

    
799
static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
800
{
801
    int addr_reg, r0, r1, rbase, data_reg, bswap;
802
#ifdef CONFIG_SOFTMMU
803
    int r2, mem_index, ir;
804
    void *label1_ptr, *label2_ptr;
805
#endif
806

    
807
    data_reg = *args++;
808
    addr_reg = *args++;
809

    
810
#ifdef CONFIG_SOFTMMU
811
    mem_index = *args;
812

    
813
    r0 = 3;
814
    r1 = 4;
815
    r2 = 0;
816
    rbase = 0;
817

    
818
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, opc,
819
                      offsetof (CPUArchState, tlb_table[mem_index][0].addr_write));
820

    
821
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
822

    
823
    label1_ptr = s->code_ptr;
824
#ifdef FAST_PATH
825
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
826
#endif
827

    
828
    /* slow path */
829
    ir = 3;
830
    tcg_out_mov (s, TCG_TYPE_I64, ir++, addr_reg);
831
    tcg_out_rld (s, RLDICL, ir++, data_reg, 0, 64 - (1 << (3 + opc)));
832
    tcg_out_movi (s, TCG_TYPE_I64, ir++, mem_index);
833

    
834
#ifdef CONFIG_TCG_PASS_AREG0
835
    /* XXX/FIXME: suboptimal */
836
    tcg_out_mov (s, TCG_TYPE_I32, tcg_target_call_iarg_regs[3],
837
                 tcg_target_call_iarg_regs[2]);
838
    tcg_out_mov (s, TCG_TYPE_I64, tcg_target_call_iarg_regs[2],
839
                 tcg_target_call_iarg_regs[1]);
840
    tcg_out_mov (s, TCG_TYPE_TL, tcg_target_call_iarg_regs[1],
841
                 tcg_target_call_iarg_regs[0]);
842
    tcg_out_mov (s, TCG_TYPE_PTR, tcg_target_call_iarg_regs[0],
843
                 TCG_AREG0);
844
#endif
845
    tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
846

    
847
    label2_ptr = s->code_ptr;
848
    tcg_out32 (s, B);
849

    
850
    /* label1: fast path */
851
#ifdef FAST_PATH
852
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
853
#endif
854

    
855
    tcg_out32 (s, (LD
856
                   | RT (r0)
857
                   | RA (r0)
858
                   | (offsetof (CPUTLBEntry, addend)
859
                      - offsetof (CPUTLBEntry, addr_write))
860
                   ));
861
    /* r0 = env->tlb_table[mem_index][index].addend */
862
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
863
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
864

    
865
#else  /* !CONFIG_SOFTMMU */
866
#if TARGET_LONG_BITS == 32
867
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
868
#endif
869
    r1 = 3;
870
    r0 = addr_reg;
871
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
872
#endif
873

    
874
#ifdef TARGET_WORDS_BIGENDIAN
875
    bswap = 0;
876
#else
877
    bswap = 1;
878
#endif
879
    switch (opc) {
880
    case 0:
881
        tcg_out32 (s, STBX | SAB (data_reg, rbase, r0));
882
        break;
883
    case 1:
884
        if (bswap)
885
            tcg_out32 (s, STHBRX | SAB (data_reg, rbase, r0));
886
        else
887
            tcg_out32 (s, STHX | SAB (data_reg, rbase, r0));
888
        break;
889
    case 2:
890
        if (bswap)
891
            tcg_out32 (s, STWBRX | SAB (data_reg, rbase, r0));
892
        else
893
            tcg_out32 (s, STWX | SAB (data_reg, rbase, r0));
894
        break;
895
    case 3:
896
        if (bswap) {
897
            tcg_out32 (s, STWBRX | SAB (data_reg, rbase, r0));
898
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
899
            tcg_out_rld (s, RLDICL, 0, data_reg, 32, 0);
900
            tcg_out32 (s, STWBRX | SAB (0, rbase, r1));
901
        }
902
        else tcg_out32 (s, STDX | SAB (data_reg, rbase, r0));
903
        break;
904
    }
905

    
906
#ifdef CONFIG_SOFTMMU
907
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
908
#endif
909
}
910

    
911
static void tcg_target_qemu_prologue (TCGContext *s)
912
{
913
    int i, frame_size;
914
#ifndef __APPLE__
915
    uint64_t addr;
916
#endif
917

    
918
    frame_size = 0
919
        + 8                     /* back chain */
920
        + 8                     /* CR */
921
        + 8                     /* LR */
922
        + 8                     /* compiler doubleword */
923
        + 8                     /* link editor doubleword */
924
        + 8                     /* TOC save area */
925
        + TCG_STATIC_CALL_ARGS_SIZE
926
        + ARRAY_SIZE (tcg_target_callee_save_regs) * 8
927
        + CPU_TEMP_BUF_NLONGS * sizeof(long)
928
        ;
929
    frame_size = (frame_size + 15) & ~15;
930

    
931
    tcg_set_frame (s, TCG_REG_CALL_STACK, frame_size
932
                   - CPU_TEMP_BUF_NLONGS * sizeof (long),
933
                   CPU_TEMP_BUF_NLONGS * sizeof (long));
934

    
935
#ifndef __APPLE__
936
    /* First emit adhoc function descriptor */
937
    addr = (uint64_t) s->code_ptr + 24;
938
    tcg_out32 (s, addr >> 32); tcg_out32 (s, addr); /* entry point */
939
    s->code_ptr += 16;          /* skip TOC and environment pointer */
940
#endif
941

    
942
    /* Prologue */
943
    tcg_out32 (s, MFSPR | RT (0) | LR);
944
    tcg_out32 (s, STDU | RS (1) | RA (1) | (-frame_size & 0xffff));
945
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
946
        tcg_out32 (s, (STD
947
                       | RS (tcg_target_callee_save_regs[i])
948
                       | RA (1)
949
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
950
                       )
951
            );
952
    tcg_out32 (s, STD | RS (0) | RA (1) | (frame_size + 16));
953

    
954
#ifdef CONFIG_USE_GUEST_BASE
955
    if (GUEST_BASE) {
956
        tcg_out_movi (s, TCG_TYPE_I64, TCG_GUEST_BASE_REG, GUEST_BASE);
957
        tcg_regset_set_reg (s->reserved_regs, TCG_GUEST_BASE_REG);
958
    }
959
#endif
960

    
961
    tcg_out_mov (s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
962
    tcg_out32 (s, MTSPR | RS (tcg_target_call_iarg_regs[1]) | CTR);
963
    tcg_out32 (s, BCCTR | BO_ALWAYS);
964

    
965
    /* Epilogue */
966
    tb_ret_addr = s->code_ptr;
967

    
968
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
969
        tcg_out32 (s, (LD
970
                       | RT (tcg_target_callee_save_regs[i])
971
                       | RA (1)
972
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
973
                       )
974
            );
975
    tcg_out32 (s, LD | RT (0) | RA (1) | (frame_size + 16));
976
    tcg_out32 (s, MTSPR | RS (0) | LR);
977
    tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
978
    tcg_out32 (s, BCLR | BO_ALWAYS);
979
}
980

    
981
static void tcg_out_ld (TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
982
                        tcg_target_long arg2)
983
{
984
    if (type == TCG_TYPE_I32)
985
        tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
986
    else
987
        tcg_out_ldsta (s, ret, arg1, arg2, LD, LDX);
988
}
989

    
990
static void tcg_out_st (TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
991
                        tcg_target_long arg2)
992
{
993
    if (type == TCG_TYPE_I32)
994
        tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
995
    else
996
        tcg_out_ldsta (s, arg, arg1, arg2, STD, STDX);
997
}
998

    
999
static void ppc_addi32 (TCGContext *s, int rt, int ra, tcg_target_long si)
1000
{
1001
    if (!si && rt == ra)
1002
        return;
1003

    
1004
    if (si == (int16_t) si)
1005
        tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
1006
    else {
1007
        uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
1008
        tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
1009
        tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
1010
    }
1011
}
1012

    
1013
static void ppc_addi64 (TCGContext *s, int rt, int ra, tcg_target_long si)
1014
{
1015
    /* XXX: suboptimal */
1016
    if (si == (int16_t) si
1017
        || ((((uint64_t) si >> 31) == 0) && (si & 0x8000) == 0))
1018
        ppc_addi32 (s, rt, ra, si);
1019
    else {
1020
        tcg_out_movi (s, TCG_TYPE_I64, 0, si);
1021
        tcg_out32 (s, ADD | RT (rt) | RA (ra));
1022
    }
1023
}
1024

    
1025
static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
1026
                         int const_arg2, int cr, int arch64)
1027
{
1028
    int imm;
1029
    uint32_t op;
1030

    
1031
    switch (cond) {
1032
    case TCG_COND_EQ:
1033
    case TCG_COND_NE:
1034
        if (const_arg2) {
1035
            if ((int16_t) arg2 == arg2) {
1036
                op = CMPI;
1037
                imm = 1;
1038
                break;
1039
            }
1040
            else if ((uint16_t) arg2 == arg2) {
1041
                op = CMPLI;
1042
                imm = 1;
1043
                break;
1044
            }
1045
        }
1046
        op = CMPL;
1047
        imm = 0;
1048
        break;
1049

    
1050
    case TCG_COND_LT:
1051
    case TCG_COND_GE:
1052
    case TCG_COND_LE:
1053
    case TCG_COND_GT:
1054
        if (const_arg2) {
1055
            if ((int16_t) arg2 == arg2) {
1056
                op = CMPI;
1057
                imm = 1;
1058
                break;
1059
            }
1060
        }
1061
        op = CMP;
1062
        imm = 0;
1063
        break;
1064

    
1065
    case TCG_COND_LTU:
1066
    case TCG_COND_GEU:
1067
    case TCG_COND_LEU:
1068
    case TCG_COND_GTU:
1069
        if (const_arg2) {
1070
            if ((uint16_t) arg2 == arg2) {
1071
                op = CMPLI;
1072
                imm = 1;
1073
                break;
1074
            }
1075
        }
1076
        op = CMPL;
1077
        imm = 0;
1078
        break;
1079

    
1080
    default:
1081
        tcg_abort ();
1082
    }
1083
    op |= BF (cr) | (arch64 << 21);
1084

    
1085
    if (imm)
1086
        tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
1087
    else {
1088
        if (const_arg2) {
1089
            tcg_out_movi (s, TCG_TYPE_I64, 0, arg2);
1090
            tcg_out32 (s, op | RA (arg1) | RB (0));
1091
        }
1092
        else
1093
            tcg_out32 (s, op | RA (arg1) | RB (arg2));
1094
    }
1095

    
1096
}
1097

    
1098
static void tcg_out_setcond (TCGContext *s, TCGType type, TCGCond cond,
1099
                             TCGArg arg0, TCGArg arg1, TCGArg arg2,
1100
                             int const_arg2)
1101
{
1102
    int crop, sh, arg;
1103

    
1104
    switch (cond) {
1105
    case TCG_COND_EQ:
1106
        if (const_arg2) {
1107
            if (!arg2) {
1108
                arg = arg1;
1109
            }
1110
            else {
1111
                arg = 0;
1112
                if ((uint16_t) arg2 == arg2) {
1113
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1114
                }
1115
                else {
1116
                    tcg_out_movi (s, type, 0, arg2);
1117
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1118
                }
1119
            }
1120
        }
1121
        else {
1122
            arg = 0;
1123
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1124
        }
1125

    
1126
        if (type == TCG_TYPE_I64) {
1127
            tcg_out32 (s, CNTLZD | RS (arg) | RA (0));
1128
            tcg_out_rld (s, RLDICL, arg0, 0, 58, 6);
1129
        }
1130
        else {
1131
            tcg_out32 (s, CNTLZW | RS (arg) | RA (0));
1132
            tcg_out32 (s, (RLWINM
1133
                           | RA (arg0)
1134
                           | RS (0)
1135
                           | SH (27)
1136
                           | MB (5)
1137
                           | ME (31)
1138
                           )
1139
                );
1140
        }
1141
        break;
1142

    
1143
    case TCG_COND_NE:
1144
        if (const_arg2) {
1145
            if (!arg2) {
1146
                arg = arg1;
1147
            }
1148
            else {
1149
                arg = 0;
1150
                if ((uint16_t) arg2 == arg2) {
1151
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1152
                }
1153
                else {
1154
                    tcg_out_movi (s, type, 0, arg2);
1155
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1156
                }
1157
            }
1158
        }
1159
        else {
1160
            arg = 0;
1161
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1162
        }
1163

    
1164
        if (arg == arg1 && arg1 == arg0) {
1165
            tcg_out32 (s, ADDIC | RT (0) | RA (arg) | 0xffff);
1166
            tcg_out32 (s, SUBFE | TAB (arg0, 0, arg));
1167
        }
1168
        else {
1169
            tcg_out32 (s, ADDIC | RT (arg0) | RA (arg) | 0xffff);
1170
            tcg_out32 (s, SUBFE | TAB (arg0, arg0, arg));
1171
        }
1172
        break;
1173

    
1174
    case TCG_COND_GT:
1175
    case TCG_COND_GTU:
1176
        sh = 30;
1177
        crop = 0;
1178
        goto crtest;
1179

    
1180
    case TCG_COND_LT:
1181
    case TCG_COND_LTU:
1182
        sh = 29;
1183
        crop = 0;
1184
        goto crtest;
1185

    
1186
    case TCG_COND_GE:
1187
    case TCG_COND_GEU:
1188
        sh = 31;
1189
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_LT) | BB (7, CR_LT);
1190
        goto crtest;
1191

    
1192
    case TCG_COND_LE:
1193
    case TCG_COND_LEU:
1194
        sh = 31;
1195
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_GT) | BB (7, CR_GT);
1196
    crtest:
1197
        tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, type == TCG_TYPE_I64);
1198
        if (crop) tcg_out32 (s, crop);
1199
        tcg_out32 (s, MFCR | RT (0));
1200
        tcg_out32 (s, (RLWINM
1201
                       | RA (arg0)
1202
                       | RS (0)
1203
                       | SH (sh)
1204
                       | MB (31)
1205
                       | ME (31)
1206
                       )
1207
            );
1208
        break;
1209

    
1210
    default:
1211
        tcg_abort ();
1212
    }
1213
}
1214

    
1215
static void tcg_out_bc (TCGContext *s, int bc, int label_index)
1216
{
1217
    TCGLabel *l = &s->labels[label_index];
1218

    
1219
    if (l->has_value)
1220
        tcg_out32 (s, bc | reloc_pc14_val (s->code_ptr, l->u.value));
1221
    else {
1222
        uint16_t val = *(uint16_t *) &s->code_ptr[2];
1223

    
1224
        /* Thanks to Andrzej Zaborowski */
1225
        tcg_out32 (s, bc | (val & 0xfffc));
1226
        tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
1227
    }
1228
}
1229

    
1230
static void tcg_out_brcond (TCGContext *s, TCGCond cond,
1231
                            TCGArg arg1, TCGArg arg2, int const_arg2,
1232
                            int label_index, int arch64)
1233
{
1234
    tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, arch64);
1235
    tcg_out_bc (s, tcg_to_bc[cond], label_index);
1236
}
1237

    
1238
void ppc_tb_set_jmp_target (unsigned long jmp_addr, unsigned long addr)
1239
{
1240
    TCGContext s;
1241
    unsigned long patch_size;
1242

    
1243
    s.code_ptr = (uint8_t *) jmp_addr;
1244
    tcg_out_b (&s, 0, addr);
1245
    patch_size = s.code_ptr - (uint8_t *) jmp_addr;
1246
    flush_icache_range (jmp_addr, jmp_addr + patch_size);
1247
}
1248

    
1249
static void tcg_out_op (TCGContext *s, TCGOpcode opc, const TCGArg *args,
1250
                        const int *const_args)
1251
{
1252
    int c;
1253

    
1254
    switch (opc) {
1255
    case INDEX_op_exit_tb:
1256
        tcg_out_movi (s, TCG_TYPE_I64, TCG_REG_R3, args[0]);
1257
        tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1258
        break;
1259
    case INDEX_op_goto_tb:
1260
        if (s->tb_jmp_offset) {
1261
            /* direct jump method */
1262

    
1263
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1264
            s->code_ptr += 28;
1265
        }
1266
        else {
1267
            tcg_abort ();
1268
        }
1269
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1270
        break;
1271
    case INDEX_op_br:
1272
        {
1273
            TCGLabel *l = &s->labels[args[0]];
1274

    
1275
            if (l->has_value) {
1276
                tcg_out_b (s, 0, l->u.value);
1277
            }
1278
            else {
1279
                uint32_t val = *(uint32_t *) s->code_ptr;
1280

    
1281
                /* Thanks to Andrzej Zaborowski */
1282
                tcg_out32 (s, B | (val & 0x3fffffc));
1283
                tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1284
            }
1285
        }
1286
        break;
1287
    case INDEX_op_call:
1288
        tcg_out_call (s, args[0], const_args[0]);
1289
        break;
1290
    case INDEX_op_jmp:
1291
        if (const_args[0]) {
1292
            tcg_out_b (s, 0, args[0]);
1293
        }
1294
        else {
1295
            tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1296
            tcg_out32 (s, BCCTR | BO_ALWAYS);
1297
        }
1298
        break;
1299
    case INDEX_op_movi_i32:
1300
        tcg_out_movi (s, TCG_TYPE_I32, args[0], args[1]);
1301
        break;
1302
    case INDEX_op_movi_i64:
1303
        tcg_out_movi (s, TCG_TYPE_I64, args[0], args[1]);
1304
        break;
1305
    case INDEX_op_ld8u_i32:
1306
    case INDEX_op_ld8u_i64:
1307
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1308
        break;
1309
    case INDEX_op_ld8s_i32:
1310
    case INDEX_op_ld8s_i64:
1311
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1312
        tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1313
        break;
1314
    case INDEX_op_ld16u_i32:
1315
    case INDEX_op_ld16u_i64:
1316
        tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1317
        break;
1318
    case INDEX_op_ld16s_i32:
1319
    case INDEX_op_ld16s_i64:
1320
        tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1321
        break;
1322
    case INDEX_op_ld_i32:
1323
    case INDEX_op_ld32u_i64:
1324
        tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1325
        break;
1326
    case INDEX_op_ld32s_i64:
1327
        tcg_out_ldsta (s, args[0], args[1], args[2], LWA, LWAX);
1328
        break;
1329
    case INDEX_op_ld_i64:
1330
        tcg_out_ldsta (s, args[0], args[1], args[2], LD, LDX);
1331
        break;
1332
    case INDEX_op_st8_i32:
1333
    case INDEX_op_st8_i64:
1334
        tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1335
        break;
1336
    case INDEX_op_st16_i32:
1337
    case INDEX_op_st16_i64:
1338
        tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1339
        break;
1340
    case INDEX_op_st_i32:
1341
    case INDEX_op_st32_i64:
1342
        tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1343
        break;
1344
    case INDEX_op_st_i64:
1345
        tcg_out_ldsta (s, args[0], args[1], args[2], STD, STDX);
1346
        break;
1347

    
1348
    case INDEX_op_add_i32:
1349
        if (const_args[2])
1350
            ppc_addi32 (s, args[0], args[1], args[2]);
1351
        else
1352
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1353
        break;
1354
    case INDEX_op_sub_i32:
1355
        if (const_args[2])
1356
            ppc_addi32 (s, args[0], args[1], -args[2]);
1357
        else
1358
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1359
        break;
1360

    
1361
    case INDEX_op_and_i64:
1362
    case INDEX_op_and_i32:
1363
        if (const_args[2]) {
1364
            if ((args[2] & 0xffff) == args[2])
1365
                tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | args[2]);
1366
            else if ((args[2] & 0xffff0000) == args[2])
1367
                tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1368
                           | ((args[2] >> 16) & 0xffff));
1369
            else {
1370
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1371
                                  ? TCG_TYPE_I32
1372
                                  : TCG_TYPE_I64),
1373
                              0, args[2]);
1374
                tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1375
            }
1376
        }
1377
        else
1378
            tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1379
        break;
1380
    case INDEX_op_or_i64:
1381
    case INDEX_op_or_i32:
1382
        if (const_args[2]) {
1383
            if (args[2] & 0xffff) {
1384
                tcg_out32 (s, ORI | RS (args[1]) | RA (args[0])
1385
                           | (args[2] & 0xffff));
1386
                if (args[2] >> 16)
1387
                    tcg_out32 (s, ORIS | RS (args[0])  | RA (args[0])
1388
                               | ((args[2] >> 16) & 0xffff));
1389
            }
1390
            else {
1391
                tcg_out32 (s, ORIS | RS (args[1])  | RA (args[0])
1392
                           | ((args[2] >> 16) & 0xffff));
1393
            }
1394
        }
1395
        else
1396
            tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1397
        break;
1398
    case INDEX_op_xor_i64:
1399
    case INDEX_op_xor_i32:
1400
        if (const_args[2]) {
1401
            if ((args[2] & 0xffff) == args[2])
1402
                tcg_out32 (s, XORI | RS (args[1])  | RA (args[0])
1403
                           | (args[2] & 0xffff));
1404
            else if ((args[2] & 0xffff0000) == args[2])
1405
                tcg_out32 (s, XORIS | RS (args[1])  | RA (args[0])
1406
                           | ((args[2] >> 16) & 0xffff));
1407
            else {
1408
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1409
                                  ? TCG_TYPE_I32
1410
                                  : TCG_TYPE_I64),
1411
                              0, args[2]);
1412
                tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1413
            }
1414
        }
1415
        else
1416
            tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1417
        break;
1418

    
1419
    case INDEX_op_mul_i32:
1420
        if (const_args[2]) {
1421
            if (args[2] == (int16_t) args[2])
1422
                tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1423
                           | (args[2] & 0xffff));
1424
            else {
1425
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1426
                tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1427
            }
1428
        }
1429
        else
1430
            tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1431
        break;
1432

    
1433
    case INDEX_op_div_i32:
1434
        tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1435
        break;
1436

    
1437
    case INDEX_op_divu_i32:
1438
        tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1439
        break;
1440

    
1441
    case INDEX_op_rem_i32:
1442
        tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1443
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1444
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1445
        break;
1446

    
1447
    case INDEX_op_remu_i32:
1448
        tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1449
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1450
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1451
        break;
1452

    
1453
    case INDEX_op_shl_i32:
1454
        if (const_args[2]) {
1455
            tcg_out32 (s, (RLWINM
1456
                           | RA (args[0])
1457
                           | RS (args[1])
1458
                           | SH (args[2])
1459
                           | MB (0)
1460
                           | ME (31 - args[2])
1461
                           )
1462
                );
1463
        }
1464
        else
1465
            tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1466
        break;
1467
    case INDEX_op_shr_i32:
1468
        if (const_args[2]) {
1469
            tcg_out32 (s, (RLWINM
1470
                           | RA (args[0])
1471
                           | RS (args[1])
1472
                           | SH (32 - args[2])
1473
                           | MB (args[2])
1474
                           | ME (31)
1475
                           )
1476
                );
1477
        }
1478
        else
1479
            tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1480
        break;
1481
    case INDEX_op_sar_i32:
1482
        if (const_args[2])
1483
            tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1484
        else
1485
            tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1486
        break;
1487

    
1488
    case INDEX_op_brcond_i32:
1489
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 0);
1490
        break;
1491

    
1492
    case INDEX_op_brcond_i64:
1493
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 1);
1494
        break;
1495

    
1496
    case INDEX_op_neg_i32:
1497
    case INDEX_op_neg_i64:
1498
        tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1499
        break;
1500

    
1501
    case INDEX_op_not_i32:
1502
    case INDEX_op_not_i64:
1503
        tcg_out32 (s, NOR | SAB (args[1], args[0], args[1]));
1504
        break;
1505

    
1506
    case INDEX_op_add_i64:
1507
        if (const_args[2])
1508
            ppc_addi64 (s, args[0], args[1], args[2]);
1509
        else
1510
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1511
        break;
1512
    case INDEX_op_sub_i64:
1513
        if (const_args[2])
1514
            ppc_addi64 (s, args[0], args[1], -args[2]);
1515
        else
1516
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1517
        break;
1518

    
1519
    case INDEX_op_shl_i64:
1520
        if (const_args[2])
1521
            tcg_out_rld (s, RLDICR, args[0], args[1], args[2], 63 - args[2]);
1522
        else
1523
            tcg_out32 (s, SLD | SAB (args[1], args[0], args[2]));
1524
        break;
1525
    case INDEX_op_shr_i64:
1526
        if (const_args[2])
1527
            tcg_out_rld (s, RLDICL, args[0], args[1], 64 - args[2], args[2]);
1528
        else
1529
            tcg_out32 (s, SRD | SAB (args[1], args[0], args[2]));
1530
        break;
1531
    case INDEX_op_sar_i64:
1532
        if (const_args[2]) {
1533
            int sh = SH (args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1534
            tcg_out32 (s, SRADI | RA (args[0]) | RS (args[1]) | sh);
1535
        }
1536
        else
1537
            tcg_out32 (s, SRAD | SAB (args[1], args[0], args[2]));
1538
        break;
1539

    
1540
    case INDEX_op_mul_i64:
1541
        tcg_out32 (s, MULLD | TAB (args[0], args[1], args[2]));
1542
        break;
1543
    case INDEX_op_div_i64:
1544
        tcg_out32 (s, DIVD | TAB (args[0], args[1], args[2]));
1545
        break;
1546
    case INDEX_op_divu_i64:
1547
        tcg_out32 (s, DIVDU | TAB (args[0], args[1], args[2]));
1548
        break;
1549
    case INDEX_op_rem_i64:
1550
        tcg_out32 (s, DIVD | TAB (0, args[1], args[2]));
1551
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1552
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1553
        break;
1554
    case INDEX_op_remu_i64:
1555
        tcg_out32 (s, DIVDU | TAB (0, args[1], args[2]));
1556
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1557
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1558
        break;
1559

    
1560
    case INDEX_op_qemu_ld8u:
1561
        tcg_out_qemu_ld (s, args, 0);
1562
        break;
1563
    case INDEX_op_qemu_ld8s:
1564
        tcg_out_qemu_ld (s, args, 0 | 4);
1565
        break;
1566
    case INDEX_op_qemu_ld16u:
1567
        tcg_out_qemu_ld (s, args, 1);
1568
        break;
1569
    case INDEX_op_qemu_ld16s:
1570
        tcg_out_qemu_ld (s, args, 1 | 4);
1571
        break;
1572
    case INDEX_op_qemu_ld32:
1573
    case INDEX_op_qemu_ld32u:
1574
        tcg_out_qemu_ld (s, args, 2);
1575
        break;
1576
    case INDEX_op_qemu_ld32s:
1577
        tcg_out_qemu_ld (s, args, 2 | 4);
1578
        break;
1579
    case INDEX_op_qemu_ld64:
1580
        tcg_out_qemu_ld (s, args, 3);
1581
        break;
1582
    case INDEX_op_qemu_st8:
1583
        tcg_out_qemu_st (s, args, 0);
1584
        break;
1585
    case INDEX_op_qemu_st16:
1586
        tcg_out_qemu_st (s, args, 1);
1587
        break;
1588
    case INDEX_op_qemu_st32:
1589
        tcg_out_qemu_st (s, args, 2);
1590
        break;
1591
    case INDEX_op_qemu_st64:
1592
        tcg_out_qemu_st (s, args, 3);
1593
        break;
1594

    
1595
    case INDEX_op_ext8s_i32:
1596
    case INDEX_op_ext8s_i64:
1597
        c = EXTSB;
1598
        goto gen_ext;
1599
    case INDEX_op_ext16s_i32:
1600
    case INDEX_op_ext16s_i64:
1601
        c = EXTSH;
1602
        goto gen_ext;
1603
    case INDEX_op_ext32s_i64:
1604
        c = EXTSW;
1605
        goto gen_ext;
1606
    gen_ext:
1607
        tcg_out32 (s, c | RS (args[1]) | RA (args[0]));
1608
        break;
1609

    
1610
    case INDEX_op_ext32u_i64:
1611
        tcg_out_rld (s, RLDICL, args[0], args[1], 0, 32);
1612
        break;
1613

    
1614
    case INDEX_op_setcond_i32:
1615
        tcg_out_setcond (s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
1616
                         const_args[2]);
1617
        break;
1618
    case INDEX_op_setcond_i64:
1619
        tcg_out_setcond (s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
1620
                         const_args[2]);
1621
        break;
1622

    
1623
    default:
1624
        tcg_dump_ops (s, stderr);
1625
        tcg_abort ();
1626
    }
1627
}
1628

    
1629
static const TCGTargetOpDef ppc_op_defs[] = {
1630
    { INDEX_op_exit_tb, { } },
1631
    { INDEX_op_goto_tb, { } },
1632
    { INDEX_op_call, { "ri" } },
1633
    { INDEX_op_jmp, { "ri" } },
1634
    { INDEX_op_br, { } },
1635

    
1636
    { INDEX_op_mov_i32, { "r", "r" } },
1637
    { INDEX_op_mov_i64, { "r", "r" } },
1638
    { INDEX_op_movi_i32, { "r" } },
1639
    { INDEX_op_movi_i64, { "r" } },
1640

    
1641
    { INDEX_op_ld8u_i32, { "r", "r" } },
1642
    { INDEX_op_ld8s_i32, { "r", "r" } },
1643
    { INDEX_op_ld16u_i32, { "r", "r" } },
1644
    { INDEX_op_ld16s_i32, { "r", "r" } },
1645
    { INDEX_op_ld_i32, { "r", "r" } },
1646
    { INDEX_op_ld_i64, { "r", "r" } },
1647
    { INDEX_op_st8_i32, { "r", "r" } },
1648
    { INDEX_op_st8_i64, { "r", "r" } },
1649
    { INDEX_op_st16_i32, { "r", "r" } },
1650
    { INDEX_op_st16_i64, { "r", "r" } },
1651
    { INDEX_op_st_i32, { "r", "r" } },
1652
    { INDEX_op_st_i64, { "r", "r" } },
1653
    { INDEX_op_st32_i64, { "r", "r" } },
1654

    
1655
    { INDEX_op_ld8u_i64, { "r", "r" } },
1656
    { INDEX_op_ld8s_i64, { "r", "r" } },
1657
    { INDEX_op_ld16u_i64, { "r", "r" } },
1658
    { INDEX_op_ld16s_i64, { "r", "r" } },
1659
    { INDEX_op_ld32u_i64, { "r", "r" } },
1660
    { INDEX_op_ld32s_i64, { "r", "r" } },
1661

    
1662
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1663
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1664
    { INDEX_op_div_i32, { "r", "r", "r" } },
1665
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1666
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1667
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1668
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1669
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1670
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1671
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1672

    
1673
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1674
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1675
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1676

    
1677
    { INDEX_op_brcond_i32, { "r", "ri" } },
1678
    { INDEX_op_brcond_i64, { "r", "ri" } },
1679

    
1680
    { INDEX_op_neg_i32, { "r", "r" } },
1681
    { INDEX_op_not_i32, { "r", "r" } },
1682

    
1683
    { INDEX_op_add_i64, { "r", "r", "ri" } },
1684
    { INDEX_op_sub_i64, { "r", "r", "ri" } },
1685
    { INDEX_op_and_i64, { "r", "r", "rZ" } },
1686
    { INDEX_op_or_i64, { "r", "r", "rZ" } },
1687
    { INDEX_op_xor_i64, { "r", "r", "rZ" } },
1688

    
1689
    { INDEX_op_shl_i64, { "r", "r", "ri" } },
1690
    { INDEX_op_shr_i64, { "r", "r", "ri" } },
1691
    { INDEX_op_sar_i64, { "r", "r", "ri" } },
1692

    
1693
    { INDEX_op_mul_i64, { "r", "r", "r" } },
1694
    { INDEX_op_div_i64, { "r", "r", "r" } },
1695
    { INDEX_op_divu_i64, { "r", "r", "r" } },
1696
    { INDEX_op_rem_i64, { "r", "r", "r" } },
1697
    { INDEX_op_remu_i64, { "r", "r", "r" } },
1698

    
1699
    { INDEX_op_neg_i64, { "r", "r" } },
1700
    { INDEX_op_not_i64, { "r", "r" } },
1701

    
1702
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1703
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1704
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1705
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1706
    { INDEX_op_qemu_ld32, { "r", "L" } },
1707
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1708
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1709
    { INDEX_op_qemu_ld64, { "r", "L" } },
1710

    
1711
    { INDEX_op_qemu_st8, { "S", "S" } },
1712
    { INDEX_op_qemu_st16, { "S", "S" } },
1713
    { INDEX_op_qemu_st32, { "S", "S" } },
1714
    { INDEX_op_qemu_st64, { "S", "S" } },
1715

    
1716
    { INDEX_op_ext8s_i32, { "r", "r" } },
1717
    { INDEX_op_ext16s_i32, { "r", "r" } },
1718
    { INDEX_op_ext8s_i64, { "r", "r" } },
1719
    { INDEX_op_ext16s_i64, { "r", "r" } },
1720
    { INDEX_op_ext32s_i64, { "r", "r" } },
1721
    { INDEX_op_ext32u_i64, { "r", "r" } },
1722

    
1723
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1724
    { INDEX_op_setcond_i64, { "r", "r", "ri" } },
1725

    
1726
    { -1 },
1727
};
1728

    
1729
static void tcg_target_init (TCGContext *s)
1730
{
1731
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1732
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1733
    tcg_regset_set32 (tcg_target_call_clobber_regs, 0,
1734
                     (1 << TCG_REG_R0) |
1735
#ifdef __APPLE__
1736
                     (1 << TCG_REG_R2) |
1737
#endif
1738
                     (1 << TCG_REG_R3) |
1739
                     (1 << TCG_REG_R4) |
1740
                     (1 << TCG_REG_R5) |
1741
                     (1 << TCG_REG_R6) |
1742
                     (1 << TCG_REG_R7) |
1743
                     (1 << TCG_REG_R8) |
1744
                     (1 << TCG_REG_R9) |
1745
                     (1 << TCG_REG_R10) |
1746
                     (1 << TCG_REG_R11) |
1747
                     (1 << TCG_REG_R12)
1748
        );
1749

    
1750
    tcg_regset_clear (s->reserved_regs);
1751
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R0);
1752
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R1);
1753
#ifndef __APPLE__
1754
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R2);
1755
#endif
1756
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R13);
1757

    
1758
    tcg_add_target_add_op_defs (ppc_op_defs);
1759
}