2 * i386 specific functions for TCC assembler
4 * Copyright (c) 2001, 2002 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define MAX_OPERANDS 3
23 typedef struct ASMInstr
{
27 #define OPC_JMP 0x01 /* jmp operand */
28 #define OPC_B 0x02 /* only used zith OPC_WL */
29 #define OPC_WL 0x04 /* accepts w, l or no suffix */
30 #define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */
31 #define OPC_REG 0x08 /* register is added to opcode */
32 #define OPC_MODRM 0x10 /* modrm encoding */
33 #define OPC_FWAIT 0x20 /* add fwait opcode */
34 #define OPC_TEST 0x40 /* test opcodes */
35 #define OPC_SHIFT 0x80 /* shift opcodes */
36 #define OPC_D16 0x0100 /* generate data16 prefix */
37 #define OPC_ARITH 0x0200 /* arithmetic opcodes */
38 #define OPC_SHORTJMP 0x0400 /* short jmp operand */
39 #define OPC_FARITH 0x0800 /* FPU arithmetic opcodes */
40 #define OPC_GROUP_SHIFT 13
42 /* in order to compress the operand type, we use specific operands and
44 #define OPT_REG8 0 /* warning: value is hardcoded from TOK_ASM_xxx */
45 #define OPT_REG16 1 /* warning: value is hardcoded from TOK_ASM_xxx */
46 #define OPT_REG32 2 /* warning: value is hardcoded from TOK_ASM_xxx */
47 #define OPT_MMX 3 /* warning: value is hardcoded from TOK_ASM_xxx */
48 #define OPT_SSE 4 /* warning: value is hardcoded from TOK_ASM_xxx */
49 #define OPT_CR 5 /* warning: value is hardcoded from TOK_ASM_xxx */
50 #define OPT_TR 6 /* warning: value is hardcoded from TOK_ASM_xxx */
51 #define OPT_DB 7 /* warning: value is hardcoded from TOK_ASM_xxx */
58 #define OPT_EAX 14 /* %al, %ax or %eax register */
59 #define OPT_ST0 15 /* %st(0) register */
60 #define OPT_CL 16 /* %cl register */
61 #define OPT_DX 17 /* %dx register */
62 #define OPT_ADDR 18 /* OP_EA with only offset */
63 #define OPT_INDIR 19 /* *(expr) */
66 #define OPT_COMPOSITE_FIRST 20
67 #define OPT_IM 20 /* IM8 | IM16 | IM32 */
68 #define OPT_REG 21 /* REG8 | REG16 | REG32 */
69 #define OPT_REGW 22 /* REG16 | REG32 */
70 #define OPT_IMW 23 /* IM16 | IM32 */
72 /* can be ored with any OPT_xxx */
76 uint8_t op_type
[MAX_OPERANDS
]; /* see OP_xxx */
79 typedef struct Operand
{
81 #define OP_REG8 (1 << OPT_REG8)
82 #define OP_REG16 (1 << OPT_REG16)
83 #define OP_REG32 (1 << OPT_REG32)
84 #define OP_MMX (1 << OPT_MMX)
85 #define OP_SSE (1 << OPT_SSE)
86 #define OP_CR (1 << OPT_CR)
87 #define OP_TR (1 << OPT_TR)
88 #define OP_DB (1 << OPT_DB)
89 #define OP_SEG (1 << OPT_SEG)
90 #define OP_ST (1 << OPT_ST)
91 #define OP_IM8 (1 << OPT_IM8)
92 #define OP_IM8S (1 << OPT_IM8S)
93 #define OP_IM16 (1 << OPT_IM16)
94 #define OP_IM32 (1 << OPT_IM32)
95 #define OP_EAX (1 << OPT_EAX)
96 #define OP_ST0 (1 << OPT_ST0)
97 #define OP_CL (1 << OPT_CL)
98 #define OP_DX (1 << OPT_DX)
99 #define OP_ADDR (1 << OPT_ADDR)
100 #define OP_INDIR (1 << OPT_INDIR)
102 #define OP_EA 0x40000000
103 #define OP_REG (OP_REG8 | OP_REG16 | OP_REG32)
104 #define OP_IM OP_IM32
105 int8_t reg
; /* register, -1 if none */
106 int8_t reg2
; /* second register, -1 if none */
111 static const uint8_t reg_to_size
[5] = {
117 #define WORD_PREFIX_OPCODE 0x66
119 #define NB_TEST_OPCODES 30
121 static const uint8_t test_bits
[NB_TEST_OPCODES
] = {
154 static const ASMInstr asm_instrs
[] = {
156 #define DEF_ASM_OP0(name, opcode)
157 #define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
158 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
159 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
160 #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 }},
161 #include "i386-asm.h"
167 static const uint16_t op0_codes
[] = {
169 #define DEF_ASM_OP0(x, opcode) opcode,
170 #define DEF_ASM_OP0L(name, opcode, group, instr_type)
171 #define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
172 #define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
173 #define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
174 #include "i386-asm.h"
177 static inline int get_reg_shift(TCCState
*s1
)
181 v
= asm_int_expr(s1
);
196 expect("1, 2, 4 or 8 constant");
203 static int asm_parse_reg(void)
209 if (tok
>= TOK_ASM_eax
&& tok
<= TOK_ASM_edi
) {
210 reg
= tok
- TOK_ASM_eax
;
215 expect("32 bit register");
220 static void parse_operand(TCCState
*s1
, Operand
*op
)
234 if (tok
>= TOK_ASM_al
&& tok
<= TOK_ASM_db7
) {
235 reg
= tok
- TOK_ASM_al
;
236 op
->type
= 1 << (reg
>> 3); /* WARNING: do not change constant order */
238 if ((op
->type
& OP_REG
) && op
->reg
== TREG_EAX
)
240 else if (op
->type
== OP_REG8
&& op
->reg
== TREG_ECX
)
242 else if (op
->type
== OP_REG16
&& op
->reg
== TREG_EDX
)
244 } else if (tok
>= TOK_ASM_dr0
&& tok
<= TOK_ASM_dr7
) {
246 op
->reg
= tok
- TOK_ASM_dr0
;
247 } else if (tok
>= TOK_ASM_es
&& tok
<= TOK_ASM_gs
) {
249 op
->reg
= tok
- TOK_ASM_es
;
250 } else if (tok
== TOK_ASM_st
) {
256 if (tok
!= TOK_PPNUM
)
260 if ((unsigned)reg
>= 8 || p
[1] != '\0')
271 error("unknown register");
275 } else if (tok
== '$') {
283 if (op
->e
.v
== (uint8_t)op
->e
.v
)
285 if (op
->e
.v
== (int8_t)op
->e
.v
)
287 if (op
->e
.v
== (uint16_t)op
->e
.v
)
291 /* address(reg,reg2,shift) with all variants */
307 op
->reg
= asm_parse_reg();
312 op
->reg2
= asm_parse_reg();
315 op
->shift
= get_reg_shift(s1
);
319 if (op
->reg
== -1 && op
->reg2
== -1)
325 /* XXX: unify with C code output ? */
326 static void gen_expr32(ExprValue
*pe
)
329 greloc(cur_text_section
, pe
->sym
, ind
, R_386_32
);
333 /* XXX: unify with C code output ? */
334 static void gen_disp32(ExprValue
*pe
)
339 if (sym
->r
== cur_text_section
->sh_num
) {
340 /* same section: we can output an absolute value. Note
341 that the TCC compiler behaves differently here because
342 it always outputs a relocation to ease (future) code
343 elimination in the linker */
344 gen_le32(pe
->v
+ (long)sym
->next
- ind
- 4);
346 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
350 /* put an empty PC32 relocation */
351 put_elf_reloc(symtab_section
, cur_text_section
,
358 static void gen_le16(int v
)
364 /* generate the modrm operand */
365 static inline void asm_modrm(int reg
, Operand
*op
)
369 if (op
->type
& (OP_REG
| OP_MMX
| OP_SSE
)) {
370 g(0xc0 + (reg
<< 3) + op
->reg
);
371 } else if (op
->reg
== -1 && op
->reg2
== -1) {
372 /* displacement only */
373 g(0x05 + (reg
<< 3));
376 /* fist compute displacement encoding */
377 if (op
->e
.v
== 0 && !op
->e
.sym
&& op
->reg
!= 5) {
379 } else if (op
->e
.v
== (int8_t)op
->e
.v
&& !op
->e
.sym
) {
384 /* compute if sib byte needed */
388 g(mod
+ (reg
<< 3) + reg1
);
393 reg2
= 4; /* indicate no index */
394 g((op
->shift
<< 6) + (reg2
<< 3) + op
->reg
);
400 } else if (mod
== 0x80) {
406 static void asm_opcode(TCCState
*s1
, int opcode
)
409 int i
, modrm_index
, reg
, v
, op1
, is_short_jmp
;
411 Operand ops
[MAX_OPERANDS
], *pop
;
412 int op_type
[3]; /* decoded op type */
418 if (tok
== ';' || tok
== TOK_LINEFEED
)
420 if (nb_ops
>= MAX_OPERANDS
) {
421 error("incorrect number of operands");
423 parse_operand(s1
, pop
);
432 s
= 0; /* avoid warning */
434 /* optimize matching by using a lookup table (no hashing is needed
436 for(pa
= asm_instrs
; pa
->sym
!= 0; pa
++) {
438 if (pa
->instr_type
& OPC_FARITH
) {
439 v
= opcode
- pa
->sym
;
440 if (!((unsigned)v
< 8 * 6 && (v
% 6) == 0))
442 } else if (pa
->instr_type
& OPC_ARITH
) {
443 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 8 * 4))
446 } else if (pa
->instr_type
& OPC_SHIFT
) {
447 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ 7 * 4))
450 } else if (pa
->instr_type
& OPC_TEST
) {
451 if (!(opcode
>= pa
->sym
&& opcode
< pa
->sym
+ NB_TEST_OPCODES
))
453 } else if (pa
->instr_type
& OPC_B
) {
454 if (!(opcode
>= pa
->sym
&& opcode
<= pa
->sym
+ 3))
457 s
= (opcode
- pa
->sym
) & 3;
458 } else if (pa
->instr_type
& OPC_WL
) {
459 if (!(opcode
>= pa
->sym
&& opcode
<= pa
->sym
+ 2))
461 s
= opcode
- pa
->sym
+ 1;
463 if (pa
->sym
!= opcode
)
466 if (pa
->nb_ops
!= nb_ops
)
468 /* now decode and check each operand */
469 for(i
= 0; i
< nb_ops
; i
++) {
471 op1
= pa
->op_type
[i
];
475 v
= OP_IM8
| OP_IM16
| OP_IM32
;
478 v
= OP_REG8
| OP_REG16
| OP_REG32
;
481 v
= OP_REG16
| OP_REG32
;
484 v
= OP_IM16
| OP_IM32
;
493 if ((ops
[i
].type
& v
) == 0)
496 /* all is matching ! */
501 if (opcode
>= TOK_ASM_pusha
&& opcode
<= TOK_ASM_emms
) {
503 b
= op0_codes
[opcode
- TOK_ASM_pusha
];
509 error("unknown opcode '%s'",
510 get_tok_str(opcode
, NULL
));
513 /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
515 for(i
= 0; s
== 3 && i
< nb_ops
; i
++) {
516 if ((ops
[i
].type
& OP_REG
) && !(op_type
[i
] & (OP_CL
| OP_DX
)))
517 s
= reg_to_size
[ops
[i
].type
& OP_REG
];
520 error("cannot infer opcode suffix");
524 /* generate data16 prefix if needed */
526 if (s
== 1 || (pa
->instr_type
& OPC_D16
))
527 g(WORD_PREFIX_OPCODE
);
530 /* now generates the operation */
531 if (pa
->instr_type
& OPC_FWAIT
)
535 if (v
== 0x69 || v
== 0x69) {
536 /* kludge for imul $im, %reg */
539 } else if (v
== 0xcd && ops
[0].e
.v
== 3 && !ops
[0].e
.sym
) {
540 v
--; /* int $3 case */
542 } else if ((v
== 0x06 || v
== 0x07)) {
543 if (ops
[0].reg
>= 4) {
544 /* push/pop %fs or %gs */
545 v
= 0x0fa0 + (v
- 0x06) + ((ops
[0].reg
- 4) << 3);
547 v
+= ops
[0].reg
<< 3;
550 } else if (v
<= 0x05) {
552 v
+= ((opcode
- TOK_ASM_addb
) >> 2) << 3;
553 } else if ((pa
->instr_type
& (OPC_FARITH
| OPC_MODRM
)) == OPC_FARITH
) {
555 v
+= ((opcode
- pa
->sym
) / 6) << 3;
557 if (pa
->instr_type
& OPC_REG
) {
558 for(i
= 0; i
< nb_ops
; i
++) {
559 if (op_type
[i
] & (OP_REG
| OP_ST
)) {
564 /* mov $im, %reg case */
565 if (pa
->opcode
== 0xb0 && s
>= 1)
568 if (pa
->instr_type
& OPC_B
)
570 if (pa
->instr_type
& OPC_TEST
)
571 v
+= test_bits
[opcode
- pa
->sym
];
572 if (pa
->instr_type
& OPC_SHORTJMP
) {
576 /* see if we can really generate the jump with a byte offset */
580 if (sym
->r
!= cur_text_section
->sh_num
)
582 jmp_disp
= ops
[0].e
.v
+ (long)sym
->next
- ind
- 2;
583 if (jmp_disp
== (int8_t)jmp_disp
) {
584 /* OK to generate jump */
586 ops
[0].e
.v
= jmp_disp
;
589 if (pa
->instr_type
& OPC_JMP
) {
590 /* long jump will be allowed. need to modify the
597 error("invalid displacement");
606 /* search which operand will used for modrm */
608 if (pa
->instr_type
& OPC_SHIFT
) {
609 reg
= (opcode
- pa
->sym
) >> 2;
612 } else if (pa
->instr_type
& OPC_ARITH
) {
613 reg
= (opcode
- pa
->sym
) >> 2;
614 } else if (pa
->instr_type
& OPC_FARITH
) {
615 reg
= (opcode
- pa
->sym
) / 6;
617 reg
= (pa
->instr_type
>> OPC_GROUP_SHIFT
) & 7;
619 if (pa
->instr_type
& OPC_MODRM
) {
620 /* first look for an ea operand */
621 for(i
= 0;i
< nb_ops
; i
++) {
622 if (op_type
[i
] & OP_EA
)
625 /* then if not found, a register or indirection (shift instructions) */
626 for(i
= 0;i
< nb_ops
; i
++) {
627 if (op_type
[i
] & (OP_REG
| OP_MMX
| OP_SSE
| OP_INDIR
))
631 error("bad op table");
635 /* if a register is used in another operand then it is
636 used instead of group */
637 for(i
= 0;i
< nb_ops
; i
++) {
639 if (i
!= modrm_index
&&
640 (v
& (OP_REG
| OP_MMX
| OP_SSE
| OP_CR
| OP_TR
| OP_DB
| OP_SEG
))) {
646 asm_modrm(reg
, &ops
[modrm_index
]);
650 if (pa
->opcode
== 0x9a || pa
->opcode
== 0xea) {
651 /* ljmp or lcall kludge */
652 gen_expr32(&ops
[1].e
);
654 error("cannot relocate");
655 gen_le16(ops
[0].e
.v
);
657 for(i
= 0;i
< nb_ops
; i
++) {
659 if (v
& (OP_IM8
| OP_IM16
| OP_IM32
| OP_IM8S
| OP_ADDR
)) {
660 /* if multiple sizes are given it means we must look
662 if (v
== (OP_IM8
| OP_IM16
| OP_IM32
) ||
663 v
== (OP_IM16
| OP_IM32
)) {
671 if (v
& (OP_IM8
| OP_IM8S
)) {
675 } else if (v
& OP_IM16
) {
678 error("cannot relocate");
680 gen_le16(ops
[i
].e
.v
);
682 if (pa
->instr_type
& (OPC_JMP
| OPC_SHORTJMP
)) {
686 gen_disp32(&ops
[i
].e
);
688 gen_expr32(&ops
[i
].e
);
696 #define NB_SAVED_REGS 3
697 #define NB_ASM_REGS 8
699 /* return the constraint priority (we allocate first the lowest
700 numbered constraints) */
701 static inline int constraint_priority(const char *str
)
705 /* we take the lowest priority */
739 error("unknown constraint '%c'", c
);
748 static const char *skip_constraint_modifiers(const char *p
)
750 while (*p
== '=' || *p
== '&' || *p
== '+' || *p
== '%')
755 static void asm_compute_constraints(uint8_t *regs_allocated
,
756 ASMOperand
*operands
,
757 int nb_operands1
, int nb_outputs
,
759 uint8_t *input_regs_allocated
)
762 int sorted_op
[MAX_ASM_OPERANDS
];
763 int i
, j
, k
, p1
, p2
, tmp
, reg
, c
, base
, nb_operands
;
768 nb_operands
= nb_outputs
;
771 nb_operands
= nb_operands1
- nb_outputs
;
774 /* compute constraint priority and evaluate references to output
775 constraints if input constraints */
776 for(i
=0;i
<nb_operands
;i
++) {
779 str
= op
->constraint
;
782 str
= skip_constraint_modifiers(str
);
783 if (!is_output
&& (isnum(*str
) || *str
== '[')) {
784 /* this is a reference to another constraint */
785 k
= find_constraint(operands
, nb_operands1
, str
, NULL
);
786 if ((unsigned)k
>= j
)
787 error("invalid reference in constraint %d ('%s')",
790 str
= operands
[k
].constraint
;
791 str
= skip_constraint_modifiers(str
);
793 op
->priority
= constraint_priority(str
);
796 /* sort operands according to their priority */
797 for(i
=0;i
<nb_operands
;i
++)
798 sorted_op
[i
] = base
+ i
;
799 for(i
=0;i
<nb_operands
- 1;i
++) {
800 for(j
=i
+1;j
<nb_operands
;j
++) {
801 p1
= operands
[sorted_op
[i
]].priority
;
802 p2
= operands
[sorted_op
[j
]].priority
;
805 sorted_op
[i
] = sorted_op
[j
];
811 memset(regs_allocated
, 0, NB_ASM_REGS
);
812 regs_allocated
[4] = 1; /* esp cannot be used */
813 regs_allocated
[5] = 1; /* ebp cannot be used yet */
815 /* allocate registers and generate corresponding asm moves */
816 for(i
=0;i
<nb_operands
;i
++) {
819 str
= op
->constraint
;
821 if (op
->ref_index
>= 0) {
822 str
= operands
[op
->ref_index
].constraint
;
825 str
= skip_constraint_modifiers(str
);
830 /* allocate both eax and edx */
831 if (regs_allocated
[TREG_EAX
] || regs_allocated
[TREG_EDX
])
835 regs_allocated
[TREG_EAX
] = 1;
836 regs_allocated
[TREG_EDX
] = 1;
856 if (regs_allocated
[reg
])
860 /* eax, ebx, ecx or edx */
861 for(reg
= 0; reg
< 4; reg
++) {
862 if (!regs_allocated
[reg
])
867 /* any general register */
868 for(reg
= 0; reg
< 8; reg
++) {
869 if (!regs_allocated
[reg
])
874 /* now we can reload in the register */
877 regs_allocated
[reg
] = 1;
880 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
))
886 if (!((op
->vt
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
))
891 /* nothing special to do because the operand is
893 /* XXX: fix constant case */
895 /* if it is a reference to a memory zone, it must lie
896 in a register, so we reserve the register in the
897 input registers and a load will be generated
899 if ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
) {
900 /* any general register */
901 for(reg
= 0; reg
< 8; reg
++) {
902 if (!input_regs_allocated
[reg
])
907 /* now we can reload in the register */
908 input_regs_allocated
[reg
] = 1;
914 error("asm constraint %d ('%s') could not be satisfied",
920 /* print sorted constraints */
923 printf("outputs=\n");
926 for(i
=0;i
<nb_operands
;i
++) {
929 printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
931 op
->id
? get_tok_str(op
->id
, NULL
) : "",
939 static void subst_asm_operand(CString
*add_str
,
940 SValue
*sv
, int modifier
)
942 int r
, reg
, size
, val
;
946 if ((r
& VT_VALMASK
) == VT_CONST
) {
947 if (!(r
& VT_LVAL
) && modifier
!= 'c' && modifier
!= 'n')
948 cstr_ccat(add_str
, '$');
950 cstr_cat(add_str
, get_tok_str(sv
->sym
->v
, NULL
));
952 cstr_ccat(add_str
, '+');
960 snprintf(buf
, sizeof(buf
), "%d", sv
->c
.i
);
961 cstr_cat(add_str
, buf
);
962 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
963 snprintf(buf
, sizeof(buf
), "%d(%%ebp)", sv
->c
.i
);
964 cstr_cat(add_str
, buf
);
965 } else if (r
& VT_LVAL
) {
966 reg
= r
& VT_VALMASK
;
968 error("internal compiler error");
969 snprintf(buf
, sizeof(buf
), "(%%%s)",
970 get_tok_str(TOK_ASM_eax
+ reg
, NULL
));
971 cstr_cat(add_str
, buf
);
974 reg
= r
& VT_VALMASK
;
976 error("internal compiler error");
978 /* choose register operand size */
979 if ((sv
->type
.t
& VT_BTYPE
) == VT_BYTE
)
981 else if ((sv
->type
.t
& VT_BTYPE
) == VT_SHORT
)
985 if (size
== 1 && reg
>= 4)
988 if (modifier
== 'b') {
990 error("cannot use byte register");
992 } else if (modifier
== 'h') {
994 error("cannot use byte register");
996 } else if (modifier
== 'w') {
1002 reg
= TOK_ASM_ah
+ reg
;
1005 reg
= TOK_ASM_al
+ reg
;
1008 reg
= TOK_ASM_ax
+ reg
;
1011 reg
= TOK_ASM_eax
+ reg
;
1014 snprintf(buf
, sizeof(buf
), "%%%s", get_tok_str(reg
, NULL
));
1015 cstr_cat(add_str
, buf
);
1019 /* generate prolog and epilog code for asm statment */
1020 static void asm_gen_code(ASMOperand
*operands
, int nb_operands
,
1021 int nb_outputs
, int is_output
,
1022 uint8_t *clobber_regs
)
1024 uint8_t regs_allocated
[NB_ASM_REGS
];
1027 static uint8_t reg_saved
[NB_SAVED_REGS
] = { 3, 6, 7 };
1029 /* mark all used registers */
1030 memcpy(regs_allocated
, clobber_regs
, sizeof(regs_allocated
));
1031 for(i
= 0; i
< nb_operands
;i
++) {
1034 regs_allocated
[op
->reg
] = 1;
1037 /* generate reg save code */
1038 for(i
= 0; i
< NB_SAVED_REGS
; i
++) {
1040 if (regs_allocated
[reg
])
1044 /* generate load code */
1045 for(i
= nb_outputs
; i
< nb_operands
; i
++) {
1048 load(op
->reg
, op
->vt
);
1053 load(TREG_EDX
, &sv
);
1057 /* generate load code for output memory references */
1058 for(i
= 0 ; i
< nb_outputs
; i
++) {
1060 if (op
->reg
>= 0 && ((op
->vt
->r
& VT_VALMASK
) == VT_LLOCAL
)) {
1063 sv
.r
= (sv
.r
& ~VT_VALMASK
) | VT_LOCAL
;
1068 /* generate save code */
1069 for(i
= 0 ; i
< nb_outputs
; i
++) {
1071 if (op
->reg
>= 0 && ((op
->vt
->r
& VT_VALMASK
) != VT_LLOCAL
)) {
1072 store(op
->reg
, op
->vt
);
1077 store(TREG_EDX
, &sv
);
1081 /* generate reg restore code */
1082 for(i
= NB_SAVED_REGS
- 1; i
>= 0; i
--) {
1084 if (regs_allocated
[reg
])
1090 static void asm_clobber(uint8_t *clobber_regs
, const char *str
)
1095 if (!strcmp(str
, "memory") ||
1098 ts
= tok_alloc(str
, strlen(str
));
1100 if (reg
>= TOK_ASM_eax
&& reg
<= TOK_ASM_edi
) {
1102 } else if (reg
>= TOK_ASM_ax
&& reg
<= TOK_ASM_di
) {
1105 error("invalid clobber register '%s'", str
);
1107 clobber_regs
[reg
] = 1;