gas/
[external/binutils.git] / gas / config / tc-i386.c
1 /* i386.c -- Assemble code for the Intel 80386
2    Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004
4    Free Software Foundation, Inc.
5
6    This file is part of GAS, the GNU Assembler.
7
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)
11    any later version.
12
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.
17
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
21    02111-1307, USA.  */
22
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    VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
27    Bugs & suggestions are completely welcome.  This is free software.
28    Please help us make it better.  */
29
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "subsegs.h"
33 #include "dwarf2dbg.h"
34 #include "dw2gencfi.h"
35 #include "opcode/i386.h"
36 #include "elf/x86-64.h"
37
38 #ifndef REGISTER_WARNINGS
39 #define REGISTER_WARNINGS 1
40 #endif
41
42 #ifndef INFER_ADDR_PREFIX
43 #define INFER_ADDR_PREFIX 1
44 #endif
45
46 #ifndef SCALE1_WHEN_NO_INDEX
47 /* Specifying a scale factor besides 1 when there is no index is
48    futile.  eg. `mov (%ebx,2),%al' does exactly the same as
49    `mov (%ebx),%al'.  To slavishly follow what the programmer
50    specified, set SCALE1_WHEN_NO_INDEX to 0.  */
51 #define SCALE1_WHEN_NO_INDEX 1
52 #endif
53
54 #ifndef DEFAULT_ARCH
55 #define DEFAULT_ARCH "i386"
56 #endif
57
58 #ifndef INLINE
59 #if __GNUC__ >= 2
60 #define INLINE __inline__
61 #else
62 #define INLINE
63 #endif
64 #endif
65
66 static INLINE unsigned int mode_from_disp_size PARAMS ((unsigned int));
67 static INLINE int fits_in_signed_byte PARAMS ((offsetT));
68 static INLINE int fits_in_unsigned_byte PARAMS ((offsetT));
69 static INLINE int fits_in_unsigned_word PARAMS ((offsetT));
70 static INLINE int fits_in_signed_word PARAMS ((offsetT));
71 static INLINE int fits_in_unsigned_long PARAMS ((offsetT));
72 static INLINE int fits_in_signed_long PARAMS ((offsetT));
73 static int smallest_imm_type PARAMS ((offsetT));
74 static offsetT offset_in_range PARAMS ((offsetT, int));
75 static int add_prefix PARAMS ((unsigned int));
76 static void set_code_flag PARAMS ((int));
77 static void set_16bit_gcc_code_flag PARAMS ((int));
78 static void set_intel_syntax PARAMS ((int));
79 static void set_cpu_arch PARAMS ((int));
80 #ifdef TE_PE
81 static void pe_directive_secrel PARAMS ((int));
82 #endif
83 static char *output_invalid PARAMS ((int c));
84 static int i386_operand PARAMS ((char *operand_string));
85 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
86 static const reg_entry *parse_register PARAMS ((char *reg_string,
87                                                 char **end_op));
88 static char *parse_insn PARAMS ((char *, char *));
89 static char *parse_operands PARAMS ((char *, const char *));
90 static void swap_operands PARAMS ((void));
91 static void optimize_imm PARAMS ((void));
92 static void optimize_disp PARAMS ((void));
93 static int match_template PARAMS ((void));
94 static int check_string PARAMS ((void));
95 static int process_suffix PARAMS ((void));
96 static int check_byte_reg PARAMS ((void));
97 static int check_long_reg PARAMS ((void));
98 static int check_qword_reg PARAMS ((void));
99 static int check_word_reg PARAMS ((void));
100 static int finalize_imm PARAMS ((void));
101 static int process_operands PARAMS ((void));
102 static const seg_entry *build_modrm_byte PARAMS ((void));
103 static void output_insn PARAMS ((void));
104 static void output_branch PARAMS ((void));
105 static void output_jump PARAMS ((void));
106 static void output_interseg_jump PARAMS ((void));
107 static void output_imm PARAMS ((fragS *insn_start_frag,
108                                 offsetT insn_start_off));
109 static void output_disp PARAMS ((fragS *insn_start_frag,
110                                  offsetT insn_start_off));
111 #ifndef I386COFF
112 static void s_bss PARAMS ((int));
113 #endif
114
115 static const char *default_arch = DEFAULT_ARCH;
116
117 /* 'md_assemble ()' gathers together information and puts it into a
118    i386_insn.  */
119
120 union i386_op
121   {
122     expressionS *disps;
123     expressionS *imms;
124     const reg_entry *regs;
125   };
126
127 struct _i386_insn
128   {
129     /* TM holds the template for the insn were currently assembling.  */
130     template tm;
131
132     /* SUFFIX holds the instruction mnemonic suffix if given.
133        (e.g. 'l' for 'movl')  */
134     char suffix;
135
136     /* OPERANDS gives the number of given operands.  */
137     unsigned int operands;
138
139     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
140        of given register, displacement, memory operands and immediate
141        operands.  */
142     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
143
144     /* TYPES [i] is the type (see above #defines) which tells us how to
145        use OP[i] for the corresponding operand.  */
146     unsigned int types[MAX_OPERANDS];
147
148     /* Displacement expression, immediate expression, or register for each
149        operand.  */
150     union i386_op op[MAX_OPERANDS];
151
152     /* Flags for operands.  */
153     unsigned int flags[MAX_OPERANDS];
154 #define Operand_PCrel 1
155
156     /* Relocation type for operand */
157     enum bfd_reloc_code_real reloc[MAX_OPERANDS];
158
159     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
160        the base index byte below.  */
161     const reg_entry *base_reg;
162     const reg_entry *index_reg;
163     unsigned int log2_scale_factor;
164
165     /* SEG gives the seg_entries of this insn.  They are zero unless
166        explicit segment overrides are given.  */
167     const seg_entry *seg[2];
168
169     /* PREFIX holds all the given prefix opcodes (usually null).
170        PREFIXES is the number of prefix opcodes.  */
171     unsigned int prefixes;
172     unsigned char prefix[MAX_PREFIXES];
173
174     /* RM and SIB are the modrm byte and the sib byte where the
175        addressing modes of this insn are encoded.  */
176
177     modrm_byte rm;
178     rex_byte rex;
179     sib_byte sib;
180   };
181
182 typedef struct _i386_insn i386_insn;
183
184 /* List of chars besides those in app.c:symbol_chars that can start an
185    operand.  Used to prevent the scrubber eating vital white-space.  */
186 const char extra_symbol_chars[] = "*%-(["
187 #ifdef LEX_AT
188         "@"
189 #endif
190 #ifdef LEX_QM
191         "?"
192 #endif
193         ;
194
195 #if (defined (TE_I386AIX)                               \
196      || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
197          && !defined (TE_LINUX)                         \
198          && !defined (TE_NETWARE)                       \
199          && !defined (TE_FreeBSD)                       \
200          && !defined (TE_NetBSD)))
201 /* This array holds the chars that always start a comment.  If the
202    pre-processor is disabled, these aren't very useful.  */
203 const char comment_chars[] = "#/";
204 #define PREFIX_SEPARATOR '\\'
205
206 /* This array holds the chars that only start a comment at the beginning of
207    a line.  If the line seems to have the form '# 123 filename'
208    .line and .file directives will appear in the pre-processed output.
209    Note that input_file.c hand checks for '#' at the beginning of the
210    first line of the input file.  This is because the compiler outputs
211    #NO_APP at the beginning of its output.
212    Also note that comments started like this one will always work if
213    '/' isn't otherwise defined.  */
214 const char line_comment_chars[] = "#";
215
216 #else
217 /* Putting '/' here makes it impossible to use the divide operator.
218    However, we need it for compatibility with SVR4 systems.  */
219 const char comment_chars[] = "#";
220 #define PREFIX_SEPARATOR '/'
221
222 const char line_comment_chars[] = "/#";
223 #endif
224
225 const char line_separator_chars[] = ";";
226
227 /* Chars that can be used to separate mant from exp in floating point
228    nums.  */
229 const char EXP_CHARS[] = "eE";
230
231 /* Chars that mean this number is a floating point constant
232    As in 0f12.456
233    or    0d1.2345e12.  */
234 const char FLT_CHARS[] = "fFdDxX";
235
236 /* Tables for lexical analysis.  */
237 static char mnemonic_chars[256];
238 static char register_chars[256];
239 static char operand_chars[256];
240 static char identifier_chars[256];
241 static char digit_chars[256];
242
243 /* Lexical macros.  */
244 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
245 #define is_operand_char(x) (operand_chars[(unsigned char) x])
246 #define is_register_char(x) (register_chars[(unsigned char) x])
247 #define is_space_char(x) ((x) == ' ')
248 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
249 #define is_digit_char(x) (digit_chars[(unsigned char) x])
250
251 /* All non-digit non-letter characters that may occur in an operand.  */
252 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
253
254 /* md_assemble() always leaves the strings it's passed unaltered.  To
255    effect this we maintain a stack of saved characters that we've smashed
256    with '\0's (indicating end of strings for various sub-fields of the
257    assembler instruction).  */
258 static char save_stack[32];
259 static char *save_stack_p;
260 #define END_STRING_AND_SAVE(s) \
261         do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
262 #define RESTORE_END_STRING(s) \
263         do { *(s) = *--save_stack_p; } while (0)
264
265 /* The instruction we're assembling.  */
266 static i386_insn i;
267
268 /* Possible templates for current insn.  */
269 static const templates *current_templates;
270
271 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max.  */
272 static expressionS disp_expressions[2], im_expressions[2];
273
274 /* Current operand we are working on.  */
275 static int this_operand;
276
277 /* We support four different modes.  FLAG_CODE variable is used to distinguish
278    these.  */
279
280 enum flag_code {
281         CODE_32BIT,
282         CODE_16BIT,
283         CODE_64BIT };
284 #define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
285
286 static enum flag_code flag_code;
287 static int use_rela_relocations = 0;
288
289 /* The names used to print error messages.  */
290 static const char *flag_code_names[] =
291   {
292     "32",
293     "16",
294     "64"
295   };
296
297 /* 1 for intel syntax,
298    0 if att syntax.  */
299 static int intel_syntax = 0;
300
301 /* 1 if register prefix % not required.  */
302 static int allow_naked_reg = 0;
303
304 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
305    leave, push, and pop instructions so that gcc has the same stack
306    frame as in 32 bit mode.  */
307 static char stackop_size = '\0';
308
309 /* Non-zero to optimize code alignment.  */
310 int optimize_align_code = 1;
311
312 /* Non-zero to quieten some warnings.  */
313 static int quiet_warnings = 0;
314
315 /* CPU name.  */
316 static const char *cpu_arch_name = NULL;
317 static const char *cpu_sub_arch_name = NULL;
318
319 /* CPU feature flags.  */
320 static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
321
322 /* If set, conditional jumps are not automatically promoted to handle
323    larger than a byte offset.  */
324 static unsigned int no_cond_jump_promotion = 0;
325
326 /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
327 symbolS *GOT_symbol;
328
329 /* The dwarf2 return column, adjusted for 32 or 64 bit.  */
330 unsigned int x86_dwarf2_return_column;
331
332 /* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
333 int x86_cie_data_alignment;
334
335 /* Interface to relax_segment.
336    There are 3 major relax states for 386 jump insns because the
337    different types of jumps add different sizes to frags when we're
338    figuring out what sort of jump to choose to reach a given label.  */
339
340 /* Types.  */
341 #define UNCOND_JUMP 0
342 #define COND_JUMP 1
343 #define COND_JUMP86 2
344
345 /* Sizes.  */
346 #define CODE16  1
347 #define SMALL   0
348 #define SMALL16 (SMALL | CODE16)
349 #define BIG     2
350 #define BIG16   (BIG | CODE16)
351
352 #ifndef INLINE
353 #ifdef __GNUC__
354 #define INLINE __inline__
355 #else
356 #define INLINE
357 #endif
358 #endif
359
360 #define ENCODE_RELAX_STATE(type, size) \
361   ((relax_substateT) (((type) << 2) | (size)))
362 #define TYPE_FROM_RELAX_STATE(s) \
363   ((s) >> 2)
364 #define DISP_SIZE_FROM_RELAX_STATE(s) \
365     ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
366
367 /* This table is used by relax_frag to promote short jumps to long
368    ones where necessary.  SMALL (short) jumps may be promoted to BIG
369    (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
370    don't allow a short jump in a 32 bit code segment to be promoted to
371    a 16 bit offset jump because it's slower (requires data size
372    prefix), and doesn't work, unless the destination is in the bottom
373    64k of the code segment (The top 16 bits of eip are zeroed).  */
374
375 const relax_typeS md_relax_table[] =
376 {
377   /* The fields are:
378      1) most positive reach of this state,
379      2) most negative reach of this state,
380      3) how many bytes this mode will have in the variable part of the frag
381      4) which index into the table to try if we can't fit into this one.  */
382
383   /* UNCOND_JUMP states.  */
384   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
385   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
386   /* dword jmp adds 4 bytes to frag:
387      0 extra opcode bytes, 4 displacement bytes.  */
388   {0, 0, 4, 0},
389   /* word jmp adds 2 byte2 to frag:
390      0 extra opcode bytes, 2 displacement bytes.  */
391   {0, 0, 2, 0},
392
393   /* COND_JUMP states.  */
394   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
395   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
396   /* dword conditionals adds 5 bytes to frag:
397      1 extra opcode byte, 4 displacement bytes.  */
398   {0, 0, 5, 0},
399   /* word conditionals add 3 bytes to frag:
400      1 extra opcode byte, 2 displacement bytes.  */
401   {0, 0, 3, 0},
402
403   /* COND_JUMP86 states.  */
404   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
405   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
406   /* dword conditionals adds 5 bytes to frag:
407      1 extra opcode byte, 4 displacement bytes.  */
408   {0, 0, 5, 0},
409   /* word conditionals add 4 bytes to frag:
410      1 displacement byte and a 3 byte long branch insn.  */
411   {0, 0, 4, 0}
412 };
413
414 static const arch_entry cpu_arch[] = {
415   {"i8086",     Cpu086 },
416   {"i186",      Cpu086|Cpu186 },
417   {"i286",      Cpu086|Cpu186|Cpu286 },
418   {"i386",      Cpu086|Cpu186|Cpu286|Cpu386 },
419   {"i486",      Cpu086|Cpu186|Cpu286|Cpu386|Cpu486 },
420   {"i586",      Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
421   {"i686",      Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
422   {"pentium",   Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
423   {"pentiumpro",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
424   {"pentiumii", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX },
425   {"pentiumiii",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuMMX2|CpuSSE },
426   {"pentium4",  Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
427   {"prescott",  Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuPNI },
428   {"k6",        Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX },
429   {"k6_2",      Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow },
430   {"athlon",    Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
431   {"sledgehammer",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2 },
432   {".mmx",      CpuMMX },
433   {".sse",      CpuMMX|CpuMMX2|CpuSSE },
434   {".sse2",     CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
435   {".3dnow",    CpuMMX|Cpu3dnow },
436   {".3dnowa",   CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
437   {".padlock",  CpuPadLock },
438   {NULL, 0 }
439 };
440
441 const pseudo_typeS md_pseudo_table[] =
442 {
443 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
444   {"align", s_align_bytes, 0},
445 #else
446   {"align", s_align_ptwo, 0},
447 #endif
448   {"arch", set_cpu_arch, 0},
449 #ifndef I386COFF
450   {"bss", s_bss, 0},
451 #endif
452   {"ffloat", float_cons, 'f'},
453   {"dfloat", float_cons, 'd'},
454   {"tfloat", float_cons, 'x'},
455   {"value", cons, 2},
456   {"noopt", s_ignore, 0},
457   {"optim", s_ignore, 0},
458   {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
459   {"code16", set_code_flag, CODE_16BIT},
460   {"code32", set_code_flag, CODE_32BIT},
461   {"code64", set_code_flag, CODE_64BIT},
462   {"intel_syntax", set_intel_syntax, 1},
463   {"att_syntax", set_intel_syntax, 0},
464   {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
465   {"loc", dwarf2_directive_loc, 0},
466 #ifdef TE_PE
467   {"secrel32", pe_directive_secrel, 0},
468 #endif
469   {0, 0, 0}
470 };
471
472 /* For interface with expression ().  */
473 extern char *input_line_pointer;
474
475 /* Hash table for instruction mnemonic lookup.  */
476 static struct hash_control *op_hash;
477
478 /* Hash table for register lookup.  */
479 static struct hash_control *reg_hash;
480 \f
481 void
482 i386_align_code (fragP, count)
483      fragS *fragP;
484      int count;
485 {
486   /* Various efficient no-op patterns for aligning code labels.
487      Note: Don't try to assemble the instructions in the comments.
488      0L and 0w are not legal.  */
489   static const char f32_1[] =
490     {0x90};                                     /* nop                  */
491   static const char f32_2[] =
492     {0x89,0xf6};                                /* movl %esi,%esi       */
493   static const char f32_3[] =
494     {0x8d,0x76,0x00};                           /* leal 0(%esi),%esi    */
495   static const char f32_4[] =
496     {0x8d,0x74,0x26,0x00};                      /* leal 0(%esi,1),%esi  */
497   static const char f32_5[] =
498     {0x90,                                      /* nop                  */
499      0x8d,0x74,0x26,0x00};                      /* leal 0(%esi,1),%esi  */
500   static const char f32_6[] =
501     {0x8d,0xb6,0x00,0x00,0x00,0x00};            /* leal 0L(%esi),%esi   */
502   static const char f32_7[] =
503     {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};       /* leal 0L(%esi,1),%esi */
504   static const char f32_8[] =
505     {0x90,                                      /* nop                  */
506      0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};       /* leal 0L(%esi,1),%esi */
507   static const char f32_9[] =
508     {0x89,0xf6,                                 /* movl %esi,%esi       */
509      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
510   static const char f32_10[] =
511     {0x8d,0x76,0x00,                            /* leal 0(%esi),%esi    */
512      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
513   static const char f32_11[] =
514     {0x8d,0x74,0x26,0x00,                       /* leal 0(%esi,1),%esi  */
515      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
516   static const char f32_12[] =
517     {0x8d,0xb6,0x00,0x00,0x00,0x00,             /* leal 0L(%esi),%esi   */
518      0x8d,0xbf,0x00,0x00,0x00,0x00};            /* leal 0L(%edi),%edi   */
519   static const char f32_13[] =
520     {0x8d,0xb6,0x00,0x00,0x00,0x00,             /* leal 0L(%esi),%esi   */
521      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
522   static const char f32_14[] =
523     {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00,        /* leal 0L(%esi,1),%esi */
524      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
525   static const char f32_15[] =
526     {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90,        /* jmp .+15; lotsa nops */
527      0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
528   static const char f16_3[] =
529     {0x8d,0x74,0x00};                           /* lea 0(%esi),%esi     */
530   static const char f16_4[] =
531     {0x8d,0xb4,0x00,0x00};                      /* lea 0w(%si),%si      */
532   static const char f16_5[] =
533     {0x90,                                      /* nop                  */
534      0x8d,0xb4,0x00,0x00};                      /* lea 0w(%si),%si      */
535   static const char f16_6[] =
536     {0x89,0xf6,                                 /* mov %si,%si          */
537      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
538   static const char f16_7[] =
539     {0x8d,0x74,0x00,                            /* lea 0(%si),%si       */
540      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
541   static const char f16_8[] =
542     {0x8d,0xb4,0x00,0x00,                       /* lea 0w(%si),%si      */
543      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
544   static const char *const f32_patt[] = {
545     f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
546     f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
547   };
548   static const char *const f16_patt[] = {
549     f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
550     f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
551   };
552
553   if (count <= 0 || count > 15)
554     return;
555
556   /* The recommended way to pad 64bit code is to use NOPs preceded by
557      maximally four 0x66 prefixes.  Balance the size of nops.  */
558   if (flag_code == CODE_64BIT)
559     {
560       int i;
561       int nnops = (count + 3) / 4;
562       int len = count / nnops;
563       int remains = count - nnops * len;
564       int pos = 0;
565
566       for (i = 0; i < remains; i++)
567         {
568           memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len);
569           fragP->fr_literal[fragP->fr_fix + pos + len] = 0x90;
570           pos += len + 1;
571         }
572       for (; i < nnops; i++)
573         {
574           memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len - 1);
575           fragP->fr_literal[fragP->fr_fix + pos + len - 1] = 0x90;
576           pos += len;
577         }
578     }
579   else
580     if (flag_code == CODE_16BIT)
581       {
582         memcpy (fragP->fr_literal + fragP->fr_fix,
583                 f16_patt[count - 1], count);
584         if (count > 8)
585           /* Adjust jump offset.  */
586           fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
587       }
588     else
589       memcpy (fragP->fr_literal + fragP->fr_fix,
590               f32_patt[count - 1], count);
591   fragP->fr_var = count;
592 }
593
594 static INLINE unsigned int
595 mode_from_disp_size (t)
596      unsigned int t;
597 {
598   return (t & Disp8) ? 1 : (t & (Disp16 | Disp32 | Disp32S)) ? 2 : 0;
599 }
600
601 static INLINE int
602 fits_in_signed_byte (num)
603      offsetT num;
604 {
605   return (num >= -128) && (num <= 127);
606 }
607
608 static INLINE int
609 fits_in_unsigned_byte (num)
610      offsetT num;
611 {
612   return (num & 0xff) == num;
613 }
614
615 static INLINE int
616 fits_in_unsigned_word (num)
617      offsetT num;
618 {
619   return (num & 0xffff) == num;
620 }
621
622 static INLINE int
623 fits_in_signed_word (num)
624      offsetT num;
625 {
626   return (-32768 <= num) && (num <= 32767);
627 }
628 static INLINE int
629 fits_in_signed_long (num)
630      offsetT num ATTRIBUTE_UNUSED;
631 {
632 #ifndef BFD64
633   return 1;
634 #else
635   return (!(((offsetT) -1 << 31) & num)
636           || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
637 #endif
638 }                               /* fits_in_signed_long() */
639 static INLINE int
640 fits_in_unsigned_long (num)
641      offsetT num ATTRIBUTE_UNUSED;
642 {
643 #ifndef BFD64
644   return 1;
645 #else
646   return (num & (((offsetT) 2 << 31) - 1)) == num;
647 #endif
648 }                               /* fits_in_unsigned_long() */
649
650 static int
651 smallest_imm_type (num)
652      offsetT num;
653 {
654   if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
655     {
656       /* This code is disabled on the 486 because all the Imm1 forms
657          in the opcode table are slower on the i486.  They're the
658          versions with the implicitly specified single-position
659          displacement, which has another syntax if you really want to
660          use that form.  */
661       if (num == 1)
662         return Imm1 | Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64;
663     }
664   return (fits_in_signed_byte (num)
665           ? (Imm8S | Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
666           : fits_in_unsigned_byte (num)
667           ? (Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
668           : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
669           ? (Imm16 | Imm32 | Imm32S | Imm64)
670           : fits_in_signed_long (num)
671           ? (Imm32 | Imm32S | Imm64)
672           : fits_in_unsigned_long (num)
673           ? (Imm32 | Imm64)
674           : Imm64);
675 }
676
677 static offsetT
678 offset_in_range (val, size)
679      offsetT val;
680      int size;
681 {
682   addressT mask;
683
684   switch (size)
685     {
686     case 1: mask = ((addressT) 1 <<  8) - 1; break;
687     case 2: mask = ((addressT) 1 << 16) - 1; break;
688     case 4: mask = ((addressT) 2 << 31) - 1; break;
689 #ifdef BFD64
690     case 8: mask = ((addressT) 2 << 63) - 1; break;
691 #endif
692     default: abort ();
693     }
694
695   /* If BFD64, sign extend val.  */
696   if (!use_rela_relocations)
697     if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
698       val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
699
700   if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
701     {
702       char buf1[40], buf2[40];
703
704       sprint_value (buf1, val);
705       sprint_value (buf2, val & mask);
706       as_warn (_("%s shortened to %s"), buf1, buf2);
707     }
708   return val & mask;
709 }
710
711 /* Returns 0 if attempting to add a prefix where one from the same
712    class already exists, 1 if non rep/repne added, 2 if rep/repne
713    added.  */
714 static int
715 add_prefix (prefix)
716      unsigned int prefix;
717 {
718   int ret = 1;
719   int q;
720
721   if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
722       && flag_code == CODE_64BIT)
723     q = REX_PREFIX;
724   else
725     switch (prefix)
726       {
727       default:
728         abort ();
729
730       case CS_PREFIX_OPCODE:
731       case DS_PREFIX_OPCODE:
732       case ES_PREFIX_OPCODE:
733       case FS_PREFIX_OPCODE:
734       case GS_PREFIX_OPCODE:
735       case SS_PREFIX_OPCODE:
736         q = SEG_PREFIX;
737         break;
738
739       case REPNE_PREFIX_OPCODE:
740       case REPE_PREFIX_OPCODE:
741         ret = 2;
742         /* fall thru */
743       case LOCK_PREFIX_OPCODE:
744         q = LOCKREP_PREFIX;
745         break;
746
747       case FWAIT_OPCODE:
748         q = WAIT_PREFIX;
749         break;
750
751       case ADDR_PREFIX_OPCODE:
752         q = ADDR_PREFIX;
753         break;
754
755       case DATA_PREFIX_OPCODE:
756         q = DATA_PREFIX;
757         break;
758       }
759
760   if (i.prefix[q] != 0)
761     {
762       as_bad (_("same type of prefix used twice"));
763       return 0;
764     }
765
766   i.prefixes += 1;
767   i.prefix[q] = prefix;
768   return ret;
769 }
770
771 static void
772 set_code_flag (value)
773      int value;
774 {
775   flag_code = value;
776   cpu_arch_flags &= ~(Cpu64 | CpuNo64);
777   cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
778   if (value == CODE_64BIT && !(cpu_arch_flags & CpuSledgehammer))
779     {
780       as_bad (_("64bit mode not supported on this CPU."));
781     }
782   if (value == CODE_32BIT && !(cpu_arch_flags & Cpu386))
783     {
784       as_bad (_("32bit mode not supported on this CPU."));
785     }
786   stackop_size = '\0';
787 }
788
789 static void
790 set_16bit_gcc_code_flag (new_code_flag)
791      int new_code_flag;
792 {
793   flag_code = new_code_flag;
794   cpu_arch_flags &= ~(Cpu64 | CpuNo64);
795   cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
796   stackop_size = LONG_MNEM_SUFFIX;
797 }
798
799 static void
800 set_intel_syntax (syntax_flag)
801      int syntax_flag;
802 {
803   /* Find out if register prefixing is specified.  */
804   int ask_naked_reg = 0;
805
806   SKIP_WHITESPACE ();
807   if (!is_end_of_line[(unsigned char) *input_line_pointer])
808     {
809       char *string = input_line_pointer;
810       int e = get_symbol_end ();
811
812       if (strcmp (string, "prefix") == 0)
813         ask_naked_reg = 1;
814       else if (strcmp (string, "noprefix") == 0)
815         ask_naked_reg = -1;
816       else
817         as_bad (_("bad argument to syntax directive."));
818       *input_line_pointer = e;
819     }
820   demand_empty_rest_of_line ();
821
822   intel_syntax = syntax_flag;
823
824   if (ask_naked_reg == 0)
825     allow_naked_reg = (intel_syntax
826                        && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
827   else
828     allow_naked_reg = (ask_naked_reg < 0);
829
830   identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
831   identifier_chars['$'] = intel_syntax ? '$' : 0;
832 }
833
834 static void
835 set_cpu_arch (dummy)
836      int dummy ATTRIBUTE_UNUSED;
837 {
838   SKIP_WHITESPACE ();
839
840   if (!is_end_of_line[(unsigned char) *input_line_pointer])
841     {
842       char *string = input_line_pointer;
843       int e = get_symbol_end ();
844       int i;
845
846       for (i = 0; cpu_arch[i].name; i++)
847         {
848           if (strcmp (string, cpu_arch[i].name) == 0)
849             {
850               if (*string != '.')
851                 {
852                   cpu_arch_name = cpu_arch[i].name;
853                   cpu_sub_arch_name = NULL;
854                   cpu_arch_flags = (cpu_arch[i].flags
855                                     | (flag_code == CODE_64BIT ? Cpu64 : CpuNo64));
856                   break;
857                 }
858               if ((cpu_arch_flags | cpu_arch[i].flags) != cpu_arch_flags)
859                 {
860                   cpu_sub_arch_name = cpu_arch[i].name;
861                   cpu_arch_flags |= cpu_arch[i].flags;
862                 }
863               *input_line_pointer = e;
864               demand_empty_rest_of_line ();
865               return;
866             }
867         }
868       if (!cpu_arch[i].name)
869         as_bad (_("no such architecture: `%s'"), string);
870
871       *input_line_pointer = e;
872     }
873   else
874     as_bad (_("missing cpu architecture"));
875
876   no_cond_jump_promotion = 0;
877   if (*input_line_pointer == ','
878       && !is_end_of_line[(unsigned char) input_line_pointer[1]])
879     {
880       char *string = ++input_line_pointer;
881       int e = get_symbol_end ();
882
883       if (strcmp (string, "nojumps") == 0)
884         no_cond_jump_promotion = 1;
885       else if (strcmp (string, "jumps") == 0)
886         ;
887       else
888         as_bad (_("no such architecture modifier: `%s'"), string);
889
890       *input_line_pointer = e;
891     }
892
893   demand_empty_rest_of_line ();
894 }
895
896 unsigned long
897 i386_mach ()
898 {
899   if (!strcmp (default_arch, "x86_64"))
900     return bfd_mach_x86_64;
901   else if (!strcmp (default_arch, "i386"))
902     return bfd_mach_i386_i386;
903   else
904     as_fatal (_("Unknown architecture"));
905 }
906 \f
907 void
908 md_begin ()
909 {
910   const char *hash_err;
911
912   /* Initialize op_hash hash table.  */
913   op_hash = hash_new ();
914
915   {
916     const template *optab;
917     templates *core_optab;
918
919     /* Setup for loop.  */
920     optab = i386_optab;
921     core_optab = (templates *) xmalloc (sizeof (templates));
922     core_optab->start = optab;
923
924     while (1)
925       {
926         ++optab;
927         if (optab->name == NULL
928             || strcmp (optab->name, (optab - 1)->name) != 0)
929           {
930             /* different name --> ship out current template list;
931                add to hash table; & begin anew.  */
932             core_optab->end = optab;
933             hash_err = hash_insert (op_hash,
934                                     (optab - 1)->name,
935                                     (PTR) core_optab);
936             if (hash_err)
937               {
938                 as_fatal (_("Internal Error:  Can't hash %s: %s"),
939                           (optab - 1)->name,
940                           hash_err);
941               }
942             if (optab->name == NULL)
943               break;
944             core_optab = (templates *) xmalloc (sizeof (templates));
945             core_optab->start = optab;
946           }
947       }
948   }
949
950   /* Initialize reg_hash hash table.  */
951   reg_hash = hash_new ();
952   {
953     const reg_entry *regtab;
954
955     for (regtab = i386_regtab;
956          regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
957          regtab++)
958       {
959         hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
960         if (hash_err)
961           as_fatal (_("Internal Error:  Can't hash %s: %s"),
962                     regtab->reg_name,
963                     hash_err);
964       }
965   }
966
967   /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
968   {
969     int c;
970     char *p;
971
972     for (c = 0; c < 256; c++)
973       {
974         if (ISDIGIT (c))
975           {
976             digit_chars[c] = c;
977             mnemonic_chars[c] = c;
978             register_chars[c] = c;
979             operand_chars[c] = c;
980           }
981         else if (ISLOWER (c))
982           {
983             mnemonic_chars[c] = c;
984             register_chars[c] = c;
985             operand_chars[c] = c;
986           }
987         else if (ISUPPER (c))
988           {
989             mnemonic_chars[c] = TOLOWER (c);
990             register_chars[c] = mnemonic_chars[c];
991             operand_chars[c] = c;
992           }
993
994         if (ISALPHA (c) || ISDIGIT (c))
995           identifier_chars[c] = c;
996         else if (c >= 128)
997           {
998             identifier_chars[c] = c;
999             operand_chars[c] = c;
1000           }
1001       }
1002
1003 #ifdef LEX_AT
1004     identifier_chars['@'] = '@';
1005 #endif
1006 #ifdef LEX_QM
1007     identifier_chars['?'] = '?';
1008     operand_chars['?'] = '?';
1009 #endif
1010     digit_chars['-'] = '-';
1011     identifier_chars['_'] = '_';
1012     identifier_chars['.'] = '.';
1013
1014     for (p = operand_special_chars; *p != '\0'; p++)
1015       operand_chars[(unsigned char) *p] = *p;
1016   }
1017
1018 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1019   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1020     {
1021       record_alignment (text_section, 2);
1022       record_alignment (data_section, 2);
1023       record_alignment (bss_section, 2);
1024     }
1025 #endif
1026
1027   if (flag_code == CODE_64BIT)
1028     {
1029       x86_dwarf2_return_column = 16;
1030       x86_cie_data_alignment = -8;
1031     }
1032   else
1033     {
1034       x86_dwarf2_return_column = 8;
1035       x86_cie_data_alignment = -4;
1036     }
1037 }
1038
1039 void
1040 i386_print_statistics (file)
1041      FILE *file;
1042 {
1043   hash_print_statistics (file, "i386 opcode", op_hash);
1044   hash_print_statistics (file, "i386 register", reg_hash);
1045 }
1046 \f
1047 #ifdef DEBUG386
1048
1049 /* Debugging routines for md_assemble.  */
1050 static void pi PARAMS ((char *, i386_insn *));
1051 static void pte PARAMS ((template *));
1052 static void pt PARAMS ((unsigned int));
1053 static void pe PARAMS ((expressionS *));
1054 static void ps PARAMS ((symbolS *));
1055
1056 static void
1057 pi (line, x)
1058      char *line;
1059      i386_insn *x;
1060 {
1061   unsigned int i;
1062
1063   fprintf (stdout, "%s: template ", line);
1064   pte (&x->tm);
1065   fprintf (stdout, "  address: base %s  index %s  scale %x\n",
1066            x->base_reg ? x->base_reg->reg_name : "none",
1067            x->index_reg ? x->index_reg->reg_name : "none",
1068            x->log2_scale_factor);
1069   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
1070            x->rm.mode, x->rm.reg, x->rm.regmem);
1071   fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
1072            x->sib.base, x->sib.index, x->sib.scale);
1073   fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
1074            (x->rex & REX_MODE64) != 0,
1075            (x->rex & REX_EXTX) != 0,
1076            (x->rex & REX_EXTY) != 0,
1077            (x->rex & REX_EXTZ) != 0);
1078   for (i = 0; i < x->operands; i++)
1079     {
1080       fprintf (stdout, "    #%d:  ", i + 1);
1081       pt (x->types[i]);
1082       fprintf (stdout, "\n");
1083       if (x->types[i]
1084           & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
1085         fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
1086       if (x->types[i] & Imm)
1087         pe (x->op[i].imms);
1088       if (x->types[i] & Disp)
1089         pe (x->op[i].disps);
1090     }
1091 }
1092
1093 static void
1094 pte (t)
1095      template *t;
1096 {
1097   unsigned int i;
1098   fprintf (stdout, " %d operands ", t->operands);
1099   fprintf (stdout, "opcode %x ", t->base_opcode);
1100   if (t->extension_opcode != None)
1101     fprintf (stdout, "ext %x ", t->extension_opcode);
1102   if (t->opcode_modifier & D)
1103     fprintf (stdout, "D");
1104   if (t->opcode_modifier & W)
1105     fprintf (stdout, "W");
1106   fprintf (stdout, "\n");
1107   for (i = 0; i < t->operands; i++)
1108     {
1109       fprintf (stdout, "    #%d type ", i + 1);
1110       pt (t->operand_types[i]);
1111       fprintf (stdout, "\n");
1112     }
1113 }
1114
1115 static void
1116 pe (e)
1117      expressionS *e;
1118 {
1119   fprintf (stdout, "    operation     %d\n", e->X_op);
1120   fprintf (stdout, "    add_number    %ld (%lx)\n",
1121            (long) e->X_add_number, (long) e->X_add_number);
1122   if (e->X_add_symbol)
1123     {
1124       fprintf (stdout, "    add_symbol    ");
1125       ps (e->X_add_symbol);
1126       fprintf (stdout, "\n");
1127     }
1128   if (e->X_op_symbol)
1129     {
1130       fprintf (stdout, "    op_symbol    ");
1131       ps (e->X_op_symbol);
1132       fprintf (stdout, "\n");
1133     }
1134 }
1135
1136 static void
1137 ps (s)
1138      symbolS *s;
1139 {
1140   fprintf (stdout, "%s type %s%s",
1141            S_GET_NAME (s),
1142            S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
1143            segment_name (S_GET_SEGMENT (s)));
1144 }
1145
1146 struct type_name
1147   {
1148     unsigned int mask;
1149     char *tname;
1150   }
1151
1152 static const type_names[] =
1153 {
1154   { Reg8, "r8" },
1155   { Reg16, "r16" },
1156   { Reg32, "r32" },
1157   { Reg64, "r64" },
1158   { Imm8, "i8" },
1159   { Imm8S, "i8s" },
1160   { Imm16, "i16" },
1161   { Imm32, "i32" },
1162   { Imm32S, "i32s" },
1163   { Imm64, "i64" },
1164   { Imm1, "i1" },
1165   { BaseIndex, "BaseIndex" },
1166   { Disp8, "d8" },
1167   { Disp16, "d16" },
1168   { Disp32, "d32" },
1169   { Disp32S, "d32s" },
1170   { Disp64, "d64" },
1171   { InOutPortReg, "InOutPortReg" },
1172   { ShiftCount, "ShiftCount" },
1173   { Control, "control reg" },
1174   { Test, "test reg" },
1175   { Debug, "debug reg" },
1176   { FloatReg, "FReg" },
1177   { FloatAcc, "FAcc" },
1178   { SReg2, "SReg2" },
1179   { SReg3, "SReg3" },
1180   { Acc, "Acc" },
1181   { JumpAbsolute, "Jump Absolute" },
1182   { RegMMX, "rMMX" },
1183   { RegXMM, "rXMM" },
1184   { EsSeg, "es" },
1185   { 0, "" }
1186 };
1187
1188 static void
1189 pt (t)
1190      unsigned int t;
1191 {
1192   const struct type_name *ty;
1193
1194   for (ty = type_names; ty->mask; ty++)
1195     if (t & ty->mask)
1196       fprintf (stdout, "%s, ", ty->tname);
1197   fflush (stdout);
1198 }
1199
1200 #endif /* DEBUG386 */
1201 \f
1202 static bfd_reloc_code_real_type reloc
1203   PARAMS ((int, int, int, bfd_reloc_code_real_type));
1204
1205 static bfd_reloc_code_real_type
1206 reloc (size, pcrel, sign, other)
1207      int size;
1208      int pcrel;
1209      int sign;
1210      bfd_reloc_code_real_type other;
1211 {
1212   if (other != NO_RELOC)
1213     return other;
1214
1215   if (pcrel)
1216     {
1217       if (!sign)
1218         as_bad (_("There are no unsigned pc-relative relocations"));
1219       switch (size)
1220         {
1221         case 1: return BFD_RELOC_8_PCREL;
1222         case 2: return BFD_RELOC_16_PCREL;
1223         case 4: return BFD_RELOC_32_PCREL;
1224         }
1225       as_bad (_("can not do %d byte pc-relative relocation"), size);
1226     }
1227   else
1228     {
1229       if (sign)
1230         switch (size)
1231           {
1232           case 4: return BFD_RELOC_X86_64_32S;
1233           }
1234       else
1235         switch (size)
1236           {
1237           case 1: return BFD_RELOC_8;
1238           case 2: return BFD_RELOC_16;
1239           case 4: return BFD_RELOC_32;
1240           case 8: return BFD_RELOC_64;
1241           }
1242       as_bad (_("can not do %s %d byte relocation"),
1243               sign ? "signed" : "unsigned", size);
1244     }
1245
1246   abort ();
1247   return BFD_RELOC_NONE;
1248 }
1249
1250 /* Here we decide which fixups can be adjusted to make them relative to
1251    the beginning of the section instead of the symbol.  Basically we need
1252    to make sure that the dynamic relocations are done correctly, so in
1253    some cases we force the original symbol to be used.  */
1254
1255 int
1256 tc_i386_fix_adjustable (fixP)
1257      fixS *fixP ATTRIBUTE_UNUSED;
1258 {
1259 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1260   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
1261     return 1;
1262
1263   /* Don't adjust pc-relative references to merge sections in 64-bit
1264      mode.  */
1265   if (use_rela_relocations
1266       && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
1267       && fixP->fx_pcrel)
1268     return 0;
1269
1270   /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
1271      and changed later by validate_fix.  */
1272   if (GOT_symbol && fixP->fx_subsy == GOT_symbol
1273       && fixP->fx_r_type == BFD_RELOC_32_PCREL)
1274     return 0;
1275
1276   /* adjust_reloc_syms doesn't know about the GOT.  */
1277   if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1278       || fixP->fx_r_type == BFD_RELOC_386_PLT32
1279       || fixP->fx_r_type == BFD_RELOC_386_GOT32
1280       || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
1281       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
1282       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
1283       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
1284       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
1285       || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
1286       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
1287       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
1288       || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
1289       || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
1290       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
1291       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
1292       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
1293       || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
1294       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
1295       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
1296       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1297       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1298     return 0;
1299 #endif
1300   return 1;
1301 }
1302
1303 static int intel_float_operand PARAMS ((const char *mnemonic));
1304
1305 static int
1306 intel_float_operand (mnemonic)
1307      const char *mnemonic;
1308 {
1309   /* Note that the value returned is meaningful only for opcodes with (memory)
1310      operands, hence the code here is free to improperly handle opcodes that
1311      have no operands (for better performance and smaller code). */
1312
1313   if (mnemonic[0] != 'f')
1314     return 0; /* non-math */
1315
1316   switch (mnemonic[1])
1317     {
1318     /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
1319        the fs segment override prefix not currently handled because no
1320        call path can make opcodes without operands get here */
1321     case 'i':
1322       return 2 /* integer op */;
1323     case 'l':
1324       if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
1325         return 3; /* fldcw/fldenv */
1326       break;
1327     case 'n':
1328       if (mnemonic[2] != 'o' /* fnop */)
1329         return 3; /* non-waiting control op */
1330       break;
1331     case 'r':
1332       if (mnemonic[2] == 's')
1333         return 3; /* frstor/frstpm */
1334       break;
1335     case 's':
1336       if (mnemonic[2] == 'a')
1337         return 3; /* fsave */
1338       if (mnemonic[2] == 't')
1339         {
1340           switch (mnemonic[3])
1341             {
1342             case 'c': /* fstcw */
1343             case 'd': /* fstdw */
1344             case 'e': /* fstenv */
1345             case 's': /* fsts[gw] */
1346               return 3;
1347             }
1348         }
1349       break;
1350     case 'x':
1351       if (mnemonic[2] == 'r' || mnemonic[2] == 's')
1352         return 0; /* fxsave/fxrstor are not really math ops */
1353       break;
1354     }
1355
1356   return 1;
1357 }
1358
1359 /* This is the guts of the machine-dependent assembler.  LINE points to a
1360    machine dependent instruction.  This function is supposed to emit
1361    the frags/bytes it assembles to.  */
1362
1363 void
1364 md_assemble (line)
1365      char *line;
1366 {
1367   int j;
1368   char mnemonic[MAX_MNEM_SIZE];
1369
1370   /* Initialize globals.  */
1371   memset (&i, '\0', sizeof (i));
1372   for (j = 0; j < MAX_OPERANDS; j++)
1373     i.reloc[j] = NO_RELOC;
1374   memset (disp_expressions, '\0', sizeof (disp_expressions));
1375   memset (im_expressions, '\0', sizeof (im_expressions));
1376   save_stack_p = save_stack;
1377
1378   /* First parse an instruction mnemonic & call i386_operand for the operands.
1379      We assume that the scrubber has arranged it so that line[0] is the valid
1380      start of a (possibly prefixed) mnemonic.  */
1381
1382   line = parse_insn (line, mnemonic);
1383   if (line == NULL)
1384     return;
1385
1386   line = parse_operands (line, mnemonic);
1387   if (line == NULL)
1388     return;
1389
1390   /* Now we've parsed the mnemonic into a set of templates, and have the
1391      operands at hand.  */
1392
1393   /* All intel opcodes have reversed operands except for "bound" and
1394      "enter".  We also don't reverse intersegment "jmp" and "call"
1395      instructions with 2 immediate operands so that the immediate segment
1396      precedes the offset, as it does when in AT&T mode.  "enter" and the
1397      intersegment "jmp" and "call" instructions are the only ones that
1398      have two immediate operands.  */
1399   if (intel_syntax && i.operands > 1
1400       && (strcmp (mnemonic, "bound") != 0)
1401       && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1402     swap_operands ();
1403
1404   if (i.imm_operands)
1405     optimize_imm ();
1406
1407   if (i.disp_operands)
1408     optimize_disp ();
1409
1410   /* Next, we find a template that matches the given insn,
1411      making sure the overlap of the given operands types is consistent
1412      with the template operand types.  */
1413
1414   if (!match_template ())
1415     return;
1416
1417   if (intel_syntax)
1418     {
1419       /* Undo SYSV386_COMPAT brokenness when in Intel mode.  See i386.h  */
1420       if (SYSV386_COMPAT
1421           && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1422         i.tm.base_opcode ^= FloatR;
1423
1424       /* Zap movzx and movsx suffix.  The suffix may have been set from
1425          "word ptr" or "byte ptr" on the source operand, but we'll use
1426          the suffix later to choose the destination register.  */
1427       if ((i.tm.base_opcode & ~9) == 0x0fb6)
1428         {
1429           if (i.reg_operands < 2
1430               && !i.suffix
1431               && (~i.tm.opcode_modifier
1432                   & (No_bSuf
1433                      | No_wSuf
1434                      | No_lSuf
1435                      | No_sSuf
1436                      | No_xSuf
1437                      | No_qSuf)))
1438             as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
1439
1440           i.suffix = 0;
1441         }
1442     }
1443
1444   if (i.tm.opcode_modifier & FWait)
1445     if (!add_prefix (FWAIT_OPCODE))
1446       return;
1447
1448   /* Check string instruction segment overrides.  */
1449   if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1450     {
1451       if (!check_string ())
1452         return;
1453     }
1454
1455   if (!process_suffix ())
1456     return;
1457
1458   /* Make still unresolved immediate matches conform to size of immediate
1459      given in i.suffix.  */
1460   if (!finalize_imm ())
1461     return;
1462
1463   if (i.types[0] & Imm1)
1464     i.imm_operands = 0; /* kludge for shift insns.  */
1465   if (i.types[0] & ImplicitRegister)
1466     i.reg_operands--;
1467   if (i.types[1] & ImplicitRegister)
1468     i.reg_operands--;
1469   if (i.types[2] & ImplicitRegister)
1470     i.reg_operands--;
1471
1472   if (i.tm.opcode_modifier & ImmExt)
1473     {
1474       expressionS *exp;
1475
1476       if ((i.tm.cpu_flags & CpuPNI) && i.operands > 0)
1477         {
1478           /* These Intel Prescott New Instructions have the fixed
1479              operands with an opcode suffix which is coded in the same
1480              place as an 8-bit immediate field would be. Here we check
1481              those operands and remove them afterwards.  */
1482           unsigned int x;
1483
1484           for (x = 0; x < i.operands; x++)
1485             if (i.op[x].regs->reg_num != x)
1486               as_bad (_("can't use register '%%%s' as operand %d in '%s'."),
1487                         i.op[x].regs->reg_name, x + 1, i.tm.name);
1488           i.operands = 0;
1489         }
1490
1491       /* These AMD 3DNow! and Intel Katmai New Instructions have an
1492          opcode suffix which is coded in the same place as an 8-bit
1493          immediate field would be.  Here we fake an 8-bit immediate
1494          operand from the opcode suffix stored in tm.extension_opcode.  */
1495
1496       assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1497
1498       exp = &im_expressions[i.imm_operands++];
1499       i.op[i.operands].imms = exp;
1500       i.types[i.operands++] = Imm8;
1501       exp->X_op = O_constant;
1502       exp->X_add_number = i.tm.extension_opcode;
1503       i.tm.extension_opcode = None;
1504     }
1505
1506   /* For insns with operands there are more diddles to do to the opcode.  */
1507   if (i.operands)
1508     {
1509       if (!process_operands ())
1510         return;
1511     }
1512   else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1513     {
1514       /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
1515       as_warn (_("translating to `%sp'"), i.tm.name);
1516     }
1517
1518   /* Handle conversion of 'int $3' --> special int3 insn.  */
1519   if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
1520     {
1521       i.tm.base_opcode = INT3_OPCODE;
1522       i.imm_operands = 0;
1523     }
1524
1525   if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
1526       && i.op[0].disps->X_op == O_constant)
1527     {
1528       /* Convert "jmp constant" (and "call constant") to a jump (call) to
1529          the absolute address given by the constant.  Since ix86 jumps and
1530          calls are pc relative, we need to generate a reloc.  */
1531       i.op[0].disps->X_add_symbol = &abs_symbol;
1532       i.op[0].disps->X_op = O_symbol;
1533     }
1534
1535   if ((i.tm.opcode_modifier & Rex64) != 0)
1536     i.rex |= REX_MODE64;
1537
1538   /* For 8 bit registers we need an empty rex prefix.  Also if the
1539      instruction already has a prefix, we need to convert old
1540      registers to new ones.  */
1541
1542   if (((i.types[0] & Reg8) != 0
1543        && (i.op[0].regs->reg_flags & RegRex64) != 0)
1544       || ((i.types[1] & Reg8) != 0
1545           && (i.op[1].regs->reg_flags & RegRex64) != 0)
1546       || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
1547           && i.rex != 0))
1548     {
1549       int x;
1550
1551       i.rex |= REX_OPCODE;
1552       for (x = 0; x < 2; x++)
1553         {
1554           /* Look for 8 bit operand that uses old registers.  */
1555           if ((i.types[x] & Reg8) != 0
1556               && (i.op[x].regs->reg_flags & RegRex64) == 0)
1557             {
1558               /* In case it is "hi" register, give up.  */
1559               if (i.op[x].regs->reg_num > 3)
1560                 as_bad (_("can't encode register '%%%s' in an instruction requiring REX prefix."),
1561                         i.op[x].regs->reg_name);
1562
1563               /* Otherwise it is equivalent to the extended register.
1564                  Since the encoding doesn't change this is merely
1565                  cosmetic cleanup for debug output.  */
1566
1567               i.op[x].regs = i.op[x].regs + 8;
1568             }
1569         }
1570     }
1571
1572   if (i.rex != 0)
1573     add_prefix (REX_OPCODE | i.rex);
1574
1575   /* We are ready to output the insn.  */
1576   output_insn ();
1577 }
1578
1579 static char *
1580 parse_insn (line, mnemonic)
1581      char *line;
1582      char *mnemonic;
1583 {
1584   char *l = line;
1585   char *token_start = l;
1586   char *mnem_p;
1587   int supported;
1588   const template *t;
1589
1590   /* Non-zero if we found a prefix only acceptable with string insns.  */
1591   const char *expecting_string_instruction = NULL;
1592
1593   while (1)
1594     {
1595       mnem_p = mnemonic;
1596       while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1597         {
1598           mnem_p++;
1599           if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
1600             {
1601               as_bad (_("no such instruction: `%s'"), token_start);
1602               return NULL;
1603             }
1604           l++;
1605         }
1606       if (!is_space_char (*l)
1607           && *l != END_OF_INSN
1608           && *l != PREFIX_SEPARATOR
1609           && *l != ',')
1610         {
1611           as_bad (_("invalid character %s in mnemonic"),
1612                   output_invalid (*l));
1613           return NULL;
1614         }
1615       if (token_start == l)
1616         {
1617           if (*l == PREFIX_SEPARATOR)
1618             as_bad (_("expecting prefix; got nothing"));
1619           else
1620             as_bad (_("expecting mnemonic; got nothing"));
1621           return NULL;
1622         }
1623
1624       /* Look up instruction (or prefix) via hash table.  */
1625       current_templates = hash_find (op_hash, mnemonic);
1626
1627       if (*l != END_OF_INSN
1628           && (!is_space_char (*l) || l[1] != END_OF_INSN)
1629           && current_templates
1630           && (current_templates->start->opcode_modifier & IsPrefix))
1631         {
1632           /* If we are in 16-bit mode, do not allow addr16 or data16.
1633              Similarly, in 32-bit mode, do not allow addr32 or data32.  */
1634           if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1635               && flag_code != CODE_64BIT
1636               && (((current_templates->start->opcode_modifier & Size32) != 0)
1637                   ^ (flag_code == CODE_16BIT)))
1638             {
1639               as_bad (_("redundant %s prefix"),
1640                       current_templates->start->name);
1641               return NULL;
1642             }
1643           /* Add prefix, checking for repeated prefixes.  */
1644           switch (add_prefix (current_templates->start->base_opcode))
1645             {
1646             case 0:
1647               return NULL;
1648             case 2:
1649               expecting_string_instruction = current_templates->start->name;
1650               break;
1651             }
1652           /* Skip past PREFIX_SEPARATOR and reset token_start.  */
1653           token_start = ++l;
1654         }
1655       else
1656         break;
1657     }
1658
1659   if (!current_templates)
1660     {
1661       /* See if we can get a match by trimming off a suffix.  */
1662       switch (mnem_p[-1])
1663         {
1664         case WORD_MNEM_SUFFIX:
1665           if (intel_syntax && (intel_float_operand (mnemonic) & 2))
1666             i.suffix = SHORT_MNEM_SUFFIX;
1667           else
1668         case BYTE_MNEM_SUFFIX:
1669         case QWORD_MNEM_SUFFIX:
1670           i.suffix = mnem_p[-1];
1671           mnem_p[-1] = '\0';
1672           current_templates = hash_find (op_hash, mnemonic);
1673           break;
1674         case SHORT_MNEM_SUFFIX:
1675         case LONG_MNEM_SUFFIX:
1676           if (!intel_syntax)
1677             {
1678               i.suffix = mnem_p[-1];
1679               mnem_p[-1] = '\0';
1680               current_templates = hash_find (op_hash, mnemonic);
1681             }
1682           break;
1683
1684           /* Intel Syntax.  */
1685         case 'd':
1686           if (intel_syntax)
1687             {
1688               if (intel_float_operand (mnemonic) == 1)
1689                 i.suffix = SHORT_MNEM_SUFFIX;
1690               else
1691                 i.suffix = LONG_MNEM_SUFFIX;
1692               mnem_p[-1] = '\0';
1693               current_templates = hash_find (op_hash, mnemonic);
1694             }
1695           break;
1696         }
1697       if (!current_templates)
1698         {
1699           as_bad (_("no such instruction: `%s'"), token_start);
1700           return NULL;
1701         }
1702     }
1703
1704   if (current_templates->start->opcode_modifier & (Jump | JumpByte))
1705     {
1706       /* Check for a branch hint.  We allow ",pt" and ",pn" for
1707          predict taken and predict not taken respectively.
1708          I'm not sure that branch hints actually do anything on loop
1709          and jcxz insns (JumpByte) for current Pentium4 chips.  They
1710          may work in the future and it doesn't hurt to accept them
1711          now.  */
1712       if (l[0] == ',' && l[1] == 'p')
1713         {
1714           if (l[2] == 't')
1715             {
1716               if (!add_prefix (DS_PREFIX_OPCODE))
1717                 return NULL;
1718               l += 3;
1719             }
1720           else if (l[2] == 'n')
1721             {
1722               if (!add_prefix (CS_PREFIX_OPCODE))
1723                 return NULL;
1724               l += 3;
1725             }
1726         }
1727     }
1728   /* Any other comma loses.  */
1729   if (*l == ',')
1730     {
1731       as_bad (_("invalid character %s in mnemonic"),
1732               output_invalid (*l));
1733       return NULL;
1734     }
1735
1736   /* Check if instruction is supported on specified architecture.  */
1737   supported = 0;
1738   for (t = current_templates->start; t < current_templates->end; ++t)
1739     {
1740       if (!((t->cpu_flags & ~(Cpu64 | CpuNo64))
1741             & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64))))
1742           supported |= 1;
1743       if (!(t->cpu_flags & (flag_code == CODE_64BIT ? CpuNo64 : Cpu64)))
1744           supported |= 2;
1745     }
1746   if (!(supported & 2))
1747     {
1748       as_bad (flag_code == CODE_64BIT
1749               ? _("`%s' is not supported in 64-bit mode")
1750               : _("`%s' is only supported in 64-bit mode"),
1751               current_templates->start->name);
1752       return NULL;
1753     }
1754   if (!(supported & 1))
1755     {
1756       as_warn (_("`%s' is not supported on `%s%s'"),
1757                current_templates->start->name,
1758                cpu_arch_name,
1759                cpu_sub_arch_name ? cpu_sub_arch_name : "");
1760     }
1761   else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
1762     {
1763       as_warn (_("use .code16 to ensure correct addressing mode"));
1764     }
1765
1766   /* Check for rep/repne without a string instruction.  */
1767   if (expecting_string_instruction
1768       && !(current_templates->start->opcode_modifier & IsString))
1769     {
1770       as_bad (_("expecting string instruction after `%s'"),
1771               expecting_string_instruction);
1772       return NULL;
1773     }
1774
1775   return l;
1776 }
1777
1778 static char *
1779 parse_operands (l, mnemonic)
1780      char *l;
1781      const char *mnemonic;
1782 {
1783   char *token_start;
1784
1785   /* 1 if operand is pending after ','.  */
1786   unsigned int expecting_operand = 0;
1787
1788   /* Non-zero if operand parens not balanced.  */
1789   unsigned int paren_not_balanced;
1790
1791   while (*l != END_OF_INSN)
1792     {
1793       /* Skip optional white space before operand.  */
1794       if (is_space_char (*l))
1795         ++l;
1796       if (!is_operand_char (*l) && *l != END_OF_INSN)
1797         {
1798           as_bad (_("invalid character %s before operand %d"),
1799                   output_invalid (*l),
1800                   i.operands + 1);
1801           return NULL;
1802         }
1803       token_start = l;  /* after white space */
1804       paren_not_balanced = 0;
1805       while (paren_not_balanced || *l != ',')
1806         {
1807           if (*l == END_OF_INSN)
1808             {
1809               if (paren_not_balanced)
1810                 {
1811                   if (!intel_syntax)
1812                     as_bad (_("unbalanced parenthesis in operand %d."),
1813                             i.operands + 1);
1814                   else
1815                     as_bad (_("unbalanced brackets in operand %d."),
1816                             i.operands + 1);
1817                   return NULL;
1818                 }
1819               else
1820                 break;  /* we are done */
1821             }
1822           else if (!is_operand_char (*l) && !is_space_char (*l))
1823             {
1824               as_bad (_("invalid character %s in operand %d"),
1825                       output_invalid (*l),
1826                       i.operands + 1);
1827               return NULL;
1828             }
1829           if (!intel_syntax)
1830             {
1831               if (*l == '(')
1832                 ++paren_not_balanced;
1833               if (*l == ')')
1834                 --paren_not_balanced;
1835             }
1836           else
1837             {
1838               if (*l == '[')
1839                 ++paren_not_balanced;
1840               if (*l == ']')
1841                 --paren_not_balanced;
1842             }
1843           l++;
1844         }
1845       if (l != token_start)
1846         {                       /* Yes, we've read in another operand.  */
1847           unsigned int operand_ok;
1848           this_operand = i.operands++;
1849           if (i.operands > MAX_OPERANDS)
1850             {
1851               as_bad (_("spurious operands; (%d operands/instruction max)"),
1852                       MAX_OPERANDS);
1853               return NULL;
1854             }
1855           /* Now parse operand adding info to 'i' as we go along.  */
1856           END_STRING_AND_SAVE (l);
1857
1858           if (intel_syntax)
1859             operand_ok =
1860               i386_intel_operand (token_start,
1861                                   intel_float_operand (mnemonic));
1862           else
1863             operand_ok = i386_operand (token_start);
1864
1865           RESTORE_END_STRING (l);
1866           if (!operand_ok)
1867             return NULL;
1868         }
1869       else
1870         {
1871           if (expecting_operand)
1872             {
1873             expecting_operand_after_comma:
1874               as_bad (_("expecting operand after ','; got nothing"));
1875               return NULL;
1876             }
1877           if (*l == ',')
1878             {
1879               as_bad (_("expecting operand before ','; got nothing"));
1880               return NULL;
1881             }
1882         }
1883
1884       /* Now *l must be either ',' or END_OF_INSN.  */
1885       if (*l == ',')
1886         {
1887           if (*++l == END_OF_INSN)
1888             {
1889               /* Just skip it, if it's \n complain.  */
1890               goto expecting_operand_after_comma;
1891             }
1892           expecting_operand = 1;
1893         }
1894     }
1895   return l;
1896 }
1897
1898 static void
1899 swap_operands ()
1900 {
1901   union i386_op temp_op;
1902   unsigned int temp_type;
1903   enum bfd_reloc_code_real temp_reloc;
1904   int xchg1 = 0;
1905   int xchg2 = 0;
1906
1907   if (i.operands == 2)
1908     {
1909       xchg1 = 0;
1910       xchg2 = 1;
1911     }
1912   else if (i.operands == 3)
1913     {
1914       xchg1 = 0;
1915       xchg2 = 2;
1916     }
1917   temp_type = i.types[xchg2];
1918   i.types[xchg2] = i.types[xchg1];
1919   i.types[xchg1] = temp_type;
1920   temp_op = i.op[xchg2];
1921   i.op[xchg2] = i.op[xchg1];
1922   i.op[xchg1] = temp_op;
1923   temp_reloc = i.reloc[xchg2];
1924   i.reloc[xchg2] = i.reloc[xchg1];
1925   i.reloc[xchg1] = temp_reloc;
1926
1927   if (i.mem_operands == 2)
1928     {
1929       const seg_entry *temp_seg;
1930       temp_seg = i.seg[0];
1931       i.seg[0] = i.seg[1];
1932       i.seg[1] = temp_seg;
1933     }
1934 }
1935
1936 /* Try to ensure constant immediates are represented in the smallest
1937    opcode possible.  */
1938 static void
1939 optimize_imm ()
1940 {
1941   char guess_suffix = 0;
1942   int op;
1943
1944   if (i.suffix)
1945     guess_suffix = i.suffix;
1946   else if (i.reg_operands)
1947     {
1948       /* Figure out a suffix from the last register operand specified.
1949          We can't do this properly yet, ie. excluding InOutPortReg,
1950          but the following works for instructions with immediates.
1951          In any case, we can't set i.suffix yet.  */
1952       for (op = i.operands; --op >= 0;)
1953         if (i.types[op] & Reg)
1954           {
1955             if (i.types[op] & Reg8)
1956               guess_suffix = BYTE_MNEM_SUFFIX;
1957             else if (i.types[op] & Reg16)
1958               guess_suffix = WORD_MNEM_SUFFIX;
1959             else if (i.types[op] & Reg32)
1960               guess_suffix = LONG_MNEM_SUFFIX;
1961             else if (i.types[op] & Reg64)
1962               guess_suffix = QWORD_MNEM_SUFFIX;
1963             break;
1964           }
1965     }
1966   else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
1967     guess_suffix = WORD_MNEM_SUFFIX;
1968
1969   for (op = i.operands; --op >= 0;)
1970     if (i.types[op] & Imm)
1971       {
1972         switch (i.op[op].imms->X_op)
1973           {
1974           case O_constant:
1975             /* If a suffix is given, this operand may be shortened.  */
1976             switch (guess_suffix)
1977               {
1978               case LONG_MNEM_SUFFIX:
1979                 i.types[op] |= Imm32 | Imm64;
1980                 break;
1981               case WORD_MNEM_SUFFIX:
1982                 i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
1983                 break;
1984               case BYTE_MNEM_SUFFIX:
1985                 i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
1986                 break;
1987               }
1988
1989             /* If this operand is at most 16 bits, convert it
1990                to a signed 16 bit number before trying to see
1991                whether it will fit in an even smaller size.
1992                This allows a 16-bit operand such as $0xffe0 to
1993                be recognised as within Imm8S range.  */
1994             if ((i.types[op] & Imm16)
1995                 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
1996               {
1997                 i.op[op].imms->X_add_number =
1998                   (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
1999               }
2000             if ((i.types[op] & Imm32)
2001                 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
2002                     == 0))
2003               {
2004                 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
2005                                                 ^ ((offsetT) 1 << 31))
2006                                                - ((offsetT) 1 << 31));
2007               }
2008             i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
2009
2010             /* We must avoid matching of Imm32 templates when 64bit
2011                only immediate is available.  */
2012             if (guess_suffix == QWORD_MNEM_SUFFIX)
2013               i.types[op] &= ~Imm32;
2014             break;
2015
2016           case O_absent:
2017           case O_register:
2018             abort ();
2019
2020             /* Symbols and expressions.  */
2021           default:
2022             /* Convert symbolic operand to proper sizes for matching.  */
2023             switch (guess_suffix)
2024               {
2025               case QWORD_MNEM_SUFFIX:
2026                 i.types[op] = Imm64 | Imm32S;
2027                 break;
2028               case LONG_MNEM_SUFFIX:
2029                 i.types[op] = Imm32;
2030                 break;
2031               case WORD_MNEM_SUFFIX:
2032                 i.types[op] = Imm16;
2033                 break;
2034               case BYTE_MNEM_SUFFIX:
2035                 i.types[op] = Imm8 | Imm8S;
2036                 break;
2037               }
2038             break;
2039           }
2040       }
2041 }
2042
2043 /* Try to use the smallest displacement type too.  */
2044 static void
2045 optimize_disp ()
2046 {
2047   int op;
2048
2049   for (op = i.operands; --op >= 0;)
2050     if ((i.types[op] & Disp) && i.op[op].disps->X_op == O_constant)
2051       {
2052         offsetT disp = i.op[op].disps->X_add_number;
2053
2054         if (i.types[op] & Disp16)
2055           {
2056             /* We know this operand is at most 16 bits, so
2057                convert to a signed 16 bit number before trying
2058                to see whether it will fit in an even smaller
2059                size.  */
2060
2061             disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
2062           }
2063         else if (i.types[op] & Disp32)
2064           {
2065             /* We know this operand is at most 32 bits, so convert to a
2066                signed 32 bit number before trying to see whether it will
2067                fit in an even smaller size.  */
2068             disp &= (((offsetT) 2 << 31) - 1);
2069             disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
2070           }
2071         if (flag_code == CODE_64BIT)
2072           {
2073             if (fits_in_signed_long (disp))
2074               i.types[op] |= Disp32S;
2075             if (fits_in_unsigned_long (disp))
2076               i.types[op] |= Disp32;
2077           }
2078         if ((i.types[op] & (Disp32 | Disp32S | Disp16))
2079             && fits_in_signed_byte (disp))
2080           i.types[op] |= Disp8;
2081       }
2082 }
2083
2084 static int
2085 match_template ()
2086 {
2087   /* Points to template once we've found it.  */
2088   const template *t;
2089   unsigned int overlap0, overlap1, overlap2;
2090   unsigned int found_reverse_match;
2091   int suffix_check;
2092
2093 #define MATCH(overlap, given, template)                         \
2094   ((overlap & ~JumpAbsolute)                                    \
2095    && (((given) & (BaseIndex | JumpAbsolute))                   \
2096        == ((overlap) & (BaseIndex | JumpAbsolute))))
2097
2098   /* If given types r0 and r1 are registers they must be of the same type
2099      unless the expected operand type register overlap is null.
2100      Note that Acc in a template matches every size of reg.  */
2101 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1)       \
2102   (((g0) & Reg) == 0 || ((g1) & Reg) == 0                       \
2103    || ((g0) & Reg) == ((g1) & Reg)                              \
2104    || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
2105
2106   overlap0 = 0;
2107   overlap1 = 0;
2108   overlap2 = 0;
2109   found_reverse_match = 0;
2110   suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
2111                   ? No_bSuf
2112                   : (i.suffix == WORD_MNEM_SUFFIX
2113                      ? No_wSuf
2114                      : (i.suffix == SHORT_MNEM_SUFFIX
2115                         ? No_sSuf
2116                         : (i.suffix == LONG_MNEM_SUFFIX
2117                            ? No_lSuf
2118                            : (i.suffix == QWORD_MNEM_SUFFIX
2119                               ? No_qSuf
2120                               : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
2121                                  ? No_xSuf : 0))))));
2122
2123   t = current_templates->start;
2124   if (i.suffix == QWORD_MNEM_SUFFIX
2125       && flag_code != CODE_64BIT
2126       && (intel_syntax
2127           ? !(t->opcode_modifier & IgnoreSize)
2128             && !intel_float_operand (t->name)
2129           : intel_float_operand (t->name) != 2)
2130       && (!(t->operand_types[0] & (RegMMX | RegXMM))
2131           || !(t->operand_types[t->operands > 1] & (RegMMX | RegXMM)))
2132       && (t->base_opcode != 0x0fc7
2133           || t->extension_opcode != 1 /* cmpxchg8b */))
2134     t = current_templates->end;
2135   for (; t < current_templates->end; t++)
2136     {
2137       /* Must have right number of operands.  */
2138       if (i.operands != t->operands)
2139         continue;
2140
2141       /* Check the suffix, except for some instructions in intel mode.  */
2142       if ((t->opcode_modifier & suffix_check)
2143           && !(intel_syntax
2144                && (t->opcode_modifier & IgnoreSize)))
2145         continue;
2146
2147       /* Do not verify operands when there are none.  */
2148       else if (!t->operands)
2149         {
2150           if (t->cpu_flags & ~cpu_arch_flags)
2151             continue;
2152           /* We've found a match; break out of loop.  */
2153           break;
2154         }
2155
2156       overlap0 = i.types[0] & t->operand_types[0];
2157       switch (t->operands)
2158         {
2159         case 1:
2160           if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
2161             continue;
2162           break;
2163         case 2:
2164         case 3:
2165           overlap1 = i.types[1] & t->operand_types[1];
2166           if (!MATCH (overlap0, i.types[0], t->operand_types[0])
2167               || !MATCH (overlap1, i.types[1], t->operand_types[1])
2168               || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2169                                              t->operand_types[0],
2170                                              overlap1, i.types[1],
2171                                              t->operand_types[1]))
2172             {
2173               /* Check if other direction is valid ...  */
2174               if ((t->opcode_modifier & (D | FloatD)) == 0)
2175                 continue;
2176
2177               /* Try reversing direction of operands.  */
2178               overlap0 = i.types[0] & t->operand_types[1];
2179               overlap1 = i.types[1] & t->operand_types[0];
2180               if (!MATCH (overlap0, i.types[0], t->operand_types[1])
2181                   || !MATCH (overlap1, i.types[1], t->operand_types[0])
2182                   || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2183                                                  t->operand_types[1],
2184                                                  overlap1, i.types[1],
2185                                                  t->operand_types[0]))
2186                 {
2187                   /* Does not match either direction.  */
2188                   continue;
2189                 }
2190               /* found_reverse_match holds which of D or FloatDR
2191                  we've found.  */
2192               found_reverse_match = t->opcode_modifier & (D | FloatDR);
2193             }
2194           /* Found a forward 2 operand match here.  */
2195           else if (t->operands == 3)
2196             {
2197               /* Here we make use of the fact that there are no
2198                  reverse match 3 operand instructions, and all 3
2199                  operand instructions only need to be checked for
2200                  register consistency between operands 2 and 3.  */
2201               overlap2 = i.types[2] & t->operand_types[2];
2202               if (!MATCH (overlap2, i.types[2], t->operand_types[2])
2203                   || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
2204                                                  t->operand_types[1],
2205                                                  overlap2, i.types[2],
2206                                                  t->operand_types[2]))
2207
2208                 continue;
2209             }
2210           /* Found either forward/reverse 2 or 3 operand match here:
2211              slip through to break.  */
2212         }
2213       if (t->cpu_flags & ~cpu_arch_flags)
2214         {
2215           found_reverse_match = 0;
2216           continue;
2217         }
2218       /* We've found a match; break out of loop.  */
2219       break;
2220     }
2221
2222   if (t == current_templates->end)
2223     {
2224       /* We found no match.  */
2225       as_bad (_("suffix or operands invalid for `%s'"),
2226               current_templates->start->name);
2227       return 0;
2228     }
2229
2230   if (!quiet_warnings)
2231     {
2232       if (!intel_syntax
2233           && ((i.types[0] & JumpAbsolute)
2234               != (t->operand_types[0] & JumpAbsolute)))
2235         {
2236           as_warn (_("indirect %s without `*'"), t->name);
2237         }
2238
2239       if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
2240           == (IsPrefix | IgnoreSize))
2241         {
2242           /* Warn them that a data or address size prefix doesn't
2243              affect assembly of the next line of code.  */
2244           as_warn (_("stand-alone `%s' prefix"), t->name);
2245         }
2246     }
2247
2248   /* Copy the template we found.  */
2249   i.tm = *t;
2250   if (found_reverse_match)
2251     {
2252       /* If we found a reverse match we must alter the opcode
2253          direction bit.  found_reverse_match holds bits to change
2254          (different for int & float insns).  */
2255
2256       i.tm.base_opcode ^= found_reverse_match;
2257
2258       i.tm.operand_types[0] = t->operand_types[1];
2259       i.tm.operand_types[1] = t->operand_types[0];
2260     }
2261
2262   return 1;
2263 }
2264
2265 static int
2266 check_string ()
2267 {
2268   int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
2269   if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
2270     {
2271       if (i.seg[0] != NULL && i.seg[0] != &es)
2272         {
2273           as_bad (_("`%s' operand %d must use `%%es' segment"),
2274                   i.tm.name,
2275                   mem_op + 1);
2276           return 0;
2277         }
2278       /* There's only ever one segment override allowed per instruction.
2279          This instruction possibly has a legal segment override on the
2280          second operand, so copy the segment to where non-string
2281          instructions store it, allowing common code.  */
2282       i.seg[0] = i.seg[1];
2283     }
2284   else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
2285     {
2286       if (i.seg[1] != NULL && i.seg[1] != &es)
2287         {
2288           as_bad (_("`%s' operand %d must use `%%es' segment"),
2289                   i.tm.name,
2290                   mem_op + 2);
2291           return 0;
2292         }
2293     }
2294   return 1;
2295 }
2296
2297 static int
2298 process_suffix (void)
2299 {
2300   /* If matched instruction specifies an explicit instruction mnemonic
2301      suffix, use it.  */
2302   if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
2303     {
2304       if (i.tm.opcode_modifier & Size16)
2305         i.suffix = WORD_MNEM_SUFFIX;
2306       else if (i.tm.opcode_modifier & Size64)
2307         i.suffix = QWORD_MNEM_SUFFIX;
2308       else
2309         i.suffix = LONG_MNEM_SUFFIX;
2310     }
2311   else if (i.reg_operands)
2312     {
2313       /* If there's no instruction mnemonic suffix we try to invent one
2314          based on register operands.  */
2315       if (!i.suffix)
2316         {
2317           /* We take i.suffix from the last register operand specified,
2318              Destination register type is more significant than source
2319              register type.  */
2320           int op;
2321
2322           for (op = i.operands; --op >= 0;)
2323             if ((i.types[op] & Reg)
2324                 && !(i.tm.operand_types[op] & InOutPortReg))
2325               {
2326                 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
2327                             (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
2328                             (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
2329                             LONG_MNEM_SUFFIX);
2330                 break;
2331               }
2332         }
2333       else if (i.suffix == BYTE_MNEM_SUFFIX)
2334         {
2335           if (!check_byte_reg ())
2336             return 0;
2337         }
2338       else if (i.suffix == LONG_MNEM_SUFFIX)
2339         {
2340           if (!check_long_reg ())
2341             return 0;
2342         }
2343       else if (i.suffix == QWORD_MNEM_SUFFIX)
2344         {
2345           if (!check_qword_reg ())
2346             return 0;
2347         }
2348       else if (i.suffix == WORD_MNEM_SUFFIX)
2349         {
2350           if (!check_word_reg ())
2351             return 0;
2352         }
2353       else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
2354         /* Do nothing if the instruction is going to ignore the prefix.  */
2355         ;
2356       else
2357         abort ();
2358     }
2359   else if ((i.tm.opcode_modifier & DefaultSize)
2360            && !i.suffix
2361            /* exclude fldenv/frstor/fsave/fstenv */
2362            && (i.tm.opcode_modifier & No_sSuf))
2363     {
2364       i.suffix = stackop_size;
2365     }
2366   else if (intel_syntax
2367            && !i.suffix
2368            && ((i.tm.operand_types[0] & JumpAbsolute)
2369             || (i.tm.opcode_modifier & (JumpByte|JumpInterSegment))
2370             || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
2371                 && i.tm.extension_opcode <= 3)))
2372     {
2373       switch (flag_code)
2374         {
2375         case CODE_64BIT:
2376           if (!(i.tm.opcode_modifier & No_qSuf))
2377             {
2378               i.suffix = QWORD_MNEM_SUFFIX;
2379               break;
2380             }
2381         case CODE_32BIT:
2382           if (!(i.tm.opcode_modifier & No_lSuf))
2383             i.suffix = LONG_MNEM_SUFFIX;
2384           break;
2385         case CODE_16BIT:
2386           if (!(i.tm.opcode_modifier & No_wSuf))
2387             i.suffix = WORD_MNEM_SUFFIX;
2388           break;
2389         }
2390     }
2391
2392   if (!i.suffix)
2393     {
2394       if (!intel_syntax)
2395         {
2396           if (i.tm.opcode_modifier & W)
2397             {
2398               as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
2399               return 0;
2400             }
2401         }
2402       else
2403         {
2404           unsigned int suffixes = ~i.tm.opcode_modifier
2405                                   & (No_bSuf
2406                                      | No_wSuf
2407                                      | No_lSuf
2408                                      | No_sSuf
2409                                      | No_xSuf
2410                                      | No_qSuf);
2411
2412           if ((i.tm.opcode_modifier & W)
2413               || ((suffixes & (suffixes - 1))
2414                   && !(i.tm.opcode_modifier & (DefaultSize | IgnoreSize))))
2415             {
2416               as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
2417               return 0;
2418             }
2419         }
2420     }
2421
2422   /* Change the opcode based on the operand size given by i.suffix;
2423      We don't need to change things for byte insns.  */
2424
2425   if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
2426     {
2427       /* It's not a byte, select word/dword operation.  */
2428       if (i.tm.opcode_modifier & W)
2429         {
2430           if (i.tm.opcode_modifier & ShortForm)
2431             i.tm.base_opcode |= 8;
2432           else
2433             i.tm.base_opcode |= 1;
2434         }
2435
2436       /* Now select between word & dword operations via the operand
2437          size prefix, except for instructions that will ignore this
2438          prefix anyway.  */
2439       if (i.suffix != QWORD_MNEM_SUFFIX
2440           && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
2441           && !(i.tm.opcode_modifier & (IgnoreSize | FloatMF))
2442           && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
2443               || (flag_code == CODE_64BIT
2444                   && (i.tm.opcode_modifier & JumpByte))))
2445         {
2446           unsigned int prefix = DATA_PREFIX_OPCODE;
2447
2448           if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
2449             prefix = ADDR_PREFIX_OPCODE;
2450
2451           if (!add_prefix (prefix))
2452             return 0;
2453         }
2454
2455       /* Set mode64 for an operand.  */
2456       if (i.suffix == QWORD_MNEM_SUFFIX
2457           && flag_code == CODE_64BIT
2458           && (i.tm.opcode_modifier & NoRex64) == 0)
2459         i.rex |= REX_MODE64;
2460
2461       /* Size floating point instruction.  */
2462       if (i.suffix == LONG_MNEM_SUFFIX)
2463         if (i.tm.opcode_modifier & FloatMF)
2464           i.tm.base_opcode ^= 4;
2465     }
2466
2467   return 1;
2468 }
2469
2470 static int
2471 check_byte_reg (void)
2472 {
2473   int op;
2474
2475   for (op = i.operands; --op >= 0;)
2476     {
2477       /* If this is an eight bit register, it's OK.  If it's the 16 or
2478          32 bit version of an eight bit register, we will just use the
2479          low portion, and that's OK too.  */
2480       if (i.types[op] & Reg8)
2481         continue;
2482
2483       /* movzx and movsx should not generate this warning.  */
2484       if (intel_syntax
2485           && (i.tm.base_opcode == 0xfb7
2486               || i.tm.base_opcode == 0xfb6
2487               || i.tm.base_opcode == 0x63
2488               || i.tm.base_opcode == 0xfbe
2489               || i.tm.base_opcode == 0xfbf))
2490         continue;
2491
2492       if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4)
2493         {
2494           /* Prohibit these changes in the 64bit mode, since the
2495              lowering is more complicated.  */
2496           if (flag_code == CODE_64BIT
2497               && (i.tm.operand_types[op] & InOutPortReg) == 0)
2498             {
2499               as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2500                       i.op[op].regs->reg_name,
2501                       i.suffix);
2502               return 0;
2503             }
2504 #if REGISTER_WARNINGS
2505           if (!quiet_warnings
2506               && (i.tm.operand_types[op] & InOutPortReg) == 0)
2507             as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2508                      (i.op[op].regs + (i.types[op] & Reg16
2509                                        ? REGNAM_AL - REGNAM_AX
2510                                        : REGNAM_AL - REGNAM_EAX))->reg_name,
2511                      i.op[op].regs->reg_name,
2512                      i.suffix);
2513 #endif
2514           continue;
2515         }
2516       /* Any other register is bad.  */
2517       if (i.types[op] & (Reg | RegMMX | RegXMM
2518                          | SReg2 | SReg3
2519                          | Control | Debug | Test
2520                          | FloatReg | FloatAcc))
2521         {
2522           as_bad (_("`%%%s' not allowed with `%s%c'"),
2523                   i.op[op].regs->reg_name,
2524                   i.tm.name,
2525                   i.suffix);
2526           return 0;
2527         }
2528     }
2529   return 1;
2530 }
2531
2532 static int
2533 check_long_reg ()
2534 {
2535   int op;
2536
2537   for (op = i.operands; --op >= 0;)
2538     /* Reject eight bit registers, except where the template requires
2539        them. (eg. movzb)  */
2540     if ((i.types[op] & Reg8) != 0
2541         && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2542       {
2543         as_bad (_("`%%%s' not allowed with `%s%c'"),
2544                 i.op[op].regs->reg_name,
2545                 i.tm.name,
2546                 i.suffix);
2547         return 0;
2548       }
2549   /* Warn if the e prefix on a general reg is missing.  */
2550     else if ((!quiet_warnings || flag_code == CODE_64BIT)
2551              && (i.types[op] & Reg16) != 0
2552              && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2553       {
2554         /* Prohibit these changes in the 64bit mode, since the
2555            lowering is more complicated.  */
2556         if (flag_code == CODE_64BIT)
2557           {
2558             as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2559                     i.op[op].regs->reg_name,
2560                     i.suffix);
2561             return 0;
2562           }
2563 #if REGISTER_WARNINGS
2564         else
2565           as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2566                    (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
2567                    i.op[op].regs->reg_name,
2568                    i.suffix);
2569 #endif
2570       }
2571   /* Warn if the r prefix on a general reg is missing.  */
2572     else if ((i.types[op] & Reg64) != 0
2573              && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2574       {
2575         as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2576                 i.op[op].regs->reg_name,
2577                 i.suffix);
2578         return 0;
2579       }
2580   return 1;
2581 }
2582
2583 static int
2584 check_qword_reg ()
2585 {
2586   int op;
2587
2588   for (op = i.operands; --op >= 0; )
2589     /* Reject eight bit registers, except where the template requires
2590        them. (eg. movzb)  */
2591     if ((i.types[op] & Reg8) != 0
2592         && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2593       {
2594         as_bad (_("`%%%s' not allowed with `%s%c'"),
2595                 i.op[op].regs->reg_name,
2596                 i.tm.name,
2597                 i.suffix);
2598         return 0;
2599       }
2600   /* Warn if the e prefix on a general reg is missing.  */
2601     else if (((i.types[op] & Reg16) != 0
2602               || (i.types[op] & Reg32) != 0)
2603              && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2604       {
2605         /* Prohibit these changes in the 64bit mode, since the
2606            lowering is more complicated.  */
2607         as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2608                 i.op[op].regs->reg_name,
2609                 i.suffix);
2610         return 0;
2611       }
2612   return 1;
2613 }
2614
2615 static int
2616 check_word_reg ()
2617 {
2618   int op;
2619   for (op = i.operands; --op >= 0;)
2620     /* Reject eight bit registers, except where the template requires
2621        them. (eg. movzb)  */
2622     if ((i.types[op] & Reg8) != 0
2623         && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2624       {
2625         as_bad (_("`%%%s' not allowed with `%s%c'"),
2626                 i.op[op].regs->reg_name,
2627                 i.tm.name,
2628                 i.suffix);
2629         return 0;
2630       }
2631   /* Warn if the e prefix on a general reg is present.  */
2632     else if ((!quiet_warnings || flag_code == CODE_64BIT)
2633              && (i.types[op] & Reg32) != 0
2634              && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
2635       {
2636         /* Prohibit these changes in the 64bit mode, since the
2637            lowering is more complicated.  */
2638         if (flag_code == CODE_64BIT)
2639           {
2640             as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2641                     i.op[op].regs->reg_name,
2642                     i.suffix);
2643             return 0;
2644           }
2645         else
2646 #if REGISTER_WARNINGS
2647           as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2648                    (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
2649                    i.op[op].regs->reg_name,
2650                    i.suffix);
2651 #endif
2652       }
2653   return 1;
2654 }
2655
2656 static int
2657 finalize_imm ()
2658 {
2659   unsigned int overlap0, overlap1, overlap2;
2660
2661   overlap0 = i.types[0] & i.tm.operand_types[0];
2662   if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64))
2663       && overlap0 != Imm8 && overlap0 != Imm8S
2664       && overlap0 != Imm16 && overlap0 != Imm32S
2665       && overlap0 != Imm32 && overlap0 != Imm64)
2666     {
2667       if (i.suffix)
2668         {
2669           overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
2670                        ? Imm8 | Imm8S
2671                        : (i.suffix == WORD_MNEM_SUFFIX
2672                           ? Imm16
2673                           : (i.suffix == QWORD_MNEM_SUFFIX
2674                              ? Imm64 | Imm32S
2675                              : Imm32)));
2676         }
2677       else if (overlap0 == (Imm16 | Imm32S | Imm32)
2678                || overlap0 == (Imm16 | Imm32)
2679                || overlap0 == (Imm16 | Imm32S))
2680         {
2681           overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2682                       ? Imm16 : Imm32S);
2683         }
2684       if (overlap0 != Imm8 && overlap0 != Imm8S
2685           && overlap0 != Imm16 && overlap0 != Imm32S
2686           && overlap0 != Imm32 && overlap0 != Imm64)
2687         {
2688           as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
2689           return 0;
2690         }
2691     }
2692   i.types[0] = overlap0;
2693
2694   overlap1 = i.types[1] & i.tm.operand_types[1];
2695   if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32 | Imm64))
2696       && overlap1 != Imm8 && overlap1 != Imm8S
2697       && overlap1 != Imm16 && overlap1 != Imm32S
2698       && overlap1 != Imm32 && overlap1 != Imm64)
2699     {
2700       if (i.suffix)
2701         {
2702           overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
2703                        ? Imm8 | Imm8S
2704                        : (i.suffix == WORD_MNEM_SUFFIX
2705                           ? Imm16
2706                           : (i.suffix == QWORD_MNEM_SUFFIX
2707                              ? Imm64 | Imm32S
2708                              : Imm32)));
2709         }
2710       else if (overlap1 == (Imm16 | Imm32 | Imm32S)
2711                || overlap1 == (Imm16 | Imm32)
2712                || overlap1 == (Imm16 | Imm32S))
2713         {
2714           overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2715                       ? Imm16 : Imm32S);
2716         }
2717       if (overlap1 != Imm8 && overlap1 != Imm8S
2718           && overlap1 != Imm16 && overlap1 != Imm32S
2719           && overlap1 != Imm32 && overlap1 != Imm64)
2720         {
2721           as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
2722           return 0;
2723         }
2724     }
2725   i.types[1] = overlap1;
2726
2727   overlap2 = i.types[2] & i.tm.operand_types[2];
2728   assert ((overlap2 & Imm) == 0);
2729   i.types[2] = overlap2;
2730
2731   return 1;
2732 }
2733
2734 static int
2735 process_operands ()
2736 {
2737   /* Default segment register this instruction will use for memory
2738      accesses.  0 means unknown.  This is only for optimizing out
2739      unnecessary segment overrides.  */
2740   const seg_entry *default_seg = 0;
2741
2742   /* The imul $imm, %reg instruction is converted into
2743      imul $imm, %reg, %reg, and the clr %reg instruction
2744      is converted into xor %reg, %reg.  */
2745   if (i.tm.opcode_modifier & regKludge)
2746     {
2747       unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
2748       /* Pretend we saw the extra register operand.  */
2749       assert (i.op[first_reg_op + 1].regs == 0);
2750       i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
2751       i.types[first_reg_op + 1] = i.types[first_reg_op];
2752       i.reg_operands = 2;
2753     }
2754
2755   if (i.tm.opcode_modifier & ShortForm)
2756     {
2757       /* The register or float register operand is in operand 0 or 1.  */
2758       unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
2759       /* Register goes in low 3 bits of opcode.  */
2760       i.tm.base_opcode |= i.op[op].regs->reg_num;
2761       if ((i.op[op].regs->reg_flags & RegRex) != 0)
2762         i.rex |= REX_EXTZ;
2763       if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
2764         {
2765           /* Warn about some common errors, but press on regardless.
2766              The first case can be generated by gcc (<= 2.8.1).  */
2767           if (i.operands == 2)
2768             {
2769               /* Reversed arguments on faddp, fsubp, etc.  */
2770               as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
2771                        i.op[1].regs->reg_name,
2772                        i.op[0].regs->reg_name);
2773             }
2774           else
2775             {
2776               /* Extraneous `l' suffix on fp insn.  */
2777               as_warn (_("translating to `%s %%%s'"), i.tm.name,
2778                        i.op[0].regs->reg_name);
2779             }
2780         }
2781     }
2782   else if (i.tm.opcode_modifier & Modrm)
2783     {
2784       /* The opcode is completed (modulo i.tm.extension_opcode which
2785          must be put into the modrm byte).  Now, we make the modrm and
2786          index base bytes based on all the info we've collected.  */
2787
2788       default_seg = build_modrm_byte ();
2789     }
2790   else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2791     {
2792       if (i.tm.base_opcode == POP_SEG_SHORT
2793           && i.op[0].regs->reg_num == 1)
2794         {
2795           as_bad (_("you can't `pop %%cs'"));
2796           return 0;
2797         }
2798       i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2799       if ((i.op[0].regs->reg_flags & RegRex) != 0)
2800         i.rex |= REX_EXTZ;
2801     }
2802   else if ((i.tm.base_opcode & ~(D | W)) == MOV_AX_DISP32)
2803     {
2804       default_seg = &ds;
2805     }
2806   else if ((i.tm.opcode_modifier & IsString) != 0)
2807     {
2808       /* For the string instructions that allow a segment override
2809          on one of their operands, the default segment is ds.  */
2810       default_seg = &ds;
2811     }
2812
2813   if (i.tm.base_opcode == 0x8d /* lea */ && i.seg[0] && !quiet_warnings)
2814     as_warn (_("segment override on `lea' is ineffectual"));
2815
2816   /* If a segment was explicitly specified, and the specified segment
2817      is not the default, use an opcode prefix to select it.  If we
2818      never figured out what the default segment is, then default_seg
2819      will be zero at this point, and the specified segment prefix will
2820      always be used.  */
2821   if ((i.seg[0]) && (i.seg[0] != default_seg))
2822     {
2823       if (!add_prefix (i.seg[0]->seg_prefix))
2824         return 0;
2825     }
2826   return 1;
2827 }
2828
2829 static const seg_entry *
2830 build_modrm_byte ()
2831 {
2832   const seg_entry *default_seg = 0;
2833
2834   /* i.reg_operands MUST be the number of real register operands;
2835      implicit registers do not count.  */
2836   if (i.reg_operands == 2)
2837     {
2838       unsigned int source, dest;
2839       source = ((i.types[0]
2840                  & (Reg | RegMMX | RegXMM
2841                     | SReg2 | SReg3
2842                     | Control | Debug | Test))
2843                 ? 0 : 1);
2844       dest = source + 1;
2845
2846       i.rm.mode = 3;
2847       /* One of the register operands will be encoded in the i.tm.reg
2848          field, the other in the combined i.tm.mode and i.tm.regmem
2849          fields.  If no form of this instruction supports a memory
2850          destination operand, then we assume the source operand may
2851          sometimes be a memory operand and so we need to store the
2852          destination in the i.rm.reg field.  */
2853       if ((i.tm.operand_types[dest] & AnyMem) == 0)
2854         {
2855           i.rm.reg = i.op[dest].regs->reg_num;
2856           i.rm.regmem = i.op[source].regs->reg_num;
2857           if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2858             i.rex |= REX_EXTX;
2859           if ((i.op[source].regs->reg_flags & RegRex) != 0)
2860             i.rex |= REX_EXTZ;
2861         }
2862       else
2863         {
2864           i.rm.reg = i.op[source].regs->reg_num;
2865           i.rm.regmem = i.op[dest].regs->reg_num;
2866           if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2867             i.rex |= REX_EXTZ;
2868           if ((i.op[source].regs->reg_flags & RegRex) != 0)
2869             i.rex |= REX_EXTX;
2870         }
2871     }
2872   else
2873     {                   /* If it's not 2 reg operands...  */
2874       if (i.mem_operands)
2875         {
2876           unsigned int fake_zero_displacement = 0;
2877           unsigned int op = ((i.types[0] & AnyMem)
2878                              ? 0
2879                              : (i.types[1] & AnyMem) ? 1 : 2);
2880
2881           default_seg = &ds;
2882
2883           if (i.base_reg == 0)
2884             {
2885               i.rm.mode = 0;
2886               if (!i.disp_operands)
2887                 fake_zero_displacement = 1;
2888               if (i.index_reg == 0)
2889                 {
2890                   /* Operand is just <disp>  */
2891                   if (flag_code == CODE_64BIT)
2892                     {
2893                       /* 64bit mode overwrites the 32bit absolute
2894                          addressing by RIP relative addressing and
2895                          absolute addressing is encoded by one of the
2896                          redundant SIB forms.  */
2897                       i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2898                       i.sib.base = NO_BASE_REGISTER;
2899                       i.sib.index = NO_INDEX_REGISTER;
2900                       i.types[op] = ((i.prefix[ADDR_PREFIX] == 0) ? Disp32S : Disp32);
2901                     }
2902                   else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
2903                     {
2904                       i.rm.regmem = NO_BASE_REGISTER_16;
2905                       i.types[op] = Disp16;
2906                     }
2907                   else
2908                     {
2909                       i.rm.regmem = NO_BASE_REGISTER;
2910                       i.types[op] = Disp32;
2911                     }
2912                 }
2913               else /* !i.base_reg && i.index_reg  */
2914                 {
2915                   i.sib.index = i.index_reg->reg_num;
2916                   i.sib.base = NO_BASE_REGISTER;
2917                   i.sib.scale = i.log2_scale_factor;
2918                   i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2919                   i.types[op] &= ~Disp;
2920                   if (flag_code != CODE_64BIT)
2921                     i.types[op] |= Disp32;      /* Must be 32 bit */
2922                   else
2923                     i.types[op] |= Disp32S;
2924                   if ((i.index_reg->reg_flags & RegRex) != 0)
2925                     i.rex |= REX_EXTY;
2926                 }
2927             }
2928           /* RIP addressing for 64bit mode.  */
2929           else if (i.base_reg->reg_type == BaseIndex)
2930             {
2931               i.rm.regmem = NO_BASE_REGISTER;
2932               i.types[op] &= ~ Disp;
2933               i.types[op] |= Disp32S;
2934               i.flags[op] = Operand_PCrel;
2935               if (! i.disp_operands)
2936                 fake_zero_displacement = 1;
2937             }
2938           else if (i.base_reg->reg_type & Reg16)
2939             {
2940               switch (i.base_reg->reg_num)
2941                 {
2942                 case 3: /* (%bx)  */
2943                   if (i.index_reg == 0)
2944                     i.rm.regmem = 7;
2945                   else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
2946                     i.rm.regmem = i.index_reg->reg_num - 6;
2947                   break;
2948                 case 5: /* (%bp)  */
2949                   default_seg = &ss;
2950                   if (i.index_reg == 0)
2951                     {
2952                       i.rm.regmem = 6;
2953                       if ((i.types[op] & Disp) == 0)
2954                         {
2955                           /* fake (%bp) into 0(%bp)  */
2956                           i.types[op] |= Disp8;
2957                           fake_zero_displacement = 1;
2958                         }
2959                     }
2960                   else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
2961                     i.rm.regmem = i.index_reg->reg_num - 6 + 2;
2962                   break;
2963                 default: /* (%si) -> 4 or (%di) -> 5  */
2964                   i.rm.regmem = i.base_reg->reg_num - 6 + 4;
2965                 }
2966               i.rm.mode = mode_from_disp_size (i.types[op]);
2967             }
2968           else /* i.base_reg and 32/64 bit mode  */
2969             {
2970               if (flag_code == CODE_64BIT
2971                   && (i.types[op] & Disp))
2972                 i.types[op] = (i.types[op] & Disp8) | (i.prefix[ADDR_PREFIX] == 0 ? Disp32S : Disp32);
2973
2974               i.rm.regmem = i.base_reg->reg_num;
2975               if ((i.base_reg->reg_flags & RegRex) != 0)
2976                 i.rex |= REX_EXTZ;
2977               i.sib.base = i.base_reg->reg_num;
2978               /* x86-64 ignores REX prefix bit here to avoid decoder
2979                  complications.  */
2980               if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
2981                 {
2982                   default_seg = &ss;
2983                   if (i.disp_operands == 0)
2984                     {
2985                       fake_zero_displacement = 1;
2986                       i.types[op] |= Disp8;
2987                     }
2988                 }
2989               else if (i.base_reg->reg_num == ESP_REG_NUM)
2990                 {
2991                   default_seg = &ss;
2992                 }
2993               i.sib.scale = i.log2_scale_factor;
2994               if (i.index_reg == 0)
2995                 {
2996                   /* <disp>(%esp) becomes two byte modrm with no index
2997                      register.  We've already stored the code for esp
2998                      in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
2999                      Any base register besides %esp will not use the
3000                      extra modrm byte.  */
3001                   i.sib.index = NO_INDEX_REGISTER;
3002 #if !SCALE1_WHEN_NO_INDEX
3003                   /* Another case where we force the second modrm byte.  */
3004                   if (i.log2_scale_factor)
3005                     i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3006 #endif
3007                 }
3008               else
3009                 {
3010                   i.sib.index = i.index_reg->reg_num;
3011                   i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3012                   if ((i.index_reg->reg_flags & RegRex) != 0)
3013                     i.rex |= REX_EXTY;
3014                 }
3015               i.rm.mode = mode_from_disp_size (i.types[op]);
3016             }
3017
3018           if (fake_zero_displacement)
3019             {
3020               /* Fakes a zero displacement assuming that i.types[op]
3021                  holds the correct displacement size.  */
3022               expressionS *exp;
3023
3024               assert (i.op[op].disps == 0);
3025               exp = &disp_expressions[i.disp_operands++];
3026               i.op[op].disps = exp;
3027               exp->X_op = O_constant;
3028               exp->X_add_number = 0;
3029               exp->X_add_symbol = (symbolS *) 0;
3030               exp->X_op_symbol = (symbolS *) 0;
3031             }
3032         }
3033
3034       /* Fill in i.rm.reg or i.rm.regmem field with register operand
3035          (if any) based on i.tm.extension_opcode.  Again, we must be
3036          careful to make sure that segment/control/debug/test/MMX
3037          registers are coded into the i.rm.reg field.  */
3038       if (i.reg_operands)
3039         {
3040           unsigned int op =
3041             ((i.types[0]
3042               & (Reg | RegMMX | RegXMM
3043                  | SReg2 | SReg3
3044                  | Control | Debug | Test))
3045              ? 0
3046              : ((i.types[1]
3047                  & (Reg | RegMMX | RegXMM
3048                     | SReg2 | SReg3
3049                     | Control | Debug | Test))
3050                 ? 1
3051                 : 2));
3052           /* If there is an extension opcode to put here, the register
3053              number must be put into the regmem field.  */
3054           if (i.tm.extension_opcode != None)
3055             {
3056               i.rm.regmem = i.op[op].regs->reg_num;
3057               if ((i.op[op].regs->reg_flags & RegRex) != 0)
3058                 i.rex |= REX_EXTZ;
3059             }
3060           else
3061             {
3062               i.rm.reg = i.op[op].regs->reg_num;
3063               if ((i.op[op].regs->reg_flags & RegRex) != 0)
3064                 i.rex |= REX_EXTX;
3065             }
3066
3067           /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
3068              must set it to 3 to indicate this is a register operand
3069              in the regmem field.  */
3070           if (!i.mem_operands)
3071             i.rm.mode = 3;
3072         }
3073
3074       /* Fill in i.rm.reg field with extension opcode (if any).  */
3075       if (i.tm.extension_opcode != None)
3076         i.rm.reg = i.tm.extension_opcode;
3077     }
3078   return default_seg;
3079 }
3080
3081 static void
3082 output_branch ()
3083 {
3084   char *p;
3085   int code16;
3086   int prefix;
3087   relax_substateT subtype;
3088   symbolS *sym;
3089   offsetT off;
3090
3091   code16 = 0;
3092   if (flag_code == CODE_16BIT)
3093     code16 = CODE16;
3094
3095   prefix = 0;
3096   if (i.prefix[DATA_PREFIX] != 0)
3097     {
3098       prefix = 1;
3099       i.prefixes -= 1;
3100       code16 ^= CODE16;
3101     }
3102   /* Pentium4 branch hints.  */
3103   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3104       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3105     {
3106       prefix++;
3107       i.prefixes--;
3108     }
3109   if (i.prefix[REX_PREFIX] != 0)
3110     {
3111       prefix++;
3112       i.prefixes--;
3113     }
3114
3115   if (i.prefixes != 0 && !intel_syntax)
3116     as_warn (_("skipping prefixes on this instruction"));
3117
3118   /* It's always a symbol;  End frag & setup for relax.
3119      Make sure there is enough room in this frag for the largest
3120      instruction we may generate in md_convert_frag.  This is 2
3121      bytes for the opcode and room for the prefix and largest
3122      displacement.  */
3123   frag_grow (prefix + 2 + 4);
3124   /* Prefix and 1 opcode byte go in fr_fix.  */
3125   p = frag_more (prefix + 1);
3126   if (i.prefix[DATA_PREFIX] != 0)
3127     *p++ = DATA_PREFIX_OPCODE;
3128   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
3129       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
3130     *p++ = i.prefix[SEG_PREFIX];
3131   if (i.prefix[REX_PREFIX] != 0)
3132     *p++ = i.prefix[REX_PREFIX];
3133   *p = i.tm.base_opcode;
3134
3135   if ((unsigned char) *p == JUMP_PC_RELATIVE)
3136     subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
3137   else if ((cpu_arch_flags & Cpu386) != 0)
3138     subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
3139   else
3140     subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
3141   subtype |= code16;
3142
3143   sym = i.op[0].disps->X_add_symbol;
3144   off = i.op[0].disps->X_add_number;
3145
3146   if (i.op[0].disps->X_op != O_constant
3147       && i.op[0].disps->X_op != O_symbol)
3148     {
3149       /* Handle complex expressions.  */
3150       sym = make_expr_symbol (i.op[0].disps);
3151       off = 0;
3152     }
3153
3154   /* 1 possible extra opcode + 4 byte displacement go in var part.
3155      Pass reloc in fr_var.  */
3156   frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
3157 }
3158
3159 static void
3160 output_jump ()
3161 {
3162   char *p;
3163   int size;
3164   fixS *fixP;
3165
3166   if (i.tm.opcode_modifier & JumpByte)
3167     {
3168       /* This is a loop or jecxz type instruction.  */
3169       size = 1;
3170       if (i.prefix[ADDR_PREFIX] != 0)
3171         {
3172           FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
3173           i.prefixes -= 1;
3174         }
3175       /* Pentium4 branch hints.  */
3176       if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3177           || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3178         {
3179           FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
3180           i.prefixes--;
3181         }
3182     }
3183   else
3184     {
3185       int code16;
3186
3187       code16 = 0;
3188       if (flag_code == CODE_16BIT)
3189         code16 = CODE16;
3190
3191       if (i.prefix[DATA_PREFIX] != 0)
3192         {
3193           FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
3194           i.prefixes -= 1;
3195           code16 ^= CODE16;
3196         }
3197
3198       size = 4;
3199       if (code16)
3200         size = 2;
3201     }
3202
3203   if (i.prefix[REX_PREFIX] != 0)
3204     {
3205       FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
3206       i.prefixes -= 1;
3207     }
3208
3209   if (i.prefixes != 0 && !intel_syntax)
3210     as_warn (_("skipping prefixes on this instruction"));
3211
3212   p = frag_more (1 + size);
3213   *p++ = i.tm.base_opcode;
3214
3215   fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3216                       i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
3217
3218   /* All jumps handled here are signed, but don't use a signed limit
3219      check for 32 and 16 bit jumps as we want to allow wrap around at
3220      4G and 64k respectively.  */
3221   if (size == 1)
3222     fixP->fx_signed = 1;
3223 }
3224
3225 static void
3226 output_interseg_jump ()
3227 {
3228   char *p;
3229   int size;
3230   int prefix;
3231   int code16;
3232
3233   code16 = 0;
3234   if (flag_code == CODE_16BIT)
3235     code16 = CODE16;
3236
3237   prefix = 0;
3238   if (i.prefix[DATA_PREFIX] != 0)
3239     {
3240       prefix = 1;
3241       i.prefixes -= 1;
3242       code16 ^= CODE16;
3243     }
3244   if (i.prefix[REX_PREFIX] != 0)
3245     {
3246       prefix++;
3247       i.prefixes -= 1;
3248     }
3249
3250   size = 4;
3251   if (code16)
3252     size = 2;
3253
3254   if (i.prefixes != 0 && !intel_syntax)
3255     as_warn (_("skipping prefixes on this instruction"));
3256
3257   /* 1 opcode; 2 segment; offset  */
3258   p = frag_more (prefix + 1 + 2 + size);
3259
3260   if (i.prefix[DATA_PREFIX] != 0)
3261     *p++ = DATA_PREFIX_OPCODE;
3262
3263   if (i.prefix[REX_PREFIX] != 0)
3264     *p++ = i.prefix[REX_PREFIX];
3265
3266   *p++ = i.tm.base_opcode;
3267   if (i.op[1].imms->X_op == O_constant)
3268     {
3269       offsetT n = i.op[1].imms->X_add_number;
3270
3271       if (size == 2
3272           && !fits_in_unsigned_word (n)
3273           && !fits_in_signed_word (n))
3274         {
3275           as_bad (_("16-bit jump out of range"));
3276           return;
3277         }
3278       md_number_to_chars (p, n, size);
3279     }
3280   else
3281     fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3282                  i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
3283   if (i.op[0].imms->X_op != O_constant)
3284     as_bad (_("can't handle non absolute segment in `%s'"),
3285             i.tm.name);
3286   md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
3287 }
3288
3289 static void
3290 output_insn ()
3291 {
3292   fragS *insn_start_frag;
3293   offsetT insn_start_off;
3294
3295   /* Tie dwarf2 debug info to the address at the start of the insn.
3296      We can't do this after the insn has been output as the current
3297      frag may have been closed off.  eg. by frag_var.  */
3298   dwarf2_emit_insn (0);
3299
3300   insn_start_frag = frag_now;
3301   insn_start_off = frag_now_fix ();
3302
3303   /* Output jumps.  */
3304   if (i.tm.opcode_modifier & Jump)
3305     output_branch ();
3306   else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
3307     output_jump ();
3308   else if (i.tm.opcode_modifier & JumpInterSegment)
3309     output_interseg_jump ();
3310   else
3311     {
3312       /* Output normal instructions here.  */
3313       char *p;
3314       unsigned char *q;
3315
3316       /* All opcodes on i386 have either 1 or 2 bytes, PadLock instructions
3317          have 3 bytes.  We may use one more higher byte to specify a prefix
3318          the instruction requires.  */
3319       if ((i.tm.cpu_flags & CpuPadLock) != 0
3320           && (i.tm.base_opcode & 0xff000000) != 0)
3321         {
3322           unsigned int prefix;
3323           prefix = (i.tm.base_opcode >> 24) & 0xff;
3324
3325           if (prefix != REPE_PREFIX_OPCODE
3326               || i.prefix[LOCKREP_PREFIX] != REPE_PREFIX_OPCODE)
3327             add_prefix (prefix);
3328         }
3329       else
3330         if ((i.tm.cpu_flags & CpuPadLock) == 0
3331             && (i.tm.base_opcode & 0xff0000) != 0)
3332           add_prefix ((i.tm.base_opcode >> 16) & 0xff);
3333
3334       /* The prefix bytes.  */
3335       for (q = i.prefix;
3336            q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
3337            q++)
3338         {
3339           if (*q)
3340             {
3341               p = frag_more (1);
3342               md_number_to_chars (p, (valueT) *q, 1);
3343             }
3344         }
3345
3346       /* Now the opcode; be careful about word order here!  */
3347       if (fits_in_unsigned_byte (i.tm.base_opcode))
3348         {
3349           FRAG_APPEND_1_CHAR (i.tm.base_opcode);
3350         }
3351       else
3352         {
3353           if ((i.tm.cpu_flags & CpuPadLock) != 0)
3354             {
3355               p = frag_more (3);
3356               *p++ = (i.tm.base_opcode >> 16) & 0xff;
3357             }
3358           else
3359             p = frag_more (2);
3360
3361           /* Put out high byte first: can't use md_number_to_chars!  */
3362           *p++ = (i.tm.base_opcode >> 8) & 0xff;
3363           *p = i.tm.base_opcode & 0xff;
3364         }
3365
3366       /* Now the modrm byte and sib byte (if present).  */
3367       if (i.tm.opcode_modifier & Modrm)
3368         {
3369           p = frag_more (1);
3370           md_number_to_chars (p,
3371                               (valueT) (i.rm.regmem << 0
3372                                         | i.rm.reg << 3
3373                                         | i.rm.mode << 6),
3374                               1);
3375           /* If i.rm.regmem == ESP (4)
3376              && i.rm.mode != (Register mode)
3377              && not 16 bit
3378              ==> need second modrm byte.  */
3379           if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
3380               && i.rm.mode != 3
3381               && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
3382             {
3383               p = frag_more (1);
3384               md_number_to_chars (p,
3385                                   (valueT) (i.sib.base << 0
3386                                             | i.sib.index << 3
3387                                             | i.sib.scale << 6),
3388                                   1);
3389             }
3390         }
3391
3392       if (i.disp_operands)
3393         output_disp (insn_start_frag, insn_start_off);
3394
3395       if (i.imm_operands)
3396         output_imm (insn_start_frag, insn_start_off);
3397     }
3398
3399 #ifdef DEBUG386
3400   if (flag_debug)
3401     {
3402       pi (line, &i);
3403     }
3404 #endif /* DEBUG386  */
3405 }
3406
3407 static void
3408 output_disp (insn_start_frag, insn_start_off)
3409     fragS *insn_start_frag;
3410     offsetT insn_start_off;
3411 {
3412   char *p;
3413   unsigned int n;
3414
3415   for (n = 0; n < i.operands; n++)
3416     {
3417       if (i.types[n] & Disp)
3418         {
3419           if (i.op[n].disps->X_op == O_constant)
3420             {
3421               int size;
3422               offsetT val;
3423
3424               size = 4;
3425               if (i.types[n] & (Disp8 | Disp16 | Disp64))
3426                 {
3427                   size = 2;
3428                   if (i.types[n] & Disp8)
3429                     size = 1;
3430                   if (i.types[n] & Disp64)
3431                     size = 8;
3432                 }
3433               val = offset_in_range (i.op[n].disps->X_add_number,
3434                                      size);
3435               p = frag_more (size);
3436               md_number_to_chars (p, val, size);
3437             }
3438           else
3439             {
3440               enum bfd_reloc_code_real reloc_type;
3441               int size = 4;
3442               int sign = 0;
3443               int pcrel = (i.flags[n] & Operand_PCrel) != 0;
3444
3445               /* The PC relative address is computed relative
3446                  to the instruction boundary, so in case immediate
3447                  fields follows, we need to adjust the value.  */
3448               if (pcrel && i.imm_operands)
3449                 {
3450                   int imm_size = 4;
3451                   unsigned int n1;
3452
3453                   for (n1 = 0; n1 < i.operands; n1++)
3454                     if (i.types[n1] & Imm)
3455                       {
3456                         if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
3457                           {
3458                             imm_size = 2;
3459                             if (i.types[n1] & (Imm8 | Imm8S))
3460                               imm_size = 1;
3461                             if (i.types[n1] & Imm64)
3462                               imm_size = 8;
3463                           }
3464                         break;
3465                       }
3466                   /* We should find the immediate.  */
3467                   if (n1 == i.operands)
3468                     abort ();
3469                   i.op[n].disps->X_add_number -= imm_size;
3470                 }
3471
3472               if (i.types[n] & Disp32S)
3473                 sign = 1;
3474
3475               if (i.types[n] & (Disp16 | Disp64))
3476                 {
3477                   size = 2;
3478                   if (i.types[n] & Disp64)
3479                     size = 8;
3480                 }
3481
3482               p = frag_more (size);
3483               reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
3484               if (reloc_type == BFD_RELOC_32
3485                   && GOT_symbol
3486                   && GOT_symbol == i.op[n].disps->X_add_symbol
3487                   && (i.op[n].disps->X_op == O_symbol
3488                       || (i.op[n].disps->X_op == O_add
3489                           && ((symbol_get_value_expression
3490                                (i.op[n].disps->X_op_symbol)->X_op)
3491                               == O_subtract))))
3492                 {
3493                   offsetT add;
3494
3495                   if (insn_start_frag == frag_now)
3496                     add = (p - frag_now->fr_literal) - insn_start_off;
3497                   else
3498                     {
3499                       fragS *fr;
3500
3501                       add = insn_start_frag->fr_fix - insn_start_off;
3502                       for (fr = insn_start_frag->fr_next;
3503                            fr && fr != frag_now; fr = fr->fr_next)
3504                         add += fr->fr_fix;
3505                       add += p - frag_now->fr_literal;
3506                     }
3507
3508                   /* We don't support dynamic linking on x86-64 yet.  */
3509                   if (flag_code == CODE_64BIT)
3510                     abort ();
3511                   reloc_type = BFD_RELOC_386_GOTPC;
3512                   i.op[n].disps->X_add_number += add;
3513                 }
3514               fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3515                            i.op[n].disps, pcrel, reloc_type);
3516             }
3517         }
3518     }
3519 }
3520
3521 static void
3522 output_imm (insn_start_frag, insn_start_off)
3523     fragS *insn_start_frag;
3524     offsetT insn_start_off;
3525 {
3526   char *p;
3527   unsigned int n;
3528
3529   for (n = 0; n < i.operands; n++)
3530     {
3531       if (i.types[n] & Imm)
3532         {
3533           if (i.op[n].imms->X_op == O_constant)
3534             {
3535               int size;
3536               offsetT val;
3537
3538               size = 4;
3539               if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3540                 {
3541                   size = 2;
3542                   if (i.types[n] & (Imm8 | Imm8S))
3543                     size = 1;
3544                   else if (i.types[n] & Imm64)
3545                     size = 8;
3546                 }
3547               val = offset_in_range (i.op[n].imms->X_add_number,
3548                                      size);
3549               p = frag_more (size);
3550               md_number_to_chars (p, val, size);
3551             }
3552           else
3553             {
3554               /* Not absolute_section.
3555                  Need a 32-bit fixup (don't support 8bit
3556                  non-absolute imms).  Try to support other
3557                  sizes ...  */
3558               enum bfd_reloc_code_real reloc_type;
3559               int size = 4;
3560               int sign = 0;
3561
3562               if ((i.types[n] & (Imm32S))
3563                   && i.suffix == QWORD_MNEM_SUFFIX)
3564                 sign = 1;
3565               if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3566                 {
3567                   size = 2;
3568                   if (i.types[n] & (Imm8 | Imm8S))
3569                     size = 1;
3570                   if (i.types[n] & Imm64)
3571                     size = 8;
3572                 }
3573
3574               p = frag_more (size);
3575               reloc_type = reloc (size, 0, sign, i.reloc[n]);
3576
3577               /*   This is tough to explain.  We end up with this one if we
3578                * have operands that look like
3579                * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
3580                * obtain the absolute address of the GOT, and it is strongly
3581                * preferable from a performance point of view to avoid using
3582                * a runtime relocation for this.  The actual sequence of
3583                * instructions often look something like:
3584                *
3585                *        call    .L66
3586                * .L66:
3587                *        popl    %ebx
3588                *        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
3589                *
3590                *   The call and pop essentially return the absolute address
3591                * of the label .L66 and store it in %ebx.  The linker itself
3592                * will ultimately change the first operand of the addl so
3593                * that %ebx points to the GOT, but to keep things simple, the
3594                * .o file must have this operand set so that it generates not
3595                * the absolute address of .L66, but the absolute address of
3596                * itself.  This allows the linker itself simply treat a GOTPC
3597                * relocation as asking for a pcrel offset to the GOT to be
3598                * added in, and the addend of the relocation is stored in the
3599                * operand field for the instruction itself.
3600                *
3601                *   Our job here is to fix the operand so that it would add
3602                * the correct offset so that %ebx would point to itself.  The
3603                * thing that is tricky is that .-.L66 will point to the
3604                * beginning of the instruction, so we need to further modify
3605                * the operand so that it will point to itself.  There are
3606                * other cases where you have something like:
3607                *
3608                *        .long   $_GLOBAL_OFFSET_TABLE_+[.-.L66]
3609                *
3610                * and here no correction would be required.  Internally in
3611                * the assembler we treat operands of this form as not being
3612                * pcrel since the '.' is explicitly mentioned, and I wonder
3613                * whether it would simplify matters to do it this way.  Who
3614                * knows.  In earlier versions of the PIC patches, the
3615                * pcrel_adjust field was used to store the correction, but
3616                * since the expression is not pcrel, I felt it would be
3617                * confusing to do it this way.  */
3618
3619               if (reloc_type == BFD_RELOC_32
3620                   && GOT_symbol
3621                   && GOT_symbol == i.op[n].imms->X_add_symbol
3622                   && (i.op[n].imms->X_op == O_symbol
3623                       || (i.op[n].imms->X_op == O_add
3624                           && ((symbol_get_value_expression
3625                                (i.op[n].imms->X_op_symbol)->X_op)
3626                               == O_subtract))))
3627                 {
3628                   offsetT add;
3629
3630                   if (insn_start_frag == frag_now)
3631                     add = (p - frag_now->fr_literal) - insn_start_off;
3632                   else
3633                     {
3634                       fragS *fr;
3635
3636                       add = insn_start_frag->fr_fix - insn_start_off;
3637                       for (fr = insn_start_frag->fr_next;
3638                            fr && fr != frag_now; fr = fr->fr_next)
3639                         add += fr->fr_fix;
3640                       add += p - frag_now->fr_literal;
3641                     }
3642
3643                   /* We don't support dynamic linking on x86-64 yet.  */
3644                   if (flag_code == CODE_64BIT)
3645                     abort ();
3646                   reloc_type = BFD_RELOC_386_GOTPC;
3647                   i.op[n].imms->X_add_number += add;
3648                 }
3649               fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3650                            i.op[n].imms, 0, reloc_type);
3651             }
3652         }
3653     }
3654 }
3655 \f
3656 #ifndef LEX_AT
3657 static char *lex_got PARAMS ((enum bfd_reloc_code_real *, int *));
3658
3659 /* Parse operands of the form
3660    <symbol>@GOTOFF+<nnn>
3661    and similar .plt or .got references.
3662
3663    If we find one, set up the correct relocation in RELOC and copy the
3664    input string, minus the `@GOTOFF' into a malloc'd buffer for
3665    parsing by the calling routine.  Return this buffer, and if ADJUST
3666    is non-null set it to the length of the string we removed from the
3667    input line.  Otherwise return NULL.  */
3668 static char *
3669 lex_got (reloc, adjust)
3670      enum bfd_reloc_code_real *reloc;
3671      int *adjust;
3672 {
3673   static const char * const mode_name[NUM_FLAG_CODE] = { "32", "16", "64" };
3674   static const struct {
3675     const char *str;
3676     const enum bfd_reloc_code_real rel[NUM_FLAG_CODE];
3677   } gotrel[] = {
3678     { "PLT",      { BFD_RELOC_386_PLT32,      0, BFD_RELOC_X86_64_PLT32    } },
3679     { "GOTOFF",   { BFD_RELOC_386_GOTOFF,     0, 0                         } },
3680     { "GOTPCREL", { 0,                        0, BFD_RELOC_X86_64_GOTPCREL } },
3681     { "TLSGD",    { BFD_RELOC_386_TLS_GD,     0, BFD_RELOC_X86_64_TLSGD    } },
3682     { "TLSLDM",   { BFD_RELOC_386_TLS_LDM,    0, 0                         } },
3683     { "TLSLD",    { 0,                        0, BFD_RELOC_X86_64_TLSLD    } },
3684     { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32,  0, BFD_RELOC_X86_64_GOTTPOFF } },
3685     { "TPOFF",    { BFD_RELOC_386_TLS_LE_32,  0, BFD_RELOC_X86_64_TPOFF32  } },
3686     { "NTPOFF",   { BFD_RELOC_386_TLS_LE,     0, 0                         } },
3687     { "DTPOFF",   { BFD_RELOC_386_TLS_LDO_32, 0, BFD_RELOC_X86_64_DTPOFF32 } },
3688     { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE,  0, 0                         } },
3689     { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE,     0, 0                         } },
3690     { "GOT",      { BFD_RELOC_386_GOT32,      0, BFD_RELOC_X86_64_GOT32    } }
3691   };
3692   char *cp;
3693   unsigned int j;
3694
3695   for (cp = input_line_pointer; *cp != '@'; cp++)
3696     if (is_end_of_line[(unsigned char) *cp])
3697       return NULL;
3698
3699   for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
3700     {
3701       int len;
3702
3703       len = strlen (gotrel[j].str);
3704       if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
3705         {
3706           if (gotrel[j].rel[(unsigned int) flag_code] != 0)
3707             {
3708               int first, second;
3709               char *tmpbuf, *past_reloc;
3710
3711               *reloc = gotrel[j].rel[(unsigned int) flag_code];
3712               if (adjust)
3713                 *adjust = len;
3714
3715               if (GOT_symbol == NULL)
3716                 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
3717
3718               /* Replace the relocation token with ' ', so that
3719                  errors like foo@GOTOFF1 will be detected.  */
3720
3721               /* The length of the first part of our input line.  */
3722               first = cp - input_line_pointer;
3723
3724               /* The second part goes from after the reloc token until
3725                  (and including) an end_of_line char.  Don't use strlen
3726                  here as the end_of_line char may not be a NUL.  */
3727               past_reloc = cp + 1 + len;
3728               for (cp = past_reloc; !is_end_of_line[(unsigned char) *cp++]; )
3729                 ;
3730               second = cp - past_reloc;
3731
3732               /* Allocate and copy string.  The trailing NUL shouldn't
3733                  be necessary, but be safe.  */
3734               tmpbuf = xmalloc (first + second + 2);
3735               memcpy (tmpbuf, input_line_pointer, first);
3736               tmpbuf[first] = ' ';
3737               memcpy (tmpbuf + first + 1, past_reloc, second);
3738               tmpbuf[first + second + 1] = '\0';
3739               return tmpbuf;
3740             }
3741
3742           as_bad (_("@%s reloc is not supported in %s bit mode"),
3743                   gotrel[j].str, mode_name[(unsigned int) flag_code]);
3744           return NULL;
3745         }
3746     }
3747
3748   /* Might be a symbol version string.  Don't as_bad here.  */
3749   return NULL;
3750 }
3751
3752 /* x86_cons_fix_new is called via the expression parsing code when a
3753    reloc is needed.  We use this hook to get the correct .got reloc.  */
3754 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
3755
3756 void
3757 x86_cons_fix_new (frag, off, len, exp)
3758      fragS *frag;
3759      unsigned int off;
3760      unsigned int len;
3761      expressionS *exp;
3762 {
3763   enum bfd_reloc_code_real r = reloc (len, 0, 0, got_reloc);
3764   got_reloc = NO_RELOC;
3765   fix_new_exp (frag, off, len, exp, 0, r);
3766 }
3767
3768 void
3769 x86_cons (exp, size)
3770      expressionS *exp;
3771      int size;
3772 {
3773   if (size == 4)
3774     {
3775       /* Handle @GOTOFF and the like in an expression.  */
3776       char *save;
3777       char *gotfree_input_line;
3778       int adjust;
3779
3780       save = input_line_pointer;
3781       gotfree_input_line = lex_got (&got_reloc, &adjust);
3782       if (gotfree_input_line)
3783         input_line_pointer = gotfree_input_line;
3784
3785       expression (exp);
3786
3787       if (gotfree_input_line)
3788         {
3789           /* expression () has merrily parsed up to the end of line,
3790              or a comma - in the wrong buffer.  Transfer how far
3791              input_line_pointer has moved to the right buffer.  */
3792           input_line_pointer = (save
3793                                 + (input_line_pointer - gotfree_input_line)
3794                                 + adjust);
3795           free (gotfree_input_line);
3796         }
3797     }
3798   else
3799     expression (exp);
3800 }
3801 #endif
3802
3803 #ifdef TE_PE
3804
3805 void
3806 x86_pe_cons_fix_new (frag, off, len, exp)
3807      fragS *frag;
3808      unsigned int off;
3809      unsigned int len;
3810      expressionS *exp;
3811 {
3812   enum bfd_reloc_code_real r = reloc (len, 0, 0, NO_RELOC);
3813
3814   if (exp->X_op == O_secrel)
3815     {
3816       exp->X_op = O_symbol;
3817       r = BFD_RELOC_32_SECREL;
3818     }
3819
3820   fix_new_exp (frag, off, len, exp, 0, r);
3821 }
3822
3823 static void
3824 pe_directive_secrel (dummy)
3825      int dummy ATTRIBUTE_UNUSED;
3826 {
3827   expressionS exp;
3828
3829   do
3830     {
3831       expression (&exp);
3832       if (exp.X_op == O_symbol)
3833         exp.X_op = O_secrel;
3834
3835       emit_expr (&exp, 4);
3836     }
3837   while (*input_line_pointer++ == ',');
3838
3839   input_line_pointer--;
3840   demand_empty_rest_of_line ();
3841 }
3842
3843 #endif
3844
3845 static int i386_immediate PARAMS ((char *));
3846
3847 static int
3848 i386_immediate (imm_start)
3849      char *imm_start;
3850 {
3851   char *save_input_line_pointer;
3852 #ifndef LEX_AT
3853   char *gotfree_input_line;
3854 #endif
3855   segT exp_seg = 0;
3856   expressionS *exp;
3857
3858   if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
3859     {
3860       as_bad (_("only 1 or 2 immediate operands are allowed"));
3861       return 0;
3862     }
3863
3864   exp = &im_expressions[i.imm_operands++];
3865   i.op[this_operand].imms = exp;
3866
3867   if (is_space_char (*imm_start))
3868     ++imm_start;
3869
3870   save_input_line_pointer = input_line_pointer;
3871   input_line_pointer = imm_start;
3872
3873 #ifndef LEX_AT
3874   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
3875   if (gotfree_input_line)
3876     input_line_pointer = gotfree_input_line;
3877 #endif
3878
3879   exp_seg = expression (exp);
3880
3881   SKIP_WHITESPACE ();
3882   if (*input_line_pointer)
3883     as_bad (_("junk `%s' after expression"), input_line_pointer);
3884
3885   input_line_pointer = save_input_line_pointer;
3886 #ifndef LEX_AT
3887   if (gotfree_input_line)
3888     free (gotfree_input_line);
3889 #endif
3890
3891   if (exp->X_op == O_absent || exp->X_op == O_big)
3892     {
3893       /* Missing or bad expr becomes absolute 0.  */
3894       as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
3895               imm_start);
3896       exp->X_op = O_constant;
3897       exp->X_add_number = 0;
3898       exp->X_add_symbol = (symbolS *) 0;
3899       exp->X_op_symbol = (symbolS *) 0;
3900     }
3901   else if (exp->X_op == O_constant)
3902     {
3903       /* Size it properly later.  */
3904       i.types[this_operand] |= Imm64;
3905       /* If BFD64, sign extend val.  */
3906       if (!use_rela_relocations)
3907         if ((exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
3908           exp->X_add_number = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
3909     }
3910 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3911   else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3912            && exp_seg != absolute_section
3913            && exp_seg != text_section
3914            && exp_seg != data_section
3915            && exp_seg != bss_section
3916            && exp_seg != undefined_section
3917            && !bfd_is_com_section (exp_seg))
3918     {
3919       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3920       return 0;
3921     }
3922 #endif
3923   else
3924     {
3925       /* This is an address.  The size of the address will be
3926          determined later, depending on destination register,
3927          suffix, or the default for the section.  */
3928       i.types[this_operand] |= Imm8 | Imm16 | Imm32 | Imm32S | Imm64;
3929     }
3930
3931   return 1;
3932 }
3933
3934 static char *i386_scale PARAMS ((char *));
3935
3936 static char *
3937 i386_scale (scale)
3938      char *scale;
3939 {
3940   offsetT val;
3941   char *save = input_line_pointer;
3942
3943   input_line_pointer = scale;
3944   val = get_absolute_expression ();
3945
3946   switch (val)
3947     {
3948     case 1:
3949       i.log2_scale_factor = 0;
3950       break;
3951     case 2:
3952       i.log2_scale_factor = 1;
3953       break;
3954     case 4:
3955       i.log2_scale_factor = 2;
3956       break;
3957     case 8:
3958       i.log2_scale_factor = 3;
3959       break;
3960     default:
3961       as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
3962               scale);
3963       input_line_pointer = save;
3964       return NULL;
3965     }
3966   if (i.log2_scale_factor != 0 && i.index_reg == 0)
3967     {
3968       as_warn (_("scale factor of %d without an index register"),
3969                1 << i.log2_scale_factor);
3970 #if SCALE1_WHEN_NO_INDEX
3971       i.log2_scale_factor = 0;
3972 #endif
3973     }
3974   scale = input_line_pointer;
3975   input_line_pointer = save;
3976   return scale;
3977 }
3978
3979 static int i386_displacement PARAMS ((char *, char *));
3980
3981 static int
3982 i386_displacement (disp_start, disp_end)
3983      char *disp_start;
3984      char *disp_end;
3985 {
3986   expressionS *exp;
3987   segT exp_seg = 0;
3988   char *save_input_line_pointer;
3989 #ifndef LEX_AT
3990   char *gotfree_input_line;
3991 #endif
3992   int bigdisp = Disp32;
3993
3994   if (flag_code == CODE_64BIT)
3995     {
3996       if (i.prefix[ADDR_PREFIX] == 0)
3997         bigdisp = Disp64;
3998     }
3999   else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
4000     bigdisp = Disp16;
4001   i.types[this_operand] |= bigdisp;
4002
4003   exp = &disp_expressions[i.disp_operands];
4004   i.op[this_operand].disps = exp;
4005   i.disp_operands++;
4006   save_input_line_pointer = input_line_pointer;
4007   input_line_pointer = disp_start;
4008   END_STRING_AND_SAVE (disp_end);
4009
4010 #ifndef GCC_ASM_O_HACK
4011 #define GCC_ASM_O_HACK 0
4012 #endif
4013 #if GCC_ASM_O_HACK
4014   END_STRING_AND_SAVE (disp_end + 1);
4015   if ((i.types[this_operand] & BaseIndex) != 0
4016       && displacement_string_end[-1] == '+')
4017     {
4018       /* This hack is to avoid a warning when using the "o"
4019          constraint within gcc asm statements.
4020          For instance:
4021
4022          #define _set_tssldt_desc(n,addr,limit,type) \
4023          __asm__ __volatile__ ( \
4024          "movw %w2,%0\n\t" \
4025          "movw %w1,2+%0\n\t" \
4026          "rorl $16,%1\n\t" \
4027          "movb %b1,4+%0\n\t" \
4028          "movb %4,5+%0\n\t" \
4029          "movb $0,6+%0\n\t" \
4030          "movb %h1,7+%0\n\t" \
4031          "rorl $16,%1" \
4032          : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
4033
4034          This works great except that the output assembler ends
4035          up looking a bit weird if it turns out that there is
4036          no offset.  You end up producing code that looks like:
4037
4038          #APP
4039          movw $235,(%eax)
4040          movw %dx,2+(%eax)
4041          rorl $16,%edx
4042          movb %dl,4+(%eax)
4043          movb $137,5+(%eax)
4044          movb $0,6+(%eax)
4045          movb %dh,7+(%eax)
4046          rorl $16,%edx
4047          #NO_APP
4048
4049          So here we provide the missing zero.  */
4050
4051       *displacement_string_end = '0';
4052     }
4053 #endif
4054 #ifndef LEX_AT
4055   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
4056   if (gotfree_input_line)
4057     input_line_pointer = gotfree_input_line;
4058 #endif
4059
4060   exp_seg = expression (exp);
4061
4062   SKIP_WHITESPACE ();
4063   if (*input_line_pointer)
4064     as_bad (_("junk `%s' after expression"), input_line_pointer);
4065 #if GCC_ASM_O_HACK
4066   RESTORE_END_STRING (disp_end + 1);
4067 #endif
4068   RESTORE_END_STRING (disp_end);
4069   input_line_pointer = save_input_line_pointer;
4070 #ifndef LEX_AT
4071   if (gotfree_input_line)
4072     free (gotfree_input_line);
4073 #endif
4074
4075   /* We do this to make sure that the section symbol is in
4076      the symbol table.  We will ultimately change the relocation
4077      to be relative to the beginning of the section.  */
4078   if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
4079       || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
4080     {
4081       if (exp->X_op != O_symbol)
4082         {
4083           as_bad (_("bad expression used with @%s"),
4084                   (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
4085                    ? "GOTPCREL"
4086                    : "GOTOFF"));
4087           return 0;
4088         }
4089
4090       if (S_IS_LOCAL (exp->X_add_symbol)
4091           && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
4092         section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
4093       exp->X_op = O_subtract;
4094       exp->X_op_symbol = GOT_symbol;
4095       if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
4096         i.reloc[this_operand] = BFD_RELOC_32_PCREL;
4097       else
4098         i.reloc[this_operand] = BFD_RELOC_32;
4099     }
4100
4101   if (exp->X_op == O_absent || exp->X_op == O_big)
4102     {
4103       /* Missing or bad expr becomes absolute 0.  */
4104       as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
4105               disp_start);
4106       exp->X_op = O_constant;
4107       exp->X_add_number = 0;
4108       exp->X_add_symbol = (symbolS *) 0;
4109       exp->X_op_symbol = (symbolS *) 0;
4110     }
4111
4112 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4113   if (exp->X_op != O_constant
4114       && OUTPUT_FLAVOR == bfd_target_aout_flavour
4115       && exp_seg != absolute_section
4116       && exp_seg != text_section
4117       && exp_seg != data_section
4118       && exp_seg != bss_section
4119       && exp_seg != undefined_section
4120       && !bfd_is_com_section (exp_seg))
4121     {
4122       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
4123       return 0;
4124     }
4125 #endif
4126   else if (flag_code == CODE_64BIT)
4127     i.types[this_operand] |= Disp32S | Disp32;
4128   return 1;
4129 }
4130
4131 static int i386_index_check PARAMS ((const char *));
4132
4133 /* Make sure the memory operand we've been dealt is valid.
4134    Return 1 on success, 0 on a failure.  */
4135
4136 static int
4137 i386_index_check (operand_string)
4138      const char *operand_string;
4139 {
4140   int ok;
4141 #if INFER_ADDR_PREFIX
4142   int fudged = 0;
4143
4144  tryprefix:
4145 #endif
4146   ok = 1;
4147    if (flag_code == CODE_64BIT)
4148      {
4149        unsigned RegXX = (i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32);
4150
4151        if ((i.base_reg
4152             && ((i.base_reg->reg_type & RegXX) == 0)
4153             && (i.base_reg->reg_type != BaseIndex
4154                 || i.index_reg))
4155            || (i.index_reg
4156                && ((i.index_reg->reg_type & (RegXX | BaseIndex))
4157                    != (RegXX | BaseIndex))))
4158          ok = 0;
4159     }
4160   else
4161     {
4162       if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
4163         {
4164           /* 16bit checks.  */
4165           if ((i.base_reg
4166                && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
4167                    != (Reg16 | BaseIndex)))
4168               || (i.index_reg
4169                   && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
4170                        != (Reg16 | BaseIndex))
4171                       || !(i.base_reg
4172                            && i.base_reg->reg_num < 6
4173                            && i.index_reg->reg_num >= 6
4174                            && i.log2_scale_factor == 0))))
4175             ok = 0;
4176         }
4177       else
4178         {
4179           /* 32bit checks.  */
4180           if ((i.base_reg
4181                && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
4182               || (i.index_reg
4183                   && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
4184                       != (Reg32 | BaseIndex))))
4185             ok = 0;
4186         }
4187     }
4188   if (!ok)
4189     {
4190 #if INFER_ADDR_PREFIX
4191       if (i.prefix[ADDR_PREFIX] == 0)
4192         {
4193           i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
4194           i.prefixes += 1;
4195           /* Change the size of any displacement too.  At most one of
4196              Disp16 or Disp32 is set.
4197              FIXME.  There doesn't seem to be any real need for separate
4198              Disp16 and Disp32 flags.  The same goes for Imm16 and Imm32.
4199              Removing them would probably clean up the code quite a lot.  */
4200           if (flag_code != CODE_64BIT && (i.types[this_operand] & (Disp16 | Disp32)))
4201              i.types[this_operand] ^= (Disp16 | Disp32);
4202           fudged = 1;
4203           goto tryprefix;
4204         }
4205       if (fudged)
4206         as_bad (_("`%s' is not a valid base/index expression"),
4207                 operand_string);
4208       else
4209 #endif
4210         as_bad (_("`%s' is not a valid %s bit base/index expression"),
4211                 operand_string,
4212                 flag_code_names[flag_code]);
4213     }
4214   return ok;
4215 }
4216
4217 /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
4218    on error.  */
4219
4220 static int
4221 i386_operand (operand_string)
4222      char *operand_string;
4223 {
4224   const reg_entry *r;
4225   char *end_op;
4226   char *op_string = operand_string;
4227
4228   if (is_space_char (*op_string))
4229     ++op_string;
4230
4231   /* We check for an absolute prefix (differentiating,
4232      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
4233   if (*op_string == ABSOLUTE_PREFIX)
4234     {
4235       ++op_string;
4236       if (is_space_char (*op_string))
4237         ++op_string;
4238       i.types[this_operand] |= JumpAbsolute;
4239     }
4240
4241   /* Check if operand is a register.  */
4242   if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
4243       && (r = parse_register (op_string, &end_op)) != NULL)
4244     {
4245       /* Check for a segment override by searching for ':' after a
4246          segment register.  */
4247       op_string = end_op;
4248       if (is_space_char (*op_string))
4249         ++op_string;
4250       if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
4251         {
4252           switch (r->reg_num)
4253             {
4254             case 0:
4255               i.seg[i.mem_operands] = &es;
4256               break;
4257             case 1:
4258               i.seg[i.mem_operands] = &cs;
4259               break;
4260             case 2:
4261               i.seg[i.mem_operands] = &ss;
4262               break;
4263             case 3:
4264               i.seg[i.mem_operands] = &ds;
4265               break;
4266             case 4:
4267               i.seg[i.mem_operands] = &fs;
4268               break;
4269             case 5:
4270               i.seg[i.mem_operands] = &gs;
4271               break;
4272             }
4273
4274           /* Skip the ':' and whitespace.  */
4275           ++op_string;
4276           if (is_space_char (*op_string))
4277             ++op_string;
4278
4279           if (!is_digit_char (*op_string)
4280               && !is_identifier_char (*op_string)
4281               && *op_string != '('
4282               && *op_string != ABSOLUTE_PREFIX)
4283             {
4284               as_bad (_("bad memory operand `%s'"), op_string);
4285               return 0;
4286             }
4287           /* Handle case of %es:*foo.  */
4288           if (*op_string == ABSOLUTE_PREFIX)
4289             {
4290               ++op_string;
4291               if (is_space_char (*op_string))
4292                 ++op_string;
4293               i.types[this_operand] |= JumpAbsolute;
4294             }
4295           goto do_memory_reference;
4296         }
4297       if (*op_string)
4298         {
4299           as_bad (_("junk `%s' after register"), op_string);
4300           return 0;
4301         }
4302       i.types[this_operand] |= r->reg_type & ~BaseIndex;
4303       i.op[this_operand].regs = r;
4304       i.reg_operands++;
4305     }
4306   else if (*op_string == REGISTER_PREFIX)
4307     {
4308       as_bad (_("bad register name `%s'"), op_string);
4309       return 0;
4310     }
4311   else if (*op_string == IMMEDIATE_PREFIX)
4312     {
4313       ++op_string;
4314       if (i.types[this_operand] & JumpAbsolute)
4315         {
4316           as_bad (_("immediate operand illegal with absolute jump"));
4317           return 0;
4318         }
4319       if (!i386_immediate (op_string))
4320         return 0;
4321     }
4322   else if (is_digit_char (*op_string)
4323            || is_identifier_char (*op_string)
4324            || *op_string == '(')
4325     {
4326       /* This is a memory reference of some sort.  */
4327       char *base_string;
4328
4329       /* Start and end of displacement string expression (if found).  */
4330       char *displacement_string_start;
4331       char *displacement_string_end;
4332
4333     do_memory_reference:
4334       if ((i.mem_operands == 1
4335            && (current_templates->start->opcode_modifier & IsString) == 0)
4336           || i.mem_operands == 2)
4337         {
4338           as_bad (_("too many memory references for `%s'"),
4339                   current_templates->start->name);
4340           return 0;
4341         }
4342
4343       /* Check for base index form.  We detect the base index form by
4344          looking for an ')' at the end of the operand, searching
4345          for the '(' matching it, and finding a REGISTER_PREFIX or ','
4346          after the '('.  */
4347       base_string = op_string + strlen (op_string);
4348
4349       --base_string;
4350       if (is_space_char (*base_string))
4351         --base_string;
4352
4353       /* If we only have a displacement, set-up for it to be parsed later.  */
4354       displacement_string_start = op_string;
4355       displacement_string_end = base_string + 1;
4356
4357       if (*base_string == ')')
4358         {
4359           char *temp_string;
4360           unsigned int parens_balanced = 1;
4361           /* We've already checked that the number of left & right ()'s are
4362              equal, so this loop will not be infinite.  */
4363           do
4364             {
4365               base_string--;
4366               if (*base_string == ')')
4367                 parens_balanced++;
4368               if (*base_string == '(')
4369                 parens_balanced--;
4370             }
4371           while (parens_balanced);
4372
4373           temp_string = base_string;
4374
4375           /* Skip past '(' and whitespace.  */
4376           ++base_string;
4377           if (is_space_char (*base_string))
4378             ++base_string;
4379
4380           if (*base_string == ','
4381               || ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4382                   && (i.base_reg = parse_register (base_string, &end_op)) != NULL))
4383             {
4384               displacement_string_end = temp_string;
4385
4386               i.types[this_operand] |= BaseIndex;
4387
4388               if (i.base_reg)
4389                 {
4390                   base_string = end_op;
4391                   if (is_space_char (*base_string))
4392                     ++base_string;
4393                 }
4394
4395               /* There may be an index reg or scale factor here.  */
4396               if (*base_string == ',')
4397                 {
4398                   ++base_string;
4399                   if (is_space_char (*base_string))
4400                     ++base_string;
4401
4402                   if ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4403                       && (i.index_reg = parse_register (base_string, &end_op)) != NULL)
4404                     {
4405                       base_string = end_op;
4406                       if (is_space_char (*base_string))
4407                         ++base_string;
4408                       if (*base_string == ',')
4409                         {
4410                           ++base_string;
4411                           if (is_space_char (*base_string))
4412                             ++base_string;
4413                         }
4414                       else if (*base_string != ')')
4415                         {
4416                           as_bad (_("expecting `,' or `)' after index register in `%s'"),
4417                                   operand_string);
4418                           return 0;
4419                         }
4420                     }
4421                   else if (*base_string == REGISTER_PREFIX)
4422                     {
4423                       as_bad (_("bad register name `%s'"), base_string);
4424                       return 0;
4425                     }
4426
4427                   /* Check for scale factor.  */
4428                   if (*base_string != ')')
4429                     {
4430                       char *end_scale = i386_scale (base_string);
4431
4432                       if (!end_scale)
4433                         return 0;
4434
4435                       base_string = end_scale;
4436                       if (is_space_char (*base_string))
4437                         ++base_string;
4438                       if (*base_string != ')')
4439                         {
4440                           as_bad (_("expecting `)' after scale factor in `%s'"),
4441                                   operand_string);
4442                           return 0;
4443                         }
4444                     }
4445                   else if (!i.index_reg)
4446                     {
4447                       as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
4448                               *base_string);
4449                       return 0;
4450                     }
4451                 }
4452               else if (*base_string != ')')
4453                 {
4454                   as_bad (_("expecting `,' or `)' after base register in `%s'"),
4455                           operand_string);
4456                   return 0;
4457                 }
4458             }
4459           else if (*base_string == REGISTER_PREFIX)
4460             {
4461               as_bad (_("bad register name `%s'"), base_string);
4462               return 0;
4463             }
4464         }
4465
4466       /* If there's an expression beginning the operand, parse it,
4467          assuming displacement_string_start and
4468          displacement_string_end are meaningful.  */
4469       if (displacement_string_start != displacement_string_end)
4470         {
4471           if (!i386_displacement (displacement_string_start,
4472                                   displacement_string_end))
4473             return 0;
4474         }
4475
4476       /* Special case for (%dx) while doing input/output op.  */
4477       if (i.base_reg
4478           && i.base_reg->reg_type == (Reg16 | InOutPortReg)
4479           && i.index_reg == 0
4480           && i.log2_scale_factor == 0
4481           && i.seg[i.mem_operands] == 0
4482           && (i.types[this_operand] & Disp) == 0)
4483         {
4484           i.types[this_operand] = InOutPortReg;
4485           return 1;
4486         }
4487
4488       if (i386_index_check (operand_string) == 0)
4489         return 0;
4490       i.mem_operands++;
4491     }
4492   else
4493     {
4494       /* It's not a memory operand; argh!  */
4495       as_bad (_("invalid char %s beginning operand %d `%s'"),
4496               output_invalid (*op_string),
4497               this_operand + 1,
4498               op_string);
4499       return 0;
4500     }
4501   return 1;                     /* Normal return.  */
4502 }
4503 \f
4504 /* md_estimate_size_before_relax()
4505
4506    Called just before relax() for rs_machine_dependent frags.  The x86
4507    assembler uses these frags to handle variable size jump
4508    instructions.
4509
4510    Any symbol that is now undefined will not become defined.
4511    Return the correct fr_subtype in the frag.
4512    Return the initial "guess for variable size of frag" to caller.
4513    The guess is actually the growth beyond the fixed part.  Whatever
4514    we do to grow the fixed or variable part contributes to our
4515    returned value.  */
4516
4517 int
4518 md_estimate_size_before_relax (fragP, segment)
4519      fragS *fragP;
4520      segT segment;
4521 {
4522   /* We've already got fragP->fr_subtype right;  all we have to do is
4523      check for un-relaxable symbols.  On an ELF system, we can't relax
4524      an externally visible symbol, because it may be overridden by a
4525      shared library.  */
4526   if (S_GET_SEGMENT (fragP->fr_symbol) != segment
4527 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4528       || (OUTPUT_FLAVOR == bfd_target_elf_flavour
4529           && (S_IS_EXTERNAL (fragP->fr_symbol)
4530               || S_IS_WEAK (fragP->fr_symbol)))
4531 #endif
4532       )
4533     {
4534       /* Symbol is undefined in this segment, or we need to keep a
4535          reloc so that weak symbols can be overridden.  */
4536       int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
4537       enum bfd_reloc_code_real reloc_type;
4538       unsigned char *opcode;
4539       int old_fr_fix;
4540
4541       if (fragP->fr_var != NO_RELOC)
4542         reloc_type = fragP->fr_var;
4543       else if (size == 2)
4544         reloc_type = BFD_RELOC_16_PCREL;
4545       else
4546         reloc_type = BFD_RELOC_32_PCREL;
4547
4548       old_fr_fix = fragP->fr_fix;
4549       opcode = (unsigned char *) fragP->fr_opcode;
4550
4551       switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
4552         {
4553         case UNCOND_JUMP:
4554           /* Make jmp (0xeb) a (d)word displacement jump.  */
4555           opcode[0] = 0xe9;
4556           fragP->fr_fix += size;
4557           fix_new (fragP, old_fr_fix, size,
4558                    fragP->fr_symbol,
4559                    fragP->fr_offset, 1,
4560                    reloc_type);
4561           break;
4562
4563         case COND_JUMP86:
4564           if (size == 2
4565               && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
4566             {
4567               /* Negate the condition, and branch past an
4568                  unconditional jump.  */
4569               opcode[0] ^= 1;
4570               opcode[1] = 3;
4571               /* Insert an unconditional jump.  */
4572               opcode[2] = 0xe9;
4573               /* We added two extra opcode bytes, and have a two byte
4574                  offset.  */
4575               fragP->fr_fix += 2 + 2;
4576               fix_new (fragP, old_fr_fix + 2, 2,
4577                        fragP->fr_symbol,
4578                        fragP->fr_offset, 1,
4579                        reloc_type);
4580               break;
4581             }
4582           /* Fall through.  */
4583
4584         case COND_JUMP:
4585           if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
4586             {
4587               fixS *fixP;
4588
4589               fragP->fr_fix += 1;
4590               fixP = fix_new (fragP, old_fr_fix, 1,
4591                               fragP->fr_symbol,
4592                               fragP->fr_offset, 1,
4593                               BFD_RELOC_8_PCREL);
4594               fixP->fx_signed = 1;
4595               break;
4596             }
4597
4598           /* This changes the byte-displacement jump 0x7N
4599              to the (d)word-displacement jump 0x0f,0x8N.  */
4600           opcode[1] = opcode[0] + 0x10;
4601           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4602           /* We've added an opcode byte.  */
4603           fragP->fr_fix += 1 + size;
4604           fix_new (fragP, old_fr_fix + 1, size,
4605                    fragP->fr_symbol,
4606                    fragP->fr_offset, 1,
4607                    reloc_type);
4608           break;
4609
4610         default:
4611           BAD_CASE (fragP->fr_subtype);
4612           break;
4613         }
4614       frag_wane (fragP);
4615       return fragP->fr_fix - old_fr_fix;
4616     }
4617
4618   /* Guess size depending on current relax state.  Initially the relax
4619      state will correspond to a short jump and we return 1, because
4620      the variable part of the frag (the branch offset) is one byte
4621      long.  However, we can relax a section more than once and in that
4622      case we must either set fr_subtype back to the unrelaxed state,
4623      or return the value for the appropriate branch.  */
4624   return md_relax_table[fragP->fr_subtype].rlx_length;
4625 }
4626
4627 /* Called after relax() is finished.
4628
4629    In:  Address of frag.
4630         fr_type == rs_machine_dependent.
4631         fr_subtype is what the address relaxed to.
4632
4633    Out: Any fixSs and constants are set up.
4634         Caller will turn frag into a ".space 0".  */
4635
4636 void
4637 md_convert_frag (abfd, sec, fragP)
4638      bfd *abfd ATTRIBUTE_UNUSED;
4639      segT sec ATTRIBUTE_UNUSED;
4640      fragS *fragP;
4641 {
4642   unsigned char *opcode;
4643   unsigned char *where_to_put_displacement = NULL;
4644   offsetT target_address;
4645   offsetT opcode_address;
4646   unsigned int extension = 0;
4647   offsetT displacement_from_opcode_start;
4648
4649   opcode = (unsigned char *) fragP->fr_opcode;
4650
4651   /* Address we want to reach in file space.  */
4652   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
4653
4654   /* Address opcode resides at in file space.  */
4655   opcode_address = fragP->fr_address + fragP->fr_fix;
4656
4657   /* Displacement from opcode start to fill into instruction.  */
4658   displacement_from_opcode_start = target_address - opcode_address;
4659
4660   if ((fragP->fr_subtype & BIG) == 0)
4661     {
4662       /* Don't have to change opcode.  */
4663       extension = 1;            /* 1 opcode + 1 displacement  */
4664       where_to_put_displacement = &opcode[1];
4665     }
4666   else
4667     {
4668       if (no_cond_jump_promotion
4669           && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4670         as_warn_where (fragP->fr_file, fragP->fr_line, _("long jump required"));
4671
4672       switch (fragP->fr_subtype)
4673         {
4674         case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
4675           extension = 4;                /* 1 opcode + 4 displacement  */
4676           opcode[0] = 0xe9;
4677           where_to_put_displacement = &opcode[1];
4678           break;
4679
4680         case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
4681           extension = 2;                /* 1 opcode + 2 displacement  */
4682           opcode[0] = 0xe9;
4683           where_to_put_displacement = &opcode[1];
4684           break;
4685
4686         case ENCODE_RELAX_STATE (COND_JUMP, BIG):
4687         case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
4688           extension = 5;                /* 2 opcode + 4 displacement  */
4689           opcode[1] = opcode[0] + 0x10;
4690           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4691           where_to_put_displacement = &opcode[2];
4692           break;
4693
4694         case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
4695           extension = 3;                /* 2 opcode + 2 displacement  */
4696           opcode[1] = opcode[0] + 0x10;
4697           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4698           where_to_put_displacement = &opcode[2];
4699           break;
4700
4701         case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
4702           extension = 4;
4703           opcode[0] ^= 1;
4704           opcode[1] = 3;
4705           opcode[2] = 0xe9;
4706           where_to_put_displacement = &opcode[3];
4707           break;
4708
4709         default:
4710           BAD_CASE (fragP->fr_subtype);
4711           break;
4712         }
4713     }
4714
4715   /* Now put displacement after opcode.  */
4716   md_number_to_chars ((char *) where_to_put_displacement,
4717                       (valueT) (displacement_from_opcode_start - extension),
4718                       DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
4719   fragP->fr_fix += extension;
4720 }
4721 \f
4722 /* Size of byte displacement jmp.  */
4723 int md_short_jump_size = 2;
4724
4725 /* Size of dword displacement jmp.  */
4726 int md_long_jump_size = 5;
4727
4728 /* Size of relocation record.  */
4729 const int md_reloc_size = 8;
4730
4731 void
4732 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
4733      char *ptr;
4734      addressT from_addr, to_addr;
4735      fragS *frag ATTRIBUTE_UNUSED;
4736      symbolS *to_symbol ATTRIBUTE_UNUSED;
4737 {
4738   offsetT offset;
4739
4740   offset = to_addr - (from_addr + 2);
4741   /* Opcode for byte-disp jump.  */
4742   md_number_to_chars (ptr, (valueT) 0xeb, 1);
4743   md_number_to_chars (ptr + 1, (valueT) offset, 1);
4744 }
4745
4746 void
4747 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
4748      char *ptr;
4749      addressT from_addr, to_addr;
4750      fragS *frag ATTRIBUTE_UNUSED;
4751      symbolS *to_symbol ATTRIBUTE_UNUSED;
4752 {
4753   offsetT offset;
4754
4755   offset = to_addr - (from_addr + 5);
4756   md_number_to_chars (ptr, (valueT) 0xe9, 1);
4757   md_number_to_chars (ptr + 1, (valueT) offset, 4);
4758 }
4759 \f
4760 /* Apply a fixup (fixS) to segment data, once it has been determined
4761    by our caller that we have all the info we need to fix it up.
4762
4763    On the 386, immediates, displacements, and data pointers are all in
4764    the same (little-endian) format, so we don't need to care about which
4765    we are handling.  */
4766
4767 void
4768 md_apply_fix3 (fixP, valP, seg)
4769      /* The fix we're to put in.  */
4770      fixS *fixP;
4771      /* Pointer to the value of the bits.  */
4772      valueT *valP;
4773      /* Segment fix is from.  */
4774      segT seg ATTRIBUTE_UNUSED;
4775 {
4776   char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
4777   valueT value = *valP;
4778
4779 #if !defined (TE_Mach)
4780   if (fixP->fx_pcrel)
4781     {
4782       switch (fixP->fx_r_type)
4783         {
4784         default:
4785           break;
4786
4787         case BFD_RELOC_32:
4788           fixP->fx_r_type = BFD_RELOC_32_PCREL;
4789           break;
4790         case BFD_RELOC_16:
4791           fixP->fx_r_type = BFD_RELOC_16_PCREL;
4792           break;
4793         case BFD_RELOC_8:
4794           fixP->fx_r_type = BFD_RELOC_8_PCREL;
4795           break;
4796         }
4797     }
4798
4799   if (fixP->fx_addsy != NULL
4800       && (fixP->fx_r_type == BFD_RELOC_32_PCREL
4801           || fixP->fx_r_type == BFD_RELOC_16_PCREL
4802           || fixP->fx_r_type == BFD_RELOC_8_PCREL)
4803       && !use_rela_relocations)
4804     {
4805       /* This is a hack.  There should be a better way to handle this.
4806          This covers for the fact that bfd_install_relocation will
4807          subtract the current location (for partial_inplace, PC relative
4808          relocations); see more below.  */
4809 #ifndef OBJ_AOUT
4810       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4811 #ifdef TE_PE
4812           || OUTPUT_FLAVOR == bfd_target_coff_flavour
4813 #endif
4814           )
4815         value += fixP->fx_where + fixP->fx_frag->fr_address;
4816 #endif
4817 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4818       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
4819         {
4820           segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
4821
4822           if ((sym_seg == seg
4823                || (symbol_section_p (fixP->fx_addsy)
4824                    && sym_seg != absolute_section))
4825               && !generic_force_reloc (fixP))
4826             {
4827               /* Yes, we add the values in twice.  This is because
4828                  bfd_install_relocation subtracts them out again.  I think
4829                  bfd_install_relocation is broken, but I don't dare change
4830                  it.  FIXME.  */
4831               value += fixP->fx_where + fixP->fx_frag->fr_address;
4832             }
4833         }
4834 #endif
4835 #if defined (OBJ_COFF) && defined (TE_PE)
4836       /* For some reason, the PE format does not store a
4837          section address offset for a PC relative symbol.  */
4838       if (S_GET_SEGMENT (fixP->fx_addsy) != seg
4839 #if defined(BFD_ASSEMBLER) || defined(S_IS_WEAK)
4840           || S_IS_WEAK (fixP->fx_addsy)
4841 #endif
4842           )
4843         value += md_pcrel_from (fixP);
4844 #endif
4845     }
4846
4847   /* Fix a few things - the dynamic linker expects certain values here,
4848      and we must not disappoint it.  */
4849 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4850   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4851       && fixP->fx_addsy)
4852     switch (fixP->fx_r_type)
4853       {
4854       case BFD_RELOC_386_PLT32:
4855       case BFD_RELOC_X86_64_PLT32:
4856         /* Make the jump instruction point to the address of the operand.  At
4857            runtime we merely add the offset to the actual PLT entry.  */
4858         value = -4;
4859         break;
4860
4861       case BFD_RELOC_386_TLS_GD:
4862       case BFD_RELOC_386_TLS_LDM:
4863       case BFD_RELOC_386_TLS_IE_32:
4864       case BFD_RELOC_386_TLS_IE:
4865       case BFD_RELOC_386_TLS_GOTIE:
4866       case BFD_RELOC_X86_64_TLSGD:
4867       case BFD_RELOC_X86_64_TLSLD:
4868       case BFD_RELOC_X86_64_GOTTPOFF:
4869         value = 0; /* Fully resolved at runtime.  No addend.  */
4870         /* Fallthrough */
4871       case BFD_RELOC_386_TLS_LE:
4872       case BFD_RELOC_386_TLS_LDO_32:
4873       case BFD_RELOC_386_TLS_LE_32:
4874       case BFD_RELOC_X86_64_DTPOFF32:
4875       case BFD_RELOC_X86_64_TPOFF32:
4876         S_SET_THREAD_LOCAL (fixP->fx_addsy);
4877         break;
4878
4879       case BFD_RELOC_386_GOT32:
4880       case BFD_RELOC_X86_64_GOT32:
4881         value = 0; /* Fully resolved at runtime.  No addend.  */
4882         break;
4883
4884       case BFD_RELOC_VTABLE_INHERIT:
4885       case BFD_RELOC_VTABLE_ENTRY:
4886         fixP->fx_done = 0;
4887         return;
4888
4889       default:
4890         break;
4891       }
4892 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)  */
4893   *valP = value;
4894 #endif /* !defined (TE_Mach)  */
4895
4896   /* Are we finished with this relocation now?  */
4897   if (fixP->fx_addsy == NULL)
4898     fixP->fx_done = 1;
4899   else if (use_rela_relocations)
4900     {
4901       fixP->fx_no_overflow = 1;
4902       /* Remember value for tc_gen_reloc.  */
4903       fixP->fx_addnumber = value;
4904       value = 0;
4905     }
4906
4907   md_number_to_chars (p, value, fixP->fx_size);
4908 }
4909 \f
4910 #define MAX_LITTLENUMS 6
4911
4912 /* Turn the string pointed to by litP into a floating point constant
4913    of type TYPE, and emit the appropriate bytes.  The number of
4914    LITTLENUMS emitted is stored in *SIZEP.  An error message is
4915    returned, or NULL on OK.  */
4916
4917 char *
4918 md_atof (type, litP, sizeP)
4919      int type;
4920      char *litP;
4921      int *sizeP;
4922 {
4923   int prec;
4924   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4925   LITTLENUM_TYPE *wordP;
4926   char *t;
4927
4928   switch (type)
4929     {
4930     case 'f':
4931     case 'F':
4932       prec = 2;
4933       break;
4934
4935     case 'd':
4936     case 'D':
4937       prec = 4;
4938       break;
4939
4940     case 'x':
4941     case 'X':
4942       prec = 5;
4943       break;
4944
4945     default:
4946       *sizeP = 0;
4947       return _("Bad call to md_atof ()");
4948     }
4949   t = atof_ieee (input_line_pointer, type, words);
4950   if (t)
4951     input_line_pointer = t;
4952
4953   *sizeP = prec * sizeof (LITTLENUM_TYPE);
4954   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
4955      the bigendian 386.  */
4956   for (wordP = words + prec - 1; prec--;)
4957     {
4958       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
4959       litP += sizeof (LITTLENUM_TYPE);
4960     }
4961   return 0;
4962 }
4963 \f
4964 char output_invalid_buf[8];
4965
4966 static char *
4967 output_invalid (c)
4968      int c;
4969 {
4970   if (ISPRINT (c))
4971     sprintf (output_invalid_buf, "'%c'", c);
4972   else
4973     sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
4974   return output_invalid_buf;
4975 }
4976
4977 /* REG_STRING starts *before* REGISTER_PREFIX.  */
4978
4979 static const reg_entry *
4980 parse_register (reg_string, end_op)
4981      char *reg_string;
4982      char **end_op;
4983 {
4984   char *s = reg_string;
4985   char *p;
4986   char reg_name_given[MAX_REG_NAME_SIZE + 1];
4987   const reg_entry *r;
4988
4989   /* Skip possible REGISTER_PREFIX and possible whitespace.  */
4990   if (*s == REGISTER_PREFIX)
4991     ++s;
4992
4993   if (is_space_char (*s))
4994     ++s;
4995
4996   p = reg_name_given;
4997   while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
4998     {
4999       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
5000         return (const reg_entry *) NULL;
5001       s++;
5002     }
5003
5004   /* For naked regs, make sure that we are not dealing with an identifier.
5005      This prevents confusing an identifier like `eax_var' with register
5006      `eax'.  */
5007   if (allow_naked_reg && identifier_chars[(unsigned char) *s])
5008     return (const reg_entry *) NULL;
5009
5010   *end_op = s;
5011
5012   r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
5013
5014   /* Handle floating point regs, allowing spaces in the (i) part.  */
5015   if (r == i386_regtab /* %st is first entry of table  */)
5016     {
5017       if (is_space_char (*s))
5018         ++s;
5019       if (*s == '(')
5020         {
5021           ++s;
5022           if (is_space_char (*s))
5023             ++s;
5024           if (*s >= '0' && *s <= '7')
5025             {
5026               r = &i386_float_regtab[*s - '0'];
5027               ++s;
5028               if (is_space_char (*s))
5029                 ++s;
5030               if (*s == ')')
5031                 {
5032                   *end_op = s + 1;
5033                   return r;
5034                 }
5035             }
5036           /* We have "%st(" then garbage.  */
5037           return (const reg_entry *) NULL;
5038         }
5039     }
5040
5041   if (r != NULL
5042       && ((r->reg_flags & (RegRex64 | RegRex)) | (r->reg_type & Reg64)) != 0
5043       && flag_code != CODE_64BIT)
5044     return (const reg_entry *) NULL;
5045
5046   return r;
5047 }
5048 \f
5049 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5050 const char *md_shortopts = "kVQ:sqn";
5051 #else
5052 const char *md_shortopts = "qn";
5053 #endif
5054
5055 struct option md_longopts[] = {
5056 #define OPTION_32 (OPTION_MD_BASE + 0)
5057   {"32", no_argument, NULL, OPTION_32},
5058 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5059 #define OPTION_64 (OPTION_MD_BASE + 1)
5060   {"64", no_argument, NULL, OPTION_64},
5061 #endif
5062   {NULL, no_argument, NULL, 0}
5063 };
5064 size_t md_longopts_size = sizeof (md_longopts);
5065
5066 int
5067 md_parse_option (c, arg)
5068      int c;
5069      char *arg ATTRIBUTE_UNUSED;
5070 {
5071   switch (c)
5072     {
5073     case 'n':
5074       optimize_align_code = 0;
5075       break;
5076
5077     case 'q':
5078       quiet_warnings = 1;
5079       break;
5080
5081 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5082       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
5083          should be emitted or not.  FIXME: Not implemented.  */
5084     case 'Q':
5085       break;
5086
5087       /* -V: SVR4 argument to print version ID.  */
5088     case 'V':
5089       print_version_id ();
5090       break;
5091
5092       /* -k: Ignore for FreeBSD compatibility.  */
5093     case 'k':
5094       break;
5095
5096     case 's':
5097       /* -s: On i386 Solaris, this tells the native assembler to use
5098          .stab instead of .stab.excl.  We always use .stab anyhow.  */
5099       break;
5100
5101     case OPTION_64:
5102       {
5103         const char **list, **l;
5104
5105         list = bfd_target_list ();
5106         for (l = list; *l != NULL; l++)
5107           if (strcmp (*l, "elf64-x86-64") == 0)
5108             {
5109               default_arch = "x86_64";
5110               break;
5111             }
5112         if (*l == NULL)
5113           as_fatal (_("No compiled in support for x86_64"));
5114         free (list);
5115       }
5116       break;
5117 #endif
5118
5119     case OPTION_32:
5120       default_arch = "i386";
5121       break;
5122
5123     default:
5124       return 0;
5125     }
5126   return 1;
5127 }
5128
5129 void
5130 md_show_usage (stream)
5131      FILE *stream;
5132 {
5133 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5134   fprintf (stream, _("\
5135   -Q                      ignored\n\
5136   -V                      print assembler version number\n\
5137   -k                      ignored\n\
5138   -n                      Do not optimize code alignment\n\
5139   -q                      quieten some warnings\n\
5140   -s                      ignored\n"));
5141 #else
5142   fprintf (stream, _("\
5143   -n                      Do not optimize code alignment\n\
5144   -q                      quieten some warnings\n"));
5145 #endif
5146 }
5147
5148 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
5149      || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
5150
5151 /* Pick the target format to use.  */
5152
5153 const char *
5154 i386_target_format ()
5155 {
5156   if (!strcmp (default_arch, "x86_64"))
5157     set_code_flag (CODE_64BIT);
5158   else if (!strcmp (default_arch, "i386"))
5159     set_code_flag (CODE_32BIT);
5160   else
5161     as_fatal (_("Unknown architecture"));
5162   switch (OUTPUT_FLAVOR)
5163     {
5164 #ifdef OBJ_MAYBE_AOUT
5165     case bfd_target_aout_flavour:
5166       return AOUT_TARGET_FORMAT;
5167 #endif
5168 #ifdef OBJ_MAYBE_COFF
5169     case bfd_target_coff_flavour:
5170       return "coff-i386";
5171 #endif
5172 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
5173     case bfd_target_elf_flavour:
5174       {
5175         if (flag_code == CODE_64BIT)
5176           use_rela_relocations = 1;
5177         return flag_code == CODE_64BIT ? "elf64-x86-64" : ELF_TARGET_FORMAT;
5178       }
5179 #endif
5180     default:
5181       abort ();
5182       return NULL;
5183     }
5184 }
5185
5186 #endif /* OBJ_MAYBE_ more than one  */
5187
5188 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
5189 void i386_elf_emit_arch_note ()
5190 {
5191   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
5192       && cpu_arch_name != NULL)
5193     {
5194       char *p;
5195       asection *seg = now_seg;
5196       subsegT subseg = now_subseg;
5197       Elf_Internal_Note i_note;
5198       Elf_External_Note e_note;
5199       asection *note_secp;
5200       int len;
5201
5202       /* Create the .note section.  */
5203       note_secp = subseg_new (".note", 0);
5204       bfd_set_section_flags (stdoutput,
5205                              note_secp,
5206                              SEC_HAS_CONTENTS | SEC_READONLY);
5207
5208       /* Process the arch string.  */
5209       len = strlen (cpu_arch_name);
5210
5211       i_note.namesz = len + 1;
5212       i_note.descsz = 0;
5213       i_note.type = NT_ARCH;
5214       p = frag_more (sizeof (e_note.namesz));
5215       md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
5216       p = frag_more (sizeof (e_note.descsz));
5217       md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
5218       p = frag_more (sizeof (e_note.type));
5219       md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
5220       p = frag_more (len + 1);
5221       strcpy (p, cpu_arch_name);
5222
5223       frag_align (2, 0, 0);
5224
5225       subseg_set (seg, subseg);
5226     }
5227 }
5228 #endif
5229 \f
5230 symbolS *
5231 md_undefined_symbol (name)
5232      char *name;
5233 {
5234   if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
5235       && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
5236       && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
5237       && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
5238     {
5239       if (!GOT_symbol)
5240         {
5241           if (symbol_find (name))
5242             as_bad (_("GOT already in symbol table"));
5243           GOT_symbol = symbol_new (name, undefined_section,
5244                                    (valueT) 0, &zero_address_frag);
5245         };
5246       return GOT_symbol;
5247     }
5248   return 0;
5249 }
5250
5251 /* Round up a section size to the appropriate boundary.  */
5252
5253 valueT
5254 md_section_align (segment, size)
5255      segT segment ATTRIBUTE_UNUSED;
5256      valueT size;
5257 {
5258 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5259   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
5260     {
5261       /* For a.out, force the section size to be aligned.  If we don't do
5262          this, BFD will align it for us, but it will not write out the
5263          final bytes of the section.  This may be a bug in BFD, but it is
5264          easier to fix it here since that is how the other a.out targets
5265          work.  */
5266       int align;
5267
5268       align = bfd_get_section_alignment (stdoutput, segment);
5269       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
5270     }
5271 #endif
5272
5273   return size;
5274 }
5275
5276 /* On the i386, PC-relative offsets are relative to the start of the
5277    next instruction.  That is, the address of the offset, plus its
5278    size, since the offset is always the last part of the insn.  */
5279
5280 long
5281 md_pcrel_from (fixP)
5282      fixS *fixP;
5283 {
5284   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
5285 }
5286
5287 #ifndef I386COFF
5288
5289 static void
5290 s_bss (ignore)
5291      int ignore ATTRIBUTE_UNUSED;
5292 {
5293   int temp;
5294
5295   temp = get_absolute_expression ();
5296   subseg_set (bss_section, (subsegT) temp);
5297   demand_empty_rest_of_line ();
5298 }
5299
5300 #endif
5301
5302 void
5303 i386_validate_fix (fixp)
5304      fixS *fixp;
5305 {
5306   if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
5307     {
5308       /* GOTOFF relocation are nonsense in 64bit mode.  */
5309       if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
5310         {
5311           if (flag_code != CODE_64BIT)
5312             abort ();
5313           fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
5314         }
5315       else
5316         {
5317           if (flag_code == CODE_64BIT)
5318             abort ();
5319           fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
5320         }
5321       fixp->fx_subsy = 0;
5322     }
5323 }
5324
5325 arelent *
5326 tc_gen_reloc (section, fixp)
5327      asection *section ATTRIBUTE_UNUSED;
5328      fixS *fixp;
5329 {
5330   arelent *rel;
5331   bfd_reloc_code_real_type code;
5332
5333   switch (fixp->fx_r_type)
5334     {
5335     case BFD_RELOC_X86_64_PLT32:
5336     case BFD_RELOC_X86_64_GOT32:
5337     case BFD_RELOC_X86_64_GOTPCREL:
5338     case BFD_RELOC_386_PLT32:
5339     case BFD_RELOC_386_GOT32:
5340     case BFD_RELOC_386_GOTOFF:
5341     case BFD_RELOC_386_GOTPC:
5342     case BFD_RELOC_386_TLS_GD:
5343     case BFD_RELOC_386_TLS_LDM:
5344     case BFD_RELOC_386_TLS_LDO_32:
5345     case BFD_RELOC_386_TLS_IE_32:
5346     case BFD_RELOC_386_TLS_IE:
5347     case BFD_RELOC_386_TLS_GOTIE:
5348     case BFD_RELOC_386_TLS_LE_32:
5349     case BFD_RELOC_386_TLS_LE:
5350     case BFD_RELOC_X86_64_32S:
5351     case BFD_RELOC_X86_64_TLSGD:
5352     case BFD_RELOC_X86_64_TLSLD:
5353     case BFD_RELOC_X86_64_DTPOFF32:
5354     case BFD_RELOC_X86_64_GOTTPOFF:
5355     case BFD_RELOC_X86_64_TPOFF32:
5356     case BFD_RELOC_RVA:
5357     case BFD_RELOC_VTABLE_ENTRY:
5358     case BFD_RELOC_VTABLE_INHERIT:
5359 #ifdef TE_PE
5360     case BFD_RELOC_32_SECREL:
5361 #endif
5362       code = fixp->fx_r_type;
5363       break;
5364     default:
5365       if (fixp->fx_pcrel)
5366         {
5367           switch (fixp->fx_size)
5368             {
5369             default:
5370               as_bad_where (fixp->fx_file, fixp->fx_line,
5371                             _("can not do %d byte pc-relative relocation"),
5372                             fixp->fx_size);
5373               code = BFD_RELOC_32_PCREL;
5374               break;
5375             case 1: code = BFD_RELOC_8_PCREL;  break;
5376             case 2: code = BFD_RELOC_16_PCREL; break;
5377             case 4: code = BFD_RELOC_32_PCREL; break;
5378             }
5379         }
5380       else
5381         {
5382           switch (fixp->fx_size)
5383             {
5384             default:
5385               as_bad_where (fixp->fx_file, fixp->fx_line,
5386                             _("can not do %d byte relocation"),
5387                             fixp->fx_size);
5388               code = BFD_RELOC_32;
5389               break;
5390             case 1: code = BFD_RELOC_8;  break;
5391             case 2: code = BFD_RELOC_16; break;
5392             case 4: code = BFD_RELOC_32; break;
5393 #ifdef BFD64
5394             case 8: code = BFD_RELOC_64; break;
5395 #endif
5396             }
5397         }
5398       break;
5399     }
5400
5401   if (code == BFD_RELOC_32
5402       && GOT_symbol
5403       && fixp->fx_addsy == GOT_symbol)
5404     {
5405       /* We don't support GOTPC on 64bit targets.  */
5406       if (flag_code == CODE_64BIT)
5407         abort ();
5408       code = BFD_RELOC_386_GOTPC;
5409     }
5410
5411   rel = (arelent *) xmalloc (sizeof (arelent));
5412   rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5413   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5414
5415   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
5416
5417   if (!use_rela_relocations)
5418     {
5419       /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
5420          vtable entry to be used in the relocation's section offset.  */
5421       if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5422         rel->address = fixp->fx_offset;
5423
5424       rel->addend = 0;
5425     }
5426   /* Use the rela in 64bit mode.  */
5427   else
5428     {
5429       if (!fixp->fx_pcrel)
5430         rel->addend = fixp->fx_offset;
5431       else
5432         switch (code)
5433           {
5434           case BFD_RELOC_X86_64_PLT32:
5435           case BFD_RELOC_X86_64_GOT32:
5436           case BFD_RELOC_X86_64_GOTPCREL:
5437           case BFD_RELOC_X86_64_TLSGD:
5438           case BFD_RELOC_X86_64_TLSLD:
5439           case BFD_RELOC_X86_64_GOTTPOFF:
5440             rel->addend = fixp->fx_offset - fixp->fx_size;
5441             break;
5442           default:
5443             rel->addend = (section->vma
5444                            - fixp->fx_size
5445                            + fixp->fx_addnumber
5446                            + md_pcrel_from (fixp));
5447             break;
5448           }
5449     }
5450
5451   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
5452   if (rel->howto == NULL)
5453     {
5454       as_bad_where (fixp->fx_file, fixp->fx_line,
5455                     _("cannot represent relocation type %s"),
5456                     bfd_get_reloc_code_name (code));
5457       /* Set howto to a garbage value so that we can keep going.  */
5458       rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
5459       assert (rel->howto != NULL);
5460     }
5461
5462   return rel;
5463 }
5464
5465 \f
5466 /* Parse operands using Intel syntax. This implements a recursive descent
5467    parser based on the BNF grammar published in Appendix B of the MASM 6.1
5468    Programmer's Guide.
5469
5470    FIXME: We do not recognize the full operand grammar defined in the MASM
5471           documentation.  In particular, all the structure/union and
5472           high-level macro operands are missing.
5473
5474    Uppercase words are terminals, lower case words are non-terminals.
5475    Objects surrounded by double brackets '[[' ']]' are optional. Vertical
5476    bars '|' denote choices. Most grammar productions are implemented in
5477    functions called 'intel_<production>'.
5478
5479    Initial production is 'expr'.
5480
5481     addOp               + | -
5482
5483     alpha               [a-zA-Z]
5484
5485     binOp               & | AND | \| | OR | ^ | XOR
5486
5487     byteRegister        AL | AH | BL | BH | CL | CH | DL | DH
5488
5489     constant            digits [[ radixOverride ]]
5490
5491     dataType            BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD
5492
5493     digits              decdigit
5494                         | digits decdigit
5495                         | digits hexdigit
5496
5497     decdigit            [0-9]
5498
5499     e04                 e04 addOp e05
5500                         | e05
5501
5502     e05                 e05 binOp e06
5503                         | e06
5504
5505     e06                 e06 mulOp e09
5506                         | e09
5507
5508     e09                 OFFSET e10
5509                         | ~ e10
5510                         | NOT e10
5511                         | e09 PTR e10
5512                         | e09 : e10
5513                         | e10
5514
5515     e10                 e10 [ expr ]
5516                         | e11
5517
5518     e11                 ( expr )
5519                         | [ expr ]
5520                         | constant
5521                         | dataType
5522                         | id
5523                         | $
5524                         | register
5525
5526  => expr                SHORT e04
5527                         | e04
5528
5529     gpRegister          AX | EAX | BX | EBX | CX | ECX | DX | EDX
5530                         | BP | EBP | SP | ESP | DI | EDI | SI | ESI
5531
5532     hexdigit            a | b | c | d | e | f
5533                         | A | B | C | D | E | F
5534
5535     id                  alpha
5536                         | id alpha
5537                         | id decdigit
5538
5539     mulOp               * | / | % | MOD | << | SHL | >> | SHR
5540
5541     quote               " | '
5542
5543     register            specialRegister
5544                         | gpRegister
5545                         | byteRegister
5546
5547     segmentRegister     CS | DS | ES | FS | GS | SS
5548
5549     specialRegister     CR0 | CR2 | CR3 | CR4
5550                         | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
5551                         | TR3 | TR4 | TR5 | TR6 | TR7
5552
5553     We simplify the grammar in obvious places (e.g., register parsing is
5554     done by calling parse_register) and eliminate immediate left recursion
5555     to implement a recursive-descent parser.
5556
5557     expr        SHORT e04
5558                 | e04
5559
5560     e04         e05 e04'
5561
5562     e04'        addOp e05 e04'
5563                 | Empty
5564
5565     e05         e06 e05'
5566
5567     e05'        binOp e06 e05'
5568                 | Empty
5569
5570     e06         e09 e06'
5571
5572     e06'        mulOp e09 e06'
5573                 | Empty
5574
5575     e09         OFFSET e10 e09'
5576                 | ~ e10
5577                 | NOT e10
5578                 | e10 e09'
5579
5580     e09'        PTR e10 e09'
5581                 | : e10 e09'
5582                 | Empty
5583
5584     e10         e11 e10'
5585
5586     e10'        [ expr ] e10'
5587                 | Empty
5588
5589     e11         ( expr )
5590                 | [ expr ]
5591                 | BYTE
5592                 | WORD
5593                 | DWORD
5594                 | FWORD
5595                 | QWORD
5596                 | TBYTE
5597                 | OWORD
5598                 | XMMWORD
5599                 | .
5600                 | $
5601                 | register
5602                 | id
5603                 | constant  */
5604
5605 /* Parsing structure for the intel syntax parser. Used to implement the
5606    semantic actions for the operand grammar.  */
5607 struct intel_parser_s
5608   {
5609     char *op_string;            /* The string being parsed.  */
5610     int got_a_float;            /* Whether the operand is a float.  */
5611     int op_modifier;            /* Operand modifier.  */
5612     int is_mem;                 /* 1 if operand is memory reference.  */
5613     const reg_entry *reg;       /* Last register reference found.  */
5614     char *disp;                 /* Displacement string being built.  */
5615   };
5616
5617 static struct intel_parser_s intel_parser;
5618
5619 /* Token structure for parsing intel syntax.  */
5620 struct intel_token
5621   {
5622     int code;                   /* Token code.  */
5623     const reg_entry *reg;       /* Register entry for register tokens.  */
5624     char *str;                  /* String representation.  */
5625   };
5626
5627 static struct intel_token cur_token, prev_token;
5628
5629 /* Token codes for the intel parser. Since T_SHORT is already used
5630    by COFF, undefine it first to prevent a warning.  */
5631 #define T_NIL           -1
5632 #define T_CONST         1
5633 #define T_REG           2
5634 #define T_BYTE          3
5635 #define T_WORD          4
5636 #define T_DWORD         5
5637 #define T_FWORD         6
5638 #define T_QWORD         7
5639 #define T_TBYTE         8
5640 #define T_XMMWORD       9
5641 #undef  T_SHORT
5642 #define T_SHORT         10
5643 #define T_OFFSET        11
5644 #define T_PTR           12
5645 #define T_ID            13
5646 #define T_SHL           14
5647 #define T_SHR           15
5648
5649 /* Prototypes for intel parser functions.  */
5650 static int intel_match_token    PARAMS ((int code));
5651 static void intel_get_token     PARAMS ((void));
5652 static void intel_putback_token PARAMS ((void));
5653 static int intel_expr           PARAMS ((void));
5654 static int intel_e04            PARAMS ((void));
5655 static int intel_e04_1          PARAMS ((void));
5656 static int intel_e05            PARAMS ((void));
5657 static int intel_e05_1          PARAMS ((void));
5658 static int intel_e06            PARAMS ((void));
5659 static int intel_e06_1          PARAMS ((void));
5660 static int intel_e09            PARAMS ((void));
5661 static int intel_e09_1          PARAMS ((void));
5662 static int intel_e10            PARAMS ((void));
5663 static int intel_e10_1          PARAMS ((void));
5664 static int intel_e11            PARAMS ((void));
5665
5666 static int
5667 i386_intel_operand (operand_string, got_a_float)
5668      char *operand_string;
5669      int got_a_float;
5670 {
5671   int ret;
5672   char *p;
5673
5674   /* Initialize token holders.  */
5675   cur_token.code = prev_token.code = T_NIL;
5676   cur_token.reg = prev_token.reg = NULL;
5677   cur_token.str = prev_token.str = NULL;
5678
5679   /* Initialize parser structure.  */
5680   p = intel_parser.op_string = (char *) malloc (strlen (operand_string) + 1);
5681   if (p == NULL)
5682     abort ();
5683   strcpy (intel_parser.op_string, operand_string);
5684   intel_parser.got_a_float = got_a_float;
5685   intel_parser.op_modifier = -1;
5686   intel_parser.is_mem = 0;
5687   intel_parser.reg = NULL;
5688   intel_parser.disp = (char *) malloc (strlen (operand_string) + 1);
5689   if (intel_parser.disp == NULL)
5690     abort ();
5691   intel_parser.disp[0] = '\0';
5692
5693   /* Read the first token and start the parser.  */
5694   intel_get_token ();
5695   ret = intel_expr ();
5696
5697   if (ret)
5698     {
5699       if (cur_token.code != T_NIL)
5700         {
5701           as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
5702                   current_templates->start->name, cur_token.str);
5703           ret = 0;
5704         }
5705       /* If we found a memory reference, hand it over to i386_displacement
5706          to fill in the rest of the operand fields.  */
5707       else if (intel_parser.is_mem)
5708         {
5709           if ((i.mem_operands == 1
5710                && (current_templates->start->opcode_modifier & IsString) == 0)
5711               || i.mem_operands == 2)
5712             {
5713               as_bad (_("too many memory references for '%s'"),
5714                       current_templates->start->name);
5715               ret = 0;
5716             }
5717           else
5718             {
5719               char *s = intel_parser.disp;
5720               i.mem_operands++;
5721
5722               /* Add the displacement expression.  */
5723               if (*s != '\0')
5724                 ret = i386_displacement (s, s + strlen (s));
5725               if (ret)
5726                 ret = i386_index_check (operand_string);
5727             }
5728         }
5729
5730       /* Constant and OFFSET expressions are handled by i386_immediate.  */
5731       else if (intel_parser.op_modifier == T_OFFSET
5732                || intel_parser.reg == NULL)
5733         ret = i386_immediate (intel_parser.disp);
5734     }
5735
5736   free (p);
5737   free (intel_parser.disp);
5738
5739   return ret;
5740 }
5741
5742 /* expr SHORT e04
5743         | e04  */
5744 static int
5745 intel_expr ()
5746 {
5747   /* expr  SHORT e04  */
5748   if (cur_token.code == T_SHORT)
5749     {
5750       intel_parser.op_modifier = T_SHORT;
5751       intel_match_token (T_SHORT);
5752
5753       return (intel_e04 ());
5754     }
5755
5756   /* expr  e04  */
5757   else
5758     return intel_e04 ();
5759 }
5760
5761 /* e04  e06 e04'
5762
5763    e04' addOp e06 e04'
5764         | Empty  */
5765 static int
5766 intel_e04 ()
5767 {
5768   return (intel_e05 () && intel_e04_1 ());
5769 }
5770
5771 static int
5772 intel_e04_1 ()
5773 {
5774   /* e04'  addOp e05 e04'  */
5775   if (cur_token.code == '+' || cur_token.code == '-')
5776     {
5777       char str[2];
5778
5779       str[0] = cur_token.code;
5780       str[1] = 0;
5781       strcat (intel_parser.disp, str);
5782       intel_match_token (cur_token.code);
5783
5784       return (intel_e05 () && intel_e04_1 ());
5785     }
5786
5787   /* e04'  Empty  */
5788   else
5789     return 1;
5790 }
5791
5792 /* e05  e06 e05'
5793
5794    e05' binOp e06 e05'
5795         | Empty  */
5796 static int
5797 intel_e05 ()
5798 {
5799   return (intel_e06 () && intel_e05_1 ());
5800 }
5801
5802 static int
5803 intel_e05_1 ()
5804 {
5805   /* e05'  binOp e06 e05'  */
5806   if (cur_token.code == '&' || cur_token.code == '|' || cur_token.code == '^')
5807     {
5808       char str[2];
5809
5810       str[0] = cur_token.code;
5811       str[1] = 0;
5812       strcat (intel_parser.disp, str);
5813       intel_match_token (cur_token.code);
5814
5815       return (intel_e06 () && intel_e05_1 ());
5816     }
5817
5818   /* e05'  Empty  */
5819   else
5820     return 1;
5821 }
5822
5823 /* e06  e09 e06'
5824
5825    e06' mulOp e09 e06'
5826         | Empty  */
5827 static int
5828 intel_e06 ()
5829 {
5830   return (intel_e09 () && intel_e06_1 ());
5831 }
5832
5833 static int
5834 intel_e06_1 ()
5835 {
5836   /* e06'  mulOp e09 e06'  */
5837   if (cur_token.code == '*' || cur_token.code == '/' || cur_token.code == '%')
5838     {
5839       char str[2];
5840
5841       str[0] = cur_token.code;
5842       str[1] = 0;
5843       strcat (intel_parser.disp, str);
5844       intel_match_token (cur_token.code);
5845
5846       return (intel_e09 () && intel_e06_1 ());
5847     }
5848   else if (cur_token.code == T_SHL)
5849     {
5850       strcat (intel_parser.disp, "<<");
5851       intel_match_token (cur_token.code);
5852
5853       return (intel_e09 () && intel_e06_1 ());
5854     }
5855   else if (cur_token.code == T_SHR)
5856     {
5857       strcat (intel_parser.disp, ">>");
5858       intel_match_token (cur_token.code);
5859
5860       return (intel_e09 () && intel_e06_1 ());
5861     }
5862
5863   /* e06'  Empty  */
5864   else
5865     return 1;
5866 }
5867
5868 /* e09  OFFSET e10 e09'
5869         | e10 e09'
5870
5871    e09  ~ e10 e09'
5872         | NOT e10 e09'
5873         | e10 e09'
5874
5875    e09' PTR e10 e09'
5876         | : e10 e09'
5877         | Empty */
5878 static int
5879 intel_e09 ()
5880 {
5881   /* e09  OFFSET e10 e09'  */
5882   if (cur_token.code == T_OFFSET)
5883     {
5884       intel_parser.is_mem = 0;
5885       intel_parser.op_modifier = T_OFFSET;
5886       intel_match_token (T_OFFSET);
5887
5888       return (intel_e10 () && intel_e09_1 ());
5889     }
5890
5891   /* e09  NOT e10 e09'  */
5892   else if (cur_token.code == '~')
5893     {
5894       char str[2];
5895
5896       str[0] = cur_token.code;
5897       str[1] = 0;
5898       strcat (intel_parser.disp, str);
5899       intel_match_token (cur_token.code);
5900
5901       return (intel_e10 () && intel_e09_1 ());
5902     }
5903
5904   /* e09  e10 e09'  */
5905   else
5906     return (intel_e10 () && intel_e09_1 ());
5907 }
5908
5909 static int
5910 intel_e09_1 ()
5911 {
5912   /* e09'  PTR e10 e09' */
5913   if (cur_token.code == T_PTR)
5914     {
5915       char suffix;
5916
5917       if (prev_token.code == T_BYTE)
5918         suffix = BYTE_MNEM_SUFFIX;
5919
5920       else if (prev_token.code == T_WORD)
5921         {
5922           if (current_templates->start->name[0] == 'l'
5923               && current_templates->start->name[2] == 's'
5924               && current_templates->start->name[3] == 0)
5925             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
5926           else if (intel_parser.got_a_float == 2)       /* "fi..." */
5927             suffix = SHORT_MNEM_SUFFIX;
5928           else
5929             suffix = WORD_MNEM_SUFFIX;
5930         }
5931
5932       else if (prev_token.code == T_DWORD)
5933         {
5934           if (current_templates->start->name[0] == 'l'
5935               && current_templates->start->name[2] == 's'
5936               && current_templates->start->name[3] == 0)
5937             suffix = WORD_MNEM_SUFFIX;
5938           else if (flag_code == CODE_16BIT
5939                    && (current_templates->start->opcode_modifier
5940                        & (Jump|JumpDword|JumpInterSegment)))
5941             suffix = LONG_DOUBLE_MNEM_SUFFIX;
5942           else if (intel_parser.got_a_float == 1)       /* "f..." */
5943             suffix = SHORT_MNEM_SUFFIX;
5944           else
5945             suffix = LONG_MNEM_SUFFIX;
5946         }
5947
5948       else if (prev_token.code == T_FWORD)
5949         {
5950           if (current_templates->start->name[0] == 'l'
5951               && current_templates->start->name[2] == 's'
5952               && current_templates->start->name[3] == 0)
5953             suffix = LONG_MNEM_SUFFIX;
5954           else if (!intel_parser.got_a_float)
5955             {
5956               if (flag_code == CODE_16BIT)
5957                 add_prefix (DATA_PREFIX_OPCODE);
5958               suffix = LONG_DOUBLE_MNEM_SUFFIX;
5959             }
5960           else
5961             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
5962         }
5963
5964       else if (prev_token.code == T_QWORD)
5965         {
5966           if (intel_parser.got_a_float == 1)    /* "f..." */
5967             suffix = LONG_MNEM_SUFFIX;
5968           else
5969             suffix = QWORD_MNEM_SUFFIX;
5970         }
5971
5972       else if (prev_token.code == T_TBYTE)
5973         {
5974           if (intel_parser.got_a_float == 1)
5975             suffix = LONG_DOUBLE_MNEM_SUFFIX;
5976           else
5977             suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
5978         }
5979
5980       else if (prev_token.code == T_XMMWORD)
5981         {
5982           /* XXX ignored for now, but accepted since gcc uses it */
5983           suffix = 0;
5984         }
5985
5986       else
5987         {
5988           as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
5989           return 0;
5990         }
5991
5992       if (current_templates->start->base_opcode == 0x8d /* lea */)
5993         ;
5994       else if (!i.suffix)
5995         i.suffix = suffix;
5996       else if (i.suffix != suffix)
5997         {
5998           as_bad (_("Conflicting operand modifiers"));
5999           return 0;
6000         }
6001
6002       intel_match_token (T_PTR);
6003
6004       return (intel_e10 () && intel_e09_1 ());
6005     }
6006
6007   /* e09  : e10 e09'  */
6008   else if (cur_token.code == ':')
6009     {
6010       /* Mark as a memory operand only if it's not already known to be an
6011          offset expression.  */
6012       if (intel_parser.op_modifier != T_OFFSET)
6013         intel_parser.is_mem = 1;
6014
6015       return (intel_match_token (':') && intel_e10 () && intel_e09_1 ());
6016     }
6017
6018   /* e09'  Empty  */
6019   else
6020     return 1;
6021 }
6022
6023 /* e10  e11 e10'
6024
6025    e10' [ expr ] e10'
6026         | Empty  */
6027 static int
6028 intel_e10 ()
6029 {
6030   return (intel_e11 () && intel_e10_1 ());
6031 }
6032
6033 static int
6034 intel_e10_1 ()
6035 {
6036   /* e10'  [ expr ]  e10'  */
6037   if (cur_token.code == '[')
6038     {
6039       intel_match_token ('[');
6040
6041       /* Mark as a memory operand only if it's not already known to be an
6042          offset expression.  If it's an offset expression, we need to keep
6043          the brace in.  */
6044       if (intel_parser.op_modifier != T_OFFSET)
6045         intel_parser.is_mem = 1;
6046       else
6047         strcat (intel_parser.disp, "[");
6048
6049       /* Add a '+' to the displacement string if necessary.  */
6050       if (*intel_parser.disp != '\0'
6051           && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
6052         strcat (intel_parser.disp, "+");
6053
6054       if (intel_expr () && intel_match_token (']'))
6055         {
6056           /* Preserve brackets when the operand is an offset expression.  */
6057           if (intel_parser.op_modifier == T_OFFSET)
6058             strcat (intel_parser.disp, "]");
6059
6060           return intel_e10_1 ();
6061         }
6062       else
6063         return 0;
6064     }
6065
6066   /* e10'  Empty  */
6067   else
6068     return 1;
6069 }
6070
6071 /* e11  ( expr )
6072         | [ expr ]
6073         | BYTE
6074         | WORD
6075         | DWORD
6076         | FWORD
6077         | QWORD
6078         | TBYTE
6079         | OWORD
6080         | XMMWORD
6081         | $
6082         | .
6083         | register
6084         | id
6085         | constant  */
6086 static int
6087 intel_e11 ()
6088 {
6089   /* e11  ( expr ) */
6090   if (cur_token.code == '(')
6091     {
6092       intel_match_token ('(');
6093       strcat (intel_parser.disp, "(");
6094
6095       if (intel_expr () && intel_match_token (')'))
6096         {
6097           strcat (intel_parser.disp, ")");
6098           return 1;
6099         }
6100       else
6101         return 0;
6102     }
6103
6104   /* e11  [ expr ] */
6105   else if (cur_token.code == '[')
6106     {
6107       intel_match_token ('[');
6108
6109       /* Mark as a memory operand only if it's not already known to be an
6110          offset expression.  If it's an offset expression, we need to keep
6111          the brace in.  */
6112       if (intel_parser.op_modifier != T_OFFSET)
6113         intel_parser.is_mem = 1;
6114       else
6115         strcat (intel_parser.disp, "[");
6116
6117       /* Operands for jump/call inside brackets denote absolute addresses.  */
6118       if (current_templates->start->opcode_modifier
6119           & (Jump|JumpDword|JumpByte|JumpInterSegment))
6120         i.types[this_operand] |= JumpAbsolute;
6121
6122       /* Add a '+' to the displacement string if necessary.  */
6123       if (*intel_parser.disp != '\0'
6124           && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
6125         strcat (intel_parser.disp, "+");
6126
6127       if (intel_expr () && intel_match_token (']'))
6128         {
6129           /* Preserve brackets when the operand is an offset expression.  */
6130           if (intel_parser.op_modifier == T_OFFSET)
6131             strcat (intel_parser.disp, "]");
6132
6133           return 1;
6134         }
6135       else
6136         return 0;
6137     }
6138
6139   /* e11  BYTE
6140           | WORD
6141           | DWORD
6142           | FWORD
6143           | QWORD
6144           | TBYTE
6145           | OWORD
6146           | XMMWORD  */
6147   else if (cur_token.code == T_BYTE
6148            || cur_token.code == T_WORD
6149            || cur_token.code == T_DWORD
6150            || cur_token.code == T_FWORD
6151            || cur_token.code == T_QWORD
6152            || cur_token.code == T_TBYTE
6153            || cur_token.code == T_XMMWORD)
6154     {
6155       intel_match_token (cur_token.code);
6156
6157       if (cur_token.code != T_PTR)
6158         {
6159           /* It must have been an identifier; add it to the displacement string.  */
6160           strcat (intel_parser.disp, prev_token.str);
6161
6162           /* The identifier represents a memory reference only if it's not
6163              preceded by an offset modifier and if it's not an equate.  */
6164           if (intel_parser.op_modifier != T_OFFSET)
6165             {
6166               symbolS *symbolP;
6167
6168               symbolP = symbol_find(prev_token.str);
6169               if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
6170                 intel_parser.is_mem = 1;
6171             }
6172         }
6173
6174       return 1;
6175     }
6176
6177   /* e11  $
6178           | .  */
6179   else if (cur_token.code == '.')
6180     {
6181       strcat (intel_parser.disp, cur_token.str);
6182       intel_match_token (cur_token.code);
6183
6184       /* Mark as a memory operand only if it's not already known to be an
6185          offset expression.  */
6186       if (intel_parser.op_modifier != T_OFFSET)
6187         intel_parser.is_mem = 1;
6188
6189       return 1;
6190     }
6191
6192   /* e11  register  */
6193   else if (cur_token.code == T_REG)
6194     {
6195       const reg_entry *reg = intel_parser.reg = cur_token.reg;
6196
6197       intel_match_token (T_REG);
6198
6199       /* Check for segment change.  */
6200       if (cur_token.code == ':')
6201         {
6202           if (reg->reg_type & (SReg2 | SReg3))
6203             {
6204               switch (reg->reg_num)
6205                 {
6206                 case 0:
6207                   i.seg[i.mem_operands] = &es;
6208                   break;
6209                 case 1:
6210                   i.seg[i.mem_operands] = &cs;
6211                   break;
6212                 case 2:
6213                   i.seg[i.mem_operands] = &ss;
6214                   break;
6215                 case 3:
6216                   i.seg[i.mem_operands] = &ds;
6217                   break;
6218                 case 4:
6219                   i.seg[i.mem_operands] = &fs;
6220                   break;
6221                 case 5:
6222                   i.seg[i.mem_operands] = &gs;
6223                   break;
6224                 }
6225             }
6226           else
6227             {
6228               as_bad (_("`%s' is not a valid segment register"), reg->reg_name);
6229               return 0;
6230             }
6231         }
6232
6233       /* Not a segment register. Check for register scaling.  */
6234       else if (cur_token.code == '*')
6235         {
6236           if (!intel_parser.is_mem)
6237             {
6238               as_bad (_("Register scaling only allowed in memory operands."));
6239               return 0;
6240             }
6241
6242           /* What follows must be a valid scale.  */
6243           if (intel_match_token ('*')
6244               && strchr ("01248", *cur_token.str))
6245             {
6246               i.index_reg = reg;
6247               i.types[this_operand] |= BaseIndex;
6248
6249               /* Set the scale after setting the register (otherwise,
6250                  i386_scale will complain)  */
6251               i386_scale (cur_token.str);
6252               intel_match_token (T_CONST);
6253             }
6254           else
6255             {
6256               as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
6257                       cur_token.str);
6258               return 0;
6259             }
6260         }
6261
6262       /* No scaling. If this is a memory operand, the register is either a
6263          base register (first occurrence) or an index register (second
6264          occurrence).  */
6265       else if (intel_parser.is_mem && !(reg->reg_type & (SReg2 | SReg3)))
6266         {
6267           if (i.base_reg && i.index_reg)
6268             {
6269               as_bad (_("Too many register references in memory operand."));
6270               return 0;
6271             }
6272
6273           if (i.base_reg == NULL)
6274             i.base_reg = reg;
6275           else
6276             i.index_reg = reg;
6277
6278           i.types[this_operand] |= BaseIndex;
6279         }
6280
6281       /* Offset modifier. Add the register to the displacement string to be
6282          parsed as an immediate expression after we're done.  */
6283       else if (intel_parser.op_modifier == T_OFFSET)
6284         strcat (intel_parser.disp, reg->reg_name);
6285
6286       /* It's neither base nor index nor offset.  */
6287       else
6288         {
6289           i.types[this_operand] |= reg->reg_type & ~BaseIndex;
6290           i.op[this_operand].regs = reg;
6291           i.reg_operands++;
6292         }
6293
6294       /* Since registers are not part of the displacement string (except
6295          when we're parsing offset operands), we may need to remove any
6296          preceding '+' from the displacement string.  */
6297       if (*intel_parser.disp != '\0'
6298           && intel_parser.op_modifier != T_OFFSET)
6299         {
6300           char *s = intel_parser.disp;
6301           s += strlen (s) - 1;
6302           if (*s == '+')
6303             *s = '\0';
6304         }
6305
6306       return 1;
6307     }
6308
6309   /* e11  id  */
6310   else if (cur_token.code == T_ID)
6311     {
6312       /* Add the identifier to the displacement string.  */
6313       strcat (intel_parser.disp, cur_token.str);
6314
6315       /* The identifier represents a memory reference only if it's not
6316          preceded by an offset modifier and if it's not an equate.  */
6317       if (intel_parser.op_modifier != T_OFFSET)
6318         {
6319           symbolS *symbolP;
6320
6321           symbolP = symbol_find(cur_token.str);
6322           if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
6323             intel_parser.is_mem = 1;
6324         }
6325
6326       intel_match_token (T_ID);
6327       return 1;
6328     }
6329
6330   /* e11  constant  */
6331   else if (cur_token.code == T_CONST
6332            || cur_token.code == '-'
6333            || cur_token.code == '+')
6334     {
6335       char *save_str;
6336
6337       /* Allow constants that start with `+' or `-'.  */
6338       if (cur_token.code == '-' || cur_token.code == '+')
6339         {
6340           strcat (intel_parser.disp, cur_token.str);
6341           intel_match_token (cur_token.code);
6342           if (cur_token.code != T_CONST)
6343             {
6344               as_bad (_("Syntax error. Expecting a constant. Got `%s'."),
6345                       cur_token.str);
6346               return 0;
6347             }
6348         }
6349
6350       save_str = (char *) malloc (strlen (cur_token.str) + 1);
6351       if (save_str == NULL)
6352         abort ();
6353       strcpy (save_str, cur_token.str);
6354
6355       /* Get the next token to check for register scaling.  */
6356       intel_match_token (cur_token.code);
6357
6358       /* Check if this constant is a scaling factor for an index register.  */
6359       if (cur_token.code == '*')
6360         {
6361           if (intel_match_token ('*') && cur_token.code == T_REG)
6362             {
6363               if (!intel_parser.is_mem)
6364                 {
6365                   as_bad (_("Register scaling only allowed in memory operands."));
6366                   return 0;
6367                 }
6368
6369               /* The constant is followed by `* reg', so it must be
6370                  a valid scale.  */
6371               if (strchr ("01248", *save_str))
6372                 {
6373                   i.index_reg = cur_token.reg;
6374                   i.types[this_operand] |= BaseIndex;
6375
6376                   /* Set the scale after setting the register (otherwise,
6377                      i386_scale will complain)  */
6378                   i386_scale (save_str);
6379                   intel_match_token (T_REG);
6380
6381                   /* Since registers are not part of the displacement
6382                      string, we may need to remove any preceding '+' from
6383                      the displacement string.  */
6384                   if (*intel_parser.disp != '\0')
6385                     {
6386                       char *s = intel_parser.disp;
6387                       s += strlen (s) - 1;
6388                       if (*s == '+')
6389                         *s = '\0';
6390                     }
6391
6392                   free (save_str);
6393
6394                   return 1;
6395                 }
6396               else
6397                 return 0;
6398             }
6399
6400           /* The constant was not used for register scaling. Since we have
6401              already consumed the token following `*' we now need to put it
6402              back in the stream.  */
6403           else
6404             intel_putback_token ();
6405         }
6406
6407       /* Add the constant to the displacement string.  */
6408       strcat (intel_parser.disp, save_str);
6409       free (save_str);
6410
6411       return 1;
6412     }
6413
6414   as_bad (_("Unrecognized token '%s'"), cur_token.str);
6415   return 0;
6416 }
6417
6418 /* Match the given token against cur_token. If they match, read the next
6419    token from the operand string.  */
6420 static int
6421 intel_match_token (code)
6422      int code;
6423 {
6424   if (cur_token.code == code)
6425     {
6426       intel_get_token ();
6427       return 1;
6428     }
6429   else
6430     {
6431       as_bad (_("Unexpected token `%s'"), cur_token.str);
6432       return 0;
6433     }
6434 }
6435
6436 /* Read a new token from intel_parser.op_string and store it in cur_token.  */
6437 static void
6438 intel_get_token ()
6439 {
6440   char *end_op;
6441   const reg_entry *reg;
6442   struct intel_token new_token;
6443
6444   new_token.code = T_NIL;
6445   new_token.reg = NULL;
6446   new_token.str = NULL;
6447
6448   /* Free the memory allocated to the previous token and move
6449      cur_token to prev_token.  */
6450   if (prev_token.str)
6451     free (prev_token.str);
6452
6453   prev_token = cur_token;
6454
6455   /* Skip whitespace.  */
6456   while (is_space_char (*intel_parser.op_string))
6457     intel_parser.op_string++;
6458
6459   /* Return an empty token if we find nothing else on the line.  */
6460   if (*intel_parser.op_string == '\0')
6461     {
6462       cur_token = new_token;
6463       return;
6464     }
6465
6466   /* The new token cannot be larger than the remainder of the operand
6467      string.  */
6468   new_token.str = (char *) malloc (strlen (intel_parser.op_string) + 1);
6469   if (new_token.str == NULL)
6470     abort ();
6471   new_token.str[0] = '\0';
6472
6473   if (strchr ("0123456789", *intel_parser.op_string))
6474     {
6475       char *p = new_token.str;
6476       char *q = intel_parser.op_string;
6477       new_token.code = T_CONST;
6478
6479       /* Allow any kind of identifier char to encompass floating point and
6480          hexadecimal numbers.  */
6481       while (is_identifier_char (*q))
6482         *p++ = *q++;
6483       *p = '\0';
6484
6485       /* Recognize special symbol names [0-9][bf].  */
6486       if (strlen (intel_parser.op_string) == 2
6487           && (intel_parser.op_string[1] == 'b'
6488               || intel_parser.op_string[1] == 'f'))
6489         new_token.code = T_ID;
6490     }
6491
6492   else if ((*intel_parser.op_string == REGISTER_PREFIX || allow_naked_reg)
6493            && ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL))
6494     {
6495       new_token.code = T_REG;
6496       new_token.reg = reg;
6497
6498       if (*intel_parser.op_string == REGISTER_PREFIX)
6499         {
6500           new_token.str[0] = REGISTER_PREFIX;
6501           new_token.str[1] = '\0';
6502         }
6503
6504       strcat (new_token.str, reg->reg_name);
6505     }
6506
6507   else if (is_identifier_char (*intel_parser.op_string))
6508     {
6509       char *p = new_token.str;
6510       char *q = intel_parser.op_string;
6511
6512       /* A '.' or '$' followed by an identifier char is an identifier.
6513          Otherwise, it's operator '.' followed by an expression.  */
6514       if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
6515         {
6516           new_token.code = '.';
6517           new_token.str[0] = '.';
6518           new_token.str[1] = '\0';
6519         }
6520       else
6521         {
6522           while (is_identifier_char (*q) || *q == '@')
6523             *p++ = *q++;
6524           *p = '\0';
6525
6526           if (strcasecmp (new_token.str, "NOT") == 0)
6527             new_token.code = '~';
6528
6529           else if (strcasecmp (new_token.str, "MOD") == 0)
6530             new_token.code = '%';
6531
6532           else if (strcasecmp (new_token.str, "AND") == 0)
6533             new_token.code = '&';
6534
6535           else if (strcasecmp (new_token.str, "OR") == 0)
6536             new_token.code = '|';
6537
6538           else if (strcasecmp (new_token.str, "XOR") == 0)
6539             new_token.code = '^';
6540
6541           else if (strcasecmp (new_token.str, "SHL") == 0)
6542             new_token.code = T_SHL;
6543
6544           else if (strcasecmp (new_token.str, "SHR") == 0)
6545             new_token.code = T_SHR;
6546
6547           else if (strcasecmp (new_token.str, "BYTE") == 0)
6548             new_token.code = T_BYTE;
6549
6550           else if (strcasecmp (new_token.str, "WORD") == 0)
6551             new_token.code = T_WORD;
6552
6553           else if (strcasecmp (new_token.str, "DWORD") == 0)
6554             new_token.code = T_DWORD;
6555
6556           else if (strcasecmp (new_token.str, "FWORD") == 0)
6557             new_token.code = T_FWORD;
6558
6559           else if (strcasecmp (new_token.str, "QWORD") == 0)
6560             new_token.code = T_QWORD;
6561
6562           else if (strcasecmp (new_token.str, "TBYTE") == 0
6563                    /* XXX remove (gcc still uses it) */
6564                    || strcasecmp (new_token.str, "XWORD") == 0)
6565             new_token.code = T_TBYTE;
6566
6567           else if (strcasecmp (new_token.str, "XMMWORD") == 0
6568                    || strcasecmp (new_token.str, "OWORD") == 0)
6569             new_token.code = T_XMMWORD;
6570
6571           else if (strcasecmp (new_token.str, "PTR") == 0)
6572             new_token.code = T_PTR;
6573
6574           else if (strcasecmp (new_token.str, "SHORT") == 0)
6575             new_token.code = T_SHORT;
6576
6577           else if (strcasecmp (new_token.str, "OFFSET") == 0)
6578             {
6579               new_token.code = T_OFFSET;
6580
6581               /* ??? This is not mentioned in the MASM grammar but gcc
6582                      makes use of it with -mintel-syntax.  OFFSET may be
6583                      followed by FLAT:  */
6584               if (strncasecmp (q, " FLAT:", 6) == 0)
6585                 strcat (new_token.str, " FLAT:");
6586             }
6587
6588           /* ??? This is not mentioned in the MASM grammar.  */
6589           else if (strcasecmp (new_token.str, "FLAT") == 0)
6590             new_token.code = T_OFFSET;
6591
6592           else
6593             new_token.code = T_ID;
6594         }
6595     }
6596
6597   else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
6598     {
6599       new_token.code = *intel_parser.op_string;
6600       new_token.str[0] = *intel_parser.op_string;
6601       new_token.str[1] = '\0';
6602     }
6603
6604   else if (strchr ("<>", *intel_parser.op_string)
6605            && *intel_parser.op_string == *(intel_parser.op_string + 1))
6606     {
6607       new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
6608       new_token.str[0] = *intel_parser.op_string;
6609       new_token.str[1] = *intel_parser.op_string;
6610       new_token.str[2] = '\0';
6611     }
6612
6613   else
6614     as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
6615
6616   intel_parser.op_string += strlen (new_token.str);
6617   cur_token = new_token;
6618 }
6619
6620 /* Put cur_token back into the token stream and make cur_token point to
6621    prev_token.  */
6622 static void
6623 intel_putback_token ()
6624 {
6625   intel_parser.op_string -= strlen (cur_token.str);
6626   free (cur_token.str);
6627   cur_token = prev_token;
6628
6629   /* Forget prev_token.  */
6630   prev_token.code = T_NIL;
6631   prev_token.reg = NULL;
6632   prev_token.str = NULL;
6633 }
6634
6635 int
6636 tc_x86_regname_to_dw2regnum (const char *regname)
6637 {
6638   unsigned int regnum;
6639   unsigned int regnames_count;
6640   char *regnames_32[] =
6641     {
6642       "eax", "ecx", "edx", "ebx",
6643       "esp", "ebp", "esi", "edi",
6644       "eip"
6645     };
6646   char *regnames_64[] =
6647     {
6648       "rax", "rbx", "rcx", "rdx",
6649       "rdi", "rsi", "rbp", "rsp",
6650       "r8", "r9", "r10", "r11",
6651       "r12", "r13", "r14", "r15",
6652       "rip"
6653     };
6654   char **regnames;
6655
6656   if (flag_code == CODE_64BIT)
6657     {
6658       regnames = regnames_64;
6659       regnames_count = ARRAY_SIZE (regnames_64);
6660     }
6661   else
6662     {
6663       regnames = regnames_32;
6664       regnames_count = ARRAY_SIZE (regnames_32);
6665     }
6666
6667   for (regnum = 0; regnum < regnames_count; regnum++)
6668     if (strcmp (regname, regnames[regnum]) == 0)
6669       return regnum;
6670
6671   return -1;
6672 }
6673
6674 void
6675 tc_x86_frame_initial_instructions (void)
6676 {
6677   static unsigned int sp_regno;
6678
6679   if (!sp_regno)
6680     sp_regno = tc_x86_regname_to_dw2regnum (flag_code == CODE_64BIT
6681                                             ? "rsp" : "esp");
6682
6683   cfi_add_CFA_def_cfa (sp_regno, -x86_cie_data_alignment);
6684   cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
6685 }
6686
6687 int
6688 i386_elf_section_type (const char *str, size_t len)
6689 {
6690   if (flag_code == CODE_64BIT
6691       && len == sizeof ("unwind") - 1
6692       && strncmp (str, "unwind", 6) == 0)
6693     return SHT_X86_64_UNWIND;
6694
6695   return -1;
6696 }
6697
6698 #ifdef TE_PE
6699 void
6700 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
6701 {
6702   expressionS expr;
6703
6704   expr.X_op = O_secrel;
6705   expr.X_add_symbol = symbol;
6706   expr.X_add_number = 0;
6707   emit_expr (&expr, size);
6708 }
6709 #endif