6e18f03cdec5dcb04304316a332b5075993321f8
[qemu] / tcg / sparc / tcg-target.c
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 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
26     "%g0",
27     "%g1",
28     "%g2",
29     "%g3",
30     "%g4",
31     "%g5",
32     "%g6",
33     "%g7",
34     "%o0",
35     "%o1",
36     "%o2",
37     "%o3",
38     "%o4",
39     "%o5",
40     "%o6",
41     "%o7",
42     "%l0",
43     "%l1",
44     "%l2",
45     "%l3",
46     "%l4",
47     "%l5",
48     "%l6",
49     "%l7",
50     "%i0",
51     "%i1",
52     "%i2",
53     "%i3",
54     "%i4",
55     "%i5",
56     "%i6",
57     "%i7",
58 };
59
60 static const int tcg_target_reg_alloc_order[] = {
61     TCG_REG_L0,
62     TCG_REG_L1,
63     TCG_REG_L2,
64     TCG_REG_L3,
65     TCG_REG_L4,
66     TCG_REG_L5,
67     TCG_REG_L6,
68     TCG_REG_L7,
69     TCG_REG_I0,
70     TCG_REG_I1,
71     TCG_REG_I2,
72     TCG_REG_I3,
73     TCG_REG_I4,
74 };
75
76 static const int tcg_target_call_iarg_regs[6] = {
77     TCG_REG_O0,
78     TCG_REG_O1,
79     TCG_REG_O2,
80     TCG_REG_O3,
81     TCG_REG_O4,
82     TCG_REG_O5,
83 };
84
85 static const int tcg_target_call_oarg_regs[2] = {
86     TCG_REG_O0,
87     TCG_REG_O1,
88 };
89
90 static void patch_reloc(uint8_t *code_ptr, int type,
91                         tcg_target_long value, tcg_target_long addend)
92 {
93     value += addend;
94     switch (type) {
95     case R_SPARC_32:
96         if (value != (uint32_t)value)
97             tcg_abort();
98         *(uint32_t *)code_ptr = value;
99         break;
100     default:
101         tcg_abort();
102     }
103 }
104
105 /* maximum number of register used for input function arguments */
106 static inline int tcg_target_get_call_iarg_regs_count(int flags)
107 {
108     return 6;
109 }
110
111 /* parse target specific constraints */
112 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
113 {
114     const char *ct_str;
115
116     ct_str = *pct_str;
117     switch (ct_str[0]) {
118     case 'r':
119     case 'L': /* qemu_ld/st constraint */
120         ct->ct |= TCG_CT_REG;
121         tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
122         break;
123     case 'I':
124         ct->ct |= TCG_CT_CONST_S11;
125         break;
126     case 'J':
127         ct->ct |= TCG_CT_CONST_S13;
128         break;
129     default:
130         return -1;
131     }
132     ct_str++;
133     *pct_str = ct_str;
134     return 0;
135 }
136
137 #define ABS(x) ((x) < 0? -(x) : (x))
138 /* test if a constant matches the constraint */
139 static inline int tcg_target_const_match(tcg_target_long val,
140                                          const TCGArgConstraint *arg_ct)
141 {
142     int ct;
143
144     ct = arg_ct->ct;
145     if (ct & TCG_CT_CONST)
146         return 1;
147     else if ((ct & TCG_CT_CONST_S11) && ABS(val) == (ABS(val) & 0x3ff))
148         return 1;
149     else if ((ct & TCG_CT_CONST_S13) && ABS(val) == (ABS(val) & 0xfff))
150         return 1;
151     else
152         return 0;
153 }
154
155 #define INSN_OP(x)  ((x) << 30)
156 #define INSN_OP2(x) ((x) << 22)
157 #define INSN_OP3(x) ((x) << 19)
158 #define INSN_OPF(x) ((x) << 5)
159 #define INSN_RD(x)  ((x) << 25)
160 #define INSN_RS1(x) ((x) << 14)
161 #define INSN_RS2(x) (x)
162
163 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
164 #define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
165
166 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
167 #define COND_A     0x8
168 #define BA         (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
169
170 #define ARITH_ADD  (INSN_OP(2) | INSN_OP3(0x00))
171 #define ARITH_AND  (INSN_OP(2) | INSN_OP3(0x01))
172 #define ARITH_OR   (INSN_OP(2) | INSN_OP3(0x02))
173 #define ARITH_XOR  (INSN_OP(2) | INSN_OP3(0x03))
174 #define ARITH_SUB  (INSN_OP(2) | INSN_OP3(0x08))
175 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
176 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
177 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
178 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
179 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
180 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
181 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
182 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
183
184 #define SHIFT_SLL  (INSN_OP(2) | INSN_OP3(0x25))
185 #define SHIFT_SRL  (INSN_OP(2) | INSN_OP3(0x26))
186 #define SHIFT_SRA  (INSN_OP(2) | INSN_OP3(0x27))
187
188 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
189 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
190 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
191
192 #define WRY        (INSN_OP(2) | INSN_OP3(0x30))
193 #define JMPL       (INSN_OP(2) | INSN_OP3(0x38))
194 #define SAVE       (INSN_OP(2) | INSN_OP3(0x3c))
195 #define RESTORE    (INSN_OP(2) | INSN_OP3(0x3d))
196 #define SETHI      (INSN_OP(0) | INSN_OP2(0x4))
197 #define CALL       INSN_OP(1)
198 #define LDUB       (INSN_OP(3) | INSN_OP3(0x01))
199 #define LDSB       (INSN_OP(3) | INSN_OP3(0x09))
200 #define LDUH       (INSN_OP(3) | INSN_OP3(0x02))
201 #define LDSH       (INSN_OP(3) | INSN_OP3(0x0a))
202 #define LDUW       (INSN_OP(3) | INSN_OP3(0x00))
203 #define LDSW       (INSN_OP(3) | INSN_OP3(0x08))
204 #define LDX        (INSN_OP(3) | INSN_OP3(0x0b))
205 #define STB        (INSN_OP(3) | INSN_OP3(0x05))
206 #define STH        (INSN_OP(3) | INSN_OP3(0x06))
207 #define STW        (INSN_OP(3) | INSN_OP3(0x04))
208 #define STX        (INSN_OP(3) | INSN_OP3(0x0e))
209
210 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
211 {
212     tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(arg) |
213               INSN_RS2(TCG_REG_G0));
214 }
215
216 static inline void tcg_out_movi(TCGContext *s, TCGType type,
217                                 int ret, tcg_target_long arg)
218 {
219 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
220     if (arg != (arg & 0xffffffff))
221         fprintf(stderr, "unimplemented %s with constant %ld\n", __func__, arg);
222 #endif
223     if (arg == (arg & 0xfff))
224         tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(TCG_REG_G0) |
225                   INSN_IMM13(arg));
226     else {
227         tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
228         if (arg & 0x3ff)
229             tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(ret) |
230                       INSN_IMM13(arg & 0x3ff));
231     }
232 }
233
234 static inline void tcg_out_ld_raw(TCGContext *s, int ret,
235                                   tcg_target_long arg)
236 {
237     tcg_out32(s, SETHI | INSN_RD(ret) | (((uint32_t)arg & 0xfffffc00) >> 10));
238     tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
239               INSN_IMM13(arg & 0x3ff));
240 }
241
242 static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
243                                   tcg_target_long arg)
244 {
245 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
246     if (arg != (arg & 0xffffffff))
247         fprintf(stderr, "unimplemented %s with offset %ld\n", __func__, arg);
248     if (arg != (arg & 0xfff))
249         tcg_out32(s, SETHI | INSN_RD(ret) | (((uint32_t)arg & 0xfffffc00) >> 10));
250     tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
251               INSN_IMM13(arg & 0x3ff));
252 #else
253     tcg_out_ld_raw(s, ret, arg);
254 #endif
255 }
256
257 static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
258 {
259     if (offset == (offset & 0xfff))
260         tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
261                   INSN_IMM13(offset));
262     else
263         fprintf(stderr, "unimplemented %s with offset %d\n", __func__, offset);
264 }
265
266 static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
267                               int arg1, tcg_target_long arg2)
268 {
269     if (type == TCG_TYPE_I32)
270         tcg_out_ldst(s, ret, arg1, arg2, LDUW);
271     else
272         tcg_out_ldst(s, ret, arg1, arg2, LDX);
273 }
274
275 static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
276                               int arg1, tcg_target_long arg2)
277 {
278     if (type == TCG_TYPE_I32)
279         tcg_out_ldst(s, arg, arg1, arg2, STW);
280     else
281         tcg_out_ldst(s, arg, arg1, arg2, STX);
282 }
283
284 static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
285                                  int op)
286 {
287     tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
288               INSN_RS2(rs2));
289 }
290
291 static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1, int offset,
292                                   int op)
293 {
294     tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
295               INSN_IMM13(offset));
296 }
297
298 static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
299 {
300     if (val == 0 || val == -1)
301         tcg_out32(s, WRY | INSN_IMM13(val));
302     else
303         fprintf(stderr, "unimplemented sety %ld\n", (long)val);
304 }
305
306 static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
307 {
308     if (val != 0) {
309         if (val == (val & 0xfff))
310             tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
311         else
312             fprintf(stderr, "unimplemented addi %ld\n", (long)val);
313     }
314 }
315
316 static inline void tcg_out_nop(TCGContext *s)
317 {
318     tcg_out32(s, SETHI | INSN_RD(TCG_REG_G0) | 0);
319 }
320
321 /* Generate global QEMU prologue and epilogue code */
322 void tcg_target_qemu_prologue(TCGContext *s)
323 {
324     tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
325               INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
326     tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_O0) |
327               INSN_RS2(TCG_REG_G0));
328     tcg_out_nop(s);
329 }
330
331 static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
332                               const int *const_args)
333 {
334     int c;
335
336     switch (opc) {
337     case INDEX_op_exit_tb:
338         tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
339         tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
340                   INSN_IMM13(8));
341         tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
342                       INSN_RS2(TCG_REG_G0));
343         break;
344     case INDEX_op_goto_tb:
345         if (s->tb_jmp_offset) {
346             /* direct jump method */
347             if (ABS(args[0] - (unsigned long)s->code_ptr) ==
348                 (ABS(args[0] - (unsigned long)s->code_ptr) & 0x1fffff)) {
349                 tcg_out32(s, BA |
350                           INSN_OFF22(args[0] - (unsigned long)s->code_ptr));
351             } else {
352                 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, args[0]);
353                 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
354                           INSN_RS2(TCG_REG_G0));
355             }
356             s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
357         } else {
358             /* indirect jump method */
359             tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
360             tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
361                       INSN_RS2(TCG_REG_G0));
362         }
363         tcg_out_nop(s);
364         s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
365         break;
366     case INDEX_op_call:
367         if (const_args[0]) {
368             tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
369                                   - (tcg_target_ulong)s->code_ptr) >> 2)
370                                  & 0x3fffffff));
371             tcg_out_nop(s);
372         } else {
373             tcg_out_ld_ptr(s, TCG_REG_O7, (tcg_target_long)(s->tb_next + args[0]));
374             tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_O7) |
375                       INSN_RS2(TCG_REG_G0));
376             tcg_out_nop(s);
377         }
378         break;
379     case INDEX_op_jmp:
380         fprintf(stderr, "unimplemented jmp\n");
381         break;
382     case INDEX_op_br:
383         fprintf(stderr, "unimplemented br\n");
384         break;
385     case INDEX_op_movi_i32:
386         tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
387         break;
388
389 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
390 #define OP_32_64(x)                             \
391         glue(glue(case INDEX_op_, x), _i32:)    \
392         glue(glue(case INDEX_op_, x), _i64:)
393 #else
394 #define OP_32_64(x)                             \
395         glue(glue(case INDEX_op_, x), _i32:)
396 #endif
397         OP_32_64(ld8u);
398         tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
399         break;
400         OP_32_64(ld8s);
401         tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
402         break;
403         OP_32_64(ld16u);
404         tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
405         break;
406         OP_32_64(ld16s);
407         tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
408         break;
409     case INDEX_op_ld_i32:
410 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
411     case INDEX_op_ld32u_i64:
412 #endif
413         tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
414         break;
415         OP_32_64(st8);
416         tcg_out_ldst(s, args[0], args[1], args[2], STB);
417         break;
418         OP_32_64(st16);
419         tcg_out_ldst(s, args[0], args[1], args[2], STH);
420         break;
421     case INDEX_op_st_i32:
422 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
423     case INDEX_op_st32_i64:
424 #endif
425         tcg_out_ldst(s, args[0], args[1], args[2], STW);
426         break;
427         OP_32_64(add);
428         c = ARITH_ADD;
429         goto gen_arith32;
430         OP_32_64(sub);
431         c = ARITH_SUB;
432         goto gen_arith32;
433         OP_32_64(and);
434         c = ARITH_AND;
435         goto gen_arith32;
436         OP_32_64(or);
437         c = ARITH_OR;
438         goto gen_arith32;
439         OP_32_64(xor);
440         c = ARITH_XOR;
441         goto gen_arith32;
442     case INDEX_op_shl_i32:
443         c = SHIFT_SLL;
444         goto gen_arith32;
445     case INDEX_op_shr_i32:
446         c = SHIFT_SRL;
447         goto gen_arith32;
448     case INDEX_op_sar_i32:
449         c = SHIFT_SRA;
450         goto gen_arith32;
451     case INDEX_op_mul_i32:
452         c = ARITH_UMUL;
453         goto gen_arith32;
454     case INDEX_op_div2_i32:
455 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
456         c = ARITH_SDIVX;
457         goto gen_arith32;
458 #else
459         tcg_out_sety(s, 0);
460         c = ARITH_SDIV;
461         goto gen_arith32;
462 #endif
463     case INDEX_op_divu2_i32:
464 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
465         c = ARITH_UDIVX;
466         goto gen_arith32;
467 #else
468         tcg_out_sety(s, 0);
469         c = ARITH_UDIV;
470         goto gen_arith32;
471 #endif
472
473     case INDEX_op_brcond_i32:
474         fprintf(stderr, "unimplemented brcond\n");
475         break;
476
477     case INDEX_op_qemu_ld8u:
478         fprintf(stderr, "unimplemented qld\n");
479         break;
480     case INDEX_op_qemu_ld8s:
481         fprintf(stderr, "unimplemented qld\n");
482         break;
483     case INDEX_op_qemu_ld16u:
484         fprintf(stderr, "unimplemented qld\n");
485         break;
486     case INDEX_op_qemu_ld16s:
487         fprintf(stderr, "unimplemented qld\n");
488         break;
489     case INDEX_op_qemu_ld32u:
490         fprintf(stderr, "unimplemented qld\n");
491         break;
492     case INDEX_op_qemu_ld32s:
493         fprintf(stderr, "unimplemented qld\n");
494         break;
495     case INDEX_op_qemu_st8:
496         fprintf(stderr, "unimplemented qst\n");
497         break;
498     case INDEX_op_qemu_st16:
499         fprintf(stderr, "unimplemented qst\n");
500         break;
501     case INDEX_op_qemu_st32:
502         fprintf(stderr, "unimplemented qst\n");
503         break;
504
505 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
506     case INDEX_op_movi_i64:
507         tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
508         break;
509     case INDEX_op_ld32s_i64:
510         tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
511         break;
512     case INDEX_op_ld_i64:
513         tcg_out_ldst(s, args[0], args[1], args[2], LDX);
514         break;
515     case INDEX_op_st_i64:
516         tcg_out_ldst(s, args[0], args[1], args[2], STX);
517         break;
518     case INDEX_op_shl_i64:
519         c = SHIFT_SLLX;
520         goto gen_arith32;
521     case INDEX_op_shr_i64:
522         c = SHIFT_SRLX;
523         goto gen_arith32;
524     case INDEX_op_sar_i64:
525         c = SHIFT_SRAX;
526         goto gen_arith32;
527     case INDEX_op_mul_i64:
528         c = ARITH_MULX;
529         goto gen_arith32;
530     case INDEX_op_div2_i64:
531         c = ARITH_SDIVX;
532         goto gen_arith32;
533     case INDEX_op_divu2_i64:
534         c = ARITH_UDIVX;
535         goto gen_arith32;
536
537     case INDEX_op_brcond_i64:
538         fprintf(stderr, "unimplemented brcond\n");
539         break;
540     case INDEX_op_qemu_ld64:
541         fprintf(stderr, "unimplemented qld\n");
542         break;
543     case INDEX_op_qemu_st64:
544         fprintf(stderr, "unimplemented qst\n");
545         break;
546
547 #endif
548     gen_arith32:
549         if (const_args[2]) {
550             tcg_out_arithi(s, args[0], args[1], args[2], c);
551         } else {
552             tcg_out_arith(s, args[0], args[1], args[2], c);
553         }
554         break;
555
556     default:
557         fprintf(stderr, "unknown opcode 0x%x\n", opc);
558         tcg_abort();
559     }
560 }
561
562 static const TCGTargetOpDef sparc_op_defs[] = {
563     { INDEX_op_exit_tb, { } },
564     { INDEX_op_goto_tb, { } },
565     { INDEX_op_call, { "ri" } },
566     { INDEX_op_jmp, { "ri" } },
567     { INDEX_op_br, { } },
568
569     { INDEX_op_mov_i32, { "r", "r" } },
570     { INDEX_op_movi_i32, { "r" } },
571     { INDEX_op_ld8u_i32, { "r", "r" } },
572     { INDEX_op_ld8s_i32, { "r", "r" } },
573     { INDEX_op_ld16u_i32, { "r", "r" } },
574     { INDEX_op_ld16s_i32, { "r", "r" } },
575     { INDEX_op_ld_i32, { "r", "r" } },
576     { INDEX_op_st8_i32, { "r", "r" } },
577     { INDEX_op_st16_i32, { "r", "r" } },
578     { INDEX_op_st_i32, { "r", "r" } },
579
580     { INDEX_op_add_i32, { "r", "r", "rJ" } },
581     { INDEX_op_mul_i32, { "r", "r", "rJ" } },
582     { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
583     { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
584     { INDEX_op_sub_i32, { "r", "r", "rJ" } },
585     { INDEX_op_and_i32, { "r", "r", "rJ" } },
586     { INDEX_op_or_i32, { "r", "r", "rJ" } },
587     { INDEX_op_xor_i32, { "r", "r", "rJ" } },
588
589     { INDEX_op_shl_i32, { "r", "r", "rJ" } },
590     { INDEX_op_shr_i32, { "r", "r", "rJ" } },
591     { INDEX_op_sar_i32, { "r", "r", "rJ" } },
592
593     { INDEX_op_brcond_i32, { "r", "ri" } },
594
595     { INDEX_op_qemu_ld8u, { "r", "L" } },
596     { INDEX_op_qemu_ld8s, { "r", "L" } },
597     { INDEX_op_qemu_ld16u, { "r", "L" } },
598     { INDEX_op_qemu_ld16s, { "r", "L" } },
599     { INDEX_op_qemu_ld32u, { "r", "L" } },
600     { INDEX_op_qemu_ld32s, { "r", "L" } },
601
602     { INDEX_op_qemu_st8, { "L", "L" } },
603     { INDEX_op_qemu_st16, { "L", "L" } },
604     { INDEX_op_qemu_st32, { "L", "L" } },
605
606 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
607     { INDEX_op_mov_i64, { "r", "r" } },
608     { INDEX_op_movi_i64, { "r" } },
609     { INDEX_op_ld8u_i64, { "r", "r" } },
610     { INDEX_op_ld8s_i64, { "r", "r" } },
611     { INDEX_op_ld16u_i64, { "r", "r" } },
612     { INDEX_op_ld16s_i64, { "r", "r" } },
613     { INDEX_op_ld32u_i64, { "r", "r" } },
614     { INDEX_op_ld32s_i64, { "r", "r" } },
615     { INDEX_op_ld_i64, { "r", "r" } },
616     { INDEX_op_st8_i64, { "r", "r" } },
617     { INDEX_op_st16_i64, { "r", "r" } },
618     { INDEX_op_st32_i64, { "r", "r" } },
619     { INDEX_op_st_i64, { "r", "r" } },
620
621     { INDEX_op_add_i64, { "r", "r", "rJ" } },
622     { INDEX_op_mul_i64, { "r", "r", "rJ" } },
623     { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
624     { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
625     { INDEX_op_sub_i64, { "r", "r", "rJ" } },
626     { INDEX_op_and_i64, { "r", "r", "rJ" } },
627     { INDEX_op_or_i64, { "r", "r", "rJ" } },
628     { INDEX_op_xor_i64, { "r", "r", "rJ" } },
629
630     { INDEX_op_shl_i64, { "r", "r", "rJ" } },
631     { INDEX_op_shr_i64, { "r", "r", "rJ" } },
632     { INDEX_op_sar_i64, { "r", "r", "rJ" } },
633
634     { INDEX_op_brcond_i64, { "r", "ri" } },
635 #endif
636     { -1 },
637 };
638
639 void tcg_target_init(TCGContext *s)
640 {
641     tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
642 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
643     tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
644 #endif
645     tcg_regset_set32(tcg_target_call_clobber_regs, 0,
646                      (1 << TCG_REG_G1) |
647                      (1 << TCG_REG_G2) |
648                      (1 << TCG_REG_G3) |
649                      (1 << TCG_REG_G4) |
650                      (1 << TCG_REG_G5) |
651                      (1 << TCG_REG_G6) |
652                      (1 << TCG_REG_G7) |
653                      (1 << TCG_REG_O0) |
654                      (1 << TCG_REG_O1) |
655                      (1 << TCG_REG_O2) |
656                      (1 << TCG_REG_O3) |
657                      (1 << TCG_REG_O4) |
658                      (1 << TCG_REG_O5) |
659                      (1 << TCG_REG_O7));
660
661     tcg_regset_clear(s->reserved_regs);
662     tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
663     tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
664     tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
665     tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
666     tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
667     tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
668     tcg_add_target_add_op_defs(sparc_op_defs);
669 }