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
;
288 static void parse_operand(TCCState
*s1
, Operand
*op
)
302 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
303 reg
= tok
- TOK_ASM_al
;
304 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
306 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
308 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
310 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
312 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
314 op
->reg
= tok
- TOK_ASM_dr0
;
315 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
317 op
->reg
= tok
- TOK_ASM_es
;
318 } else if (tok
== TOK_ASM_st
) {
324 if (tok
!= TOK_PPNUM
)
328 if ((unsigned)reg
>= 8 || p
[1] != '\0')
339 tcc_error("unknown register");
343 } else if (tok
== '$') {
351 if (op
->e
.v
== (uint8_t)op
->e
.v
)
353 if (op
->e
.v
== (int8_t)op
->e
.v
)
355 if (op
->e
.v
== (uint16_t)op
->e
.v
)
357 #ifdef TCC_TARGET_X86_64
358 if (op
->e
.v
!= (int32_t)op
->e
.v
)
363 /* address(reg,reg2,shift) with all variants */
379 /* bracketed offset expression */
392 op
->reg
= asm_parse_reg(&type
);
397 op
->reg2
= asm_parse_reg(&type
);
401 op
->shift
= get_reg_shift(s1
);
408 if (op
->reg
== -1 && op
->reg2
== -1)
414 /* XXX: unify with C code output ? */
415 ST_FUNC
void gen_expr32(ExprValue
*pe
)
417 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
420 #ifdef TCC_TARGET_X86_64
421 static void gen_expr64(ExprValue
*pe
)
423 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
427 /* XXX: unify with C code output ? */
428 static void gen_disp32(ExprValue
*pe
)
431 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
432 /* same section: we can output an absolute value. Note
433 that the TCC compiler behaves differently here because
434 it always outputs a relocation to ease (future) code
435 elimination in the linker */
436 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
438 if (sym
&& sym
->type
.t
== VT_VOID
) {
439 sym
->type
.t
= VT_FUNC
;
440 sym
->type
.ref
= NULL
;
442 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
446 /* generate the modrm operand */
447 static inline void asm_modrm(int reg
, Operand
*op
)
449 int mod
, reg1
, reg2
, sib_reg1
;
451 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
452 g(0xc0 + (reg
<< 3) + op
->reg
);
453 } else if (op
->reg
== -1 && op
->reg2
== -1) {
454 /* displacement only */
455 #ifdef TCC_TARGET_X86_64
456 g(0x04 + (reg
<< 3));
459 g(0x05 + (reg
<< 3));
464 /* fist compute displacement encoding */
465 if (sib_reg1
== -1) {
468 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
470 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
475 /* compute if sib byte needed */
479 g(mod
+ (reg
<< 3) + reg1
);
484 reg2
= 4; /* indicate no index */
485 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
490 } else if (mod
== 0x80 || op
->reg
== -1) {
496 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
499 int i
, modrm_index
, reg
, v
, op1
, seg_prefix
;
501 Operand ops
[MAX_OPERANDS
], *pop
;
502 int op_type
[3]; /* decoded op type */
503 int alltypes
; /* OR of all operand types */
506 /* force synthetic ';' after prefix instruction, so we can handle */
507 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
508 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
517 if (tok
== ';' || tok
== TOK_LINEFEED
)
519 if (nb_ops
>= MAX_OPERANDS
) {
520 tcc_error("incorrect number of operands");
522 parse_operand(s1
, pop
);
524 if (pop
->type
!= OP_SEG
|| seg_prefix
)
525 tcc_error("incorrect prefix");
526 seg_prefix
= segment_prefixes
[pop
->reg
];
528 parse_operand(s1
, pop
);
529 if (!(pop
->type
& OP_EA
)) {
530 tcc_error("segment prefix must be followed by memory reference");
540 s
= 0; /* avoid warning */
542 /* optimize matching by using a lookup table (no hashing is needed
544 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
546 if (pa
->instr_type
& OPC_FARITH
) {
547 v
= opcode
- pa
->sym
;
548 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
550 } else if (pa
->instr_type
& OPC_ARITH
) {
551 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
553 s
= (opcode
- pa
->sym
) % NBWLX
;
554 if ((pa
->instr_type
& OPC_BWLX
) == OPC_WLX
)
556 /* We need to reject the xxxb opcodes that we accepted above.
557 Note that pa->sym for WLX opcodes is the 'w' token,
558 to get the 'b' token subtract one. */
559 if (((opcode
- pa
->sym
+ 1) % NBWLX
) == 0)
563 } else if (pa
->instr_type
& OPC_SHIFT
) {
564 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
566 s
= (opcode
- pa
->sym
) % NBWLX
;
567 } else if (pa
->instr_type
& OPC_TEST
) {
568 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
570 /* cmovxx is a test opcode but accepts multiple sizes.
571 TCC doesn't accept the suffixed mnemonic, instead we
572 simply force size autodetection always. */
573 if (pa
->instr_type
& OPC_WLX
)
575 } else if (pa
->instr_type
& OPC_B
) {
576 #ifdef TCC_TARGET_X86_64
577 /* Some instructions don't have the full size but only
578 bwl form. insb e.g. */
579 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
580 && !(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
583 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
585 s
= opcode
- pa
->sym
;
586 } else if (pa
->instr_type
& OPC_WLX
) {
587 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
589 s
= opcode
- pa
->sym
+ 1;
591 if (pa
->sym
!= opcode
)
594 if (pa
->nb_ops
!= nb_ops
)
596 #ifdef TCC_TARGET_X86_64
597 /* Special case for moves. Selecting the IM64->REG64 form
598 should only be done if we really have an >32bit imm64, and that
599 is hardcoded. Ignore it here. */
600 if (pa
->opcode
== 0xb0 && ops
[0].type
!= OP_IM64
601 && ops
[1].type
== OP_REG64
)
604 /* now decode and check each operand */
606 for(i
= 0; i
< nb_ops
; i
++) {
608 op1
= pa
->op_type
[i
];
612 v
= OP_IM8
| OP_IM16
| OP_IM32
;
615 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
618 v
= OP_REG16
| OP_REG32
| OP_REG64
;
621 v
= OP_IM16
| OP_IM32
;
630 if ((ops
[i
].type
& v
) == 0)
632 alltypes
|= ops
[i
].type
;
634 /* all is matching ! */
639 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
641 b
= op0_codes
[opcode
- TOK_ASM_first
];
646 } else if (opcode
<= TOK_ASM_alllast
) {
647 tcc_error("bad operand with opcode '%s'",
648 get_tok_str(opcode
, NULL
));
650 tcc_error("unknown opcode '%s'",
651 get_tok_str(opcode
, NULL
));
654 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
656 #ifdef TCC_TARGET_X86_64
657 /* XXX the autosize should rather be zero, to not have to adjust this
659 if ((pa
->instr_type
& OPC_BWLQ
) == OPC_B
)
663 for(i
= 0; s
== autosize
&& i
< nb_ops
; i
++) {
664 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
665 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
668 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
669 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
)))
672 tcc_error("cannot infer opcode suffix");
676 #ifdef TCC_TARGET_X86_64
677 /* Generate addr32 prefix if needed */
678 for(i
= 0; i
< nb_ops
; i
++) {
679 if (ops
[i
].type
& OP_EA32
) {
685 /* generate data16 prefix if needed */
686 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
688 #ifdef TCC_TARGET_X86_64
689 if (s
== 3 || (alltypes
& OP_REG64
)) {
690 /* generate REX prefix */
692 for(i
= 0; i
< nb_ops
; i
++) {
693 if (op_type
[i
] == OP_REG64
) {
694 /* If only 64bit regs are accepted in one operand
695 this is a default64 instruction without need for
701 /* XXX find better encoding for the default64 instructions. */
702 if (((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
703 && opcode
!= TOK_ASM_pushw
&& opcode
!= TOK_ASM_pushl
704 && opcode
!= TOK_ASM_pushq
&& opcode
!= TOK_ASM_popw
705 && opcode
!= TOK_ASM_popl
&& opcode
!= TOK_ASM_popq
706 && opcode
!= TOK_ASM_call
&& opcode
!= TOK_ASM_jmp
))
712 /* now generates the operation */
713 if (pa
->instr_type
& OPC_FWAIT
)
719 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
720 /* kludge for imul $im, %reg */
723 op_type
[2] = op_type
[1];
724 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
725 v
--; /* int $3 case */
727 } else if ((v
== 0x06 || v
== 0x07)) {
728 if (ops
[0].reg
>= 4) {
729 /* push/pop %fs or %gs */
730 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
732 v
+= ops
[0].reg
<< 3;
735 } else if (v
<= 0x05) {
737 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
738 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
740 v
+= ((opcode
- pa
->sym
) / 6) << 3;
742 if (pa
->instr_type
& OPC_REG
) {
743 for(i
= 0; i
< nb_ops
; i
++) {
744 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
749 /* mov $im, %reg case */
750 if (pa
->opcode
== 0xb0 && s
>= 1)
753 if (pa
->instr_type
& OPC_B
)
755 if (pa
->instr_type
& OPC_TEST
)
756 v
+= test_bits
[opcode
- pa
->sym
];
757 if (pa
->instr_type
& OPC_SHORTJMP
) {
761 /* see if we can really generate the jump with a byte offset */
765 if (sym
->r
!= cur_text_section
->sh_num
)
767 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2 - (v
>= 0xff);
768 if (jmp_disp
== (int8_t)jmp_disp
) {
769 /* OK to generate jump */
771 ops
[0].e
.v
= jmp_disp
;
772 op_type
[0] = OP_IM8S
;
775 if (pa
->instr_type
& OPC_JMP
) {
776 /* long jump will be allowed. need to modify the
783 tcc_error("invalid displacement");
792 /* search which operand will used for modrm */
794 if (pa
->instr_type
& OPC_SHIFT
) {
795 reg
= (opcode
- pa
->sym
) / NBWLX
;
798 } else if (pa
->instr_type
& OPC_ARITH
) {
799 reg
= (opcode
- pa
->sym
) / NBWLX
;
800 } else if (pa
->instr_type
& OPC_FARITH
) {
801 reg
= (opcode
- pa
->sym
) / 6;
803 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
805 if (pa
->instr_type
& OPC_MODRM
) {
806 /* first look for an ea operand */
807 for(i
= 0;i
< nb_ops
; i
++) {
808 if (op_type
[i
] & OP_EA
)
811 /* then if not found, a register or indirection (shift instructions) */
812 for(i
= 0;i
< nb_ops
; i
++) {
813 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
817 tcc_error("bad op table");
821 /* if a register is used in another operand then it is
822 used instead of group */
823 for(i
= 0;i
< nb_ops
; i
++) {
825 if (i
!= modrm_index
&&
826 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
832 asm_modrm(reg
, &ops
[modrm_index
]);
836 #ifndef TCC_TARGET_X86_64
837 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
838 /* ljmp or lcall kludge */
839 gen_expr32(&ops
[1].e
);
841 tcc_error("cannot relocate");
842 gen_le16(ops
[0].e
.v
);
846 for(i
= 0;i
< nb_ops
; i
++) {
848 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
849 /* if multiple sizes are given it means we must look
851 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
856 else if (s
== 2 || (v
& OP_IM64
) == 0)
861 if (v
& (OP_IM8
| OP_IM8S
)) {
865 } else if (v
& OP_IM16
) {
868 tcc_error("cannot relocate");
870 gen_le16(ops
[i
].e
.v
);
872 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
873 gen_disp32(&ops
[i
].e
);
875 #ifdef TCC_TARGET_X86_64
877 gen_expr64(&ops
[i
].e
);
880 gen_expr32(&ops
[i
].e
);
887 /* return the constraint priority (we allocate first the lowest
888 numbered constraints) */
889 static inline int constraint_priority(const char *str
)
893 /* we take the lowest priority */
927 tcc_error("unknown constraint '%c'", c
);
936 static const char *skip_constraint_modifiers(const char *p
)
938 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
943 #define REG_OUT_MASK 0x01
944 #define REG_IN_MASK 0x02
946 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
948 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
949 int nb_operands
, int nb_outputs
,
950 const uint8_t *clobber_regs
,
954 int sorted_op
[MAX_ASM_OPERANDS
];
955 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
957 uint8_t regs_allocated
[NB_ASM_REGS
];
960 for(i
=0;i
<nb_operands
;i
++) {
962 op
->input_index
= -1;
968 /* compute constraint priority and evaluate references to output
969 constraints if input constraints */
970 for(i
=0;i
<nb_operands
;i
++) {
972 str
= op
->constraint
;
973 str
= skip_constraint_modifiers(str
);
974 if (isnum(*str
) || *str
== '[') {
975 /* this is a reference to another constraint */
976 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
977 if ((unsigned)k
>= i
|| i
< nb_outputs
)
978 tcc_error("invalid reference in constraint %d ('%s')",
981 if (operands
[k
].input_index
>= 0)
982 tcc_error("cannot reference twice the same operand");
983 operands
[k
].input_index
= i
;
986 op
->priority
= constraint_priority(str
);
990 /* sort operands according to their priority */
991 for(i
=0;i
<nb_operands
;i
++)
993 for(i
=0;i
<nb_operands
- 1;i
++) {
994 for(j
=i
+1;j
<nb_operands
;j
++) {
995 p1
= operands
[sorted_op
[i
]].priority
;
996 p2
= operands
[sorted_op
[j
]].priority
;
999 sorted_op
[i
] = sorted_op
[j
];
1005 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1006 if (clobber_regs
[i
])
1007 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1009 regs_allocated
[i
] = 0;
1011 /* esp cannot be used */
1012 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1013 /* ebp cannot be used yet */
1014 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1016 /* allocate registers and generate corresponding asm moves */
1017 for(i
=0;i
<nb_operands
;i
++) {
1020 str
= op
->constraint
;
1021 /* no need to allocate references */
1022 if (op
->ref_index
>= 0)
1024 /* select if register is used for output, input or both */
1025 if (op
->input_index
>= 0) {
1026 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1027 } else if (j
< nb_outputs
) {
1028 reg_mask
= REG_OUT_MASK
;
1030 reg_mask
= REG_IN_MASK
;
1041 if (j
>= nb_outputs
)
1042 tcc_error("'%c' modifier can only be applied to outputs", c
);
1043 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1046 /* allocate both eax and edx */
1047 if (is_reg_allocated(TREG_XAX
) ||
1048 is_reg_allocated(TREG_XDX
))
1052 regs_allocated
[TREG_XAX
] |= reg_mask
;
1053 regs_allocated
[TREG_XDX
] |= reg_mask
;
1073 if (is_reg_allocated(reg
))
1077 /* eax, ebx, ecx or edx */
1078 for(reg
= 0; reg
< 4; reg
++) {
1079 if (!is_reg_allocated(reg
))
1084 /* any general register */
1085 for(reg
= 0; reg
< 8; reg
++) {
1086 if (!is_reg_allocated(reg
))
1091 /* now we can reload in the register */
1094 regs_allocated
[reg
] |= reg_mask
;
1097 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1103 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1108 /* nothing special to do because the operand is already in
1109 memory, except if the pointer itself is stored in a
1110 memory variable (VT_LLOCAL case) */
1111 /* XXX: fix constant case */
1112 /* if it is a reference to a memory zone, it must lie
1113 in a register, so we reserve the register in the
1114 input registers and a load will be generated
1116 if (j
< nb_outputs
|| c
== 'm') {
1117 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1118 /* any general register */
1119 for(reg
= 0; reg
< 8; reg
++) {
1120 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1125 /* now we can reload in the register */
1126 regs_allocated
[reg
] |= REG_IN_MASK
;
1133 tcc_error("asm constraint %d ('%s') could not be satisfied",
1137 /* if a reference is present for that operand, we assign it too */
1138 if (op
->input_index
>= 0) {
1139 operands
[op
->input_index
].reg
= op
->reg
;
1140 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1144 /* compute out_reg. It is used to store outputs registers to memory
1145 locations references by pointers (VT_LLOCAL case) */
1147 for(i
=0;i
<nb_operands
;i
++) {
1150 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1152 for(reg
= 0; reg
< 8; reg
++) {
1153 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1156 tcc_error("could not find free output register for reloading");
1163 /* print sorted constraints */
1165 for(i
=0;i
<nb_operands
;i
++) {
1168 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1170 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1176 printf("out_reg=%d\n", *pout_reg
);
1180 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1181 SValue
*sv
, int modifier
)
1183 int r
, reg
, size
, val
;
1187 if ((r
& VT_VALMASK
) == VT_CONST
) {
1188 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1189 cstr_ccat(add_str
, '$');
1191 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
), -1);
1192 if ((uint32_t)sv
->c
.i
!= 0) {
1193 cstr_ccat(add_str
, '+');
1199 if (modifier
== 'n')
1201 snprintf(buf
, sizeof(buf
), "%d", (int)sv
->c
.i
);
1202 cstr_cat(add_str
, buf
, -1);
1203 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1204 #ifdef TCC_TARGET_X86_64
1205 snprintf(buf
, sizeof(buf
), "%d(%%rbp)", (int)sv
->c
.i
);
1207 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", (int)sv
->c
.i
);
1209 cstr_cat(add_str
, buf
, -1);
1210 } else if (r
& VT_LVAL
) {
1211 reg
= r
& VT_VALMASK
;
1212 if (reg
>= VT_CONST
)
1213 tcc_error("internal compiler error");
1214 snprintf(buf
, sizeof(buf
), "(%%%s)",
1215 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1216 cstr_cat(add_str
, buf
, -1);
1219 reg
= r
& VT_VALMASK
;
1220 if (reg
>= VT_CONST
)
1221 tcc_error("internal compiler error");
1223 /* choose register operand size */
1224 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1226 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1228 #ifdef TCC_TARGET_X86_64
1229 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1234 if (size
== 1 && reg
>= 4)
1237 if (modifier
== 'b') {
1239 tcc_error("cannot use byte register");
1241 } else if (modifier
== 'h') {
1243 tcc_error("cannot use byte register");
1245 } else if (modifier
== 'w') {
1247 #ifdef TCC_TARGET_X86_64
1248 } else if (modifier
== 'q') {
1255 reg
= TOK_ASM_ah
+ reg
;
1258 reg
= TOK_ASM_al
+ reg
;
1261 reg
= TOK_ASM_ax
+ reg
;
1264 reg
= TOK_ASM_eax
+ reg
;
1266 #ifdef TCC_TARGET_X86_64
1268 reg
= TOK_ASM_rax
+ reg
;
1272 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1273 cstr_cat(add_str
, buf
, -1);
1277 /* generate prolog and epilog code for asm statement */
1278 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1279 int nb_outputs
, int is_output
,
1280 uint8_t *clobber_regs
,
1283 uint8_t regs_allocated
[NB_ASM_REGS
];
1286 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1288 /* mark all used registers */
1289 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1290 for(i
= 0; i
< nb_operands
;i
++) {
1293 regs_allocated
[op
->reg
] = 1;
1296 /* generate reg save code */
1297 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1299 if (regs_allocated
[reg
]) {
1304 /* generate load code */
1305 for(i
= 0; i
< nb_operands
; i
++) {
1308 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1310 /* memory reference case (for both input and
1314 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1316 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1317 /* load value in register */
1318 load(op
->reg
, op
->vt
);
1323 load(TREG_XDX
, &sv
);
1329 /* generate save code */
1330 for(i
= 0 ; i
< nb_outputs
; i
++) {
1333 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1334 if (!op
->is_memory
) {
1337 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1340 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1341 store(op
->reg
, &sv
);
1344 store(op
->reg
, op
->vt
);
1349 store(TREG_XDX
, &sv
);
1354 /* generate reg restore code */
1355 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1357 if (regs_allocated
[reg
]) {
1364 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1369 if (!strcmp(str
, "memory") ||
1372 ts
= tok_alloc(str
, strlen(str
));
1374 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1376 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1378 #ifdef TCC_TARGET_X86_64
1379 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1383 tcc_error("invalid clobber register '%s'", str
);
1385 clobber_regs
[reg
] = 1;