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
24 const char *tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
43 int tcg_target_reg_alloc_order
[] = {
62 const int tcg_target_call_iarg_regs
[6] = {
71 const int tcg_target_call_oarg_regs
[2] = {
76 static uint8_t *tb_ret_addr
;
78 static void patch_reloc(uint8_t *code_ptr
, int type
,
79 tcg_target_long value
, tcg_target_long addend
)
84 if (value
!= (uint32_t)value
)
86 *(uint32_t *)code_ptr
= value
;
89 if (value
!= (int32_t)value
)
91 *(uint32_t *)code_ptr
= value
;
94 value
-= (long)code_ptr
;
95 if (value
!= (int32_t)value
)
97 *(uint32_t *)code_ptr
= value
;
104 /* maximum number of register used for input function arguments */
105 static inline int tcg_target_get_call_iarg_regs_count(int flags
)
110 /* parse target specific constraints */
111 int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
118 ct
->ct
|= TCG_CT_REG
;
119 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RAX
);
122 ct
->ct
|= TCG_CT_REG
;
123 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RBX
);
126 ct
->ct
|= TCG_CT_REG
;
127 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RCX
);
130 ct
->ct
|= TCG_CT_REG
;
131 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RDX
);
134 ct
->ct
|= TCG_CT_REG
;
135 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RSI
);
138 ct
->ct
|= TCG_CT_REG
;
139 tcg_regset_set_reg(ct
->u
.regs
, TCG_REG_RDI
);
142 ct
->ct
|= TCG_CT_REG
;
143 tcg_regset_set32(ct
->u
.regs
, 0, 0xf);
146 ct
->ct
|= TCG_CT_REG
;
147 tcg_regset_set32(ct
->u
.regs
, 0, 0xffff);
149 case 'L': /* qemu_ld/st constraint */
150 ct
->ct
|= TCG_CT_REG
;
151 tcg_regset_set32(ct
->u
.regs
, 0, 0xffff);
152 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_RSI
);
153 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_RDI
);
156 ct
->ct
|= TCG_CT_CONST_S32
;
159 ct
->ct
|= TCG_CT_CONST_U32
;
169 /* test if a constant matches the constraint */
170 static inline int tcg_target_const_match(tcg_target_long val
,
171 const TCGArgConstraint
*arg_ct
)
175 if (ct
& TCG_CT_CONST
)
177 else if ((ct
& TCG_CT_CONST_S32
) && val
== (int32_t)val
)
179 else if ((ct
& TCG_CT_CONST_U32
) && val
== (uint32_t)val
)
216 #define P_EXT 0x100 /* 0x0f opcode prefix */
217 #define P_REXW 0x200 /* set rex.w = 1 */
218 #define P_REXB 0x400 /* force rex use for byte registers */
220 static const uint8_t tcg_cond_to_jcc
[10] = {
221 [TCG_COND_EQ
] = JCC_JE
,
222 [TCG_COND_NE
] = JCC_JNE
,
223 [TCG_COND_LT
] = JCC_JL
,
224 [TCG_COND_GE
] = JCC_JGE
,
225 [TCG_COND_LE
] = JCC_JLE
,
226 [TCG_COND_GT
] = JCC_JG
,
227 [TCG_COND_LTU
] = JCC_JB
,
228 [TCG_COND_GEU
] = JCC_JAE
,
229 [TCG_COND_LEU
] = JCC_JBE
,
230 [TCG_COND_GTU
] = JCC_JA
,
233 static inline void tcg_out_opc(TCGContext
*s
, int opc
, int r
, int rm
, int x
)
236 rex
= ((opc
>> 6) & 0x8) | ((r
>> 1) & 0x4) |
237 ((x
>> 2) & 2) | ((rm
>> 3) & 1);
238 if (rex
|| (opc
& P_REXB
)) {
239 tcg_out8(s
, rex
| 0x40);
246 static inline void tcg_out_modrm(TCGContext
*s
, int opc
, int r
, int rm
)
248 tcg_out_opc(s
, opc
, r
, rm
, 0);
249 tcg_out8(s
, 0xc0 | ((r
& 7) << 3) | (rm
& 7));
252 /* rm < 0 means no register index plus (-rm - 1 immediate bytes) */
253 static inline void tcg_out_modrm_offset(TCGContext
*s
, int opc
, int r
, int rm
,
254 tcg_target_long offset
)
258 tcg_out_opc(s
, opc
, r
, 0, 0);
259 val
= offset
- ((tcg_target_long
)s
->code_ptr
+ 5 + (-rm
- 1));
260 if (val
== (int32_t)val
) {
262 tcg_out8(s
, 0x05 | ((r
& 7) << 3));
264 } else if (offset
== (int32_t)offset
) {
265 tcg_out8(s
, 0x04 | ((r
& 7) << 3));
266 tcg_out8(s
, 0x25); /* sib */
267 tcg_out32(s
, offset
);
271 } else if (offset
== 0 && (rm
& 7) != TCG_REG_RBP
) {
272 tcg_out_opc(s
, opc
, r
, rm
, 0);
273 if ((rm
& 7) == TCG_REG_RSP
) {
274 tcg_out8(s
, 0x04 | ((r
& 7) << 3));
277 tcg_out8(s
, 0x00 | ((r
& 7) << 3) | (rm
& 7));
279 } else if ((int8_t)offset
== offset
) {
280 tcg_out_opc(s
, opc
, r
, rm
, 0);
281 if ((rm
& 7) == TCG_REG_RSP
) {
282 tcg_out8(s
, 0x44 | ((r
& 7) << 3));
285 tcg_out8(s
, 0x40 | ((r
& 7) << 3) | (rm
& 7));
289 tcg_out_opc(s
, opc
, r
, rm
, 0);
290 if ((rm
& 7) == TCG_REG_RSP
) {
291 tcg_out8(s
, 0x84 | ((r
& 7) << 3));
294 tcg_out8(s
, 0x80 | ((r
& 7) << 3) | (rm
& 7));
296 tcg_out32(s
, offset
);
300 #if defined(CONFIG_SOFTMMU)
301 /* XXX: incomplete. index must be different from ESP */
302 static void tcg_out_modrm_offset2(TCGContext
*s
, int opc
, int r
, int rm
,
303 int index
, int shift
,
304 tcg_target_long offset
)
309 if (offset
== 0 && (rm
& 7) != TCG_REG_RBP
) {
311 } else if (offset
== (int8_t)offset
) {
313 } else if (offset
== (int32_t)offset
) {
319 tcg_out_opc(s
, opc
, r
, rm
, 0);
320 if ((rm
& 7) == TCG_REG_RSP
) {
321 tcg_out8(s
, mod
| ((r
& 7) << 3) | 0x04);
322 tcg_out8(s
, 0x04 | (rm
& 7));
324 tcg_out8(s
, mod
| ((r
& 7) << 3) | (rm
& 7));
327 tcg_out_opc(s
, opc
, r
, rm
, index
);
328 tcg_out8(s
, mod
| ((r
& 7) << 3) | 0x04);
329 tcg_out8(s
, (shift
<< 6) | ((index
& 7) << 3) | (rm
& 7));
333 } else if (mod
== 0x80) {
334 tcg_out32(s
, offset
);
339 static inline void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
341 tcg_out_modrm(s
, 0x8b | P_REXW
, ret
, arg
);
344 static inline void tcg_out_movi(TCGContext
*s
, TCGType type
,
345 int ret
, tcg_target_long arg
)
348 tcg_out_modrm(s
, 0x01 | (ARITH_XOR
<< 3), ret
, ret
); /* xor r0,r0 */
349 } else if (arg
== (uint32_t)arg
|| type
== TCG_TYPE_I32
) {
350 tcg_out_opc(s
, 0xb8 + (ret
& 7), 0, ret
, 0);
352 } else if (arg
== (int32_t)arg
) {
353 tcg_out_modrm(s
, 0xc7 | P_REXW
, 0, ret
);
356 tcg_out_opc(s
, (0xb8 + (ret
& 7)) | P_REXW
, 0, ret
, 0);
358 tcg_out32(s
, arg
>> 32);
362 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, int ret
,
363 int arg1
, tcg_target_long arg2
)
365 if (type
== TCG_TYPE_I32
)
366 tcg_out_modrm_offset(s
, 0x8b, ret
, arg1
, arg2
); /* movl */
368 tcg_out_modrm_offset(s
, 0x8b | P_REXW
, ret
, arg1
, arg2
); /* movq */
371 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, int arg
,
372 int arg1
, tcg_target_long arg2
)
374 if (type
== TCG_TYPE_I32
)
375 tcg_out_modrm_offset(s
, 0x89, arg
, arg1
, arg2
); /* movl */
377 tcg_out_modrm_offset(s
, 0x89 | P_REXW
, arg
, arg1
, arg2
); /* movq */
380 static inline void tgen_arithi32(TCGContext
*s
, int c
, int r0
, int32_t val
)
382 if (val
== (int8_t)val
) {
383 tcg_out_modrm(s
, 0x83, c
, r0
);
386 tcg_out_modrm(s
, 0x81, c
, r0
);
391 static inline void tgen_arithi64(TCGContext
*s
, int c
, int r0
, int64_t val
)
393 if (val
== (int8_t)val
) {
394 tcg_out_modrm(s
, 0x83 | P_REXW
, c
, r0
);
396 } else if (val
== (int32_t)val
) {
397 tcg_out_modrm(s
, 0x81 | P_REXW
, c
, r0
);
399 } else if (c
== ARITH_AND
&& val
== (uint32_t)val
) {
400 tcg_out_modrm(s
, 0x81, c
, r0
);
407 void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
410 tgen_arithi64(s
, ARITH_ADD
, reg
, val
);
413 static void tcg_out_jxx(TCGContext
*s
, int opc
, int label_index
)
416 TCGLabel
*l
= &s
->labels
[label_index
];
419 val
= l
->u
.value
- (tcg_target_long
)s
->code_ptr
;
421 if ((int8_t)val1
== val1
) {
425 tcg_out8(s
, 0x70 + opc
);
430 tcg_out32(s
, val
- 5);
433 tcg_out8(s
, 0x80 + opc
);
434 tcg_out32(s
, val
- 6);
442 tcg_out8(s
, 0x80 + opc
);
444 tcg_out_reloc(s
, s
->code_ptr
, R_386_PC32
, label_index
, -4);
449 static void tcg_out_brcond(TCGContext
*s
, int cond
,
450 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
451 int label_index
, int rexw
)
456 tcg_out_modrm(s
, 0x85 | rexw
, arg1
, arg1
);
459 tgen_arithi64(s
, ARITH_CMP
, arg1
, arg2
);
461 tgen_arithi32(s
, ARITH_CMP
, arg1
, arg2
);
464 tcg_out_modrm(s
, 0x01 | (ARITH_CMP
<< 3) | rexw
, arg2
, arg1
);
466 tcg_out_jxx(s
, tcg_cond_to_jcc
[cond
], label_index
);
469 #if defined(CONFIG_SOFTMMU)
470 extern void __ldb_mmu(void);
471 extern void __ldw_mmu(void);
472 extern void __ldl_mmu(void);
473 extern void __ldq_mmu(void);
475 extern void __stb_mmu(void);
476 extern void __stw_mmu(void);
477 extern void __stl_mmu(void);
478 extern void __stq_mmu(void);
481 static void *qemu_ld_helpers
[4] = {
488 static void *qemu_st_helpers
[4] = {
496 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
,
499 int addr_reg
, data_reg
, r0
, r1
, mem_index
, s_bits
, bswap
, rexw
;
500 #if defined(CONFIG_SOFTMMU)
501 uint8_t *label1_ptr
, *label2_ptr
;
512 #if TARGET_LONG_BITS == 32
517 #if defined(CONFIG_SOFTMMU)
519 tcg_out_modrm(s
, 0x8b | rexw
, r1
, addr_reg
);
522 tcg_out_modrm(s
, 0x8b | rexw
, r0
, addr_reg
);
524 tcg_out_modrm(s
, 0xc1 | rexw
, 5, r1
); /* shr $x, r1 */
525 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
527 tcg_out_modrm(s
, 0x81 | rexw
, 4, r0
); /* andl $x, r0 */
528 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
530 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
531 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
533 /* lea offset(r1, env), r1 */
534 tcg_out_modrm_offset2(s
, 0x8d | P_REXW
, r1
, r1
, TCG_AREG0
, 0,
535 offsetof(CPUState
, tlb_table
[mem_index
][0].addr_read
));
538 tcg_out_modrm_offset(s
, 0x3b | rexw
, r0
, r1
, 0);
541 tcg_out_modrm(s
, 0x8b | rexw
, r0
, addr_reg
);
544 tcg_out8(s
, 0x70 + JCC_JE
);
545 label1_ptr
= s
->code_ptr
;
548 /* XXX: move that code at the end of the TB */
549 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_RSI
, mem_index
);
551 tcg_out32(s
, (tcg_target_long
)qemu_ld_helpers
[s_bits
] -
552 (tcg_target_long
)s
->code_ptr
- 4);
557 tcg_out_modrm(s
, 0xbe | P_EXT
| P_REXW
, data_reg
, TCG_REG_RAX
);
561 tcg_out_modrm(s
, 0xbf | P_EXT
| P_REXW
, data_reg
, TCG_REG_RAX
);
565 tcg_out_modrm(s
, 0x63 | P_REXW
, data_reg
, TCG_REG_RAX
);
572 tcg_out_modrm(s
, 0x8b, data_reg
, TCG_REG_RAX
);
575 tcg_out_mov(s
, data_reg
, TCG_REG_RAX
);
581 label2_ptr
= s
->code_ptr
;
585 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
588 tcg_out_modrm_offset(s
, 0x03 | P_REXW
, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
589 offsetof(CPUTLBEntry
, addr_read
));
594 #ifdef TARGET_WORDS_BIGENDIAN
602 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, data_reg
, r0
, 0);
606 tcg_out_modrm_offset(s
, 0xbe | P_EXT
| rexw
, data_reg
, r0
, 0);
610 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, data_reg
, r0
, 0);
612 /* rolw $8, data_reg */
614 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
621 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, data_reg
, r0
, 0);
622 /* rolw $8, data_reg */
624 tcg_out_modrm(s
, 0xc1, 0, data_reg
);
627 /* movswX data_reg, data_reg */
628 tcg_out_modrm(s
, 0xbf | P_EXT
| rexw
, data_reg
, data_reg
);
631 tcg_out_modrm_offset(s
, 0xbf | P_EXT
| rexw
, data_reg
, r0
, 0);
635 /* movl (r0), data_reg */
636 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, 0);
639 tcg_out_opc(s
, (0xc8 + (data_reg
& 7)) | P_EXT
, 0, data_reg
, 0);
644 /* movl (r0), data_reg */
645 tcg_out_modrm_offset(s
, 0x8b, data_reg
, r0
, 0);
647 tcg_out_opc(s
, (0xc8 + (data_reg
& 7)) | P_EXT
, 0, data_reg
, 0);
649 tcg_out_modrm(s
, 0x63 | P_REXW
, data_reg
, data_reg
);
652 tcg_out_modrm_offset(s
, 0x63 | P_REXW
, data_reg
, r0
, 0);
656 /* movq (r0), data_reg */
657 tcg_out_modrm_offset(s
, 0x8b | P_REXW
, data_reg
, r0
, 0);
660 tcg_out_opc(s
, (0xc8 + (data_reg
& 7)) | P_EXT
| P_REXW
, 0, data_reg
, 0);
667 #if defined(CONFIG_SOFTMMU)
669 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
673 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
,
676 int addr_reg
, data_reg
, r0
, r1
, mem_index
, s_bits
, bswap
, rexw
;
677 #if defined(CONFIG_SOFTMMU)
678 uint8_t *label1_ptr
, *label2_ptr
;
690 #if TARGET_LONG_BITS == 32
695 #if defined(CONFIG_SOFTMMU)
697 tcg_out_modrm(s
, 0x8b | rexw
, r1
, addr_reg
);
700 tcg_out_modrm(s
, 0x8b | rexw
, r0
, addr_reg
);
702 tcg_out_modrm(s
, 0xc1 | rexw
, 5, r1
); /* shr $x, r1 */
703 tcg_out8(s
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
);
705 tcg_out_modrm(s
, 0x81 | rexw
, 4, r0
); /* andl $x, r0 */
706 tcg_out32(s
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1));
708 tcg_out_modrm(s
, 0x81, 4, r1
); /* andl $x, r1 */
709 tcg_out32(s
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
711 /* lea offset(r1, env), r1 */
712 tcg_out_modrm_offset2(s
, 0x8d | P_REXW
, r1
, r1
, TCG_AREG0
, 0,
713 offsetof(CPUState
, tlb_table
[mem_index
][0].addr_write
));
716 tcg_out_modrm_offset(s
, 0x3b | rexw
, r0
, r1
, 0);
719 tcg_out_modrm(s
, 0x8b | rexw
, r0
, addr_reg
);
722 tcg_out8(s
, 0x70 + JCC_JE
);
723 label1_ptr
= s
->code_ptr
;
726 /* XXX: move that code at the end of the TB */
730 tcg_out_modrm(s
, 0xb6 | P_EXT
| P_REXB
, TCG_REG_RSI
, data_reg
);
734 tcg_out_modrm(s
, 0xb7 | P_EXT
, TCG_REG_RSI
, data_reg
);
738 tcg_out_modrm(s
, 0x8b, TCG_REG_RSI
, data_reg
);
742 tcg_out_mov(s
, TCG_REG_RSI
, data_reg
);
745 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_RDX
, mem_index
);
747 tcg_out32(s
, (tcg_target_long
)qemu_st_helpers
[s_bits
] -
748 (tcg_target_long
)s
->code_ptr
- 4);
752 label2_ptr
= s
->code_ptr
;
756 *label1_ptr
= s
->code_ptr
- label1_ptr
- 1;
759 tcg_out_modrm_offset(s
, 0x03 | P_REXW
, r0
, r1
, offsetof(CPUTLBEntry
, addend
) -
760 offsetof(CPUTLBEntry
, addr_write
));
765 #ifdef TARGET_WORDS_BIGENDIAN
773 tcg_out_modrm_offset(s
, 0x88 | P_REXB
, data_reg
, r0
, 0);
777 tcg_out_modrm(s
, 0x8b, r1
, data_reg
); /* movl */
778 tcg_out8(s
, 0x66); /* rolw $8, %ecx */
779 tcg_out_modrm(s
, 0xc1, 0, r1
);
785 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, 0);
789 tcg_out_modrm(s
, 0x8b, r1
, data_reg
); /* movl */
791 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
, 0, r1
, 0);
795 tcg_out_modrm_offset(s
, 0x89, data_reg
, r0
, 0);
799 tcg_out_mov(s
, r1
, data_reg
);
801 tcg_out_opc(s
, (0xc8 + r1
) | P_EXT
| P_REXW
, 0, r1
, 0);
805 tcg_out_modrm_offset(s
, 0x89 | P_REXW
, data_reg
, r0
, 0);
811 #if defined(CONFIG_SOFTMMU)
813 *label2_ptr
= s
->code_ptr
- label2_ptr
- 1;
817 static inline void tcg_out_op(TCGContext
*s
, int opc
, const TCGArg
*args
,
818 const int *const_args
)
823 case INDEX_op_exit_tb
:
824 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_RAX
, args
[0]);
825 tcg_out8(s
, 0xe9); /* jmp tb_ret_addr */
826 tcg_out32(s
, tb_ret_addr
- s
->code_ptr
- 4);
828 case INDEX_op_goto_tb
:
829 if (s
->tb_jmp_offset
) {
830 /* direct jump method */
831 tcg_out8(s
, 0xe9); /* jmp im */
832 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
835 /* indirect jump method */
837 tcg_out_modrm_offset(s
, 0xff, 4, -1,
838 (tcg_target_long
)(s
->tb_next
+
841 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
846 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
848 tcg_out_modrm(s
, 0xff, 2, args
[0]);
854 tcg_out32(s
, args
[0] - (tcg_target_long
)s
->code_ptr
- 4);
856 tcg_out_modrm(s
, 0xff, 4, args
[0]);
860 tcg_out_jxx(s
, JCC_JMP
, args
[0]);
862 case INDEX_op_movi_i32
:
863 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], (uint32_t)args
[1]);
865 case INDEX_op_movi_i64
:
866 tcg_out_movi(s
, TCG_TYPE_I64
, args
[0], args
[1]);
868 case INDEX_op_ld8u_i32
:
869 case INDEX_op_ld8u_i64
:
871 tcg_out_modrm_offset(s
, 0xb6 | P_EXT
, args
[0], args
[1], args
[2]);
873 case INDEX_op_ld8s_i32
:
875 tcg_out_modrm_offset(s
, 0xbe | P_EXT
, args
[0], args
[1], args
[2]);
877 case INDEX_op_ld8s_i64
:
879 tcg_out_modrm_offset(s
, 0xbe | P_EXT
| P_REXW
, args
[0], args
[1], args
[2]);
881 case INDEX_op_ld16u_i32
:
882 case INDEX_op_ld16u_i64
:
884 tcg_out_modrm_offset(s
, 0xb7 | P_EXT
, args
[0], args
[1], args
[2]);
886 case INDEX_op_ld16s_i32
:
888 tcg_out_modrm_offset(s
, 0xbf | P_EXT
, args
[0], args
[1], args
[2]);
890 case INDEX_op_ld16s_i64
:
892 tcg_out_modrm_offset(s
, 0xbf | P_EXT
| P_REXW
, args
[0], args
[1], args
[2]);
894 case INDEX_op_ld_i32
:
895 case INDEX_op_ld32u_i64
:
897 tcg_out_modrm_offset(s
, 0x8b, args
[0], args
[1], args
[2]);
899 case INDEX_op_ld32s_i64
:
901 tcg_out_modrm_offset(s
, 0x63 | P_REXW
, args
[0], args
[1], args
[2]);
903 case INDEX_op_ld_i64
:
905 tcg_out_modrm_offset(s
, 0x8b | P_REXW
, args
[0], args
[1], args
[2]);
908 case INDEX_op_st8_i32
:
909 case INDEX_op_st8_i64
:
911 tcg_out_modrm_offset(s
, 0x88 | P_REXB
, args
[0], args
[1], args
[2]);
913 case INDEX_op_st16_i32
:
914 case INDEX_op_st16_i64
:
917 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
919 case INDEX_op_st_i32
:
920 case INDEX_op_st32_i64
:
922 tcg_out_modrm_offset(s
, 0x89, args
[0], args
[1], args
[2]);
924 case INDEX_op_st_i64
:
926 tcg_out_modrm_offset(s
, 0x89 | P_REXW
, args
[0], args
[1], args
[2]);
929 case INDEX_op_sub_i32
:
932 case INDEX_op_and_i32
:
935 case INDEX_op_or_i32
:
938 case INDEX_op_xor_i32
:
941 case INDEX_op_add_i32
:
945 tgen_arithi32(s
, c
, args
[0], args
[2]);
947 tcg_out_modrm(s
, 0x01 | (c
<< 3), args
[2], args
[0]);
951 case INDEX_op_sub_i64
:
954 case INDEX_op_and_i64
:
957 case INDEX_op_or_i64
:
960 case INDEX_op_xor_i64
:
963 case INDEX_op_add_i64
:
967 tgen_arithi64(s
, c
, args
[0], args
[2]);
969 tcg_out_modrm(s
, 0x01 | (c
<< 3) | P_REXW
, args
[2], args
[0]);
973 case INDEX_op_mul_i32
:
977 if (val
== (int8_t)val
) {
978 tcg_out_modrm(s
, 0x6b, args
[0], args
[0]);
981 tcg_out_modrm(s
, 0x69, args
[0], args
[0]);
985 tcg_out_modrm(s
, 0xaf | P_EXT
, args
[0], args
[2]);
988 case INDEX_op_mul_i64
:
992 if (val
== (int8_t)val
) {
993 tcg_out_modrm(s
, 0x6b | P_REXW
, args
[0], args
[0]);
996 tcg_out_modrm(s
, 0x69 | P_REXW
, args
[0], args
[0]);
1000 tcg_out_modrm(s
, 0xaf | P_EXT
| P_REXW
, args
[0], args
[2]);
1003 case INDEX_op_div2_i32
:
1004 tcg_out_modrm(s
, 0xf7, 7, args
[4]);
1006 case INDEX_op_divu2_i32
:
1007 tcg_out_modrm(s
, 0xf7, 6, args
[4]);
1009 case INDEX_op_div2_i64
:
1010 tcg_out_modrm(s
, 0xf7 | P_REXW
, 7, args
[4]);
1012 case INDEX_op_divu2_i64
:
1013 tcg_out_modrm(s
, 0xf7 | P_REXW
, 6, args
[4]);
1016 case INDEX_op_shl_i32
:
1019 if (const_args
[2]) {
1021 tcg_out_modrm(s
, 0xd1, c
, args
[0]);
1023 tcg_out_modrm(s
, 0xc1, c
, args
[0]);
1024 tcg_out8(s
, args
[2]);
1027 tcg_out_modrm(s
, 0xd3, c
, args
[0]);
1030 case INDEX_op_shr_i32
:
1033 case INDEX_op_sar_i32
:
1037 case INDEX_op_shl_i64
:
1040 if (const_args
[2]) {
1042 tcg_out_modrm(s
, 0xd1 | P_REXW
, c
, args
[0]);
1044 tcg_out_modrm(s
, 0xc1 | P_REXW
, c
, args
[0]);
1045 tcg_out8(s
, args
[2]);
1048 tcg_out_modrm(s
, 0xd3 | P_REXW
, c
, args
[0]);
1051 case INDEX_op_shr_i64
:
1054 case INDEX_op_sar_i64
:
1058 case INDEX_op_brcond_i32
:
1059 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1],
1062 case INDEX_op_brcond_i64
:
1063 tcg_out_brcond(s
, args
[2], args
[0], args
[1], const_args
[1],
1067 case INDEX_op_bswap_i32
:
1068 tcg_out_opc(s
, (0xc8 + (args
[0] & 7)) | P_EXT
, 0, args
[0], 0);
1070 case INDEX_op_bswap_i64
:
1071 tcg_out_opc(s
, (0xc8 + (args
[0] & 7)) | P_EXT
| P_REXW
, 0, args
[0], 0);
1074 case INDEX_op_neg_i32
:
1075 tcg_out_modrm(s
, 0xf7, 3, args
[0]);
1077 case INDEX_op_neg_i64
:
1078 tcg_out_modrm(s
, 0xf7 | P_REXW
, 3, args
[0]);
1081 case INDEX_op_qemu_ld8u
:
1082 tcg_out_qemu_ld(s
, args
, 0);
1084 case INDEX_op_qemu_ld8s
:
1085 tcg_out_qemu_ld(s
, args
, 0 | 4);
1087 case INDEX_op_qemu_ld16u
:
1088 tcg_out_qemu_ld(s
, args
, 1);
1090 case INDEX_op_qemu_ld16s
:
1091 tcg_out_qemu_ld(s
, args
, 1 | 4);
1093 case INDEX_op_qemu_ld32u
:
1094 tcg_out_qemu_ld(s
, args
, 2);
1096 case INDEX_op_qemu_ld32s
:
1097 tcg_out_qemu_ld(s
, args
, 2 | 4);
1099 case INDEX_op_qemu_ld64
:
1100 tcg_out_qemu_ld(s
, args
, 3);
1103 case INDEX_op_qemu_st8
:
1104 tcg_out_qemu_st(s
, args
, 0);
1106 case INDEX_op_qemu_st16
:
1107 tcg_out_qemu_st(s
, args
, 1);
1109 case INDEX_op_qemu_st32
:
1110 tcg_out_qemu_st(s
, args
, 2);
1112 case INDEX_op_qemu_st64
:
1113 tcg_out_qemu_st(s
, args
, 3);
1121 static int tcg_target_callee_save_regs
[] = {
1126 /* TCG_REG_R14, */ /* currently used for the global env, so no
1131 static inline void tcg_out_push(TCGContext
*s
, int reg
)
1133 tcg_out_opc(s
, (0x50 + (reg
& 7)), 0, reg
, 0);
1136 static inline void tcg_out_pop(TCGContext
*s
, int reg
)
1138 tcg_out_opc(s
, (0x58 + (reg
& 7)), 0, reg
, 0);
1141 /* Generate global QEMU prologue and epilogue code */
1142 void tcg_target_qemu_prologue(TCGContext
*s
)
1144 int i
, frame_size
, push_size
, stack_addend
;
1147 /* save all callee saved registers */
1148 for(i
= 0; i
< ARRAY_SIZE(tcg_target_callee_save_regs
); i
++) {
1149 tcg_out_push(s
, tcg_target_callee_save_regs
[i
]);
1152 /* reserve some stack space */
1153 push_size
= 8 + ARRAY_SIZE(tcg_target_callee_save_regs
) * 8;
1154 frame_size
= push_size
+ TCG_STATIC_CALL_ARGS_SIZE
;
1155 frame_size
= (frame_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1156 ~(TCG_TARGET_STACK_ALIGN
- 1);
1157 stack_addend
= frame_size
- push_size
;
1158 tcg_out_addi(s
, TCG_REG_RSP
, -stack_addend
);
1160 tcg_out_modrm(s
, 0xff, 4, TCG_REG_RDI
); /* jmp *%rdi */
1163 tb_ret_addr
= s
->code_ptr
;
1164 tcg_out_addi(s
, TCG_REG_RSP
, stack_addend
);
1165 for(i
= ARRAY_SIZE(tcg_target_callee_save_regs
) - 1; i
>= 0; i
--) {
1166 tcg_out_pop(s
, tcg_target_callee_save_regs
[i
]);
1168 tcg_out8(s
, 0xc3); /* ret */
1171 static const TCGTargetOpDef x86_64_op_defs
[] = {
1172 { INDEX_op_exit_tb
, { } },
1173 { INDEX_op_goto_tb
, { } },
1174 { INDEX_op_call
, { "ri" } }, /* XXX: might need a specific constant constraint */
1175 { INDEX_op_jmp
, { "ri" } }, /* XXX: might need a specific constant constraint */
1176 { INDEX_op_br
, { } },
1178 { INDEX_op_mov_i32
, { "r", "r" } },
1179 { INDEX_op_movi_i32
, { "r" } },
1180 { INDEX_op_ld8u_i32
, { "r", "r" } },
1181 { INDEX_op_ld8s_i32
, { "r", "r" } },
1182 { INDEX_op_ld16u_i32
, { "r", "r" } },
1183 { INDEX_op_ld16s_i32
, { "r", "r" } },
1184 { INDEX_op_ld_i32
, { "r", "r" } },
1185 { INDEX_op_st8_i32
, { "r", "r" } },
1186 { INDEX_op_st16_i32
, { "r", "r" } },
1187 { INDEX_op_st_i32
, { "r", "r" } },
1189 { INDEX_op_add_i32
, { "r", "0", "ri" } },
1190 { INDEX_op_mul_i32
, { "r", "0", "ri" } },
1191 { INDEX_op_div2_i32
, { "a", "d", "0", "1", "r" } },
1192 { INDEX_op_divu2_i32
, { "a", "d", "0", "1", "r" } },
1193 { INDEX_op_sub_i32
, { "r", "0", "ri" } },
1194 { INDEX_op_and_i32
, { "r", "0", "ri" } },
1195 { INDEX_op_or_i32
, { "r", "0", "ri" } },
1196 { INDEX_op_xor_i32
, { "r", "0", "ri" } },
1198 { INDEX_op_shl_i32
, { "r", "0", "ci" } },
1199 { INDEX_op_shr_i32
, { "r", "0", "ci" } },
1200 { INDEX_op_sar_i32
, { "r", "0", "ci" } },
1202 { INDEX_op_brcond_i32
, { "r", "ri" } },
1204 { INDEX_op_mov_i64
, { "r", "r" } },
1205 { INDEX_op_movi_i64
, { "r" } },
1206 { INDEX_op_ld8u_i64
, { "r", "r" } },
1207 { INDEX_op_ld8s_i64
, { "r", "r" } },
1208 { INDEX_op_ld16u_i64
, { "r", "r" } },
1209 { INDEX_op_ld16s_i64
, { "r", "r" } },
1210 { INDEX_op_ld32u_i64
, { "r", "r" } },
1211 { INDEX_op_ld32s_i64
, { "r", "r" } },
1212 { INDEX_op_ld_i64
, { "r", "r" } },
1213 { INDEX_op_st8_i64
, { "r", "r" } },
1214 { INDEX_op_st16_i64
, { "r", "r" } },
1215 { INDEX_op_st32_i64
, { "r", "r" } },
1216 { INDEX_op_st_i64
, { "r", "r" } },
1218 { INDEX_op_add_i64
, { "r", "0", "re" } },
1219 { INDEX_op_mul_i64
, { "r", "0", "re" } },
1220 { INDEX_op_div2_i64
, { "a", "d", "0", "1", "r" } },
1221 { INDEX_op_divu2_i64
, { "a", "d", "0", "1", "r" } },
1222 { INDEX_op_sub_i64
, { "r", "0", "re" } },
1223 { INDEX_op_and_i64
, { "r", "0", "reZ" } },
1224 { INDEX_op_or_i64
, { "r", "0", "re" } },
1225 { INDEX_op_xor_i64
, { "r", "0", "re" } },
1227 { INDEX_op_shl_i64
, { "r", "0", "ci" } },
1228 { INDEX_op_shr_i64
, { "r", "0", "ci" } },
1229 { INDEX_op_sar_i64
, { "r", "0", "ci" } },
1231 { INDEX_op_brcond_i64
, { "r", "re" } },
1233 { INDEX_op_bswap_i32
, { "r", "0" } },
1234 { INDEX_op_bswap_i64
, { "r", "0" } },
1236 { INDEX_op_neg_i32
, { "r", "0" } },
1237 { INDEX_op_neg_i64
, { "r", "0" } },
1239 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1240 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1241 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1242 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1243 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1244 { INDEX_op_qemu_ld32s
, { "r", "L" } },
1245 { INDEX_op_qemu_ld64
, { "r", "L" } },
1247 { INDEX_op_qemu_st8
, { "L", "L" } },
1248 { INDEX_op_qemu_st16
, { "L", "L" } },
1249 { INDEX_op_qemu_st32
, { "L", "L" } },
1250 { INDEX_op_qemu_st64
, { "L", "L", "L" } },
1255 void tcg_target_init(TCGContext
*s
)
1258 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
))
1261 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffff);
1262 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I64
], 0, 0xffff);
1263 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1264 (1 << TCG_REG_RDI
) |
1265 (1 << TCG_REG_RSI
) |
1266 (1 << TCG_REG_RDX
) |
1267 (1 << TCG_REG_RCX
) |
1270 (1 << TCG_REG_RAX
) |
1271 (1 << TCG_REG_R10
) |
1272 (1 << TCG_REG_R11
));
1274 tcg_regset_clear(s
->reserved_regs
);
1275 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_RSP
);
1277 tcg_add_target_add_op_defs(x86_64_op_defs
);