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 | IM64 */
87 OPT_REG
, /* REG8 | REG16 | REG32 | REG64 */
88 OPT_REGW
, /* REG16 | REG32 | REG64 */
89 OPT_IMW
, /* IM16 | IM32 | IM64 */
90 #ifdef TCC_TARGET_X86_64
91 OPT_IMNO64
, /* IM16 | IM32 */
93 /* can be ored with any OPT_xxx */
97 #define OP_REG8 (1 << OPT_REG8)
98 #define OP_REG16 (1 << OPT_REG16)
99 #define OP_REG32 (1 << OPT_REG32)
100 #define OP_MMX (1 << OPT_MMX)
101 #define OP_SSE (1 << OPT_SSE)
102 #define OP_CR (1 << OPT_CR)
103 #define OP_TR (1 << OPT_TR)
104 #define OP_DB (1 << OPT_DB)
105 #define OP_SEG (1 << OPT_SEG)
106 #define OP_ST (1 << OPT_ST)
107 #define OP_IM8 (1 << OPT_IM8)
108 #define OP_IM8S (1 << OPT_IM8S)
109 #define OP_IM16 (1 << OPT_IM16)
110 #define OP_IM32 (1 << OPT_IM32)
111 #define OP_EAX (1 << OPT_EAX)
112 #define OP_ST0 (1 << OPT_ST0)
113 #define OP_CL (1 << OPT_CL)
114 #define OP_DX (1 << OPT_DX)
115 #define OP_ADDR (1 << OPT_ADDR)
116 #define OP_INDIR (1 << OPT_INDIR)
117 #ifdef TCC_TARGET_X86_64
118 # define OP_REG64 (1 << OPT_REG64)
119 # define OP_IM64 (1 << OPT_IM64)
125 #define OP_EA 0x40000000
126 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
128 #ifdef TCC_TARGET_X86_64
129 # define OP_IM OP_IM64
130 # define TREG_XAX TREG_RAX
131 # define TREG_XCX TREG_RCX
132 # define TREG_XDX TREG_RDX
134 # define OP_IM OP_IM32
135 # define TREG_XAX TREG_EAX
136 # define TREG_XCX TREG_ECX
137 # define TREG_XDX TREG_EDX
140 typedef struct ASMInstr
{
145 uint8_t op_type
[MAX_OPERANDS
]; /* see OP_xxx */
148 typedef struct Operand
{
150 int8_t reg
; /* register, -1 if none */
151 int8_t reg2
; /* second register, -1 if none */
156 static const uint8_t reg_to_size
[9] = {
161 #ifdef TCC_TARGET_X86_64
165 0, 0, 1, 0, 2, 0, 0, 0, 3
168 #define NB_TEST_OPCODES 30
170 static const uint8_t test_bits
[NB_TEST_OPCODES
] = {
203 static const uint8_t segment_prefixes
[] = {
212 static const ASMInstr asm_instrs
[] = {
214 #define DEF_ASM_OP0(name, opcode)
215 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
216 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
217 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
218 #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 }},
219 #ifdef TCC_TARGET_X86_64
220 # include "x86_64-asm.h"
222 # include "i386-asm.h"
228 static const uint16_t op0_codes
[] = {
230 #define DEF_ASM_OP0(x, opcode) opcode,
231 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
232 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
233 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
234 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
235 #ifdef TCC_TARGET_X86_64
236 # include "x86_64-asm.h"
238 # include "i386-asm.h"
242 #ifdef PRINTF_ASM_CODE
243 void printf_asm_opcode(){
247 int nb_op_vals
, i
, j
;
249 memset(freq
, 0, sizeof(freq
));
250 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
252 for(i
=0;i
<pa
->nb_ops
;i
++) {
253 for(j
=0;j
<nb_op_vals
;j
++) {
254 if (pa
->op_type
[i
] == op_vals
[j
])
257 op_vals
[nb_op_vals
++] = pa
->op_type
[i
];
261 for(i
=0;i
<nb_op_vals
;i
++) {
263 if ((v
& (v
- 1)) != 0)
264 printf("%3d: %08x\n", i
, v
);
266 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
267 (int)sizeof(asm_instrs
), (int)sizeof(asm_instrs
) / sizeof(ASMInstr
),
268 freq
[0], freq
[1], freq
[2], freq
[3]);
272 static inline int get_reg_shift(TCCState
*s1
)
276 if (s1
->seg_size
== 16)
277 tcc_error("invalid effective address");
279 v
= asm_int_expr(s1
);
294 expect("1, 2, 4 or 8 constant");
301 static int asm_parse_reg(void)
307 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
308 reg
= tok
- TOK_ASM_eax
;
309 #ifdef TCC_TARGET_X86_64
310 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
311 reg
= tok
- TOK_ASM_rax
;
314 } else if (tok
>= TOK_ASM_ax
&& tok
<= TOK_ASM_di
) {
315 reg
= tok
- TOK_ASM_ax
;
325 static void parse_operand(TCCState
*s1
, Operand
*op
)
339 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
340 reg
= tok
- TOK_ASM_al
;
341 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
343 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
345 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
347 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
349 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
351 op
->reg
= tok
- TOK_ASM_dr0
;
352 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
354 op
->reg
= tok
- TOK_ASM_es
;
355 } else if (tok
== TOK_ASM_st
) {
361 if (tok
!= TOK_PPNUM
)
365 if ((unsigned)reg
>= 8 || p
[1] != '\0')
376 tcc_error("unknown register");
380 } else if (tok
== '$') {
388 if (op
->e
.v
== (uint8_t)op
->e
.v
)
390 if (op
->e
.v
== (int8_t)op
->e
.v
)
392 if (op
->e
.v
== (uint16_t)op
->e
.v
)
394 #ifdef TCC_TARGET_X86_64
395 if (op
->e
.v
== (uint32_t)op
->e
.v
)
400 /* address(reg,reg2,shift) with all variants */
416 /* bracketed offset expression */
428 op
->reg
= asm_parse_reg();
433 op
->reg2
= asm_parse_reg();
437 op
->shift
= get_reg_shift(s1
);
442 if (op
->reg
== -1 && op
->reg2
== -1)
448 /* XXX: unify with C code output ? */
449 ST_FUNC
void gen_expr32(ExprValue
*pe
)
451 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
454 #ifdef TCC_TARGET_X86_64
455 static void gen_expr64(ExprValue
*pe
)
457 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
461 /* XXX: unify with C code output ? */
462 static void gen_disp32(ExprValue
*pe
)
465 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
466 /* same section: we can output an absolute value. Note
467 that the TCC compiler behaves differently here because
468 it always outputs a relocation to ease (future) code
469 elimination in the linker */
470 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
472 if (sym
&& sym
->type
.t
== VT_VOID
) {
473 sym
->type
.t
= VT_FUNC
;
474 sym
->type
.ref
= NULL
;
476 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
481 static void gen_expr16(ExprValue
*pe
)
484 greloc(cur_text_section
, pe
->sym
, ind
, R_386_16
);
487 static void gen_disp16(ExprValue
*pe
)
492 if (sym
->r
== cur_text_section
->sh_num
) {
493 /* same section: we can output an absolute value. Note
494 that the TCC compiler behaves differently here because
495 it always outputs a relocation to ease (future) code
496 elimination in the linker */
497 gen_le16(pe
->v
+ sym
->jnext
- ind
- 2);
499 greloc(cur_text_section
, sym
, ind
, R_386_PC16
);
503 /* put an empty PC32 relocation */
504 put_elf_reloc(symtab_section
, cur_text_section
,
511 /* generate the modrm operand */
512 static inline void asm_modrm(int reg
, Operand
*op
)
514 int mod
, reg1
, reg2
, sib_reg1
;
516 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
517 g(0xc0 + (reg
<< 3) + op
->reg
);
518 } else if (op
->reg
== -1 && op
->reg2
== -1) {
519 /* displacement only */
521 if (tcc_state
->seg_size
== 16) {
522 g(0x06 + (reg
<< 3));
524 } else if (tcc_state
->seg_size
== 32)
527 g(0x05 + (reg
<< 3));
532 /* fist compute displacement encoding */
533 if (sib_reg1
== -1) {
536 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
538 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
543 /* compute if sib byte needed */
548 if (tcc_state
->seg_size
== 32) {
550 g(mod
+ (reg
<< 3) + reg1
);
555 reg2
= 4; /* indicate no index */
556 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
559 } else if (tcc_state
->seg_size
== 16) {
560 /* edi = 7, esi = 6 --> di = 5, si = 4 */
561 if ((reg1
== 6) || (reg1
== 7)) {
563 /* ebx = 3 --> bx = 7 */
564 } else if (reg1
== 3) {
566 /* o32 = 5 --> o16 = 6 */
567 } else if (reg1
== 5) {
569 /* sib not valid in 16-bit mode */
570 } else if (reg1
== 4) {
572 /* bp + si + offset */
573 if ((sib_reg1
== 5) && (reg2
== 6)) {
575 /* bp + di + offset */
576 } else if ((sib_reg1
== 5) && (reg2
== 7)) {
578 /* bx + si + offset */
579 } else if ((sib_reg1
== 3) && (reg2
== 6)) {
581 /* bx + di + offset */
582 } else if ((sib_reg1
== 3) && (reg2
== 7)) {
585 tcc_error("invalid effective address");
590 tcc_error("invalid register");
592 g(mod
+ (reg
<< 3) + reg1
);
598 } else if (mod
== 0x80 || op
->reg
== -1) {
600 if (tcc_state
->seg_size
== 16)
602 else if (tcc_state
->seg_size
== 32)
609 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
612 int i
, modrm_index
, reg
, v
, op1
, is_short_jmp
, seg_prefix
;
614 Operand ops
[MAX_OPERANDS
], *pop
;
615 int op_type
[3]; /* decoded op type */
617 static int a32
= 0, o32
= 0, addr32
= 0, data32
= 0;
620 /* force synthetic ';' after prefix instruction, so we can handle */
621 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
622 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
630 if (tok
== ';' || tok
== TOK_LINEFEED
)
632 if (nb_ops
>= MAX_OPERANDS
) {
633 tcc_error("incorrect number of operands");
635 parse_operand(s1
, pop
);
637 if (pop
->type
!= OP_SEG
|| seg_prefix
)
638 tcc_error("incorrect prefix");
639 seg_prefix
= segment_prefixes
[pop
->reg
];
641 parse_operand(s1
, pop
);
643 if (!(pop
->type
& OP_EA
)) {
644 tcc_error("segment prefix must be followed by memory reference");
656 s
= 0; /* avoid warning */
658 /* optimize matching by using a lookup table (no hashing is needed
660 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
662 if (pa
->instr_type
& OPC_FARITH
) {
663 v
= opcode
- pa
->sym
;
664 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
666 } else if (pa
->instr_type
& OPC_ARITH
) {
667 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
669 s
= (opcode
- pa
->sym
) % NBWLX
;
670 } else if (pa
->instr_type
& OPC_SHIFT
) {
671 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
673 s
= (opcode
- pa
->sym
) % NBWLX
;
674 } else if (pa
->instr_type
& OPC_TEST
) {
675 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
677 } else if (pa
->instr_type
& OPC_B
) {
678 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
680 s
= opcode
- pa
->sym
;
681 } else if (pa
->instr_type
& OPC_WLX
) {
682 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
684 s
= opcode
- pa
->sym
+ 1;
686 if (pa
->sym
!= opcode
)
689 if (pa
->nb_ops
!= nb_ops
)
691 /* now decode and check each operand */
692 for(i
= 0; i
< nb_ops
; i
++) {
694 op1
= pa
->op_type
[i
];
698 v
= OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
;
701 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
704 v
= OP_REG16
| OP_REG32
| OP_REG64
;
707 v
= OP_IM16
| OP_IM32
| OP_IM64
;
709 #ifdef TCC_TARGET_X86_64
711 v
= OP_IM16
| OP_IM32
;
721 if ((ops
[i
].type
& v
) == 0)
724 /* all is matching ! */
729 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
731 b
= op0_codes
[opcode
- TOK_ASM_first
];
733 if (opcode
== TOK_ASM_o32
) {
734 if (s1
->seg_size
== 32)
735 tcc_error("incorrect prefix");
738 } else if (opcode
== TOK_ASM_a32
) {
739 if (s1
->seg_size
== 32)
740 tcc_error("incorrect prefix");
749 } else if (opcode
<= TOK_ASM_alllast
) {
750 tcc_error("bad operand with opcode '%s'", get_tok_str(opcode
, NULL
));
752 tcc_error("unknown opcode '%s'",
753 get_tok_str(opcode
, NULL
));
756 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
758 for(i
= 0; s
== NBWLX
-1 && i
< nb_ops
; i
++) {
759 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
760 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
763 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
764 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
| OP_IM64
)))
767 tcc_error("cannot infer opcode suffix");
772 for(i
= 0; i
< nb_ops
; i
++) {
773 if (ops
[i
].type
& OP_REG32
) {
774 if (s1
->seg_size
== 16)
776 } else if (!(ops
[i
].type
& OP_REG32
)) {
777 if (s1
->seg_size
== 32)
783 if (s
== 1 || (pa
->instr_type
& OPC_D16
)) {
784 if (s1
->seg_size
== 32)
787 if (s1
->seg_size
== 16) {
788 if (!(pa
->instr_type
& OPC_D16
))
793 /* generate a16/a32 prefix if needed */
794 if ((a32
== 1) && (addr32
== 0))
796 /* generate o16/o32 prefix if needed */
797 if ((o32
== 1) && (data32
== 0))
802 /* generate data16 prefix if needed */
803 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
805 #ifdef TCC_TARGET_X86_64
807 /* generate REX prefix */
808 if ((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
)
809 || !(ops
[0].type
& OP_REG64
))
815 /* now generates the operation */
816 if (pa
->instr_type
& OPC_FWAIT
)
822 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
823 /* kludge for imul $im, %reg */
826 op_type
[2] = op_type
[1];
827 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
828 v
--; /* int $3 case */
830 } else if ((v
== 0x06 || v
== 0x07)) {
831 if (ops
[0].reg
>= 4) {
832 /* push/pop %fs or %gs */
833 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
835 v
+= ops
[0].reg
<< 3;
838 } else if (v
<= 0x05) {
840 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
841 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
843 v
+= ((opcode
- pa
->sym
) / 6) << 3;
845 if (pa
->instr_type
& OPC_REG
) {
846 for(i
= 0; i
< nb_ops
; i
++) {
847 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
852 /* mov $im, %reg case */
853 if (pa
->opcode
== 0xb0 && s
>= 1)
856 if (pa
->instr_type
& OPC_B
)
858 if (pa
->instr_type
& OPC_TEST
)
859 v
+= test_bits
[opcode
- pa
->sym
];
860 if (pa
->instr_type
& OPC_SHORTJMP
) {
864 /* see if we can really generate the jump with a byte offset */
868 if (sym
->r
!= cur_text_section
->sh_num
)
870 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2;
871 if (jmp_disp
== (int8_t)jmp_disp
) {
872 /* OK to generate jump */
874 ops
[0].e
.v
= jmp_disp
;
877 if (pa
->instr_type
& OPC_JMP
) {
878 /* long jump will be allowed. need to modify the
885 tcc_error("invalid displacement");
894 /* search which operand will used for modrm */
896 if (pa
->instr_type
& OPC_SHIFT
) {
897 reg
= (opcode
- pa
->sym
) / NBWLX
;
900 } else if (pa
->instr_type
& OPC_ARITH
) {
901 reg
= (opcode
- pa
->sym
) / NBWLX
;
902 } else if (pa
->instr_type
& OPC_FARITH
) {
903 reg
= (opcode
- pa
->sym
) / 6;
905 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
907 if (pa
->instr_type
& OPC_MODRM
) {
908 /* first look for an ea operand */
909 for(i
= 0;i
< nb_ops
; i
++) {
910 if (op_type
[i
] & OP_EA
)
913 /* then if not found, a register or indirection (shift instructions) */
914 for(i
= 0;i
< nb_ops
; i
++) {
915 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
919 tcc_error("bad op table");
923 /* if a register is used in another operand then it is
924 used instead of group */
925 for(i
= 0;i
< nb_ops
; i
++) {
927 if (i
!= modrm_index
&&
928 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
934 asm_modrm(reg
, &ops
[modrm_index
]);
938 #ifndef TCC_TARGET_X86_64
939 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
940 /* ljmp or lcall kludge */
942 if (s1
->seg_size
== 16 && o32
== 0)
943 gen_expr16(&ops
[1].e
);
946 gen_expr32(&ops
[1].e
);
948 tcc_error("cannot relocate");
949 gen_le16(ops
[0].e
.v
);
953 for(i
= 0;i
< nb_ops
; i
++) {
955 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
956 /* if multiple sizes are given it means we must look
958 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
963 else if (s
== 2 || (v
& OP_IM64
) == 0)
968 if (v
& (OP_IM8
| OP_IM8S
)) {
972 } else if (v
& OP_IM16
) {
974 if (s1
->seg_size
== 16)
975 gen_expr16(&ops
[i
].e
);
980 tcc_error("cannot relocate");
982 gen_le16(ops
[i
].e
.v
);
984 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
988 else if (s1
->seg_size
== 16)
989 gen_disp16(&ops
[i
].e
);
992 gen_disp32(&ops
[i
].e
);
995 if (s1
->seg_size
== 16 && !((o32
== 1) && (v
& OP_IM32
)))
996 gen_expr16(&ops
[i
].e
);
999 #ifdef TCC_TARGET_X86_64
1001 gen_expr64(&ops
[i
].e
);
1004 gen_expr32(&ops
[i
].e
);
1008 } else if (v
& (OP_REG16
| OP_REG32
)) {
1009 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1011 g(0xE0 + ops
[i
].reg
);
1014 #ifdef TCC_TARGET_X86_64
1015 } else if (v
& (OP_REG32
| OP_REG64
)) {
1016 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1018 g(0xE0 + ops
[i
].reg
);
1028 /* return the constraint priority (we allocate first the lowest
1029 numbered constraints) */
1030 static inline int constraint_priority(const char *str
)
1032 int priority
, c
, pr
;
1034 /* we take the lowest priority */
1068 tcc_error("unknown constraint '%c'", c
);
1077 static const char *skip_constraint_modifiers(const char *p
)
1079 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
1084 #define REG_OUT_MASK 0x01
1085 #define REG_IN_MASK 0x02
1087 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1089 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
1090 int nb_operands
, int nb_outputs
,
1091 const uint8_t *clobber_regs
,
1095 int sorted_op
[MAX_ASM_OPERANDS
];
1096 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
1098 uint8_t regs_allocated
[NB_ASM_REGS
];
1101 for(i
=0; i
<nb_operands
; i
++) {
1103 op
->input_index
= -1;
1109 /* compute constraint priority and evaluate references to output
1110 constraints if input constraints */
1111 for(i
=0; i
<nb_operands
; i
++) {
1113 str
= op
->constraint
;
1114 str
= skip_constraint_modifiers(str
);
1115 if (isnum(*str
) || *str
== '[') {
1116 /* this is a reference to another constraint */
1117 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
1118 if ((unsigned)k
>= i
|| i
< nb_outputs
)
1119 tcc_error("invalid reference in constraint %d ('%s')",
1122 if (operands
[k
].input_index
>= 0)
1123 tcc_error("cannot reference twice the same operand");
1124 operands
[k
].input_index
= i
;
1127 op
->priority
= constraint_priority(str
);
1131 /* sort operands according to their priority */
1132 for(i
=0;i
<nb_operands
;i
++)
1134 for(i
=0;i
<nb_operands
- 1;i
++) {
1135 for(j
=i
+1;j
<nb_operands
;j
++) {
1136 p1
= operands
[sorted_op
[i
]].priority
;
1137 p2
= operands
[sorted_op
[j
]].priority
;
1140 sorted_op
[i
] = sorted_op
[j
];
1146 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1147 if (clobber_regs
[i
])
1148 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1150 regs_allocated
[i
] = 0;
1152 /* esp cannot be used */
1153 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1154 /* ebp cannot be used yet */
1155 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1157 /* allocate registers and generate corresponding asm moves */
1158 for(i
=0;i
<nb_operands
;i
++) {
1161 str
= op
->constraint
;
1162 /* no need to allocate references */
1163 if (op
->ref_index
>= 0)
1165 /* select if register is used for output, input or both */
1166 if (op
->input_index
>= 0) {
1167 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1168 } else if (j
< nb_outputs
) {
1169 reg_mask
= REG_OUT_MASK
;
1171 reg_mask
= REG_IN_MASK
;
1182 if (j
>= nb_outputs
)
1183 tcc_error("'%c' modifier can only be applied to outputs", c
);
1184 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1187 /* allocate both eax and edx */
1188 if (is_reg_allocated(TREG_XAX
) ||
1189 is_reg_allocated(TREG_XDX
))
1193 regs_allocated
[TREG_XAX
] |= reg_mask
;
1194 regs_allocated
[TREG_XDX
] |= reg_mask
;
1214 if (is_reg_allocated(reg
))
1218 /* eax, ebx, ecx or edx */
1219 for(reg
= 0; reg
< 4; reg
++) {
1220 if (!is_reg_allocated(reg
))
1225 /* any general register */
1226 for(reg
= 0; reg
< 8; reg
++) {
1227 if (!is_reg_allocated(reg
))
1232 /* now we can reload in the register */
1235 regs_allocated
[reg
] |= reg_mask
;
1238 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1244 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1249 /* nothing special to do because the operand is already in
1250 memory, except if the pointer itself is stored in a
1251 memory variable (VT_LLOCAL case) */
1252 /* XXX: fix constant case */
1253 /* if it is a reference to a memory zone, it must lie
1254 in a register, so we reserve the register in the
1255 input registers and a load will be generated
1257 if (j
< nb_outputs
|| c
== 'm') {
1258 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1259 /* any general register */
1260 for(reg
= 0; reg
< 8; reg
++) {
1261 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1266 /* now we can reload in the register */
1267 regs_allocated
[reg
] |= REG_IN_MASK
;
1274 tcc_error("asm constraint %d ('%s') could not be satisfied",
1278 /* if a reference is present for that operand, we assign it too */
1279 if (op
->input_index
>= 0) {
1280 operands
[op
->input_index
].reg
= op
->reg
;
1281 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1285 /* compute out_reg. It is used to store outputs registers to memory
1286 locations references by pointers (VT_LLOCAL case) */
1288 for(i
=0;i
<nb_operands
;i
++) {
1291 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1293 for(reg
= 0; reg
< 8; reg
++) {
1294 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1297 tcc_error("could not find free output register for reloading");
1304 /* print sorted constraints */
1306 for(i
=0;i
<nb_operands
;i
++) {
1309 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1311 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1317 printf("out_reg=%d\n", *pout_reg
);
1321 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1322 SValue
*sv
, int modifier
)
1324 int r
, reg
, size
, val
;
1328 if ((r
& VT_VALMASK
) == VT_CONST
) {
1329 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1330 cstr_ccat(add_str
, '$');
1332 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
));
1334 cstr_ccat(add_str
, '+');
1340 if (modifier
== 'n')
1342 snprintf(buf
, sizeof(buf
), "%d", sv
->c
.i
);
1343 cstr_cat(add_str
, buf
);
1344 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1345 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", sv
->c
.i
);
1346 cstr_cat(add_str
, buf
);
1347 } else if (r
& VT_LVAL
) {
1348 reg
= r
& VT_VALMASK
;
1349 if (reg
>= VT_CONST
)
1350 tcc_error("internal compiler error");
1351 snprintf(buf
, sizeof(buf
), "(%%%s)",
1352 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1353 cstr_cat(add_str
, buf
);
1356 reg
= r
& VT_VALMASK
;
1357 if (reg
>= VT_CONST
)
1358 tcc_error("internal compiler error");
1360 /* choose register operand size */
1361 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1363 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1365 #ifdef TCC_TARGET_X86_64
1366 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1371 if (size
== 1 && reg
>= 4)
1374 if (modifier
== 'b') {
1376 tcc_error("cannot use byte register");
1378 } else if (modifier
== 'h') {
1380 tcc_error("cannot use byte register");
1382 } else if (modifier
== 'w') {
1384 #ifdef TCC_TARGET_X86_64
1385 } else if (modifier
== 'q') {
1392 reg
= TOK_ASM_ah
+ reg
;
1395 reg
= TOK_ASM_al
+ reg
;
1398 reg
= TOK_ASM_ax
+ reg
;
1401 reg
= TOK_ASM_eax
+ reg
;
1403 #ifdef TCC_TARGET_X86_64
1405 reg
= TOK_ASM_rax
+ reg
;
1409 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1410 cstr_cat(add_str
, buf
);
1414 /* generate prolog and epilog code for asm statement */
1415 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1416 int nb_outputs
, int is_output
,
1417 uint8_t *clobber_regs
,
1420 uint8_t regs_allocated
[NB_ASM_REGS
];
1423 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1425 /* mark all used registers */
1426 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1427 for(i
= 0; i
< nb_operands
;i
++) {
1430 regs_allocated
[op
->reg
] = 1;
1433 /* generate reg save code */
1434 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1436 if (regs_allocated
[reg
]) {
1438 if (tcc_state
->seg_size
== 16)
1445 /* generate load code */
1446 for(i
= 0; i
< nb_operands
; i
++) {
1449 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1451 /* memory reference case (for both input and
1455 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1457 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1458 /* load value in register */
1459 load(op
->reg
, op
->vt
);
1464 load(TREG_XDX
, &sv
);
1470 /* generate save code */
1471 for(i
= 0 ; i
< nb_outputs
; i
++) {
1474 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1475 if (!op
->is_memory
) {
1478 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1481 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1482 store(op
->reg
, &sv
);
1485 store(op
->reg
, op
->vt
);
1490 store(TREG_XDX
, &sv
);
1495 /* generate reg restore code */
1496 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1498 if (regs_allocated
[reg
]) {
1500 if (tcc_state
->seg_size
== 16)
1509 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1514 if (!strcmp(str
, "memory") ||
1517 ts
= tok_alloc(str
, strlen(str
));
1519 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1521 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1523 #ifdef TCC_TARGET_X86_64
1524 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1528 tcc_error("invalid clobber register '%s'", str
);
1530 clobber_regs
[reg
] = 1;