2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2009, 2011 Stefan Weil
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
26 * - See TODO comments in code.
29 /* Marker for missing code. */
32 fprintf(stderr, "TODO %s:%u: %s()\n", \
33 __FILE__, __LINE__, __func__); \
37 /* Bitfield n...m (in 32 bit value). */
38 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
40 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
43 case INDEX_op_ld8u_i32:
44 case INDEX_op_ld8s_i32:
45 case INDEX_op_ld16u_i32:
46 case INDEX_op_ld16s_i32:
48 case INDEX_op_ld8u_i64:
49 case INDEX_op_ld8s_i64:
50 case INDEX_op_ld16u_i64:
51 case INDEX_op_ld16s_i64:
52 case INDEX_op_ld32u_i64:
53 case INDEX_op_ld32s_i64:
55 case INDEX_op_not_i32:
56 case INDEX_op_not_i64:
57 case INDEX_op_neg_i32:
58 case INDEX_op_neg_i64:
59 case INDEX_op_ext8s_i32:
60 case INDEX_op_ext8s_i64:
61 case INDEX_op_ext16s_i32:
62 case INDEX_op_ext16s_i64:
63 case INDEX_op_ext8u_i32:
64 case INDEX_op_ext8u_i64:
65 case INDEX_op_ext16u_i32:
66 case INDEX_op_ext16u_i64:
67 case INDEX_op_ext32s_i64:
68 case INDEX_op_ext32u_i64:
69 case INDEX_op_ext_i32_i64:
70 case INDEX_op_extu_i32_i64:
71 case INDEX_op_bswap16_i32:
72 case INDEX_op_bswap16_i64:
73 case INDEX_op_bswap32_i32:
74 case INDEX_op_bswap32_i64:
75 case INDEX_op_bswap64_i64:
78 case INDEX_op_st8_i32:
79 case INDEX_op_st16_i32:
81 case INDEX_op_st8_i64:
82 case INDEX_op_st16_i64:
83 case INDEX_op_st32_i64:
87 case INDEX_op_div_i32:
88 case INDEX_op_div_i64:
89 case INDEX_op_divu_i32:
90 case INDEX_op_divu_i64:
91 case INDEX_op_rem_i32:
92 case INDEX_op_rem_i64:
93 case INDEX_op_remu_i32:
94 case INDEX_op_remu_i64:
95 case INDEX_op_add_i32:
96 case INDEX_op_add_i64:
97 case INDEX_op_sub_i32:
98 case INDEX_op_sub_i64:
99 case INDEX_op_mul_i32:
100 case INDEX_op_mul_i64:
101 case INDEX_op_and_i32:
102 case INDEX_op_and_i64:
103 case INDEX_op_andc_i32:
104 case INDEX_op_andc_i64:
105 case INDEX_op_eqv_i32:
106 case INDEX_op_eqv_i64:
107 case INDEX_op_nand_i32:
108 case INDEX_op_nand_i64:
109 case INDEX_op_nor_i32:
110 case INDEX_op_nor_i64:
111 case INDEX_op_or_i32:
112 case INDEX_op_or_i64:
113 case INDEX_op_orc_i32:
114 case INDEX_op_orc_i64:
115 case INDEX_op_xor_i32:
116 case INDEX_op_xor_i64:
117 case INDEX_op_shl_i32:
118 case INDEX_op_shl_i64:
119 case INDEX_op_shr_i32:
120 case INDEX_op_shr_i64:
121 case INDEX_op_sar_i32:
122 case INDEX_op_sar_i64:
123 case INDEX_op_rotl_i32:
124 case INDEX_op_rotl_i64:
125 case INDEX_op_rotr_i32:
126 case INDEX_op_rotr_i64:
127 case INDEX_op_setcond_i32:
128 case INDEX_op_setcond_i64:
129 case INDEX_op_deposit_i32:
130 case INDEX_op_deposit_i64:
131 return C_O1_I2(r, r, r);
133 case INDEX_op_brcond_i32:
134 case INDEX_op_brcond_i64:
135 return C_O0_I2(r, r);
137 #if TCG_TARGET_REG_BITS == 32
138 /* TODO: Support R, R, R, R, RI, RI? Will it be faster? */
139 case INDEX_op_add2_i32:
140 case INDEX_op_sub2_i32:
141 return C_O2_I4(r, r, r, r, r, r);
142 case INDEX_op_brcond2_i32:
143 return C_O0_I4(r, r, r, r);
144 case INDEX_op_mulu2_i32:
145 return C_O2_I2(r, r, r, r);
146 case INDEX_op_setcond2_i32:
147 return C_O1_I4(r, r, r, r, r);
150 case INDEX_op_qemu_ld_i32:
151 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
154 case INDEX_op_qemu_ld_i64:
155 return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r)
156 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, r)
157 : C_O2_I2(r, r, r, r));
158 case INDEX_op_qemu_st_i32:
159 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
162 case INDEX_op_qemu_st_i64:
163 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(r, r)
164 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(r, r, r)
165 : C_O0_I4(r, r, r, r));
168 g_assert_not_reached();
172 static const int tcg_target_reg_alloc_order[] = {
191 #if MAX_OPC_PARAM_IARGS != 6
192 # error Fix needed, number of supported input arguments changed!
195 static const int tcg_target_call_iarg_regs[] = {
202 #if TCG_TARGET_REG_BITS == 32
203 /* 32 bit hosts need 2 * MAX_OPC_PARAM_IARGS registers. */
213 static const int tcg_target_call_oarg_regs[] = {
215 #if TCG_TARGET_REG_BITS == 32
220 #ifdef CONFIG_DEBUG_TCG
221 static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
241 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
242 intptr_t value, intptr_t addend)
244 /* tcg_out_reloc always uses the same type, addend. */
245 tcg_debug_assert(type == sizeof(tcg_target_long));
246 tcg_debug_assert(addend == 0);
247 tcg_debug_assert(value != 0);
248 if (TCG_TARGET_REG_BITS == 32) {
249 tcg_patch32(code_ptr, value);
251 tcg_patch64(code_ptr, value);
256 /* Write value (native size). */
257 static void tcg_out_i(TCGContext *s, tcg_target_ulong v)
259 if (TCG_TARGET_REG_BITS == 32) {
267 static void tcg_out_op_t(TCGContext *s, TCGOpcode op)
273 /* Write register. */
274 static void tcg_out_r(TCGContext *s, TCGArg t0)
276 tcg_debug_assert(t0 < TCG_TARGET_NB_REGS);
281 static void tci_out_label(TCGContext *s, TCGLabel *label)
283 if (label->has_value) {
284 tcg_out_i(s, label->u.value);
285 tcg_debug_assert(label->u.value);
287 tcg_out_reloc(s, s->code_ptr, sizeof(tcg_target_ulong), label, 0);
288 s->code_ptr += sizeof(tcg_target_ulong);
292 static void stack_bounds_check(TCGReg base, target_long offset)
294 if (base == TCG_REG_CALL_STACK) {
295 tcg_debug_assert(offset < 0);
296 tcg_debug_assert(offset >= -(CPU_TEMP_BUF_NLONGS * sizeof(long)));
300 static void tcg_out_op_l(TCGContext *s, TCGOpcode op, TCGLabel *l0)
302 uint8_t *old_code_ptr = s->code_ptr;
305 tci_out_label(s, l0);
307 old_code_ptr[1] = s->code_ptr - old_code_ptr;
310 static void tcg_out_op_p(TCGContext *s, TCGOpcode op, void *p0)
312 uint8_t *old_code_ptr = s->code_ptr;
315 tcg_out_i(s, (uintptr_t)p0);
317 old_code_ptr[1] = s->code_ptr - old_code_ptr;
320 static void tcg_out_op_rr(TCGContext *s, TCGOpcode op, TCGReg r0, TCGReg r1)
322 uint8_t *old_code_ptr = s->code_ptr;
328 old_code_ptr[1] = s->code_ptr - old_code_ptr;
331 static void tcg_out_op_rrr(TCGContext *s, TCGOpcode op,
332 TCGReg r0, TCGReg r1, TCGReg r2)
334 uint8_t *old_code_ptr = s->code_ptr;
341 old_code_ptr[1] = s->code_ptr - old_code_ptr;
344 static void tcg_out_op_rrs(TCGContext *s, TCGOpcode op,
345 TCGReg r0, TCGReg r1, intptr_t i2)
347 uint8_t *old_code_ptr = s->code_ptr;
352 tcg_debug_assert(i2 == (int32_t)i2);
355 old_code_ptr[1] = s->code_ptr - old_code_ptr;
358 static void tcg_out_op_rrcl(TCGContext *s, TCGOpcode op,
359 TCGReg r0, TCGReg r1, TCGCond c2, TCGLabel *l3)
361 uint8_t *old_code_ptr = s->code_ptr;
367 tci_out_label(s, l3);
369 old_code_ptr[1] = s->code_ptr - old_code_ptr;
372 static void tcg_out_op_rrrc(TCGContext *s, TCGOpcode op,
373 TCGReg r0, TCGReg r1, TCGReg r2, TCGCond c3)
375 uint8_t *old_code_ptr = s->code_ptr;
383 old_code_ptr[1] = s->code_ptr - old_code_ptr;
386 static void tcg_out_op_rrrbb(TCGContext *s, TCGOpcode op, TCGReg r0,
387 TCGReg r1, TCGReg r2, uint8_t b3, uint8_t b4)
389 uint8_t *old_code_ptr = s->code_ptr;
398 old_code_ptr[1] = s->code_ptr - old_code_ptr;
401 #if TCG_TARGET_REG_BITS == 32
402 static void tcg_out_op_rrrr(TCGContext *s, TCGOpcode op,
403 TCGReg r0, TCGReg r1, TCGReg r2, TCGReg r3)
405 uint8_t *old_code_ptr = s->code_ptr;
413 old_code_ptr[1] = s->code_ptr - old_code_ptr;
416 static void tcg_out_op_rrrrrc(TCGContext *s, TCGOpcode op,
417 TCGReg r0, TCGReg r1, TCGReg r2,
418 TCGReg r3, TCGReg r4, TCGCond c5)
420 uint8_t *old_code_ptr = s->code_ptr;
430 old_code_ptr[1] = s->code_ptr - old_code_ptr;
433 static void tcg_out_op_rrrrrr(TCGContext *s, TCGOpcode op,
434 TCGReg r0, TCGReg r1, TCGReg r2,
435 TCGReg r3, TCGReg r4, TCGReg r5)
437 uint8_t *old_code_ptr = s->code_ptr;
447 old_code_ptr[1] = s->code_ptr - old_code_ptr;
451 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg val, TCGReg base,
454 stack_bounds_check(base, offset);
457 tcg_out_op_rrs(s, INDEX_op_ld_i32, val, base, offset);
459 #if TCG_TARGET_REG_BITS == 64
461 tcg_out_op_rrs(s, INDEX_op_ld_i64, val, base, offset);
465 g_assert_not_reached();
469 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
473 tcg_out_op_rr(s, INDEX_op_mov_i32, ret, arg);
475 #if TCG_TARGET_REG_BITS == 64
477 tcg_out_op_rr(s, INDEX_op_mov_i64, ret, arg);
481 g_assert_not_reached();
486 static void tcg_out_movi(TCGContext *s, TCGType type,
487 TCGReg t0, tcg_target_long arg)
489 uint8_t *old_code_ptr = s->code_ptr;
490 uint32_t arg32 = arg;
491 if (type == TCG_TYPE_I32 || arg == arg32) {
492 tcg_out_op_t(s, INDEX_op_tci_movi_i32);
496 tcg_debug_assert(type == TCG_TYPE_I64);
497 #if TCG_TARGET_REG_BITS == 64
498 tcg_out_op_t(s, INDEX_op_tci_movi_i64);
505 old_code_ptr[1] = s->code_ptr - old_code_ptr;
508 static inline void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
510 uint8_t *old_code_ptr = s->code_ptr;
511 tcg_out_op_t(s, INDEX_op_call);
512 tcg_out_i(s, (uintptr_t)arg);
513 old_code_ptr[1] = s->code_ptr - old_code_ptr;
516 #if TCG_TARGET_REG_BITS == 64
517 # define CASE_32_64(x) \
518 case glue(glue(INDEX_op_, x), _i64): \
519 case glue(glue(INDEX_op_, x), _i32):
520 # define CASE_64(x) \
521 case glue(glue(INDEX_op_, x), _i64):
523 # define CASE_32_64(x) \
524 case glue(glue(INDEX_op_, x), _i32):
528 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
529 const int *const_args)
531 uint8_t *old_code_ptr = s->code_ptr;
534 case INDEX_op_exit_tb:
535 tcg_out_op_p(s, opc, (void *)args[0]);
538 case INDEX_op_goto_tb:
539 tcg_debug_assert(s->tb_jmp_insn_offset == 0);
540 /* indirect jump method. */
541 tcg_out_op_p(s, opc, s->tb_jmp_target_addr + args[0]);
542 set_jmp_reset_offset(s, args[0]);
546 tcg_out_op_l(s, opc, arg_label(args[0]));
550 tcg_out_op_rrrc(s, opc, args[0], args[1], args[2], args[3]);
553 #if TCG_TARGET_REG_BITS == 32
554 case INDEX_op_setcond2_i32:
555 tcg_out_op_rrrrrc(s, opc, args[0], args[1], args[2],
556 args[3], args[4], args[5]);
564 case INDEX_op_ld_i32:
570 case INDEX_op_st_i32:
573 stack_bounds_check(args[1], args[2]);
574 tcg_out_op_rrs(s, opc, args[0], args[1], args[2]);
583 CASE_32_64(andc) /* Optional (TCG_TARGET_HAS_andc_*). */
584 CASE_32_64(orc) /* Optional (TCG_TARGET_HAS_orc_*). */
585 CASE_32_64(eqv) /* Optional (TCG_TARGET_HAS_eqv_*). */
586 CASE_32_64(nand) /* Optional (TCG_TARGET_HAS_nand_*). */
587 CASE_32_64(nor) /* Optional (TCG_TARGET_HAS_nor_*). */
591 CASE_32_64(rotl) /* Optional (TCG_TARGET_HAS_rot_*). */
592 CASE_32_64(rotr) /* Optional (TCG_TARGET_HAS_rot_*). */
593 CASE_32_64(div) /* Optional (TCG_TARGET_HAS_div_*). */
594 CASE_32_64(divu) /* Optional (TCG_TARGET_HAS_div_*). */
595 CASE_32_64(rem) /* Optional (TCG_TARGET_HAS_div_*). */
596 CASE_32_64(remu) /* Optional (TCG_TARGET_HAS_div_*). */
597 tcg_out_op_rrr(s, opc, args[0], args[1], args[2]);
600 CASE_32_64(deposit) /* Optional (TCG_TARGET_HAS_deposit_*). */
602 TCGArg pos = args[3], len = args[4];
603 TCGArg max = opc == INDEX_op_deposit_i32 ? 32 : 64;
605 tcg_debug_assert(pos < max);
606 tcg_debug_assert(pos + len <= max);
608 tcg_out_op_rrrbb(s, opc, args[0], args[1], args[2], pos, len);
613 tcg_out_op_rrcl(s, opc, args[0], args[1], args[2], arg_label(args[3]));
616 CASE_32_64(neg) /* Optional (TCG_TARGET_HAS_neg_*). */
617 CASE_32_64(not) /* Optional (TCG_TARGET_HAS_not_*). */
618 CASE_32_64(ext8s) /* Optional (TCG_TARGET_HAS_ext8s_*). */
619 CASE_32_64(ext8u) /* Optional (TCG_TARGET_HAS_ext8u_*). */
620 CASE_32_64(ext16s) /* Optional (TCG_TARGET_HAS_ext16s_*). */
621 CASE_32_64(ext16u) /* Optional (TCG_TARGET_HAS_ext16u_*). */
622 CASE_64(ext32s) /* Optional (TCG_TARGET_HAS_ext32s_i64). */
623 CASE_64(ext32u) /* Optional (TCG_TARGET_HAS_ext32u_i64). */
626 CASE_32_64(bswap16) /* Optional (TCG_TARGET_HAS_bswap16_*). */
627 CASE_32_64(bswap32) /* Optional (TCG_TARGET_HAS_bswap32_*). */
628 CASE_64(bswap64) /* Optional (TCG_TARGET_HAS_bswap64_i64). */
629 tcg_out_op_rr(s, opc, args[0], args[1]);
632 #if TCG_TARGET_REG_BITS == 32
633 case INDEX_op_add2_i32:
634 case INDEX_op_sub2_i32:
635 tcg_out_op_rrrrrr(s, opc, args[0], args[1], args[2],
636 args[3], args[4], args[5]);
638 case INDEX_op_brcond2_i32:
639 tcg_out_op_t(s, opc);
640 tcg_out_r(s, args[0]);
641 tcg_out_r(s, args[1]);
642 tcg_out_r(s, args[2]);
643 tcg_out_r(s, args[3]);
644 tcg_out8(s, args[4]); /* condition */
645 tci_out_label(s, arg_label(args[5]));
646 old_code_ptr[1] = s->code_ptr - old_code_ptr;
648 case INDEX_op_mulu2_i32:
649 tcg_out_op_rrrr(s, opc, args[0], args[1], args[2], args[3]);
653 case INDEX_op_qemu_ld_i32:
654 case INDEX_op_qemu_st_i32:
655 tcg_out_op_t(s, opc);
656 tcg_out_r(s, *args++);
657 tcg_out_r(s, *args++);
658 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
659 tcg_out_r(s, *args++);
661 tcg_out32(s, *args++);
662 old_code_ptr[1] = s->code_ptr - old_code_ptr;
665 case INDEX_op_qemu_ld_i64:
666 case INDEX_op_qemu_st_i64:
667 tcg_out_op_t(s, opc);
668 tcg_out_r(s, *args++);
669 if (TCG_TARGET_REG_BITS == 32) {
670 tcg_out_r(s, *args++);
672 tcg_out_r(s, *args++);
673 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
674 tcg_out_r(s, *args++);
676 tcg_out32(s, *args++);
677 old_code_ptr[1] = s->code_ptr - old_code_ptr;
681 tcg_out_op_t(s, opc);
682 old_code_ptr[1] = s->code_ptr - old_code_ptr;
685 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
686 case INDEX_op_mov_i64:
687 case INDEX_op_call: /* Always emitted via tcg_out_call. */
693 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg val, TCGReg base,
696 stack_bounds_check(base, offset);
699 tcg_out_op_rrs(s, INDEX_op_st_i32, val, base, offset);
701 #if TCG_TARGET_REG_BITS == 64
703 tcg_out_op_rrs(s, INDEX_op_st_i64, val, base, offset);
707 g_assert_not_reached();
711 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
712 TCGReg base, intptr_t ofs)
717 /* Test if a constant matches the constraint. */
718 static int tcg_target_const_match(tcg_target_long val, TCGType type,
719 const TCGArgConstraint *arg_ct)
721 /* No need to return 0 or 1, 0 or != 0 is good enough. */
722 return arg_ct->ct & TCG_CT_CONST;
725 static void tcg_target_init(TCGContext *s)
727 #if defined(CONFIG_DEBUG_TCG_INTERPRETER)
728 const char *envval = getenv("DEBUG_TCG");
730 qemu_set_log(strtol(envval, NULL, 0));
734 /* The current code uses uint8_t for tcg operations. */
735 tcg_debug_assert(tcg_op_defs_max <= UINT8_MAX);
737 /* Registers available for 32 bit operations. */
738 tcg_target_available_regs[TCG_TYPE_I32] = BIT(TCG_TARGET_NB_REGS) - 1;
739 /* Registers available for 64 bit operations. */
740 tcg_target_available_regs[TCG_TYPE_I64] = BIT(TCG_TARGET_NB_REGS) - 1;
741 /* TODO: Which registers should be set here? */
742 tcg_target_call_clobber_regs = BIT(TCG_TARGET_NB_REGS) - 1;
744 s->reserved_regs = 0;
745 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
747 /* We use negative offsets from "sp" so that we can distinguish
748 stores that might pretend to be call arguments. */
749 tcg_set_frame(s, TCG_REG_CALL_STACK,
750 -CPU_TEMP_BUF_NLONGS * sizeof(long),
751 CPU_TEMP_BUF_NLONGS * sizeof(long));
754 /* Generate global QEMU prologue and epilogue code. */
755 static inline void tcg_target_qemu_prologue(TCGContext *s)