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 return C_O1_I2(r, r, r);
97 case INDEX_op_add_i32:
98 case INDEX_op_add_i64:
99 case INDEX_op_sub_i32:
100 case INDEX_op_sub_i64:
101 case INDEX_op_mul_i32:
102 case INDEX_op_mul_i64:
103 case INDEX_op_and_i32:
104 case INDEX_op_and_i64:
105 case INDEX_op_andc_i32:
106 case INDEX_op_andc_i64:
107 case INDEX_op_eqv_i32:
108 case INDEX_op_eqv_i64:
109 case INDEX_op_nand_i32:
110 case INDEX_op_nand_i64:
111 case INDEX_op_nor_i32:
112 case INDEX_op_nor_i64:
113 case INDEX_op_or_i32:
114 case INDEX_op_or_i64:
115 case INDEX_op_orc_i32:
116 case INDEX_op_orc_i64:
117 case INDEX_op_xor_i32:
118 case INDEX_op_xor_i64:
119 case INDEX_op_shl_i32:
120 case INDEX_op_shl_i64:
121 case INDEX_op_shr_i32:
122 case INDEX_op_shr_i64:
123 case INDEX_op_sar_i32:
124 case INDEX_op_sar_i64:
125 case INDEX_op_rotl_i32:
126 case INDEX_op_rotl_i64:
127 case INDEX_op_rotr_i32:
128 case INDEX_op_rotr_i64:
129 /* TODO: Does R, RI, RI result in faster code than R, R, RI? */
130 return C_O1_I2(r, ri, ri);
132 case INDEX_op_deposit_i32:
133 case INDEX_op_deposit_i64:
134 return C_O1_I2(r, 0, r);
136 case INDEX_op_brcond_i32:
137 case INDEX_op_brcond_i64:
138 return C_O0_I2(r, ri);
140 case INDEX_op_setcond_i32:
141 case INDEX_op_setcond_i64:
142 return C_O1_I2(r, r, ri);
144 #if TCG_TARGET_REG_BITS == 32
145 /* TODO: Support R, R, R, R, RI, RI? Will it be faster? */
146 case INDEX_op_add2_i32:
147 case INDEX_op_sub2_i32:
148 return C_O2_I4(r, r, r, r, r, r);
149 case INDEX_op_brcond2_i32:
150 return C_O0_I4(r, r, ri, ri);
151 case INDEX_op_mulu2_i32:
152 return C_O2_I2(r, r, r, r);
153 case INDEX_op_setcond2_i32:
154 return C_O1_I4(r, r, r, ri, ri);
157 case INDEX_op_qemu_ld_i32:
158 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
161 case INDEX_op_qemu_ld_i64:
162 return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r)
163 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, r)
164 : C_O2_I2(r, r, r, r));
165 case INDEX_op_qemu_st_i32:
166 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
169 case INDEX_op_qemu_st_i64:
170 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(r, r)
171 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(r, r, r)
172 : C_O0_I4(r, r, r, r));
175 g_assert_not_reached();
179 static const int tcg_target_reg_alloc_order[] = {
184 #if 0 /* used for TCG_REG_CALL_STACK */
190 #if TCG_TARGET_NB_REGS >= 16
202 #if MAX_OPC_PARAM_IARGS != 6
203 # error Fix needed, number of supported input arguments changed!
206 static const int tcg_target_call_iarg_regs[] = {
211 #if 0 /* used for TCG_REG_CALL_STACK */
216 #if TCG_TARGET_REG_BITS == 32
217 /* 32 bit hosts need 2 * MAX_OPC_PARAM_IARGS registers. */
219 #if TCG_TARGET_NB_REGS >= 16
226 # error Too few input registers available
231 static const int tcg_target_call_oarg_regs[] = {
233 #if TCG_TARGET_REG_BITS == 32
238 #ifdef CONFIG_DEBUG_TCG
239 static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
248 #if TCG_TARGET_NB_REGS >= 16
257 #if TCG_TARGET_NB_REGS >= 32
279 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
280 intptr_t value, intptr_t addend)
282 /* tcg_out_reloc always uses the same type, addend. */
283 tcg_debug_assert(type == sizeof(tcg_target_long));
284 tcg_debug_assert(addend == 0);
285 tcg_debug_assert(value != 0);
286 if (TCG_TARGET_REG_BITS == 32) {
287 tcg_patch32(code_ptr, value);
289 tcg_patch64(code_ptr, value);
294 #if defined(CONFIG_DEBUG_TCG_INTERPRETER)
295 /* Show current bytecode. Used by tcg interpreter. */
296 void tci_disas(uint8_t opc)
298 const TCGOpDef *def = &tcg_op_defs[opc];
299 fprintf(stderr, "TCG %s %u, %u, %u\n",
300 def->name, def->nb_oargs, def->nb_iargs, def->nb_cargs);
304 /* Write value (native size). */
305 static void tcg_out_i(TCGContext *s, tcg_target_ulong v)
307 if (TCG_TARGET_REG_BITS == 32) {
315 static void tcg_out_op_t(TCGContext *s, TCGOpcode op)
321 /* Write register. */
322 static void tcg_out_r(TCGContext *s, TCGArg t0)
324 tcg_debug_assert(t0 < TCG_TARGET_NB_REGS);
328 /* Write register or constant (native size). */
329 static void tcg_out_ri(TCGContext *s, int const_arg, TCGArg arg)
332 tcg_debug_assert(const_arg == 1);
333 tcg_out8(s, TCG_CONST);
340 /* Write register or constant (32 bit). */
341 static void tcg_out_ri32(TCGContext *s, int const_arg, TCGArg arg)
344 tcg_debug_assert(const_arg == 1);
345 tcg_out8(s, TCG_CONST);
352 #if TCG_TARGET_REG_BITS == 64
353 /* Write register or constant (64 bit). */
354 static void tcg_out_ri64(TCGContext *s, int const_arg, TCGArg arg)
357 tcg_debug_assert(const_arg == 1);
358 tcg_out8(s, TCG_CONST);
367 static void tci_out_label(TCGContext *s, TCGLabel *label)
369 if (label->has_value) {
370 tcg_out_i(s, label->u.value);
371 tcg_debug_assert(label->u.value);
373 tcg_out_reloc(s, s->code_ptr, sizeof(tcg_target_ulong), label, 0);
374 s->code_ptr += sizeof(tcg_target_ulong);
378 static void stack_bounds_check(TCGReg base, target_long offset)
380 if (base == TCG_REG_CALL_STACK) {
381 tcg_debug_assert(offset < 0);
382 tcg_debug_assert(offset >= -(CPU_TEMP_BUF_NLONGS * sizeof(long)));
386 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
389 uint8_t *old_code_ptr = s->code_ptr;
391 stack_bounds_check(arg1, arg2);
392 if (type == TCG_TYPE_I32) {
393 tcg_out_op_t(s, INDEX_op_ld_i32);
398 tcg_debug_assert(type == TCG_TYPE_I64);
399 #if TCG_TARGET_REG_BITS == 64
400 tcg_out_op_t(s, INDEX_op_ld_i64);
403 tcg_debug_assert(arg2 == (int32_t)arg2);
409 old_code_ptr[1] = s->code_ptr - old_code_ptr;
412 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
414 uint8_t *old_code_ptr = s->code_ptr;
415 tcg_debug_assert(ret != arg);
416 #if TCG_TARGET_REG_BITS == 32
417 tcg_out_op_t(s, INDEX_op_mov_i32);
419 tcg_out_op_t(s, INDEX_op_mov_i64);
423 old_code_ptr[1] = s->code_ptr - old_code_ptr;
427 static void tcg_out_movi(TCGContext *s, TCGType type,
428 TCGReg t0, tcg_target_long arg)
430 uint8_t *old_code_ptr = s->code_ptr;
431 uint32_t arg32 = arg;
432 if (type == TCG_TYPE_I32 || arg == arg32) {
433 tcg_out_op_t(s, INDEX_op_tci_movi_i32);
437 tcg_debug_assert(type == TCG_TYPE_I64);
438 #if TCG_TARGET_REG_BITS == 64
439 tcg_out_op_t(s, INDEX_op_tci_movi_i64);
446 old_code_ptr[1] = s->code_ptr - old_code_ptr;
449 static inline void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
451 uint8_t *old_code_ptr = s->code_ptr;
452 tcg_out_op_t(s, INDEX_op_call);
453 tcg_out_ri(s, 1, (uintptr_t)arg);
454 old_code_ptr[1] = s->code_ptr - old_code_ptr;
457 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
458 const int *const_args)
460 uint8_t *old_code_ptr = s->code_ptr;
462 tcg_out_op_t(s, opc);
465 case INDEX_op_exit_tb:
466 tcg_out64(s, args[0]);
468 case INDEX_op_goto_tb:
469 if (s->tb_jmp_insn_offset) {
470 /* Direct jump method. */
471 /* Align for atomic patching and thread safety */
472 s->code_ptr = QEMU_ALIGN_PTR_UP(s->code_ptr, 4);
473 s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
476 /* Indirect jump method. */
479 set_jmp_reset_offset(s, args[0]);
482 tci_out_label(s, arg_label(args[0]));
484 case INDEX_op_setcond_i32:
485 tcg_out_r(s, args[0]);
486 tcg_out_r(s, args[1]);
487 tcg_out_ri32(s, const_args[2], args[2]);
488 tcg_out8(s, args[3]); /* condition */
490 #if TCG_TARGET_REG_BITS == 32
491 case INDEX_op_setcond2_i32:
492 /* setcond2_i32 cond, t0, t1_low, t1_high, t2_low, t2_high */
493 tcg_out_r(s, args[0]);
494 tcg_out_r(s, args[1]);
495 tcg_out_r(s, args[2]);
496 tcg_out_ri32(s, const_args[3], args[3]);
497 tcg_out_ri32(s, const_args[4], args[4]);
498 tcg_out8(s, args[5]); /* condition */
500 #elif TCG_TARGET_REG_BITS == 64
501 case INDEX_op_setcond_i64:
502 tcg_out_r(s, args[0]);
503 tcg_out_r(s, args[1]);
504 tcg_out_ri64(s, const_args[2], args[2]);
505 tcg_out8(s, args[3]); /* condition */
508 case INDEX_op_ld8u_i32:
509 case INDEX_op_ld8s_i32:
510 case INDEX_op_ld16u_i32:
511 case INDEX_op_ld16s_i32:
512 case INDEX_op_ld_i32:
513 case INDEX_op_st8_i32:
514 case INDEX_op_st16_i32:
515 case INDEX_op_st_i32:
516 case INDEX_op_ld8u_i64:
517 case INDEX_op_ld8s_i64:
518 case INDEX_op_ld16u_i64:
519 case INDEX_op_ld16s_i64:
520 case INDEX_op_ld32u_i64:
521 case INDEX_op_ld32s_i64:
522 case INDEX_op_ld_i64:
523 case INDEX_op_st8_i64:
524 case INDEX_op_st16_i64:
525 case INDEX_op_st32_i64:
526 case INDEX_op_st_i64:
527 stack_bounds_check(args[1], args[2]);
528 tcg_out_r(s, args[0]);
529 tcg_out_r(s, args[1]);
530 tcg_debug_assert(args[2] == (int32_t)args[2]);
531 tcg_out32(s, args[2]);
533 case INDEX_op_add_i32:
534 case INDEX_op_sub_i32:
535 case INDEX_op_mul_i32:
536 case INDEX_op_and_i32:
537 case INDEX_op_andc_i32: /* Optional (TCG_TARGET_HAS_andc_i32). */
538 case INDEX_op_eqv_i32: /* Optional (TCG_TARGET_HAS_eqv_i32). */
539 case INDEX_op_nand_i32: /* Optional (TCG_TARGET_HAS_nand_i32). */
540 case INDEX_op_nor_i32: /* Optional (TCG_TARGET_HAS_nor_i32). */
541 case INDEX_op_or_i32:
542 case INDEX_op_orc_i32: /* Optional (TCG_TARGET_HAS_orc_i32). */
543 case INDEX_op_xor_i32:
544 case INDEX_op_shl_i32:
545 case INDEX_op_shr_i32:
546 case INDEX_op_sar_i32:
547 case INDEX_op_rotl_i32: /* Optional (TCG_TARGET_HAS_rot_i32). */
548 case INDEX_op_rotr_i32: /* Optional (TCG_TARGET_HAS_rot_i32). */
549 tcg_out_r(s, args[0]);
550 tcg_out_ri32(s, const_args[1], args[1]);
551 tcg_out_ri32(s, const_args[2], args[2]);
553 case INDEX_op_deposit_i32: /* Optional (TCG_TARGET_HAS_deposit_i32). */
554 tcg_out_r(s, args[0]);
555 tcg_out_r(s, args[1]);
556 tcg_out_r(s, args[2]);
557 tcg_debug_assert(args[3] <= UINT8_MAX);
558 tcg_out8(s, args[3]);
559 tcg_debug_assert(args[4] <= UINT8_MAX);
560 tcg_out8(s, args[4]);
563 #if TCG_TARGET_REG_BITS == 64
564 case INDEX_op_add_i64:
565 case INDEX_op_sub_i64:
566 case INDEX_op_mul_i64:
567 case INDEX_op_and_i64:
568 case INDEX_op_andc_i64: /* Optional (TCG_TARGET_HAS_andc_i64). */
569 case INDEX_op_eqv_i64: /* Optional (TCG_TARGET_HAS_eqv_i64). */
570 case INDEX_op_nand_i64: /* Optional (TCG_TARGET_HAS_nand_i64). */
571 case INDEX_op_nor_i64: /* Optional (TCG_TARGET_HAS_nor_i64). */
572 case INDEX_op_or_i64:
573 case INDEX_op_orc_i64: /* Optional (TCG_TARGET_HAS_orc_i64). */
574 case INDEX_op_xor_i64:
575 case INDEX_op_shl_i64:
576 case INDEX_op_shr_i64:
577 case INDEX_op_sar_i64:
578 case INDEX_op_rotl_i64: /* Optional (TCG_TARGET_HAS_rot_i64). */
579 case INDEX_op_rotr_i64: /* Optional (TCG_TARGET_HAS_rot_i64). */
580 tcg_out_r(s, args[0]);
581 tcg_out_ri64(s, const_args[1], args[1]);
582 tcg_out_ri64(s, const_args[2], args[2]);
584 case INDEX_op_deposit_i64: /* Optional (TCG_TARGET_HAS_deposit_i64). */
585 tcg_out_r(s, args[0]);
586 tcg_out_r(s, args[1]);
587 tcg_out_r(s, args[2]);
588 tcg_debug_assert(args[3] <= UINT8_MAX);
589 tcg_out8(s, args[3]);
590 tcg_debug_assert(args[4] <= UINT8_MAX);
591 tcg_out8(s, args[4]);
593 case INDEX_op_div_i64: /* Optional (TCG_TARGET_HAS_div_i64). */
594 case INDEX_op_divu_i64: /* Optional (TCG_TARGET_HAS_div_i64). */
595 case INDEX_op_rem_i64: /* Optional (TCG_TARGET_HAS_div_i64). */
596 case INDEX_op_remu_i64: /* Optional (TCG_TARGET_HAS_div_i64). */
599 case INDEX_op_div2_i64: /* Optional (TCG_TARGET_HAS_div2_i64). */
600 case INDEX_op_divu2_i64: /* Optional (TCG_TARGET_HAS_div2_i64). */
603 case INDEX_op_brcond_i64:
604 tcg_out_r(s, args[0]);
605 tcg_out_ri64(s, const_args[1], args[1]);
606 tcg_out8(s, args[2]); /* condition */
607 tci_out_label(s, arg_label(args[3]));
609 case INDEX_op_bswap16_i64: /* Optional (TCG_TARGET_HAS_bswap16_i64). */
610 case INDEX_op_bswap32_i64: /* Optional (TCG_TARGET_HAS_bswap32_i64). */
611 case INDEX_op_bswap64_i64: /* Optional (TCG_TARGET_HAS_bswap64_i64). */
612 case INDEX_op_not_i64: /* Optional (TCG_TARGET_HAS_not_i64). */
613 case INDEX_op_neg_i64: /* Optional (TCG_TARGET_HAS_neg_i64). */
614 case INDEX_op_ext8s_i64: /* Optional (TCG_TARGET_HAS_ext8s_i64). */
615 case INDEX_op_ext8u_i64: /* Optional (TCG_TARGET_HAS_ext8u_i64). */
616 case INDEX_op_ext16s_i64: /* Optional (TCG_TARGET_HAS_ext16s_i64). */
617 case INDEX_op_ext16u_i64: /* Optional (TCG_TARGET_HAS_ext16u_i64). */
618 case INDEX_op_ext32s_i64: /* Optional (TCG_TARGET_HAS_ext32s_i64). */
619 case INDEX_op_ext32u_i64: /* Optional (TCG_TARGET_HAS_ext32u_i64). */
620 case INDEX_op_ext_i32_i64:
621 case INDEX_op_extu_i32_i64:
622 #endif /* TCG_TARGET_REG_BITS == 64 */
623 case INDEX_op_neg_i32: /* Optional (TCG_TARGET_HAS_neg_i32). */
624 case INDEX_op_not_i32: /* Optional (TCG_TARGET_HAS_not_i32). */
625 case INDEX_op_ext8s_i32: /* Optional (TCG_TARGET_HAS_ext8s_i32). */
626 case INDEX_op_ext16s_i32: /* Optional (TCG_TARGET_HAS_ext16s_i32). */
627 case INDEX_op_ext8u_i32: /* Optional (TCG_TARGET_HAS_ext8u_i32). */
628 case INDEX_op_ext16u_i32: /* Optional (TCG_TARGET_HAS_ext16u_i32). */
629 case INDEX_op_bswap16_i32: /* Optional (TCG_TARGET_HAS_bswap16_i32). */
630 case INDEX_op_bswap32_i32: /* Optional (TCG_TARGET_HAS_bswap32_i32). */
631 tcg_out_r(s, args[0]);
632 tcg_out_r(s, args[1]);
634 case INDEX_op_div_i32: /* Optional (TCG_TARGET_HAS_div_i32). */
635 case INDEX_op_divu_i32: /* Optional (TCG_TARGET_HAS_div_i32). */
636 case INDEX_op_rem_i32: /* Optional (TCG_TARGET_HAS_div_i32). */
637 case INDEX_op_remu_i32: /* Optional (TCG_TARGET_HAS_div_i32). */
638 tcg_out_r(s, args[0]);
639 tcg_out_ri32(s, const_args[1], args[1]);
640 tcg_out_ri32(s, const_args[2], args[2]);
642 case INDEX_op_div2_i32: /* Optional (TCG_TARGET_HAS_div2_i32). */
643 case INDEX_op_divu2_i32: /* Optional (TCG_TARGET_HAS_div2_i32). */
646 #if TCG_TARGET_REG_BITS == 32
647 case INDEX_op_add2_i32:
648 case INDEX_op_sub2_i32:
649 tcg_out_r(s, args[0]);
650 tcg_out_r(s, args[1]);
651 tcg_out_r(s, args[2]);
652 tcg_out_r(s, args[3]);
653 tcg_out_r(s, args[4]);
654 tcg_out_r(s, args[5]);
656 case INDEX_op_brcond2_i32:
657 tcg_out_r(s, args[0]);
658 tcg_out_r(s, args[1]);
659 tcg_out_ri32(s, const_args[2], args[2]);
660 tcg_out_ri32(s, const_args[3], args[3]);
661 tcg_out8(s, args[4]); /* condition */
662 tci_out_label(s, arg_label(args[5]));
664 case INDEX_op_mulu2_i32:
665 tcg_out_r(s, args[0]);
666 tcg_out_r(s, args[1]);
667 tcg_out_r(s, args[2]);
668 tcg_out_r(s, args[3]);
671 case INDEX_op_brcond_i32:
672 tcg_out_r(s, args[0]);
673 tcg_out_ri32(s, const_args[1], args[1]);
674 tcg_out8(s, args[2]); /* condition */
675 tci_out_label(s, arg_label(args[3]));
677 case INDEX_op_qemu_ld_i32:
678 tcg_out_r(s, *args++);
679 tcg_out_r(s, *args++);
680 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
681 tcg_out_r(s, *args++);
683 tcg_out_i(s, *args++);
685 case INDEX_op_qemu_ld_i64:
686 tcg_out_r(s, *args++);
687 if (TCG_TARGET_REG_BITS == 32) {
688 tcg_out_r(s, *args++);
690 tcg_out_r(s, *args++);
691 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
692 tcg_out_r(s, *args++);
694 tcg_out_i(s, *args++);
696 case INDEX_op_qemu_st_i32:
697 tcg_out_r(s, *args++);
698 tcg_out_r(s, *args++);
699 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
700 tcg_out_r(s, *args++);
702 tcg_out_i(s, *args++);
704 case INDEX_op_qemu_st_i64:
705 tcg_out_r(s, *args++);
706 if (TCG_TARGET_REG_BITS == 32) {
707 tcg_out_r(s, *args++);
709 tcg_out_r(s, *args++);
710 if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
711 tcg_out_r(s, *args++);
713 tcg_out_i(s, *args++);
717 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
718 case INDEX_op_mov_i64:
719 case INDEX_op_call: /* Always emitted via tcg_out_call. */
723 old_code_ptr[1] = s->code_ptr - old_code_ptr;
726 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
729 uint8_t *old_code_ptr = s->code_ptr;
731 stack_bounds_check(arg1, arg2);
732 if (type == TCG_TYPE_I32) {
733 tcg_out_op_t(s, INDEX_op_st_i32);
738 tcg_debug_assert(type == TCG_TYPE_I64);
739 #if TCG_TARGET_REG_BITS == 64
740 tcg_out_op_t(s, INDEX_op_st_i64);
748 old_code_ptr[1] = s->code_ptr - old_code_ptr;
751 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
752 TCGReg base, intptr_t ofs)
757 /* Test if a constant matches the constraint. */
758 static int tcg_target_const_match(tcg_target_long val, TCGType type,
759 const TCGArgConstraint *arg_ct)
761 /* No need to return 0 or 1, 0 or != 0 is good enough. */
762 return arg_ct->ct & TCG_CT_CONST;
765 static void tcg_target_init(TCGContext *s)
767 #if defined(CONFIG_DEBUG_TCG_INTERPRETER)
768 const char *envval = getenv("DEBUG_TCG");
770 qemu_set_log(strtol(envval, NULL, 0));
774 /* The current code uses uint8_t for tcg operations. */
775 tcg_debug_assert(tcg_op_defs_max <= UINT8_MAX);
777 /* Registers available for 32 bit operations. */
778 tcg_target_available_regs[TCG_TYPE_I32] = BIT(TCG_TARGET_NB_REGS) - 1;
779 /* Registers available for 64 bit operations. */
780 tcg_target_available_regs[TCG_TYPE_I64] = BIT(TCG_TARGET_NB_REGS) - 1;
781 /* TODO: Which registers should be set here? */
782 tcg_target_call_clobber_regs = BIT(TCG_TARGET_NB_REGS) - 1;
784 s->reserved_regs = 0;
785 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
787 /* We use negative offsets from "sp" so that we can distinguish
788 stores that might pretend to be call arguments. */
789 tcg_set_frame(s, TCG_REG_CALL_STACK,
790 -CPU_TEMP_BUF_NLONGS * sizeof(long),
791 CPU_TEMP_BUF_NLONGS * sizeof(long));
794 /* Generate global QEMU prologue and epilogue code. */
795 static inline void tcg_target_qemu_prologue(TCGContext *s)