Statistics
| Branch: | Revision:

root / tcg / ppc64 / tcg-target.c @ 9de187a0

History | View | Annotate | Download (41.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
#if TARGET_PHYS_ADDR_BITS == 32
32
#define LD_ADDEND LWZ
33
#else
34
#define LD_ADDEND LD
35
#endif
36

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

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

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

    
117
static const int tcg_target_call_iarg_regs[] = {
118
    TCG_REG_R3,
119
    TCG_REG_R4,
120
    TCG_REG_R5,
121
    TCG_REG_R6,
122
    TCG_REG_R7,
123
    TCG_REG_R8,
124
    TCG_REG_R9,
125
    TCG_REG_R10
126
};
127

    
128
static const int tcg_target_call_oarg_regs[2] = {
129
    TCG_REG_R3
130
};
131

    
132
static const int tcg_target_callee_save_regs[] = {
133
    TCG_REG_R14,
134
    TCG_REG_R15,
135
    TCG_REG_R16,
136
    TCG_REG_R17,
137
    TCG_REG_R18,
138
    TCG_REG_R19,
139
    TCG_REG_R20,
140
    TCG_REG_R21,
141
    TCG_REG_R22,
142
    TCG_REG_R23,
143
    TCG_REG_R24,
144
    TCG_REG_R25,
145
    TCG_REG_R26,
146
    /* TCG_REG_R27, */ /* currently used for the global env, so no
147
                          need to save */
148
    TCG_REG_R28,
149
    TCG_REG_R29,
150
    TCG_REG_R30,
151
    TCG_REG_R31
152
};
153

    
154
static uint32_t reloc_pc24_val (void *pc, tcg_target_long target)
155
{
156
    tcg_target_long disp;
157

    
158
    disp = target - (tcg_target_long) pc;
159
    if ((disp << 38) >> 38 != disp)
160
        tcg_abort ();
161

    
162
    return disp & 0x3fffffc;
163
}
164

    
165
static void reloc_pc24 (void *pc, tcg_target_long target)
166
{
167
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0x3fffffc)
168
        | reloc_pc24_val (pc, target);
169
}
170

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

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

    
179
    return disp & 0xfffc;
180
}
181

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

    
188
static void patch_reloc (uint8_t *code_ptr, int type,
189
                         tcg_target_long value, tcg_target_long addend)
190
{
191
    value += addend;
192
    switch (type) {
193
    case R_PPC_REL14:
194
        reloc_pc14 (code_ptr, value);
195
        break;
196
    case R_PPC_REL24:
197
        reloc_pc24 (code_ptr, value);
198
        break;
199
    default:
200
        tcg_abort ();
201
    }
202
}
203

    
204
/* maximum number of register used for input function arguments */
205
static int tcg_target_get_call_iarg_regs_count (int flags)
206
{
207
    return ARRAY_SIZE (tcg_target_call_iarg_regs);
208
}
209

    
210
/* parse target specific constraints */
211
static int target_parse_constraint (TCGArgConstraint *ct, const char **pct_str)
212
{
213
    const char *ct_str;
214

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

    
253
/* test if a constant matches the constraint */
254
static int tcg_target_const_match (tcg_target_long val,
255
                                   const TCGArgConstraint *arg_ct)
256
{
257
    int ct;
258

    
259
    ct = arg_ct->ct;
260
    if (ct & TCG_CT_CONST)
261
        return 1;
262
    else if ((ct & TCG_CT_CONST_U32) && (val == (uint32_t) val))
263
        return 1;
264
    return 0;
265
}
266

    
267
#define OPCD(opc) ((opc)<<26)
268
#define XO19(opc) (OPCD(19)|((opc)<<1))
269
#define XO30(opc) (OPCD(30)|((opc)<<2))
270
#define XO31(opc) (OPCD(31)|((opc)<<1))
271
#define XO58(opc) (OPCD(58)|(opc))
272
#define XO62(opc) (OPCD(62)|(opc))
273

    
274
#define B      OPCD( 18)
275
#define BC     OPCD( 16)
276
#define LBZ    OPCD( 34)
277
#define LHZ    OPCD( 40)
278
#define LHA    OPCD( 42)
279
#define LWZ    OPCD( 32)
280
#define STB    OPCD( 38)
281
#define STH    OPCD( 44)
282
#define STW    OPCD( 36)
283

    
284
#define STD    XO62(  0)
285
#define STDU   XO62(  1)
286
#define STDX   XO31(149)
287

    
288
#define LD     XO58(  0)
289
#define LDX    XO31( 21)
290
#define LDU    XO58(  1)
291
#define LWA    XO58(  2)
292
#define LWAX   XO31(341)
293

    
294
#define ADDI   OPCD( 14)
295
#define ADDIS  OPCD( 15)
296
#define ORI    OPCD( 24)
297
#define ORIS   OPCD( 25)
298
#define XORI   OPCD( 26)
299
#define XORIS  OPCD( 27)
300
#define ANDI   OPCD( 28)
301
#define ANDIS  OPCD( 29)
302
#define MULLI  OPCD(  7)
303
#define CMPLI  OPCD( 10)
304
#define CMPI   OPCD( 11)
305

    
306
#define LWZU   OPCD( 33)
307
#define STWU   OPCD( 37)
308

    
309
#define RLWINM OPCD( 21)
310

    
311
#define RLDICL XO30(  0)
312
#define RLDICR XO30(  1)
313
#define RLDIMI XO30(  3)
314

    
315
#define BCLR   XO19( 16)
316
#define BCCTR  XO19(528)
317
#define CRAND  XO19(257)
318
#define CRANDC XO19(129)
319
#define CRNAND XO19(225)
320
#define CROR   XO19(449)
321

    
322
#define EXTSB  XO31(954)
323
#define EXTSH  XO31(922)
324
#define EXTSW  XO31(986)
325
#define ADD    XO31(266)
326
#define ADDE   XO31(138)
327
#define ADDC   XO31( 10)
328
#define AND    XO31( 28)
329
#define SUBF   XO31( 40)
330
#define SUBFC  XO31(  8)
331
#define SUBFE  XO31(136)
332
#define OR     XO31(444)
333
#define XOR    XO31(316)
334
#define MULLW  XO31(235)
335
#define MULHWU XO31( 11)
336
#define DIVW   XO31(491)
337
#define DIVWU  XO31(459)
338
#define CMP    XO31(  0)
339
#define CMPL   XO31( 32)
340
#define LHBRX  XO31(790)
341
#define LWBRX  XO31(534)
342
#define STHBRX XO31(918)
343
#define STWBRX XO31(662)
344
#define MFSPR  XO31(339)
345
#define MTSPR  XO31(467)
346
#define SRAWI  XO31(824)
347
#define NEG    XO31(104)
348

    
349
#define MULLD  XO31(233)
350
#define MULHD  XO31( 73)
351
#define MULHDU XO31(  9)
352
#define DIVD   XO31(489)
353
#define DIVDU  XO31(457)
354

    
355
#define LBZX   XO31( 87)
356
#define LHZX   XO31(276)
357
#define LHAX   XO31(343)
358
#define LWZX   XO31( 23)
359
#define STBX   XO31(215)
360
#define STHX   XO31(407)
361
#define STWX   XO31(151)
362

    
363
#define SPR(a,b) ((((a)<<5)|(b))<<11)
364
#define LR     SPR(8, 0)
365
#define CTR    SPR(9, 0)
366

    
367
#define SLW    XO31( 24)
368
#define SRW    XO31(536)
369
#define SRAW   XO31(792)
370

    
371
#define SLD    XO31( 27)
372
#define SRD    XO31(539)
373
#define SRAD   XO31(794)
374
#define SRADI  XO31(413<<1)
375

    
376
#define TW     XO31( 4)
377
#define TRAP   (TW | TO (31))
378

    
379
#define RT(r) ((r)<<21)
380
#define RS(r) ((r)<<21)
381
#define RA(r) ((r)<<16)
382
#define RB(r) ((r)<<11)
383
#define TO(t) ((t)<<21)
384
#define SH(s) ((s)<<11)
385
#define MB(b) ((b)<<6)
386
#define ME(e) ((e)<<1)
387
#define BO(o) ((o)<<21)
388
#define MB64(b) ((b)<<5)
389

    
390
#define LK    1
391

    
392
#define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
393
#define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
394

    
395
#define BF(n)    ((n)<<23)
396
#define BI(n, c) (((c)+((n)*4))<<16)
397
#define BT(n, c) (((c)+((n)*4))<<21)
398
#define BA(n, c) (((c)+((n)*4))<<16)
399
#define BB(n, c) (((c)+((n)*4))<<11)
400

    
401
#define BO_COND_TRUE  BO (12)
402
#define BO_COND_FALSE BO ( 4)
403
#define BO_ALWAYS     BO (20)
404

    
405
enum {
406
    CR_LT,
407
    CR_GT,
408
    CR_EQ,
409
    CR_SO
410
};
411

    
412
static const uint32_t tcg_to_bc[10] = {
413
    [TCG_COND_EQ]  = BC | BI (7, CR_EQ) | BO_COND_TRUE,
414
    [TCG_COND_NE]  = BC | BI (7, CR_EQ) | BO_COND_FALSE,
415
    [TCG_COND_LT]  = BC | BI (7, CR_LT) | BO_COND_TRUE,
416
    [TCG_COND_GE]  = BC | BI (7, CR_LT) | BO_COND_FALSE,
417
    [TCG_COND_LE]  = BC | BI (7, CR_GT) | BO_COND_FALSE,
418
    [TCG_COND_GT]  = BC | BI (7, CR_GT) | BO_COND_TRUE,
419
    [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
420
    [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
421
    [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
422
    [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
423
};
424

    
425
static void tcg_out_mov (TCGContext *s, int ret, int arg)
426
{
427
    tcg_out32 (s, OR | SAB (arg, ret, arg));
428
}
429

    
430
static void tcg_out_rld (TCGContext *s, int op, int ra, int rs, int sh, int mb)
431
{
432
    sh = SH (sh & 0x1f) | (((sh >> 5) & 1) << 1);
433
    mb = MB64 ((mb >> 5) | ((mb << 1) & 0x3f));
434
    tcg_out32 (s, op | RA (ra) | RS (rs) | sh | mb);
435
}
436

    
437
static void tcg_out_movi32 (TCGContext *s, int ret, int32_t arg)
438
{
439
    if (arg == (int16_t) arg)
440
        tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
441
    else {
442
        tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
443
        if (arg & 0xffff)
444
            tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
445
    }
446
}
447

    
448
static void tcg_out_movi (TCGContext *s, TCGType type,
449
                          int ret, tcg_target_long arg)
450
{
451
    int32_t arg32 = arg;
452

    
453
    if (type == TCG_TYPE_I32 || arg == arg32) {
454
        tcg_out_movi32 (s, ret, arg32);
455
    }
456
    else {
457
        if ((uint64_t) arg >> 32) {
458
            uint16_t h16 = arg >> 16;
459
            uint16_t l16 = arg;
460

    
461
            tcg_out_movi32 (s, ret, arg >> 32);
462
            tcg_out_rld (s, RLDICR, ret, ret, 32, 31);
463
            if (h16) tcg_out32 (s, ORIS | RS (ret) | RA (ret) | h16);
464
            if (l16) tcg_out32 (s, ORI | RS (ret) | RA (ret) | l16);
465
        }
466
        else {
467
            tcg_out_movi32 (s, ret, arg32);
468
            if (arg32 < 0)
469
                tcg_out_rld (s, RLDICL, ret, ret, 0, 32);
470
        }
471
    }
472
}
473

    
474
static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
475
{
476
    int reg;
477

    
478
    if (const_arg) {
479
        reg = 2;
480
        tcg_out_movi (s, TCG_TYPE_I64, reg, arg);
481
    }
482
    else reg = arg;
483

    
484
    tcg_out32 (s, LD | RT (0) | RA (reg));
485
    tcg_out32 (s, MTSPR | RA (0) | CTR);
486
    tcg_out32 (s, LD | RT (11) | RA (reg) | 16);
487
    tcg_out32 (s, LD | RT (2) | RA (reg) | 8);
488
    tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
489
}
490

    
491
static void tcg_out_ldst (TCGContext *s, int ret, int addr,
492
                          int offset, int op1, int op2)
493
{
494
    if (offset == (int16_t) offset)
495
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
496
    else {
497
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
498
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
499
    }
500
}
501

    
502
static void tcg_out_ldsta (TCGContext *s, int ret, int addr,
503
                           int offset, int op1, int op2)
504
{
505
    if (offset == (int16_t) (offset & ~3))
506
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
507
    else {
508
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
509
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
510
    }
511
}
512

    
513
static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
514
{
515
    tcg_target_long disp;
516

    
517
    disp = target - (tcg_target_long) s->code_ptr;
518
    if ((disp << 38) >> 38 == disp)
519
        tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
520
    else {
521
        tcg_out_movi (s, TCG_TYPE_I64, 0, (tcg_target_long) target);
522
        tcg_out32 (s, MTSPR | RS (0) | CTR);
523
        tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
524
    }
525
}
526

    
527
#if defined (CONFIG_SOFTMMU)
528

    
529
#include "../../softmmu_defs.h"
530

    
531
static void *qemu_ld_helpers[4] = {
532
    __ldb_mmu,
533
    __ldw_mmu,
534
    __ldl_mmu,
535
    __ldq_mmu,
536
};
537

    
538
static void *qemu_st_helpers[4] = {
539
    __stb_mmu,
540
    __stw_mmu,
541
    __stl_mmu,
542
    __stq_mmu,
543
};
544

    
545
static void tcg_out_tlb_read (TCGContext *s, int r0, int r1, int r2,
546
                              int addr_reg, int s_bits, int offset)
547
{
548
#if TARGET_LONG_BITS == 32
549
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
550

    
551
    tcg_out32 (s, (RLWINM
552
                   | RA (r0)
553
                   | RS (addr_reg)
554
                   | SH (32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS))
555
                   | MB (32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS))
556
                   | ME (31 - CPU_TLB_ENTRY_BITS)
557
                   )
558
        );
559
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
560
    tcg_out32 (s, (LWZU | RT (r1) | RA (r0) | offset));
561
    tcg_out32 (s, (RLWINM
562
                   | RA (r2)
563
                   | RS (addr_reg)
564
                   | SH (0)
565
                   | MB ((32 - s_bits) & 31)
566
                   | ME (31 - TARGET_PAGE_BITS)
567
                   )
568
        );
569
#else
570
    tcg_out_rld (s, RLDICL, r0, addr_reg,
571
                 64 - TARGET_PAGE_BITS,
572
                 64 - CPU_TLB_BITS);
573
    tcg_out_rld (s, RLDICR, r0, r0,
574
                 CPU_TLB_ENTRY_BITS,
575
                 63 - CPU_TLB_ENTRY_BITS);
576

    
577
    tcg_out32 (s, ADD | TAB (r0, r0, TCG_AREG0));
578
    tcg_out32 (s, LD_ADDR | RT (r1) | RA (r0) | offset);
579

    
580
    if (!s_bits) {
581
        tcg_out_rld (s, RLDICR, r2, addr_reg, 0, 63 - TARGET_PAGE_BITS);
582
    }
583
    else {
584
        tcg_out_rld (s, RLDICL, r2, addr_reg,
585
                     64 - TARGET_PAGE_BITS,
586
                     TARGET_PAGE_BITS - s_bits);
587
        tcg_out_rld (s, RLDICL, r2, r2, TARGET_PAGE_BITS, 0);
588
    }
589
#endif
590
}
591
#endif
592

    
593
static void tcg_out_qemu_ld (TCGContext *s, const TCGArg *args, int opc)
594
{
595
    int addr_reg, data_reg, r0, r1, mem_index, s_bits, bswap;
596
#ifdef CONFIG_SOFTMMU
597
    int r2;
598
    void *label1_ptr, *label2_ptr;
599
#endif
600

    
601
    data_reg = *args++;
602
    addr_reg = *args++;
603
    mem_index = *args;
604
    s_bits = opc & 3;
605

    
606
#ifdef CONFIG_SOFTMMU
607
    r0 = 3;
608
    r1 = 4;
609
    r2 = 0;
610

    
611
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, s_bits,
612
                      offsetof (CPUState, tlb_table[mem_index][0].addr_read));
613

    
614
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
615

    
616
    label1_ptr = s->code_ptr;
617
#ifdef FAST_PATH
618
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
619
#endif
620

    
621
    /* slow path */
622
    tcg_out_mov (s, 3, addr_reg);
623
    tcg_out_movi (s, TCG_TYPE_I64, 4, mem_index);
624

    
625
    tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
626

    
627
    switch (opc) {
628
    case 0|4:
629
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (3));
630
        break;
631
    case 1|4:
632
        tcg_out32 (s, EXTSH | RA (data_reg) | RS (3));
633
        break;
634
    case 2|4:
635
        tcg_out32 (s, EXTSW | RA (data_reg) | RS (3));
636
        break;
637
    case 0:
638
    case 1:
639
    case 2:
640
    case 3:
641
        if (data_reg != 3)
642
            tcg_out_mov (s, data_reg, 3);
643
        break;
644
    }
645
    label2_ptr = s->code_ptr;
646
    tcg_out32 (s, B);
647

    
648
    /* label1: fast path */
649
#ifdef FAST_PATH
650
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
651
#endif
652

    
653
    /* r0 now contains &env->tlb_table[mem_index][index].addr_read */
654
    tcg_out32 (s, (LD_ADDEND
655
                   | RT (r0)
656
                   | RA (r0)
657
                   | (offsetof (CPUTLBEntry, addend)
658
                      - offsetof (CPUTLBEntry, addr_read))
659
                   ));
660
    /* r0 = env->tlb_table[mem_index][index].addend */
661
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
662
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
663

    
664
#else  /* !CONFIG_SOFTMMU */
665
#if TARGET_LONG_BITS == 32
666
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
667
#endif
668
    r0 = addr_reg;
669
    r1 = 3;
670
#endif
671

    
672
#ifdef TARGET_WORDS_BIGENDIAN
673
    bswap = 0;
674
#else
675
    bswap = 1;
676
#endif
677
    switch (opc) {
678
    default:
679
    case 0:
680
        tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
681
        break;
682
    case 0|4:
683
        tcg_out32 (s, LBZ | RT (data_reg) | RA (r0));
684
        tcg_out32 (s, EXTSB | RA (data_reg) | RS (data_reg));
685
        break;
686
    case 1:
687
        if (bswap) tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
688
        else tcg_out32 (s, LHZ | RT (data_reg) | RA (r0));
689
        break;
690
    case 1|4:
691
        if (bswap) {
692
            tcg_out32 (s, LHBRX | RT (data_reg) | RB (r0));
693
            tcg_out32 (s, EXTSH | RA (data_reg) | RS (data_reg));
694
        }
695
        else tcg_out32 (s, LHA | RT (data_reg) | RA (r0));
696
        break;
697
    case 2:
698
        if (bswap) tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
699
        else tcg_out32 (s, LWZ | RT (data_reg)| RA (r0));
700
        break;
701
    case 2|4:
702
        if (bswap) {
703
            tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
704
            tcg_out32 (s, EXTSW | RA (data_reg) | RS (data_reg));
705
        }
706
        else tcg_out32 (s, LWA | RT (data_reg)| RA (r0));
707
        break;
708
    case 3:
709
        if (bswap) {
710
            tcg_out_movi32 (s, 0, 4);
711
            tcg_out32 (s, LWBRX | RT (data_reg) | RB (r0));
712
            tcg_out32 (s, LWBRX | RT (      r1) | RA (r0));
713
            tcg_out_rld (s, RLDIMI, data_reg, r1, 32, 0);
714
        }
715
        else tcg_out32 (s, LD | RT (data_reg) | RA (r0));
716
        break;
717
    }
718

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

    
724
static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
725
{
726
    int addr_reg, r0, r1, data_reg, mem_index, bswap;
727
#ifdef CONFIG_SOFTMMU
728
    int r2;
729
    void *label1_ptr, *label2_ptr;
730
#endif
731

    
732
    data_reg = *args++;
733
    addr_reg = *args++;
734
    mem_index = *args;
735

    
736
#ifdef CONFIG_SOFTMMU
737
    r0 = 3;
738
    r1 = 4;
739
    r2 = 0;
740

    
741
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, opc,
742
                      offsetof (CPUState, tlb_table[mem_index][0].addr_write));
743

    
744
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
745

    
746
    label1_ptr = s->code_ptr;
747
#ifdef FAST_PATH
748
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
749
#endif
750

    
751
    /* slow path */
752
    tcg_out_mov (s, 3, addr_reg);
753
    tcg_out_rld (s, RLDICL, 4, data_reg, 0, 64 - (1 << (3 + opc)));
754
    tcg_out_movi (s, TCG_TYPE_I64, 5, mem_index);
755

    
756
    tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
757

    
758
    label2_ptr = s->code_ptr;
759
    tcg_out32 (s, B);
760

    
761
    /* label1: fast path */
762
#ifdef FAST_PATH
763
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
764
#endif
765

    
766
    tcg_out32 (s, (LD_ADDEND
767
                   | RT (r0)
768
                   | RA (r0)
769
                   | (offsetof (CPUTLBEntry, addend)
770
                      - offsetof (CPUTLBEntry, addr_write))
771
                   ));
772
    /* r0 = env->tlb_table[mem_index][index].addend */
773
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
774
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
775

    
776
#else  /* !CONFIG_SOFTMMU */
777
#if TARGET_LONG_BITS == 32
778
    tcg_out_rld (s, RLDICL, addr_reg, addr_reg, 0, 32);
779
#endif
780
    r1 = 3;
781
    r0 = addr_reg;
782
#endif
783

    
784
#ifdef TARGET_WORDS_BIGENDIAN
785
    bswap = 0;
786
#else
787
    bswap = 1;
788
#endif
789
    switch (opc) {
790
    case 0:
791
        tcg_out32 (s, STB | RS (data_reg) | RA (r0));
792
        break;
793
    case 1:
794
        if (bswap) tcg_out32 (s, STHBRX | RS (data_reg) | RA (0) | RB (r0));
795
        else tcg_out32 (s, STH | RS (data_reg) | RA (r0));
796
        break;
797
    case 2:
798
        if (bswap) tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
799
        else tcg_out32 (s, STW | RS (data_reg) | RA (r0));
800
        break;
801
    case 3:
802
        if (bswap) {
803
            tcg_out32 (s, STWBRX | RS (data_reg) | RA (0) | RB (r0));
804
            tcg_out32 (s, ADDI | RT (r1) | RA (r0) | 4);
805
            tcg_out_rld (s, RLDICL, 0, data_reg, 32, 0);
806
            tcg_out32 (s, STWBRX | RS (0) | RA (0) | RB (r1));
807
        }
808
        else tcg_out32 (s, STD | RS (data_reg) | RA (r0));
809
        break;
810
    }
811

    
812
#ifdef CONFIG_SOFTMMU
813
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
814
#endif
815
}
816

    
817
void tcg_target_qemu_prologue (TCGContext *s)
818
{
819
    int i, frame_size;
820
    uint64_t addr;
821

    
822
    frame_size = 0
823
        + 8                     /* back chain */
824
        + 8                     /* CR */
825
        + 8                     /* LR */
826
        + 8                     /* compiler doubleword */
827
        + 8                     /* link editor doubleword */
828
        + 8                     /* TOC save area */
829
        + TCG_STATIC_CALL_ARGS_SIZE
830
        + ARRAY_SIZE (tcg_target_callee_save_regs) * 8
831
        ;
832
    frame_size = (frame_size + 15) & ~15;
833

    
834
    /* First emit adhoc function descriptor */
835
    addr = (uint64_t) s->code_ptr + 24;
836
    tcg_out32 (s, addr >> 32); tcg_out32 (s, addr); /* entry point */
837
    s->code_ptr += 16;          /* skip TOC and environment pointer */
838

    
839
    /* Prologue */
840
    tcg_out32 (s, MFSPR | RT (0) | LR);
841
    tcg_out32 (s, STDU | RS (1) | RA (1) | (-frame_size & 0xffff));
842
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
843
        tcg_out32 (s, (STD
844
                       | RS (tcg_target_callee_save_regs[i])
845
                       | RA (1)
846
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
847
                       )
848
            );
849
    tcg_out32 (s, STD | RS (0) | RA (1) | (frame_size + 16));
850

    
851
    tcg_out32 (s, MTSPR | RS (3) | CTR);
852
    tcg_out32 (s, BCCTR | BO_ALWAYS);
853

    
854
    /* Epilogue */
855
    tb_ret_addr = s->code_ptr;
856

    
857
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
858
        tcg_out32 (s, (LD
859
                       | RT (tcg_target_callee_save_regs[i])
860
                       | RA (1)
861
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
862
                       )
863
            );
864
    tcg_out32 (s, LD | RT (0) | RA (1) | (frame_size + 16));
865
    tcg_out32 (s, MTSPR | RS (0) | LR);
866
    tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
867
    tcg_out32 (s, BCLR | BO_ALWAYS);
868
}
869

    
870
static void tcg_out_ld (TCGContext *s, TCGType type, int ret, int arg1,
871
                        tcg_target_long arg2)
872
{
873
    if (type == TCG_TYPE_I32)
874
        tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
875
    else
876
        tcg_out_ldsta (s, ret, arg1, arg2, LD, LDX);
877
}
878

    
879
static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,
880
                        tcg_target_long arg2)
881
{
882
    if (type == TCG_TYPE_I32)
883
        tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
884
    else
885
        tcg_out_ldsta (s, arg, arg1, arg2, STD, STDX);
886
}
887

    
888
static void ppc_addi32 (TCGContext *s, int rt, int ra, tcg_target_long si)
889
{
890
    if (!si && rt == ra)
891
        return;
892

    
893
    if (si == (int16_t) si)
894
        tcg_out32 (s, ADDI | RT (rt) | RA (ra) | (si & 0xffff));
895
    else {
896
        uint16_t h = ((si >> 16) & 0xffff) + ((uint16_t) si >> 15);
897
        tcg_out32 (s, ADDIS | RT (rt) | RA (ra) | h);
898
        tcg_out32 (s, ADDI | RT (rt) | RA (rt) | (si & 0xffff));
899
    }
900
}
901

    
902
static void ppc_addi64 (TCGContext *s, int rt, int ra, tcg_target_long si)
903
{
904
    /* XXX: suboptimal */
905
    if (si == (int16_t) si
906
        || ((((uint64_t) si >> 31) == 0) && (si & 0x8000) == 0))
907
        ppc_addi32 (s, rt, ra, si);
908
    else {
909
        tcg_out_movi (s, TCG_TYPE_I64, 0, si);
910
        tcg_out32 (s, ADD | RT (rt) | RA (ra));
911
    }
912
}
913

    
914
static void tcg_out_addi (TCGContext *s, int reg, tcg_target_long val)
915
{
916
    ppc_addi64 (s, reg, reg, val);
917
}
918

    
919
static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
920
                         int const_arg2, int cr, int arch64)
921
{
922
    int imm;
923
    uint32_t op;
924

    
925
    switch (cond) {
926
    case TCG_COND_EQ:
927
    case TCG_COND_NE:
928
        if (const_arg2) {
929
            if ((int16_t) arg2 == arg2) {
930
                op = CMPI;
931
                imm = 1;
932
                break;
933
            }
934
            else if ((uint16_t) arg2 == arg2) {
935
                op = CMPLI;
936
                imm = 1;
937
                break;
938
            }
939
        }
940
        op = CMPL;
941
        imm = 0;
942
        break;
943

    
944
    case TCG_COND_LT:
945
    case TCG_COND_GE:
946
    case TCG_COND_LE:
947
    case TCG_COND_GT:
948
        if (const_arg2) {
949
            if ((int16_t) arg2 == arg2) {
950
                op = CMPI;
951
                imm = 1;
952
                break;
953
            }
954
        }
955
        op = CMP;
956
        imm = 0;
957
        break;
958

    
959
    case TCG_COND_LTU:
960
    case TCG_COND_GEU:
961
    case TCG_COND_LEU:
962
    case TCG_COND_GTU:
963
        if (const_arg2) {
964
            if ((uint16_t) arg2 == arg2) {
965
                op = CMPLI;
966
                imm = 1;
967
                break;
968
            }
969
        }
970
        op = CMPL;
971
        imm = 0;
972
        break;
973

    
974
    default:
975
        tcg_abort ();
976
    }
977
    op |= BF (cr) | (arch64 << 21);
978

    
979
    if (imm)
980
        tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
981
    else {
982
        if (const_arg2) {
983
            tcg_out_movi (s, TCG_TYPE_I64, 0, arg2);
984
            tcg_out32 (s, op | RA (arg1) | RB (0));
985
        }
986
        else
987
            tcg_out32 (s, op | RA (arg1) | RB (arg2));
988
    }
989

    
990
}
991

    
992
static void tcg_out_bc (TCGContext *s, int bc, int label_index)
993
{
994
    TCGLabel *l = &s->labels[label_index];
995

    
996
    if (l->has_value)
997
        tcg_out32 (s, bc | reloc_pc14_val (s->code_ptr, l->u.value));
998
    else {
999
        uint16_t val = *(uint16_t *) &s->code_ptr[2];
1000

    
1001
        /* Thanks to Andrzej Zaborowski */
1002
        tcg_out32 (s, bc | (val & 0xfffc));
1003
        tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL14, label_index, 0);
1004
    }
1005
}
1006

    
1007
static void tcg_out_brcond (TCGContext *s, int cond,
1008
                            TCGArg arg1, TCGArg arg2, int const_arg2,
1009
                            int label_index, int arch64)
1010
{
1011
    tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, arch64);
1012
    tcg_out_bc (s, tcg_to_bc[cond], label_index);
1013
}
1014

    
1015
void ppc_tb_set_jmp_target (unsigned long jmp_addr, unsigned long addr)
1016
{
1017
    TCGContext s;
1018
    unsigned long patch_size;
1019

    
1020
    s.code_ptr = (uint8_t *) jmp_addr;
1021
    tcg_out_b (&s, 0, addr);
1022
    patch_size = s.code_ptr - (uint8_t *) jmp_addr;
1023
    flush_icache_range (jmp_addr, jmp_addr + patch_size);
1024
}
1025

    
1026
static void tcg_out_op (TCGContext *s, int opc, const TCGArg *args,
1027
                        const int *const_args)
1028
{
1029
    int c;
1030

    
1031
    switch (opc) {
1032
    case INDEX_op_exit_tb:
1033
        tcg_out_movi (s, TCG_TYPE_I64, TCG_REG_R3, args[0]);
1034
        tcg_out_b (s, 0, (tcg_target_long) tb_ret_addr);
1035
        break;
1036
    case INDEX_op_goto_tb:
1037
        if (s->tb_jmp_offset) {
1038
            /* direct jump method */
1039

    
1040
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1041
            s->code_ptr += 28;
1042
        }
1043
        else {
1044
            tcg_abort ();
1045
        }
1046
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1047
        break;
1048
    case INDEX_op_br:
1049
        {
1050
            TCGLabel *l = &s->labels[args[0]];
1051

    
1052
            if (l->has_value) {
1053
                tcg_out_b (s, 0, l->u.value);
1054
            }
1055
            else {
1056
                uint32_t val = *(uint32_t *) s->code_ptr;
1057

    
1058
                /* Thanks to Andrzej Zaborowski */
1059
                tcg_out32 (s, B | (val & 0x3fffffc));
1060
                tcg_out_reloc (s, s->code_ptr - 4, R_PPC_REL24, args[0], 0);
1061
            }
1062
        }
1063
        break;
1064
    case INDEX_op_call:
1065
        tcg_out_call (s, args[0], const_args[0]);
1066
        break;
1067
    case INDEX_op_jmp:
1068
        if (const_args[0]) {
1069
            tcg_out_b (s, 0, args[0]);
1070
        }
1071
        else {
1072
            tcg_out32 (s, MTSPR | RS (args[0]) | CTR);
1073
            tcg_out32 (s, BCCTR | BO_ALWAYS);
1074
        }
1075
        break;
1076
    case INDEX_op_movi_i32:
1077
        tcg_out_movi (s, TCG_TYPE_I32, args[0], args[1]);
1078
        break;
1079
    case INDEX_op_movi_i64:
1080
        tcg_out_movi (s, TCG_TYPE_I64, args[0], args[1]);
1081
        break;
1082
    case INDEX_op_ld8u_i32:
1083
    case INDEX_op_ld8u_i64:
1084
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1085
        break;
1086
    case INDEX_op_ld8s_i32:
1087
    case INDEX_op_ld8s_i64:
1088
        tcg_out_ldst (s, args[0], args[1], args[2], LBZ, LBZX);
1089
        tcg_out32 (s, EXTSB | RS (args[0]) | RA (args[0]));
1090
        break;
1091
    case INDEX_op_ld16u_i32:
1092
    case INDEX_op_ld16u_i64:
1093
        tcg_out_ldst (s, args[0], args[1], args[2], LHZ, LHZX);
1094
        break;
1095
    case INDEX_op_ld16s_i32:
1096
    case INDEX_op_ld16s_i64:
1097
        tcg_out_ldst (s, args[0], args[1], args[2], LHA, LHAX);
1098
        break;
1099
    case INDEX_op_ld_i32:
1100
    case INDEX_op_ld32u_i64:
1101
        tcg_out_ldst (s, args[0], args[1], args[2], LWZ, LWZX);
1102
        break;
1103
    case INDEX_op_ld32s_i64:
1104
        tcg_out_ldsta (s, args[0], args[1], args[2], LWA, LWAX);
1105
        break;
1106
    case INDEX_op_ld_i64:
1107
        tcg_out_ldsta (s, args[0], args[1], args[2], LD, LDX);
1108
        break;
1109
    case INDEX_op_st8_i32:
1110
    case INDEX_op_st8_i64:
1111
        tcg_out_ldst (s, args[0], args[1], args[2], STB, STBX);
1112
        break;
1113
    case INDEX_op_st16_i32:
1114
    case INDEX_op_st16_i64:
1115
        tcg_out_ldst (s, args[0], args[1], args[2], STH, STHX);
1116
        break;
1117
    case INDEX_op_st_i32:
1118
    case INDEX_op_st32_i64:
1119
        tcg_out_ldst (s, args[0], args[1], args[2], STW, STWX);
1120
        break;
1121
    case INDEX_op_st_i64:
1122
        tcg_out_ldsta (s, args[0], args[1], args[2], STD, STDX);
1123
        break;
1124

    
1125
    case INDEX_op_add_i32:
1126
        if (const_args[2])
1127
            ppc_addi32 (s, args[0], args[1], args[2]);
1128
        else
1129
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1130
        break;
1131
    case INDEX_op_sub_i32:
1132
        if (const_args[2])
1133
            ppc_addi32 (s, args[0], args[1], -args[2]);
1134
        else
1135
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1136
        break;
1137

    
1138
    case INDEX_op_and_i64:
1139
    case INDEX_op_and_i32:
1140
        if (const_args[2]) {
1141
            if ((args[2] & 0xffff) == args[2])
1142
                tcg_out32 (s, ANDI | RS (args[1]) | RA (args[0]) | args[2]);
1143
            else if ((args[2] & 0xffff0000) == args[2])
1144
                tcg_out32 (s, ANDIS | RS (args[1]) | RA (args[0])
1145
                           | ((args[2] >> 16) & 0xffff));
1146
            else {
1147
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1148
                                  ? TCG_TYPE_I32
1149
                                  : TCG_TYPE_I64),
1150
                              0, args[2]);
1151
                tcg_out32 (s, AND | SAB (args[1], args[0], 0));
1152
            }
1153
        }
1154
        else
1155
            tcg_out32 (s, AND | SAB (args[1], args[0], args[2]));
1156
        break;
1157
    case INDEX_op_or_i64:
1158
    case INDEX_op_or_i32:
1159
        if (const_args[2]) {
1160
            if (args[2] & 0xffff) {
1161
                tcg_out32 (s, ORI | RS (args[1]) | RA (args[0])
1162
                           | (args[2] & 0xffff));
1163
                if (args[2] >> 16)
1164
                    tcg_out32 (s, ORIS | RS (args[0])  | RA (args[0])
1165
                               | ((args[2] >> 16) & 0xffff));
1166
            }
1167
            else {
1168
                tcg_out32 (s, ORIS | RS (args[1])  | RA (args[0])
1169
                           | ((args[2] >> 16) & 0xffff));
1170
            }
1171
        }
1172
        else
1173
            tcg_out32 (s, OR | SAB (args[1], args[0], args[2]));
1174
        break;
1175
    case INDEX_op_xor_i64:
1176
    case INDEX_op_xor_i32:
1177
        if (const_args[2]) {
1178
            if ((args[2] & 0xffff) == args[2])
1179
                tcg_out32 (s, XORI | RS (args[1])  | RA (args[0])
1180
                           | (args[2] & 0xffff));
1181
            else if ((args[2] & 0xffff0000) == args[2])
1182
                tcg_out32 (s, XORIS | RS (args[1])  | RA (args[0])
1183
                           | ((args[2] >> 16) & 0xffff));
1184
            else {
1185
                tcg_out_movi (s, (opc == INDEX_op_and_i32
1186
                                  ? TCG_TYPE_I32
1187
                                  : TCG_TYPE_I64),
1188
                              0, args[2]);
1189
                tcg_out32 (s, XOR | SAB (args[1], args[0], 0));
1190
            }
1191
        }
1192
        else
1193
            tcg_out32 (s, XOR | SAB (args[1], args[0], args[2]));
1194
        break;
1195

    
1196
    case INDEX_op_mul_i32:
1197
        if (const_args[2]) {
1198
            if (args[2] == (int16_t) args[2])
1199
                tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1200
                           | (args[2] & 0xffff));
1201
            else {
1202
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1203
                tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1204
            }
1205
        }
1206
        else
1207
            tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1208
        break;
1209

    
1210
    case INDEX_op_div_i32:
1211
        tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1212
        break;
1213

    
1214
    case INDEX_op_divu_i32:
1215
        tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1216
        break;
1217

    
1218
    case INDEX_op_rem_i32:
1219
        tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1220
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1221
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1222
        break;
1223

    
1224
    case INDEX_op_remu_i32:
1225
        tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1226
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1227
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1228
        break;
1229

    
1230
    case INDEX_op_shl_i32:
1231
        if (const_args[2]) {
1232
            tcg_out32 (s, (RLWINM
1233
                           | RA (args[0])
1234
                           | RS (args[1])
1235
                           | SH (args[2])
1236
                           | MB (0)
1237
                           | ME (31 - args[2])
1238
                           )
1239
                );
1240
        }
1241
        else
1242
            tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1243
        break;
1244
    case INDEX_op_shr_i32:
1245
        if (const_args[2]) {
1246
            tcg_out32 (s, (RLWINM
1247
                           | RA (args[0])
1248
                           | RS (args[1])
1249
                           | SH (32 - args[2])
1250
                           | MB (args[2])
1251
                           | ME (31)
1252
                           )
1253
                );
1254
        }
1255
        else
1256
            tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1257
        break;
1258
    case INDEX_op_sar_i32:
1259
        if (const_args[2])
1260
            tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1261
        else
1262
            tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1263
        break;
1264

    
1265
    case INDEX_op_brcond_i32:
1266
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 0);
1267
        break;
1268

    
1269
    case INDEX_op_brcond_i64:
1270
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 1);
1271
        break;
1272

    
1273
    case INDEX_op_neg_i32:
1274
    case INDEX_op_neg_i64:
1275
        tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1276
        break;
1277

    
1278
    case INDEX_op_add_i64:
1279
        if (const_args[2])
1280
            ppc_addi64 (s, args[0], args[1], args[2]);
1281
        else
1282
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1283
        break;
1284
    case INDEX_op_sub_i64:
1285
        if (const_args[2])
1286
            ppc_addi64 (s, args[0], args[1], -args[2]);
1287
        else
1288
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1289
        break;
1290

    
1291
    case INDEX_op_shl_i64:
1292
        if (const_args[2])
1293
            tcg_out_rld (s, RLDICR, args[0], args[1], args[2], 63 - args[2]);
1294
        else
1295
            tcg_out32 (s, SLD | SAB (args[1], args[0], args[2]));
1296
        break;
1297
    case INDEX_op_shr_i64:
1298
        if (const_args[2])
1299
            tcg_out_rld (s, RLDICL, args[0], args[1], 64 - args[2], args[2]);
1300
        else
1301
            tcg_out32 (s, SRD | SAB (args[1], args[0], args[2]));
1302
        break;
1303
    case INDEX_op_sar_i64:
1304
        if (const_args[2]) {
1305
            int sh = SH (args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1306
            tcg_out32 (s, SRADI | RA (args[0]) | RS (args[1]) | sh);
1307
        }
1308
        else
1309
            tcg_out32 (s, SRAD | SAB (args[1], args[0], args[2]));
1310
        break;
1311

    
1312
    case INDEX_op_mul_i64:
1313
        tcg_out32 (s, MULLD | TAB (args[0], args[1], args[2]));
1314
        break;
1315
    case INDEX_op_div_i64:
1316
        tcg_out32 (s, DIVD | TAB (args[0], args[1], args[2]));
1317
        break;
1318
    case INDEX_op_divu_i64:
1319
        tcg_out32 (s, DIVDU | TAB (args[0], args[1], args[2]));
1320
        break;
1321
    case INDEX_op_rem_i64:
1322
        tcg_out32 (s, DIVD | TAB (0, args[1], args[2]));
1323
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1324
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1325
        break;
1326
    case INDEX_op_remu_i64:
1327
        tcg_out32 (s, DIVDU | TAB (0, args[1], args[2]));
1328
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1329
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1330
        break;
1331

    
1332
    case INDEX_op_qemu_ld8u:
1333
        tcg_out_qemu_ld (s, args, 0);
1334
        break;
1335
    case INDEX_op_qemu_ld8s:
1336
        tcg_out_qemu_ld (s, args, 0 | 4);
1337
        break;
1338
    case INDEX_op_qemu_ld16u:
1339
        tcg_out_qemu_ld (s, args, 1);
1340
        break;
1341
    case INDEX_op_qemu_ld16s:
1342
        tcg_out_qemu_ld (s, args, 1 | 4);
1343
        break;
1344
    case INDEX_op_qemu_ld32u:
1345
        tcg_out_qemu_ld (s, args, 2);
1346
        break;
1347
    case INDEX_op_qemu_ld32s:
1348
        tcg_out_qemu_ld (s, args, 2 | 4);
1349
        break;
1350
    case INDEX_op_qemu_ld64:
1351
        tcg_out_qemu_ld (s, args, 3);
1352
        break;
1353
    case INDEX_op_qemu_st8:
1354
        tcg_out_qemu_st (s, args, 0);
1355
        break;
1356
    case INDEX_op_qemu_st16:
1357
        tcg_out_qemu_st (s, args, 1);
1358
        break;
1359
    case INDEX_op_qemu_st32:
1360
        tcg_out_qemu_st (s, args, 2);
1361
        break;
1362
    case INDEX_op_qemu_st64:
1363
        tcg_out_qemu_st (s, args, 3);
1364
        break;
1365

    
1366
    case INDEX_op_ext8s_i32:
1367
    case INDEX_op_ext8s_i64:
1368
        c = EXTSB;
1369
        goto gen_ext;
1370
    case INDEX_op_ext16s_i32:
1371
    case INDEX_op_ext16s_i64:
1372
        c = EXTSH;
1373
        goto gen_ext;
1374
    case INDEX_op_ext32s_i64:
1375
        c = EXTSW;
1376
        goto gen_ext;
1377
    gen_ext:
1378
        tcg_out32 (s, c | RS (args[1]) | RA (args[0]));
1379
        break;
1380

    
1381
    default:
1382
        tcg_dump_ops (s, stderr);
1383
        tcg_abort ();
1384
    }
1385
}
1386

    
1387
static const TCGTargetOpDef ppc_op_defs[] = {
1388
    { INDEX_op_exit_tb, { } },
1389
    { INDEX_op_goto_tb, { } },
1390
    { INDEX_op_call, { "ri" } },
1391
    { INDEX_op_jmp, { "ri" } },
1392
    { INDEX_op_br, { } },
1393

    
1394
    { INDEX_op_mov_i32, { "r", "r" } },
1395
    { INDEX_op_mov_i64, { "r", "r" } },
1396
    { INDEX_op_movi_i32, { "r" } },
1397
    { INDEX_op_movi_i64, { "r" } },
1398

    
1399
    { INDEX_op_ld8u_i32, { "r", "r" } },
1400
    { INDEX_op_ld8s_i32, { "r", "r" } },
1401
    { INDEX_op_ld16u_i32, { "r", "r" } },
1402
    { INDEX_op_ld16s_i32, { "r", "r" } },
1403
    { INDEX_op_ld_i32, { "r", "r" } },
1404
    { INDEX_op_ld_i64, { "r", "r" } },
1405
    { INDEX_op_st8_i32, { "r", "r" } },
1406
    { INDEX_op_st8_i64, { "r", "r" } },
1407
    { INDEX_op_st16_i32, { "r", "r" } },
1408
    { INDEX_op_st16_i64, { "r", "r" } },
1409
    { INDEX_op_st_i32, { "r", "r" } },
1410
    { INDEX_op_st_i64, { "r", "r" } },
1411
    { INDEX_op_st32_i64, { "r", "r" } },
1412

    
1413
    { INDEX_op_ld8u_i64, { "r", "r" } },
1414
    { INDEX_op_ld8s_i64, { "r", "r" } },
1415
    { INDEX_op_ld16u_i64, { "r", "r" } },
1416
    { INDEX_op_ld16s_i64, { "r", "r" } },
1417
    { INDEX_op_ld32u_i64, { "r", "r" } },
1418
    { INDEX_op_ld32s_i64, { "r", "r" } },
1419
    { INDEX_op_ld_i64, { "r", "r" } },
1420

    
1421
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1422
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1423
    { INDEX_op_div_i32, { "r", "r", "r" } },
1424
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1425
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1426
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1427
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1428
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1429
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1430
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1431

    
1432
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1433
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1434
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1435

    
1436
    { INDEX_op_brcond_i32, { "r", "ri" } },
1437
    { INDEX_op_brcond_i64, { "r", "ri" } },
1438

    
1439
    { INDEX_op_neg_i32, { "r", "r" } },
1440

    
1441
    { INDEX_op_add_i64, { "r", "r", "ri" } },
1442
    { INDEX_op_sub_i64, { "r", "r", "ri" } },
1443
    { INDEX_op_and_i64, { "r", "r", "rZ" } },
1444
    { INDEX_op_or_i64, { "r", "r", "rZ" } },
1445
    { INDEX_op_xor_i64, { "r", "r", "rZ" } },
1446

    
1447
    { INDEX_op_shl_i64, { "r", "r", "ri" } },
1448
    { INDEX_op_shr_i64, { "r", "r", "ri" } },
1449
    { INDEX_op_sar_i64, { "r", "r", "ri" } },
1450

    
1451
    { INDEX_op_mul_i64, { "r", "r", "r" } },
1452
    { INDEX_op_div_i64, { "r", "r", "r" } },
1453
    { INDEX_op_divu_i64, { "r", "r", "r" } },
1454
    { INDEX_op_rem_i64, { "r", "r", "r" } },
1455
    { INDEX_op_remu_i64, { "r", "r", "r" } },
1456

    
1457
    { INDEX_op_neg_i64, { "r", "r" } },
1458

    
1459
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1460
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1461
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1462
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1463
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1464
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1465
    { INDEX_op_qemu_ld64, { "r", "L" } },
1466

    
1467
    { INDEX_op_qemu_st8, { "S", "S" } },
1468
    { INDEX_op_qemu_st16, { "S", "S" } },
1469
    { INDEX_op_qemu_st32, { "S", "S" } },
1470
    { INDEX_op_qemu_st64, { "S", "S", "S" } },
1471

    
1472
    { INDEX_op_ext8s_i32, { "r", "r" } },
1473
    { INDEX_op_ext16s_i32, { "r", "r" } },
1474
    { INDEX_op_ext8s_i64, { "r", "r" } },
1475
    { INDEX_op_ext16s_i64, { "r", "r" } },
1476
    { INDEX_op_ext32s_i64, { "r", "r" } },
1477

    
1478
    { -1 },
1479
};
1480

    
1481
void tcg_target_init (TCGContext *s)
1482
{
1483
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1484
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1485
    tcg_regset_set32 (tcg_target_call_clobber_regs, 0,
1486
                     (1 << TCG_REG_R0) |
1487
                     (1 << TCG_REG_R3) |
1488
                     (1 << TCG_REG_R4) |
1489
                     (1 << TCG_REG_R5) |
1490
                     (1 << TCG_REG_R6) |
1491
                     (1 << TCG_REG_R7) |
1492
                     (1 << TCG_REG_R8) |
1493
                     (1 << TCG_REG_R9) |
1494
                     (1 << TCG_REG_R10) |
1495
                     (1 << TCG_REG_R11) |
1496
                     (1 << TCG_REG_R12)
1497
        );
1498

    
1499
    tcg_regset_clear (s->reserved_regs);
1500
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R0);
1501
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R1);
1502
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R2);
1503
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R13);
1504

    
1505
    tcg_add_target_add_op_defs (ppc_op_defs);
1506
}