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
;
69 /* maximum number of register used for input function arguments */
70 static inline int tcg_target_get_call_iarg_regs_count(int flags
)
72 flags
&= TCG_CALL_TYPE_MASK
;
74 case TCG_CALL_TYPE_STD
:
76 case TCG_CALL_TYPE_REGPARM_1
:
77 case TCG_CALL_TYPE_REGPARM_2
:
78 case TCG_CALL_TYPE_REGPARM
:
79 return flags
- TCG_CALL_TYPE_REGPARM_1
+ 1;
85 /* parse target specific constraints */
86 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
94 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EAX
);
98 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EBX
);
101 ct
->ct
|= TCG_CT_REG
;
102 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_ECX
);
105 ct
->ct
|= TCG_CT_REG
;
106 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EDX
);
109 ct
->ct
|= TCG_CT_REG
;
110 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_ESI
);
113 ct
->ct
|= TCG_CT_REG
;
114 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_EDI
);
117 ct
->ct
|= TCG_CT_REG
;
118 tcg_regset_set32(ct
->u
.regs
, 0, 0xf);
121 ct
->ct
|= TCG_CT_REG
;
122 tcg_regset_set32(ct
->u
.regs
, 0, 0xff);
125 /* qemu_ld/st address constraint */
127 ct
->ct
|= TCG_CT_REG
;
128 tcg_regset_set32(ct
->u
.regs
, 0, 0xff);
129 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_EAX
);
130 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_EDX
);
140 /* test if a constant matches the constraint */
141 static inline int tcg_target_const_match(tcg_target_long val
,
142 const TCGArgConstraint
*arg_ct
)
146 if (ct
& TCG_CT_CONST
)
185 #define P_EXT 0x100 /* 0x0f opcode prefix */
187 static const uint8_t tcg_cond_to_jcc
[10] = {
188 [TCG_COND_EQ
] = JCC_JE
,
189 [TCG_COND_NE
] = JCC_JNE
,
190 [TCG_COND_LT
] = JCC_JL
,
191 [TCG_COND_GE
] = JCC_JGE
,
192 [TCG_COND_LE
] = JCC_JLE
,
193 [TCG_COND_GT
] = JCC_JG
,
194 [TCG_COND_LTU
] = JCC_JB
,
195 [TCG_COND_GEU
] = JCC_JAE
,
196 [TCG_COND_LEU
] = JCC_JBE
,
197 [TCG_COND_GTU
] = JCC_JA
,
200 static inline void tcg_out_opc(TCGContext
*s
, int opc
)
207 static inline void tcg_out_modrm(TCGContext
*s
, int opc
, int r
, int rm
)
210 tcg_out8(s
, 0xc0 | (r
<< 3) | rm
);
213 /* rm == -1 means no register index */
214 static inline void tcg_out_modrm_offset(TCGContext
*s
, int opc
, int r
, int rm
,
219 tcg_out8(s
, 0x05 | (r
<< 3));
220 tcg_out32(s
, offset
);
221 } else if (offset
== 0 && rm
!= TCG_REG_EBP
) {
222 if (rm
== TCG_REG_ESP
) {
223 tcg_out8(s
, 0x04 | (r
<< 3));
226 tcg_out8(s
, 0x00 | (r
<< 3) | rm
);
228 } else if ((int8_t)offset
== offset
) {
229 if (rm
== TCG_REG_ESP
) {
230 tcg_out8(s
, 0x44 | (r
<< 3));
233 tcg_out8(s
, 0x40 | (r
<< 3) | rm
);
237 if (rm
== TCG_REG_ESP
) {
238 tcg_out8(s
, 0x84 | (r
<< 3));
241 tcg_out8(s
, 0x80 | (r
<< 3) | rm
);
243 tcg_out32(s
, offset
);
247 static inline void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
250 tcg_out_modrm(s
, 0x8b, ret
, arg
);
253 static inline void tcg_out_movi(TCGContext
*s
, TCGType type
,
254 int ret
, int32_t arg
)
258 tcg_out_modrm(s
, 0x01 | (ARITH_XOR
<< 3), ret
, ret
);
260 tcg_out8(s
, 0xb8 + ret
);
265 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, int ret
,
266 int arg1
, tcg_target_long arg2
)
269 tcg_out_modrm_offset(s
, 0x8b, ret
, arg1
, arg2
);
272 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, int arg
,
273 int arg1
, tcg_target_long arg2
)
276 tcg_out_modrm_offset(s
, 0x89, arg
, arg1
, arg2
);
279 static inline void tgen_arithi(TCGContext
*s
, int c
, int r0
, int32_t val
)
281 if (val
== (int8_t)val
) {
282 tcg_out_modrm(s
, 0x83, c
, r0
);
285 tcg_out_modrm(s
, 0x81, c
, r0
);
290 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
293 tgen_arithi(s
, ARITH_ADD
, reg
, val
);
296 static void tcg_out_jxx(TCGContext
*s
, int opc
, int label_index
)
299 TCGLabel
*l
= &s
->labels
[label_index
];
302 val
= l
->u
.value
- (tcg_target_long
)s
->code_ptr
;
304 if ((int8_t)val1
== val1
) {
308 tcg_out8(s
, 0x70 + opc
);
313 tcg_out32(s
, val
- 5);
316 tcg_out8(s
, 0x80 + opc
);
317 tcg_out32(s
, val
- 6);
325 tcg_out8(s
, 0x80 + opc
);
327 tcg_out_reloc(s
, s
->code_ptr
, R_386_PC32
, label_index
, -4);
332 static void tcg_out_brcond(TCGContext
*s
, int cond
,
333 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
339 tcg_out_modrm(s
, 0x85, arg1
, arg1
);
341 tgen_arithi(s
, ARITH_CMP
, arg1
, arg2
);
344 tcg_out_modrm(s
, 0x01 | (ARITH_CMP
<< 3), arg2
, arg1
);
346 tcg_out_jxx(s
, tcg_cond_to_jcc
[cond
], label_index
);
349 /* XXX: we implement it at the target level to avoid having to
350 handle cross basic blocks temporaries */
351 static void tcg_out_brcond2(TCGContext
*s
,
352 const TCGArg
*args
, const int *const_args
)
355 label_next
= gen_new_label();
358 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2], label_next
);
359 tcg_out_brcond(s
, TCG_COND_EQ
, args
[1], args
[3], const_args
[3], args
[5]);
362 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2], args
[5]);
363 tcg_out_brcond(s
, TCG_COND_NE
, args
[1], args
[3], const_args
[3], args
[5]);
366 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3], args
[5]);
367 tcg_out_jxx(s
, JCC_JNE
, label_next
);
368 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2], args
[5]);
371 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3], args
[5]);
372 tcg_out_jxx(s
, JCC_JNE
, label_next
);
373 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2], args
[5]);
376 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3], args
[5]);
377 tcg_out_jxx(s
, JCC_JNE
, label_next
);
378 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2], args
[5]);
381 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3], args
[5]);
382 tcg_out_jxx(s
, JCC_JNE
, label_next
);
383 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2], args
[5]);
386 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3], args
[5]);
387 tcg_out_jxx(s
, JCC_JNE
, label_next
);
388 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2], args
[5]);
391 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3], args
[5]);
392 tcg_out_jxx(s
, JCC_JNE
, label_next
);
393 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2], args
[5]);
396 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3], args
[5]);
397 tcg_out_jxx(s
, JCC_JNE
, label_next
);
398 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2], args
[5]);
401 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3], args
[5]);
402 tcg_out_jxx(s
, JCC_JNE
, label_next
);
403 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2], args
[5]);
408 tcg_out_label(s
, label_next
, (tcg_target_long
)s
->code_ptr
);
411 #if defined(CONFIG_SOFTMMU)
413 #include "../../softmmu_defs.h"
415 static void *qemu_ld_helpers
[4] = {
422 static void *qemu_st_helpers
[4] = {
430 /* XXX: qemu_ld and qemu_st could be modified to clobber only EDX and
431 EAX. It will be useful once fixed registers globals are less
433 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
,
436 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
437 #if defined(CONFIG_SOFTMMU)
438 uint8_t *label1_ptr
, *label2_ptr
;
440 #if TARGET_LONG_BITS == 64
441 #if defined(CONFIG_SOFTMMU)
453 #if TARGET_LONG_BITS == 64
462 #if defined(CONFIG_SOFTMMU)
463 tcg_out_mov(s
, r1
, addr_reg
);
465 tcg_out_mov(s
, r0
, addr_reg
);
467 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
468 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
470 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
471 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
473 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
474 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
476 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
477 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
478 tcg_out8(s
, (5 << 3) | r1
);
479 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_read
));
482 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
484 tcg_out_mov(s
, r0
, addr_reg
);
486 #if TARGET_LONG_BITS == 32
488 tcg_out8(s
, 0x70 + JCC_JE
);
489 label1_ptr
= s
->code_ptr
;
493 tcg_out8(s
, 0x70 + JCC_JNE
);
494 label3_ptr
= s
->code_ptr
;
497 /* cmp 4(r1), addr_reg2 */
498 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
501 tcg_out8(s
, 0x70 + JCC_JE
);
502 label1_ptr
= s
->code_ptr
;
506 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
509 /* XXX: move that code at the end of the TB */
510 #if TARGET_LONG_BITS == 32
511 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EDX
, mem_index
);
513 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
514 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
517 tcg_out32(s
, (tcg_target_long
)qemu_ld_helpers
[s_bits
] -
518 (tcg_target_long
)s
->code_ptr
- 4);
523 tcg_out_modrm(s
, 0xbe | P_EXT
, data_reg
, TCG_REG_EAX
);
527 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, TCG_REG_EAX
);
531 tcg_out_modrm(s
, 0xb6 | P_EXT
, data_reg
, TCG_REG_EAX
);
535 tcg_out_modrm(s
, 0xb7 | P_EXT
, data_reg
, TCG_REG_EAX
);
539 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
542 if (data_reg
== TCG_REG_EDX
) {
543 tcg_out_opc(s
, 0x90 + TCG_REG_EDX
); /* xchg %edx, %eax */
544 tcg_out_mov(s
, data_reg2
, TCG_REG_EAX
);
546 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
547 tcg_out_mov(s
, data_reg2
, TCG_REG_EDX
);
554 label2_ptr
= s
->code_ptr
;
558 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
561 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
562 offsetof(CPUTLBEntry
, addr_read
));
567 #ifdef TARGET_WORDS_BIGENDIAN
575 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, data_reg
, r0
, 0);
579 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, data_reg
, r0
, 0);
583 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, data_reg
, r0
, 0);
585 /* rolw $8, data_reg */
587 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
593 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, data_reg
, r0
, 0);
595 /* rolw $8, data_reg */
597 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
600 /* movswl data_reg, data_reg */
601 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, data_reg
);
605 /* movl (r0), data_reg */
606 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, 0);
609 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
613 /* XXX: could be nicer */
614 if (r0
== data_reg
) {
618 tcg_out_mov(s
, r1
, r0
);
622 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, 0);
623 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, 4);
625 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, 4);
626 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
628 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, 0);
630 tcg_out_opc(s
, (0xc8 + data_reg2
) | P_EXT
);
637 #if defined(CONFIG_SOFTMMU)
639 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
644 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
,
647 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
648 #if defined(CONFIG_SOFTMMU)
649 uint8_t *label1_ptr
, *label2_ptr
;
651 #if TARGET_LONG_BITS == 64
652 #if defined(CONFIG_SOFTMMU)
664 #if TARGET_LONG_BITS == 64
674 #if defined(CONFIG_SOFTMMU)
675 tcg_out_mov(s
, r1
, addr_reg
);
677 tcg_out_mov(s
, r0
, addr_reg
);
679 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
680 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
682 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
683 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
685 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
686 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
688 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
689 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
690 tcg_out8(s
, (5 << 3) | r1
);
691 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_write
));
694 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
696 tcg_out_mov(s
, r0
, addr_reg
);
698 #if TARGET_LONG_BITS == 32
700 tcg_out8(s
, 0x70 + JCC_JE
);
701 label1_ptr
= s
->code_ptr
;
705 tcg_out8(s
, 0x70 + JCC_JNE
);
706 label3_ptr
= s
->code_ptr
;
709 /* cmp 4(r1), addr_reg2 */
710 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
713 tcg_out8(s
, 0x70 + JCC_JE
);
714 label1_ptr
= s
->code_ptr
;
718 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
721 /* XXX: move that code at the end of the TB */
722 #if TARGET_LONG_BITS == 32
724 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
725 tcg_out_mov(s
, TCG_REG_ECX
, data_reg2
);
726 tcg_out8(s
, 0x6a); /* push Ib */
727 tcg_out8(s
, mem_index
);
729 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
730 (tcg_target_long
)s
->code_ptr
- 4);
731 tcg_out_addi(s
, TCG_REG_ESP
, 4);
736 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_EDX
, data_reg
);
740 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_EDX
, data_reg
);
743 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
746 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
748 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
749 (tcg_target_long
)s
->code_ptr
- 4);
753 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
754 tcg_out8(s
, 0x6a); /* push Ib */
755 tcg_out8(s
, mem_index
);
756 tcg_out_opc(s
, 0x50 + data_reg2
); /* push */
757 tcg_out_opc(s
, 0x50 + data_reg
); /* push */
759 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
760 (tcg_target_long
)s
->code_ptr
- 4);
761 tcg_out_addi(s
, TCG_REG_ESP
, 12);
763 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
767 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_ECX
, data_reg
);
771 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_ECX
, data_reg
);
774 tcg_out_mov(s
, TCG_REG_ECX
, data_reg
);
777 tcg_out8(s
, 0x6a); /* push Ib */
778 tcg_out8(s
, mem_index
);
780 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
781 (tcg_target_long
)s
->code_ptr
- 4);
782 tcg_out_addi(s
, TCG_REG_ESP
, 4);
788 label2_ptr
= s
->code_ptr
;
792 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
795 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
796 offsetof(CPUTLBEntry
, addr_write
));
801 #ifdef TARGET_WORDS_BIGENDIAN
809 tcg_out_modrm_offset(s
, 0x88, data_reg
, r0
, 0);
813 tcg_out_mov(s
, r1
, data_reg
);
814 tcg_out8(s
, 0x66); /* rolw $8, %ecx */
815 tcg_out_modrm(s
, 0xc1, 0, r1
);
821 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, 0);
825 tcg_out_mov(s
, r1
, data_reg
);
827 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
831 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, 0);
835 tcg_out_mov(s
, r1
, data_reg2
);
837 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
838 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, 0);
839 tcg_out_mov(s
, r1
, data_reg
);
841 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
842 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, 4);
844 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, 0);
845 tcg_out_modrm_offset(s
, 0x89, data_reg2
, r0
, 4);
852 #if defined(CONFIG_SOFTMMU)
854 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
858 static inline void tcg_out_op(TCGContext
*s
, int opc
,
859 const TCGArg
*args
, const int *const_args
)
864 case INDEX_op_exit_tb
:
865 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EAX
, args
[0]);
866 tcg_out8(s
, 0xe9); /* jmp tb_ret_addr */
867 tcg_out32(s
, tb_ret_addr
- s
->code_ptr
- 4);
869 case INDEX_op_goto_tb
:
870 if (s
->tb_jmp_offset
) {
871 /* direct jump method */
872 tcg_out8(s
, 0xe9); /* jmp im */
873 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
876 /* indirect jump method */
878 tcg_out_modrm_offset(s
, 0xff, 4, -1,
879 (tcg_target_long
)(s
->tb_next
+ args
[0]));
881 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
886 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
888 tcg_out_modrm(s
, 0xff, 2, args
[0]);
894 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
896 tcg_out_modrm(s
, 0xff, 4, args
[0]);
900 tcg_out_jxx(s
, JCC_JMP
, args
[0]);
902 case INDEX_op_movi_i32
:
903 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
905 case INDEX_op_ld8u_i32
:
907 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, args
[0], args
[1], args
[2]);
909 case INDEX_op_ld8s_i32
:
911 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, args
[0], args
[1], args
[2]);
913 case INDEX_op_ld16u_i32
:
915 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, args
[0], args
[1], args
[2]);
917 case INDEX_op_ld16s_i32
:
919 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, args
[0], args
[1], args
[2]);
921 case INDEX_op_ld_i32
:
923 tcg_out_modrm_offset(s
, 0x8b, args
[0], args
[1], args
[2]);
925 case INDEX_op_st8_i32
:
927 tcg_out_modrm_offset(s
, 0x88, args
[0], args
[1], args
[2]);
929 case INDEX_op_st16_i32
:
932 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
934 case INDEX_op_st_i32
:
936 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
938 case INDEX_op_sub_i32
:
941 case INDEX_op_and_i32
:
944 case INDEX_op_or_i32
:
947 case INDEX_op_xor_i32
:
950 case INDEX_op_add_i32
:
954 tgen_arithi(s
, c
, args
[0], args
[2]);
956 tcg_out_modrm(s
, 0x01 | (c
<< 3), args
[2], args
[0]);
959 case INDEX_op_mul_i32
:
963 if (val
== (int8_t)val
) {
964 tcg_out_modrm(s
, 0x6b, args
[0], args
[0]);
967 tcg_out_modrm(s
, 0x69, args
[0], args
[0]);
971 tcg_out_modrm(s
, 0xaf | P_EXT
, args
[0], args
[2]);
974 case INDEX_op_mulu2_i32
:
975 tcg_out_modrm(s
, 0xf7, 4, args
[3]);
977 case INDEX_op_div2_i32
:
978 tcg_out_modrm(s
, 0xf7, 7, args
[4]);
980 case INDEX_op_divu2_i32
:
981 tcg_out_modrm(s
, 0xf7, 6, args
[4]);
983 case INDEX_op_shl_i32
:
988 tcg_out_modrm(s
, 0xd1, c
, args
[0]);
990 tcg_out_modrm(s
, 0xc1, c
, args
[0]);
991 tcg_out8(s
, args
[2]);
994 tcg_out_modrm(s
, 0xd3, c
, args
[0]);
997 case INDEX_op_shr_i32
:
1000 case INDEX_op_sar_i32
:
1003 case INDEX_op_rotl_i32
:
1006 case INDEX_op_rotr_i32
:
1010 case INDEX_op_add2_i32
:
1012 tgen_arithi(s
, ARITH_ADD
, args
[0], args
[4]);
1014 tcg_out_modrm(s
, 0x01 | (ARITH_ADD
<< 3), args
[4], args
[0]);
1016 tgen_arithi(s
, ARITH_ADC
, args
[1], args
[5]);
1018 tcg_out_modrm(s
, 0x01 | (ARITH_ADC
<< 3), args
[5], args
[1]);
1020 case INDEX_op_sub2_i32
:
1022 tgen_arithi(s
, ARITH_SUB
, args
[0], args
[4]);
1024 tcg_out_modrm(s
, 0x01 | (ARITH_SUB
<< 3), args
[4], args
[0]);
1026 tgen_arithi(s
, ARITH_SBB
, args
[1], args
[5]);
1028 tcg_out_modrm(s
, 0x01 | (ARITH_SBB
<< 3), args
[5], args
[1]);
1030 case INDEX_op_brcond_i32
:
1031 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1], args
[3]);
1033 case INDEX_op_brcond2_i32
:
1034 tcg_out_brcond2(s
, args
, const_args
);
1037 case INDEX_op_bswap16_i32
:
1039 tcg_out_modrm(s
, 0xc1, SHIFT_ROL
, args
[0]);
1042 case INDEX_op_bswap32_i32
:
1043 tcg_out_opc(s
, (0xc8 + args
[0]) | P_EXT
);
1046 case INDEX_op_neg_i32
:
1047 tcg_out_modrm(s
, 0xf7, 3, args
[0]);
1050 case INDEX_op_not_i32
:
1051 tcg_out_modrm(s
, 0xf7, 2, args
[0]);
1054 case INDEX_op_ext8s_i32
:
1055 tcg_out_modrm(s
, 0xbe | P_EXT
, args
[0], args
[1]);
1057 case INDEX_op_ext16s_i32
:
1058 tcg_out_modrm(s
, 0xbf | P_EXT
, args
[0], args
[1]);
1061 case INDEX_op_qemu_ld8u
:
1062 tcg_out_qemu_ld(s
, args
, 0);
1064 case INDEX_op_qemu_ld8s
:
1065 tcg_out_qemu_ld(s
, args
, 0 | 4);
1067 case INDEX_op_qemu_ld16u
:
1068 tcg_out_qemu_ld(s
, args
, 1);
1070 case INDEX_op_qemu_ld16s
:
1071 tcg_out_qemu_ld(s
, args
, 1 | 4);
1073 case INDEX_op_qemu_ld32u
:
1074 tcg_out_qemu_ld(s
, args
, 2);
1076 case INDEX_op_qemu_ld64
:
1077 tcg_out_qemu_ld(s
, args
, 3);
1080 case INDEX_op_qemu_st8
:
1081 tcg_out_qemu_st(s
, args
, 0);
1083 case INDEX_op_qemu_st16
:
1084 tcg_out_qemu_st(s
, args
, 1);
1086 case INDEX_op_qemu_st32
:
1087 tcg_out_qemu_st(s
, args
, 2);
1089 case INDEX_op_qemu_st64
:
1090 tcg_out_qemu_st(s
, args
, 3);
1098 static const TCGTargetOpDef x86_op_defs
[] = {
1099 { INDEX_op_exit_tb
, { } },
1100 { INDEX_op_goto_tb
, { } },
1101 { INDEX_op_call
, { "ri" } },
1102 { INDEX_op_jmp
, { "ri" } },
1103 { INDEX_op_br
, { } },
1104 { INDEX_op_mov_i32
, { "r", "r" } },
1105 { INDEX_op_movi_i32
, { "r" } },
1106 { INDEX_op_ld8u_i32
, { "r", "r" } },
1107 { INDEX_op_ld8s_i32
, { "r", "r" } },
1108 { INDEX_op_ld16u_i32
, { "r", "r" } },
1109 { INDEX_op_ld16s_i32
, { "r", "r" } },
1110 { INDEX_op_ld_i32
, { "r", "r" } },
1111 { INDEX_op_st8_i32
, { "q", "r" } },
1112 { INDEX_op_st16_i32
, { "r", "r" } },
1113 { INDEX_op_st_i32
, { "r", "r" } },
1115 { INDEX_op_add_i32
, { "r", "0", "ri" } },
1116 { INDEX_op_sub_i32
, { "r", "0", "ri" } },
1117 { INDEX_op_mul_i32
, { "r", "0", "ri" } },
1118 { INDEX_op_mulu2_i32
, { "a", "d", "a", "r" } },
1119 { INDEX_op_div2_i32
, { "a", "d", "0", "1", "r" } },
1120 { INDEX_op_divu2_i32
, { "a", "d", "0", "1", "r" } },
1121 { INDEX_op_and_i32
, { "r", "0", "ri" } },
1122 { INDEX_op_or_i32
, { "r", "0", "ri" } },
1123 { INDEX_op_xor_i32
, { "r", "0", "ri" } },
1125 { INDEX_op_shl_i32
, { "r", "0", "ci" } },
1126 { INDEX_op_shr_i32
, { "r", "0", "ci" } },
1127 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1128 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1129 { INDEX_op_rotl_i32
, { "r", "0", "ci" } },
1130 { INDEX_op_rotr_i32
, { "r", "0", "ci" } },
1132 { INDEX_op_brcond_i32
, { "r", "ri" } },
1134 { INDEX_op_add2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1135 { INDEX_op_sub2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1136 { INDEX_op_brcond2_i32
, { "r", "r", "ri", "ri" } },
1138 { INDEX_op_bswap16_i32
, { "r", "0" } },
1139 { INDEX_op_bswap32_i32
, { "r", "0" } },
1141 { INDEX_op_neg_i32
, { "r", "0" } },
1143 { INDEX_op_not_i32
, { "r", "0" } },
1145 { INDEX_op_ext8s_i32
, { "r", "q" } },
1146 { INDEX_op_ext16s_i32
, { "r", "r" } },
1148 #if TARGET_LONG_BITS == 32
1149 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1150 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1151 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1152 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1153 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1154 { INDEX_op_qemu_ld64
, { "r", "r", "L" } },
1156 { INDEX_op_qemu_st8
, { "cb", "L" } },
1157 { INDEX_op_qemu_st16
, { "L", "L" } },
1158 { INDEX_op_qemu_st32
, { "L", "L" } },
1159 { INDEX_op_qemu_st64
, { "L", "L", "L" } },
1161 { INDEX_op_qemu_ld8u
, { "r", "L", "L" } },
1162 { INDEX_op_qemu_ld8s
, { "r", "L", "L" } },
1163 { INDEX_op_qemu_ld16u
, { "r", "L", "L" } },
1164 { INDEX_op_qemu_ld16s
, { "r", "L", "L" } },
1165 { INDEX_op_qemu_ld32u
, { "r", "L", "L" } },
1166 { INDEX_op_qemu_ld64
, { "r", "r", "L", "L" } },
1168 { INDEX_op_qemu_st8
, { "cb", "L", "L" } },
1169 { INDEX_op_qemu_st16
, { "L", "L", "L" } },
1170 { INDEX_op_qemu_st32
, { "L", "L", "L" } },
1171 { INDEX_op_qemu_st64
, { "L", "L", "L", "L" } },
1176 static int tcg_target_callee_save_regs
[] = {
1177 /* TCG_REG_EBP, */ /* currently used for the global env, so no
1184 static inline void tcg_out_push(TCGContext
*s
, int reg
)
1186 tcg_out_opc(s
, 0x50 + reg
);
1189 static inline void tcg_out_pop(TCGContext
*s
, int reg
)
1191 tcg_out_opc(s
, 0x58 + reg
);
1194 /* Generate global QEMU prologue and epilogue code */
1195 void tcg_target_qemu_prologue(TCGContext
*s
)
1197 int i
, frame_size
, push_size
, stack_addend
;
1200 /* save all callee saved registers */
1201 for(i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); i
++) {
1202 tcg_out_push(s
, tcg_target_callee_save_regs
[i
]);
1204 /* reserve some stack space */
1205 push_size
= 4 + ARRAY_SIZE(tcg_target_callee_save_regs
) * 4;
1206 frame_size
= push_size
+ TCG_STATIC_CALL_ARGS_SIZE
;
1207 frame_size
= (frame_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1208 ~(TCG_TARGET_STACK_ALIGN
- 1);
1209 stack_addend
= frame_size
- push_size
;
1210 tcg_out_addi(s
, TCG_REG_ESP
, -stack_addend
);
1212 tcg_out_modrm(s
, 0xff, 4, TCG_REG_EAX
); /* jmp *%eax */
1215 tb_ret_addr
= s
->code_ptr
;
1216 tcg_out_addi(s
, TCG_REG_ESP
, stack_addend
);
1217 for(i
= ARRAY_SIZE(tcg_target_callee_save_regs
) - 1; i
>= 0; i
--) {
1218 tcg_out_pop(s
, tcg_target_callee_save_regs
[i
]);
1220 tcg_out8(s
, 0xc3); /* ret */
1223 void tcg_target_init(TCGContext
*s
)
1226 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
))
1229 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xff);
1230 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1231 (1 << TCG_REG_EAX
) |
1232 (1 << TCG_REG_EDX
) |
1233 (1 << TCG_REG_ECX
));
1235 tcg_regset_clear(s
->reserved_regs
);
1236 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_ESP
);
1238 tcg_add_target_add_op_defs(x86_op_defs
);