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
26 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
38 static const int tcg_target_reg_alloc_order
[] = {
48 static const int tcg_target_call_iarg_regs
[3] = { TCG_REG_EAX
, TCG_REG_EDX
, TCG_REG_ECX
};
49 static const int tcg_target_call_oarg_regs
[2] = { TCG_REG_EAX
, TCG_REG_EDX
};
51 static uint8_t *tb_ret_addr
;
53 static void patch_reloc(uint8_t *code_ptr
, int type
,
54 tcg_target_long value
, tcg_target_long addend
)
59 *(uint32_t *)code_ptr
= value
;
62 *(uint32_t *)code_ptr
= value
- (long)code_ptr
;
65 value
-= (long)code_ptr
;
66 if (value
!= (int8_t)value
) {
69 *(uint8_t *)code_ptr
= value
;
76 /* maximum number of register used for input function arguments */
77 static inline int tcg_target_get_call_iarg_regs_count(int flags
)
79 flags
&= TCG_CALL_TYPE_MASK
;
81 case TCG_CALL_TYPE_STD
:
83 case TCG_CALL_TYPE_REGPARM_1
:
84 case TCG_CALL_TYPE_REGPARM_2
:
85 case TCG_CALL_TYPE_REGPARM
:
86 return flags
- TCG_CALL_TYPE_REGPARM_1
+ 1;
92 /* parse target specific constraints */
93 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
100 ct
->ct
|= TCG_CT_REG
;
101 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EAX
);
104 ct
->ct
|= TCG_CT_REG
;
105 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EBX
);
108 ct
->ct
|= TCG_CT_REG
;
109 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_ECX
);
112 ct
->ct
|= TCG_CT_REG
;
113 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EDX
);
116 ct
->ct
|= TCG_CT_REG
;
117 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_ESI
);
120 ct
->ct
|= TCG_CT_REG
;
121 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EDI
);
124 ct
->ct
|= TCG_CT_REG
;
125 tcg_regset_set32(ct
->u
.regs
, 0, 0xf);
128 ct
->ct
|= TCG_CT_REG
;
129 tcg_regset_set32(ct
->u
.regs
, 0, 0xff);
132 /* qemu_ld/st address constraint */
134 ct
->ct
|= TCG_CT_REG
;
135 tcg_regset_set32(ct
->u
.regs
, 0, 0xff);
136 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_EAX
);
137 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_EDX
);
147 /* test if a constant matches the constraint */
148 static inline int tcg_target_const_match(tcg_target_long val
,
149 const TCGArgConstraint
*arg_ct
)
153 if (ct
& TCG_CT_CONST
)
192 #define P_EXT 0x100 /* 0x0f opcode prefix */
194 static const uint8_t tcg_cond_to_jcc
[10] = {
195 [TCG_COND_EQ
] = JCC_JE
,
196 [TCG_COND_NE
] = JCC_JNE
,
197 [TCG_COND_LT
] = JCC_JL
,
198 [TCG_COND_GE
] = JCC_JGE
,
199 [TCG_COND_LE
] = JCC_JLE
,
200 [TCG_COND_GT
] = JCC_JG
,
201 [TCG_COND_LTU
] = JCC_JB
,
202 [TCG_COND_GEU
] = JCC_JAE
,
203 [TCG_COND_LEU
] = JCC_JBE
,
204 [TCG_COND_GTU
] = JCC_JA
,
207 static inline void tcg_out_opc(TCGContext
*s
, int opc
)
214 static inline void tcg_out_modrm(TCGContext
*s
, int opc
, int r
, int rm
)
217 tcg_out8(s
, 0xc0 | (r
<< 3) | rm
);
220 /* rm == -1 means no register index */
221 static inline void tcg_out_modrm_offset(TCGContext
*s
, int opc
, int r
, int rm
,
226 tcg_out8(s
, 0x05 | (r
<< 3));
227 tcg_out32(s
, offset
);
228 } else if (offset
== 0 && rm
!= TCG_REG_EBP
) {
229 if (rm
== TCG_REG_ESP
) {
230 tcg_out8(s
, 0x04 | (r
<< 3));
233 tcg_out8(s
, 0x00 | (r
<< 3) | rm
);
235 } else if ((int8_t)offset
== offset
) {
236 if (rm
== TCG_REG_ESP
) {
237 tcg_out8(s
, 0x44 | (r
<< 3));
240 tcg_out8(s
, 0x40 | (r
<< 3) | rm
);
244 if (rm
== TCG_REG_ESP
) {
245 tcg_out8(s
, 0x84 | (r
<< 3));
248 tcg_out8(s
, 0x80 | (r
<< 3) | rm
);
250 tcg_out32(s
, offset
);
254 static inline void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
257 tcg_out_modrm(s
, 0x8b, ret
, arg
);
260 static inline void tcg_out_movi(TCGContext
*s
, TCGType type
,
261 int ret
, int32_t arg
)
265 tcg_out_modrm(s
, 0x01 | (ARITH_XOR
<< 3), ret
, ret
);
267 tcg_out8(s
, 0xb8 + ret
);
272 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, int ret
,
273 int arg1
, tcg_target_long arg2
)
276 tcg_out_modrm_offset(s
, 0x8b, ret
, arg1
, arg2
);
279 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, int arg
,
280 int arg1
, tcg_target_long arg2
)
283 tcg_out_modrm_offset(s
, 0x89, arg
, arg1
, arg2
);
286 static inline void tgen_arithi(TCGContext
*s
, int c
, int r0
, int32_t val
, int cf
)
288 if (!cf
&& ((c
== ARITH_ADD
&& val
== 1) || (c
== ARITH_SUB
&& val
== -1))) {
290 tcg_out_opc(s
, 0x40 + r0
);
291 } else if (!cf
&& ((c
== ARITH_ADD
&& val
== -1) || (c
== ARITH_SUB
&& val
== 1))) {
293 tcg_out_opc(s
, 0x48 + r0
);
294 } else if (val
== (int8_t)val
) {
295 tcg_out_modrm(s
, 0x83, c
, r0
);
297 } else if (c
== ARITH_AND
&& val
== 0xffu
&& r0
< 4) {
299 tcg_out_modrm(s
, 0xb6 | P_EXT
, r0
, r0
);
300 } else if (c
== ARITH_AND
&& val
== 0xffffu
) {
302 tcg_out_modrm(s
, 0xb7 | P_EXT
, r0
, r0
);
304 tcg_out_modrm(s
, 0x81, c
, r0
);
309 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
312 tgen_arithi(s
, ARITH_ADD
, reg
, val
, 0);
315 /* Use SMALL != 0 to force a short forward branch. */
316 static void tcg_out_jxx(TCGContext
*s
, int opc
, int label_index
, int small
)
319 TCGLabel
*l
= &s
->labels
[label_index
];
322 val
= l
->u
.value
- (tcg_target_long
)s
->code_ptr
;
324 if ((int8_t)val1
== val1
) {
328 tcg_out8(s
, 0x70 + opc
);
337 tcg_out32(s
, val
- 5);
340 tcg_out8(s
, 0x80 + opc
);
341 tcg_out32(s
, val
- 6);
348 tcg_out8(s
, 0x70 + opc
);
350 tcg_out_reloc(s
, s
->code_ptr
, R_386_PC8
, label_index
, -1);
357 tcg_out8(s
, 0x80 + opc
);
359 tcg_out_reloc(s
, s
->code_ptr
, R_386_PC32
, label_index
, -4);
364 static void tcg_out_brcond(TCGContext
*s
, int cond
,
365 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
366 int label_index
, int small
)
371 tcg_out_modrm(s
, 0x85, arg1
, arg1
);
373 tgen_arithi(s
, ARITH_CMP
, arg1
, arg2
, 0);
376 tcg_out_modrm(s
, 0x01 | (ARITH_CMP
<< 3), arg2
, arg1
);
378 tcg_out_jxx(s
, tcg_cond_to_jcc
[cond
], label_index
, small
);
381 /* XXX: we implement it at the target level to avoid having to
382 handle cross basic blocks temporaries */
383 static void tcg_out_brcond2(TCGContext
*s
, const TCGArg
*args
,
384 const int *const_args
, int small
)
387 label_next
= gen_new_label();
390 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2],
392 tcg_out_brcond(s
, TCG_COND_EQ
, args
[1], args
[3], const_args
[3],
396 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2],
398 tcg_out_brcond(s
, TCG_COND_NE
, args
[1], args
[3], const_args
[3],
402 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3],
404 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
405 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2],
409 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3],
411 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
412 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2],
416 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3],
418 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
419 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2],
423 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3],
425 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
426 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2],
430 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3],
432 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
433 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2],
437 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3],
439 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
440 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2],
444 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3],
446 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
447 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2],
451 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3],
453 tcg_out_jxx(s
, JCC_JNE
, label_next
, 1);
454 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2],
460 tcg_out_label(s
, label_next
, (tcg_target_long
)s
->code_ptr
);
463 #if defined(CONFIG_SOFTMMU)
465 #include "../../softmmu_defs.h"
467 static void *qemu_ld_helpers
[4] = {
474 static void *qemu_st_helpers
[4] = {
482 #ifndef CONFIG_USER_ONLY
486 /* XXX: qemu_ld and qemu_st could be modified to clobber only EDX and
487 EAX. It will be useful once fixed registers globals are less
489 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
,
492 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
493 #if defined(CONFIG_SOFTMMU)
494 uint8_t *label1_ptr
, *label2_ptr
;
496 #if TARGET_LONG_BITS == 64
497 #if defined(CONFIG_SOFTMMU)
509 #if TARGET_LONG_BITS == 64
518 #if defined(CONFIG_SOFTMMU)
519 tcg_out_mov(s
, r1
, addr_reg
);
521 tcg_out_mov(s
, r0
, addr_reg
);
523 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
524 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
526 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
527 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
529 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
530 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
532 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
533 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
534 tcg_out8(s
, (5 << 3) | r1
);
535 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_read
));
538 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
540 tcg_out_mov(s
, r0
, addr_reg
);
542 #if TARGET_LONG_BITS == 32
544 tcg_out8(s
, 0x70 + JCC_JE
);
545 label1_ptr
= s
->code_ptr
;
549 tcg_out8(s
, 0x70 + JCC_JNE
);
550 label3_ptr
= s
->code_ptr
;
553 /* cmp 4(r1), addr_reg2 */
554 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
557 tcg_out8(s
, 0x70 + JCC_JE
);
558 label1_ptr
= s
->code_ptr
;
562 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
565 /* XXX: move that code at the end of the TB */
566 #if TARGET_LONG_BITS == 32
567 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EDX
, mem_index
);
569 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
570 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
573 tcg_out32(s
, (tcg_target_long
)qemu_ld_helpers
[s_bits
] -
574 (tcg_target_long
)s
->code_ptr
- 4);
579 tcg_out_modrm(s
, 0xbe | P_EXT
, data_reg
, TCG_REG_EAX
);
583 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, TCG_REG_EAX
);
587 tcg_out_modrm(s
, 0xb6 | P_EXT
, data_reg
, TCG_REG_EAX
);
591 tcg_out_modrm(s
, 0xb7 | P_EXT
, data_reg
, TCG_REG_EAX
);
595 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
598 if (data_reg
== TCG_REG_EDX
) {
599 tcg_out_opc(s
, 0x90 + TCG_REG_EDX
); /* xchg %edx, %eax */
600 tcg_out_mov(s
, data_reg2
, TCG_REG_EAX
);
602 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
603 tcg_out_mov(s
, data_reg2
, TCG_REG_EDX
);
610 label2_ptr
= s
->code_ptr
;
614 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
617 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
618 offsetof(CPUTLBEntry
, addr_read
));
623 #ifdef TARGET_WORDS_BIGENDIAN
631 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, data_reg
, r0
, GUEST_BASE
);
635 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, data_reg
, r0
, GUEST_BASE
);
639 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, data_reg
, r0
, GUEST_BASE
);
641 /* rolw $8, data_reg */
643 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
649 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, data_reg
, r0
, GUEST_BASE
);
651 /* rolw $8, data_reg */
653 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
656 /* movswl data_reg, data_reg */
657 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, data_reg
);
661 /* movl (r0), data_reg */
662 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
);
665 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
669 /* XXX: could be nicer */
670 if (r0
== data_reg
) {
674 tcg_out_mov(s
, r1
, r0
);
678 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
);
679 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, GUEST_BASE
+ 4);
681 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
+ 4);
682 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
684 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, GUEST_BASE
);
686 tcg_out_opc(s
, (0xc8 + data_reg2
) | P_EXT
);
693 #if defined(CONFIG_SOFTMMU)
695 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
700 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
,
703 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
704 #if defined(CONFIG_SOFTMMU)
705 uint8_t *label1_ptr
, *label2_ptr
;
707 #if TARGET_LONG_BITS == 64
708 #if defined(CONFIG_SOFTMMU)
720 #if TARGET_LONG_BITS == 64
730 #if defined(CONFIG_SOFTMMU)
731 tcg_out_mov(s
, r1
, addr_reg
);
733 tcg_out_mov(s
, r0
, addr_reg
);
735 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
736 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
738 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
739 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
741 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
742 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
744 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
745 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
746 tcg_out8(s
, (5 << 3) | r1
);
747 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_write
));
750 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
752 tcg_out_mov(s
, r0
, addr_reg
);
754 #if TARGET_LONG_BITS == 32
756 tcg_out8(s
, 0x70 + JCC_JE
);
757 label1_ptr
= s
->code_ptr
;
761 tcg_out8(s
, 0x70 + JCC_JNE
);
762 label3_ptr
= s
->code_ptr
;
765 /* cmp 4(r1), addr_reg2 */
766 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
769 tcg_out8(s
, 0x70 + JCC_JE
);
770 label1_ptr
= s
->code_ptr
;
774 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
777 /* XXX: move that code at the end of the TB */
778 #if TARGET_LONG_BITS == 32
780 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
781 tcg_out_mov(s
, TCG_REG_ECX
, data_reg2
);
782 tcg_out8(s
, 0x6a); /* push Ib */
783 tcg_out8(s
, mem_index
);
785 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
786 (tcg_target_long
)s
->code_ptr
- 4);
787 tcg_out_addi(s
, TCG_REG_ESP
, 4);
792 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_EDX
, data_reg
);
796 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_EDX
, data_reg
);
799 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
802 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
804 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
805 (tcg_target_long
)s
->code_ptr
- 4);
809 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
810 tcg_out8(s
, 0x6a); /* push Ib */
811 tcg_out8(s
, mem_index
);
812 tcg_out_opc(s
, 0x50 + data_reg2
); /* push */
813 tcg_out_opc(s
, 0x50 + data_reg
); /* push */
815 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
816 (tcg_target_long
)s
->code_ptr
- 4);
817 tcg_out_addi(s
, TCG_REG_ESP
, 12);
819 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
823 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_ECX
, data_reg
);
827 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_ECX
, data_reg
);
830 tcg_out_mov(s
, TCG_REG_ECX
, data_reg
);
833 tcg_out8(s
, 0x6a); /* push Ib */
834 tcg_out8(s
, mem_index
);
836 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
837 (tcg_target_long
)s
->code_ptr
- 4);
838 tcg_out_addi(s
, TCG_REG_ESP
, 4);
844 label2_ptr
= s
->code_ptr
;
848 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
851 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
852 offsetof(CPUTLBEntry
, addr_write
));
857 #ifdef TARGET_WORDS_BIGENDIAN
865 tcg_out_modrm_offset(s
, 0x88, data_reg
, r0
, GUEST_BASE
);
869 tcg_out_mov(s
, r1
, data_reg
);
870 tcg_out8(s
, 0x66); /* rolw $8, %ecx */
871 tcg_out_modrm(s
, 0xc1, 0, r1
);
877 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
881 tcg_out_mov(s
, r1
, data_reg
);
883 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
887 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
891 tcg_out_mov(s
, r1
, data_reg2
);
893 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
894 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, GUEST_BASE
);
895 tcg_out_mov(s
, r1
, data_reg
);
897 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
898 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, GUEST_BASE
+ 4);
900 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
901 tcg_out_modrm_offset(s
, 0x89, data_reg2
, r0
, GUEST_BASE
+ 4);
908 #if defined(CONFIG_SOFTMMU)
910 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
914 static inline void tcg_out_op(TCGContext
*s
, int opc
,
915 const TCGArg
*args
, const int *const_args
)
920 case INDEX_op_exit_tb
:
921 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EAX
, args
[0]);
922 tcg_out8(s
, 0xe9); /* jmp tb_ret_addr */
923 tcg_out32(s
, tb_ret_addr
- s
->code_ptr
- 4);
925 case INDEX_op_goto_tb
:
926 if (s
->tb_jmp_offset
) {
927 /* direct jump method */
928 tcg_out8(s
, 0xe9); /* jmp im */
929 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
932 /* indirect jump method */
934 tcg_out_modrm_offset(s
, 0xff, 4, -1,
935 (tcg_target_long
)(s
->tb_next
+ args
[0]));
937 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
942 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
944 tcg_out_modrm(s
, 0xff, 2, args
[0]);
950 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
952 tcg_out_modrm(s
, 0xff, 4, args
[0]);
956 tcg_out_jxx(s
, JCC_JMP
, args
[0], 0);
958 case INDEX_op_movi_i32
:
959 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
961 case INDEX_op_ld8u_i32
:
963 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, args
[0], args
[1], args
[2]);
965 case INDEX_op_ld8s_i32
:
967 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, args
[0], args
[1], args
[2]);
969 case INDEX_op_ld16u_i32
:
971 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, args
[0], args
[1], args
[2]);
973 case INDEX_op_ld16s_i32
:
975 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, args
[0], args
[1], args
[2]);
977 case INDEX_op_ld_i32
:
979 tcg_out_modrm_offset(s
, 0x8b, args
[0], args
[1], args
[2]);
981 case INDEX_op_st8_i32
:
983 tcg_out_modrm_offset(s
, 0x88, args
[0], args
[1], args
[2]);
985 case INDEX_op_st16_i32
:
988 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
990 case INDEX_op_st_i32
:
992 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
994 case INDEX_op_sub_i32
:
997 case INDEX_op_and_i32
:
1000 case INDEX_op_or_i32
:
1003 case INDEX_op_xor_i32
:
1006 case INDEX_op_add_i32
:
1009 if (const_args
[2]) {
1010 tgen_arithi(s
, c
, args
[0], args
[2], 0);
1012 tcg_out_modrm(s
, 0x01 | (c
<< 3), args
[2], args
[0]);
1015 case INDEX_op_mul_i32
:
1016 if (const_args
[2]) {
1019 if (val
== (int8_t)val
) {
1020 tcg_out_modrm(s
, 0x6b, args
[0], args
[0]);
1023 tcg_out_modrm(s
, 0x69, args
[0], args
[0]);
1027 tcg_out_modrm(s
, 0xaf | P_EXT
, args
[0], args
[2]);
1030 case INDEX_op_mulu2_i32
:
1031 tcg_out_modrm(s
, 0xf7, 4, args
[3]);
1033 case INDEX_op_div2_i32
:
1034 tcg_out_modrm(s
, 0xf7, 7, args
[4]);
1036 case INDEX_op_divu2_i32
:
1037 tcg_out_modrm(s
, 0xf7, 6, args
[4]);
1039 case INDEX_op_shl_i32
:
1042 if (const_args
[2]) {
1044 tcg_out_modrm(s
, 0xd1, c
, args
[0]);
1046 tcg_out_modrm(s
, 0xc1, c
, args
[0]);
1047 tcg_out8(s
, args
[2]);
1050 tcg_out_modrm(s
, 0xd3, c
, args
[0]);
1053 case INDEX_op_shr_i32
:
1056 case INDEX_op_sar_i32
:
1059 case INDEX_op_rotl_i32
:
1062 case INDEX_op_rotr_i32
:
1066 case INDEX_op_add2_i32
:
1068 tgen_arithi(s
, ARITH_ADD
, args
[0], args
[4], 1);
1070 tcg_out_modrm(s
, 0x01 | (ARITH_ADD
<< 3), args
[4], args
[0]);
1072 tgen_arithi(s
, ARITH_ADC
, args
[1], args
[5], 1);
1074 tcg_out_modrm(s
, 0x01 | (ARITH_ADC
<< 3), args
[5], args
[1]);
1076 case INDEX_op_sub2_i32
:
1078 tgen_arithi(s
, ARITH_SUB
, args
[0], args
[4], 1);
1080 tcg_out_modrm(s
, 0x01 | (ARITH_SUB
<< 3), args
[4], args
[0]);
1082 tgen_arithi(s
, ARITH_SBB
, args
[1], args
[5], 1);
1084 tcg_out_modrm(s
, 0x01 | (ARITH_SBB
<< 3), args
[5], args
[1]);
1086 case INDEX_op_brcond_i32
:
1087 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1],
1090 case INDEX_op_brcond2_i32
:
1091 tcg_out_brcond2(s
, args
, const_args
, 0);
1094 case INDEX_op_bswap16_i32
:
1096 tcg_out_modrm(s
, 0xc1, SHIFT_ROL
, args
[0]);
1099 case INDEX_op_bswap32_i32
:
1100 tcg_out_opc(s
, (0xc8 + args
[0]) | P_EXT
);
1103 case INDEX_op_neg_i32
:
1104 tcg_out_modrm(s
, 0xf7, 3, args
[0]);
1107 case INDEX_op_not_i32
:
1108 tcg_out_modrm(s
, 0xf7, 2, args
[0]);
1111 case INDEX_op_ext8s_i32
:
1112 tcg_out_modrm(s
, 0xbe | P_EXT
, args
[0], args
[1]);
1114 case INDEX_op_ext16s_i32
:
1115 tcg_out_modrm(s
, 0xbf | P_EXT
, args
[0], args
[1]);
1117 case INDEX_op_ext8u_i32
:
1118 tcg_out_modrm(s
, 0xb6 | P_EXT
, args
[0], args
[1]);
1120 case INDEX_op_ext16u_i32
:
1121 tcg_out_modrm(s
, 0xb7 | P_EXT
, args
[0], args
[1]);
1124 case INDEX_op_qemu_ld8u
:
1125 tcg_out_qemu_ld(s
, args
, 0);
1127 case INDEX_op_qemu_ld8s
:
1128 tcg_out_qemu_ld(s
, args
, 0 | 4);
1130 case INDEX_op_qemu_ld16u
:
1131 tcg_out_qemu_ld(s
, args
, 1);
1133 case INDEX_op_qemu_ld16s
:
1134 tcg_out_qemu_ld(s
, args
, 1 | 4);
1136 case INDEX_op_qemu_ld32u
:
1137 tcg_out_qemu_ld(s
, args
, 2);
1139 case INDEX_op_qemu_ld64
:
1140 tcg_out_qemu_ld(s
, args
, 3);
1143 case INDEX_op_qemu_st8
:
1144 tcg_out_qemu_st(s
, args
, 0);
1146 case INDEX_op_qemu_st16
:
1147 tcg_out_qemu_st(s
, args
, 1);
1149 case INDEX_op_qemu_st32
:
1150 tcg_out_qemu_st(s
, args
, 2);
1152 case INDEX_op_qemu_st64
:
1153 tcg_out_qemu_st(s
, args
, 3);
1161 static const TCGTargetOpDef x86_op_defs
[] = {
1162 { INDEX_op_exit_tb
, { } },
1163 { INDEX_op_goto_tb
, { } },
1164 { INDEX_op_call
, { "ri" } },
1165 { INDEX_op_jmp
, { "ri" } },
1166 { INDEX_op_br
, { } },
1167 { INDEX_op_mov_i32
, { "r", "r" } },
1168 { INDEX_op_movi_i32
, { "r" } },
1169 { INDEX_op_ld8u_i32
, { "r", "r" } },
1170 { INDEX_op_ld8s_i32
, { "r", "r" } },
1171 { INDEX_op_ld16u_i32
, { "r", "r" } },
1172 { INDEX_op_ld16s_i32
, { "r", "r" } },
1173 { INDEX_op_ld_i32
, { "r", "r" } },
1174 { INDEX_op_st8_i32
, { "q", "r" } },
1175 { INDEX_op_st16_i32
, { "r", "r" } },
1176 { INDEX_op_st_i32
, { "r", "r" } },
1178 { INDEX_op_add_i32
, { "r", "0", "ri" } },
1179 { INDEX_op_sub_i32
, { "r", "0", "ri" } },
1180 { INDEX_op_mul_i32
, { "r", "0", "ri" } },
1181 { INDEX_op_mulu2_i32
, { "a", "d", "a", "r" } },
1182 { INDEX_op_div2_i32
, { "a", "d", "0", "1", "r" } },
1183 { INDEX_op_divu2_i32
, { "a", "d", "0", "1", "r" } },
1184 { INDEX_op_and_i32
, { "r", "0", "ri" } },
1185 { INDEX_op_or_i32
, { "r", "0", "ri" } },
1186 { INDEX_op_xor_i32
, { "r", "0", "ri" } },
1188 { INDEX_op_shl_i32
, { "r", "0", "ci" } },
1189 { INDEX_op_shr_i32
, { "r", "0", "ci" } },
1190 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1191 { INDEX_op_rotl_i32
, { "r", "0", "ci" } },
1192 { INDEX_op_rotr_i32
, { "r", "0", "ci" } },
1194 { INDEX_op_brcond_i32
, { "r", "ri" } },
1196 { INDEX_op_add2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1197 { INDEX_op_sub2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1198 { INDEX_op_brcond2_i32
, { "r", "r", "ri", "ri" } },
1200 { INDEX_op_bswap16_i32
, { "r", "0" } },
1201 { INDEX_op_bswap32_i32
, { "r", "0" } },
1203 { INDEX_op_neg_i32
, { "r", "0" } },
1205 { INDEX_op_not_i32
, { "r", "0" } },
1207 { INDEX_op_ext8s_i32
, { "r", "q" } },
1208 { INDEX_op_ext16s_i32
, { "r", "r" } },
1209 { INDEX_op_ext8u_i32
, { "r", "q"} },
1210 { INDEX_op_ext16u_i32
, { "r", "r"} },
1212 #if TARGET_LONG_BITS == 32
1213 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1214 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1215 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1216 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1217 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1218 { INDEX_op_qemu_ld64
, { "r", "r", "L" } },
1220 { INDEX_op_qemu_st8
, { "cb", "L" } },
1221 { INDEX_op_qemu_st16
, { "L", "L" } },
1222 { INDEX_op_qemu_st32
, { "L", "L" } },
1223 { INDEX_op_qemu_st64
, { "L", "L", "L" } },
1225 { INDEX_op_qemu_ld8u
, { "r", "L", "L" } },
1226 { INDEX_op_qemu_ld8s
, { "r", "L", "L" } },
1227 { INDEX_op_qemu_ld16u
, { "r", "L", "L" } },
1228 { INDEX_op_qemu_ld16s
, { "r", "L", "L" } },
1229 { INDEX_op_qemu_ld32u
, { "r", "L", "L" } },
1230 { INDEX_op_qemu_ld64
, { "r", "r", "L", "L" } },
1232 { INDEX_op_qemu_st8
, { "cb", "L", "L" } },
1233 { INDEX_op_qemu_st16
, { "L", "L", "L" } },
1234 { INDEX_op_qemu_st32
, { "L", "L", "L" } },
1235 { INDEX_op_qemu_st64
, { "L", "L", "L", "L" } },
1240 static int tcg_target_callee_save_regs
[] = {
1241 /* TCG_REG_EBP, */ /* currently used for the global env, so no
1248 static inline void tcg_out_push(TCGContext
*s
, int reg
)
1250 tcg_out_opc(s
, 0x50 + reg
);
1253 static inline void tcg_out_pop(TCGContext
*s
, int reg
)
1255 tcg_out_opc(s
, 0x58 + reg
);
1258 /* Generate global QEMU prologue and epilogue code */
1259 void tcg_target_qemu_prologue(TCGContext
*s
)
1261 int i
, frame_size
, push_size
, stack_addend
;
1264 /* save all callee saved registers */
1265 for(i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); i
++) {
1266 tcg_out_push(s
, tcg_target_callee_save_regs
[i
]);
1268 /* reserve some stack space */
1269 push_size
= 4 + ARRAY_SIZE(tcg_target_callee_save_regs
) * 4;
1270 frame_size
= push_size
+ TCG_STATIC_CALL_ARGS_SIZE
;
1271 frame_size
= (frame_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1272 ~(TCG_TARGET_STACK_ALIGN
- 1);
1273 stack_addend
= frame_size
- push_size
;
1274 tcg_out_addi(s
, TCG_REG_ESP
, -stack_addend
);
1276 tcg_out_modrm(s
, 0xff, 4, TCG_REG_EAX
); /* jmp *%eax */
1279 tb_ret_addr
= s
->code_ptr
;
1280 tcg_out_addi(s
, TCG_REG_ESP
, stack_addend
);
1281 for(i
= ARRAY_SIZE(tcg_target_callee_save_regs
) - 1; i
>= 0; i
--) {
1282 tcg_out_pop(s
, tcg_target_callee_save_regs
[i
]);
1284 tcg_out8(s
, 0xc3); /* ret */
1287 void tcg_target_init(TCGContext
*s
)
1290 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
))
1293 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xff);
1294 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1295 (1 << TCG_REG_EAX
) |
1296 (1 << TCG_REG_EDX
) |
1297 (1 << TCG_REG_ECX
));
1299 tcg_regset_clear(s
->reserved_regs
);
1300 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_ESP
);
1302 tcg_add_target_add_op_defs(x86_op_defs
);