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 /* cmovxx is a test opcode but accepts multiple sizes.
663 TCC doesn't accept the suffixed mnemonic, instead we
664 simply force size autodetection always. */
665 if (pa
->instr_type
& OPC_WLX
)
667 } else if (pa
->instr_type
& OPC_B
) {
668 #ifdef TCC_TARGET_X86_64
669 /* Some instructions don't have the full size but only
670 bwl form. insb e.g. */
671 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
672 && !(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
675 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
))
677 s
= opcode
- pa
->sym
;
678 } else if (pa
->instr_type
& OPC_WLX
) {
679 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NBWLX
-1))
681 s
= opcode
- pa
->sym
+ 1;
683 if (pa
->sym
!= opcode
)
686 if (pa
->nb_ops
!= nb_ops
)
688 /* now decode and check each operand */
690 for(i
= 0; i
< nb_ops
; i
++) {
692 op1
= pa
->op_type
[i
];
696 v
= OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
;
699 v
= OP_REG8
| OP_REG16
| OP_REG32
| OP_REG64
;
702 v
= OP_REG16
| OP_REG32
| OP_REG64
;
705 v
= OP_IM16
| OP_IM32
| OP_IM64
;
707 #ifdef TCC_TARGET_X86_64
709 v
= OP_IM16
| OP_IM32
;
719 if ((ops
[i
].type
& v
) == 0)
721 alltypes
|= ops
[i
].type
;
723 /* all is matching ! */
728 if (opcode
>= TOK_ASM_first
&& opcode
<= TOK_ASM_last
) {
730 b
= op0_codes
[opcode
- TOK_ASM_first
];
732 if (opcode
== TOK_ASM_o32
) {
733 if (s1
->seg_size
== 32)
734 tcc_error("incorrect prefix");
737 } else if (opcode
== TOK_ASM_a32
) {
738 if (s1
->seg_size
== 32)
739 tcc_error("incorrect prefix");
748 } else if (opcode
<= TOK_ASM_alllast
) {
749 tcc_error("bad operand with opcode '%s'",
750 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 #ifdef TCC_TARGET_X86_64
759 /* XXX the autosize should rather be zero, to not have to adjust this
761 if ((pa
->instr_type
& OPC_WLQ
) != OPC_WLQ
)
765 for(i
= 0; s
== autosize
&& i
< nb_ops
; i
++) {
766 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
767 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
770 if ((opcode
== TOK_ASM_push
|| opcode
== TOK_ASM_pop
) &&
771 (ops
[0].type
& (OP_SEG
| OP_IM8S
| OP_IM32
| OP_IM64
)))
774 tcc_error("cannot infer opcode suffix");
779 for(i
= 0; i
< nb_ops
; i
++) {
780 if (ops
[i
].type
& OP_REG32
) {
781 if (s1
->seg_size
== 16)
783 } else if (!(ops
[i
].type
& OP_REG32
)) {
784 if (s1
->seg_size
== 32)
790 if (s
== 1 || (pa
->instr_type
& OPC_D16
)) {
791 if (s1
->seg_size
== 32)
794 if (s1
->seg_size
== 16) {
795 if (!(pa
->instr_type
& OPC_D16
))
800 /* generate a16/a32 prefix if needed */
801 if ((a32
== 1) && (addr32
== 0))
803 /* generate o16/o32 prefix if needed */
804 if ((o32
== 1) && (data32
== 0))
809 #ifdef TCC_TARGET_X86_64
810 /* Generate addr32 prefix if needed */
811 for(i
= 0; i
< nb_ops
; i
++) {
812 if (ops
[i
].type
& OP_EA32
) {
818 /* generate data16 prefix if needed */
819 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
821 #ifdef TCC_TARGET_X86_64
822 if (s
== 3 || (alltypes
& OP_REG64
)) {
823 /* generate REX prefix */
825 for(i
= 0; i
< nb_ops
; i
++) {
826 if (op_type
[i
] == OP_REG64
) {
827 /* If only 64bit regs are accepted in one operand
828 this is a default64 instruction without need for
834 /* XXX find better encoding for the default64 instructions. */
835 if (((opcode
!= TOK_ASM_push
&& opcode
!= TOK_ASM_pop
836 && opcode
!= TOK_ASM_pushw
&& opcode
!= TOK_ASM_pushl
837 && opcode
!= TOK_ASM_pushq
&& opcode
!= TOK_ASM_popw
838 && opcode
!= TOK_ASM_popl
&& opcode
!= TOK_ASM_popq
839 && opcode
!= TOK_ASM_call
&& opcode
!= TOK_ASM_jmp
))
846 /* now generates the operation */
847 if (pa
->instr_type
& OPC_FWAIT
)
853 if ((v
== 0x69 || v
== 0x6b) && nb_ops
== 2) {
854 /* kludge for imul $im, %reg */
857 op_type
[2] = op_type
[1];
858 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
859 v
--; /* int $3 case */
861 } else if ((v
== 0x06 || v
== 0x07)) {
862 if (ops
[0].reg
>= 4) {
863 /* push/pop %fs or %gs */
864 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
866 v
+= ops
[0].reg
<< 3;
869 } else if (v
<= 0x05) {
871 v
+= ((opcode
- TOK_ASM_addb
) / NBWLX
) << 3;
872 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
874 v
+= ((opcode
- pa
->sym
) / 6) << 3;
876 if (pa
->instr_type
& OPC_REG
) {
877 for(i
= 0; i
< nb_ops
; i
++) {
878 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
883 /* mov $im, %reg case */
884 if (pa
->opcode
== 0xb0 && s
>= 1)
887 if (pa
->instr_type
& OPC_B
)
889 if (pa
->instr_type
& OPC_TEST
)
890 v
+= test_bits
[opcode
- pa
->sym
];
891 if (pa
->instr_type
& OPC_SHORTJMP
) {
895 /* see if we can really generate the jump with a byte offset */
899 if (sym
->r
!= cur_text_section
->sh_num
)
901 jmp_disp
= ops
[0].e
.v
+ sym
->jnext
- ind
- 2 - (v
>= 0xff);
902 if (jmp_disp
== (int8_t)jmp_disp
) {
903 /* OK to generate jump */
905 ops
[0].e
.v
= jmp_disp
;
908 if (pa
->instr_type
& OPC_JMP
) {
909 /* long jump will be allowed. need to modify the
916 tcc_error("invalid displacement");
925 /* search which operand will used for modrm */
927 if (pa
->instr_type
& OPC_SHIFT
) {
928 reg
= (opcode
- pa
->sym
) / NBWLX
;
931 } else if (pa
->instr_type
& OPC_ARITH
) {
932 reg
= (opcode
- pa
->sym
) / NBWLX
;
933 } else if (pa
->instr_type
& OPC_FARITH
) {
934 reg
= (opcode
- pa
->sym
) / 6;
936 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
938 if (pa
->instr_type
& OPC_MODRM
) {
939 /* first look for an ea operand */
940 for(i
= 0;i
< nb_ops
; i
++) {
941 if (op_type
[i
] & OP_EA
)
944 /* then if not found, a register or indirection (shift instructions) */
945 for(i
= 0;i
< nb_ops
; i
++) {
946 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
950 tcc_error("bad op table");
954 /* if a register is used in another operand then it is
955 used instead of group */
956 for(i
= 0;i
< nb_ops
; i
++) {
958 if (i
!= modrm_index
&&
959 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
965 asm_modrm(reg
, &ops
[modrm_index
]);
969 #ifndef TCC_TARGET_X86_64
970 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
971 /* ljmp or lcall kludge */
973 if (s1
->seg_size
== 16 && o32
== 0)
974 gen_expr16(&ops
[1].e
);
977 gen_expr32(&ops
[1].e
);
979 tcc_error("cannot relocate");
980 gen_le16(ops
[0].e
.v
);
984 for(i
= 0;i
< nb_ops
; i
++) {
986 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
| OP_IM8S
| OP_ADDR
)) {
987 /* if multiple sizes are given it means we must look
989 if ((v
| OP_IM8
| OP_IM64
) == (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM64
)) {
994 else if (s
== 2 || (v
& OP_IM64
) == 0)
999 if (v
& (OP_IM8
| OP_IM8S
)) {
1001 goto error_relocate
;
1003 } else if (v
& OP_IM16
) {
1005 if (s1
->seg_size
== 16)
1006 gen_expr16(&ops
[i
].e
);
1011 tcc_error("cannot relocate");
1013 gen_le16(ops
[i
].e
.v
);
1015 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1019 else if (s1
->seg_size
== 16)
1020 gen_disp16(&ops
[i
].e
);
1023 gen_disp32(&ops
[i
].e
);
1026 if (s1
->seg_size
== 16 && !((o32
== 1) && (v
& OP_IM32
)))
1027 gen_expr16(&ops
[i
].e
);
1030 #ifdef TCC_TARGET_X86_64
1032 gen_expr64(&ops
[i
].e
);
1035 gen_expr32(&ops
[i
].e
);
1039 } else if (v
& (OP_REG16
| OP_REG32
)) {
1040 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1042 g(0xE0 + ops
[i
].reg
);
1045 #ifdef TCC_TARGET_X86_64
1046 } else if (v
& (OP_REG32
| OP_REG64
)) {
1047 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
1049 g(0xE0 + ops
[i
].reg
);
1059 /* return the constraint priority (we allocate first the lowest
1060 numbered constraints) */
1061 static inline int constraint_priority(const char *str
)
1063 int priority
, c
, pr
;
1065 /* we take the lowest priority */
1099 tcc_error("unknown constraint '%c'", c
);
1108 static const char *skip_constraint_modifiers(const char *p
)
1110 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
1115 #define REG_OUT_MASK 0x01
1116 #define REG_IN_MASK 0x02
1118 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1120 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
,
1121 int nb_operands
, int nb_outputs
,
1122 const uint8_t *clobber_regs
,
1126 int sorted_op
[MAX_ASM_OPERANDS
];
1127 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, reg_mask
;
1129 uint8_t regs_allocated
[NB_ASM_REGS
];
1132 for(i
=0;i
<nb_operands
;i
++) {
1134 op
->input_index
= -1;
1140 /* compute constraint priority and evaluate references to output
1141 constraints if input constraints */
1142 for(i
=0;i
<nb_operands
;i
++) {
1144 str
= op
->constraint
;
1145 str
= skip_constraint_modifiers(str
);
1146 if (isnum(*str
) || *str
== '[') {
1147 /* this is a reference to another constraint */
1148 k
= find_constraint(operands
, nb_operands
, str
, NULL
);
1149 if ((unsigned)k
>= i
|| i
< nb_outputs
)
1150 tcc_error("invalid reference in constraint %d ('%s')",
1153 if (operands
[k
].input_index
>= 0)
1154 tcc_error("cannot reference twice the same operand");
1155 operands
[k
].input_index
= i
;
1158 op
->priority
= constraint_priority(str
);
1162 /* sort operands according to their priority */
1163 for(i
=0;i
<nb_operands
;i
++)
1165 for(i
=0;i
<nb_operands
- 1;i
++) {
1166 for(j
=i
+1;j
<nb_operands
;j
++) {
1167 p1
= operands
[sorted_op
[i
]].priority
;
1168 p2
= operands
[sorted_op
[j
]].priority
;
1171 sorted_op
[i
] = sorted_op
[j
];
1177 for(i
= 0;i
< NB_ASM_REGS
; i
++) {
1178 if (clobber_regs
[i
])
1179 regs_allocated
[i
] = REG_IN_MASK
| REG_OUT_MASK
;
1181 regs_allocated
[i
] = 0;
1183 /* esp cannot be used */
1184 regs_allocated
[4] = REG_IN_MASK
| REG_OUT_MASK
;
1185 /* ebp cannot be used yet */
1186 regs_allocated
[5] = REG_IN_MASK
| REG_OUT_MASK
;
1188 /* allocate registers and generate corresponding asm moves */
1189 for(i
=0;i
<nb_operands
;i
++) {
1192 str
= op
->constraint
;
1193 /* no need to allocate references */
1194 if (op
->ref_index
>= 0)
1196 /* select if register is used for output, input or both */
1197 if (op
->input_index
>= 0) {
1198 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1199 } else if (j
< nb_outputs
) {
1200 reg_mask
= REG_OUT_MASK
;
1202 reg_mask
= REG_IN_MASK
;
1213 if (j
>= nb_outputs
)
1214 tcc_error("'%c' modifier can only be applied to outputs", c
);
1215 reg_mask
= REG_IN_MASK
| REG_OUT_MASK
;
1218 /* allocate both eax and edx */
1219 if (is_reg_allocated(TREG_XAX
) ||
1220 is_reg_allocated(TREG_XDX
))
1224 regs_allocated
[TREG_XAX
] |= reg_mask
;
1225 regs_allocated
[TREG_XDX
] |= reg_mask
;
1245 if (is_reg_allocated(reg
))
1249 /* eax, ebx, ecx or edx */
1250 for(reg
= 0; reg
< 4; reg
++) {
1251 if (!is_reg_allocated(reg
))
1256 /* any general register */
1257 for(reg
= 0; reg
< 8; reg
++) {
1258 if (!is_reg_allocated(reg
))
1263 /* now we can reload in the register */
1266 regs_allocated
[reg
] |= reg_mask
;
1269 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
1275 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
1280 /* nothing special to do because the operand is already in
1281 memory, except if the pointer itself is stored in a
1282 memory variable (VT_LLOCAL case) */
1283 /* XXX: fix constant case */
1284 /* if it is a reference to a memory zone, it must lie
1285 in a register, so we reserve the register in the
1286 input registers and a load will be generated
1288 if (j
< nb_outputs
|| c
== 'm') {
1289 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1290 /* any general register */
1291 for(reg
= 0; reg
< 8; reg
++) {
1292 if (!(regs_allocated
[reg
] & REG_IN_MASK
))
1297 /* now we can reload in the register */
1298 regs_allocated
[reg
] |= REG_IN_MASK
;
1305 tcc_error("asm constraint %d ('%s') could not be satisfied",
1309 /* if a reference is present for that operand, we assign it too */
1310 if (op
->input_index
>= 0) {
1311 operands
[op
->input_index
].reg
= op
->reg
;
1312 operands
[op
->input_index
].is_llong
= op
->is_llong
;
1316 /* compute out_reg. It is used to store outputs registers to memory
1317 locations references by pointers (VT_LLOCAL case) */
1319 for(i
=0;i
<nb_operands
;i
++) {
1322 (op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1324 for(reg
= 0; reg
< 8; reg
++) {
1325 if (!(regs_allocated
[reg
] & REG_OUT_MASK
))
1328 tcc_error("could not find free output register for reloading");
1335 /* print sorted constraints */
1337 for(i
=0;i
<nb_operands
;i
++) {
1340 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1342 op
->id
? get_tok_str(op
->id
, NULL
) : "",
1348 printf("out_reg=%d\n", *pout_reg
);
1352 ST_FUNC
void subst_asm_operand(CString
*add_str
,
1353 SValue
*sv
, int modifier
)
1355 int r
, reg
, size
, val
;
1359 if ((r
& VT_VALMASK
) == VT_CONST
) {
1360 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
1361 cstr_ccat(add_str
, '$');
1363 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
), -1);
1364 if ((uint32_t)sv
->c
.i
!= 0) {
1365 cstr_ccat(add_str
, '+');
1371 if (modifier
== 'n')
1373 snprintf(buf
, sizeof(buf
), "%d", (int)sv
->c
.i
);
1374 cstr_cat(add_str
, buf
, -1);
1375 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
1376 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", (int)sv
->c
.i
);
1377 cstr_cat(add_str
, buf
, -1);
1378 } else if (r
& VT_LVAL
) {
1379 reg
= r
& VT_VALMASK
;
1380 if (reg
>= VT_CONST
)
1381 tcc_error("internal compiler error");
1382 snprintf(buf
, sizeof(buf
), "(%%%s)",
1383 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
1384 cstr_cat(add_str
, buf
, -1);
1387 reg
= r
& VT_VALMASK
;
1388 if (reg
>= VT_CONST
)
1389 tcc_error("internal compiler error");
1391 /* choose register operand size */
1392 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
1394 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
1396 #ifdef TCC_TARGET_X86_64
1397 else if ((sv
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1402 if (size
== 1 && reg
>= 4)
1405 if (modifier
== 'b') {
1407 tcc_error("cannot use byte register");
1409 } else if (modifier
== 'h') {
1411 tcc_error("cannot use byte register");
1413 } else if (modifier
== 'w') {
1415 #ifdef TCC_TARGET_X86_64
1416 } else if (modifier
== 'q') {
1423 reg
= TOK_ASM_ah
+ reg
;
1426 reg
= TOK_ASM_al
+ reg
;
1429 reg
= TOK_ASM_ax
+ reg
;
1432 reg
= TOK_ASM_eax
+ reg
;
1434 #ifdef TCC_TARGET_X86_64
1436 reg
= TOK_ASM_rax
+ reg
;
1440 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1441 cstr_cat(add_str
, buf
, -1);
1445 /* generate prolog and epilog code for asm statement */
1446 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1447 int nb_outputs
, int is_output
,
1448 uint8_t *clobber_regs
,
1451 uint8_t regs_allocated
[NB_ASM_REGS
];
1454 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1456 /* mark all used registers */
1457 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1458 for(i
= 0; i
< nb_operands
;i
++) {
1461 regs_allocated
[op
->reg
] = 1;
1464 /* generate reg save code */
1465 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1467 if (regs_allocated
[reg
]) {
1469 if (tcc_state
->seg_size
== 16)
1476 /* generate load code */
1477 for(i
= 0; i
< nb_operands
; i
++) {
1480 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
&&
1482 /* memory reference case (for both input and
1486 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1488 } else if (i
>= nb_outputs
|| op
->is_rw
) {
1489 /* load value in register */
1490 load(op
->reg
, op
->vt
);
1495 load(TREG_XDX
, &sv
);
1501 /* generate save code */
1502 for(i
= 0 ; i
< nb_outputs
; i
++) {
1505 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
1506 if (!op
->is_memory
) {
1509 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1512 sv
.r
= (sv
.r
& ~VT_VALMASK
) | out_reg
;
1513 store(op
->reg
, &sv
);
1516 store(op
->reg
, op
->vt
);
1521 store(TREG_XDX
, &sv
);
1526 /* generate reg restore code */
1527 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1529 if (regs_allocated
[reg
]) {
1531 if (tcc_state
->seg_size
== 16)
1540 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1545 if (!strcmp(str
, "memory") ||
1548 ts
= tok_alloc(str
, strlen(str
));
1550 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1552 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1554 #ifdef TCC_TARGET_X86_64
1555 } else if (reg
>= TOK_ASM_rax
&& reg
<= TOK_ASM_rdi
) {
1559 tcc_error("invalid clobber register '%s'", str
);
1561 clobber_regs
[reg
] = 1;