Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (38.8 kB)

1
/*
2
 * Tiny Code Generator for QEMU
3
 *
4
 * Copyright (c) 2008 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#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 inline void tcg_out_mov(TCGContext *s, int ret, int arg)
289
{
290
    tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
291
}
292

    
293
static inline void tcg_out_sethi(TCGContext *s, int ret, uint32_t arg)
294
{
295
    tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
296
}
297

    
298
static inline void tcg_out_movi_imm13(TCGContext *s, int ret, uint32_t arg)
299
{
300
    tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
301
}
302

    
303
static inline void tcg_out_movi_imm32(TCGContext *s, int ret, uint32_t arg)
304
{
305
    if (check_fit_tl(arg, 13))
306
        tcg_out_movi_imm13(s, ret, arg);
307
    else {
308
        tcg_out_sethi(s, ret, arg);
309
        if (arg & 0x3ff)
310
            tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
311
    }
312
}
313

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

    
338
static inline void tcg_out_ld_raw(TCGContext *s, int ret,
339
                                  tcg_target_long arg)
340
{
341
    tcg_out_sethi(s, ret, arg);
342
    tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
343
              INSN_IMM13(arg & 0x3ff));
344
}
345

    
346
static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
347
                                  tcg_target_long arg)
348
{
349
    if (!check_fit_tl(arg, 10))
350
        tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ffULL);
351
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
352
    tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
353
              INSN_IMM13(arg & 0x3ff));
354
#else
355
    tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
356
              INSN_IMM13(arg & 0x3ff));
357
#endif
358
}
359

    
360
static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
361
{
362
    if (check_fit_tl(offset, 13))
363
        tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
364
                  INSN_IMM13(offset));
365
    else {
366
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
367
        tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
368
                  INSN_RS2(addr));
369
    }
370
}
371

    
372
static inline void tcg_out_ldst_asi(TCGContext *s, int ret, int addr,
373
                                    int offset, int op, int asi)
374
{
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_ASI(asi) | INSN_RS2(addr));
378
}
379

    
380
static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
381
                              int arg1, tcg_target_long arg2)
382
{
383
    if (type == TCG_TYPE_I32)
384
        tcg_out_ldst(s, ret, arg1, arg2, LDUW);
385
    else
386
        tcg_out_ldst(s, ret, arg1, arg2, LDX);
387
}
388

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

    
398
static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
399
{
400
    if (val == 0 || val == -1)
401
        tcg_out32(s, WRY | INSN_IMM13(val));
402
    else
403
        fprintf(stderr, "unimplemented sety %ld\n", (long)val);
404
}
405

    
406
static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
407
{
408
    if (val != 0) {
409
        if (check_fit_tl(val, 13))
410
            tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
411
        else {
412
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
413
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
414
        }
415
    }
416
}
417

    
418
static inline void tcg_out_andi(TCGContext *s, int reg, tcg_target_long val)
419
{
420
    if (val != 0) {
421
        if (check_fit_tl(val, 13))
422
            tcg_out_arithi(s, reg, reg, val, ARITH_AND);
423
        else {
424
            tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, val);
425
            tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_AND);
426
        }
427
    }
428
}
429

    
430
static inline void tcg_out_nop(TCGContext *s)
431
{
432
    tcg_out_sethi(s, TCG_REG_G0, 0);
433
}
434

    
435
static void tcg_out_branch_i32(TCGContext *s, int opc, int label_index)
436
{
437
    int32_t val;
438
    TCGLabel *l = &s->labels[label_index];
439

    
440
    if (l->has_value) {
441
        val = l->u.value - (tcg_target_long)s->code_ptr;
442
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
443
                      | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
444
    } else {
445
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
446
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
447
    }
448
}
449

    
450
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
451
static void tcg_out_branch_i64(TCGContext *s, int opc, int label_index)
452
{
453
    int32_t val;
454
    TCGLabel *l = &s->labels[label_index];
455

    
456
    if (l->has_value) {
457
        val = l->u.value - (tcg_target_long)s->code_ptr;
458
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
459
                      (0x5 << 19) |
460
                      INSN_OFF19(l->u.value - (unsigned long)s->code_ptr)));
461
    } else {
462
        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP19, label_index, 0);
463
        tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x1) |
464
                      (0x5 << 19) | 0));
465
    }
466
}
467
#endif
468

    
469
static const uint8_t tcg_cond_to_bcond[10] = {
470
    [TCG_COND_EQ] = COND_E,
471
    [TCG_COND_NE] = COND_NE,
472
    [TCG_COND_LT] = COND_L,
473
    [TCG_COND_GE] = COND_GE,
474
    [TCG_COND_LE] = COND_LE,
475
    [TCG_COND_GT] = COND_G,
476
    [TCG_COND_LTU] = COND_CS,
477
    [TCG_COND_GEU] = COND_CC,
478
    [TCG_COND_LEU] = COND_LEU,
479
    [TCG_COND_GTU] = COND_GU,
480
};
481

    
482
static void tcg_out_brcond_i32(TCGContext *s, int cond,
483
                               TCGArg arg1, TCGArg arg2, int const_arg2,
484
                               int label_index)
485
{
486
    if (const_arg2 && arg2 == 0)
487
        /* orcc %g0, r, %g0 */
488
        tcg_out_arith(s, TCG_REG_G0, TCG_REG_G0, arg1, ARITH_ORCC);
489
    else
490
        /* subcc r1, r2, %g0 */
491
        tcg_out_arith(s, TCG_REG_G0, arg1, arg2, ARITH_SUBCC);
492
    tcg_out_branch_i32(s, tcg_cond_to_bcond[cond], label_index);
493
    tcg_out_nop(s);
494
}
495

    
496
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
497
static void tcg_out_brcond_i64(TCGContext *s, int cond,
498
                               TCGArg arg1, TCGArg arg2, int const_arg2,
499
                               int label_index)
500
{
501
    if (const_arg2 && arg2 == 0)
502
        /* orcc %g0, r, %g0 */
503
        tcg_out_arith(s, TCG_REG_G0, TCG_REG_G0, arg1, ARITH_ORCC);
504
    else
505
        /* subcc r1, r2, %g0 */
506
        tcg_out_arith(s, TCG_REG_G0, arg1, arg2, ARITH_SUBCC);
507
    tcg_out_branch_i64(s, tcg_cond_to_bcond[cond], label_index);
508
    tcg_out_nop(s);
509
}
510
#endif
511

    
512
/* Generate global QEMU prologue and epilogue code */
513
void tcg_target_qemu_prologue(TCGContext *s)
514
{
515
    tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
516
              INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
517
    tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
518
              INSN_RS2(TCG_REG_G0));
519
    tcg_out_nop(s);
520
}
521

    
522
#if defined(CONFIG_SOFTMMU)
523

    
524
#include "../../softmmu_defs.h"
525

    
526
static const void * const qemu_ld_helpers[4] = {
527
    __ldb_mmu,
528
    __ldw_mmu,
529
    __ldl_mmu,
530
    __ldq_mmu,
531
};
532

    
533
static const void * const qemu_st_helpers[4] = {
534
    __stb_mmu,
535
    __stw_mmu,
536
    __stl_mmu,
537
    __stq_mmu,
538
};
539
#endif
540

    
541
#if TARGET_LONG_BITS == 32
542
#define TARGET_LD_OP LDUW
543
#else
544
#define TARGET_LD_OP LDX
545
#endif
546

    
547
#if TARGET_PHYS_ADDR_BITS == 32
548
#define TARGET_ADDEND_LD_OP LDUW
549
#else
550
#define TARGET_ADDEND_LD_OP LDX
551
#endif
552

    
553
#ifdef __arch64__
554
#define HOST_LD_OP LDX
555
#define HOST_ST_OP STX
556
#define HOST_SLL_OP SHIFT_SLLX
557
#define HOST_SRA_OP SHIFT_SRAX
558
#else
559
#define HOST_LD_OP LDUW
560
#define HOST_ST_OP STW
561
#define HOST_SLL_OP SHIFT_SLL
562
#define HOST_SRA_OP SHIFT_SRA
563
#endif
564

    
565
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
566
                            int opc)
567
{
568
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
569
#if defined(CONFIG_SOFTMMU)
570
    uint32_t *label1_ptr, *label2_ptr;
571
#endif
572

    
573
    data_reg = *args++;
574
    addr_reg = *args++;
575
    mem_index = *args;
576
    s_bits = opc & 3;
577

    
578
    arg0 = TCG_REG_O0;
579
    arg1 = TCG_REG_O1;
580
    arg2 = TCG_REG_O2;
581

    
582
#if defined(CONFIG_SOFTMMU)
583
    /* srl addr_reg, x, arg1 */
584
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
585
                   SHIFT_SRL);
586
    /* and addr_reg, x, arg0 */
587
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
588
                   ARITH_AND);
589

    
590
    /* and arg1, x, arg1 */
591
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
592

    
593
    /* add arg1, x, arg1 */
594
    tcg_out_addi(s, arg1, offsetof(CPUState,
595
                                   tlb_table[mem_index][0].addr_read));
596

    
597
    /* add env, arg1, arg1 */
598
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
599

    
600
    /* ld [arg1], arg2 */
601
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
602
              INSN_RS2(TCG_REG_G0));
603

    
604
    /* subcc arg0, arg2, %g0 */
605
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
606

    
607
    /* will become:
608
       be label1
609
        or
610
       be,pt %xcc label1 */
611
    label1_ptr = (uint32_t *)s->code_ptr;
612
    tcg_out32(s, 0);
613

    
614
    /* mov (delay slot) */
615
    tcg_out_mov(s, arg0, addr_reg);
616

    
617
    /* mov */
618
    tcg_out_movi(s, TCG_TYPE_I32, arg1, mem_index);
619

    
620
    /* XXX: move that code at the end of the TB */
621
    /* qemu_ld_helper[s_bits](arg0, arg1) */
622
    tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
623
                           - (tcg_target_ulong)s->code_ptr) >> 2)
624
                         & 0x3fffffff));
625
    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
626
       global registers */
627
    // delay slot
628
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
629
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
630
                 sizeof(long), HOST_ST_OP);
631
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
632
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
633
                 sizeof(long), HOST_LD_OP);
634

    
635
    /* data_reg = sign_extend(arg0) */
636
    switch(opc) {
637
    case 0 | 4:
638
        /* sll arg0, 24/56, data_reg */
639
        tcg_out_arithi(s, data_reg, arg0, (int)sizeof(tcg_target_long) * 8 - 8,
640
                       HOST_SLL_OP);
641
        /* sra data_reg, 24/56, data_reg */
642
        tcg_out_arithi(s, data_reg, data_reg,
643
                       (int)sizeof(tcg_target_long) * 8 - 8, HOST_SRA_OP);
644
        break;
645
    case 1 | 4:
646
        /* sll arg0, 16/48, data_reg */
647
        tcg_out_arithi(s, data_reg, arg0,
648
                       (int)sizeof(tcg_target_long) * 8 - 16, HOST_SLL_OP);
649
        /* sra data_reg, 16/48, data_reg */
650
        tcg_out_arithi(s, data_reg, data_reg,
651
                       (int)sizeof(tcg_target_long) * 8 - 16, HOST_SRA_OP);
652
        break;
653
    case 2 | 4:
654
        /* sll arg0, 32, data_reg */
655
        tcg_out_arithi(s, data_reg, arg0, 32, HOST_SLL_OP);
656
        /* sra data_reg, 32, data_reg */
657
        tcg_out_arithi(s, data_reg, data_reg, 32, HOST_SRA_OP);
658
        break;
659
    case 0:
660
    case 1:
661
    case 2:
662
    case 3:
663
    default:
664
        /* mov */
665
        tcg_out_mov(s, data_reg, arg0);
666
        break;
667
    }
668

    
669
    /* will become:
670
       ba label2 */
671
    label2_ptr = (uint32_t *)s->code_ptr;
672
    tcg_out32(s, 0);
673

    
674
    /* nop (delay slot */
675
    tcg_out_nop(s);
676

    
677
    /* label1: */
678
#if TARGET_LONG_BITS == 32
679
    /* be label1 */
680
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
681
                   INSN_OFF22((unsigned long)s->code_ptr -
682
                              (unsigned long)label1_ptr));
683
#else
684
    /* be,pt %xcc label1 */
685
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
686
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
687
                              (unsigned long)label1_ptr));
688
#endif
689

    
690
    /* ld [arg1 + x], arg1 */
691
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
692
                 offsetof(CPUTLBEntry, addr_read), TARGET_ADDEND_LD_OP);
693

    
694
#if TARGET_LONG_BITS == 32
695
    /* and addr_reg, x, arg0 */
696
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
697
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
698
    /* add arg0, arg1, arg0 */
699
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
700
#else
701
    /* add addr_reg, arg1, arg0 */
702
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
703
#endif
704

    
705
#else
706
    arg0 = addr_reg;
707
#endif
708

    
709
    switch(opc) {
710
    case 0:
711
        /* ldub [arg0], data_reg */
712
        tcg_out_ldst(s, data_reg, arg0, 0, LDUB);
713
        break;
714
    case 0 | 4:
715
        /* ldsb [arg0], data_reg */
716
        tcg_out_ldst(s, data_reg, arg0, 0, LDSB);
717
        break;
718
    case 1:
719
#ifdef TARGET_WORDS_BIGENDIAN
720
        /* lduh [arg0], data_reg */
721
        tcg_out_ldst(s, data_reg, arg0, 0, LDUH);
722
#else
723
        /* lduha [arg0] ASI_PRIMARY_LITTLE, data_reg */
724
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUHA, ASI_PRIMARY_LITTLE);
725
#endif
726
        break;
727
    case 1 | 4:
728
#ifdef TARGET_WORDS_BIGENDIAN
729
        /* ldsh [arg0], data_reg */
730
        tcg_out_ldst(s, data_reg, arg0, 0, LDSH);
731
#else
732
        /* ldsha [arg0] ASI_PRIMARY_LITTLE, data_reg */
733
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSHA, ASI_PRIMARY_LITTLE);
734
#endif
735
        break;
736
    case 2:
737
#ifdef TARGET_WORDS_BIGENDIAN
738
        /* lduw [arg0], data_reg */
739
        tcg_out_ldst(s, data_reg, arg0, 0, LDUW);
740
#else
741
        /* lduwa [arg0] ASI_PRIMARY_LITTLE, data_reg */
742
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUWA, ASI_PRIMARY_LITTLE);
743
#endif
744
        break;
745
    case 2 | 4:
746
#ifdef TARGET_WORDS_BIGENDIAN
747
        /* ldsw [arg0], data_reg */
748
        tcg_out_ldst(s, data_reg, arg0, 0, LDSW);
749
#else
750
        /* ldswa [arg0] ASI_PRIMARY_LITTLE, data_reg */
751
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSWA, ASI_PRIMARY_LITTLE);
752
#endif
753
        break;
754
    case 3:
755
#ifdef TARGET_WORDS_BIGENDIAN
756
        /* ldx [arg0], data_reg */
757
        tcg_out_ldst(s, data_reg, arg0, 0, LDX);
758
#else
759
        /* ldxa [arg0] ASI_PRIMARY_LITTLE, data_reg */
760
        tcg_out_ldst_asi(s, data_reg, arg0, 0, LDXA, ASI_PRIMARY_LITTLE);
761
#endif
762
        break;
763
    default:
764
        tcg_abort();
765
    }
766

    
767
#if defined(CONFIG_SOFTMMU)
768
    /* label2: */
769
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
770
                   INSN_OFF22((unsigned long)s->code_ptr -
771
                              (unsigned long)label2_ptr));
772
#endif
773
}
774

    
775
static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
776
                            int opc)
777
{
778
    int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
779
#if defined(CONFIG_SOFTMMU)
780
    uint32_t *label1_ptr, *label2_ptr;
781
#endif
782

    
783
    data_reg = *args++;
784
    addr_reg = *args++;
785
    mem_index = *args;
786

    
787
    s_bits = opc;
788

    
789
    arg0 = TCG_REG_O0;
790
    arg1 = TCG_REG_O1;
791
    arg2 = TCG_REG_O2;
792

    
793
#if defined(CONFIG_SOFTMMU)
794
    /* srl addr_reg, x, arg1 */
795
    tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
796
                   SHIFT_SRL);
797

    
798
    /* and addr_reg, x, arg0 */
799
    tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
800
                   ARITH_AND);
801

    
802
    /* and arg1, x, arg1 */
803
    tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
804

    
805
    /* add arg1, x, arg1 */
806
    tcg_out_addi(s, arg1, offsetof(CPUState,
807
                                   tlb_table[mem_index][0].addr_write));
808

    
809
    /* add env, arg1, arg1 */
810
    tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
811

    
812
    /* ld [arg1], arg2 */
813
    tcg_out32(s, TARGET_LD_OP | INSN_RD(arg2) | INSN_RS1(arg1) |
814
              INSN_RS2(TCG_REG_G0));
815

    
816
    /* subcc arg0, arg2, %g0 */
817
    tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
818

    
819
    /* will become:
820
       be label1
821
        or
822
       be,pt %xcc label1 */
823
    label1_ptr = (uint32_t *)s->code_ptr;
824
    tcg_out32(s, 0);
825

    
826
    /* mov (delay slot) */
827
    tcg_out_mov(s, arg0, addr_reg);
828

    
829
    /* mov */
830
    tcg_out_mov(s, arg1, data_reg);
831

    
832
    /* mov */
833
    tcg_out_movi(s, TCG_TYPE_I32, arg2, mem_index);
834

    
835
    /* XXX: move that code at the end of the TB */
836
    /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
837
    tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[s_bits]
838
                           - (tcg_target_ulong)s->code_ptr) >> 2)
839
                         & 0x3fffffff));
840
    /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
841
       global registers */
842
    // delay slot
843
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
844
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
845
                 sizeof(long), HOST_ST_OP);
846
    tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
847
                 TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
848
                 sizeof(long), HOST_LD_OP);
849

    
850
    /* will become:
851
       ba label2 */
852
    label2_ptr = (uint32_t *)s->code_ptr;
853
    tcg_out32(s, 0);
854

    
855
    /* nop (delay slot) */
856
    tcg_out_nop(s);
857

    
858
#if TARGET_LONG_BITS == 32
859
    /* be label1 */
860
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
861
                   INSN_OFF22((unsigned long)s->code_ptr -
862
                              (unsigned long)label1_ptr));
863
#else
864
    /* be,pt %xcc label1 */
865
    *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x1) |
866
                   (0x5 << 19) | INSN_OFF19((unsigned long)s->code_ptr -
867
                              (unsigned long)label1_ptr));
868
#endif
869

    
870
    /* ld [arg1 + x], arg1 */
871
    tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
872
                 offsetof(CPUTLBEntry, addr_write), TARGET_ADDEND_LD_OP);
873

    
874
#if TARGET_LONG_BITS == 32
875
    /* and addr_reg, x, arg0 */
876
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, 0xffffffff);
877
    tcg_out_arith(s, arg0, addr_reg, TCG_REG_I5, ARITH_AND);
878
    /* add arg0, arg1, arg0 */
879
    tcg_out_arith(s, arg0, arg0, arg1, ARITH_ADD);
880
#else
881
    /* add addr_reg, arg1, arg0 */
882
    tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
883
#endif
884

    
885
#else
886
    arg0 = addr_reg;
887
#endif
888

    
889
    switch(opc) {
890
    case 0:
891
        /* stb data_reg, [arg0] */
892
        tcg_out_ldst(s, data_reg, arg0, 0, STB);
893
        break;
894
    case 1:
895
#ifdef TARGET_WORDS_BIGENDIAN
896
        /* sth data_reg, [arg0] */
897
        tcg_out_ldst(s, data_reg, arg0, 0, STH);
898
#else
899
        /* stha data_reg, [arg0] ASI_PRIMARY_LITTLE */
900
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STHA, ASI_PRIMARY_LITTLE);
901
#endif
902
        break;
903
    case 2:
904
#ifdef TARGET_WORDS_BIGENDIAN
905
        /* stw data_reg, [arg0] */
906
        tcg_out_ldst(s, data_reg, arg0, 0, STW);
907
#else
908
        /* stwa data_reg, [arg0] ASI_PRIMARY_LITTLE */
909
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STWA, ASI_PRIMARY_LITTLE);
910
#endif
911
        break;
912
    case 3:
913
#ifdef TARGET_WORDS_BIGENDIAN
914
        /* stx data_reg, [arg0] */
915
        tcg_out_ldst(s, data_reg, arg0, 0, STX);
916
#else
917
        /* stxa data_reg, [arg0] ASI_PRIMARY_LITTLE */
918
        tcg_out_ldst_asi(s, data_reg, arg0, 0, STXA, ASI_PRIMARY_LITTLE);
919
#endif
920
        break;
921
    default:
922
        tcg_abort();
923
    }
924

    
925
#if defined(CONFIG_SOFTMMU)
926
    /* label2: */
927
    *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
928
                   INSN_OFF22((unsigned long)s->code_ptr -
929
                              (unsigned long)label2_ptr));
930
#endif
931
}
932

    
933
static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
934
                              const int *const_args)
935
{
936
    int c;
937

    
938
    switch (opc) {
939
    case INDEX_op_exit_tb:
940
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
941
        tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
942
                  INSN_IMM13(8));
943
        tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
944
                      INSN_RS2(TCG_REG_G0));
945
        break;
946
    case INDEX_op_goto_tb:
947
        if (s->tb_jmp_offset) {
948
            /* direct jump method */
949
            tcg_out_sethi(s, TCG_REG_I5, args[0] & 0xffffe000);
950
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
951
                      INSN_IMM13((args[0] & 0x1fff)));
952
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
953
        } else {
954
            /* indirect jump method */
955
            tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
956
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
957
                      INSN_RS2(TCG_REG_G0));
958
        }
959
        tcg_out_nop(s);
960
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
961
        break;
962
    case INDEX_op_call:
963
        if (const_args[0])
964
            tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
965
                                   - (tcg_target_ulong)s->code_ptr) >> 2)
966
                                 & 0x3fffffff));
967
        else {
968
            tcg_out_ld_ptr(s, TCG_REG_I5,
969
                           (tcg_target_long)(s->tb_next + args[0]));
970
            tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
971
                      INSN_RS2(TCG_REG_G0));
972
        }
973
        /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
974
           global registers */
975
        // delay slot
976
        tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
977
                     TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
978
                     sizeof(long), HOST_ST_OP);
979
        tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
980
                     TCG_TARGET_CALL_STACK_OFFSET - TCG_STATIC_CALL_ARGS_SIZE -
981
                     sizeof(long), HOST_LD_OP);
982
        break;
983
    case INDEX_op_jmp:
984
    case INDEX_op_br:
985
        tcg_out_branch_i32(s, COND_A, args[0]);
986
        tcg_out_nop(s);
987
        break;
988
    case INDEX_op_movi_i32:
989
        tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
990
        break;
991

    
992
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
993
#define OP_32_64(x)                             \
994
        glue(glue(case INDEX_op_, x), _i32:)    \
995
        glue(glue(case INDEX_op_, x), _i64:)
996
#else
997
#define OP_32_64(x)                             \
998
        glue(glue(case INDEX_op_, x), _i32:)
999
#endif
1000
        OP_32_64(ld8u);
1001
        tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
1002
        break;
1003
        OP_32_64(ld8s);
1004
        tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
1005
        break;
1006
        OP_32_64(ld16u);
1007
        tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
1008
        break;
1009
        OP_32_64(ld16s);
1010
        tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
1011
        break;
1012
    case INDEX_op_ld_i32:
1013
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1014
    case INDEX_op_ld32u_i64:
1015
#endif
1016
        tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
1017
        break;
1018
        OP_32_64(st8);
1019
        tcg_out_ldst(s, args[0], args[1], args[2], STB);
1020
        break;
1021
        OP_32_64(st16);
1022
        tcg_out_ldst(s, args[0], args[1], args[2], STH);
1023
        break;
1024
    case INDEX_op_st_i32:
1025
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1026
    case INDEX_op_st32_i64:
1027
#endif
1028
        tcg_out_ldst(s, args[0], args[1], args[2], STW);
1029
        break;
1030
        OP_32_64(add);
1031
        c = ARITH_ADD;
1032
        goto gen_arith32;
1033
        OP_32_64(sub);
1034
        c = ARITH_SUB;
1035
        goto gen_arith32;
1036
        OP_32_64(and);
1037
        c = ARITH_AND;
1038
        goto gen_arith32;
1039
        OP_32_64(or);
1040
        c = ARITH_OR;
1041
        goto gen_arith32;
1042
        OP_32_64(xor);
1043
        c = ARITH_XOR;
1044
        goto gen_arith32;
1045
    case INDEX_op_shl_i32:
1046
        c = SHIFT_SLL;
1047
        goto gen_arith32;
1048
    case INDEX_op_shr_i32:
1049
        c = SHIFT_SRL;
1050
        goto gen_arith32;
1051
    case INDEX_op_sar_i32:
1052
        c = SHIFT_SRA;
1053
        goto gen_arith32;
1054
    case INDEX_op_mul_i32:
1055
        c = ARITH_UMUL;
1056
        goto gen_arith32;
1057
    case INDEX_op_div2_i32:
1058
#if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1059
        c = ARITH_SDIVX;
1060
        goto gen_arith32;
1061
#else
1062
        tcg_out_sety(s, 0);
1063
        c = ARITH_SDIV;
1064
        goto gen_arith32;
1065
#endif
1066
    case INDEX_op_divu2_i32:
1067
#if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1068
        c = ARITH_UDIVX;
1069
        goto gen_arith32;
1070
#else
1071
        tcg_out_sety(s, 0);
1072
        c = ARITH_UDIV;
1073
        goto gen_arith32;
1074
#endif
1075

    
1076
    case INDEX_op_brcond_i32:
1077
        tcg_out_brcond_i32(s, args[2], args[0], args[1], const_args[1],
1078
                           args[3]);
1079
        break;
1080

    
1081
    case INDEX_op_qemu_ld8u:
1082
        tcg_out_qemu_ld(s, args, 0);
1083
        break;
1084
    case INDEX_op_qemu_ld8s:
1085
        tcg_out_qemu_ld(s, args, 0 | 4);
1086
        break;
1087
    case INDEX_op_qemu_ld16u:
1088
        tcg_out_qemu_ld(s, args, 1);
1089
        break;
1090
    case INDEX_op_qemu_ld16s:
1091
        tcg_out_qemu_ld(s, args, 1 | 4);
1092
        break;
1093
    case INDEX_op_qemu_ld32u:
1094
        tcg_out_qemu_ld(s, args, 2);
1095
        break;
1096
    case INDEX_op_qemu_ld32s:
1097
        tcg_out_qemu_ld(s, args, 2 | 4);
1098
        break;
1099
    case INDEX_op_qemu_st8:
1100
        tcg_out_qemu_st(s, args, 0);
1101
        break;
1102
    case INDEX_op_qemu_st16:
1103
        tcg_out_qemu_st(s, args, 1);
1104
        break;
1105
    case INDEX_op_qemu_st32:
1106
        tcg_out_qemu_st(s, args, 2);
1107
        break;
1108

    
1109
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1110
    case INDEX_op_movi_i64:
1111
        tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1112
        break;
1113
    case INDEX_op_ld32s_i64:
1114
        tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
1115
        break;
1116
    case INDEX_op_ld_i64:
1117
        tcg_out_ldst(s, args[0], args[1], args[2], LDX);
1118
        break;
1119
    case INDEX_op_st_i64:
1120
        tcg_out_ldst(s, args[0], args[1], args[2], STX);
1121
        break;
1122
    case INDEX_op_shl_i64:
1123
        c = SHIFT_SLLX;
1124
        goto gen_arith32;
1125
    case INDEX_op_shr_i64:
1126
        c = SHIFT_SRLX;
1127
        goto gen_arith32;
1128
    case INDEX_op_sar_i64:
1129
        c = SHIFT_SRAX;
1130
        goto gen_arith32;
1131
    case INDEX_op_mul_i64:
1132
        c = ARITH_MULX;
1133
        goto gen_arith32;
1134
    case INDEX_op_div2_i64:
1135
        c = ARITH_SDIVX;
1136
        goto gen_arith32;
1137
    case INDEX_op_divu2_i64:
1138
        c = ARITH_UDIVX;
1139
        goto gen_arith32;
1140

    
1141
    case INDEX_op_brcond_i64:
1142
        tcg_out_brcond_i64(s, args[2], args[0], args[1], const_args[1],
1143
                           args[3]);
1144
        break;
1145
    case INDEX_op_qemu_ld64:
1146
        tcg_out_qemu_ld(s, args, 3);
1147
        break;
1148
    case INDEX_op_qemu_st64:
1149
        tcg_out_qemu_st(s, args, 3);
1150
        break;
1151

    
1152
#endif
1153
    gen_arith32:
1154
        if (const_args[2]) {
1155
            tcg_out_arithi(s, args[0], args[1], args[2], c);
1156
        } else {
1157
            tcg_out_arith(s, args[0], args[1], args[2], c);
1158
        }
1159
        break;
1160

    
1161
    default:
1162
        fprintf(stderr, "unknown opcode 0x%x\n", opc);
1163
        tcg_abort();
1164
    }
1165
}
1166

    
1167
static const TCGTargetOpDef sparc_op_defs[] = {
1168
    { INDEX_op_exit_tb, { } },
1169
    { INDEX_op_goto_tb, { } },
1170
    { INDEX_op_call, { "ri" } },
1171
    { INDEX_op_jmp, { "ri" } },
1172
    { INDEX_op_br, { } },
1173

    
1174
    { INDEX_op_mov_i32, { "r", "r" } },
1175
    { INDEX_op_movi_i32, { "r" } },
1176
    { INDEX_op_ld8u_i32, { "r", "r" } },
1177
    { INDEX_op_ld8s_i32, { "r", "r" } },
1178
    { INDEX_op_ld16u_i32, { "r", "r" } },
1179
    { INDEX_op_ld16s_i32, { "r", "r" } },
1180
    { INDEX_op_ld_i32, { "r", "r" } },
1181
    { INDEX_op_st8_i32, { "r", "r" } },
1182
    { INDEX_op_st16_i32, { "r", "r" } },
1183
    { INDEX_op_st_i32, { "r", "r" } },
1184

    
1185
    { INDEX_op_add_i32, { "r", "r", "rJ" } },
1186
    { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1187
    { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1188
    { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1189
    { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1190
    { INDEX_op_and_i32, { "r", "r", "rJ" } },
1191
    { INDEX_op_or_i32, { "r", "r", "rJ" } },
1192
    { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1193

    
1194
    { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1195
    { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1196
    { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1197

    
1198
    { INDEX_op_brcond_i32, { "r", "ri" } },
1199

    
1200
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1201
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1202
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1203
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1204
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1205
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1206

    
1207
    { INDEX_op_qemu_st8, { "L", "L" } },
1208
    { INDEX_op_qemu_st16, { "L", "L" } },
1209
    { INDEX_op_qemu_st32, { "L", "L" } },
1210

    
1211
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1212
    { INDEX_op_mov_i64, { "r", "r" } },
1213
    { INDEX_op_movi_i64, { "r" } },
1214
    { INDEX_op_ld8u_i64, { "r", "r" } },
1215
    { INDEX_op_ld8s_i64, { "r", "r" } },
1216
    { INDEX_op_ld16u_i64, { "r", "r" } },
1217
    { INDEX_op_ld16s_i64, { "r", "r" } },
1218
    { INDEX_op_ld32u_i64, { "r", "r" } },
1219
    { INDEX_op_ld32s_i64, { "r", "r" } },
1220
    { INDEX_op_ld_i64, { "r", "r" } },
1221
    { INDEX_op_st8_i64, { "r", "r" } },
1222
    { INDEX_op_st16_i64, { "r", "r" } },
1223
    { INDEX_op_st32_i64, { "r", "r" } },
1224
    { INDEX_op_st_i64, { "r", "r" } },
1225
    { INDEX_op_qemu_ld64, { "L", "L" } },
1226
    { INDEX_op_qemu_st64, { "L", "L" } },
1227

    
1228
    { INDEX_op_add_i64, { "r", "r", "rJ" } },
1229
    { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1230
    { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1231
    { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1232
    { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1233
    { INDEX_op_and_i64, { "r", "r", "rJ" } },
1234
    { INDEX_op_or_i64, { "r", "r", "rJ" } },
1235
    { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1236

    
1237
    { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1238
    { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1239
    { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1240

    
1241
    { INDEX_op_brcond_i64, { "r", "ri" } },
1242
#endif
1243
    { -1 },
1244
};
1245

    
1246
void tcg_target_init(TCGContext *s)
1247
{
1248
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1249
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1250
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1251
#endif
1252
    tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1253
                     (1 << TCG_REG_G1) |
1254
                     (1 << TCG_REG_G2) |
1255
                     (1 << TCG_REG_G3) |
1256
                     (1 << TCG_REG_G4) |
1257
                     (1 << TCG_REG_G5) |
1258
                     (1 << TCG_REG_G6) |
1259
                     (1 << TCG_REG_G7) |
1260
                     (1 << TCG_REG_O0) |
1261
                     (1 << TCG_REG_O1) |
1262
                     (1 << TCG_REG_O2) |
1263
                     (1 << TCG_REG_O3) |
1264
                     (1 << TCG_REG_O4) |
1265
                     (1 << TCG_REG_O5) |
1266
                     (1 << TCG_REG_O7));
1267

    
1268
    tcg_regset_clear(s->reserved_regs);
1269
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1270
#if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1271
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I4); // for internal use
1272
#endif
1273
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1274
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1275
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1276
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1277
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1278
    tcg_add_target_add_op_defs(sparc_op_defs);
1279
}