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
31 #define OPC_JMP 0x01 /* jmp operand */
32 #define OPC_B 0x02 /* only used with OPC_WL */
33 #define OPC_WL 0x04 /* accepts w, l or no suffix */
34 #define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */
35 #define OPC_REG 0x08 /* register is added to opcode */
36 #define OPC_MODRM 0x10 /* modrm encoding */
37 #define OPC_FWAIT 0x20 /* add fwait opcode */
38 #define OPC_TEST 0x40 /* test opcodes */
39 #define OPC_SHIFT 0x80 /* shift opcodes */
40 #define OPC_D16 0x0100 /* generate data16 prefix */
41 #define OPC_ARITH 0x0200 /* arithmetic opcodes */
42 #define OPC_SHORTJMP 0x0400 /* short jmp operand */
43 #define OPC_FARITH 0x0800 /* FPU arithmetic opcodes */
44 #ifdef TCC_TARGET_X86_64
45 # define OPC_WLQ 0x1000 /* accepts w, l, q or no suffix */
46 # define OPC_BWLQ (OPC_B | OPC_WLQ) /* accepts b, w, l, q or no suffix */
47 # define OPC_WLX OPC_WLQ
49 # define OPC_WLX OPC_WL
52 #define OPC_GROUP_SHIFT 13
54 /* in order to compress the operand type, we use specific operands and
57 OPT_REG8
=0, /* warning: value is hardcoded from TOK_ASM_xxx */
58 OPT_REG16
, /* warning: value is hardcoded from TOK_ASM_xxx */
59 OPT_REG32
, /* warning: value is hardcoded from TOK_ASM_xxx */
60 #ifdef TCC_TARGET_X86_64
61 OPT_REG64
, /* warning: value is hardcoded from TOK_ASM_xxx */
63 OPT_MMX
, /* warning: value is hardcoded from TOK_ASM_xxx */
64 OPT_SSE
, /* warning: value is hardcoded from TOK_ASM_xxx */
65 OPT_CR
, /* warning: value is hardcoded from TOK_ASM_xxx */
66 OPT_TR
, /* warning: value is hardcoded from TOK_ASM_xxx */
67 OPT_DB
, /* warning: value is hardcoded from TOK_ASM_xxx */
74 #ifdef TCC_TARGET_X86_64
77 OPT_EAX
, /* %al, %ax, %eax or %rax register */
78 OPT_ST0
, /* %st(0) register */
79 OPT_CL
, /* %cl register */
80 OPT_DX
, /* %dx register */
81 OPT_ADDR
, /* OP_EA with only offset */
82 OPT_INDIR
, /* *(expr) */
85 OPT_IM
, /* IM8 | IM16 | IM32 | IM64 */
86 OPT_REG
, /* REG8 | REG16 | REG32 | REG64 */
87 OPT_REGW
, /* REG16 | REG32 | REG64 */
88 OPT_IMW
, /* IM16 | IM32 | IM64 */
89 #ifdef TCC_TARGET_X86_64
90 OPT_IMNO64
, /* IM16 | IM32 */
92 /* can be ored with any OPT_xxx */
96 #define OP_REG8 (1 << OPT_REG8)
97 #define OP_REG16 (1 << OPT_REG16)
98 #define OP_REG32 (1 << OPT_REG32)
99 #define OP_MMX (1 << OPT_MMX)
100 #define OP_SSE (1 << OPT_SSE)
101 #define OP_CR (1 << OPT_CR)
102 #define OP_TR (1 << OPT_TR)
103 #define OP_DB (1 << OPT_DB)
104 #define OP_SEG (1 << OPT_SEG)
105 #define OP_ST (1 << OPT_ST)
106 #define OP_IM8 (1 << OPT_IM8)
107 #define OP_IM8S (1 << OPT_IM8S)
108 #define OP_IM16 (1 << OPT_IM16)
109 #define OP_IM32 (1 << OPT_IM32)
110 #define OP_EAX (1 << OPT_EAX)
111 #define OP_ST0 (1 << OPT_ST0)
112 #define OP_CL (1 << OPT_CL)
113 #define OP_DX (1 << OPT_DX)
114 #define OP_ADDR (1 << OPT_ADDR)
115 #define OP_INDIR (1 << OPT_INDIR)
116 #ifdef TCC_TARGET_X86_64
117 # define OP_REG64 (1 << OPT_REG64)
118 # define OP_IM64 (1 << OPT_IM64)
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 OP_IM OP_IM64
129 # define TREG_XAX TREG_RAX
130 # define TREG_XCX TREG_RCX
131 # define TREG_XDX TREG_RDX
133 # define OP_IM OP_IM32
134 # define TREG_XAX TREG_EAX
135 # define TREG_XCX TREG_ECX
136 # define TREG_XDX TREG_EDX
139 typedef struct ASMInstr
{
144 uint8_t op_type
[MAX_OPERANDS
]; /* see OP_xxx */
147 typedef struct Operand
{
149 int8_t reg
; /* register, -1 if none */
150 int8_t reg2
; /* second register, -1 if none */
155 static const uint8_t reg_to_size
[9] = {
160 #ifdef TCC_TARGET_X86_64
164 0, 0, 1, 0, 2, 0, 0, 0, 3
167 #define NB_TEST_OPCODES 30
169 static const uint8_t test_bits
[NB_TEST_OPCODES
] = {
202 static const uint8_t segment_prefixes
[] = {
211 static const ASMInstr asm_instrs
[] = {
213 #define DEF_ASM_OP0(name, opcode)
214 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
215 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
216 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
217 #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 }},
218 #ifdef TCC_TARGET_X86_64
219 # include "x86_64-asm.h"
221 # include "i386-asm.h"
227 static const uint16_t op0_codes
[] = {
229 #define DEF_ASM_OP0(x, opcode) opcode,
230 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
231 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
232 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
233 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
234 #ifdef TCC_TARGET_X86_64
235 # include "x86_64-asm.h"
237 # include "i386-asm.h"
241 static inline int get_reg_shift(TCCState
*s1
)
245 if (s1
->seg_size
== 16)
246 error("invalid effective address");
248 v
= asm_int_expr(s1
);
263 expect("1, 2, 4 or 8 constant");
270 static int asm_parse_reg(void)
276 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
277 reg
= tok
- TOK_ASM_eax
;
278 #ifdef TCC_TARGET_X86_64
279 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
280 reg
= tok
- TOK_ASM_rax
;
283 } else if (tok
>= TOK_ASM_ax
&& tok
<= TOK_ASM_di
) {
284 reg
= tok
- TOK_ASM_ax
;
294 static void parse_operand(TCCState
*s1
, Operand
*op
)
308 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
309 reg
= tok
- TOK_ASM_al
;
310 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
312 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
314 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
316 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
318 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
320 op
->reg
= tok
- TOK_ASM_dr0
;
321 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
323 op
->reg
= tok
- TOK_ASM_es
;
324 } else if (tok
== TOK_ASM_st
) {
330 if (tok
!= TOK_PPNUM
)
334 if ((unsigned)reg
>= 8 || p
[1] != '\0')
345 error("unknown register");
349 } else if (tok
== '$') {
357 if (op
->e
.v
== (uint8_t)op
->e
.v
)
359 if (op
->e
.v
== (int8_t)op
->e
.v
)
361 if (op
->e
.v
== (uint16_t)op
->e
.v
)
363 #ifdef TCC_TARGET_X86_64
364 if (op
->e
.v
== (uint32_t)op
->e
.v
)
369 /* address(reg,reg2,shift) with all variants */
385 op
->reg
= asm_parse_reg();
390 op
->reg2
= asm_parse_reg();
394 op
->shift
= get_reg_shift(s1
);
399 if (op
->reg
== -1 && op
->reg2
== -1)
405 /* XXX: unify with C code output ? */
406 ST_FUNC
void gen_expr32(ExprValue
*pe
)
408 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
411 #ifdef TCC_TARGET_X86_64
412 static void gen_expr64(ExprValue
*pe
)
414 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
418 /* XXX: unify with C code output ? */
419 static void gen_disp32(ExprValue
*pe
)
422 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
423 /* same section: we can output an absolute value. Note
424 that the TCC compiler behaves differently here because
425 it always outputs a relocation to ease (future) code
426 elimination in the linker */
427 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
429 if (sym
&& sym
->type
.t
== VT_VOID
) {
430 sym
->type
.t
= VT_FUNC
;
431 sym
->type
.ref
= NULL
;
433 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
438 static void gen_expr16(ExprValue
*pe
)
441 greloc(cur_text_section
, pe
->sym
, ind
, R_386_16
);
444 static void gen_disp16(ExprValue
*pe
)
449 if (sym
->r
== cur_text_section
->sh_num
) {
450 /* same section: we can output an absolute value. Note
451 that the TCC compiler behaves differently here because
452 it always outputs a relocation to ease (future) code
453 elimination in the linker */
454 gen_le16(pe
->v
+ sym
->jnext
- ind
- 2);
456 greloc(cur_text_section
, sym
, ind
, R_386_PC16
);
460 /* put an empty PC32 relocation */
461 put_elf_reloc(symtab_section
, cur_text_section
,
468 /* generate the modrm operand */
469 static inline void asm_modrm(int reg
, Operand
*op
)
471 int mod
, reg1
, reg2
, sib_reg1
;
473 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
474 g(0xc0 + (reg
<< 3) + op
->reg
);
475 } else if (op
->reg
== -1 && op
->reg2
== -1) {
476 /* displacement only */
478 if (tcc_state
->seg_size
== 16) {
479 g(0x06 + (reg
<< 3));
481 } else if (tcc_state
->seg_size
== 32)
484 g(0x05 + (reg
<< 3));
489 /* fist compute displacement encoding */
490 if (sib_reg1
== -1) {
493 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
495 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
500 /* compute if sib byte needed */
505 if (tcc_state
->seg_size
== 32) {
507 g(mod
+ (reg
<< 3) + reg1
);
512 reg2
= 4; /* indicate no index */
513 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
516 } else if (tcc_state
->seg_size
== 16) {
517 /* edi = 7, esi = 6 --> di = 5, si = 4 */
518 if ((reg1
== 6) || (reg1
== 7)) {
520 /* ebx = 3 --> bx = 7 */
521 } else if (reg1
== 3) {
523 /* o32 = 5 --> o16 = 6 */
524 } else if (reg1
== 5) {
526 /* sib not valid in 16-bit mode */
527 } else if (reg1
== 4) {
529 /* bp + si + offset */
530 if ((sib_reg1
== 5) && (reg2
== 6)) {
532 /* bp + di + offset */
533 } else if ((sib_reg1
== 5) && (reg2
== 7)) {
535 /* bx + si + offset */
536 } else if ((sib_reg1
== 3) && (reg2
== 6)) {
538 /* bx + di + offset */
539 } else if ((sib_reg1
== 3) && (reg2
== 7)) {
542 error("invalid effective address");
547 error("invalid register");
549 g(mod
+ (reg
<< 3) + reg1
);
555 } else if (mod
== 0x80 || op
->reg
== -1) {
557 if (tcc_state
->seg_size
== 16)
559 else if (tcc_state
->seg_size
== 32)
566 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
569 int i
, modrm_index
, reg
, v
, op1
, is_short_jmp
, seg_prefix
;
571 Operand ops
[MAX_OPERANDS
], *pop
;
572 int op_type
[3]; /* decoded op type */
574 static int a32
= 0, o32
= 0, addr32
= 0, data32
= 0;
582 if (tok
== ';' || tok
== TOK_LINEFEED
)
584 if (nb_ops
>= MAX_OPERANDS
) {
585 error("incorrect number of operands");
587 parse_operand(s1
, pop
);
589 if (pop
->type
!= OP_SEG
|| seg_prefix
)
590 error("incorrect prefix");
591 seg_prefix
= segment_prefixes
[pop
->reg
];
593 parse_operand(s1
, pop
);
595 if (!(pop
->type
& OP_EA
)) {
596 error("segment prefix must be followed by memory reference");
608 s
= 0; /* avoid warning */
610 /* optimize matching by using a lookup table (no hashing is needed
612 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
614 if (pa
->instr_type
& OPC_FARITH
) {
615 v
= opcode
- pa
->sym
;
616 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
618 } else if (pa
->instr_type
& OPC_ARITH
) {
619 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
621 s
= (opcode
- pa
->sym
) % NBWLX
;
622 } else if (pa
->instr_type
& OPC_SHIFT
) {
623 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
625 s
= (opcode
- pa
->sym
) % NBWLX
;
626 } else if (pa
->instr_type
& OPC_TEST
) {
627 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
629 } else if (pa
->instr_type
& OPC_B
) {
630 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
632 s
= opcode
- pa
->sym
;
633 } else if (pa
->instr_type
& OPC_WLX
) {
634 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
636 s
= opcode
- pa
->sym
+ 1;
638 if (pa
->sym
!= opcode
)
641 if (pa
->nb_ops
!= nb_ops
)
643 /* now decode and check each operand */
644 for(i
= 0; i
< nb_ops
; i
++) {
646 op1
= pa
->op_type
[i
];
650 v
= OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
;
653 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
656 v
= OP_REG16
| OP_REG32
| OP_REG64
;
659 v
= OP_IM16
| OP_IM32
| OP_IM64
;
661 #ifdef TCC_TARGET_X86_64
663 v
= OP_IM16
| OP_IM32
;
673 if ((ops
[i
].type
& v
) == 0)
676 /* all is matching ! */
681 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
683 b
= op0_codes
[opcode
- TOK_ASM_first
];
685 if (opcode
== TOK_ASM_o32
) {
686 if (s1
->seg_size
== 32)
687 error("incorrect prefix");
690 } else if (opcode
== TOK_ASM_a32
) {
691 if (s1
->seg_size
== 32)
692 error("incorrect prefix");
702 error("unknown opcode '%s'",
703 get_tok_str(opcode
, NULL
));
706 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
708 for(i
= 0; s
== NBWLX
-1 && i
< nb_ops
; i
++) {
709 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
710 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
713 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
714 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
| OP_IM64
)))
717 error("cannot infer opcode suffix");
722 for(i
= 0; i
< nb_ops
; i
++) {
723 if (ops
[i
].type
& OP_REG32
) {
724 if (s1
->seg_size
== 16)
726 } else if (!(ops
[i
].type
& OP_REG32
)) {
727 if (s1
->seg_size
== 32)
733 if (s
== 1 || (pa
->instr_type
& OPC_D16
)) {
734 if (s1
->seg_size
== 32)
737 if (s1
->seg_size
== 16) {
738 if (!(pa
->instr_type
& OPC_D16
))
743 /* generate a16/a32 prefix if needed */
744 if ((a32
== 1) && (addr32
== 0))
746 /* generate o16/o32 prefix if needed */
747 if ((o32
== 1) && (data32
== 0))
752 /* generate data16 prefix if needed */
753 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
755 #ifdef TCC_TARGET_X86_64
757 /* generate REX prefix */
758 if ((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
)
759 || !(ops
[0].type
& OP_REG64
))
765 /* now generates the operation */
766 if (pa
->instr_type
& OPC_FWAIT
)
772 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
773 /* kludge for imul $im, %reg */
776 op_type
[2] = op_type
[1];
777 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
778 v
--; /* int $3 case */
780 } else if ((v
== 0x06 || v
== 0x07)) {
781 if (ops
[0].reg
>= 4) {
782 /* push/pop %fs or %gs */
783 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
785 v
+= ops
[0].reg
<< 3;
788 } else if (v
<= 0x05) {
790 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
791 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
793 v
+= ((opcode
- pa
->sym
) / 6) << 3;
795 if (pa
->instr_type
& OPC_REG
) {
796 for(i
= 0; i
< nb_ops
; i
++) {
797 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
802 /* mov $im, %reg case */
803 if (pa
->opcode
== 0xb0 && s
>= 1)
806 if (pa
->instr_type
& OPC_B
)
808 if (pa
->instr_type
& OPC_TEST
)
809 v
+= test_bits
[opcode
- pa
->sym
];
810 if (pa
->instr_type
& OPC_SHORTJMP
) {
814 /* see if we can really generate the jump with a byte offset */
818 if (sym
->r
!= cur_text_section
->sh_num
)
820 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2;
821 if (jmp_disp
== (int8_t)jmp_disp
) {
822 /* OK to generate jump */
824 ops
[0].e
.v
= jmp_disp
;
827 if (pa
->instr_type
& OPC_JMP
) {
828 /* long jump will be allowed. need to modify the
835 error("invalid displacement");
844 /* search which operand will used for modrm */
846 if (pa
->instr_type
& OPC_SHIFT
) {
847 reg
= (opcode
- pa
->sym
) / NBWLX
;
850 } else if (pa
->instr_type
& OPC_ARITH
) {
851 reg
= (opcode
- pa
->sym
) / NBWLX
;
852 } else if (pa
->instr_type
& OPC_FARITH
) {
853 reg
= (opcode
- pa
->sym
) / 6;
855 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
857 if (pa
->instr_type
& OPC_MODRM
) {
858 /* first look for an ea operand */
859 for(i
= 0;i
< nb_ops
; i
++) {
860 if (op_type
[i
] & OP_EA
)
863 /* then if not found, a register or indirection (shift instructions) */
864 for(i
= 0;i
< nb_ops
; i
++) {
865 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
869 error("bad op table");
873 /* if a register is used in another operand then it is
874 used instead of group */
875 for(i
= 0;i
< nb_ops
; i
++) {
877 if (i
!= modrm_index
&&
878 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
884 asm_modrm(reg
, &ops
[modrm_index
]);
888 #ifndef TCC_TARGET_X86_64
889 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
890 /* ljmp or lcall kludge */
892 if (s1
->seg_size
== 16 && o32
== 0)
893 gen_expr16(&ops
[1].e
);
896 gen_expr32(&ops
[1].e
);
898 error("cannot relocate");
899 gen_le16(ops
[0].e
.v
);
903 for(i
= 0;i
< nb_ops
; i
++) {
905 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
906 /* if multiple sizes are given it means we must look
908 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
913 else if (s
== 2 || (v
& OP_IM64
) == 0)
918 if (v
& (OP_IM8
| OP_IM8S
)) {
922 } else if (v
& OP_IM16
) {
924 if (s1
->seg_size
== 16)
925 gen_expr16(&ops
[i
].e
);
930 error("cannot relocate");
932 gen_le16(ops
[i
].e
.v
);
934 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
938 else if (s1
->seg_size
== 16)
939 gen_disp16(&ops
[i
].e
);
942 gen_disp32(&ops
[i
].e
);
945 if (s1
->seg_size
== 16 && !((o32
== 1) && (v
& OP_IM32
)))
946 gen_expr16(&ops
[i
].e
);
949 #ifdef TCC_TARGET_X86_64
951 gen_expr64(&ops
[i
].e
);
954 gen_expr32(&ops
[i
].e
);
958 } else if (v
& (OP_REG16
| OP_REG32
)) {
959 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
961 g(0xE0 + ops
[i
].reg
);
964 #ifdef TCC_TARGET_X86_64
965 } else if (v
& (OP_REG32
| OP_REG64
)) {
966 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
968 g(0xE0 + ops
[i
].reg
);
978 /* return the constraint priority (we allocate first the lowest
979 numbered constraints) */
980 static inline int constraint_priority(const char *str
)
984 /* we take the lowest priority */
1018 error("unknown constraint '%c'", c
);
1027 static const char *skip_constraint_modifiers(const char *p
)
1029 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
1034 #define REG_OUT_MASK 0x01
1035 #define REG_IN_MASK 0x02
1037 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1039 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
1040 int nb_operands
, int nb_outputs
,
1041 const uint8_t *clobber_regs
,
1045 int sorted_op
[MAX_ASM_OPERANDS
];
1046 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
1048 uint8_t regs_allocated
[NB_ASM_REGS
];
1051 for(i
=0;i
<nb_operands
;i
++) {
1053 op
->input_index
= -1;
1059 /* compute constraint priority and evaluate references to output
1060 constraints if input constraints */
1061 for(i
=0;i
<nb_operands
;i
++) {
1063 str
= op
->constraint
;
1064 str
= skip_constraint_modifiers(str
);
1065 if (isnum(*str
) || *str
== '[') {
1066 /* this is a reference to another constraint */
1067 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
1068 if ((unsigned)k
>= i
|| i
< nb_outputs
)
1069 error("invalid reference in constraint %d ('%s')",
1072 if (operands
[k
].input_index
>= 0)
1073 error("cannot reference twice the same operand");
1074 operands
[k
].input_index
= i
;
1077 op
->priority
= constraint_priority(str
);
1081 /* sort operands according to their priority */
1082 for(i
=0;i
<nb_operands
;i
++)
1084 for(i
=0;i
<nb_operands
- 1;i
++) {
1085 for(j
=i
+1;j
<nb_operands
;j
++) {
1086 p1
= operands
[sorted_op
[i
]].priority
;
1087 p2
= operands
[sorted_op
[j
]].priority
;
1090 sorted_op
[i
] = sorted_op
[j
];
1096 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1097 if (clobber_regs
[i
])
1098 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1100 regs_allocated
[i
] = 0;
1102 /* esp cannot be used */
1103 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1104 /* ebp cannot be used yet */
1105 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1107 /* allocate registers and generate corresponding asm moves */
1108 for(i
=0;i
<nb_operands
;i
++) {
1111 str
= op
->constraint
;
1112 /* no need to allocate references */
1113 if (op
->ref_index
>= 0)
1115 /* select if register is used for output, input or both */
1116 if (op
->input_index
>= 0) {
1117 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1118 } else if (j
< nb_outputs
) {
1119 reg_mask
= REG_OUT_MASK
;
1121 reg_mask
= REG_IN_MASK
;
1132 if (j
>= nb_outputs
)
1133 error("'%c' modifier can only be applied to outputs", c
);
1134 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1137 /* allocate both eax and edx */
1138 if (is_reg_allocated(TREG_XAX
) ||
1139 is_reg_allocated(TREG_XDX
))
1143 regs_allocated
[TREG_XAX
] |= reg_mask
;
1144 regs_allocated
[TREG_XDX
] |= reg_mask
;
1164 if (is_reg_allocated(reg
))
1168 /* eax, ebx, ecx or edx */
1169 for(reg
= 0; reg
< 4; reg
++) {
1170 if (!is_reg_allocated(reg
))
1175 /* any general register */
1176 for(reg
= 0; reg
< 8; reg
++) {
1177 if (!is_reg_allocated(reg
))
1182 /* now we can reload in the register */
1185 regs_allocated
[reg
] |= reg_mask
;
1188 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1194 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1199 /* nothing special to do because the operand is already in
1200 memory, except if the pointer itself is stored in a
1201 memory variable (VT_LLOCAL case) */
1202 /* XXX: fix constant case */
1203 /* if it is a reference to a memory zone, it must lie
1204 in a register, so we reserve the register in the
1205 input registers and a load will be generated
1207 if (j
< nb_outputs
|| c
== 'm') {
1208 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1209 /* any general register */
1210 for(reg
= 0; reg
< 8; reg
++) {
1211 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1216 /* now we can reload in the register */
1217 regs_allocated
[reg
] |= REG_IN_MASK
;
1224 error("asm constraint %d ('%s') could not be satisfied",
1228 /* if a reference is present for that operand, we assign it too */
1229 if (op
->input_index
>= 0) {
1230 operands
[op
->input_index
].reg
= op
->reg
;
1231 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1235 /* compute out_reg. It is used to store outputs registers to memory
1236 locations references by pointers (VT_LLOCAL case) */
1238 for(i
=0;i
<nb_operands
;i
++) {
1241 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1243 for(reg
= 0; reg
< 8; reg
++) {
1244 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1247 error("could not find free output register for reloading");
1254 /* print sorted constraints */
1256 for(i
=0;i
<nb_operands
;i
++) {
1259 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1261 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1267 printf("out_reg=%d\n", *pout_reg
);
1271 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1272 SValue
*sv
, int modifier
)
1274 int r
, reg
, size
, val
;
1278 if ((r
& VT_VALMASK
) == VT_CONST
) {
1279 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1280 cstr_ccat(add_str
, '$');
1282 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
));
1284 cstr_ccat(add_str
, '+');
1290 if (modifier
== 'n')
1292 snprintf(buf
, sizeof(buf
), "%d", sv
->c
.i
);
1293 cstr_cat(add_str
, buf
);
1294 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1295 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", sv
->c
.i
);
1296 cstr_cat(add_str
, buf
);
1297 } else if (r
& VT_LVAL
) {
1298 reg
= r
& VT_VALMASK
;
1299 if (reg
>= VT_CONST
)
1300 error("internal compiler error");
1301 snprintf(buf
, sizeof(buf
), "(%%%s)",
1302 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1303 cstr_cat(add_str
, buf
);
1306 reg
= r
& VT_VALMASK
;
1307 if (reg
>= VT_CONST
)
1308 error("internal compiler error");
1310 /* choose register operand size */
1311 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1313 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1315 #ifdef TCC_TARGET_X86_64
1316 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1321 if (size
== 1 && reg
>= 4)
1324 if (modifier
== 'b') {
1326 error("cannot use byte register");
1328 } else if (modifier
== 'h') {
1330 error("cannot use byte register");
1332 } else if (modifier
== 'w') {
1334 #ifdef TCC_TARGET_X86_64
1335 } else if (modifier
== 'q') {
1342 reg
= TOK_ASM_ah
+ reg
;
1345 reg
= TOK_ASM_al
+ reg
;
1348 reg
= TOK_ASM_ax
+ reg
;
1351 reg
= TOK_ASM_eax
+ reg
;
1353 #ifdef TCC_TARGET_X86_64
1355 reg
= TOK_ASM_rax
+ reg
;
1359 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1360 cstr_cat(add_str
, buf
);
1364 /* generate prolog and epilog code for asm statment */
1365 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1366 int nb_outputs
, int is_output
,
1367 uint8_t *clobber_regs
,
1370 uint8_t regs_allocated
[NB_ASM_REGS
];
1373 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1375 /* mark all used registers */
1376 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1377 for(i
= 0; i
< nb_operands
;i
++) {
1380 regs_allocated
[op
->reg
] = 1;
1383 /* generate reg save code */
1384 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1386 if (regs_allocated
[reg
]) {
1388 if (tcc_state
->seg_size
== 16)
1395 /* generate load code */
1396 for(i
= 0; i
< nb_operands
; i
++) {
1399 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1401 /* memory reference case (for both input and
1405 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1407 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1408 /* load value in register */
1409 load(op
->reg
, op
->vt
);
1414 load(TREG_XDX
, &sv
);
1420 /* generate save code */
1421 for(i
= 0 ; i
< nb_outputs
; i
++) {
1424 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1425 if (!op
->is_memory
) {
1428 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1431 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1432 store(op
->reg
, &sv
);
1435 store(op
->reg
, op
->vt
);
1440 store(TREG_XDX
, &sv
);
1445 /* generate reg restore code */
1446 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1448 if (regs_allocated
[reg
]) {
1450 if (tcc_state
->seg_size
== 16)
1459 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1464 if (!strcmp(str
, "memory") ||
1467 ts
= tok_alloc(str
, strlen(str
));
1469 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1471 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1473 #ifdef TCC_TARGET_X86_64
1474 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1478 error("invalid clobber register '%s'", str
);
1480 clobber_regs
[reg
] = 1;