Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (46.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
#define TCG_CT_CONST_U32 0x100
26

    
27
static uint8_t *tb_ret_addr;
28

    
29
#define FAST_PATH
30

    
31
#ifdef CONFIG_SOFTMMU
32
#if TARGET_PHYS_ADDR_BITS == 32
33
#define LD_ADDEND LWZ
34
#else
35
#define LD_ADDEND LD
36
#endif
37
#endif
38

    
39
#if TARGET_LONG_BITS == 32
40
#define LD_ADDR LWZU
41
#define CMP_L 0
42
#else
43
#define LD_ADDR LDU
44
#define CMP_L (1<<21)
45
#endif
46

    
47
#ifndef GUEST_BASE
48
#define GUEST_BASE 0
49
#endif
50

    
51
#ifdef CONFIG_USE_GUEST_BASE
52
#define TCG_GUEST_BASE_REG 30
53
#else
54
#define TCG_GUEST_BASE_REG 0
55
#endif
56

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

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

    
130
static const int tcg_target_call_iarg_regs[] = {
131
    TCG_REG_R3,
132
    TCG_REG_R4,
133
    TCG_REG_R5,
134
    TCG_REG_R6,
135
    TCG_REG_R7,
136
    TCG_REG_R8,
137
    TCG_REG_R9,
138
    TCG_REG_R10
139
};
140

    
141
static const int tcg_target_call_oarg_regs[2] = {
142
    TCG_REG_R3
143
};
144

    
145
static const int tcg_target_callee_save_regs[] = {
146
#ifdef __APPLE__
147
    TCG_REG_R11,
148
#endif
149
    TCG_REG_R14,
150
    TCG_REG_R15,
151
    TCG_REG_R16,
152
    TCG_REG_R17,
153
    TCG_REG_R18,
154
    TCG_REG_R19,
155
    TCG_REG_R20,
156
    TCG_REG_R21,
157
    TCG_REG_R22,
158
    TCG_REG_R23,
159
    TCG_REG_R24,
160
    TCG_REG_R25,
161
    TCG_REG_R26,
162
    /* TCG_REG_R27, */ /* currently used for the global env, so no
163
                          need to save */
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 << 38) >> 38 != 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
    case 'L':                   /* qemu_ld constraint */
242
        ct->ct |= TCG_CT_REG;
243
        tcg_regset_set32 (ct->u.regs, 0, 0xffffffff);
244
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
245
#ifdef CONFIG_SOFTMMU
246
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
247
#endif
248
        break;
249
    case 'S':                   /* qemu_st 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
#ifdef CONFIG_SOFTMMU
254
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
255
        tcg_regset_reset_reg (ct->u.regs, TCG_REG_R5);
256
#endif
257
        break;
258
    case 'Z':
259
        ct->ct |= TCG_CT_CONST_U32;
260
        break;
261
    default:
262
        return -1;
263
    }
264
    ct_str++;
265
    *pct_str = ct_str;
266
    return 0;
267
}
268

    
269
/* test if a constant matches the constraint */
270
static int tcg_target_const_match (tcg_target_long val,
271
                                   const TCGArgConstraint *arg_ct)
272
{
273
    int ct;
274

    
275
    ct = arg_ct->ct;
276
    if (ct & TCG_CT_CONST)
277
        return 1;
278
    else if ((ct & TCG_CT_CONST_U32) && (val == (uint32_t) val))
279
        return 1;
280
    return 0;
281
}
282

    
283
#define OPCD(opc) ((opc)<<26)
284
#define XO19(opc) (OPCD(19)|((opc)<<1))
285
#define XO30(opc) (OPCD(30)|((opc)<<2))
286
#define XO31(opc) (OPCD(31)|((opc)<<1))
287
#define XO58(opc) (OPCD(58)|(opc))
288
#define XO62(opc) (OPCD(62)|(opc))
289

    
290
#define B      OPCD( 18)
291
#define BC     OPCD( 16)
292
#define LBZ    OPCD( 34)
293
#define LHZ    OPCD( 40)
294
#define LHA    OPCD( 42)
295
#define LWZ    OPCD( 32)
296
#define STB    OPCD( 38)
297
#define STH    OPCD( 44)
298
#define STW    OPCD( 36)
299

    
300
#define STD    XO62(  0)
301
#define STDU   XO62(  1)
302
#define STDX   XO31(149)
303

    
304
#define LD     XO58(  0)
305
#define LDX    XO31( 21)
306
#define LDU    XO58(  1)
307
#define LWA    XO58(  2)
308
#define LWAX   XO31(341)
309

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

    
323
#define LWZU   OPCD( 33)
324
#define STWU   OPCD( 37)
325

    
326
#define RLWINM OPCD( 21)
327

    
328
#define RLDICL XO30(  0)
329
#define RLDICR XO30(  1)
330
#define RLDIMI XO30(  3)
331

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

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

    
370
#define MULLD  XO31(233)
371
#define MULHD  XO31( 73)
372
#define MULHDU XO31(  9)
373
#define DIVD   XO31(489)
374
#define DIVDU  XO31(457)
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 SLD    XO31( 27)
393
#define SRD    XO31(539)
394
#define SRAD   XO31(794)
395
#define SRADI  XO31(413<<1)
396

    
397
#define TW     XO31( 4)
398
#define TRAP   (TW | TO (31))
399

    
400
#define RT(r) ((r)<<21)
401
#define RS(r) ((r)<<21)
402
#define RA(r) ((r)<<16)
403
#define RB(r) ((r)<<11)
404
#define TO(t) ((t)<<21)
405
#define SH(s) ((s)<<11)
406
#define MB(b) ((b)<<6)
407
#define ME(e) ((e)<<1)
408
#define BO(o) ((o)<<21)
409
#define MB64(b) ((b)<<5)
410

    
411
#define LK    1
412

    
413
#define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
414
#define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
415

    
416
#define BF(n)    ((n)<<23)
417
#define BI(n, c) (((c)+((n)*4))<<16)
418
#define BT(n, c) (((c)+((n)*4))<<21)
419
#define BA(n, c) (((c)+((n)*4))<<16)
420
#define BB(n, c) (((c)+((n)*4))<<11)
421

    
422
#define BO_COND_TRUE  BO (12)
423
#define BO_COND_FALSE BO ( 4)
424
#define BO_ALWAYS     BO (20)
425

    
426
enum {
427
    CR_LT,
428
    CR_GT,
429
    CR_EQ,
430
    CR_SO
431
};
432

    
433
static const uint32_t tcg_to_bc[10] = {
434
    [TCG_COND_EQ]  = BC | BI (7, CR_EQ) | BO_COND_TRUE,
435
    [TCG_COND_NE]  = BC | BI (7, CR_EQ) | BO_COND_FALSE,
436
    [TCG_COND_LT]  = BC | BI (7, CR_LT) | BO_COND_TRUE,
437
    [TCG_COND_GE]  = BC | BI (7, CR_LT) | BO_COND_FALSE,
438
    [TCG_COND_LE]  = BC | BI (7, CR_GT) | BO_COND_FALSE,
439
    [TCG_COND_GT]  = BC | BI (7, CR_GT) | BO_COND_TRUE,
440
    [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
441
    [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
442
    [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
443
    [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
444
};
445

    
446
static void tcg_out_mov (TCGContext *s, int ret, int arg)
447
{
448
    tcg_out32 (s, OR | SAB (arg, ret, arg));
449
}
450

    
451
static void tcg_out_rld (TCGContext *s, int op, int ra, int rs, int sh, int mb)
452
{
453
    sh = SH (sh & 0x1f) | (((sh >> 5) & 1) << 1);
454
    mb = MB64 ((mb >> 5) | ((mb << 1) & 0x3f));
455
    tcg_out32 (s, op | RA (ra) | RS (rs) | sh | mb);
456
}
457

    
458
static void tcg_out_movi32 (TCGContext *s, int ret, int32_t arg)
459
{
460
    if (arg == (int16_t) arg)
461
        tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
462
    else {
463
        tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
464
        if (arg & 0xffff)
465
            tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
466
    }
467
}
468

    
469
static void tcg_out_movi (TCGContext *s, TCGType type,
470
                          int ret, tcg_target_long arg)
471
{
472
    int32_t arg32 = arg;
473
    arg = type == TCG_TYPE_I32 ? arg & 0xffffffff : arg;
474

    
475
    if (arg == arg32) {
476
        tcg_out_movi32 (s, ret, arg32);
477
    }
478
    else {
479
        if ((uint64_t) arg >> 32) {
480
            uint16_t h16 = arg >> 16;
481
            uint16_t l16 = arg;
482

    
483
            tcg_out_movi32 (s, ret, arg >> 32);
484
            tcg_out_rld (s, RLDICR, ret, ret, 32, 31);
485
            if (h16) tcg_out32 (s, ORIS | RS (ret) | RA (ret) | h16);
486
            if (l16) tcg_out32 (s, ORI | RS (ret) | RA (ret) | l16);
487
        }
488
        else {
489
            tcg_out_movi32 (s, ret, arg32);
490
            if (arg32 < 0)
491
                tcg_out_rld (s, RLDICL, ret, ret, 0, 32);
492
        }
493
    }
494
}
495

    
496
static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
497
{
498
    tcg_target_long disp;
499

    
500
    disp = target - (tcg_target_long) s->code_ptr;
501
    if ((disp << 38) >> 38 == disp)
502
        tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
503
    else {
504
        tcg_out_movi (s, TCG_TYPE_I64, 0, (tcg_target_long) target);
505
        tcg_out32 (s, MTSPR | RS (0) | CTR);
506
        tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
507
    }
508
}
509

    
510
static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
511
{
512
#ifdef __APPLE__
513
    if (const_arg) {
514
        tcg_out_b (s, LK, arg);
515
    }
516
    else {
517
        tcg_out32 (s, MTSPR | RS (arg) | LR);
518
        tcg_out32 (s, BCLR | BO_ALWAYS | LK);
519
    }
520
#else
521
    int reg;
522

    
523
    if (const_arg) {
524
        reg = 2;
525
        tcg_out_movi (s, TCG_TYPE_I64, reg, arg);
526
    }
527
    else reg = arg;
528

    
529
    tcg_out32 (s, LD | RT (0) | RA (reg));
530
    tcg_out32 (s, MTSPR | RA (0) | CTR);
531
    tcg_out32 (s, LD | RT (11) | RA (reg) | 16);
532
    tcg_out32 (s, LD | RT (2) | RA (reg) | 8);
533
    tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
534
#endif
535
}
536

    
537
static void tcg_out_ldst (TCGContext *s, int ret, int addr,
538
                          int offset, int op1, int op2)
539
{
540
    if (offset == (int16_t) offset)
541
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
542
    else {
543
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
544
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
545
    }
546
}
547

    
548
static void tcg_out_ldsta (TCGContext *s, int ret, int addr,
549
                           int offset, int op1, int op2)
550
{
551
    if (offset == (int16_t) (offset & ~3))
552
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
553
    else {
554
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
555
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
556
    }
557
}
558

    
559
#if defined (CONFIG_SOFTMMU)
560

    
561
#include "../../softmmu_defs.h"
562

    
563
static void *qemu_ld_helpers[4] = {
564
    __ldb_mmu,
565
    __ldw_mmu,
566
    __ldl_mmu,
567
    __ldq_mmu,
568
};
569

    
570
static void *qemu_st_helpers[4] = {
571
    __stb_mmu,
572
    __stw_mmu,
573
    __stl_mmu,
574
    __stq_mmu,
575
};
576

    
577
static void tcg_out_tlb_read (TCGContext *s, int r0, int r1, int r2,
578
                              int addr_reg, int s_bits, int offset)
579
{
580
#if TARGET_LONG_BITS == 32
581
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
582

    
583
    tcg_out32 (s, (RLWINM
584
                   | RA (r0)
585
                   | RS (addr_reg)
586
                   | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
587
                   | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
588
                   | ME (31 - CPU_TLB_ENTRY_BITS)
589
                   )
590
        );
591
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
592
    tcg_out32 (s, (LWZU | RT (r1) | RA (r0) | offset));
593
    tcg_out32 (s, (RLWINM
594
                   | RA (r2)
595
                   | RS (addr_reg)
596
                   | SH (0)
597
                   | MB ((32 - s_bits) & 31)
598
                   | ME (31 - TARGET_PAGE_BITS)
599
                   )
600
        );
601
#else
602
    tcg_out_rld (s, RLDICL, r0, addr_reg,
603
                 64 - TARGET_PAGE_BITS,
604
                 64 - CPU_TLB_BITS);
605
    tcg_out_rld (s, RLDICR, r0, r0,
606
                 CPU_TLB_ENTRY_BITS,
607
                 63 - CPU_TLB_ENTRY_BITS);
608

    
609
    tcg_out32 (s, ADD | TAB (r0, r0, TCG_AREG0));
610
    tcg_out32 (s, LD_ADDR | RT (r1) | RA (r0) | offset);
611

    
612
    if (!s_bits) {
613
        tcg_out_rld (s, RLDICR, r2, addr_reg, 0, 63 - TARGET_PAGE_BITS);
614
    }
615
    else {
616
        tcg_out_rld (s, RLDICL, r2, addr_reg,
617
                     64 - TARGET_PAGE_BITS,
618
                     TARGET_PAGE_BITS - s_bits);
619
        tcg_out_rld (s, RLDICL, r2, r2, TARGET_PAGE_BITS, 0);
620
    }
621
#endif
622
}
623
#endif
624

    
625
static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
626
{
627
    int addr_reg, data_reg, r0, r1, rbase, mem_index, s_bits, bswap;
628
#ifdef CONFIG_SOFTMMU
629
    int r2;
630
    void *label1_ptr, *label2_ptr;
631
#endif
632

    
633
    data_reg = *args++;
634
    addr_reg = *args++;
635
    mem_index = *args;
636
    s_bits = opc & 3;
637

    
638
#ifdef CONFIG_SOFTMMU
639
    r0 = 3;
640
    r1 = 4;
641
    r2 = 0;
642
    rbase = 0;
643

    
644
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, s_bits,
645
                      offsetof (CPUState, tlb_table[mem_index][0].addr_read));
646

    
647
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
648

    
649
    label1_ptr = s->code_ptr;
650
#ifdef FAST_PATH
651
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
652
#endif
653

    
654
    /* slow path */
655
    tcg_out_mov (s, 3, addr_reg);
656
    tcg_out_movi (s, TCG_TYPE_I64, 4, mem_index);
657

    
658
    tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
659

    
660
    switch (opc) {
661
    case 0|4:
662
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
663
        break;
664
    case 1|4:
665
        tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
666
        break;
667
    case 2|4:
668
        tcg_out32 (s, EXTSW | RA (data_reg) | RS (3));
669
        break;
670
    case 0:
671
    case 1:
672
    case 2:
673
    case 3:
674
        if (data_reg != 3)
675
            tcg_out_mov (s, data_reg, 3);
676
        break;
677
    }
678
    label2_ptr = s->code_ptr;
679
    tcg_out32 (s, B);
680

    
681
    /* label1: fast path */
682
#ifdef FAST_PATH
683
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
684
#endif
685

    
686
    /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
687
    tcg_out32 (s, (LD_ADDEND
688
                   | RT (r0)
689
                   | RA (r0)
690
                   | (offsetof (CPUTLBEntry, addend)
691
                      - offsetof (CPUTLBEntry, addr_read))
692
                   ));
693
    /* r0 = env->tlb_table[mem_index][index].addend */
694
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
695
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
696

    
697
#else  /* !CONFIG_SOFTMMU */
698
#if TARGET_LONG_BITS == 32
699
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
700
#endif
701
    r0 = addr_reg;
702
    r1 = 3;
703
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
704
#endif
705

    
706
#ifdef TARGET_WORDS_BIGENDIAN
707
    bswap = 0;
708
#else
709
    bswap = 1;
710
#endif
711
    switch (opc) {
712
    default:
713
    case 0:
714
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
715
        break;
716
    case 0|4:
717
        tcg_out32 (s, LBZX | TAB (data_reg, rbase, r0));
718
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
719
        break;
720
    case 1:
721
        if (bswap)
722
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
723
        else
724
            tcg_out32 (s, LHZX | TAB (data_reg, rbase, r0));
725
        break;
726
    case 1|4:
727
        if (bswap) {
728
            tcg_out32 (s, LHBRX | TAB (data_reg, rbase, r0));
729
            tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
730
        }
731
        else tcg_out32 (s, LHAX | TAB (data_reg, rbase, r0));
732
        break;
733
    case 2:
734
        if (bswap)
735
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
736
        else
737
            tcg_out32 (s, LWZX | TAB (data_reg, rbase, r0));
738
        break;
739
    case 2|4:
740
        if (bswap) {
741
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
742
            tcg_out32 (s, EXTSW | RA (data_reg) | RS (data_reg));
743
        }
744
        else tcg_out32 (s, LWAX | TAB (data_reg, rbase, r0));
745
        break;
746
    case 3:
747
#ifdef CONFIG_USE_GUEST_BASE
748
        if (bswap) {
749
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
750
            tcg_out32 (s, LWBRX | TAB (data_reg, rbase, r0));
751
            tcg_out32 (s, LWBRX | TAB (      r1, rbase, r1));
752
            tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
753
        }
754
        else tcg_out32 (s, LDX | TAB (data_reg, rbase, r0));
755
#else
756
        if (bswap) {
757
            tcg_out_movi32 (s, 0, 4);
758
            tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
759
            tcg_out32 (s, LWBRX | RT (      r1) | RA (r0));
760
            tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
761
        }
762
        else tcg_out32 (s, LD | RT (data_reg) | RA (r0));
763
#endif
764
        break;
765
    }
766

    
767
#ifdef CONFIG_SOFTMMU
768
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
769
#endif
770
}
771

    
772
static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
773
{
774
    int addr_reg, r0, r1, rbase, data_reg, mem_index, bswap;
775
#ifdef CONFIG_SOFTMMU
776
    int r2;
777
    void *label1_ptr, *label2_ptr;
778
#endif
779

    
780
    data_reg = *args++;
781
    addr_reg = *args++;
782
    mem_index = *args;
783

    
784
#ifdef CONFIG_SOFTMMU
785
    r0 = 3;
786
    r1 = 4;
787
    r2 = 0;
788
    rbase = 0;
789

    
790
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, opc,
791
                      offsetof (CPUState, tlb_table[mem_index][0].addr_write));
792

    
793
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
794

    
795
    label1_ptr = s->code_ptr;
796
#ifdef FAST_PATH
797
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
798
#endif
799

    
800
    /* slow path */
801
    tcg_out_mov (s, 3, addr_reg);
802
    tcg_out_rld (s, RLDICL, 4, data_reg, 0, 64 - (1 << (3 + opc)));
803
    tcg_out_movi (s, TCG_TYPE_I64, 5, mem_index);
804

    
805
    tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
806

    
807
    label2_ptr = s->code_ptr;
808
    tcg_out32 (s, B);
809

    
810
    /* label1: fast path */
811
#ifdef FAST_PATH
812
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
813
#endif
814

    
815
    tcg_out32 (s, (LD_ADDEND
816
                   | RT (r0)
817
                   | RA (r0)
818
                   | (offsetof (CPUTLBEntry, addend)
819
                      - offsetof (CPUTLBEntry, addr_write))
820
                   ));
821
    /* r0 = env->tlb_table[mem_index][index].addend */
822
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
823
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
824

    
825
#else  /* !CONFIG_SOFTMMU */
826
#if TARGET_LONG_BITS == 32
827
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
828
#endif
829
    r1 = 3;
830
    r0 = addr_reg;
831
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
832
#endif
833

    
834
#ifdef TARGET_WORDS_BIGENDIAN
835
    bswap = 0;
836
#else
837
    bswap = 1;
838
#endif
839
    switch (opc) {
840
    case 0:
841
        tcg_out32 (s, STBX | SAB (data_reg, rbase, r0));
842
        break;
843
    case 1:
844
        if (bswap)
845
            tcg_out32 (s, STHBRX | SAB (data_reg, rbase, r0));
846
        else
847
            tcg_out32 (s, STHX | SAB (data_reg, rbase, r0));
848
        break;
849
    case 2:
850
        if (bswap)
851
            tcg_out32 (s, STWBRX | SAB (data_reg, rbase, r0));
852
        else
853
            tcg_out32 (s, STWX | SAB (data_reg, rbase, r0));
854
        break;
855
    case 3:
856
        if (bswap) {
857
            tcg_out32 (s, STWBRX | SAB (data_reg, rbase, r0));
858
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
859
            tcg_out_rld (s, RLDICL, 0, data_reg, 32, 0);
860
            tcg_out32 (s, STWBRX | SAB (0, rbase, r1));
861
        }
862
        else tcg_out32 (s, STDX | SAB (data_reg, rbase, r0));
863
        break;
864
    }
865

    
866
#ifdef CONFIG_SOFTMMU
867
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
868
#endif
869
}
870

    
871
void tcg_target_qemu_prologue (TCGContext *s)
872
{
873
    int i, frame_size;
874
#ifndef __APPLE__
875
    uint64_t addr;
876
#endif
877

    
878
    frame_size = 0
879
        + 8                     /* back chain */
880
        + 8                     /* CR */
881
        + 8                     /* LR */
882
        + 8                     /* compiler doubleword */
883
        + 8                     /* link editor doubleword */
884
        + 8                     /* TOC save area */
885
        + TCG_STATIC_CALL_ARGS_SIZE
886
        + ARRAY_SIZE (tcg_target_callee_save_regs) * 8
887
        ;
888
    frame_size = (frame_size + 15) & ~15;
889

    
890
#ifndef __APPLE__
891
    /* First emit adhoc function descriptor */
892
    addr = (uint64_t) s->code_ptr + 24;
893
    tcg_out32 (s, addr >> 32); tcg_out32 (s, addr); /* entry point */
894
    s->code_ptr += 16;          /* skip TOC and environment pointer */
895
#endif
896

    
897
    /* Prologue */
898
    tcg_out32 (s, MFSPR | RT (0) | LR);
899
    tcg_out32 (s, STDU | RS (1) | RA (1) | (-frame_size & 0xffff));
900
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
901
        tcg_out32 (s, (STD
902
                       | RS (tcg_target_callee_save_regs[i])
903
                       | RA (1)
904
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
905
                       )
906
            );
907
    tcg_out32 (s, STD | RS (0) | RA (1) | (frame_size + 16));
908

    
909
#ifdef CONFIG_USE_GUEST_BASE
910
    tcg_out_movi (s, TCG_TYPE_I64, TCG_GUEST_BASE_REG, GUEST_BASE);
911
#endif
912

    
913
    tcg_out32 (s, MTSPR | RS (3) | CTR);
914
    tcg_out32 (s, BCCTR | BO_ALWAYS);
915

    
916
    /* Epilogue */
917
    tb_ret_addr = s->code_ptr;
918

    
919
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
920
        tcg_out32 (s, (LD
921
                       | RT (tcg_target_callee_save_regs[i])
922
                       | RA (1)
923
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
924
                       )
925
            );
926
    tcg_out32 (s, LD | RT (0) | RA (1) | (frame_size + 16));
927
    tcg_out32 (s, MTSPR | RS (0) | LR);
928
    tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
929
    tcg_out32 (s, BCLR | BO_ALWAYS);
930
}
931

    
932
static void tcg_out_ld (TCGContext *s, TCGType type, int ret, int arg1,
933
                        tcg_target_long arg2)
934
{
935
    if (type == TCG_TYPE_I32)
936
        tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
937
    else
938
        tcg_out_ldsta (s, ret, arg1, arg2, LD, LDX);
939
}
940

    
941
static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,
942
                        tcg_target_long arg2)
943
{
944
    if (type == TCG_TYPE_I32)
945
        tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
946
    else
947
        tcg_out_ldsta (s, arg, arg1, arg2, STD, STDX);
948
}
949

    
950
static void ppc_addi32 (TCGContext *s, int rt, int ra, tcg_target_long si)
951
{
952
    if (!si && rt == ra)
953
        return;
954

    
955
    if (si == (int16_t) si)
956
        tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
957
    else {
958
        uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
959
        tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
960
        tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
961
    }
962
}
963

    
964
static void ppc_addi64 (TCGContext *s, int rt, int ra, tcg_target_long si)
965
{
966
    /* XXX: suboptimal */
967
    if (si == (int16_t) si
968
        || ((((uint64_t) si >> 31) == 0) && (si & 0x8000) == 0))
969
        ppc_addi32 (s, rt, ra, si);
970
    else {
971
        tcg_out_movi (s, TCG_TYPE_I64, 0, si);
972
        tcg_out32 (s, ADD | RT (rt) | RA (ra));
973
    }
974
}
975

    
976
static void tcg_out_addi (TCGContext *s, int reg, tcg_target_long val)
977
{
978
    ppc_addi64 (s, reg, reg, val);
979
}
980

    
981
static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
982
                         int const_arg2, int cr, int arch64)
983
{
984
    int imm;
985
    uint32_t op;
986

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

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

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

    
1036
    default:
1037
        tcg_abort ();
1038
    }
1039
    op |= BF (cr) | (arch64 << 21);
1040

    
1041
    if (imm)
1042
        tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
1043
    else {
1044
        if (const_arg2) {
1045
            tcg_out_movi (s, TCG_TYPE_I64, 0, arg2);
1046
            tcg_out32 (s, op | RA (arg1) | RB (0));
1047
        }
1048
        else
1049
            tcg_out32 (s, op | RA (arg1) | RB (arg2));
1050
    }
1051

    
1052
}
1053

    
1054
static void tcg_out_setcond (TCGContext *s, TCGType type, int cond, TCGArg arg0,
1055
                             TCGArg arg1, TCGArg arg2, int const_arg2)
1056
{
1057
    int crop, sh, arg;
1058

    
1059
    switch (cond) {
1060
    case TCG_COND_EQ:
1061
        if (const_arg2) {
1062
            if (!arg2) {
1063
                arg = arg1;
1064
            }
1065
            else {
1066
                arg = 0;
1067
                if ((uint16_t) arg2 == arg2) {
1068
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1069
                }
1070
                else {
1071
                    tcg_out_movi (s, type, 0, arg2);
1072
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1073
                }
1074
            }
1075
        }
1076
        else {
1077
            arg = 0;
1078
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1079
        }
1080

    
1081
        if (type == TCG_TYPE_I64) {
1082
            tcg_out32 (s, CNTLZD | RS (arg) | RA (0));
1083
            tcg_out_rld (s, RLDICL, arg0, 0, 58, 6);
1084
        }
1085
        else {
1086
            tcg_out32 (s, CNTLZW | RS (arg) | RA (0));
1087
            tcg_out32 (s, (RLWINM
1088
                           | RA (arg0)
1089
                           | RS (0)
1090
                           | SH (27)
1091
                           | MB (5)
1092
                           | ME (31)
1093
                           )
1094
                );
1095
        }
1096
        break;
1097

    
1098
    case TCG_COND_NE:
1099
        if (const_arg2) {
1100
            if (!arg2) {
1101
                arg = arg1;
1102
            }
1103
            else {
1104
                arg = 0;
1105
                if ((uint16_t) arg2 == arg2) {
1106
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1107
                }
1108
                else {
1109
                    tcg_out_movi (s, type, 0, arg2);
1110
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1111
                }
1112
            }
1113
        }
1114
        else {
1115
            arg = 0;
1116
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1117
        }
1118

    
1119
        if (arg == arg1 && arg1 == arg0) {
1120
            tcg_out32 (s, ADDIC | RT (0) | RA (arg) | 0xffff);
1121
            tcg_out32 (s, SUBFE | TAB (arg0, 0, arg));
1122
        }
1123
        else {
1124
            tcg_out32 (s, ADDIC | RT (arg0) | RA (arg) | 0xffff);
1125
            tcg_out32 (s, SUBFE | TAB (arg0, arg0, arg));
1126
        }
1127
        break;
1128

    
1129
    case TCG_COND_GT:
1130
    case TCG_COND_GTU:
1131
        sh = 30;
1132
        crop = 0;
1133
        goto crtest;
1134

    
1135
    case TCG_COND_LT:
1136
    case TCG_COND_LTU:
1137
        sh = 29;
1138
        crop = 0;
1139
        goto crtest;
1140

    
1141
    case TCG_COND_GE:
1142
    case TCG_COND_GEU:
1143
        sh = 31;
1144
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_LT) | BB (7, CR_LT);
1145
        goto crtest;
1146

    
1147
    case TCG_COND_LE:
1148
    case TCG_COND_LEU:
1149
        sh = 31;
1150
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_GT) | BB (7, CR_GT);
1151
    crtest:
1152
        tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, type == TCG_TYPE_I64);
1153
        if (crop) tcg_out32 (s, crop);
1154
        tcg_out32 (s, MFCR | RT (0));
1155
        tcg_out32 (s, (RLWINM
1156
                       | RA (arg0)
1157
                       | RS (0)
1158
                       | SH (sh)
1159
                       | MB (31)
1160
                       | ME (31)
1161
                       )
1162
            );
1163
        break;
1164

    
1165
    default:
1166
        tcg_abort ();
1167
    }
1168
}
1169

    
1170
static void tcg_out_bc (TCGContext *s, int bc, int label_index)
1171
{
1172
    TCGLabel *l = &s->labels[label_index];
1173

    
1174
    if (l->has_value)
1175
        tcg_out32 (s, bc | reloc_pc14_val (s->code_ptr, l->u.value));
1176
    else {
1177
        uint16_t val = *(uint16_t *) &s->code_ptr[2];
1178

    
1179
        /* Thanks to Andrzej Zaborowski */
1180
        tcg_out32 (s, bc | (val & 0xfffc));
1181
        tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
1182
    }
1183
}
1184

    
1185
static void tcg_out_brcond (TCGContext *s, int cond,
1186
                            TCGArg arg1, TCGArg arg2, int const_arg2,
1187
                            int label_index, int arch64)
1188
{
1189
    tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, arch64);
1190
    tcg_out_bc (s, tcg_to_bc[cond], label_index);
1191
}
1192

    
1193
void ppc_tb_set_jmp_target (unsigned long jmp_addr, unsigned long addr)
1194
{
1195
    TCGContext s;
1196
    unsigned long patch_size;
1197

    
1198
    s.code_ptr = (uint8_t *) jmp_addr;
1199
    tcg_out_b (&s, 0, addr);
1200
    patch_size = s.code_ptr - (uint8_t *) jmp_addr;
1201
    flush_icache_range (jmp_addr, jmp_addr + patch_size);
1202
}
1203

    
1204
static void tcg_out_op (TCGContext *s, TCGOpcode opc, const TCGArg *args,
1205
                        const int *const_args)
1206
{
1207
    int c;
1208

    
1209
    switch (opc) {
1210
    case INDEX_op_exit_tb:
1211
        tcg_out_movi (s, TCG_TYPE_I64, TCG_REG_R3, args[0]);
1212
        tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1213
        break;
1214
    case INDEX_op_goto_tb:
1215
        if (s->tb_jmp_offset) {
1216
            /* direct jump method */
1217

    
1218
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1219
            s->code_ptr += 28;
1220
        }
1221
        else {
1222
            tcg_abort ();
1223
        }
1224
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1225
        break;
1226
    case INDEX_op_br:
1227
        {
1228
            TCGLabel *l = &s->labels[args[0]];
1229

    
1230
            if (l->has_value) {
1231
                tcg_out_b (s, 0, l->u.value);
1232
            }
1233
            else {
1234
                uint32_t val = *(uint32_t *) s->code_ptr;
1235

    
1236
                /* Thanks to Andrzej Zaborowski */
1237
                tcg_out32 (s, B | (val & 0x3fffffc));
1238
                tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1239
            }
1240
        }
1241
        break;
1242
    case INDEX_op_call:
1243
        tcg_out_call (s, args[0], const_args[0]);
1244
        break;
1245
    case INDEX_op_jmp:
1246
        if (const_args[0]) {
1247
            tcg_out_b (s, 0, args[0]);
1248
        }
1249
        else {
1250
            tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1251
            tcg_out32 (s, BCCTR | BO_ALWAYS);
1252
        }
1253
        break;
1254
    case INDEX_op_movi_i32:
1255
        tcg_out_movi (s, TCG_TYPE_I32, args[0], args[1]);
1256
        break;
1257
    case INDEX_op_movi_i64:
1258
        tcg_out_movi (s, TCG_TYPE_I64, args[0], args[1]);
1259
        break;
1260
    case INDEX_op_ld8u_i32:
1261
    case INDEX_op_ld8u_i64:
1262
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1263
        break;
1264
    case INDEX_op_ld8s_i32:
1265
    case INDEX_op_ld8s_i64:
1266
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1267
        tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1268
        break;
1269
    case INDEX_op_ld16u_i32:
1270
    case INDEX_op_ld16u_i64:
1271
        tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1272
        break;
1273
    case INDEX_op_ld16s_i32:
1274
    case INDEX_op_ld16s_i64:
1275
        tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1276
        break;
1277
    case INDEX_op_ld_i32:
1278
    case INDEX_op_ld32u_i64:
1279
        tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1280
        break;
1281
    case INDEX_op_ld32s_i64:
1282
        tcg_out_ldsta (s, args[0], args[1], args[2], LWA, LWAX);
1283
        break;
1284
    case INDEX_op_ld_i64:
1285
        tcg_out_ldsta (s, args[0], args[1], args[2], LD, LDX);
1286
        break;
1287
    case INDEX_op_st8_i32:
1288
    case INDEX_op_st8_i64:
1289
        tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1290
        break;
1291
    case INDEX_op_st16_i32:
1292
    case INDEX_op_st16_i64:
1293
        tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1294
        break;
1295
    case INDEX_op_st_i32:
1296
    case INDEX_op_st32_i64:
1297
        tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1298
        break;
1299
    case INDEX_op_st_i64:
1300
        tcg_out_ldsta (s, args[0], args[1], args[2], STD, STDX);
1301
        break;
1302

    
1303
    case INDEX_op_add_i32:
1304
        if (const_args[2])
1305
            ppc_addi32 (s, args[0], args[1], args[2]);
1306
        else
1307
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1308
        break;
1309
    case INDEX_op_sub_i32:
1310
        if (const_args[2])
1311
            ppc_addi32 (s, args[0], args[1], -args[2]);
1312
        else
1313
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1314
        break;
1315

    
1316
    case INDEX_op_and_i64:
1317
    case INDEX_op_and_i32:
1318
        if (const_args[2]) {
1319
            if ((args[2] & 0xffff) == args[2])
1320
                tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | args[2]);
1321
            else if ((args[2] & 0xffff0000) == args[2])
1322
                tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1323
                           | ((args[2] >> 16) & 0xffff));
1324
            else {
1325
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1326
                                  ? TCG_TYPE_I32
1327
                                  : TCG_TYPE_I64),
1328
                              0, args[2]);
1329
                tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1330
            }
1331
        }
1332
        else
1333
            tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1334
        break;
1335
    case INDEX_op_or_i64:
1336
    case INDEX_op_or_i32:
1337
        if (const_args[2]) {
1338
            if (args[2] & 0xffff) {
1339
                tcg_out32 (s, ORI | RS (args[1]) | RA (args[0])
1340
                           | (args[2] & 0xffff));
1341
                if (args[2] >> 16)
1342
                    tcg_out32 (s, ORIS | RS (args[0])  | RA (args[0])
1343
                               | ((args[2] >> 16) & 0xffff));
1344
            }
1345
            else {
1346
                tcg_out32 (s, ORIS | RS (args[1])  | RA (args[0])
1347
                           | ((args[2] >> 16) & 0xffff));
1348
            }
1349
        }
1350
        else
1351
            tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1352
        break;
1353
    case INDEX_op_xor_i64:
1354
    case INDEX_op_xor_i32:
1355
        if (const_args[2]) {
1356
            if ((args[2] & 0xffff) == args[2])
1357
                tcg_out32 (s, XORI | RS (args[1])  | RA (args[0])
1358
                           | (args[2] & 0xffff));
1359
            else if ((args[2] & 0xffff0000) == args[2])
1360
                tcg_out32 (s, XORIS | RS (args[1])  | RA (args[0])
1361
                           | ((args[2] >> 16) & 0xffff));
1362
            else {
1363
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1364
                                  ? TCG_TYPE_I32
1365
                                  : TCG_TYPE_I64),
1366
                              0, args[2]);
1367
                tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1368
            }
1369
        }
1370
        else
1371
            tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1372
        break;
1373

    
1374
    case INDEX_op_mul_i32:
1375
        if (const_args[2]) {
1376
            if (args[2] == (int16_t) args[2])
1377
                tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1378
                           | (args[2] & 0xffff));
1379
            else {
1380
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1381
                tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1382
            }
1383
        }
1384
        else
1385
            tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1386
        break;
1387

    
1388
    case INDEX_op_div_i32:
1389
        tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1390
        break;
1391

    
1392
    case INDEX_op_divu_i32:
1393
        tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1394
        break;
1395

    
1396
    case INDEX_op_rem_i32:
1397
        tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1398
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1399
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1400
        break;
1401

    
1402
    case INDEX_op_remu_i32:
1403
        tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1404
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1405
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1406
        break;
1407

    
1408
    case INDEX_op_shl_i32:
1409
        if (const_args[2]) {
1410
            tcg_out32 (s, (RLWINM
1411
                           | RA (args[0])
1412
                           | RS (args[1])
1413
                           | SH (args[2])
1414
                           | MB (0)
1415
                           | ME (31 - args[2])
1416
                           )
1417
                );
1418
        }
1419
        else
1420
            tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1421
        break;
1422
    case INDEX_op_shr_i32:
1423
        if (const_args[2]) {
1424
            tcg_out32 (s, (RLWINM
1425
                           | RA (args[0])
1426
                           | RS (args[1])
1427
                           | SH (32 - args[2])
1428
                           | MB (args[2])
1429
                           | ME (31)
1430
                           )
1431
                );
1432
        }
1433
        else
1434
            tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1435
        break;
1436
    case INDEX_op_sar_i32:
1437
        if (const_args[2])
1438
            tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1439
        else
1440
            tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1441
        break;
1442

    
1443
    case INDEX_op_brcond_i32:
1444
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 0);
1445
        break;
1446

    
1447
    case INDEX_op_brcond_i64:
1448
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 1);
1449
        break;
1450

    
1451
    case INDEX_op_neg_i32:
1452
    case INDEX_op_neg_i64:
1453
        tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1454
        break;
1455

    
1456
    case INDEX_op_add_i64:
1457
        if (const_args[2])
1458
            ppc_addi64 (s, args[0], args[1], args[2]);
1459
        else
1460
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1461
        break;
1462
    case INDEX_op_sub_i64:
1463
        if (const_args[2])
1464
            ppc_addi64 (s, args[0], args[1], -args[2]);
1465
        else
1466
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1467
        break;
1468

    
1469
    case INDEX_op_shl_i64:
1470
        if (const_args[2])
1471
            tcg_out_rld (s, RLDICR, args[0], args[1], args[2], 63 - args[2]);
1472
        else
1473
            tcg_out32 (s, SLD | SAB (args[1], args[0], args[2]));
1474
        break;
1475
    case INDEX_op_shr_i64:
1476
        if (const_args[2])
1477
            tcg_out_rld (s, RLDICL, args[0], args[1], 64 - args[2], args[2]);
1478
        else
1479
            tcg_out32 (s, SRD | SAB (args[1], args[0], args[2]));
1480
        break;
1481
    case INDEX_op_sar_i64:
1482
        if (const_args[2]) {
1483
            int sh = SH (args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1484
            tcg_out32 (s, SRADI | RA (args[0]) | RS (args[1]) | sh);
1485
        }
1486
        else
1487
            tcg_out32 (s, SRAD | SAB (args[1], args[0], args[2]));
1488
        break;
1489

    
1490
    case INDEX_op_mul_i64:
1491
        tcg_out32 (s, MULLD | TAB (args[0], args[1], args[2]));
1492
        break;
1493
    case INDEX_op_div_i64:
1494
        tcg_out32 (s, DIVD | TAB (args[0], args[1], args[2]));
1495
        break;
1496
    case INDEX_op_divu_i64:
1497
        tcg_out32 (s, DIVDU | TAB (args[0], args[1], args[2]));
1498
        break;
1499
    case INDEX_op_rem_i64:
1500
        tcg_out32 (s, DIVD | TAB (0, args[1], args[2]));
1501
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1502
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1503
        break;
1504
    case INDEX_op_remu_i64:
1505
        tcg_out32 (s, DIVDU | TAB (0, args[1], args[2]));
1506
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1507
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1508
        break;
1509

    
1510
    case INDEX_op_qemu_ld8u:
1511
        tcg_out_qemu_ld (s, args, 0);
1512
        break;
1513
    case INDEX_op_qemu_ld8s:
1514
        tcg_out_qemu_ld (s, args, 0 | 4);
1515
        break;
1516
    case INDEX_op_qemu_ld16u:
1517
        tcg_out_qemu_ld (s, args, 1);
1518
        break;
1519
    case INDEX_op_qemu_ld16s:
1520
        tcg_out_qemu_ld (s, args, 1 | 4);
1521
        break;
1522
    case INDEX_op_qemu_ld32u:
1523
        tcg_out_qemu_ld (s, args, 2);
1524
        break;
1525
    case INDEX_op_qemu_ld32s:
1526
        tcg_out_qemu_ld (s, args, 2 | 4);
1527
        break;
1528
    case INDEX_op_qemu_ld64:
1529
        tcg_out_qemu_ld (s, args, 3);
1530
        break;
1531
    case INDEX_op_qemu_st8:
1532
        tcg_out_qemu_st (s, args, 0);
1533
        break;
1534
    case INDEX_op_qemu_st16:
1535
        tcg_out_qemu_st (s, args, 1);
1536
        break;
1537
    case INDEX_op_qemu_st32:
1538
        tcg_out_qemu_st (s, args, 2);
1539
        break;
1540
    case INDEX_op_qemu_st64:
1541
        tcg_out_qemu_st (s, args, 3);
1542
        break;
1543

    
1544
    case INDEX_op_ext8s_i32:
1545
    case INDEX_op_ext8s_i64:
1546
        c = EXTSB;
1547
        goto gen_ext;
1548
    case INDEX_op_ext16s_i32:
1549
    case INDEX_op_ext16s_i64:
1550
        c = EXTSH;
1551
        goto gen_ext;
1552
    case INDEX_op_ext32s_i64:
1553
        c = EXTSW;
1554
        goto gen_ext;
1555
    gen_ext:
1556
        tcg_out32 (s, c | RS (args[1]) | RA (args[0]));
1557
        break;
1558

    
1559
    case INDEX_op_setcond_i32:
1560
        tcg_out_setcond (s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
1561
                         const_args[2]);
1562
        break;
1563
    case INDEX_op_setcond_i64:
1564
        tcg_out_setcond (s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
1565
                         const_args[2]);
1566
        break;
1567

    
1568
    default:
1569
        tcg_dump_ops (s, stderr);
1570
        tcg_abort ();
1571
    }
1572
}
1573

    
1574
static const TCGTargetOpDef ppc_op_defs[] = {
1575
    { INDEX_op_exit_tb, { } },
1576
    { INDEX_op_goto_tb, { } },
1577
    { INDEX_op_call, { "ri" } },
1578
    { INDEX_op_jmp, { "ri" } },
1579
    { INDEX_op_br, { } },
1580

    
1581
    { INDEX_op_mov_i32, { "r", "r" } },
1582
    { INDEX_op_mov_i64, { "r", "r" } },
1583
    { INDEX_op_movi_i32, { "r" } },
1584
    { INDEX_op_movi_i64, { "r" } },
1585

    
1586
    { INDEX_op_ld8u_i32, { "r", "r" } },
1587
    { INDEX_op_ld8s_i32, { "r", "r" } },
1588
    { INDEX_op_ld16u_i32, { "r", "r" } },
1589
    { INDEX_op_ld16s_i32, { "r", "r" } },
1590
    { INDEX_op_ld_i32, { "r", "r" } },
1591
    { INDEX_op_ld_i64, { "r", "r" } },
1592
    { INDEX_op_st8_i32, { "r", "r" } },
1593
    { INDEX_op_st8_i64, { "r", "r" } },
1594
    { INDEX_op_st16_i32, { "r", "r" } },
1595
    { INDEX_op_st16_i64, { "r", "r" } },
1596
    { INDEX_op_st_i32, { "r", "r" } },
1597
    { INDEX_op_st_i64, { "r", "r" } },
1598
    { INDEX_op_st32_i64, { "r", "r" } },
1599

    
1600
    { INDEX_op_ld8u_i64, { "r", "r" } },
1601
    { INDEX_op_ld8s_i64, { "r", "r" } },
1602
    { INDEX_op_ld16u_i64, { "r", "r" } },
1603
    { INDEX_op_ld16s_i64, { "r", "r" } },
1604
    { INDEX_op_ld32u_i64, { "r", "r" } },
1605
    { INDEX_op_ld32s_i64, { "r", "r" } },
1606
    { INDEX_op_ld_i64, { "r", "r" } },
1607

    
1608
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1609
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1610
    { INDEX_op_div_i32, { "r", "r", "r" } },
1611
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1612
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1613
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1614
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1615
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1616
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1617
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1618

    
1619
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1620
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1621
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1622

    
1623
    { INDEX_op_brcond_i32, { "r", "ri" } },
1624
    { INDEX_op_brcond_i64, { "r", "ri" } },
1625

    
1626
    { INDEX_op_neg_i32, { "r", "r" } },
1627

    
1628
    { INDEX_op_add_i64, { "r", "r", "ri" } },
1629
    { INDEX_op_sub_i64, { "r", "r", "ri" } },
1630
    { INDEX_op_and_i64, { "r", "r", "rZ" } },
1631
    { INDEX_op_or_i64, { "r", "r", "rZ" } },
1632
    { INDEX_op_xor_i64, { "r", "r", "rZ" } },
1633

    
1634
    { INDEX_op_shl_i64, { "r", "r", "ri" } },
1635
    { INDEX_op_shr_i64, { "r", "r", "ri" } },
1636
    { INDEX_op_sar_i64, { "r", "r", "ri" } },
1637

    
1638
    { INDEX_op_mul_i64, { "r", "r", "r" } },
1639
    { INDEX_op_div_i64, { "r", "r", "r" } },
1640
    { INDEX_op_divu_i64, { "r", "r", "r" } },
1641
    { INDEX_op_rem_i64, { "r", "r", "r" } },
1642
    { INDEX_op_remu_i64, { "r", "r", "r" } },
1643

    
1644
    { INDEX_op_neg_i64, { "r", "r" } },
1645

    
1646
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1647
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1648
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1649
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1650
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1651
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1652
    { INDEX_op_qemu_ld64, { "r", "L" } },
1653

    
1654
    { INDEX_op_qemu_st8, { "S", "S" } },
1655
    { INDEX_op_qemu_st16, { "S", "S" } },
1656
    { INDEX_op_qemu_st32, { "S", "S" } },
1657
    { INDEX_op_qemu_st64, { "S", "S" } },
1658

    
1659
    { INDEX_op_ext8s_i32, { "r", "r" } },
1660
    { INDEX_op_ext16s_i32, { "r", "r" } },
1661
    { INDEX_op_ext8s_i64, { "r", "r" } },
1662
    { INDEX_op_ext16s_i64, { "r", "r" } },
1663
    { INDEX_op_ext32s_i64, { "r", "r" } },
1664

    
1665
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1666
    { INDEX_op_setcond_i64, { "r", "r", "ri" } },
1667

    
1668
    { -1 },
1669
};
1670

    
1671
void tcg_target_init (TCGContext *s)
1672
{
1673
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1674
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1675
    tcg_regset_set32 (tcg_target_call_clobber_regs, 0,
1676
                     (1 << TCG_REG_R0) |
1677
#ifdef __APPLE__
1678
                     (1 << TCG_REG_R2) |
1679
#endif
1680
                     (1 << TCG_REG_R3) |
1681
                     (1 << TCG_REG_R4) |
1682
                     (1 << TCG_REG_R5) |
1683
                     (1 << TCG_REG_R6) |
1684
                     (1 << TCG_REG_R7) |
1685
                     (1 << TCG_REG_R8) |
1686
                     (1 << TCG_REG_R9) |
1687
                     (1 << TCG_REG_R10) |
1688
                     (1 << TCG_REG_R11) |
1689
                     (1 << TCG_REG_R12)
1690
        );
1691

    
1692
    tcg_regset_clear (s->reserved_regs);
1693
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R0);
1694
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R1);
1695
#ifndef __APPLE__
1696
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R2);
1697
#endif
1698
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R13);
1699

    
1700
#ifdef CONFIG_USE_GUEST_BASE
1701
    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1702
#endif
1703

    
1704
    tcg_add_target_add_op_defs (ppc_op_defs);
1705
}