2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
60 static const int tcg_target_reg_alloc_order[] = {
76 static const int tcg_target_call_iarg_regs[6] = {
85 static const int tcg_target_call_oarg_regs[2] = {
90 static inline int check_fit(tcg_target_long val, unsigned int bits)
92 return ((val << ((sizeof(tcg_target_long) * 8 - bits))
93 >> (sizeof(tcg_target_long) * 8 - bits)) == val);
96 static void patch_reloc(uint8_t *code_ptr, int type,
97 tcg_target_long value, tcg_target_long addend)
102 if (value != (uint32_t)value)
104 *(uint32_t *)code_ptr = value;
106 case R_SPARC_WDISP22:
107 value -= (long)code_ptr;
109 if (!check_fit(value, 22))
111 *(uint32_t *)code_ptr = ((*(uint32_t *)code_ptr) & ~0x3fffff) | value;
118 /* maximum number of register used for input function arguments */
119 static inline int tcg_target_get_call_iarg_regs_count(int flags)
124 /* parse target specific constraints */
125 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
132 case 'L': /* qemu_ld/st constraint */
133 ct->ct |= TCG_CT_REG;
134 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
135 tcg_regset_reset_reg(ct->u.regs, TCG_REG_I0);
136 tcg_regset_reset_reg(ct->u.regs, TCG_REG_I1);
139 ct->ct |= TCG_CT_CONST_S11;
142 ct->ct |= TCG_CT_CONST_S13;
152 /* test if a constant matches the constraint */
153 static inline int tcg_target_const_match(tcg_target_long val,
154 const TCGArgConstraint *arg_ct)
159 if (ct & TCG_CT_CONST)
161 else if ((ct & TCG_CT_CONST_S11) && check_fit(val, 11))
163 else if ((ct & TCG_CT_CONST_S13) && check_fit(val, 13))
169 #define INSN_OP(x) ((x) << 30)
170 #define INSN_OP2(x) ((x) << 22)
171 #define INSN_OP3(x) ((x) << 19)
172 #define INSN_OPF(x) ((x) << 5)
173 #define INSN_RD(x) ((x) << 25)
174 #define INSN_RS1(x) ((x) << 14)
175 #define INSN_RS2(x) (x)
177 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
178 #define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
180 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
197 #define BA (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
199 #define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
200 #define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
201 #define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
202 #define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
203 #define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
204 #define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x04))
205 #define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
206 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
207 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
208 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
209 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
210 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
211 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
212 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
213 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
215 #define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
216 #define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
217 #define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
219 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
220 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
221 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
223 #define WRY (INSN_OP(2) | INSN_OP3(0x30))
224 #define JMPL (INSN_OP(2) | INSN_OP3(0x38))
225 #define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
226 #define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
227 #define SETHI (INSN_OP(0) | INSN_OP2(0x4))
228 #define CALL INSN_OP(1)
229 #define LDUB (INSN_OP(3) | INSN_OP3(0x01))
230 #define LDSB (INSN_OP(3) | INSN_OP3(0x09))
231 #define LDUH (INSN_OP(3) | INSN_OP3(0x02))
232 #define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
233 #define LDUW (INSN_OP(3) | INSN_OP3(0x00))
234 #define LDSW (INSN_OP(3) | INSN_OP3(0x08))
235 #define LDX (INSN_OP(3) | INSN_OP3(0x0b))
236 #define STB (INSN_OP(3) | INSN_OP3(0x05))
237 #define STH (INSN_OP(3) | INSN_OP3(0x06))
238 #define STW (INSN_OP(3) | INSN_OP3(0x04))
239 #define STX (INSN_OP(3) | INSN_OP3(0x0e))
241 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
243 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(arg) |
244 INSN_RS2(TCG_REG_G0));
247 static inline void tcg_out_movi(TCGContext *s, TCGType type,
248 int ret, tcg_target_long arg)
250 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
251 if (!check_fit(arg, 32))
252 fprintf(stderr, "unimplemented %s with constant %ld\n", __func__, arg);
254 if (check_fit(arg, 13))
255 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(TCG_REG_G0) |
258 tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
260 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(ret) |
261 INSN_IMM13(arg & 0x3ff));
265 static inline void tcg_out_ld_raw(TCGContext *s, int ret,
268 tcg_out32(s, SETHI | INSN_RD(ret) | (((uint32_t)arg & 0xfffffc00) >> 10));
269 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
270 INSN_IMM13(arg & 0x3ff));
273 static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
276 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
277 if (!check_fit(arg, 32))
278 fprintf(stderr, "unimplemented %s with offset %ld\n", __func__, arg);
279 if (!check_fit(arg, 13))
280 tcg_out32(s, SETHI | INSN_RD(ret) | (((uint32_t)arg & 0xfffffc00) >> 10));
281 tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
282 INSN_IMM13(arg & 0x3ff));
284 tcg_out_ld_raw(s, ret, arg);
288 static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
290 if (check_fit(offset, 13))
291 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
294 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
295 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
300 static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
301 int arg1, tcg_target_long arg2)
303 if (type == TCG_TYPE_I32)
304 tcg_out_ldst(s, ret, arg1, arg2, LDUW);
306 tcg_out_ldst(s, ret, arg1, arg2, LDX);
309 static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
310 int arg1, tcg_target_long arg2)
312 if (type == TCG_TYPE_I32)
313 tcg_out_ldst(s, arg, arg1, arg2, STW);
315 tcg_out_ldst(s, arg, arg1, arg2, STX);
318 static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
321 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
325 static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1, int offset,
328 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
332 static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
334 if (val == 0 || val == -1)
335 tcg_out32(s, WRY | INSN_IMM13(val));
337 fprintf(stderr, "unimplemented sety %ld\n", (long)val);
340 static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
343 if (check_fit(val, 13))
344 tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
346 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
347 tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
352 static inline void tcg_out_nop(TCGContext *s)
354 tcg_out32(s, SETHI | INSN_RD(TCG_REG_G0) | 0);
357 static void tcg_out_branch(TCGContext *s, int opc, int label_index)
360 TCGLabel *l = &s->labels[label_index];
363 val = l->u.value - (tcg_target_long)s->code_ptr;
364 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
365 | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
367 tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
368 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
372 static const uint8_t tcg_cond_to_bcond[10] = {
373 [TCG_COND_EQ] = COND_E,
374 [TCG_COND_NE] = COND_NE,
375 [TCG_COND_LT] = COND_L,
376 [TCG_COND_GE] = COND_GE,
377 [TCG_COND_LE] = COND_LE,
378 [TCG_COND_GT] = COND_G,
379 [TCG_COND_LTU] = COND_CS,
380 [TCG_COND_GEU] = COND_CC,
381 [TCG_COND_LEU] = COND_LEU,
382 [TCG_COND_GTU] = COND_GU,
385 static void tcg_out_brcond(TCGContext *s, int cond,
386 TCGArg arg1, TCGArg arg2, int const_arg2,
389 if (const_arg2 && arg2 == 0)
391 tcg_out_arith(s, TCG_REG_G0, TCG_REG_G0, arg1, ARITH_ORCC);
393 /* subcc r1, r2, %g0 */
394 tcg_out_arith(s, TCG_REG_G0, arg1, arg2, ARITH_SUBCC);
395 tcg_out_branch(s, tcg_cond_to_bcond[cond], label_index);
399 /* Generate global QEMU prologue and epilogue code */
400 void tcg_target_qemu_prologue(TCGContext *s)
402 tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
403 INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
404 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
405 INSN_RS2(TCG_REG_G0));
409 #if defined(CONFIG_SOFTMMU)
410 extern void __ldb_mmu(void);
411 extern void __ldw_mmu(void);
412 extern void __ldl_mmu(void);
413 extern void __ldq_mmu(void);
415 extern void __stb_mmu(void);
416 extern void __stw_mmu(void);
417 extern void __stl_mmu(void);
418 extern void __stq_mmu(void);
421 static const void * const qemu_ld_helpers[4] = {
428 static const void * const qemu_st_helpers[4] = {
436 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
439 int addr_reg, data_reg, r0, r1, mem_index, s_bits, bswap, ld_op;
440 #if defined(CONFIG_SOFTMMU)
441 uint8_t *label1_ptr, *label2_ptr;
452 #if TARGET_LONG_BITS == 32
458 #if defined(CONFIG_SOFTMMU)
459 /* srl addr_reg, x, r1 */
460 tcg_out_arithi(s, r1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
462 /* and addr_reg, x, r0 */
463 tcg_out_arithi(s, r0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
467 tcg_out_arithi(s, r1, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS,
471 tcg_out_arithi(s, r1, r1, offsetof(CPUState, tlb_table[mem_index][0].addr_read),
474 /* ld [env + r1], r1 */
475 tcg_out_ldst(s, r1, TCG_AREG0, r1, ld_op);
477 /* subcc r0, r1, %g0 */
478 tcg_out_arith(s, TCG_REG_G0, r0, r1, ARITH_SUBCC);
482 label1_ptr = s->code_ptr;
485 /* mov (delay slot)*/
486 tcg_out_mov(s, r0, addr_reg);
488 /* XXX: move that code at the end of the TB */
489 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
490 - (tcg_target_ulong)s->code_ptr) >> 2)
492 /* mov (delay slot)*/
493 tcg_out_movi(s, TCG_TYPE_I32, r1, mem_index);
497 /* sll i0, 24/56, i0 */
498 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0,
499 sizeof(tcg_target_long) * 8 - 8, SHIFT_SLL);
500 /* sra i0, 24/56, data_reg */
501 tcg_out_arithi(s, data_reg, TCG_REG_I0,
502 sizeof(tcg_target_long) * 8 - 8, SHIFT_SRA);
505 /* sll i0, 16/48, i0 */
506 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0,
507 sizeof(tcg_target_long) * 8 - 16, SHIFT_SLL);
508 /* sra i0, 16/48, data_reg */
509 tcg_out_arithi(s, data_reg, TCG_REG_I0,
510 sizeof(tcg_target_long) * 8 - 16, SHIFT_SRA);
514 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0, 32, SHIFT_SLL);
515 /* sra i0, 32, data_reg */
516 tcg_out_arithi(s, data_reg, TCG_REG_I0, 32, SHIFT_SRA);
524 tcg_out_mov(s, data_reg, TCG_REG_I0);
530 label2_ptr = s->code_ptr;
534 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
535 INSN_OFF22((unsigned long)label1_ptr -
536 (unsigned long)s->code_ptr));
538 /* ld [r1 + x], r1 */
539 tcg_out_ldst(s, r1, r1, offsetof(CPUTLBEntry, addend) -
540 offsetof(CPUTLBEntry, addr_read), ld_op);
542 tcg_out_arith(s, r0, r1, r0, ARITH_ADD);
547 #ifdef TARGET_WORDS_BIGENDIAN
554 /* ldub [r0], data_reg */
555 tcg_out_ldst(s, data_reg, r0, 0, LDUB);
558 /* ldsb [r0], data_reg */
559 tcg_out_ldst(s, data_reg, r0, 0, LDSB);
562 /* lduh [r0], data_reg */
563 tcg_out_ldst(s, data_reg, r0, 0, LDUH);
565 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
569 /* ldsh [r0], data_reg */
570 tcg_out_ldst(s, data_reg, r0, 0, LDSH);
572 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
576 /* lduw [r0], data_reg */
577 tcg_out_ldst(s, data_reg, r0, 0, LDUW);
579 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
583 /* ldsw [r0], data_reg */
584 tcg_out_ldst(s, data_reg, r0, 0, LDSW);
586 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
590 /* ldx [r0], data_reg */
591 tcg_out_ldst(s, data_reg, r0, 0, LDX);
593 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
600 #if defined(CONFIG_SOFTMMU)
602 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
603 INSN_OFF22((unsigned long)label2_ptr -
604 (unsigned long)s->code_ptr));
608 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
611 int addr_reg, data_reg, r0, r1, mem_index, s_bits, bswap, ld_op;
612 #if defined(CONFIG_SOFTMMU)
613 uint8_t *label1_ptr, *label2_ptr;
625 #if TARGET_LONG_BITS == 32
631 #if defined(CONFIG_SOFTMMU)
632 /* srl addr_reg, x, r1 */
633 tcg_out_arithi(s, r1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
635 /* and addr_reg, x, r0 */
636 tcg_out_arithi(s, r0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
640 tcg_out_arithi(s, r1, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS,
644 tcg_out_arithi(s, r1, r1,
645 offsetof(CPUState, tlb_table[mem_index][0].addr_write),
648 /* ld [env + r1], r1 */
649 tcg_out_ldst(s, r1, TCG_AREG0, r1, ld_op);
651 /* subcc r0, r1, %g0 */
652 tcg_out_arith(s, TCG_REG_G0, r0, r1, ARITH_SUBCC);
656 label1_ptr = s->code_ptr;
658 /* mov (delay slot)*/
659 tcg_out_mov(s, r0, addr_reg);
663 /* sll i0, 24/56, i0 */
664 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0,
665 sizeof(tcg_target_long) * 8 - 8, SHIFT_SLL);
666 /* sra i0, 24/56, data_reg */
667 tcg_out_arithi(s, data_reg, TCG_REG_I0,
668 sizeof(tcg_target_long) * 8 - 8, SHIFT_SRA);
671 /* sll i0, 16/48, i0 */
672 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0,
673 sizeof(tcg_target_long) * 8 - 16, SHIFT_SLL);
674 /* sra i0, 16/48, data_reg */
675 tcg_out_arithi(s, data_reg, TCG_REG_I0,
676 sizeof(tcg_target_long) * 8 - 16, SHIFT_SRA);
680 tcg_out_arithi(s, TCG_REG_I0, TCG_REG_I0, 32, SHIFT_SLL);
681 /* sra i0, 32, data_reg */
682 tcg_out_arithi(s, data_reg, TCG_REG_I0, 32, SHIFT_SRA);
690 tcg_out_mov(s, data_reg, TCG_REG_I0);
694 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[s_bits]
695 - (tcg_target_ulong)s->code_ptr) >> 2)
697 /* mov (delay slot)*/
698 tcg_out_movi(s, TCG_TYPE_I32, r1, mem_index);
702 label2_ptr = s->code_ptr;
706 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
707 INSN_OFF22((unsigned long)label1_ptr -
708 (unsigned long)s->code_ptr));
710 /* ld [r1 + x], r1 */
711 tcg_out_ldst(s, r1, r1, offsetof(CPUTLBEntry, addend) -
712 offsetof(CPUTLBEntry, addr_write), ld_op);
714 tcg_out_arith(s, r0, r1, r0, ARITH_ADD);
719 #ifdef TARGET_WORDS_BIGENDIAN
726 /* stb data_reg, [r0] */
727 tcg_out_ldst(s, data_reg, r0, 0, STB);
731 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
733 /* sth data_reg, [r0] */
734 tcg_out_ldst(s, data_reg, r0, 0, STH);
738 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
740 /* stw data_reg, [r0] */
741 tcg_out_ldst(s, data_reg, r0, 0, STW);
745 fprintf(stderr, "unimplemented %s with bswap\n", __func__);
747 /* stx data_reg, [r0] */
748 tcg_out_ldst(s, data_reg, r0, 0, STX);
754 #if defined(CONFIG_SOFTMMU)
756 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
757 INSN_OFF22((unsigned long)label2_ptr -
758 (unsigned long)s->code_ptr));
762 static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
763 const int *const_args)
768 case INDEX_op_exit_tb:
769 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
770 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
772 tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
773 INSN_RS2(TCG_REG_G0));
775 case INDEX_op_goto_tb:
776 if (s->tb_jmp_offset) {
777 /* direct jump method */
778 tcg_out32(s, SETHI | INSN_RD(TCG_REG_I5) |
779 ((args[0] & 0xffffe000) >> 10));
780 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
781 INSN_IMM13((args[0] & 0x1fff)));
782 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
784 /* indirect jump method */
785 tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
786 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
787 INSN_RS2(TCG_REG_G0));
790 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
794 tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
795 - (tcg_target_ulong)s->code_ptr) >> 2)
799 tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
800 tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
801 INSN_RS2(TCG_REG_G0));
807 tcg_out_branch(s, COND_A, args[0]);
810 case INDEX_op_movi_i32:
811 tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
814 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
815 #define OP_32_64(x) \
816 glue(glue(case INDEX_op_, x), _i32:) \
817 glue(glue(case INDEX_op_, x), _i64:)
819 #define OP_32_64(x) \
820 glue(glue(case INDEX_op_, x), _i32:)
823 tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
826 tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
829 tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
832 tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
834 case INDEX_op_ld_i32:
835 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
836 case INDEX_op_ld32u_i64:
838 tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
841 tcg_out_ldst(s, args[0], args[1], args[2], STB);
844 tcg_out_ldst(s, args[0], args[1], args[2], STH);
846 case INDEX_op_st_i32:
847 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
848 case INDEX_op_st32_i64:
850 tcg_out_ldst(s, args[0], args[1], args[2], STW);
867 case INDEX_op_shl_i32:
870 case INDEX_op_shr_i32:
873 case INDEX_op_sar_i32:
876 case INDEX_op_mul_i32:
879 case INDEX_op_div2_i32:
880 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
888 case INDEX_op_divu2_i32:
889 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
898 case INDEX_op_brcond_i32:
899 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
903 case INDEX_op_qemu_ld8u:
904 tcg_out_qemu_ld(s, args, 0);
906 case INDEX_op_qemu_ld8s:
907 tcg_out_qemu_ld(s, args, 0 | 4);
909 case INDEX_op_qemu_ld16u:
910 tcg_out_qemu_ld(s, args, 1);
912 case INDEX_op_qemu_ld16s:
913 tcg_out_qemu_ld(s, args, 1 | 4);
915 case INDEX_op_qemu_ld32u:
916 tcg_out_qemu_ld(s, args, 2);
918 case INDEX_op_qemu_ld32s:
919 tcg_out_qemu_ld(s, args, 2 | 4);
921 case INDEX_op_qemu_st8:
922 tcg_out_qemu_st(s, args, 0);
924 case INDEX_op_qemu_st16:
925 tcg_out_qemu_st(s, args, 1);
927 case INDEX_op_qemu_st32:
928 tcg_out_qemu_st(s, args, 2);
931 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
932 case INDEX_op_movi_i64:
933 tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
935 case INDEX_op_ld32s_i64:
936 tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
938 case INDEX_op_ld_i64:
939 tcg_out_ldst(s, args[0], args[1], args[2], LDX);
941 case INDEX_op_st_i64:
942 tcg_out_ldst(s, args[0], args[1], args[2], STX);
944 case INDEX_op_shl_i64:
947 case INDEX_op_shr_i64:
950 case INDEX_op_sar_i64:
953 case INDEX_op_mul_i64:
956 case INDEX_op_div2_i64:
959 case INDEX_op_divu2_i64:
963 case INDEX_op_brcond_i64:
964 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
967 case INDEX_op_qemu_ld64:
968 tcg_out_qemu_ld(s, args, 3);
970 case INDEX_op_qemu_st64:
971 tcg_out_qemu_st(s, args, 3);
977 tcg_out_arithi(s, args[0], args[1], args[2], c);
979 tcg_out_arith(s, args[0], args[1], args[2], c);
984 fprintf(stderr, "unknown opcode 0x%x\n", opc);
989 static const TCGTargetOpDef sparc_op_defs[] = {
990 { INDEX_op_exit_tb, { } },
991 { INDEX_op_goto_tb, { } },
992 { INDEX_op_call, { "ri" } },
993 { INDEX_op_jmp, { "ri" } },
994 { INDEX_op_br, { } },
996 { INDEX_op_mov_i32, { "r", "r" } },
997 { INDEX_op_movi_i32, { "r" } },
998 { INDEX_op_ld8u_i32, { "r", "r" } },
999 { INDEX_op_ld8s_i32, { "r", "r" } },
1000 { INDEX_op_ld16u_i32, { "r", "r" } },
1001 { INDEX_op_ld16s_i32, { "r", "r" } },
1002 { INDEX_op_ld_i32, { "r", "r" } },
1003 { INDEX_op_st8_i32, { "r", "r" } },
1004 { INDEX_op_st16_i32, { "r", "r" } },
1005 { INDEX_op_st_i32, { "r", "r" } },
1007 { INDEX_op_add_i32, { "r", "r", "rJ" } },
1008 { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1009 { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1010 { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1011 { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1012 { INDEX_op_and_i32, { "r", "r", "rJ" } },
1013 { INDEX_op_or_i32, { "r", "r", "rJ" } },
1014 { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1016 { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1017 { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1018 { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1020 { INDEX_op_brcond_i32, { "r", "ri" } },
1022 { INDEX_op_qemu_ld8u, { "r", "L" } },
1023 { INDEX_op_qemu_ld8s, { "r", "L" } },
1024 { INDEX_op_qemu_ld16u, { "r", "L" } },
1025 { INDEX_op_qemu_ld16s, { "r", "L" } },
1026 { INDEX_op_qemu_ld32u, { "r", "L" } },
1027 { INDEX_op_qemu_ld32s, { "r", "L" } },
1029 { INDEX_op_qemu_st8, { "L", "L" } },
1030 { INDEX_op_qemu_st16, { "L", "L" } },
1031 { INDEX_op_qemu_st32, { "L", "L" } },
1033 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1034 { INDEX_op_mov_i64, { "r", "r" } },
1035 { INDEX_op_movi_i64, { "r" } },
1036 { INDEX_op_ld8u_i64, { "r", "r" } },
1037 { INDEX_op_ld8s_i64, { "r", "r" } },
1038 { INDEX_op_ld16u_i64, { "r", "r" } },
1039 { INDEX_op_ld16s_i64, { "r", "r" } },
1040 { INDEX_op_ld32u_i64, { "r", "r" } },
1041 { INDEX_op_ld32s_i64, { "r", "r" } },
1042 { INDEX_op_ld_i64, { "r", "r" } },
1043 { INDEX_op_st8_i64, { "r", "r" } },
1044 { INDEX_op_st16_i64, { "r", "r" } },
1045 { INDEX_op_st32_i64, { "r", "r" } },
1046 { INDEX_op_st_i64, { "r", "r" } },
1048 { INDEX_op_add_i64, { "r", "r", "rJ" } },
1049 { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1050 { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1051 { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1052 { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1053 { INDEX_op_and_i64, { "r", "r", "rJ" } },
1054 { INDEX_op_or_i64, { "r", "r", "rJ" } },
1055 { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1057 { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1058 { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1059 { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1061 { INDEX_op_brcond_i64, { "r", "ri" } },
1066 void tcg_target_init(TCGContext *s)
1068 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1069 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1070 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1072 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1088 tcg_regset_clear(s->reserved_regs);
1089 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1090 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1091 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1092 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1093 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1094 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1095 tcg_add_target_add_op_defs(sparc_op_defs);