Statistics
| Branch: | Revision:

root / tcg / sparc / tcg-target.c @ ba225198

History | View | Annotate | Download (40.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_AND  (INSN_OP(2) | INSN_OP3(0x01))
219
#define ARITH_OR   (INSN_OP(2) | INSN_OP3(0x02))
220
#define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
221
#define ARITH_XOR  (INSN_OP(2) | INSN_OP3(0x03))
222
#define ARITH_SUB  (INSN_OP(2) | INSN_OP3(0x04))
223
#define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
224
#define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
225
#define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
226
#define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
227
#define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
228
#define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
229
#define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
230
#define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
231
#define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
232

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

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

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

    
270
#ifndef ASI_PRIMARY_LITTLE
271
#define ASI_PRIMARY_LITTLE 0x88
272
#endif
273

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
413
static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
414
{
415
    if (val != 0) {
416
        if (check_fit_tl(val, 13))
417
            tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
418
        else {
419
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
420
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
421
        }
422
    }
423
}
424

    
425
static inline void tcg_out_andi(TCGContext *s, int reg, tcg_target_long val)
426
{
427
    if (val != 0) {
428
        if (check_fit_tl(val, 13))
429
            tcg_out_arithi(s, reg, reg, val, ARITH_AND);
430
        else {
431
            tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, val);
432
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_AND);
433
        }
434
    }
435
}
436

    
437
static inline void tcg_out_nop(TCGContext *s)
438
{
439
    tcg_out_sethi(s, TCG_REG_G0, 0);
440
}
441

    
442
static void tcg_out_branch_i32(TCGContext *s, int opc, int label_index)
443
{
444
    int32_t val;
445
    TCGLabel *l = &s->labels[label_index];
446

    
447
    if (l->has_value) {
448
        val = l->u.value - (tcg_target_long)s->code_ptr;
449
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
450
                      | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
451
    } else {
452
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
453
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
454
    }
455
}
456

    
457
#if TCG_TARGET_REG_BITS == 64
458
static void tcg_out_branch_i64(TCGContext *s, int opc, int label_index)
459
{
460
    int32_t val;
461
    TCGLabel *l = &s->labels[label_index];
462

    
463
    if (l->has_value) {
464
        val = l->u.value - (tcg_target_long)s->code_ptr;
465
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
466
                      (0x5 << 19) |
467
                      INSN_OFF19(l->u.value - (unsigned long)s->code_ptr)));
468
    } else {
469
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP19, label_index, 0);
470
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
471
                      (0x5 << 19) | 0));
472
    }
473
}
474
#endif
475

    
476
static const uint8_t tcg_cond_to_bcond[10] = {
477
    [TCG_COND_EQ] = COND_E,
478
    [TCG_COND_NE] = COND_NE,
479
    [TCG_COND_LT] = COND_L,
480
    [TCG_COND_GE] = COND_GE,
481
    [TCG_COND_LE] = COND_LE,
482
    [TCG_COND_GT] = COND_G,
483
    [TCG_COND_LTU] = COND_CS,
484
    [TCG_COND_GEU] = COND_CC,
485
    [TCG_COND_LEU] = COND_LEU,
486
    [TCG_COND_GTU] = COND_GU,
487
};
488

    
489
static void tcg_out_cmp(TCGContext *s, TCGArg c1, TCGArg c2, int c2const)
490
{
491
    tcg_out_arithc(s, TCG_REG_G0, c1, c2, c2const, ARITH_SUBCC);
492
}
493

    
494
static void tcg_out_brcond_i32(TCGContext *s, int cond,
495
                               TCGArg arg1, TCGArg arg2, int const_arg2,
496
                               int label_index)
497
{
498
    tcg_out_cmp(s, arg1, arg2, const_arg2);
499
    tcg_out_branch_i32(s, tcg_cond_to_bcond[cond], label_index);
500
    tcg_out_nop(s);
501
}
502

    
503
#if TCG_TARGET_REG_BITS == 64
504
static void tcg_out_brcond_i64(TCGContext *s, int cond,
505
                               TCGArg arg1, TCGArg arg2, int const_arg2,
506
                               int label_index)
507
{
508
    tcg_out_cmp(s, arg1, arg2, const_arg2);
509
    tcg_out_branch_i64(s, tcg_cond_to_bcond[cond], label_index);
510
    tcg_out_nop(s);
511
}
512
#else
513
static void tcg_out_brcond2_i32(TCGContext *s, int cond,
514
                                TCGArg al, TCGArg ah,
515
                                TCGArg bl, int blconst,
516
                                TCGArg bh, int bhconst, int label_dest)
517
{
518
    int cc, label_next = gen_new_label();
519

    
520
    tcg_out_cmp(s, ah, bh, bhconst);
521

    
522
    /* Note that we fill one of the delay slots with the second compare.  */
523
    switch (cond) {
524
    case TCG_COND_EQ:
525
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_NE], 0);
526
        tcg_out_branch_i32(s, cc, label_next);
527
        tcg_out_cmp(s, al, bl, blconst);
528
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_EQ], 0);
529
        tcg_out_branch_i32(s, cc, label_dest);
530
        break;
531

    
532
    case TCG_COND_NE:
533
        cc = INSN_COND(tcg_cond_to_bcond[TCG_COND_NE], 0);
534
        tcg_out_branch_i32(s, cc, label_dest);
535
        tcg_out_cmp(s, al, bl, blconst);
536
        tcg_out_branch_i32(s, cc, label_dest);
537
        break;
538

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

    
557
    tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
558
}
559
#endif
560

    
561
/* Generate global QEMU prologue and epilogue code */
562
void tcg_target_qemu_prologue(TCGContext *s)
563
{
564
    tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
565
              INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
566
    tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
567
              INSN_RS2(TCG_REG_G0));
568
    tcg_out_nop(s);
569
}
570

    
571
#if defined(CONFIG_SOFTMMU)
572

    
573
#include "../../softmmu_defs.h"
574

    
575
static const void * const qemu_ld_helpers[4] = {
576
    __ldb_mmu,
577
    __ldw_mmu,
578
    __ldl_mmu,
579
    __ldq_mmu,
580
};
581

    
582
static const void * const qemu_st_helpers[4] = {
583
    __stb_mmu,
584
    __stw_mmu,
585
    __stl_mmu,
586
    __stq_mmu,
587
};
588
#endif
589

    
590
#if TARGET_LONG_BITS == 32
591
#define TARGET_LD_OP LDUW
592
#else
593
#define TARGET_LD_OP LDX
594
#endif
595

    
596
#if TARGET_PHYS_ADDR_BITS == 32
597
#define TARGET_ADDEND_LD_OP LDUW
598
#else
599
#define TARGET_ADDEND_LD_OP LDX
600
#endif
601

    
602
#ifdef __arch64__
603
#define HOST_LD_OP LDX
604
#define HOST_ST_OP STX
605
#define HOST_SLL_OP SHIFT_SLLX
606
#define HOST_SRA_OP SHIFT_SRAX
607
#else
608
#define HOST_LD_OP LDUW
609
#define HOST_ST_OP STW
610
#define HOST_SLL_OP SHIFT_SLL
611
#define HOST_SRA_OP SHIFT_SRA
612
#endif
613

    
614
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
615
                            int opc)
616
{
617
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
618
#if defined(CONFIG_SOFTMMU)
619
    uint32_t *label1_ptr, *label2_ptr;
620
#endif
621

    
622
    data_reg = *args++;
623
    addr_reg = *args++;
624
    mem_index = *args;
625
    s_bits = opc & 3;
626

    
627
    arg0 = TCG_REG_O0;
628
    arg1 = TCG_REG_O1;
629
    arg2 = TCG_REG_O2;
630

    
631
#if defined(CONFIG_SOFTMMU)
632
    /* srl addr_reg, x, arg1 */
633
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
634
                   SHIFT_SRL);
635
    /* and addr_reg, x, arg0 */
636
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
637
                   ARITH_AND);
638

    
639
    /* and arg1, x, arg1 */
640
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
641

    
642
    /* add arg1, x, arg1 */
643
    tcg_out_addi(s, arg1, offsetof(CPUState,
644
                                   tlb_table[mem_index][0].addr_read));
645

    
646
    /* add env, arg1, arg1 */
647
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
648

    
649
    /* ld [arg1], arg2 */
650
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
651
              INSN_RS2(TCG_REG_G0));
652

    
653
    /* subcc arg0, arg2, %g0 */
654
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
655

    
656
    /* will become:
657
       be label1
658
        or
659
       be,pt %xcc label1 */
660
    label1_ptr = (uint32_t *)s->code_ptr;
661
    tcg_out32(s, 0);
662

    
663
    /* mov (delay slot) */
664
    tcg_out_mov(s, arg0, addr_reg);
665

    
666
    /* mov */
667
    tcg_out_movi(s, TCG_TYPE_I32, arg1, mem_index);
668

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

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

    
718
    /* will become:
719
       ba label2 */
720
    label2_ptr = (uint32_t *)s->code_ptr;
721
    tcg_out32(s, 0);
722

    
723
    /* nop (delay slot */
724
    tcg_out_nop(s);
725

    
726
    /* label1: */
727
#if TARGET_LONG_BITS == 32
728
    /* be label1 */
729
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
730
                   INSN_OFF22((unsigned long)s->code_ptr -
731
                              (unsigned long)label1_ptr));
732
#else
733
    /* be,pt %xcc label1 */
734
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
735
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
736
                              (unsigned long)label1_ptr));
737
#endif
738

    
739
    /* ld [arg1 + x], arg1 */
740
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
741
                 offsetof(CPUTLBEntry, addr_read), TARGET_ADDEND_LD_OP);
742

    
743
#if TARGET_LONG_BITS == 32
744
    /* and addr_reg, x, arg0 */
745
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
746
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
747
    /* add arg0, arg1, arg0 */
748
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
749
#else
750
    /* add addr_reg, arg1, arg0 */
751
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
752
#endif
753

    
754
#else
755
    arg0 = addr_reg;
756
#endif
757

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

    
816
#if defined(CONFIG_SOFTMMU)
817
    /* label2: */
818
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
819
                   INSN_OFF22((unsigned long)s->code_ptr -
820
                              (unsigned long)label2_ptr));
821
#endif
822
}
823

    
824
static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
825
                            int opc)
826
{
827
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
828
#if defined(CONFIG_SOFTMMU)
829
    uint32_t *label1_ptr, *label2_ptr;
830
#endif
831

    
832
    data_reg = *args++;
833
    addr_reg = *args++;
834
    mem_index = *args;
835

    
836
    s_bits = opc;
837

    
838
    arg0 = TCG_REG_O0;
839
    arg1 = TCG_REG_O1;
840
    arg2 = TCG_REG_O2;
841

    
842
#if defined(CONFIG_SOFTMMU)
843
    /* srl addr_reg, x, arg1 */
844
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
845
                   SHIFT_SRL);
846

    
847
    /* and addr_reg, x, arg0 */
848
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
849
                   ARITH_AND);
850

    
851
    /* and arg1, x, arg1 */
852
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
853

    
854
    /* add arg1, x, arg1 */
855
    tcg_out_addi(s, arg1, offsetof(CPUState,
856
                                   tlb_table[mem_index][0].addr_write));
857

    
858
    /* add env, arg1, arg1 */
859
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
860

    
861
    /* ld [arg1], arg2 */
862
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
863
              INSN_RS2(TCG_REG_G0));
864

    
865
    /* subcc arg0, arg2, %g0 */
866
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
867

    
868
    /* will become:
869
       be label1
870
        or
871
       be,pt %xcc label1 */
872
    label1_ptr = (uint32_t *)s->code_ptr;
873
    tcg_out32(s, 0);
874

    
875
    /* mov (delay slot) */
876
    tcg_out_mov(s, arg0, addr_reg);
877

    
878
    /* mov */
879
    tcg_out_mov(s, arg1, data_reg);
880

    
881
    /* mov */
882
    tcg_out_movi(s, TCG_TYPE_I32, arg2, mem_index);
883

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

    
899
    /* will become:
900
       ba label2 */
901
    label2_ptr = (uint32_t *)s->code_ptr;
902
    tcg_out32(s, 0);
903

    
904
    /* nop (delay slot) */
905
    tcg_out_nop(s);
906

    
907
#if TARGET_LONG_BITS == 32
908
    /* be label1 */
909
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
910
                   INSN_OFF22((unsigned long)s->code_ptr -
911
                              (unsigned long)label1_ptr));
912
#else
913
    /* be,pt %xcc label1 */
914
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
915
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
916
                              (unsigned long)label1_ptr));
917
#endif
918

    
919
    /* ld [arg1 + x], arg1 */
920
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
921
                 offsetof(CPUTLBEntry, addr_write), TARGET_ADDEND_LD_OP);
922

    
923
#if TARGET_LONG_BITS == 32
924
    /* and addr_reg, x, arg0 */
925
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
926
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
927
    /* add arg0, arg1, arg0 */
928
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
929
#else
930
    /* add addr_reg, arg1, arg0 */
931
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
932
#endif
933

    
934
#else
935
    arg0 = addr_reg;
936
#endif
937

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

    
974
#if defined(CONFIG_SOFTMMU)
975
    /* label2: */
976
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
977
                   INSN_OFF22((unsigned long)s->code_ptr -
978
                              (unsigned long)label2_ptr));
979
#endif
980
}
981

    
982
static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
983
                              const int *const_args)
984
{
985
    int c;
986

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

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

    
1125
    case INDEX_op_brcond_i32:
1126
        tcg_out_brcond_i32(s, args[2], args[0], args[1], const_args[1],
1127
                           args[3]);
1128
        break;
1129
#if TCG_TARGET_REG_BITS == 32
1130
    case INDEX_op_brcond2_i32:
1131
        tcg_out_brcond2_i32(s, args[4], args[0], args[1],
1132
                            args[2], const_args[2],
1133
                            args[3], const_args[3], args[5]);
1134
        break;
1135
#endif
1136

    
1137
    case INDEX_op_qemu_ld8u:
1138
        tcg_out_qemu_ld(s, args, 0);
1139
        break;
1140
    case INDEX_op_qemu_ld8s:
1141
        tcg_out_qemu_ld(s, args, 0 | 4);
1142
        break;
1143
    case INDEX_op_qemu_ld16u:
1144
        tcg_out_qemu_ld(s, args, 1);
1145
        break;
1146
    case INDEX_op_qemu_ld16s:
1147
        tcg_out_qemu_ld(s, args, 1 | 4);
1148
        break;
1149
    case INDEX_op_qemu_ld32u:
1150
        tcg_out_qemu_ld(s, args, 2);
1151
        break;
1152
    case INDEX_op_qemu_ld32s:
1153
        tcg_out_qemu_ld(s, args, 2 | 4);
1154
        break;
1155
    case INDEX_op_qemu_st8:
1156
        tcg_out_qemu_st(s, args, 0);
1157
        break;
1158
    case INDEX_op_qemu_st16:
1159
        tcg_out_qemu_st(s, args, 1);
1160
        break;
1161
    case INDEX_op_qemu_st32:
1162
        tcg_out_qemu_st(s, args, 2);
1163
        break;
1164

    
1165
#if TCG_TARGET_REG_BITS == 64
1166
    case INDEX_op_movi_i64:
1167
        tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1168
        break;
1169
    case INDEX_op_ld32s_i64:
1170
        tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
1171
        break;
1172
    case INDEX_op_ld_i64:
1173
        tcg_out_ldst(s, args[0], args[1], args[2], LDX);
1174
        break;
1175
    case INDEX_op_st_i64:
1176
        tcg_out_ldst(s, args[0], args[1], args[2], STX);
1177
        break;
1178
    case INDEX_op_shl_i64:
1179
        c = SHIFT_SLLX;
1180
        goto gen_arith;
1181
    case INDEX_op_shr_i64:
1182
        c = SHIFT_SRLX;
1183
        goto gen_arith;
1184
    case INDEX_op_sar_i64:
1185
        c = SHIFT_SRAX;
1186
        goto gen_arith;
1187
    case INDEX_op_mul_i64:
1188
        c = ARITH_MULX;
1189
        goto gen_arith;
1190
    case INDEX_op_div2_i64:
1191
        c = ARITH_SDIVX;
1192
        goto gen_arith;
1193
    case INDEX_op_divu2_i64:
1194
        c = ARITH_UDIVX;
1195
        goto gen_arith;
1196

    
1197
    case INDEX_op_brcond_i64:
1198
        tcg_out_brcond_i64(s, args[2], args[0], args[1], const_args[1],
1199
                           args[3]);
1200
        break;
1201
    case INDEX_op_qemu_ld64:
1202
        tcg_out_qemu_ld(s, args, 3);
1203
        break;
1204
    case INDEX_op_qemu_st64:
1205
        tcg_out_qemu_st(s, args, 3);
1206
        break;
1207

    
1208
#endif
1209
    gen_arith:
1210
        tcg_out_arithc(s, args[0], args[1], args[2], const_args[2], c);
1211
        break;
1212

    
1213
    default:
1214
        fprintf(stderr, "unknown opcode 0x%x\n", opc);
1215
        tcg_abort();
1216
    }
1217
}
1218

    
1219
static const TCGTargetOpDef sparc_op_defs[] = {
1220
    { INDEX_op_exit_tb, { } },
1221
    { INDEX_op_goto_tb, { } },
1222
    { INDEX_op_call, { "ri" } },
1223
    { INDEX_op_jmp, { "ri" } },
1224
    { INDEX_op_br, { } },
1225

    
1226
    { INDEX_op_mov_i32, { "r", "r" } },
1227
    { INDEX_op_movi_i32, { "r" } },
1228
    { INDEX_op_ld8u_i32, { "r", "r" } },
1229
    { INDEX_op_ld8s_i32, { "r", "r" } },
1230
    { INDEX_op_ld16u_i32, { "r", "r" } },
1231
    { INDEX_op_ld16s_i32, { "r", "r" } },
1232
    { INDEX_op_ld_i32, { "r", "r" } },
1233
    { INDEX_op_st8_i32, { "r", "r" } },
1234
    { INDEX_op_st16_i32, { "r", "r" } },
1235
    { INDEX_op_st_i32, { "r", "r" } },
1236

    
1237
    { INDEX_op_add_i32, { "r", "r", "rJ" } },
1238
    { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1239
    { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1240
    { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1241
    { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1242
    { INDEX_op_and_i32, { "r", "r", "rJ" } },
1243
    { INDEX_op_or_i32, { "r", "r", "rJ" } },
1244
    { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1245

    
1246
    { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1247
    { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1248
    { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1249

    
1250
    { INDEX_op_brcond_i32, { "r", "rJ" } },
1251
#if TCG_TARGET_REG_BITS == 32
1252
    { INDEX_op_brcond2_i32, { "r", "r", "rJ", "rJ" } },
1253
#endif
1254

    
1255
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1256
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1257
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1258
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1259
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1260
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1261

    
1262
    { INDEX_op_qemu_st8, { "L", "L" } },
1263
    { INDEX_op_qemu_st16, { "L", "L" } },
1264
    { INDEX_op_qemu_st32, { "L", "L" } },
1265

    
1266
#if TCG_TARGET_REG_BITS == 64
1267
    { INDEX_op_mov_i64, { "r", "r" } },
1268
    { INDEX_op_movi_i64, { "r" } },
1269
    { INDEX_op_ld8u_i64, { "r", "r" } },
1270
    { INDEX_op_ld8s_i64, { "r", "r" } },
1271
    { INDEX_op_ld16u_i64, { "r", "r" } },
1272
    { INDEX_op_ld16s_i64, { "r", "r" } },
1273
    { INDEX_op_ld32u_i64, { "r", "r" } },
1274
    { INDEX_op_ld32s_i64, { "r", "r" } },
1275
    { INDEX_op_ld_i64, { "r", "r" } },
1276
    { INDEX_op_st8_i64, { "r", "r" } },
1277
    { INDEX_op_st16_i64, { "r", "r" } },
1278
    { INDEX_op_st32_i64, { "r", "r" } },
1279
    { INDEX_op_st_i64, { "r", "r" } },
1280
    { INDEX_op_qemu_ld64, { "L", "L" } },
1281
    { INDEX_op_qemu_st64, { "L", "L" } },
1282

    
1283
    { INDEX_op_add_i64, { "r", "r", "rJ" } },
1284
    { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1285
    { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1286
    { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1287
    { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1288
    { INDEX_op_and_i64, { "r", "r", "rJ" } },
1289
    { INDEX_op_or_i64, { "r", "r", "rJ" } },
1290
    { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1291

    
1292
    { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1293
    { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1294
    { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1295

    
1296
    { INDEX_op_brcond_i64, { "r", "rJ" } },
1297
#endif
1298
    { -1 },
1299
};
1300

    
1301
void tcg_target_init(TCGContext *s)
1302
{
1303
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1304
#if TCG_TARGET_REG_BITS == 64
1305
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1306
#endif
1307
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1308
                     (1 << TCG_REG_G1) |
1309
                     (1 << TCG_REG_G2) |
1310
                     (1 << TCG_REG_G3) |
1311
                     (1 << TCG_REG_G4) |
1312
                     (1 << TCG_REG_G5) |
1313
                     (1 << TCG_REG_G6) |
1314
                     (1 << TCG_REG_G7) |
1315
                     (1 << TCG_REG_O0) |
1316
                     (1 << TCG_REG_O1) |
1317
                     (1 << TCG_REG_O2) |
1318
                     (1 << TCG_REG_O3) |
1319
                     (1 << TCG_REG_O4) |
1320
                     (1 << TCG_REG_O5) |
1321
                     (1 << TCG_REG_O7));
1322

    
1323
    tcg_regset_clear(s->reserved_regs);
1324
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1325
#if TCG_TARGET_REG_BITS == 64
1326
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I4); // for internal use
1327
#endif
1328
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1329
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1330
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1331
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1332
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1333
    tcg_add_target_add_op_defs(sparc_op_defs);
1334
}