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);
145 tcg_regset_reset_reg(ct->u.regs, TCG_REG_L0);
146 tcg_regset_reset_reg(ct->u.regs, TCG_REG_L1);
149 ct->ct |= TCG_CT_CONST_S11;
152 ct->ct |= TCG_CT_CONST_S13;
162 /* test if a constant matches the constraint */
163 static inline int tcg_target_const_match(tcg_target_long val,
164 const TCGArgConstraint *arg_ct)
169 if (ct & TCG_CT_CONST)
171 else if ((ct & TCG_CT_CONST_S11) && check_fit_tl(val, 11))
173 else if ((ct & TCG_CT_CONST_S13) && check_fit_tl(val, 13))
179 #define INSN_OP(x) ((x) << 30)
180 #define INSN_OP2(x) ((x) << 22)
181 #define INSN_OP3(x) ((x) << 19)
182 #define INSN_OPF(x) ((x) << 5)
183 #define INSN_RD(x) ((x) << 25)
184 #define INSN_RS1(x) ((x) << 14)
185 #define INSN_RS2(x) (x)
186 #define INSN_ASI(x) ((x) << 5)
188 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
189 #define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
191 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
208 #define BA (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
210 #define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
211 #define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
212 #define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
213 #define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
214 #define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
215 #define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x04))
216 #define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
217 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
218 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
219 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
220 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
221 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
222 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
223 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
224 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
226 #define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
227 #define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
228 #define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
230 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
231 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
232 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
234 #define WRY (INSN_OP(2) | INSN_OP3(0x30))
235 #define JMPL (INSN_OP(2) | INSN_OP3(0x38))
236 #define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
237 #define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
238 #define SETHI (INSN_OP(0) | INSN_OP2(0x4))
239 #define CALL INSN_OP(1)
240 #define LDUB (INSN_OP(3) | INSN_OP3(0x01))
241 #define LDSB (INSN_OP(3) | INSN_OP3(0x09))
242 #define LDUH (INSN_OP(3) | INSN_OP3(0x02))
243 #define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
244 #define LDUW (INSN_OP(3) | INSN_OP3(0x00))
245 #define LDSW (INSN_OP(3) | INSN_OP3(0x08))
246 #define LDX (INSN_OP(3) | INSN_OP3(0x0b))
247 #define STB (INSN_OP(3) | INSN_OP3(0x05))
248 #define STH (INSN_OP(3) | INSN_OP3(0x06))
249 #define STW (INSN_OP(3) | INSN_OP3(0x04))
250 #define STX (INSN_OP(3) | INSN_OP3(0x0e))
251 #define LDUBA (INSN_OP(3) | INSN_OP3(0x11))
252 #define LDSBA (INSN_OP(3) | INSN_OP3(0x19))
253 #define LDUHA (INSN_OP(3) | INSN_OP3(0x12))
254 #define LDSHA (INSN_OP(3) | INSN_OP3(0x1a))
255 #define LDUWA (INSN_OP(3) | INSN_OP3(0x10))
256 #define LDSWA (INSN_OP(3) | INSN_OP3(0x18))
257 #define LDXA (INSN_OP(3) | INSN_OP3(0x1b))
258 #define STBA (INSN_OP(3) | INSN_OP3(0x15))
259 #define STHA (INSN_OP(3) | INSN_OP3(0x16))
260 #define STWA (INSN_OP(3) | INSN_OP3(0x14))
261 #define STXA (INSN_OP(3) | INSN_OP3(0x1e))
263 #ifndef ASI_PRIMARY_LITTLE
264 #define ASI_PRIMARY_LITTLE 0x88
267 static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
270 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
274 static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1, int offset,
277 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
281 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
283 tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
286 static inline void tcg_out_sethi(TCGContext *s, int ret, uint32_t arg)
288 tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
291 static inline void tcg_out_movi_imm13(TCGContext *s, int ret, uint32_t arg)
293 tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
296 static inline void tcg_out_movi_imm32(TCGContext *s, int ret, uint32_t arg)
298 if (check_fit_i32(arg, 12))
299 tcg_out_movi_imm13(s, ret, arg);
301 tcg_out_sethi(s, ret, arg);
303 tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
307 static inline void tcg_out_movi(TCGContext *s, TCGType type,
308 int ret, tcg_target_long arg)
310 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
311 if (!check_fit_tl(arg, 32) && (arg & ~0xffffffffULL) != 0) {
312 tcg_out_movi_imm32(s, TCG_REG_I4, arg >> 32);
313 tcg_out_arithi(s, TCG_REG_I4, TCG_REG_I4, 32, SHIFT_SLLX);
314 tcg_out_movi_imm32(s, ret, arg);
315 tcg_out_arith(s, ret, ret, TCG_REG_I4, ARITH_OR);
318 tcg_out_movi_imm32(s, ret, arg);
321 static inline void tcg_out_ld_raw(TCGContext *s, int ret,
324 tcg_out_sethi(s, ret, arg);
325 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
326 INSN_IMM13(arg & 0x3ff));
329 static inline void tcg_out_ld_ptr(TCGContext *s, int ret,
332 if (!check_fit_tl(arg, 10))
333 tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ffULL);
334 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
335 tcg_out32(s, LDX | INSN_RD(ret) | INSN_RS1(ret) |
336 INSN_IMM13(arg & 0x3ff));
338 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
339 INSN_IMM13(arg & 0x3ff));
343 static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
345 if (check_fit_tl(offset, 13))
346 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
349 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
350 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
355 static inline void tcg_out_ldst_asi(TCGContext *s, int ret, int addr,
356 int offset, int op, int asi)
358 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, offset);
359 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(TCG_REG_I5) |
360 INSN_ASI(asi) | INSN_RS2(addr));
363 static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
364 int arg1, tcg_target_long arg2)
366 if (type == TCG_TYPE_I32)
367 tcg_out_ldst(s, ret, arg1, arg2, LDUW);
369 tcg_out_ldst(s, ret, arg1, arg2, LDX);
372 static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
373 int arg1, tcg_target_long arg2)
375 if (type == TCG_TYPE_I32)
376 tcg_out_ldst(s, arg, arg1, arg2, STW);
378 tcg_out_ldst(s, arg, arg1, arg2, STX);
381 static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
383 if (val == 0 || val == -1)
384 tcg_out32(s, WRY | INSN_IMM13(val));
386 fprintf(stderr, "unimplemented sety %ld\n", (long)val);
389 static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
392 if (check_fit_tl(val, 13))
393 tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
395 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I5, val);
396 tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_ADD);
401 static inline void tcg_out_andi(TCGContext *s, int reg, tcg_target_long val)
404 if (check_fit_tl(val, 13))
405 tcg_out_arithi(s, reg, reg, val, ARITH_AND);
407 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_I5, val);
408 tcg_out_arith(s, reg, reg, TCG_REG_I5, ARITH_AND);
413 static inline void tcg_out_nop(TCGContext *s)
415 tcg_out_sethi(s, TCG_REG_G0, 0);
418 static void tcg_out_branch(TCGContext *s, int opc, int label_index)
421 TCGLabel *l = &s->labels[label_index];
424 val = l->u.value - (tcg_target_long)s->code_ptr;
425 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2)
426 | INSN_OFF22(l->u.value - (unsigned long)s->code_ptr)));
428 tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP22, label_index, 0);
429 tcg_out32(s, (INSN_OP(0) | INSN_COND(opc, 0) | INSN_OP2(0x2) | 0));
433 static const uint8_t tcg_cond_to_bcond[10] = {
434 [TCG_COND_EQ] = COND_E,
435 [TCG_COND_NE] = COND_NE,
436 [TCG_COND_LT] = COND_L,
437 [TCG_COND_GE] = COND_GE,
438 [TCG_COND_LE] = COND_LE,
439 [TCG_COND_GT] = COND_G,
440 [TCG_COND_LTU] = COND_CS,
441 [TCG_COND_GEU] = COND_CC,
442 [TCG_COND_LEU] = COND_LEU,
443 [TCG_COND_GTU] = COND_GU,
446 static void tcg_out_brcond(TCGContext *s, int cond,
447 TCGArg arg1, TCGArg arg2, int const_arg2,
450 if (const_arg2 && arg2 == 0)
451 /* orcc %g0, r, %g0 */
452 tcg_out_arith(s, TCG_REG_G0, TCG_REG_G0, arg1, ARITH_ORCC);
454 /* subcc r1, r2, %g0 */
455 tcg_out_arith(s, TCG_REG_G0, arg1, arg2, ARITH_SUBCC);
456 tcg_out_branch(s, tcg_cond_to_bcond[cond], label_index);
460 /* Generate global QEMU prologue and epilogue code */
461 void tcg_target_qemu_prologue(TCGContext *s)
463 tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
464 INSN_IMM13(-TCG_TARGET_STACK_MINFRAME));
465 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I0) |
466 INSN_RS2(TCG_REG_G0));
470 #if defined(CONFIG_SOFTMMU)
471 extern void __ldb_mmu(void);
472 extern void __ldw_mmu(void);
473 extern void __ldl_mmu(void);
474 extern void __ldq_mmu(void);
476 extern void __stb_mmu(void);
477 extern void __stw_mmu(void);
478 extern void __stl_mmu(void);
479 extern void __stq_mmu(void);
482 static const void * const qemu_ld_helpers[4] = {
489 static const void * const qemu_st_helpers[4] = {
497 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
500 int addr_reg, data_reg, r0, r1, arg0, arg1, mem_index, s_bits;
501 int target_ld_op, host_ld_op;
502 #if defined(CONFIG_SOFTMMU)
503 uint32_t *label1_ptr, *label2_ptr;
516 #if TARGET_LONG_BITS == 32
529 #if defined(CONFIG_SOFTMMU)
530 /* srl addr_reg, x, r1 */
531 tcg_out_arithi(s, r1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
533 /* and addr_reg, x, r0 */
534 tcg_out_arithi(s, r0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
538 tcg_out_andi(s, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
541 tcg_out_addi(s, r1, offsetof(CPUState, tlb_table[mem_index][0].addr_read));
543 /* add env, r1, r1 */
544 tcg_out_arith(s, r1, TCG_AREG0, r1, ARITH_ADD);
547 tcg_out32(s, target_ld_op | INSN_RD(arg1) | INSN_RS1(r1) | INSN_RS2(TCG_REG_G0));
549 /* subcc r0, arg1, %g0 */
550 tcg_out_arith(s, TCG_REG_G0, r0, arg1, ARITH_SUBCC);
554 label1_ptr = (uint32_t *)s->code_ptr;
557 /* mov (delay slot) */
558 tcg_out_mov(s, arg0, addr_reg);
560 /* XXX: move that code at the end of the TB */
561 /* qemu_ld_helper[s_bits](arg0, arg1) */
562 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_ld_helpers[s_bits]
563 - (tcg_target_ulong)s->code_ptr) >> 2)
565 /* mov (delay slot) */
566 tcg_out_movi(s, TCG_TYPE_I32, arg1, mem_index);
568 /* data_reg = sign_extend(arg0) */
571 /* sll arg0, 24/56, data_reg */
572 tcg_out_arithi(s, data_reg, arg0, sizeof(tcg_target_long) * 8 - 8,
574 /* sra data_reg, 24/56, data_reg */
575 tcg_out_arithi(s, data_reg, data_reg, sizeof(tcg_target_long) * 8 - 8,
579 /* sll arg0, 16/48, data_reg */
580 tcg_out_arithi(s, data_reg, arg0, sizeof(tcg_target_long) * 8 - 16,
582 /* sra data_reg, 16/48, data_reg */
583 tcg_out_arithi(s, data_reg, data_reg, sizeof(tcg_target_long) * 8 - 16,
587 /* sll arg0, 32, data_reg */
588 tcg_out_arithi(s, data_reg, arg0, 32, SHIFT_SLL);
589 /* sra data_reg, 32, data_reg */
590 tcg_out_arithi(s, data_reg, data_reg, 32, SHIFT_SRA);
598 tcg_out_mov(s, data_reg, arg0);
604 label2_ptr = (uint32_t *)s->code_ptr;
607 /* nop (delay slot */
611 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
612 INSN_OFF22((unsigned long)s->code_ptr -
613 (unsigned long)label1_ptr));
615 /* ld [r1 + x], r1 */
616 tcg_out_ldst(s, r1, r1, offsetof(CPUTLBEntry, addend) -
617 offsetof(CPUTLBEntry, addr_read), host_ld_op);
619 tcg_out_arith(s, r0, r1, r0, ARITH_ADD);
626 /* ldub [r0], data_reg */
627 tcg_out_ldst(s, data_reg, r0, 0, LDUB);
630 /* ldsb [r0], data_reg */
631 tcg_out_ldst(s, data_reg, r0, 0, LDSB);
634 #ifdef TARGET_WORDS_BIGENDIAN
635 /* lduh [r0], data_reg */
636 tcg_out_ldst(s, data_reg, r0, 0, LDUH);
638 /* lduha [r0] ASI_PRIMARY_LITTLE, data_reg */
639 tcg_out_ldst_asi(s, data_reg, r0, 0, LDUHA, ASI_PRIMARY_LITTLE);
643 #ifdef TARGET_WORDS_BIGENDIAN
644 /* ldsh [r0], data_reg */
645 tcg_out_ldst(s, data_reg, r0, 0, LDSH);
647 /* ldsha [r0] ASI_PRIMARY_LITTLE, data_reg */
648 tcg_out_ldst_asi(s, data_reg, r0, 0, LDSHA, ASI_PRIMARY_LITTLE);
652 #ifdef TARGET_WORDS_BIGENDIAN
653 /* lduw [r0], data_reg */
654 tcg_out_ldst(s, data_reg, r0, 0, LDUW);
656 /* lduwa [r0] ASI_PRIMARY_LITTLE, data_reg */
657 tcg_out_ldst_asi(s, data_reg, r0, 0, LDUWA, ASI_PRIMARY_LITTLE);
661 #ifdef TARGET_WORDS_BIGENDIAN
662 /* ldsw [r0], data_reg */
663 tcg_out_ldst(s, data_reg, r0, 0, LDSW);
665 /* ldswa [r0] ASI_PRIMARY_LITTLE, data_reg */
666 tcg_out_ldst_asi(s, data_reg, r0, 0, LDSWA, ASI_PRIMARY_LITTLE);
670 #ifdef TARGET_WORDS_BIGENDIAN
671 /* ldx [r0], data_reg */
672 tcg_out_ldst(s, data_reg, r0, 0, LDX);
674 /* ldxa [r0] ASI_PRIMARY_LITTLE, data_reg */
675 tcg_out_ldst_asi(s, data_reg, r0, 0, LDXA, ASI_PRIMARY_LITTLE);
682 #if defined(CONFIG_SOFTMMU)
684 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
685 INSN_OFF22((unsigned long)s->code_ptr -
686 (unsigned long)label2_ptr));
690 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
693 int addr_reg, data_reg, r0, r1, arg0, arg1, arg2, mem_index, s_bits;
694 int target_ld_op, host_ld_op;
695 #if defined(CONFIG_SOFTMMU)
696 uint32_t *label1_ptr, *label2_ptr;
711 #if TARGET_LONG_BITS == 32
723 #if defined(CONFIG_SOFTMMU)
724 /* srl addr_reg, x, r1 */
725 tcg_out_arithi(s, r1, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS,
728 /* and addr_reg, x, r0 */
729 tcg_out_arithi(s, r0, addr_reg, TARGET_PAGE_MASK | ((1 << s_bits) - 1),
733 tcg_out_andi(s, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
736 tcg_out_addi(s, r1, offsetof(CPUState, tlb_table[mem_index][0].addr_write));
738 /* add env, r1, r1 */
739 tcg_out_arith(s, r1, TCG_AREG0, r1, ARITH_ADD);
742 tcg_out32(s, target_ld_op | INSN_RD(arg1) | INSN_RS1(r1) | INSN_RS2(TCG_REG_G0));
744 /* subcc r0, arg1, %g0 */
745 tcg_out_arith(s, TCG_REG_G0, r0, arg1, ARITH_SUBCC);
749 label1_ptr = (uint32_t *)s->code_ptr;
752 /* mov (delay slot) */
753 tcg_out_mov(s, arg0, addr_reg);
755 /* arg1 = sign_extend(data_reg); */
758 /* sll data_reg, 24/56, arg1 */
759 tcg_out_arithi(s, arg1, data_reg, sizeof(tcg_target_long) * 8 - 8, SHIFT_SLL);
760 /* sra arg1, 24/56, arg1 */
761 tcg_out_arithi(s, arg1, arg1, sizeof(tcg_target_long) * 8 - 8,
765 /* sll data_reg, 16/48, arg1 */
766 tcg_out_arithi(s, data_reg, arg1, sizeof(tcg_target_long) * 8 - 16, SHIFT_SLL);
767 /* sra arg1, 16/48, arg1 */
768 tcg_out_arithi(s, arg1, arg1, sizeof(tcg_target_long) * 8 - 16,
772 /* sll data_reg, 32, arg1 */
773 tcg_out_arithi(s, data_reg, arg1, 32, SHIFT_SLL);
774 /* sra arg1, 32, arg1 */
775 tcg_out_arithi(s, arg1, arg1, 32, SHIFT_SRA);
783 tcg_out_mov(s, arg1, data_reg);
788 tcg_out_mov(s, arg0, addr_reg);
790 /* XXX: move that code at the end of the TB */
791 /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
792 tcg_out32(s, CALL | ((((tcg_target_ulong)qemu_st_helpers[s_bits]
793 - (tcg_target_ulong)s->code_ptr) >> 2)
795 /* mov (delay slot) */
796 tcg_out_movi(s, TCG_TYPE_I32, arg2, mem_index);
800 label2_ptr = (uint32_t *)s->code_ptr;
803 /* nop (delay slot) */
807 *label1_ptr = (INSN_OP(0) | INSN_COND(COND_E, 0) | INSN_OP2(0x2) |
808 INSN_OFF22((unsigned long)s->code_ptr -
809 (unsigned long)label1_ptr));
811 /* ld [r1 + x], r1 */
812 tcg_out_ldst(s, arg1, r1, offsetof(CPUTLBEntry, addend) -
813 offsetof(CPUTLBEntry, addr_write), host_ld_op);
816 tcg_out_arith(s, r0, arg1, r0, ARITH_ADD);
823 /* stb data_reg, [r0] */
824 tcg_out_ldst(s, data_reg, r0, 0, STB);
827 #ifdef TARGET_WORDS_BIGENDIAN
828 /* sth data_reg, [r0] */
829 tcg_out_ldst(s, data_reg, r0, 0, STH);
831 /* stha data_reg, [r0] ASI_PRIMARY_LITTLE */
832 tcg_out_ldst_asi(s, data_reg, r0, 0, STHA, ASI_PRIMARY_LITTLE);
836 #ifdef TARGET_WORDS_BIGENDIAN
837 /* stw data_reg, [r0] */
838 tcg_out_ldst(s, data_reg, r0, 0, STW);
840 /* stwa data_reg, [r0] ASI_PRIMARY_LITTLE */
841 tcg_out_ldst_asi(s, data_reg, r0, 0, STWA, ASI_PRIMARY_LITTLE);
845 #ifdef TARGET_WORDS_BIGENDIAN
846 /* stx data_reg, [r0] */
847 tcg_out_ldst(s, data_reg, r0, 0, STX);
849 /* stxa data_reg, [r0] ASI_PRIMARY_LITTLE */
850 tcg_out_ldst_asi(s, data_reg, r0, 0, STXA, ASI_PRIMARY_LITTLE);
857 #if defined(CONFIG_SOFTMMU)
859 *label2_ptr = (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2) |
860 INSN_OFF22((unsigned long)s->code_ptr -
861 (unsigned long)label2_ptr));
865 static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
866 const int *const_args)
871 case INDEX_op_exit_tb:
872 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, args[0]);
873 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I7) |
875 tcg_out32(s, RESTORE | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_G0) |
876 INSN_RS2(TCG_REG_G0));
878 case INDEX_op_goto_tb:
879 if (s->tb_jmp_offset) {
880 /* direct jump method */
881 tcg_out_sethi(s, TCG_REG_I5, args[0] & 0xffffe000);
882 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
883 INSN_IMM13((args[0] & 0x1fff)));
884 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
886 /* indirect jump method */
887 tcg_out_ld_ptr(s, TCG_REG_I5, (tcg_target_long)(s->tb_next + args[0]));
888 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_I5) |
889 INSN_RS2(TCG_REG_G0));
892 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
896 unsigned int st_op, ld_op;
906 tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
907 - (tcg_target_ulong)s->code_ptr) >> 2)
910 tcg_out_ld_ptr(s, TCG_REG_I5,
911 (tcg_target_long)(s->tb_next + args[0]));
912 tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_I5) |
913 INSN_RS2(TCG_REG_G0));
915 /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
917 tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
918 TCG_TARGET_CALL_STACK_OFFSET - sizeof(long),
919 st_op); // delay slot
920 tcg_out_ldst(s, TCG_AREG0, TCG_REG_CALL_STACK,
921 TCG_TARGET_CALL_STACK_OFFSET - sizeof(long),
927 tcg_out_branch(s, COND_A, args[0]);
930 case INDEX_op_movi_i32:
931 tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
934 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
935 #define OP_32_64(x) \
936 glue(glue(case INDEX_op_, x), _i32:) \
937 glue(glue(case INDEX_op_, x), _i64:)
939 #define OP_32_64(x) \
940 glue(glue(case INDEX_op_, x), _i32:)
943 tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
946 tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
949 tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
952 tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
954 case INDEX_op_ld_i32:
955 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
956 case INDEX_op_ld32u_i64:
958 tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
961 tcg_out_ldst(s, args[0], args[1], args[2], STB);
964 tcg_out_ldst(s, args[0], args[1], args[2], STH);
966 case INDEX_op_st_i32:
967 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
968 case INDEX_op_st32_i64:
970 tcg_out_ldst(s, args[0], args[1], args[2], STW);
987 case INDEX_op_shl_i32:
990 case INDEX_op_shr_i32:
993 case INDEX_op_sar_i32:
996 case INDEX_op_mul_i32:
999 case INDEX_op_div2_i32:
1000 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1008 case INDEX_op_divu2_i32:
1009 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
1018 case INDEX_op_brcond_i32:
1019 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1023 case INDEX_op_qemu_ld8u:
1024 tcg_out_qemu_ld(s, args, 0);
1026 case INDEX_op_qemu_ld8s:
1027 tcg_out_qemu_ld(s, args, 0 | 4);
1029 case INDEX_op_qemu_ld16u:
1030 tcg_out_qemu_ld(s, args, 1);
1032 case INDEX_op_qemu_ld16s:
1033 tcg_out_qemu_ld(s, args, 1 | 4);
1035 case INDEX_op_qemu_ld32u:
1036 tcg_out_qemu_ld(s, args, 2);
1038 case INDEX_op_qemu_ld32s:
1039 tcg_out_qemu_ld(s, args, 2 | 4);
1041 case INDEX_op_qemu_st8:
1042 tcg_out_qemu_st(s, args, 0);
1044 case INDEX_op_qemu_st16:
1045 tcg_out_qemu_st(s, args, 1);
1047 case INDEX_op_qemu_st32:
1048 tcg_out_qemu_st(s, args, 2);
1051 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1052 case INDEX_op_movi_i64:
1053 tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1055 case INDEX_op_ld32s_i64:
1056 tcg_out_ldst(s, args[0], args[1], args[2], LDSW);
1058 case INDEX_op_ld_i64:
1059 tcg_out_ldst(s, args[0], args[1], args[2], LDX);
1061 case INDEX_op_st_i64:
1062 tcg_out_ldst(s, args[0], args[1], args[2], STX);
1064 case INDEX_op_shl_i64:
1067 case INDEX_op_shr_i64:
1070 case INDEX_op_sar_i64:
1073 case INDEX_op_mul_i64:
1076 case INDEX_op_div2_i64:
1079 case INDEX_op_divu2_i64:
1083 case INDEX_op_brcond_i64:
1084 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1087 case INDEX_op_qemu_ld64:
1088 tcg_out_qemu_ld(s, args, 3);
1090 case INDEX_op_qemu_st64:
1091 tcg_out_qemu_st(s, args, 3);
1096 if (const_args[2]) {
1097 tcg_out_arithi(s, args[0], args[1], args[2], c);
1099 tcg_out_arith(s, args[0], args[1], args[2], c);
1104 fprintf(stderr, "unknown opcode 0x%x\n", opc);
1109 static const TCGTargetOpDef sparc_op_defs[] = {
1110 { INDEX_op_exit_tb, { } },
1111 { INDEX_op_goto_tb, { } },
1112 { INDEX_op_call, { "ri" } },
1113 { INDEX_op_jmp, { "ri" } },
1114 { INDEX_op_br, { } },
1116 { INDEX_op_mov_i32, { "r", "r" } },
1117 { INDEX_op_movi_i32, { "r" } },
1118 { INDEX_op_ld8u_i32, { "r", "r" } },
1119 { INDEX_op_ld8s_i32, { "r", "r" } },
1120 { INDEX_op_ld16u_i32, { "r", "r" } },
1121 { INDEX_op_ld16s_i32, { "r", "r" } },
1122 { INDEX_op_ld_i32, { "r", "r" } },
1123 { INDEX_op_st8_i32, { "r", "r" } },
1124 { INDEX_op_st16_i32, { "r", "r" } },
1125 { INDEX_op_st_i32, { "r", "r" } },
1127 { INDEX_op_add_i32, { "r", "r", "rJ" } },
1128 { INDEX_op_mul_i32, { "r", "r", "rJ" } },
1129 { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
1130 { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
1131 { INDEX_op_sub_i32, { "r", "r", "rJ" } },
1132 { INDEX_op_and_i32, { "r", "r", "rJ" } },
1133 { INDEX_op_or_i32, { "r", "r", "rJ" } },
1134 { INDEX_op_xor_i32, { "r", "r", "rJ" } },
1136 { INDEX_op_shl_i32, { "r", "r", "rJ" } },
1137 { INDEX_op_shr_i32, { "r", "r", "rJ" } },
1138 { INDEX_op_sar_i32, { "r", "r", "rJ" } },
1140 { INDEX_op_brcond_i32, { "r", "ri" } },
1142 { INDEX_op_qemu_ld8u, { "r", "L" } },
1143 { INDEX_op_qemu_ld8s, { "r", "L" } },
1144 { INDEX_op_qemu_ld16u, { "r", "L" } },
1145 { INDEX_op_qemu_ld16s, { "r", "L" } },
1146 { INDEX_op_qemu_ld32u, { "r", "L" } },
1147 { INDEX_op_qemu_ld32s, { "r", "L" } },
1149 { INDEX_op_qemu_st8, { "L", "L" } },
1150 { INDEX_op_qemu_st16, { "L", "L" } },
1151 { INDEX_op_qemu_st32, { "L", "L" } },
1153 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1154 { INDEX_op_mov_i64, { "r", "r" } },
1155 { INDEX_op_movi_i64, { "r" } },
1156 { INDEX_op_ld8u_i64, { "r", "r" } },
1157 { INDEX_op_ld8s_i64, { "r", "r" } },
1158 { INDEX_op_ld16u_i64, { "r", "r" } },
1159 { INDEX_op_ld16s_i64, { "r", "r" } },
1160 { INDEX_op_ld32u_i64, { "r", "r" } },
1161 { INDEX_op_ld32s_i64, { "r", "r" } },
1162 { INDEX_op_ld_i64, { "r", "r" } },
1163 { INDEX_op_st8_i64, { "r", "r" } },
1164 { INDEX_op_st16_i64, { "r", "r" } },
1165 { INDEX_op_st32_i64, { "r", "r" } },
1166 { INDEX_op_st_i64, { "r", "r" } },
1168 { INDEX_op_add_i64, { "r", "r", "rJ" } },
1169 { INDEX_op_mul_i64, { "r", "r", "rJ" } },
1170 { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
1171 { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
1172 { INDEX_op_sub_i64, { "r", "r", "rJ" } },
1173 { INDEX_op_and_i64, { "r", "r", "rJ" } },
1174 { INDEX_op_or_i64, { "r", "r", "rJ" } },
1175 { INDEX_op_xor_i64, { "r", "r", "rJ" } },
1177 { INDEX_op_shl_i64, { "r", "r", "rJ" } },
1178 { INDEX_op_shr_i64, { "r", "r", "rJ" } },
1179 { INDEX_op_sar_i64, { "r", "r", "rJ" } },
1181 { INDEX_op_brcond_i64, { "r", "ri" } },
1186 void tcg_target_init(TCGContext *s)
1188 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1189 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1190 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
1192 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
1208 tcg_regset_clear(s->reserved_regs);
1209 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
1210 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
1211 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I4); // for internal use
1213 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I5); // for internal use
1214 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
1215 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
1216 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
1217 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
1218 tcg_add_target_add_op_defs(sparc_op_defs);