Statistics
| Branch: | Revision:

root / tcg / sparc / tcg-target.c @ 7a3766f3

History | View | Annotate | Download (41.5 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
#ifndef NDEBUG
26
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
27
    "%g0",
28
    "%g1",
29
    "%g2",
30
    "%g3",
31
    "%g4",
32
    "%g5",
33
    "%g6",
34
    "%g7",
35
    "%o0",
36
    "%o1",
37
    "%o2",
38
    "%o3",
39
    "%o4",
40
    "%o5",
41
    "%o6",
42
    "%o7",
43
    "%l0",
44
    "%l1",
45
    "%l2",
46
    "%l3",
47
    "%l4",
48
    "%l5",
49
    "%l6",
50
    "%l7",
51
    "%i0",
52
    "%i1",
53
    "%i2",
54
    "%i3",
55
    "%i4",
56
    "%i5",
57
    "%i6",
58
    "%i7",
59
};
60
#endif
61

    
62
static const int tcg_target_reg_alloc_order[] = {
63
    TCG_REG_L0,
64
    TCG_REG_L1,
65
    TCG_REG_L2,
66
    TCG_REG_L3,
67
    TCG_REG_L4,
68
    TCG_REG_L5,
69
    TCG_REG_L6,
70
    TCG_REG_L7,
71
    TCG_REG_I0,
72
    TCG_REG_I1,
73
    TCG_REG_I2,
74
    TCG_REG_I3,
75
    TCG_REG_I4,
76
};
77

    
78
static const int tcg_target_call_iarg_regs[6] = {
79
    TCG_REG_O0,
80
    TCG_REG_O1,
81
    TCG_REG_O2,
82
    TCG_REG_O3,
83
    TCG_REG_O4,
84
    TCG_REG_O5,
85
};
86

    
87
static const int tcg_target_call_oarg_regs[2] = {
88
    TCG_REG_O0,
89
    TCG_REG_O1,
90
};
91

    
92
static inline int check_fit_tl(tcg_target_long val, unsigned int bits)
93
{
94
    return (val << ((sizeof(tcg_target_long) * 8 - bits))
95
            >> (sizeof(tcg_target_long) * 8 - bits)) == val;
96
}
97

    
98
static inline int check_fit_i32(uint32_t val, unsigned int bits)
99
{
100
    return ((val << (32 - bits)) >> (32 - bits)) == val;
101
}
102

    
103
static void patch_reloc(uint8_t *code_ptr, int type,
104
                        tcg_target_long value, tcg_target_long addend)
105
{
106
    value += addend;
107
    switch (type) {
108
    case R_SPARC_32:
109
        if (value != (uint32_t)value)
110
            tcg_abort();
111
        *(uint32_t *)code_ptr = value;
112
        break;
113
    case R_SPARC_WDISP22:
114
        value -= (long)code_ptr;
115
        value >>= 2;
116
        if (!check_fit_tl(value, 22))
117
            tcg_abort();
118
        *(uint32_t *)code_ptr = ((*(uint32_t *)code_ptr) & ~0x3fffff) | value;
119
        break;
120
    case R_SPARC_WDISP19:
121
        value -= (long)code_ptr;
122
        value >>= 2;
123
        if (!check_fit_tl(value, 19))
124
            tcg_abort();
125
        *(uint32_t *)code_ptr = ((*(uint32_t *)code_ptr) & ~0x7ffff) | value;
126
        break;
127
    default:
128
        tcg_abort();
129
    }
130
}
131

    
132
/* maximum number of register used for input function arguments */
133
static inline int tcg_target_get_call_iarg_regs_count(int flags)
134
{
135
    return 6;
136
}
137

    
138
/* parse target specific constraints */
139
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
140
{
141
    const char *ct_str;
142

    
143
    ct_str = *pct_str;
144
    switch (ct_str[0]) {
145
    case 'r':
146
    case 'L': /* qemu_ld/st constraint */
147
        ct->ct |= TCG_CT_REG;
148
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
149
        // Helper args
150
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_O0);
151
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_O1);
152
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_O2);
153
        break;
154
    case 'I':
155
        ct->ct |= TCG_CT_CONST_S11;
156
        break;
157
    case 'J':
158
        ct->ct |= TCG_CT_CONST_S13;
159
        break;
160
    default:
161
        return -1;
162
    }
163
    ct_str++;
164
    *pct_str = ct_str;
165
    return 0;
166
}
167

    
168
/* test if a constant matches the constraint */
169
static inline int tcg_target_const_match(tcg_target_long val,
170
                                         const TCGArgConstraint *arg_ct)
171
{
172
    int ct;
173

    
174
    ct = arg_ct->ct;
175
    if (ct & TCG_CT_CONST)
176
        return 1;
177
    else if ((ct & TCG_CT_CONST_S11) && check_fit_tl(val, 11))
178
        return 1;
179
    else if ((ct & TCG_CT_CONST_S13) && check_fit_tl(val, 13))
180
        return 1;
181
    else
182
        return 0;
183
}
184

    
185
#define INSN_OP(x)  ((x) << 30)
186
#define INSN_OP2(x) ((x) << 22)
187
#define INSN_OP3(x) ((x) << 19)
188
#define INSN_OPF(x) ((x) << 5)
189
#define INSN_RD(x)  ((x) << 25)
190
#define INSN_RS1(x) ((x) << 14)
191
#define INSN_RS2(x) (x)
192
#define INSN_ASI(x) ((x) << 5)
193

    
194
#define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
195
#define INSN_OFF19(x) (((x) >> 2) & 0x07ffff)
196
#define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
197

    
198
#define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
199
#define COND_N     0x0
200
#define COND_E     0x1
201
#define COND_LE    0x2
202
#define COND_L     0x3
203
#define COND_LEU   0x4
204
#define COND_CS    0x5
205
#define COND_NEG   0x6
206
#define COND_VS    0x7
207
#define COND_A     0x8
208
#define COND_NE    0x9
209
#define COND_G     0xa
210
#define COND_GE    0xb
211
#define COND_GU    0xc
212
#define COND_CC    0xd
213
#define COND_POS   0xe
214
#define COND_VC    0xf
215
#define BA         (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
216

    
217
#define ARITH_ADD  (INSN_OP(2) | INSN_OP3(0x00))
218
#define ARITH_ADDCC (INSN_OP(2) | INSN_OP3(0x10))
219
#define ARITH_AND  (INSN_OP(2) | INSN_OP3(0x01))
220
#define ARITH_OR   (INSN_OP(2) | INSN_OP3(0x02))
221
#define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
222
#define ARITH_XOR  (INSN_OP(2) | INSN_OP3(0x03))
223
#define ARITH_SUB  (INSN_OP(2) | INSN_OP3(0x04))
224
#define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
225
#define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
226
#define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
227
#define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
228
#define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
229
#define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
230
#define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
231
#define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
232
#define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
233

    
234
#define SHIFT_SLL  (INSN_OP(2) | INSN_OP3(0x25))
235
#define SHIFT_SRL  (INSN_OP(2) | INSN_OP3(0x26))
236
#define SHIFT_SRA  (INSN_OP(2) | INSN_OP3(0x27))
237

    
238
#define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
239
#define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
240
#define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
241

    
242
#define RDY        (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(0))
243
#define WRY        (INSN_OP(2) | INSN_OP3(0x30))
244
#define JMPL       (INSN_OP(2) | INSN_OP3(0x38))
245
#define SAVE       (INSN_OP(2) | INSN_OP3(0x3c))
246
#define RESTORE    (INSN_OP(2) | INSN_OP3(0x3d))
247
#define SETHI      (INSN_OP(0) | INSN_OP2(0x4))
248
#define CALL       INSN_OP(1)
249
#define LDUB       (INSN_OP(3) | INSN_OP3(0x01))
250
#define LDSB       (INSN_OP(3) | INSN_OP3(0x09))
251
#define LDUH       (INSN_OP(3) | INSN_OP3(0x02))
252
#define LDSH       (INSN_OP(3) | INSN_OP3(0x0a))
253
#define LDUW       (INSN_OP(3) | INSN_OP3(0x00))
254
#define LDSW       (INSN_OP(3) | INSN_OP3(0x08))
255
#define LDX        (INSN_OP(3) | INSN_OP3(0x0b))
256
#define STB        (INSN_OP(3) | INSN_OP3(0x05))
257
#define STH        (INSN_OP(3) | INSN_OP3(0x06))
258
#define STW        (INSN_OP(3) | INSN_OP3(0x04))
259
#define STX        (INSN_OP(3) | INSN_OP3(0x0e))
260
#define LDUBA      (INSN_OP(3) | INSN_OP3(0x11))
261
#define LDSBA      (INSN_OP(3) | INSN_OP3(0x19))
262
#define LDUHA      (INSN_OP(3) | INSN_OP3(0x12))
263
#define LDSHA      (INSN_OP(3) | INSN_OP3(0x1a))
264
#define LDUWA      (INSN_OP(3) | INSN_OP3(0x10))
265
#define LDSWA      (INSN_OP(3) | INSN_OP3(0x18))
266
#define LDXA       (INSN_OP(3) | INSN_OP3(0x1b))
267
#define STBA       (INSN_OP(3) | INSN_OP3(0x15))
268
#define STHA       (INSN_OP(3) | INSN_OP3(0x16))
269
#define STWA       (INSN_OP(3) | INSN_OP3(0x14))
270
#define STXA       (INSN_OP(3) | INSN_OP3(0x1e))
271

    
272
#ifndef ASI_PRIMARY_LITTLE
273
#define ASI_PRIMARY_LITTLE 0x88
274
#endif
275

    
276
static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
277
                                 int op)
278
{
279
    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
280
              INSN_RS2(rs2));
281
}
282

    
283
static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1,
284
                                  uint32_t offset, int op)
285
{
286
    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
287
              INSN_IMM13(offset));
288
}
289

    
290
static void tcg_out_arithc(TCGContext *s, int rd, int rs1,
291
                           int val2, int val2const, int op)
292
{
293
    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1)
294
              | (val2const ? INSN_IMM13(val2) : INSN_RS2(val2)));
295
}
296

    
297
static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
298
{
299
    tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
300
}
301

    
302
static inline void tcg_out_sethi(TCGContext *s, int ret, uint32_t arg)
303
{
304
    tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
305
}
306

    
307
static inline void tcg_out_movi_imm13(TCGContext *s, int ret, uint32_t arg)
308
{
309
    tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
310
}
311

    
312
static inline void tcg_out_movi_imm32(TCGContext *s, int ret, uint32_t arg)
313
{
314
    if (check_fit_tl(arg, 13))
315
        tcg_out_movi_imm13(s, ret, arg);
316
    else {
317
        tcg_out_sethi(s, ret, arg);
318
        if (arg & 0x3ff)
319
            tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
320
    }
321
}
322

    
323
static inline void tcg_out_movi(TCGContext *s, TCGType type,
324
                                int ret, tcg_target_long arg)
325
{
326
    /* All 32-bit constants, as well as 64-bit constants with
327
       no high bits set go through movi_imm32.  */
328
    if (TCG_TARGET_REG_BITS == 32
329
        || type == TCG_TYPE_I32
330
        || (arg & ~(tcg_target_long)0xffffffff) == 0) {
331
        tcg_out_movi_imm32(s, ret, arg);
332
    } else if (check_fit_tl(arg, 13)) {
333
        /* A 13-bit constant sign-extended to 64-bits.  */
334
        tcg_out_movi_imm13(s, ret, arg);
335
    } else if (check_fit_tl(arg, 32)) {
336
        /* A 32-bit constant sign-extended to 64-bits.  */
337
        tcg_out_sethi(s, ret, ~arg);
338
        tcg_out_arithi(s, ret, ret, (arg & 0x3ff) | -0x400, ARITH_XOR);
339
    } else {
340
        tcg_out_movi_imm32(s, TCG_REG_I4, arg >> (TCG_TARGET_REG_BITS / 2));
341
        tcg_out_arithi(s, TCG_REG_I4, TCG_REG_I4, 32, SHIFT_SLLX);
342
        tcg_out_movi_imm32(s, ret, arg);
343
        tcg_out_arith(s, ret, ret, TCG_REG_I4, ARITH_OR);
344
    }
345
}
346

    
347
static inline void tcg_out_ld_raw(TCGContext *s, int ret,
348
                                  tcg_target_long arg)
349
{
350
    tcg_out_sethi(s, ret, arg);
351
    tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
352
              INSN_IMM13(arg & 0x3ff));
353
}
354

    
355
static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
356
                                  tcg_target_long arg)
357
{
358
    if (!check_fit_tl(arg, 10))
359
        tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ffULL);
360
    if (TCG_TARGET_REG_BITS == 64) {
361
        tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
362
                  INSN_IMM13(arg & 0x3ff));
363
    } else {
364
        tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
365
                  INSN_IMM13(arg & 0x3ff));
366
    }
367
}
368

    
369
static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
370
{
371
    if (check_fit_tl(offset, 13))
372
        tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
373
                  INSN_IMM13(offset));
374
    else {
375
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
376
        tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
377
                  INSN_RS2(addr));
378
    }
379
}
380

    
381
static inline void tcg_out_ldst_asi(TCGContext *s, int ret, int addr,
382
                                    int offset, int op, int asi)
383
{
384
    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
385
    tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
386
              INSN_ASI(asi) | INSN_RS2(addr));
387
}
388

    
389
static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
390
                              int arg1, tcg_target_long arg2)
391
{
392
    if (type == TCG_TYPE_I32)
393
        tcg_out_ldst(s, ret, arg1, arg2, LDUW);
394
    else
395
        tcg_out_ldst(s, ret, arg1, arg2, LDX);
396
}
397

    
398
static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
399
                              int arg1, tcg_target_long arg2)
400
{
401
    if (type == TCG_TYPE_I32)
402
        tcg_out_ldst(s, arg, arg1, arg2, STW);
403
    else
404
        tcg_out_ldst(s, arg, arg1, arg2, STX);
405
}
406

    
407
static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
408
{
409
    if (val == 0 || val == -1)
410
        tcg_out32(s, WRY | INSN_IMM13(val));
411
    else
412
        fprintf(stderr, "unimplemented sety %ld\n", (long)val);
413
}
414

    
415
static inline void tcg_out_rdy(TCGContext *s, int rd)
416
{
417
    tcg_out32(s, RDY | INSN_RD(rd));
418
}
419

    
420
static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
421
{
422
    if (val != 0) {
423
        if (check_fit_tl(val, 13))
424
            tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
425
        else {
426
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
427
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
428
        }
429
    }
430
}
431

    
432
static inline void tcg_out_andi(TCGContext *s, int reg, tcg_target_long val)
433
{
434
    if (val != 0) {
435
        if (check_fit_tl(val, 13))
436
            tcg_out_arithi(s, reg, reg, val, ARITH_AND);
437
        else {
438
            tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, val);
439
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_AND);
440
        }
441
    }
442
}
443

    
444
static inline void tcg_out_nop(TCGContext *s)
445
{
446
    tcg_out_sethi(s, TCG_REG_G0, 0);
447
}
448

    
449
static void tcg_out_branch_i32(TCGContext *s, int opc, int label_index)
450
{
451
    int32_t val;
452
    TCGLabel *l = &s->labels[label_index];
453

    
454
    if (l->has_value) {
455
        val = l->u.value - (tcg_target_long)s->code_ptr;
456
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
457
                      | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
458
    } else {
459
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
460
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
461
    }
462
}
463

    
464
#if TCG_TARGET_REG_BITS == 64
465
static void tcg_out_branch_i64(TCGContext *s, int opc, int label_index)
466
{
467
    int32_t val;
468
    TCGLabel *l = &s->labels[label_index];
469

    
470
    if (l->has_value) {
471
        val = l->u.value - (tcg_target_long)s->code_ptr;
472
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
473
                      (0x5 << 19) |
474
                      INSN_OFF19(l->u.value - (unsigned long)s->code_ptr)));
475
    } else {
476
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP19, label_index, 0);
477
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
478
                      (0x5 << 19) | 0));
479
    }
480
}
481
#endif
482

    
483
static const uint8_t tcg_cond_to_bcond[10] = {
484
    [TCG_COND_EQ] = COND_E,
485
    [TCG_COND_NE] = COND_NE,
486
    [TCG_COND_LT] = COND_L,
487
    [TCG_COND_GE] = COND_GE,
488
    [TCG_COND_LE] = COND_LE,
489
    [TCG_COND_GT] = COND_G,
490
    [TCG_COND_LTU] = COND_CS,
491
    [TCG_COND_GEU] = COND_CC,
492
    [TCG_COND_LEU] = COND_LEU,
493
    [TCG_COND_GTU] = COND_GU,
494
};
495

    
496
static void tcg_out_cmp(TCGContext *s, TCGArg c1, TCGArg c2, int c2const)
497
{
498
    tcg_out_arithc(s, TCG_REG_G0, c1, c2, c2const, ARITH_SUBCC);
499
}
500

    
501
static void tcg_out_brcond_i32(TCGContext *s, int cond,
502
                               TCGArg arg1, TCGArg arg2, int const_arg2,
503
                               int label_index)
504
{
505
    tcg_out_cmp(s, arg1, arg2, const_arg2);
506
    tcg_out_branch_i32(s, tcg_cond_to_bcond[cond], label_index);
507
    tcg_out_nop(s);
508
}
509

    
510
#if TCG_TARGET_REG_BITS == 64
511
static void tcg_out_brcond_i64(TCGContext *s, int cond,
512
                               TCGArg arg1, TCGArg arg2, int const_arg2,
513
                               int label_index)
514
{
515
    tcg_out_cmp(s, arg1, arg2, const_arg2);
516
    tcg_out_branch_i64(s, tcg_cond_to_bcond[cond], label_index);
517
    tcg_out_nop(s);
518
}
519
#else
520
static void tcg_out_brcond2_i32(TCGContext *s, int cond,
521
                                TCGArg al, TCGArg ah,
522
                                TCGArg bl, int blconst,
523
                                TCGArg bh, int bhconst, int label_dest)
524
{
525
    int cc, label_next = gen_new_label();
526

    
527
    tcg_out_cmp(s, ah, bh, bhconst);
528

    
529
    /* Note that we fill one of the delay slots with the second compare.  */
530
    switch (cond) {
531
    case TCG_COND_EQ:
532
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_NE], 0);
533
        tcg_out_branch_i32(s, cc, label_next);
534
        tcg_out_cmp(s, al, bl, blconst);
535
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_EQ], 0);
536
        tcg_out_branch_i32(s, cc, label_dest);
537
        break;
538

    
539
    case TCG_COND_NE:
540
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_NE], 0);
541
        tcg_out_branch_i32(s, cc, label_dest);
542
        tcg_out_cmp(s, al, bl, blconst);
543
        tcg_out_branch_i32(s, cc, label_dest);
544
        break;
545

    
546
    default:
547
        /* ??? One could fairly easily special-case 64-bit unsigned
548
           compares against 32-bit zero-extended constants.  For instance,
549
           we know that (unsigned)AH < 0 is false and need not emit it.
550
           Similarly, (unsigned)AH > 0 being true implies AH != 0, so the
551
           second branch will never be taken.  */
552
        cc = INSN_COND(tcg_cond_to_bcond[cond], 0);
553
        tcg_out_branch_i32(s, cc, label_dest);
554
        tcg_out_nop(s);
555
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_NE], 0);
556
        tcg_out_branch_i32(s, cc, label_next);
557
        tcg_out_cmp(s, al, bl, blconst);
558
        cc = INSN_COND(tcg_cond_to_bcond[tcg_unsigned_cond(cond)], 0);
559
        tcg_out_branch_i32(s, cc, label_dest);
560
        break;
561
    }
562
    tcg_out_nop(s);
563

    
564
    tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
565
}
566
#endif
567

    
568
/* Generate global QEMU prologue and epilogue code */
569
void tcg_target_qemu_prologue(TCGContext *s)
570
{
571
    tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
572
              INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
573
    tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
574
              INSN_RS2(TCG_REG_G0));
575
    tcg_out_nop(s);
576
}
577

    
578
#if defined(CONFIG_SOFTMMU)
579

    
580
#include "../../softmmu_defs.h"
581

    
582
static const void * const qemu_ld_helpers[4] = {
583
    __ldb_mmu,
584
    __ldw_mmu,
585
    __ldl_mmu,
586
    __ldq_mmu,
587
};
588

    
589
static const void * const qemu_st_helpers[4] = {
590
    __stb_mmu,
591
    __stw_mmu,
592
    __stl_mmu,
593
    __stq_mmu,
594
};
595
#endif
596

    
597
#if TARGET_LONG_BITS == 32
598
#define TARGET_LD_OP LDUW
599
#else
600
#define TARGET_LD_OP LDX
601
#endif
602

    
603
#if TARGET_PHYS_ADDR_BITS == 32
604
#define TARGET_ADDEND_LD_OP LDUW
605
#else
606
#define TARGET_ADDEND_LD_OP LDX
607
#endif
608

    
609
#ifdef __arch64__
610
#define HOST_LD_OP LDX
611
#define HOST_ST_OP STX
612
#define HOST_SLL_OP SHIFT_SLLX
613
#define HOST_SRA_OP SHIFT_SRAX
614
#else
615
#define HOST_LD_OP LDUW
616
#define HOST_ST_OP STW
617
#define HOST_SLL_OP SHIFT_SLL
618
#define HOST_SRA_OP SHIFT_SRA
619
#endif
620

    
621
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
622
                            int opc)
623
{
624
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
625
#if defined(CONFIG_SOFTMMU)
626
    uint32_t *label1_ptr, *label2_ptr;
627
#endif
628

    
629
    data_reg = *args++;
630
    addr_reg = *args++;
631
    mem_index = *args;
632
    s_bits = opc & 3;
633

    
634
    arg0 = TCG_REG_O0;
635
    arg1 = TCG_REG_O1;
636
    arg2 = TCG_REG_O2;
637

    
638
#if defined(CONFIG_SOFTMMU)
639
    /* srl addr_reg, x, arg1 */
640
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
641
                   SHIFT_SRL);
642
    /* and addr_reg, x, arg0 */
643
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
644
                   ARITH_AND);
645

    
646
    /* and arg1, x, arg1 */
647
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
648

    
649
    /* add arg1, x, arg1 */
650
    tcg_out_addi(s, arg1, offsetof(CPUState,
651
                                   tlb_table[mem_index][0].addr_read));
652

    
653
    /* add env, arg1, arg1 */
654
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
655

    
656
    /* ld [arg1], arg2 */
657
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
658
              INSN_RS2(TCG_REG_G0));
659

    
660
    /* subcc arg0, arg2, %g0 */
661
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
662

    
663
    /* will become:
664
       be label1
665
        or
666
       be,pt %xcc label1 */
667
    label1_ptr = (uint32_t *)s->code_ptr;
668
    tcg_out32(s, 0);
669

    
670
    /* mov (delay slot) */
671
    tcg_out_mov(s, arg0, addr_reg);
672

    
673
    /* mov */
674
    tcg_out_movi(s, TCG_TYPE_I32, arg1, mem_index);
675

    
676
    /* XXX: move that code at the end of the TB */
677
    /* qemu_ld_helper[s_bits](arg0, arg1) */
678
    tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
679
                           - (tcg_target_ulong)s->code_ptr) >> 2)
680
                         & 0x3fffffff));
681
    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
682
       global registers */
683
    // delay slot
684
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
685
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
686
                 sizeof(long), HOST_ST_OP);
687
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
688
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
689
                 sizeof(long), HOST_LD_OP);
690

    
691
    /* data_reg = sign_extend(arg0) */
692
    switch(opc) {
693
    case 0 | 4:
694
        /* sll arg0, 24/56, data_reg */
695
        tcg_out_arithi(s, data_reg, arg0, (int)sizeof(tcg_target_long) * 8 - 8,
696
                       HOST_SLL_OP);
697
        /* sra data_reg, 24/56, data_reg */
698
        tcg_out_arithi(s, data_reg, data_reg,
699
                       (int)sizeof(tcg_target_long) * 8 - 8, HOST_SRA_OP);
700
        break;
701
    case 1 | 4:
702
        /* sll arg0, 16/48, data_reg */
703
        tcg_out_arithi(s, data_reg, arg0,
704
                       (int)sizeof(tcg_target_long) * 8 - 16, HOST_SLL_OP);
705
        /* sra data_reg, 16/48, data_reg */
706
        tcg_out_arithi(s, data_reg, data_reg,
707
                       (int)sizeof(tcg_target_long) * 8 - 16, HOST_SRA_OP);
708
        break;
709
    case 2 | 4:
710
        /* sll arg0, 32, data_reg */
711
        tcg_out_arithi(s, data_reg, arg0, 32, HOST_SLL_OP);
712
        /* sra data_reg, 32, data_reg */
713
        tcg_out_arithi(s, data_reg, data_reg, 32, HOST_SRA_OP);
714
        break;
715
    case 0:
716
    case 1:
717
    case 2:
718
    case 3:
719
    default:
720
        /* mov */
721
        tcg_out_mov(s, data_reg, arg0);
722
        break;
723
    }
724

    
725
    /* will become:
726
       ba label2 */
727
    label2_ptr = (uint32_t *)s->code_ptr;
728
    tcg_out32(s, 0);
729

    
730
    /* nop (delay slot */
731
    tcg_out_nop(s);
732

    
733
    /* label1: */
734
#if TARGET_LONG_BITS == 32
735
    /* be label1 */
736
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
737
                   INSN_OFF22((unsigned long)s->code_ptr -
738
                              (unsigned long)label1_ptr));
739
#else
740
    /* be,pt %xcc label1 */
741
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
742
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
743
                              (unsigned long)label1_ptr));
744
#endif
745

    
746
    /* ld [arg1 + x], arg1 */
747
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
748
                 offsetof(CPUTLBEntry, addr_read), TARGET_ADDEND_LD_OP);
749

    
750
#if TARGET_LONG_BITS == 32
751
    /* and addr_reg, x, arg0 */
752
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
753
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
754
    /* add arg0, arg1, arg0 */
755
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
756
#else
757
    /* add addr_reg, arg1, arg0 */
758
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
759
#endif
760

    
761
#else
762
    arg0 = addr_reg;
763
#endif
764

    
765
    switch(opc) {
766
    case 0:
767
        /* ldub [arg0], data_reg */
768
        tcg_out_ldst(s, data_reg, arg0, 0, LDUB);
769
        break;
770
    case 0 | 4:
771
        /* ldsb [arg0], data_reg */
772
        tcg_out_ldst(s, data_reg, arg0, 0, LDSB);
773
        break;
774
    case 1:
775
#ifdef TARGET_WORDS_BIGENDIAN
776
        /* lduh [arg0], data_reg */
777
        tcg_out_ldst(s, data_reg, arg0, 0, LDUH);
778
#else
779
        /* lduha [arg0] ASI_PRIMARY_LITTLE, data_reg */
780
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUHA, ASI_PRIMARY_LITTLE);
781
#endif
782
        break;
783
    case 1 | 4:
784
#ifdef TARGET_WORDS_BIGENDIAN
785
        /* ldsh [arg0], data_reg */
786
        tcg_out_ldst(s, data_reg, arg0, 0, LDSH);
787
#else
788
        /* ldsha [arg0] ASI_PRIMARY_LITTLE, data_reg */
789
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSHA, ASI_PRIMARY_LITTLE);
790
#endif
791
        break;
792
    case 2:
793
#ifdef TARGET_WORDS_BIGENDIAN
794
        /* lduw [arg0], data_reg */
795
        tcg_out_ldst(s, data_reg, arg0, 0, LDUW);
796
#else
797
        /* lduwa [arg0] ASI_PRIMARY_LITTLE, data_reg */
798
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUWA, ASI_PRIMARY_LITTLE);
799
#endif
800
        break;
801
    case 2 | 4:
802
#ifdef TARGET_WORDS_BIGENDIAN
803
        /* ldsw [arg0], data_reg */
804
        tcg_out_ldst(s, data_reg, arg0, 0, LDSW);
805
#else
806
        /* ldswa [arg0] ASI_PRIMARY_LITTLE, data_reg */
807
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSWA, ASI_PRIMARY_LITTLE);
808
#endif
809
        break;
810
    case 3:
811
#ifdef TARGET_WORDS_BIGENDIAN
812
        /* ldx [arg0], data_reg */
813
        tcg_out_ldst(s, data_reg, arg0, 0, LDX);
814
#else
815
        /* ldxa [arg0] ASI_PRIMARY_LITTLE, data_reg */
816
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDXA, ASI_PRIMARY_LITTLE);
817
#endif
818
        break;
819
    default:
820
        tcg_abort();
821
    }
822

    
823
#if defined(CONFIG_SOFTMMU)
824
    /* label2: */
825
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
826
                   INSN_OFF22((unsigned long)s->code_ptr -
827
                              (unsigned long)label2_ptr));
828
#endif
829
}
830

    
831
static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
832
                            int opc)
833
{
834
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
835
#if defined(CONFIG_SOFTMMU)
836
    uint32_t *label1_ptr, *label2_ptr;
837
#endif
838

    
839
    data_reg = *args++;
840
    addr_reg = *args++;
841
    mem_index = *args;
842

    
843
    s_bits = opc;
844

    
845
    arg0 = TCG_REG_O0;
846
    arg1 = TCG_REG_O1;
847
    arg2 = TCG_REG_O2;
848

    
849
#if defined(CONFIG_SOFTMMU)
850
    /* srl addr_reg, x, arg1 */
851
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
852
                   SHIFT_SRL);
853

    
854
    /* and addr_reg, x, arg0 */
855
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
856
                   ARITH_AND);
857

    
858
    /* and arg1, x, arg1 */
859
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
860

    
861
    /* add arg1, x, arg1 */
862
    tcg_out_addi(s, arg1, offsetof(CPUState,
863
                                   tlb_table[mem_index][0].addr_write));
864

    
865
    /* add env, arg1, arg1 */
866
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
867

    
868
    /* ld [arg1], arg2 */
869
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
870
              INSN_RS2(TCG_REG_G0));
871

    
872
    /* subcc arg0, arg2, %g0 */
873
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
874

    
875
    /* will become:
876
       be label1
877
        or
878
       be,pt %xcc label1 */
879
    label1_ptr = (uint32_t *)s->code_ptr;
880
    tcg_out32(s, 0);
881

    
882
    /* mov (delay slot) */
883
    tcg_out_mov(s, arg0, addr_reg);
884

    
885
    /* mov */
886
    tcg_out_mov(s, arg1, data_reg);
887

    
888
    /* mov */
889
    tcg_out_movi(s, TCG_TYPE_I32, arg2, mem_index);
890

    
891
    /* XXX: move that code at the end of the TB */
892
    /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
893
    tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[s_bits]
894
                           - (tcg_target_ulong)s->code_ptr) >> 2)
895
                         & 0x3fffffff));
896
    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
897
       global registers */
898
    // delay slot
899
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
900
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
901
                 sizeof(long), HOST_ST_OP);
902
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
903
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
904
                 sizeof(long), HOST_LD_OP);
905

    
906
    /* will become:
907
       ba label2 */
908
    label2_ptr = (uint32_t *)s->code_ptr;
909
    tcg_out32(s, 0);
910

    
911
    /* nop (delay slot) */
912
    tcg_out_nop(s);
913

    
914
#if TARGET_LONG_BITS == 32
915
    /* be label1 */
916
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
917
                   INSN_OFF22((unsigned long)s->code_ptr -
918
                              (unsigned long)label1_ptr));
919
#else
920
    /* be,pt %xcc label1 */
921
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
922
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
923
                              (unsigned long)label1_ptr));
924
#endif
925

    
926
    /* ld [arg1 + x], arg1 */
927
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
928
                 offsetof(CPUTLBEntry, addr_write), TARGET_ADDEND_LD_OP);
929

    
930
#if TARGET_LONG_BITS == 32
931
    /* and addr_reg, x, arg0 */
932
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
933
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
934
    /* add arg0, arg1, arg0 */
935
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
936
#else
937
    /* add addr_reg, arg1, arg0 */
938
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
939
#endif
940

    
941
#else
942
    arg0 = addr_reg;
943
#endif
944

    
945
    switch(opc) {
946
    case 0:
947
        /* stb data_reg, [arg0] */
948
        tcg_out_ldst(s, data_reg, arg0, 0, STB);
949
        break;
950
    case 1:
951
#ifdef TARGET_WORDS_BIGENDIAN
952
        /* sth data_reg, [arg0] */
953
        tcg_out_ldst(s, data_reg, arg0, 0, STH);
954
#else
955
        /* stha data_reg, [arg0] ASI_PRIMARY_LITTLE */
956
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STHA, ASI_PRIMARY_LITTLE);
957
#endif
958
        break;
959
    case 2:
960
#ifdef TARGET_WORDS_BIGENDIAN
961
        /* stw data_reg, [arg0] */
962
        tcg_out_ldst(s, data_reg, arg0, 0, STW);
963
#else
964
        /* stwa data_reg, [arg0] ASI_PRIMARY_LITTLE */
965
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STWA, ASI_PRIMARY_LITTLE);
966
#endif
967
        break;
968
    case 3:
969
#ifdef TARGET_WORDS_BIGENDIAN
970
        /* stx data_reg, [arg0] */
971
        tcg_out_ldst(s, data_reg, arg0, 0, STX);
972
#else
973
        /* stxa data_reg, [arg0] ASI_PRIMARY_LITTLE */
974
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STXA, ASI_PRIMARY_LITTLE);
975
#endif
976
        break;
977
    default:
978
        tcg_abort();
979
    }
980

    
981
#if defined(CONFIG_SOFTMMU)
982
    /* label2: */
983
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
984
                   INSN_OFF22((unsigned long)s->code_ptr -
985
                              (unsigned long)label2_ptr));
986
#endif
987
}
988

    
989
static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
990
                              const int *const_args)
991
{
992
    int c;
993

    
994
    switch (opc) {
995
    case INDEX_op_exit_tb:
996
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
997
        tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
998
                  INSN_IMM13(8));
999
        tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
1000
                      INSN_RS2(TCG_REG_G0));
1001
        break;
1002
    case INDEX_op_goto_tb:
1003
        if (s->tb_jmp_offset) {
1004
            /* direct jump method */
1005
            tcg_out_sethi(s, TCG_REG_I5, args[0] & 0xffffe000);
1006
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
1007
                      INSN_IMM13((args[0] & 0x1fff)));
1008
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1009
        } else {
1010
            /* indirect jump method */
1011
            tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
1012
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
1013
                      INSN_RS2(TCG_REG_G0));
1014
        }
1015
        tcg_out_nop(s);
1016
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1017
        break;
1018
    case INDEX_op_call:
1019
        if (const_args[0])
1020
            tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
1021
                                   - (tcg_target_ulong)s->code_ptr) >> 2)
1022
                                 & 0x3fffffff));
1023
        else {
1024
            tcg_out_ld_ptr(s, TCG_REG_I5,
1025
                           (tcg_target_long)(s->tb_next + args[0]));
1026
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
1027
                      INSN_RS2(TCG_REG_G0));
1028
        }
1029
        /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
1030
           global registers */
1031
        // delay slot
1032
        tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
1033
                     TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
1034
                     sizeof(long), HOST_ST_OP);
1035
        tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
1036
                     TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
1037
                     sizeof(long), HOST_LD_OP);
1038
        break;
1039
    case INDEX_op_jmp:
1040
    case INDEX_op_br:
1041
        tcg_out_branch_i32(s, COND_A, args[0]);
1042
        tcg_out_nop(s);
1043
        break;
1044
    case INDEX_op_movi_i32:
1045
        tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
1046
        break;
1047

    
1048
#if TCG_TARGET_REG_BITS == 64
1049
#define OP_32_64(x)                             \
1050
        glue(glue(case INDEX_op_, x), _i32):    \
1051
        glue(glue(case INDEX_op_, x), _i64)
1052
#else
1053
#define OP_32_64(x)                             \
1054
        glue(glue(case INDEX_op_, x), _i32)
1055
#endif
1056
    OP_32_64(ld8u):
1057
        tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
1058
        break;
1059
    OP_32_64(ld8s):
1060
        tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
1061
        break;
1062
    OP_32_64(ld16u):
1063
        tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
1064
        break;
1065
    OP_32_64(ld16s):
1066
        tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
1067
        break;
1068
    case INDEX_op_ld_i32:
1069
#if TCG_TARGET_REG_BITS == 64
1070
    case INDEX_op_ld32u_i64:
1071
#endif
1072
        tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
1073
        break;
1074
    OP_32_64(st8):
1075
        tcg_out_ldst(s, args[0], args[1], args[2], STB);
1076
        break;
1077
    OP_32_64(st16):
1078
        tcg_out_ldst(s, args[0], args[1], args[2], STH);
1079
        break;
1080
    case INDEX_op_st_i32:
1081
#if TCG_TARGET_REG_BITS == 64
1082
    case INDEX_op_st32_i64:
1083
#endif
1084
        tcg_out_ldst(s, args[0], args[1], args[2], STW);
1085
        break;
1086
    OP_32_64(add):
1087
        c = ARITH_ADD;
1088
        goto gen_arith;
1089
    OP_32_64(sub):
1090
        c = ARITH_SUB;
1091
        goto gen_arith;
1092
    OP_32_64(and):
1093
        c = ARITH_AND;
1094
        goto gen_arith;
1095
    OP_32_64(or):
1096
        c = ARITH_OR;
1097
        goto gen_arith;
1098
    OP_32_64(xor):
1099
        c = ARITH_XOR;
1100
        goto gen_arith;
1101
    case INDEX_op_shl_i32:
1102
        c = SHIFT_SLL;
1103
        goto gen_arith;
1104
    case INDEX_op_shr_i32:
1105
        c = SHIFT_SRL;
1106
        goto gen_arith;
1107
    case INDEX_op_sar_i32:
1108
        c = SHIFT_SRA;
1109
        goto gen_arith;
1110
    case INDEX_op_mul_i32:
1111
        c = ARITH_UMUL;
1112
        goto gen_arith;
1113
    case INDEX_op_div2_i32:
1114
#if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1115
        c = ARITH_SDIVX;
1116
        goto gen_arith;
1117
#else
1118
        tcg_out_sety(s, 0);
1119
        c = ARITH_SDIV;
1120
        goto gen_arith;
1121
#endif
1122
    case INDEX_op_divu2_i32:
1123
#if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1124
        c = ARITH_UDIVX;
1125
        goto gen_arith;
1126
#else
1127
        tcg_out_sety(s, 0);
1128
        c = ARITH_UDIV;
1129
        goto gen_arith;
1130
#endif
1131

    
1132
    case INDEX_op_brcond_i32:
1133
        tcg_out_brcond_i32(s, args[2], args[0], args[1], const_args[1],
1134
                           args[3]);
1135
        break;
1136
#if TCG_TARGET_REG_BITS == 32
1137
    case INDEX_op_brcond2_i32:
1138
        tcg_out_brcond2_i32(s, args[4], args[0], args[1],
1139
                            args[2], const_args[2],
1140
                            args[3], const_args[3], args[5]);
1141
        break;
1142
    case INDEX_op_add2_i32:
1143
        tcg_out_arithc(s, args[0], args[2], args[4], const_args[4],
1144
                       ARITH_ADDCC);
1145
        tcg_out_arithc(s, args[1], args[3], args[5], const_args[5],
1146
                       ARITH_ADDX);
1147
        break;
1148
    case INDEX_op_sub2_i32:
1149
        tcg_out_arithc(s, args[0], args[2], args[4], const_args[4],
1150
                       ARITH_SUBCC);
1151
        tcg_out_arithc(s, args[1], args[3], args[5], const_args[5],
1152
                       ARITH_SUBX);
1153
        break;
1154
    case INDEX_op_mulu2_i32:
1155
        tcg_out_arithc(s, args[0], args[2], args[3], const_args[3],
1156
                       ARITH_UMUL);
1157
        tcg_out_rdy(s, args[1]);
1158
        break;
1159
#endif
1160

    
1161
    case INDEX_op_qemu_ld8u:
1162
        tcg_out_qemu_ld(s, args, 0);
1163
        break;
1164
    case INDEX_op_qemu_ld8s:
1165
        tcg_out_qemu_ld(s, args, 0 | 4);
1166
        break;
1167
    case INDEX_op_qemu_ld16u:
1168
        tcg_out_qemu_ld(s, args, 1);
1169
        break;
1170
    case INDEX_op_qemu_ld16s:
1171
        tcg_out_qemu_ld(s, args, 1 | 4);
1172
        break;
1173
    case INDEX_op_qemu_ld32u:
1174
        tcg_out_qemu_ld(s, args, 2);
1175
        break;
1176
    case INDEX_op_qemu_ld32s:
1177
        tcg_out_qemu_ld(s, args, 2 | 4);
1178
        break;
1179
    case INDEX_op_qemu_st8:
1180
        tcg_out_qemu_st(s, args, 0);
1181
        break;
1182
    case INDEX_op_qemu_st16:
1183
        tcg_out_qemu_st(s, args, 1);
1184
        break;
1185
    case INDEX_op_qemu_st32:
1186
        tcg_out_qemu_st(s, args, 2);
1187
        break;
1188

    
1189
#if TCG_TARGET_REG_BITS == 64
1190
    case INDEX_op_movi_i64:
1191
        tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1192
        break;
1193
    case INDEX_op_ld32s_i64:
1194
        tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
1195
        break;
1196
    case INDEX_op_ld_i64:
1197
        tcg_out_ldst(s, args[0], args[1], args[2], LDX);
1198
        break;
1199
    case INDEX_op_st_i64:
1200
        tcg_out_ldst(s, args[0], args[1], args[2], STX);
1201
        break;
1202
    case INDEX_op_shl_i64:
1203
        c = SHIFT_SLLX;
1204
        goto gen_arith;
1205
    case INDEX_op_shr_i64:
1206
        c = SHIFT_SRLX;
1207
        goto gen_arith;
1208
    case INDEX_op_sar_i64:
1209
        c = SHIFT_SRAX;
1210
        goto gen_arith;
1211
    case INDEX_op_mul_i64:
1212
        c = ARITH_MULX;
1213
        goto gen_arith;
1214
    case INDEX_op_div2_i64:
1215
        c = ARITH_SDIVX;
1216
        goto gen_arith;
1217
    case INDEX_op_divu2_i64:
1218
        c = ARITH_UDIVX;
1219
        goto gen_arith;
1220

    
1221
    case INDEX_op_brcond_i64:
1222
        tcg_out_brcond_i64(s, args[2], args[0], args[1], const_args[1],
1223
                           args[3]);
1224
        break;
1225
    case INDEX_op_qemu_ld64:
1226
        tcg_out_qemu_ld(s, args, 3);
1227
        break;
1228
    case INDEX_op_qemu_st64:
1229
        tcg_out_qemu_st(s, args, 3);
1230
        break;
1231

    
1232
#endif
1233
    gen_arith:
1234
        tcg_out_arithc(s, args[0], args[1], args[2], const_args[2], c);
1235
        break;
1236

    
1237
    default:
1238
        fprintf(stderr, "unknown opcode 0x%x\n", opc);
1239
        tcg_abort();
1240
    }
1241
}
1242

    
1243
static const TCGTargetOpDef sparc_op_defs[] = {
1244
    { INDEX_op_exit_tb, { } },
1245
    { INDEX_op_goto_tb, { } },
1246
    { INDEX_op_call, { "ri" } },
1247
    { INDEX_op_jmp, { "ri" } },
1248
    { INDEX_op_br, { } },
1249

    
1250
    { INDEX_op_mov_i32, { "r", "r" } },
1251
    { INDEX_op_movi_i32, { "r" } },
1252
    { INDEX_op_ld8u_i32, { "r", "r" } },
1253
    { INDEX_op_ld8s_i32, { "r", "r" } },
1254
    { INDEX_op_ld16u_i32, { "r", "r" } },
1255
    { INDEX_op_ld16s_i32, { "r", "r" } },
1256
    { INDEX_op_ld_i32, { "r", "r" } },
1257
    { INDEX_op_st8_i32, { "r", "r" } },
1258
    { INDEX_op_st16_i32, { "r", "r" } },
1259
    { INDEX_op_st_i32, { "r", "r" } },
1260

    
1261
    { INDEX_op_add_i32, { "r", "r", "rJ" } },
1262
    { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1263
    { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1264
    { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1265
    { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1266
    { INDEX_op_and_i32, { "r", "r", "rJ" } },
1267
    { INDEX_op_or_i32, { "r", "r", "rJ" } },
1268
    { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1269

    
1270
    { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1271
    { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1272
    { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1273

    
1274
    { INDEX_op_brcond_i32, { "r", "rJ" } },
1275
#if TCG_TARGET_REG_BITS == 32
1276
    { INDEX_op_brcond2_i32, { "r", "r", "rJ", "rJ" } },
1277
    { INDEX_op_add2_i32, { "r", "r", "r", "r", "rJ", "rJ" } },
1278
    { INDEX_op_sub2_i32, { "r", "r", "r", "r", "rJ", "rJ" } },
1279
    { INDEX_op_mulu2_i32, { "r", "r", "r", "rJ" } },
1280
#endif
1281

    
1282
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1283
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1284
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1285
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1286
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1287
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1288

    
1289
    { INDEX_op_qemu_st8, { "L", "L" } },
1290
    { INDEX_op_qemu_st16, { "L", "L" } },
1291
    { INDEX_op_qemu_st32, { "L", "L" } },
1292

    
1293
#if TCG_TARGET_REG_BITS == 64
1294
    { INDEX_op_mov_i64, { "r", "r" } },
1295
    { INDEX_op_movi_i64, { "r" } },
1296
    { INDEX_op_ld8u_i64, { "r", "r" } },
1297
    { INDEX_op_ld8s_i64, { "r", "r" } },
1298
    { INDEX_op_ld16u_i64, { "r", "r" } },
1299
    { INDEX_op_ld16s_i64, { "r", "r" } },
1300
    { INDEX_op_ld32u_i64, { "r", "r" } },
1301
    { INDEX_op_ld32s_i64, { "r", "r" } },
1302
    { INDEX_op_ld_i64, { "r", "r" } },
1303
    { INDEX_op_st8_i64, { "r", "r" } },
1304
    { INDEX_op_st16_i64, { "r", "r" } },
1305
    { INDEX_op_st32_i64, { "r", "r" } },
1306
    { INDEX_op_st_i64, { "r", "r" } },
1307
    { INDEX_op_qemu_ld64, { "L", "L" } },
1308
    { INDEX_op_qemu_st64, { "L", "L" } },
1309

    
1310
    { INDEX_op_add_i64, { "r", "r", "rJ" } },
1311
    { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1312
    { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1313
    { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1314
    { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1315
    { INDEX_op_and_i64, { "r", "r", "rJ" } },
1316
    { INDEX_op_or_i64, { "r", "r", "rJ" } },
1317
    { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1318

    
1319
    { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1320
    { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1321
    { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1322

    
1323
    { INDEX_op_brcond_i64, { "r", "rJ" } },
1324
#endif
1325
    { -1 },
1326
};
1327

    
1328
void tcg_target_init(TCGContext *s)
1329
{
1330
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1331
#if TCG_TARGET_REG_BITS == 64
1332
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1333
#endif
1334
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1335
                     (1 << TCG_REG_G1) |
1336
                     (1 << TCG_REG_G2) |
1337
                     (1 << TCG_REG_G3) |
1338
                     (1 << TCG_REG_G4) |
1339
                     (1 << TCG_REG_G5) |
1340
                     (1 << TCG_REG_G6) |
1341
                     (1 << TCG_REG_G7) |
1342
                     (1 << TCG_REG_O0) |
1343
                     (1 << TCG_REG_O1) |
1344
                     (1 << TCG_REG_O2) |
1345
                     (1 << TCG_REG_O3) |
1346
                     (1 << TCG_REG_O4) |
1347
                     (1 << TCG_REG_O5) |
1348
                     (1 << TCG_REG_O7));
1349

    
1350
    tcg_regset_clear(s->reserved_regs);
1351
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1352
#if TCG_TARGET_REG_BITS == 64
1353
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I4); // for internal use
1354
#endif
1355
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1356
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1357
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1358
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1359
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1360
    tcg_add_target_add_op_defs(sparc_op_defs);
1361
}