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
49 # define OPC_BWLX OPC_BWLQ
51 # define OPC_WLX OPC_WL
52 # define OPC_BWLX OPC_BWL
55 #define OPC_GROUP_SHIFT 13
57 /* in order to compress the operand type, we use specific operands and
60 OPT_REG8
=0, /* warning: value is hardcoded from TOK_ASM_xxx */
61 OPT_REG16
, /* warning: value is hardcoded from TOK_ASM_xxx */
62 OPT_REG32
, /* warning: value is hardcoded from TOK_ASM_xxx */
63 #ifdef TCC_TARGET_X86_64
64 OPT_REG64
, /* warning: value is hardcoded from TOK_ASM_xxx */
66 OPT_MMX
, /* warning: value is hardcoded from TOK_ASM_xxx */
67 OPT_SSE
, /* warning: value is hardcoded from TOK_ASM_xxx */
68 OPT_CR
, /* warning: value is hardcoded from TOK_ASM_xxx */
69 OPT_TR
, /* warning: value is hardcoded from TOK_ASM_xxx */
70 OPT_DB
, /* warning: value is hardcoded from TOK_ASM_xxx */
77 #ifdef TCC_TARGET_X86_64
80 OPT_EAX
, /* %al, %ax, %eax or %rax register */
81 OPT_ST0
, /* %st(0) register */
82 OPT_CL
, /* %cl register */
83 OPT_DX
, /* %dx register */
84 OPT_ADDR
, /* OP_EA with only offset */
85 OPT_INDIR
, /* *(expr) */
88 OPT_IM
, /* IM8 | IM16 | IM32 */
89 OPT_REG
, /* REG8 | REG16 | REG32 | REG64 */
90 OPT_REGW
, /* REG16 | REG32 | REG64 */
91 OPT_IMW
, /* 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)
119 # define OP_EA32 (OP_EA << 1)
126 #define OP_EA 0x40000000
127 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
129 #ifdef TCC_TARGET_X86_64
130 # define TREG_XAX TREG_RAX
131 # define TREG_XCX TREG_RCX
132 # define TREG_XDX TREG_RDX
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
)
244 v
= asm_int_expr(s1
);
259 expect("1, 2, 4 or 8 constant");
266 static int asm_parse_reg(int *type
)
273 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
274 reg
= tok
- TOK_ASM_eax
;
275 #ifdef TCC_TARGET_X86_64
277 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
278 reg
= tok
- TOK_ASM_rax
;
279 } else if (tok
== TOK_ASM_rip
) {
290 static void parse_operand(TCCState
*s1
, Operand
*op
)
304 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
305 reg
= tok
- TOK_ASM_al
;
306 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
308 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
310 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
312 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
314 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
316 op
->reg
= tok
- TOK_ASM_dr0
;
317 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
319 op
->reg
= tok
- TOK_ASM_es
;
320 } else if (tok
== TOK_ASM_st
) {
326 if (tok
!= TOK_PPNUM
)
330 if ((unsigned)reg
>= 8 || p
[1] != '\0')
341 tcc_error("unknown register");
345 } else if (tok
== '$') {
353 if (op
->e
.v
== (uint8_t)op
->e
.v
)
355 if (op
->e
.v
== (int8_t)op
->e
.v
)
357 if (op
->e
.v
== (uint16_t)op
->e
.v
)
359 #ifdef TCC_TARGET_X86_64
360 if (op
->e
.v
!= (int32_t)op
->e
.v
)
365 /* address(reg,reg2,shift) with all variants */
381 /* bracketed offset expression */
394 op
->reg
= asm_parse_reg(&type
);
399 op
->reg2
= asm_parse_reg(&type
);
403 op
->shift
= get_reg_shift(s1
);
410 if (op
->reg
== -1 && op
->reg2
== -1)
416 /* XXX: unify with C code output ? */
417 ST_FUNC
void gen_expr32(ExprValue
*pe
)
419 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
422 #ifdef TCC_TARGET_X86_64
423 static void gen_expr64(ExprValue
*pe
)
425 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
429 /* XXX: unify with C code output ? */
430 static void gen_disp32(ExprValue
*pe
)
433 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
434 /* same section: we can output an absolute value. Note
435 that the TCC compiler behaves differently here because
436 it always outputs a relocation to ease (future) code
437 elimination in the linker */
438 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
440 if (sym
&& sym
->type
.t
== VT_VOID
) {
441 sym
->type
.t
= VT_FUNC
;
442 sym
->type
.ref
= NULL
;
444 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
448 /* generate the modrm operand */
449 static inline int asm_modrm(int reg
, Operand
*op
)
451 int mod
, reg1
, reg2
, sib_reg1
;
453 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
454 g(0xc0 + (reg
<< 3) + op
->reg
);
455 } else if (op
->reg
== -1 && op
->reg2
== -1) {
456 /* displacement only */
457 #ifdef TCC_TARGET_X86_64
458 g(0x04 + (reg
<< 3));
461 g(0x05 + (reg
<< 3));
464 #ifdef TCC_TARGET_X86_64
465 } else if (op
->reg
== 8) {
466 ExprValue
*pe
= &op
->e
;
467 g(0x05 + (reg
<< 3));
468 gen_addrpc32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
473 /* fist compute displacement encoding */
474 if (sib_reg1
== -1) {
477 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
479 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
484 /* compute if sib byte needed */
488 g(mod
+ (reg
<< 3) + reg1
);
493 reg2
= 4; /* indicate no index */
494 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
499 } else if (mod
== 0x80 || op
->reg
== -1) {
506 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
509 int i
, modrm_index
, reg
, v
, op1
, seg_prefix
, pc
;
511 Operand ops
[MAX_OPERANDS
], *pop
;
512 int op_type
[3]; /* decoded op type */
513 int alltypes
; /* OR of all operand types */
516 /* force synthetic ';' after prefix instruction, so we can handle */
517 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
518 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
527 if (tok
== ';' || tok
== TOK_LINEFEED
)
529 if (nb_ops
>= MAX_OPERANDS
) {
530 tcc_error("incorrect number of operands");
532 parse_operand(s1
, pop
);
534 if (pop
->type
!= OP_SEG
|| seg_prefix
)
535 tcc_error("incorrect prefix");
536 seg_prefix
= segment_prefixes
[pop
->reg
];
538 parse_operand(s1
, pop
);
539 if (!(pop
->type
& OP_EA
)) {
540 tcc_error("segment prefix must be followed by memory reference");
550 s
= 0; /* avoid warning */
552 /* optimize matching by using a lookup table (no hashing is needed
554 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
556 if (pa
->instr_type
& OPC_FARITH
) {
557 v
= opcode
- pa
->sym
;
558 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
560 } else if (pa
->instr_type
& OPC_ARITH
) {
561 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
563 s
= (opcode
- pa
->sym
) % NBWLX
;
564 if ((pa
->instr_type
& OPC_BWLX
) == OPC_WLX
)
566 /* We need to reject the xxxb opcodes that we accepted above.
567 Note that pa->sym for WLX opcodes is the 'w' token,
568 to get the 'b' token subtract one. */
569 if (((opcode
- pa
->sym
+ 1) % NBWLX
) == 0)
573 } else if (pa
->instr_type
& OPC_SHIFT
) {
574 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
576 s
= (opcode
- pa
->sym
) % NBWLX
;
577 } else if (pa
->instr_type
& OPC_TEST
) {
578 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
580 /* cmovxx is a test opcode but accepts multiple sizes.
581 TCC doesn't accept the suffixed mnemonic, instead we
582 simply force size autodetection always. */
583 if (pa
->instr_type
& OPC_WLX
)
585 } else if (pa
->instr_type
& OPC_B
) {
586 #ifdef TCC_TARGET_X86_64
587 /* Some instructions don't have the full size but only
588 bwl form. insb e.g. */
589 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
590 && !(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
593 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
595 s
= opcode
- pa
->sym
;
596 } else if (pa
->instr_type
& OPC_WLX
) {
597 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
599 s
= opcode
- pa
->sym
+ 1;
601 if (pa
->sym
!= opcode
)
604 if (pa
->nb_ops
!= nb_ops
)
606 #ifdef TCC_TARGET_X86_64
607 /* Special case for moves. Selecting the IM64->REG64 form
608 should only be done if we really have an >32bit imm64, and that
609 is hardcoded. Ignore it here. */
610 if (pa
->opcode
== 0xb0 && ops
[0].type
!= OP_IM64
611 && ops
[1].type
== OP_REG64
)
614 /* now decode and check each operand */
616 for(i
= 0; i
< nb_ops
; i
++) {
618 op1
= pa
->op_type
[i
];
622 v
= OP_IM8
| OP_IM16
| OP_IM32
;
625 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
628 v
= OP_REG16
| OP_REG32
| OP_REG64
;
631 v
= OP_IM16
| OP_IM32
;
640 if ((ops
[i
].type
& v
) == 0)
642 alltypes
|= ops
[i
].type
;
644 /* all is matching ! */
649 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
651 b
= op0_codes
[opcode
- TOK_ASM_first
];
656 } else if (opcode
<= TOK_ASM_alllast
) {
657 tcc_error("bad operand with opcode '%s'",
658 get_tok_str(opcode
, NULL
));
660 tcc_error("unknown opcode '%s'",
661 get_tok_str(opcode
, NULL
));
664 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
666 #ifdef TCC_TARGET_X86_64
667 /* XXX the autosize should rather be zero, to not have to adjust this
669 if ((pa
->instr_type
& OPC_BWLQ
) == OPC_B
)
673 for(i
= 0; s
== autosize
&& i
< nb_ops
; i
++) {
674 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
675 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
678 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
679 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
)))
682 tcc_error("cannot infer opcode suffix");
686 #ifdef TCC_TARGET_X86_64
687 /* Generate addr32 prefix if needed */
688 for(i
= 0; i
< nb_ops
; i
++) {
689 if (ops
[i
].type
& OP_EA32
) {
695 /* generate data16 prefix if needed */
696 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
698 #ifdef TCC_TARGET_X86_64
699 if (s
== 3 || (alltypes
& OP_REG64
)) {
700 /* generate REX prefix */
702 for(i
= 0; i
< nb_ops
; i
++) {
703 if (op_type
[i
] == OP_REG64
) {
704 /* If only 64bit regs are accepted in one operand
705 this is a default64 instruction without need for
711 /* XXX find better encoding for the default64 instructions. */
712 if (((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
713 && opcode
!= TOK_ASM_pushw
&& opcode
!= TOK_ASM_pushl
714 && opcode
!= TOK_ASM_pushq
&& opcode
!= TOK_ASM_popw
715 && opcode
!= TOK_ASM_popl
&& opcode
!= TOK_ASM_popq
716 && opcode
!= TOK_ASM_call
&& opcode
!= TOK_ASM_jmp
))
722 /* now generates the operation */
723 if (pa
->instr_type
& OPC_FWAIT
)
729 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
730 /* kludge for imul $im, %reg */
733 op_type
[2] = op_type
[1];
734 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
735 v
--; /* int $3 case */
737 } else if ((v
== 0x06 || v
== 0x07)) {
738 if (ops
[0].reg
>= 4) {
739 /* push/pop %fs or %gs */
740 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
742 v
+= ops
[0].reg
<< 3;
745 } else if (v
<= 0x05) {
747 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
748 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
750 v
+= ((opcode
- pa
->sym
) / 6) << 3;
752 if (pa
->instr_type
& OPC_REG
) {
753 for(i
= 0; i
< nb_ops
; i
++) {
754 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
759 /* mov $im, %reg case */
760 if (pa
->opcode
== 0xb0 && s
>= 1)
763 if (pa
->instr_type
& OPC_B
)
765 if (pa
->instr_type
& OPC_TEST
)
766 v
+= test_bits
[opcode
- pa
->sym
];
767 if (pa
->instr_type
& OPC_SHORTJMP
) {
771 /* see if we can really generate the jump with a byte offset */
775 if (sym
->r
!= cur_text_section
->sh_num
)
777 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2 - (v
>= 0xff);
778 if (jmp_disp
== (int8_t)jmp_disp
) {
779 /* OK to generate jump */
781 ops
[0].e
.v
= jmp_disp
;
782 op_type
[0] = OP_IM8S
;
785 if (pa
->instr_type
& OPC_JMP
) {
786 /* long jump will be allowed. need to modify the
793 tcc_error("invalid displacement");
802 /* search which operand will used for modrm */
804 if (pa
->instr_type
& OPC_SHIFT
) {
805 reg
= (opcode
- pa
->sym
) / NBWLX
;
808 } else if (pa
->instr_type
& OPC_ARITH
) {
809 reg
= (opcode
- pa
->sym
) / NBWLX
;
810 } else if (pa
->instr_type
& OPC_FARITH
) {
811 reg
= (opcode
- pa
->sym
) / 6;
813 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
817 if (pa
->instr_type
& OPC_MODRM
) {
818 /* first look for an ea operand */
819 for(i
= 0;i
< nb_ops
; i
++) {
820 if (op_type
[i
] & OP_EA
)
823 /* then if not found, a register or indirection (shift instructions) */
824 for(i
= 0;i
< nb_ops
; i
++) {
825 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
829 tcc_error("bad op table");
833 /* if a register is used in another operand then it is
834 used instead of group */
835 for(i
= 0;i
< nb_ops
; i
++) {
837 if (i
!= modrm_index
&&
838 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
843 pc
= asm_modrm(reg
, &ops
[modrm_index
]);
847 #ifndef TCC_TARGET_X86_64
848 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
849 /* ljmp or lcall kludge */
850 gen_expr32(&ops
[1].e
);
852 tcc_error("cannot relocate");
853 gen_le16(ops
[0].e
.v
);
857 for(i
= 0;i
< nb_ops
; i
++) {
859 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
860 /* if multiple sizes are given it means we must look
862 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
867 else if (s
== 2 || (v
& OP_IM64
) == 0)
873 if ((v
& (OP_IM8
| OP_IM8S
| OP_IM16
)) && ops
[i
].e
.sym
)
874 tcc_error("cannot relocate");
876 if (v
& (OP_IM8
| OP_IM8S
)) {
878 } else if (v
& OP_IM16
) {
879 gen_le16(ops
[i
].e
.v
);
880 #ifdef TCC_TARGET_X86_64
881 } else if (v
& OP_IM64
) {
882 gen_expr64(&ops
[i
].e
);
884 } else if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
885 gen_disp32(&ops
[i
].e
);
887 gen_expr32(&ops
[i
].e
);
892 /* after immediate operands, adjust pc-relative address */
894 add32le(text_section
->data
+ pc
- 4, pc
- ind
);
897 /* return the constraint priority (we allocate first the lowest
898 numbered constraints) */
899 static inline int constraint_priority(const char *str
)
903 /* we take the lowest priority */
937 tcc_error("unknown constraint '%c'", c
);
946 static const char *skip_constraint_modifiers(const char *p
)
948 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
953 #define REG_OUT_MASK 0x01
954 #define REG_IN_MASK 0x02
956 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
958 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
959 int nb_operands
, int nb_outputs
,
960 const uint8_t *clobber_regs
,
964 int sorted_op
[MAX_ASM_OPERANDS
];
965 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
967 uint8_t regs_allocated
[NB_ASM_REGS
];
970 for(i
=0;i
<nb_operands
;i
++) {
972 op
->input_index
= -1;
978 /* compute constraint priority and evaluate references to output
979 constraints if input constraints */
980 for(i
=0;i
<nb_operands
;i
++) {
982 str
= op
->constraint
;
983 str
= skip_constraint_modifiers(str
);
984 if (isnum(*str
) || *str
== '[') {
985 /* this is a reference to another constraint */
986 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
987 if ((unsigned)k
>= i
|| i
< nb_outputs
)
988 tcc_error("invalid reference in constraint %d ('%s')",
991 if (operands
[k
].input_index
>= 0)
992 tcc_error("cannot reference twice the same operand");
993 operands
[k
].input_index
= i
;
996 op
->priority
= constraint_priority(str
);
1000 /* sort operands according to their priority */
1001 for(i
=0;i
<nb_operands
;i
++)
1003 for(i
=0;i
<nb_operands
- 1;i
++) {
1004 for(j
=i
+1;j
<nb_operands
;j
++) {
1005 p1
= operands
[sorted_op
[i
]].priority
;
1006 p2
= operands
[sorted_op
[j
]].priority
;
1009 sorted_op
[i
] = sorted_op
[j
];
1015 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1016 if (clobber_regs
[i
])
1017 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1019 regs_allocated
[i
] = 0;
1021 /* esp cannot be used */
1022 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1023 /* ebp cannot be used yet */
1024 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1026 /* allocate registers and generate corresponding asm moves */
1027 for(i
=0;i
<nb_operands
;i
++) {
1030 str
= op
->constraint
;
1031 /* no need to allocate references */
1032 if (op
->ref_index
>= 0)
1034 /* select if register is used for output, input or both */
1035 if (op
->input_index
>= 0) {
1036 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1037 } else if (j
< nb_outputs
) {
1038 reg_mask
= REG_OUT_MASK
;
1040 reg_mask
= REG_IN_MASK
;
1051 if (j
>= nb_outputs
)
1052 tcc_error("'%c' modifier can only be applied to outputs", c
);
1053 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1056 /* allocate both eax and edx */
1057 if (is_reg_allocated(TREG_XAX
) ||
1058 is_reg_allocated(TREG_XDX
))
1062 regs_allocated
[TREG_XAX
] |= reg_mask
;
1063 regs_allocated
[TREG_XDX
] |= reg_mask
;
1083 if (is_reg_allocated(reg
))
1087 /* eax, ebx, ecx or edx */
1088 for(reg
= 0; reg
< 4; reg
++) {
1089 if (!is_reg_allocated(reg
))
1094 /* any general register */
1095 for(reg
= 0; reg
< 8; reg
++) {
1096 if (!is_reg_allocated(reg
))
1101 /* now we can reload in the register */
1104 regs_allocated
[reg
] |= reg_mask
;
1107 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1113 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1118 /* nothing special to do because the operand is already in
1119 memory, except if the pointer itself is stored in a
1120 memory variable (VT_LLOCAL case) */
1121 /* XXX: fix constant case */
1122 /* if it is a reference to a memory zone, it must lie
1123 in a register, so we reserve the register in the
1124 input registers and a load will be generated
1126 if (j
< nb_outputs
|| c
== 'm') {
1127 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1128 /* any general register */
1129 for(reg
= 0; reg
< 8; reg
++) {
1130 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1135 /* now we can reload in the register */
1136 regs_allocated
[reg
] |= REG_IN_MASK
;
1143 tcc_error("asm constraint %d ('%s') could not be satisfied",
1147 /* if a reference is present for that operand, we assign it too */
1148 if (op
->input_index
>= 0) {
1149 operands
[op
->input_index
].reg
= op
->reg
;
1150 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1154 /* compute out_reg. It is used to store outputs registers to memory
1155 locations references by pointers (VT_LLOCAL case) */
1157 for(i
=0;i
<nb_operands
;i
++) {
1160 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1162 for(reg
= 0; reg
< 8; reg
++) {
1163 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1166 tcc_error("could not find free output register for reloading");
1173 /* print sorted constraints */
1175 for(i
=0;i
<nb_operands
;i
++) {
1178 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1180 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1186 printf("out_reg=%d\n", *pout_reg
);
1190 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1191 SValue
*sv
, int modifier
)
1193 int r
, reg
, size
, val
;
1197 if ((r
& VT_VALMASK
) == VT_CONST
) {
1198 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1199 cstr_ccat(add_str
, '$');
1201 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
), -1);
1202 if ((uint32_t)sv
->c
.i
== 0)
1204 cstr_ccat(add_str
, '+');
1207 if (modifier
== 'n')
1209 snprintf(buf
, sizeof(buf
), "%d", (int)sv
->c
.i
);
1210 cstr_cat(add_str
, buf
, -1);
1212 #ifdef TCC_TARGET_X86_64
1214 cstr_cat(add_str
, "(%rip)", -1);
1216 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1217 #ifdef TCC_TARGET_X86_64
1218 snprintf(buf
, sizeof(buf
), "%d(%%rbp)", (int)sv
->c
.i
);
1220 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", (int)sv
->c
.i
);
1222 cstr_cat(add_str
, buf
, -1);
1223 } else if (r
& VT_LVAL
) {
1224 reg
= r
& VT_VALMASK
;
1225 if (reg
>= VT_CONST
)
1226 tcc_error("internal compiler error");
1227 snprintf(buf
, sizeof(buf
), "(%%%s)",
1228 #ifdef TCC_TARGET_X86_64
1229 get_tok_str(TOK_ASM_rax
+ reg
, NULL
)
1231 get_tok_str(TOK_ASM_eax
+ reg
, NULL
)
1234 cstr_cat(add_str
, buf
, -1);
1237 reg
= r
& VT_VALMASK
;
1238 if (reg
>= VT_CONST
)
1239 tcc_error("internal compiler error");
1241 /* choose register operand size */
1242 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1244 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1246 #ifdef TCC_TARGET_X86_64
1247 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1252 if (size
== 1 && reg
>= 4)
1255 if (modifier
== 'b') {
1257 tcc_error("cannot use byte register");
1259 } else if (modifier
== 'h') {
1261 tcc_error("cannot use byte register");
1263 } else if (modifier
== 'w') {
1265 } else if (modifier
== 'k') {
1267 #ifdef TCC_TARGET_X86_64
1268 } else if (modifier
== 'q') {
1275 reg
= TOK_ASM_ah
+ reg
;
1278 reg
= TOK_ASM_al
+ reg
;
1281 reg
= TOK_ASM_ax
+ reg
;
1284 reg
= TOK_ASM_eax
+ reg
;
1286 #ifdef TCC_TARGET_X86_64
1288 reg
= TOK_ASM_rax
+ reg
;
1292 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1293 cstr_cat(add_str
, buf
, -1);
1297 /* generate prolog and epilog code for asm statement */
1298 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1299 int nb_outputs
, int is_output
,
1300 uint8_t *clobber_regs
,
1303 uint8_t regs_allocated
[NB_ASM_REGS
];
1306 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1308 /* mark all used registers */
1309 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1310 for(i
= 0; i
< nb_operands
;i
++) {
1313 regs_allocated
[op
->reg
] = 1;
1316 /* generate reg save code */
1317 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1319 if (regs_allocated
[reg
]) {
1324 /* generate load code */
1325 for(i
= 0; i
< nb_operands
; i
++) {
1328 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1330 /* memory reference case (for both input and
1334 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
| VT_LVAL
;
1337 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1338 /* load value in register */
1339 load(op
->reg
, op
->vt
);
1344 load(TREG_XDX
, &sv
);
1350 /* generate save code */
1351 for(i
= 0 ; i
< nb_outputs
; i
++) {
1354 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1355 if (!op
->is_memory
) {
1358 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1361 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1362 store(op
->reg
, &sv
);
1365 store(op
->reg
, op
->vt
);
1370 store(TREG_XDX
, &sv
);
1375 /* generate reg restore code */
1376 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1378 if (regs_allocated
[reg
]) {
1385 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1390 if (!strcmp(str
, "memory") ||
1393 ts
= tok_alloc(str
, strlen(str
));
1395 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1397 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1399 #ifdef TCC_TARGET_X86_64
1400 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1404 tcc_error("invalid clobber register '%s'", str
);
1406 clobber_regs
[reg
] = 1;