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
, int cf
)
281 if (!cf
&& ((c
== ARITH_ADD
&& val
== 1) || (c
== ARITH_SUB
&& val
== -1))) {
283 tcg_out_opc(s
, 0x40 + r0
);
284 } else if (!cf
&& ((c
== ARITH_ADD
&& val
== -1) || (c
== ARITH_SUB
&& val
== 1))) {
286 tcg_out_opc(s
, 0x48 + r0
);
287 } else if (val
== (int8_t)val
) {
288 tcg_out_modrm(s
, 0x83, c
, r0
);
290 } else if (c
== ARITH_AND
&& val
== 0xffu
&& r0
< 4) {
292 tcg_out_modrm(s
, 0xb6 | P_EXT
, r0
, r0
);
293 } else if (c
== ARITH_AND
&& val
== 0xffffu
) {
295 tcg_out_modrm(s
, 0xb7 | P_EXT
, r0
, r0
);
297 tcg_out_modrm(s
, 0x81, c
, r0
);
302 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
305 tgen_arithi(s
, ARITH_ADD
, reg
, val
, 0);
308 static void tcg_out_jxx(TCGContext
*s
, int opc
, int label_index
)
311 TCGLabel
*l
= &s
->labels
[label_index
];
314 val
= l
->u
.value
- (tcg_target_long
)s
->code_ptr
;
316 if ((int8_t)val1
== val1
) {
320 tcg_out8(s
, 0x70 + opc
);
325 tcg_out32(s
, val
- 5);
328 tcg_out8(s
, 0x80 + opc
);
329 tcg_out32(s
, val
- 6);
337 tcg_out8(s
, 0x80 + opc
);
339 tcg_out_reloc(s
, s
->code_ptr
, R_386_PC32
, label_index
, -4);
344 static void tcg_out_brcond(TCGContext
*s
, int cond
,
345 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
351 tcg_out_modrm(s
, 0x85, arg1
, arg1
);
353 tgen_arithi(s
, ARITH_CMP
, arg1
, arg2
, 0);
356 tcg_out_modrm(s
, 0x01 | (ARITH_CMP
<< 3), arg2
, arg1
);
358 tcg_out_jxx(s
, tcg_cond_to_jcc
[cond
], label_index
);
361 /* XXX: we implement it at the target level to avoid having to
362 handle cross basic blocks temporaries */
363 static void tcg_out_brcond2(TCGContext
*s
,
364 const TCGArg
*args
, const int *const_args
)
367 label_next
= gen_new_label();
370 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2], label_next
);
371 tcg_out_brcond(s
, TCG_COND_EQ
, args
[1], args
[3], const_args
[3], args
[5]);
374 tcg_out_brcond(s
, TCG_COND_NE
, args
[0], args
[2], const_args
[2], args
[5]);
375 tcg_out_brcond(s
, TCG_COND_NE
, args
[1], args
[3], const_args
[3], args
[5]);
378 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3], args
[5]);
379 tcg_out_jxx(s
, JCC_JNE
, label_next
);
380 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2], args
[5]);
383 tcg_out_brcond(s
, TCG_COND_LT
, args
[1], args
[3], const_args
[3], args
[5]);
384 tcg_out_jxx(s
, JCC_JNE
, label_next
);
385 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2], args
[5]);
388 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3], args
[5]);
389 tcg_out_jxx(s
, JCC_JNE
, label_next
);
390 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2], args
[5]);
393 tcg_out_brcond(s
, TCG_COND_GT
, args
[1], args
[3], const_args
[3], args
[5]);
394 tcg_out_jxx(s
, JCC_JNE
, label_next
);
395 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2], args
[5]);
398 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3], args
[5]);
399 tcg_out_jxx(s
, JCC_JNE
, label_next
);
400 tcg_out_brcond(s
, TCG_COND_LTU
, args
[0], args
[2], const_args
[2], args
[5]);
403 tcg_out_brcond(s
, TCG_COND_LTU
, args
[1], args
[3], const_args
[3], args
[5]);
404 tcg_out_jxx(s
, JCC_JNE
, label_next
);
405 tcg_out_brcond(s
, TCG_COND_LEU
, args
[0], args
[2], const_args
[2], args
[5]);
408 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3], args
[5]);
409 tcg_out_jxx(s
, JCC_JNE
, label_next
);
410 tcg_out_brcond(s
, TCG_COND_GTU
, args
[0], args
[2], const_args
[2], args
[5]);
413 tcg_out_brcond(s
, TCG_COND_GTU
, args
[1], args
[3], const_args
[3], args
[5]);
414 tcg_out_jxx(s
, JCC_JNE
, label_next
);
415 tcg_out_brcond(s
, TCG_COND_GEU
, args
[0], args
[2], const_args
[2], args
[5]);
420 tcg_out_label(s
, label_next
, (tcg_target_long
)s
->code_ptr
);
423 #if defined(CONFIG_SOFTMMU)
425 #include "../../softmmu_defs.h"
427 static void *qemu_ld_helpers
[4] = {
434 static void *qemu_st_helpers
[4] = {
442 #ifndef CONFIG_USER_ONLY
446 /* XXX: qemu_ld and qemu_st could be modified to clobber only EDX and
447 EAX. It will be useful once fixed registers globals are less
449 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
,
452 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
453 #if defined(CONFIG_SOFTMMU)
454 uint8_t *label1_ptr
, *label2_ptr
;
456 #if TARGET_LONG_BITS == 64
457 #if defined(CONFIG_SOFTMMU)
469 #if TARGET_LONG_BITS == 64
478 #if defined(CONFIG_SOFTMMU)
479 tcg_out_mov(s
, r1
, addr_reg
);
481 tcg_out_mov(s
, r0
, addr_reg
);
483 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
484 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
486 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
487 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
489 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
490 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
492 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
493 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
494 tcg_out8(s
, (5 << 3) | r1
);
495 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_read
));
498 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
500 tcg_out_mov(s
, r0
, addr_reg
);
502 #if TARGET_LONG_BITS == 32
504 tcg_out8(s
, 0x70 + JCC_JE
);
505 label1_ptr
= s
->code_ptr
;
509 tcg_out8(s
, 0x70 + JCC_JNE
);
510 label3_ptr
= s
->code_ptr
;
513 /* cmp 4(r1), addr_reg2 */
514 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
517 tcg_out8(s
, 0x70 + JCC_JE
);
518 label1_ptr
= s
->code_ptr
;
522 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
525 /* XXX: move that code at the end of the TB */
526 #if TARGET_LONG_BITS == 32
527 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EDX
, mem_index
);
529 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
530 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
533 tcg_out32(s
, (tcg_target_long
)qemu_ld_helpers
[s_bits
] -
534 (tcg_target_long
)s
->code_ptr
- 4);
539 tcg_out_modrm(s
, 0xbe | P_EXT
, data_reg
, TCG_REG_EAX
);
543 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, TCG_REG_EAX
);
547 tcg_out_modrm(s
, 0xb6 | P_EXT
, data_reg
, TCG_REG_EAX
);
551 tcg_out_modrm(s
, 0xb7 | P_EXT
, data_reg
, TCG_REG_EAX
);
555 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
558 if (data_reg
== TCG_REG_EDX
) {
559 tcg_out_opc(s
, 0x90 + TCG_REG_EDX
); /* xchg %edx, %eax */
560 tcg_out_mov(s
, data_reg2
, TCG_REG_EAX
);
562 tcg_out_mov(s
, data_reg
, TCG_REG_EAX
);
563 tcg_out_mov(s
, data_reg2
, TCG_REG_EDX
);
570 label2_ptr
= s
->code_ptr
;
574 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
577 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
578 offsetof(CPUTLBEntry
, addr_read
));
583 #ifdef TARGET_WORDS_BIGENDIAN
591 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, data_reg
, r0
, GUEST_BASE
);
595 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, data_reg
, r0
, GUEST_BASE
);
599 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, data_reg
, r0
, GUEST_BASE
);
601 /* rolw $8, data_reg */
603 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
609 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, data_reg
, r0
, GUEST_BASE
);
611 /* rolw $8, data_reg */
613 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
616 /* movswl data_reg, data_reg */
617 tcg_out_modrm(s
, 0xbf | P_EXT
, data_reg
, data_reg
);
621 /* movl (r0), data_reg */
622 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
);
625 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
629 /* XXX: could be nicer */
630 if (r0
== data_reg
) {
634 tcg_out_mov(s
, r1
, r0
);
638 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
);
639 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, GUEST_BASE
+ 4);
641 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, GUEST_BASE
+ 4);
642 tcg_out_opc(s
, (0xc8 + data_reg
) | P_EXT
);
644 tcg_out_modrm_offset(s
, 0x8b, data_reg2
, r0
, GUEST_BASE
);
646 tcg_out_opc(s
, (0xc8 + data_reg2
) | P_EXT
);
653 #if defined(CONFIG_SOFTMMU)
655 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
660 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
,
663 int addr_reg
, data_reg
, data_reg2
, r0
, r1
, mem_index
, s_bits
, bswap
;
664 #if defined(CONFIG_SOFTMMU)
665 uint8_t *label1_ptr
, *label2_ptr
;
667 #if TARGET_LONG_BITS == 64
668 #if defined(CONFIG_SOFTMMU)
680 #if TARGET_LONG_BITS == 64
690 #if defined(CONFIG_SOFTMMU)
691 tcg_out_mov(s
, r1
, addr_reg
);
693 tcg_out_mov(s
, r0
, addr_reg
);
695 tcg_out_modrm(s
, 0xc1, 5, r1
); /* shr $x, r1 */
696 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
698 tcg_out_modrm(s
, 0x81, 4, r0
); /* andl $x, r0 */
699 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
701 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
702 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
704 tcg_out_opc(s
, 0x8d); /* lea offset(r1, %ebp), r1 */
705 tcg_out8(s
, 0x80 | (r1
<< 3) | 0x04);
706 tcg_out8(s
, (5 << 3) | r1
);
707 tcg_out32(s
, offsetof(CPUState
, tlb_table
[mem_index
][0].addr_write
));
710 tcg_out_modrm_offset(s
, 0x3b, r0
, r1
, 0);
712 tcg_out_mov(s
, r0
, addr_reg
);
714 #if TARGET_LONG_BITS == 32
716 tcg_out8(s
, 0x70 + JCC_JE
);
717 label1_ptr
= s
->code_ptr
;
721 tcg_out8(s
, 0x70 + JCC_JNE
);
722 label3_ptr
= s
->code_ptr
;
725 /* cmp 4(r1), addr_reg2 */
726 tcg_out_modrm_offset(s
, 0x3b, addr_reg2
, r1
, 4);
729 tcg_out8(s
, 0x70 + JCC_JE
);
730 label1_ptr
= s
->code_ptr
;
734 *label3_ptr
= s
->code_ptr
- label3_ptr
- 1;
737 /* XXX: move that code at the end of the TB */
738 #if TARGET_LONG_BITS == 32
740 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
741 tcg_out_mov(s
, TCG_REG_ECX
, data_reg2
);
742 tcg_out8(s
, 0x6a); /* push Ib */
743 tcg_out8(s
, mem_index
);
745 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
746 (tcg_target_long
)s
->code_ptr
- 4);
747 tcg_out_addi(s
, TCG_REG_ESP
, 4);
752 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_EDX
, data_reg
);
756 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_EDX
, data_reg
);
759 tcg_out_mov(s
, TCG_REG_EDX
, data_reg
);
762 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_ECX
, mem_index
);
764 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
765 (tcg_target_long
)s
->code_ptr
- 4);
769 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
770 tcg_out8(s
, 0x6a); /* push Ib */
771 tcg_out8(s
, mem_index
);
772 tcg_out_opc(s
, 0x50 + data_reg2
); /* push */
773 tcg_out_opc(s
, 0x50 + data_reg
); /* push */
775 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
776 (tcg_target_long
)s
->code_ptr
- 4);
777 tcg_out_addi(s
, TCG_REG_ESP
, 12);
779 tcg_out_mov(s
, TCG_REG_EDX
, addr_reg2
);
783 tcg_out_modrm(s
, 0xb6 | P_EXT
, TCG_REG_ECX
, data_reg
);
787 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_ECX
, data_reg
);
790 tcg_out_mov(s
, TCG_REG_ECX
, data_reg
);
793 tcg_out8(s
, 0x6a); /* push Ib */
794 tcg_out8(s
, mem_index
);
796 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
797 (tcg_target_long
)s
->code_ptr
- 4);
798 tcg_out_addi(s
, TCG_REG_ESP
, 4);
804 label2_ptr
= s
->code_ptr
;
808 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
811 tcg_out_modrm_offset(s
, 0x03, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
812 offsetof(CPUTLBEntry
, addr_write
));
817 #ifdef TARGET_WORDS_BIGENDIAN
825 tcg_out_modrm_offset(s
, 0x88, data_reg
, r0
, GUEST_BASE
);
829 tcg_out_mov(s
, r1
, data_reg
);
830 tcg_out8(s
, 0x66); /* rolw $8, %ecx */
831 tcg_out_modrm(s
, 0xc1, 0, r1
);
837 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
841 tcg_out_mov(s
, r1
, data_reg
);
843 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
847 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
851 tcg_out_mov(s
, r1
, data_reg2
);
853 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
854 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, GUEST_BASE
);
855 tcg_out_mov(s
, r1
, data_reg
);
857 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
);
858 tcg_out_modrm_offset(s
, 0x89, r1
, r0
, GUEST_BASE
+ 4);
860 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, GUEST_BASE
);
861 tcg_out_modrm_offset(s
, 0x89, data_reg2
, r0
, GUEST_BASE
+ 4);
868 #if defined(CONFIG_SOFTMMU)
870 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
874 static inline void tcg_out_op(TCGContext
*s
, int opc
,
875 const TCGArg
*args
, const int *const_args
)
880 case INDEX_op_exit_tb
:
881 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_EAX
, args
[0]);
882 tcg_out8(s
, 0xe9); /* jmp tb_ret_addr */
883 tcg_out32(s
, tb_ret_addr
- s
->code_ptr
- 4);
885 case INDEX_op_goto_tb
:
886 if (s
->tb_jmp_offset
) {
887 /* direct jump method */
888 tcg_out8(s
, 0xe9); /* jmp im */
889 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
892 /* indirect jump method */
894 tcg_out_modrm_offset(s
, 0xff, 4, -1,
895 (tcg_target_long
)(s
->tb_next
+ args
[0]));
897 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
902 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
904 tcg_out_modrm(s
, 0xff, 2, args
[0]);
910 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
912 tcg_out_modrm(s
, 0xff, 4, args
[0]);
916 tcg_out_jxx(s
, JCC_JMP
, args
[0]);
918 case INDEX_op_movi_i32
:
919 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], args
[1]);
921 case INDEX_op_ld8u_i32
:
923 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, args
[0], args
[1], args
[2]);
925 case INDEX_op_ld8s_i32
:
927 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, args
[0], args
[1], args
[2]);
929 case INDEX_op_ld16u_i32
:
931 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, args
[0], args
[1], args
[2]);
933 case INDEX_op_ld16s_i32
:
935 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, args
[0], args
[1], args
[2]);
937 case INDEX_op_ld_i32
:
939 tcg_out_modrm_offset(s
, 0x8b, args
[0], args
[1], args
[2]);
941 case INDEX_op_st8_i32
:
943 tcg_out_modrm_offset(s
, 0x88, args
[0], args
[1], args
[2]);
945 case INDEX_op_st16_i32
:
948 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
950 case INDEX_op_st_i32
:
952 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
954 case INDEX_op_sub_i32
:
957 case INDEX_op_and_i32
:
960 case INDEX_op_or_i32
:
963 case INDEX_op_xor_i32
:
966 case INDEX_op_add_i32
:
970 tgen_arithi(s
, c
, args
[0], args
[2], 0);
972 tcg_out_modrm(s
, 0x01 | (c
<< 3), args
[2], args
[0]);
975 case INDEX_op_mul_i32
:
979 if (val
== (int8_t)val
) {
980 tcg_out_modrm(s
, 0x6b, args
[0], args
[0]);
983 tcg_out_modrm(s
, 0x69, args
[0], args
[0]);
987 tcg_out_modrm(s
, 0xaf | P_EXT
, args
[0], args
[2]);
990 case INDEX_op_mulu2_i32
:
991 tcg_out_modrm(s
, 0xf7, 4, args
[3]);
993 case INDEX_op_div2_i32
:
994 tcg_out_modrm(s
, 0xf7, 7, args
[4]);
996 case INDEX_op_divu2_i32
:
997 tcg_out_modrm(s
, 0xf7, 6, args
[4]);
999 case INDEX_op_shl_i32
:
1002 if (const_args
[2]) {
1004 tcg_out_modrm(s
, 0xd1, c
, args
[0]);
1006 tcg_out_modrm(s
, 0xc1, c
, args
[0]);
1007 tcg_out8(s
, args
[2]);
1010 tcg_out_modrm(s
, 0xd3, c
, args
[0]);
1013 case INDEX_op_shr_i32
:
1016 case INDEX_op_sar_i32
:
1019 case INDEX_op_rotl_i32
:
1022 case INDEX_op_rotr_i32
:
1026 case INDEX_op_add2_i32
:
1028 tgen_arithi(s
, ARITH_ADD
, args
[0], args
[4], 1);
1030 tcg_out_modrm(s
, 0x01 | (ARITH_ADD
<< 3), args
[4], args
[0]);
1032 tgen_arithi(s
, ARITH_ADC
, args
[1], args
[5], 1);
1034 tcg_out_modrm(s
, 0x01 | (ARITH_ADC
<< 3), args
[5], args
[1]);
1036 case INDEX_op_sub2_i32
:
1038 tgen_arithi(s
, ARITH_SUB
, args
[0], args
[4], 1);
1040 tcg_out_modrm(s
, 0x01 | (ARITH_SUB
<< 3), args
[4], args
[0]);
1042 tgen_arithi(s
, ARITH_SBB
, args
[1], args
[5], 1);
1044 tcg_out_modrm(s
, 0x01 | (ARITH_SBB
<< 3), args
[5], args
[1]);
1046 case INDEX_op_brcond_i32
:
1047 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1], args
[3]);
1049 case INDEX_op_brcond2_i32
:
1050 tcg_out_brcond2(s
, args
, const_args
);
1053 case INDEX_op_bswap16_i32
:
1055 tcg_out_modrm(s
, 0xc1, SHIFT_ROL
, args
[0]);
1058 case INDEX_op_bswap32_i32
:
1059 tcg_out_opc(s
, (0xc8 + args
[0]) | P_EXT
);
1062 case INDEX_op_neg_i32
:
1063 tcg_out_modrm(s
, 0xf7, 3, args
[0]);
1066 case INDEX_op_not_i32
:
1067 tcg_out_modrm(s
, 0xf7, 2, args
[0]);
1070 case INDEX_op_ext8s_i32
:
1071 tcg_out_modrm(s
, 0xbe | P_EXT
, args
[0], args
[1]);
1073 case INDEX_op_ext16s_i32
:
1074 tcg_out_modrm(s
, 0xbf | P_EXT
, args
[0], args
[1]);
1076 case INDEX_op_ext8u_i32
:
1077 tcg_out_modrm(s
, 0xb6 | P_EXT
, args
[0], args
[1]);
1079 case INDEX_op_ext16u_i32
:
1080 tcg_out_modrm(s
, 0xb7 | P_EXT
, args
[0], args
[1]);
1083 case INDEX_op_qemu_ld8u
:
1084 tcg_out_qemu_ld(s
, args
, 0);
1086 case INDEX_op_qemu_ld8s
:
1087 tcg_out_qemu_ld(s
, args
, 0 | 4);
1089 case INDEX_op_qemu_ld16u
:
1090 tcg_out_qemu_ld(s
, args
, 1);
1092 case INDEX_op_qemu_ld16s
:
1093 tcg_out_qemu_ld(s
, args
, 1 | 4);
1095 case INDEX_op_qemu_ld32u
:
1096 tcg_out_qemu_ld(s
, args
, 2);
1098 case INDEX_op_qemu_ld64
:
1099 tcg_out_qemu_ld(s
, args
, 3);
1102 case INDEX_op_qemu_st8
:
1103 tcg_out_qemu_st(s
, args
, 0);
1105 case INDEX_op_qemu_st16
:
1106 tcg_out_qemu_st(s
, args
, 1);
1108 case INDEX_op_qemu_st32
:
1109 tcg_out_qemu_st(s
, args
, 2);
1111 case INDEX_op_qemu_st64
:
1112 tcg_out_qemu_st(s
, args
, 3);
1120 static const TCGTargetOpDef x86_op_defs
[] = {
1121 { INDEX_op_exit_tb
, { } },
1122 { INDEX_op_goto_tb
, { } },
1123 { INDEX_op_call
, { "ri" } },
1124 { INDEX_op_jmp
, { "ri" } },
1125 { INDEX_op_br
, { } },
1126 { INDEX_op_mov_i32
, { "r", "r" } },
1127 { INDEX_op_movi_i32
, { "r" } },
1128 { INDEX_op_ld8u_i32
, { "r", "r" } },
1129 { INDEX_op_ld8s_i32
, { "r", "r" } },
1130 { INDEX_op_ld16u_i32
, { "r", "r" } },
1131 { INDEX_op_ld16s_i32
, { "r", "r" } },
1132 { INDEX_op_ld_i32
, { "r", "r" } },
1133 { INDEX_op_st8_i32
, { "q", "r" } },
1134 { INDEX_op_st16_i32
, { "r", "r" } },
1135 { INDEX_op_st_i32
, { "r", "r" } },
1137 { INDEX_op_add_i32
, { "r", "0", "ri" } },
1138 { INDEX_op_sub_i32
, { "r", "0", "ri" } },
1139 { INDEX_op_mul_i32
, { "r", "0", "ri" } },
1140 { INDEX_op_mulu2_i32
, { "a", "d", "a", "r" } },
1141 { INDEX_op_div2_i32
, { "a", "d", "0", "1", "r" } },
1142 { INDEX_op_divu2_i32
, { "a", "d", "0", "1", "r" } },
1143 { INDEX_op_and_i32
, { "r", "0", "ri" } },
1144 { INDEX_op_or_i32
, { "r", "0", "ri" } },
1145 { INDEX_op_xor_i32
, { "r", "0", "ri" } },
1147 { INDEX_op_shl_i32
, { "r", "0", "ci" } },
1148 { INDEX_op_shr_i32
, { "r", "0", "ci" } },
1149 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1150 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1151 { INDEX_op_rotl_i32
, { "r", "0", "ci" } },
1152 { INDEX_op_rotr_i32
, { "r", "0", "ci" } },
1154 { INDEX_op_brcond_i32
, { "r", "ri" } },
1156 { INDEX_op_add2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1157 { INDEX_op_sub2_i32
, { "r", "r", "0", "1", "ri", "ri" } },
1158 { INDEX_op_brcond2_i32
, { "r", "r", "ri", "ri" } },
1160 { INDEX_op_bswap16_i32
, { "r", "0" } },
1161 { INDEX_op_bswap32_i32
, { "r", "0" } },
1163 { INDEX_op_neg_i32
, { "r", "0" } },
1165 { INDEX_op_not_i32
, { "r", "0" } },
1167 { INDEX_op_ext8s_i32
, { "r", "q" } },
1168 { INDEX_op_ext16s_i32
, { "r", "r" } },
1169 { INDEX_op_ext8u_i32
, { "r", "q"} },
1170 { INDEX_op_ext16u_i32
, { "r", "r"} },
1172 #if TARGET_LONG_BITS == 32
1173 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1174 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1175 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1176 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1177 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1178 { INDEX_op_qemu_ld64
, { "r", "r", "L" } },
1180 { INDEX_op_qemu_st8
, { "cb", "L" } },
1181 { INDEX_op_qemu_st16
, { "L", "L" } },
1182 { INDEX_op_qemu_st32
, { "L", "L" } },
1183 { INDEX_op_qemu_st64
, { "L", "L", "L" } },
1185 { INDEX_op_qemu_ld8u
, { "r", "L", "L" } },
1186 { INDEX_op_qemu_ld8s
, { "r", "L", "L" } },
1187 { INDEX_op_qemu_ld16u
, { "r", "L", "L" } },
1188 { INDEX_op_qemu_ld16s
, { "r", "L", "L" } },
1189 { INDEX_op_qemu_ld32u
, { "r", "L", "L" } },
1190 { INDEX_op_qemu_ld64
, { "r", "r", "L", "L" } },
1192 { INDEX_op_qemu_st8
, { "cb", "L", "L" } },
1193 { INDEX_op_qemu_st16
, { "L", "L", "L" } },
1194 { INDEX_op_qemu_st32
, { "L", "L", "L" } },
1195 { INDEX_op_qemu_st64
, { "L", "L", "L", "L" } },
1200 static int tcg_target_callee_save_regs
[] = {
1201 /* TCG_REG_EBP, */ /* currently used for the global env, so no
1208 static inline void tcg_out_push(TCGContext
*s
, int reg
)
1210 tcg_out_opc(s
, 0x50 + reg
);
1213 static inline void tcg_out_pop(TCGContext
*s
, int reg
)
1215 tcg_out_opc(s
, 0x58 + reg
);
1218 /* Generate global QEMU prologue and epilogue code */
1219 void tcg_target_qemu_prologue(TCGContext
*s
)
1221 int i
, frame_size
, push_size
, stack_addend
;
1224 /* save all callee saved registers */
1225 for(i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); i
++) {
1226 tcg_out_push(s
, tcg_target_callee_save_regs
[i
]);
1228 /* reserve some stack space */
1229 push_size
= 4 + ARRAY_SIZE(tcg_target_callee_save_regs
) * 4;
1230 frame_size
= push_size
+ TCG_STATIC_CALL_ARGS_SIZE
;
1231 frame_size
= (frame_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1232 ~(TCG_TARGET_STACK_ALIGN
- 1);
1233 stack_addend
= frame_size
- push_size
;
1234 tcg_out_addi(s
, TCG_REG_ESP
, -stack_addend
);
1236 tcg_out_modrm(s
, 0xff, 4, TCG_REG_EAX
); /* jmp *%eax */
1239 tb_ret_addr
= s
->code_ptr
;
1240 tcg_out_addi(s
, TCG_REG_ESP
, stack_addend
);
1241 for(i
= ARRAY_SIZE(tcg_target_callee_save_regs
) - 1; i
>= 0; i
--) {
1242 tcg_out_pop(s
, tcg_target_callee_save_regs
[i
]);
1244 tcg_out8(s
, 0xc3); /* ret */
1247 void tcg_target_init(TCGContext
*s
)
1250 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
))
1253 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xff);
1254 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1255 (1 << TCG_REG_EAX
) |
1256 (1 << TCG_REG_EDX
) |
1257 (1 << TCG_REG_ECX
));
1259 tcg_regset_clear(s
->reserved_regs
);
1260 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_ESP
);
1262 tcg_add_target_add_op_defs(x86_op_defs
);