Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (52.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
    "r2",
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 RLWIMI OPCD(20)
332
#define RLWINM OPCD(21)
333
#define RLWNM  OPCD(23)
334

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

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

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

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

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

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

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

    
405
#define LK    1
406

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

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

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

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

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

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

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

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

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

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

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

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

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

    
508
#if defined(CONFIG_SOFTMMU)
509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
914
#ifdef _CALL_AIX
915
    {
916
        uint32_t addr;
917

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1058
}
1059

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1224
    default:
1225
        tcg_abort ();
1226
    }
1227
}
1228

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

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

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

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

    
1267
    ptr = (uint32_t *)jmp_addr;
1268

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

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

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

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

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

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

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

    
1386
            c = args[2];
1387

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

    
1396
            n = c ^ -(c & 1);
1397
            t = n + (n & -n);
1398

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

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

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

    
1413
                    mb = lzc;
1414
                    me = 31 - tzc;
1415
                }
1416

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1720
    case INDEX_op_bswap16_i32:
1721
        /* Stolen from gcc's builtin_bswap16 */
1722

    
1723
        /* a1 = abcd */
1724

    
1725
        /* r0 = (a1 << 8) & 0xff00 # 00d0 */
1726
        tcg_out32 (s, RLWINM
1727
                   | RA (0)
1728
                   | RS (args[1])
1729
                   | SH (8)
1730
                   | MB (16)
1731
                   | ME (23)
1732
            );
1733

    
1734
        /* a0 = rotate_left (a1, 24) & 0xff # 000c */
1735
        tcg_out32 (s, RLWINM
1736
                   | RA (args[0])
1737
                   | RS (args[1])
1738
                   | SH (24)
1739
                   | MB (24)
1740
                   | ME (31)
1741
            );
1742

    
1743
        /* a0 = a0 | r0 # 00dc */
1744
        tcg_out32 (s, OR | SAB (0, args[0], args[0]));
1745
        break;
1746

    
1747
    case INDEX_op_bswap32_i32:
1748
        /* Stolen from gcc's builtin_bswap32 */
1749
        {
1750
            int a0 = args[0];
1751

    
1752
            /* a1 = args[1] # abcd */
1753

    
1754
            if (a0 == args[1]) {
1755
                a0 = 0;
1756
            }
1757

    
1758
            /* a0 = rotate_left (a1, 8) # bcda */
1759
            tcg_out32 (s, RLWINM
1760
                       | RA (a0)
1761
                       | RS (args[1])
1762
                       | SH (8)
1763
                       | MB (0)
1764
                       | ME (31)
1765
                );
1766

    
1767
            /* a0 = (a0 & ~0xff000000) | ((a1 << 24) & 0xff000000) # dcda */
1768
            tcg_out32 (s, RLWIMI
1769
                       | RA (a0)
1770
                       | RS (args[1])
1771
                       | SH (24)
1772
                       | MB (0)
1773
                       | ME (7)
1774
                );
1775

    
1776
            /* a0 = (a0 & ~0x0000ff00) | ((a1 << 24) & 0x0000ff00) # dcba */
1777
            tcg_out32 (s, RLWIMI
1778
                       | RA (a0)
1779
                       | RS (args[1])
1780
                       | SH (24)
1781
                       | MB (16)
1782
                       | ME (23)
1783
                );
1784

    
1785
            if (!a0) {
1786
                tcg_out_mov (s, args[0], a0);
1787
            }
1788
        }
1789
        break;
1790

    
1791
    default:
1792
        tcg_dump_ops (s, stderr);
1793
        tcg_abort ();
1794
    }
1795
}
1796

    
1797
static const TCGTargetOpDef ppc_op_defs[] = {
1798
    { INDEX_op_exit_tb, { } },
1799
    { INDEX_op_goto_tb, { } },
1800
    { INDEX_op_call, { "ri" } },
1801
    { INDEX_op_jmp, { "ri" } },
1802
    { INDEX_op_br, { } },
1803

    
1804
    { INDEX_op_mov_i32, { "r", "r" } },
1805
    { INDEX_op_movi_i32, { "r" } },
1806
    { INDEX_op_ld8u_i32, { "r", "r" } },
1807
    { INDEX_op_ld8s_i32, { "r", "r" } },
1808
    { INDEX_op_ld16u_i32, { "r", "r" } },
1809
    { INDEX_op_ld16s_i32, { "r", "r" } },
1810
    { INDEX_op_ld_i32, { "r", "r" } },
1811
    { INDEX_op_st8_i32, { "r", "r" } },
1812
    { INDEX_op_st16_i32, { "r", "r" } },
1813
    { INDEX_op_st_i32, { "r", "r" } },
1814

    
1815
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1816
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1817
    { INDEX_op_div_i32, { "r", "r", "r" } },
1818
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1819
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1820
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1821
    { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
1822
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1823
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1824
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1825
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1826

    
1827
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1828
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1829
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1830

    
1831
    { INDEX_op_rotl_i32, { "r", "r", "ri" } },
1832
    { INDEX_op_rotr_i32, { "r", "r", "ri" } },
1833

    
1834
    { INDEX_op_brcond_i32, { "r", "ri" } },
1835

    
1836
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
1837
    { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
1838
    { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },
1839

    
1840
    { INDEX_op_neg_i32, { "r", "r" } },
1841
    { INDEX_op_not_i32, { "r", "r" } },
1842

    
1843
    { INDEX_op_andc_i32, { "r", "r", "r" } },
1844
    { INDEX_op_orc_i32, { "r", "r", "r" } },
1845
    { INDEX_op_eqv_i32, { "r", "r", "r" } },
1846
    { INDEX_op_nand_i32, { "r", "r", "r" } },
1847
    { INDEX_op_nor_i32, { "r", "r", "r" } },
1848

    
1849
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1850
    { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
1851

    
1852
    { INDEX_op_bswap16_i32, { "r", "r" } },
1853
    { INDEX_op_bswap32_i32, { "r", "r" } },
1854

    
1855
#if TARGET_LONG_BITS == 32
1856
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1857
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1858
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1859
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1860
    { INDEX_op_qemu_ld32, { "r", "L" } },
1861
    { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1862

    
1863
    { INDEX_op_qemu_st8, { "K", "K" } },
1864
    { INDEX_op_qemu_st16, { "K", "K" } },
1865
    { INDEX_op_qemu_st32, { "K", "K" } },
1866
    { INDEX_op_qemu_st64, { "M", "M", "M" } },
1867
#else
1868
    { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1869
    { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1870
    { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1871
    { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
1872
    { INDEX_op_qemu_ld32, { "r", "L", "L" } },
1873
    { INDEX_op_qemu_ld64, { "r", "L", "L", "L" } },
1874

    
1875
    { INDEX_op_qemu_st8, { "K", "K", "K" } },
1876
    { INDEX_op_qemu_st16, { "K", "K", "K" } },
1877
    { INDEX_op_qemu_st32, { "K", "K", "K" } },
1878
    { INDEX_op_qemu_st64, { "M", "M", "M", "M" } },
1879
#endif
1880

    
1881
    { INDEX_op_ext8s_i32, { "r", "r" } },
1882
    { INDEX_op_ext8u_i32, { "r", "r" } },
1883
    { INDEX_op_ext16s_i32, { "r", "r" } },
1884
    { INDEX_op_ext16u_i32, { "r", "r" } },
1885

    
1886
    { -1 },
1887
};
1888

    
1889
void tcg_target_init(TCGContext *s)
1890
{
1891
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1892
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1893
                     (1 << TCG_REG_R0) |
1894
#ifdef _CALL_DARWIN
1895
                     (1 << TCG_REG_R2) |
1896
#endif
1897
                     (1 << TCG_REG_R3) |
1898
                     (1 << TCG_REG_R4) |
1899
                     (1 << TCG_REG_R5) |
1900
                     (1 << TCG_REG_R6) |
1901
                     (1 << TCG_REG_R7) |
1902
                     (1 << TCG_REG_R8) |
1903
                     (1 << TCG_REG_R9) |
1904
                     (1 << TCG_REG_R10) |
1905
                     (1 << TCG_REG_R11) |
1906
                     (1 << TCG_REG_R12)
1907
        );
1908

    
1909
    tcg_regset_clear(s->reserved_regs);
1910
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
1911
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);
1912
#ifndef _CALL_DARWIN
1913
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2);
1914
#endif
1915
#ifdef _CALL_SYSV
1916
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13);
1917
#endif
1918
#ifdef CONFIG_USE_GUEST_BASE
1919
    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1920
#endif
1921

    
1922
    tcg_add_target_add_op_defs(ppc_op_defs);
1923
}