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_tl(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 inline int check_fit_i32(uint32_t val, unsigned int bits)
98 return ((val << (32 - bits)) >> (32 - bits)) == val;
101 static void patch_reloc(uint8_t *code_ptr, int type,
102 tcg_target_long value, tcg_target_long addend)
107 if (value != (uint32_t)value)
109 *(uint32_t *)code_ptr = value;
111 case R_SPARC_WDISP22:
112 value -= (long)code_ptr;
114 if (!check_fit_tl(value, 22))
116 *(uint32_t *)code_ptr = ((*(uint32_t *)code_ptr) & ~0x3fffff) | value;
123 /* maximum number of register used for input function arguments */
124 static inline int tcg_target_get_call_iarg_regs_count(int flags)
129 /* parse target specific constraints */
130 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
137 case 'L': /* qemu_ld/st constraint */
138 ct->ct |= TCG_CT_REG;
139 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
141 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O0);
142 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O1);
143 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O2);
146 ct->ct |= TCG_CT_CONST_S11;
149 ct->ct |= TCG_CT_CONST_S13;
159 /* test if a constant matches the constraint */
160 static inline int tcg_target_const_match(tcg_target_long val,
161 const TCGArgConstraint *arg_ct)
166 if (ct & TCG_CT_CONST)
168 else if ((ct & TCG_CT_CONST_S11) && check_fit_tl(val, 11))
170 else if ((ct & TCG_CT_CONST_S13) && check_fit_tl(val, 13))
176 #define INSN_OP(x) ((x) << 30)
177 #define INSN_OP2(x) ((x) << 22)
178 #define INSN_OP3(x) ((x) << 19)
179 #define INSN_OPF(x) ((x) << 5)
180 #define INSN_RD(x) ((x) << 25)
181 #define INSN_RS1(x) ((x) << 14)
182 #define INSN_RS2(x) (x)
183 #define INSN_ASI(x) ((x) << 5)
185 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
186 #define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
188 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
205 #define BA (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
207 #define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
208 #define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
209 #define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
210 #define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
211 #define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
212 #define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x04))
213 #define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
214 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
215 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
216 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
217 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
218 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
219 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
220 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
221 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
223 #define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
224 #define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
225 #define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
227 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
228 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
229 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
231 #define WRY (INSN_OP(2) | INSN_OP3(0x30))
232 #define JMPL (INSN_OP(2) | INSN_OP3(0x38))
233 #define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
234 #define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
235 #define SETHI (INSN_OP(0) | INSN_OP2(0x4))
236 #define CALL INSN_OP(1)
237 #define LDUB (INSN_OP(3) | INSN_OP3(0x01))
238 #define LDSB (INSN_OP(3) | INSN_OP3(0x09))
239 #define LDUH (INSN_OP(3) | INSN_OP3(0x02))
240 #define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
241 #define LDUW (INSN_OP(3) | INSN_OP3(0x00))
242 #define LDSW (INSN_OP(3) | INSN_OP3(0x08))
243 #define LDX (INSN_OP(3) | INSN_OP3(0x0b))
244 #define STB (INSN_OP(3) | INSN_OP3(0x05))
245 #define STH (INSN_OP(3) | INSN_OP3(0x06))
246 #define STW (INSN_OP(3) | INSN_OP3(0x04))
247 #define STX (INSN_OP(3) | INSN_OP3(0x0e))
248 #define LDUBA (INSN_OP(3) | INSN_OP3(0x11))
249 #define LDSBA (INSN_OP(3) | INSN_OP3(0x19))
250 #define LDUHA (INSN_OP(3) | INSN_OP3(0x12))
251 #define LDSHA (INSN_OP(3) | INSN_OP3(0x1a))
252 #define LDUWA (INSN_OP(3) | INSN_OP3(0x10))
253 #define LDSWA (INSN_OP(3) | INSN_OP3(0x18))
254 #define LDXA (INSN_OP(3) | INSN_OP3(0x1b))
255 #define STBA (INSN_OP(3) | INSN_OP3(0x15))
256 #define STHA (INSN_OP(3) | INSN_OP3(0x16))
257 #define STWA (INSN_OP(3) | INSN_OP3(0x14))
258 #define STXA (INSN_OP(3) | INSN_OP3(0x1e))
260 #ifndef ASI_PRIMARY_LITTLE
261 #define ASI_PRIMARY_LITTLE 0x88
264 static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
267 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
271 static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1, int offset,
274 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
278 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
280 tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
283 static inline void tcg_out_sethi(TCGContext *s, int ret, uint32_t arg)
285 tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
288 static inline void tcg_out_movi_imm13(TCGContext *s, int ret, uint32_t arg)
290 tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
293 static inline void tcg_out_movi_imm32(TCGContext *s, int ret, uint32_t arg)
295 if (check_fit_i32(arg, 12))
296 tcg_out_movi_imm13(s, ret, arg);
298 tcg_out_sethi(s, ret, arg);
300 tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
304 static inline void tcg_out_movi(TCGContext *s, TCGType type,
305 int ret, tcg_target_long arg)
307 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
308 if (!check_fit_tl(arg, 32) && (arg & ~0xffffffffULL) != 0) {
309 tcg_out_movi_imm32(s, TCG_REG_I4, arg >> 32);
310 tcg_out_arithi(s, TCG_REG_I4, TCG_REG_I4, 32, SHIFT_SLLX);
311 tcg_out_movi_imm32(s, ret, arg);
312 tcg_out_arith(s, ret, ret, TCG_REG_I4, ARITH_OR);
315 tcg_out_movi_imm32(s, ret, arg);
318 static inline void tcg_out_ld_raw(TCGContext *s, int ret,
321 tcg_out_sethi(s, ret, arg);
322 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
323 INSN_IMM13(arg & 0x3ff));
326 static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
329 if (!check_fit_tl(arg, 10))
330 tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ffULL);
331 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
332 tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
333 INSN_IMM13(arg & 0x3ff));
335 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
336 INSN_IMM13(arg & 0x3ff));
340 static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
342 if (check_fit_tl(offset, 13))
343 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
346 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
347 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
352 static inline void tcg_out_ldst_asi(TCGContext *s, int ret, int addr,
353 int offset, int op, int asi)
355 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
356 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
357 INSN_ASI(asi) | INSN_RS2(addr));
360 static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
361 int arg1, tcg_target_long arg2)
363 if (type == TCG_TYPE_I32)
364 tcg_out_ldst(s, ret, arg1, arg2, LDUW);
366 tcg_out_ldst(s, ret, arg1, arg2, LDX);
369 static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
370 int arg1, tcg_target_long arg2)
372 if (type == TCG_TYPE_I32)
373 tcg_out_ldst(s, arg, arg1, arg2, STW);
375 tcg_out_ldst(s, arg, arg1, arg2, STX);
378 static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
380 if (val == 0 || val == -1)
381 tcg_out32(s, WRY | INSN_IMM13(val));
383 fprintf(stderr, "unimplemented sety %ld\n", (long)val);
386 static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
389 if (check_fit_tl(val, 13))
390 tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
392 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
393 tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
398 static inline void tcg_out_andi(TCGContext *s, int reg, tcg_target_long val)
401 if (check_fit_tl(val, 13))
402 tcg_out_arithi(s, reg, reg, val, ARITH_AND);
404 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, val);
405 tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_AND);
410 static inline void tcg_out_nop(TCGContext *s)
412 tcg_out_sethi(s, TCG_REG_G0, 0);
415 static void tcg_out_branch(TCGContext *s, int opc, int label_index)
418 TCGLabel *l = &s->labels[label_index];
421 val = l->u.value - (tcg_target_long)s->code_ptr;
422 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
423 | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
425 tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
426 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
430 static const uint8_t tcg_cond_to_bcond[10] = {
431 [TCG_COND_EQ] = COND_E,
432 [TCG_COND_NE] = COND_NE,
433 [TCG_COND_LT] = COND_L,
434 [TCG_COND_GE] = COND_GE,
435 [TCG_COND_LE] = COND_LE,
436 [TCG_COND_GT] = COND_G,
437 [TCG_COND_LTU] = COND_CS,
438 [TCG_COND_GEU] = COND_CC,
439 [TCG_COND_LEU] = COND_LEU,
440 [TCG_COND_GTU] = COND_GU,
443 static void tcg_out_brcond(TCGContext *s, int cond,
444 TCGArg arg1, TCGArg arg2, int const_arg2,
447 if (const_arg2 && arg2 == 0)
448 /* orcc %g0, r, %g0 */
449 tcg_out_arith(s, TCG_REG_G0, TCG_REG_G0, arg1, ARITH_ORCC);
451 /* subcc r1, r2, %g0 */
452 tcg_out_arith(s, TCG_REG_G0, arg1, arg2, ARITH_SUBCC);
453 tcg_out_branch(s, tcg_cond_to_bcond[cond], label_index);
457 /* Generate global QEMU prologue and epilogue code */
458 void tcg_target_qemu_prologue(TCGContext *s)
460 tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
461 INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
462 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
463 INSN_RS2(TCG_REG_G0));
467 #if defined(CONFIG_SOFTMMU)
468 extern void __ldb_mmu(void);
469 extern void __ldw_mmu(void);
470 extern void __ldl_mmu(void);
471 extern void __ldq_mmu(void);
473 extern void __stb_mmu(void);
474 extern void __stw_mmu(void);
475 extern void __stl_mmu(void);
476 extern void __stq_mmu(void);
479 static const void * const qemu_ld_helpers[4] = {
486 static const void * const qemu_st_helpers[4] = {
494 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
497 int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
498 int target_ld_op, host_ld_op, sll_op, sra_op;
499 #if defined(CONFIG_SOFTMMU)
500 uint32_t *label1_ptr, *label2_ptr;
512 #if TARGET_LONG_BITS == 32
529 #if defined(CONFIG_SOFTMMU)
530 /* srl addr_reg, x, arg1 */
531 tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
533 /* and addr_reg, x, arg0 */
534 tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
537 /* and arg1, x, arg1 */
538 tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
540 /* add arg1, x, arg1 */
541 tcg_out_addi(s, arg1, offsetof(CPUState,
542 tlb_table[mem_index][0].addr_read));
544 /* add env, arg1, arg1 */
545 tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
547 /* ld [arg1], arg2 */
548 tcg_out32(s, target_ld_op | INSN_RD(arg2) | INSN_RS1(arg1) |
549 INSN_RS2(TCG_REG_G0));
551 /* subcc arg0, arg2, %g0 */
552 tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
556 label1_ptr = (uint32_t *)s->code_ptr;
559 /* mov (delay slot) */
560 tcg_out_mov(s, arg0, addr_reg);
562 /* XXX: move that code at the end of the TB */
563 /* qemu_ld_helper[s_bits](arg0, arg1) */
564 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
565 - (tcg_target_ulong)s->code_ptr) >> 2)
567 /* mov (delay slot) */
568 tcg_out_movi(s, TCG_TYPE_I32, arg1, mem_index);
570 /* data_reg = sign_extend(arg0) */
573 /* sll arg0, 24/56, data_reg */
574 tcg_out_arithi(s, data_reg, arg0, (int)sizeof(tcg_target_long) * 8 - 8,
576 /* sra data_reg, 24/56, data_reg */
577 tcg_out_arithi(s, data_reg, data_reg,
578 (int)sizeof(tcg_target_long) * 8 - 8, sra_op);
581 /* sll arg0, 16/48, data_reg */
582 tcg_out_arithi(s, data_reg, arg0,
583 (int)sizeof(tcg_target_long) * 8 - 16, sll_op);
584 /* sra data_reg, 16/48, data_reg */
585 tcg_out_arithi(s, data_reg, data_reg,
586 (int)sizeof(tcg_target_long) * 8 - 16, sra_op);
589 /* sll arg0, 32, data_reg */
590 tcg_out_arithi(s, data_reg, arg0, 32, sll_op);
591 /* sra data_reg, 32, data_reg */
592 tcg_out_arithi(s, data_reg, data_reg, 32, sra_op);
600 tcg_out_mov(s, data_reg, arg0);
606 label2_ptr = (uint32_t *)s->code_ptr;
609 /* nop (delay slot */
613 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
614 INSN_OFF22((unsigned long)s->code_ptr -
615 (unsigned long)label1_ptr));
617 /* ld [arg1 + x], arg1 */
618 tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
619 offsetof(CPUTLBEntry, addr_read), host_ld_op);
620 /* add addr_reg, arg1, arg0 */
621 tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
628 /* ldub [arg0], data_reg */
629 tcg_out_ldst(s, data_reg, arg0, 0, LDUB);
632 /* ldsb [arg0], data_reg */
633 tcg_out_ldst(s, data_reg, arg0, 0, LDSB);
636 #ifdef TARGET_WORDS_BIGENDIAN
637 /* lduh [arg0], data_reg */
638 tcg_out_ldst(s, data_reg, arg0, 0, LDUH);
640 /* lduha [arg0] ASI_PRIMARY_LITTLE, data_reg */
641 tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUHA, ASI_PRIMARY_LITTLE);
645 #ifdef TARGET_WORDS_BIGENDIAN
646 /* ldsh [arg0], data_reg */
647 tcg_out_ldst(s, data_reg, arg0, 0, LDSH);
649 /* ldsha [arg0] ASI_PRIMARY_LITTLE, data_reg */
650 tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSHA, ASI_PRIMARY_LITTLE);
654 #ifdef TARGET_WORDS_BIGENDIAN
655 /* lduw [arg0], data_reg */
656 tcg_out_ldst(s, data_reg, arg0, 0, LDUW);
658 /* lduwa [arg0] ASI_PRIMARY_LITTLE, data_reg */
659 tcg_out_ldst_asi(s, data_reg, arg0, 0, LDUWA, ASI_PRIMARY_LITTLE);
663 #ifdef TARGET_WORDS_BIGENDIAN
664 /* ldsw [arg0], data_reg */
665 tcg_out_ldst(s, data_reg, arg0, 0, LDSW);
667 /* ldswa [arg0] ASI_PRIMARY_LITTLE, data_reg */
668 tcg_out_ldst_asi(s, data_reg, arg0, 0, LDSWA, ASI_PRIMARY_LITTLE);
672 #ifdef TARGET_WORDS_BIGENDIAN
673 /* ldx [arg0], data_reg */
674 tcg_out_ldst(s, data_reg, arg0, 0, LDX);
676 /* ldxa [arg0] ASI_PRIMARY_LITTLE, data_reg */
677 tcg_out_ldst_asi(s, data_reg, arg0, 0, LDXA, ASI_PRIMARY_LITTLE);
684 #if defined(CONFIG_SOFTMMU)
686 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
687 INSN_OFF22((unsigned long)s->code_ptr -
688 (unsigned long)label2_ptr));
692 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
695 int addr_reg, data_reg, arg0, arg1, arg2, mem_index, s_bits;
696 int target_ld_op, host_ld_op;
697 #if defined(CONFIG_SOFTMMU)
698 uint32_t *label1_ptr, *label2_ptr;
711 #if TARGET_LONG_BITS == 32
723 #if defined(CONFIG_SOFTMMU)
724 /* srl addr_reg, x, arg1 */
725 tcg_out_arithi(s, arg1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
728 /* and addr_reg, x, arg0 */
729 tcg_out_arithi(s, arg0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
732 /* and arg1, x, arg1 */
733 tcg_out_andi(s, arg1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
735 /* add arg1, x, arg1 */
736 tcg_out_addi(s, arg1, offsetof(CPUState,
737 tlb_table[mem_index][0].addr_write));
739 /* add env, arg1, arg1 */
740 tcg_out_arith(s, arg1, TCG_AREG0, arg1, ARITH_ADD);
742 /* ld [arg1], arg2 */
743 tcg_out32(s, target_ld_op | INSN_RD(arg2) | INSN_RS1(arg1) |
744 INSN_RS2(TCG_REG_G0));
746 /* subcc arg0, arg2, %g0 */
747 tcg_out_arith(s, TCG_REG_G0, arg0, arg2, ARITH_SUBCC);
751 label1_ptr = (uint32_t *)s->code_ptr;
754 /* mov (delay slot) */
755 tcg_out_mov(s, arg0, addr_reg);
758 tcg_out_mov(s, arg1, data_reg);
760 /* XXX: move that code at the end of the TB */
761 /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
762 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[s_bits]
763 - (tcg_target_ulong)s->code_ptr) >> 2)
765 /* mov (delay slot) */
766 tcg_out_movi(s, TCG_TYPE_I32, arg2, mem_index);
770 label2_ptr = (uint32_t *)s->code_ptr;
773 /* nop (delay slot) */
777 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
778 INSN_OFF22((unsigned long)s->code_ptr -
779 (unsigned long)label1_ptr));
781 /* ld [arg1 + x], arg1 */
782 tcg_out_ldst(s, arg1, arg1, offsetof(CPUTLBEntry, addend) -
783 offsetof(CPUTLBEntry, addr_write), host_ld_op);
785 /* add addr_reg, arg1, arg0 */
786 tcg_out_arith(s, arg0, addr_reg, arg1, ARITH_ADD);
793 /* stb data_reg, [arg0] */
794 tcg_out_ldst(s, data_reg, arg0, 0, STB);
797 #ifdef TARGET_WORDS_BIGENDIAN
798 /* sth data_reg, [arg0] */
799 tcg_out_ldst(s, data_reg, arg0, 0, STH);
801 /* stha data_reg, [arg0] ASI_PRIMARY_LITTLE */
802 tcg_out_ldst_asi(s, data_reg, arg0, 0, STHA, ASI_PRIMARY_LITTLE);
806 #ifdef TARGET_WORDS_BIGENDIAN
807 /* stw data_reg, [arg0] */
808 tcg_out_ldst(s, data_reg, arg0, 0, STW);
810 /* stwa data_reg, [arg0] ASI_PRIMARY_LITTLE */
811 tcg_out_ldst_asi(s, data_reg, arg0, 0, STWA, ASI_PRIMARY_LITTLE);
815 #ifdef TARGET_WORDS_BIGENDIAN
816 /* stx data_reg, [arg0] */
817 tcg_out_ldst(s, data_reg, arg0, 0, STX);
819 /* stxa data_reg, [arg0] ASI_PRIMARY_LITTLE */
820 tcg_out_ldst_asi(s, data_reg, arg0, 0, STXA, ASI_PRIMARY_LITTLE);
827 #if defined(CONFIG_SOFTMMU)
829 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
830 INSN_OFF22((unsigned long)s->code_ptr -
831 (unsigned long)label2_ptr));
835 static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
836 const int *const_args)
841 case INDEX_op_exit_tb:
842 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
843 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
845 tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
846 INSN_RS2(TCG_REG_G0));
848 case INDEX_op_goto_tb:
849 if (s->tb_jmp_offset) {
850 /* direct jump method */
851 tcg_out_sethi(s, TCG_REG_I5, args[0] & 0xffffe000);
852 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
853 INSN_IMM13((args[0] & 0x1fff)));
854 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
856 /* indirect jump method */
857 tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
858 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
859 INSN_RS2(TCG_REG_G0));
862 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
866 unsigned int st_op, ld_op;
876 tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
877 - (tcg_target_ulong)s->code_ptr) >> 2)
880 tcg_out_ld_ptr(s, TCG_REG_I5,
881 (tcg_target_long)(s->tb_next + args[0]));
882 tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
883 INSN_RS2(TCG_REG_G0));
885 /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
887 tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
888 TCG_TARGET_CALL_STACK_OFFSET - sizeof(long),
889 st_op); // delay slot
890 tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
891 TCG_TARGET_CALL_STACK_OFFSET - sizeof(long),
897 tcg_out_branch(s, COND_A, args[0]);
900 case INDEX_op_movi_i32:
901 tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
904 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
905 #define OP_32_64(x) \
906 glue(glue(case INDEX_op_, x), _i32:) \
907 glue(glue(case INDEX_op_, x), _i64:)
909 #define OP_32_64(x) \
910 glue(glue(case INDEX_op_, x), _i32:)
913 tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
916 tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
919 tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
922 tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
924 case INDEX_op_ld_i32:
925 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
926 case INDEX_op_ld32u_i64:
928 tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
931 tcg_out_ldst(s, args[0], args[1], args[2], STB);
934 tcg_out_ldst(s, args[0], args[1], args[2], STH);
936 case INDEX_op_st_i32:
937 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
938 case INDEX_op_st32_i64:
940 tcg_out_ldst(s, args[0], args[1], args[2], STW);
957 case INDEX_op_shl_i32:
960 case INDEX_op_shr_i32:
963 case INDEX_op_sar_i32:
966 case INDEX_op_mul_i32:
969 case INDEX_op_div2_i32:
970 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
978 case INDEX_op_divu2_i32:
979 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
988 case INDEX_op_brcond_i32:
989 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
993 case INDEX_op_qemu_ld8u:
994 tcg_out_qemu_ld(s, args, 0);
996 case INDEX_op_qemu_ld8s:
997 tcg_out_qemu_ld(s, args, 0 | 4);
999 case INDEX_op_qemu_ld16u:
1000 tcg_out_qemu_ld(s, args, 1);
1002 case INDEX_op_qemu_ld16s:
1003 tcg_out_qemu_ld(s, args, 1 | 4);
1005 case INDEX_op_qemu_ld32u:
1006 tcg_out_qemu_ld(s, args, 2);
1008 case INDEX_op_qemu_ld32s:
1009 tcg_out_qemu_ld(s, args, 2 | 4);
1011 case INDEX_op_qemu_st8:
1012 tcg_out_qemu_st(s, args, 0);
1014 case INDEX_op_qemu_st16:
1015 tcg_out_qemu_st(s, args, 1);
1017 case INDEX_op_qemu_st32:
1018 tcg_out_qemu_st(s, args, 2);
1021 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1022 case INDEX_op_movi_i64:
1023 tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1025 case INDEX_op_ld32s_i64:
1026 tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
1028 case INDEX_op_ld_i64:
1029 tcg_out_ldst(s, args[0], args[1], args[2], LDX);
1031 case INDEX_op_st_i64:
1032 tcg_out_ldst(s, args[0], args[1], args[2], STX);
1034 case INDEX_op_shl_i64:
1037 case INDEX_op_shr_i64:
1040 case INDEX_op_sar_i64:
1043 case INDEX_op_mul_i64:
1046 case INDEX_op_div2_i64:
1049 case INDEX_op_divu2_i64:
1053 case INDEX_op_brcond_i64:
1054 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1057 case INDEX_op_qemu_ld64:
1058 tcg_out_qemu_ld(s, args, 3);
1060 case INDEX_op_qemu_st64:
1061 tcg_out_qemu_st(s, args, 3);
1066 if (const_args[2]) {
1067 tcg_out_arithi(s, args[0], args[1], args[2], c);
1069 tcg_out_arith(s, args[0], args[1], args[2], c);
1074 fprintf(stderr, "unknown opcode 0x%x\n", opc);
1079 static const TCGTargetOpDef sparc_op_defs[] = {
1080 { INDEX_op_exit_tb, { } },
1081 { INDEX_op_goto_tb, { } },
1082 { INDEX_op_call, { "ri" } },
1083 { INDEX_op_jmp, { "ri" } },
1084 { INDEX_op_br, { } },
1086 { INDEX_op_mov_i32, { "r", "r" } },
1087 { INDEX_op_movi_i32, { "r" } },
1088 { INDEX_op_ld8u_i32, { "r", "r" } },
1089 { INDEX_op_ld8s_i32, { "r", "r" } },
1090 { INDEX_op_ld16u_i32, { "r", "r" } },
1091 { INDEX_op_ld16s_i32, { "r", "r" } },
1092 { INDEX_op_ld_i32, { "r", "r" } },
1093 { INDEX_op_st8_i32, { "r", "r" } },
1094 { INDEX_op_st16_i32, { "r", "r" } },
1095 { INDEX_op_st_i32, { "r", "r" } },
1097 { INDEX_op_add_i32, { "r", "r", "rJ" } },
1098 { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1099 { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1100 { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1101 { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1102 { INDEX_op_and_i32, { "r", "r", "rJ" } },
1103 { INDEX_op_or_i32, { "r", "r", "rJ" } },
1104 { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1106 { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1107 { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1108 { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1110 { INDEX_op_brcond_i32, { "r", "ri" } },
1112 { INDEX_op_qemu_ld8u, { "r", "L" } },
1113 { INDEX_op_qemu_ld8s, { "r", "L" } },
1114 { INDEX_op_qemu_ld16u, { "r", "L" } },
1115 { INDEX_op_qemu_ld16s, { "r", "L" } },
1116 { INDEX_op_qemu_ld32u, { "r", "L" } },
1117 { INDEX_op_qemu_ld32s, { "r", "L" } },
1119 { INDEX_op_qemu_st8, { "L", "L" } },
1120 { INDEX_op_qemu_st16, { "L", "L" } },
1121 { INDEX_op_qemu_st32, { "L", "L" } },
1123 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1124 { INDEX_op_mov_i64, { "r", "r" } },
1125 { INDEX_op_movi_i64, { "r" } },
1126 { INDEX_op_ld8u_i64, { "r", "r" } },
1127 { INDEX_op_ld8s_i64, { "r", "r" } },
1128 { INDEX_op_ld16u_i64, { "r", "r" } },
1129 { INDEX_op_ld16s_i64, { "r", "r" } },
1130 { INDEX_op_ld32u_i64, { "r", "r" } },
1131 { INDEX_op_ld32s_i64, { "r", "r" } },
1132 { INDEX_op_ld_i64, { "r", "r" } },
1133 { INDEX_op_st8_i64, { "r", "r" } },
1134 { INDEX_op_st16_i64, { "r", "r" } },
1135 { INDEX_op_st32_i64, { "r", "r" } },
1136 { INDEX_op_st_i64, { "r", "r" } },
1137 { INDEX_op_qemu_ld64, { "L", "L" } },
1138 { INDEX_op_qemu_st64, { "L", "L" } },
1140 { INDEX_op_add_i64, { "r", "r", "rJ" } },
1141 { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1142 { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1143 { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1144 { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1145 { INDEX_op_and_i64, { "r", "r", "rJ" } },
1146 { INDEX_op_or_i64, { "r", "r", "rJ" } },
1147 { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1149 { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1150 { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1151 { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1153 { INDEX_op_brcond_i64, { "r", "ri" } },
1158 void tcg_target_init(TCGContext *s)
1160 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1161 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1162 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1164 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1180 tcg_regset_clear(s->reserved_regs);
1181 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1182 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1183 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I4); // for internal use
1185 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1186 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1187 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1188 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1189 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1190 tcg_add_target_add_op_defs(sparc_op_defs);