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)
120 # define OP_EA32 (OP_EA << 1)
127 #define OP_EA 0x40000000
128 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
130 #ifdef TCC_TARGET_X86_64
131 # define OP_IM OP_IM64
132 # define TREG_XAX TREG_RAX
133 # define TREG_XCX TREG_RCX
134 # define TREG_XDX TREG_RDX
136 # define OP_IM OP_IM32
137 # define TREG_XAX TREG_EAX
138 # define TREG_XCX TREG_ECX
139 # define TREG_XDX TREG_EDX
142 typedef struct ASMInstr
{
147 uint8_t op_type
[MAX_OPERANDS
]; /* see OP_xxx */
150 typedef struct Operand
{
152 int8_t reg
; /* register, -1 if none */
153 int8_t reg2
; /* second register, -1 if none */
158 static const uint8_t reg_to_size
[9] = {
163 #ifdef TCC_TARGET_X86_64
167 0, 0, 1, 0, 2, 0, 0, 0, 3
170 #define NB_TEST_OPCODES 30
172 static const uint8_t test_bits
[NB_TEST_OPCODES
] = {
205 static const uint8_t segment_prefixes
[] = {
214 static const ASMInstr asm_instrs
[] = {
216 #define DEF_ASM_OP0(name, opcode)
217 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
218 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
219 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
220 #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 }},
221 #ifdef TCC_TARGET_X86_64
222 # include "x86_64-asm.h"
224 # include "i386-asm.h"
230 static const uint16_t op0_codes
[] = {
232 #define DEF_ASM_OP0(x, opcode) opcode,
233 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
234 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
235 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
236 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
237 #ifdef TCC_TARGET_X86_64
238 # include "x86_64-asm.h"
240 # include "i386-asm.h"
244 static inline int get_reg_shift(TCCState
*s1
)
248 if (s1
->seg_size
== 16)
249 tcc_error("invalid effective address");
251 v
= asm_int_expr(s1
);
266 expect("1, 2, 4 or 8 constant");
273 static int asm_parse_reg(int *type
)
280 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
281 reg
= tok
- TOK_ASM_eax
;
282 #ifdef TCC_TARGET_X86_64
284 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
285 reg
= tok
- TOK_ASM_rax
;
288 } else if (tok
>= TOK_ASM_ax
&& tok
<= TOK_ASM_di
) {
289 reg
= tok
- TOK_ASM_ax
;
299 static void parse_operand(TCCState
*s1
, Operand
*op
)
313 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
314 reg
= tok
- TOK_ASM_al
;
315 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
317 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
319 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
321 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
323 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
325 op
->reg
= tok
- TOK_ASM_dr0
;
326 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
328 op
->reg
= tok
- TOK_ASM_es
;
329 } else if (tok
== TOK_ASM_st
) {
335 if (tok
!= TOK_PPNUM
)
339 if ((unsigned)reg
>= 8 || p
[1] != '\0')
350 tcc_error("unknown register");
354 } else if (tok
== '$') {
362 if (op
->e
.v
== (uint8_t)op
->e
.v
)
364 if (op
->e
.v
== (int8_t)op
->e
.v
)
366 if (op
->e
.v
== (uint16_t)op
->e
.v
)
368 #ifdef TCC_TARGET_X86_64
369 if (op
->e
.v
== (uint32_t)op
->e
.v
)
374 /* address(reg,reg2,shift) with all variants */
390 /* bracketed offset expression */
403 op
->reg
= asm_parse_reg(&type
);
408 op
->reg2
= asm_parse_reg(&type
);
412 op
->shift
= get_reg_shift(s1
);
419 if (op
->reg
== -1 && op
->reg2
== -1)
425 /* XXX: unify with C code output ? */
426 ST_FUNC
void gen_expr32(ExprValue
*pe
)
428 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
431 #ifdef TCC_TARGET_X86_64
432 static void gen_expr64(ExprValue
*pe
)
434 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
438 /* XXX: unify with C code output ? */
439 static void gen_disp32(ExprValue
*pe
)
442 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
443 /* same section: we can output an absolute value. Note
444 that the TCC compiler behaves differently here because
445 it always outputs a relocation to ease (future) code
446 elimination in the linker */
447 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
449 if (sym
&& sym
->type
.t
== VT_VOID
) {
450 sym
->type
.t
= VT_FUNC
;
451 sym
->type
.ref
= NULL
;
453 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
458 static void gen_expr16(ExprValue
*pe
)
461 greloc(cur_text_section
, pe
->sym
, ind
, R_386_16
);
464 static void gen_disp16(ExprValue
*pe
)
469 if (sym
->r
== cur_text_section
->sh_num
) {
470 /* same section: we can output an absolute value. Note
471 that the TCC compiler behaves differently here because
472 it always outputs a relocation to ease (future) code
473 elimination in the linker */
474 gen_le16(pe
->v
+ sym
->jnext
- ind
- 2);
476 greloc(cur_text_section
, sym
, ind
, R_386_PC16
);
480 /* put an empty PC32 relocation */
481 put_elf_reloc(symtab_section
, cur_text_section
,
488 /* generate the modrm operand */
489 static inline void asm_modrm(int reg
, Operand
*op
)
491 int mod
, reg1
, reg2
, sib_reg1
;
493 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
494 g(0xc0 + (reg
<< 3) + op
->reg
);
495 } else if (op
->reg
== -1 && op
->reg2
== -1) {
496 /* displacement only */
498 if (tcc_state
->seg_size
== 16) {
499 g(0x06 + (reg
<< 3));
501 } else if (tcc_state
->seg_size
== 32)
504 #ifdef TCC_TARGET_X86_64
505 g(0x04 + (reg
<< 3));
508 g(0x05 + (reg
<< 3));
514 /* fist compute displacement encoding */
515 if (sib_reg1
== -1) {
518 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
520 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
525 /* compute if sib byte needed */
530 if (tcc_state
->seg_size
== 32) {
532 g(mod
+ (reg
<< 3) + reg1
);
537 reg2
= 4; /* indicate no index */
538 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
541 } else if (tcc_state
->seg_size
== 16) {
542 /* edi = 7, esi = 6 --> di = 5, si = 4 */
543 if ((reg1
== 6) || (reg1
== 7)) {
545 /* ebx = 3 --> bx = 7 */
546 } else if (reg1
== 3) {
548 /* o32 = 5 --> o16 = 6 */
549 } else if (reg1
== 5) {
551 /* sib not valid in 16-bit mode */
552 } else if (reg1
== 4) {
554 /* bp + si + offset */
555 if ((sib_reg1
== 5) && (reg2
== 6)) {
557 /* bp + di + offset */
558 } else if ((sib_reg1
== 5) && (reg2
== 7)) {
560 /* bx + si + offset */
561 } else if ((sib_reg1
== 3) && (reg2
== 6)) {
563 /* bx + di + offset */
564 } else if ((sib_reg1
== 3) && (reg2
== 7)) {
567 tcc_error("invalid effective address");
572 tcc_error("invalid register");
574 g(mod
+ (reg
<< 3) + reg1
);
580 } else if (mod
== 0x80 || op
->reg
== -1) {
582 if (tcc_state
->seg_size
== 16)
584 else if (tcc_state
->seg_size
== 32)
591 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
594 int i
, modrm_index
, reg
, v
, op1
, is_short_jmp
, seg_prefix
;
596 Operand ops
[MAX_OPERANDS
], *pop
;
597 int op_type
[3]; /* decoded op type */
598 int alltypes
; /* OR of all operand types */
601 static int a32
= 0, o32
= 0, addr32
= 0, data32
= 0;
604 /* force synthetic ';' after prefix instruction, so we can handle */
605 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
606 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
615 if (tok
== ';' || tok
== TOK_LINEFEED
)
617 if (nb_ops
>= MAX_OPERANDS
) {
618 tcc_error("incorrect number of operands");
620 parse_operand(s1
, pop
);
622 if (pop
->type
!= OP_SEG
|| seg_prefix
)
623 tcc_error("incorrect prefix");
624 seg_prefix
= segment_prefixes
[pop
->reg
];
626 parse_operand(s1
, pop
);
628 if (!(pop
->type
& OP_EA
)) {
629 tcc_error("segment prefix must be followed by memory reference");
641 s
= 0; /* avoid warning */
643 /* optimize matching by using a lookup table (no hashing is needed
645 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
647 if (pa
->instr_type
& OPC_FARITH
) {
648 v
= opcode
- pa
->sym
;
649 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
651 } else if (pa
->instr_type
& OPC_ARITH
) {
652 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
654 s
= (opcode
- pa
->sym
) % NBWLX
;
655 } else if (pa
->instr_type
& OPC_SHIFT
) {
656 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
658 s
= (opcode
- pa
->sym
) % NBWLX
;
659 } else if (pa
->instr_type
& OPC_TEST
) {
660 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
662 } else if (pa
->instr_type
& OPC_B
) {
663 #ifdef TCC_TARGET_X86_64
664 /* Some instructions don't have the full size but only
665 bwl form. insb e.g. */
666 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
667 && !(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
670 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
672 s
= opcode
- pa
->sym
;
673 } else if (pa
->instr_type
& OPC_WLX
) {
674 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
676 s
= opcode
- pa
->sym
+ 1;
678 if (pa
->sym
!= opcode
)
681 if (pa
->nb_ops
!= nb_ops
)
683 /* now decode and check each operand */
685 for(i
= 0; i
< nb_ops
; i
++) {
687 op1
= pa
->op_type
[i
];
691 v
= OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
;
694 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
697 v
= OP_REG16
| OP_REG32
| OP_REG64
;
700 v
= OP_IM16
| OP_IM32
| OP_IM64
;
702 #ifdef TCC_TARGET_X86_64
704 v
= OP_IM16
| OP_IM32
;
714 if ((ops
[i
].type
& v
) == 0)
716 alltypes
|= ops
[i
].type
;
718 /* all is matching ! */
723 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
725 b
= op0_codes
[opcode
- TOK_ASM_first
];
727 if (opcode
== TOK_ASM_o32
) {
728 if (s1
->seg_size
== 32)
729 tcc_error("incorrect prefix");
732 } else if (opcode
== TOK_ASM_a32
) {
733 if (s1
->seg_size
== 32)
734 tcc_error("incorrect prefix");
743 } else if (opcode
<= TOK_ASM_alllast
) {
744 tcc_error("bad operand with opcode '%s'",
745 get_tok_str(opcode
, NULL
));
747 tcc_error("unknown opcode '%s'",
748 get_tok_str(opcode
, NULL
));
751 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
753 #ifdef TCC_TARGET_X86_64
754 /* XXX the autosize should rather be zero, to not have to adjust this
756 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
)
760 for(i
= 0; s
== autosize
&& i
< nb_ops
; i
++) {
761 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
762 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
765 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
766 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
| OP_IM64
)))
769 tcc_error("cannot infer opcode suffix");
774 for(i
= 0; i
< nb_ops
; i
++) {
775 if (ops
[i
].type
& OP_REG32
) {
776 if (s1
->seg_size
== 16)
778 } else if (!(ops
[i
].type
& OP_REG32
)) {
779 if (s1
->seg_size
== 32)
785 if (s
== 1 || (pa
->instr_type
& OPC_D16
)) {
786 if (s1
->seg_size
== 32)
789 if (s1
->seg_size
== 16) {
790 if (!(pa
->instr_type
& OPC_D16
))
795 /* generate a16/a32 prefix if needed */
796 if ((a32
== 1) && (addr32
== 0))
798 /* generate o16/o32 prefix if needed */
799 if ((o32
== 1) && (data32
== 0))
804 #ifdef TCC_TARGET_X86_64
805 /* Generate addr32 prefix if needed */
806 for(i
= 0; i
< nb_ops
; i
++) {
807 if (ops
[i
].type
& OP_EA32
) {
813 /* generate data16 prefix if needed */
814 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
816 #ifdef TCC_TARGET_X86_64
817 if (s
== 3 || (alltypes
& OP_REG64
)) {
818 /* generate REX prefix */
820 for(i
= 0; i
< nb_ops
; i
++) {
821 if (op_type
[i
] == OP_REG64
) {
822 /* If only 64bit regs are accepted in one operand
823 this is a default64 instruction without need for
829 /* XXX find better encoding for the default64 instructions. */
830 if (((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
831 && opcode
!= TOK_ASM_pushw
&& opcode
!= TOK_ASM_pushl
832 && opcode
!= TOK_ASM_pushq
&& opcode
!= TOK_ASM_popw
833 && opcode
!= TOK_ASM_popl
&& opcode
!= TOK_ASM_popq
834 && opcode
!= TOK_ASM_call
&& opcode
!= TOK_ASM_jmp
))
841 /* now generates the operation */
842 if (pa
->instr_type
& OPC_FWAIT
)
848 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
849 /* kludge for imul $im, %reg */
852 op_type
[2] = op_type
[1];
853 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
854 v
--; /* int $3 case */
856 } else if ((v
== 0x06 || v
== 0x07)) {
857 if (ops
[0].reg
>= 4) {
858 /* push/pop %fs or %gs */
859 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
861 v
+= ops
[0].reg
<< 3;
864 } else if (v
<= 0x05) {
866 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
867 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
869 v
+= ((opcode
- pa
->sym
) / 6) << 3;
871 if (pa
->instr_type
& OPC_REG
) {
872 for(i
= 0; i
< nb_ops
; i
++) {
873 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
878 /* mov $im, %reg case */
879 if (pa
->opcode
== 0xb0 && s
>= 1)
882 if (pa
->instr_type
& OPC_B
)
884 if (pa
->instr_type
& OPC_TEST
)
885 v
+= test_bits
[opcode
- pa
->sym
];
886 if (pa
->instr_type
& OPC_SHORTJMP
) {
890 /* see if we can really generate the jump with a byte offset */
894 if (sym
->r
!= cur_text_section
->sh_num
)
896 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2 - (v
>= 0xff);
897 if (jmp_disp
== (int8_t)jmp_disp
) {
898 /* OK to generate jump */
900 ops
[0].e
.v
= jmp_disp
;
903 if (pa
->instr_type
& OPC_JMP
) {
904 /* long jump will be allowed. need to modify the
911 tcc_error("invalid displacement");
920 /* search which operand will used for modrm */
922 if (pa
->instr_type
& OPC_SHIFT
) {
923 reg
= (opcode
- pa
->sym
) / NBWLX
;
926 } else if (pa
->instr_type
& OPC_ARITH
) {
927 reg
= (opcode
- pa
->sym
) / NBWLX
;
928 } else if (pa
->instr_type
& OPC_FARITH
) {
929 reg
= (opcode
- pa
->sym
) / 6;
931 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
933 if (pa
->instr_type
& OPC_MODRM
) {
934 /* first look for an ea operand */
935 for(i
= 0;i
< nb_ops
; i
++) {
936 if (op_type
[i
] & OP_EA
)
939 /* then if not found, a register or indirection (shift instructions) */
940 for(i
= 0;i
< nb_ops
; i
++) {
941 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
945 tcc_error("bad op table");
949 /* if a register is used in another operand then it is
950 used instead of group */
951 for(i
= 0;i
< nb_ops
; i
++) {
953 if (i
!= modrm_index
&&
954 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
960 asm_modrm(reg
, &ops
[modrm_index
]);
964 #ifndef TCC_TARGET_X86_64
965 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
966 /* ljmp or lcall kludge */
968 if (s1
->seg_size
== 16 && o32
== 0)
969 gen_expr16(&ops
[1].e
);
972 gen_expr32(&ops
[1].e
);
974 tcc_error("cannot relocate");
975 gen_le16(ops
[0].e
.v
);
979 for(i
= 0;i
< nb_ops
; i
++) {
981 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
982 /* if multiple sizes are given it means we must look
984 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
989 else if (s
== 2 || (v
& OP_IM64
) == 0)
994 if (v
& (OP_IM8
| OP_IM8S
)) {
998 } else if (v
& OP_IM16
) {
1000 if (s1
->seg_size
== 16)
1001 gen_expr16(&ops
[i
].e
);
1006 tcc_error("cannot relocate");
1008 gen_le16(ops
[i
].e
.v
);
1010 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1014 else if (s1
->seg_size
== 16)
1015 gen_disp16(&ops
[i
].e
);
1018 gen_disp32(&ops
[i
].e
);
1021 if (s1
->seg_size
== 16 && !((o32
== 1) && (v
& OP_IM32
)))
1022 gen_expr16(&ops
[i
].e
);
1025 #ifdef TCC_TARGET_X86_64
1027 gen_expr64(&ops
[i
].e
);
1030 gen_expr32(&ops
[i
].e
);
1034 } else if (v
& (OP_REG16
| OP_REG32
)) {
1035 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1037 g(0xE0 + ops
[i
].reg
);
1040 #ifdef TCC_TARGET_X86_64
1041 } else if (v
& (OP_REG32
| OP_REG64
)) {
1042 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1044 g(0xE0 + ops
[i
].reg
);
1054 /* return the constraint priority (we allocate first the lowest
1055 numbered constraints) */
1056 static inline int constraint_priority(const char *str
)
1058 int priority
, c
, pr
;
1060 /* we take the lowest priority */
1094 tcc_error("unknown constraint '%c'", c
);
1103 static const char *skip_constraint_modifiers(const char *p
)
1105 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
1110 #define REG_OUT_MASK 0x01
1111 #define REG_IN_MASK 0x02
1113 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1115 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
1116 int nb_operands
, int nb_outputs
,
1117 const uint8_t *clobber_regs
,
1121 int sorted_op
[MAX_ASM_OPERANDS
];
1122 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
1124 uint8_t regs_allocated
[NB_ASM_REGS
];
1127 for(i
=0;i
<nb_operands
;i
++) {
1129 op
->input_index
= -1;
1135 /* compute constraint priority and evaluate references to output
1136 constraints if input constraints */
1137 for(i
=0;i
<nb_operands
;i
++) {
1139 str
= op
->constraint
;
1140 str
= skip_constraint_modifiers(str
);
1141 if (isnum(*str
) || *str
== '[') {
1142 /* this is a reference to another constraint */
1143 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
1144 if ((unsigned)k
>= i
|| i
< nb_outputs
)
1145 tcc_error("invalid reference in constraint %d ('%s')",
1148 if (operands
[k
].input_index
>= 0)
1149 tcc_error("cannot reference twice the same operand");
1150 operands
[k
].input_index
= i
;
1153 op
->priority
= constraint_priority(str
);
1157 /* sort operands according to their priority */
1158 for(i
=0;i
<nb_operands
;i
++)
1160 for(i
=0;i
<nb_operands
- 1;i
++) {
1161 for(j
=i
+1;j
<nb_operands
;j
++) {
1162 p1
= operands
[sorted_op
[i
]].priority
;
1163 p2
= operands
[sorted_op
[j
]].priority
;
1166 sorted_op
[i
] = sorted_op
[j
];
1172 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1173 if (clobber_regs
[i
])
1174 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1176 regs_allocated
[i
] = 0;
1178 /* esp cannot be used */
1179 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1180 /* ebp cannot be used yet */
1181 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1183 /* allocate registers and generate corresponding asm moves */
1184 for(i
=0;i
<nb_operands
;i
++) {
1187 str
= op
->constraint
;
1188 /* no need to allocate references */
1189 if (op
->ref_index
>= 0)
1191 /* select if register is used for output, input or both */
1192 if (op
->input_index
>= 0) {
1193 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1194 } else if (j
< nb_outputs
) {
1195 reg_mask
= REG_OUT_MASK
;
1197 reg_mask
= REG_IN_MASK
;
1208 if (j
>= nb_outputs
)
1209 tcc_error("'%c' modifier can only be applied to outputs", c
);
1210 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1213 /* allocate both eax and edx */
1214 if (is_reg_allocated(TREG_XAX
) ||
1215 is_reg_allocated(TREG_XDX
))
1219 regs_allocated
[TREG_XAX
] |= reg_mask
;
1220 regs_allocated
[TREG_XDX
] |= reg_mask
;
1240 if (is_reg_allocated(reg
))
1244 /* eax, ebx, ecx or edx */
1245 for(reg
= 0; reg
< 4; reg
++) {
1246 if (!is_reg_allocated(reg
))
1251 /* any general register */
1252 for(reg
= 0; reg
< 8; reg
++) {
1253 if (!is_reg_allocated(reg
))
1258 /* now we can reload in the register */
1261 regs_allocated
[reg
] |= reg_mask
;
1264 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1270 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1275 /* nothing special to do because the operand is already in
1276 memory, except if the pointer itself is stored in a
1277 memory variable (VT_LLOCAL case) */
1278 /* XXX: fix constant case */
1279 /* if it is a reference to a memory zone, it must lie
1280 in a register, so we reserve the register in the
1281 input registers and a load will be generated
1283 if (j
< nb_outputs
|| c
== 'm') {
1284 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1285 /* any general register */
1286 for(reg
= 0; reg
< 8; reg
++) {
1287 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1292 /* now we can reload in the register */
1293 regs_allocated
[reg
] |= REG_IN_MASK
;
1300 tcc_error("asm constraint %d ('%s') could not be satisfied",
1304 /* if a reference is present for that operand, we assign it too */
1305 if (op
->input_index
>= 0) {
1306 operands
[op
->input_index
].reg
= op
->reg
;
1307 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1311 /* compute out_reg. It is used to store outputs registers to memory
1312 locations references by pointers (VT_LLOCAL case) */
1314 for(i
=0;i
<nb_operands
;i
++) {
1317 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1319 for(reg
= 0; reg
< 8; reg
++) {
1320 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1323 tcc_error("could not find free output register for reloading");
1330 /* print sorted constraints */
1332 for(i
=0;i
<nb_operands
;i
++) {
1335 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1337 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1343 printf("out_reg=%d\n", *pout_reg
);
1347 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1348 SValue
*sv
, int modifier
)
1350 int r
, reg
, size
, val
;
1354 if ((r
& VT_VALMASK
) == VT_CONST
) {
1355 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1356 cstr_ccat(add_str
, '$');
1358 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
), -1);
1359 if ((uint32_t)sv
->c
.i
!= 0) {
1360 cstr_ccat(add_str
, '+');
1366 if (modifier
== 'n')
1368 snprintf(buf
, sizeof(buf
), "%d", (int)sv
->c
.i
);
1369 cstr_cat(add_str
, buf
, -1);
1370 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1371 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", (int)sv
->c
.i
);
1372 cstr_cat(add_str
, buf
, -1);
1373 } else if (r
& VT_LVAL
) {
1374 reg
= r
& VT_VALMASK
;
1375 if (reg
>= VT_CONST
)
1376 tcc_error("internal compiler error");
1377 snprintf(buf
, sizeof(buf
), "(%%%s)",
1378 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1379 cstr_cat(add_str
, buf
, -1);
1382 reg
= r
& VT_VALMASK
;
1383 if (reg
>= VT_CONST
)
1384 tcc_error("internal compiler error");
1386 /* choose register operand size */
1387 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1389 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1391 #ifdef TCC_TARGET_X86_64
1392 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1397 if (size
== 1 && reg
>= 4)
1400 if (modifier
== 'b') {
1402 tcc_error("cannot use byte register");
1404 } else if (modifier
== 'h') {
1406 tcc_error("cannot use byte register");
1408 } else if (modifier
== 'w') {
1410 #ifdef TCC_TARGET_X86_64
1411 } else if (modifier
== 'q') {
1418 reg
= TOK_ASM_ah
+ reg
;
1421 reg
= TOK_ASM_al
+ reg
;
1424 reg
= TOK_ASM_ax
+ reg
;
1427 reg
= TOK_ASM_eax
+ reg
;
1429 #ifdef TCC_TARGET_X86_64
1431 reg
= TOK_ASM_rax
+ reg
;
1435 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1436 cstr_cat(add_str
, buf
, -1);
1440 /* generate prolog and epilog code for asm statement */
1441 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1442 int nb_outputs
, int is_output
,
1443 uint8_t *clobber_regs
,
1446 uint8_t regs_allocated
[NB_ASM_REGS
];
1449 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1451 /* mark all used registers */
1452 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1453 for(i
= 0; i
< nb_operands
;i
++) {
1456 regs_allocated
[op
->reg
] = 1;
1459 /* generate reg save code */
1460 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1462 if (regs_allocated
[reg
]) {
1464 if (tcc_state
->seg_size
== 16)
1471 /* generate load code */
1472 for(i
= 0; i
< nb_operands
; i
++) {
1475 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1477 /* memory reference case (for both input and
1481 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1483 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1484 /* load value in register */
1485 load(op
->reg
, op
->vt
);
1490 load(TREG_XDX
, &sv
);
1496 /* generate save code */
1497 for(i
= 0 ; i
< nb_outputs
; i
++) {
1500 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1501 if (!op
->is_memory
) {
1504 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1507 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1508 store(op
->reg
, &sv
);
1511 store(op
->reg
, op
->vt
);
1516 store(TREG_XDX
, &sv
);
1521 /* generate reg restore code */
1522 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1524 if (regs_allocated
[reg
]) {
1526 if (tcc_state
->seg_size
== 16)
1535 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1540 if (!strcmp(str
, "memory") ||
1543 ts
= tok_alloc(str
, strlen(str
));
1545 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1547 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1549 #ifdef TCC_TARGET_X86_64
1550 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1554 tcc_error("invalid clobber register '%s'", str
);
1556 clobber_regs
[reg
] = 1;