Statistics
| Branch: | Revision:

root / tcg / ppc / tcg-target.c @ aa77bebd

History | View | Annotate | Download (50.8 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
static uint8_t *tb_ret_addr;
26

    
27
#ifdef _CALL_DARWIN
28
#define LINKAGE_AREA_SIZE 24
29
#define LR_OFFSET 8
30
#elif defined _CALL_AIX
31
#define LINKAGE_AREA_SIZE 52
32
#define LR_OFFSET 8
33
#else
34
#define LINKAGE_AREA_SIZE 8
35
#define LR_OFFSET 4
36
#endif
37

    
38
#define FAST_PATH
39

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

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

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

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

    
126
static const int tcg_target_call_iarg_regs[] = {
127
    TCG_REG_R3,
128
    TCG_REG_R4,
129
    TCG_REG_R5,
130
    TCG_REG_R6,
131
    TCG_REG_R7,
132
    TCG_REG_R8,
133
    TCG_REG_R9,
134
    TCG_REG_R10
135
};
136

    
137
static const int tcg_target_call_oarg_regs[2] = {
138
    TCG_REG_R3,
139
    TCG_REG_R4
140
};
141

    
142
static const int tcg_target_callee_save_regs[] = {
143
#ifdef _CALL_DARWIN
144
    TCG_REG_R11,
145
    TCG_REG_R13,
146
#endif
147
#ifdef _CALL_AIX
148
    TCG_REG_R13,
149
#endif
150
    TCG_REG_R14,
151
    TCG_REG_R15,
152
    TCG_REG_R16,
153
    TCG_REG_R17,
154
    TCG_REG_R18,
155
    TCG_REG_R19,
156
    TCG_REG_R20,
157
    TCG_REG_R21,
158
    TCG_REG_R22,
159
    TCG_REG_R23,
160
    TCG_REG_R24,
161
    TCG_REG_R25,
162
    TCG_REG_R26,
163
    /* TCG_REG_R27, */ /* currently used for the global env, so no
164
                          need to save */
165
    TCG_REG_R28,
166
    TCG_REG_R29,
167
    TCG_REG_R30,
168
    TCG_REG_R31
169
};
170

    
171
static uint32_t reloc_pc24_val (void *pc, tcg_target_long target)
172
{
173
    tcg_target_long disp;
174

    
175
    disp = target - (tcg_target_long) pc;
176
    if ((disp << 6) >> 6 != disp)
177
        tcg_abort ();
178

    
179
    return disp & 0x3fffffc;
180
}
181

    
182
static void reloc_pc24 (void *pc, tcg_target_long target)
183
{
184
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0x3fffffc)
185
        | reloc_pc24_val (pc, target);
186
}
187

    
188
static uint16_t reloc_pc14_val (void *pc, tcg_target_long target)
189
{
190
    tcg_target_long disp;
191

    
192
    disp = target - (tcg_target_long) pc;
193
    if (disp != (int16_t) disp)
194
        tcg_abort ();
195

    
196
    return disp & 0xfffc;
197
}
198

    
199
static void reloc_pc14 (void *pc, tcg_target_long target)
200
{
201
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0xfffc)
202
        | reloc_pc14_val (pc, target);
203
}
204

    
205
static void patch_reloc(uint8_t *code_ptr, int type,
206
                        tcg_target_long value, tcg_target_long addend)
207
{
208
    value += addend;
209
    switch (type) {
210
    case R_PPC_REL14:
211
        reloc_pc14 (code_ptr, value);
212
        break;
213
    case R_PPC_REL24:
214
        reloc_pc24 (code_ptr, value);
215
        break;
216
    default:
217
        tcg_abort();
218
    }
219
}
220

    
221
/* maximum number of register used for input function arguments */
222
static int tcg_target_get_call_iarg_regs_count(int flags)
223
{
224
    return ARRAY_SIZE (tcg_target_call_iarg_regs);
225
}
226

    
227
/* parse target specific constraints */
228
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
229
{
230
    const char *ct_str;
231

    
232
    ct_str = *pct_str;
233
    switch (ct_str[0]) {
234
    case 'A': case 'B': case 'C': case 'D':
235
        ct->ct |= TCG_CT_REG;
236
        tcg_regset_set_reg(ct->u.regs, 3 + ct_str[0] - 'A');
237
        break;
238
    case 'r':
239
        ct->ct |= TCG_CT_REG;
240
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
241
        break;
242
#ifdef CONFIG_SOFTMMU
243
    case 'L':                   /* qemu_ld constraint */
244
        ct->ct |= TCG_CT_REG;
245
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
246
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
247
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
248
        break;
249
    case 'K':                   /* qemu_st[8..32] constraint */
250
        ct->ct |= TCG_CT_REG;
251
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
252
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
253
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
254
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
255
#if TARGET_LONG_BITS == 64
256
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R6);
257
#endif
258
        break;
259
    case 'M':                   /* qemu_st64 constraint */
260
        ct->ct |= TCG_CT_REG;
261
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
262
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
263
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R4);
264
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R5);
265
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R6);
266
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R7);
267
        break;
268
#else
269
    case 'L':
270
    case 'K':
271
        ct->ct |= TCG_CT_REG;
272
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
273
        break;
274
    case 'M':
275
        ct->ct |= TCG_CT_REG;
276
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
277
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R3);
278
        break;
279
#endif
280
    default:
281
        return -1;
282
    }
283
    ct_str++;
284
    *pct_str = ct_str;
285
    return 0;
286
}
287

    
288
/* test if a constant matches the constraint */
289
static int tcg_target_const_match(tcg_target_long val,
290
                                  const TCGArgConstraint *arg_ct)
291
{
292
    int ct;
293

    
294
    ct = arg_ct->ct;
295
    if (ct & TCG_CT_CONST)
296
        return 1;
297
    return 0;
298
}
299

    
300
#define OPCD(opc) ((opc)<<26)
301
#define XO31(opc) (OPCD(31)|((opc)<<1))
302
#define XO19(opc) (OPCD(19)|((opc)<<1))
303

    
304
#define B      OPCD(18)
305
#define BC     OPCD(16)
306
#define LBZ    OPCD(34)
307
#define LHZ    OPCD(40)
308
#define LHA    OPCD(42)
309
#define LWZ    OPCD(32)
310
#define STB    OPCD(38)
311
#define STH    OPCD(44)
312
#define STW    OPCD(36)
313

    
314
#define ADDIC  OPCD(12)
315
#define ADDI   OPCD(14)
316
#define ADDIS  OPCD(15)
317
#define ORI    OPCD(24)
318
#define ORIS   OPCD(25)
319
#define XORI   OPCD(26)
320
#define XORIS  OPCD(27)
321
#define ANDI   OPCD(28)
322
#define ANDIS  OPCD(29)
323
#define MULLI  OPCD( 7)
324
#define CMPLI  OPCD(10)
325
#define CMPI   OPCD(11)
326
#define SUBFIC OPCD( 8)
327

    
328
#define LWZU   OPCD(33)
329
#define STWU   OPCD(37)
330

    
331
#define RLWINM OPCD(21)
332
#define RLWNM  OPCD(23)
333

    
334
#define BCLR   XO19( 16)
335
#define BCCTR  XO19(528)
336
#define CRAND  XO19(257)
337
#define CRANDC XO19(129)
338
#define CRNAND XO19(225)
339
#define CROR   XO19(449)
340
#define CRNOR  XO19( 33)
341

    
342
#define EXTSB  XO31(954)
343
#define EXTSH  XO31(922)
344
#define ADD    XO31(266)
345
#define ADDE   XO31(138)
346
#define ADDC   XO31( 10)
347
#define AND    XO31( 28)
348
#define SUBF   XO31( 40)
349
#define SUBFC  XO31(  8)
350
#define SUBFE  XO31(136)
351
#define OR     XO31(444)
352
#define XOR    XO31(316)
353
#define MULLW  XO31(235)
354
#define MULHWU XO31( 11)
355
#define DIVW   XO31(491)
356
#define DIVWU  XO31(459)
357
#define CMP    XO31(  0)
358
#define CMPL   XO31( 32)
359
#define LHBRX  XO31(790)
360
#define LWBRX  XO31(534)
361
#define STHBRX XO31(918)
362
#define STWBRX XO31(662)
363
#define MFSPR  XO31(339)
364
#define MTSPR  XO31(467)
365
#define SRAWI  XO31(824)
366
#define NEG    XO31(104)
367
#define MFCR   XO31( 19)
368
#define CNTLZW XO31( 26)
369
#define NOR    XO31(124)
370
#define ANDC   XO31( 60)
371
#define ORC    XO31(412)
372
#define EQV    XO31(284)
373
#define NAND   XO31(476)
374

    
375
#define LBZX   XO31( 87)
376
#define LHZX   XO31(279)
377
#define LHAX   XO31(343)
378
#define LWZX   XO31( 23)
379
#define STBX   XO31(215)
380
#define STHX   XO31(407)
381
#define STWX   XO31(151)
382

    
383
#define SPR(a,b) ((((a)<<5)|(b))<<11)
384
#define LR     SPR(8, 0)
385
#define CTR    SPR(9, 0)
386

    
387
#define SLW    XO31( 24)
388
#define SRW    XO31(536)
389
#define SRAW   XO31(792)
390

    
391
#define TW     XO31(4)
392
#define TRAP   (TW | TO (31))
393

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

    
404
#define LK    1
405

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

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

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

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

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

    
439
static void tcg_out_mov(TCGContext *s, int ret, int arg)
440
{
441
    tcg_out32 (s, OR | SAB (arg, ret, arg));
442
}
443

    
444
static void tcg_out_movi(TCGContext *s, TCGType type,
445
                         int ret, tcg_target_long arg)
446
{
447
    if (arg == (int16_t) arg)
448
        tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
449
    else {
450
        tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
451
        if (arg & 0xffff)
452
            tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
453
    }
454
}
455

    
456
static void tcg_out_ldst (TCGContext *s, int ret, int addr,
457
                          int offset, int op1, int op2)
458
{
459
    if (offset == (int16_t) offset)
460
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
461
    else {
462
        tcg_out_movi (s, TCG_TYPE_I32, 0, offset);
463
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
464
    }
465
}
466

    
467
static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
468
{
469
    tcg_target_long disp;
470

    
471
    disp = target - (tcg_target_long) s->code_ptr;
472
    if ((disp << 6) >> 6 == disp)
473
        tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
474
    else {
475
        tcg_out_movi (s, TCG_TYPE_I32, 0, (tcg_target_long) target);
476
        tcg_out32 (s, MTSPR | RS (0) | CTR);
477
        tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
478
    }
479
}
480

    
481
static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
482
{
483
#ifdef _CALL_AIX
484
    int reg;
485

    
486
    if (const_arg) {
487
        reg = 2;
488
        tcg_out_movi (s, TCG_TYPE_I32, reg, arg);
489
    }
490
    else reg = arg;
491

    
492
    tcg_out32 (s, LWZ | RT (0) | RA (reg));
493
    tcg_out32 (s, MTSPR | RA (0) | CTR);
494
    tcg_out32 (s, LWZ | RT (2) | RA (reg) | 4);
495
    tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
496
#else
497
    if (const_arg) {
498
        tcg_out_b (s, LK, arg);
499
    }
500
    else {
501
        tcg_out32 (s, MTSPR | RS (arg) | LR);
502
        tcg_out32 (s, BCLR | BO_ALWAYS | LK);
503
    }
504
#endif
505
}
506

    
507
#if defined(CONFIG_SOFTMMU)
508

    
509
#include "../../softmmu_defs.h"
510

    
511
static void *qemu_ld_helpers[4] = {
512
    __ldb_mmu,
513
    __ldw_mmu,
514
    __ldl_mmu,
515
    __ldq_mmu,
516
};
517

    
518
static void *qemu_st_helpers[4] = {
519
    __stb_mmu,
520
    __stw_mmu,
521
    __stl_mmu,
522
    __stq_mmu,
523
};
524
#endif
525

    
526
static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
527
{
528
    int addr_reg, data_reg, data_reg2, r0, r1, rbase, mem_index, s_bits, bswap;
529
#ifdef CONFIG_SOFTMMU
530
    int r2;
531
    void *label1_ptr, *label2_ptr;
532
#endif
533
#if TARGET_LONG_BITS == 64
534
    int addr_reg2;
535
#endif
536

    
537
    data_reg = *args++;
538
    if (opc == 3)
539
        data_reg2 = *args++;
540
    else
541
        data_reg2 = 0;
542
    addr_reg = *args++;
543
#if TARGET_LONG_BITS == 64
544
    addr_reg2 = *args++;
545
#endif
546
    mem_index = *args;
547
    s_bits = opc & 3;
548

    
549
#ifdef CONFIG_SOFTMMU
550
    r0 = 3;
551
    r1 = 4;
552
    r2 = 0;
553
    rbase = 0;
554

    
555
    tcg_out32 (s, (RLWINM
556
                   | RA (r0)
557
                   | RS (addr_reg)
558
                   | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
559
                   | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
560
                   | ME (31 - CPU_TLB_ENTRY_BITS)
561
                   )
562
        );
563
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
564
    tcg_out32 (s, (LWZU
565
                   | RT (r1)
566
                   | RA (r0)
567
                   | offsetof (CPUState, tlb_table[mem_index][0].addr_read)
568
                   )
569
        );
570
    tcg_out32 (s, (RLWINM
571
                   | RA (r2)
572
                   | RS (addr_reg)
573
                   | SH (0)
574
                   | MB ((32 - s_bits) & 31)
575
                   | ME (31 - TARGET_PAGE_BITS)
576
                   )
577
        );
578

    
579
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1));
580
#if TARGET_LONG_BITS == 64
581
    tcg_out32 (s, LWZ | RT (r1) | RA (r0) | 4);
582
    tcg_out32 (s, CMP | BF (6) | RA (addr_reg2) | RB (r1));
583
    tcg_out32 (s, CRAND | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, CR_EQ));
584
#endif
585

    
586
    label1_ptr = s->code_ptr;
587
#ifdef FAST_PATH
588
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
589
#endif
590

    
591
    /* slow path */
592
#if TARGET_LONG_BITS == 32
593
    tcg_out_mov (s, 3, addr_reg);
594
    tcg_out_movi (s, TCG_TYPE_I32, 4, mem_index);
595
#else
596
    tcg_out_mov (s, 3, addr_reg2);
597
    tcg_out_mov (s, 4, addr_reg);
598
    tcg_out_movi (s, TCG_TYPE_I32, 5, mem_index);
599
#endif
600

    
601
    tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
602
    switch (opc) {
603
    case 0|4:
604
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
605
        break;
606
    case 1|4:
607
        tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
608
        break;
609
    case 0:
610
    case 1:
611
    case 2:
612
        if (data_reg != 3)
613
            tcg_out_mov (s, data_reg, 3);
614
        break;
615
    case 3:
616
        if (data_reg == 3) {
617
            if (data_reg2 == 4) {
618
                tcg_out_mov (s, 0, 4);
619
                tcg_out_mov (s, 4, 3);
620
                tcg_out_mov (s, 3, 0);
621
            }
622
            else {
623
                tcg_out_mov (s, data_reg2, 3);
624
                tcg_out_mov (s, 3, 4);
625
            }
626
        }
627
        else {
628
            if (data_reg != 4) tcg_out_mov (s, data_reg, 4);
629
            if (data_reg2 != 3) tcg_out_mov (s, data_reg2, 3);
630
        }
631
        break;
632
    }
633
    label2_ptr = s->code_ptr;
634
    tcg_out32 (s, B);
635

    
636
    /* label1: fast path */
637
#ifdef FAST_PATH
638
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
639
#endif
640

    
641
    /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
642
    tcg_out32 (s, (LWZ
643
                   | RT (r0)
644
                   | RA (r0)
645
                   | (offsetof (CPUTLBEntry, addend)
646
                      - offsetof (CPUTLBEntry, addr_read))
647
                   ));
648
    /* r0 = env->tlb_table[mem_index][index].addend */
649
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
650
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
651

    
652
#else  /* !CONFIG_SOFTMMU */
653
    r0 = addr_reg;
654
    r1 = 3;
655
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
656
#endif
657

    
658
#ifdef TARGET_WORDS_BIGENDIAN
659
    bswap = 0;
660
#else
661
    bswap = 1;
662
#endif
663

    
664
    switch (opc) {
665
    default:
666
    case 0:
667
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
668
        break;
669
    case 0|4:
670
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
671
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
672
        break;
673
    case 1:
674
        if (bswap)
675
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
676
        else
677
            tcg_out32 (s, LHZX | TAB (data_reg, rbase, r0));
678
        break;
679
    case 1|4:
680
        if (bswap) {
681
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
682
            tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
683
        }
684
        else tcg_out32 (s, LHAX | TAB (data_reg, rbase, r0));
685
        break;
686
    case 2:
687
        if (bswap)
688
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
689
        else
690
            tcg_out32 (s, LWZX | TAB (data_reg, rbase, r0));
691
        break;
692
    case 3:
693
        if (bswap) {
694
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
695
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
696
            tcg_out32 (s, LWBRX | TAB (data_reg2, rbase, r1));
697
        }
698
        else {
699
#ifdef CONFIG_USE_GUEST_BASE
700
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
701
            tcg_out32 (s, LWZX | TAB (data_reg2, rbase, r0));
702
            tcg_out32 (s, LWZX | TAB (data_reg, rbase, r1));
703
#else
704
            if (r0 == data_reg2) {
705
                tcg_out32 (s, LWZ | RT (0) | RA (r0));
706
                tcg_out32 (s, LWZ | RT (data_reg) | RA (r0) | 4);
707
                tcg_out_mov (s, data_reg2, 0);
708
            }
709
            else {
710
                tcg_out32 (s, LWZ | RT (data_reg2) | RA (r0));
711
                tcg_out32 (s, LWZ | RT (data_reg) | RA (r0) | 4);
712
            }
713
#endif
714
        }
715
        break;
716
    }
717

    
718
#ifdef CONFIG_SOFTMMU
719
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
720
#endif
721
}
722

    
723
static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
724
{
725
    int addr_reg, r0, r1, data_reg, data_reg2, mem_index, bswap, rbase;
726
#ifdef CONFIG_SOFTMMU
727
    int r2, ir;
728
    void *label1_ptr, *label2_ptr;
729
#endif
730
#if TARGET_LONG_BITS == 64
731
    int addr_reg2;
732
#endif
733

    
734
    data_reg = *args++;
735
    if (opc == 3)
736
        data_reg2 = *args++;
737
    else
738
        data_reg2 = 0;
739
    addr_reg = *args++;
740
#if TARGET_LONG_BITS == 64
741
    addr_reg2 = *args++;
742
#endif
743
    mem_index = *args;
744

    
745
#ifdef CONFIG_SOFTMMU
746
    r0 = 3;
747
    r1 = 4;
748
    r2 = 0;
749
    rbase = 0;
750

    
751
    tcg_out32 (s, (RLWINM
752
                   | RA (r0)
753
                   | RS (addr_reg)
754
                   | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
755
                   | MB (32 - (CPU_TLB_ENTRY_BITS + CPU_TLB_BITS))
756
                   | ME (31 - CPU_TLB_ENTRY_BITS)
757
                   )
758
        );
759
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
760
    tcg_out32 (s, (LWZU
761
                   | RT (r1)
762
                   | RA (r0)
763
                   | offsetof (CPUState, tlb_table[mem_index][0].addr_write)
764
                   )
765
        );
766
    tcg_out32 (s, (RLWINM
767
                   | RA (r2)
768
                   | RS (addr_reg)
769
                   | SH (0)
770
                   | MB ((32 - opc) & 31)
771
                   | ME (31 - TARGET_PAGE_BITS)
772
                   )
773
        );
774

    
775
    tcg_out32 (s, CMP | (7 << 23) | RA (r2) | RB (r1));
776
#if TARGET_LONG_BITS == 64
777
    tcg_out32 (s, LWZ | RT (r1) | RA (r0) | 4);
778
    tcg_out32 (s, CMP | BF (6) | RA (addr_reg2) | RB (r1));
779
    tcg_out32 (s, CRAND | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, CR_EQ));
780
#endif
781

    
782
    label1_ptr = s->code_ptr;
783
#ifdef FAST_PATH
784
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
785
#endif
786

    
787
    /* slow path */
788
#if TARGET_LONG_BITS == 32
789
    tcg_out_mov (s, 3, addr_reg);
790
    ir = 4;
791
#else
792
    tcg_out_mov (s, 3, addr_reg2);
793
    tcg_out_mov (s, 4, addr_reg);
794
#ifdef TCG_TARGET_CALL_ALIGN_ARGS
795
    ir = 5;
796
#else
797
    ir = 4;
798
#endif
799
#endif
800

    
801
    switch (opc) {
802
    case 0:
803
        tcg_out32 (s, (RLWINM
804
                       | RA (ir)
805
                       | RS (data_reg)
806
                       | SH (0)
807
                       | MB (24)
808
                       | ME (31)));
809
        break;
810
    case 1:
811
        tcg_out32 (s, (RLWINM
812
                       | RA (ir)
813
                       | RS (data_reg)
814
                       | SH (0)
815
                       | MB (16)
816
                       | ME (31)));
817
        break;
818
    case 2:
819
        tcg_out_mov (s, ir, data_reg);
820
        break;
821
    case 3:
822
#ifdef TCG_TARGET_CALL_ALIGN_ARGS
823
        ir = 5;
824
#endif
825
        tcg_out_mov (s, ir++, data_reg2);
826
        tcg_out_mov (s, ir, data_reg);
827
        break;
828
    }
829
    ir++;
830

    
831
    tcg_out_movi (s, TCG_TYPE_I32, ir, mem_index);
832
    tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
833
    label2_ptr = s->code_ptr;
834
    tcg_out32 (s, B);
835

    
836
    /* label1: fast path */
837
#ifdef FAST_PATH
838
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
839
#endif
840

    
841
    tcg_out32 (s, (LWZ
842
                   | RT (r0)
843
                   | RA (r0)
844
                   | (offsetof (CPUTLBEntry, addend)
845
                      - offsetof (CPUTLBEntry, addr_write))
846
                   ));
847
    /* r0 = env->tlb_table[mem_index][index].addend */
848
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
849
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
850

    
851
#else  /* !CONFIG_SOFTMMU */
852
    r0 = addr_reg;
853
    r1 = 3;
854
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
855
#endif
856

    
857
#ifdef TARGET_WORDS_BIGENDIAN
858
    bswap = 0;
859
#else
860
    bswap = 1;
861
#endif
862
    switch (opc) {
863
    case 0:
864
        tcg_out32 (s, STBX | SAB (data_reg, rbase, r0));
865
        break;
866
    case 1:
867
        if (bswap)
868
            tcg_out32 (s, STHBRX | SAB (data_reg, rbase, r0));
869
        else
870
            tcg_out32 (s, STHX | SAB (data_reg, rbase, r0));
871
        break;
872
    case 2:
873
        if (bswap)
874
            tcg_out32 (s, STWBRX | SAB (data_reg, rbase, r0));
875
        else
876
            tcg_out32 (s, STWX | SAB (data_reg, rbase, r0));
877
        break;
878
    case 3:
879
        if (bswap) {
880
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
881
            tcg_out32 (s, STWBRX | SAB (data_reg,  rbase, r0));
882
            tcg_out32 (s, STWBRX | SAB (data_reg2, rbase, r1));
883
        }
884
        else {
885
#ifdef CONFIG_USE_GUEST_BASE
886
            tcg_out32 (s, STWX | SAB (data_reg2, rbase, r0));
887
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
888
            tcg_out32 (s, STWX | SAB (data_reg,  rbase, r1));
889
#else
890
            tcg_out32 (s, STW | RS (data_reg2) | RA (r0));
891
            tcg_out32 (s, STW | RS (data_reg) | RA (r0) | 4);
892
#endif
893
        }
894
        break;
895
    }
896

    
897
#ifdef CONFIG_SOFTMMU
898
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
899
#endif
900
}
901

    
902
void tcg_target_qemu_prologue (TCGContext *s)
903
{
904
    int i, frame_size;
905

    
906
    frame_size = 0
907
        + LINKAGE_AREA_SIZE
908
        + TCG_STATIC_CALL_ARGS_SIZE
909
        + ARRAY_SIZE (tcg_target_callee_save_regs) * 4
910
        ;
911
    frame_size = (frame_size + 15) & ~15;
912

    
913
#ifdef _CALL_AIX
914
    {
915
        uint32_t addr;
916

    
917
        /* First emit adhoc function descriptor */
918
        addr = (uint32_t) s->code_ptr + 12;
919
        tcg_out32 (s, addr);        /* entry point */
920
        s->code_ptr += 8;           /* skip TOC and environment pointer */
921
    }
922
#endif
923
    tcg_out32 (s, MFSPR | RT (0) | LR);
924
    tcg_out32 (s, STWU | RS (1) | RA (1) | (-frame_size & 0xffff));
925
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
926
        tcg_out32 (s, (STW
927
                       | RS (tcg_target_callee_save_regs[i])
928
                       | RA (1)
929
                       | (i * 4 + LINKAGE_AREA_SIZE + TCG_STATIC_CALL_ARGS_SIZE)
930
                       )
931
            );
932
    tcg_out32 (s, STW | RS (0) | RA (1) | (frame_size + LR_OFFSET));
933

    
934
#ifdef CONFIG_USE_GUEST_BASE
935
    tcg_out_movi (s, TCG_TYPE_I32, TCG_GUEST_BASE_REG, GUEST_BASE);
936
#endif
937

    
938
    tcg_out32 (s, MTSPR | RS (3) | CTR);
939
    tcg_out32 (s, BCCTR | BO_ALWAYS);
940
    tb_ret_addr = s->code_ptr;
941

    
942
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
943
        tcg_out32 (s, (LWZ
944
                       | RT (tcg_target_callee_save_regs[i])
945
                       | RA (1)
946
                       | (i * 4 + LINKAGE_AREA_SIZE + TCG_STATIC_CALL_ARGS_SIZE)
947
                       )
948
            );
949
    tcg_out32 (s, LWZ | RT (0) | RA (1) | (frame_size + LR_OFFSET));
950
    tcg_out32 (s, MTSPR | RS (0) | LR);
951
    tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
952
    tcg_out32 (s, BCLR | BO_ALWAYS);
953
}
954

    
955
static void tcg_out_ld (TCGContext *s, TCGType type, int ret, int arg1,
956
                        tcg_target_long arg2)
957
{
958
    tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
959
}
960

    
961
static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,
962
                        tcg_target_long arg2)
963
{
964
    tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
965
}
966

    
967
static void ppc_addi (TCGContext *s, int rt, int ra, tcg_target_long si)
968
{
969
    if (!si && rt == ra)
970
        return;
971

    
972
    if (si == (int16_t) si)
973
        tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
974
    else {
975
        uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
976
        tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
977
        tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
978
    }
979
}
980

    
981
static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
982
{
983
    ppc_addi (s, reg, reg, val);
984
}
985

    
986
static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
987
                         int const_arg2, int cr)
988
{
989
    int imm;
990
    uint32_t op;
991

    
992
    switch (cond) {
993
    case TCG_COND_EQ:
994
    case TCG_COND_NE:
995
        if (const_arg2) {
996
            if ((int16_t) arg2 == arg2) {
997
                op = CMPI;
998
                imm = 1;
999
                break;
1000
            }
1001
            else if ((uint16_t) arg2 == arg2) {
1002
                op = CMPLI;
1003
                imm = 1;
1004
                break;
1005
            }
1006
        }
1007
        op = CMPL;
1008
        imm = 0;
1009
        break;
1010

    
1011
    case TCG_COND_LT:
1012
    case TCG_COND_GE:
1013
    case TCG_COND_LE:
1014
    case TCG_COND_GT:
1015
        if (const_arg2) {
1016
            if ((int16_t) arg2 == arg2) {
1017
                op = CMPI;
1018
                imm = 1;
1019
                break;
1020
            }
1021
        }
1022
        op = CMP;
1023
        imm = 0;
1024
        break;
1025

    
1026
    case TCG_COND_LTU:
1027
    case TCG_COND_GEU:
1028
    case TCG_COND_LEU:
1029
    case TCG_COND_GTU:
1030
        if (const_arg2) {
1031
            if ((uint16_t) arg2 == arg2) {
1032
                op = CMPLI;
1033
                imm = 1;
1034
                break;
1035
            }
1036
        }
1037
        op = CMPL;
1038
        imm = 0;
1039
        break;
1040

    
1041
    default:
1042
        tcg_abort ();
1043
    }
1044
    op |= BF (cr);
1045

    
1046
    if (imm)
1047
        tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
1048
    else {
1049
        if (const_arg2) {
1050
            tcg_out_movi (s, TCG_TYPE_I32, 0, arg2);
1051
            tcg_out32 (s, op | RA (arg1) | RB (0));
1052
        }
1053
        else
1054
            tcg_out32 (s, op | RA (arg1) | RB (arg2));
1055
    }
1056

    
1057
}
1058

    
1059
static void tcg_out_bc (TCGContext *s, int bc, int label_index)
1060
{
1061
    TCGLabel *l = &s->labels[label_index];
1062

    
1063
    if (l->has_value)
1064
        tcg_out32 (s, bc | reloc_pc14_val (s->code_ptr, l->u.value));
1065
    else {
1066
        uint16_t val = *(uint16_t *) &s->code_ptr[2];
1067

    
1068
        /* Thanks to Andrzej Zaborowski */
1069
        tcg_out32 (s, bc | (val & 0xfffc));
1070
        tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
1071
    }
1072
}
1073

    
1074
static void tcg_out_cr7eq_from_cond (TCGContext *s, const TCGArg *args,
1075
                                     const int *const_args)
1076
{
1077
    TCGCond cond = args[4];
1078
    int op;
1079
    struct { int bit1; int bit2; int cond2; } bits[] = {
1080
        [TCG_COND_LT ] = { CR_LT, CR_LT, TCG_COND_LT  },
1081
        [TCG_COND_LE ] = { CR_LT, CR_GT, TCG_COND_LT  },
1082
        [TCG_COND_GT ] = { CR_GT, CR_GT, TCG_COND_GT  },
1083
        [TCG_COND_GE ] = { CR_GT, CR_LT, TCG_COND_GT  },
1084
        [TCG_COND_LTU] = { CR_LT, CR_LT, TCG_COND_LTU },
1085
        [TCG_COND_LEU] = { CR_LT, CR_GT, TCG_COND_LTU },
1086
        [TCG_COND_GTU] = { CR_GT, CR_GT, TCG_COND_GTU },
1087
        [TCG_COND_GEU] = { CR_GT, CR_LT, TCG_COND_GTU },
1088
    }, *b = &bits[cond];
1089

    
1090
    switch (cond) {
1091
    case TCG_COND_EQ:
1092
    case TCG_COND_NE:
1093
        op = (cond == TCG_COND_EQ) ? CRAND : CRNAND;
1094
        tcg_out_cmp (s, cond, args[0], args[2], const_args[2], 6);
1095
        tcg_out_cmp (s, cond, args[1], args[3], const_args[3], 7);
1096
        tcg_out32 (s, op | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, CR_EQ));
1097
        break;
1098
    case TCG_COND_LT:
1099
    case TCG_COND_LE:
1100
    case TCG_COND_GT:
1101
    case TCG_COND_GE:
1102
    case TCG_COND_LTU:
1103
    case TCG_COND_LEU:
1104
    case TCG_COND_GTU:
1105
    case TCG_COND_GEU:
1106
        op = (b->bit1 != b->bit2) ? CRANDC : CRAND;
1107
        tcg_out_cmp (s, b->cond2, args[1], args[3], const_args[3], 5);
1108
        tcg_out_cmp (s, TCG_COND_EQ, args[1], args[3], const_args[3], 6);
1109
        tcg_out_cmp (s, cond, args[0], args[2], const_args[2], 7);
1110
        tcg_out32 (s, op | BT (7, CR_EQ) | BA (6, CR_EQ) | BB (7, b->bit2));
1111
        tcg_out32 (s, CROR | BT (7, CR_EQ) | BA (5, b->bit1) | BB (7, CR_EQ));
1112
        break;
1113
    default:
1114
        tcg_abort();
1115
    }
1116
}
1117

    
1118
static void tcg_out_setcond (TCGContext *s, TCGCond cond, TCGArg arg0,
1119
                             TCGArg arg1, TCGArg arg2, int const_arg2)
1120
{
1121
    int crop, sh, arg;
1122

    
1123
    switch (cond) {
1124
    case TCG_COND_EQ:
1125
        if (const_arg2) {
1126
            if (!arg2) {
1127
                arg = arg1;
1128
            }
1129
            else {
1130
                arg = 0;
1131
                if ((uint16_t) arg2 == arg2) {
1132
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1133
                }
1134
                else {
1135
                    tcg_out_movi (s, TCG_TYPE_I32, 0, arg2);
1136
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1137
                }
1138
            }
1139
        }
1140
        else {
1141
            arg = 0;
1142
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1143
        }
1144
        tcg_out32 (s, CNTLZW | RS (arg) | RA (0));
1145
        tcg_out32 (s, (RLWINM
1146
                       | RA (arg0)
1147
                       | RS (0)
1148
                       | SH (27)
1149
                       | MB (5)
1150
                       | ME (31)
1151
                       )
1152
            );
1153
        break;
1154

    
1155
    case TCG_COND_NE:
1156
        if (const_arg2) {
1157
            if (!arg2) {
1158
                arg = arg1;
1159
            }
1160
            else {
1161
                arg = 0;
1162
                if ((uint16_t) arg2 == arg2) {
1163
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1164
                }
1165
                else {
1166
                    tcg_out_movi (s, TCG_TYPE_I32, 0, arg2);
1167
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1168
                }
1169
            }
1170
        }
1171
        else {
1172
            arg = 0;
1173
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1174
        }
1175

    
1176
        if (arg == arg1 && arg1 == arg0) {
1177
            tcg_out32 (s, ADDIC | RT (0) | RA (arg) | 0xffff);
1178
            tcg_out32 (s, SUBFE | TAB (arg0, 0, arg));
1179
        }
1180
        else {
1181
            tcg_out32 (s, ADDIC | RT (arg0) | RA (arg) | 0xffff);
1182
            tcg_out32 (s, SUBFE | TAB (arg0, arg0, arg));
1183
        }
1184
        break;
1185

    
1186
    case TCG_COND_GT:
1187
    case TCG_COND_GTU:
1188
        sh = 30;
1189
        crop = 0;
1190
        goto crtest;
1191

    
1192
    case TCG_COND_LT:
1193
    case TCG_COND_LTU:
1194
        sh = 29;
1195
        crop = 0;
1196
        goto crtest;
1197

    
1198
    case TCG_COND_GE:
1199
    case TCG_COND_GEU:
1200
        sh = 31;
1201
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_LT) | BB (7, CR_LT);
1202
        goto crtest;
1203

    
1204
    case TCG_COND_LE:
1205
    case TCG_COND_LEU:
1206
        sh = 31;
1207
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_GT) | BB (7, CR_GT);
1208
    crtest:
1209
        tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7);
1210
        if (crop) tcg_out32 (s, crop);
1211
        tcg_out32 (s, MFCR | RT (0));
1212
        tcg_out32 (s, (RLWINM
1213
                       | RA (arg0)
1214
                       | RS (0)
1215
                       | SH (sh)
1216
                       | MB (31)
1217
                       | ME (31)
1218
                       )
1219
            );
1220
        break;
1221

    
1222
    default:
1223
        tcg_abort ();
1224
    }
1225
}
1226

    
1227
static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args,
1228
                              const int *const_args)
1229
{
1230
    tcg_out_cr7eq_from_cond (s, args + 1, const_args + 1);
1231
    tcg_out32 (s, MFCR | RT (0));
1232
    tcg_out32 (s, (RLWINM
1233
                   | RA (args[0])
1234
                   | RS (0)
1235
                   | SH (31)
1236
                   | MB (31)
1237
                   | ME (31)
1238
                   )
1239
        );
1240
}
1241

    
1242
static void tcg_out_brcond (TCGContext *s, TCGCond cond,
1243
                            TCGArg arg1, TCGArg arg2, int const_arg2,
1244
                            int label_index)
1245
{
1246
    tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7);
1247
    tcg_out_bc (s, tcg_to_bc[cond], label_index);
1248
}
1249

    
1250
/* XXX: we implement it at the target level to avoid having to
1251
   handle cross basic blocks temporaries */
1252
static void tcg_out_brcond2 (TCGContext *s, const TCGArg *args,
1253
                             const int *const_args)
1254
{
1255
    tcg_out_cr7eq_from_cond (s, args, const_args);
1256
    tcg_out_bc (s, (BC | BI (7, CR_EQ) | BO_COND_TRUE), args[5]);
1257
}
1258

    
1259
void ppc_tb_set_jmp_target (unsigned long jmp_addr, unsigned long addr)
1260
{
1261
    uint32_t *ptr;
1262
    long disp = addr - jmp_addr;
1263
    unsigned long patch_size;
1264

    
1265
    ptr = (uint32_t *)jmp_addr;
1266

    
1267
    if ((disp << 6) >> 6 != disp) {
1268
        ptr[0] = 0x3c000000 | (addr >> 16);    /* lis 0,addr@ha */
1269
        ptr[1] = 0x60000000 | (addr & 0xffff); /* la  0,addr@l(0) */
1270
        ptr[2] = 0x7c0903a6;                   /* mtctr 0 */
1271
        ptr[3] = 0x4e800420;                   /* brctr */
1272
        patch_size = 16;
1273
    } else {
1274
        /* patch the branch destination */
1275
        if (disp != 16) {
1276
            *ptr = 0x48000000 | (disp & 0x03fffffc); /* b disp */
1277
            patch_size = 4;
1278
        } else {
1279
            ptr[0] = 0x60000000; /* nop */
1280
            ptr[1] = 0x60000000;
1281
            ptr[2] = 0x60000000;
1282
            ptr[3] = 0x60000000;
1283
            patch_size = 16;
1284
        }
1285
    }
1286
    /* flush icache */
1287
    flush_icache_range(jmp_addr, jmp_addr + patch_size);
1288
}
1289

    
1290
static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
1291
                       const int *const_args)
1292
{
1293
    switch (opc) {
1294
    case INDEX_op_exit_tb:
1295
        tcg_out_movi (s, TCG_TYPE_I32, TCG_REG_R3, args[0]);
1296
        tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1297
        break;
1298
    case INDEX_op_goto_tb:
1299
        if (s->tb_jmp_offset) {
1300
            /* direct jump method */
1301

    
1302
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1303
            s->code_ptr += 16;
1304
        }
1305
        else {
1306
            tcg_abort ();
1307
        }
1308
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1309
        break;
1310
    case INDEX_op_br:
1311
        {
1312
            TCGLabel *l = &s->labels[args[0]];
1313

    
1314
            if (l->has_value) {
1315
                tcg_out_b (s, 0, l->u.value);
1316
            }
1317
            else {
1318
                uint32_t val = *(uint32_t *) s->code_ptr;
1319

    
1320
                /* Thanks to Andrzej Zaborowski */
1321
                tcg_out32 (s, B | (val & 0x3fffffc));
1322
                tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1323
            }
1324
        }
1325
        break;
1326
    case INDEX_op_call:
1327
        tcg_out_call (s, args[0], const_args[0]);
1328
        break;
1329
    case INDEX_op_jmp:
1330
        if (const_args[0]) {
1331
            tcg_out_b (s, 0, args[0]);
1332
        }
1333
        else {
1334
            tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1335
            tcg_out32 (s, BCCTR | BO_ALWAYS);
1336
        }
1337
        break;
1338
    case INDEX_op_movi_i32:
1339
        tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1]);
1340
        break;
1341
    case INDEX_op_ld8u_i32:
1342
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1343
        break;
1344
    case INDEX_op_ld8s_i32:
1345
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1346
        tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1347
        break;
1348
    case INDEX_op_ld16u_i32:
1349
        tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1350
        break;
1351
    case INDEX_op_ld16s_i32:
1352
        tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1353
        break;
1354
    case INDEX_op_ld_i32:
1355
        tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1356
        break;
1357
    case INDEX_op_st8_i32:
1358
        tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1359
        break;
1360
    case INDEX_op_st16_i32:
1361
        tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1362
        break;
1363
    case INDEX_op_st_i32:
1364
        tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1365
        break;
1366

    
1367
    case INDEX_op_add_i32:
1368
        if (const_args[2])
1369
            ppc_addi (s, args[0], args[1], args[2]);
1370
        else
1371
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1372
        break;
1373
    case INDEX_op_sub_i32:
1374
        if (const_args[2])
1375
            ppc_addi (s, args[0], args[1], -args[2]);
1376
        else
1377
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1378
        break;
1379

    
1380
    case INDEX_op_and_i32:
1381
        if (const_args[2]) {
1382
            uint32_t c;
1383

    
1384
            c = args[2];
1385

    
1386
            if (!c) {
1387
                tcg_out_movi (s, TCG_TYPE_I32, args[0], 0);
1388
                break;
1389
            }
1390
#ifdef __PPU__
1391
            uint32_t t, n;
1392
            int mb, me;
1393

    
1394
            n = c ^ -(c & 1);
1395
            t = n + (n & -n);
1396

    
1397
            if ((t & (t - 1)) == 0) {
1398
                int lzc, tzc;
1399

    
1400
                if ((c & 0x80000001) == 0x80000001) {
1401
                    lzc = clz32 (n);
1402
                    tzc = ctz32 (n);
1403

    
1404
                    mb = 32 - tzc;
1405
                    me = lzc - 1;
1406
                }
1407
                else {
1408
                    lzc = clz32 (c);
1409
                    tzc = ctz32 (c);
1410

    
1411
                    mb = lzc;
1412
                    me = 31 - tzc;
1413
                }
1414

    
1415
                tcg_out32 (s, (RLWINM
1416
                               | RA (args[0])
1417
                               | RS (args[1])
1418
                               | SH (0)
1419
                               | MB (mb)
1420
                               | ME (me)
1421
                               )
1422
                    );
1423
            }
1424
            else
1425
#endif /* !__PPU__ */
1426
            {
1427
                if ((c & 0xffff) == c)
1428
                    tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | c);
1429
                else if ((c & 0xffff0000) == c)
1430
                    tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1431
                               | ((c >> 16) & 0xffff));
1432
                else {
1433
                    tcg_out_movi (s, TCG_TYPE_I32, 0, c);
1434
                    tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1435
                }
1436
            }
1437
        }
1438
        else
1439
            tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1440
        break;
1441
    case INDEX_op_or_i32:
1442
        if (const_args[2]) {
1443
            if (args[2] & 0xffff) {
1444
                tcg_out32 (s, ORI | RS (args[1])  | RA (args[0])
1445
                           | (args[2] & 0xffff));
1446
                if (args[2] >> 16)
1447
                    tcg_out32 (s, ORIS | RS (args[0])  | RA (args[0])
1448
                               | ((args[2] >> 16) & 0xffff));
1449
            }
1450
            else {
1451
                tcg_out32 (s, ORIS | RS (args[1])  | RA (args[0])
1452
                           | ((args[2] >> 16) & 0xffff));
1453
            }
1454
        }
1455
        else
1456
            tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1457
        break;
1458
    case INDEX_op_xor_i32:
1459
        if (const_args[2]) {
1460
            if ((args[2] & 0xffff) == args[2])
1461
                tcg_out32 (s, XORI | RS (args[1])  | RA (args[0])
1462
                           | (args[2] & 0xffff));
1463
            else if ((args[2] & 0xffff0000) == args[2])
1464
                tcg_out32 (s, XORIS | RS (args[1])  | RA (args[0])
1465
                           | ((args[2] >> 16) & 0xffff));
1466
            else {
1467
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1468
                tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1469
            }
1470
        }
1471
        else
1472
            tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1473
        break;
1474
    case INDEX_op_andc_i32:
1475
        tcg_out32 (s, ANDC | SAB (args[1], args[0], args[2]));
1476
        break;
1477
    case INDEX_op_orc_i32:
1478
        tcg_out32 (s, ORC | SAB (args[1], args[0], args[2]));
1479
        break;
1480
    case INDEX_op_eqv_i32:
1481
        tcg_out32 (s, EQV | SAB (args[1], args[0], args[2]));
1482
        break;
1483
    case INDEX_op_nand_i32:
1484
        tcg_out32 (s, NAND | SAB (args[1], args[0], args[2]));
1485
        break;
1486
    case INDEX_op_nor_i32:
1487
        tcg_out32 (s, NOR | SAB (args[1], args[0], args[2]));
1488
        break;
1489

    
1490
    case INDEX_op_mul_i32:
1491
        if (const_args[2]) {
1492
            if (args[2] == (int16_t) args[2])
1493
                tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1494
                           | (args[2] & 0xffff));
1495
            else {
1496
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1497
                tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1498
            }
1499
        }
1500
        else
1501
            tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1502
        break;
1503

    
1504
    case INDEX_op_div_i32:
1505
        tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1506
        break;
1507

    
1508
    case INDEX_op_divu_i32:
1509
        tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1510
        break;
1511

    
1512
    case INDEX_op_rem_i32:
1513
        tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1514
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1515
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1516
        break;
1517

    
1518
    case INDEX_op_remu_i32:
1519
        tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1520
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1521
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1522
        break;
1523

    
1524
    case INDEX_op_mulu2_i32:
1525
        if (args[0] == args[2] || args[0] == args[3]) {
1526
            tcg_out32 (s, MULLW | TAB (0, args[2], args[3]));
1527
            tcg_out32 (s, MULHWU | TAB (args[1], args[2], args[3]));
1528
            tcg_out_mov (s, args[0], 0);
1529
        }
1530
        else {
1531
            tcg_out32 (s, MULLW | TAB (args[0], args[2], args[3]));
1532
            tcg_out32 (s, MULHWU | TAB (args[1], args[2], args[3]));
1533
        }
1534
        break;
1535

    
1536
    case INDEX_op_shl_i32:
1537
        if (const_args[2]) {
1538
            tcg_out32 (s, (RLWINM
1539
                           | RA (args[0])
1540
                           | RS (args[1])
1541
                           | SH (args[2])
1542
                           | MB (0)
1543
                           | ME (31 - args[2])
1544
                           )
1545
                );
1546
        }
1547
        else
1548
            tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1549
        break;
1550
    case INDEX_op_shr_i32:
1551
        if (const_args[2]) {
1552
            tcg_out32 (s, (RLWINM
1553
                           | RA (args[0])
1554
                           | RS (args[1])
1555
                           | SH (32 - args[2])
1556
                           | MB (args[2])
1557
                           | ME (31)
1558
                           )
1559
                );
1560
        }
1561
        else
1562
            tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1563
        break;
1564
    case INDEX_op_sar_i32:
1565
        if (const_args[2])
1566
            tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1567
        else
1568
            tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1569
        break;
1570
    case INDEX_op_rotl_i32:
1571
        {
1572
            int op = 0
1573
                | RA (args[0])
1574
                | RS (args[1])
1575
                | MB (0)
1576
                | ME (31)
1577
                | (const_args[2] ? RLWINM | SH (args[2])
1578
                                 : RLWNM | RB (args[2]))
1579
                ;
1580
            tcg_out32 (s, op);
1581
        }
1582
        break;
1583
    case INDEX_op_rotr_i32:
1584
        if (const_args[2]) {
1585
            if (!args[2]) {
1586
                tcg_out_mov (s, args[0], args[1]);
1587
            }
1588
            else {
1589
                tcg_out32 (s, RLWINM
1590
                           | RA (args[0])
1591
                           | RS (args[1])
1592
                           | SH (32 - args[2])
1593
                           | MB (0)
1594
                           | ME (31)
1595
                    );
1596
            }
1597
        }
1598
        else {
1599
            tcg_out32 (s, SUBFIC | RT (0) | RA (args[2]) | 32);
1600
            tcg_out32 (s, RLWNM
1601
                       | RA (args[0])
1602
                       | RS (args[1])
1603
                       | RB (0)
1604
                       | MB (0)
1605
                       | ME (31)
1606
                );
1607
        }
1608
        break;
1609

    
1610
    case INDEX_op_add2_i32:
1611
        if (args[0] == args[3] || args[0] == args[5]) {
1612
            tcg_out32 (s, ADDC | TAB (0, args[2], args[4]));
1613
            tcg_out32 (s, ADDE | TAB (args[1], args[3], args[5]));
1614
            tcg_out_mov (s, args[0], 0);
1615
        }
1616
        else {
1617
            tcg_out32 (s, ADDC | TAB (args[0], args[2], args[4]));
1618
            tcg_out32 (s, ADDE | TAB (args[1], args[3], args[5]));
1619
        }
1620
        break;
1621
    case INDEX_op_sub2_i32:
1622
        if (args[0] == args[3] || args[0] == args[5]) {
1623
            tcg_out32 (s, SUBFC | TAB (0, args[4], args[2]));
1624
            tcg_out32 (s, SUBFE | TAB (args[1], args[5], args[3]));
1625
            tcg_out_mov (s, args[0], 0);
1626
        }
1627
        else {
1628
            tcg_out32 (s, SUBFC | TAB (args[0], args[4], args[2]));
1629
            tcg_out32 (s, SUBFE | TAB (args[1], args[5], args[3]));
1630
        }
1631
        break;
1632

    
1633
    case INDEX_op_brcond_i32:
1634
        /*
1635
          args[0] = r0
1636
          args[1] = r1
1637
          args[2] = cond
1638
          args[3] = r1 is const
1639
          args[4] = label_index
1640
        */
1641
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3]);
1642
        break;
1643
    case INDEX_op_brcond2_i32:
1644
        tcg_out_brcond2(s, args, const_args);
1645
        break;
1646

    
1647
    case INDEX_op_neg_i32:
1648
        tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1649
        break;
1650

    
1651
    case INDEX_op_not_i32:
1652
        tcg_out32 (s, NOR | SAB (args[1], args[0], args[1]));
1653
        break;
1654

    
1655
    case INDEX_op_qemu_ld8u:
1656
        tcg_out_qemu_ld(s, args, 0);
1657
        break;
1658
    case INDEX_op_qemu_ld8s:
1659
        tcg_out_qemu_ld(s, args, 0 | 4);
1660
        break;
1661
    case INDEX_op_qemu_ld16u:
1662
        tcg_out_qemu_ld(s, args, 1);
1663
        break;
1664
    case INDEX_op_qemu_ld16s:
1665
        tcg_out_qemu_ld(s, args, 1 | 4);
1666
        break;
1667
    case INDEX_op_qemu_ld32:
1668
        tcg_out_qemu_ld(s, args, 2);
1669
        break;
1670
    case INDEX_op_qemu_ld64:
1671
        tcg_out_qemu_ld(s, args, 3);
1672
        break;
1673
    case INDEX_op_qemu_st8:
1674
        tcg_out_qemu_st(s, args, 0);
1675
        break;
1676
    case INDEX_op_qemu_st16:
1677
        tcg_out_qemu_st(s, args, 1);
1678
        break;
1679
    case INDEX_op_qemu_st32:
1680
        tcg_out_qemu_st(s, args, 2);
1681
        break;
1682
    case INDEX_op_qemu_st64:
1683
        tcg_out_qemu_st(s, args, 3);
1684
        break;
1685

    
1686
    case INDEX_op_ext8s_i32:
1687
        tcg_out32 (s, EXTSB | RS (args[1]) | RA (args[0]));
1688
        break;
1689
    case INDEX_op_ext8u_i32:
1690
        tcg_out32 (s, RLWINM
1691
                   | RA (args[0])
1692
                   | RS (args[1])
1693
                   | SH (0)
1694
                   | MB (24)
1695
                   | ME (31)
1696
            );
1697
        break;
1698
    case INDEX_op_ext16s_i32:
1699
        tcg_out32 (s, EXTSH | RS (args[1]) | RA (args[0]));
1700
        break;
1701
    case INDEX_op_ext16u_i32:
1702
        tcg_out32 (s, RLWINM
1703
                   | RA (args[0])
1704
                   | RS (args[1])
1705
                   | SH (0)
1706
                   | MB (16)
1707
                   | ME (31)
1708
            );
1709
        break;
1710

    
1711
    case INDEX_op_setcond_i32:
1712
        tcg_out_setcond (s, args[3], args[0], args[1], args[2], const_args[2]);
1713
        break;
1714
    case INDEX_op_setcond2_i32:
1715
        tcg_out_setcond2 (s, args, const_args);
1716
        break;
1717

    
1718
    default:
1719
        tcg_dump_ops (s, stderr);
1720
        tcg_abort ();
1721
    }
1722
}
1723

    
1724
static const TCGTargetOpDef ppc_op_defs[] = {
1725
    { INDEX_op_exit_tb, { } },
1726
    { INDEX_op_goto_tb, { } },
1727
    { INDEX_op_call, { "ri" } },
1728
    { INDEX_op_jmp, { "ri" } },
1729
    { INDEX_op_br, { } },
1730

    
1731
    { INDEX_op_mov_i32, { "r", "r" } },
1732
    { INDEX_op_movi_i32, { "r" } },
1733
    { INDEX_op_ld8u_i32, { "r", "r" } },
1734
    { INDEX_op_ld8s_i32, { "r", "r" } },
1735
    { INDEX_op_ld16u_i32, { "r", "r" } },
1736
    { INDEX_op_ld16s_i32, { "r", "r" } },
1737
    { INDEX_op_ld_i32, { "r", "r" } },
1738
    { INDEX_op_st8_i32, { "r", "r" } },
1739
    { INDEX_op_st16_i32, { "r", "r" } },
1740
    { INDEX_op_st_i32, { "r", "r" } },
1741

    
1742
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1743
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1744
    { INDEX_op_div_i32, { "r", "r", "r" } },
1745
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1746
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1747
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1748
    { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
1749
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1750
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1751
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1752
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1753

    
1754
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1755
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1756
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1757

    
1758
    { INDEX_op_rotl_i32, { "r", "r", "ri" } },
1759
    { INDEX_op_rotr_i32, { "r", "r", "ri" } },
1760

    
1761
    { INDEX_op_brcond_i32, { "r", "ri" } },
1762

    
1763
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
1764
    { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
1765
    { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },
1766

    
1767
    { INDEX_op_neg_i32, { "r", "r" } },
1768
    { INDEX_op_not_i32, { "r", "r" } },
1769

    
1770
    { INDEX_op_andc_i32, { "r", "r", "r" } },
1771
    { INDEX_op_orc_i32, { "r", "r", "r" } },
1772
    { INDEX_op_eqv_i32, { "r", "r", "r" } },
1773
    { INDEX_op_nand_i32, { "r", "r", "r" } },
1774
    { INDEX_op_nor_i32, { "r", "r", "r" } },
1775

    
1776
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1777
    { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
1778

    
1779
#if TARGET_LONG_BITS == 32
1780
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1781
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1782
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1783
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1784
    { INDEX_op_qemu_ld32, { "r", "L" } },
1785
    { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1786

    
1787
    { INDEX_op_qemu_st8, { "K", "K" } },
1788
    { INDEX_op_qemu_st16, { "K", "K" } },
1789
    { INDEX_op_qemu_st32, { "K", "K" } },
1790
    { INDEX_op_qemu_st64, { "M", "M", "M" } },
1791
#else
1792
    { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1793
    { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1794
    { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1795
    { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
1796
    { INDEX_op_qemu_ld32, { "r", "L", "L" } },
1797
    { INDEX_op_qemu_ld64, { "r", "L", "L", "L" } },
1798

    
1799
    { INDEX_op_qemu_st8, { "K", "K", "K" } },
1800
    { INDEX_op_qemu_st16, { "K", "K", "K" } },
1801
    { INDEX_op_qemu_st32, { "K", "K", "K" } },
1802
    { INDEX_op_qemu_st64, { "M", "M", "M", "M" } },
1803
#endif
1804

    
1805
    { INDEX_op_ext8s_i32, { "r", "r" } },
1806
    { INDEX_op_ext8u_i32, { "r", "r" } },
1807
    { INDEX_op_ext16s_i32, { "r", "r" } },
1808
    { INDEX_op_ext16u_i32, { "r", "r" } },
1809

    
1810
    { -1 },
1811
};
1812

    
1813
void tcg_target_init(TCGContext *s)
1814
{
1815
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1816
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1817
                     (1 << TCG_REG_R0) |
1818
#ifdef _CALL_DARWIN
1819
                     (1 << TCG_REG_R2) |
1820
#endif
1821
                     (1 << TCG_REG_R3) |
1822
                     (1 << TCG_REG_R4) |
1823
                     (1 << TCG_REG_R5) |
1824
                     (1 << TCG_REG_R6) |
1825
                     (1 << TCG_REG_R7) |
1826
                     (1 << TCG_REG_R8) |
1827
                     (1 << TCG_REG_R9) |
1828
                     (1 << TCG_REG_R10) |
1829
                     (1 << TCG_REG_R11) |
1830
                     (1 << TCG_REG_R12)
1831
        );
1832

    
1833
    tcg_regset_clear(s->reserved_regs);
1834
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
1835
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);
1836
#ifndef _CALL_DARWIN
1837
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2);
1838
#endif
1839
#ifdef _CALL_SYSV
1840
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13);
1841
#endif
1842
#ifdef CONFIG_USE_GUEST_BASE
1843
    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1844
#endif
1845

    
1846
    tcg_add_target_add_op_defs(ppc_op_defs);
1847
}