Statistics
| Branch: | Revision:

root / tcg / ppc64 / tcg-target.c @ 0a9564b9

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
#if TARGET_LONG_BITS == 32
32
#define LD_ADDR LWZU
33
#define CMP_L 0
34
#else
35
#define LD_ADDR LDU
36
#define CMP_L (1<<21)
37
#endif
38

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

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

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

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

    
122
static const int tcg_target_call_iarg_regs[] = {
123
    TCG_REG_R3,
124
    TCG_REG_R4,
125
    TCG_REG_R5,
126
    TCG_REG_R6,
127
    TCG_REG_R7,
128
    TCG_REG_R8,
129
    TCG_REG_R9,
130
    TCG_REG_R10
131
};
132

    
133
static const int tcg_target_call_oarg_regs[] = {
134
    TCG_REG_R3
135
};
136

    
137
static const int tcg_target_callee_save_regs[] = {
138
#ifdef __APPLE__
139
    TCG_REG_R11,
140
#endif
141
    TCG_REG_R14,
142
    TCG_REG_R15,
143
    TCG_REG_R16,
144
    TCG_REG_R17,
145
    TCG_REG_R18,
146
    TCG_REG_R19,
147
    TCG_REG_R20,
148
    TCG_REG_R21,
149
    TCG_REG_R22,
150
    TCG_REG_R23,
151
    TCG_REG_R24,
152
    TCG_REG_R25,
153
    TCG_REG_R26,
154
    TCG_REG_R27, /* currently used for the global env */
155
    TCG_REG_R28,
156
    TCG_REG_R29,
157
    TCG_REG_R30,
158
    TCG_REG_R31
159
};
160

    
161
static uint32_t reloc_pc24_val (void *pc, tcg_target_long target)
162
{
163
    tcg_target_long disp;
164

    
165
    disp = target - (tcg_target_long) pc;
166
    if ((disp << 38) >> 38 != disp)
167
        tcg_abort ();
168

    
169
    return disp & 0x3fffffc;
170
}
171

    
172
static void reloc_pc24 (void *pc, tcg_target_long target)
173
{
174
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0x3fffffc)
175
        | reloc_pc24_val (pc, target);
176
}
177

    
178
static uint16_t reloc_pc14_val (void *pc, tcg_target_long target)
179
{
180
    tcg_target_long disp;
181

    
182
    disp = target - (tcg_target_long) pc;
183
    if (disp != (int16_t) disp)
184
        tcg_abort ();
185

    
186
    return disp & 0xfffc;
187
}
188

    
189
static void reloc_pc14 (void *pc, tcg_target_long target)
190
{
191
    *(uint32_t *) pc = (*(uint32_t *) pc & ~0xfffc)
192
        | reloc_pc14_val (pc, target);
193
}
194

    
195
static void patch_reloc (uint8_t *code_ptr, int type,
196
                         tcg_target_long value, tcg_target_long addend)
197
{
198
    value += addend;
199
    switch (type) {
200
    case R_PPC_REL14:
201
        reloc_pc14 (code_ptr, value);
202
        break;
203
    case R_PPC_REL24:
204
        reloc_pc24 (code_ptr, value);
205
        break;
206
    default:
207
        tcg_abort ();
208
    }
209
}
210

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

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

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

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

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

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

    
287
#define STD    XO62(  0)
288
#define STDU   XO62(  1)
289
#define STDX   XO31(149)
290

    
291
#define LD     XO58(  0)
292
#define LDX    XO31( 21)
293
#define LDU    XO58(  1)
294
#define LWA    XO58(  2)
295
#define LWAX   XO31(341)
296

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

    
310
#define LWZU   OPCD( 33)
311
#define STWU   OPCD( 37)
312

    
313
#define RLWINM OPCD( 21)
314

    
315
#define RLDICL XO30(  0)
316
#define RLDICR XO30(  1)
317
#define RLDIMI XO30(  3)
318

    
319
#define BCLR   XO19( 16)
320
#define BCCTR  XO19(528)
321
#define CRAND  XO19(257)
322
#define CRANDC XO19(129)
323
#define CRNAND XO19(225)
324
#define CROR   XO19(449)
325
#define CRNOR  XO19( 33)
326

    
327
#define EXTSB  XO31(954)
328
#define EXTSH  XO31(922)
329
#define EXTSW  XO31(986)
330
#define ADD    XO31(266)
331
#define ADDE   XO31(138)
332
#define ADDC   XO31( 10)
333
#define AND    XO31( 28)
334
#define SUBF   XO31( 40)
335
#define SUBFC  XO31(  8)
336
#define SUBFE  XO31(136)
337
#define OR     XO31(444)
338
#define XOR    XO31(316)
339
#define MULLW  XO31(235)
340
#define MULHWU XO31( 11)
341
#define DIVW   XO31(491)
342
#define DIVWU  XO31(459)
343
#define CMP    XO31(  0)
344
#define CMPL   XO31( 32)
345
#define LHBRX  XO31(790)
346
#define LWBRX  XO31(534)
347
#define STHBRX XO31(918)
348
#define STWBRX XO31(662)
349
#define MFSPR  XO31(339)
350
#define MTSPR  XO31(467)
351
#define SRAWI  XO31(824)
352
#define NEG    XO31(104)
353
#define MFCR   XO31( 19)
354
#define NOR    XO31(124)
355
#define CNTLZW XO31( 26)
356
#define CNTLZD XO31( 58)
357

    
358
#define MULLD  XO31(233)
359
#define MULHD  XO31( 73)
360
#define MULHDU XO31(  9)
361
#define DIVD   XO31(489)
362
#define DIVDU  XO31(457)
363

    
364
#define LBZX   XO31( 87)
365
#define LHZX   XO31(279)
366
#define LHAX   XO31(343)
367
#define LWZX   XO31( 23)
368
#define STBX   XO31(215)
369
#define STHX   XO31(407)
370
#define STWX   XO31(151)
371

    
372
#define SPR(a,b) ((((a)<<5)|(b))<<11)
373
#define LR     SPR(8, 0)
374
#define CTR    SPR(9, 0)
375

    
376
#define SLW    XO31( 24)
377
#define SRW    XO31(536)
378
#define SRAW   XO31(792)
379

    
380
#define SLD    XO31( 27)
381
#define SRD    XO31(539)
382
#define SRAD   XO31(794)
383
#define SRADI  XO31(413<<1)
384

    
385
#define TW     XO31( 4)
386
#define TRAP   (TW | TO (31))
387

    
388
#define RT(r) ((r)<<21)
389
#define RS(r) ((r)<<21)
390
#define RA(r) ((r)<<16)
391
#define RB(r) ((r)<<11)
392
#define TO(t) ((t)<<21)
393
#define SH(s) ((s)<<11)
394
#define MB(b) ((b)<<6)
395
#define ME(e) ((e)<<1)
396
#define BO(o) ((o)<<21)
397
#define MB64(b) ((b)<<5)
398

    
399
#define LK    1
400

    
401
#define TAB(t,a,b) (RT(t) | RA(a) | RB(b))
402
#define SAB(s,a,b) (RS(s) | RA(a) | RB(b))
403

    
404
#define BF(n)    ((n)<<23)
405
#define BI(n, c) (((c)+((n)*4))<<16)
406
#define BT(n, c) (((c)+((n)*4))<<21)
407
#define BA(n, c) (((c)+((n)*4))<<16)
408
#define BB(n, c) (((c)+((n)*4))<<11)
409

    
410
#define BO_COND_TRUE  BO (12)
411
#define BO_COND_FALSE BO ( 4)
412
#define BO_ALWAYS     BO (20)
413

    
414
enum {
415
    CR_LT,
416
    CR_GT,
417
    CR_EQ,
418
    CR_SO
419
};
420

    
421
static const uint32_t tcg_to_bc[] = {
422
    [TCG_COND_EQ]  = BC | BI (7, CR_EQ) | BO_COND_TRUE,
423
    [TCG_COND_NE]  = BC | BI (7, CR_EQ) | BO_COND_FALSE,
424
    [TCG_COND_LT]  = BC | BI (7, CR_LT) | BO_COND_TRUE,
425
    [TCG_COND_GE]  = BC | BI (7, CR_LT) | BO_COND_FALSE,
426
    [TCG_COND_LE]  = BC | BI (7, CR_GT) | BO_COND_FALSE,
427
    [TCG_COND_GT]  = BC | BI (7, CR_GT) | BO_COND_TRUE,
428
    [TCG_COND_LTU] = BC | BI (7, CR_LT) | BO_COND_TRUE,
429
    [TCG_COND_GEU] = BC | BI (7, CR_LT) | BO_COND_FALSE,
430
    [TCG_COND_LEU] = BC | BI (7, CR_GT) | BO_COND_FALSE,
431
    [TCG_COND_GTU] = BC | BI (7, CR_GT) | BO_COND_TRUE,
432
};
433

    
434
static inline void tcg_out_mov(TCGContext *s, TCGType type,
435
                               TCGReg ret, TCGReg arg)
436
{
437
    tcg_out32 (s, OR | SAB (arg, ret, arg));
438
}
439

    
440
static inline void tcg_out_rld(TCGContext *s, int op, TCGReg ra, TCGReg rs,
441
                               int sh, int mb)
442
{
443
    sh = SH (sh & 0x1f) | (((sh >> 5) & 1) << 1);
444
    mb = MB64 ((mb >> 5) | ((mb << 1) & 0x3f));
445
    tcg_out32 (s, op | RA (ra) | RS (rs) | sh | mb);
446
}
447

    
448
static inline void tcg_out_rlw(TCGContext *s, int op, TCGReg ra, TCGReg rs,
449
                               int sh, int mb, int me)
450
{
451
    tcg_out32(s, op | RA(ra) | RS(rs) | SH(sh) | MB(mb) | ME(me));
452
}
453

    
454
static inline void tcg_out_ext32u(TCGContext *s, TCGReg dst, TCGReg src)
455
{
456
    tcg_out_rld(s, RLDICL, dst, src, 0, 32);
457
}
458

    
459
static inline void tcg_out_shli64(TCGContext *s, TCGReg dst, TCGReg src, int c)
460
{
461
    tcg_out_rld(s, RLDICR, dst, src, c, 63 - c);
462
}
463

    
464
static void tcg_out_movi32(TCGContext *s, TCGReg ret, int32_t arg)
465
{
466
    if (arg == (int16_t) arg)
467
        tcg_out32 (s, ADDI | RT (ret) | RA (0) | (arg & 0xffff));
468
    else {
469
        tcg_out32 (s, ADDIS | RT (ret) | RA (0) | ((arg >> 16) & 0xffff));
470
        if (arg & 0xffff)
471
            tcg_out32 (s, ORI | RS (ret) | RA (ret) | (arg & 0xffff));
472
    }
473
}
474

    
475
static void tcg_out_movi (TCGContext *s, TCGType type,
476
                          TCGReg ret, tcg_target_long arg)
477
{
478
    int32_t arg32 = arg;
479
    arg = type == TCG_TYPE_I32 ? arg & 0xffffffff : arg;
480

    
481
    if (arg == arg32) {
482
        tcg_out_movi32 (s, ret, arg32);
483
    }
484
    else {
485
        if ((uint64_t) arg >> 32) {
486
            uint16_t h16 = arg >> 16;
487
            uint16_t l16 = arg;
488

    
489
            tcg_out_movi32 (s, ret, arg >> 32);
490
            tcg_out_shli64(s, ret, ret, 32);
491
            if (h16) tcg_out32 (s, ORIS | RS (ret) | RA (ret) | h16);
492
            if (l16) tcg_out32 (s, ORI | RS (ret) | RA (ret) | l16);
493
        }
494
        else {
495
            tcg_out_movi32 (s, ret, arg32);
496
            if (arg32 < 0)
497
                tcg_out_ext32u(s, ret, ret);
498
        }
499
    }
500
}
501

    
502
static void tcg_out_b (TCGContext *s, int mask, tcg_target_long target)
503
{
504
    tcg_target_long disp;
505

    
506
    disp = target - (tcg_target_long) s->code_ptr;
507
    if ((disp << 38) >> 38 == disp)
508
        tcg_out32 (s, B | (disp & 0x3fffffc) | mask);
509
    else {
510
        tcg_out_movi (s, TCG_TYPE_I64, 0, (tcg_target_long) target);
511
        tcg_out32 (s, MTSPR | RS (0) | CTR);
512
        tcg_out32 (s, BCCTR | BO_ALWAYS | mask);
513
    }
514
}
515

    
516
static void tcg_out_call (TCGContext *s, tcg_target_long arg, int const_arg)
517
{
518
#ifdef __APPLE__
519
    if (const_arg) {
520
        tcg_out_b (s, LK, arg);
521
    }
522
    else {
523
        tcg_out32 (s, MTSPR | RS (arg) | LR);
524
        tcg_out32 (s, BCLR | BO_ALWAYS | LK);
525
    }
526
#else
527
    int reg;
528

    
529
    if (const_arg) {
530
        reg = 2;
531
        tcg_out_movi (s, TCG_TYPE_I64, reg, arg);
532
    }
533
    else reg = arg;
534

    
535
    tcg_out32 (s, LD | RT (0) | RA (reg));
536
    tcg_out32 (s, MTSPR | RA (0) | CTR);
537
    tcg_out32 (s, LD | RT (11) | RA (reg) | 16);
538
    tcg_out32 (s, LD | RT (2) | RA (reg) | 8);
539
    tcg_out32 (s, BCCTR | BO_ALWAYS | LK);
540
#endif
541
}
542

    
543
static void tcg_out_ldst(TCGContext *s, TCGReg ret, TCGReg addr,
544
                         int offset, int op1, int op2)
545
{
546
    if (offset == (int16_t) offset)
547
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
548
    else {
549
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
550
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
551
    }
552
}
553

    
554
static void tcg_out_ldsta(TCGContext *s, TCGReg ret, TCGReg addr,
555
                          int offset, int op1, int op2)
556
{
557
    if (offset == (int16_t) (offset & ~3))
558
        tcg_out32 (s, op1 | RT (ret) | RA (addr) | (offset & 0xffff));
559
    else {
560
        tcg_out_movi (s, TCG_TYPE_I64, 0, offset);
561
        tcg_out32 (s, op2 | RT (ret) | RA (addr) | RB (0));
562
    }
563
}
564

    
565
#if defined (CONFIG_SOFTMMU)
566

    
567
#include "exec/softmmu_defs.h"
568

    
569
/* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
570
   int mmu_idx) */
571
static const void * const qemu_ld_helpers[4] = {
572
    helper_ldb_mmu,
573
    helper_ldw_mmu,
574
    helper_ldl_mmu,
575
    helper_ldq_mmu,
576
};
577

    
578
/* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
579
   uintxx_t val, int mmu_idx) */
580
static const void * const qemu_st_helpers[4] = {
581
    helper_stb_mmu,
582
    helper_stw_mmu,
583
    helper_stl_mmu,
584
    helper_stq_mmu,
585
};
586

    
587
static void tcg_out_tlb_read(TCGContext *s, TCGReg r0, TCGReg r1, TCGReg r2,
588
                             TCGReg addr_reg, int s_bits, int offset)
589
{
590
#if TARGET_LONG_BITS == 32
591
    tcg_out_ext32u(s, addr_reg, addr_reg);
592

    
593
    tcg_out_rlw(s, RLWINM, r0, addr_reg,
594
                32 - (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
595
                32 - (CPU_TLB_BITS + CPU_TLB_ENTRY_BITS),
596
                31 - CPU_TLB_ENTRY_BITS);
597
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (TCG_AREG0));
598
    tcg_out32 (s, (LWZU | RT (r1) | RA (r0) | offset));
599
    tcg_out_rlw(s, RLWINM, r2, addr_reg, 0,
600
                (32 - s_bits) & 31, 31 - TARGET_PAGE_BITS);
601
#else
602
    tcg_out_rld (s, RLDICL, r0, addr_reg,
603
                 64 - TARGET_PAGE_BITS,
604
                 64 - CPU_TLB_BITS);
605
    tcg_out_shli64(s, r0, r0, CPU_TLB_ENTRY_BITS);
606

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

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

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

    
633
    data_reg = *args++;
634
    addr_reg = *args++;
635

    
636
#ifdef CONFIG_SOFTMMU
637
    mem_index = *args;
638
    s_bits = opc & 3;
639

    
640
    r0 = 3;
641
    r1 = 4;
642
    r2 = 0;
643
    rbase = 0;
644

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

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

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

    
655
    /* slow path */
656
    ir = 3;
657
    tcg_out_mov (s, TCG_TYPE_I64, ir++, TCG_AREG0);
658
    tcg_out_mov (s, TCG_TYPE_I64, ir++, addr_reg);
659
    tcg_out_movi (s, TCG_TYPE_I64, ir++, mem_index);
660

    
661
    tcg_out_call (s, (tcg_target_long) qemu_ld_helpers[s_bits], 1);
662

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

    
684
    /* label1: fast path */
685
#ifdef FAST_PATH
686
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
687
#endif
688

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

    
700
#else  /* !CONFIG_SOFTMMU */
701
#if TARGET_LONG_BITS == 32
702
    tcg_out_ext32u(s, addr_reg, addr_reg);
703
#endif
704
    r0 = addr_reg;
705
    r1 = 3;
706
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
707
#endif
708

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

    
770
#ifdef CONFIG_SOFTMMU
771
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
772
#endif
773
}
774

    
775
static void tcg_out_qemu_st (TCGContext *s, const TCGArg *args, int opc)
776
{
777
    TCGReg addr_reg, r0, r1, rbase, data_reg;
778
    int bswap;
779
#ifdef CONFIG_SOFTMMU
780
    TCGReg r2, ir;
781
    int mem_index;
782
    void *label1_ptr, *label2_ptr;
783
#endif
784

    
785
    data_reg = *args++;
786
    addr_reg = *args++;
787

    
788
#ifdef CONFIG_SOFTMMU
789
    mem_index = *args;
790

    
791
    r0 = 3;
792
    r1 = 4;
793
    r2 = 0;
794
    rbase = 0;
795

    
796
    tcg_out_tlb_read (s, r0, r1, r2, addr_reg, opc,
797
                      offsetof (CPUArchState, tlb_table[mem_index][0].addr_write));
798

    
799
    tcg_out32 (s, CMP | BF (7) | RA (r2) | RB (r1) | CMP_L);
800

    
801
    label1_ptr = s->code_ptr;
802
#ifdef FAST_PATH
803
    tcg_out32 (s, BC | BI (7, CR_EQ) | BO_COND_TRUE);
804
#endif
805

    
806
    /* slow path */
807
    ir = 3;
808
    tcg_out_mov (s, TCG_TYPE_I64, ir++, TCG_AREG0);
809
    tcg_out_mov (s, TCG_TYPE_I64, ir++, addr_reg);
810
    tcg_out_rld (s, RLDICL, ir++, data_reg, 0, 64 - (1 << (3 + opc)));
811
    tcg_out_movi (s, TCG_TYPE_I64, ir++, mem_index);
812

    
813
    tcg_out_call (s, (tcg_target_long) qemu_st_helpers[opc], 1);
814

    
815
    label2_ptr = s->code_ptr;
816
    tcg_out32 (s, B);
817

    
818
    /* label1: fast path */
819
#ifdef FAST_PATH
820
    reloc_pc14 (label1_ptr, (tcg_target_long) s->code_ptr);
821
#endif
822

    
823
    tcg_out32 (s, (LD
824
                   | RT (r0)
825
                   | RA (r0)
826
                   | (offsetof (CPUTLBEntry, addend)
827
                      - offsetof (CPUTLBEntry, addr_write))
828
                   ));
829
    /* r0 = env->tlb_table[mem_index][index].addend */
830
    tcg_out32 (s, ADD | RT (r0) | RA (r0) | RB (addr_reg));
831
    /* r0 = env->tlb_table[mem_index][index].addend + addr */
832

    
833
#else  /* !CONFIG_SOFTMMU */
834
#if TARGET_LONG_BITS == 32
835
    tcg_out_ext32u(s, addr_reg, addr_reg);
836
#endif
837
    r1 = 3;
838
    r0 = addr_reg;
839
    rbase = GUEST_BASE ? TCG_GUEST_BASE_REG : 0;
840
#endif
841

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

    
874
#ifdef CONFIG_SOFTMMU
875
    reloc_pc24 (label2_ptr, (tcg_target_long) s->code_ptr);
876
#endif
877
}
878

    
879
static void tcg_target_qemu_prologue (TCGContext *s)
880
{
881
    int i, frame_size;
882
#ifndef __APPLE__
883
    uint64_t addr;
884
#endif
885

    
886
    frame_size = 0
887
        + 8                     /* back chain */
888
        + 8                     /* CR */
889
        + 8                     /* LR */
890
        + 8                     /* compiler doubleword */
891
        + 8                     /* link editor doubleword */
892
        + 8                     /* TOC save area */
893
        + TCG_STATIC_CALL_ARGS_SIZE
894
        + ARRAY_SIZE (tcg_target_callee_save_regs) * 8
895
        + CPU_TEMP_BUF_NLONGS * sizeof(long)
896
        ;
897
    frame_size = (frame_size + 15) & ~15;
898

    
899
    tcg_set_frame (s, TCG_REG_CALL_STACK, frame_size
900
                   - CPU_TEMP_BUF_NLONGS * sizeof (long),
901
                   CPU_TEMP_BUF_NLONGS * sizeof (long));
902

    
903
#ifndef __APPLE__
904
    /* First emit adhoc function descriptor */
905
    addr = (uint64_t) s->code_ptr + 24;
906
    tcg_out32 (s, addr >> 32); tcg_out32 (s, addr); /* entry point */
907
    s->code_ptr += 16;          /* skip TOC and environment pointer */
908
#endif
909

    
910
    /* Prologue */
911
    tcg_out32 (s, MFSPR | RT (0) | LR);
912
    tcg_out32 (s, STDU | RS (1) | RA (1) | (-frame_size & 0xffff));
913
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
914
        tcg_out32 (s, (STD
915
                       | RS (tcg_target_callee_save_regs[i])
916
                       | RA (1)
917
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
918
                       )
919
            );
920
    tcg_out32 (s, STD | RS (0) | RA (1) | (frame_size + 16));
921

    
922
#ifdef CONFIG_USE_GUEST_BASE
923
    if (GUEST_BASE) {
924
        tcg_out_movi (s, TCG_TYPE_I64, TCG_GUEST_BASE_REG, GUEST_BASE);
925
        tcg_regset_set_reg (s->reserved_regs, TCG_GUEST_BASE_REG);
926
    }
927
#endif
928

    
929
    tcg_out_mov (s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
930
    tcg_out32 (s, MTSPR | RS (tcg_target_call_iarg_regs[1]) | CTR);
931
    tcg_out32 (s, BCCTR | BO_ALWAYS);
932

    
933
    /* Epilogue */
934
    tb_ret_addr = s->code_ptr;
935

    
936
    for (i = 0; i < ARRAY_SIZE (tcg_target_callee_save_regs); ++i)
937
        tcg_out32 (s, (LD
938
                       | RT (tcg_target_callee_save_regs[i])
939
                       | RA (1)
940
                       | (i * 8 + 48 + TCG_STATIC_CALL_ARGS_SIZE)
941
                       )
942
            );
943
    tcg_out32 (s, LD | RT (0) | RA (1) | (frame_size + 16));
944
    tcg_out32 (s, MTSPR | RS (0) | LR);
945
    tcg_out32 (s, ADDI | RT (1) | RA (1) | frame_size);
946
    tcg_out32 (s, BCLR | BO_ALWAYS);
947
}
948

    
949
static void tcg_out_ld (TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
950
                        tcg_target_long arg2)
951
{
952
    if (type == TCG_TYPE_I32)
953
        tcg_out_ldst (s, ret, arg1, arg2, LWZ, LWZX);
954
    else
955
        tcg_out_ldsta (s, ret, arg1, arg2, LD, LDX);
956
}
957

    
958
static void tcg_out_st (TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
959
                        tcg_target_long arg2)
960
{
961
    if (type == TCG_TYPE_I32)
962
        tcg_out_ldst (s, arg, arg1, arg2, STW, STWX);
963
    else
964
        tcg_out_ldsta (s, arg, arg1, arg2, STD, STDX);
965
}
966

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

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

    
981
static void ppc_addi64(TCGContext *s, TCGReg rt, TCGReg ra, tcg_target_long si)
982
{
983
    /* XXX: suboptimal */
984
    if (si == (int16_t) si
985
        || ((((uint64_t) si >> 31) == 0) && (si & 0x8000) == 0))
986
        ppc_addi32 (s, rt, ra, si);
987
    else {
988
        tcg_out_movi (s, TCG_TYPE_I64, 0, si);
989
        tcg_out32 (s, ADD | RT (rt) | RA (ra));
990
    }
991
}
992

    
993
static void tcg_out_cmp (TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
994
                         int const_arg2, int cr, int arch64)
995
{
996
    int imm;
997
    uint32_t op;
998

    
999
    switch (cond) {
1000
    case TCG_COND_EQ:
1001
    case TCG_COND_NE:
1002
        if (const_arg2) {
1003
            if ((int16_t) arg2 == arg2) {
1004
                op = CMPI;
1005
                imm = 1;
1006
                break;
1007
            }
1008
            else if ((uint16_t) arg2 == arg2) {
1009
                op = CMPLI;
1010
                imm = 1;
1011
                break;
1012
            }
1013
        }
1014
        op = CMPL;
1015
        imm = 0;
1016
        break;
1017

    
1018
    case TCG_COND_LT:
1019
    case TCG_COND_GE:
1020
    case TCG_COND_LE:
1021
    case TCG_COND_GT:
1022
        if (const_arg2) {
1023
            if ((int16_t) arg2 == arg2) {
1024
                op = CMPI;
1025
                imm = 1;
1026
                break;
1027
            }
1028
        }
1029
        op = CMP;
1030
        imm = 0;
1031
        break;
1032

    
1033
    case TCG_COND_LTU:
1034
    case TCG_COND_GEU:
1035
    case TCG_COND_LEU:
1036
    case TCG_COND_GTU:
1037
        if (const_arg2) {
1038
            if ((uint16_t) arg2 == arg2) {
1039
                op = CMPLI;
1040
                imm = 1;
1041
                break;
1042
            }
1043
        }
1044
        op = CMPL;
1045
        imm = 0;
1046
        break;
1047

    
1048
    default:
1049
        tcg_abort ();
1050
    }
1051
    op |= BF (cr) | (arch64 << 21);
1052

    
1053
    if (imm)
1054
        tcg_out32 (s, op | RA (arg1) | (arg2 & 0xffff));
1055
    else {
1056
        if (const_arg2) {
1057
            tcg_out_movi (s, TCG_TYPE_I64, 0, arg2);
1058
            tcg_out32 (s, op | RA (arg1) | RB (0));
1059
        }
1060
        else
1061
            tcg_out32 (s, op | RA (arg1) | RB (arg2));
1062
    }
1063

    
1064
}
1065

    
1066
static void tcg_out_setcond (TCGContext *s, TCGType type, TCGCond cond,
1067
                             TCGArg arg0, TCGArg arg1, TCGArg arg2,
1068
                             int const_arg2)
1069
{
1070
    int crop, sh, arg;
1071

    
1072
    switch (cond) {
1073
    case TCG_COND_EQ:
1074
        if (const_arg2) {
1075
            if (!arg2) {
1076
                arg = arg1;
1077
            }
1078
            else {
1079
                arg = 0;
1080
                if ((uint16_t) arg2 == arg2) {
1081
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1082
                }
1083
                else {
1084
                    tcg_out_movi (s, type, 0, arg2);
1085
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1086
                }
1087
            }
1088
        }
1089
        else {
1090
            arg = 0;
1091
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1092
        }
1093

    
1094
        if (type == TCG_TYPE_I64) {
1095
            tcg_out32 (s, CNTLZD | RS (arg) | RA (0));
1096
            tcg_out_rld (s, RLDICL, arg0, 0, 58, 6);
1097
        }
1098
        else {
1099
            tcg_out32 (s, CNTLZW | RS (arg) | RA (0));
1100
            tcg_out_rlw(s, RLWINM, arg0, 0, 27, 5, 31);
1101
        }
1102
        break;
1103

    
1104
    case TCG_COND_NE:
1105
        if (const_arg2) {
1106
            if (!arg2) {
1107
                arg = arg1;
1108
            }
1109
            else {
1110
                arg = 0;
1111
                if ((uint16_t) arg2 == arg2) {
1112
                    tcg_out32 (s, XORI | RS (arg1) | RA (0) | arg2);
1113
                }
1114
                else {
1115
                    tcg_out_movi (s, type, 0, arg2);
1116
                    tcg_out32 (s, XOR | SAB (arg1, 0, 0));
1117
                }
1118
            }
1119
        }
1120
        else {
1121
            arg = 0;
1122
            tcg_out32 (s, XOR | SAB (arg1, 0, arg2));
1123
        }
1124

    
1125
        if (arg == arg1 && arg1 == arg0) {
1126
            tcg_out32 (s, ADDIC | RT (0) | RA (arg) | 0xffff);
1127
            tcg_out32 (s, SUBFE | TAB (arg0, 0, arg));
1128
        }
1129
        else {
1130
            tcg_out32 (s, ADDIC | RT (arg0) | RA (arg) | 0xffff);
1131
            tcg_out32 (s, SUBFE | TAB (arg0, arg0, arg));
1132
        }
1133
        break;
1134

    
1135
    case TCG_COND_GT:
1136
    case TCG_COND_GTU:
1137
        sh = 30;
1138
        crop = 0;
1139
        goto crtest;
1140

    
1141
    case TCG_COND_LT:
1142
    case TCG_COND_LTU:
1143
        sh = 29;
1144
        crop = 0;
1145
        goto crtest;
1146

    
1147
    case TCG_COND_GE:
1148
    case TCG_COND_GEU:
1149
        sh = 31;
1150
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_LT) | BB (7, CR_LT);
1151
        goto crtest;
1152

    
1153
    case TCG_COND_LE:
1154
    case TCG_COND_LEU:
1155
        sh = 31;
1156
        crop = CRNOR | BT (7, CR_EQ) | BA (7, CR_GT) | BB (7, CR_GT);
1157
    crtest:
1158
        tcg_out_cmp (s, cond, arg1, arg2, const_arg2, 7, type == TCG_TYPE_I64);
1159
        if (crop) tcg_out32 (s, crop);
1160
        tcg_out32 (s, MFCR | RT (0));
1161
        tcg_out_rlw(s, RLWINM, arg0, 0, sh, 31, 31);
1162
        break;
1163

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

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

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

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

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

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

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

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

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

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

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

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

    
1293
    case INDEX_op_add_i32:
1294
        if (const_args[2])
1295
            ppc_addi32 (s, args[0], args[1], args[2]);
1296
        else
1297
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1298
        break;
1299
    case INDEX_op_sub_i32:
1300
        if (const_args[2])
1301
            ppc_addi32 (s, args[0], args[1], -args[2]);
1302
        else
1303
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1304
        break;
1305

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

    
1364
    case INDEX_op_mul_i32:
1365
        if (const_args[2]) {
1366
            if (args[2] == (int16_t) args[2])
1367
                tcg_out32 (s, MULLI | RT (args[0]) | RA (args[1])
1368
                           | (args[2] & 0xffff));
1369
            else {
1370
                tcg_out_movi (s, TCG_TYPE_I32, 0, args[2]);
1371
                tcg_out32 (s, MULLW | TAB (args[0], args[1], 0));
1372
            }
1373
        }
1374
        else
1375
            tcg_out32 (s, MULLW | TAB (args[0], args[1], args[2]));
1376
        break;
1377

    
1378
    case INDEX_op_div_i32:
1379
        tcg_out32 (s, DIVW | TAB (args[0], args[1], args[2]));
1380
        break;
1381

    
1382
    case INDEX_op_divu_i32:
1383
        tcg_out32 (s, DIVWU | TAB (args[0], args[1], args[2]));
1384
        break;
1385

    
1386
    case INDEX_op_rem_i32:
1387
        tcg_out32 (s, DIVW | TAB (0, args[1], args[2]));
1388
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1389
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1390
        break;
1391

    
1392
    case INDEX_op_remu_i32:
1393
        tcg_out32 (s, DIVWU | TAB (0, args[1], args[2]));
1394
        tcg_out32 (s, MULLW | TAB (0, 0, args[2]));
1395
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1396
        break;
1397

    
1398
    case INDEX_op_shl_i32:
1399
        if (const_args[2]) {
1400
            tcg_out_rlw(s, RLWINM, args[0], args[1], args[2], 0, 31 - args[2]);
1401
        } else {
1402
            tcg_out32 (s, SLW | SAB (args[1], args[0], args[2]));
1403
        }
1404
        break;
1405
    case INDEX_op_shr_i32:
1406
        if (const_args[2]) {
1407
            tcg_out_rlw(s, RLWINM, args[0], args[1], 32 - args[2], args[2], 31);
1408
        } else {
1409
            tcg_out32 (s, SRW | SAB (args[1], args[0], args[2]));
1410
        }
1411
        break;
1412
    case INDEX_op_sar_i32:
1413
        if (const_args[2])
1414
            tcg_out32 (s, SRAWI | RS (args[1]) | RA (args[0]) | SH (args[2]));
1415
        else
1416
            tcg_out32 (s, SRAW | SAB (args[1], args[0], args[2]));
1417
        break;
1418

    
1419
    case INDEX_op_brcond_i32:
1420
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 0);
1421
        break;
1422

    
1423
    case INDEX_op_brcond_i64:
1424
        tcg_out_brcond (s, args[2], args[0], args[1], const_args[1], args[3], 1);
1425
        break;
1426

    
1427
    case INDEX_op_neg_i32:
1428
    case INDEX_op_neg_i64:
1429
        tcg_out32 (s, NEG | RT (args[0]) | RA (args[1]));
1430
        break;
1431

    
1432
    case INDEX_op_not_i32:
1433
    case INDEX_op_not_i64:
1434
        tcg_out32 (s, NOR | SAB (args[1], args[0], args[1]));
1435
        break;
1436

    
1437
    case INDEX_op_add_i64:
1438
        if (const_args[2])
1439
            ppc_addi64 (s, args[0], args[1], args[2]);
1440
        else
1441
            tcg_out32 (s, ADD | TAB (args[0], args[1], args[2]));
1442
        break;
1443
    case INDEX_op_sub_i64:
1444
        if (const_args[2])
1445
            ppc_addi64 (s, args[0], args[1], -args[2]);
1446
        else
1447
            tcg_out32 (s, SUBF | TAB (args[0], args[2], args[1]));
1448
        break;
1449

    
1450
    case INDEX_op_shl_i64:
1451
        if (const_args[2])
1452
            tcg_out_shli64(s, args[0], args[1], args[2]);
1453
        else
1454
            tcg_out32 (s, SLD | SAB (args[1], args[0], args[2]));
1455
        break;
1456
    case INDEX_op_shr_i64:
1457
        if (const_args[2])
1458
            tcg_out_rld (s, RLDICL, args[0], args[1], 64 - args[2], args[2]);
1459
        else
1460
            tcg_out32 (s, SRD | SAB (args[1], args[0], args[2]));
1461
        break;
1462
    case INDEX_op_sar_i64:
1463
        if (const_args[2]) {
1464
            int sh = SH (args[2] & 0x1f) | (((args[2] >> 5) & 1) << 1);
1465
            tcg_out32 (s, SRADI | RA (args[0]) | RS (args[1]) | sh);
1466
        }
1467
        else
1468
            tcg_out32 (s, SRAD | SAB (args[1], args[0], args[2]));
1469
        break;
1470

    
1471
    case INDEX_op_mul_i64:
1472
        tcg_out32 (s, MULLD | TAB (args[0], args[1], args[2]));
1473
        break;
1474
    case INDEX_op_div_i64:
1475
        tcg_out32 (s, DIVD | TAB (args[0], args[1], args[2]));
1476
        break;
1477
    case INDEX_op_divu_i64:
1478
        tcg_out32 (s, DIVDU | TAB (args[0], args[1], args[2]));
1479
        break;
1480
    case INDEX_op_rem_i64:
1481
        tcg_out32 (s, DIVD | TAB (0, args[1], args[2]));
1482
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1483
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1484
        break;
1485
    case INDEX_op_remu_i64:
1486
        tcg_out32 (s, DIVDU | TAB (0, args[1], args[2]));
1487
        tcg_out32 (s, MULLD | TAB (0, 0, args[2]));
1488
        tcg_out32 (s, SUBF | TAB (args[0], 0, args[1]));
1489
        break;
1490

    
1491
    case INDEX_op_qemu_ld8u:
1492
        tcg_out_qemu_ld (s, args, 0);
1493
        break;
1494
    case INDEX_op_qemu_ld8s:
1495
        tcg_out_qemu_ld (s, args, 0 | 4);
1496
        break;
1497
    case INDEX_op_qemu_ld16u:
1498
        tcg_out_qemu_ld (s, args, 1);
1499
        break;
1500
    case INDEX_op_qemu_ld16s:
1501
        tcg_out_qemu_ld (s, args, 1 | 4);
1502
        break;
1503
    case INDEX_op_qemu_ld32:
1504
    case INDEX_op_qemu_ld32u:
1505
        tcg_out_qemu_ld (s, args, 2);
1506
        break;
1507
    case INDEX_op_qemu_ld32s:
1508
        tcg_out_qemu_ld (s, args, 2 | 4);
1509
        break;
1510
    case INDEX_op_qemu_ld64:
1511
        tcg_out_qemu_ld (s, args, 3);
1512
        break;
1513
    case INDEX_op_qemu_st8:
1514
        tcg_out_qemu_st (s, args, 0);
1515
        break;
1516
    case INDEX_op_qemu_st16:
1517
        tcg_out_qemu_st (s, args, 1);
1518
        break;
1519
    case INDEX_op_qemu_st32:
1520
        tcg_out_qemu_st (s, args, 2);
1521
        break;
1522
    case INDEX_op_qemu_st64:
1523
        tcg_out_qemu_st (s, args, 3);
1524
        break;
1525

    
1526
    case INDEX_op_ext8s_i32:
1527
    case INDEX_op_ext8s_i64:
1528
        c = EXTSB;
1529
        goto gen_ext;
1530
    case INDEX_op_ext16s_i32:
1531
    case INDEX_op_ext16s_i64:
1532
        c = EXTSH;
1533
        goto gen_ext;
1534
    case INDEX_op_ext32s_i64:
1535
        c = EXTSW;
1536
        goto gen_ext;
1537
    gen_ext:
1538
        tcg_out32 (s, c | RS (args[1]) | RA (args[0]));
1539
        break;
1540

    
1541
    case INDEX_op_ext32u_i64:
1542
        tcg_out_ext32u(s, args[0], args[1]);
1543
        break;
1544

    
1545
    case INDEX_op_setcond_i32:
1546
        tcg_out_setcond (s, TCG_TYPE_I32, args[3], args[0], args[1], args[2],
1547
                         const_args[2]);
1548
        break;
1549
    case INDEX_op_setcond_i64:
1550
        tcg_out_setcond (s, TCG_TYPE_I64, args[3], args[0], args[1], args[2],
1551
                         const_args[2]);
1552
        break;
1553

    
1554
    default:
1555
        tcg_dump_ops (s);
1556
        tcg_abort ();
1557
    }
1558
}
1559

    
1560
static const TCGTargetOpDef ppc_op_defs[] = {
1561
    { INDEX_op_exit_tb, { } },
1562
    { INDEX_op_goto_tb, { } },
1563
    { INDEX_op_call, { "ri" } },
1564
    { INDEX_op_br, { } },
1565

    
1566
    { INDEX_op_mov_i32, { "r", "r" } },
1567
    { INDEX_op_mov_i64, { "r", "r" } },
1568
    { INDEX_op_movi_i32, { "r" } },
1569
    { INDEX_op_movi_i64, { "r" } },
1570

    
1571
    { INDEX_op_ld8u_i32, { "r", "r" } },
1572
    { INDEX_op_ld8s_i32, { "r", "r" } },
1573
    { INDEX_op_ld16u_i32, { "r", "r" } },
1574
    { INDEX_op_ld16s_i32, { "r", "r" } },
1575
    { INDEX_op_ld_i32, { "r", "r" } },
1576
    { INDEX_op_ld_i64, { "r", "r" } },
1577
    { INDEX_op_st8_i32, { "r", "r" } },
1578
    { INDEX_op_st8_i64, { "r", "r" } },
1579
    { INDEX_op_st16_i32, { "r", "r" } },
1580
    { INDEX_op_st16_i64, { "r", "r" } },
1581
    { INDEX_op_st_i32, { "r", "r" } },
1582
    { INDEX_op_st_i64, { "r", "r" } },
1583
    { INDEX_op_st32_i64, { "r", "r" } },
1584

    
1585
    { INDEX_op_ld8u_i64, { "r", "r" } },
1586
    { INDEX_op_ld8s_i64, { "r", "r" } },
1587
    { INDEX_op_ld16u_i64, { "r", "r" } },
1588
    { INDEX_op_ld16s_i64, { "r", "r" } },
1589
    { INDEX_op_ld32u_i64, { "r", "r" } },
1590
    { INDEX_op_ld32s_i64, { "r", "r" } },
1591

    
1592
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1593
    { INDEX_op_mul_i32, { "r", "r", "ri" } },
1594
    { INDEX_op_div_i32, { "r", "r", "r" } },
1595
    { INDEX_op_divu_i32, { "r", "r", "r" } },
1596
    { INDEX_op_rem_i32, { "r", "r", "r" } },
1597
    { INDEX_op_remu_i32, { "r", "r", "r" } },
1598
    { INDEX_op_sub_i32, { "r", "r", "ri" } },
1599
    { INDEX_op_and_i32, { "r", "r", "ri" } },
1600
    { INDEX_op_or_i32, { "r", "r", "ri" } },
1601
    { INDEX_op_xor_i32, { "r", "r", "ri" } },
1602

    
1603
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1604
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1605
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1606

    
1607
    { INDEX_op_brcond_i32, { "r", "ri" } },
1608
    { INDEX_op_brcond_i64, { "r", "ri" } },
1609

    
1610
    { INDEX_op_neg_i32, { "r", "r" } },
1611
    { INDEX_op_not_i32, { "r", "r" } },
1612

    
1613
    { INDEX_op_add_i64, { "r", "r", "ri" } },
1614
    { INDEX_op_sub_i64, { "r", "r", "ri" } },
1615
    { INDEX_op_and_i64, { "r", "r", "rZ" } },
1616
    { INDEX_op_or_i64, { "r", "r", "rZ" } },
1617
    { INDEX_op_xor_i64, { "r", "r", "rZ" } },
1618

    
1619
    { INDEX_op_shl_i64, { "r", "r", "ri" } },
1620
    { INDEX_op_shr_i64, { "r", "r", "ri" } },
1621
    { INDEX_op_sar_i64, { "r", "r", "ri" } },
1622

    
1623
    { INDEX_op_mul_i64, { "r", "r", "r" } },
1624
    { INDEX_op_div_i64, { "r", "r", "r" } },
1625
    { INDEX_op_divu_i64, { "r", "r", "r" } },
1626
    { INDEX_op_rem_i64, { "r", "r", "r" } },
1627
    { INDEX_op_remu_i64, { "r", "r", "r" } },
1628

    
1629
    { INDEX_op_neg_i64, { "r", "r" } },
1630
    { INDEX_op_not_i64, { "r", "r" } },
1631

    
1632
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1633
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1634
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1635
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1636
    { INDEX_op_qemu_ld32, { "r", "L" } },
1637
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1638
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1639
    { INDEX_op_qemu_ld64, { "r", "L" } },
1640

    
1641
    { INDEX_op_qemu_st8, { "S", "S" } },
1642
    { INDEX_op_qemu_st16, { "S", "S" } },
1643
    { INDEX_op_qemu_st32, { "S", "S" } },
1644
    { INDEX_op_qemu_st64, { "S", "S" } },
1645

    
1646
    { INDEX_op_ext8s_i32, { "r", "r" } },
1647
    { INDEX_op_ext16s_i32, { "r", "r" } },
1648
    { INDEX_op_ext8s_i64, { "r", "r" } },
1649
    { INDEX_op_ext16s_i64, { "r", "r" } },
1650
    { INDEX_op_ext32s_i64, { "r", "r" } },
1651
    { INDEX_op_ext32u_i64, { "r", "r" } },
1652

    
1653
    { INDEX_op_setcond_i32, { "r", "r", "ri" } },
1654
    { INDEX_op_setcond_i64, { "r", "r", "ri" } },
1655

    
1656
    { -1 },
1657
};
1658

    
1659
static void tcg_target_init (TCGContext *s)
1660
{
1661
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1662
    tcg_regset_set32 (tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1663
    tcg_regset_set32 (tcg_target_call_clobber_regs, 0,
1664
                     (1 << TCG_REG_R0) |
1665
#ifdef __APPLE__
1666
                     (1 << TCG_REG_R2) |
1667
#endif
1668
                     (1 << TCG_REG_R3) |
1669
                     (1 << TCG_REG_R4) |
1670
                     (1 << TCG_REG_R5) |
1671
                     (1 << TCG_REG_R6) |
1672
                     (1 << TCG_REG_R7) |
1673
                     (1 << TCG_REG_R8) |
1674
                     (1 << TCG_REG_R9) |
1675
                     (1 << TCG_REG_R10) |
1676
                     (1 << TCG_REG_R11) |
1677
                     (1 << TCG_REG_R12)
1678
        );
1679

    
1680
    tcg_regset_clear (s->reserved_regs);
1681
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R0);
1682
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R1);
1683
#ifndef __APPLE__
1684
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R2);
1685
#endif
1686
    tcg_regset_set_reg (s->reserved_regs, TCG_REG_R13);
1687

    
1688
    tcg_add_target_add_op_defs (ppc_op_defs);
1689
}