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 /* Macros used in tcg_target_op_defs. */
43 #if TCG_TARGET_REG_BITS == 32
48 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
56 /* TODO: documentation. */
57 static const TCGTargetOpDef tcg_target_op_defs[] = {
58 { INDEX_op_exit_tb, { NULL } },
59 { INDEX_op_goto_tb, { NULL } },
60 { INDEX_op_br, { NULL } },
62 { INDEX_op_ld8u_i32, { R, R } },
63 { INDEX_op_ld8s_i32, { R, R } },
64 { INDEX_op_ld16u_i32, { R, R } },
65 { INDEX_op_ld16s_i32, { R, R } },
66 { INDEX_op_ld_i32, { R, R } },
67 { INDEX_op_st8_i32, { R, R } },
68 { INDEX_op_st16_i32, { R, R } },
69 { INDEX_op_st_i32, { R, R } },
71 { INDEX_op_add_i32, { R, RI, RI } },
72 { INDEX_op_sub_i32, { R, RI, RI } },
73 { INDEX_op_mul_i32, { R, RI, RI } },
74 { INDEX_op_div_i32, { R, R, R } },
75 { INDEX_op_divu_i32, { R, R, R } },
76 { INDEX_op_rem_i32, { R, R, R } },
77 { INDEX_op_remu_i32, { R, R, R } },
78 /* TODO: Does R, RI, RI result in faster code than R, R, RI?
79 If both operands are constants, we can optimize. */
80 { INDEX_op_and_i32, { R, RI, RI } },
81 { INDEX_op_andc_i32, { R, RI, RI } },
82 { INDEX_op_eqv_i32, { R, RI, RI } },
83 { INDEX_op_nand_i32, { R, RI, RI } },
84 { INDEX_op_nor_i32, { R, RI, RI } },
85 { INDEX_op_or_i32, { R, RI, RI } },
86 { INDEX_op_orc_i32, { R, RI, RI } },
87 { INDEX_op_xor_i32, { R, RI, RI } },
88 { INDEX_op_shl_i32, { R, RI, RI } },
89 { INDEX_op_shr_i32, { R, RI, RI } },
90 { INDEX_op_sar_i32, { R, RI, RI } },
91 { INDEX_op_rotl_i32, { R, RI, RI } },
92 { INDEX_op_rotr_i32, { R, RI, RI } },
93 { INDEX_op_deposit_i32, { R, "0", R } },
95 { INDEX_op_brcond_i32, { R, RI } },
97 { INDEX_op_setcond_i32, { R, R, RI } },
98 { INDEX_op_setcond_i64, { R, R, RI } },
100 /* TODO: Support R, R, R, R, RI, RI? Will it be faster? */
101 { INDEX_op_add2_i32, { R, R, R, R, R, R } },
102 { INDEX_op_sub2_i32, { R, R, R, R, R, R } },
103 { INDEX_op_brcond2_i32, { R, R, RI, RI } },
104 { INDEX_op_mulu2_i32, { R, R, R, R } },
105 { INDEX_op_setcond2_i32, { R, R, R, RI, RI } },
107 { INDEX_op_not_i32, { R, R } },
108 { INDEX_op_neg_i32, { R, R } },
110 { INDEX_op_ld8u_i64, { R, R } },
111 { INDEX_op_ld8s_i64, { R, R } },
112 { INDEX_op_ld16u_i64, { R, R } },
113 { INDEX_op_ld16s_i64, { R, R } },
114 { INDEX_op_ld32u_i64, { R, R } },
115 { INDEX_op_ld32s_i64, { R, R } },
116 { INDEX_op_ld_i64, { R, R } },
118 { INDEX_op_st8_i64, { R, R } },
119 { INDEX_op_st16_i64, { R, R } },
120 { INDEX_op_st32_i64, { R, R } },
121 { INDEX_op_st_i64, { R, R } },
123 { INDEX_op_add_i64, { R, RI, RI } },
124 { INDEX_op_sub_i64, { R, RI, RI } },
125 { INDEX_op_mul_i64, { R, RI, RI } },
126 { INDEX_op_div_i64, { R, R, R } },
127 { INDEX_op_divu_i64, { R, R, R } },
128 { INDEX_op_rem_i64, { R, R, R } },
129 { INDEX_op_remu_i64, { R, R, R } },
130 { INDEX_op_and_i64, { R, RI, RI } },
131 { INDEX_op_andc_i64, { R, RI, RI } },
132 { INDEX_op_eqv_i64, { R, RI, RI } },
133 { INDEX_op_nand_i64, { R, RI, RI } },
134 { INDEX_op_nor_i64, { R, RI, RI } },
135 { INDEX_op_or_i64, { R, RI, RI } },
136 { INDEX_op_orc_i64, { R, RI, RI } },
137 { INDEX_op_xor_i64, { R, RI, RI } },
138 { INDEX_op_shl_i64, { R, RI, RI } },
139 { INDEX_op_shr_i64, { R, RI, RI } },
140 { INDEX_op_sar_i64, { R, RI, RI } },
141 { INDEX_op_rotl_i64, { R, RI, RI } },
142 { INDEX_op_rotr_i64, { R, RI, RI } },
143 { INDEX_op_deposit_i64, { R, "0", R } },
144 { INDEX_op_brcond_i64, { R, RI } },
146 { INDEX_op_ext8s_i64, { R, R } },
147 { INDEX_op_ext16s_i64, { R, R } },
148 { INDEX_op_ext32s_i64, { R, R } },
149 { INDEX_op_ext8u_i64, { R, R } },
150 { INDEX_op_ext16u_i64, { R, R } },
151 { INDEX_op_ext32u_i64, { R, R } },
152 { INDEX_op_ext_i32_i64, { R, R } },
153 { INDEX_op_extu_i32_i64, { R, R } },
154 { INDEX_op_bswap16_i64, { R, R } },
155 { INDEX_op_bswap32_i64, { R, R } },
156 { INDEX_op_bswap64_i64, { R, R } },
157 { INDEX_op_not_i64, { R, R } },
158 { INDEX_op_neg_i64, { R, R } },
160 { INDEX_op_qemu_ld_i32, { R, L } },
161 { INDEX_op_qemu_ld_i64, { R64, L } },
163 { INDEX_op_qemu_st_i32, { R, S } },
164 { INDEX_op_qemu_st_i64, { R64, S } },
166 { INDEX_op_ext8s_i32, { R, R } },
167 { INDEX_op_ext16s_i32, { R, R } },
168 { INDEX_op_ext8u_i32, { R, R } },
169 { INDEX_op_ext16u_i32, { R, R } },
171 { INDEX_op_bswap16_i32, { R, R } },
172 { INDEX_op_bswap32_i32, { R, R } },
174 { INDEX_op_mb, { } },
178 static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode op)
180 int i, n = ARRAY_SIZE(tcg_target_op_defs);
182 for (i = 0; i < n; ++i) {
183 if (tcg_target_op_defs[i].op == op) {
184 return &tcg_target_op_defs[i];
190 static const int tcg_target_reg_alloc_order[] = {
195 #if 0 /* used for TCG_REG_CALL_STACK */
201 #if TCG_TARGET_NB_REGS >= 16
213 #if MAX_OPC_PARAM_IARGS != 6
214 # error Fix needed, number of supported input arguments changed!
217 static const int tcg_target_call_iarg_regs[] = {
222 #if 0 /* used for TCG_REG_CALL_STACK */
227 #if TCG_TARGET_REG_BITS == 32
228 /* 32 bit hosts need 2 * MAX_OPC_PARAM_IARGS registers. */
230 #if TCG_TARGET_NB_REGS >= 16
237 # error Too few input registers available
242 static const int tcg_target_call_oarg_regs[] = {
244 #if TCG_TARGET_REG_BITS == 32
249 #ifdef CONFIG_DEBUG_TCG
250 static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
259 #if TCG_TARGET_NB_REGS >= 16
268 #if TCG_TARGET_NB_REGS >= 32
290 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
291 intptr_t value, intptr_t addend)
293 /* tcg_out_reloc always uses the same type, addend. */
294 tcg_debug_assert(type == sizeof(tcg_target_long));
295 tcg_debug_assert(addend == 0);
296 tcg_debug_assert(value != 0);
297 if (TCG_TARGET_REG_BITS == 32) {
298 tcg_patch32(code_ptr, value);
300 tcg_patch64(code_ptr, value);
305 #if defined(CONFIG_DEBUG_TCG_INTERPRETER)
306 /* Show current bytecode. Used by tcg interpreter. */
307 void tci_disas(uint8_t opc)
309 const TCGOpDef *def = &tcg_op_defs[opc];
310 fprintf(stderr, "TCG %s %u, %u, %u\n",
311 def->name, def->nb_oargs, def->nb_iargs, def->nb_cargs);
315 /* Write value (native size). */
316 static void tcg_out_i(TCGContext *s, tcg_target_ulong v)
318 if (TCG_TARGET_REG_BITS == 32) {
326 static void tcg_out_op_t(TCGContext *s, TCGOpcode op)
332 /* Write register. */
333 static void tcg_out_r(TCGContext *s, TCGArg t0)
335 tcg_debug_assert(t0 < TCG_TARGET_NB_REGS);
339 /* Write register or constant (native size). */
340 static void tcg_out_ri(TCGContext *s, int const_arg, TCGArg arg)
343 tcg_debug_assert(const_arg == 1);
344 tcg_out8(s, TCG_CONST);
351 /* Write register or constant (32 bit). */
352 static void tcg_out_ri32(TCGContext *s, int const_arg, TCGArg arg)
355 tcg_debug_assert(const_arg == 1);
356 tcg_out8(s, TCG_CONST);
363 #if TCG_TARGET_REG_BITS == 64
364 /* Write register or constant (64 bit). */
365 static void tcg_out_ri64(TCGContext *s, int const_arg, TCGArg arg)
368 tcg_debug_assert(const_arg == 1);
369 tcg_out8(s, TCG_CONST);
378 static void tci_out_label(TCGContext *s, TCGLabel *label)
380 if (label->has_value) {
381 tcg_out_i(s, label->u.value);
382 tcg_debug_assert(label->u.value);
384 tcg_out_reloc(s, s->code_ptr, sizeof(tcg_target_ulong), label, 0);
385 s->code_ptr += sizeof(tcg_target_ulong);
389 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
392 uint8_t *old_code_ptr = s->code_ptr;
393 if (type == TCG_TYPE_I32) {
394 tcg_out_op_t(s, INDEX_op_ld_i32);
399 tcg_debug_assert(type == TCG_TYPE_I64);
400 #if TCG_TARGET_REG_BITS == 64
401 tcg_out_op_t(s, INDEX_op_ld_i64);
404 tcg_debug_assert(arg2 == (int32_t)arg2);
410 old_code_ptr[1] = s->code_ptr - old_code_ptr;
413 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
415 uint8_t *old_code_ptr = s->code_ptr;
416 tcg_debug_assert(ret != arg);
417 #if TCG_TARGET_REG_BITS == 32
418 tcg_out_op_t(s, INDEX_op_mov_i32);
420 tcg_out_op_t(s, INDEX_op_mov_i64);
424 old_code_ptr[1] = s->code_ptr - old_code_ptr;
428 static void tcg_out_movi(TCGContext *s, TCGType type,
429 TCGReg t0, tcg_target_long arg)
431 uint8_t *old_code_ptr = s->code_ptr;
432 uint32_t arg32 = arg;
433 if (type == TCG_TYPE_I32 || arg == arg32) {
434 tcg_out_op_t(s, INDEX_op_tci_movi_i32);
438 tcg_debug_assert(type == TCG_TYPE_I64);
439 #if TCG_TARGET_REG_BITS == 64
440 tcg_out_op_t(s, INDEX_op_tci_movi_i64);
447 old_code_ptr[1] = s->code_ptr - old_code_ptr;
450 static inline void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
452 uint8_t *old_code_ptr = s->code_ptr;
453 tcg_out_op_t(s, INDEX_op_call);
454 tcg_out_ri(s, 1, (uintptr_t)arg);
455 old_code_ptr[1] = s->code_ptr - old_code_ptr;
458 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
459 const int *const_args)
461 uint8_t *old_code_ptr = s->code_ptr;
463 tcg_out_op_t(s, opc);
466 case INDEX_op_exit_tb:
467 tcg_out64(s, args[0]);
469 case INDEX_op_goto_tb:
470 if (s->tb_jmp_insn_offset) {
471 /* Direct jump method. */
472 /* Align for atomic patching and thread safety */
473 s->code_ptr = QEMU_ALIGN_PTR_UP(s->code_ptr, 4);
474 s->tb_jmp_insn_offset[args[0]] = tcg_current_code_size(s);
477 /* Indirect jump method. */
480 set_jmp_reset_offset(s, args[0]);
483 tci_out_label(s, arg_label(args[0]));
485 case INDEX_op_setcond_i32:
486 tcg_out_r(s, args[0]);
487 tcg_out_r(s, args[1]);
488 tcg_out_ri32(s, const_args[2], args[2]);
489 tcg_out8(s, args[3]); /* condition */
491 #if TCG_TARGET_REG_BITS == 32
492 case INDEX_op_setcond2_i32:
493 /* setcond2_i32 cond, t0, t1_low, t1_high, t2_low, t2_high */
494 tcg_out_r(s, args[0]);
495 tcg_out_r(s, args[1]);
496 tcg_out_r(s, args[2]);
497 tcg_out_ri32(s, const_args[3], args[3]);
498 tcg_out_ri32(s, const_args[4], args[4]);
499 tcg_out8(s, args[5]); /* condition */
501 #elif TCG_TARGET_REG_BITS == 64
502 case INDEX_op_setcond_i64:
503 tcg_out_r(s, args[0]);
504 tcg_out_r(s, args[1]);
505 tcg_out_ri64(s, const_args[2], args[2]);
506 tcg_out8(s, args[3]); /* condition */
509 case INDEX_op_ld8u_i32:
510 case INDEX_op_ld8s_i32:
511 case INDEX_op_ld16u_i32:
512 case INDEX_op_ld16s_i32:
513 case INDEX_op_ld_i32:
514 case INDEX_op_st8_i32:
515 case INDEX_op_st16_i32:
516 case INDEX_op_st_i32:
517 case INDEX_op_ld8u_i64:
518 case INDEX_op_ld8s_i64:
519 case INDEX_op_ld16u_i64:
520 case INDEX_op_ld16s_i64:
521 case INDEX_op_ld32u_i64:
522 case INDEX_op_ld32s_i64:
523 case INDEX_op_ld_i64:
524 case INDEX_op_st8_i64:
525 case INDEX_op_st16_i64:
526 case INDEX_op_st32_i64:
527 case INDEX_op_st_i64:
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;
730 if (type == TCG_TYPE_I32) {
731 tcg_out_op_t(s, INDEX_op_st_i32);
736 tcg_debug_assert(type == TCG_TYPE_I64);
737 #if TCG_TARGET_REG_BITS == 64
738 tcg_out_op_t(s, INDEX_op_st_i64);
746 old_code_ptr[1] = s->code_ptr - old_code_ptr;
749 static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
750 TCGReg base, intptr_t ofs)
755 /* Test if a constant matches the constraint. */
756 static int tcg_target_const_match(tcg_target_long val, TCGType type,
757 const TCGArgConstraint *arg_ct)
759 /* No need to return 0 or 1, 0 or != 0 is good enough. */
760 return arg_ct->ct & TCG_CT_CONST;
763 static void tcg_target_init(TCGContext *s)
765 #if defined(CONFIG_DEBUG_TCG_INTERPRETER)
766 const char *envval = getenv("DEBUG_TCG");
768 qemu_set_log(strtol(envval, NULL, 0));
772 /* The current code uses uint8_t for tcg operations. */
773 tcg_debug_assert(tcg_op_defs_max <= UINT8_MAX);
775 /* Registers available for 32 bit operations. */
776 tcg_target_available_regs[TCG_TYPE_I32] = BIT(TCG_TARGET_NB_REGS) - 1;
777 /* Registers available for 64 bit operations. */
778 tcg_target_available_regs[TCG_TYPE_I64] = BIT(TCG_TARGET_NB_REGS) - 1;
779 /* TODO: Which registers should be set here? */
780 tcg_target_call_clobber_regs = BIT(TCG_TARGET_NB_REGS) - 1;
782 s->reserved_regs = 0;
783 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
785 /* We use negative offsets from "sp" so that we can distinguish
786 stores that might pretend to be call arguments. */
787 tcg_set_frame(s, TCG_REG_CALL_STACK,
788 -CPU_TEMP_BUF_NLONGS * sizeof(long),
789 CPU_TEMP_BUF_NLONGS * sizeof(long));
792 /* Generate global QEMU prologue and epilogue code. */
793 static inline void tcg_target_qemu_prologue(TCGContext *s)