1 /* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GAS 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 operatorT op_modifier
; /* Operand modifier. */
25 int is_mem
; /* 1 if operand is memory reference. */
26 unsigned int in_offset
; /* >=1 if processing operand of offset. */
27 unsigned int in_bracket
; /* >=1 if processing operand in brackets. */
28 unsigned int in_scale
; /* >=1 if processing multipication operand
30 i386_operand_type reloc_types
; /* Value obtained from lex_got(). */
31 const reg_entry
*base
; /* Base register (if any). */
32 const reg_entry
*index
; /* Index register (if any). */
33 offsetT scale_factor
; /* Accumulated scale factor. */
38 /* offset X_add_symbol */
39 #define O_offset O_md32
40 /* offset X_add_symbol */
41 #define O_short O_md31
42 /* near ptr X_add_symbol */
43 #define O_near_ptr O_md30
44 /* far ptr X_add_symbol */
45 #define O_far_ptr O_md29
46 /* byte ptr X_add_symbol */
47 #define O_byte_ptr O_md28
48 /* word ptr X_add_symbol */
49 #define O_word_ptr O_md27
50 /* dword ptr X_add_symbol */
51 #define O_dword_ptr O_md26
52 /* qword ptr X_add_symbol */
53 #define O_qword_ptr O_md25
54 /* oword ptr X_add_symbol */
55 #define O_oword_ptr O_md24
56 /* fword ptr X_add_symbol */
57 #define O_fword_ptr O_md23
58 /* tbyte ptr X_add_symbol */
59 #define O_tbyte_ptr O_md22
60 /* xmmword ptr X_add_symbol */
61 #define O_xmmword_ptr O_md21
62 /* ymmword ptr X_add_symbol */
63 #define O_ymmword_ptr O_md20
69 unsigned int operands
;
71 const i386_operators
[] =
73 { "and", O_bit_and
, 2 },
79 { "mod", O_modulus
, 2 },
81 { "not", O_bit_not
, 1 },
82 { "offset", O_offset
, 1 },
83 { "or", O_bit_inclusive_or
, 2 },
84 { "shl", O_left_shift
, 2 },
85 { "short", O_short
, 1 },
86 { "shr", O_right_shift
, 2 },
87 { "xor", O_bit_exclusive_or
, 2 },
88 { NULL
, O_illegal
, 0 }
99 #define I386_TYPE(t, n) { #t, O_##t##_ptr, { n, n, n } }
105 I386_TYPE(tbyte
, 10),
106 I386_TYPE(oword
, 16),
107 I386_TYPE(xmmword
, 16),
108 I386_TYPE(ymmword
, 32),
110 { "near", O_near_ptr
, { 0xff04, 0xff02, 0xff08 } },
111 { "far", O_far_ptr
, { 0xff06, 0xff05, 0xff06 } },
112 { NULL
, O_illegal
, { 0, 0, 0 } }
115 operatorT
i386_operator (const char *name
, unsigned int operands
, char *pc
)
126 switch (*input_line_pointer
)
129 ++input_line_pointer
;
132 ++input_line_pointer
;
135 if (this_operand
>= 0 && i
.reloc
[this_operand
] == NO_RELOC
)
138 char *gotfree_input_line
= lex_got (&i
.reloc
[this_operand
],
140 &intel_state
.reloc_types
);
142 if (!gotfree_input_line
)
144 free (gotfree_input_line
);
145 *input_line_pointer
++ = '+';
146 memset (input_line_pointer
, '0', adjust
- 1);
147 input_line_pointer
[adjust
- 1] = ' ';
155 for (j
= 0; i386_operators
[j
].name
; ++j
)
156 if (strcasecmp(i386_operators
[j
].name
, name
) == 0)
158 if (i386_operators
[j
].operands
159 && i386_operators
[j
].operands
!= operands
)
161 return i386_operators
[j
].operator;
164 for (j
= 0; i386_types
[j
].name
; ++j
)
165 if (strcasecmp(i386_types
[j
].name
, name
) == 0)
167 if (i386_types
[j
].name
&& *pc
== ' ')
169 char *name
= ++input_line_pointer
;
170 char c
= get_symbol_end ();
172 if (strcasecmp (name
, "ptr") == 0)
176 if (intel_syntax
> 0 || operands
!= 1)
178 return i386_types
[j
].operator;
181 *input_line_pointer
= c
;
182 input_line_pointer
= name
- 1;
188 static int i386_intel_parse_name (const char *name
, expressionS
*e
)
192 for (i
= 0; i386_types
[i
].name
; ++i
)
193 if (strcasecmp(i386_types
[i
].name
, name
) == 0)
195 e
->X_op
= O_constant
;
196 e
->X_add_number
= i386_types
[i
].sz
[flag_code
];
197 e
->X_add_symbol
= NULL
;
198 e
->X_op_symbol
= NULL
;
205 static INLINE
int i386_intel_check (const reg_entry
*reg
,
206 const reg_entry
*base
,
207 const reg_entry
*index
)
209 if ((this_operand
>= 0 && reg
!= i
.op
[this_operand
].regs
)
210 || base
!= intel_state
.base
|| index
!= intel_state
.index
)
212 as_bad (_("invalid use of register"));
218 static INLINE
void i386_intel_fold (expressionS
*e
, symbolS
*sym
)
220 if (S_GET_SEGMENT (sym
) == absolute_section
)
222 offsetT val
= e
->X_add_number
;
224 *e
= *symbol_get_value_expression (sym
);
225 e
->X_add_number
+= val
;
229 e
->X_add_symbol
= sym
;
230 e
->X_op_symbol
= NULL
;
235 static int i386_intel_simplify (expressionS
*);
237 static INLINE
int i386_intel_simplify_symbol(symbolS
*sym
)
239 int ret
= i386_intel_simplify (symbol_get_value_expression (sym
));
243 S_SET_SEGMENT(sym
, absolute_section
);
249 static int i386_intel_simplify (expressionS
*e
)
251 const reg_entry
*reg
= this_operand
>= 0 ? i
.op
[this_operand
].regs
: NULL
;
252 const reg_entry
*base
= intel_state
.base
;
253 const reg_entry
*index
= intel_state
.index
;
264 if (!i386_intel_simplify_symbol (e
->X_add_symbol
)
265 || !i386_intel_check(reg
, intel_state
.base
, intel_state
.index
))
268 if (!intel_state
.in_offset
)
269 ++intel_state
.in_bracket
;
270 ret
= i386_intel_simplify_symbol (e
->X_op_symbol
);
271 if (!intel_state
.in_offset
)
272 --intel_state
.in_bracket
;
278 i386_intel_fold (e
, e
->X_op_symbol
);
282 ++intel_state
.in_offset
;
283 ret
= i386_intel_simplify_symbol (e
->X_add_symbol
);
284 --intel_state
.in_offset
;
285 if (!ret
|| !i386_intel_check(reg
, base
, index
))
287 i386_intel_fold (e
, e
->X_add_symbol
);
301 if (intel_state
.op_modifier
== O_absent
)
302 intel_state
.op_modifier
= e
->X_op
;
305 if (symbol_get_value_expression (e
->X_add_symbol
)->X_op
== O_register
)
307 as_bad (_("invalid use of register"));
310 if (!i386_intel_simplify_symbol (e
->X_add_symbol
))
312 i386_intel_fold (e
, e
->X_add_symbol
);
316 if (symbol_get_value_expression (e
->X_op_symbol
)->X_op
== O_register
)
318 as_bad (_("invalid use of register"));
321 if (!i386_intel_simplify_symbol (e
->X_op_symbol
)
322 || !i386_intel_check(reg
, intel_state
.base
, intel_state
.index
))
324 if (!intel_state
.in_offset
)
325 intel_state
.seg
= e
->X_add_symbol
;
326 i386_intel_fold (e
, e
->X_op_symbol
);
330 if (this_operand
< 0 || intel_state
.in_offset
)
332 as_bad (_("invalid use of register"));
335 if (!intel_state
.in_bracket
)
337 if (i
.op
[this_operand
].regs
)
339 as_bad (_("invalid use of register"));
342 if (i386_regtab
[e
->X_add_number
].reg_type
.bitfield
.sreg3
343 && i386_regtab
[e
->X_add_number
].reg_num
== RegFlat
)
345 as_bad (_("invalid use of pseudo-register"));
348 i
.op
[this_operand
].regs
= i386_regtab
+ e
->X_add_number
;
350 else if (!intel_state
.base
&& !intel_state
.in_scale
)
351 intel_state
.base
= i386_regtab
+ e
->X_add_number
;
352 else if (!intel_state
.index
)
353 intel_state
.index
= i386_regtab
+ e
->X_add_number
;
356 /* esp is invalid as index */
357 intel_state
.index
= i386_regtab
+ REGNAM_EAX
+ 4;
359 e
->X_op
= O_constant
;
364 if (this_operand
>= 0 && intel_state
.in_bracket
)
366 expressionS
*scale
= NULL
;
368 if (intel_state
.index
)
371 if (!intel_state
.in_scale
++)
372 intel_state
.scale_factor
= 1;
374 ret
= i386_intel_simplify_symbol (e
->X_add_symbol
);
375 if (ret
&& !scale
&& intel_state
.index
)
376 scale
= symbol_get_value_expression (e
->X_op_symbol
);
379 ret
= i386_intel_simplify_symbol (e
->X_op_symbol
);
380 if (ret
&& !scale
&& intel_state
.index
)
381 scale
= symbol_get_value_expression (e
->X_add_symbol
);
383 if (ret
&& scale
&& (scale
+ 1))
385 resolve_expression (scale
);
386 if (scale
->X_op
!= O_constant
387 || intel_state
.index
->reg_type
.bitfield
.reg16
)
388 scale
->X_add_number
= 0;
389 intel_state
.scale_factor
*= scale
->X_add_number
;
392 --intel_state
.in_scale
;
396 if (!intel_state
.in_scale
)
397 switch (intel_state
.scale_factor
)
400 i
.log2_scale_factor
= 0;
403 i
.log2_scale_factor
= 1;
406 i
.log2_scale_factor
= 2;
409 i
.log2_scale_factor
= 3;
412 /* esp is invalid as index */
413 intel_state
.index
= i386_regtab
+ REGNAM_EAX
+ 4;
421 if (e
->X_add_symbol
&& !i386_intel_simplify_symbol (e
->X_add_symbol
))
423 if (e
->X_op
== O_add
|| e
->X_op
== O_subtract
)
425 base
= intel_state
.base
;
426 index
= intel_state
.index
;
428 if (!i386_intel_check (reg
, base
, index
)
429 || (e
->X_op_symbol
&& !i386_intel_simplify_symbol (e
->X_op_symbol
))
430 || !i386_intel_check (reg
,
431 e
->X_op
!= O_add
? base
: intel_state
.base
,
432 e
->X_op
!= O_add
? index
: intel_state
.index
))
437 if (this_operand
>= 0 && e
->X_op
== O_symbol
&& !intel_state
.in_offset
)
439 segT seg
= S_GET_SEGMENT (e
->X_add_symbol
);
441 if (seg
!= absolute_section
442 && seg
!= reg_section
443 && seg
!= expr_section
)
444 intel_state
.is_mem
|= 2 - !intel_state
.in_bracket
;
450 int i386_need_index_operator (void)
452 return intel_syntax
< 0;
456 i386_intel_operand (char *operand_string
, int got_a_float
)
458 char *saved_input_line_pointer
, *buf
;
460 expressionS exp
, *expP
;
464 /* Initialize state structure. */
465 intel_state
.op_modifier
= O_absent
;
466 intel_state
.is_mem
= 0;
467 intel_state
.base
= NULL
;
468 intel_state
.index
= NULL
;
469 intel_state
.seg
= NULL
;
470 operand_type_set (&intel_state
.reloc_types
, ~0);
471 assert (!intel_state
.in_offset
);
472 assert (!intel_state
.in_bracket
);
473 assert (!intel_state
.in_scale
);
475 saved_input_line_pointer
= input_line_pointer
;
476 input_line_pointer
= buf
= xstrdup (operand_string
);
479 memset (&exp
, 0, sizeof(exp
));
480 exp_seg
= expression (&exp
);
481 ret
= i386_intel_simplify (&exp
);
485 if (!is_end_of_line
[(unsigned char) *input_line_pointer
])
487 as_bad (_("junk `%s' after expression"), input_line_pointer
);
490 else if (exp
.X_op
== O_illegal
|| exp
.X_op
== O_absent
)
492 as_bad (_("invalid expression"));
496 input_line_pointer
= saved_input_line_pointer
;
499 assert (!intel_state
.in_offset
);
500 assert (!intel_state
.in_bracket
);
501 assert (!intel_state
.in_scale
);
506 if (intel_state
.op_modifier
!= O_absent
507 && current_templates
->start
->base_opcode
!= 0x8d /* lea */)
509 i
.types
[this_operand
].bitfield
.unspecified
= 0;
511 switch (intel_state
.op_modifier
)
514 i
.types
[this_operand
].bitfield
.byte
= 1;
515 suffix
= BYTE_MNEM_SUFFIX
;
519 i
.types
[this_operand
].bitfield
.word
= 1;
520 if ((current_templates
->start
->name
[0] == 'l'
521 && current_templates
->start
->name
[2] == 's'
522 && current_templates
->start
->name
[3] == 0)
523 || current_templates
->start
->base_opcode
== 0x62 /* bound */)
524 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
525 else if (got_a_float
== 2) /* "fi..." */
526 suffix
= SHORT_MNEM_SUFFIX
;
528 suffix
= WORD_MNEM_SUFFIX
;
532 i
.types
[this_operand
].bitfield
.dword
= 1;
533 if ((current_templates
->start
->name
[0] == 'l'
534 && current_templates
->start
->name
[2] == 's'
535 && current_templates
->start
->name
[3] == 0)
536 || current_templates
->start
->base_opcode
== 0x62 /* bound */)
537 suffix
= WORD_MNEM_SUFFIX
;
538 else if (flag_code
== CODE_16BIT
539 && (current_templates
->start
->opcode_modifier
.jump
540 || current_templates
->start
->opcode_modifier
.jumpdword
))
541 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
542 else if (got_a_float
== 1) /* "f..." */
543 suffix
= SHORT_MNEM_SUFFIX
;
545 suffix
= LONG_MNEM_SUFFIX
;
549 i
.types
[this_operand
].bitfield
.fword
= 1;
550 if (current_templates
->start
->name
[0] == 'l'
551 && current_templates
->start
->name
[2] == 's'
552 && current_templates
->start
->name
[3] == 0)
553 suffix
= LONG_MNEM_SUFFIX
;
554 else if (!got_a_float
)
556 if (flag_code
== CODE_16BIT
)
557 add_prefix (DATA_PREFIX_OPCODE
);
558 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
561 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
565 i
.types
[this_operand
].bitfield
.qword
= 1;
566 if (current_templates
->start
->base_opcode
== 0x62 /* bound */
567 || got_a_float
== 1) /* "f..." */
568 suffix
= LONG_MNEM_SUFFIX
;
570 suffix
= QWORD_MNEM_SUFFIX
;
574 i
.types
[this_operand
].bitfield
.tbyte
= 1;
575 if (got_a_float
== 1)
576 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
578 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
583 i
.types
[this_operand
].bitfield
.xmmword
= 1;
584 suffix
= XMMWORD_MNEM_SUFFIX
;
588 i
.types
[this_operand
].bitfield
.ymmword
= 1;
589 suffix
= YMMWORD_MNEM_SUFFIX
;
593 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
596 if (!current_templates
->start
->opcode_modifier
.jump
597 && !current_templates
->start
->opcode_modifier
.jumpdword
)
598 suffix
= got_a_float
/* so it will cause an error */
600 : LONG_DOUBLE_MNEM_SUFFIX
;
604 BAD_CASE (intel_state
.op_modifier
);
610 else if (i
.suffix
!= suffix
)
612 as_bad (_("conflicting operand size modifiers"));
617 /* Operands for jump/call need special consideration. */
618 if (current_templates
->start
->opcode_modifier
.jump
619 || current_templates
->start
->opcode_modifier
.jumpdword
620 || current_templates
->start
->opcode_modifier
.jumpintersegment
)
622 if (i
.op
[this_operand
].regs
|| intel_state
.base
|| intel_state
.index
623 || intel_state
.is_mem
> 1)
624 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
626 switch (intel_state
.op_modifier
)
630 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
632 intel_state
.is_mem
= 1;
636 if (!intel_state
.seg
)
638 intel_state
.is_mem
= 1;
639 if (intel_state
.op_modifier
== O_absent
)
641 as_bad (_("cannot infer the segment part of the operand"));
644 else if (S_GET_SEGMENT (intel_state
.seg
) == reg_section
)
645 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
648 i386_operand_type types
;
650 if (i
.imm_operands
>= MAX_IMMEDIATE_OPERANDS
)
652 as_bad (_("at most %d immediate operands are allowed"),
653 MAX_IMMEDIATE_OPERANDS
);
656 expP
= &im_expressions
[i
.imm_operands
++];
657 memset (expP
, 0, sizeof(*expP
));
658 expP
->X_op
= O_symbol
;
659 expP
->X_add_symbol
= intel_state
.seg
;
660 i
.op
[this_operand
].imms
= expP
;
662 resolve_expression (expP
);
663 operand_type_set (&types
, ~0);
664 if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state
.seg
),
665 expP
, types
, operand_string
))
667 if (i
.operands
< MAX_OPERANDS
)
669 this_operand
= i
.operands
++;
670 i
.types
[this_operand
].bitfield
.unspecified
= 1;
672 if (suffix
== LONG_DOUBLE_MNEM_SUFFIX
)
674 intel_state
.seg
= NULL
;
675 intel_state
.is_mem
= 0;
679 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
682 if (i
.types
[this_operand
].bitfield
.jumpabsolute
)
683 intel_state
.is_mem
|= 1;
685 else if (intel_state
.seg
)
686 intel_state
.is_mem
|= 1;
688 if (i
.op
[this_operand
].regs
)
690 i386_operand_type temp
;
692 /* Register operand. */
693 if (intel_state
.base
|| intel_state
.index
|| intel_state
.seg
)
695 as_bad (_("invalid operand"));
699 temp
= i
.op
[this_operand
].regs
->reg_type
;
700 temp
.bitfield
.baseindex
= 0;
701 i
.types
[this_operand
] = operand_type_or (i
.types
[this_operand
], temp
);
702 i
.types
[this_operand
].bitfield
.unspecified
= 0;
705 else if (intel_state
.base
|| intel_state
.index
|| intel_state
.seg
706 || intel_state
.is_mem
)
708 /* Memory operand. */
710 >= 2 - !current_templates
->start
->opcode_modifier
.isstring
)
712 as_bad (_("too many memory references for `%s'"),
713 current_templates
->start
->name
);
717 expP
= &disp_expressions
[i
.disp_operands
];
718 memcpy (expP
, &exp
, sizeof(exp
));
719 resolve_expression (expP
);
721 if (expP
->X_op
!= O_constant
|| expP
->X_add_number
722 || (!intel_state
.base
&& !intel_state
.index
))
724 i
.op
[this_operand
].disps
= expP
;
727 if (flag_code
== CODE_64BIT
)
729 i
.types
[this_operand
].bitfield
.disp32
= 1;
730 if (!i
.prefix
[ADDR_PREFIX
])
732 i
.types
[this_operand
].bitfield
.disp64
= 1;
733 i
.types
[this_operand
].bitfield
.disp32s
= 1;
736 else if (!i
.prefix
[ADDR_PREFIX
] ^ (flag_code
== CODE_16BIT
))
737 i
.types
[this_operand
].bitfield
.disp32
= 1;
739 i
.types
[this_operand
].bitfield
.disp16
= 1;
741 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
743 * exp_seg is used only for verification in
744 * i386_finalize_displacement, and we can end up seeing reg_section
745 * here - but we know we removed all registers from the expression
746 * (or error-ed on any remaining ones) in i386_intel_simplify. I
747 * consider the check in i386_finalize_displacement bogus anyway, in
748 * particular because it doesn't allow for expr_section, so I'd
749 * rather see that check (and the similar one in
750 * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
751 * expert I can't really say whether that would have other bad side
754 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
755 && exp_seg
== reg_section
)
756 exp_seg
= expP
->X_op
!= O_constant
? undefined_section
760 if (!i386_finalize_displacement (exp_seg
, expP
,
761 intel_state
.reloc_types
,
766 if (intel_state
.base
|| intel_state
.index
)
767 i
.types
[this_operand
].bitfield
.baseindex
= 1;
773 expP
= symbol_get_value_expression (intel_state
.seg
);
774 if (expP
->X_op
!= O_full_ptr
)
776 intel_state
.seg
= expP
->X_add_symbol
;
778 if (expP
->X_op
!= O_register
)
780 as_bad (_("segment register name expected"));
783 if (!i386_regtab
[expP
->X_add_number
].reg_type
.bitfield
.sreg2
784 && !i386_regtab
[expP
->X_add_number
].reg_type
.bitfield
.sreg3
)
786 as_bad (_("invalid use of register"));
789 switch (i386_regtab
[expP
->X_add_number
].reg_num
)
791 case 0: i
.seg
[i
.mem_operands
] = &es
; break;
792 case 1: i
.seg
[i
.mem_operands
] = &cs
; break;
793 case 2: i
.seg
[i
.mem_operands
] = &ss
; break;
794 case 3: i
.seg
[i
.mem_operands
] = &ds
; break;
795 case 4: i
.seg
[i
.mem_operands
] = &fs
; break;
796 case 5: i
.seg
[i
.mem_operands
] = &gs
; break;
797 case RegFlat
: i
.seg
[i
.mem_operands
] = NULL
; break;
801 /* Swap base and index in 16-bit memory operands like
802 [si+bx]. Since i386_index_check is also used in AT&T
803 mode we have to do that here. */
806 && intel_state
.base
->reg_type
.bitfield
.reg16
807 && intel_state
.index
->reg_type
.bitfield
.reg16
808 && intel_state
.base
->reg_num
>= 6
809 && intel_state
.index
->reg_num
< 6)
811 i
.base_reg
= intel_state
.index
;
812 i
.index_reg
= intel_state
.base
;
816 i
.base_reg
= intel_state
.base
;
817 i
.index_reg
= intel_state
.index
;
820 if (!i386_index_check (operand_string
))
823 i
.types
[this_operand
].bitfield
.mem
= 1;
829 if (i
.imm_operands
>= MAX_IMMEDIATE_OPERANDS
)
831 as_bad (_("at most %d immediate operands are allowed"),
832 MAX_IMMEDIATE_OPERANDS
);
836 expP
= &im_expressions
[i
.imm_operands
++];
837 i
.op
[this_operand
].imms
= expP
;
840 return i386_finalize_immediate (exp_seg
, expP
, intel_state
.reloc_types
,