xref: /qemu/tcg/sparc64/tcg-target.c.inc (revision ec6f3fc3)
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/* We only support generating code for 64-bit mode.  */
26#ifndef __arch64__
27#error "unsupported code generation mode"
28#endif
29
30#include "../tcg-ldst.c.inc"
31#include "../tcg-pool.c.inc"
32
33#ifdef CONFIG_DEBUG_TCG
34static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
35    "%g0",
36    "%g1",
37    "%g2",
38    "%g3",
39    "%g4",
40    "%g5",
41    "%g6",
42    "%g7",
43    "%o0",
44    "%o1",
45    "%o2",
46    "%o3",
47    "%o4",
48    "%o5",
49    "%o6",
50    "%o7",
51    "%l0",
52    "%l1",
53    "%l2",
54    "%l3",
55    "%l4",
56    "%l5",
57    "%l6",
58    "%l7",
59    "%i0",
60    "%i1",
61    "%i2",
62    "%i3",
63    "%i4",
64    "%i5",
65    "%i6",
66    "%i7",
67};
68#endif
69
70#define TCG_CT_CONST_S11  0x100
71#define TCG_CT_CONST_S13  0x200
72#define TCG_CT_CONST_ZERO 0x400
73
74#define ALL_GENERAL_REGS  MAKE_64BIT_MASK(0, 32)
75
76/* Define some temporary registers.  T3 is used for constant generation.  */
77#define TCG_REG_T1  TCG_REG_G1
78#define TCG_REG_T2  TCG_REG_G2
79#define TCG_REG_T3  TCG_REG_O7
80
81#ifndef CONFIG_SOFTMMU
82# define TCG_GUEST_BASE_REG TCG_REG_I5
83#endif
84
85#define TCG_REG_TB  TCG_REG_I1
86
87static const int tcg_target_reg_alloc_order[] = {
88    TCG_REG_L0,
89    TCG_REG_L1,
90    TCG_REG_L2,
91    TCG_REG_L3,
92    TCG_REG_L4,
93    TCG_REG_L5,
94    TCG_REG_L6,
95    TCG_REG_L7,
96
97    TCG_REG_I0,
98    TCG_REG_I1,
99    TCG_REG_I2,
100    TCG_REG_I3,
101    TCG_REG_I4,
102    TCG_REG_I5,
103
104    TCG_REG_G3,
105    TCG_REG_G4,
106    TCG_REG_G5,
107
108    TCG_REG_O0,
109    TCG_REG_O1,
110    TCG_REG_O2,
111    TCG_REG_O3,
112    TCG_REG_O4,
113    TCG_REG_O5,
114};
115
116static const int tcg_target_call_iarg_regs[6] = {
117    TCG_REG_O0,
118    TCG_REG_O1,
119    TCG_REG_O2,
120    TCG_REG_O3,
121    TCG_REG_O4,
122    TCG_REG_O5,
123};
124
125static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
126{
127    tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
128    tcg_debug_assert(slot >= 0 && slot <= 3);
129    return TCG_REG_O0 + slot;
130}
131
132#define INSN_OP(x)  ((x) << 30)
133#define INSN_OP2(x) ((x) << 22)
134#define INSN_OP3(x) ((x) << 19)
135#define INSN_OPF(x) ((x) << 5)
136#define INSN_RD(x)  ((x) << 25)
137#define INSN_RS1(x) ((x) << 14)
138#define INSN_RS2(x) (x)
139#define INSN_ASI(x) ((x) << 5)
140
141#define INSN_IMM10(x) ((1 << 13) | ((x) & 0x3ff))
142#define INSN_IMM11(x) ((1 << 13) | ((x) & 0x7ff))
143#define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
144#define INSN_OFF16(x) ((((x) >> 2) & 0x3fff) | ((((x) >> 16) & 3) << 20))
145#define INSN_OFF19(x) (((x) >> 2) & 0x07ffff)
146#define INSN_COND(x) ((x) << 25)
147
148#define COND_N     0x0
149#define COND_E     0x1
150#define COND_LE    0x2
151#define COND_L     0x3
152#define COND_LEU   0x4
153#define COND_CS    0x5
154#define COND_NEG   0x6
155#define COND_VS    0x7
156#define COND_A     0x8
157#define COND_NE    0x9
158#define COND_G     0xa
159#define COND_GE    0xb
160#define COND_GU    0xc
161#define COND_CC    0xd
162#define COND_POS   0xe
163#define COND_VC    0xf
164#define BA         (INSN_OP(0) | INSN_COND(COND_A) | INSN_OP2(0x2))
165
166#define RCOND_Z    1
167#define RCOND_LEZ  2
168#define RCOND_LZ   3
169#define RCOND_NZ   5
170#define RCOND_GZ   6
171#define RCOND_GEZ  7
172
173#define MOVCC_ICC  (1 << 18)
174#define MOVCC_XCC  (1 << 18 | 1 << 12)
175
176#define BPCC_ICC   0
177#define BPCC_XCC   (2 << 20)
178#define BPCC_PT    (1 << 19)
179#define BPCC_PN    0
180#define BPCC_A     (1 << 29)
181
182#define BPR_PT     BPCC_PT
183
184#define ARITH_ADD  (INSN_OP(2) | INSN_OP3(0x00))
185#define ARITH_ADDCC (INSN_OP(2) | INSN_OP3(0x10))
186#define ARITH_AND  (INSN_OP(2) | INSN_OP3(0x01))
187#define ARITH_ANDCC (INSN_OP(2) | INSN_OP3(0x11))
188#define ARITH_ANDN (INSN_OP(2) | INSN_OP3(0x05))
189#define ARITH_OR   (INSN_OP(2) | INSN_OP3(0x02))
190#define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
191#define ARITH_ORN  (INSN_OP(2) | INSN_OP3(0x06))
192#define ARITH_XOR  (INSN_OP(2) | INSN_OP3(0x03))
193#define ARITH_SUB  (INSN_OP(2) | INSN_OP3(0x04))
194#define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
195#define ARITH_ADDC (INSN_OP(2) | INSN_OP3(0x08))
196#define ARITH_SUBC (INSN_OP(2) | INSN_OP3(0x0c))
197#define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
198#define ARITH_SMUL (INSN_OP(2) | INSN_OP3(0x0b))
199#define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
200#define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
201#define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
202#define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
203#define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
204#define ARITH_MOVCC (INSN_OP(2) | INSN_OP3(0x2c))
205#define ARITH_MOVR (INSN_OP(2) | INSN_OP3(0x2f))
206
207#define ARITH_ADDXC (INSN_OP(2) | INSN_OP3(0x36) | INSN_OPF(0x11))
208#define ARITH_UMULXHI (INSN_OP(2) | INSN_OP3(0x36) | INSN_OPF(0x16))
209
210#define SHIFT_SLL  (INSN_OP(2) | INSN_OP3(0x25))
211#define SHIFT_SRL  (INSN_OP(2) | INSN_OP3(0x26))
212#define SHIFT_SRA  (INSN_OP(2) | INSN_OP3(0x27))
213
214#define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
215#define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
216#define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
217
218#define RDY        (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(0))
219#define WRY        (INSN_OP(2) | INSN_OP3(0x30) | INSN_RD(0))
220#define JMPL       (INSN_OP(2) | INSN_OP3(0x38))
221#define RETURN     (INSN_OP(2) | INSN_OP3(0x39))
222#define SAVE       (INSN_OP(2) | INSN_OP3(0x3c))
223#define RESTORE    (INSN_OP(2) | INSN_OP3(0x3d))
224#define SETHI      (INSN_OP(0) | INSN_OP2(0x4))
225#define CALL       INSN_OP(1)
226#define LDUB       (INSN_OP(3) | INSN_OP3(0x01))
227#define LDSB       (INSN_OP(3) | INSN_OP3(0x09))
228#define LDUH       (INSN_OP(3) | INSN_OP3(0x02))
229#define LDSH       (INSN_OP(3) | INSN_OP3(0x0a))
230#define LDUW       (INSN_OP(3) | INSN_OP3(0x00))
231#define LDSW       (INSN_OP(3) | INSN_OP3(0x08))
232#define LDX        (INSN_OP(3) | INSN_OP3(0x0b))
233#define STB        (INSN_OP(3) | INSN_OP3(0x05))
234#define STH        (INSN_OP(3) | INSN_OP3(0x06))
235#define STW        (INSN_OP(3) | INSN_OP3(0x04))
236#define STX        (INSN_OP(3) | INSN_OP3(0x0e))
237#define LDUBA      (INSN_OP(3) | INSN_OP3(0x11))
238#define LDSBA      (INSN_OP(3) | INSN_OP3(0x19))
239#define LDUHA      (INSN_OP(3) | INSN_OP3(0x12))
240#define LDSHA      (INSN_OP(3) | INSN_OP3(0x1a))
241#define LDUWA      (INSN_OP(3) | INSN_OP3(0x10))
242#define LDSWA      (INSN_OP(3) | INSN_OP3(0x18))
243#define LDXA       (INSN_OP(3) | INSN_OP3(0x1b))
244#define STBA       (INSN_OP(3) | INSN_OP3(0x15))
245#define STHA       (INSN_OP(3) | INSN_OP3(0x16))
246#define STWA       (INSN_OP(3) | INSN_OP3(0x14))
247#define STXA       (INSN_OP(3) | INSN_OP3(0x1e))
248
249#define MEMBAR     (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(15) | (1 << 13))
250
251#define NOP        (SETHI | INSN_RD(TCG_REG_G0) | 0)
252
253#ifndef ASI_PRIMARY_LITTLE
254#define ASI_PRIMARY_LITTLE 0x88
255#endif
256
257#define LDUH_LE    (LDUHA | INSN_ASI(ASI_PRIMARY_LITTLE))
258#define LDSH_LE    (LDSHA | INSN_ASI(ASI_PRIMARY_LITTLE))
259#define LDUW_LE    (LDUWA | INSN_ASI(ASI_PRIMARY_LITTLE))
260#define LDSW_LE    (LDSWA | INSN_ASI(ASI_PRIMARY_LITTLE))
261#define LDX_LE     (LDXA  | INSN_ASI(ASI_PRIMARY_LITTLE))
262
263#define STH_LE     (STHA  | INSN_ASI(ASI_PRIMARY_LITTLE))
264#define STW_LE     (STWA  | INSN_ASI(ASI_PRIMARY_LITTLE))
265#define STX_LE     (STXA  | INSN_ASI(ASI_PRIMARY_LITTLE))
266
267#ifndef use_vis3_instructions
268bool use_vis3_instructions;
269#endif
270
271static bool check_fit_i64(int64_t val, unsigned int bits)
272{
273    return val == sextract64(val, 0, bits);
274}
275
276static bool check_fit_i32(int32_t val, unsigned int bits)
277{
278    return val == sextract32(val, 0, bits);
279}
280
281#define check_fit_tl    check_fit_i64
282#define check_fit_ptr   check_fit_i64
283
284static bool patch_reloc(tcg_insn_unit *src_rw, int type,
285                        intptr_t value, intptr_t addend)
286{
287    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
288    uint32_t insn = *src_rw;
289    intptr_t pcrel;
290
291    value += addend;
292    pcrel = tcg_ptr_byte_diff((tcg_insn_unit *)value, src_rx);
293
294    switch (type) {
295    case R_SPARC_WDISP16:
296        if (!check_fit_ptr(pcrel >> 2, 16)) {
297            return false;
298        }
299        insn &= ~INSN_OFF16(-1);
300        insn |= INSN_OFF16(pcrel);
301        break;
302    case R_SPARC_WDISP19:
303        if (!check_fit_ptr(pcrel >> 2, 19)) {
304            return false;
305        }
306        insn &= ~INSN_OFF19(-1);
307        insn |= INSN_OFF19(pcrel);
308        break;
309    case R_SPARC_13:
310        if (!check_fit_ptr(value, 13)) {
311            return false;
312        }
313        insn &= ~INSN_IMM13(-1);
314        insn |= INSN_IMM13(value);
315        break;
316    default:
317        g_assert_not_reached();
318    }
319
320    *src_rw = insn;
321    return true;
322}
323
324/* test if a constant matches the constraint */
325static bool tcg_target_const_match(int64_t val, TCGType type, int ct, int vece)
326{
327    if (ct & TCG_CT_CONST) {
328        return 1;
329    }
330
331    if (type == TCG_TYPE_I32) {
332        val = (int32_t)val;
333    }
334
335    if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
336        return 1;
337    } else if ((ct & TCG_CT_CONST_S11) && check_fit_tl(val, 11)) {
338        return 1;
339    } else if ((ct & TCG_CT_CONST_S13) && check_fit_tl(val, 13)) {
340        return 1;
341    } else {
342        return 0;
343    }
344}
345
346static void tcg_out_nop(TCGContext *s)
347{
348    tcg_out32(s, NOP);
349}
350
351static void tcg_out_arith(TCGContext *s, TCGReg rd, TCGReg rs1,
352                          TCGReg rs2, int op)
353{
354    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) | INSN_RS2(rs2));
355}
356
357static void tcg_out_arithi(TCGContext *s, TCGReg rd, TCGReg rs1,
358                           int32_t offset, int op)
359{
360    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) | INSN_IMM13(offset));
361}
362
363static void tcg_out_arithc(TCGContext *s, TCGReg rd, TCGReg rs1,
364			   int32_t val2, int val2const, int op)
365{
366    tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1)
367              | (val2const ? INSN_IMM13(val2) : INSN_RS2(val2)));
368}
369
370static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
371{
372    if (ret != arg) {
373        tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
374    }
375    return true;
376}
377
378static void tcg_out_mov_delay(TCGContext *s, TCGReg ret, TCGReg arg)
379{
380    if (ret != arg) {
381        tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
382    } else {
383        tcg_out_nop(s);
384    }
385}
386
387static void tcg_out_sethi(TCGContext *s, TCGReg ret, uint32_t arg)
388{
389    tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
390}
391
392/* A 13-bit constant sign-extended to 64 bits.  */
393static void tcg_out_movi_s13(TCGContext *s, TCGReg ret, int32_t arg)
394{
395    tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
396}
397
398/* A 32-bit constant sign-extended to 64 bits.  */
399static void tcg_out_movi_s32(TCGContext *s, TCGReg ret, int32_t arg)
400{
401    tcg_out_sethi(s, ret, ~arg);
402    tcg_out_arithi(s, ret, ret, (arg & 0x3ff) | -0x400, ARITH_XOR);
403}
404
405/* A 32-bit constant zero-extended to 64 bits.  */
406static void tcg_out_movi_u32(TCGContext *s, TCGReg ret, uint32_t arg)
407{
408    tcg_out_sethi(s, ret, arg);
409    if (arg & 0x3ff) {
410        tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
411    }
412}
413
414static void tcg_out_movi_int(TCGContext *s, TCGType type, TCGReg ret,
415                             tcg_target_long arg, bool in_prologue,
416                             TCGReg scratch)
417{
418    tcg_target_long hi, lo = (int32_t)arg;
419    tcg_target_long test, lsb;
420
421    /* A 13-bit constant sign-extended to 64-bits.  */
422    if (check_fit_tl(arg, 13)) {
423        tcg_out_movi_s13(s, ret, arg);
424        return;
425    }
426
427    /* A 32-bit constant, or 32-bit zero-extended to 64-bits.  */
428    if (type == TCG_TYPE_I32 || arg == (uint32_t)arg) {
429        tcg_out_movi_u32(s, ret, arg);
430        return;
431    }
432
433    /* A 13-bit constant relative to the TB.  */
434    if (!in_prologue) {
435        test = tcg_tbrel_diff(s, (void *)arg);
436        if (check_fit_ptr(test, 13)) {
437            tcg_out_arithi(s, ret, TCG_REG_TB, test, ARITH_ADD);
438            return;
439        }
440    }
441
442    /* A 32-bit constant sign-extended to 64-bits.  */
443    if (arg == lo) {
444        tcg_out_movi_s32(s, ret, arg);
445        return;
446    }
447
448    /* A 32-bit constant, shifted.  */
449    lsb = ctz64(arg);
450    test = (tcg_target_long)arg >> lsb;
451    if (lsb > 10 && test == extract64(test, 0, 21)) {
452        tcg_out_sethi(s, ret, test << 10);
453        tcg_out_arithi(s, ret, ret, lsb - 10, SHIFT_SLLX);
454        return;
455    } else if (test == (uint32_t)test || test == (int32_t)test) {
456        tcg_out_movi_int(s, TCG_TYPE_I64, ret, test, in_prologue, scratch);
457        tcg_out_arithi(s, ret, ret, lsb, SHIFT_SLLX);
458        return;
459    }
460
461    /* Use the constant pool, if possible. */
462    if (!in_prologue) {
463        new_pool_label(s, arg, R_SPARC_13, s->code_ptr,
464                       tcg_tbrel_diff(s, NULL));
465        tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(TCG_REG_TB));
466        return;
467    }
468
469    /* A 64-bit constant decomposed into 2 32-bit pieces.  */
470    if (check_fit_i32(lo, 13)) {
471        hi = (arg - lo) >> 32;
472        tcg_out_movi_u32(s, ret, hi);
473        tcg_out_arithi(s, ret, ret, 32, SHIFT_SLLX);
474        tcg_out_arithi(s, ret, ret, lo, ARITH_ADD);
475    } else {
476        hi = arg >> 32;
477        tcg_out_movi_u32(s, ret, hi);
478        tcg_out_movi_u32(s, scratch, lo);
479        tcg_out_arithi(s, ret, ret, 32, SHIFT_SLLX);
480        tcg_out_arith(s, ret, ret, scratch, ARITH_OR);
481    }
482}
483
484static void tcg_out_movi(TCGContext *s, TCGType type,
485                         TCGReg ret, tcg_target_long arg)
486{
487    tcg_debug_assert(ret != TCG_REG_T3);
488    tcg_out_movi_int(s, type, ret, arg, false, TCG_REG_T3);
489}
490
491static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
492{
493    g_assert_not_reached();
494}
495
496static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
497{
498    g_assert_not_reached();
499}
500
501static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs)
502{
503    tcg_out_arithi(s, rd, rs, 0xff, ARITH_AND);
504}
505
506static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs)
507{
508    tcg_out_arithi(s, rd, rs, 16, SHIFT_SLL);
509    tcg_out_arithi(s, rd, rd, 16, SHIFT_SRL);
510}
511
512static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs)
513{
514    tcg_out_arithi(s, rd, rs, 0, SHIFT_SRA);
515}
516
517static void tcg_out_ext32u(TCGContext *s, TCGReg rd, TCGReg rs)
518{
519    tcg_out_arithi(s, rd, rs, 0, SHIFT_SRL);
520}
521
522static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
523{
524    tcg_out_ext32s(s, rd, rs);
525}
526
527static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
528{
529    tcg_out_ext32u(s, rd, rs);
530}
531
532static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs)
533{
534    tcg_out_ext32u(s, rd, rs);
535}
536
537static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
538{
539    return false;
540}
541
542static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
543                             tcg_target_long imm)
544{
545    /* This function is only used for passing structs by reference. */
546    g_assert_not_reached();
547}
548
549static void tcg_out_ldst_rr(TCGContext *s, TCGReg data, TCGReg a1,
550                            TCGReg a2, int op)
551{
552    tcg_out32(s, op | INSN_RD(data) | INSN_RS1(a1) | INSN_RS2(a2));
553}
554
555static void tcg_out_ldst(TCGContext *s, TCGReg ret, TCGReg addr,
556                         intptr_t offset, int op)
557{
558    if (check_fit_ptr(offset, 13)) {
559        tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
560                  INSN_IMM13(offset));
561    } else {
562        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T1, offset);
563        tcg_out_ldst_rr(s, ret, addr, TCG_REG_T1, op);
564    }
565}
566
567static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
568                       TCGReg arg1, intptr_t arg2)
569{
570    tcg_out_ldst(s, ret, arg1, arg2, (type == TCG_TYPE_I32 ? LDUW : LDX));
571}
572
573static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
574                       TCGReg arg1, intptr_t arg2)
575{
576    tcg_out_ldst(s, arg, arg1, arg2, (type == TCG_TYPE_I32 ? STW : STX));
577}
578
579static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
580                        TCGReg base, intptr_t ofs)
581{
582    if (val == 0) {
583        tcg_out_st(s, type, TCG_REG_G0, base, ofs);
584        return true;
585    }
586    return false;
587}
588
589static void tcg_out_sety(TCGContext *s, TCGReg rs)
590{
591    tcg_out32(s, WRY | INSN_RS1(TCG_REG_G0) | INSN_RS2(rs));
592}
593
594static void tcg_out_div32(TCGContext *s, TCGReg rd, TCGReg rs1,
595                          int32_t val2, int val2const, int uns)
596{
597    /* Load Y with the sign/zero extension of RS1 to 64-bits.  */
598    if (uns) {
599        tcg_out_sety(s, TCG_REG_G0);
600    } else {
601        tcg_out_arithi(s, TCG_REG_T1, rs1, 31, SHIFT_SRA);
602        tcg_out_sety(s, TCG_REG_T1);
603    }
604
605    tcg_out_arithc(s, rd, rs1, val2, val2const,
606                   uns ? ARITH_UDIV : ARITH_SDIV);
607}
608
609static const uint8_t tcg_cond_to_bcond[] = {
610    [TCG_COND_EQ] = COND_E,
611    [TCG_COND_NE] = COND_NE,
612    [TCG_COND_LT] = COND_L,
613    [TCG_COND_GE] = COND_GE,
614    [TCG_COND_LE] = COND_LE,
615    [TCG_COND_GT] = COND_G,
616    [TCG_COND_LTU] = COND_CS,
617    [TCG_COND_GEU] = COND_CC,
618    [TCG_COND_LEU] = COND_LEU,
619    [TCG_COND_GTU] = COND_GU,
620};
621
622static const uint8_t tcg_cond_to_rcond[] = {
623    [TCG_COND_EQ] = RCOND_Z,
624    [TCG_COND_NE] = RCOND_NZ,
625    [TCG_COND_LT] = RCOND_LZ,
626    [TCG_COND_GT] = RCOND_GZ,
627    [TCG_COND_LE] = RCOND_LEZ,
628    [TCG_COND_GE] = RCOND_GEZ
629};
630
631static void tcg_out_bpcc0(TCGContext *s, int scond, int flags, int off19)
632{
633    tcg_out32(s, INSN_OP(0) | INSN_OP2(1) | INSN_COND(scond) | flags | off19);
634}
635
636static void tcg_out_bpcc(TCGContext *s, int scond, int flags, TCGLabel *l)
637{
638    int off19 = 0;
639
640    if (l->has_value) {
641        off19 = INSN_OFF19(tcg_pcrel_diff(s, l->u.value_ptr));
642    } else {
643        tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP19, l, 0);
644    }
645    tcg_out_bpcc0(s, scond, flags, off19);
646}
647
648static void tcg_out_cmp(TCGContext *s, TCGReg c1, int32_t c2, int c2const)
649{
650    tcg_out_arithc(s, TCG_REG_G0, c1, c2, c2const, ARITH_SUBCC);
651}
652
653static void tcg_out_brcond_i32(TCGContext *s, TCGCond cond, TCGReg arg1,
654                               int32_t arg2, int const_arg2, TCGLabel *l)
655{
656    tcg_out_cmp(s, arg1, arg2, const_arg2);
657    tcg_out_bpcc(s, tcg_cond_to_bcond[cond], BPCC_ICC | BPCC_PT, l);
658    tcg_out_nop(s);
659}
660
661static void tcg_out_movcc(TCGContext *s, TCGCond cond, int cc, TCGReg ret,
662                          int32_t v1, int v1const)
663{
664    tcg_out32(s, ARITH_MOVCC | cc | INSN_RD(ret)
665              | INSN_RS1(tcg_cond_to_bcond[cond])
666              | (v1const ? INSN_IMM11(v1) : INSN_RS2(v1)));
667}
668
669static void tcg_out_movcond_i32(TCGContext *s, TCGCond cond, TCGReg ret,
670                                TCGReg c1, int32_t c2, int c2const,
671                                int32_t v1, int v1const)
672{
673    tcg_out_cmp(s, c1, c2, c2const);
674    tcg_out_movcc(s, cond, MOVCC_ICC, ret, v1, v1const);
675}
676
677static void tcg_out_brcond_i64(TCGContext *s, TCGCond cond, TCGReg arg1,
678                               int32_t arg2, int const_arg2, TCGLabel *l)
679{
680    /* For 64-bit signed comparisons vs zero, we can avoid the compare.  */
681    if (arg2 == 0 && !is_unsigned_cond(cond)) {
682        int off16 = 0;
683
684        if (l->has_value) {
685            off16 = INSN_OFF16(tcg_pcrel_diff(s, l->u.value_ptr));
686        } else {
687            tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP16, l, 0);
688        }
689        tcg_out32(s, INSN_OP(0) | INSN_OP2(3) | BPR_PT | INSN_RS1(arg1)
690                  | INSN_COND(tcg_cond_to_rcond[cond]) | off16);
691    } else {
692        tcg_out_cmp(s, arg1, arg2, const_arg2);
693        tcg_out_bpcc(s, tcg_cond_to_bcond[cond], BPCC_XCC | BPCC_PT, l);
694    }
695    tcg_out_nop(s);
696}
697
698static void tcg_out_movr(TCGContext *s, TCGCond cond, TCGReg ret, TCGReg c1,
699                         int32_t v1, int v1const)
700{
701    tcg_out32(s, ARITH_MOVR | INSN_RD(ret) | INSN_RS1(c1)
702              | (tcg_cond_to_rcond[cond] << 10)
703              | (v1const ? INSN_IMM10(v1) : INSN_RS2(v1)));
704}
705
706static void tcg_out_movcond_i64(TCGContext *s, TCGCond cond, TCGReg ret,
707                                TCGReg c1, int32_t c2, int c2const,
708                                int32_t v1, int v1const)
709{
710    /* For 64-bit signed comparisons vs zero, we can avoid the compare.
711       Note that the immediate range is one bit smaller, so we must check
712       for that as well.  */
713    if (c2 == 0 && !is_unsigned_cond(cond)
714        && (!v1const || check_fit_i32(v1, 10))) {
715        tcg_out_movr(s, cond, ret, c1, v1, v1const);
716    } else {
717        tcg_out_cmp(s, c1, c2, c2const);
718        tcg_out_movcc(s, cond, MOVCC_XCC, ret, v1, v1const);
719    }
720}
721
722static void tcg_out_setcond_i32(TCGContext *s, TCGCond cond, TCGReg ret,
723                                TCGReg c1, int32_t c2, int c2const, bool neg)
724{
725    /* For 32-bit comparisons, we can play games with ADDC/SUBC.  */
726    switch (cond) {
727    case TCG_COND_LTU:
728    case TCG_COND_GEU:
729        /* The result of the comparison is in the carry bit.  */
730        break;
731
732    case TCG_COND_EQ:
733    case TCG_COND_NE:
734        /* For equality, we can transform to inequality vs zero.  */
735        if (c2 != 0) {
736            tcg_out_arithc(s, TCG_REG_T1, c1, c2, c2const, ARITH_XOR);
737            c2 = TCG_REG_T1;
738        } else {
739            c2 = c1;
740        }
741        c1 = TCG_REG_G0, c2const = 0;
742        cond = (cond == TCG_COND_EQ ? TCG_COND_GEU : TCG_COND_LTU);
743	break;
744
745    case TCG_COND_GTU:
746    case TCG_COND_LEU:
747        /* If we don't need to load a constant into a register, we can
748           swap the operands on GTU/LEU.  There's no benefit to loading
749           the constant into a temporary register.  */
750        if (!c2const || c2 == 0) {
751            TCGReg t = c1;
752            c1 = c2;
753            c2 = t;
754            c2const = 0;
755            cond = tcg_swap_cond(cond);
756            break;
757        }
758        /* FALLTHRU */
759
760    default:
761        tcg_out_cmp(s, c1, c2, c2const);
762        tcg_out_movi_s13(s, ret, 0);
763        tcg_out_movcc(s, cond, MOVCC_ICC, ret, neg ? -1 : 1, 1);
764        return;
765    }
766
767    tcg_out_cmp(s, c1, c2, c2const);
768    if (cond == TCG_COND_LTU) {
769        if (neg) {
770            /* 0 - 0 - C = -C = (C ? -1 : 0) */
771            tcg_out_arithi(s, ret, TCG_REG_G0, 0, ARITH_SUBC);
772        } else {
773            /* 0 + 0 + C =  C = (C ? 1 : 0) */
774            tcg_out_arithi(s, ret, TCG_REG_G0, 0, ARITH_ADDC);
775        }
776    } else {
777        if (neg) {
778            /* 0 + -1 + C = C - 1 = (C ? 0 : -1) */
779            tcg_out_arithi(s, ret, TCG_REG_G0, -1, ARITH_ADDC);
780        } else {
781            /* 0 - -1 - C = 1 - C = (C ? 0 : 1) */
782            tcg_out_arithi(s, ret, TCG_REG_G0, -1, ARITH_SUBC);
783        }
784    }
785}
786
787static void tcg_out_setcond_i64(TCGContext *s, TCGCond cond, TCGReg ret,
788                                TCGReg c1, int32_t c2, int c2const, bool neg)
789{
790    if (use_vis3_instructions && !neg) {
791        switch (cond) {
792        case TCG_COND_NE:
793            if (c2 != 0) {
794                break;
795            }
796            c2 = c1, c2const = 0, c1 = TCG_REG_G0;
797            /* FALLTHRU */
798        case TCG_COND_LTU:
799            tcg_out_cmp(s, c1, c2, c2const);
800            tcg_out_arith(s, ret, TCG_REG_G0, TCG_REG_G0, ARITH_ADDXC);
801            return;
802        default:
803            break;
804        }
805    }
806
807    /* For 64-bit signed comparisons vs zero, we can avoid the compare
808       if the input does not overlap the output.  */
809    if (c2 == 0 && !is_unsigned_cond(cond) && c1 != ret) {
810        tcg_out_movi_s13(s, ret, 0);
811        tcg_out_movr(s, cond, ret, c1, neg ? -1 : 1, 1);
812    } else {
813        tcg_out_cmp(s, c1, c2, c2const);
814        tcg_out_movi_s13(s, ret, 0);
815        tcg_out_movcc(s, cond, MOVCC_XCC, ret, neg ? -1 : 1, 1);
816    }
817}
818
819static void tcg_out_addsub2_i32(TCGContext *s, TCGReg rl, TCGReg rh,
820                                TCGReg al, TCGReg ah, int32_t bl, int blconst,
821                                int32_t bh, int bhconst, int opl, int oph)
822{
823    TCGReg tmp = TCG_REG_T1;
824
825    /* Note that the low parts are fully consumed before tmp is set.  */
826    if (rl != ah && (bhconst || rl != bh)) {
827        tmp = rl;
828    }
829
830    tcg_out_arithc(s, tmp, al, bl, blconst, opl);
831    tcg_out_arithc(s, rh, ah, bh, bhconst, oph);
832    tcg_out_mov(s, TCG_TYPE_I32, rl, tmp);
833}
834
835static void tcg_out_addsub2_i64(TCGContext *s, TCGReg rl, TCGReg rh,
836                                TCGReg al, TCGReg ah, int32_t bl, int blconst,
837                                int32_t bh, int bhconst, bool is_sub)
838{
839    TCGReg tmp = TCG_REG_T1;
840
841    /* Note that the low parts are fully consumed before tmp is set.  */
842    if (rl != ah && (bhconst || rl != bh)) {
843        tmp = rl;
844    }
845
846    tcg_out_arithc(s, tmp, al, bl, blconst, is_sub ? ARITH_SUBCC : ARITH_ADDCC);
847
848    if (use_vis3_instructions && !is_sub) {
849        /* Note that ADDXC doesn't accept immediates.  */
850        if (bhconst && bh != 0) {
851           tcg_out_movi_s13(s, TCG_REG_T2, bh);
852           bh = TCG_REG_T2;
853        }
854        tcg_out_arith(s, rh, ah, bh, ARITH_ADDXC);
855    } else if (bh == TCG_REG_G0) {
856	/* If we have a zero, we can perform the operation in two insns,
857           with the arithmetic first, and a conditional move into place.  */
858	if (rh == ah) {
859            tcg_out_arithi(s, TCG_REG_T2, ah, 1,
860			   is_sub ? ARITH_SUB : ARITH_ADD);
861            tcg_out_movcc(s, TCG_COND_LTU, MOVCC_XCC, rh, TCG_REG_T2, 0);
862	} else {
863            tcg_out_arithi(s, rh, ah, 1, is_sub ? ARITH_SUB : ARITH_ADD);
864	    tcg_out_movcc(s, TCG_COND_GEU, MOVCC_XCC, rh, ah, 0);
865	}
866    } else {
867        /*
868         * Otherwise adjust BH as if there is carry into T2.
869         * Note that constant BH is constrained to 11 bits for the MOVCC,
870         * so the adjustment fits 12 bits.
871         */
872        if (bhconst) {
873            tcg_out_movi_s13(s, TCG_REG_T2, bh + (is_sub ? -1 : 1));
874        } else {
875            tcg_out_arithi(s, TCG_REG_T2, bh, 1,
876                           is_sub ? ARITH_SUB : ARITH_ADD);
877        }
878        /* ... smoosh T2 back to original BH if carry is clear ... */
879        tcg_out_movcc(s, TCG_COND_GEU, MOVCC_XCC, TCG_REG_T2, bh, bhconst);
880	/* ... and finally perform the arithmetic with the new operand.  */
881        tcg_out_arith(s, rh, ah, TCG_REG_T2, is_sub ? ARITH_SUB : ARITH_ADD);
882    }
883
884    tcg_out_mov(s, TCG_TYPE_I64, rl, tmp);
885}
886
887static void tcg_out_jmpl_const(TCGContext *s, const tcg_insn_unit *dest,
888                               bool in_prologue, bool tail_call)
889{
890    uintptr_t desti = (uintptr_t)dest;
891
892    tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_REG_T1,
893                     desti & ~0xfff, in_prologue, TCG_REG_T2);
894    tcg_out_arithi(s, tail_call ? TCG_REG_G0 : TCG_REG_O7,
895                   TCG_REG_T1, desti & 0xfff, JMPL);
896}
897
898static void tcg_out_call_nodelay(TCGContext *s, const tcg_insn_unit *dest,
899                                 bool in_prologue)
900{
901    ptrdiff_t disp = tcg_pcrel_diff(s, dest);
902
903    if (disp == (int32_t)disp) {
904        tcg_out32(s, CALL | (uint32_t)disp >> 2);
905    } else {
906        tcg_out_jmpl_const(s, dest, in_prologue, false);
907    }
908}
909
910static void tcg_out_call(TCGContext *s, const tcg_insn_unit *dest,
911                         const TCGHelperInfo *info)
912{
913    tcg_out_call_nodelay(s, dest, false);
914    tcg_out_nop(s);
915}
916
917static void tcg_out_mb(TCGContext *s, TCGArg a0)
918{
919    /* Note that the TCG memory order constants mirror the Sparc MEMBAR.  */
920    tcg_out32(s, MEMBAR | (a0 & TCG_MO_ALL));
921}
922
923/* Generate global QEMU prologue and epilogue code */
924static void tcg_target_qemu_prologue(TCGContext *s)
925{
926    int tmp_buf_size, frame_size;
927
928    /*
929     * The TCG temp buffer is at the top of the frame, immediately
930     * below the frame pointer.  Use the logical (aligned) offset here;
931     * the stack bias is applied in temp_allocate_frame().
932     */
933    tmp_buf_size = CPU_TEMP_BUF_NLONGS * (int)sizeof(long);
934    tcg_set_frame(s, TCG_REG_I6, -tmp_buf_size, tmp_buf_size);
935
936    /*
937     * TCG_TARGET_CALL_STACK_OFFSET includes the stack bias, but is
938     * otherwise the minimal frame usable by callees.
939     */
940    frame_size = TCG_TARGET_CALL_STACK_OFFSET - TCG_TARGET_STACK_BIAS;
941    frame_size += TCG_STATIC_CALL_ARGS_SIZE + tmp_buf_size;
942    frame_size += TCG_TARGET_STACK_ALIGN - 1;
943    frame_size &= -TCG_TARGET_STACK_ALIGN;
944    tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
945              INSN_IMM13(-frame_size));
946
947#ifndef CONFIG_SOFTMMU
948    if (guest_base != 0) {
949        tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG,
950                         guest_base, true, TCG_REG_T1);
951        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
952    }
953#endif
954
955    /* We choose TCG_REG_TB such that no move is required.  */
956    QEMU_BUILD_BUG_ON(TCG_REG_TB != TCG_REG_I1);
957    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB);
958
959    tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I1, 0, JMPL);
960    /* delay slot */
961    tcg_out_nop(s);
962
963    /* Epilogue for goto_ptr.  */
964    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
965    tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
966    /* delay slot */
967    tcg_out_movi_s13(s, TCG_REG_O0, 0);
968}
969
970static void tcg_out_tb_start(TCGContext *s)
971{
972    /* nothing to do */
973}
974
975static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
976{
977    int i;
978    for (i = 0; i < count; ++i) {
979        p[i] = NOP;
980    }
981}
982
983static const TCGLdstHelperParam ldst_helper_param = {
984    .ntmp = 1, .tmp = { TCG_REG_T1 }
985};
986
987static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
988{
989    MemOp opc = get_memop(lb->oi);
990    MemOp sgn;
991
992    if (!patch_reloc(lb->label_ptr[0], R_SPARC_WDISP19,
993                     (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 0)) {
994        return false;
995    }
996
997    /* Use inline tcg_out_ext32s; otherwise let the helper sign-extend. */
998    sgn = (opc & MO_SIZE) < MO_32 ? MO_SIGN : 0;
999
1000    tcg_out_ld_helper_args(s, lb, &ldst_helper_param);
1001    tcg_out_call(s, qemu_ld_helpers[opc & (MO_SIZE | sgn)], NULL);
1002    tcg_out_ld_helper_ret(s, lb, sgn, &ldst_helper_param);
1003
1004    tcg_out_bpcc0(s, COND_A, BPCC_A | BPCC_PT, 0);
1005    return patch_reloc(s->code_ptr - 1, R_SPARC_WDISP19,
1006                       (intptr_t)lb->raddr, 0);
1007}
1008
1009static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1010{
1011    MemOp opc = get_memop(lb->oi);
1012
1013    if (!patch_reloc(lb->label_ptr[0], R_SPARC_WDISP19,
1014                     (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 0)) {
1015        return false;
1016    }
1017
1018    tcg_out_st_helper_args(s, lb, &ldst_helper_param);
1019    tcg_out_call(s, qemu_st_helpers[opc & MO_SIZE], NULL);
1020
1021    tcg_out_bpcc0(s, COND_A, BPCC_A | BPCC_PT, 0);
1022    return patch_reloc(s->code_ptr - 1, R_SPARC_WDISP19,
1023                       (intptr_t)lb->raddr, 0);
1024}
1025
1026typedef struct {
1027    TCGReg base;
1028    TCGReg index;
1029    TCGAtomAlign aa;
1030} HostAddress;
1031
1032bool tcg_target_has_memory_bswap(MemOp memop)
1033{
1034    return true;
1035}
1036
1037/* We expect to use a 13-bit negative offset from ENV.  */
1038#define MIN_TLB_MASK_TABLE_OFS  -(1 << 12)
1039
1040/*
1041 * For system-mode, perform the TLB load and compare.
1042 * For user-mode, perform any required alignment tests.
1043 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1044 * is required and fill in @h with the host address for the fast path.
1045 */
1046static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1047                                           TCGReg addr_reg, MemOpIdx oi,
1048                                           bool is_ld)
1049{
1050    TCGType addr_type = s->addr_type;
1051    TCGLabelQemuLdst *ldst = NULL;
1052    MemOp opc = get_memop(oi);
1053    MemOp s_bits = opc & MO_SIZE;
1054    unsigned a_mask;
1055
1056    /* We don't support unaligned accesses. */
1057    h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
1058    h->aa.align = MAX(h->aa.align, s_bits);
1059    a_mask = (1u << h->aa.align) - 1;
1060
1061#ifdef CONFIG_SOFTMMU
1062    int mem_index = get_mmuidx(oi);
1063    int fast_off = tlb_mask_table_ofs(s, mem_index);
1064    int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1065    int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1066    int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1067                        : offsetof(CPUTLBEntry, addr_write);
1068    int add_off = offsetof(CPUTLBEntry, addend);
1069    int compare_mask;
1070    int cc;
1071
1072    /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx].  */
1073    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_T2, TCG_AREG0, mask_off);
1074    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_T3, TCG_AREG0, table_off);
1075
1076    /* Extract the page index, shifted into place for tlb index.  */
1077    tcg_out_arithi(s, TCG_REG_T1, addr_reg,
1078                   s->page_bits - CPU_TLB_ENTRY_BITS, SHIFT_SRL);
1079    tcg_out_arith(s, TCG_REG_T1, TCG_REG_T1, TCG_REG_T2, ARITH_AND);
1080
1081    /* Add the tlb_table pointer, creating the CPUTLBEntry address into R2.  */
1082    tcg_out_arith(s, TCG_REG_T1, TCG_REG_T1, TCG_REG_T3, ARITH_ADD);
1083
1084    /*
1085     * Load the tlb comparator and the addend.
1086     * Always load the entire 64-bit comparator for simplicity.
1087     * We will ignore the high bits via BPCC_ICC below.
1088     */
1089    tcg_out_ld(s, TCG_TYPE_I64, TCG_REG_T2, TCG_REG_T1, cmp_off);
1090    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_T1, TCG_REG_T1, add_off);
1091    h->base = TCG_REG_T1;
1092
1093    /* Mask out the page offset, except for the required alignment. */
1094    compare_mask = s->page_mask | a_mask;
1095    if (check_fit_tl(compare_mask, 13)) {
1096        tcg_out_arithi(s, TCG_REG_T3, addr_reg, compare_mask, ARITH_AND);
1097    } else {
1098        tcg_out_movi_s32(s, TCG_REG_T3, compare_mask);
1099        tcg_out_arith(s, TCG_REG_T3, addr_reg, TCG_REG_T3, ARITH_AND);
1100    }
1101    tcg_out_cmp(s, TCG_REG_T2, TCG_REG_T3, 0);
1102
1103    ldst = new_ldst_label(s);
1104    ldst->is_ld = is_ld;
1105    ldst->oi = oi;
1106    ldst->addrlo_reg = addr_reg;
1107    ldst->label_ptr[0] = s->code_ptr;
1108
1109    /* bne,pn %[xi]cc, label0 */
1110    cc = addr_type == TCG_TYPE_I32 ? BPCC_ICC : BPCC_XCC;
1111    tcg_out_bpcc0(s, COND_NE, BPCC_PN | cc, 0);
1112#else
1113    /*
1114     * If the size equals the required alignment, we can skip the test
1115     * and allow host SIGBUS to deliver SIGBUS to the guest.
1116     * Otherwise, test for at least natural alignment and defer
1117     * everything else to the helper functions.
1118     */
1119    if (s_bits != get_alignment_bits(opc)) {
1120        tcg_debug_assert(check_fit_tl(a_mask, 13));
1121        tcg_out_arithi(s, TCG_REG_G0, addr_reg, a_mask, ARITH_ANDCC);
1122
1123        ldst = new_ldst_label(s);
1124        ldst->is_ld = is_ld;
1125        ldst->oi = oi;
1126        ldst->addrlo_reg = addr_reg;
1127        ldst->label_ptr[0] = s->code_ptr;
1128
1129        /* bne,pn %icc, label0 */
1130        tcg_out_bpcc0(s, COND_NE, BPCC_PN | BPCC_ICC, 0);
1131    }
1132    h->base = guest_base ? TCG_GUEST_BASE_REG : TCG_REG_G0;
1133#endif
1134
1135    /* If the guest address must be zero-extended, do in the delay slot.  */
1136    if (addr_type == TCG_TYPE_I32) {
1137        tcg_out_ext32u(s, TCG_REG_T2, addr_reg);
1138        h->index = TCG_REG_T2;
1139    } else {
1140        if (ldst) {
1141            tcg_out_nop(s);
1142        }
1143        h->index = addr_reg;
1144    }
1145    return ldst;
1146}
1147
1148static void tcg_out_qemu_ld(TCGContext *s, TCGReg data, TCGReg addr,
1149                            MemOpIdx oi, TCGType data_type)
1150{
1151    static const int ld_opc[(MO_SSIZE | MO_BSWAP) + 1] = {
1152        [MO_UB]   = LDUB,
1153        [MO_SB]   = LDSB,
1154        [MO_UB | MO_LE] = LDUB,
1155        [MO_SB | MO_LE] = LDSB,
1156
1157        [MO_BEUW] = LDUH,
1158        [MO_BESW] = LDSH,
1159        [MO_BEUL] = LDUW,
1160        [MO_BESL] = LDSW,
1161        [MO_BEUQ] = LDX,
1162        [MO_BESQ] = LDX,
1163
1164        [MO_LEUW] = LDUH_LE,
1165        [MO_LESW] = LDSH_LE,
1166        [MO_LEUL] = LDUW_LE,
1167        [MO_LESL] = LDSW_LE,
1168        [MO_LEUQ] = LDX_LE,
1169        [MO_LESQ] = LDX_LE,
1170    };
1171
1172    TCGLabelQemuLdst *ldst;
1173    HostAddress h;
1174
1175    ldst = prepare_host_addr(s, &h, addr, oi, true);
1176
1177    tcg_out_ldst_rr(s, data, h.base, h.index,
1178                    ld_opc[get_memop(oi) & (MO_BSWAP | MO_SSIZE)]);
1179
1180    if (ldst) {
1181        ldst->type = data_type;
1182        ldst->datalo_reg = data;
1183        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1184    }
1185}
1186
1187static void tcg_out_qemu_st(TCGContext *s, TCGReg data, TCGReg addr,
1188                            MemOpIdx oi, TCGType data_type)
1189{
1190    static const int st_opc[(MO_SIZE | MO_BSWAP) + 1] = {
1191        [MO_UB]   = STB,
1192
1193        [MO_BEUW] = STH,
1194        [MO_BEUL] = STW,
1195        [MO_BEUQ] = STX,
1196
1197        [MO_LEUW] = STH_LE,
1198        [MO_LEUL] = STW_LE,
1199        [MO_LEUQ] = STX_LE,
1200    };
1201
1202    TCGLabelQemuLdst *ldst;
1203    HostAddress h;
1204
1205    ldst = prepare_host_addr(s, &h, addr, oi, false);
1206
1207    tcg_out_ldst_rr(s, data, h.base, h.index,
1208                    st_opc[get_memop(oi) & (MO_BSWAP | MO_SIZE)]);
1209
1210    if (ldst) {
1211        ldst->type = data_type;
1212        ldst->datalo_reg = data;
1213        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1214    }
1215}
1216
1217static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1218{
1219    if (check_fit_ptr(a0, 13)) {
1220        tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
1221        tcg_out_movi_s13(s, TCG_REG_O0, a0);
1222        return;
1223    } else {
1224        intptr_t tb_diff = tcg_tbrel_diff(s, (void *)a0);
1225        if (check_fit_ptr(tb_diff, 13)) {
1226            tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
1227            /* Note that TCG_REG_TB has been unwound to O1.  */
1228            tcg_out_arithi(s, TCG_REG_O0, TCG_REG_O1, tb_diff, ARITH_ADD);
1229            return;
1230        }
1231    }
1232    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, a0 & ~0x3ff);
1233    tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
1234    tcg_out_arithi(s, TCG_REG_O0, TCG_REG_O0, a0 & 0x3ff, ARITH_OR);
1235}
1236
1237static void tcg_out_goto_tb(TCGContext *s, int which)
1238{
1239    ptrdiff_t off = tcg_tbrel_diff(s, (void *)get_jmp_target_addr(s, which));
1240
1241    /* Load link and indirect branch. */
1242    set_jmp_insn_offset(s, which);
1243    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TB, TCG_REG_TB, off);
1244    tcg_out_arithi(s, TCG_REG_G0, TCG_REG_TB, 0, JMPL);
1245    /* delay slot */
1246    tcg_out_nop(s);
1247    set_jmp_reset_offset(s, which);
1248
1249    /*
1250     * For the unlinked path of goto_tb, we need to reset TCG_REG_TB
1251     * to the beginning of this TB.
1252     */
1253    off = -tcg_current_code_size(s);
1254    if (check_fit_i32(off, 13)) {
1255        tcg_out_arithi(s, TCG_REG_TB, TCG_REG_TB, off, ARITH_ADD);
1256    } else {
1257        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T1, off);
1258        tcg_out_arith(s, TCG_REG_TB, TCG_REG_TB, TCG_REG_T1, ARITH_ADD);
1259    }
1260}
1261
1262void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1263                              uintptr_t jmp_rx, uintptr_t jmp_rw)
1264{
1265}
1266
1267static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1268                       const TCGArg args[TCG_MAX_OP_ARGS],
1269                       const int const_args[TCG_MAX_OP_ARGS])
1270{
1271    TCGArg a0, a1, a2;
1272    int c, c2;
1273
1274    /* Hoist the loads of the most common arguments.  */
1275    a0 = args[0];
1276    a1 = args[1];
1277    a2 = args[2];
1278    c2 = const_args[2];
1279
1280    switch (opc) {
1281    case INDEX_op_goto_ptr:
1282        tcg_out_arithi(s, TCG_REG_G0, a0, 0, JMPL);
1283        tcg_out_mov_delay(s, TCG_REG_TB, a0);
1284        break;
1285    case INDEX_op_br:
1286        tcg_out_bpcc(s, COND_A, BPCC_PT, arg_label(a0));
1287        tcg_out_nop(s);
1288        break;
1289
1290#define OP_32_64(x)                             \
1291        glue(glue(case INDEX_op_, x), _i32):    \
1292        glue(glue(case INDEX_op_, x), _i64)
1293
1294    OP_32_64(ld8u):
1295        tcg_out_ldst(s, a0, a1, a2, LDUB);
1296        break;
1297    OP_32_64(ld8s):
1298        tcg_out_ldst(s, a0, a1, a2, LDSB);
1299        break;
1300    OP_32_64(ld16u):
1301        tcg_out_ldst(s, a0, a1, a2, LDUH);
1302        break;
1303    OP_32_64(ld16s):
1304        tcg_out_ldst(s, a0, a1, a2, LDSH);
1305        break;
1306    case INDEX_op_ld_i32:
1307    case INDEX_op_ld32u_i64:
1308        tcg_out_ldst(s, a0, a1, a2, LDUW);
1309        break;
1310    OP_32_64(st8):
1311        tcg_out_ldst(s, a0, a1, a2, STB);
1312        break;
1313    OP_32_64(st16):
1314        tcg_out_ldst(s, a0, a1, a2, STH);
1315        break;
1316    case INDEX_op_st_i32:
1317    case INDEX_op_st32_i64:
1318        tcg_out_ldst(s, a0, a1, a2, STW);
1319        break;
1320    OP_32_64(add):
1321        c = ARITH_ADD;
1322        goto gen_arith;
1323    OP_32_64(sub):
1324        c = ARITH_SUB;
1325        goto gen_arith;
1326    OP_32_64(and):
1327        c = ARITH_AND;
1328        goto gen_arith;
1329    OP_32_64(andc):
1330        c = ARITH_ANDN;
1331        goto gen_arith;
1332    OP_32_64(or):
1333        c = ARITH_OR;
1334        goto gen_arith;
1335    OP_32_64(orc):
1336        c = ARITH_ORN;
1337        goto gen_arith;
1338    OP_32_64(xor):
1339        c = ARITH_XOR;
1340        goto gen_arith;
1341    case INDEX_op_shl_i32:
1342        c = SHIFT_SLL;
1343    do_shift32:
1344        /* Limit immediate shift count lest we create an illegal insn.  */
1345        tcg_out_arithc(s, a0, a1, a2 & 31, c2, c);
1346        break;
1347    case INDEX_op_shr_i32:
1348        c = SHIFT_SRL;
1349        goto do_shift32;
1350    case INDEX_op_sar_i32:
1351        c = SHIFT_SRA;
1352        goto do_shift32;
1353    case INDEX_op_mul_i32:
1354        c = ARITH_UMUL;
1355        goto gen_arith;
1356
1357    OP_32_64(neg):
1358	c = ARITH_SUB;
1359	goto gen_arith1;
1360    OP_32_64(not):
1361	c = ARITH_ORN;
1362	goto gen_arith1;
1363
1364    case INDEX_op_div_i32:
1365        tcg_out_div32(s, a0, a1, a2, c2, 0);
1366        break;
1367    case INDEX_op_divu_i32:
1368        tcg_out_div32(s, a0, a1, a2, c2, 1);
1369        break;
1370
1371    case INDEX_op_brcond_i32:
1372        tcg_out_brcond_i32(s, a2, a0, a1, const_args[1], arg_label(args[3]));
1373        break;
1374    case INDEX_op_setcond_i32:
1375        tcg_out_setcond_i32(s, args[3], a0, a1, a2, c2, false);
1376        break;
1377    case INDEX_op_negsetcond_i32:
1378        tcg_out_setcond_i32(s, args[3], a0, a1, a2, c2, true);
1379        break;
1380    case INDEX_op_movcond_i32:
1381        tcg_out_movcond_i32(s, args[5], a0, a1, a2, c2, args[3], const_args[3]);
1382        break;
1383
1384    case INDEX_op_add2_i32:
1385        tcg_out_addsub2_i32(s, args[0], args[1], args[2], args[3],
1386                            args[4], const_args[4], args[5], const_args[5],
1387                            ARITH_ADDCC, ARITH_ADDC);
1388        break;
1389    case INDEX_op_sub2_i32:
1390        tcg_out_addsub2_i32(s, args[0], args[1], args[2], args[3],
1391                            args[4], const_args[4], args[5], const_args[5],
1392                            ARITH_SUBCC, ARITH_SUBC);
1393        break;
1394    case INDEX_op_mulu2_i32:
1395        c = ARITH_UMUL;
1396        goto do_mul2;
1397    case INDEX_op_muls2_i32:
1398        c = ARITH_SMUL;
1399    do_mul2:
1400        /* The 32-bit multiply insns produce a full 64-bit result. */
1401        tcg_out_arithc(s, a0, a2, args[3], const_args[3], c);
1402        tcg_out_arithi(s, a1, a0, 32, SHIFT_SRLX);
1403        break;
1404
1405    case INDEX_op_qemu_ld_a32_i32:
1406    case INDEX_op_qemu_ld_a64_i32:
1407        tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32);
1408        break;
1409    case INDEX_op_qemu_ld_a32_i64:
1410    case INDEX_op_qemu_ld_a64_i64:
1411        tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64);
1412        break;
1413    case INDEX_op_qemu_st_a32_i32:
1414    case INDEX_op_qemu_st_a64_i32:
1415        tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32);
1416        break;
1417    case INDEX_op_qemu_st_a32_i64:
1418    case INDEX_op_qemu_st_a64_i64:
1419        tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64);
1420        break;
1421
1422    case INDEX_op_ld32s_i64:
1423        tcg_out_ldst(s, a0, a1, a2, LDSW);
1424        break;
1425    case INDEX_op_ld_i64:
1426        tcg_out_ldst(s, a0, a1, a2, LDX);
1427        break;
1428    case INDEX_op_st_i64:
1429        tcg_out_ldst(s, a0, a1, a2, STX);
1430        break;
1431    case INDEX_op_shl_i64:
1432        c = SHIFT_SLLX;
1433    do_shift64:
1434        /* Limit immediate shift count lest we create an illegal insn.  */
1435        tcg_out_arithc(s, a0, a1, a2 & 63, c2, c);
1436        break;
1437    case INDEX_op_shr_i64:
1438        c = SHIFT_SRLX;
1439        goto do_shift64;
1440    case INDEX_op_sar_i64:
1441        c = SHIFT_SRAX;
1442        goto do_shift64;
1443    case INDEX_op_mul_i64:
1444        c = ARITH_MULX;
1445        goto gen_arith;
1446    case INDEX_op_div_i64:
1447        c = ARITH_SDIVX;
1448        goto gen_arith;
1449    case INDEX_op_divu_i64:
1450        c = ARITH_UDIVX;
1451        goto gen_arith;
1452
1453    case INDEX_op_brcond_i64:
1454        tcg_out_brcond_i64(s, a2, a0, a1, const_args[1], arg_label(args[3]));
1455        break;
1456    case INDEX_op_setcond_i64:
1457        tcg_out_setcond_i64(s, args[3], a0, a1, a2, c2, false);
1458        break;
1459    case INDEX_op_negsetcond_i64:
1460        tcg_out_setcond_i64(s, args[3], a0, a1, a2, c2, true);
1461        break;
1462    case INDEX_op_movcond_i64:
1463        tcg_out_movcond_i64(s, args[5], a0, a1, a2, c2, args[3], const_args[3]);
1464        break;
1465    case INDEX_op_add2_i64:
1466        tcg_out_addsub2_i64(s, args[0], args[1], args[2], args[3], args[4],
1467                            const_args[4], args[5], const_args[5], false);
1468        break;
1469    case INDEX_op_sub2_i64:
1470        tcg_out_addsub2_i64(s, args[0], args[1], args[2], args[3], args[4],
1471                            const_args[4], args[5], const_args[5], true);
1472        break;
1473    case INDEX_op_muluh_i64:
1474        tcg_out_arith(s, args[0], args[1], args[2], ARITH_UMULXHI);
1475        break;
1476
1477    gen_arith:
1478        tcg_out_arithc(s, a0, a1, a2, c2, c);
1479        break;
1480
1481    gen_arith1:
1482	tcg_out_arithc(s, a0, TCG_REG_G0, a1, const_args[1], c);
1483	break;
1484
1485    case INDEX_op_mb:
1486        tcg_out_mb(s, a0);
1487        break;
1488
1489    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1490    case INDEX_op_mov_i64:
1491    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1492    case INDEX_op_exit_tb:  /* Always emitted via tcg_out_exit_tb.  */
1493    case INDEX_op_goto_tb:  /* Always emitted via tcg_out_goto_tb.  */
1494    case INDEX_op_ext8s_i32:  /* Always emitted via tcg_reg_alloc_op.  */
1495    case INDEX_op_ext8s_i64:
1496    case INDEX_op_ext8u_i32:
1497    case INDEX_op_ext8u_i64:
1498    case INDEX_op_ext16s_i32:
1499    case INDEX_op_ext16s_i64:
1500    case INDEX_op_ext16u_i32:
1501    case INDEX_op_ext16u_i64:
1502    case INDEX_op_ext32s_i64:
1503    case INDEX_op_ext32u_i64:
1504    case INDEX_op_ext_i32_i64:
1505    case INDEX_op_extu_i32_i64:
1506    default:
1507        g_assert_not_reached();
1508    }
1509}
1510
1511static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1512{
1513    switch (op) {
1514    case INDEX_op_goto_ptr:
1515        return C_O0_I1(r);
1516
1517    case INDEX_op_ld8u_i32:
1518    case INDEX_op_ld8u_i64:
1519    case INDEX_op_ld8s_i32:
1520    case INDEX_op_ld8s_i64:
1521    case INDEX_op_ld16u_i32:
1522    case INDEX_op_ld16u_i64:
1523    case INDEX_op_ld16s_i32:
1524    case INDEX_op_ld16s_i64:
1525    case INDEX_op_ld_i32:
1526    case INDEX_op_ld32u_i64:
1527    case INDEX_op_ld32s_i64:
1528    case INDEX_op_ld_i64:
1529    case INDEX_op_neg_i32:
1530    case INDEX_op_neg_i64:
1531    case INDEX_op_not_i32:
1532    case INDEX_op_not_i64:
1533    case INDEX_op_ext32s_i64:
1534    case INDEX_op_ext32u_i64:
1535    case INDEX_op_ext_i32_i64:
1536    case INDEX_op_extu_i32_i64:
1537    case INDEX_op_qemu_ld_a32_i32:
1538    case INDEX_op_qemu_ld_a64_i32:
1539    case INDEX_op_qemu_ld_a32_i64:
1540    case INDEX_op_qemu_ld_a64_i64:
1541        return C_O1_I1(r, r);
1542
1543    case INDEX_op_st8_i32:
1544    case INDEX_op_st8_i64:
1545    case INDEX_op_st16_i32:
1546    case INDEX_op_st16_i64:
1547    case INDEX_op_st_i32:
1548    case INDEX_op_st32_i64:
1549    case INDEX_op_st_i64:
1550    case INDEX_op_qemu_st_a32_i32:
1551    case INDEX_op_qemu_st_a64_i32:
1552    case INDEX_op_qemu_st_a32_i64:
1553    case INDEX_op_qemu_st_a64_i64:
1554        return C_O0_I2(rZ, r);
1555
1556    case INDEX_op_add_i32:
1557    case INDEX_op_add_i64:
1558    case INDEX_op_mul_i32:
1559    case INDEX_op_mul_i64:
1560    case INDEX_op_div_i32:
1561    case INDEX_op_div_i64:
1562    case INDEX_op_divu_i32:
1563    case INDEX_op_divu_i64:
1564    case INDEX_op_sub_i32:
1565    case INDEX_op_sub_i64:
1566    case INDEX_op_and_i32:
1567    case INDEX_op_and_i64:
1568    case INDEX_op_andc_i32:
1569    case INDEX_op_andc_i64:
1570    case INDEX_op_or_i32:
1571    case INDEX_op_or_i64:
1572    case INDEX_op_orc_i32:
1573    case INDEX_op_orc_i64:
1574    case INDEX_op_xor_i32:
1575    case INDEX_op_xor_i64:
1576    case INDEX_op_shl_i32:
1577    case INDEX_op_shl_i64:
1578    case INDEX_op_shr_i32:
1579    case INDEX_op_shr_i64:
1580    case INDEX_op_sar_i32:
1581    case INDEX_op_sar_i64:
1582    case INDEX_op_setcond_i32:
1583    case INDEX_op_setcond_i64:
1584    case INDEX_op_negsetcond_i32:
1585    case INDEX_op_negsetcond_i64:
1586        return C_O1_I2(r, rZ, rJ);
1587
1588    case INDEX_op_brcond_i32:
1589    case INDEX_op_brcond_i64:
1590        return C_O0_I2(rZ, rJ);
1591    case INDEX_op_movcond_i32:
1592    case INDEX_op_movcond_i64:
1593        return C_O1_I4(r, rZ, rJ, rI, 0);
1594    case INDEX_op_add2_i32:
1595    case INDEX_op_add2_i64:
1596    case INDEX_op_sub2_i32:
1597    case INDEX_op_sub2_i64:
1598        return C_O2_I4(r, r, rZ, rZ, rJ, rJ);
1599    case INDEX_op_mulu2_i32:
1600    case INDEX_op_muls2_i32:
1601        return C_O2_I2(r, r, rZ, rJ);
1602    case INDEX_op_muluh_i64:
1603        return C_O1_I2(r, r, r);
1604
1605    default:
1606        g_assert_not_reached();
1607    }
1608}
1609
1610static void tcg_target_init(TCGContext *s)
1611{
1612    /*
1613     * Only probe for the platform and capabilities if we haven't already
1614     * determined maximum values at compile time.
1615     */
1616#ifndef use_vis3_instructions
1617    {
1618        unsigned long hwcap = qemu_getauxval(AT_HWCAP);
1619        use_vis3_instructions = (hwcap & HWCAP_SPARC_VIS3) != 0;
1620    }
1621#endif
1622
1623    tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
1624    tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS;
1625
1626    tcg_target_call_clobber_regs = 0;
1627    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G1);
1628    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G2);
1629    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G3);
1630    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G4);
1631    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G5);
1632    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G6);
1633    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_G7);
1634    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O0);
1635    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O1);
1636    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O2);
1637    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O3);
1638    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O4);
1639    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O5);
1640    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O6);
1641    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_O7);
1642
1643    s->reserved_regs = 0;
1644    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0); /* zero */
1645    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G6); /* reserved for os */
1646    tcg_regset_set_reg(s->reserved_regs, TCG_REG_G7); /* thread pointer */
1647    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6); /* frame pointer */
1648    tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7); /* return address */
1649    tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6); /* stack pointer */
1650    tcg_regset_set_reg(s->reserved_regs, TCG_REG_T1); /* for internal use */
1651    tcg_regset_set_reg(s->reserved_regs, TCG_REG_T2); /* for internal use */
1652    tcg_regset_set_reg(s->reserved_regs, TCG_REG_T3); /* for internal use */
1653}
1654
1655#define ELF_HOST_MACHINE  EM_SPARCV9
1656
1657typedef struct {
1658    DebugFrameHeader h;
1659    uint8_t fde_def_cfa[4];
1660    uint8_t fde_win_save;
1661    uint8_t fde_ret_save[3];
1662} DebugFrame;
1663
1664static const DebugFrame debug_frame = {
1665    .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
1666    .h.cie.id = -1,
1667    .h.cie.version = 1,
1668    .h.cie.code_align = 1,
1669    .h.cie.data_align = -sizeof(void *) & 0x7f,
1670    .h.cie.return_column = 15,            /* o7 */
1671
1672    /* Total FDE size does not include the "len" member.  */
1673    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1674
1675    .fde_def_cfa = {
1676        12, 30,                         /* DW_CFA_def_cfa i6, 2047 */
1677        (2047 & 0x7f) | 0x80, (2047 >> 7)
1678    },
1679    .fde_win_save = 0x2d,               /* DW_CFA_GNU_window_save */
1680    .fde_ret_save = { 9, 15, 31 },      /* DW_CFA_register o7, i7 */
1681};
1682
1683void tcg_register_jit(const void *buf, size_t buf_size)
1684{
1685    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1686}
1687