1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Intel 80386 machine specific gas.
24 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
25 x86_64 support by Jan Hubicka (jh@suse.cz)
26 Bugs & suggestions are completely welcome. This is free software.
27 Please help us make it better. */
30 #include "safe-ctype.h"
32 #include "dwarf2dbg.h"
33 #include "opcode/i386.h"
35 #ifndef REGISTER_WARNINGS
36 #define REGISTER_WARNINGS 1
39 #ifndef INFER_ADDR_PREFIX
40 #define INFER_ADDR_PREFIX 1
43 #ifndef SCALE1_WHEN_NO_INDEX
44 /* Specifying a scale factor besides 1 when there is no index is
45 futile. eg. `mov (%ebx,2),%al' does exactly the same as
46 `mov (%ebx),%al'. To slavishly follow what the programmer
47 specified, set SCALE1_WHEN_NO_INDEX to 0. */
48 #define SCALE1_WHEN_NO_INDEX 1
54 static unsigned int mode_from_disp_size
PARAMS ((unsigned int));
55 static int fits_in_signed_byte
PARAMS ((offsetT
));
56 static int fits_in_unsigned_byte
PARAMS ((offsetT
));
57 static int fits_in_unsigned_word
PARAMS ((offsetT
));
58 static int fits_in_signed_word
PARAMS ((offsetT
));
59 static int fits_in_unsigned_long
PARAMS ((offsetT
));
60 static int fits_in_signed_long
PARAMS ((offsetT
));
61 static int smallest_imm_type
PARAMS ((offsetT
));
62 static offsetT offset_in_range
PARAMS ((offsetT
, int));
63 static int add_prefix
PARAMS ((unsigned int));
64 static void set_code_flag
PARAMS ((int));
65 static void set_16bit_gcc_code_flag
PARAMS ((int));
66 static void set_intel_syntax
PARAMS ((int));
67 static void set_cpu_arch
PARAMS ((int));
70 static bfd_reloc_code_real_type reloc
71 PARAMS ((int, int, int, bfd_reloc_code_real_type
));
72 #define RELOC_ENUM enum bfd_reloc_code_real
74 #define RELOC_ENUM int
78 #define DEFAULT_ARCH "i386"
80 static const char *default_arch
= DEFAULT_ARCH
;
82 /* 'md_assemble ()' gathers together information and puts it into a
89 const reg_entry
*regs
;
94 /* TM holds the template for the insn were currently assembling. */
97 /* SUFFIX holds the instruction mnemonic suffix if given.
98 (e.g. 'l' for 'movl') */
101 /* OPERANDS gives the number of given operands. */
102 unsigned int operands
;
104 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
105 of given register, displacement, memory operands and immediate
107 unsigned int reg_operands
, disp_operands
, mem_operands
, imm_operands
;
109 /* TYPES [i] is the type (see above #defines) which tells us how to
110 use OP[i] for the corresponding operand. */
111 unsigned int types
[MAX_OPERANDS
];
113 /* Displacement expression, immediate expression, or register for each
115 union i386_op op
[MAX_OPERANDS
];
117 /* Flags for operands. */
118 unsigned int flags
[MAX_OPERANDS
];
119 #define Operand_PCrel 1
121 /* Relocation type for operand */
122 RELOC_ENUM reloc
[MAX_OPERANDS
];
124 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
125 the base index byte below. */
126 const reg_entry
*base_reg
;
127 const reg_entry
*index_reg
;
128 unsigned int log2_scale_factor
;
130 /* SEG gives the seg_entries of this insn. They are zero unless
131 explicit segment overrides are given. */
132 const seg_entry
*seg
[2];
134 /* PREFIX holds all the given prefix opcodes (usually null).
135 PREFIXES is the number of prefix opcodes. */
136 unsigned int prefixes
;
137 unsigned char prefix
[MAX_PREFIXES
];
139 /* RM and SIB are the modrm byte and the sib byte where the
140 addressing modes of this insn are encoded. */
147 typedef struct _i386_insn i386_insn
;
149 /* List of chars besides those in app.c:symbol_chars that can start an
150 operand. Used to prevent the scrubber eating vital white-space. */
152 const char extra_symbol_chars
[] = "*%-(@";
154 const char extra_symbol_chars
[] = "*%-(";
157 /* This array holds the chars that always start a comment. If the
158 pre-processor is disabled, these aren't very useful. */
159 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD) && !defined(TE_NetBSD))
160 /* Putting '/' here makes it impossible to use the divide operator.
161 However, we need it for compatibility with SVR4 systems. */
162 const char comment_chars
[] = "#/";
163 #define PREFIX_SEPARATOR '\\'
165 const char comment_chars
[] = "#";
166 #define PREFIX_SEPARATOR '/'
169 /* This array holds the chars that only start a comment at the beginning of
170 a line. If the line seems to have the form '# 123 filename'
171 .line and .file directives will appear in the pre-processed output.
172 Note that input_file.c hand checks for '#' at the beginning of the
173 first line of the input file. This is because the compiler outputs
174 #NO_APP at the beginning of its output.
175 Also note that comments started like this one will always work if
176 '/' isn't otherwise defined. */
177 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD) && !defined(TE_NetBSD))
178 const char line_comment_chars
[] = "";
180 const char line_comment_chars
[] = "/";
183 const char line_separator_chars
[] = ";";
185 /* Chars that can be used to separate mant from exp in floating point
187 const char EXP_CHARS
[] = "eE";
189 /* Chars that mean this number is a floating point constant
192 const char FLT_CHARS
[] = "fFdDxX";
194 /* Tables for lexical analysis. */
195 static char mnemonic_chars
[256];
196 static char register_chars
[256];
197 static char operand_chars
[256];
198 static char identifier_chars
[256];
199 static char digit_chars
[256];
201 /* Lexical macros. */
202 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
203 #define is_operand_char(x) (operand_chars[(unsigned char) x])
204 #define is_register_char(x) (register_chars[(unsigned char) x])
205 #define is_space_char(x) ((x) == ' ')
206 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
207 #define is_digit_char(x) (digit_chars[(unsigned char) x])
209 /* All non-digit non-letter charcters that may occur in an operand. */
210 static char operand_special_chars
[] = "%$-+(,)*._~/<>|&^!:[@]";
212 /* md_assemble() always leaves the strings it's passed unaltered. To
213 effect this we maintain a stack of saved characters that we've smashed
214 with '\0's (indicating end of strings for various sub-fields of the
215 assembler instruction). */
216 static char save_stack
[32];
217 static char *save_stack_p
;
218 #define END_STRING_AND_SAVE(s) \
219 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
220 #define RESTORE_END_STRING(s) \
221 do { *(s) = *--save_stack_p; } while (0)
223 /* The instruction we're assembling. */
226 /* Possible templates for current insn. */
227 static const templates
*current_templates
;
229 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
230 static expressionS disp_expressions
[2], im_expressions
[2];
232 /* Current operand we are working on. */
233 static int this_operand
;
235 /* We support four different modes. FLAG_CODE variable is used to distinguish
242 #define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
244 static enum flag_code flag_code
;
245 static int use_rela_relocations
= 0;
247 /* The names used to print error messages. */
248 static const char *flag_code_names
[] =
255 /* 1 for intel syntax,
257 static int intel_syntax
= 0;
259 /* 1 if register prefix % not required. */
260 static int allow_naked_reg
= 0;
262 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
263 leave, push, and pop instructions so that gcc has the same stack
264 frame as in 32 bit mode. */
265 static char stackop_size
= '\0';
267 /* Non-zero to quieten some warnings. */
268 static int quiet_warnings
= 0;
271 static const char *cpu_arch_name
= NULL
;
273 /* CPU feature flags. */
274 static unsigned int cpu_arch_flags
= CpuUnknownFlags
|CpuNo64
;
276 /* If set, conditional jumps are not automatically promoted to handle
277 larger than a byte offset. */
278 static unsigned int no_cond_jump_promotion
= 0;
280 /* Interface to relax_segment.
281 There are 3 major relax states for 386 jump insns because the
282 different types of jumps add different sizes to frags when we're
283 figuring out what sort of jump to choose to reach a given label. */
286 #define UNCOND_JUMP 0
288 #define COND_JUMP86 2
293 #define SMALL16 (SMALL|CODE16)
295 #define BIG16 (BIG|CODE16)
299 #define INLINE __inline__
305 #define ENCODE_RELAX_STATE(type, size) \
306 ((relax_substateT) (((type) << 2) | (size)))
307 #define TYPE_FROM_RELAX_STATE(s) \
309 #define DISP_SIZE_FROM_RELAX_STATE(s) \
310 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
312 /* This table is used by relax_frag to promote short jumps to long
313 ones where necessary. SMALL (short) jumps may be promoted to BIG
314 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
315 don't allow a short jump in a 32 bit code segment to be promoted to
316 a 16 bit offset jump because it's slower (requires data size
317 prefix), and doesn't work, unless the destination is in the bottom
318 64k of the code segment (The top 16 bits of eip are zeroed). */
320 const relax_typeS md_relax_table
[] =
323 1) most positive reach of this state,
324 2) most negative reach of this state,
325 3) how many bytes this mode will have in the variable part of the frag
326 4) which index into the table to try if we can't fit into this one. */
328 /* UNCOND_JUMP states. */
329 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP
, BIG
)},
330 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP
, BIG16
)},
331 /* dword jmp adds 4 bytes to frag:
332 0 extra opcode bytes, 4 displacement bytes. */
334 /* word jmp adds 2 byte2 to frag:
335 0 extra opcode bytes, 2 displacement bytes. */
338 /* COND_JUMP states. */
339 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP
, BIG
)},
340 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP
, BIG16
)},
341 /* dword conditionals adds 5 bytes to frag:
342 1 extra opcode byte, 4 displacement bytes. */
344 /* word conditionals add 3 bytes to frag:
345 1 extra opcode byte, 2 displacement bytes. */
348 /* COND_JUMP86 states. */
349 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86
, BIG
)},
350 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86
, BIG16
)},
351 /* dword conditionals adds 5 bytes to frag:
352 1 extra opcode byte, 4 displacement bytes. */
354 /* word conditionals add 4 bytes to frag:
355 1 displacement byte and a 3 byte long branch insn. */
359 static const arch_entry cpu_arch
[] = {
361 {"i186", Cpu086
|Cpu186
},
362 {"i286", Cpu086
|Cpu186
|Cpu286
},
363 {"i386", Cpu086
|Cpu186
|Cpu286
|Cpu386
},
364 {"i486", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
},
365 {"i586", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|CpuMMX
},
366 {"i686", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|Cpu686
|CpuMMX
|CpuSSE
},
367 {"pentium", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|CpuMMX
},
368 {"pentiumpro",Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|Cpu686
|CpuMMX
|CpuSSE
},
369 {"pentium4", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|Cpu686
|CpuP4
|CpuMMX
|CpuSSE
|CpuSSE2
},
370 {"k6", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|CpuK6
|CpuMMX
|Cpu3dnow
},
371 {"athlon", Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|Cpu686
|CpuK6
|CpuAthlon
|CpuMMX
|Cpu3dnow
},
372 {"sledgehammer",Cpu086
|Cpu186
|Cpu286
|Cpu386
|Cpu486
|Cpu586
|Cpu686
|CpuK6
|CpuAthlon
|CpuSledgehammer
|CpuMMX
|Cpu3dnow
|CpuSSE
|CpuSSE2
},
377 i386_align_code (fragP
, count
)
381 /* Various efficient no-op patterns for aligning code labels.
382 Note: Don't try to assemble the instructions in the comments.
383 0L and 0w are not legal. */
384 static const char f32_1
[] =
386 static const char f32_2
[] =
387 {0x89,0xf6}; /* movl %esi,%esi */
388 static const char f32_3
[] =
389 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
390 static const char f32_4
[] =
391 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
392 static const char f32_5
[] =
394 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
395 static const char f32_6
[] =
396 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
397 static const char f32_7
[] =
398 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
399 static const char f32_8
[] =
401 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
402 static const char f32_9
[] =
403 {0x89,0xf6, /* movl %esi,%esi */
404 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
405 static const char f32_10
[] =
406 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
407 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
408 static const char f32_11
[] =
409 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
410 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
411 static const char f32_12
[] =
412 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
413 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
414 static const char f32_13
[] =
415 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
416 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
417 static const char f32_14
[] =
418 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
419 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
420 static const char f32_15
[] =
421 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
422 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
423 static const char f16_3
[] =
424 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
425 static const char f16_4
[] =
426 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
427 static const char f16_5
[] =
429 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
430 static const char f16_6
[] =
431 {0x89,0xf6, /* mov %si,%si */
432 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
433 static const char f16_7
[] =
434 {0x8d,0x74,0x00, /* lea 0(%si),%si */
435 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
436 static const char f16_8
[] =
437 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
438 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
439 static const char *const f32_patt
[] = {
440 f32_1
, f32_2
, f32_3
, f32_4
, f32_5
, f32_6
, f32_7
, f32_8
,
441 f32_9
, f32_10
, f32_11
, f32_12
, f32_13
, f32_14
, f32_15
443 static const char *const f16_patt
[] = {
444 f32_1
, f32_2
, f16_3
, f16_4
, f16_5
, f16_6
, f16_7
, f16_8
,
445 f32_15
, f32_15
, f32_15
, f32_15
, f32_15
, f32_15
, f32_15
448 /* ??? We can't use these fillers for x86_64, since they often kills the
449 upper halves. Solve later. */
450 if (flag_code
== CODE_64BIT
)
453 if (count
> 0 && count
<= 15)
455 if (flag_code
== CODE_16BIT
)
457 memcpy (fragP
->fr_literal
+ fragP
->fr_fix
,
458 f16_patt
[count
- 1], count
);
460 /* Adjust jump offset. */
461 fragP
->fr_literal
[fragP
->fr_fix
+ 1] = count
- 2;
464 memcpy (fragP
->fr_literal
+ fragP
->fr_fix
,
465 f32_patt
[count
- 1], count
);
466 fragP
->fr_var
= count
;
470 static char *output_invalid
PARAMS ((int c
));
471 static int i386_operand
PARAMS ((char *operand_string
));
472 static int i386_intel_operand
PARAMS ((char *operand_string
, int got_a_float
));
473 static const reg_entry
*parse_register
PARAMS ((char *reg_string
,
477 static void s_bss
PARAMS ((int));
480 symbolS
*GOT_symbol
; /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
482 static INLINE
unsigned int
483 mode_from_disp_size (t
)
486 return (t
& Disp8
) ? 1 : (t
& (Disp16
| Disp32
| Disp32S
)) ? 2 : 0;
490 fits_in_signed_byte (num
)
493 return (num
>= -128) && (num
<= 127);
497 fits_in_unsigned_byte (num
)
500 return (num
& 0xff) == num
;
504 fits_in_unsigned_word (num
)
507 return (num
& 0xffff) == num
;
511 fits_in_signed_word (num
)
514 return (-32768 <= num
) && (num
<= 32767);
517 fits_in_signed_long (num
)
518 offsetT num ATTRIBUTE_UNUSED
;
523 return (!(((offsetT
) -1 << 31) & num
)
524 || (((offsetT
) -1 << 31) & num
) == ((offsetT
) -1 << 31));
526 } /* fits_in_signed_long() */
528 fits_in_unsigned_long (num
)
529 offsetT num ATTRIBUTE_UNUSED
;
534 return (num
& (((offsetT
) 2 << 31) - 1)) == num
;
536 } /* fits_in_unsigned_long() */
539 smallest_imm_type (num
)
542 if (cpu_arch_flags
!= (Cpu086
| Cpu186
| Cpu286
| Cpu386
| Cpu486
| CpuNo64
))
544 /* This code is disabled on the 486 because all the Imm1 forms
545 in the opcode table are slower on the i486. They're the
546 versions with the implicitly specified single-position
547 displacement, which has another syntax if you really want to
550 return Imm1
| Imm8
| Imm8S
| Imm16
| Imm32
| Imm32S
| Imm64
;
552 return (fits_in_signed_byte (num
)
553 ? (Imm8S
| Imm8
| Imm16
| Imm32
| Imm32S
| Imm64
)
554 : fits_in_unsigned_byte (num
)
555 ? (Imm8
| Imm16
| Imm32
| Imm32S
| Imm64
)
556 : (fits_in_signed_word (num
) || fits_in_unsigned_word (num
))
557 ? (Imm16
| Imm32
| Imm32S
| Imm64
)
558 : fits_in_signed_long (num
)
559 ? (Imm32
| Imm32S
| Imm64
)
560 : fits_in_unsigned_long (num
)
566 offset_in_range (val
, size
)
574 case 1: mask
= ((addressT
) 1 << 8) - 1; break;
575 case 2: mask
= ((addressT
) 1 << 16) - 1; break;
576 case 4: mask
= ((addressT
) 2 << 31) - 1; break;
578 case 8: mask
= ((addressT
) 2 << 63) - 1; break;
583 /* If BFD64, sign extend val. */
584 if (!use_rela_relocations
)
585 if ((val
& ~(((addressT
) 2 << 31) - 1)) == 0)
586 val
= (val
^ ((addressT
) 1 << 31)) - ((addressT
) 1 << 31);
588 if ((val
& ~mask
) != 0 && (val
& ~mask
) != ~mask
)
590 char buf1
[40], buf2
[40];
592 sprint_value (buf1
, val
);
593 sprint_value (buf2
, val
& mask
);
594 as_warn (_("%s shortened to %s"), buf1
, buf2
);
599 /* Returns 0 if attempting to add a prefix where one from the same
600 class already exists, 1 if non rep/repne added, 2 if rep/repne
609 if (prefix
>= 0x40 && prefix
< 0x50 && flag_code
== CODE_64BIT
)
617 case CS_PREFIX_OPCODE
:
618 case DS_PREFIX_OPCODE
:
619 case ES_PREFIX_OPCODE
:
620 case FS_PREFIX_OPCODE
:
621 case GS_PREFIX_OPCODE
:
622 case SS_PREFIX_OPCODE
:
626 case REPNE_PREFIX_OPCODE
:
627 case REPE_PREFIX_OPCODE
:
630 case LOCK_PREFIX_OPCODE
:
638 case ADDR_PREFIX_OPCODE
:
642 case DATA_PREFIX_OPCODE
:
649 as_bad (_("same type of prefix used twice"));
654 i
.prefix
[q
] = prefix
;
659 set_code_flag (value
)
663 cpu_arch_flags
&= ~(Cpu64
| CpuNo64
);
664 cpu_arch_flags
|= (flag_code
== CODE_64BIT
? Cpu64
: CpuNo64
);
665 if (value
== CODE_64BIT
&& !(cpu_arch_flags
& CpuSledgehammer
))
667 as_bad (_("64bit mode not supported on this CPU."));
669 if (value
== CODE_32BIT
&& !(cpu_arch_flags
& Cpu386
))
671 as_bad (_("32bit mode not supported on this CPU."));
677 set_16bit_gcc_code_flag (new_code_flag
)
680 flag_code
= new_code_flag
;
681 cpu_arch_flags
&= ~(Cpu64
| CpuNo64
);
682 cpu_arch_flags
|= (flag_code
== CODE_64BIT
? Cpu64
: CpuNo64
);
687 set_intel_syntax (syntax_flag
)
690 /* Find out if register prefixing is specified. */
691 int ask_naked_reg
= 0;
694 if (! is_end_of_line
[(unsigned char) *input_line_pointer
])
696 char *string
= input_line_pointer
;
697 int e
= get_symbol_end ();
699 if (strcmp (string
, "prefix") == 0)
701 else if (strcmp (string
, "noprefix") == 0)
704 as_bad (_("bad argument to syntax directive."));
705 *input_line_pointer
= e
;
707 demand_empty_rest_of_line ();
709 intel_syntax
= syntax_flag
;
711 if (ask_naked_reg
== 0)
714 allow_naked_reg
= (intel_syntax
715 && (bfd_get_symbol_leading_char (stdoutput
) != '\0'));
717 /* Conservative default. */
722 allow_naked_reg
= (ask_naked_reg
< 0);
727 int dummy ATTRIBUTE_UNUSED
;
731 if (! is_end_of_line
[(unsigned char) *input_line_pointer
])
733 char *string
= input_line_pointer
;
734 int e
= get_symbol_end ();
737 for (i
= 0; cpu_arch
[i
].name
; i
++)
739 if (strcmp (string
, cpu_arch
[i
].name
) == 0)
741 cpu_arch_name
= cpu_arch
[i
].name
;
742 cpu_arch_flags
= (cpu_arch
[i
].flags
743 | (flag_code
== CODE_64BIT
? Cpu64
: CpuNo64
));
747 if (!cpu_arch
[i
].name
)
748 as_bad (_("no such architecture: `%s'"), string
);
750 *input_line_pointer
= e
;
753 as_bad (_("missing cpu architecture"));
755 no_cond_jump_promotion
= 0;
756 if (*input_line_pointer
== ','
757 && ! is_end_of_line
[(unsigned char) input_line_pointer
[1]])
759 char *string
= ++input_line_pointer
;
760 int e
= get_symbol_end ();
762 if (strcmp (string
, "nojumps") == 0)
763 no_cond_jump_promotion
= 1;
764 else if (strcmp (string
, "jumps") == 0)
767 as_bad (_("no such architecture modifier: `%s'"), string
);
769 *input_line_pointer
= e
;
772 demand_empty_rest_of_line ();
775 const pseudo_typeS md_pseudo_table
[] =
777 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
778 {"align", s_align_bytes
, 0},
780 {"align", s_align_ptwo
, 0},
782 {"arch", set_cpu_arch
, 0},
786 {"ffloat", float_cons
, 'f'},
787 {"dfloat", float_cons
, 'd'},
788 {"tfloat", float_cons
, 'x'},
790 {"noopt", s_ignore
, 0},
791 {"optim", s_ignore
, 0},
792 {"code16gcc", set_16bit_gcc_code_flag
, CODE_16BIT
},
793 {"code16", set_code_flag
, CODE_16BIT
},
794 {"code32", set_code_flag
, CODE_32BIT
},
795 {"code64", set_code_flag
, CODE_64BIT
},
796 {"intel_syntax", set_intel_syntax
, 1},
797 {"att_syntax", set_intel_syntax
, 0},
798 {"file", dwarf2_directive_file
, 0},
799 {"loc", dwarf2_directive_loc
, 0},
803 /* For interface with expression (). */
804 extern char *input_line_pointer
;
806 /* Hash table for instruction mnemonic lookup. */
807 static struct hash_control
*op_hash
;
809 /* Hash table for register lookup. */
810 static struct hash_control
*reg_hash
;
816 if (!strcmp (default_arch
, "x86_64"))
817 return bfd_mach_x86_64
;
818 else if (!strcmp (default_arch
, "i386"))
819 return bfd_mach_i386_i386
;
821 as_fatal (_("Unknown architecture"));
828 const char *hash_err
;
830 /* Initialize op_hash hash table. */
831 op_hash
= hash_new ();
834 register const template *optab
;
835 register templates
*core_optab
;
837 /* Setup for loop. */
839 core_optab
= (templates
*) xmalloc (sizeof (templates
));
840 core_optab
->start
= optab
;
845 if (optab
->name
== NULL
846 || strcmp (optab
->name
, (optab
- 1)->name
) != 0)
848 /* different name --> ship out current template list;
849 add to hash table; & begin anew. */
850 core_optab
->end
= optab
;
851 hash_err
= hash_insert (op_hash
,
856 as_fatal (_("Internal Error: Can't hash %s: %s"),
860 if (optab
->name
== NULL
)
862 core_optab
= (templates
*) xmalloc (sizeof (templates
));
863 core_optab
->start
= optab
;
868 /* Initialize reg_hash hash table. */
869 reg_hash
= hash_new ();
871 register const reg_entry
*regtab
;
873 for (regtab
= i386_regtab
;
874 regtab
< i386_regtab
+ sizeof (i386_regtab
) / sizeof (i386_regtab
[0]);
877 hash_err
= hash_insert (reg_hash
, regtab
->reg_name
, (PTR
) regtab
);
879 as_fatal (_("Internal Error: Can't hash %s: %s"),
885 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
890 for (c
= 0; c
< 256; c
++)
895 mnemonic_chars
[c
] = c
;
896 register_chars
[c
] = c
;
897 operand_chars
[c
] = c
;
899 else if (ISLOWER (c
))
901 mnemonic_chars
[c
] = c
;
902 register_chars
[c
] = c
;
903 operand_chars
[c
] = c
;
905 else if (ISUPPER (c
))
907 mnemonic_chars
[c
] = TOLOWER (c
);
908 register_chars
[c
] = mnemonic_chars
[c
];
909 operand_chars
[c
] = c
;
912 if (ISALPHA (c
) || ISDIGIT (c
))
913 identifier_chars
[c
] = c
;
916 identifier_chars
[c
] = c
;
917 operand_chars
[c
] = c
;
922 identifier_chars
['@'] = '@';
924 digit_chars
['-'] = '-';
925 identifier_chars
['_'] = '_';
926 identifier_chars
['.'] = '.';
928 for (p
= operand_special_chars
; *p
!= '\0'; p
++)
929 operand_chars
[(unsigned char) *p
] = *p
;
932 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
933 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
)
935 record_alignment (text_section
, 2);
936 record_alignment (data_section
, 2);
937 record_alignment (bss_section
, 2);
943 i386_print_statistics (file
)
946 hash_print_statistics (file
, "i386 opcode", op_hash
);
947 hash_print_statistics (file
, "i386 register", reg_hash
);
952 /* Debugging routines for md_assemble. */
953 static void pi
PARAMS ((char *, i386_insn
*));
954 static void pte
PARAMS ((template *));
955 static void pt
PARAMS ((unsigned int));
956 static void pe
PARAMS ((expressionS
*));
957 static void ps
PARAMS ((symbolS
*));
966 fprintf (stdout
, "%s: template ", line
);
968 fprintf (stdout
, " address: base %s index %s scale %x\n",
969 x
->base_reg
? x
->base_reg
->reg_name
: "none",
970 x
->index_reg
? x
->index_reg
->reg_name
: "none",
971 x
->log2_scale_factor
);
972 fprintf (stdout
, " modrm: mode %x reg %x reg/mem %x\n",
973 x
->rm
.mode
, x
->rm
.reg
, x
->rm
.regmem
);
974 fprintf (stdout
, " sib: base %x index %x scale %x\n",
975 x
->sib
.base
, x
->sib
.index
, x
->sib
.scale
);
976 fprintf (stdout
, " rex: 64bit %x extX %x extY %x extZ %x\n",
977 x
->rex
.mode64
, x
->rex
.extX
, x
->rex
.extY
, x
->rex
.extZ
);
978 for (i
= 0; i
< x
->operands
; i
++)
980 fprintf (stdout
, " #%d: ", i
+ 1);
982 fprintf (stdout
, "\n");
984 & (Reg
| SReg2
| SReg3
| Control
| Debug
| Test
| RegMMX
| RegXMM
))
985 fprintf (stdout
, "%s\n", x
->op
[i
].regs
->reg_name
);
986 if (x
->types
[i
] & Imm
)
988 if (x
->types
[i
] & Disp
)
998 fprintf (stdout
, " %d operands ", t
->operands
);
999 fprintf (stdout
, "opcode %x ", t
->base_opcode
);
1000 if (t
->extension_opcode
!= None
)
1001 fprintf (stdout
, "ext %x ", t
->extension_opcode
);
1002 if (t
->opcode_modifier
& D
)
1003 fprintf (stdout
, "D");
1004 if (t
->opcode_modifier
& W
)
1005 fprintf (stdout
, "W");
1006 fprintf (stdout
, "\n");
1007 for (i
= 0; i
< t
->operands
; i
++)
1009 fprintf (stdout
, " #%d type ", i
+ 1);
1010 pt (t
->operand_types
[i
]);
1011 fprintf (stdout
, "\n");
1019 fprintf (stdout
, " operation %d\n", e
->X_op
);
1020 fprintf (stdout
, " add_number %ld (%lx)\n",
1021 (long) e
->X_add_number
, (long) e
->X_add_number
);
1022 if (e
->X_add_symbol
)
1024 fprintf (stdout
, " add_symbol ");
1025 ps (e
->X_add_symbol
);
1026 fprintf (stdout
, "\n");
1030 fprintf (stdout
, " op_symbol ");
1031 ps (e
->X_op_symbol
);
1032 fprintf (stdout
, "\n");
1040 fprintf (stdout
, "%s type %s%s",
1042 S_IS_EXTERNAL (s
) ? "EXTERNAL " : "",
1043 segment_name (S_GET_SEGMENT (s
)));
1065 { BaseIndex
, "BaseIndex" },
1069 { Disp32S
, "d32s" },
1071 { InOutPortReg
, "InOutPortReg" },
1072 { ShiftCount
, "ShiftCount" },
1073 { Control
, "control reg" },
1074 { Test
, "test reg" },
1075 { Debug
, "debug reg" },
1076 { FloatReg
, "FReg" },
1077 { FloatAcc
, "FAcc" },
1081 { JumpAbsolute
, "Jump Absolute" },
1092 register struct type_name
*ty
;
1094 for (ty
= type_names
; ty
->mask
; ty
++)
1096 fprintf (stdout
, "%s, ", ty
->tname
);
1100 #endif /* DEBUG386 */
1103 tc_i386_force_relocation (fixp
)
1106 #ifdef BFD_ASSEMBLER
1107 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1108 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1113 return fixp
->fx_r_type
== 7;
1117 #ifdef BFD_ASSEMBLER
1119 static bfd_reloc_code_real_type
1120 reloc (size
, pcrel
, sign
, other
)
1124 bfd_reloc_code_real_type other
;
1126 if (other
!= NO_RELOC
)
1132 as_bad (_("There are no unsigned pc-relative relocations"));
1135 case 1: return BFD_RELOC_8_PCREL
;
1136 case 2: return BFD_RELOC_16_PCREL
;
1137 case 4: return BFD_RELOC_32_PCREL
;
1139 as_bad (_("can not do %d byte pc-relative relocation"), size
);
1146 case 4: return BFD_RELOC_X86_64_32S
;
1151 case 1: return BFD_RELOC_8
;
1152 case 2: return BFD_RELOC_16
;
1153 case 4: return BFD_RELOC_32
;
1154 case 8: return BFD_RELOC_64
;
1156 as_bad (_("can not do %s %d byte relocation"),
1157 sign
? "signed" : "unsigned", size
);
1161 return BFD_RELOC_NONE
;
1164 /* Here we decide which fixups can be adjusted to make them relative to
1165 the beginning of the section instead of the symbol. Basically we need
1166 to make sure that the dynamic relocations are done correctly, so in
1167 some cases we force the original symbol to be used. */
1170 tc_i386_fix_adjustable (fixP
)
1173 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1174 /* Prevent all adjustments to global symbols, or else dynamic
1175 linking will not work correctly. */
1176 if (S_IS_EXTERNAL (fixP
->fx_addsy
)
1177 || S_IS_WEAK (fixP
->fx_addsy
))
1180 /* adjust_reloc_syms doesn't know about the GOT. */
1181 if (fixP
->fx_r_type
== BFD_RELOC_386_GOTOFF
1182 || fixP
->fx_r_type
== BFD_RELOC_386_PLT32
1183 || fixP
->fx_r_type
== BFD_RELOC_386_GOT32
1184 || fixP
->fx_r_type
== BFD_RELOC_X86_64_PLT32
1185 || fixP
->fx_r_type
== BFD_RELOC_X86_64_GOT32
1186 || fixP
->fx_r_type
== BFD_RELOC_X86_64_GOTPCREL
1187 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1188 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1193 #define reloc(SIZE,PCREL,SIGN,OTHER) 0
1194 #define BFD_RELOC_16 0
1195 #define BFD_RELOC_32 0
1196 #define BFD_RELOC_16_PCREL 0
1197 #define BFD_RELOC_32_PCREL 0
1198 #define BFD_RELOC_386_PLT32 0
1199 #define BFD_RELOC_386_GOT32 0
1200 #define BFD_RELOC_386_GOTOFF 0
1201 #define BFD_RELOC_X86_64_PLT32 0
1202 #define BFD_RELOC_X86_64_GOT32 0
1203 #define BFD_RELOC_X86_64_GOTPCREL 0
1206 static int intel_float_operand
PARAMS ((char *mnemonic
));
1209 intel_float_operand (mnemonic
)
1212 if (mnemonic
[0] == 'f' && mnemonic
[1] == 'i')
1215 if (mnemonic
[0] == 'f')
1221 /* This is the guts of the machine-dependent assembler. LINE points to a
1222 machine dependent instruction. This function is supposed to emit
1223 the frags/bytes it assembles to. */
1229 /* Points to template once we've found it. */
1234 char mnemonic
[MAX_MNEM_SIZE
];
1236 /* Initialize globals. */
1237 memset (&i
, '\0', sizeof (i
));
1238 for (j
= 0; j
< MAX_OPERANDS
; j
++)
1239 i
.reloc
[j
] = NO_RELOC
;
1240 memset (disp_expressions
, '\0', sizeof (disp_expressions
));
1241 memset (im_expressions
, '\0', sizeof (im_expressions
));
1242 save_stack_p
= save_stack
;
1244 /* First parse an instruction mnemonic & call i386_operand for the operands.
1245 We assume that the scrubber has arranged it so that line[0] is the valid
1246 start of a (possibly prefixed) mnemonic. */
1249 char *token_start
= l
;
1252 /* Non-zero if we found a prefix only acceptable with string insns. */
1253 const char *expecting_string_instruction
= NULL
;
1258 while ((*mnem_p
= mnemonic_chars
[(unsigned char) *l
]) != 0)
1261 if (mnem_p
>= mnemonic
+ sizeof (mnemonic
))
1263 as_bad (_("no such instruction: `%s'"), token_start
);
1268 if (!is_space_char (*l
)
1269 && *l
!= END_OF_INSN
1270 && *l
!= PREFIX_SEPARATOR
1273 as_bad (_("invalid character %s in mnemonic"),
1274 output_invalid (*l
));
1277 if (token_start
== l
)
1279 if (*l
== PREFIX_SEPARATOR
)
1280 as_bad (_("expecting prefix; got nothing"));
1282 as_bad (_("expecting mnemonic; got nothing"));
1286 /* Look up instruction (or prefix) via hash table. */
1287 current_templates
= hash_find (op_hash
, mnemonic
);
1289 if (*l
!= END_OF_INSN
1290 && (! is_space_char (*l
) || l
[1] != END_OF_INSN
)
1291 && current_templates
1292 && (current_templates
->start
->opcode_modifier
& IsPrefix
))
1294 /* If we are in 16-bit mode, do not allow addr16 or data16.
1295 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1296 if ((current_templates
->start
->opcode_modifier
& (Size16
| Size32
))
1297 && (((current_templates
->start
->opcode_modifier
& Size32
) != 0)
1298 ^ (flag_code
== CODE_16BIT
)))
1300 as_bad (_("redundant %s prefix"),
1301 current_templates
->start
->name
);
1304 /* Add prefix, checking for repeated prefixes. */
1305 switch (add_prefix (current_templates
->start
->base_opcode
))
1310 expecting_string_instruction
= current_templates
->start
->name
;
1313 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1320 if (!current_templates
)
1322 /* See if we can get a match by trimming off a suffix. */
1325 case WORD_MNEM_SUFFIX
:
1326 case BYTE_MNEM_SUFFIX
:
1327 case QWORD_MNEM_SUFFIX
:
1328 i
.suffix
= mnem_p
[-1];
1330 current_templates
= hash_find (op_hash
, mnemonic
);
1332 case SHORT_MNEM_SUFFIX
:
1333 case LONG_MNEM_SUFFIX
:
1336 i
.suffix
= mnem_p
[-1];
1338 current_templates
= hash_find (op_hash
, mnemonic
);
1346 if (intel_float_operand (mnemonic
))
1347 i
.suffix
= SHORT_MNEM_SUFFIX
;
1349 i
.suffix
= LONG_MNEM_SUFFIX
;
1351 current_templates
= hash_find (op_hash
, mnemonic
);
1355 if (!current_templates
)
1357 as_bad (_("no such instruction: `%s'"), token_start
);
1362 if (current_templates
->start
->opcode_modifier
& (Jump
| JumpByte
))
1364 /* Check for a branch hint. We allow ",pt" and ",pn" for
1365 predict taken and predict not taken respectively.
1366 I'm not sure that branch hints actually do anything on loop
1367 and jcxz insns (JumpByte) for current Pentium4 chips. They
1368 may work in the future and it doesn't hurt to accept them
1370 if (l
[0] == ',' && l
[1] == 'p')
1374 if (! add_prefix (DS_PREFIX_OPCODE
))
1378 else if (l
[2] == 'n')
1380 if (! add_prefix (CS_PREFIX_OPCODE
))
1386 /* Any other comma loses. */
1389 as_bad (_("invalid character %s in mnemonic"),
1390 output_invalid (*l
));
1394 /* Check if instruction is supported on specified architecture. */
1395 if ((current_templates
->start
->cpu_flags
& ~(Cpu64
| CpuNo64
))
1396 & ~(cpu_arch_flags
& ~(Cpu64
| CpuNo64
)))
1398 as_warn (_("`%s' is not supported on `%s'"),
1399 current_templates
->start
->name
, cpu_arch_name
);
1401 else if ((Cpu386
& ~cpu_arch_flags
) && (flag_code
!= CODE_16BIT
))
1403 as_warn (_("use .code16 to ensure correct addressing mode"));
1406 /* Check for rep/repne without a string instruction. */
1407 if (expecting_string_instruction
1408 && !(current_templates
->start
->opcode_modifier
& IsString
))
1410 as_bad (_("expecting string instruction after `%s'"),
1411 expecting_string_instruction
);
1415 /* There may be operands to parse. */
1416 if (*l
!= END_OF_INSN
)
1418 /* 1 if operand is pending after ','. */
1419 unsigned int expecting_operand
= 0;
1421 /* Non-zero if operand parens not balanced. */
1422 unsigned int paren_not_balanced
;
1426 /* Skip optional white space before operand. */
1427 if (is_space_char (*l
))
1429 if (!is_operand_char (*l
) && *l
!= END_OF_INSN
)
1431 as_bad (_("invalid character %s before operand %d"),
1432 output_invalid (*l
),
1436 token_start
= l
; /* after white space */
1437 paren_not_balanced
= 0;
1438 while (paren_not_balanced
|| *l
!= ',')
1440 if (*l
== END_OF_INSN
)
1442 if (paren_not_balanced
)
1445 as_bad (_("unbalanced parenthesis in operand %d."),
1448 as_bad (_("unbalanced brackets in operand %d."),
1453 break; /* we are done */
1455 else if (!is_operand_char (*l
) && !is_space_char (*l
))
1457 as_bad (_("invalid character %s in operand %d"),
1458 output_invalid (*l
),
1465 ++paren_not_balanced
;
1467 --paren_not_balanced
;
1472 ++paren_not_balanced
;
1474 --paren_not_balanced
;
1478 if (l
!= token_start
)
1479 { /* Yes, we've read in another operand. */
1480 unsigned int operand_ok
;
1481 this_operand
= i
.operands
++;
1482 if (i
.operands
> MAX_OPERANDS
)
1484 as_bad (_("spurious operands; (%d operands/instruction max)"),
1488 /* Now parse operand adding info to 'i' as we go along. */
1489 END_STRING_AND_SAVE (l
);
1493 i386_intel_operand (token_start
,
1494 intel_float_operand (mnemonic
));
1496 operand_ok
= i386_operand (token_start
);
1498 RESTORE_END_STRING (l
);
1504 if (expecting_operand
)
1506 expecting_operand_after_comma
:
1507 as_bad (_("expecting operand after ','; got nothing"));
1512 as_bad (_("expecting operand before ','; got nothing"));
1517 /* Now *l must be either ',' or END_OF_INSN. */
1520 if (*++l
== END_OF_INSN
)
1522 /* Just skip it, if it's \n complain. */
1523 goto expecting_operand_after_comma
;
1525 expecting_operand
= 1;
1528 while (*l
!= END_OF_INSN
);
1532 /* Now we've parsed the mnemonic into a set of templates, and have the
1535 Next, we find a template that matches the given insn,
1536 making sure the overlap of the given operands types is consistent
1537 with the template operand types. */
1539 #define MATCH(overlap, given, template) \
1540 ((overlap & ~JumpAbsolute) \
1541 && ((given) & (BaseIndex|JumpAbsolute)) == ((overlap) & (BaseIndex|JumpAbsolute)))
1543 /* If given types r0 and r1 are registers they must be of the same type
1544 unless the expected operand type register overlap is null.
1545 Note that Acc in a template matches every size of reg. */
1546 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
1547 ( ((g0) & Reg) == 0 || ((g1) & Reg) == 0 || \
1548 ((g0) & Reg) == ((g1) & Reg) || \
1549 ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
1552 register unsigned int overlap0
, overlap1
;
1553 unsigned int overlap2
;
1554 unsigned int found_reverse_match
;
1557 /* All intel opcodes have reversed operands except for "bound" and
1558 "enter". We also don't reverse intersegment "jmp" and "call"
1559 instructions with 2 immediate operands so that the immediate segment
1560 precedes the offset, as it does when in AT&T mode. "enter" and the
1561 intersegment "jmp" and "call" instructions are the only ones that
1562 have two immediate operands. */
1563 if (intel_syntax
&& i
.operands
> 1
1564 && (strcmp (mnemonic
, "bound") != 0)
1565 && !((i
.types
[0] & Imm
) && (i
.types
[1] & Imm
)))
1567 union i386_op temp_op
;
1568 unsigned int temp_type
;
1569 RELOC_ENUM temp_reloc
;
1573 if (i
.operands
== 2)
1578 else if (i
.operands
== 3)
1583 temp_type
= i
.types
[xchg2
];
1584 i
.types
[xchg2
] = i
.types
[xchg1
];
1585 i
.types
[xchg1
] = temp_type
;
1586 temp_op
= i
.op
[xchg2
];
1587 i
.op
[xchg2
] = i
.op
[xchg1
];
1588 i
.op
[xchg1
] = temp_op
;
1589 temp_reloc
= i
.reloc
[xchg2
];
1590 i
.reloc
[xchg2
] = i
.reloc
[xchg1
];
1591 i
.reloc
[xchg1
] = temp_reloc
;
1593 if (i
.mem_operands
== 2)
1595 const seg_entry
*temp_seg
;
1596 temp_seg
= i
.seg
[0];
1597 i
.seg
[0] = i
.seg
[1];
1598 i
.seg
[1] = temp_seg
;
1604 /* Try to ensure constant immediates are represented in the smallest
1606 char guess_suffix
= 0;
1610 guess_suffix
= i
.suffix
;
1611 else if (i
.reg_operands
)
1613 /* Figure out a suffix from the last register operand specified.
1614 We can't do this properly yet, ie. excluding InOutPortReg,
1615 but the following works for instructions with immediates.
1616 In any case, we can't set i.suffix yet. */
1617 for (op
= i
.operands
; --op
>= 0;)
1618 if (i
.types
[op
] & Reg
)
1620 if (i
.types
[op
] & Reg8
)
1621 guess_suffix
= BYTE_MNEM_SUFFIX
;
1622 else if (i
.types
[op
] & Reg16
)
1623 guess_suffix
= WORD_MNEM_SUFFIX
;
1624 else if (i
.types
[op
] & Reg32
)
1625 guess_suffix
= LONG_MNEM_SUFFIX
;
1626 else if (i
.types
[op
] & Reg64
)
1627 guess_suffix
= QWORD_MNEM_SUFFIX
;
1631 else if ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[DATA_PREFIX
] != 0))
1632 guess_suffix
= WORD_MNEM_SUFFIX
;
1634 for (op
= i
.operands
; --op
>= 0;)
1635 if (i
.types
[op
] & Imm
)
1637 switch (i
.op
[op
].imms
->X_op
)
1640 /* If a suffix is given, this operand may be shortened. */
1641 switch (guess_suffix
)
1643 case LONG_MNEM_SUFFIX
:
1644 i
.types
[op
] |= Imm32
| Imm64
;
1646 case WORD_MNEM_SUFFIX
:
1647 i
.types
[op
] |= Imm16
| Imm32S
| Imm32
| Imm64
;
1649 case BYTE_MNEM_SUFFIX
:
1650 i
.types
[op
] |= Imm16
| Imm8
| Imm8S
| Imm32S
| Imm32
| Imm64
;
1654 /* If this operand is at most 16 bits, convert it
1655 to a signed 16 bit number before trying to see
1656 whether it will fit in an even smaller size.
1657 This allows a 16-bit operand such as $0xffe0 to
1658 be recognised as within Imm8S range. */
1659 if ((i
.types
[op
] & Imm16
)
1660 && (i
.op
[op
].imms
->X_add_number
& ~(offsetT
) 0xffff) == 0)
1662 i
.op
[op
].imms
->X_add_number
=
1663 (((i
.op
[op
].imms
->X_add_number
& 0xffff) ^ 0x8000) - 0x8000);
1665 if ((i
.types
[op
] & Imm32
)
1666 && (i
.op
[op
].imms
->X_add_number
& ~(((offsetT
) 2 << 31) - 1)) == 0)
1668 i
.op
[op
].imms
->X_add_number
=
1669 (i
.op
[op
].imms
->X_add_number
^ ((offsetT
) 1 << 31)) - ((addressT
) 1 << 31);
1671 i
.types
[op
] |= smallest_imm_type (i
.op
[op
].imms
->X_add_number
);
1672 /* We must avoid matching of Imm32 templates when 64bit only immediate is available. */
1673 if (guess_suffix
== QWORD_MNEM_SUFFIX
)
1674 i
.types
[op
] &= ~Imm32
;
1679 /* Symbols and expressions. */
1681 /* Convert symbolic operand to proper sizes for matching. */
1682 switch (guess_suffix
)
1684 case QWORD_MNEM_SUFFIX
:
1685 i
.types
[op
] = Imm64
| Imm32S
;
1687 case LONG_MNEM_SUFFIX
:
1688 i
.types
[op
] = Imm32
| Imm64
;
1690 case WORD_MNEM_SUFFIX
:
1691 i
.types
[op
] = Imm16
| Imm32
| Imm64
;
1694 case BYTE_MNEM_SUFFIX
:
1695 i
.types
[op
] = Imm8
| Imm8S
| Imm16
| Imm32S
| Imm32
;
1704 if (i
.disp_operands
)
1706 /* Try to use the smallest displacement type too. */
1709 for (op
= i
.operands
; --op
>= 0;)
1710 if ((i
.types
[op
] & Disp
)
1711 && i
.op
[op
].disps
->X_op
== O_constant
)
1713 offsetT disp
= i
.op
[op
].disps
->X_add_number
;
1715 if (i
.types
[op
] & Disp16
)
1717 /* We know this operand is at most 16 bits, so
1718 convert to a signed 16 bit number before trying
1719 to see whether it will fit in an even smaller
1722 disp
= (((disp
& 0xffff) ^ 0x8000) - 0x8000);
1724 else if (i
.types
[op
] & Disp32
)
1726 /* We know this operand is at most 32 bits, so convert to a
1727 signed 32 bit number before trying to see whether it will
1728 fit in an even smaller size. */
1729 disp
&= (((offsetT
) 2 << 31) - 1);
1730 disp
= (disp
^ ((offsetT
) 1 << 31)) - ((addressT
) 1 << 31);
1732 if (flag_code
== CODE_64BIT
)
1734 if (fits_in_signed_long (disp
))
1735 i
.types
[op
] |= Disp32S
;
1736 if (fits_in_unsigned_long (disp
))
1737 i
.types
[op
] |= Disp32
;
1739 if ((i
.types
[op
] & (Disp32
| Disp32S
| Disp16
))
1740 && fits_in_signed_byte (disp
))
1741 i
.types
[op
] |= Disp8
;
1748 found_reverse_match
= 0;
1749 suffix_check
= (i
.suffix
== BYTE_MNEM_SUFFIX
1751 : (i
.suffix
== WORD_MNEM_SUFFIX
1753 : (i
.suffix
== SHORT_MNEM_SUFFIX
1755 : (i
.suffix
== LONG_MNEM_SUFFIX
1757 : (i
.suffix
== QWORD_MNEM_SUFFIX
1759 : (i
.suffix
== LONG_DOUBLE_MNEM_SUFFIX
? No_xSuf
: 0))))));
1761 for (t
= current_templates
->start
;
1762 t
< current_templates
->end
;
1765 /* Must have right number of operands. */
1766 if (i
.operands
!= t
->operands
)
1769 /* Check the suffix, except for some instructions in intel mode. */
1770 if ((t
->opcode_modifier
& suffix_check
)
1772 && (t
->opcode_modifier
& IgnoreSize
))
1774 && t
->base_opcode
== 0xd9
1775 && (t
->extension_opcode
== 5 /* 0xd9,5 "fldcw" */
1776 || t
->extension_opcode
== 7))) /* 0xd9,7 "f{n}stcw" */
1779 /* Do not verify operands when there are none. */
1780 else if (!t
->operands
)
1782 if (t
->cpu_flags
& ~cpu_arch_flags
)
1784 /* We've found a match; break out of loop. */
1788 overlap0
= i
.types
[0] & t
->operand_types
[0];
1789 switch (t
->operands
)
1792 if (!MATCH (overlap0
, i
.types
[0], t
->operand_types
[0]))
1797 overlap1
= i
.types
[1] & t
->operand_types
[1];
1798 if (!MATCH (overlap0
, i
.types
[0], t
->operand_types
[0])
1799 || !MATCH (overlap1
, i
.types
[1], t
->operand_types
[1])
1800 || !CONSISTENT_REGISTER_MATCH (overlap0
, i
.types
[0],
1801 t
->operand_types
[0],
1802 overlap1
, i
.types
[1],
1803 t
->operand_types
[1]))
1805 /* Check if other direction is valid ... */
1806 if ((t
->opcode_modifier
& (D
|FloatD
)) == 0)
1809 /* Try reversing direction of operands. */
1810 overlap0
= i
.types
[0] & t
->operand_types
[1];
1811 overlap1
= i
.types
[1] & t
->operand_types
[0];
1812 if (!MATCH (overlap0
, i
.types
[0], t
->operand_types
[1])
1813 || !MATCH (overlap1
, i
.types
[1], t
->operand_types
[0])
1814 || !CONSISTENT_REGISTER_MATCH (overlap0
, i
.types
[0],
1815 t
->operand_types
[1],
1816 overlap1
, i
.types
[1],
1817 t
->operand_types
[0]))
1819 /* Does not match either direction. */
1822 /* found_reverse_match holds which of D or FloatDR
1824 found_reverse_match
= t
->opcode_modifier
& (D
|FloatDR
);
1826 /* Found a forward 2 operand match here. */
1827 else if (t
->operands
== 3)
1829 /* Here we make use of the fact that there are no
1830 reverse match 3 operand instructions, and all 3
1831 operand instructions only need to be checked for
1832 register consistency between operands 2 and 3. */
1833 overlap2
= i
.types
[2] & t
->operand_types
[2];
1834 if (!MATCH (overlap2
, i
.types
[2], t
->operand_types
[2])
1835 || !CONSISTENT_REGISTER_MATCH (overlap1
, i
.types
[1],
1836 t
->operand_types
[1],
1837 overlap2
, i
.types
[2],
1838 t
->operand_types
[2]))
1842 /* Found either forward/reverse 2 or 3 operand match here:
1843 slip through to break. */
1845 if (t
->cpu_flags
& ~cpu_arch_flags
)
1847 found_reverse_match
= 0;
1850 /* We've found a match; break out of loop. */
1853 if (t
== current_templates
->end
)
1855 /* We found no match. */
1856 as_bad (_("suffix or operands invalid for `%s'"),
1857 current_templates
->start
->name
);
1861 if (!quiet_warnings
)
1864 && ((i
.types
[0] & JumpAbsolute
)
1865 != (t
->operand_types
[0] & JumpAbsolute
)))
1867 as_warn (_("indirect %s without `*'"), t
->name
);
1870 if ((t
->opcode_modifier
& (IsPrefix
|IgnoreSize
))
1871 == (IsPrefix
|IgnoreSize
))
1873 /* Warn them that a data or address size prefix doesn't
1874 affect assembly of the next line of code. */
1875 as_warn (_("stand-alone `%s' prefix"), t
->name
);
1879 /* Copy the template we found. */
1881 if (found_reverse_match
)
1883 /* If we found a reverse match we must alter the opcode
1884 direction bit. found_reverse_match holds bits to change
1885 (different for int & float insns). */
1887 i
.tm
.base_opcode
^= found_reverse_match
;
1889 i
.tm
.operand_types
[0] = t
->operand_types
[1];
1890 i
.tm
.operand_types
[1] = t
->operand_types
[0];
1893 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1896 && (i
.tm
.base_opcode
& 0xfffffde0) == 0xdce0)
1897 i
.tm
.base_opcode
^= FloatR
;
1899 if (i
.tm
.opcode_modifier
& FWait
)
1900 if (! add_prefix (FWAIT_OPCODE
))
1903 /* Check string instruction segment overrides. */
1904 if ((i
.tm
.opcode_modifier
& IsString
) != 0 && i
.mem_operands
!= 0)
1906 int mem_op
= (i
.types
[0] & AnyMem
) ? 0 : 1;
1907 if ((i
.tm
.operand_types
[mem_op
] & EsSeg
) != 0)
1909 if (i
.seg
[0] != NULL
&& i
.seg
[0] != &es
)
1911 as_bad (_("`%s' operand %d must use `%%es' segment"),
1916 /* There's only ever one segment override allowed per instruction.
1917 This instruction possibly has a legal segment override on the
1918 second operand, so copy the segment to where non-string
1919 instructions store it, allowing common code. */
1920 i
.seg
[0] = i
.seg
[1];
1922 else if ((i
.tm
.operand_types
[mem_op
+ 1] & EsSeg
) != 0)
1924 if (i
.seg
[1] != NULL
&& i
.seg
[1] != &es
)
1926 as_bad (_("`%s' operand %d must use `%%es' segment"),
1934 /* If matched instruction specifies an explicit instruction mnemonic
1936 if (i
.tm
.opcode_modifier
& (Size16
| Size32
| Size64
))
1938 if (i
.tm
.opcode_modifier
& Size16
)
1939 i
.suffix
= WORD_MNEM_SUFFIX
;
1940 else if (i
.tm
.opcode_modifier
& Size64
)
1941 i
.suffix
= QWORD_MNEM_SUFFIX
;
1943 i
.suffix
= LONG_MNEM_SUFFIX
;
1945 else if (i
.reg_operands
)
1947 /* If there's no instruction mnemonic suffix we try to invent one
1948 based on register operands. */
1951 /* We take i.suffix from the last register operand specified,
1952 Destination register type is more significant than source
1955 for (op
= i
.operands
; --op
>= 0;)
1956 if ((i
.types
[op
] & Reg
)
1957 && !(i
.tm
.operand_types
[op
] & InOutPortReg
))
1959 i
.suffix
= ((i
.types
[op
] & Reg8
) ? BYTE_MNEM_SUFFIX
:
1960 (i
.types
[op
] & Reg16
) ? WORD_MNEM_SUFFIX
:
1961 (i
.types
[op
] & Reg64
) ? QWORD_MNEM_SUFFIX
:
1966 else if (i
.suffix
== BYTE_MNEM_SUFFIX
)
1969 for (op
= i
.operands
; --op
>= 0;)
1971 /* If this is an eight bit register, it's OK. If it's
1972 the 16 or 32 bit version of an eight bit register,
1973 we will just use the low portion, and that's OK too. */
1974 if (i
.types
[op
] & Reg8
)
1977 /* movzx and movsx should not generate this warning. */
1979 && (i
.tm
.base_opcode
== 0xfb7
1980 || i
.tm
.base_opcode
== 0xfb6
1981 || i
.tm
.base_opcode
== 0x63
1982 || i
.tm
.base_opcode
== 0xfbe
1983 || i
.tm
.base_opcode
== 0xfbf))
1986 if ((i
.types
[op
] & WordReg
) && i
.op
[op
].regs
->reg_num
< 4
1988 /* Check that the template allows eight bit regs
1989 This kills insns such as `orb $1,%edx', which
1990 maybe should be allowed. */
1991 && (i
.tm
.operand_types
[op
] & (Reg8
|InOutPortReg
))
1995 /* Prohibit these changes in the 64bit mode, since
1996 the lowering is more complicated. */
1997 if (flag_code
== CODE_64BIT
1998 && (i
.tm
.operand_types
[op
] & InOutPortReg
) == 0)
1999 as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
2000 i
.op
[op
].regs
->reg_name
,
2002 #if REGISTER_WARNINGS
2004 && (i
.tm
.operand_types
[op
] & InOutPortReg
) == 0)
2005 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2007 + (i
.types
[op
] & Reg16
2008 ? REGNAM_AL
- REGNAM_AX
2009 : REGNAM_AL
- REGNAM_EAX
))->reg_name
,
2010 i
.op
[op
].regs
->reg_name
,
2015 /* Any other register is bad. */
2016 if (i
.types
[op
] & (Reg
| RegMMX
| RegXMM
2018 | Control
| Debug
| Test
2019 | FloatReg
| FloatAcc
))
2021 as_bad (_("`%%%s' not allowed with `%s%c'"),
2022 i
.op
[op
].regs
->reg_name
,
2029 else if (i
.suffix
== LONG_MNEM_SUFFIX
)
2033 for (op
= i
.operands
; --op
>= 0;)
2034 /* Reject eight bit registers, except where the template
2035 requires them. (eg. movzb) */
2036 if ((i
.types
[op
] & Reg8
) != 0
2037 && (i
.tm
.operand_types
[op
] & (Reg16
| Reg32
| Acc
)) != 0)
2039 as_bad (_("`%%%s' not allowed with `%s%c'"),
2040 i
.op
[op
].regs
->reg_name
,
2045 /* Warn if the e prefix on a general reg is missing. */
2046 else if ((!quiet_warnings
|| flag_code
== CODE_64BIT
)
2047 && (i
.types
[op
] & Reg16
) != 0
2048 && (i
.tm
.operand_types
[op
] & (Reg32
|Acc
)) != 0)
2050 /* Prohibit these changes in the 64bit mode, since
2051 the lowering is more complicated. */
2052 if (flag_code
== CODE_64BIT
)
2053 as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
2054 i
.op
[op
].regs
->reg_name
,
2056 #if REGISTER_WARNINGS
2058 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2059 (i
.op
[op
].regs
+ REGNAM_EAX
- REGNAM_AX
)->reg_name
,
2060 i
.op
[op
].regs
->reg_name
,
2064 /* Warn if the r prefix on a general reg is missing. */
2065 else if ((i
.types
[op
] & Reg64
) != 0
2066 && (i
.tm
.operand_types
[op
] & (Reg32
|Acc
)) != 0)
2068 as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
2069 i
.op
[op
].regs
->reg_name
,
2073 else if (i
.suffix
== QWORD_MNEM_SUFFIX
)
2077 for (op
= i
.operands
; --op
>= 0; )
2078 /* Reject eight bit registers, except where the template
2079 requires them. (eg. movzb) */
2080 if ((i
.types
[op
] & Reg8
) != 0
2081 && (i
.tm
.operand_types
[op
] & (Reg16
|Reg32
|Acc
)) != 0)
2083 as_bad (_("`%%%s' not allowed with `%s%c'"),
2084 i
.op
[op
].regs
->reg_name
,
2089 /* Warn if the e prefix on a general reg is missing. */
2090 else if (((i
.types
[op
] & Reg16
) != 0
2091 || (i
.types
[op
] & Reg32
) != 0)
2092 && (i
.tm
.operand_types
[op
] & (Reg32
|Acc
)) != 0)
2094 /* Prohibit these changes in the 64bit mode, since
2095 the lowering is more complicated. */
2096 as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
2097 i
.op
[op
].regs
->reg_name
,
2101 else if (i
.suffix
== WORD_MNEM_SUFFIX
)
2104 for (op
= i
.operands
; --op
>= 0;)
2105 /* Reject eight bit registers, except where the template
2106 requires them. (eg. movzb) */
2107 if ((i
.types
[op
] & Reg8
) != 0
2108 && (i
.tm
.operand_types
[op
] & (Reg16
|Reg32
|Acc
)) != 0)
2110 as_bad (_("`%%%s' not allowed with `%s%c'"),
2111 i
.op
[op
].regs
->reg_name
,
2116 /* Warn if the e prefix on a general reg is present. */
2117 else if ((!quiet_warnings
|| flag_code
== CODE_64BIT
)
2118 && (i
.types
[op
] & Reg32
) != 0
2119 && (i
.tm
.operand_types
[op
] & (Reg16
|Acc
)) != 0)
2121 /* Prohibit these changes in the 64bit mode, since
2122 the lowering is more complicated. */
2123 if (flag_code
== CODE_64BIT
)
2124 as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
2125 i
.op
[op
].regs
->reg_name
,
2128 #if REGISTER_WARNINGS
2129 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2130 (i
.op
[op
].regs
+ REGNAM_AX
- REGNAM_EAX
)->reg_name
,
2131 i
.op
[op
].regs
->reg_name
,
2136 else if (intel_syntax
&& (i
.tm
.opcode_modifier
& IgnoreSize
))
2137 /* Do nothing if the instruction is going to ignore the prefix. */
2142 else if ((i
.tm
.opcode_modifier
& DefaultSize
) && !i
.suffix
)
2144 i
.suffix
= stackop_size
;
2146 /* Make still unresolved immediate matches conform to size of immediate
2147 given in i.suffix. Note: overlap2 cannot be an immediate! */
2148 if ((overlap0
& (Imm8
| Imm8S
| Imm16
| Imm32
| Imm32S
))
2149 && overlap0
!= Imm8
&& overlap0
!= Imm8S
2150 && overlap0
!= Imm16
&& overlap0
!= Imm32S
2151 && overlap0
!= Imm32
&& overlap0
!= Imm64
)
2155 overlap0
&= (i
.suffix
== BYTE_MNEM_SUFFIX
? (Imm8
| Imm8S
) :
2156 (i
.suffix
== WORD_MNEM_SUFFIX
? Imm16
:
2157 (i
.suffix
== QWORD_MNEM_SUFFIX
? Imm64
| Imm32S
: Imm32
)));
2159 else if (overlap0
== (Imm16
| Imm32S
| Imm32
)
2160 || overlap0
== (Imm16
| Imm32
)
2161 || overlap0
== (Imm16
| Imm32S
))
2164 ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[DATA_PREFIX
] != 0)) ? Imm16
: Imm32S
;
2166 if (overlap0
!= Imm8
&& overlap0
!= Imm8S
2167 && overlap0
!= Imm16
&& overlap0
!= Imm32S
2168 && overlap0
!= Imm32
&& overlap0
!= Imm64
)
2170 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
2174 if ((overlap1
& (Imm8
| Imm8S
| Imm16
| Imm32S
| Imm32
))
2175 && overlap1
!= Imm8
&& overlap1
!= Imm8S
2176 && overlap1
!= Imm16
&& overlap1
!= Imm32S
2177 && overlap1
!= Imm32
&& overlap1
!= Imm64
)
2181 overlap1
&= (i
.suffix
== BYTE_MNEM_SUFFIX
? (Imm8
| Imm8S
) :
2182 (i
.suffix
== WORD_MNEM_SUFFIX
? Imm16
:
2183 (i
.suffix
== QWORD_MNEM_SUFFIX
? Imm64
| Imm32S
: Imm32
)));
2185 else if (overlap1
== (Imm16
| Imm32
| Imm32S
)
2186 || overlap1
== (Imm16
| Imm32
)
2187 || overlap1
== (Imm16
| Imm32S
))
2190 ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[DATA_PREFIX
] != 0)) ? Imm16
: Imm32S
;
2192 if (overlap1
!= Imm8
&& overlap1
!= Imm8S
2193 && overlap1
!= Imm16
&& overlap1
!= Imm32S
2194 && overlap1
!= Imm32
&& overlap1
!= Imm64
)
2196 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1
, i
.suffix
);
2200 assert ((overlap2
& Imm
) == 0);
2202 i
.types
[0] = overlap0
;
2203 if (overlap0
& ImplicitRegister
)
2205 if (overlap0
& Imm1
)
2206 i
.imm_operands
= 0; /* kludge for shift insns. */
2208 i
.types
[1] = overlap1
;
2209 if (overlap1
& ImplicitRegister
)
2212 i
.types
[2] = overlap2
;
2213 if (overlap2
& ImplicitRegister
)
2216 /* Finalize opcode. First, we change the opcode based on the operand
2217 size given by i.suffix: We need not change things for byte insns. */
2219 if (!i
.suffix
&& (i
.tm
.opcode_modifier
& W
))
2221 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
2225 /* For movzx and movsx, need to check the register type. */
2227 && (i
.tm
.base_opcode
== 0xfb6 || i
.tm
.base_opcode
== 0xfbe))
2228 if (i
.suffix
&& i
.suffix
== BYTE_MNEM_SUFFIX
)
2230 unsigned int prefix
= DATA_PREFIX_OPCODE
;
2232 if ((i
.op
[1].regs
->reg_type
& Reg16
) != 0)
2233 if (!add_prefix (prefix
))
2237 if (i
.suffix
&& i
.suffix
!= BYTE_MNEM_SUFFIX
)
2239 /* It's not a byte, select word/dword operation. */
2240 if (i
.tm
.opcode_modifier
& W
)
2242 if (i
.tm
.opcode_modifier
& ShortForm
)
2243 i
.tm
.base_opcode
|= 8;
2245 i
.tm
.base_opcode
|= 1;
2247 /* Now select between word & dword operations via the operand
2248 size prefix, except for instructions that will ignore this
2250 if (i
.suffix
!= QWORD_MNEM_SUFFIX
2251 && (i
.suffix
== LONG_MNEM_SUFFIX
) == (flag_code
== CODE_16BIT
)
2252 && !(i
.tm
.opcode_modifier
& IgnoreSize
))
2254 unsigned int prefix
= DATA_PREFIX_OPCODE
;
2255 if (i
.tm
.opcode_modifier
& JumpByte
) /* jcxz, loop */
2256 prefix
= ADDR_PREFIX_OPCODE
;
2258 if (! add_prefix (prefix
))
2262 /* Set mode64 for an operand. */
2263 if (i
.suffix
== QWORD_MNEM_SUFFIX
2264 && !(i
.tm
.opcode_modifier
& NoRex64
))
2267 if (flag_code
< CODE_64BIT
)
2269 as_bad (_("64bit operations available only in 64bit modes."));
2274 /* Size floating point instruction. */
2275 if (i
.suffix
== LONG_MNEM_SUFFIX
)
2277 if (i
.tm
.opcode_modifier
& FloatMF
)
2278 i
.tm
.base_opcode
^= 4;
2282 if (i
.tm
.opcode_modifier
& ImmExt
)
2284 /* These AMD 3DNow! and Intel Katmai New Instructions have an
2285 opcode suffix which is coded in the same place as an 8-bit
2286 immediate field would be. Here we fake an 8-bit immediate
2287 operand from the opcode suffix stored in tm.extension_opcode. */
2291 assert (i
.imm_operands
== 0 && i
.operands
<= 2 && 2 < MAX_OPERANDS
);
2293 exp
= &im_expressions
[i
.imm_operands
++];
2294 i
.op
[i
.operands
].imms
= exp
;
2295 i
.types
[i
.operands
++] = Imm8
;
2296 exp
->X_op
= O_constant
;
2297 exp
->X_add_number
= i
.tm
.extension_opcode
;
2298 i
.tm
.extension_opcode
= None
;
2301 /* For insns with operands there are more diddles to do to the opcode. */
2304 /* Default segment register this instruction will use
2305 for memory accesses. 0 means unknown.
2306 This is only for optimizing out unnecessary segment overrides. */
2307 const seg_entry
*default_seg
= 0;
2309 /* The imul $imm, %reg instruction is converted into
2310 imul $imm, %reg, %reg, and the clr %reg instruction
2311 is converted into xor %reg, %reg. */
2312 if (i
.tm
.opcode_modifier
& regKludge
)
2314 unsigned int first_reg_op
= (i
.types
[0] & Reg
) ? 0 : 1;
2315 /* Pretend we saw the extra register operand. */
2316 assert (i
.op
[first_reg_op
+ 1].regs
== 0);
2317 i
.op
[first_reg_op
+ 1].regs
= i
.op
[first_reg_op
].regs
;
2318 i
.types
[first_reg_op
+ 1] = i
.types
[first_reg_op
];
2322 if (i
.tm
.opcode_modifier
& ShortForm
)
2324 /* The register or float register operand is in operand 0 or 1. */
2325 unsigned int op
= (i
.types
[0] & (Reg
| FloatReg
)) ? 0 : 1;
2326 /* Register goes in low 3 bits of opcode. */
2327 i
.tm
.base_opcode
|= i
.op
[op
].regs
->reg_num
;
2328 if (i
.op
[op
].regs
->reg_flags
& RegRex
)
2330 if (!quiet_warnings
&& (i
.tm
.opcode_modifier
& Ugh
) != 0)
2332 /* Warn about some common errors, but press on regardless.
2333 The first case can be generated by gcc (<= 2.8.1). */
2334 if (i
.operands
== 2)
2336 /* Reversed arguments on faddp, fsubp, etc. */
2337 as_warn (_("translating to `%s %%%s,%%%s'"), i
.tm
.name
,
2338 i
.op
[1].regs
->reg_name
,
2339 i
.op
[0].regs
->reg_name
);
2343 /* Extraneous `l' suffix on fp insn. */
2344 as_warn (_("translating to `%s %%%s'"), i
.tm
.name
,
2345 i
.op
[0].regs
->reg_name
);
2349 else if (i
.tm
.opcode_modifier
& Modrm
)
2351 /* The opcode is completed (modulo i.tm.extension_opcode which
2352 must be put into the modrm byte).
2353 Now, we make the modrm & index base bytes based on all the
2354 info we've collected. */
2356 /* i.reg_operands MUST be the number of real register operands;
2357 implicit registers do not count. */
2358 if (i
.reg_operands
== 2)
2360 unsigned int source
, dest
;
2361 source
= ((i
.types
[0]
2362 & (Reg
| RegMMX
| RegXMM
2364 | Control
| Debug
| Test
))
2369 /* One of the register operands will be encoded in the
2370 i.tm.reg field, the other in the combined i.tm.mode
2371 and i.tm.regmem fields. If no form of this
2372 instruction supports a memory destination operand,
2373 then we assume the source operand may sometimes be
2374 a memory operand and so we need to store the
2375 destination in the i.rm.reg field. */
2376 if ((i
.tm
.operand_types
[dest
] & AnyMem
) == 0)
2378 i
.rm
.reg
= i
.op
[dest
].regs
->reg_num
;
2379 i
.rm
.regmem
= i
.op
[source
].regs
->reg_num
;
2380 if (i
.op
[dest
].regs
->reg_flags
& RegRex
)
2382 if (i
.op
[source
].regs
->reg_flags
& RegRex
)
2387 i
.rm
.reg
= i
.op
[source
].regs
->reg_num
;
2388 i
.rm
.regmem
= i
.op
[dest
].regs
->reg_num
;
2389 if (i
.op
[dest
].regs
->reg_flags
& RegRex
)
2391 if (i
.op
[source
].regs
->reg_flags
& RegRex
)
2396 { /* If it's not 2 reg operands... */
2399 unsigned int fake_zero_displacement
= 0;
2400 unsigned int op
= ((i
.types
[0] & AnyMem
)
2402 : (i
.types
[1] & AnyMem
) ? 1 : 2);
2409 if (! i
.disp_operands
)
2410 fake_zero_displacement
= 1;
2413 /* Operand is just <disp> */
2414 if ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[ADDR_PREFIX
] != 0))
2416 i
.rm
.regmem
= NO_BASE_REGISTER_16
;
2417 i
.types
[op
] &= ~Disp
;
2418 i
.types
[op
] |= Disp16
;
2420 else if (flag_code
!= CODE_64BIT
)
2422 i
.rm
.regmem
= NO_BASE_REGISTER
;
2423 i
.types
[op
] &= ~Disp
;
2424 i
.types
[op
] |= Disp32
;
2428 /* 64bit mode overwrites the 32bit
2429 absolute addressing by RIP relative
2430 addressing and absolute addressing
2431 is encoded by one of the redundant
2434 i
.rm
.regmem
= ESCAPE_TO_TWO_BYTE_ADDRESSING
;
2435 i
.sib
.base
= NO_BASE_REGISTER
;
2436 i
.sib
.index
= NO_INDEX_REGISTER
;
2437 i
.types
[op
] &= ~Disp
;
2438 i
.types
[op
] |= Disp32S
;
2441 else /* ! i.base_reg && i.index_reg */
2443 i
.sib
.index
= i
.index_reg
->reg_num
;
2444 i
.sib
.base
= NO_BASE_REGISTER
;
2445 i
.sib
.scale
= i
.log2_scale_factor
;
2446 i
.rm
.regmem
= ESCAPE_TO_TWO_BYTE_ADDRESSING
;
2447 i
.types
[op
] &= ~Disp
;
2448 if (flag_code
!= CODE_64BIT
)
2449 i
.types
[op
] |= Disp32
; /* Must be 32 bit */
2451 i
.types
[op
] |= Disp32S
;
2452 if (i
.index_reg
->reg_flags
& RegRex
)
2456 /* RIP addressing for 64bit mode. */
2457 else if (i
.base_reg
->reg_type
== BaseIndex
)
2459 i
.rm
.regmem
= NO_BASE_REGISTER
;
2460 i
.types
[op
] &= ~Disp
;
2461 i
.types
[op
] |= Disp32S
;
2462 i
.flags
[op
] = Operand_PCrel
;
2464 else if (i
.base_reg
->reg_type
& Reg16
)
2466 switch (i
.base_reg
->reg_num
)
2471 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
2472 i
.rm
.regmem
= i
.index_reg
->reg_num
- 6;
2479 if ((i
.types
[op
] & Disp
) == 0)
2481 /* fake (%bp) into 0(%bp) */
2482 i
.types
[op
] |= Disp8
;
2483 fake_zero_displacement
= 1;
2486 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
2487 i
.rm
.regmem
= i
.index_reg
->reg_num
- 6 + 2;
2489 default: /* (%si) -> 4 or (%di) -> 5 */
2490 i
.rm
.regmem
= i
.base_reg
->reg_num
- 6 + 4;
2492 i
.rm
.mode
= mode_from_disp_size (i
.types
[op
]);
2494 else /* i.base_reg and 32/64 bit mode */
2496 if (flag_code
== CODE_64BIT
2497 && (i
.types
[op
] & Disp
))
2499 if (i
.types
[op
] & Disp8
)
2500 i
.types
[op
] = Disp8
| Disp32S
;
2502 i
.types
[op
] = Disp32S
;
2504 i
.rm
.regmem
= i
.base_reg
->reg_num
;
2505 if (i
.base_reg
->reg_flags
& RegRex
)
2507 i
.sib
.base
= i
.base_reg
->reg_num
;
2508 /* x86-64 ignores REX prefix bit here to avoid
2509 decoder complications. */
2510 if ((i
.base_reg
->reg_num
& 7) == EBP_REG_NUM
)
2513 if (i
.disp_operands
== 0)
2515 fake_zero_displacement
= 1;
2516 i
.types
[op
] |= Disp8
;
2519 else if (i
.base_reg
->reg_num
== ESP_REG_NUM
)
2523 i
.sib
.scale
= i
.log2_scale_factor
;
2526 /* <disp>(%esp) becomes two byte modrm
2527 with no index register. We've already
2528 stored the code for esp in i.rm.regmem
2529 ie. ESCAPE_TO_TWO_BYTE_ADDRESSING. Any
2530 base register besides %esp will not use
2531 the extra modrm byte. */
2532 i
.sib
.index
= NO_INDEX_REGISTER
;
2533 #if ! SCALE1_WHEN_NO_INDEX
2534 /* Another case where we force the second
2536 if (i
.log2_scale_factor
)
2537 i
.rm
.regmem
= ESCAPE_TO_TWO_BYTE_ADDRESSING
;
2542 i
.sib
.index
= i
.index_reg
->reg_num
;
2543 i
.rm
.regmem
= ESCAPE_TO_TWO_BYTE_ADDRESSING
;
2544 if (i
.index_reg
->reg_flags
& RegRex
)
2547 i
.rm
.mode
= mode_from_disp_size (i
.types
[op
]);
2550 if (fake_zero_displacement
)
2552 /* Fakes a zero displacement assuming that i.types[op]
2553 holds the correct displacement size. */
2556 assert (i
.op
[op
].disps
== 0);
2557 exp
= &disp_expressions
[i
.disp_operands
++];
2558 i
.op
[op
].disps
= exp
;
2559 exp
->X_op
= O_constant
;
2560 exp
->X_add_number
= 0;
2561 exp
->X_add_symbol
= (symbolS
*) 0;
2562 exp
->X_op_symbol
= (symbolS
*) 0;
2566 /* Fill in i.rm.reg or i.rm.regmem field with register
2567 operand (if any) based on i.tm.extension_opcode.
2568 Again, we must be careful to make sure that
2569 segment/control/debug/test/MMX registers are coded
2570 into the i.rm.reg field. */
2575 & (Reg
| RegMMX
| RegXMM
2577 | Control
| Debug
| Test
))
2580 & (Reg
| RegMMX
| RegXMM
2582 | Control
| Debug
| Test
))
2585 /* If there is an extension opcode to put here, the
2586 register number must be put into the regmem field. */
2587 if (i
.tm
.extension_opcode
!= None
)
2589 i
.rm
.regmem
= i
.op
[op
].regs
->reg_num
;
2590 if (i
.op
[op
].regs
->reg_flags
& RegRex
)
2595 i
.rm
.reg
= i
.op
[op
].regs
->reg_num
;
2596 if (i
.op
[op
].regs
->reg_flags
& RegRex
)
2600 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
2601 we must set it to 3 to indicate this is a register
2602 operand in the regmem field. */
2603 if (!i
.mem_operands
)
2607 /* Fill in i.rm.reg field with extension opcode (if any). */
2608 if (i
.tm
.extension_opcode
!= None
)
2609 i
.rm
.reg
= i
.tm
.extension_opcode
;
2612 else if (i
.tm
.opcode_modifier
& (Seg2ShortForm
| Seg3ShortForm
))
2614 if (i
.tm
.base_opcode
== POP_SEG_SHORT
2615 && i
.op
[0].regs
->reg_num
== 1)
2617 as_bad (_("you can't `pop %%cs'"));
2620 i
.tm
.base_opcode
|= (i
.op
[0].regs
->reg_num
<< 3);
2621 if (i
.op
[0].regs
->reg_flags
& RegRex
)
2624 else if ((i
.tm
.base_opcode
& ~(D
|W
)) == MOV_AX_DISP32
)
2628 else if ((i
.tm
.opcode_modifier
& IsString
) != 0)
2630 /* For the string instructions that allow a segment override
2631 on one of their operands, the default segment is ds. */
2635 /* If a segment was explicitly specified,
2636 and the specified segment is not the default,
2637 use an opcode prefix to select it.
2638 If we never figured out what the default segment is,
2639 then default_seg will be zero at this point,
2640 and the specified segment prefix will always be used. */
2641 if ((i
.seg
[0]) && (i
.seg
[0] != default_seg
))
2643 if (! add_prefix (i
.seg
[0]->seg_prefix
))
2647 else if (!quiet_warnings
&& (i
.tm
.opcode_modifier
& Ugh
) != 0)
2649 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
2650 as_warn (_("translating to `%sp'"), i
.tm
.name
);
2654 /* Handle conversion of 'int $3' --> special int3 insn. */
2655 if (i
.tm
.base_opcode
== INT_OPCODE
&& i
.op
[0].imms
->X_add_number
== 3)
2657 i
.tm
.base_opcode
= INT3_OPCODE
;
2661 if ((i
.tm
.opcode_modifier
& (Jump
| JumpByte
| JumpDword
))
2662 && i
.op
[0].disps
->X_op
== O_constant
)
2664 /* Convert "jmp constant" (and "call constant") to a jump (call) to
2665 the absolute address given by the constant. Since ix86 jumps and
2666 calls are pc relative, we need to generate a reloc. */
2667 i
.op
[0].disps
->X_add_symbol
= &abs_symbol
;
2668 i
.op
[0].disps
->X_op
= O_symbol
;
2671 if (i
.tm
.opcode_modifier
& Rex64
)
2674 /* For 8bit registers we would need an empty rex prefix.
2675 Also in the case instruction is already having prefix,
2676 we need to convert old registers to new ones. */
2678 if (((i
.types
[0] & Reg8
) && (i
.op
[0].regs
->reg_flags
& RegRex64
))
2679 || ((i
.types
[1] & Reg8
) && (i
.op
[1].regs
->reg_flags
& RegRex64
))
2680 || ((i
.rex
.mode64
|| i
.rex
.extX
|| i
.rex
.extY
|| i
.rex
.extZ
|| i
.rex
.empty
)
2681 && ((i
.types
[0] & Reg8
) || (i
.types
[1] & Reg8
))))
2685 for (x
= 0; x
< 2; x
++)
2687 /* Look for 8bit operand that does use old registers. */
2688 if (i
.types
[x
] & Reg8
2689 && !(i
.op
[x
].regs
->reg_flags
& RegRex64
))
2691 /* In case it is "hi" register, give up. */
2692 if (i
.op
[x
].regs
->reg_num
> 3)
2693 as_bad (_("Can't encode registers '%%%s' in the instruction requiring REX prefix.\n"),
2694 i
.op
[x
].regs
->reg_name
);
2696 /* Otherwise it is equivalent to the extended register.
2697 Since the encoding don't change this is merely cosmetical
2698 cleanup for debug output. */
2700 i
.op
[x
].regs
= i
.op
[x
].regs
+ 8;
2705 if (i
.rex
.mode64
|| i
.rex
.extX
|| i
.rex
.extY
|| i
.rex
.extZ
|| i
.rex
.empty
)
2707 | (i
.rex
.mode64
? 8 : 0)
2708 | (i
.rex
.extX
? 4 : 0)
2709 | (i
.rex
.extY
? 2 : 0)
2710 | (i
.rex
.extZ
? 1 : 0));
2712 /* We are ready to output the insn. */
2716 /* Tie dwarf2 debug info to the address at the start of the insn.
2717 We can't do this after the insn has been output as the current
2718 frag may have been closed off. eg. by frag_var. */
2719 dwarf2_emit_insn (0);
2722 if (i
.tm
.opcode_modifier
& Jump
)
2726 relax_substateT subtype
;
2731 if (flag_code
== CODE_16BIT
)
2735 if (i
.prefix
[DATA_PREFIX
])
2741 /* Pentium4 branch hints. */
2742 if (i
.prefix
[SEG_PREFIX
] == CS_PREFIX_OPCODE
/* not taken */
2743 || i
.prefix
[SEG_PREFIX
] == DS_PREFIX_OPCODE
/* taken */)
2748 if (i
.prefix
[REX_PREFIX
])
2754 if (i
.prefixes
!= 0 && !intel_syntax
)
2755 as_warn (_("skipping prefixes on this instruction"));
2757 /* It's always a symbol; End frag & setup for relax.
2758 Make sure there is enough room in this frag for the largest
2759 instruction we may generate in md_convert_frag. This is 2
2760 bytes for the opcode and room for the prefix and largest
2762 frag_grow (prefix
+ 2 + 4);
2763 /* Prefix and 1 opcode byte go in fr_fix. */
2764 p
= frag_more (prefix
+ 1);
2765 if (i
.prefix
[DATA_PREFIX
])
2766 *p
++ = DATA_PREFIX_OPCODE
;
2767 if (i
.prefix
[SEG_PREFIX
] == CS_PREFIX_OPCODE
2768 || i
.prefix
[SEG_PREFIX
] == DS_PREFIX_OPCODE
)
2769 *p
++ = i
.prefix
[SEG_PREFIX
];
2770 if (i
.prefix
[REX_PREFIX
])
2771 *p
++ = i
.prefix
[REX_PREFIX
];
2772 *p
= i
.tm
.base_opcode
;
2774 if ((unsigned char) *p
== JUMP_PC_RELATIVE
)
2775 subtype
= ENCODE_RELAX_STATE (UNCOND_JUMP
, SMALL
);
2776 else if ((cpu_arch_flags
& Cpu386
) != 0)
2777 subtype
= ENCODE_RELAX_STATE (COND_JUMP
, SMALL
);
2779 subtype
= ENCODE_RELAX_STATE (COND_JUMP86
, SMALL
);
2782 sym
= i
.op
[0].disps
->X_add_symbol
;
2783 off
= i
.op
[0].disps
->X_add_number
;
2785 if (i
.op
[0].disps
->X_op
!= O_constant
2786 && i
.op
[0].disps
->X_op
!= O_symbol
)
2788 /* Handle complex expressions. */
2789 sym
= make_expr_symbol (i
.op
[0].disps
);
2793 /* 1 possible extra opcode + 4 byte displacement go in var part.
2794 Pass reloc in fr_var. */
2795 frag_var (rs_machine_dependent
, 5, i
.reloc
[0], subtype
, sym
, off
, p
);
2797 else if (i
.tm
.opcode_modifier
& (JumpByte
| JumpDword
))
2801 if (i
.tm
.opcode_modifier
& JumpByte
)
2803 /* This is a loop or jecxz type instruction. */
2805 if (i
.prefix
[ADDR_PREFIX
])
2807 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE
);
2810 /* Pentium4 branch hints. */
2811 if (i
.prefix
[SEG_PREFIX
] == CS_PREFIX_OPCODE
/* not taken */
2812 || i
.prefix
[SEG_PREFIX
] == DS_PREFIX_OPCODE
/* taken */)
2814 FRAG_APPEND_1_CHAR (i
.prefix
[SEG_PREFIX
]);
2823 if (flag_code
== CODE_16BIT
)
2826 if (i
.prefix
[DATA_PREFIX
])
2828 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE
);
2838 if (i
.prefix
[REX_PREFIX
])
2840 FRAG_APPEND_1_CHAR (i
.prefix
[REX_PREFIX
]);
2844 if (i
.prefixes
!= 0 && !intel_syntax
)
2845 as_warn (_("skipping prefixes on this instruction"));
2847 p
= frag_more (1 + size
);
2848 *p
++ = i
.tm
.base_opcode
;
2850 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, size
,
2851 i
.op
[0].disps
, 1, reloc (size
, 1, 1, i
.reloc
[0]));
2853 else if (i
.tm
.opcode_modifier
& JumpInterSegment
)
2860 if (flag_code
== CODE_16BIT
)
2864 if (i
.prefix
[DATA_PREFIX
])
2870 if (i
.prefix
[REX_PREFIX
])
2880 if (i
.prefixes
!= 0 && !intel_syntax
)
2881 as_warn (_("skipping prefixes on this instruction"));
2883 /* 1 opcode; 2 segment; offset */
2884 p
= frag_more (prefix
+ 1 + 2 + size
);
2886 if (i
.prefix
[DATA_PREFIX
])
2887 *p
++ = DATA_PREFIX_OPCODE
;
2889 if (i
.prefix
[REX_PREFIX
])
2890 *p
++ = i
.prefix
[REX_PREFIX
];
2892 *p
++ = i
.tm
.base_opcode
;
2893 if (i
.op
[1].imms
->X_op
== O_constant
)
2895 offsetT n
= i
.op
[1].imms
->X_add_number
;
2898 && !fits_in_unsigned_word (n
)
2899 && !fits_in_signed_word (n
))
2901 as_bad (_("16-bit jump out of range"));
2904 md_number_to_chars (p
, n
, size
);
2907 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, size
,
2908 i
.op
[1].imms
, 0, reloc (size
, 0, 0, i
.reloc
[1]));
2909 if (i
.op
[0].imms
->X_op
!= O_constant
)
2910 as_bad (_("can't handle non absolute segment in `%s'"),
2912 md_number_to_chars (p
+ size
, (valueT
) i
.op
[0].imms
->X_add_number
, 2);
2916 /* Output normal instructions here. */
2919 /* All opcodes on i386 have eighter 1 or 2 bytes. We may use third
2920 byte for the SSE instructions to specify prefix they require. */
2921 if (i
.tm
.base_opcode
& 0xff0000)
2922 add_prefix ((i
.tm
.base_opcode
>> 16) & 0xff);
2924 /* The prefix bytes. */
2926 q
< i
.prefix
+ sizeof (i
.prefix
) / sizeof (i
.prefix
[0]);
2932 md_number_to_chars (p
, (valueT
) *q
, 1);
2936 /* Now the opcode; be careful about word order here! */
2937 if (fits_in_unsigned_byte (i
.tm
.base_opcode
))
2939 FRAG_APPEND_1_CHAR (i
.tm
.base_opcode
);
2944 /* Put out high byte first: can't use md_number_to_chars! */
2945 *p
++ = (i
.tm
.base_opcode
>> 8) & 0xff;
2946 *p
= i
.tm
.base_opcode
& 0xff;
2949 /* Now the modrm byte and sib byte (if present). */
2950 if (i
.tm
.opcode_modifier
& Modrm
)
2953 md_number_to_chars (p
,
2954 (valueT
) (i
.rm
.regmem
<< 0
2958 /* If i.rm.regmem == ESP (4)
2959 && i.rm.mode != (Register mode)
2961 ==> need second modrm byte. */
2962 if (i
.rm
.regmem
== ESCAPE_TO_TWO_BYTE_ADDRESSING
2964 && !(i
.base_reg
&& (i
.base_reg
->reg_type
& Reg16
) != 0))
2967 md_number_to_chars (p
,
2968 (valueT
) (i
.sib
.base
<< 0
2970 | i
.sib
.scale
<< 6),
2975 if (i
.disp_operands
)
2977 register unsigned int n
;
2979 for (n
= 0; n
< i
.operands
; n
++)
2981 if (i
.types
[n
] & Disp
)
2983 if (i
.op
[n
].disps
->X_op
== O_constant
)
2989 if (i
.types
[n
] & (Disp8
| Disp16
| Disp64
))
2992 if (i
.types
[n
] & Disp8
)
2994 if (i
.types
[n
] & Disp64
)
2997 val
= offset_in_range (i
.op
[n
].disps
->X_add_number
,
2999 p
= frag_more (size
);
3000 md_number_to_chars (p
, val
, size
);
3006 int pcrel
= (i
.flags
[n
] & Operand_PCrel
) != 0;
3008 /* The PC relative address is computed relative
3009 to the instruction boundary, so in case immediate
3010 fields follows, we need to adjust the value. */
3011 if (pcrel
&& i
.imm_operands
)
3014 register unsigned int n1
;
3016 for (n1
= 0; n1
< i
.operands
; n1
++)
3017 if (i
.types
[n1
] & Imm
)
3019 if (i
.types
[n1
] & (Imm8
| Imm8S
| Imm16
| Imm64
))
3022 if (i
.types
[n1
] & (Imm8
| Imm8S
))
3024 if (i
.types
[n1
] & Imm64
)
3029 /* We should find the immediate. */
3030 if (n1
== i
.operands
)
3032 i
.op
[n
].disps
->X_add_number
-= imm_size
;
3035 if (i
.types
[n
] & Disp32S
)
3038 if (i
.types
[n
] & (Disp16
| Disp64
))
3041 if (i
.types
[n
] & Disp64
)
3045 p
= frag_more (size
);
3046 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, size
,
3047 i
.op
[n
].disps
, pcrel
,
3048 reloc (size
, pcrel
, sign
, i
.reloc
[n
]));
3054 /* Output immediate. */
3057 register unsigned int n
;
3059 for (n
= 0; n
< i
.operands
; n
++)
3061 if (i
.types
[n
] & Imm
)
3063 if (i
.op
[n
].imms
->X_op
== O_constant
)
3069 if (i
.types
[n
] & (Imm8
| Imm8S
| Imm16
| Imm64
))
3072 if (i
.types
[n
] & (Imm8
| Imm8S
))
3074 else if (i
.types
[n
] & Imm64
)
3077 val
= offset_in_range (i
.op
[n
].imms
->X_add_number
,
3079 p
= frag_more (size
);
3080 md_number_to_chars (p
, val
, size
);
3084 /* Not absolute_section.
3085 Need a 32-bit fixup (don't support 8bit
3086 non-absolute imms). Try to support other
3088 RELOC_ENUM reloc_type
;
3092 if ((i
.types
[n
] & (Imm32S
))
3093 && i
.suffix
== QWORD_MNEM_SUFFIX
)
3095 if (i
.types
[n
] & (Imm8
| Imm8S
| Imm16
| Imm64
))
3098 if (i
.types
[n
] & (Imm8
| Imm8S
))
3100 if (i
.types
[n
] & Imm64
)
3104 p
= frag_more (size
);
3105 reloc_type
= reloc (size
, 0, sign
, i
.reloc
[n
]);
3106 #ifdef BFD_ASSEMBLER
3107 if (reloc_type
== BFD_RELOC_32
3109 && GOT_symbol
== i
.op
[n
].imms
->X_add_symbol
3110 && (i
.op
[n
].imms
->X_op
== O_symbol
3111 || (i
.op
[n
].imms
->X_op
== O_add
3112 && ((symbol_get_value_expression
3113 (i
.op
[n
].imms
->X_op_symbol
)->X_op
)
3116 /* We don't support dynamic linking on x86-64 yet. */
3117 if (flag_code
== CODE_64BIT
)
3119 reloc_type
= BFD_RELOC_386_GOTPC
;
3120 i
.op
[n
].imms
->X_add_number
+= 3;
3123 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, size
,
3124 i
.op
[n
].imms
, 0, reloc_type
);
3136 #endif /* DEBUG386 */
3141 static char *lex_got
PARAMS ((RELOC_ENUM
*, int *));
3143 /* Parse operands of the form
3144 <symbol>@GOTOFF+<nnn>
3145 and similar .plt or .got references.
3147 If we find one, set up the correct relocation in RELOC and copy the
3148 input string, minus the `@GOTOFF' into a malloc'd buffer for
3149 parsing by the calling routine. Return this buffer, and if ADJUST
3150 is non-null set it to the length of the string we removed from the
3151 input line. Otherwise return NULL. */
3153 lex_got (reloc
, adjust
)
3157 static const char * const mode_name
[NUM_FLAG_CODE
] = { "32", "16", "64" };
3158 static const struct {
3160 const RELOC_ENUM rel
[NUM_FLAG_CODE
];
3162 { "PLT", { BFD_RELOC_386_PLT32
, 0, BFD_RELOC_X86_64_PLT32
} },
3163 { "GOTOFF", { BFD_RELOC_386_GOTOFF
, 0, 0 } },
3164 { "GOTPCREL", { 0, 0, BFD_RELOC_X86_64_GOTPCREL
} },
3165 { "GOT", { BFD_RELOC_386_GOT32
, 0, BFD_RELOC_X86_64_GOT32
} }
3170 for (cp
= input_line_pointer
; *cp
!= '@'; cp
++)
3171 if (is_end_of_line
[(unsigned char) *cp
])
3174 for (j
= 0; j
< sizeof (gotrel
) / sizeof (gotrel
[0]); j
++)
3178 len
= strlen (gotrel
[j
].str
);
3179 if (strncasecmp (cp
+ 1, gotrel
[j
].str
, len
) == 0)
3181 if (gotrel
[j
].rel
[(unsigned int) flag_code
] != 0)
3184 char *tmpbuf
, *past_reloc
;
3186 *reloc
= gotrel
[j
].rel
[(unsigned int) flag_code
];
3190 if (GOT_symbol
== NULL
)
3191 GOT_symbol
= symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME
);
3193 /* Replace the relocation token with ' ', so that
3194 errors like foo@GOTOFF1 will be detected. */
3196 /* The length of the first part of our input line. */
3197 first
= cp
- input_line_pointer
;
3199 /* The second part goes from after the reloc token until
3200 (and including) an end_of_line char. Don't use strlen
3201 here as the end_of_line char may not be a NUL. */
3202 past_reloc
= cp
+ 1 + len
;
3203 for (cp
= past_reloc
; !is_end_of_line
[(unsigned char) *cp
++]; )
3205 second
= cp
- past_reloc
;
3207 /* Allocate and copy string. The trailing NUL shouldn't
3208 be necessary, but be safe. */
3209 tmpbuf
= xmalloc (first
+ second
+ 2);
3210 memcpy (tmpbuf
, input_line_pointer
, first
);
3211 tmpbuf
[first
] = ' ';
3212 memcpy (tmpbuf
+ first
+ 1, past_reloc
, second
);
3213 tmpbuf
[first
+ second
+ 1] = '\0';
3217 as_bad (_("@%s reloc is not supported in %s bit mode"),
3218 gotrel
[j
].str
, mode_name
[(unsigned int) flag_code
]);
3223 /* Might be a symbol version string. Don't as_bad here. */
3227 /* x86_cons_fix_new is called via the expression parsing code when a
3228 reloc is needed. We use this hook to get the correct .got reloc. */
3229 static RELOC_ENUM got_reloc
= NO_RELOC
;
3232 x86_cons_fix_new (frag
, off
, len
, exp
)
3238 RELOC_ENUM r
= reloc (len
, 0, 0, got_reloc
);
3239 got_reloc
= NO_RELOC
;
3240 fix_new_exp (frag
, off
, len
, exp
, 0, r
);
3244 x86_cons (exp
, size
)
3250 /* Handle @GOTOFF and the like in an expression. */
3252 char *gotfree_input_line
;
3255 save
= input_line_pointer
;
3256 gotfree_input_line
= lex_got (&got_reloc
, &adjust
);
3257 if (gotfree_input_line
)
3258 input_line_pointer
= gotfree_input_line
;
3262 if (gotfree_input_line
)
3264 /* expression () has merrily parsed up to the end of line,
3265 or a comma - in the wrong buffer. Transfer how far
3266 input_line_pointer has moved to the right buffer. */
3267 input_line_pointer
= (save
3268 + (input_line_pointer
- gotfree_input_line
)
3270 free (gotfree_input_line
);
3278 static int i386_immediate
PARAMS ((char *));
3281 i386_immediate (imm_start
)
3284 char *save_input_line_pointer
;
3286 char *gotfree_input_line
;
3291 if (i
.imm_operands
== MAX_IMMEDIATE_OPERANDS
)
3293 as_bad (_("only 1 or 2 immediate operands are allowed"));
3297 exp
= &im_expressions
[i
.imm_operands
++];
3298 i
.op
[this_operand
].imms
= exp
;
3300 if (is_space_char (*imm_start
))
3303 save_input_line_pointer
= input_line_pointer
;
3304 input_line_pointer
= imm_start
;
3307 gotfree_input_line
= lex_got (&i
.reloc
[this_operand
], NULL
);
3308 if (gotfree_input_line
)
3309 input_line_pointer
= gotfree_input_line
;
3312 exp_seg
= expression (exp
);
3315 if (*input_line_pointer
)
3316 as_bad (_("junk `%s' after expression"), input_line_pointer
);
3318 input_line_pointer
= save_input_line_pointer
;
3320 if (gotfree_input_line
)
3321 free (gotfree_input_line
);
3324 if (exp
->X_op
== O_absent
|| exp
->X_op
== O_big
)
3326 /* Missing or bad expr becomes absolute 0. */
3327 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
3329 exp
->X_op
= O_constant
;
3330 exp
->X_add_number
= 0;
3331 exp
->X_add_symbol
= (symbolS
*) 0;
3332 exp
->X_op_symbol
= (symbolS
*) 0;
3334 else if (exp
->X_op
== O_constant
)
3336 /* Size it properly later. */
3337 i
.types
[this_operand
] |= Imm64
;
3338 /* If BFD64, sign extend val. */
3339 if (!use_rela_relocations
)
3340 if ((exp
->X_add_number
& ~(((addressT
) 2 << 31) - 1)) == 0)
3341 exp
->X_add_number
= (exp
->X_add_number
^ ((addressT
) 1 << 31)) - ((addressT
) 1 << 31);
3343 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3345 #ifdef BFD_ASSEMBLER
3346 && OUTPUT_FLAVOR
== bfd_target_aout_flavour
3348 && exp_seg
!= text_section
3349 && exp_seg
!= data_section
3350 && exp_seg
!= bss_section
3351 && exp_seg
!= undefined_section
3352 #ifdef BFD_ASSEMBLER
3353 && !bfd_is_com_section (exp_seg
)
3357 #ifdef BFD_ASSEMBLER
3358 as_bad (_("unimplemented segment %s in operand"), exp_seg
->name
);
3360 as_bad (_("unimplemented segment type %d in operand"), exp_seg
);
3367 /* This is an address. The size of the address will be
3368 determined later, depending on destination register,
3369 suffix, or the default for the section. */
3370 i
.types
[this_operand
] |= Imm8
| Imm16
| Imm32
| Imm32S
| Imm64
;
3376 static char *i386_scale
PARAMS ((char *));
3383 char *save
= input_line_pointer
;
3385 input_line_pointer
= scale
;
3386 val
= get_absolute_expression ();
3392 i
.log2_scale_factor
= 0;
3395 i
.log2_scale_factor
= 1;
3398 i
.log2_scale_factor
= 2;
3401 i
.log2_scale_factor
= 3;
3404 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
3406 input_line_pointer
= save
;
3409 if (i
.log2_scale_factor
!= 0 && ! i
.index_reg
)
3411 as_warn (_("scale factor of %d without an index register"),
3412 1 << i
.log2_scale_factor
);
3413 #if SCALE1_WHEN_NO_INDEX
3414 i
.log2_scale_factor
= 0;
3417 scale
= input_line_pointer
;
3418 input_line_pointer
= save
;
3422 static int i386_displacement
PARAMS ((char *, char *));
3425 i386_displacement (disp_start
, disp_end
)
3429 register expressionS
*exp
;
3431 char *save_input_line_pointer
;
3433 char *gotfree_input_line
;
3435 int bigdisp
= Disp32
;
3437 if ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[ADDR_PREFIX
] != 0))
3439 if (flag_code
== CODE_64BIT
)
3441 i
.types
[this_operand
] |= bigdisp
;
3443 exp
= &disp_expressions
[i
.disp_operands
];
3444 i
.op
[this_operand
].disps
= exp
;
3446 save_input_line_pointer
= input_line_pointer
;
3447 input_line_pointer
= disp_start
;
3448 END_STRING_AND_SAVE (disp_end
);
3450 #ifndef GCC_ASM_O_HACK
3451 #define GCC_ASM_O_HACK 0
3454 END_STRING_AND_SAVE (disp_end
+ 1);
3455 if ((i
.types
[this_operand
] & BaseIndex
) != 0
3456 && displacement_string_end
[-1] == '+')
3458 /* This hack is to avoid a warning when using the "o"
3459 constraint within gcc asm statements.
3462 #define _set_tssldt_desc(n,addr,limit,type) \
3463 __asm__ __volatile__ ( \
3465 "movw %w1,2+%0\n\t" \
3467 "movb %b1,4+%0\n\t" \
3468 "movb %4,5+%0\n\t" \
3469 "movb $0,6+%0\n\t" \
3470 "movb %h1,7+%0\n\t" \
3472 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
3474 This works great except that the output assembler ends
3475 up looking a bit weird if it turns out that there is
3476 no offset. You end up producing code that looks like:
3489 So here we provide the missing zero. */
3491 *displacement_string_end
= '0';
3495 gotfree_input_line
= lex_got (&i
.reloc
[this_operand
], NULL
);
3496 if (gotfree_input_line
)
3497 input_line_pointer
= gotfree_input_line
;
3500 exp_seg
= expression (exp
);
3503 if (*input_line_pointer
)
3504 as_bad (_("junk `%s' after expression"), input_line_pointer
);
3506 RESTORE_END_STRING (disp_end
+ 1);
3508 RESTORE_END_STRING (disp_end
);
3509 input_line_pointer
= save_input_line_pointer
;
3511 if (gotfree_input_line
)
3512 free (gotfree_input_line
);
3515 #ifdef BFD_ASSEMBLER
3516 /* We do this to make sure that the section symbol is in
3517 the symbol table. We will ultimately change the relocation
3518 to be relative to the beginning of the section. */
3519 if (i
.reloc
[this_operand
] == BFD_RELOC_386_GOTOFF
3520 || i
.reloc
[this_operand
] == BFD_RELOC_X86_64_GOTPCREL
)
3522 if (exp
->X_op
!= O_symbol
)
3524 as_bad (_("bad expression used with @%s"),
3525 (i
.reloc
[this_operand
] == BFD_RELOC_X86_64_GOTPCREL
3531 if (S_IS_LOCAL (exp
->X_add_symbol
)
3532 && S_GET_SEGMENT (exp
->X_add_symbol
) != undefined_section
)
3533 section_symbol (S_GET_SEGMENT (exp
->X_add_symbol
));
3534 exp
->X_op
= O_subtract
;
3535 exp
->X_op_symbol
= GOT_symbol
;
3536 if (i
.reloc
[this_operand
] == BFD_RELOC_X86_64_GOTPCREL
)
3537 i
.reloc
[this_operand
] = BFD_RELOC_32_PCREL
;
3539 i
.reloc
[this_operand
] = BFD_RELOC_32
;
3543 if (exp
->X_op
== O_absent
|| exp
->X_op
== O_big
)
3545 /* Missing or bad expr becomes absolute 0. */
3546 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
3548 exp
->X_op
= O_constant
;
3549 exp
->X_add_number
= 0;
3550 exp
->X_add_symbol
= (symbolS
*) 0;
3551 exp
->X_op_symbol
= (symbolS
*) 0;
3554 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3555 if (exp
->X_op
!= O_constant
3556 #ifdef BFD_ASSEMBLER
3557 && OUTPUT_FLAVOR
== bfd_target_aout_flavour
3559 && exp_seg
!= text_section
3560 && exp_seg
!= data_section
3561 && exp_seg
!= bss_section
3562 && exp_seg
!= undefined_section
)
3564 #ifdef BFD_ASSEMBLER
3565 as_bad (_("unimplemented segment %s in operand"), exp_seg
->name
);
3567 as_bad (_("unimplemented segment type %d in operand"), exp_seg
);
3572 else if (flag_code
== CODE_64BIT
)
3573 i
.types
[this_operand
] |= Disp32S
| Disp32
;
3577 static int i386_index_check
PARAMS ((const char *));
3579 /* Make sure the memory operand we've been dealt is valid.
3580 Return 1 on success, 0 on a failure. */
3583 i386_index_check (operand_string
)
3584 const char *operand_string
;
3587 #if INFER_ADDR_PREFIX
3593 if (flag_code
== CODE_64BIT
)
3597 && ((i
.base_reg
->reg_type
& Reg64
) == 0)
3598 && (i
.base_reg
->reg_type
!= BaseIndex
3601 && ((i
.index_reg
->reg_type
& (Reg64
|BaseIndex
))
3602 != (Reg64
|BaseIndex
))))
3607 if ((flag_code
== CODE_16BIT
) ^ (i
.prefix
[ADDR_PREFIX
] != 0))
3611 && ((i
.base_reg
->reg_type
& (Reg16
|BaseIndex
|RegRex
))
3612 != (Reg16
|BaseIndex
)))
3614 && (((i
.index_reg
->reg_type
& (Reg16
|BaseIndex
))
3615 != (Reg16
|BaseIndex
))
3617 && i
.base_reg
->reg_num
< 6
3618 && i
.index_reg
->reg_num
>= 6
3619 && i
.log2_scale_factor
== 0))))
3626 && (i
.base_reg
->reg_type
& (Reg32
| RegRex
)) != Reg32
)
3628 && ((i
.index_reg
->reg_type
& (Reg32
|BaseIndex
|RegRex
))
3629 != (Reg32
|BaseIndex
))))
3635 #if INFER_ADDR_PREFIX
3636 if (flag_code
!= CODE_64BIT
3637 && i
.prefix
[ADDR_PREFIX
] == 0 && stackop_size
!= '\0')
3639 i
.prefix
[ADDR_PREFIX
] = ADDR_PREFIX_OPCODE
;
3641 /* Change the size of any displacement too. At most one of
3642 Disp16 or Disp32 is set.
3643 FIXME. There doesn't seem to be any real need for separate
3644 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
3645 Removing them would probably clean up the code quite a lot. */
3646 if (i
.types
[this_operand
] & (Disp16
|Disp32
))
3647 i
.types
[this_operand
] ^= (Disp16
|Disp32
);
3652 as_bad (_("`%s' is not a valid base/index expression"),
3656 as_bad (_("`%s' is not a valid %s bit base/index expression"),
3658 flag_code_names
[flag_code
]);
3664 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
3668 i386_operand (operand_string
)
3669 char *operand_string
;
3673 char *op_string
= operand_string
;
3675 if (is_space_char (*op_string
))
3678 /* We check for an absolute prefix (differentiating,
3679 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
3680 if (*op_string
== ABSOLUTE_PREFIX
)
3683 if (is_space_char (*op_string
))
3685 i
.types
[this_operand
] |= JumpAbsolute
;
3688 /* Check if operand is a register. */
3689 if ((*op_string
== REGISTER_PREFIX
|| allow_naked_reg
)
3690 && (r
= parse_register (op_string
, &end_op
)) != NULL
)
3692 /* Check for a segment override by searching for ':' after a
3693 segment register. */
3695 if (is_space_char (*op_string
))
3697 if (*op_string
== ':' && (r
->reg_type
& (SReg2
| SReg3
)))
3702 i
.seg
[i
.mem_operands
] = &es
;
3705 i
.seg
[i
.mem_operands
] = &cs
;
3708 i
.seg
[i
.mem_operands
] = &ss
;
3711 i
.seg
[i
.mem_operands
] = &ds
;
3714 i
.seg
[i
.mem_operands
] = &fs
;
3717 i
.seg
[i
.mem_operands
] = &gs
;
3721 /* Skip the ':' and whitespace. */
3723 if (is_space_char (*op_string
))
3726 if (!is_digit_char (*op_string
)
3727 && !is_identifier_char (*op_string
)
3728 && *op_string
!= '('
3729 && *op_string
!= ABSOLUTE_PREFIX
)
3731 as_bad (_("bad memory operand `%s'"), op_string
);
3734 /* Handle case of %es:*foo. */
3735 if (*op_string
== ABSOLUTE_PREFIX
)
3738 if (is_space_char (*op_string
))
3740 i
.types
[this_operand
] |= JumpAbsolute
;
3742 goto do_memory_reference
;
3746 as_bad (_("junk `%s' after register"), op_string
);
3749 i
.types
[this_operand
] |= r
->reg_type
& ~BaseIndex
;
3750 i
.op
[this_operand
].regs
= r
;
3753 else if (*op_string
== REGISTER_PREFIX
)
3755 as_bad (_("bad register name `%s'"), op_string
);
3758 else if (*op_string
== IMMEDIATE_PREFIX
)
3761 if (i
.types
[this_operand
] & JumpAbsolute
)
3763 as_bad (_("immediate operand illegal with absolute jump"));
3766 if (!i386_immediate (op_string
))
3769 else if (is_digit_char (*op_string
)
3770 || is_identifier_char (*op_string
)
3771 || *op_string
== '(')
3773 /* This is a memory reference of some sort. */
3776 /* Start and end of displacement string expression (if found). */
3777 char *displacement_string_start
;
3778 char *displacement_string_end
;
3780 do_memory_reference
:
3781 if ((i
.mem_operands
== 1
3782 && (current_templates
->start
->opcode_modifier
& IsString
) == 0)
3783 || i
.mem_operands
== 2)
3785 as_bad (_("too many memory references for `%s'"),
3786 current_templates
->start
->name
);
3790 /* Check for base index form. We detect the base index form by
3791 looking for an ')' at the end of the operand, searching
3792 for the '(' matching it, and finding a REGISTER_PREFIX or ','
3794 base_string
= op_string
+ strlen (op_string
);
3797 if (is_space_char (*base_string
))
3800 /* If we only have a displacement, set-up for it to be parsed later. */
3801 displacement_string_start
= op_string
;
3802 displacement_string_end
= base_string
+ 1;
3804 if (*base_string
== ')')
3807 unsigned int parens_balanced
= 1;
3808 /* We've already checked that the number of left & right ()'s are
3809 equal, so this loop will not be infinite. */
3813 if (*base_string
== ')')
3815 if (*base_string
== '(')
3818 while (parens_balanced
);
3820 temp_string
= base_string
;
3822 /* Skip past '(' and whitespace. */
3824 if (is_space_char (*base_string
))
3827 if (*base_string
== ','
3828 || ((*base_string
== REGISTER_PREFIX
|| allow_naked_reg
)
3829 && (i
.base_reg
= parse_register (base_string
, &end_op
)) != NULL
))
3831 displacement_string_end
= temp_string
;
3833 i
.types
[this_operand
] |= BaseIndex
;
3837 base_string
= end_op
;
3838 if (is_space_char (*base_string
))
3842 /* There may be an index reg or scale factor here. */
3843 if (*base_string
== ',')
3846 if (is_space_char (*base_string
))
3849 if ((*base_string
== REGISTER_PREFIX
|| allow_naked_reg
)
3850 && (i
.index_reg
= parse_register (base_string
, &end_op
)) != NULL
)
3852 base_string
= end_op
;
3853 if (is_space_char (*base_string
))
3855 if (*base_string
== ',')
3858 if (is_space_char (*base_string
))
3861 else if (*base_string
!= ')')
3863 as_bad (_("expecting `,' or `)' after index register in `%s'"),
3868 else if (*base_string
== REGISTER_PREFIX
)
3870 as_bad (_("bad register name `%s'"), base_string
);
3874 /* Check for scale factor. */
3875 if (*base_string
!= ')')
3877 char *end_scale
= i386_scale (base_string
);
3882 base_string
= end_scale
;
3883 if (is_space_char (*base_string
))
3885 if (*base_string
!= ')')
3887 as_bad (_("expecting `)' after scale factor in `%s'"),
3892 else if (!i
.index_reg
)
3894 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
3899 else if (*base_string
!= ')')
3901 as_bad (_("expecting `,' or `)' after base register in `%s'"),
3906 else if (*base_string
== REGISTER_PREFIX
)
3908 as_bad (_("bad register name `%s'"), base_string
);
3913 /* If there's an expression beginning the operand, parse it,
3914 assuming displacement_string_start and
3915 displacement_string_end are meaningful. */
3916 if (displacement_string_start
!= displacement_string_end
)
3918 if (!i386_displacement (displacement_string_start
,
3919 displacement_string_end
))
3923 /* Special case for (%dx) while doing input/output op. */
3925 && i
.base_reg
->reg_type
== (Reg16
| InOutPortReg
)
3927 && i
.log2_scale_factor
== 0
3928 && i
.seg
[i
.mem_operands
] == 0
3929 && (i
.types
[this_operand
] & Disp
) == 0)
3931 i
.types
[this_operand
] = InOutPortReg
;
3935 if (i386_index_check (operand_string
) == 0)
3941 /* It's not a memory operand; argh! */
3942 as_bad (_("invalid char %s beginning operand %d `%s'"),
3943 output_invalid (*op_string
),
3948 return 1; /* Normal return. */
3951 /* md_estimate_size_before_relax()
3953 Called just before relax() for rs_machine_dependent frags. The x86
3954 assembler uses these frags to handle variable size jump
3957 Any symbol that is now undefined will not become defined.
3958 Return the correct fr_subtype in the frag.
3959 Return the initial "guess for variable size of frag" to caller.
3960 The guess is actually the growth beyond the fixed part. Whatever
3961 we do to grow the fixed or variable part contributes to our
3965 md_estimate_size_before_relax (fragP
, segment
)
3966 register fragS
*fragP
;
3967 register segT segment
;
3969 /* We've already got fragP->fr_subtype right; all we have to do is
3970 check for un-relaxable symbols. On an ELF system, we can't relax
3971 an externally visible symbol, because it may be overridden by a
3973 if (S_GET_SEGMENT (fragP
->fr_symbol
) != segment
3974 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3975 || S_IS_EXTERNAL (fragP
->fr_symbol
)
3976 || S_IS_WEAK (fragP
->fr_symbol
)
3980 /* Symbol is undefined in this segment, or we need to keep a
3981 reloc so that weak symbols can be overridden. */
3982 int size
= (fragP
->fr_subtype
& CODE16
) ? 2 : 4;
3983 RELOC_ENUM reloc_type
;
3984 unsigned char *opcode
;
3987 if (fragP
->fr_var
!= NO_RELOC
)
3988 reloc_type
= fragP
->fr_var
;
3990 reloc_type
= BFD_RELOC_16_PCREL
;
3992 reloc_type
= BFD_RELOC_32_PCREL
;
3994 old_fr_fix
= fragP
->fr_fix
;
3995 opcode
= (unsigned char *) fragP
->fr_opcode
;
3997 switch (TYPE_FROM_RELAX_STATE (fragP
->fr_subtype
))
4000 /* Make jmp (0xeb) a (d)word displacement jump. */
4002 fragP
->fr_fix
+= size
;
4003 fix_new (fragP
, old_fr_fix
, size
,
4005 fragP
->fr_offset
, 1,
4010 if (no_cond_jump_promotion
)
4015 /* Negate the condition, and branch past an
4016 unconditional jump. */
4019 /* Insert an unconditional jump. */
4021 /* We added two extra opcode bytes, and have a two byte
4023 fragP
->fr_fix
+= 2 + 2;
4024 fix_new (fragP
, old_fr_fix
+ 2, 2,
4026 fragP
->fr_offset
, 1,
4033 if (no_cond_jump_promotion
)
4036 /* This changes the byte-displacement jump 0x7N
4037 to the (d)word-displacement jump 0x0f,0x8N. */
4038 opcode
[1] = opcode
[0] + 0x10;
4039 opcode
[0] = TWO_BYTE_OPCODE_ESCAPE
;
4040 /* We've added an opcode byte. */
4041 fragP
->fr_fix
+= 1 + size
;
4042 fix_new (fragP
, old_fr_fix
+ 1, size
,
4044 fragP
->fr_offset
, 1,
4049 BAD_CASE (fragP
->fr_subtype
);
4053 return fragP
->fr_fix
- old_fr_fix
;
4057 /* Guess size depending on current relax state. Initially the relax
4058 state will correspond to a short jump and we return 1, because
4059 the variable part of the frag (the branch offset) is one byte
4060 long. However, we can relax a section more than once and in that
4061 case we must either set fr_subtype back to the unrelaxed state,
4062 or return the value for the appropriate branch. */
4063 return md_relax_table
[fragP
->fr_subtype
].rlx_length
;
4066 /* Called after relax() is finished.
4068 In: Address of frag.
4069 fr_type == rs_machine_dependent.
4070 fr_subtype is what the address relaxed to.
4072 Out: Any fixSs and constants are set up.
4073 Caller will turn frag into a ".space 0". */
4075 #ifndef BFD_ASSEMBLER
4077 md_convert_frag (headers
, sec
, fragP
)
4078 object_headers
*headers ATTRIBUTE_UNUSED
;
4079 segT sec ATTRIBUTE_UNUSED
;
4080 register fragS
*fragP
;
4083 md_convert_frag (abfd
, sec
, fragP
)
4084 bfd
*abfd ATTRIBUTE_UNUSED
;
4085 segT sec ATTRIBUTE_UNUSED
;
4086 register fragS
*fragP
;
4089 register unsigned char *opcode
;
4090 unsigned char *where_to_put_displacement
= NULL
;
4091 offsetT target_address
;
4092 offsetT opcode_address
;
4093 unsigned int extension
= 0;
4094 offsetT displacement_from_opcode_start
;
4096 opcode
= (unsigned char *) fragP
->fr_opcode
;
4098 /* Address we want to reach in file space. */
4099 target_address
= S_GET_VALUE (fragP
->fr_symbol
) + fragP
->fr_offset
;
4101 /* Address opcode resides at in file space. */
4102 opcode_address
= fragP
->fr_address
+ fragP
->fr_fix
;
4104 /* Displacement from opcode start to fill into instruction. */
4105 displacement_from_opcode_start
= target_address
- opcode_address
;
4107 if ((fragP
->fr_subtype
& BIG
) == 0)
4109 /* Don't have to change opcode. */
4110 extension
= 1; /* 1 opcode + 1 displacement */
4111 where_to_put_displacement
= &opcode
[1];
4115 if (no_cond_jump_promotion
4116 && TYPE_FROM_RELAX_STATE (fragP
->fr_subtype
) != UNCOND_JUMP
)
4117 as_warn_where (fragP
->fr_file
, fragP
->fr_line
, _("long jump required"));
4119 switch (fragP
->fr_subtype
)
4121 case ENCODE_RELAX_STATE (UNCOND_JUMP
, BIG
):
4122 extension
= 4; /* 1 opcode + 4 displacement */
4124 where_to_put_displacement
= &opcode
[1];
4127 case ENCODE_RELAX_STATE (UNCOND_JUMP
, BIG16
):
4128 extension
= 2; /* 1 opcode + 2 displacement */
4130 where_to_put_displacement
= &opcode
[1];
4133 case ENCODE_RELAX_STATE (COND_JUMP
, BIG
):
4134 case ENCODE_RELAX_STATE (COND_JUMP86
, BIG
):
4135 extension
= 5; /* 2 opcode + 4 displacement */
4136 opcode
[1] = opcode
[0] + 0x10;
4137 opcode
[0] = TWO_BYTE_OPCODE_ESCAPE
;
4138 where_to_put_displacement
= &opcode
[2];
4141 case ENCODE_RELAX_STATE (COND_JUMP
, BIG16
):
4142 extension
= 3; /* 2 opcode + 2 displacement */
4143 opcode
[1] = opcode
[0] + 0x10;
4144 opcode
[0] = TWO_BYTE_OPCODE_ESCAPE
;
4145 where_to_put_displacement
= &opcode
[2];
4148 case ENCODE_RELAX_STATE (COND_JUMP86
, BIG16
):
4153 where_to_put_displacement
= &opcode
[3];
4157 BAD_CASE (fragP
->fr_subtype
);
4162 /* Now put displacement after opcode. */
4163 md_number_to_chars ((char *) where_to_put_displacement
,
4164 (valueT
) (displacement_from_opcode_start
- extension
),
4165 DISP_SIZE_FROM_RELAX_STATE (fragP
->fr_subtype
));
4166 fragP
->fr_fix
+= extension
;
4169 /* Size of byte displacement jmp. */
4170 int md_short_jump_size
= 2;
4172 /* Size of dword displacement jmp. */
4173 int md_long_jump_size
= 5;
4175 /* Size of relocation record. */
4176 const int md_reloc_size
= 8;
4179 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
4181 addressT from_addr
, to_addr
;
4182 fragS
*frag ATTRIBUTE_UNUSED
;
4183 symbolS
*to_symbol ATTRIBUTE_UNUSED
;
4187 offset
= to_addr
- (from_addr
+ 2);
4188 /* Opcode for byte-disp jump. */
4189 md_number_to_chars (ptr
, (valueT
) 0xeb, 1);
4190 md_number_to_chars (ptr
+ 1, (valueT
) offset
, 1);
4194 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
4196 addressT from_addr
, to_addr
;
4197 fragS
*frag ATTRIBUTE_UNUSED
;
4198 symbolS
*to_symbol ATTRIBUTE_UNUSED
;
4202 offset
= to_addr
- (from_addr
+ 5);
4203 md_number_to_chars (ptr
, (valueT
) 0xe9, 1);
4204 md_number_to_chars (ptr
+ 1, (valueT
) offset
, 4);
4207 /* Apply a fixup (fixS) to segment data, once it has been determined
4208 by our caller that we have all the info we need to fix it up.
4210 On the 386, immediates, displacements, and data pointers are all in
4211 the same (little-endian) format, so we don't need to care about which
4215 md_apply_fix3 (fixP
, valP
, seg
)
4216 /* The fix we're to put in. */
4218 /* Pointer to the value of the bits. */
4220 /* Segment fix is from. */
4221 segT seg ATTRIBUTE_UNUSED
;
4223 char *p
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
4224 valueT value
= * valP
;
4226 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
4229 switch (fixP
->fx_r_type
)
4235 fixP
->fx_r_type
= BFD_RELOC_32_PCREL
;
4238 fixP
->fx_r_type
= BFD_RELOC_16_PCREL
;
4241 fixP
->fx_r_type
= BFD_RELOC_8_PCREL
;
4246 /* This is a hack. There should be a better way to handle this.
4247 This covers for the fact that bfd_install_relocation will
4248 subtract the current location (for partial_inplace, PC relative
4249 relocations); see more below. */
4250 if ((fixP
->fx_r_type
== BFD_RELOC_32_PCREL
4251 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
4252 || fixP
->fx_r_type
== BFD_RELOC_8_PCREL
)
4253 && fixP
->fx_addsy
&& !use_rela_relocations
)
4256 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
4258 || OUTPUT_FLAVOR
== bfd_target_coff_flavour
4261 value
+= fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4263 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4264 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
)
4266 segT fseg
= S_GET_SEGMENT (fixP
->fx_addsy
);
4269 || (symbol_section_p (fixP
->fx_addsy
)
4270 && fseg
!= absolute_section
))
4271 && ! S_IS_EXTERNAL (fixP
->fx_addsy
)
4272 && ! S_IS_WEAK (fixP
->fx_addsy
)
4273 && S_IS_DEFINED (fixP
->fx_addsy
)
4274 && ! S_IS_COMMON (fixP
->fx_addsy
))
4276 /* Yes, we add the values in twice. This is because
4277 bfd_perform_relocation subtracts them out again. I think
4278 bfd_perform_relocation is broken, but I don't dare change
4280 value
+= fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4284 #if defined (OBJ_COFF) && defined (TE_PE)
4285 /* For some reason, the PE format does not store a section
4286 address offset for a PC relative symbol. */
4287 if (S_GET_SEGMENT (fixP
->fx_addsy
) != seg
)
4288 value
+= md_pcrel_from (fixP
);
4292 /* Fix a few things - the dynamic linker expects certain values here,
4293 and we must not dissappoint it. */
4294 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4295 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
4297 switch (fixP
->fx_r_type
)
4299 case BFD_RELOC_386_PLT32
:
4300 case BFD_RELOC_X86_64_PLT32
:
4301 /* Make the jump instruction point to the address of the operand. At
4302 runtime we merely add the offset to the actual PLT entry. */
4305 case BFD_RELOC_386_GOTPC
:
4307 /* This is tough to explain. We end up with this one if we have
4308 * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal
4309 * here is to obtain the absolute address of the GOT, and it is strongly
4310 * preferable from a performance point of view to avoid using a runtime
4311 * relocation for this. The actual sequence of instructions often look
4317 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
4319 * The call and pop essentially return the absolute address of
4320 * the label .L66 and store it in %ebx. The linker itself will
4321 * ultimately change the first operand of the addl so that %ebx points to
4322 * the GOT, but to keep things simple, the .o file must have this operand
4323 * set so that it generates not the absolute address of .L66, but the
4324 * absolute address of itself. This allows the linker itself simply
4325 * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
4326 * added in, and the addend of the relocation is stored in the operand
4327 * field for the instruction itself.
4329 * Our job here is to fix the operand so that it would add the correct
4330 * offset so that %ebx would point to itself. The thing that is tricky is
4331 * that .-.L66 will point to the beginning of the instruction, so we need
4332 * to further modify the operand so that it will point to itself.
4333 * There are other cases where you have something like:
4335 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4337 * and here no correction would be required. Internally in the assembler
4338 * we treat operands of this form as not being pcrel since the '.' is
4339 * explicitly mentioned, and I wonder whether it would simplify matters
4340 * to do it this way. Who knows. In earlier versions of the PIC patches,
4341 * the pcrel_adjust field was used to store the correction, but since the
4342 * expression is not pcrel, I felt it would be confusing to do it this
4347 case BFD_RELOC_386_GOT32
:
4348 case BFD_RELOC_X86_64_GOT32
:
4349 value
= 0; /* Fully resolved at runtime. No addend. */
4351 case BFD_RELOC_386_GOTOFF
:
4352 case BFD_RELOC_X86_64_GOTPCREL
:
4355 case BFD_RELOC_VTABLE_INHERIT
:
4356 case BFD_RELOC_VTABLE_ENTRY
:
4363 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
4365 #endif /* defined (BFD_ASSEMBLER) && !defined (TE_Mach) */
4367 /* Are we finished with this relocation now? */
4368 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
4370 #ifdef BFD_ASSEMBLER
4371 else if (use_rela_relocations
)
4373 fixP
->fx_no_overflow
= 1;
4377 md_number_to_chars (p
, value
, fixP
->fx_size
);
4380 #define MAX_LITTLENUMS 6
4382 /* Turn the string pointed to by litP into a floating point constant
4383 of type TYPE, and emit the appropriate bytes. The number of
4384 LITTLENUMS emitted is stored in *SIZEP. An error message is
4385 returned, or NULL on OK. */
4388 md_atof (type
, litP
, sizeP
)
4394 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
4395 LITTLENUM_TYPE
*wordP
;
4417 return _("Bad call to md_atof ()");
4419 t
= atof_ieee (input_line_pointer
, type
, words
);
4421 input_line_pointer
= t
;
4423 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
4424 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
4425 the bigendian 386. */
4426 for (wordP
= words
+ prec
- 1; prec
--;)
4428 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
4429 litP
+= sizeof (LITTLENUM_TYPE
);
4434 char output_invalid_buf
[8];
4441 sprintf (output_invalid_buf
, "'%c'", c
);
4443 sprintf (output_invalid_buf
, "(0x%x)", (unsigned) c
);
4444 return output_invalid_buf
;
4447 /* REG_STRING starts *before* REGISTER_PREFIX. */
4449 static const reg_entry
*
4450 parse_register (reg_string
, end_op
)
4454 char *s
= reg_string
;
4456 char reg_name_given
[MAX_REG_NAME_SIZE
+ 1];
4459 /* Skip possible REGISTER_PREFIX and possible whitespace. */
4460 if (*s
== REGISTER_PREFIX
)
4463 if (is_space_char (*s
))
4467 while ((*p
++ = register_chars
[(unsigned char) *s
]) != '\0')
4469 if (p
>= reg_name_given
+ MAX_REG_NAME_SIZE
)
4470 return (const reg_entry
*) NULL
;
4474 /* For naked regs, make sure that we are not dealing with an identifier.
4475 This prevents confusing an identifier like `eax_var' with register
4477 if (allow_naked_reg
&& identifier_chars
[(unsigned char) *s
])
4478 return (const reg_entry
*) NULL
;
4482 r
= (const reg_entry
*) hash_find (reg_hash
, reg_name_given
);
4484 /* Handle floating point regs, allowing spaces in the (i) part. */
4485 if (r
== i386_regtab
/* %st is first entry of table */)
4487 if (is_space_char (*s
))
4492 if (is_space_char (*s
))
4494 if (*s
>= '0' && *s
<= '7')
4496 r
= &i386_float_regtab
[*s
- '0'];
4498 if (is_space_char (*s
))
4506 /* We have "%st(" then garbage. */
4507 return (const reg_entry
*) NULL
;
4512 && r
->reg_flags
& (RegRex64
|RegRex
)
4513 && flag_code
!= CODE_64BIT
)
4515 return (const reg_entry
*) NULL
;
4521 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4522 const char *md_shortopts
= "kVQ:sq";
4524 const char *md_shortopts
= "q";
4527 struct option md_longopts
[] = {
4528 #define OPTION_32 (OPTION_MD_BASE + 0)
4529 {"32", no_argument
, NULL
, OPTION_32
},
4530 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4531 #define OPTION_64 (OPTION_MD_BASE + 1)
4532 {"64", no_argument
, NULL
, OPTION_64
},
4534 {NULL
, no_argument
, NULL
, 0}
4536 size_t md_longopts_size
= sizeof (md_longopts
);
4539 md_parse_option (c
, arg
)
4541 char *arg ATTRIBUTE_UNUSED
;
4549 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4550 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
4551 should be emitted or not. FIXME: Not implemented. */
4555 /* -V: SVR4 argument to print version ID. */
4557 print_version_id ();
4560 /* -k: Ignore for FreeBSD compatibility. */
4565 /* -s: On i386 Solaris, this tells the native assembler to use
4566 .stab instead of .stab.excl. We always use .stab anyhow. */
4571 const char **list
, **l
;
4573 list
= bfd_target_list ();
4574 for (l
= list
; *l
!= NULL
; l
++)
4575 if (strcmp (*l
, "elf64-x86-64") == 0)
4577 default_arch
= "x86_64";
4581 as_fatal (_("No compiled in support for x86_64"));
4588 default_arch
= "i386";
4598 md_show_usage (stream
)
4601 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4602 fprintf (stream
, _("\
4604 -V print assembler version number\n\
4606 -q quieten some warnings\n\
4609 fprintf (stream
, _("\
4610 -q quieten some warnings\n"));
4614 #ifdef BFD_ASSEMBLER
4615 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
4616 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
4618 /* Pick the target format to use. */
4621 i386_target_format ()
4623 if (!strcmp (default_arch
, "x86_64"))
4624 set_code_flag (CODE_64BIT
);
4625 else if (!strcmp (default_arch
, "i386"))
4626 set_code_flag (CODE_32BIT
);
4628 as_fatal (_("Unknown architecture"));
4629 switch (OUTPUT_FLAVOR
)
4631 #ifdef OBJ_MAYBE_AOUT
4632 case bfd_target_aout_flavour
:
4633 return AOUT_TARGET_FORMAT
;
4635 #ifdef OBJ_MAYBE_COFF
4636 case bfd_target_coff_flavour
:
4639 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
4640 case bfd_target_elf_flavour
:
4642 if (flag_code
== CODE_64BIT
)
4643 use_rela_relocations
= 1;
4644 return flag_code
== CODE_64BIT
? "elf64-x86-64" : "elf32-i386";
4653 #endif /* OBJ_MAYBE_ more than one */
4655 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
4656 void i386_elf_emit_arch_note ()
4658 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
4659 && cpu_arch_name
!= NULL
)
4662 asection
*seg
= now_seg
;
4663 subsegT subseg
= now_subseg
;
4664 Elf_Internal_Note i_note
;
4665 Elf_External_Note e_note
;
4666 asection
*note_secp
;
4669 /* Create the .note section. */
4670 note_secp
= subseg_new (".note", 0);
4671 bfd_set_section_flags (stdoutput
,
4673 SEC_HAS_CONTENTS
| SEC_READONLY
);
4675 /* Process the arch string. */
4676 len
= strlen (cpu_arch_name
);
4678 i_note
.namesz
= len
+ 1;
4680 i_note
.type
= NT_ARCH
;
4681 p
= frag_more (sizeof (e_note
.namesz
));
4682 md_number_to_chars (p
, (valueT
) i_note
.namesz
, sizeof (e_note
.namesz
));
4683 p
= frag_more (sizeof (e_note
.descsz
));
4684 md_number_to_chars (p
, (valueT
) i_note
.descsz
, sizeof (e_note
.descsz
));
4685 p
= frag_more (sizeof (e_note
.type
));
4686 md_number_to_chars (p
, (valueT
) i_note
.type
, sizeof (e_note
.type
));
4687 p
= frag_more (len
+ 1);
4688 strcpy (p
, cpu_arch_name
);
4690 frag_align (2, 0, 0);
4692 subseg_set (seg
, subseg
);
4696 #endif /* BFD_ASSEMBLER */
4699 md_undefined_symbol (name
)
4702 if (name
[0] == GLOBAL_OFFSET_TABLE_NAME
[0]
4703 && name
[1] == GLOBAL_OFFSET_TABLE_NAME
[1]
4704 && name
[2] == GLOBAL_OFFSET_TABLE_NAME
[2]
4705 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
4709 if (symbol_find (name
))
4710 as_bad (_("GOT already in symbol table"));
4711 GOT_symbol
= symbol_new (name
, undefined_section
,
4712 (valueT
) 0, &zero_address_frag
);
4719 /* Round up a section size to the appropriate boundary. */
4722 md_section_align (segment
, size
)
4723 segT segment ATTRIBUTE_UNUSED
;
4726 #ifdef BFD_ASSEMBLER
4727 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4728 if (OUTPUT_FLAVOR
== bfd_target_aout_flavour
)
4730 /* For a.out, force the section size to be aligned. If we don't do
4731 this, BFD will align it for us, but it will not write out the
4732 final bytes of the section. This may be a bug in BFD, but it is
4733 easier to fix it here since that is how the other a.out targets
4737 align
= bfd_get_section_alignment (stdoutput
, segment
);
4738 size
= ((size
+ (1 << align
) - 1) & ((valueT
) -1 << align
));
4746 /* On the i386, PC-relative offsets are relative to the start of the
4747 next instruction. That is, the address of the offset, plus its
4748 size, since the offset is always the last part of the insn. */
4751 md_pcrel_from (fixP
)
4754 return fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4761 int ignore ATTRIBUTE_UNUSED
;
4765 temp
= get_absolute_expression ();
4766 subseg_set (bss_section
, (subsegT
) temp
);
4767 demand_empty_rest_of_line ();
4772 #ifdef BFD_ASSEMBLER
4775 i386_validate_fix (fixp
)
4778 if (fixp
->fx_subsy
&& fixp
->fx_subsy
== GOT_symbol
)
4780 /* GOTOFF relocation are nonsense in 64bit mode. */
4781 if (fixp
->fx_r_type
== BFD_RELOC_32_PCREL
)
4783 if (flag_code
!= CODE_64BIT
)
4785 fixp
->fx_r_type
= BFD_RELOC_X86_64_GOTPCREL
;
4789 if (flag_code
== CODE_64BIT
)
4791 fixp
->fx_r_type
= BFD_RELOC_386_GOTOFF
;
4798 tc_gen_reloc (section
, fixp
)
4799 asection
*section ATTRIBUTE_UNUSED
;
4803 bfd_reloc_code_real_type code
;
4805 switch (fixp
->fx_r_type
)
4807 case BFD_RELOC_X86_64_PLT32
:
4808 case BFD_RELOC_X86_64_GOT32
:
4809 case BFD_RELOC_X86_64_GOTPCREL
:
4810 case BFD_RELOC_386_PLT32
:
4811 case BFD_RELOC_386_GOT32
:
4812 case BFD_RELOC_386_GOTOFF
:
4813 case BFD_RELOC_386_GOTPC
:
4814 case BFD_RELOC_X86_64_32S
:
4816 case BFD_RELOC_VTABLE_ENTRY
:
4817 case BFD_RELOC_VTABLE_INHERIT
:
4818 code
= fixp
->fx_r_type
;
4823 switch (fixp
->fx_size
)
4826 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
4827 _("can not do %d byte pc-relative relocation"),
4829 code
= BFD_RELOC_32_PCREL
;
4831 case 1: code
= BFD_RELOC_8_PCREL
; break;
4832 case 2: code
= BFD_RELOC_16_PCREL
; break;
4833 case 4: code
= BFD_RELOC_32_PCREL
; break;
4838 switch (fixp
->fx_size
)
4841 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
4842 _("can not do %d byte relocation"),
4844 code
= BFD_RELOC_32
;
4846 case 1: code
= BFD_RELOC_8
; break;
4847 case 2: code
= BFD_RELOC_16
; break;
4848 case 4: code
= BFD_RELOC_32
; break;
4849 case 8: code
= BFD_RELOC_64
; break;
4855 if (code
== BFD_RELOC_32
4857 && fixp
->fx_addsy
== GOT_symbol
)
4859 /* We don't support GOTPC on 64bit targets. */
4860 if (flag_code
== CODE_64BIT
)
4862 code
= BFD_RELOC_386_GOTPC
;
4865 rel
= (arelent
*) xmalloc (sizeof (arelent
));
4866 rel
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
4867 *rel
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
4869 rel
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
4870 if (!use_rela_relocations
)
4872 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
4873 vtable entry to be used in the relocation's section offset. */
4874 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
4875 rel
->address
= fixp
->fx_offset
;
4878 rel
->addend
= fixp
->fx_addnumber
;
4882 /* Use the rela in 64bit mode. */
4885 rel
->addend
= fixp
->fx_offset
;
4887 rel
->addend
-= fixp
->fx_size
;
4890 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, code
);
4891 if (rel
->howto
== NULL
)
4893 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
4894 _("cannot represent relocation type %s"),
4895 bfd_get_reloc_code_name (code
));
4896 /* Set howto to a garbage value so that we can keep going. */
4897 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
4898 assert (rel
->howto
!= NULL
);
4904 #else /* ! BFD_ASSEMBLER */
4906 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
4908 tc_aout_fix_to_chars (where
, fixP
, segment_address_in_file
)
4911 relax_addressT segment_address_in_file
;
4913 /* In: length of relocation (or of address) in chars: 1, 2 or 4.
4914 Out: GNU LD relocation length code: 0, 1, or 2. */
4916 static const unsigned char nbytes_r_length
[] = { 42, 0, 1, 42, 2 };
4919 know (fixP
->fx_addsy
!= NULL
);
4921 md_number_to_chars (where
,
4922 (valueT
) (fixP
->fx_frag
->fr_address
4923 + fixP
->fx_where
- segment_address_in_file
),
4926 r_symbolnum
= (S_IS_DEFINED (fixP
->fx_addsy
)
4927 ? S_GET_TYPE (fixP
->fx_addsy
)
4928 : fixP
->fx_addsy
->sy_number
);
4930 where
[6] = (r_symbolnum
>> 16) & 0x0ff;
4931 where
[5] = (r_symbolnum
>> 8) & 0x0ff;
4932 where
[4] = r_symbolnum
& 0x0ff;
4933 where
[7] = ((((!S_IS_DEFINED (fixP
->fx_addsy
)) << 3) & 0x08)
4934 | ((nbytes_r_length
[fixP
->fx_size
] << 1) & 0x06)
4935 | (((fixP
->fx_pcrel
<< 0) & 0x01) & 0x0f));
4938 #endif /* OBJ_AOUT or OBJ_BOUT. */
4940 #if defined (I386COFF)
4943 tc_coff_fix2rtype (fixP
)
4946 if (fixP
->fx_r_type
== R_IMAGEBASE
)
4949 return (fixP
->fx_pcrel
?
4950 (fixP
->fx_size
== 1 ? R_PCRBYTE
:
4951 fixP
->fx_size
== 2 ? R_PCRWORD
:
4953 (fixP
->fx_size
== 1 ? R_RELBYTE
:
4954 fixP
->fx_size
== 2 ? R_RELWORD
:
4959 tc_coff_sizemachdep (frag
)
4963 return (frag
->fr_next
->fr_address
- frag
->fr_address
);
4968 #endif /* I386COFF */
4970 #endif /* ! BFD_ASSEMBLER */
4972 /* Parse operands using Intel syntax. This implements a recursive descent
4973 parser based on the BNF grammar published in Appendix B of the MASM 6.1
4976 FIXME: We do not recognize the full operand grammar defined in the MASM
4977 documentation. In particular, all the structure/union and
4978 high-level macro operands are missing.
4980 Uppercase words are terminals, lower case words are non-terminals.
4981 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
4982 bars '|' denote choices. Most grammar productions are implemented in
4983 functions called 'intel_<production>'.
4985 Initial production is 'expr'.
4991 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
4993 constant digits [[ radixOverride ]]
4995 dataType BYTE | WORD | DWORD | QWORD | XWORD
5028 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
5029 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
5031 hexdigit a | b | c | d | e | f
5032 | A | B | C | D | E | F
5042 register specialRegister
5046 segmentRegister CS | DS | ES | FS | GS | SS
5048 specialRegister CR0 | CR2 | CR3
5049 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
5050 | TR3 | TR4 | TR5 | TR6 | TR7
5052 We simplify the grammar in obvious places (e.g., register parsing is
5053 done by calling parse_register) and eliminate immediate left recursion
5054 to implement a recursive-descent parser.
5094 /* Parsing structure for the intel syntax parser. Used to implement the
5095 semantic actions for the operand grammar. */
5096 struct intel_parser_s
5098 char *op_string
; /* The string being parsed. */
5099 int got_a_float
; /* Whether the operand is a float. */
5100 int op_modifier
; /* Operand modifier. */
5101 int is_mem
; /* 1 if operand is memory reference. */
5102 const reg_entry
*reg
; /* Last register reference found. */
5103 char *disp
; /* Displacement string being built. */
5106 static struct intel_parser_s intel_parser
;
5108 /* Token structure for parsing intel syntax. */
5111 int code
; /* Token code. */
5112 const reg_entry
*reg
; /* Register entry for register tokens. */
5113 char *str
; /* String representation. */
5116 static struct intel_token cur_token
, prev_token
;
5118 /* Token codes for the intel parser. Since T_SHORT is already used
5119 by COFF, undefine it first to prevent a warning. */
5134 /* Prototypes for intel parser functions. */
5135 static int intel_match_token
PARAMS ((int code
));
5136 static void intel_get_token
PARAMS ((void));
5137 static void intel_putback_token
PARAMS ((void));
5138 static int intel_expr
PARAMS ((void));
5139 static int intel_e05
PARAMS ((void));
5140 static int intel_e05_1
PARAMS ((void));
5141 static int intel_e06
PARAMS ((void));
5142 static int intel_e06_1
PARAMS ((void));
5143 static int intel_e09
PARAMS ((void));
5144 static int intel_e09_1
PARAMS ((void));
5145 static int intel_e10
PARAMS ((void));
5146 static int intel_e10_1
PARAMS ((void));
5147 static int intel_e11
PARAMS ((void));
5150 i386_intel_operand (operand_string
, got_a_float
)
5151 char *operand_string
;
5157 /* Initialize token holders. */
5158 cur_token
.code
= prev_token
.code
= T_NIL
;
5159 cur_token
.reg
= prev_token
.reg
= NULL
;
5160 cur_token
.str
= prev_token
.str
= NULL
;
5162 /* Initialize parser structure. */
5163 p
= intel_parser
.op_string
= (char *) malloc (strlen (operand_string
) + 1);
5166 strcpy (intel_parser
.op_string
, operand_string
);
5167 intel_parser
.got_a_float
= got_a_float
;
5168 intel_parser
.op_modifier
= -1;
5169 intel_parser
.is_mem
= 0;
5170 intel_parser
.reg
= NULL
;
5171 intel_parser
.disp
= (char *) malloc (strlen (operand_string
) + 1);
5172 if (intel_parser
.disp
== NULL
)
5174 intel_parser
.disp
[0] = '\0';
5176 /* Read the first token and start the parser. */
5178 ret
= intel_expr ();
5182 /* If we found a memory reference, hand it over to i386_displacement
5183 to fill in the rest of the operand fields. */
5184 if (intel_parser
.is_mem
)
5186 if ((i
.mem_operands
== 1
5187 && (current_templates
->start
->opcode_modifier
& IsString
) == 0)
5188 || i
.mem_operands
== 2)
5190 as_bad (_("too many memory references for '%s'"),
5191 current_templates
->start
->name
);
5196 char *s
= intel_parser
.disp
;
5199 /* Add the displacement expression. */
5201 ret
= i386_displacement (s
, s
+ strlen (s
))
5202 && i386_index_check (s
);
5206 /* Constant and OFFSET expressions are handled by i386_immediate. */
5207 else if (intel_parser
.op_modifier
== OFFSET_FLAT
5208 || intel_parser
.reg
== NULL
)
5209 ret
= i386_immediate (intel_parser
.disp
);
5213 free (intel_parser
.disp
);
5223 /* expr SHORT e05 */
5224 if (cur_token
.code
== T_SHORT
)
5226 intel_parser
.op_modifier
= SHORT
;
5227 intel_match_token (T_SHORT
);
5229 return (intel_e05 ());
5234 return intel_e05 ();
5244 return (intel_e06 () && intel_e05_1 ());
5250 /* e05' addOp e06 e05' */
5251 if (cur_token
.code
== '+' || cur_token
.code
== '-')
5253 strcat (intel_parser
.disp
, cur_token
.str
);
5254 intel_match_token (cur_token
.code
);
5256 return (intel_e06 () && intel_e05_1 ());
5271 return (intel_e09 () && intel_e06_1 ());
5277 /* e06' mulOp e09 e06' */
5278 if (cur_token
.code
== '*' || cur_token
.code
== '/')
5280 strcat (intel_parser
.disp
, cur_token
.str
);
5281 intel_match_token (cur_token
.code
);
5283 return (intel_e09 () && intel_e06_1 ());
5291 /* e09 OFFSET e10 e09'
5300 /* e09 OFFSET e10 e09' */
5301 if (cur_token
.code
== T_OFFSET
)
5303 intel_parser
.is_mem
= 0;
5304 intel_parser
.op_modifier
= OFFSET_FLAT
;
5305 intel_match_token (T_OFFSET
);
5307 return (intel_e10 () && intel_e09_1 ());
5312 return (intel_e10 () && intel_e09_1 ());
5318 /* e09' PTR e10 e09' */
5319 if (cur_token
.code
== T_PTR
)
5321 if (prev_token
.code
== T_BYTE
)
5322 i
.suffix
= BYTE_MNEM_SUFFIX
;
5324 else if (prev_token
.code
== T_WORD
)
5326 if (intel_parser
.got_a_float
== 2) /* "fi..." */
5327 i
.suffix
= SHORT_MNEM_SUFFIX
;
5329 i
.suffix
= WORD_MNEM_SUFFIX
;
5332 else if (prev_token
.code
== T_DWORD
)
5334 if (intel_parser
.got_a_float
== 1) /* "f..." */
5335 i
.suffix
= SHORT_MNEM_SUFFIX
;
5337 i
.suffix
= LONG_MNEM_SUFFIX
;
5340 else if (prev_token
.code
== T_QWORD
)
5342 if (intel_parser
.got_a_float
== 1) /* "f..." */
5343 i
.suffix
= LONG_MNEM_SUFFIX
;
5345 i
.suffix
= QWORD_MNEM_SUFFIX
;
5348 else if (prev_token
.code
== T_XWORD
)
5349 i
.suffix
= LONG_DOUBLE_MNEM_SUFFIX
;
5353 as_bad (_("Unknown operand modifier `%s'\n"), prev_token
.str
);
5357 intel_match_token (T_PTR
);
5359 return (intel_e10 () && intel_e09_1 ());
5362 /* e09 : e10 e09' */
5363 else if (cur_token
.code
== ':')
5365 /* Mark as a memory operand only if it's not already known to be an
5366 offset expression. */
5367 if (intel_parser
.op_modifier
!= OFFSET_FLAT
)
5368 intel_parser
.is_mem
= 1;
5370 return (intel_match_token (':') && intel_e10 () && intel_e09_1 ());
5385 return (intel_e11 () && intel_e10_1 ());
5391 /* e10' [ expr ] e10' */
5392 if (cur_token
.code
== '[')
5394 intel_match_token ('[');
5396 /* Mark as a memory operand only if it's not already known to be an
5397 offset expression. If it's an offset expression, we need to keep
5399 if (intel_parser
.op_modifier
!= OFFSET_FLAT
)
5400 intel_parser
.is_mem
= 1;
5402 strcat (intel_parser
.disp
, "[");
5404 /* Add a '+' to the displacement string if necessary. */
5405 if (*intel_parser
.disp
!= '\0'
5406 && *(intel_parser
.disp
+ strlen (intel_parser
.disp
) - 1) != '+')
5407 strcat (intel_parser
.disp
, "+");
5409 if (intel_expr () && intel_match_token (']'))
5411 /* Preserve brackets when the operand is an offset expression. */
5412 if (intel_parser
.op_modifier
== OFFSET_FLAT
)
5413 strcat (intel_parser
.disp
, "]");
5415 return intel_e10_1 ();
5442 if (cur_token
.code
== '(')
5444 intel_match_token ('(');
5445 strcat (intel_parser
.disp
, "(");
5447 if (intel_expr () && intel_match_token (')'))
5449 strcat (intel_parser
.disp
, ")");
5457 else if (cur_token
.code
== '[')
5459 intel_match_token ('[');
5461 /* Mark as a memory operand only if it's not already known to be an
5462 offset expression. If it's an offset expression, we need to keep
5464 if (intel_parser
.op_modifier
!= OFFSET_FLAT
)
5465 intel_parser
.is_mem
= 1;
5467 strcat (intel_parser
.disp
, "[");
5469 /* Operands for jump/call inside brackets denote absolute addresses. */
5470 if (current_templates
->start
->opcode_modifier
& Jump
5471 || current_templates
->start
->opcode_modifier
& JumpDword
5472 || current_templates
->start
->opcode_modifier
& JumpByte
5473 || current_templates
->start
->opcode_modifier
& JumpInterSegment
)
5474 i
.types
[this_operand
] |= JumpAbsolute
;
5476 /* Add a '+' to the displacement string if necessary. */
5477 if (*intel_parser
.disp
!= '\0'
5478 && *(intel_parser
.disp
+ strlen (intel_parser
.disp
) - 1) != '+')
5479 strcat (intel_parser
.disp
, "+");
5481 if (intel_expr () && intel_match_token (']'))
5483 /* Preserve brackets when the operand is an offset expression. */
5484 if (intel_parser
.op_modifier
== OFFSET_FLAT
)
5485 strcat (intel_parser
.disp
, "]");
5498 else if (cur_token
.code
== T_BYTE
5499 || cur_token
.code
== T_WORD
5500 || cur_token
.code
== T_DWORD
5501 || cur_token
.code
== T_QWORD
5502 || cur_token
.code
== T_XWORD
)
5504 intel_match_token (cur_token
.code
);
5511 else if (cur_token
.code
== '$' || cur_token
.code
== '.')
5513 strcat (intel_parser
.disp
, cur_token
.str
);
5514 intel_match_token (cur_token
.code
);
5516 /* Mark as a memory operand only if it's not already known to be an
5517 offset expression. */
5518 if (intel_parser
.op_modifier
!= OFFSET_FLAT
)
5519 intel_parser
.is_mem
= 1;
5525 else if (cur_token
.code
== T_REG
)
5527 const reg_entry
*reg
= intel_parser
.reg
= cur_token
.reg
;
5529 intel_match_token (T_REG
);
5531 /* Check for segment change. */
5532 if (cur_token
.code
== ':')
5534 if (reg
->reg_type
& (SReg2
| SReg3
))
5536 switch (reg
->reg_num
)
5539 i
.seg
[i
.mem_operands
] = &es
;
5542 i
.seg
[i
.mem_operands
] = &cs
;
5545 i
.seg
[i
.mem_operands
] = &ss
;
5548 i
.seg
[i
.mem_operands
] = &ds
;
5551 i
.seg
[i
.mem_operands
] = &fs
;
5554 i
.seg
[i
.mem_operands
] = &gs
;
5560 as_bad (_("`%s' is not a valid segment register"), reg
->reg_name
);
5565 /* Not a segment register. Check for register scaling. */
5566 else if (cur_token
.code
== '*')
5568 if (!intel_parser
.is_mem
)
5570 as_bad (_("Register scaling only allowed in memory operands."));
5574 /* What follows must be a valid scale. */
5575 if (intel_match_token ('*')
5576 && strchr ("01248", *cur_token
.str
))
5579 i
.types
[this_operand
] |= BaseIndex
;
5581 /* Set the scale after setting the register (otherwise,
5582 i386_scale will complain) */
5583 i386_scale (cur_token
.str
);
5584 intel_match_token (T_CONST
);
5588 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
5594 /* No scaling. If this is a memory operand, the register is either a
5595 base register (first occurrence) or an index register (second
5597 else if (intel_parser
.is_mem
&& !(reg
->reg_type
& (SReg2
| SReg3
)))
5599 if (i
.base_reg
&& i
.index_reg
)
5601 as_bad (_("Too many register references in memory operand.\n"));
5605 if (i
.base_reg
== NULL
)
5610 i
.types
[this_operand
] |= BaseIndex
;
5613 /* Offset modifier. Add the register to the displacement string to be
5614 parsed as an immediate expression after we're done. */
5615 else if (intel_parser
.op_modifier
== OFFSET_FLAT
)
5616 strcat (intel_parser
.disp
, reg
->reg_name
);
5618 /* It's neither base nor index nor offset. */
5621 i
.types
[this_operand
] |= reg
->reg_type
& ~BaseIndex
;
5622 i
.op
[this_operand
].regs
= reg
;
5626 /* Since registers are not part of the displacement string (except
5627 when we're parsing offset operands), we may need to remove any
5628 preceding '+' from the displacement string. */
5629 if (*intel_parser
.disp
!= '\0'
5630 && intel_parser
.op_modifier
!= OFFSET_FLAT
)
5632 char *s
= intel_parser
.disp
;
5633 s
+= strlen (s
) - 1;
5642 else if (cur_token
.code
== T_ID
)
5644 /* Add the identifier to the displacement string. */
5645 strcat (intel_parser
.disp
, cur_token
.str
);
5646 intel_match_token (T_ID
);
5648 /* The identifier represents a memory reference only if it's not
5649 preceded by an offset modifier. */
5650 if (intel_parser
.op_modifier
!= OFFSET_FLAT
)
5651 intel_parser
.is_mem
= 1;
5657 else if (cur_token
.code
== T_CONST
5658 || cur_token
.code
== '-'
5659 || cur_token
.code
== '+')
5663 /* Allow constants that start with `+' or `-'. */
5664 if (cur_token
.code
== '-' || cur_token
.code
== '+')
5666 strcat (intel_parser
.disp
, cur_token
.str
);
5667 intel_match_token (cur_token
.code
);
5668 if (cur_token
.code
!= T_CONST
)
5670 as_bad (_("Syntax error. Expecting a constant. Got `%s'.\n"),
5676 save_str
= (char *) malloc (strlen (cur_token
.str
) + 1);
5677 if (save_str
== NULL
)
5679 strcpy (save_str
, cur_token
.str
);
5681 /* Get the next token to check for register scaling. */
5682 intel_match_token (cur_token
.code
);
5684 /* Check if this constant is a scaling factor for an index register. */
5685 if (cur_token
.code
== '*')
5687 if (intel_match_token ('*') && cur_token
.code
== T_REG
)
5689 if (!intel_parser
.is_mem
)
5691 as_bad (_("Register scaling only allowed in memory operands."));
5695 /* The constant is followed by `* reg', so it must be
5697 if (strchr ("01248", *save_str
))
5699 i
.index_reg
= cur_token
.reg
;
5700 i
.types
[this_operand
] |= BaseIndex
;
5702 /* Set the scale after setting the register (otherwise,
5703 i386_scale will complain) */
5704 i386_scale (save_str
);
5705 intel_match_token (T_REG
);
5707 /* Since registers are not part of the displacement
5708 string, we may need to remove any preceding '+' from
5709 the displacement string. */
5710 if (*intel_parser
.disp
!= '\0')
5712 char *s
= intel_parser
.disp
;
5713 s
+= strlen (s
) - 1;
5726 /* The constant was not used for register scaling. Since we have
5727 already consumed the token following `*' we now need to put it
5728 back in the stream. */
5730 intel_putback_token ();
5733 /* Add the constant to the displacement string. */
5734 strcat (intel_parser
.disp
, save_str
);
5740 as_bad (_("Unrecognized token '%s'"), cur_token
.str
);
5744 /* Match the given token against cur_token. If they match, read the next
5745 token from the operand string. */
5747 intel_match_token (code
)
5750 if (cur_token
.code
== code
)
5757 as_bad (_("Unexpected token `%s'\n"), cur_token
.str
);
5762 /* Read a new token from intel_parser.op_string and store it in cur_token. */
5767 const reg_entry
*reg
;
5768 struct intel_token new_token
;
5770 new_token
.code
= T_NIL
;
5771 new_token
.reg
= NULL
;
5772 new_token
.str
= NULL
;
5774 /* Free the memory allocated to the previous token and move
5775 cur_token to prev_token. */
5777 free (prev_token
.str
);
5779 prev_token
= cur_token
;
5781 /* Skip whitespace. */
5782 while (is_space_char (*intel_parser
.op_string
))
5783 intel_parser
.op_string
++;
5785 /* Return an empty token if we find nothing else on the line. */
5786 if (*intel_parser
.op_string
== '\0')
5788 cur_token
= new_token
;
5792 /* The new token cannot be larger than the remainder of the operand
5794 new_token
.str
= (char *) malloc (strlen (intel_parser
.op_string
) + 1);
5795 if (new_token
.str
== NULL
)
5797 new_token
.str
[0] = '\0';
5799 if (strchr ("0123456789", *intel_parser
.op_string
))
5801 char *p
= new_token
.str
;
5802 char *q
= intel_parser
.op_string
;
5803 new_token
.code
= T_CONST
;
5805 /* Allow any kind of identifier char to encompass floating point and
5806 hexadecimal numbers. */
5807 while (is_identifier_char (*q
))
5811 /* Recognize special symbol names [0-9][bf]. */
5812 if (strlen (intel_parser
.op_string
) == 2
5813 && (intel_parser
.op_string
[1] == 'b'
5814 || intel_parser
.op_string
[1] == 'f'))
5815 new_token
.code
= T_ID
;
5818 else if (strchr ("+-/*:[]()", *intel_parser
.op_string
))
5820 new_token
.code
= *intel_parser
.op_string
;
5821 new_token
.str
[0] = *intel_parser
.op_string
;
5822 new_token
.str
[1] = '\0';
5825 else if ((*intel_parser
.op_string
== REGISTER_PREFIX
|| allow_naked_reg
)
5826 && ((reg
= parse_register (intel_parser
.op_string
, &end_op
)) != NULL
))
5828 new_token
.code
= T_REG
;
5829 new_token
.reg
= reg
;
5831 if (*intel_parser
.op_string
== REGISTER_PREFIX
)
5833 new_token
.str
[0] = REGISTER_PREFIX
;
5834 new_token
.str
[1] = '\0';
5837 strcat (new_token
.str
, reg
->reg_name
);
5840 else if (is_identifier_char (*intel_parser
.op_string
))
5842 char *p
= new_token
.str
;
5843 char *q
= intel_parser
.op_string
;
5845 /* A '.' or '$' followed by an identifier char is an identifier.
5846 Otherwise, it's operator '.' followed by an expression. */
5847 if ((*q
== '.' || *q
== '$') && !is_identifier_char (*(q
+ 1)))
5849 new_token
.code
= *q
;
5850 new_token
.str
[0] = *q
;
5851 new_token
.str
[1] = '\0';
5855 while (is_identifier_char (*q
) || *q
== '@')
5859 if (strcasecmp (new_token
.str
, "BYTE") == 0)
5860 new_token
.code
= T_BYTE
;
5862 else if (strcasecmp (new_token
.str
, "WORD") == 0)
5863 new_token
.code
= T_WORD
;
5865 else if (strcasecmp (new_token
.str
, "DWORD") == 0)
5866 new_token
.code
= T_DWORD
;
5868 else if (strcasecmp (new_token
.str
, "QWORD") == 0)
5869 new_token
.code
= T_QWORD
;
5871 else if (strcasecmp (new_token
.str
, "XWORD") == 0)
5872 new_token
.code
= T_XWORD
;
5874 else if (strcasecmp (new_token
.str
, "PTR") == 0)
5875 new_token
.code
= T_PTR
;
5877 else if (strcasecmp (new_token
.str
, "SHORT") == 0)
5878 new_token
.code
= T_SHORT
;
5880 else if (strcasecmp (new_token
.str
, "OFFSET") == 0)
5882 new_token
.code
= T_OFFSET
;
5884 /* ??? This is not mentioned in the MASM grammar but gcc
5885 makes use of it with -mintel-syntax. OFFSET may be
5886 followed by FLAT: */
5887 if (strncasecmp (q
, " FLAT:", 6) == 0)
5888 strcat (new_token
.str
, " FLAT:");
5891 /* ??? This is not mentioned in the MASM grammar. */
5892 else if (strcasecmp (new_token
.str
, "FLAT") == 0)
5893 new_token
.code
= T_OFFSET
;
5896 new_token
.code
= T_ID
;
5901 as_bad (_("Unrecognized token `%s'\n"), intel_parser
.op_string
);
5903 intel_parser
.op_string
+= strlen (new_token
.str
);
5904 cur_token
= new_token
;
5907 /* Put cur_token back into the token stream and make cur_token point to
5910 intel_putback_token ()
5912 intel_parser
.op_string
-= strlen (cur_token
.str
);
5913 free (cur_token
.str
);
5914 cur_token
= prev_token
;
5916 /* Forget prev_token. */
5917 prev_token
.code
= T_NIL
;
5918 prev_token
.reg
= NULL
;
5919 prev_token
.str
= NULL
;