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 gas_assert (!intel_state
.in_offset
);
472 gas_assert (!intel_state
.in_bracket
);
473 gas_assert (!intel_state
.in_scale
);
475 saved_input_line_pointer
= input_line_pointer
;
476 input_line_pointer
= buf
= xstrdup (operand_string
);
478 /* A '$' followed by an identifier char is an identifier. Otherwise,
479 it's operator '.' followed by an expression. */
480 if (*buf
== '$' && !is_identifier_char (buf
[1]))
484 memset (&exp
, 0, sizeof(exp
));
485 exp_seg
= expression (&exp
);
486 ret
= i386_intel_simplify (&exp
);
490 if (!is_end_of_line
[(unsigned char) *input_line_pointer
])
492 as_bad (_("junk `%s' after expression"), input_line_pointer
);
495 else if (exp
.X_op
== O_illegal
|| exp
.X_op
== O_absent
)
497 as_bad (_("invalid expression"));
501 input_line_pointer
= saved_input_line_pointer
;
504 gas_assert (!intel_state
.in_offset
);
505 gas_assert (!intel_state
.in_bracket
);
506 gas_assert (!intel_state
.in_scale
);
511 if (intel_state
.op_modifier
!= O_absent
512 && current_templates
->start
->base_opcode
!= 0x8d /* lea */)
514 i
.types
[this_operand
].bitfield
.unspecified
= 0;
516 switch (intel_state
.op_modifier
)
519 i
.types
[this_operand
].bitfield
.byte
= 1;
520 suffix
= BYTE_MNEM_SUFFIX
;
524 i
.types
[this_operand
].bitfield
.word
= 1;
525 if ((current_templates
->start
->name
[0] == 'l'
526 && current_templates
->start
->name
[2] == 's'
527 && current_templates
->start
->name
[3] == 0)
528 || current_templates
->start
->base_opcode
== 0x62 /* bound */)
529 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
530 else if (got_a_float
== 2) /* "fi..." */
531 suffix
= SHORT_MNEM_SUFFIX
;
533 suffix
= WORD_MNEM_SUFFIX
;
537 i
.types
[this_operand
].bitfield
.dword
= 1;
538 if ((current_templates
->start
->name
[0] == 'l'
539 && current_templates
->start
->name
[2] == 's'
540 && current_templates
->start
->name
[3] == 0)
541 || current_templates
->start
->base_opcode
== 0x62 /* bound */)
542 suffix
= WORD_MNEM_SUFFIX
;
543 else if (flag_code
== CODE_16BIT
544 && (current_templates
->start
->opcode_modifier
.jump
545 || current_templates
->start
->opcode_modifier
.jumpdword
))
546 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
547 else if (got_a_float
== 1) /* "f..." */
548 suffix
= SHORT_MNEM_SUFFIX
;
550 suffix
= LONG_MNEM_SUFFIX
;
554 i
.types
[this_operand
].bitfield
.fword
= 1;
555 if (current_templates
->start
->name
[0] == 'l'
556 && current_templates
->start
->name
[2] == 's'
557 && current_templates
->start
->name
[3] == 0)
558 suffix
= LONG_MNEM_SUFFIX
;
559 else if (!got_a_float
)
561 if (flag_code
== CODE_16BIT
)
562 add_prefix (DATA_PREFIX_OPCODE
);
563 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
566 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
570 i
.types
[this_operand
].bitfield
.qword
= 1;
571 if (current_templates
->start
->base_opcode
== 0x62 /* bound */
572 || got_a_float
== 1) /* "f..." */
573 suffix
= LONG_MNEM_SUFFIX
;
575 suffix
= QWORD_MNEM_SUFFIX
;
579 i
.types
[this_operand
].bitfield
.tbyte
= 1;
580 if (got_a_float
== 1)
581 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
583 suffix
= BYTE_MNEM_SUFFIX
; /* so it will cause an error */
588 i
.types
[this_operand
].bitfield
.xmmword
= 1;
589 suffix
= XMMWORD_MNEM_SUFFIX
;
593 i
.types
[this_operand
].bitfield
.ymmword
= 1;
594 suffix
= YMMWORD_MNEM_SUFFIX
;
598 suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
601 if (!current_templates
->start
->opcode_modifier
.jump
602 && !current_templates
->start
->opcode_modifier
.jumpdword
)
603 suffix
= got_a_float
/* so it will cause an error */
605 : LONG_DOUBLE_MNEM_SUFFIX
;
609 BAD_CASE (intel_state
.op_modifier
);
615 else if (i
.suffix
!= suffix
)
617 as_bad (_("conflicting operand size modifiers"));
622 /* Operands for jump/call need special consideration. */
623 if (current_templates
->start
->opcode_modifier
.jump
624 || current_templates
->start
->opcode_modifier
.jumpdword
625 || current_templates
->start
->opcode_modifier
.jumpintersegment
)
627 if (i
.op
[this_operand
].regs
|| intel_state
.base
|| intel_state
.index
628 || intel_state
.is_mem
> 1)
629 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
631 switch (intel_state
.op_modifier
)
635 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
637 intel_state
.is_mem
= 1;
641 if (!intel_state
.seg
)
643 intel_state
.is_mem
= 1;
644 if (intel_state
.op_modifier
== O_absent
)
646 as_bad (_("cannot infer the segment part of the operand"));
649 else if (S_GET_SEGMENT (intel_state
.seg
) == reg_section
)
650 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
653 i386_operand_type types
;
655 if (i
.imm_operands
>= MAX_IMMEDIATE_OPERANDS
)
657 as_bad (_("at most %d immediate operands are allowed"),
658 MAX_IMMEDIATE_OPERANDS
);
661 expP
= &im_expressions
[i
.imm_operands
++];
662 memset (expP
, 0, sizeof(*expP
));
663 expP
->X_op
= O_symbol
;
664 expP
->X_add_symbol
= intel_state
.seg
;
665 i
.op
[this_operand
].imms
= expP
;
667 resolve_expression (expP
);
668 operand_type_set (&types
, ~0);
669 if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state
.seg
),
670 expP
, types
, operand_string
))
672 if (i
.operands
< MAX_OPERANDS
)
674 this_operand
= i
.operands
++;
675 i
.types
[this_operand
].bitfield
.unspecified
= 1;
677 if (suffix
== LONG_DOUBLE_MNEM_SUFFIX
)
679 intel_state
.seg
= NULL
;
680 intel_state
.is_mem
= 0;
684 i
.types
[this_operand
].bitfield
.jumpabsolute
= 1;
687 if (i
.types
[this_operand
].bitfield
.jumpabsolute
)
688 intel_state
.is_mem
|= 1;
690 else if (intel_state
.seg
)
691 intel_state
.is_mem
|= 1;
693 if (i
.op
[this_operand
].regs
)
695 i386_operand_type temp
;
697 /* Register operand. */
698 if (intel_state
.base
|| intel_state
.index
|| intel_state
.seg
)
700 as_bad (_("invalid operand"));
704 temp
= i
.op
[this_operand
].regs
->reg_type
;
705 temp
.bitfield
.baseindex
= 0;
706 i
.types
[this_operand
] = operand_type_or (i
.types
[this_operand
], temp
);
707 i
.types
[this_operand
].bitfield
.unspecified
= 0;
710 else if (intel_state
.base
|| intel_state
.index
|| intel_state
.seg
711 || intel_state
.is_mem
)
713 /* Memory operand. */
715 >= 2 - !current_templates
->start
->opcode_modifier
.isstring
)
717 as_bad (_("too many memory references for `%s'"),
718 current_templates
->start
->name
);
722 expP
= &disp_expressions
[i
.disp_operands
];
723 memcpy (expP
, &exp
, sizeof(exp
));
724 resolve_expression (expP
);
726 if (expP
->X_op
!= O_constant
|| expP
->X_add_number
727 || (!intel_state
.base
&& !intel_state
.index
))
729 i
.op
[this_operand
].disps
= expP
;
732 if (flag_code
== CODE_64BIT
)
734 i
.types
[this_operand
].bitfield
.disp32
= 1;
735 if (!i
.prefix
[ADDR_PREFIX
])
737 i
.types
[this_operand
].bitfield
.disp64
= 1;
738 i
.types
[this_operand
].bitfield
.disp32s
= 1;
741 else if (!i
.prefix
[ADDR_PREFIX
] ^ (flag_code
== CODE_16BIT
))
742 i
.types
[this_operand
].bitfield
.disp32
= 1;
744 i
.types
[this_operand
].bitfield
.disp16
= 1;
746 #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT)
748 * exp_seg is used only for verification in
749 * i386_finalize_displacement, and we can end up seeing reg_section
750 * here - but we know we removed all registers from the expression
751 * (or error-ed on any remaining ones) in i386_intel_simplify. I
752 * consider the check in i386_finalize_displacement bogus anyway, in
753 * particular because it doesn't allow for expr_section, so I'd
754 * rather see that check (and the similar one in
755 * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out
756 * expert I can't really say whether that would have other bad side
759 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
760 && exp_seg
== reg_section
)
761 exp_seg
= expP
->X_op
!= O_constant
? undefined_section
765 if (!i386_finalize_displacement (exp_seg
, expP
,
766 intel_state
.reloc_types
,
771 if (intel_state
.base
|| intel_state
.index
)
772 i
.types
[this_operand
].bitfield
.baseindex
= 1;
778 expP
= symbol_get_value_expression (intel_state
.seg
);
779 if (expP
->X_op
!= O_full_ptr
)
781 intel_state
.seg
= expP
->X_add_symbol
;
783 if (expP
->X_op
!= O_register
)
785 as_bad (_("segment register name expected"));
788 if (!i386_regtab
[expP
->X_add_number
].reg_type
.bitfield
.sreg2
789 && !i386_regtab
[expP
->X_add_number
].reg_type
.bitfield
.sreg3
)
791 as_bad (_("invalid use of register"));
794 switch (i386_regtab
[expP
->X_add_number
].reg_num
)
796 case 0: i
.seg
[i
.mem_operands
] = &es
; break;
797 case 1: i
.seg
[i
.mem_operands
] = &cs
; break;
798 case 2: i
.seg
[i
.mem_operands
] = &ss
; break;
799 case 3: i
.seg
[i
.mem_operands
] = &ds
; break;
800 case 4: i
.seg
[i
.mem_operands
] = &fs
; break;
801 case 5: i
.seg
[i
.mem_operands
] = &gs
; break;
802 case RegFlat
: i
.seg
[i
.mem_operands
] = NULL
; break;
806 /* Swap base and index in 16-bit memory operands like
807 [si+bx]. Since i386_index_check is also used in AT&T
808 mode we have to do that here. */
811 && intel_state
.base
->reg_type
.bitfield
.reg16
812 && intel_state
.index
->reg_type
.bitfield
.reg16
813 && intel_state
.base
->reg_num
>= 6
814 && intel_state
.index
->reg_num
< 6)
816 i
.base_reg
= intel_state
.index
;
817 i
.index_reg
= intel_state
.base
;
821 i
.base_reg
= intel_state
.base
;
822 i
.index_reg
= intel_state
.index
;
825 if (!i386_index_check (operand_string
))
828 i
.types
[this_operand
].bitfield
.mem
= 1;
834 if (i
.imm_operands
>= MAX_IMMEDIATE_OPERANDS
)
836 as_bad (_("at most %d immediate operands are allowed"),
837 MAX_IMMEDIATE_OPERANDS
);
841 expP
= &im_expressions
[i
.imm_operands
++];
842 i
.op
[this_operand
].imms
= expP
;
845 return i386_finalize_immediate (exp_seg
, expP
, intel_state
.reloc_types
,