2 * i386 specific functions for TCC assembler
4 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * Copyright (c) 2009 Frédéric Feret (x86_64 support)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* #define NB_ASM_REGS 8 */
25 #define MAX_OPERANDS 3
26 #define NB_SAVED_REGS 3
28 #define TOK_ASM_first TOK_ASM_clc
29 #define TOK_ASM_last TOK_ASM_emms
30 #define TOK_ASM_alllast TOK_ASM_pxor
32 #define OPC_JMP 0x01 /* jmp operand */
33 #define OPC_B 0x02 /* only used with OPC_WL */
34 #define OPC_WL 0x04 /* accepts w, l or no suffix */
35 #define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */
36 #define OPC_REG 0x08 /* register is added to opcode */
37 #define OPC_MODRM 0x10 /* modrm encoding */
38 #define OPC_FWAIT 0x20 /* add fwait opcode */
39 #define OPC_TEST 0x40 /* test opcodes */
40 #define OPC_SHIFT 0x80 /* shift opcodes */
41 #define OPC_D16 0x0100 /* generate data16 prefix */
42 #define OPC_ARITH 0x0200 /* arithmetic opcodes */
43 #define OPC_SHORTJMP 0x0400 /* short jmp operand */
44 #define OPC_FARITH 0x0800 /* FPU arithmetic opcodes */
45 #ifdef TCC_TARGET_X86_64
46 # define OPC_WLQ 0x1000 /* accepts w, l, q or no suffix */
47 # define OPC_BWLQ (OPC_B | OPC_WLQ) /* accepts b, w, l, q or no suffix */
48 # define OPC_WLX OPC_WLQ
50 # define OPC_WLX OPC_WL
53 #define OPC_GROUP_SHIFT 13
55 /* in order to compress the operand type, we use specific operands and
58 OPT_REG8
=0, /* warning: value is hardcoded from TOK_ASM_xxx */
59 OPT_REG16
, /* warning: value is hardcoded from TOK_ASM_xxx */
60 OPT_REG32
, /* warning: value is hardcoded from TOK_ASM_xxx */
61 #ifdef TCC_TARGET_X86_64
62 OPT_REG64
, /* warning: value is hardcoded from TOK_ASM_xxx */
64 OPT_MMX
, /* warning: value is hardcoded from TOK_ASM_xxx */
65 OPT_SSE
, /* warning: value is hardcoded from TOK_ASM_xxx */
66 OPT_CR
, /* warning: value is hardcoded from TOK_ASM_xxx */
67 OPT_TR
, /* warning: value is hardcoded from TOK_ASM_xxx */
68 OPT_DB
, /* warning: value is hardcoded from TOK_ASM_xxx */
75 #ifdef TCC_TARGET_X86_64
78 OPT_EAX
, /* %al, %ax, %eax or %rax register */
79 OPT_ST0
, /* %st(0) register */
80 OPT_CL
, /* %cl register */
81 OPT_DX
, /* %dx register */
82 OPT_ADDR
, /* OP_EA with only offset */
83 OPT_INDIR
, /* *(expr) */
86 OPT_IM
, /* IM8 | IM16 | IM32 */
87 OPT_REG
, /* REG8 | REG16 | REG32 | REG64 */
88 OPT_REGW
, /* REG16 | REG32 | REG64 */
89 OPT_IMW
, /* IM16 | IM32 */
90 /* can be ored with any OPT_xxx */
94 #define OP_REG8 (1 << OPT_REG8)
95 #define OP_REG16 (1 << OPT_REG16)
96 #define OP_REG32 (1 << OPT_REG32)
97 #define OP_MMX (1 << OPT_MMX)
98 #define OP_SSE (1 << OPT_SSE)
99 #define OP_CR (1 << OPT_CR)
100 #define OP_TR (1 << OPT_TR)
101 #define OP_DB (1 << OPT_DB)
102 #define OP_SEG (1 << OPT_SEG)
103 #define OP_ST (1 << OPT_ST)
104 #define OP_IM8 (1 << OPT_IM8)
105 #define OP_IM8S (1 << OPT_IM8S)
106 #define OP_IM16 (1 << OPT_IM16)
107 #define OP_IM32 (1 << OPT_IM32)
108 #define OP_EAX (1 << OPT_EAX)
109 #define OP_ST0 (1 << OPT_ST0)
110 #define OP_CL (1 << OPT_CL)
111 #define OP_DX (1 << OPT_DX)
112 #define OP_ADDR (1 << OPT_ADDR)
113 #define OP_INDIR (1 << OPT_INDIR)
114 #ifdef TCC_TARGET_X86_64
115 # define OP_REG64 (1 << OPT_REG64)
116 # define OP_IM64 (1 << OPT_IM64)
117 # define OP_EA32 (OP_EA << 1)
124 #define OP_EA 0x40000000
125 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
127 #ifdef TCC_TARGET_X86_64
128 # define TREG_XAX TREG_RAX
129 # define TREG_XCX TREG_RCX
130 # define TREG_XDX TREG_RDX
132 # define TREG_XAX TREG_EAX
133 # define TREG_XCX TREG_ECX
134 # define TREG_XDX TREG_EDX
137 typedef struct ASMInstr
{
142 uint8_t op_type
[MAX_OPERANDS
]; /* see OP_xxx */
145 typedef struct Operand
{
147 int8_t reg
; /* register, -1 if none */
148 int8_t reg2
; /* second register, -1 if none */
153 static const uint8_t reg_to_size
[9] = {
158 #ifdef TCC_TARGET_X86_64
162 0, 0, 1, 0, 2, 0, 0, 0, 3
165 #define NB_TEST_OPCODES 30
167 static const uint8_t test_bits
[NB_TEST_OPCODES
] = {
200 static const uint8_t segment_prefixes
[] = {
209 static const ASMInstr asm_instrs
[] = {
211 #define DEF_ASM_OP0(name, opcode)
212 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
213 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
214 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
215 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 3, { op0, op1, op2 }},
216 #ifdef TCC_TARGET_X86_64
217 # include "x86_64-asm.h"
219 # include "i386-asm.h"
225 static const uint16_t op0_codes
[] = {
227 #define DEF_ASM_OP0(x, opcode) opcode,
228 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
229 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
230 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
231 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
232 #ifdef TCC_TARGET_X86_64
233 # include "x86_64-asm.h"
235 # include "i386-asm.h"
239 static inline int get_reg_shift(TCCState
*s1
)
242 v
= asm_int_expr(s1
);
257 expect("1, 2, 4 or 8 constant");
264 static int asm_parse_reg(int *type
)
271 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
272 reg
= tok
- TOK_ASM_eax
;
273 #ifdef TCC_TARGET_X86_64
275 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
276 reg
= tok
- TOK_ASM_rax
;
286 static void parse_operand(TCCState
*s1
, Operand
*op
)
300 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
301 reg
= tok
- TOK_ASM_al
;
302 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
304 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
306 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
308 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
310 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
312 op
->reg
= tok
- TOK_ASM_dr0
;
313 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
315 op
->reg
= tok
- TOK_ASM_es
;
316 } else if (tok
== TOK_ASM_st
) {
322 if (tok
!= TOK_PPNUM
)
326 if ((unsigned)reg
>= 8 || p
[1] != '\0')
337 tcc_error("unknown register");
341 } else if (tok
== '$') {
349 if (op
->e
.v
== (uint8_t)op
->e
.v
)
351 if (op
->e
.v
== (int8_t)op
->e
.v
)
353 if (op
->e
.v
== (uint16_t)op
->e
.v
)
355 #ifdef TCC_TARGET_X86_64
356 if (op
->e
.v
!= (uint32_t)op
->e
.v
)
361 /* address(reg,reg2,shift) with all variants */
377 /* bracketed offset expression */
390 op
->reg
= asm_parse_reg(&type
);
395 op
->reg2
= asm_parse_reg(&type
);
399 op
->shift
= get_reg_shift(s1
);
406 if (op
->reg
== -1 && op
->reg2
== -1)
412 /* XXX: unify with C code output ? */
413 ST_FUNC
void gen_expr32(ExprValue
*pe
)
415 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
418 #ifdef TCC_TARGET_X86_64
419 static void gen_expr64(ExprValue
*pe
)
421 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
425 /* XXX: unify with C code output ? */
426 static void gen_disp32(ExprValue
*pe
)
429 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
430 /* same section: we can output an absolute value. Note
431 that the TCC compiler behaves differently here because
432 it always outputs a relocation to ease (future) code
433 elimination in the linker */
434 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
436 if (sym
&& sym
->type
.t
== VT_VOID
) {
437 sym
->type
.t
= VT_FUNC
;
438 sym
->type
.ref
= NULL
;
440 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
444 /* generate the modrm operand */
445 static inline void asm_modrm(int reg
, Operand
*op
)
447 int mod
, reg1
, reg2
, sib_reg1
;
449 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
450 g(0xc0 + (reg
<< 3) + op
->reg
);
451 } else if (op
->reg
== -1 && op
->reg2
== -1) {
452 /* displacement only */
453 #ifdef TCC_TARGET_X86_64
454 g(0x04 + (reg
<< 3));
457 g(0x05 + (reg
<< 3));
462 /* fist compute displacement encoding */
463 if (sib_reg1
== -1) {
466 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
468 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
473 /* compute if sib byte needed */
477 g(mod
+ (reg
<< 3) + reg1
);
482 reg2
= 4; /* indicate no index */
483 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
488 } else if (mod
== 0x80 || op
->reg
== -1) {
494 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
497 int i
, modrm_index
, reg
, v
, op1
, seg_prefix
;
499 Operand ops
[MAX_OPERANDS
], *pop
;
500 int op_type
[3]; /* decoded op type */
501 int alltypes
; /* OR of all operand types */
504 /* force synthetic ';' after prefix instruction, so we can handle */
505 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
506 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
515 if (tok
== ';' || tok
== TOK_LINEFEED
)
517 if (nb_ops
>= MAX_OPERANDS
) {
518 tcc_error("incorrect number of operands");
520 parse_operand(s1
, pop
);
522 if (pop
->type
!= OP_SEG
|| seg_prefix
)
523 tcc_error("incorrect prefix");
524 seg_prefix
= segment_prefixes
[pop
->reg
];
526 parse_operand(s1
, pop
);
527 if (!(pop
->type
& OP_EA
)) {
528 tcc_error("segment prefix must be followed by memory reference");
538 s
= 0; /* avoid warning */
540 /* optimize matching by using a lookup table (no hashing is needed
542 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
544 if (pa
->instr_type
& OPC_FARITH
) {
545 v
= opcode
- pa
->sym
;
546 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
548 } else if (pa
->instr_type
& OPC_ARITH
) {
549 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
551 s
= (opcode
- pa
->sym
) % NBWLX
;
552 } else if (pa
->instr_type
& OPC_SHIFT
) {
553 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
555 s
= (opcode
- pa
->sym
) % NBWLX
;
556 } else if (pa
->instr_type
& OPC_TEST
) {
557 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
559 /* cmovxx is a test opcode but accepts multiple sizes.
560 TCC doesn't accept the suffixed mnemonic, instead we
561 simply force size autodetection always. */
562 if (pa
->instr_type
& OPC_WLX
)
564 } else if (pa
->instr_type
& OPC_B
) {
565 #ifdef TCC_TARGET_X86_64
566 /* Some instructions don't have the full size but only
567 bwl form. insb e.g. */
568 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
569 && !(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
572 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
574 s
= opcode
- pa
->sym
;
575 } else if (pa
->instr_type
& OPC_WLX
) {
576 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
578 s
= opcode
- pa
->sym
+ 1;
580 if (pa
->sym
!= opcode
)
583 if (pa
->nb_ops
!= nb_ops
)
585 #ifdef TCC_TARGET_X86_64
586 /* Special case for moves. Selecting the IM64->REG64 form
587 should only be done if we really have an >32bit imm64, and that
588 is hardcoded. Ignore it here. */
589 if (pa
->opcode
== 0xb0 && ops
[0].type
!= OP_IM64
590 && ops
[1].type
== OP_REG64
)
593 /* now decode and check each operand */
595 for(i
= 0; i
< nb_ops
; i
++) {
597 op1
= pa
->op_type
[i
];
601 v
= OP_IM8
| OP_IM16
| OP_IM32
;
604 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
607 v
= OP_REG16
| OP_REG32
| OP_REG64
;
610 v
= OP_IM16
| OP_IM32
;
619 if ((ops
[i
].type
& v
) == 0)
621 alltypes
|= ops
[i
].type
;
623 /* all is matching ! */
628 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
630 b
= op0_codes
[opcode
- TOK_ASM_first
];
635 } else if (opcode
<= TOK_ASM_alllast
) {
636 tcc_error("bad operand with opcode '%s'",
637 get_tok_str(opcode
, NULL
));
639 tcc_error("unknown opcode '%s'",
640 get_tok_str(opcode
, NULL
));
643 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
645 #ifdef TCC_TARGET_X86_64
646 /* XXX the autosize should rather be zero, to not have to adjust this
648 if ((pa
->instr_type
& OPC_BWLQ
) == OPC_B
)
652 for(i
= 0; s
== autosize
&& i
< nb_ops
; i
++) {
653 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
654 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
657 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
658 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
)))
661 tcc_error("cannot infer opcode suffix");
665 #ifdef TCC_TARGET_X86_64
666 /* Generate addr32 prefix if needed */
667 for(i
= 0; i
< nb_ops
; i
++) {
668 if (ops
[i
].type
& OP_EA32
) {
674 /* generate data16 prefix if needed */
675 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
677 #ifdef TCC_TARGET_X86_64
678 if (s
== 3 || (alltypes
& OP_REG64
)) {
679 /* generate REX prefix */
681 for(i
= 0; i
< nb_ops
; i
++) {
682 if (op_type
[i
] == OP_REG64
) {
683 /* If only 64bit regs are accepted in one operand
684 this is a default64 instruction without need for
690 /* XXX find better encoding for the default64 instructions. */
691 if (((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
692 && opcode
!= TOK_ASM_pushw
&& opcode
!= TOK_ASM_pushl
693 && opcode
!= TOK_ASM_pushq
&& opcode
!= TOK_ASM_popw
694 && opcode
!= TOK_ASM_popl
&& opcode
!= TOK_ASM_popq
695 && opcode
!= TOK_ASM_call
&& opcode
!= TOK_ASM_jmp
))
701 /* now generates the operation */
702 if (pa
->instr_type
& OPC_FWAIT
)
708 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
709 /* kludge for imul $im, %reg */
712 op_type
[2] = op_type
[1];
713 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
714 v
--; /* int $3 case */
716 } else if ((v
== 0x06 || v
== 0x07)) {
717 if (ops
[0].reg
>= 4) {
718 /* push/pop %fs or %gs */
719 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
721 v
+= ops
[0].reg
<< 3;
724 } else if (v
<= 0x05) {
726 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
727 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
729 v
+= ((opcode
- pa
->sym
) / 6) << 3;
731 if (pa
->instr_type
& OPC_REG
) {
732 for(i
= 0; i
< nb_ops
; i
++) {
733 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
738 /* mov $im, %reg case */
739 if (pa
->opcode
== 0xb0 && s
>= 1)
742 if (pa
->instr_type
& OPC_B
)
744 if (pa
->instr_type
& OPC_TEST
)
745 v
+= test_bits
[opcode
- pa
->sym
];
746 if (pa
->instr_type
& OPC_SHORTJMP
) {
750 /* see if we can really generate the jump with a byte offset */
754 if (sym
->r
!= cur_text_section
->sh_num
)
756 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2 - (v
>= 0xff);
757 if (jmp_disp
== (int8_t)jmp_disp
) {
758 /* OK to generate jump */
760 ops
[0].e
.v
= jmp_disp
;
761 op_type
[0] = OP_IM8S
;
764 if (pa
->instr_type
& OPC_JMP
) {
765 /* long jump will be allowed. need to modify the
772 tcc_error("invalid displacement");
781 /* search which operand will used for modrm */
783 if (pa
->instr_type
& OPC_SHIFT
) {
784 reg
= (opcode
- pa
->sym
) / NBWLX
;
787 } else if (pa
->instr_type
& OPC_ARITH
) {
788 reg
= (opcode
- pa
->sym
) / NBWLX
;
789 } else if (pa
->instr_type
& OPC_FARITH
) {
790 reg
= (opcode
- pa
->sym
) / 6;
792 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
794 if (pa
->instr_type
& OPC_MODRM
) {
795 /* first look for an ea operand */
796 for(i
= 0;i
< nb_ops
; i
++) {
797 if (op_type
[i
] & OP_EA
)
800 /* then if not found, a register or indirection (shift instructions) */
801 for(i
= 0;i
< nb_ops
; i
++) {
802 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
806 tcc_error("bad op table");
810 /* if a register is used in another operand then it is
811 used instead of group */
812 for(i
= 0;i
< nb_ops
; i
++) {
814 if (i
!= modrm_index
&&
815 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
821 asm_modrm(reg
, &ops
[modrm_index
]);
825 #ifndef TCC_TARGET_X86_64
826 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
827 /* ljmp or lcall kludge */
828 gen_expr32(&ops
[1].e
);
830 tcc_error("cannot relocate");
831 gen_le16(ops
[0].e
.v
);
835 for(i
= 0;i
< nb_ops
; i
++) {
837 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
838 /* if multiple sizes are given it means we must look
840 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
845 else if (s
== 2 || (v
& OP_IM64
) == 0)
850 if (v
& (OP_IM8
| OP_IM8S
)) {
854 } else if (v
& OP_IM16
) {
857 tcc_error("cannot relocate");
859 gen_le16(ops
[i
].e
.v
);
861 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
862 gen_disp32(&ops
[i
].e
);
864 #ifdef TCC_TARGET_X86_64
866 gen_expr64(&ops
[i
].e
);
869 gen_expr32(&ops
[i
].e
);
876 /* return the constraint priority (we allocate first the lowest
877 numbered constraints) */
878 static inline int constraint_priority(const char *str
)
882 /* we take the lowest priority */
916 tcc_error("unknown constraint '%c'", c
);
925 static const char *skip_constraint_modifiers(const char *p
)
927 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
932 #define REG_OUT_MASK 0x01
933 #define REG_IN_MASK 0x02
935 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
937 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
938 int nb_operands
, int nb_outputs
,
939 const uint8_t *clobber_regs
,
943 int sorted_op
[MAX_ASM_OPERANDS
];
944 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
946 uint8_t regs_allocated
[NB_ASM_REGS
];
949 for(i
=0;i
<nb_operands
;i
++) {
951 op
->input_index
= -1;
957 /* compute constraint priority and evaluate references to output
958 constraints if input constraints */
959 for(i
=0;i
<nb_operands
;i
++) {
961 str
= op
->constraint
;
962 str
= skip_constraint_modifiers(str
);
963 if (isnum(*str
) || *str
== '[') {
964 /* this is a reference to another constraint */
965 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
966 if ((unsigned)k
>= i
|| i
< nb_outputs
)
967 tcc_error("invalid reference in constraint %d ('%s')",
970 if (operands
[k
].input_index
>= 0)
971 tcc_error("cannot reference twice the same operand");
972 operands
[k
].input_index
= i
;
975 op
->priority
= constraint_priority(str
);
979 /* sort operands according to their priority */
980 for(i
=0;i
<nb_operands
;i
++)
982 for(i
=0;i
<nb_operands
- 1;i
++) {
983 for(j
=i
+1;j
<nb_operands
;j
++) {
984 p1
= operands
[sorted_op
[i
]].priority
;
985 p2
= operands
[sorted_op
[j
]].priority
;
988 sorted_op
[i
] = sorted_op
[j
];
994 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
996 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
998 regs_allocated
[i
] = 0;
1000 /* esp cannot be used */
1001 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1002 /* ebp cannot be used yet */
1003 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1005 /* allocate registers and generate corresponding asm moves */
1006 for(i
=0;i
<nb_operands
;i
++) {
1009 str
= op
->constraint
;
1010 /* no need to allocate references */
1011 if (op
->ref_index
>= 0)
1013 /* select if register is used for output, input or both */
1014 if (op
->input_index
>= 0) {
1015 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1016 } else if (j
< nb_outputs
) {
1017 reg_mask
= REG_OUT_MASK
;
1019 reg_mask
= REG_IN_MASK
;
1030 if (j
>= nb_outputs
)
1031 tcc_error("'%c' modifier can only be applied to outputs", c
);
1032 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1035 /* allocate both eax and edx */
1036 if (is_reg_allocated(TREG_XAX
) ||
1037 is_reg_allocated(TREG_XDX
))
1041 regs_allocated
[TREG_XAX
] |= reg_mask
;
1042 regs_allocated
[TREG_XDX
] |= reg_mask
;
1062 if (is_reg_allocated(reg
))
1066 /* eax, ebx, ecx or edx */
1067 for(reg
= 0; reg
< 4; reg
++) {
1068 if (!is_reg_allocated(reg
))
1073 /* any general register */
1074 for(reg
= 0; reg
< 8; reg
++) {
1075 if (!is_reg_allocated(reg
))
1080 /* now we can reload in the register */
1083 regs_allocated
[reg
] |= reg_mask
;
1086 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1092 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1097 /* nothing special to do because the operand is already in
1098 memory, except if the pointer itself is stored in a
1099 memory variable (VT_LLOCAL case) */
1100 /* XXX: fix constant case */
1101 /* if it is a reference to a memory zone, it must lie
1102 in a register, so we reserve the register in the
1103 input registers and a load will be generated
1105 if (j
< nb_outputs
|| c
== 'm') {
1106 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1107 /* any general register */
1108 for(reg
= 0; reg
< 8; reg
++) {
1109 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1114 /* now we can reload in the register */
1115 regs_allocated
[reg
] |= REG_IN_MASK
;
1122 tcc_error("asm constraint %d ('%s') could not be satisfied",
1126 /* if a reference is present for that operand, we assign it too */
1127 if (op
->input_index
>= 0) {
1128 operands
[op
->input_index
].reg
= op
->reg
;
1129 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1133 /* compute out_reg. It is used to store outputs registers to memory
1134 locations references by pointers (VT_LLOCAL case) */
1136 for(i
=0;i
<nb_operands
;i
++) {
1139 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1141 for(reg
= 0; reg
< 8; reg
++) {
1142 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1145 tcc_error("could not find free output register for reloading");
1152 /* print sorted constraints */
1154 for(i
=0;i
<nb_operands
;i
++) {
1157 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1159 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1165 printf("out_reg=%d\n", *pout_reg
);
1169 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1170 SValue
*sv
, int modifier
)
1172 int r
, reg
, size
, val
;
1176 if ((r
& VT_VALMASK
) == VT_CONST
) {
1177 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1178 cstr_ccat(add_str
, '$');
1180 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
), -1);
1181 if ((uint32_t)sv
->c
.i
!= 0) {
1182 cstr_ccat(add_str
, '+');
1188 if (modifier
== 'n')
1190 snprintf(buf
, sizeof(buf
), "%d", (int)sv
->c
.i
);
1191 cstr_cat(add_str
, buf
, -1);
1192 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1193 #ifdef TCC_TARGET_X86_64
1194 snprintf(buf
, sizeof(buf
), "%d(%%rbp)", (int)sv
->c
.i
);
1196 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", (int)sv
->c
.i
);
1198 cstr_cat(add_str
, buf
, -1);
1199 } else if (r
& VT_LVAL
) {
1200 reg
= r
& VT_VALMASK
;
1201 if (reg
>= VT_CONST
)
1202 tcc_error("internal compiler error");
1203 snprintf(buf
, sizeof(buf
), "(%%%s)",
1204 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1205 cstr_cat(add_str
, buf
, -1);
1208 reg
= r
& VT_VALMASK
;
1209 if (reg
>= VT_CONST
)
1210 tcc_error("internal compiler error");
1212 /* choose register operand size */
1213 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1215 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1217 #ifdef TCC_TARGET_X86_64
1218 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1223 if (size
== 1 && reg
>= 4)
1226 if (modifier
== 'b') {
1228 tcc_error("cannot use byte register");
1230 } else if (modifier
== 'h') {
1232 tcc_error("cannot use byte register");
1234 } else if (modifier
== 'w') {
1236 #ifdef TCC_TARGET_X86_64
1237 } else if (modifier
== 'q') {
1244 reg
= TOK_ASM_ah
+ reg
;
1247 reg
= TOK_ASM_al
+ reg
;
1250 reg
= TOK_ASM_ax
+ reg
;
1253 reg
= TOK_ASM_eax
+ reg
;
1255 #ifdef TCC_TARGET_X86_64
1257 reg
= TOK_ASM_rax
+ reg
;
1261 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1262 cstr_cat(add_str
, buf
, -1);
1266 /* generate prolog and epilog code for asm statement */
1267 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1268 int nb_outputs
, int is_output
,
1269 uint8_t *clobber_regs
,
1272 uint8_t regs_allocated
[NB_ASM_REGS
];
1275 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1277 /* mark all used registers */
1278 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1279 for(i
= 0; i
< nb_operands
;i
++) {
1282 regs_allocated
[op
->reg
] = 1;
1285 /* generate reg save code */
1286 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1288 if (regs_allocated
[reg
]) {
1293 /* generate load code */
1294 for(i
= 0; i
< nb_operands
; i
++) {
1297 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1299 /* memory reference case (for both input and
1303 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1305 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1306 /* load value in register */
1307 load(op
->reg
, op
->vt
);
1312 load(TREG_XDX
, &sv
);
1318 /* generate save code */
1319 for(i
= 0 ; i
< nb_outputs
; i
++) {
1322 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1323 if (!op
->is_memory
) {
1326 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1329 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1330 store(op
->reg
, &sv
);
1333 store(op
->reg
, op
->vt
);
1338 store(TREG_XDX
, &sv
);
1343 /* generate reg restore code */
1344 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1346 if (regs_allocated
[reg
]) {
1353 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1358 if (!strcmp(str
, "memory") ||
1361 ts
= tok_alloc(str
, strlen(str
));
1363 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1365 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1367 #ifdef TCC_TARGET_X86_64
1368 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1372 tcc_error("invalid clobber register '%s'", str
);
1374 clobber_regs
[reg
] = 1;