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
31 #define OPC_JMP 0x01 /* jmp operand */
32 #define OPC_B 0x02 /* only used with OPC_WL */
33 #define OPC_WL 0x04 /* accepts w, l or no suffix */
34 #define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */
35 #define OPC_REG 0x08 /* register is added to opcode */
36 #define OPC_MODRM 0x10 /* modrm encoding */
37 #define OPC_FWAIT 0x20 /* add fwait opcode */
38 #define OPC_TEST 0x40 /* test opcodes */
39 #define OPC_SHIFT 0x80 /* shift opcodes */
40 #define OPC_D16 0x0100 /* generate data16 prefix */
41 #define OPC_ARITH 0x0200 /* arithmetic opcodes */
42 #define OPC_SHORTJMP 0x0400 /* short jmp operand */
43 #define OPC_FARITH 0x0800 /* FPU arithmetic opcodes */
44 #ifdef TCC_TARGET_X86_64
45 # define OPC_WLQ 0x1000 /* accepts w, l, q or no suffix */
46 # define OPC_BWLQ (OPC_B | OPC_WLQ) /* accepts b, w, l, q or no suffix */
47 # define OPC_WLX OPC_WLQ
49 # define OPC_WLX OPC_WL
52 #define OPC_GROUP_SHIFT 13
54 /* in order to compress the operand type, we use specific operands and
57 OPT_REG8=0, /* warning: value is hardcoded from TOK_ASM_xxx */
58 OPT_REG16, /* warning: value is hardcoded from TOK_ASM_xxx */
59 OPT_REG32, /* warning: value is hardcoded from TOK_ASM_xxx */
60 #ifdef TCC_TARGET_X86_64
61 OPT_REG64, /* warning: value is hardcoded from TOK_ASM_xxx */
63 OPT_MMX, /* warning: value is hardcoded from TOK_ASM_xxx */
64 OPT_SSE, /* warning: value is hardcoded from TOK_ASM_xxx */
65 OPT_CR, /* warning: value is hardcoded from TOK_ASM_xxx */
66 OPT_TR, /* warning: value is hardcoded from TOK_ASM_xxx */
67 OPT_DB, /* warning: value is hardcoded from TOK_ASM_xxx */
74 #ifdef TCC_TARGET_X86_64
77 OPT_EAX, /* %al, %ax, %eax or %rax register */
78 OPT_ST0, /* %st(0) register */
79 OPT_CL, /* %cl register */
80 OPT_DX, /* %dx register */
81 OPT_ADDR, /* OP_EA with only offset */
82 OPT_INDIR, /* *(expr) */
85 OPT_IM, /* IM8 | IM16 | IM32 | IM64 */
86 OPT_REG, /* REG8 | REG16 | REG32 | REG64 */
87 OPT_REGW, /* REG16 | REG32 | REG64 */
88 OPT_IMW, /* IM16 | IM32 | IM64 */
89 #ifdef TCC_TARGET_X86_64
90 OPT_IMNO64, /* IM16 | IM32 */
92 /* can be ored with any OPT_xxx */
96 #define OP_REG8 (1 << OPT_REG8)
97 #define OP_REG16 (1 << OPT_REG16)
98 #define OP_REG32 (1 << OPT_REG32)
99 #define OP_MMX (1 << OPT_MMX)
100 #define OP_SSE (1 << OPT_SSE)
101 #define OP_CR (1 << OPT_CR)
102 #define OP_TR (1 << OPT_TR)
103 #define OP_DB (1 << OPT_DB)
104 #define OP_SEG (1 << OPT_SEG)
105 #define OP_ST (1 << OPT_ST)
106 #define OP_IM8 (1 << OPT_IM8)
107 #define OP_IM8S (1 << OPT_IM8S)
108 #define OP_IM16 (1 << OPT_IM16)
109 #define OP_IM32 (1 << OPT_IM32)
110 #define OP_EAX (1 << OPT_EAX)
111 #define OP_ST0 (1 << OPT_ST0)
112 #define OP_CL (1 << OPT_CL)
113 #define OP_DX (1 << OPT_DX)
114 #define OP_ADDR (1 << OPT_ADDR)
115 #define OP_INDIR (1 << OPT_INDIR)
116 #ifdef TCC_TARGET_X86_64
117 # define OP_REG64 (1 << OPT_REG64)
118 # define OP_IM64 (1 << OPT_IM64)
124 #define OP_EA 0x40000000
125 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
127 #ifdef TCC_TARGET_X86_64
128 # define OP_IM OP_IM64
129 # define TREG_XAX TREG_RAX
130 # define TREG_XCX TREG_RCX
131 # define TREG_XDX TREG_RDX
133 # define OP_IM OP_IM32
134 # define TREG_XAX TREG_EAX
135 # define TREG_XCX TREG_ECX
136 # define TREG_XDX TREG_EDX
139 typedef struct ASMInstr {
144 uint8_t op_type[MAX_OPERANDS]; /* see OP_xxx */
147 typedef struct Operand {
149 int8_t reg; /* register, -1 if none */
150 int8_t reg2; /* second register, -1 if none */
155 static const uint8_t reg_to_size[9] = {
160 #ifdef TCC_TARGET_X86_64
164 0, 0, 1, 0, 2, 0, 0, 0, 3
167 #define NB_TEST_OPCODES 30
169 static const uint8_t test_bits[NB_TEST_OPCODES] = {
202 static const uint8_t segment_prefixes[] = {
211 static const ASMInstr asm_instrs[] = {
213 #define DEF_ASM_OP0(name, opcode)
214 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
215 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
216 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
217 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 3, { op0, op1, op2 }},
218 #ifdef TCC_TARGET_X86_64
219 # include "x86_64-asm.h"
221 # include "i386-asm.h"
227 static const uint16_t op0_codes[] = {
229 #define DEF_ASM_OP0(x, opcode) opcode,
230 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
231 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
232 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
233 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
234 #ifdef TCC_TARGET_X86_64
235 # include "x86_64-asm.h"
237 # include "i386-asm.h"
241 static inline int get_reg_shift(TCCState *s1)
245 if (s1->seg_size == 16)
246 tcc_error("invalid effective address");
248 v = asm_int_expr(s1);
263 expect("1, 2, 4 or 8 constant");
270 static int asm_parse_reg(void)
276 if (tok >= TOK_ASM_eax && tok <= TOK_ASM_edi) {
277 reg = tok - TOK_ASM_eax;
278 #ifdef TCC_TARGET_X86_64
279 } else if (tok >= TOK_ASM_rax && tok <= TOK_ASM_rdi) {
280 reg = tok - TOK_ASM_rax;
283 } else if (tok >= TOK_ASM_ax && tok <= TOK_ASM_di) {
284 reg = tok - TOK_ASM_ax;
294 static void parse_operand(TCCState *s1, Operand *op)
308 if (tok >= TOK_ASM_al && tok <= TOK_ASM_db7) {
309 reg = tok - TOK_ASM_al;
310 op->type = 1 << (reg >> 3); /* WARNING: do not change constant order */
312 if ((op->type & OP_REG) && op->reg == TREG_XAX)
314 else if (op->type == OP_REG8 && op->reg == TREG_XCX)
316 else if (op->type == OP_REG16 && op->reg == TREG_XDX)
318 } else if (tok >= TOK_ASM_dr0 && tok <= TOK_ASM_dr7) {
320 op->reg = tok - TOK_ASM_dr0;
321 } else if (tok >= TOK_ASM_es && tok <= TOK_ASM_gs) {
323 op->reg = tok - TOK_ASM_es;
324 } else if (tok == TOK_ASM_st) {
330 if (tok != TOK_PPNUM)
334 if ((unsigned)reg >= 8 || p[1] != '\0')
345 tcc_error("unknown register");
349 } else if (tok == '$') {
357 if (op->e.v == (uint8_t)op->e.v)
359 if (op->e.v == (int8_t)op->e.v)
361 if (op->e.v == (uint16_t)op->e.v)
363 #ifdef TCC_TARGET_X86_64
364 if (op->e.v == (uint32_t)op->e.v)
369 /* address(reg,reg2,shift) with all variants */
385 /* bracketed offset expression */
397 op->reg = asm_parse_reg();
402 op->reg2 = asm_parse_reg();
406 op->shift = get_reg_shift(s1);
411 if (op->reg == -1 && op->reg2 == -1)
417 /* XXX: unify with C code output ? */
418 ST_FUNC void gen_expr32(ExprValue *pe)
420 gen_addr32(pe->sym ? VT_SYM : 0, pe->sym, pe->v);
423 #ifdef TCC_TARGET_X86_64
424 static void gen_expr64(ExprValue *pe)
426 gen_addr64(pe->sym ? VT_SYM : 0, pe->sym, pe->v);
430 /* XXX: unify with C code output ? */
431 static void gen_disp32(ExprValue *pe)
434 if (sym && sym->r == cur_text_section->sh_num) {
435 /* same section: we can output an absolute value. Note
436 that the TCC compiler behaves differently here because
437 it always outputs a relocation to ease (future) code
438 elimination in the linker */
439 gen_le32(pe->v + sym->jnext - ind - 4);
441 if (sym && sym->type.t == VT_VOID) {
442 sym->type.t = VT_FUNC;
443 sym->type.ref = NULL;
445 gen_addrpc32(VT_SYM, sym, pe->v);
450 static void gen_expr16(ExprValue *pe)
453 greloc(cur_text_section, pe->sym, ind, R_386_16);
456 static void gen_disp16(ExprValue *pe)
461 if (sym->r == cur_text_section->sh_num) {
462 /* same section: we can output an absolute value. Note
463 that the TCC compiler behaves differently here because
464 it always outputs a relocation to ease (future) code
465 elimination in the linker */
466 gen_le16(pe->v + sym->jnext - ind - 2);
468 greloc(cur_text_section, sym, ind, R_386_PC16);
472 /* put an empty PC32 relocation */
473 put_elf_reloc(symtab_section, cur_text_section,
480 /* generate the modrm operand */
481 static inline void asm_modrm(int reg, Operand *op)
483 int mod, reg1, reg2, sib_reg1;
485 if (op->type & (OP_REG | OP_MMX | OP_SSE)) {
486 g(0xc0 + (reg << 3) + op->reg);
487 } else if (op->reg == -1 && op->reg2 == -1) {
488 /* displacement only */
490 if (tcc_state->seg_size == 16) {
491 g(0x06 + (reg << 3));
493 } else if (tcc_state->seg_size == 32)
496 g(0x05 + (reg << 3));
501 /* fist compute displacement encoding */
502 if (sib_reg1 == -1) {
505 } else if (op->e.v == 0 && !op->e.sym && op->reg != 5) {
507 } else if (op->e.v == (int8_t)op->e.v && !op->e.sym) {
512 /* compute if sib byte needed */
517 if (tcc_state->seg_size == 32) {
519 g(mod + (reg << 3) + reg1);
524 reg2 = 4; /* indicate no index */
525 g((op->shift << 6) + (reg2 << 3) + sib_reg1);
528 } else if (tcc_state->seg_size == 16) {
529 /* edi = 7, esi = 6 --> di = 5, si = 4 */
530 if ((reg1 == 6) || (reg1 == 7)) {
532 /* ebx = 3 --> bx = 7 */
533 } else if (reg1 == 3) {
535 /* o32 = 5 --> o16 = 6 */
536 } else if (reg1 == 5) {
538 /* sib not valid in 16-bit mode */
539 } else if (reg1 == 4) {
541 /* bp + si + offset */
542 if ((sib_reg1 == 5) && (reg2 == 6)) {
544 /* bp + di + offset */
545 } else if ((sib_reg1 == 5) && (reg2 == 7)) {
547 /* bx + si + offset */
548 } else if ((sib_reg1 == 3) && (reg2 == 6)) {
550 /* bx + di + offset */
551 } else if ((sib_reg1 == 3) && (reg2 == 7)) {
554 tcc_error("invalid effective address");
559 tcc_error("invalid register");
561 g(mod + (reg << 3) + reg1);
567 } else if (mod == 0x80 || op->reg == -1) {
569 if (tcc_state->seg_size == 16)
571 else if (tcc_state->seg_size == 32)
578 ST_FUNC void asm_opcode(TCCState *s1, int opcode)
581 int i, modrm_index, reg, v, op1, is_short_jmp, seg_prefix;
583 Operand ops[MAX_OPERANDS], *pop;
584 int op_type[3]; /* decoded op type */
586 static int a32 = 0, o32 = 0, addr32 = 0, data32 = 0;
589 /* force synthetic ';' after prefix instruction, so we can handle */
590 /* one-line things like "rep stosb" instead of only "rep\nstosb" */
591 if (opcode >= TOK_ASM_wait && opcode <= TOK_ASM_repnz)
599 if (tok == ';' || tok == TOK_LINEFEED)
601 if (nb_ops >= MAX_OPERANDS) {
602 tcc_error("incorrect number of operands");
604 parse_operand(s1, pop);
606 if (pop->type != OP_SEG || seg_prefix)
607 tcc_error("incorrect prefix");
608 seg_prefix = segment_prefixes[pop->reg];
610 parse_operand(s1, pop);
612 if (!(pop->type & OP_EA)) {
613 tcc_error("segment prefix must be followed by memory reference");
625 s = 0; /* avoid warning */
627 /* optimize matching by using a lookup table (no hashing is needed
629 for(pa = asm_instrs; pa->sym != 0; pa++) {
631 if (pa->instr_type & OPC_FARITH) {
632 v = opcode - pa->sym;
633 if (!((unsigned)v < 8 * 6 && (v % 6) == 0))
635 } else if (pa->instr_type & OPC_ARITH) {
636 if (!(opcode >= pa->sym && opcode < pa->sym + 8*NBWLX))
638 s = (opcode - pa->sym) % NBWLX;
639 } else if (pa->instr_type & OPC_SHIFT) {
640 if (!(opcode >= pa->sym && opcode < pa->sym + 7*NBWLX))
642 s = (opcode - pa->sym) % NBWLX;
643 } else if (pa->instr_type & OPC_TEST) {
644 if (!(opcode >= pa->sym && opcode < pa->sym + NB_TEST_OPCODES))
646 } else if (pa->instr_type & OPC_B) {
647 if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX))
649 s = opcode - pa->sym;
650 } else if (pa->instr_type & OPC_WLX) {
651 if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX-1))
653 s = opcode - pa->sym + 1;
655 if (pa->sym != opcode)
658 if (pa->nb_ops != nb_ops)
660 /* now decode and check each operand */
661 for(i = 0; i < nb_ops; i++) {
663 op1 = pa->op_type[i];
667 v = OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64;
670 v = OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64;
673 v = OP_REG16 | OP_REG32 | OP_REG64;
676 v = OP_IM16 | OP_IM32 | OP_IM64;
678 #ifdef TCC_TARGET_X86_64
680 v = OP_IM16 | OP_IM32;
690 if ((ops[i].type & v) == 0)
693 /* all is matching ! */
698 if (opcode >= TOK_ASM_first && opcode <= TOK_ASM_last) {
700 b = op0_codes[opcode - TOK_ASM_first];
702 if (opcode == TOK_ASM_o32) {
703 if (s1->seg_size == 32)
704 tcc_error("incorrect prefix");
707 } else if (opcode == TOK_ASM_a32) {
708 if (s1->seg_size == 32)
709 tcc_error("incorrect prefix");
719 tcc_error("unknown opcode '%s'",
720 get_tok_str(opcode, NULL));
723 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
725 for(i = 0; s == NBWLX-1 && i < nb_ops; i++) {
726 if ((ops[i].type & OP_REG) && !(op_type[i] & (OP_CL | OP_DX)))
727 s = reg_to_size[ops[i].type & OP_REG];
730 if ((opcode == TOK_ASM_push || opcode == TOK_ASM_pop) &&
731 (ops[0].type & (OP_SEG | OP_IM8S | OP_IM32 | OP_IM64)))
734 tcc_error("cannot infer opcode suffix");
739 for(i = 0; i < nb_ops; i++) {
740 if (ops[i].type & OP_REG32) {
741 if (s1->seg_size == 16)
743 } else if (!(ops[i].type & OP_REG32)) {
744 if (s1->seg_size == 32)
750 if (s == 1 || (pa->instr_type & OPC_D16)) {
751 if (s1->seg_size == 32)
754 if (s1->seg_size == 16) {
755 if (!(pa->instr_type & OPC_D16))
760 /* generate a16/a32 prefix if needed */
761 if ((a32 == 1) && (addr32 == 0))
763 /* generate o16/o32 prefix if needed */
764 if ((o32 == 1) && (data32 == 0))
769 /* generate data16 prefix if needed */
770 if (s == 1 || (pa->instr_type & OPC_D16))
772 #ifdef TCC_TARGET_X86_64
774 /* generate REX prefix */
775 if ((opcode != TOK_ASM_push && opcode != TOK_ASM_pop)
776 || !(ops[0].type & OP_REG64))
782 /* now generates the operation */
783 if (pa->instr_type & OPC_FWAIT)
789 if ((v == 0x69 || v == 0x6b) && nb_ops == 2) {
790 /* kludge for imul $im, %reg */
793 op_type[2] = op_type[1];
794 } else if (v == 0xcd && ops[0].e.v == 3 && !ops[0].e.sym) {
795 v--; /* int $3 case */
797 } else if ((v == 0x06 || v == 0x07)) {
798 if (ops[0].reg >= 4) {
799 /* push/pop %fs or %gs */
800 v = 0x0fa0 + (v - 0x06) + ((ops[0].reg - 4) << 3);
802 v += ops[0].reg << 3;
805 } else if (v <= 0x05) {
807 v += ((opcode - TOK_ASM_addb) / NBWLX) << 3;
808 } else if ((pa->instr_type & (OPC_FARITH | OPC_MODRM)) == OPC_FARITH) {
810 v += ((opcode - pa->sym) / 6) << 3;
812 if (pa->instr_type & OPC_REG) {
813 for(i = 0; i < nb_ops; i++) {
814 if (op_type[i] & (OP_REG | OP_ST)) {
819 /* mov $im, %reg case */
820 if (pa->opcode == 0xb0 && s >= 1)
823 if (pa->instr_type & OPC_B)
825 if (pa->instr_type & OPC_TEST)
826 v += test_bits[opcode - pa->sym];
827 if (pa->instr_type & OPC_SHORTJMP) {
831 /* see if we can really generate the jump with a byte offset */
835 if (sym->r != cur_text_section->sh_num)
837 jmp_disp = ops[0].e.v + sym->jnext - ind - 2;
838 if (jmp_disp == (int8_t)jmp_disp) {
839 /* OK to generate jump */
841 ops[0].e.v = jmp_disp;
844 if (pa->instr_type & OPC_JMP) {
845 /* long jump will be allowed. need to modify the
852 tcc_error("invalid displacement");
861 /* search which operand will used for modrm */
863 if (pa->instr_type & OPC_SHIFT) {
864 reg = (opcode - pa->sym) / NBWLX;
867 } else if (pa->instr_type & OPC_ARITH) {
868 reg = (opcode - pa->sym) / NBWLX;
869 } else if (pa->instr_type & OPC_FARITH) {
870 reg = (opcode - pa->sym) / 6;
872 reg = (pa->instr_type >> OPC_GROUP_SHIFT) & 7;
874 if (pa->instr_type & OPC_MODRM) {
875 /* first look for an ea operand */
876 for(i = 0;i < nb_ops; i++) {
877 if (op_type[i] & OP_EA)
880 /* then if not found, a register or indirection (shift instructions) */
881 for(i = 0;i < nb_ops; i++) {
882 if (op_type[i] & (OP_REG | OP_MMX | OP_SSE | OP_INDIR))
886 tcc_error("bad op table");
890 /* if a register is used in another operand then it is
891 used instead of group */
892 for(i = 0;i < nb_ops; i++) {
894 if (i != modrm_index &&
895 (v & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_TR | OP_DB | OP_SEG))) {
901 asm_modrm(reg, &ops[modrm_index]);
905 #ifndef TCC_TARGET_X86_64
906 if (pa->opcode == 0x9a || pa->opcode == 0xea) {
907 /* ljmp or lcall kludge */
909 if (s1->seg_size == 16 && o32 == 0)
910 gen_expr16(&ops[1].e);
913 gen_expr32(&ops[1].e);
915 tcc_error("cannot relocate");
916 gen_le16(ops[0].e.v);
920 for(i = 0;i < nb_ops; i++) {
922 if (v & (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64 | OP_IM8S | OP_ADDR)) {
923 /* if multiple sizes are given it means we must look
925 if ((v | OP_IM8 | OP_IM64) == (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64)) {
930 else if (s == 2 || (v & OP_IM64) == 0)
935 if (v & (OP_IM8 | OP_IM8S)) {
939 } else if (v & OP_IM16) {
941 if (s1->seg_size == 16)
942 gen_expr16(&ops[i].e);
947 tcc_error("cannot relocate");
949 gen_le16(ops[i].e.v);
951 if (pa->instr_type & (OPC_JMP | OPC_SHORTJMP)) {
955 else if (s1->seg_size == 16)
956 gen_disp16(&ops[i].e);
959 gen_disp32(&ops[i].e);
962 if (s1->seg_size == 16 && !((o32 == 1) && (v & OP_IM32)))
963 gen_expr16(&ops[i].e);
966 #ifdef TCC_TARGET_X86_64
968 gen_expr64(&ops[i].e);
971 gen_expr32(&ops[i].e);
975 } else if (v & (OP_REG16 | OP_REG32)) {
976 if (pa->instr_type & (OPC_JMP | OPC_SHORTJMP)) {
978 g(0xE0 + ops[i].reg);
981 #ifdef TCC_TARGET_X86_64
982 } else if (v & (OP_REG32 | OP_REG64)) {
983 if (pa->instr_type & (OPC_JMP | OPC_SHORTJMP)) {
985 g(0xE0 + ops[i].reg);
995 /* return the constraint priority (we allocate first the lowest
996 numbered constraints) */
997 static inline int constraint_priority(const char *str)
1001 /* we take the lowest priority */
1035 tcc_error("unknown constraint '%c'", c);
1044 static const char *skip_constraint_modifiers(const char *p)
1046 while (*p == '=' || *p == '&' || *p == '+' || *p == '%')
1051 #define REG_OUT_MASK 0x01
1052 #define REG_IN_MASK 0x02
1054 #define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask)
1056 ST_FUNC void asm_compute_constraints(ASMOperand *operands,
1057 int nb_operands, int nb_outputs,
1058 const uint8_t *clobber_regs,
1062 int sorted_op[MAX_ASM_OPERANDS];
1063 int i, j, k, p1, p2, tmp, reg, c, reg_mask;
1065 uint8_t regs_allocated[NB_ASM_REGS];
1068 for(i=0;i<nb_operands;i++) {
1070 op->input_index = -1;
1076 /* compute constraint priority and evaluate references to output
1077 constraints if input constraints */
1078 for(i=0;i<nb_operands;i++) {
1080 str = op->constraint;
1081 str = skip_constraint_modifiers(str);
1082 if (isnum(*str) || *str == '[') {
1083 /* this is a reference to another constraint */
1084 k = find_constraint(operands, nb_operands, str, NULL);
1085 if ((unsigned)k >= i || i < nb_outputs)
1086 tcc_error("invalid reference in constraint %d ('%s')",
1089 if (operands[k].input_index >= 0)
1090 tcc_error("cannot reference twice the same operand");
1091 operands[k].input_index = i;
1094 op->priority = constraint_priority(str);
1098 /* sort operands according to their priority */
1099 for(i=0;i<nb_operands;i++)
1101 for(i=0;i<nb_operands - 1;i++) {
1102 for(j=i+1;j<nb_operands;j++) {
1103 p1 = operands[sorted_op[i]].priority;
1104 p2 = operands[sorted_op[j]].priority;
1107 sorted_op[i] = sorted_op[j];
1113 for(i = 0;i < NB_ASM_REGS; i++) {
1114 if (clobber_regs[i])
1115 regs_allocated[i] = REG_IN_MASK | REG_OUT_MASK;
1117 regs_allocated[i] = 0;
1119 /* esp cannot be used */
1120 regs_allocated[4] = REG_IN_MASK | REG_OUT_MASK;
1121 /* ebp cannot be used yet */
1122 regs_allocated[5] = REG_IN_MASK | REG_OUT_MASK;
1124 /* allocate registers and generate corresponding asm moves */
1125 for(i=0;i<nb_operands;i++) {
1128 str = op->constraint;
1129 /* no need to allocate references */
1130 if (op->ref_index >= 0)
1132 /* select if register is used for output, input or both */
1133 if (op->input_index >= 0) {
1134 reg_mask = REG_IN_MASK | REG_OUT_MASK;
1135 } else if (j < nb_outputs) {
1136 reg_mask = REG_OUT_MASK;
1138 reg_mask = REG_IN_MASK;
1149 if (j >= nb_outputs)
1150 tcc_error("'%c' modifier can only be applied to outputs", c);
1151 reg_mask = REG_IN_MASK | REG_OUT_MASK;
1154 /* allocate both eax and edx */
1155 if (is_reg_allocated(TREG_XAX) ||
1156 is_reg_allocated(TREG_XDX))
1160 regs_allocated[TREG_XAX] |= reg_mask;
1161 regs_allocated[TREG_XDX] |= reg_mask;
1181 if (is_reg_allocated(reg))
1185 /* eax, ebx, ecx or edx */
1186 for(reg = 0; reg < 4; reg++) {
1187 if (!is_reg_allocated(reg))
1192 /* any general register */
1193 for(reg = 0; reg < 8; reg++) {
1194 if (!is_reg_allocated(reg))
1199 /* now we can reload in the register */
1202 regs_allocated[reg] |= reg_mask;
1205 if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST))
1211 if (!((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST))
1216 /* nothing special to do because the operand is already in
1217 memory, except if the pointer itself is stored in a
1218 memory variable (VT_LLOCAL case) */
1219 /* XXX: fix constant case */
1220 /* if it is a reference to a memory zone, it must lie
1221 in a register, so we reserve the register in the
1222 input registers and a load will be generated
1224 if (j < nb_outputs || c == 'm') {
1225 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) {
1226 /* any general register */
1227 for(reg = 0; reg < 8; reg++) {
1228 if (!(regs_allocated[reg] & REG_IN_MASK))
1233 /* now we can reload in the register */
1234 regs_allocated[reg] |= REG_IN_MASK;
1241 tcc_error("asm constraint %d ('%s') could not be satisfied",
1245 /* if a reference is present for that operand, we assign it too */
1246 if (op->input_index >= 0) {
1247 operands[op->input_index].reg = op->reg;
1248 operands[op->input_index].is_llong = op->is_llong;
1252 /* compute out_reg. It is used to store outputs registers to memory
1253 locations references by pointers (VT_LLOCAL case) */
1255 for(i=0;i<nb_operands;i++) {
1258 (op->vt->r & VT_VALMASK) == VT_LLOCAL &&
1260 for(reg = 0; reg < 8; reg++) {
1261 if (!(regs_allocated[reg] & REG_OUT_MASK))
1264 tcc_error("could not find free output register for reloading");
1271 /* print sorted constraints */
1273 for(i=0;i<nb_operands;i++) {
1276 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
1278 op->id ? get_tok_str(op->id, NULL) : "",
1284 printf("out_reg=%d\n", *pout_reg);
1288 ST_FUNC void subst_asm_operand(CString *add_str,
1289 SValue *sv, int modifier)
1291 int r, reg, size, val;
1295 if ((r & VT_VALMASK) == VT_CONST) {
1296 if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n')
1297 cstr_ccat(add_str, '$');
1299 cstr_cat(add_str, get_tok_str(sv->sym->v, NULL));
1301 cstr_ccat(add_str, '+');
1307 if (modifier == 'n')
1309 snprintf(buf, sizeof(buf), "%d", sv->c.i);
1310 cstr_cat(add_str, buf);
1311 } else if ((r & VT_VALMASK) == VT_LOCAL) {
1312 snprintf(buf, sizeof(buf), "%d(%%ebp)", sv->c.i);
1313 cstr_cat(add_str, buf);
1314 } else if (r & VT_LVAL) {
1315 reg = r & VT_VALMASK;
1316 if (reg >= VT_CONST)
1317 tcc_error("internal compiler error");
1318 snprintf(buf, sizeof(buf), "(%%%s)",
1319 get_tok_str(TOK_ASM_eax + reg, NULL));
1320 cstr_cat(add_str, buf);
1323 reg = r & VT_VALMASK;
1324 if (reg >= VT_CONST)
1325 tcc_error("internal compiler error");
1327 /* choose register operand size */
1328 if ((sv->type.t & VT_BTYPE) == VT_BYTE)
1330 else if ((sv->type.t & VT_BTYPE) == VT_SHORT)
1332 #ifdef TCC_TARGET_X86_64
1333 else if ((sv->type.t & VT_BTYPE) == VT_LLONG)
1338 if (size == 1 && reg >= 4)
1341 if (modifier == 'b') {
1343 tcc_error("cannot use byte register");
1345 } else if (modifier == 'h') {
1347 tcc_error("cannot use byte register");
1349 } else if (modifier == 'w') {
1351 #ifdef TCC_TARGET_X86_64
1352 } else if (modifier == 'q') {
1359 reg = TOK_ASM_ah + reg;
1362 reg = TOK_ASM_al + reg;
1365 reg = TOK_ASM_ax + reg;
1368 reg = TOK_ASM_eax + reg;
1370 #ifdef TCC_TARGET_X86_64
1372 reg = TOK_ASM_rax + reg;
1376 snprintf(buf, sizeof(buf), "%%%s", get_tok_str(reg, NULL));
1377 cstr_cat(add_str, buf);
1381 /* generate prolog and epilog code for asm statment */
1382 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
1383 int nb_outputs, int is_output,
1384 uint8_t *clobber_regs,
1387 uint8_t regs_allocated[NB_ASM_REGS];
1390 static uint8_t reg_saved[NB_SAVED_REGS] = { 3, 6, 7 };
1392 /* mark all used registers */
1393 memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated));
1394 for(i = 0; i < nb_operands;i++) {
1397 regs_allocated[op->reg] = 1;
1400 /* generate reg save code */
1401 for(i = 0; i < NB_SAVED_REGS; i++) {
1403 if (regs_allocated[reg]) {
1405 if (tcc_state->seg_size == 16)
1412 /* generate load code */
1413 for(i = 0; i < nb_operands; i++) {
1416 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL &&
1418 /* memory reference case (for both input and
1422 sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL;
1424 } else if (i >= nb_outputs || op->is_rw) {
1425 /* load value in register */
1426 load(op->reg, op->vt);
1431 load(TREG_XDX, &sv);
1437 /* generate save code */
1438 for(i = 0 ; i < nb_outputs; i++) {
1441 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) {
1442 if (!op->is_memory) {
1445 sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL;
1448 sv.r = (sv.r & ~VT_VALMASK) | out_reg;
1449 store(op->reg, &sv);
1452 store(op->reg, op->vt);
1457 store(TREG_XDX, &sv);
1462 /* generate reg restore code */
1463 for(i = NB_SAVED_REGS - 1; i >= 0; i--) {
1465 if (regs_allocated[reg]) {
1467 if (tcc_state->seg_size == 16)
1476 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str)
1481 if (!strcmp(str, "memory") ||
1484 ts = tok_alloc(str, strlen(str));
1486 if (reg >= TOK_ASM_eax && reg <= TOK_ASM_edi) {
1488 } else if (reg >= TOK_ASM_ax && reg <= TOK_ASM_di) {
1490 #ifdef TCC_TARGET_X86_64
1491 } else if (reg >= TOK_ASM_rax && reg <= TOK_ASM_rdi) {
1495 tcc_error("invalid clobber register '%s'", str);
1497 clobber_regs[reg] = 1;