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 static inline int get_reg_shift(TCCState
*s1
)
246 if (s1
->seg_size
== 16)
247 tcc_error("invalid effective address");
249 v
= asm_int_expr(s1
);
264 expect("1, 2, 4 or 8 constant");
271 static int asm_parse_reg(void)
277 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
278 reg
= tok
- TOK_ASM_eax
;
279 #ifdef TCC_TARGET_X86_64
280 } else if (tok
>= TOK_ASM_rax
&& tok
<= TOK_ASM_rdi
) {
281 reg
= tok
- TOK_ASM_rax
;
284 } else if (tok
>= TOK_ASM_ax
&& tok
<= TOK_ASM_di
) {
285 reg
= tok
- TOK_ASM_ax
;
295 static void parse_operand(TCCState
*s1
, Operand
*op
)
309 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
310 reg
= tok
- TOK_ASM_al
;
311 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
313 if ((op
->type
& OP_REG
) && op
->reg
== TREG_XAX
)
315 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_XCX
)
317 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_XDX
)
319 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
321 op
->reg
= tok
- TOK_ASM_dr0
;
322 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
324 op
->reg
= tok
- TOK_ASM_es
;
325 } else if (tok
== TOK_ASM_st
) {
331 if (tok
!= TOK_PPNUM
)
335 if ((unsigned)reg
>= 8 || p
[1] != '\0')
346 tcc_error("unknown register");
350 } else if (tok
== '$') {
358 if (op
->e
.v
== (uint8_t)op
->e
.v
)
360 if (op
->e
.v
== (int8_t)op
->e
.v
)
362 if (op
->e
.v
== (uint16_t)op
->e
.v
)
364 #ifdef TCC_TARGET_X86_64
365 if (op
->e
.v
== (uint32_t)op
->e
.v
)
370 /* address(reg,reg2,shift) with all variants */
386 /* bracketed offset expression */
398 op
->reg
= asm_parse_reg();
403 op
->reg2
= asm_parse_reg();
407 op
->shift
= get_reg_shift(s1
);
412 if (op
->reg
== -1 && op
->reg2
== -1)
418 /* XXX: unify with C code output ? */
419 ST_FUNC
void gen_expr32(ExprValue
*pe
)
421 gen_addr32(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
424 #ifdef TCC_TARGET_X86_64
425 static void gen_expr64(ExprValue
*pe
)
427 gen_addr64(pe
->sym
? VT_SYM
: 0, pe
->sym
, pe
->v
);
431 /* XXX: unify with C code output ? */
432 static void gen_disp32(ExprValue
*pe
)
435 if (sym
&& sym
->r
== cur_text_section
->sh_num
) {
436 /* same section: we can output an absolute value. Note
437 that the TCC compiler behaves differently here because
438 it always outputs a relocation to ease (future) code
439 elimination in the linker */
440 gen_le32(pe
->v
+ sym
->jnext
- ind
- 4);
442 if (sym
&& sym
->type
.t
== VT_VOID
) {
443 sym
->type
.t
= VT_FUNC
;
444 sym
->type
.ref
= NULL
;
446 gen_addrpc32(VT_SYM
, sym
, pe
->v
);
451 static void gen_expr16(ExprValue
*pe
)
454 greloc(cur_text_section
, pe
->sym
, ind
, R_386_16
);
457 static void gen_disp16(ExprValue
*pe
)
462 if (sym
->r
== cur_text_section
->sh_num
) {
463 /* same section: we can output an absolute value. Note
464 that the TCC compiler behaves differently here because
465 it always outputs a relocation to ease (future) code
466 elimination in the linker */
467 gen_le16(pe
->v
+ sym
->jnext
- ind
- 2);
469 greloc(cur_text_section
, sym
, ind
, R_386_PC16
);
473 /* put an empty PC32 relocation */
474 put_elf_reloc(symtab_section
, cur_text_section
,
481 /* generate the modrm operand */
482 static inline void asm_modrm(int reg
, Operand
*op
)
484 int mod
, reg1
, reg2
, sib_reg1
;
486 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
487 g(0xc0 + (reg
<< 3) + op
->reg
);
488 } else if (op
->reg
== -1 && op
->reg2
== -1) {
489 /* displacement only */
491 if (tcc_state
->seg_size
== 16) {
492 g(0x06 + (reg
<< 3));
494 } else if (tcc_state
->seg_size
== 32)
497 g(0x05 + (reg
<< 3));
502 /* fist compute displacement encoding */
503 if (sib_reg1
== -1) {
506 } else if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
508 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
513 /* compute if sib byte needed */
518 if (tcc_state
->seg_size
== 32) {
520 g(mod
+ (reg
<< 3) + reg1
);
525 reg2
= 4; /* indicate no index */
526 g((op
->shift
<< 6) + (reg2
<< 3) + sib_reg1
);
529 } else if (tcc_state
->seg_size
== 16) {
530 /* edi = 7, esi = 6 --> di = 5, si = 4 */
531 if ((reg1
== 6) || (reg1
== 7)) {
533 /* ebx = 3 --> bx = 7 */
534 } else if (reg1
== 3) {
536 /* o32 = 5 --> o16 = 6 */
537 } else if (reg1
== 5) {
539 /* sib not valid in 16-bit mode */
540 } else if (reg1
== 4) {
542 /* bp + si + offset */
543 if ((sib_reg1
== 5) && (reg2
== 6)) {
545 /* bp + di + offset */
546 } else if ((sib_reg1
== 5) && (reg2
== 7)) {
548 /* bx + si + offset */
549 } else if ((sib_reg1
== 3) && (reg2
== 6)) {
551 /* bx + di + offset */
552 } else if ((sib_reg1
== 3) && (reg2
== 7)) {
555 tcc_error("invalid effective address");
560 tcc_error("invalid register");
562 g(mod
+ (reg
<< 3) + reg1
);
568 } else if (mod
== 0x80 || op
->reg
== -1) {
570 if (tcc_state
->seg_size
== 16)
572 else if (tcc_state
->seg_size
== 32)
579 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
)
582 int i
, modrm_index
, reg
, v
, op1
, is_short_jmp
, seg_prefix
;
584 Operand ops
[MAX_OPERANDS
], *pop
;
585 int op_type
[3]; /* decoded op type */
587 static int a32
= 0, o32
= 0, addr32
= 0, data32
= 0;
590 /* force synthetic ';' after prefix instruction, so we can handle */
591 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
592 if (opcode
>= TOK_ASM_wait
&& opcode
<= TOK_ASM_repnz
)
600 if (tok
== ';' || tok
== TOK_LINEFEED
)
602 if (nb_ops
>= MAX_OPERANDS
) {
603 tcc_error("incorrect number of operands");
605 parse_operand(s1
, pop
);
607 if (pop
->type
!= OP_SEG
|| seg_prefix
)
608 tcc_error("incorrect prefix");
609 seg_prefix
= segment_prefixes
[pop
->reg
];
611 parse_operand(s1
, pop
);
613 if (!(pop
->type
& OP_EA
)) {
614 tcc_error("segment prefix must be followed by memory reference");
626 s
= 0; /* avoid warning */
628 /* optimize matching by using a lookup table (no hashing is needed
630 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
632 if (pa
->instr_type
& OPC_FARITH
) {
633 v
= opcode
- pa
->sym
;
634 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
636 } else if (pa
->instr_type
& OPC_ARITH
) {
637 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8*NBWLX
))
639 s
= (opcode
- pa
->sym
) % NBWLX
;
640 } else if (pa
->instr_type
& OPC_SHIFT
) {
641 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7*NBWLX
))
643 s
= (opcode
- pa
->sym
) % NBWLX
;
644 } else if (pa
->instr_type
& OPC_TEST
) {
645 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
647 } else if (pa
->instr_type
& OPC_B
) {
648 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
650 s
= opcode
- pa
->sym
;
651 } else if (pa
->instr_type
& OPC_WLX
) {
652 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
654 s
= opcode
- pa
->sym
+ 1;
656 if (pa
->sym
!= opcode
)
659 if (pa
->nb_ops
!= nb_ops
)
661 /* now decode and check each operand */
662 for(i
= 0; i
< nb_ops
; i
++) {
664 op1
= pa
->op_type
[i
];
668 v
= OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
;
671 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
674 v
= OP_REG16
| OP_REG32
| OP_REG64
;
677 v
= OP_IM16
| OP_IM32
| OP_IM64
;
679 #ifdef TCC_TARGET_X86_64
681 v
= OP_IM16
| OP_IM32
;
691 if ((ops
[i
].type
& v
) == 0)
694 /* all is matching ! */
699 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
701 b
= op0_codes
[opcode
- TOK_ASM_first
];
703 if (opcode
== TOK_ASM_o32
) {
704 if (s1
->seg_size
== 32)
705 tcc_error("incorrect prefix");
708 } else if (opcode
== TOK_ASM_a32
) {
709 if (s1
->seg_size
== 32)
710 tcc_error("incorrect prefix");
719 } else if (opcode
<= TOK_ASM_alllast
) {
720 tcc_error("bad operand with opcode '%s'",
721 get_tok_str(opcode
, NULL
));
723 tcc_error("unknown opcode '%s'",
724 get_tok_str(opcode
, NULL
));
727 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
729 for(i
= 0; s
== NBWLX
-1 && i
< nb_ops
; i
++) {
730 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
731 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
734 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
735 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
| OP_IM64
)))
738 tcc_error("cannot infer opcode suffix");
743 for(i
= 0; i
< nb_ops
; i
++) {
744 if (ops
[i
].type
& OP_REG32
) {
745 if (s1
->seg_size
== 16)
747 } else if (!(ops
[i
].type
& OP_REG32
)) {
748 if (s1
->seg_size
== 32)
754 if (s
== 1 || (pa
->instr_type
& OPC_D16
)) {
755 if (s1
->seg_size
== 32)
758 if (s1
->seg_size
== 16) {
759 if (!(pa
->instr_type
& OPC_D16
))
764 /* generate a16/a32 prefix if needed */
765 if ((a32
== 1) && (addr32
== 0))
767 /* generate o16/o32 prefix if needed */
768 if ((o32
== 1) && (data32
== 0))
773 /* generate data16 prefix if needed */
774 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
776 #ifdef TCC_TARGET_X86_64
778 /* generate REX prefix */
779 if ((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
)
780 || !(ops
[0].type
& OP_REG64
))
786 /* now generates the operation */
787 if (pa
->instr_type
& OPC_FWAIT
)
793 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
794 /* kludge for imul $im, %reg */
797 op_type
[2] = op_type
[1];
798 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
799 v
--; /* int $3 case */
801 } else if ((v
== 0x06 || v
== 0x07)) {
802 if (ops
[0].reg
>= 4) {
803 /* push/pop %fs or %gs */
804 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
806 v
+= ops
[0].reg
<< 3;
809 } else if (v
<= 0x05) {
811 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
812 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
814 v
+= ((opcode
- pa
->sym
) / 6) << 3;
816 if (pa
->instr_type
& OPC_REG
) {
817 for(i
= 0; i
< nb_ops
; i
++) {
818 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
823 /* mov $im, %reg case */
824 if (pa
->opcode
== 0xb0 && s
>= 1)
827 if (pa
->instr_type
& OPC_B
)
829 if (pa
->instr_type
& OPC_TEST
)
830 v
+= test_bits
[opcode
- pa
->sym
];
831 if (pa
->instr_type
& OPC_SHORTJMP
) {
835 /* see if we can really generate the jump with a byte offset */
839 if (sym
->r
!= cur_text_section
->sh_num
)
841 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2;
842 if (jmp_disp
== (int8_t)jmp_disp
) {
843 /* OK to generate jump */
845 ops
[0].e
.v
= jmp_disp
;
848 if (pa
->instr_type
& OPC_JMP
) {
849 /* long jump will be allowed. need to modify the
856 tcc_error("invalid displacement");
865 /* search which operand will used for modrm */
867 if (pa
->instr_type
& OPC_SHIFT
) {
868 reg
= (opcode
- pa
->sym
) / NBWLX
;
871 } else if (pa
->instr_type
& OPC_ARITH
) {
872 reg
= (opcode
- pa
->sym
) / NBWLX
;
873 } else if (pa
->instr_type
& OPC_FARITH
) {
874 reg
= (opcode
- pa
->sym
) / 6;
876 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
878 if (pa
->instr_type
& OPC_MODRM
) {
879 /* first look for an ea operand */
880 for(i
= 0;i
< nb_ops
; i
++) {
881 if (op_type
[i
] & OP_EA
)
884 /* then if not found, a register or indirection (shift instructions) */
885 for(i
= 0;i
< nb_ops
; i
++) {
886 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
890 tcc_error("bad op table");
894 /* if a register is used in another operand then it is
895 used instead of group */
896 for(i
= 0;i
< nb_ops
; i
++) {
898 if (i
!= modrm_index
&&
899 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
905 asm_modrm(reg
, &ops
[modrm_index
]);
909 #ifndef TCC_TARGET_X86_64
910 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
911 /* ljmp or lcall kludge */
913 if (s1
->seg_size
== 16 && o32
== 0)
914 gen_expr16(&ops
[1].e
);
917 gen_expr32(&ops
[1].e
);
919 tcc_error("cannot relocate");
920 gen_le16(ops
[0].e
.v
);
924 for(i
= 0;i
< nb_ops
; i
++) {
926 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
927 /* if multiple sizes are given it means we must look
929 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
934 else if (s
== 2 || (v
& OP_IM64
) == 0)
939 if (v
& (OP_IM8
| OP_IM8S
)) {
943 } else if (v
& OP_IM16
) {
945 if (s1
->seg_size
== 16)
946 gen_expr16(&ops
[i
].e
);
951 tcc_error("cannot relocate");
953 gen_le16(ops
[i
].e
.v
);
955 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
959 else if (s1
->seg_size
== 16)
960 gen_disp16(&ops
[i
].e
);
963 gen_disp32(&ops
[i
].e
);
966 if (s1
->seg_size
== 16 && !((o32
== 1) && (v
& OP_IM32
)))
967 gen_expr16(&ops
[i
].e
);
970 #ifdef TCC_TARGET_X86_64
972 gen_expr64(&ops
[i
].e
);
975 gen_expr32(&ops
[i
].e
);
979 } else if (v
& (OP_REG16
| OP_REG32
)) {
980 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
982 g(0xE0 + ops
[i
].reg
);
985 #ifdef TCC_TARGET_X86_64
986 } else if (v
& (OP_REG32
| OP_REG64
)) {
987 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
989 g(0xE0 + ops
[i
].reg
);
999 /* return the constraint priority (we allocate first the lowest
1000 numbered constraints) */
1001 static inline int constraint_priority(const char *str
)
1003 int priority
, c
, pr
;
1005 /* we take the lowest priority */
1039 tcc_error("unknown constraint '%c'", c
);
1048 static const char *skip_constraint_modifiers(const char *p
)
1050 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
1055 #define REG_OUT_MASK 0x01
1056 #define REG_IN_MASK 0x02
1058 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1060 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
1061 int nb_operands
, int nb_outputs
,
1062 const uint8_t *clobber_regs
,
1066 int sorted_op
[MAX_ASM_OPERANDS
];
1067 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
1069 uint8_t regs_allocated
[NB_ASM_REGS
];
1072 for(i
=0;i
<nb_operands
;i
++) {
1074 op
->input_index
= -1;
1080 /* compute constraint priority and evaluate references to output
1081 constraints if input constraints */
1082 for(i
=0;i
<nb_operands
;i
++) {
1084 str
= op
->constraint
;
1085 str
= skip_constraint_modifiers(str
);
1086 if (isnum(*str
) || *str
== '[') {
1087 /* this is a reference to another constraint */
1088 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
1089 if ((unsigned)k
>= i
|| i
< nb_outputs
)
1090 tcc_error("invalid reference in constraint %d ('%s')",
1093 if (operands
[k
].input_index
>= 0)
1094 tcc_error("cannot reference twice the same operand");
1095 operands
[k
].input_index
= i
;
1098 op
->priority
= constraint_priority(str
);
1102 /* sort operands according to their priority */
1103 for(i
=0;i
<nb_operands
;i
++)
1105 for(i
=0;i
<nb_operands
- 1;i
++) {
1106 for(j
=i
+1;j
<nb_operands
;j
++) {
1107 p1
= operands
[sorted_op
[i
]].priority
;
1108 p2
= operands
[sorted_op
[j
]].priority
;
1111 sorted_op
[i
] = sorted_op
[j
];
1117 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1118 if (clobber_regs
[i
])
1119 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1121 regs_allocated
[i
] = 0;
1123 /* esp cannot be used */
1124 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1125 /* ebp cannot be used yet */
1126 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1128 /* allocate registers and generate corresponding asm moves */
1129 for(i
=0;i
<nb_operands
;i
++) {
1132 str
= op
->constraint
;
1133 /* no need to allocate references */
1134 if (op
->ref_index
>= 0)
1136 /* select if register is used for output, input or both */
1137 if (op
->input_index
>= 0) {
1138 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1139 } else if (j
< nb_outputs
) {
1140 reg_mask
= REG_OUT_MASK
;
1142 reg_mask
= REG_IN_MASK
;
1153 if (j
>= nb_outputs
)
1154 tcc_error("'%c' modifier can only be applied to outputs", c
);
1155 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1158 /* allocate both eax and edx */
1159 if (is_reg_allocated(TREG_XAX
) ||
1160 is_reg_allocated(TREG_XDX
))
1164 regs_allocated
[TREG_XAX
] |= reg_mask
;
1165 regs_allocated
[TREG_XDX
] |= reg_mask
;
1185 if (is_reg_allocated(reg
))
1189 /* eax, ebx, ecx or edx */
1190 for(reg
= 0; reg
< 4; reg
++) {
1191 if (!is_reg_allocated(reg
))
1196 /* any general register */
1197 for(reg
= 0; reg
< 8; reg
++) {
1198 if (!is_reg_allocated(reg
))
1203 /* now we can reload in the register */
1206 regs_allocated
[reg
] |= reg_mask
;
1209 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1215 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1220 /* nothing special to do because the operand is already in
1221 memory, except if the pointer itself is stored in a
1222 memory variable (VT_LLOCAL case) */
1223 /* XXX: fix constant case */
1224 /* if it is a reference to a memory zone, it must lie
1225 in a register, so we reserve the register in the
1226 input registers and a load will be generated
1228 if (j
< nb_outputs
|| c
== 'm') {
1229 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1230 /* any general register */
1231 for(reg
= 0; reg
< 8; reg
++) {
1232 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1237 /* now we can reload in the register */
1238 regs_allocated
[reg
] |= REG_IN_MASK
;
1245 tcc_error("asm constraint %d ('%s') could not be satisfied",
1249 /* if a reference is present for that operand, we assign it too */
1250 if (op
->input_index
>= 0) {
1251 operands
[op
->input_index
].reg
= op
->reg
;
1252 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1256 /* compute out_reg. It is used to store outputs registers to memory
1257 locations references by pointers (VT_LLOCAL case) */
1259 for(i
=0;i
<nb_operands
;i
++) {
1262 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1264 for(reg
= 0; reg
< 8; reg
++) {
1265 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1268 tcc_error("could not find free output register for reloading");
1275 /* print sorted constraints */
1277 for(i
=0;i
<nb_operands
;i
++) {
1280 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1282 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1288 printf("out_reg=%d\n", *pout_reg
);
1292 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1293 SValue
*sv
, int modifier
)
1295 int r
, reg
, size
, val
;
1299 if ((r
& VT_VALMASK
) == VT_CONST
) {
1300 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1301 cstr_ccat(add_str
, '$');
1303 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
));
1305 cstr_ccat(add_str
, '+');
1311 if (modifier
== 'n')
1313 snprintf(buf
, sizeof(buf
), "%d", sv
->c
.i
);
1314 cstr_cat(add_str
, buf
);
1315 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1316 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", sv
->c
.i
);
1317 cstr_cat(add_str
, buf
);
1318 } else if (r
& VT_LVAL
) {
1319 reg
= r
& VT_VALMASK
;
1320 if (reg
>= VT_CONST
)
1321 tcc_error("internal compiler error");
1322 snprintf(buf
, sizeof(buf
), "(%%%s)",
1323 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1324 cstr_cat(add_str
, buf
);
1327 reg
= r
& VT_VALMASK
;
1328 if (reg
>= VT_CONST
)
1329 tcc_error("internal compiler error");
1331 /* choose register operand size */
1332 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1334 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1336 #ifdef TCC_TARGET_X86_64
1337 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1342 if (size
== 1 && reg
>= 4)
1345 if (modifier
== 'b') {
1347 tcc_error("cannot use byte register");
1349 } else if (modifier
== 'h') {
1351 tcc_error("cannot use byte register");
1353 } else if (modifier
== 'w') {
1355 #ifdef TCC_TARGET_X86_64
1356 } else if (modifier
== 'q') {
1363 reg
= TOK_ASM_ah
+ reg
;
1366 reg
= TOK_ASM_al
+ reg
;
1369 reg
= TOK_ASM_ax
+ reg
;
1372 reg
= TOK_ASM_eax
+ reg
;
1374 #ifdef TCC_TARGET_X86_64
1376 reg
= TOK_ASM_rax
+ reg
;
1380 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1381 cstr_cat(add_str
, buf
);
1385 /* generate prolog and epilog code for asm statment */
1386 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1387 int nb_outputs
, int is_output
,
1388 uint8_t *clobber_regs
,
1391 uint8_t regs_allocated
[NB_ASM_REGS
];
1394 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1396 /* mark all used registers */
1397 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1398 for(i
= 0; i
< nb_operands
;i
++) {
1401 regs_allocated
[op
->reg
] = 1;
1404 /* generate reg save code */
1405 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1407 if (regs_allocated
[reg
]) {
1409 if (tcc_state
->seg_size
== 16)
1416 /* generate load code */
1417 for(i
= 0; i
< nb_operands
; i
++) {
1420 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1422 /* memory reference case (for both input and
1426 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1428 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1429 /* load value in register */
1430 load(op
->reg
, op
->vt
);
1435 load(TREG_XDX
, &sv
);
1441 /* generate save code */
1442 for(i
= 0 ; i
< nb_outputs
; i
++) {
1445 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1446 if (!op
->is_memory
) {
1449 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1452 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1453 store(op
->reg
, &sv
);
1456 store(op
->reg
, op
->vt
);
1461 store(TREG_XDX
, &sv
);
1466 /* generate reg restore code */
1467 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1469 if (regs_allocated
[reg
]) {
1471 if (tcc_state
->seg_size
== 16)
1480 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1485 if (!strcmp(str
, "memory") ||
1488 ts
= tok_alloc(str
, strlen(str
));
1490 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1492 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1494 #ifdef TCC_TARGET_X86_64
1495 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1499 tcc_error("invalid clobber register '%s'", str
);
1501 clobber_regs
[reg
] = 1;