Statistics
| Branch: | Revision:

root / tcg / ppc / tcg-target.c @ 1a2eb162

History | View | Annotate | Download (53.1 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 */
164
    TCG_REG_R28,
165
    TCG_REG_R29,
166
    TCG_REG_R30,
167
    TCG_REG_R31
168
};
169

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

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

    
178
    return disp & 0x3fffffc;
179
}
180

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

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

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

    
195
    return disp & 0xfffc;
196
}
197

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

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

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

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

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

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

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

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

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

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

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

    
330
#define RLWIMI OPCD(20)
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, TCGType type, 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, TCG_TYPE_I32, 3, addr_reg);
594
    tcg_out_movi (s, TCG_TYPE_I32, 4, mem_index);
595
#else
596
    tcg_out_mov (s, TCG_TYPE_I32, 3, addr_reg2);
597
    tcg_out_mov (s, TCG_TYPE_I32, 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, TCG_TYPE_I32, data_reg, 3);
614
        break;
615
    case 3:
616
        if (data_reg == 3) {
617
            if (data_reg2 == 4) {
618
                tcg_out_mov (s, TCG_TYPE_I32, 0, 4);
619
                tcg_out_mov (s, TCG_TYPE_I32, 4, 3);
620
                tcg_out_mov (s, TCG_TYPE_I32, 3, 0);
621
            }
622
            else {
623
                tcg_out_mov (s, TCG_TYPE_I32, data_reg2, 3);
624
                tcg_out_mov (s, TCG_TYPE_I32, 3, 4);
625
            }
626
        }
627
        else {
628
            if (data_reg != 4) tcg_out_mov (s, TCG_TYPE_I32, data_reg, 4);
629
            if (data_reg2 != 3) tcg_out_mov (s, TCG_TYPE_I32, 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, TCG_TYPE_I32, 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, TCG_TYPE_I32, 3, addr_reg);
790
    ir = 4;
791
#else
792
    tcg_out_mov (s, TCG_TYPE_I32, 3, addr_reg2);
793
    tcg_out_mov (s, TCG_TYPE_I32, 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, TCG_TYPE_I32, 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, TCG_TYPE_I32, ir++, data_reg2);
826
        tcg_out_mov (s, TCG_TYPE_I32, 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
static 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
    if (GUEST_BASE) {
936
        tcg_out_movi (s, TCG_TYPE_I32, TCG_GUEST_BASE_REG, GUEST_BASE);
937
        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
938
    }
939
#endif
940

    
941
    tcg_out_mov (s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
942
    tcg_out32 (s, MTSPR | RS (tcg_target_call_iarg_regs[1]) | CTR);
943
    tcg_out32 (s, BCCTR | BO_ALWAYS);
944
    tb_ret_addr = s->code_ptr;
945

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

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

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

    
971
static void ppc_addi (TCGContext *s, int rt, int ra, tcg_target_long si)
972
{
973
    if (!si && rt == ra)
974
        return;
975

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

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

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

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

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

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

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

    
1056
}
1057

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1264
    ptr = (uint32_t *)jmp_addr;
1265

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

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

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

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

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

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

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

    
1383
            c = args[2];
1384

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1717
    case INDEX_op_bswap16_i32:
1718
        /* Stolen from gcc's builtin_bswap16 */
1719

    
1720
        /* a1 = abcd */
1721

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

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

    
1740
        /* a0 = a0 | r0 # 00dc */
1741
        tcg_out32 (s, OR | SAB (0, args[0], args[0]));
1742
        break;
1743

    
1744
    case INDEX_op_bswap32_i32:
1745
        /* Stolen from gcc's builtin_bswap32 */
1746
        {
1747
            int a0 = args[0];
1748

    
1749
            /* a1 = args[1] # abcd */
1750

    
1751
            if (a0 == args[1]) {
1752
                a0 = 0;
1753
            }
1754

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

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

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

    
1782
            if (!a0) {
1783
                tcg_out_mov (s, TCG_TYPE_I32, args[0], a0);
1784
            }
1785
        }
1786
        break;
1787

    
1788
    default:
1789
        tcg_dump_ops (s, stderr);
1790
        tcg_abort ();
1791
    }
1792
}
1793

    
1794
static const TCGTargetOpDef ppc_op_defs[] = {
1795
    { INDEX_op_exit_tb, { } },
1796
    { INDEX_op_goto_tb, { } },
1797
    { INDEX_op_call, { "ri" } },
1798
    { INDEX_op_jmp, { "ri" } },
1799
    { INDEX_op_br, { } },
1800

    
1801
    { INDEX_op_mov_i32, { "r", "r" } },
1802
    { INDEX_op_movi_i32, { "r" } },
1803
    { INDEX_op_ld8u_i32, { "r", "r" } },
1804
    { INDEX_op_ld8s_i32, { "r", "r" } },
1805
    { INDEX_op_ld16u_i32, { "r", "r" } },
1806
    { INDEX_op_ld16s_i32, { "r", "r" } },
1807
    { INDEX_op_ld_i32, { "r", "r" } },
1808
    { INDEX_op_st8_i32, { "r", "r" } },
1809
    { INDEX_op_st16_i32, { "r", "r" } },
1810
    { INDEX_op_st_i32, { "r", "r" } },
1811

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

    
1824
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1825
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1826
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1827

    
1828
    { INDEX_op_rotl_i32, { "r", "r", "ri" } },
1829
    { INDEX_op_rotr_i32, { "r", "r", "ri" } },
1830

    
1831
    { INDEX_op_brcond_i32, { "r", "ri" } },
1832

    
1833
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "r", "r" } },
1834
    { INDEX_op_sub2_i32, { "r", "r", "r", "r", "r", "r" } },
1835
    { INDEX_op_brcond2_i32, { "r", "r", "r", "r" } },
1836

    
1837
    { INDEX_op_neg_i32, { "r", "r" } },
1838
    { INDEX_op_not_i32, { "r", "r" } },
1839

    
1840
    { INDEX_op_andc_i32, { "r", "r", "r" } },
1841
    { INDEX_op_orc_i32, { "r", "r", "r" } },
1842
    { INDEX_op_eqv_i32, { "r", "r", "r" } },
1843
    { INDEX_op_nand_i32, { "r", "r", "r" } },
1844
    { INDEX_op_nor_i32, { "r", "r", "r" } },
1845

    
1846
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1847
    { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
1848

    
1849
    { INDEX_op_bswap16_i32, { "r", "r" } },
1850
    { INDEX_op_bswap32_i32, { "r", "r" } },
1851

    
1852
#if TARGET_LONG_BITS == 32
1853
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1854
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1855
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1856
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1857
    { INDEX_op_qemu_ld32, { "r", "L" } },
1858
    { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1859

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

    
1872
    { INDEX_op_qemu_st8, { "K", "K", "K" } },
1873
    { INDEX_op_qemu_st16, { "K", "K", "K" } },
1874
    { INDEX_op_qemu_st32, { "K", "K", "K" } },
1875
    { INDEX_op_qemu_st64, { "M", "M", "M", "M" } },
1876
#endif
1877

    
1878
    { INDEX_op_ext8s_i32, { "r", "r" } },
1879
    { INDEX_op_ext8u_i32, { "r", "r" } },
1880
    { INDEX_op_ext16s_i32, { "r", "r" } },
1881
    { INDEX_op_ext16u_i32, { "r", "r" } },
1882

    
1883
    { -1 },
1884
};
1885

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

    
1906
    tcg_regset_clear(s->reserved_regs);
1907
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
1908
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);
1909
#ifndef _CALL_DARWIN
1910
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R2);
1911
#endif
1912
#ifdef _CALL_SYSV
1913
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R13);
1914
#endif
1915

    
1916
    tcg_add_target_add_op_defs(ppc_op_defs);
1917
    tcg_set_frame(s, TCG_AREG0, offsetof(CPUState, temp_buf),
1918
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
1919
}