Check VEXW1 for 2-byte VEX prefix.
[external/binutils.git] / gas / config / tc-i386.c
1 /* tc-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, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
21    02110-1301, 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 "elf/x86-64.h"
36 #include "opcodes/i386-init.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 DEFAULT_ARCH
47 #define DEFAULT_ARCH "i386"
48 #endif
49
50 #ifndef INLINE
51 #if __GNUC__ >= 2
52 #define INLINE __inline__
53 #else
54 #define INLINE
55 #endif
56 #endif
57
58 /* Prefixes will be emitted in the order defined below.
59    WAIT_PREFIX must be the first prefix since FWAIT is really is an
60    instruction, and so must come before any prefixes.
61    The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
62    REP_PREFIX, LOCK_PREFIX.  */
63 #define WAIT_PREFIX     0
64 #define SEG_PREFIX      1
65 #define ADDR_PREFIX     2
66 #define DATA_PREFIX     3
67 #define REP_PREFIX      4
68 #define LOCK_PREFIX     5
69 #define REX_PREFIX      6       /* must come last.  */
70 #define MAX_PREFIXES    7       /* max prefixes per opcode */
71
72 /* we define the syntax here (modulo base,index,scale syntax) */
73 #define REGISTER_PREFIX '%'
74 #define IMMEDIATE_PREFIX '$'
75 #define ABSOLUTE_PREFIX '*'
76
77 /* these are the instruction mnemonic suffixes in AT&T syntax or
78    memory operand size in Intel syntax.  */
79 #define WORD_MNEM_SUFFIX  'w'
80 #define BYTE_MNEM_SUFFIX  'b'
81 #define SHORT_MNEM_SUFFIX 's'
82 #define LONG_MNEM_SUFFIX  'l'
83 #define QWORD_MNEM_SUFFIX  'q'
84 #define XMMWORD_MNEM_SUFFIX  'x'
85 #define YMMWORD_MNEM_SUFFIX 'y'
86 /* Intel Syntax.  Use a non-ascii letter since since it never appears
87    in instructions.  */
88 #define LONG_DOUBLE_MNEM_SUFFIX '\1'
89
90 #define END_OF_INSN '\0'
91
92 /*
93   'templates' is for grouping together 'template' structures for opcodes
94   of the same name.  This is only used for storing the insns in the grand
95   ole hash table of insns.
96   The templates themselves start at START and range up to (but not including)
97   END.
98   */
99 typedef struct
100 {
101   const insn_template *start;
102   const insn_template *end;
103 }
104 templates;
105
106 /* 386 operand encoding bytes:  see 386 book for details of this.  */
107 typedef struct
108 {
109   unsigned int regmem;  /* codes register or memory operand */
110   unsigned int reg;     /* codes register operand (or extended opcode) */
111   unsigned int mode;    /* how to interpret regmem & reg */
112 }
113 modrm_byte;
114
115 /* x86-64 extension prefix.  */
116 typedef int rex_byte;
117
118 /* 386 opcode byte to code indirect addressing.  */
119 typedef struct
120 {
121   unsigned base;
122   unsigned index;
123   unsigned scale;
124 }
125 sib_byte;
126
127 /* x86 arch names, types and features */
128 typedef struct
129 {
130   const char *name;             /* arch name */
131   unsigned int len;             /* arch string length */
132   enum processor_type type;     /* arch type */
133   i386_cpu_flags flags;         /* cpu feature flags */
134   unsigned int skip;            /* show_arch should skip this. */
135   unsigned int negated;         /* turn off indicated flags.  */
136 }
137 arch_entry;
138
139 static void update_code_flag (int, int);
140 static void set_code_flag (int);
141 static void set_16bit_gcc_code_flag (int);
142 static void set_intel_syntax (int);
143 static void set_intel_mnemonic (int);
144 static void set_allow_index_reg (int);
145 static void set_sse_check (int);
146 static void set_cpu_arch (int);
147 #ifdef TE_PE
148 static void pe_directive_secrel (int);
149 #endif
150 static void signed_cons (int);
151 static char *output_invalid (int c);
152 static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
153                                     const char *);
154 static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
155                                        const char *);
156 static int i386_att_operand (char *);
157 static int i386_intel_operand (char *, int);
158 static int i386_intel_simplify (expressionS *);
159 static int i386_intel_parse_name (const char *, expressionS *);
160 static const reg_entry *parse_register (char *, char **);
161 static char *parse_insn (char *, char *);
162 static char *parse_operands (char *, const char *);
163 static void swap_operands (void);
164 static void swap_2_operands (int, int);
165 static void optimize_imm (void);
166 static void optimize_disp (void);
167 static const insn_template *match_template (void);
168 static int check_string (void);
169 static int process_suffix (void);
170 static int check_byte_reg (void);
171 static int check_long_reg (void);
172 static int check_qword_reg (void);
173 static int check_word_reg (void);
174 static int finalize_imm (void);
175 static int process_operands (void);
176 static const seg_entry *build_modrm_byte (void);
177 static void output_insn (void);
178 static void output_imm (fragS *, offsetT);
179 static void output_disp (fragS *, offsetT);
180 #ifndef I386COFF
181 static void s_bss (int);
182 #endif
183 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
184 static void handle_large_common (int small ATTRIBUTE_UNUSED);
185 #endif
186
187 static const char *default_arch = DEFAULT_ARCH;
188
189 /* VEX prefix.  */
190 typedef struct
191 {
192   /* VEX prefix is either 2 byte or 3 byte.  */
193   unsigned char bytes[3];
194   unsigned int length;
195   /* Destination or source register specifier.  */
196   const reg_entry *register_specifier;
197 } vex_prefix;
198
199 /* 'md_assemble ()' gathers together information and puts it into a
200    i386_insn.  */
201
202 union i386_op
203   {
204     expressionS *disps;
205     expressionS *imms;
206     const reg_entry *regs;
207   };
208
209 enum i386_error
210   {
211     operand_size_mismatch,
212     operand_type_mismatch,
213     register_type_mismatch,
214     number_of_operands_mismatch,
215     invalid_instruction_suffix,
216     bad_imm4,
217     old_gcc_only,
218     unsupported_with_intel_mnemonic,
219     unsupported_syntax,
220     unsupported
221   };
222
223 struct _i386_insn
224   {
225     /* TM holds the template for the insn were currently assembling.  */
226     insn_template tm;
227
228     /* SUFFIX holds the instruction size suffix for byte, word, dword
229        or qword, if given.  */
230     char suffix;
231
232     /* OPERANDS gives the number of given operands.  */
233     unsigned int operands;
234
235     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
236        of given register, displacement, memory operands and immediate
237        operands.  */
238     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
239
240     /* TYPES [i] is the type (see above #defines) which tells us how to
241        use OP[i] for the corresponding operand.  */
242     i386_operand_type types[MAX_OPERANDS];
243
244     /* Displacement expression, immediate expression, or register for each
245        operand.  */
246     union i386_op op[MAX_OPERANDS];
247
248     /* Flags for operands.  */
249     unsigned int flags[MAX_OPERANDS];
250 #define Operand_PCrel 1
251
252     /* Relocation type for operand */
253     enum bfd_reloc_code_real reloc[MAX_OPERANDS];
254
255     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
256        the base index byte below.  */
257     const reg_entry *base_reg;
258     const reg_entry *index_reg;
259     unsigned int log2_scale_factor;
260
261     /* SEG gives the seg_entries of this insn.  They are zero unless
262        explicit segment overrides are given.  */
263     const seg_entry *seg[2];
264
265     /* PREFIX holds all the given prefix opcodes (usually null).
266        PREFIXES is the number of prefix opcodes.  */
267     unsigned int prefixes;
268     unsigned char prefix[MAX_PREFIXES];
269
270     /* RM and SIB are the modrm byte and the sib byte where the
271        addressing modes of this insn are encoded.  */
272     modrm_byte rm;
273     rex_byte rex;
274     sib_byte sib;
275     vex_prefix vex;
276
277     /* Swap operand in encoding.  */
278     unsigned int swap_operand;
279
280     /* Error message.  */
281     enum i386_error error;
282   };
283
284 typedef struct _i386_insn i386_insn;
285
286 /* List of chars besides those in app.c:symbol_chars that can start an
287    operand.  Used to prevent the scrubber eating vital white-space.  */
288 const char extra_symbol_chars[] = "*%-(["
289 #ifdef LEX_AT
290         "@"
291 #endif
292 #ifdef LEX_QM
293         "?"
294 #endif
295         ;
296
297 #if (defined (TE_I386AIX)                               \
298      || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
299          && !defined (TE_GNU)                           \
300          && !defined (TE_LINUX)                         \
301          && !defined (TE_NETWARE)                       \
302          && !defined (TE_FreeBSD)                       \
303          && !defined (TE_NetBSD)))
304 /* This array holds the chars that always start a comment.  If the
305    pre-processor is disabled, these aren't very useful.  The option
306    --divide will remove '/' from this list.  */
307 const char *i386_comment_chars = "#/";
308 #define SVR4_COMMENT_CHARS 1
309 #define PREFIX_SEPARATOR '\\'
310
311 #else
312 const char *i386_comment_chars = "#";
313 #define PREFIX_SEPARATOR '/'
314 #endif
315
316 /* This array holds the chars that only start a comment at the beginning of
317    a line.  If the line seems to have the form '# 123 filename'
318    .line and .file directives will appear in the pre-processed output.
319    Note that input_file.c hand checks for '#' at the beginning of the
320    first line of the input file.  This is because the compiler outputs
321    #NO_APP at the beginning of its output.
322    Also note that comments started like this one will always work if
323    '/' isn't otherwise defined.  */
324 const char line_comment_chars[] = "#/";
325
326 const char line_separator_chars[] = ";";
327
328 /* Chars that can be used to separate mant from exp in floating point
329    nums.  */
330 const char EXP_CHARS[] = "eE";
331
332 /* Chars that mean this number is a floating point constant
333    As in 0f12.456
334    or    0d1.2345e12.  */
335 const char FLT_CHARS[] = "fFdDxX";
336
337 /* Tables for lexical analysis.  */
338 static char mnemonic_chars[256];
339 static char register_chars[256];
340 static char operand_chars[256];
341 static char identifier_chars[256];
342 static char digit_chars[256];
343
344 /* Lexical macros.  */
345 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
346 #define is_operand_char(x) (operand_chars[(unsigned char) x])
347 #define is_register_char(x) (register_chars[(unsigned char) x])
348 #define is_space_char(x) ((x) == ' ')
349 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
350 #define is_digit_char(x) (digit_chars[(unsigned char) x])
351
352 /* All non-digit non-letter characters that may occur in an operand.  */
353 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
354
355 /* md_assemble() always leaves the strings it's passed unaltered.  To
356    effect this we maintain a stack of saved characters that we've smashed
357    with '\0's (indicating end of strings for various sub-fields of the
358    assembler instruction).  */
359 static char save_stack[32];
360 static char *save_stack_p;
361 #define END_STRING_AND_SAVE(s) \
362         do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
363 #define RESTORE_END_STRING(s) \
364         do { *(s) = *--save_stack_p; } while (0)
365
366 /* The instruction we're assembling.  */
367 static i386_insn i;
368
369 /* Possible templates for current insn.  */
370 static const templates *current_templates;
371
372 /* Per instruction expressionS buffers: max displacements & immediates.  */
373 static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
374 static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
375
376 /* Current operand we are working on.  */
377 static int this_operand = -1;
378
379 /* We support four different modes.  FLAG_CODE variable is used to distinguish
380    these.  */
381
382 enum flag_code {
383         CODE_32BIT,
384         CODE_16BIT,
385         CODE_64BIT };
386
387 static enum flag_code flag_code;
388 static unsigned int object_64bit;
389 static int use_rela_relocations = 0;
390
391 /* The names used to print error messages.  */
392 static const char *flag_code_names[] =
393   {
394     "32",
395     "16",
396     "64"
397   };
398
399 /* 1 for intel syntax,
400    0 if att syntax.  */
401 static int intel_syntax = 0;
402
403 /* 1 for intel mnemonic,
404    0 if att mnemonic.  */
405 static int intel_mnemonic = !SYSV386_COMPAT;
406
407 /* 1 if support old (<= 2.8.1) versions of gcc.  */
408 static int old_gcc = OLDGCC_COMPAT;
409
410 /* 1 if pseudo registers are permitted.  */
411 static int allow_pseudo_reg = 0;
412
413 /* 1 if register prefix % not required.  */
414 static int allow_naked_reg = 0;
415
416 /* 1 if pseudo index register, eiz/riz, is allowed .  */
417 static int allow_index_reg = 0;
418
419 static enum
420   {
421     sse_check_none = 0,
422     sse_check_warning,
423     sse_check_error
424   }
425 sse_check;
426
427 /* Register prefix used for error message.  */
428 static const char *register_prefix = "%";
429
430 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
431    leave, push, and pop instructions so that gcc has the same stack
432    frame as in 32 bit mode.  */
433 static char stackop_size = '\0';
434
435 /* Non-zero to optimize code alignment.  */
436 int optimize_align_code = 1;
437
438 /* Non-zero to quieten some warnings.  */
439 static int quiet_warnings = 0;
440
441 /* CPU name.  */
442 static const char *cpu_arch_name = NULL;
443 static char *cpu_sub_arch_name = NULL;
444
445 /* CPU feature flags.  */
446 static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
447
448 /* If we have selected a cpu we are generating instructions for.  */
449 static int cpu_arch_tune_set = 0;
450
451 /* Cpu we are generating instructions for.  */
452 enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
453
454 /* CPU feature flags of cpu we are generating instructions for.  */
455 static i386_cpu_flags cpu_arch_tune_flags;
456
457 /* CPU instruction set architecture used.  */
458 enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
459
460 /* CPU feature flags of instruction set architecture used.  */
461 i386_cpu_flags cpu_arch_isa_flags;
462
463 /* If set, conditional jumps are not automatically promoted to handle
464    larger than a byte offset.  */
465 static unsigned int no_cond_jump_promotion = 0;
466
467 /* Encode SSE instructions with VEX prefix.  */
468 static unsigned int sse2avx;
469
470 /* Encode scalar AVX instructions with specific vector length.  */
471 static enum
472   {
473     vex128 = 0,
474     vex256
475   } avxscalar;
476
477 /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
478 static symbolS *GOT_symbol;
479
480 /* The dwarf2 return column, adjusted for 32 or 64 bit.  */
481 unsigned int x86_dwarf2_return_column;
482
483 /* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
484 int x86_cie_data_alignment;
485
486 /* Interface to relax_segment.
487    There are 3 major relax states for 386 jump insns because the
488    different types of jumps add different sizes to frags when we're
489    figuring out what sort of jump to choose to reach a given label.  */
490
491 /* Types.  */
492 #define UNCOND_JUMP 0
493 #define COND_JUMP 1
494 #define COND_JUMP86 2
495
496 /* Sizes.  */
497 #define CODE16  1
498 #define SMALL   0
499 #define SMALL16 (SMALL | CODE16)
500 #define BIG     2
501 #define BIG16   (BIG | CODE16)
502
503 #ifndef INLINE
504 #ifdef __GNUC__
505 #define INLINE __inline__
506 #else
507 #define INLINE
508 #endif
509 #endif
510
511 #define ENCODE_RELAX_STATE(type, size) \
512   ((relax_substateT) (((type) << 2) | (size)))
513 #define TYPE_FROM_RELAX_STATE(s) \
514   ((s) >> 2)
515 #define DISP_SIZE_FROM_RELAX_STATE(s) \
516     ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
517
518 /* This table is used by relax_frag to promote short jumps to long
519    ones where necessary.  SMALL (short) jumps may be promoted to BIG
520    (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
521    don't allow a short jump in a 32 bit code segment to be promoted to
522    a 16 bit offset jump because it's slower (requires data size
523    prefix), and doesn't work, unless the destination is in the bottom
524    64k of the code segment (The top 16 bits of eip are zeroed).  */
525
526 const relax_typeS md_relax_table[] =
527 {
528   /* The fields are:
529      1) most positive reach of this state,
530      2) most negative reach of this state,
531      3) how many bytes this mode will have in the variable part of the frag
532      4) which index into the table to try if we can't fit into this one.  */
533
534   /* UNCOND_JUMP states.  */
535   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
536   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
537   /* dword jmp adds 4 bytes to frag:
538      0 extra opcode bytes, 4 displacement bytes.  */
539   {0, 0, 4, 0},
540   /* word jmp adds 2 byte2 to frag:
541      0 extra opcode bytes, 2 displacement bytes.  */
542   {0, 0, 2, 0},
543
544   /* COND_JUMP states.  */
545   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
546   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
547   /* dword conditionals adds 5 bytes to frag:
548      1 extra opcode byte, 4 displacement bytes.  */
549   {0, 0, 5, 0},
550   /* word conditionals add 3 bytes to frag:
551      1 extra opcode byte, 2 displacement bytes.  */
552   {0, 0, 3, 0},
553
554   /* COND_JUMP86 states.  */
555   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
556   {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
557   /* dword conditionals adds 5 bytes to frag:
558      1 extra opcode byte, 4 displacement bytes.  */
559   {0, 0, 5, 0},
560   /* word conditionals add 4 bytes to frag:
561      1 displacement byte and a 3 byte long branch insn.  */
562   {0, 0, 4, 0}
563 };
564
565 static const arch_entry cpu_arch[] =
566 {
567   /* Do not replace the first two entries - i386_target_format()
568      relies on them being there in this order.  */
569   { STRING_COMMA_LEN ("generic32"), PROCESSOR_GENERIC32,
570     CPU_GENERIC32_FLAGS, 0, 0 },
571   { STRING_COMMA_LEN ("generic64"), PROCESSOR_GENERIC64,
572     CPU_GENERIC64_FLAGS, 0, 0 },
573   { STRING_COMMA_LEN ("i8086"), PROCESSOR_UNKNOWN,
574     CPU_NONE_FLAGS, 0, 0 },
575   { STRING_COMMA_LEN ("i186"), PROCESSOR_UNKNOWN,
576     CPU_I186_FLAGS, 0, 0 },
577   { STRING_COMMA_LEN ("i286"), PROCESSOR_UNKNOWN,
578     CPU_I286_FLAGS, 0, 0 },
579   { STRING_COMMA_LEN ("i386"), PROCESSOR_I386,
580     CPU_I386_FLAGS, 0, 0 },
581   { STRING_COMMA_LEN ("i486"), PROCESSOR_I486,
582     CPU_I486_FLAGS, 0, 0 },
583   { STRING_COMMA_LEN ("i586"), PROCESSOR_PENTIUM,
584     CPU_I586_FLAGS, 0, 0 },
585   { STRING_COMMA_LEN ("i686"), PROCESSOR_PENTIUMPRO,
586     CPU_I686_FLAGS, 0, 0 },
587   { STRING_COMMA_LEN ("pentium"), PROCESSOR_PENTIUM,
588     CPU_I586_FLAGS, 0, 0 },
589   { STRING_COMMA_LEN ("pentiumpro"), PROCESSOR_PENTIUMPRO,
590     CPU_PENTIUMPRO_FLAGS, 0, 0 },
591   { STRING_COMMA_LEN ("pentiumii"), PROCESSOR_PENTIUMPRO,
592     CPU_P2_FLAGS, 0, 0 },
593   { STRING_COMMA_LEN ("pentiumiii"),PROCESSOR_PENTIUMPRO,
594     CPU_P3_FLAGS, 0, 0 },
595   { STRING_COMMA_LEN ("pentium4"), PROCESSOR_PENTIUM4,
596     CPU_P4_FLAGS, 0, 0 },
597   { STRING_COMMA_LEN ("prescott"), PROCESSOR_NOCONA,
598     CPU_CORE_FLAGS, 0, 0 },
599   { STRING_COMMA_LEN ("nocona"), PROCESSOR_NOCONA,
600     CPU_NOCONA_FLAGS, 0, 0 },
601   { STRING_COMMA_LEN ("yonah"), PROCESSOR_CORE,
602     CPU_CORE_FLAGS, 1, 0 },
603   { STRING_COMMA_LEN ("core"), PROCESSOR_CORE,
604     CPU_CORE_FLAGS, 0, 0 },
605   { STRING_COMMA_LEN ("merom"), PROCESSOR_CORE2,
606     CPU_CORE2_FLAGS, 1, 0 },
607   { STRING_COMMA_LEN ("core2"), PROCESSOR_CORE2,
608     CPU_CORE2_FLAGS, 0, 0 },
609   { STRING_COMMA_LEN ("corei7"), PROCESSOR_COREI7,
610     CPU_COREI7_FLAGS, 0, 0 },
611   { STRING_COMMA_LEN ("l1om"), PROCESSOR_L1OM,
612     CPU_L1OM_FLAGS, 0, 0 },
613   { STRING_COMMA_LEN ("k6"), PROCESSOR_K6,
614     CPU_K6_FLAGS, 0, 0 },
615   { STRING_COMMA_LEN ("k6_2"), PROCESSOR_K6,
616     CPU_K6_2_FLAGS, 0, 0 },
617   { STRING_COMMA_LEN ("athlon"), PROCESSOR_ATHLON,
618     CPU_ATHLON_FLAGS, 0, 0 },
619   { STRING_COMMA_LEN ("sledgehammer"), PROCESSOR_K8,
620     CPU_K8_FLAGS, 1, 0 },
621   { STRING_COMMA_LEN ("opteron"), PROCESSOR_K8,
622     CPU_K8_FLAGS, 0, 0 },
623   { STRING_COMMA_LEN ("k8"), PROCESSOR_K8,
624     CPU_K8_FLAGS, 0, 0 },
625   { STRING_COMMA_LEN ("amdfam10"), PROCESSOR_AMDFAM10,
626     CPU_AMDFAM10_FLAGS, 0, 0 },
627   { STRING_COMMA_LEN ("bdver1"), PROCESSOR_BDVER1,
628     CPU_BDVER1_FLAGS, 0, 0 },
629   { STRING_COMMA_LEN (".8087"), PROCESSOR_UNKNOWN,
630     CPU_8087_FLAGS, 0, 0 },
631   { STRING_COMMA_LEN (".287"), PROCESSOR_UNKNOWN,
632     CPU_287_FLAGS, 0, 0 },
633   { STRING_COMMA_LEN (".387"), PROCESSOR_UNKNOWN,
634     CPU_387_FLAGS, 0, 0 },
635   { STRING_COMMA_LEN (".no87"), PROCESSOR_UNKNOWN,
636     CPU_ANY87_FLAGS, 0, 1 },
637   { STRING_COMMA_LEN (".mmx"), PROCESSOR_UNKNOWN,
638     CPU_MMX_FLAGS, 0, 0 },
639   { STRING_COMMA_LEN (".nommx"), PROCESSOR_UNKNOWN,
640     CPU_3DNOWA_FLAGS, 0, 1 },
641   { STRING_COMMA_LEN (".sse"), PROCESSOR_UNKNOWN,
642     CPU_SSE_FLAGS, 0, 0 },
643   { STRING_COMMA_LEN (".sse2"), PROCESSOR_UNKNOWN,
644     CPU_SSE2_FLAGS, 0, 0 },
645   { STRING_COMMA_LEN (".sse3"), PROCESSOR_UNKNOWN,
646     CPU_SSE3_FLAGS, 0, 0 },
647   { STRING_COMMA_LEN (".ssse3"), PROCESSOR_UNKNOWN,
648     CPU_SSSE3_FLAGS, 0, 0 },
649   { STRING_COMMA_LEN (".sse4.1"), PROCESSOR_UNKNOWN,
650     CPU_SSE4_1_FLAGS, 0, 0 },
651   { STRING_COMMA_LEN (".sse4.2"), PROCESSOR_UNKNOWN,
652     CPU_SSE4_2_FLAGS, 0, 0 },
653   { STRING_COMMA_LEN (".sse4"), PROCESSOR_UNKNOWN,
654     CPU_SSE4_2_FLAGS, 0, 0 },
655   { STRING_COMMA_LEN (".nosse"), PROCESSOR_UNKNOWN,
656     CPU_ANY_SSE_FLAGS, 0, 1 },
657   { STRING_COMMA_LEN (".avx"), PROCESSOR_UNKNOWN,
658     CPU_AVX_FLAGS, 0, 0 },
659   { STRING_COMMA_LEN (".noavx"), PROCESSOR_UNKNOWN,
660     CPU_ANY_AVX_FLAGS, 0, 1 },
661   { STRING_COMMA_LEN (".vmx"), PROCESSOR_UNKNOWN,
662     CPU_VMX_FLAGS, 0, 0 },
663   { STRING_COMMA_LEN (".smx"), PROCESSOR_UNKNOWN,
664     CPU_SMX_FLAGS, 0, 0 },
665   { STRING_COMMA_LEN (".xsave"), PROCESSOR_UNKNOWN,
666     CPU_XSAVE_FLAGS, 0, 0 },
667   { STRING_COMMA_LEN (".xsaveopt"), PROCESSOR_UNKNOWN,
668     CPU_XSAVEOPT_FLAGS, 0, 0 },
669   { STRING_COMMA_LEN (".aes"), PROCESSOR_UNKNOWN,
670     CPU_AES_FLAGS, 0, 0 },
671   { STRING_COMMA_LEN (".pclmul"), PROCESSOR_UNKNOWN,
672     CPU_PCLMUL_FLAGS, 0, 0 },
673   { STRING_COMMA_LEN (".clmul"), PROCESSOR_UNKNOWN,
674     CPU_PCLMUL_FLAGS, 1, 0 },
675   { STRING_COMMA_LEN (".fsgsbase"), PROCESSOR_UNKNOWN,
676     CPU_FSGSBASE_FLAGS, 0, 0 },
677   { STRING_COMMA_LEN (".rdrnd"), PROCESSOR_UNKNOWN,
678     CPU_RDRND_FLAGS, 0, 0 },
679   { STRING_COMMA_LEN (".f16c"), PROCESSOR_UNKNOWN,
680     CPU_F16C_FLAGS, 0, 0 },
681   { STRING_COMMA_LEN (".fma"), PROCESSOR_UNKNOWN,
682     CPU_FMA_FLAGS, 0, 0 },
683   { STRING_COMMA_LEN (".fma4"), PROCESSOR_UNKNOWN,
684     CPU_FMA4_FLAGS, 0, 0 },
685   { STRING_COMMA_LEN (".xop"), PROCESSOR_UNKNOWN,
686     CPU_XOP_FLAGS, 0, 0 },
687   { STRING_COMMA_LEN (".lwp"), PROCESSOR_UNKNOWN,
688     CPU_LWP_FLAGS, 0, 0 },
689   { STRING_COMMA_LEN (".movbe"), PROCESSOR_UNKNOWN,
690     CPU_MOVBE_FLAGS, 0, 0 },
691   { STRING_COMMA_LEN (".ept"), PROCESSOR_UNKNOWN,
692     CPU_EPT_FLAGS, 0, 0 },
693   { STRING_COMMA_LEN (".clflush"), PROCESSOR_UNKNOWN,
694     CPU_CLFLUSH_FLAGS, 0, 0 },
695   { STRING_COMMA_LEN (".nop"), PROCESSOR_UNKNOWN,
696     CPU_NOP_FLAGS, 0, 0 },
697   { STRING_COMMA_LEN (".syscall"), PROCESSOR_UNKNOWN,
698     CPU_SYSCALL_FLAGS, 0, 0 },
699   { STRING_COMMA_LEN (".rdtscp"), PROCESSOR_UNKNOWN,
700     CPU_RDTSCP_FLAGS, 0, 0 },
701   { STRING_COMMA_LEN (".3dnow"), PROCESSOR_UNKNOWN,
702     CPU_3DNOW_FLAGS, 0, 0 },
703   { STRING_COMMA_LEN (".3dnowa"), PROCESSOR_UNKNOWN,
704     CPU_3DNOWA_FLAGS, 0, 0 },
705   { STRING_COMMA_LEN (".padlock"), PROCESSOR_UNKNOWN,
706     CPU_PADLOCK_FLAGS, 0, 0 },
707   { STRING_COMMA_LEN (".pacifica"), PROCESSOR_UNKNOWN,
708     CPU_SVME_FLAGS, 1, 0 },
709   { STRING_COMMA_LEN (".svme"), PROCESSOR_UNKNOWN,
710     CPU_SVME_FLAGS, 0, 0 },
711   { STRING_COMMA_LEN (".sse4a"), PROCESSOR_UNKNOWN,
712     CPU_SSE4A_FLAGS, 0, 0 },
713   { STRING_COMMA_LEN (".abm"), PROCESSOR_UNKNOWN,
714     CPU_ABM_FLAGS, 0, 0 },
715 };
716
717 #ifdef I386COFF
718 /* Like s_lcomm_internal in gas/read.c but the alignment string
719    is allowed to be optional.  */
720
721 static symbolS *
722 pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
723 {
724   addressT align = 0;
725
726   SKIP_WHITESPACE ();
727
728   if (needs_align
729       && *input_line_pointer == ',')
730     {
731       align = parse_align (needs_align - 1);
732
733       if (align == (addressT) -1)
734         return NULL;
735     }
736   else
737     {
738       if (size >= 8)
739         align = 3;
740       else if (size >= 4)
741         align = 2;
742       else if (size >= 2)
743         align = 1;
744       else
745         align = 0;
746     }
747
748   bss_alloc (symbolP, size, align);
749   return symbolP;
750 }
751
752 static void
753 pe_lcomm (int needs_align)
754 {
755   s_comm_internal (needs_align * 2, pe_lcomm_internal);
756 }
757 #endif
758
759 const pseudo_typeS md_pseudo_table[] =
760 {
761 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
762   {"align", s_align_bytes, 0},
763 #else
764   {"align", s_align_ptwo, 0},
765 #endif
766   {"arch", set_cpu_arch, 0},
767 #ifndef I386COFF
768   {"bss", s_bss, 0},
769 #else
770   {"lcomm", pe_lcomm, 1},
771 #endif
772   {"ffloat", float_cons, 'f'},
773   {"dfloat", float_cons, 'd'},
774   {"tfloat", float_cons, 'x'},
775   {"value", cons, 2},
776   {"slong", signed_cons, 4},
777   {"noopt", s_ignore, 0},
778   {"optim", s_ignore, 0},
779   {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
780   {"code16", set_code_flag, CODE_16BIT},
781   {"code32", set_code_flag, CODE_32BIT},
782   {"code64", set_code_flag, CODE_64BIT},
783   {"intel_syntax", set_intel_syntax, 1},
784   {"att_syntax", set_intel_syntax, 0},
785   {"intel_mnemonic", set_intel_mnemonic, 1},
786   {"att_mnemonic", set_intel_mnemonic, 0},
787   {"allow_index_reg", set_allow_index_reg, 1},
788   {"disallow_index_reg", set_allow_index_reg, 0},
789   {"sse_check", set_sse_check, 0},
790 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
791   {"largecomm", handle_large_common, 0},
792 #else
793   {"file", (void (*) (int)) dwarf2_directive_file, 0},
794   {"loc", dwarf2_directive_loc, 0},
795   {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
796 #endif
797 #ifdef TE_PE
798   {"secrel32", pe_directive_secrel, 0},
799 #endif
800   {0, 0, 0}
801 };
802
803 /* For interface with expression ().  */
804 extern char *input_line_pointer;
805
806 /* Hash table for instruction mnemonic lookup.  */
807 static struct hash_control *op_hash;
808
809 /* Hash table for register lookup.  */
810 static struct hash_control *reg_hash;
811 \f
812 void
813 i386_align_code (fragS *fragP, int count)
814 {
815   /* Various efficient no-op patterns for aligning code labels.
816      Note: Don't try to assemble the instructions in the comments.
817      0L and 0w are not legal.  */
818   static const char f32_1[] =
819     {0x90};                                     /* nop                  */
820   static const char f32_2[] =
821     {0x66,0x90};                                /* xchg %ax,%ax */
822   static const char f32_3[] =
823     {0x8d,0x76,0x00};                           /* leal 0(%esi),%esi    */
824   static const char f32_4[] =
825     {0x8d,0x74,0x26,0x00};                      /* leal 0(%esi,1),%esi  */
826   static const char f32_5[] =
827     {0x90,                                      /* nop                  */
828      0x8d,0x74,0x26,0x00};                      /* leal 0(%esi,1),%esi  */
829   static const char f32_6[] =
830     {0x8d,0xb6,0x00,0x00,0x00,0x00};            /* leal 0L(%esi),%esi   */
831   static const char f32_7[] =
832     {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};       /* leal 0L(%esi,1),%esi */
833   static const char f32_8[] =
834     {0x90,                                      /* nop                  */
835      0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};       /* leal 0L(%esi,1),%esi */
836   static const char f32_9[] =
837     {0x89,0xf6,                                 /* movl %esi,%esi       */
838      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
839   static const char f32_10[] =
840     {0x8d,0x76,0x00,                            /* leal 0(%esi),%esi    */
841      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
842   static const char f32_11[] =
843     {0x8d,0x74,0x26,0x00,                       /* leal 0(%esi,1),%esi  */
844      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
845   static const char f32_12[] =
846     {0x8d,0xb6,0x00,0x00,0x00,0x00,             /* leal 0L(%esi),%esi   */
847      0x8d,0xbf,0x00,0x00,0x00,0x00};            /* leal 0L(%edi),%edi   */
848   static const char f32_13[] =
849     {0x8d,0xb6,0x00,0x00,0x00,0x00,             /* leal 0L(%esi),%esi   */
850      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
851   static const char f32_14[] =
852     {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00,        /* leal 0L(%esi,1),%esi */
853      0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};       /* leal 0L(%edi,1),%edi */
854   static const char f16_3[] =
855     {0x8d,0x74,0x00};                           /* lea 0(%esi),%esi     */
856   static const char f16_4[] =
857     {0x8d,0xb4,0x00,0x00};                      /* lea 0w(%si),%si      */
858   static const char f16_5[] =
859     {0x90,                                      /* nop                  */
860      0x8d,0xb4,0x00,0x00};                      /* lea 0w(%si),%si      */
861   static const char f16_6[] =
862     {0x89,0xf6,                                 /* mov %si,%si          */
863      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
864   static const char f16_7[] =
865     {0x8d,0x74,0x00,                            /* lea 0(%si),%si       */
866      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
867   static const char f16_8[] =
868     {0x8d,0xb4,0x00,0x00,                       /* lea 0w(%si),%si      */
869      0x8d,0xbd,0x00,0x00};                      /* lea 0w(%di),%di      */
870   static const char jump_31[] =
871     {0xeb,0x1d,0x90,0x90,0x90,0x90,0x90,        /* jmp .+31; lotsa nops */
872      0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
873      0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,
874      0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
875   static const char *const f32_patt[] = {
876     f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
877     f32_9, f32_10, f32_11, f32_12, f32_13, f32_14
878   };
879   static const char *const f16_patt[] = {
880     f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8
881   };
882   /* nopl (%[re]ax) */
883   static const char alt_3[] =
884     {0x0f,0x1f,0x00};
885   /* nopl 0(%[re]ax) */
886   static const char alt_4[] =
887     {0x0f,0x1f,0x40,0x00};
888   /* nopl 0(%[re]ax,%[re]ax,1) */
889   static const char alt_5[] =
890     {0x0f,0x1f,0x44,0x00,0x00};
891   /* nopw 0(%[re]ax,%[re]ax,1) */
892   static const char alt_6[] =
893     {0x66,0x0f,0x1f,0x44,0x00,0x00};
894   /* nopl 0L(%[re]ax) */
895   static const char alt_7[] =
896     {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
897   /* nopl 0L(%[re]ax,%[re]ax,1) */
898   static const char alt_8[] =
899     {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
900   /* nopw 0L(%[re]ax,%[re]ax,1) */
901   static const char alt_9[] =
902     {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
903   /* nopw %cs:0L(%[re]ax,%[re]ax,1) */
904   static const char alt_10[] =
905     {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
906   /* data16
907      nopw %cs:0L(%[re]ax,%[re]ax,1) */
908   static const char alt_long_11[] =
909     {0x66,
910      0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
911   /* data16
912      data16
913      nopw %cs:0L(%[re]ax,%[re]ax,1) */
914   static const char alt_long_12[] =
915     {0x66,
916      0x66,
917      0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
918   /* data16
919      data16
920      data16
921      nopw %cs:0L(%[re]ax,%[re]ax,1) */
922   static const char alt_long_13[] =
923     {0x66,
924      0x66,
925      0x66,
926      0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
927   /* data16
928      data16
929      data16
930      data16
931      nopw %cs:0L(%[re]ax,%[re]ax,1) */
932   static const char alt_long_14[] =
933     {0x66,
934      0x66,
935      0x66,
936      0x66,
937      0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
938   /* data16
939      data16
940      data16
941      data16
942      data16
943      nopw %cs:0L(%[re]ax,%[re]ax,1) */
944   static const char alt_long_15[] =
945     {0x66,
946      0x66,
947      0x66,
948      0x66,
949      0x66,
950      0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
951   /* nopl 0(%[re]ax,%[re]ax,1)
952      nopw 0(%[re]ax,%[re]ax,1) */
953   static const char alt_short_11[] =
954     {0x0f,0x1f,0x44,0x00,0x00,
955      0x66,0x0f,0x1f,0x44,0x00,0x00};
956   /* nopw 0(%[re]ax,%[re]ax,1)
957      nopw 0(%[re]ax,%[re]ax,1) */
958   static const char alt_short_12[] =
959     {0x66,0x0f,0x1f,0x44,0x00,0x00,
960      0x66,0x0f,0x1f,0x44,0x00,0x00};
961   /* nopw 0(%[re]ax,%[re]ax,1)
962      nopl 0L(%[re]ax) */
963   static const char alt_short_13[] =
964     {0x66,0x0f,0x1f,0x44,0x00,0x00,
965      0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
966   /* nopl 0L(%[re]ax)
967      nopl 0L(%[re]ax) */
968   static const char alt_short_14[] =
969     {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
970      0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
971   /* nopl 0L(%[re]ax)
972      nopl 0L(%[re]ax,%[re]ax,1) */
973   static const char alt_short_15[] =
974     {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00,
975      0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
976   static const char *const alt_short_patt[] = {
977     f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
978     alt_9, alt_10, alt_short_11, alt_short_12, alt_short_13,
979     alt_short_14, alt_short_15
980   };
981   static const char *const alt_long_patt[] = {
982     f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
983     alt_9, alt_10, alt_long_11, alt_long_12, alt_long_13,
984     alt_long_14, alt_long_15
985   };
986
987   /* Only align for at least a positive non-zero boundary. */
988   if (count <= 0 || count > MAX_MEM_FOR_RS_ALIGN_CODE)
989     return;
990
991   /* We need to decide which NOP sequence to use for 32bit and
992      64bit. When -mtune= is used:
993
994      1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
995      PROCESSOR_GENERIC32, f32_patt will be used.
996      2. For PROCESSOR_PENTIUMPRO, PROCESSOR_PENTIUM4, PROCESSOR_NOCONA,
997      PROCESSOR_CORE, PROCESSOR_CORE2, PROCESSOR_COREI7, and
998      PROCESSOR_GENERIC64, alt_long_patt will be used.
999      3. For PROCESSOR_ATHLON, PROCESSOR_K6, PROCESSOR_K8 and
1000      PROCESSOR_AMDFAM10, and PROCESSOR_BDVER1, alt_short_patt
1001      will be used.
1002
1003      When -mtune= isn't used, alt_long_patt will be used if
1004      cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt will
1005      be used.
1006
1007      When -march= or .arch is used, we can't use anything beyond
1008      cpu_arch_isa_flags.   */
1009
1010   if (flag_code == CODE_16BIT)
1011     {
1012       if (count > 8)
1013         {
1014           memcpy (fragP->fr_literal + fragP->fr_fix,
1015                   jump_31, count);
1016           /* Adjust jump offset.  */
1017           fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
1018         }
1019       else
1020         memcpy (fragP->fr_literal + fragP->fr_fix,
1021                 f16_patt[count - 1], count);
1022     }
1023   else
1024     {
1025       const char *const *patt = NULL;
1026
1027       if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1028         {
1029           /* PROCESSOR_UNKNOWN means that all ISAs may be used.  */
1030           switch (cpu_arch_tune)
1031             {
1032             case PROCESSOR_UNKNOWN:
1033               /* We use cpu_arch_isa_flags to check if we SHOULD
1034                  optimize with nops.  */
1035               if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1036                 patt = alt_long_patt;
1037               else
1038                 patt = f32_patt;
1039               break;
1040             case PROCESSOR_PENTIUMPRO:
1041             case PROCESSOR_PENTIUM4:
1042             case PROCESSOR_NOCONA:
1043             case PROCESSOR_CORE:
1044             case PROCESSOR_CORE2:
1045             case PROCESSOR_COREI7:
1046             case PROCESSOR_L1OM:
1047             case PROCESSOR_GENERIC64:
1048               patt = alt_long_patt;
1049               break;
1050             case PROCESSOR_K6:
1051             case PROCESSOR_ATHLON:
1052             case PROCESSOR_K8:
1053             case PROCESSOR_AMDFAM10:
1054             case PROCESSOR_BDVER1:
1055               patt = alt_short_patt;
1056               break;
1057             case PROCESSOR_I386:
1058             case PROCESSOR_I486:
1059             case PROCESSOR_PENTIUM:
1060             case PROCESSOR_GENERIC32:
1061               patt = f32_patt;
1062               break;
1063             }
1064         }
1065       else
1066         {
1067           switch (fragP->tc_frag_data.tune)
1068             {
1069             case PROCESSOR_UNKNOWN:
1070               /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1071                  PROCESSOR_UNKNOWN.  */
1072               abort ();
1073               break;
1074
1075             case PROCESSOR_I386:
1076             case PROCESSOR_I486:
1077             case PROCESSOR_PENTIUM:
1078             case PROCESSOR_K6:
1079             case PROCESSOR_ATHLON:
1080             case PROCESSOR_K8:
1081             case PROCESSOR_AMDFAM10:
1082             case PROCESSOR_BDVER1:
1083             case PROCESSOR_GENERIC32:
1084               /* We use cpu_arch_isa_flags to check if we CAN optimize
1085                  with nops.  */
1086               if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1087                 patt = alt_short_patt;
1088               else
1089                 patt = f32_patt;
1090               break;
1091             case PROCESSOR_PENTIUMPRO:
1092             case PROCESSOR_PENTIUM4:
1093             case PROCESSOR_NOCONA:
1094             case PROCESSOR_CORE:
1095             case PROCESSOR_CORE2:
1096             case PROCESSOR_COREI7:
1097             case PROCESSOR_L1OM:
1098               if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1099                 patt = alt_long_patt;
1100               else
1101                 patt = f32_patt;
1102               break;
1103             case PROCESSOR_GENERIC64:
1104               patt = alt_long_patt;
1105               break;
1106             }
1107         }
1108
1109       if (patt == f32_patt)
1110         {
1111           /* If the padding is less than 15 bytes, we use the normal
1112              ones.  Otherwise, we use a jump instruction and adjust
1113              its offset.   */
1114           int limit;
1115
1116           /* For 64bit, the limit is 3 bytes.  */
1117           if (flag_code == CODE_64BIT
1118               && fragP->tc_frag_data.isa_flags.bitfield.cpulm)
1119             limit = 3;
1120           else
1121             limit = 15;
1122           if (count < limit)
1123             memcpy (fragP->fr_literal + fragP->fr_fix,
1124                     patt[count - 1], count);
1125           else
1126             {
1127               memcpy (fragP->fr_literal + fragP->fr_fix,
1128                       jump_31, count);
1129               /* Adjust jump offset.  */
1130               fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
1131             }
1132         }
1133       else
1134         {
1135           /* Maximum length of an instruction is 15 byte.  If the
1136              padding is greater than 15 bytes and we don't use jump,
1137              we have to break it into smaller pieces.  */
1138           int padding = count;
1139           while (padding > 15)
1140             {
1141               padding -= 15;
1142               memcpy (fragP->fr_literal + fragP->fr_fix + padding,
1143                       patt [14], 15);
1144             }
1145
1146           if (padding)
1147             memcpy (fragP->fr_literal + fragP->fr_fix,
1148                     patt [padding - 1], padding);
1149         }
1150     }
1151   fragP->fr_var = count;
1152 }
1153
1154 static INLINE int
1155 operand_type_all_zero (const union i386_operand_type *x)
1156 {
1157   switch (ARRAY_SIZE(x->array))
1158     {
1159     case 3:
1160       if (x->array[2])
1161         return 0;
1162     case 2:
1163       if (x->array[1])
1164         return 0;
1165     case 1:
1166       return !x->array[0];
1167     default:
1168       abort ();
1169     }
1170 }
1171
1172 static INLINE void
1173 operand_type_set (union i386_operand_type *x, unsigned int v)
1174 {
1175   switch (ARRAY_SIZE(x->array))
1176     {
1177     case 3:
1178       x->array[2] = v;
1179     case 2:
1180       x->array[1] = v;
1181     case 1:
1182       x->array[0] = v;
1183       break;
1184     default:
1185       abort ();
1186     }
1187 }
1188
1189 static INLINE int
1190 operand_type_equal (const union i386_operand_type *x,
1191                     const union i386_operand_type *y)
1192 {
1193   switch (ARRAY_SIZE(x->array))
1194     {
1195     case 3:
1196       if (x->array[2] != y->array[2])
1197         return 0;
1198     case 2:
1199       if (x->array[1] != y->array[1])
1200         return 0;
1201     case 1:
1202       return x->array[0] == y->array[0];
1203       break;
1204     default:
1205       abort ();
1206     }
1207 }
1208
1209 static INLINE int
1210 cpu_flags_all_zero (const union i386_cpu_flags *x)
1211 {
1212   switch (ARRAY_SIZE(x->array))
1213     {
1214     case 3:
1215       if (x->array[2])
1216         return 0;
1217     case 2:
1218       if (x->array[1])
1219         return 0;
1220     case 1:
1221       return !x->array[0];
1222     default:
1223       abort ();
1224     }
1225 }
1226
1227 static INLINE void
1228 cpu_flags_set (union i386_cpu_flags *x, unsigned int v)
1229 {
1230   switch (ARRAY_SIZE(x->array))
1231     {
1232     case 3:
1233       x->array[2] = v;
1234     case 2:
1235       x->array[1] = v;
1236     case 1:
1237       x->array[0] = v;
1238       break;
1239     default:
1240       abort ();
1241     }
1242 }
1243
1244 static INLINE int
1245 cpu_flags_equal (const union i386_cpu_flags *x,
1246                  const union i386_cpu_flags *y)
1247 {
1248   switch (ARRAY_SIZE(x->array))
1249     {
1250     case 3:
1251       if (x->array[2] != y->array[2])
1252         return 0;
1253     case 2:
1254       if (x->array[1] != y->array[1])
1255         return 0;
1256     case 1:
1257       return x->array[0] == y->array[0];
1258       break;
1259     default:
1260       abort ();
1261     }
1262 }
1263
1264 static INLINE int
1265 cpu_flags_check_cpu64 (i386_cpu_flags f)
1266 {
1267   return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1268            || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1269 }
1270
1271 static INLINE i386_cpu_flags
1272 cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1273 {
1274   switch (ARRAY_SIZE (x.array))
1275     {
1276     case 3:
1277       x.array [2] &= y.array [2];
1278     case 2:
1279       x.array [1] &= y.array [1];
1280     case 1:
1281       x.array [0] &= y.array [0];
1282       break;
1283     default:
1284       abort ();
1285     }
1286   return x;
1287 }
1288
1289 static INLINE i386_cpu_flags
1290 cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1291 {
1292   switch (ARRAY_SIZE (x.array))
1293     {
1294     case 3:
1295       x.array [2] |= y.array [2];
1296     case 2:
1297       x.array [1] |= y.array [1];
1298     case 1:
1299       x.array [0] |= y.array [0];
1300       break;
1301     default:
1302       abort ();
1303     }
1304   return x;
1305 }
1306
1307 static INLINE i386_cpu_flags
1308 cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1309 {
1310   switch (ARRAY_SIZE (x.array))
1311     {
1312     case 3:
1313       x.array [2] &= ~y.array [2];
1314     case 2:
1315       x.array [1] &= ~y.array [1];
1316     case 1:
1317       x.array [0] &= ~y.array [0];
1318       break;
1319     default:
1320       abort ();
1321     }
1322   return x;
1323 }
1324
1325 #define CPU_FLAGS_ARCH_MATCH            0x1
1326 #define CPU_FLAGS_64BIT_MATCH           0x2
1327 #define CPU_FLAGS_AES_MATCH             0x4
1328 #define CPU_FLAGS_PCLMUL_MATCH          0x8
1329 #define CPU_FLAGS_AVX_MATCH            0x10
1330
1331 #define CPU_FLAGS_32BIT_MATCH \
1332   (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_AES_MATCH \
1333    | CPU_FLAGS_PCLMUL_MATCH | CPU_FLAGS_AVX_MATCH)
1334 #define CPU_FLAGS_PERFECT_MATCH \
1335   (CPU_FLAGS_32BIT_MATCH | CPU_FLAGS_64BIT_MATCH)
1336
1337 /* Return CPU flags match bits. */
1338
1339 static int
1340 cpu_flags_match (const insn_template *t)
1341 {
1342   i386_cpu_flags x = t->cpu_flags;
1343   int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1344
1345   x.bitfield.cpu64 = 0;
1346   x.bitfield.cpuno64 = 0;
1347
1348   if (cpu_flags_all_zero (&x))
1349     {
1350       /* This instruction is available on all archs.  */
1351       match |= CPU_FLAGS_32BIT_MATCH;
1352     }
1353   else
1354     {
1355       /* This instruction is available only on some archs.  */
1356       i386_cpu_flags cpu = cpu_arch_flags;
1357
1358       cpu.bitfield.cpu64 = 0;
1359       cpu.bitfield.cpuno64 = 0;
1360       cpu = cpu_flags_and (x, cpu);
1361       if (!cpu_flags_all_zero (&cpu))
1362         {
1363           if (x.bitfield.cpuavx)
1364             {
1365               /* We only need to check AES/PCLMUL/SSE2AVX with AVX.  */
1366               if (cpu.bitfield.cpuavx)
1367                 {
1368                   /* Check SSE2AVX.  */
1369                   if (!t->opcode_modifier.sse2avx|| sse2avx)
1370                     {
1371                       match |= (CPU_FLAGS_ARCH_MATCH
1372                                 | CPU_FLAGS_AVX_MATCH);
1373                       /* Check AES.  */
1374                       if (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1375                         match |= CPU_FLAGS_AES_MATCH;
1376                       /* Check PCLMUL.  */
1377                       if (!x.bitfield.cpupclmul
1378                           || cpu.bitfield.cpupclmul)
1379                         match |= CPU_FLAGS_PCLMUL_MATCH;
1380                     }
1381                 }
1382               else
1383                 match |= CPU_FLAGS_ARCH_MATCH;
1384             }
1385           else
1386             match |= CPU_FLAGS_32BIT_MATCH;
1387         }
1388     }
1389   return match;
1390 }
1391
1392 static INLINE i386_operand_type
1393 operand_type_and (i386_operand_type x, i386_operand_type y)
1394 {
1395   switch (ARRAY_SIZE (x.array))
1396     {
1397     case 3:
1398       x.array [2] &= y.array [2];
1399     case 2:
1400       x.array [1] &= y.array [1];
1401     case 1:
1402       x.array [0] &= y.array [0];
1403       break;
1404     default:
1405       abort ();
1406     }
1407   return x;
1408 }
1409
1410 static INLINE i386_operand_type
1411 operand_type_or (i386_operand_type x, i386_operand_type y)
1412 {
1413   switch (ARRAY_SIZE (x.array))
1414     {
1415     case 3:
1416       x.array [2] |= y.array [2];
1417     case 2:
1418       x.array [1] |= y.array [1];
1419     case 1:
1420       x.array [0] |= y.array [0];
1421       break;
1422     default:
1423       abort ();
1424     }
1425   return x;
1426 }
1427
1428 static INLINE i386_operand_type
1429 operand_type_xor (i386_operand_type x, i386_operand_type y)
1430 {
1431   switch (ARRAY_SIZE (x.array))
1432     {
1433     case 3:
1434       x.array [2] ^= y.array [2];
1435     case 2:
1436       x.array [1] ^= y.array [1];
1437     case 1:
1438       x.array [0] ^= y.array [0];
1439       break;
1440     default:
1441       abort ();
1442     }
1443   return x;
1444 }
1445
1446 static const i386_operand_type acc32 = OPERAND_TYPE_ACC32;
1447 static const i386_operand_type acc64 = OPERAND_TYPE_ACC64;
1448 static const i386_operand_type control = OPERAND_TYPE_CONTROL;
1449 static const i386_operand_type inoutportreg
1450   = OPERAND_TYPE_INOUTPORTREG;
1451 static const i386_operand_type reg16_inoutportreg
1452   = OPERAND_TYPE_REG16_INOUTPORTREG;
1453 static const i386_operand_type disp16 = OPERAND_TYPE_DISP16;
1454 static const i386_operand_type disp32 = OPERAND_TYPE_DISP32;
1455 static const i386_operand_type disp32s = OPERAND_TYPE_DISP32S;
1456 static const i386_operand_type disp16_32 = OPERAND_TYPE_DISP16_32;
1457 static const i386_operand_type anydisp
1458   = OPERAND_TYPE_ANYDISP;
1459 static const i386_operand_type regxmm = OPERAND_TYPE_REGXMM;
1460 static const i386_operand_type regymm = OPERAND_TYPE_REGYMM;
1461 static const i386_operand_type imm8 = OPERAND_TYPE_IMM8;
1462 static const i386_operand_type imm8s = OPERAND_TYPE_IMM8S;
1463 static const i386_operand_type imm16 = OPERAND_TYPE_IMM16;
1464 static const i386_operand_type imm32 = OPERAND_TYPE_IMM32;
1465 static const i386_operand_type imm32s = OPERAND_TYPE_IMM32S;
1466 static const i386_operand_type imm64 = OPERAND_TYPE_IMM64;
1467 static const i386_operand_type imm16_32 = OPERAND_TYPE_IMM16_32;
1468 static const i386_operand_type imm16_32s = OPERAND_TYPE_IMM16_32S;
1469 static const i386_operand_type imm16_32_32s = OPERAND_TYPE_IMM16_32_32S;
1470 static const i386_operand_type vec_imm4 = OPERAND_TYPE_VEC_IMM4;
1471
1472 enum operand_type
1473 {
1474   reg,
1475   imm,
1476   disp,
1477   anymem
1478 };
1479
1480 static INLINE int
1481 operand_type_check (i386_operand_type t, enum operand_type c)
1482 {
1483   switch (c)
1484     {
1485     case reg:
1486       return (t.bitfield.reg8
1487               || t.bitfield.reg16
1488               || t.bitfield.reg32
1489               || t.bitfield.reg64);
1490
1491     case imm:
1492       return (t.bitfield.imm8
1493               || t.bitfield.imm8s
1494               || t.bitfield.imm16
1495               || t.bitfield.imm32
1496               || t.bitfield.imm32s
1497               || t.bitfield.imm64);
1498
1499     case disp:
1500       return (t.bitfield.disp8
1501               || t.bitfield.disp16
1502               || t.bitfield.disp32
1503               || t.bitfield.disp32s
1504               || t.bitfield.disp64);
1505
1506     case anymem:
1507       return (t.bitfield.disp8
1508               || t.bitfield.disp16
1509               || t.bitfield.disp32
1510               || t.bitfield.disp32s
1511               || t.bitfield.disp64
1512               || t.bitfield.baseindex);
1513
1514     default:
1515       abort ();
1516     }
1517
1518   return 0;
1519 }
1520
1521 /* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit on
1522    operand J for instruction template T.  */
1523
1524 static INLINE int
1525 match_reg_size (const insn_template *t, unsigned int j)
1526 {
1527   return !((i.types[j].bitfield.byte
1528             && !t->operand_types[j].bitfield.byte)
1529            || (i.types[j].bitfield.word
1530                && !t->operand_types[j].bitfield.word)
1531            || (i.types[j].bitfield.dword
1532                && !t->operand_types[j].bitfield.dword)
1533            || (i.types[j].bitfield.qword
1534                && !t->operand_types[j].bitfield.qword));
1535 }
1536
1537 /* Return 1 if there is no conflict in any size on operand J for
1538    instruction template T.  */
1539
1540 static INLINE int
1541 match_mem_size (const insn_template *t, unsigned int j)
1542 {
1543   return (match_reg_size (t, j)
1544           && !((i.types[j].bitfield.unspecified
1545                 && !t->operand_types[j].bitfield.unspecified)
1546                || (i.types[j].bitfield.fword
1547                    && !t->operand_types[j].bitfield.fword)
1548                || (i.types[j].bitfield.tbyte
1549                    && !t->operand_types[j].bitfield.tbyte)
1550                || (i.types[j].bitfield.xmmword
1551                    && !t->operand_types[j].bitfield.xmmword)
1552                || (i.types[j].bitfield.ymmword
1553                    && !t->operand_types[j].bitfield.ymmword)));
1554 }
1555
1556 /* Return 1 if there is no size conflict on any operands for
1557    instruction template T.  */
1558
1559 static INLINE int
1560 operand_size_match (const insn_template *t)
1561 {
1562   unsigned int j;
1563   int match = 1;
1564
1565   /* Don't check jump instructions.  */
1566   if (t->opcode_modifier.jump
1567       || t->opcode_modifier.jumpbyte
1568       || t->opcode_modifier.jumpdword
1569       || t->opcode_modifier.jumpintersegment)
1570     return match;
1571
1572   /* Check memory and accumulator operand size.  */
1573   for (j = 0; j < i.operands; j++)
1574     {
1575       if (t->operand_types[j].bitfield.anysize)
1576         continue;
1577
1578       if (t->operand_types[j].bitfield.acc && !match_reg_size (t, j))
1579         {
1580           match = 0;
1581           break;
1582         }
1583
1584       if (i.types[j].bitfield.mem && !match_mem_size (t, j))
1585         {
1586           match = 0;
1587           break;
1588         }
1589     }
1590
1591   if (match)
1592     return match;
1593   else if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
1594     {
1595 mismatch:
1596       i.error = operand_size_mismatch;
1597       return 0;
1598     }
1599
1600   /* Check reverse.  */
1601   gas_assert (i.operands == 2);
1602
1603   match = 1;
1604   for (j = 0; j < 2; j++)
1605     {
1606       if (t->operand_types[j].bitfield.acc
1607           && !match_reg_size (t, j ? 0 : 1))
1608         goto mismatch;
1609
1610       if (i.types[j].bitfield.mem
1611           && !match_mem_size (t, j ? 0 : 1))
1612         goto mismatch;
1613     }
1614
1615   return match;
1616 }
1617
1618 static INLINE int
1619 operand_type_match (i386_operand_type overlap,
1620                     i386_operand_type given)
1621 {
1622   i386_operand_type temp = overlap;
1623
1624   temp.bitfield.jumpabsolute = 0;
1625   temp.bitfield.unspecified = 0;
1626   temp.bitfield.byte = 0;
1627   temp.bitfield.word = 0;
1628   temp.bitfield.dword = 0;
1629   temp.bitfield.fword = 0;
1630   temp.bitfield.qword = 0;
1631   temp.bitfield.tbyte = 0;
1632   temp.bitfield.xmmword = 0;
1633   temp.bitfield.ymmword = 0;
1634   if (operand_type_all_zero (&temp))
1635     goto mismatch;
1636
1637   if (given.bitfield.baseindex == overlap.bitfield.baseindex
1638       && given.bitfield.jumpabsolute == overlap.bitfield.jumpabsolute)
1639     return 1;
1640
1641 mismatch:
1642   i.error = operand_type_mismatch;
1643   return 0;
1644 }
1645
1646 /* If given types g0 and g1 are registers they must be of the same type
1647    unless the expected operand type register overlap is null.
1648    Note that Acc in a template matches every size of reg.  */
1649
1650 static INLINE int
1651 operand_type_register_match (i386_operand_type m0,
1652                              i386_operand_type g0,
1653                              i386_operand_type t0,
1654                              i386_operand_type m1,
1655                              i386_operand_type g1,
1656                              i386_operand_type t1)
1657 {
1658   if (!operand_type_check (g0, reg))
1659     return 1;
1660
1661   if (!operand_type_check (g1, reg))
1662     return 1;
1663
1664   if (g0.bitfield.reg8 == g1.bitfield.reg8
1665       && g0.bitfield.reg16 == g1.bitfield.reg16
1666       && g0.bitfield.reg32 == g1.bitfield.reg32
1667       && g0.bitfield.reg64 == g1.bitfield.reg64)
1668     return 1;
1669
1670   if (m0.bitfield.acc)
1671     {
1672       t0.bitfield.reg8 = 1;
1673       t0.bitfield.reg16 = 1;
1674       t0.bitfield.reg32 = 1;
1675       t0.bitfield.reg64 = 1;
1676     }
1677
1678   if (m1.bitfield.acc)
1679     {
1680       t1.bitfield.reg8 = 1;
1681       t1.bitfield.reg16 = 1;
1682       t1.bitfield.reg32 = 1;
1683       t1.bitfield.reg64 = 1;
1684     }
1685
1686   if (!(t0.bitfield.reg8 & t1.bitfield.reg8)
1687       && !(t0.bitfield.reg16 & t1.bitfield.reg16)
1688       && !(t0.bitfield.reg32 & t1.bitfield.reg32)
1689       && !(t0.bitfield.reg64 & t1.bitfield.reg64))
1690     return 1;
1691
1692   i.error = register_type_mismatch;
1693
1694   return 0;
1695 }
1696
1697 static INLINE unsigned int
1698 mode_from_disp_size (i386_operand_type t)
1699 {
1700   if (t.bitfield.disp8)
1701     return 1;
1702   else if (t.bitfield.disp16
1703            || t.bitfield.disp32
1704            || t.bitfield.disp32s)
1705     return 2;
1706   else
1707     return 0;
1708 }
1709
1710 static INLINE int
1711 fits_in_signed_byte (offsetT num)
1712 {
1713   return (num >= -128) && (num <= 127);
1714 }
1715
1716 static INLINE int
1717 fits_in_unsigned_byte (offsetT num)
1718 {
1719   return (num & 0xff) == num;
1720 }
1721
1722 static INLINE int
1723 fits_in_unsigned_word (offsetT num)
1724 {
1725   return (num & 0xffff) == num;
1726 }
1727
1728 static INLINE int
1729 fits_in_signed_word (offsetT num)
1730 {
1731   return (-32768 <= num) && (num <= 32767);
1732 }
1733
1734 static INLINE int
1735 fits_in_signed_long (offsetT num ATTRIBUTE_UNUSED)
1736 {
1737 #ifndef BFD64
1738   return 1;
1739 #else
1740   return (!(((offsetT) -1 << 31) & num)
1741           || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
1742 #endif
1743 }                               /* fits_in_signed_long() */
1744
1745 static INLINE int
1746 fits_in_unsigned_long (offsetT num ATTRIBUTE_UNUSED)
1747 {
1748 #ifndef BFD64
1749   return 1;
1750 #else
1751   return (num & (((offsetT) 2 << 31) - 1)) == num;
1752 #endif
1753 }                               /* fits_in_unsigned_long() */
1754
1755 static INLINE int
1756 fits_in_imm4 (offsetT num)
1757 {
1758   return (num & 0xf) == num;
1759 }
1760
1761 static i386_operand_type
1762 smallest_imm_type (offsetT num)
1763 {
1764   i386_operand_type t;
1765
1766   operand_type_set (&t, 0);
1767   t.bitfield.imm64 = 1;
1768
1769   if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
1770     {
1771       /* This code is disabled on the 486 because all the Imm1 forms
1772          in the opcode table are slower on the i486.  They're the
1773          versions with the implicitly specified single-position
1774          displacement, which has another syntax if you really want to
1775          use that form.  */
1776       t.bitfield.imm1 = 1;
1777       t.bitfield.imm8 = 1;
1778       t.bitfield.imm8s = 1;
1779       t.bitfield.imm16 = 1;
1780       t.bitfield.imm32 = 1;
1781       t.bitfield.imm32s = 1;
1782     }
1783   else if (fits_in_signed_byte (num))
1784     {
1785       t.bitfield.imm8 = 1;
1786       t.bitfield.imm8s = 1;
1787       t.bitfield.imm16 = 1;
1788       t.bitfield.imm32 = 1;
1789       t.bitfield.imm32s = 1;
1790     }
1791   else if (fits_in_unsigned_byte (num))
1792     {
1793       t.bitfield.imm8 = 1;
1794       t.bitfield.imm16 = 1;
1795       t.bitfield.imm32 = 1;
1796       t.bitfield.imm32s = 1;
1797     }
1798   else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
1799     {
1800       t.bitfield.imm16 = 1;
1801       t.bitfield.imm32 = 1;
1802       t.bitfield.imm32s = 1;
1803     }
1804   else if (fits_in_signed_long (num))
1805     {
1806       t.bitfield.imm32 = 1;
1807       t.bitfield.imm32s = 1;
1808     }
1809   else if (fits_in_unsigned_long (num))
1810     t.bitfield.imm32 = 1;
1811
1812   return t;
1813 }
1814
1815 static offsetT
1816 offset_in_range (offsetT val, int size)
1817 {
1818   addressT mask;
1819
1820   switch (size)
1821     {
1822     case 1: mask = ((addressT) 1 <<  8) - 1; break;
1823     case 2: mask = ((addressT) 1 << 16) - 1; break;
1824     case 4: mask = ((addressT) 2 << 31) - 1; break;
1825 #ifdef BFD64
1826     case 8: mask = ((addressT) 2 << 63) - 1; break;
1827 #endif
1828     default: abort ();
1829     }
1830
1831 #ifdef BFD64
1832   /* If BFD64, sign extend val for 32bit address mode.  */
1833   if (flag_code != CODE_64BIT
1834       || i.prefix[ADDR_PREFIX])
1835     if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
1836       val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
1837 #endif
1838
1839   if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
1840     {
1841       char buf1[40], buf2[40];
1842
1843       sprint_value (buf1, val);
1844       sprint_value (buf2, val & mask);
1845       as_warn (_("%s shortened to %s"), buf1, buf2);
1846     }
1847   return val & mask;
1848 }
1849
1850 enum PREFIX_GROUP
1851 {
1852   PREFIX_EXIST = 0,
1853   PREFIX_LOCK,
1854   PREFIX_REP,
1855   PREFIX_OTHER
1856 };
1857
1858 /* Returns
1859    a. PREFIX_EXIST if attempting to add a prefix where one from the
1860    same class already exists.
1861    b. PREFIX_LOCK if lock prefix is added.
1862    c. PREFIX_REP if rep/repne prefix is added.
1863    d. PREFIX_OTHER if other prefix is added.
1864  */
1865
1866 static enum PREFIX_GROUP
1867 add_prefix (unsigned int prefix)
1868 {
1869   enum PREFIX_GROUP ret = PREFIX_OTHER;
1870   unsigned int q;
1871
1872   if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
1873       && flag_code == CODE_64BIT)
1874     {
1875       if ((i.prefix[REX_PREFIX] & prefix & REX_W)
1876           || ((i.prefix[REX_PREFIX] & (REX_R | REX_X | REX_B))
1877               && (prefix & (REX_R | REX_X | REX_B))))
1878         ret = PREFIX_EXIST;
1879       q = REX_PREFIX;
1880     }
1881   else
1882     {
1883       switch (prefix)
1884         {
1885         default:
1886           abort ();
1887
1888         case CS_PREFIX_OPCODE:
1889         case DS_PREFIX_OPCODE:
1890         case ES_PREFIX_OPCODE:
1891         case FS_PREFIX_OPCODE:
1892         case GS_PREFIX_OPCODE:
1893         case SS_PREFIX_OPCODE:
1894           q = SEG_PREFIX;
1895           break;
1896
1897         case REPNE_PREFIX_OPCODE:
1898         case REPE_PREFIX_OPCODE:
1899           q = REP_PREFIX;
1900           ret = PREFIX_REP;
1901           break;
1902
1903         case LOCK_PREFIX_OPCODE:
1904           q = LOCK_PREFIX;
1905           ret = PREFIX_LOCK;
1906           break;
1907
1908         case FWAIT_OPCODE:
1909           q = WAIT_PREFIX;
1910           break;
1911
1912         case ADDR_PREFIX_OPCODE:
1913           q = ADDR_PREFIX;
1914           break;
1915
1916         case DATA_PREFIX_OPCODE:
1917           q = DATA_PREFIX;
1918           break;
1919         }
1920       if (i.prefix[q] != 0)
1921         ret = PREFIX_EXIST;
1922     }
1923
1924   if (ret)
1925     {
1926       if (!i.prefix[q])
1927         ++i.prefixes;
1928       i.prefix[q] |= prefix;
1929     }
1930   else
1931     as_bad (_("same type of prefix used twice"));
1932
1933   return ret;
1934 }
1935
1936 static void
1937 update_code_flag (int value, int check)
1938 {
1939   PRINTF_LIKE ((*as_error));
1940
1941   flag_code = (enum flag_code) value;
1942   if (flag_code == CODE_64BIT)
1943     {
1944       cpu_arch_flags.bitfield.cpu64 = 1;
1945       cpu_arch_flags.bitfield.cpuno64 = 0;
1946     }
1947   else
1948     {
1949       cpu_arch_flags.bitfield.cpu64 = 0;
1950       cpu_arch_flags.bitfield.cpuno64 = 1;
1951     }
1952   if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
1953     {
1954       if (check)
1955         as_error = as_fatal;
1956       else
1957         as_error = as_bad;
1958       (*as_error) (_("64bit mode not supported on `%s'."),
1959                    cpu_arch_name ? cpu_arch_name : default_arch);
1960     }
1961   if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
1962     {
1963       if (check)
1964         as_error = as_fatal;
1965       else
1966         as_error = as_bad;
1967       (*as_error) (_("32bit mode not supported on `%s'."),
1968                    cpu_arch_name ? cpu_arch_name : default_arch);
1969     }
1970   stackop_size = '\0';
1971 }
1972
1973 static void
1974 set_code_flag (int value)
1975 {
1976   update_code_flag (value, 0);
1977 }
1978
1979 static void
1980 set_16bit_gcc_code_flag (int new_code_flag)
1981 {
1982   flag_code = (enum flag_code) new_code_flag;
1983   if (flag_code != CODE_16BIT)
1984     abort ();
1985   cpu_arch_flags.bitfield.cpu64 = 0;
1986   cpu_arch_flags.bitfield.cpuno64 = 1;
1987   stackop_size = LONG_MNEM_SUFFIX;
1988 }
1989
1990 static void
1991 set_intel_syntax (int syntax_flag)
1992 {
1993   /* Find out if register prefixing is specified.  */
1994   int ask_naked_reg = 0;
1995
1996   SKIP_WHITESPACE ();
1997   if (!is_end_of_line[(unsigned char) *input_line_pointer])
1998     {
1999       char *string = input_line_pointer;
2000       int e = get_symbol_end ();
2001
2002       if (strcmp (string, "prefix") == 0)
2003         ask_naked_reg = 1;
2004       else if (strcmp (string, "noprefix") == 0)
2005         ask_naked_reg = -1;
2006       else
2007         as_bad (_("bad argument to syntax directive."));
2008       *input_line_pointer = e;
2009     }
2010   demand_empty_rest_of_line ();
2011
2012   intel_syntax = syntax_flag;
2013
2014   if (ask_naked_reg == 0)
2015     allow_naked_reg = (intel_syntax
2016                        && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2017   else
2018     allow_naked_reg = (ask_naked_reg < 0);
2019
2020   expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2021
2022   identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
2023   identifier_chars['$'] = intel_syntax ? '$' : 0;
2024   register_prefix = allow_naked_reg ? "" : "%";
2025 }
2026
2027 static void
2028 set_intel_mnemonic (int mnemonic_flag)
2029 {
2030   intel_mnemonic = mnemonic_flag;
2031 }
2032
2033 static void
2034 set_allow_index_reg (int flag)
2035 {
2036   allow_index_reg = flag;
2037 }
2038
2039 static void
2040 set_sse_check (int dummy ATTRIBUTE_UNUSED)
2041 {
2042   SKIP_WHITESPACE ();
2043
2044   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2045     {
2046       char *string = input_line_pointer;
2047       int e = get_symbol_end ();
2048
2049       if (strcmp (string, "none") == 0)
2050         sse_check = sse_check_none;
2051       else if (strcmp (string, "warning") == 0)
2052         sse_check = sse_check_warning;
2053       else if (strcmp (string, "error") == 0)
2054         sse_check = sse_check_error;
2055       else
2056         as_bad (_("bad argument to sse_check directive."));
2057       *input_line_pointer = e;
2058     }
2059   else
2060     as_bad (_("missing argument for sse_check directive"));
2061
2062   demand_empty_rest_of_line ();
2063 }
2064
2065 static void
2066 check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2067                            i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2068 {
2069 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2070   static const char *arch;
2071
2072   /* Intel LIOM is only supported on ELF.  */
2073   if (!IS_ELF)
2074     return;
2075
2076   if (!arch)
2077     {
2078       /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
2079          use default_arch.  */
2080       arch = cpu_arch_name;
2081       if (!arch)
2082         arch = default_arch;
2083     }
2084
2085   /* If we are targeting Intel L1OM, we must enable it.  */
2086   if (get_elf_backend_data (stdoutput)->elf_machine_code != EM_L1OM
2087       || new_flag.bitfield.cpul1om)
2088     return;
2089
2090   as_bad (_("`%s' is not supported on `%s'"), name, arch);
2091 #endif
2092 }
2093
2094 static void
2095 set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2096 {
2097   SKIP_WHITESPACE ();
2098
2099   if (!is_end_of_line[(unsigned char) *input_line_pointer])
2100     {
2101       char *string = input_line_pointer;
2102       int e = get_symbol_end ();
2103       unsigned int j;
2104       i386_cpu_flags flags;
2105
2106       for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2107         {
2108           if (strcmp (string, cpu_arch[j].name) == 0)
2109             {
2110               check_cpu_arch_compatible (string, cpu_arch[j].flags);
2111
2112               if (*string != '.')
2113                 {
2114                   cpu_arch_name = cpu_arch[j].name;
2115                   cpu_sub_arch_name = NULL;
2116                   cpu_arch_flags = cpu_arch[j].flags;
2117                   if (flag_code == CODE_64BIT)
2118                     {
2119                       cpu_arch_flags.bitfield.cpu64 = 1;
2120                       cpu_arch_flags.bitfield.cpuno64 = 0;
2121                     }
2122                   else
2123                     {
2124                       cpu_arch_flags.bitfield.cpu64 = 0;
2125                       cpu_arch_flags.bitfield.cpuno64 = 1;
2126                     }
2127                   cpu_arch_isa = cpu_arch[j].type;
2128                   cpu_arch_isa_flags = cpu_arch[j].flags;
2129                   if (!cpu_arch_tune_set)
2130                     {
2131                       cpu_arch_tune = cpu_arch_isa;
2132                       cpu_arch_tune_flags = cpu_arch_isa_flags;
2133                     }
2134                   break;
2135                 }
2136
2137               if (!cpu_arch[j].negated)
2138                 flags = cpu_flags_or (cpu_arch_flags,
2139                                       cpu_arch[j].flags);
2140               else
2141                 flags = cpu_flags_and_not (cpu_arch_flags,
2142                                            cpu_arch[j].flags);
2143               if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2144                 {
2145                   if (cpu_sub_arch_name)
2146                     {
2147                       char *name = cpu_sub_arch_name;
2148                       cpu_sub_arch_name = concat (name,
2149                                                   cpu_arch[j].name,
2150                                                   (const char *) NULL);
2151                       free (name);
2152                     }
2153                   else
2154                     cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
2155                   cpu_arch_flags = flags;
2156                 }
2157               *input_line_pointer = e;
2158               demand_empty_rest_of_line ();
2159               return;
2160             }
2161         }
2162       if (j >= ARRAY_SIZE (cpu_arch))
2163         as_bad (_("no such architecture: `%s'"), string);
2164
2165       *input_line_pointer = e;
2166     }
2167   else
2168     as_bad (_("missing cpu architecture"));
2169
2170   no_cond_jump_promotion = 0;
2171   if (*input_line_pointer == ','
2172       && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2173     {
2174       char *string = ++input_line_pointer;
2175       int e = get_symbol_end ();
2176
2177       if (strcmp (string, "nojumps") == 0)
2178         no_cond_jump_promotion = 1;
2179       else if (strcmp (string, "jumps") == 0)
2180         ;
2181       else
2182         as_bad (_("no such architecture modifier: `%s'"), string);
2183
2184       *input_line_pointer = e;
2185     }
2186
2187   demand_empty_rest_of_line ();
2188 }
2189
2190 enum bfd_architecture
2191 i386_arch (void)
2192 {
2193   if (cpu_arch_isa == PROCESSOR_L1OM)
2194     {
2195       if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2196           || flag_code != CODE_64BIT)
2197         as_fatal (_("Intel L1OM is 64bit ELF only"));
2198       return bfd_arch_l1om;
2199     }
2200   else
2201     return bfd_arch_i386;
2202 }
2203
2204 unsigned long
2205 i386_mach ()
2206 {
2207   if (!strcmp (default_arch, "x86_64"))
2208     {
2209       if (cpu_arch_isa == PROCESSOR_L1OM)
2210         {
2211           if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2212             as_fatal (_("Intel L1OM is 64bit ELF only"));
2213           return bfd_mach_l1om;
2214         }
2215       else
2216         return bfd_mach_x86_64;
2217     }
2218   else if (!strcmp (default_arch, "i386"))
2219     return bfd_mach_i386_i386;
2220   else
2221     as_fatal (_("Unknown architecture"));
2222 }
2223 \f
2224 void
2225 md_begin ()
2226 {
2227   const char *hash_err;
2228
2229   /* Initialize op_hash hash table.  */
2230   op_hash = hash_new ();
2231
2232   {
2233     const insn_template *optab;
2234     templates *core_optab;
2235
2236     /* Setup for loop.  */
2237     optab = i386_optab;
2238     core_optab = (templates *) xmalloc (sizeof (templates));
2239     core_optab->start = optab;
2240
2241     while (1)
2242       {
2243         ++optab;
2244         if (optab->name == NULL
2245             || strcmp (optab->name, (optab - 1)->name) != 0)
2246           {
2247             /* different name --> ship out current template list;
2248                add to hash table; & begin anew.  */
2249             core_optab->end = optab;
2250             hash_err = hash_insert (op_hash,
2251                                     (optab - 1)->name,
2252                                     (void *) core_optab);
2253             if (hash_err)
2254               {
2255                 as_fatal (_("Internal Error:  Can't hash %s: %s"),
2256                           (optab - 1)->name,
2257                           hash_err);
2258               }
2259             if (optab->name == NULL)
2260               break;
2261             core_optab = (templates *) xmalloc (sizeof (templates));
2262             core_optab->start = optab;
2263           }
2264       }
2265   }
2266
2267   /* Initialize reg_hash hash table.  */
2268   reg_hash = hash_new ();
2269   {
2270     const reg_entry *regtab;
2271     unsigned int regtab_size = i386_regtab_size;
2272
2273     for (regtab = i386_regtab; regtab_size--; regtab++)
2274       {
2275         hash_err = hash_insert (reg_hash, regtab->reg_name, (void *) regtab);
2276         if (hash_err)
2277           as_fatal (_("Internal Error:  Can't hash %s: %s"),
2278                     regtab->reg_name,
2279                     hash_err);
2280       }
2281   }
2282
2283   /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
2284   {
2285     int c;
2286     char *p;
2287
2288     for (c = 0; c < 256; c++)
2289       {
2290         if (ISDIGIT (c))
2291           {
2292             digit_chars[c] = c;
2293             mnemonic_chars[c] = c;
2294             register_chars[c] = c;
2295             operand_chars[c] = c;
2296           }
2297         else if (ISLOWER (c))
2298           {
2299             mnemonic_chars[c] = c;
2300             register_chars[c] = c;
2301             operand_chars[c] = c;
2302           }
2303         else if (ISUPPER (c))
2304           {
2305             mnemonic_chars[c] = TOLOWER (c);
2306             register_chars[c] = mnemonic_chars[c];
2307             operand_chars[c] = c;
2308           }
2309
2310         if (ISALPHA (c) || ISDIGIT (c))
2311           identifier_chars[c] = c;
2312         else if (c >= 128)
2313           {
2314             identifier_chars[c] = c;
2315             operand_chars[c] = c;
2316           }
2317       }
2318
2319 #ifdef LEX_AT
2320     identifier_chars['@'] = '@';
2321 #endif
2322 #ifdef LEX_QM
2323     identifier_chars['?'] = '?';
2324     operand_chars['?'] = '?';
2325 #endif
2326     digit_chars['-'] = '-';
2327     mnemonic_chars['_'] = '_';
2328     mnemonic_chars['-'] = '-';
2329     mnemonic_chars['.'] = '.';
2330     identifier_chars['_'] = '_';
2331     identifier_chars['.'] = '.';
2332
2333     for (p = operand_special_chars; *p != '\0'; p++)
2334       operand_chars[(unsigned char) *p] = *p;
2335   }
2336
2337 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2338   if (IS_ELF)
2339     {
2340       record_alignment (text_section, 2);
2341       record_alignment (data_section, 2);
2342       record_alignment (bss_section, 2);
2343     }
2344 #endif
2345
2346   if (flag_code == CODE_64BIT)
2347     {
2348       x86_dwarf2_return_column = 16;
2349       x86_cie_data_alignment = -8;
2350     }
2351   else
2352     {
2353       x86_dwarf2_return_column = 8;
2354       x86_cie_data_alignment = -4;
2355     }
2356 }
2357
2358 void
2359 i386_print_statistics (FILE *file)
2360 {
2361   hash_print_statistics (file, "i386 opcode", op_hash);
2362   hash_print_statistics (file, "i386 register", reg_hash);
2363 }
2364 \f
2365 #ifdef DEBUG386
2366
2367 /* Debugging routines for md_assemble.  */
2368 static void pte (insn_template *);
2369 static void pt (i386_operand_type);
2370 static void pe (expressionS *);
2371 static void ps (symbolS *);
2372
2373 static void
2374 pi (char *line, i386_insn *x)
2375 {
2376   unsigned int j;
2377
2378   fprintf (stdout, "%s: template ", line);
2379   pte (&x->tm);
2380   fprintf (stdout, "  address: base %s  index %s  scale %x\n",
2381            x->base_reg ? x->base_reg->reg_name : "none",
2382            x->index_reg ? x->index_reg->reg_name : "none",
2383            x->log2_scale_factor);
2384   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
2385            x->rm.mode, x->rm.reg, x->rm.regmem);
2386   fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
2387            x->sib.base, x->sib.index, x->sib.scale);
2388   fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
2389            (x->rex & REX_W) != 0,
2390            (x->rex & REX_R) != 0,
2391            (x->rex & REX_X) != 0,
2392            (x->rex & REX_B) != 0);
2393   for (j = 0; j < x->operands; j++)
2394     {
2395       fprintf (stdout, "    #%d:  ", j + 1);
2396       pt (x->types[j]);
2397       fprintf (stdout, "\n");
2398       if (x->types[j].bitfield.reg8
2399           || x->types[j].bitfield.reg16
2400           || x->types[j].bitfield.reg32
2401           || x->types[j].bitfield.reg64
2402           || x->types[j].bitfield.regmmx
2403           || x->types[j].bitfield.regxmm
2404           || x->types[j].bitfield.regymm
2405           || x->types[j].bitfield.sreg2
2406           || x->types[j].bitfield.sreg3
2407           || x->types[j].bitfield.control
2408           || x->types[j].bitfield.debug
2409           || x->types[j].bitfield.test)
2410         fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
2411       if (operand_type_check (x->types[j], imm))
2412         pe (x->op[j].imms);
2413       if (operand_type_check (x->types[j], disp))
2414         pe (x->op[j].disps);
2415     }
2416 }
2417
2418 static void
2419 pte (insn_template *t)
2420 {
2421   unsigned int j;
2422   fprintf (stdout, " %d operands ", t->operands);
2423   fprintf (stdout, "opcode %x ", t->base_opcode);
2424   if (t->extension_opcode != None)
2425     fprintf (stdout, "ext %x ", t->extension_opcode);
2426   if (t->opcode_modifier.d)
2427     fprintf (stdout, "D");
2428   if (t->opcode_modifier.w)
2429     fprintf (stdout, "W");
2430   fprintf (stdout, "\n");
2431   for (j = 0; j < t->operands; j++)
2432     {
2433       fprintf (stdout, "    #%d type ", j + 1);
2434       pt (t->operand_types[j]);
2435       fprintf (stdout, "\n");
2436     }
2437 }
2438
2439 static void
2440 pe (expressionS *e)
2441 {
2442   fprintf (stdout, "    operation     %d\n", e->X_op);
2443   fprintf (stdout, "    add_number    %ld (%lx)\n",
2444            (long) e->X_add_number, (long) e->X_add_number);
2445   if (e->X_add_symbol)
2446     {
2447       fprintf (stdout, "    add_symbol    ");
2448       ps (e->X_add_symbol);
2449       fprintf (stdout, "\n");
2450     }
2451   if (e->X_op_symbol)
2452     {
2453       fprintf (stdout, "    op_symbol    ");
2454       ps (e->X_op_symbol);
2455       fprintf (stdout, "\n");
2456     }
2457 }
2458
2459 static void
2460 ps (symbolS *s)
2461 {
2462   fprintf (stdout, "%s type %s%s",
2463            S_GET_NAME (s),
2464            S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
2465            segment_name (S_GET_SEGMENT (s)));
2466 }
2467
2468 static struct type_name
2469   {
2470     i386_operand_type mask;
2471     const char *name;
2472   }
2473 const type_names[] =
2474 {
2475   { OPERAND_TYPE_REG8, "r8" },
2476   { OPERAND_TYPE_REG16, "r16" },
2477   { OPERAND_TYPE_REG32, "r32" },
2478   { OPERAND_TYPE_REG64, "r64" },
2479   { OPERAND_TYPE_IMM8, "i8" },
2480   { OPERAND_TYPE_IMM8, "i8s" },
2481   { OPERAND_TYPE_IMM16, "i16" },
2482   { OPERAND_TYPE_IMM32, "i32" },
2483   { OPERAND_TYPE_IMM32S, "i32s" },
2484   { OPERAND_TYPE_IMM64, "i64" },
2485   { OPERAND_TYPE_IMM1, "i1" },
2486   { OPERAND_TYPE_BASEINDEX, "BaseIndex" },
2487   { OPERAND_TYPE_DISP8, "d8" },
2488   { OPERAND_TYPE_DISP16, "d16" },
2489   { OPERAND_TYPE_DISP32, "d32" },
2490   { OPERAND_TYPE_DISP32S, "d32s" },
2491   { OPERAND_TYPE_DISP64, "d64" },
2492   { OPERAND_TYPE_INOUTPORTREG, "InOutPortReg" },
2493   { OPERAND_TYPE_SHIFTCOUNT, "ShiftCount" },
2494   { OPERAND_TYPE_CONTROL, "control reg" },
2495   { OPERAND_TYPE_TEST, "test reg" },
2496   { OPERAND_TYPE_DEBUG, "debug reg" },
2497   { OPERAND_TYPE_FLOATREG, "FReg" },
2498   { OPERAND_TYPE_FLOATACC, "FAcc" },
2499   { OPERAND_TYPE_SREG2, "SReg2" },
2500   { OPERAND_TYPE_SREG3, "SReg3" },
2501   { OPERAND_TYPE_ACC, "Acc" },
2502   { OPERAND_TYPE_JUMPABSOLUTE, "Jump Absolute" },
2503   { OPERAND_TYPE_REGMMX, "rMMX" },
2504   { OPERAND_TYPE_REGXMM, "rXMM" },
2505   { OPERAND_TYPE_REGYMM, "rYMM" },
2506   { OPERAND_TYPE_ESSEG, "es" },
2507 };
2508
2509 static void
2510 pt (i386_operand_type t)
2511 {
2512   unsigned int j;
2513   i386_operand_type a;
2514
2515   for (j = 0; j < ARRAY_SIZE (type_names); j++)
2516     {
2517       a = operand_type_and (t, type_names[j].mask);
2518       if (!operand_type_all_zero (&a))
2519         fprintf (stdout, "%s, ",  type_names[j].name);
2520     }
2521   fflush (stdout);
2522 }
2523
2524 #endif /* DEBUG386 */
2525 \f
2526 static bfd_reloc_code_real_type
2527 reloc (unsigned int size,
2528        int pcrel,
2529        int sign,
2530        bfd_reloc_code_real_type other)
2531 {
2532   if (other != NO_RELOC)
2533     {
2534       reloc_howto_type *rel;
2535
2536       if (size == 8)
2537         switch (other)
2538           {
2539           case BFD_RELOC_X86_64_GOT32:
2540             return BFD_RELOC_X86_64_GOT64;
2541             break;
2542           case BFD_RELOC_X86_64_PLTOFF64:
2543             return BFD_RELOC_X86_64_PLTOFF64;
2544             break;
2545           case BFD_RELOC_X86_64_GOTPC32:
2546             other = BFD_RELOC_X86_64_GOTPC64;
2547             break;
2548           case BFD_RELOC_X86_64_GOTPCREL:
2549             other = BFD_RELOC_X86_64_GOTPCREL64;
2550             break;
2551           case BFD_RELOC_X86_64_TPOFF32:
2552             other = BFD_RELOC_X86_64_TPOFF64;
2553             break;
2554           case BFD_RELOC_X86_64_DTPOFF32:
2555             other = BFD_RELOC_X86_64_DTPOFF64;
2556             break;
2557           default:
2558             break;
2559           }
2560
2561       /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
2562       if (size == 4 && flag_code != CODE_64BIT)
2563         sign = -1;
2564
2565       rel = bfd_reloc_type_lookup (stdoutput, other);
2566       if (!rel)
2567         as_bad (_("unknown relocation (%u)"), other);
2568       else if (size != bfd_get_reloc_size (rel))
2569         as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
2570                 bfd_get_reloc_size (rel),
2571                 size);
2572       else if (pcrel && !rel->pc_relative)
2573         as_bad (_("non-pc-relative relocation for pc-relative field"));
2574       else if ((rel->complain_on_overflow == complain_overflow_signed
2575                 && !sign)
2576                || (rel->complain_on_overflow == complain_overflow_unsigned
2577                    && sign > 0))
2578         as_bad (_("relocated field and relocation type differ in signedness"));
2579       else
2580         return other;
2581       return NO_RELOC;
2582     }
2583
2584   if (pcrel)
2585     {
2586       if (!sign)
2587         as_bad (_("there are no unsigned pc-relative relocations"));
2588       switch (size)
2589         {
2590         case 1: return BFD_RELOC_8_PCREL;
2591         case 2: return BFD_RELOC_16_PCREL;
2592         case 4: return BFD_RELOC_32_PCREL;
2593         case 8: return BFD_RELOC_64_PCREL;
2594         }
2595       as_bad (_("cannot do %u byte pc-relative relocation"), size);
2596     }
2597   else
2598     {
2599       if (sign > 0)
2600         switch (size)
2601           {
2602           case 4: return BFD_RELOC_X86_64_32S;
2603           }
2604       else
2605         switch (size)
2606           {
2607           case 1: return BFD_RELOC_8;
2608           case 2: return BFD_RELOC_16;
2609           case 4: return BFD_RELOC_32;
2610           case 8: return BFD_RELOC_64;
2611           }
2612       as_bad (_("cannot do %s %u byte relocation"),
2613               sign > 0 ? "signed" : "unsigned", size);
2614     }
2615
2616   return NO_RELOC;
2617 }
2618
2619 /* Here we decide which fixups can be adjusted to make them relative to
2620    the beginning of the section instead of the symbol.  Basically we need
2621    to make sure that the dynamic relocations are done correctly, so in
2622    some cases we force the original symbol to be used.  */
2623
2624 int
2625 tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
2626 {
2627 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2628   if (!IS_ELF)
2629     return 1;
2630
2631   /* Don't adjust pc-relative references to merge sections in 64-bit
2632      mode.  */
2633   if (use_rela_relocations
2634       && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
2635       && fixP->fx_pcrel)
2636     return 0;
2637
2638   /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
2639      and changed later by validate_fix.  */
2640   if (GOT_symbol && fixP->fx_subsy == GOT_symbol
2641       && fixP->fx_r_type == BFD_RELOC_32_PCREL)
2642     return 0;
2643
2644   /* adjust_reloc_syms doesn't know about the GOT.  */
2645   if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
2646       || fixP->fx_r_type == BFD_RELOC_386_PLT32
2647       || fixP->fx_r_type == BFD_RELOC_386_GOT32
2648       || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
2649       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
2650       || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
2651       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
2652       || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
2653       || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
2654       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
2655       || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
2656       || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
2657       || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
2658       || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
2659       || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
2660       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
2661       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
2662       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
2663       || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
2664       || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
2665       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
2666       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
2667       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
2668       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
2669       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
2670       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
2671       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2672       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2673     return 0;
2674 #endif
2675   return 1;
2676 }
2677
2678 static int
2679 intel_float_operand (const char *mnemonic)
2680 {
2681   /* Note that the value returned is meaningful only for opcodes with (memory)
2682      operands, hence the code here is free to improperly handle opcodes that
2683      have no operands (for better performance and smaller code). */
2684
2685   if (mnemonic[0] != 'f')
2686     return 0; /* non-math */
2687
2688   switch (mnemonic[1])
2689     {
2690     /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
2691        the fs segment override prefix not currently handled because no
2692        call path can make opcodes without operands get here */
2693     case 'i':
2694       return 2 /* integer op */;
2695     case 'l':
2696       if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
2697         return 3; /* fldcw/fldenv */
2698       break;
2699     case 'n':
2700       if (mnemonic[2] != 'o' /* fnop */)
2701         return 3; /* non-waiting control op */
2702       break;
2703     case 'r':
2704       if (mnemonic[2] == 's')
2705         return 3; /* frstor/frstpm */
2706       break;
2707     case 's':
2708       if (mnemonic[2] == 'a')
2709         return 3; /* fsave */
2710       if (mnemonic[2] == 't')
2711         {
2712           switch (mnemonic[3])
2713             {
2714             case 'c': /* fstcw */
2715             case 'd': /* fstdw */
2716             case 'e': /* fstenv */
2717             case 's': /* fsts[gw] */
2718               return 3;
2719             }
2720         }
2721       break;
2722     case 'x':
2723       if (mnemonic[2] == 'r' || mnemonic[2] == 's')
2724         return 0; /* fxsave/fxrstor are not really math ops */
2725       break;
2726     }
2727
2728   return 1;
2729 }
2730
2731 /* Build the VEX prefix.  */
2732
2733 static void
2734 build_vex_prefix (const insn_template *t)
2735 {
2736   unsigned int register_specifier;
2737   unsigned int implied_prefix;
2738   unsigned int vector_length;
2739
2740   /* Check register specifier.  */
2741   if (i.vex.register_specifier)
2742     {
2743       register_specifier = i.vex.register_specifier->reg_num;
2744       if ((i.vex.register_specifier->reg_flags & RegRex))
2745         register_specifier += 8;
2746       register_specifier = ~register_specifier & 0xf;
2747     }
2748   else
2749     register_specifier = 0xf;
2750
2751   /* Use 2-byte VEX prefix by swappping destination and source
2752      operand.  */
2753   if (!i.swap_operand
2754       && i.operands == i.reg_operands
2755       && i.tm.opcode_modifier.vexopcode == VEX0F
2756       && i.tm.opcode_modifier.s
2757       && i.rex == REX_B)
2758     {
2759       unsigned int xchg = i.operands - 1;
2760       union i386_op temp_op;
2761       i386_operand_type temp_type;
2762
2763       temp_type = i.types[xchg];
2764       i.types[xchg] = i.types[0];
2765       i.types[0] = temp_type;
2766       temp_op = i.op[xchg];
2767       i.op[xchg] = i.op[0];
2768       i.op[0] = temp_op;
2769
2770       gas_assert (i.rm.mode == 3);
2771
2772       i.rex = REX_R;
2773       xchg = i.rm.regmem;
2774       i.rm.regmem = i.rm.reg;
2775       i.rm.reg = xchg;
2776
2777       /* Use the next insn.  */
2778       i.tm = t[1];
2779     }
2780
2781   if (i.tm.opcode_modifier.vex == VEXScalar)
2782     vector_length = avxscalar;
2783   else
2784     vector_length = i.tm.opcode_modifier.vex == VEX256 ? 1 : 0;
2785
2786   switch ((i.tm.base_opcode >> 8) & 0xff)
2787     {
2788     case 0:
2789       implied_prefix = 0;
2790       break;
2791     case DATA_PREFIX_OPCODE:
2792       implied_prefix = 1;
2793       break;
2794     case REPE_PREFIX_OPCODE:
2795       implied_prefix = 2;
2796       break;
2797     case REPNE_PREFIX_OPCODE:
2798       implied_prefix = 3;
2799       break;
2800     default:
2801       abort ();
2802     }
2803
2804   /* Use 2-byte VEX prefix if possible.  */
2805   if (i.tm.opcode_modifier.vexopcode == VEX0F
2806       && i.tm.opcode_modifier.vexw != VEXW1
2807       && (i.rex & (REX_W | REX_X | REX_B)) == 0)
2808     {
2809       /* 2-byte VEX prefix.  */
2810       unsigned int r;
2811
2812       i.vex.length = 2;
2813       i.vex.bytes[0] = 0xc5;
2814
2815       /* Check the REX.R bit.  */
2816       r = (i.rex & REX_R) ? 0 : 1;
2817       i.vex.bytes[1] = (r << 7
2818                         | register_specifier << 3
2819                         | vector_length << 2
2820                         | implied_prefix);
2821     }
2822   else
2823     {
2824       /* 3-byte VEX prefix.  */
2825       unsigned int m, w;
2826
2827       i.vex.length = 3;
2828
2829       switch (i.tm.opcode_modifier.vexopcode)
2830         {
2831         case VEX0F:
2832           m = 0x1;
2833           i.vex.bytes[0] = 0xc4;
2834           break;
2835         case VEX0F38:
2836           m = 0x2;
2837           i.vex.bytes[0] = 0xc4;
2838           break;
2839         case VEX0F3A:
2840           m = 0x3;
2841           i.vex.bytes[0] = 0xc4;
2842           break;
2843         case XOP08:
2844           m = 0x8;
2845           i.vex.bytes[0] = 0x8f;
2846           break;
2847         case XOP09:
2848           m = 0x9;
2849           i.vex.bytes[0] = 0x8f;
2850           break;
2851         case XOP0A:
2852           m = 0xa;
2853           i.vex.bytes[0] = 0x8f;
2854           break;
2855         default:
2856           abort ();
2857         }
2858
2859       /* The high 3 bits of the second VEX byte are 1's compliment
2860          of RXB bits from REX.  */
2861       i.vex.bytes[1] = (~i.rex & 0x7) << 5 | m;
2862
2863       /* Check the REX.W bit.  */
2864       w = (i.rex & REX_W) ? 1 : 0;
2865       if (i.tm.opcode_modifier.vexw)
2866         {
2867           if (w)
2868             abort ();
2869
2870           if (i.tm.opcode_modifier.vexw == VEXW1)
2871             w = 1;
2872         }
2873
2874       i.vex.bytes[2] = (w << 7
2875                         | register_specifier << 3
2876                         | vector_length << 2
2877                         | implied_prefix);
2878     }
2879 }
2880
2881 static void
2882 process_immext (void)
2883 {
2884   expressionS *exp;
2885
2886   if (i.tm.cpu_flags.bitfield.cpusse3 && i.operands > 0)
2887     {
2888       /* SSE3 Instructions have the fixed operands with an opcode
2889          suffix which is coded in the same place as an 8-bit immediate
2890          field would be.  Here we check those operands and remove them
2891          afterwards.  */
2892       unsigned int x;
2893
2894       for (x = 0; x < i.operands; x++)
2895         if (i.op[x].regs->reg_num != x)
2896           as_bad (_("can't use register '%s%s' as operand %d in '%s'."),
2897                   register_prefix, i.op[x].regs->reg_name, x + 1,
2898                   i.tm.name);
2899
2900       i.operands = 0;
2901     }
2902
2903   /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
2904      which is coded in the same place as an 8-bit immediate field
2905      would be.  Here we fake an 8-bit immediate operand from the
2906      opcode suffix stored in tm.extension_opcode.
2907
2908      AVX instructions also use this encoding, for some of
2909      3 argument instructions.  */
2910
2911   gas_assert (i.imm_operands == 0
2912               && (i.operands <= 2
2913                   || (i.tm.opcode_modifier.vex
2914                       && i.operands <= 4)));
2915
2916   exp = &im_expressions[i.imm_operands++];
2917   i.op[i.operands].imms = exp;
2918   i.types[i.operands] = imm8;
2919   i.operands++;
2920   exp->X_op = O_constant;
2921   exp->X_add_number = i.tm.extension_opcode;
2922   i.tm.extension_opcode = None;
2923 }
2924
2925 /* This is the guts of the machine-dependent assembler.  LINE points to a
2926    machine dependent instruction.  This function is supposed to emit
2927    the frags/bytes it assembles to.  */
2928
2929 void
2930 md_assemble (char *line)
2931 {
2932   unsigned int j;
2933   char mnemonic[MAX_MNEM_SIZE];
2934   const insn_template *t;
2935
2936   /* Initialize globals.  */
2937   memset (&i, '\0', sizeof (i));
2938   for (j = 0; j < MAX_OPERANDS; j++)
2939     i.reloc[j] = NO_RELOC;
2940   memset (disp_expressions, '\0', sizeof (disp_expressions));
2941   memset (im_expressions, '\0', sizeof (im_expressions));
2942   save_stack_p = save_stack;
2943
2944   /* First parse an instruction mnemonic & call i386_operand for the operands.
2945      We assume that the scrubber has arranged it so that line[0] is the valid
2946      start of a (possibly prefixed) mnemonic.  */
2947
2948   line = parse_insn (line, mnemonic);
2949   if (line == NULL)
2950     return;
2951
2952   line = parse_operands (line, mnemonic);
2953   this_operand = -1;
2954   if (line == NULL)
2955     return;
2956
2957   /* Now we've parsed the mnemonic into a set of templates, and have the
2958      operands at hand.  */
2959
2960   /* All intel opcodes have reversed operands except for "bound" and
2961      "enter".  We also don't reverse intersegment "jmp" and "call"
2962      instructions with 2 immediate operands so that the immediate segment
2963      precedes the offset, as it does when in AT&T mode. */
2964   if (intel_syntax
2965       && i.operands > 1
2966       && (strcmp (mnemonic, "bound") != 0)
2967       && (strcmp (mnemonic, "invlpga") != 0)
2968       && !(operand_type_check (i.types[0], imm)
2969            && operand_type_check (i.types[1], imm)))
2970     swap_operands ();
2971
2972   /* The order of the immediates should be reversed
2973      for 2 immediates extrq and insertq instructions */
2974   if (i.imm_operands == 2
2975       && (strcmp (mnemonic, "extrq") == 0
2976           || strcmp (mnemonic, "insertq") == 0))
2977       swap_2_operands (0, 1);
2978
2979   if (i.imm_operands)
2980     optimize_imm ();
2981
2982   /* Don't optimize displacement for movabs since it only takes 64bit
2983      displacement.  */
2984   if (i.disp_operands
2985       && (flag_code != CODE_64BIT
2986           || strcmp (mnemonic, "movabs") != 0))
2987     optimize_disp ();
2988
2989   /* Next, we find a template that matches the given insn,
2990      making sure the overlap of the given operands types is consistent
2991      with the template operand types.  */
2992
2993   if (!(t = match_template ()))
2994     return;
2995
2996   if (sse_check != sse_check_none
2997       && !i.tm.opcode_modifier.noavx
2998       && (i.tm.cpu_flags.bitfield.cpusse
2999           || i.tm.cpu_flags.bitfield.cpusse2
3000           || i.tm.cpu_flags.bitfield.cpusse3
3001           || i.tm.cpu_flags.bitfield.cpussse3
3002           || i.tm.cpu_flags.bitfield.cpusse4_1
3003           || i.tm.cpu_flags.bitfield.cpusse4_2))
3004     {
3005       (sse_check == sse_check_warning
3006        ? as_warn
3007        : as_bad) (_("SSE instruction `%s' is used"), i.tm.name);
3008     }
3009
3010   /* Zap movzx and movsx suffix.  The suffix has been set from
3011      "word ptr" or "byte ptr" on the source operand in Intel syntax
3012      or extracted from mnemonic in AT&T syntax.  But we'll use
3013      the destination register to choose the suffix for encoding.  */
3014   if ((i.tm.base_opcode & ~9) == 0x0fb6)
3015     {
3016       /* In Intel syntax, there must be a suffix.  In AT&T syntax, if
3017          there is no suffix, the default will be byte extension.  */
3018       if (i.reg_operands != 2
3019           && !i.suffix
3020           && intel_syntax)
3021         as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
3022
3023       i.suffix = 0;
3024     }
3025
3026   if (i.tm.opcode_modifier.fwait)
3027     if (!add_prefix (FWAIT_OPCODE))
3028       return;
3029
3030   /* Check for lock without a lockable instruction.  Destination operand
3031      must be memory unless it is xchg (0x86).  */
3032   if (i.prefix[LOCK_PREFIX]
3033       && (!i.tm.opcode_modifier.islockable
3034           || i.mem_operands == 0
3035           || (i.tm.base_opcode != 0x86
3036               && !operand_type_check (i.types[i.operands - 1], anymem))))
3037     {
3038       as_bad (_("expecting lockable instruction after `lock'"));
3039       return;
3040     }
3041
3042   /* Check string instruction segment overrides.  */
3043   if (i.tm.opcode_modifier.isstring && i.mem_operands != 0)
3044     {
3045       if (!check_string ())
3046         return;
3047       i.disp_operands = 0;
3048     }
3049
3050   if (!process_suffix ())
3051     return;
3052
3053   /* Update operand types.  */
3054   for (j = 0; j < i.operands; j++)
3055     i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3056
3057   /* Make still unresolved immediate matches conform to size of immediate
3058      given in i.suffix.  */
3059   if (!finalize_imm ())
3060     return;
3061
3062   if (i.types[0].bitfield.imm1)
3063     i.imm_operands = 0; /* kludge for shift insns.  */
3064
3065   /* We only need to check those implicit registers for instructions
3066      with 3 operands or less.  */
3067   if (i.operands <= 3)
3068     for (j = 0; j < i.operands; j++)
3069       if (i.types[j].bitfield.inoutportreg
3070           || i.types[j].bitfield.shiftcount
3071           || i.types[j].bitfield.acc
3072           || i.types[j].bitfield.floatacc)
3073         i.reg_operands--;
3074
3075   /* ImmExt should be processed after SSE2AVX.  */
3076   if (!i.tm.opcode_modifier.sse2avx
3077       && i.tm.opcode_modifier.immext)
3078     process_immext ();
3079
3080   /* For insns with operands there are more diddles to do to the opcode.  */
3081   if (i.operands)
3082     {
3083       if (!process_operands ())
3084         return;
3085     }
3086   else if (!quiet_warnings && i.tm.opcode_modifier.ugh)
3087     {
3088       /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
3089       as_warn (_("translating to `%sp'"), i.tm.name);
3090     }
3091
3092   if (i.tm.opcode_modifier.vex)
3093     build_vex_prefix (t);
3094
3095   /* Handle conversion of 'int $3' --> special int3 insn.  XOP or FMA4
3096      instructions may define INT_OPCODE as well, so avoid this corner
3097      case for those instructions that use MODRM.  */
3098   if (i.tm.base_opcode == INT_OPCODE
3099       && !i.tm.opcode_modifier.modrm
3100       && i.op[0].imms->X_add_number == 3)
3101     {
3102       i.tm.base_opcode = INT3_OPCODE;
3103       i.imm_operands = 0;
3104     }
3105
3106   if ((i.tm.opcode_modifier.jump
3107        || i.tm.opcode_modifier.jumpbyte
3108        || i.tm.opcode_modifier.jumpdword)
3109       && i.op[0].disps->X_op == O_constant)
3110     {
3111       /* Convert "jmp constant" (and "call constant") to a jump (call) to
3112          the absolute address given by the constant.  Since ix86 jumps and
3113          calls are pc relative, we need to generate a reloc.  */
3114       i.op[0].disps->X_add_symbol = &abs_symbol;
3115       i.op[0].disps->X_op = O_symbol;
3116     }
3117
3118   if (i.tm.opcode_modifier.rex64)
3119     i.rex |= REX_W;
3120
3121   /* For 8 bit registers we need an empty rex prefix.  Also if the
3122      instruction already has a prefix, we need to convert old
3123      registers to new ones.  */
3124
3125   if ((i.types[0].bitfield.reg8
3126        && (i.op[0].regs->reg_flags & RegRex64) != 0)
3127       || (i.types[1].bitfield.reg8
3128           && (i.op[1].regs->reg_flags & RegRex64) != 0)
3129       || ((i.types[0].bitfield.reg8
3130            || i.types[1].bitfield.reg8)
3131           && i.rex != 0))
3132     {
3133       int x;
3134
3135       i.rex |= REX_OPCODE;
3136       for (x = 0; x < 2; x++)
3137         {
3138           /* Look for 8 bit operand that uses old registers.  */
3139           if (i.types[x].bitfield.reg8
3140               && (i.op[x].regs->reg_flags & RegRex64) == 0)
3141             {
3142               /* In case it is "hi" register, give up.  */
3143               if (i.op[x].regs->reg_num > 3)
3144                 as_bad (_("can't encode register '%s%s' in an "
3145                           "instruction requiring REX prefix."),
3146                         register_prefix, i.op[x].regs->reg_name);
3147
3148               /* Otherwise it is equivalent to the extended register.
3149                  Since the encoding doesn't change this is merely
3150                  cosmetic cleanup for debug output.  */
3151
3152               i.op[x].regs = i.op[x].regs + 8;
3153             }
3154         }
3155     }
3156
3157   if (i.rex != 0)
3158     add_prefix (REX_OPCODE | i.rex);
3159
3160   /* We are ready to output the insn.  */
3161   output_insn ();
3162 }
3163
3164 static char *
3165 parse_insn (char *line, char *mnemonic)
3166 {
3167   char *l = line;
3168   char *token_start = l;
3169   char *mnem_p;
3170   int supported;
3171   const insn_template *t;
3172   char *dot_p = NULL;
3173
3174   /* Non-zero if we found a prefix only acceptable with string insns.  */
3175   const char *expecting_string_instruction = NULL;
3176
3177   while (1)
3178     {
3179       mnem_p = mnemonic;
3180       while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
3181         {
3182           if (*mnem_p == '.')
3183             dot_p = mnem_p;
3184           mnem_p++;
3185           if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
3186             {
3187               as_bad (_("no such instruction: `%s'"), token_start);
3188               return NULL;
3189             }
3190           l++;
3191         }
3192       if (!is_space_char (*l)
3193           && *l != END_OF_INSN
3194           && (intel_syntax
3195               || (*l != PREFIX_SEPARATOR
3196                   && *l != ',')))
3197         {
3198           as_bad (_("invalid character %s in mnemonic"),
3199                   output_invalid (*l));
3200           return NULL;
3201         }
3202       if (token_start == l)
3203         {
3204           if (!intel_syntax && *l == PREFIX_SEPARATOR)
3205             as_bad (_("expecting prefix; got nothing"));
3206           else
3207             as_bad (_("expecting mnemonic; got nothing"));
3208           return NULL;
3209         }
3210
3211       /* Look up instruction (or prefix) via hash table.  */
3212       current_templates = (const templates *) hash_find (op_hash, mnemonic);
3213
3214       if (*l != END_OF_INSN
3215           && (!is_space_char (*l) || l[1] != END_OF_INSN)
3216           && current_templates
3217           && current_templates->start->opcode_modifier.isprefix)
3218         {
3219           if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
3220             {
3221               as_bad ((flag_code != CODE_64BIT
3222                        ? _("`%s' is only supported in 64-bit mode")
3223                        : _("`%s' is not supported in 64-bit mode")),
3224                       current_templates->start->name);
3225               return NULL;
3226             }
3227           /* If we are in 16-bit mode, do not allow addr16 or data16.
3228              Similarly, in 32-bit mode, do not allow addr32 or data32.  */
3229           if ((current_templates->start->opcode_modifier.size16
3230                || current_templates->start->opcode_modifier.size32)
3231               && flag_code != CODE_64BIT
3232               && (current_templates->start->opcode_modifier.size32
3233                   ^ (flag_code == CODE_16BIT)))
3234             {
3235               as_bad (_("redundant %s prefix"),
3236                       current_templates->start->name);
3237               return NULL;
3238             }
3239           /* Add prefix, checking for repeated prefixes.  */
3240           switch (add_prefix (current_templates->start->base_opcode))
3241             {
3242             case PREFIX_EXIST:
3243               return NULL;
3244             case PREFIX_REP:
3245               expecting_string_instruction = current_templates->start->name;
3246               break;
3247             default:
3248               break;
3249             }
3250           /* Skip past PREFIX_SEPARATOR and reset token_start.  */
3251           token_start = ++l;
3252         }
3253       else
3254         break;
3255     }
3256
3257   if (!current_templates)
3258     {
3259       /* Check if we should swap operand in encoding.  */
3260       if (mnem_p - 2 == dot_p && dot_p[1] == 's')
3261         i.swap_operand = 1;
3262       else
3263         goto check_suffix;
3264       mnem_p = dot_p;
3265       *dot_p = '\0';
3266       current_templates = (const templates *) hash_find (op_hash, mnemonic);
3267     }
3268
3269   if (!current_templates)
3270     {
3271 check_suffix:
3272       /* See if we can get a match by trimming off a suffix.  */
3273       switch (mnem_p[-1])
3274         {
3275         case WORD_MNEM_SUFFIX:
3276           if (intel_syntax && (intel_float_operand (mnemonic) & 2))
3277             i.suffix = SHORT_MNEM_SUFFIX;
3278           else
3279         case BYTE_MNEM_SUFFIX:
3280         case QWORD_MNEM_SUFFIX:
3281           i.suffix = mnem_p[-1];
3282           mnem_p[-1] = '\0';
3283           current_templates = (const templates *) hash_find (op_hash,
3284                                                              mnemonic);
3285           break;
3286         case SHORT_MNEM_SUFFIX:
3287         case LONG_MNEM_SUFFIX:
3288           if (!intel_syntax)
3289             {
3290               i.suffix = mnem_p[-1];
3291               mnem_p[-1] = '\0';
3292               current_templates = (const templates *) hash_find (op_hash,
3293                                                                  mnemonic);
3294             }
3295           break;
3296
3297           /* Intel Syntax.  */
3298         case 'd':
3299           if (intel_syntax)
3300             {
3301               if (intel_float_operand (mnemonic) == 1)
3302                 i.suffix = SHORT_MNEM_SUFFIX;
3303               else
3304                 i.suffix = LONG_MNEM_SUFFIX;
3305               mnem_p[-1] = '\0';
3306               current_templates = (const templates *) hash_find (op_hash,
3307                                                                  mnemonic);
3308             }
3309           break;
3310         }
3311       if (!current_templates)
3312         {
3313           as_bad (_("no such instruction: `%s'"), token_start);
3314           return NULL;
3315         }
3316     }
3317
3318   if (current_templates->start->opcode_modifier.jump
3319       || current_templates->start->opcode_modifier.jumpbyte)
3320     {
3321       /* Check for a branch hint.  We allow ",pt" and ",pn" for
3322          predict taken and predict not taken respectively.
3323          I'm not sure that branch hints actually do anything on loop
3324          and jcxz insns (JumpByte) for current Pentium4 chips.  They
3325          may work in the future and it doesn't hurt to accept them
3326          now.  */
3327       if (l[0] == ',' && l[1] == 'p')
3328         {
3329           if (l[2] == 't')
3330             {
3331               if (!add_prefix (DS_PREFIX_OPCODE))
3332                 return NULL;
3333               l += 3;
3334             }
3335           else if (l[2] == 'n')
3336             {
3337               if (!add_prefix (CS_PREFIX_OPCODE))
3338                 return NULL;
3339               l += 3;
3340             }
3341         }
3342     }
3343   /* Any other comma loses.  */
3344   if (*l == ',')
3345     {
3346       as_bad (_("invalid character %s in mnemonic"),
3347               output_invalid (*l));
3348       return NULL;
3349     }
3350
3351   /* Check if instruction is supported on specified architecture.  */
3352   supported = 0;
3353   for (t = current_templates->start; t < current_templates->end; ++t)
3354     {
3355       supported |= cpu_flags_match (t);
3356       if (supported == CPU_FLAGS_PERFECT_MATCH)
3357         goto skip;
3358     }
3359
3360   if (!(supported & CPU_FLAGS_64BIT_MATCH))
3361     {
3362       as_bad (flag_code == CODE_64BIT
3363               ? _("`%s' is not supported in 64-bit mode")
3364               : _("`%s' is only supported in 64-bit mode"),
3365               current_templates->start->name);
3366       return NULL;
3367     }
3368   if (supported != CPU_FLAGS_PERFECT_MATCH)
3369     {
3370       as_bad (_("`%s' is not supported on `%s%s'"),
3371               current_templates->start->name,
3372               cpu_arch_name ? cpu_arch_name : default_arch,
3373               cpu_sub_arch_name ? cpu_sub_arch_name : "");
3374       return NULL;
3375     }
3376
3377 skip:
3378   if (!cpu_arch_flags.bitfield.cpui386
3379            && (flag_code != CODE_16BIT))
3380     {
3381       as_warn (_("use .code16 to ensure correct addressing mode"));
3382     }
3383
3384   /* Check for rep/repne without a string instruction.  */
3385   if (expecting_string_instruction)
3386     {
3387       static templates override;
3388
3389       for (t = current_templates->start; t < current_templates->end; ++t)
3390         if (t->opcode_modifier.isstring)
3391           break;
3392       if (t >= current_templates->end)
3393         {
3394           as_bad (_("expecting string instruction after `%s'"),
3395                   expecting_string_instruction);
3396           return NULL;
3397         }
3398       for (override.start = t; t < current_templates->end; ++t)
3399         if (!t->opcode_modifier.isstring)
3400           break;
3401       override.end = t;
3402       current_templates = &override;
3403     }
3404
3405   return l;
3406 }
3407
3408 static char *
3409 parse_operands (char *l, const char *mnemonic)
3410 {
3411   char *token_start;
3412
3413   /* 1 if operand is pending after ','.  */
3414   unsigned int expecting_operand = 0;
3415
3416   /* Non-zero if operand parens not balanced.  */
3417   unsigned int paren_not_balanced;
3418
3419   while (*l != END_OF_INSN)
3420     {
3421       /* Skip optional white space before operand.  */
3422       if (is_space_char (*l))
3423         ++l;
3424       if (!is_operand_char (*l) && *l != END_OF_INSN)
3425         {
3426           as_bad (_("invalid character %s before operand %d"),
3427                   output_invalid (*l),
3428                   i.operands + 1);
3429           return NULL;
3430         }
3431       token_start = l;  /* after white space */
3432       paren_not_balanced = 0;
3433       while (paren_not_balanced || *l != ',')
3434         {
3435           if (*l == END_OF_INSN)
3436             {
3437               if (paren_not_balanced)
3438                 {
3439                   if (!intel_syntax)
3440                     as_bad (_("unbalanced parenthesis in operand %d."),
3441                             i.operands + 1);
3442                   else
3443                     as_bad (_("unbalanced brackets in operand %d."),
3444                             i.operands + 1);
3445                   return NULL;
3446                 }
3447               else
3448                 break;  /* we are done */
3449             }
3450           else if (!is_operand_char (*l) && !is_space_char (*l))
3451             {
3452               as_bad (_("invalid character %s in operand %d"),
3453                       output_invalid (*l),
3454                       i.operands + 1);
3455               return NULL;
3456             }
3457           if (!intel_syntax)
3458             {
3459               if (*l == '(')
3460                 ++paren_not_balanced;
3461               if (*l == ')')
3462                 --paren_not_balanced;
3463             }
3464           else
3465             {
3466               if (*l == '[')
3467                 ++paren_not_balanced;
3468               if (*l == ']')
3469                 --paren_not_balanced;
3470             }
3471           l++;
3472         }
3473       if (l != token_start)
3474         {                       /* Yes, we've read in another operand.  */
3475           unsigned int operand_ok;
3476           this_operand = i.operands++;
3477           i.types[this_operand].bitfield.unspecified = 1;
3478           if (i.operands > MAX_OPERANDS)
3479             {
3480               as_bad (_("spurious operands; (%d operands/instruction max)"),
3481                       MAX_OPERANDS);
3482               return NULL;
3483             }
3484           /* Now parse operand adding info to 'i' as we go along.  */
3485           END_STRING_AND_SAVE (l);
3486
3487           if (intel_syntax)
3488             operand_ok =
3489               i386_intel_operand (token_start,
3490                                   intel_float_operand (mnemonic));
3491           else
3492             operand_ok = i386_att_operand (token_start);
3493
3494           RESTORE_END_STRING (l);
3495           if (!operand_ok)
3496             return NULL;
3497         }
3498       else
3499         {
3500           if (expecting_operand)
3501             {
3502             expecting_operand_after_comma:
3503               as_bad (_("expecting operand after ','; got nothing"));
3504               return NULL;
3505             }
3506           if (*l == ',')
3507             {
3508               as_bad (_("expecting operand before ','; got nothing"));
3509               return NULL;
3510             }
3511         }
3512
3513       /* Now *l must be either ',' or END_OF_INSN.  */
3514       if (*l == ',')
3515         {
3516           if (*++l == END_OF_INSN)
3517             {
3518               /* Just skip it, if it's \n complain.  */
3519               goto expecting_operand_after_comma;
3520             }
3521           expecting_operand = 1;
3522         }
3523     }
3524   return l;
3525 }
3526
3527 static void
3528 swap_2_operands (int xchg1, int xchg2)
3529 {
3530   union i386_op temp_op;
3531   i386_operand_type temp_type;
3532   enum bfd_reloc_code_real temp_reloc;
3533
3534   temp_type = i.types[xchg2];
3535   i.types[xchg2] = i.types[xchg1];
3536   i.types[xchg1] = temp_type;
3537   temp_op = i.op[xchg2];
3538   i.op[xchg2] = i.op[xchg1];
3539   i.op[xchg1] = temp_op;
3540   temp_reloc = i.reloc[xchg2];
3541   i.reloc[xchg2] = i.reloc[xchg1];
3542   i.reloc[xchg1] = temp_reloc;
3543 }
3544
3545 static void
3546 swap_operands (void)
3547 {
3548   switch (i.operands)
3549     {
3550     case 5:
3551     case 4:
3552       swap_2_operands (1, i.operands - 2);
3553     case 3:
3554     case 2:
3555       swap_2_operands (0, i.operands - 1);
3556       break;
3557     default:
3558       abort ();
3559     }
3560
3561   if (i.mem_operands == 2)
3562     {
3563       const seg_entry *temp_seg;
3564       temp_seg = i.seg[0];
3565       i.seg[0] = i.seg[1];
3566       i.seg[1] = temp_seg;
3567     }
3568 }
3569
3570 /* Try to ensure constant immediates are represented in the smallest
3571    opcode possible.  */
3572 static void
3573 optimize_imm (void)
3574 {
3575   char guess_suffix = 0;
3576   int op;
3577
3578   if (i.suffix)
3579     guess_suffix = i.suffix;
3580   else if (i.reg_operands)
3581     {
3582       /* Figure out a suffix from the last register operand specified.
3583          We can't do this properly yet, ie. excluding InOutPortReg,
3584          but the following works for instructions with immediates.
3585          In any case, we can't set i.suffix yet.  */
3586       for (op = i.operands; --op >= 0;)
3587         if (i.types[op].bitfield.reg8)
3588           {
3589             guess_suffix = BYTE_MNEM_SUFFIX;
3590             break;
3591           }
3592         else if (i.types[op].bitfield.reg16)
3593           {
3594             guess_suffix = WORD_MNEM_SUFFIX;
3595             break;
3596           }
3597         else if (i.types[op].bitfield.reg32)
3598           {
3599             guess_suffix = LONG_MNEM_SUFFIX;
3600             break;
3601           }
3602         else if (i.types[op].bitfield.reg64)
3603           {
3604             guess_suffix = QWORD_MNEM_SUFFIX;
3605             break;
3606           }
3607     }
3608   else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
3609     guess_suffix = WORD_MNEM_SUFFIX;
3610
3611   for (op = i.operands; --op >= 0;)
3612     if (operand_type_check (i.types[op], imm))
3613       {
3614         switch (i.op[op].imms->X_op)
3615           {
3616           case O_constant:
3617             /* If a suffix is given, this operand may be shortened.  */
3618             switch (guess_suffix)
3619               {
3620               case LONG_MNEM_SUFFIX:
3621                 i.types[op].bitfield.imm32 = 1;
3622                 i.types[op].bitfield.imm64 = 1;
3623                 break;
3624               case WORD_MNEM_SUFFIX:
3625                 i.types[op].bitfield.imm16 = 1;
3626                 i.types[op].bitfield.imm32 = 1;
3627                 i.types[op].bitfield.imm32s = 1;
3628                 i.types[op].bitfield.imm64 = 1;
3629                 break;
3630               case BYTE_MNEM_SUFFIX:
3631                 i.types[op].bitfield.imm8 = 1;
3632                 i.types[op].bitfield.imm8s = 1;
3633                 i.types[op].bitfield.imm16 = 1;
3634                 i.types[op].bitfield.imm32 = 1;
3635                 i.types[op].bitfield.imm32s = 1;
3636                 i.types[op].bitfield.imm64 = 1;
3637                 break;
3638               }
3639
3640             /* If this operand is at most 16 bits, convert it
3641                to a signed 16 bit number before trying to see
3642                whether it will fit in an even smaller size.
3643                This allows a 16-bit operand such as $0xffe0 to
3644                be recognised as within Imm8S range.  */
3645             if ((i.types[op].bitfield.imm16)
3646                 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
3647               {
3648                 i.op[op].imms->X_add_number =
3649                   (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
3650               }
3651             if ((i.types[op].bitfield.imm32)
3652                 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
3653                     == 0))
3654               {
3655                 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
3656                                                 ^ ((offsetT) 1 << 31))
3657                                                - ((offsetT) 1 << 31));
3658               }
3659             i.types[op]
3660               = operand_type_or (i.types[op],
3661                                  smallest_imm_type (i.op[op].imms->X_add_number));
3662
3663             /* We must avoid matching of Imm32 templates when 64bit
3664                only immediate is available.  */
3665             if (guess_suffix == QWORD_MNEM_SUFFIX)
3666               i.types[op].bitfield.imm32 = 0;
3667             break;
3668
3669           case O_absent:
3670           case O_register:
3671             abort ();
3672
3673             /* Symbols and expressions.  */
3674           default:
3675             /* Convert symbolic operand to proper sizes for matching, but don't
3676                prevent matching a set of insns that only supports sizes other
3677                than those matching the insn suffix.  */
3678             {
3679               i386_operand_type mask, allowed;
3680               const insn_template *t;
3681
3682               operand_type_set (&mask, 0);
3683               operand_type_set (&allowed, 0);
3684
3685               for (t = current_templates->start;
3686                    t < current_templates->end;
3687                    ++t)
3688                 allowed = operand_type_or (allowed,
3689                                            t->operand_types[op]);
3690               switch (guess_suffix)
3691                 {
3692                 case QWORD_MNEM_SUFFIX:
3693                   mask.bitfield.imm64 = 1;
3694                   mask.bitfield.imm32s = 1;
3695                   break;
3696                 case LONG_MNEM_SUFFIX:
3697                   mask.bitfield.imm32 = 1;
3698                   break;
3699                 case WORD_MNEM_SUFFIX:
3700                   mask.bitfield.imm16 = 1;
3701                   break;
3702                 case BYTE_MNEM_SUFFIX:
3703                   mask.bitfield.imm8 = 1;
3704                   break;
3705                 default:
3706                   break;
3707                 }
3708               allowed = operand_type_and (mask, allowed);
3709               if (!operand_type_all_zero (&allowed))
3710                 i.types[op] = operand_type_and (i.types[op], mask);
3711             }
3712             break;
3713           }
3714       }
3715 }
3716
3717 /* Try to use the smallest displacement type too.  */
3718 static void
3719 optimize_disp (void)
3720 {
3721   int op;
3722
3723   for (op = i.operands; --op >= 0;)
3724     if (operand_type_check (i.types[op], disp))
3725       {
3726         if (i.op[op].disps->X_op == O_constant)
3727           {
3728             offsetT op_disp = i.op[op].disps->X_add_number;
3729
3730             if (i.types[op].bitfield.disp16
3731                 && (op_disp & ~(offsetT) 0xffff) == 0)
3732               {
3733                 /* If this operand is at most 16 bits, convert
3734                    to a signed 16 bit number and don't use 64bit
3735                    displacement.  */
3736                 op_disp = (((op_disp & 0xffff) ^ 0x8000) - 0x8000);
3737                 i.types[op].bitfield.disp64 = 0;
3738               }
3739             if (i.types[op].bitfield.disp32
3740                 && (op_disp & ~(((offsetT) 2 << 31) - 1)) == 0)
3741               {
3742                 /* If this operand is at most 32 bits, convert
3743                    to a signed 32 bit number and don't use 64bit
3744                    displacement.  */
3745                 op_disp &= (((offsetT) 2 << 31) - 1);
3746                 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
3747                 i.types[op].bitfield.disp64 = 0;
3748               }
3749             if (!op_disp && i.types[op].bitfield.baseindex)
3750               {
3751                 i.types[op].bitfield.disp8 = 0;
3752                 i.types[op].bitfield.disp16 = 0;
3753                 i.types[op].bitfield.disp32 = 0;
3754                 i.types[op].bitfield.disp32s = 0;
3755                 i.types[op].bitfield.disp64 = 0;
3756                 i.op[op].disps = 0;
3757                 i.disp_operands--;
3758               }
3759             else if (flag_code == CODE_64BIT)
3760               {
3761                 if (fits_in_signed_long (op_disp))
3762                   {
3763                     i.types[op].bitfield.disp64 = 0;
3764                     i.types[op].bitfield.disp32s = 1;
3765                   }
3766                 if (i.prefix[ADDR_PREFIX]
3767                     && fits_in_unsigned_long (op_disp))
3768                   i.types[op].bitfield.disp32 = 1;
3769               }
3770             if ((i.types[op].bitfield.disp32
3771                  || i.types[op].bitfield.disp32s
3772                  || i.types[op].bitfield.disp16)
3773                 && fits_in_signed_byte (op_disp))
3774               i.types[op].bitfield.disp8 = 1;
3775           }
3776         else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
3777                  || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
3778           {
3779             fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
3780                          i.op[op].disps, 0, i.reloc[op]);
3781             i.types[op].bitfield.disp8 = 0;
3782             i.types[op].bitfield.disp16 = 0;
3783             i.types[op].bitfield.disp32 = 0;
3784             i.types[op].bitfield.disp32s = 0;
3785             i.types[op].bitfield.disp64 = 0;
3786           }
3787         else
3788           /* We only support 64bit displacement on constants.  */
3789           i.types[op].bitfield.disp64 = 0;
3790       }
3791 }
3792
3793 /* Check if operands are valid for the instruction.  Update VEX
3794    operand types.  */
3795
3796 static int
3797 VEX_check_operands (const insn_template *t)
3798 {
3799   if (!t->opcode_modifier.vex)
3800     return 0;
3801
3802   /* Only check VEX_Imm4, which must be the first operand.  */
3803   if (t->operand_types[0].bitfield.vec_imm4)
3804     {
3805       if (i.op[0].imms->X_op != O_constant
3806           || !fits_in_imm4 (i.op[0].imms->X_add_number))
3807         {
3808           i.error = bad_imm4;
3809           return 1;
3810         }
3811
3812       /* Turn off Imm8 so that update_imm won't complain.  */
3813       i.types[0] = vec_imm4;
3814     }
3815
3816   return 0;
3817 }
3818
3819 static const insn_template *
3820 match_template (void)
3821 {
3822   /* Points to template once we've found it.  */
3823   const insn_template *t;
3824   i386_operand_type overlap0, overlap1, overlap2, overlap3;
3825   i386_operand_type overlap4;
3826   unsigned int found_reverse_match;
3827   i386_opcode_modifier suffix_check;
3828   i386_operand_type operand_types [MAX_OPERANDS];
3829   int addr_prefix_disp;
3830   unsigned int j;
3831   unsigned int found_cpu_match;
3832   unsigned int check_register;
3833
3834 #if MAX_OPERANDS != 5
3835 # error "MAX_OPERANDS must be 5."
3836 #endif
3837
3838   found_reverse_match = 0;
3839   addr_prefix_disp = -1;
3840
3841   memset (&suffix_check, 0, sizeof (suffix_check));
3842   if (i.suffix == BYTE_MNEM_SUFFIX)
3843     suffix_check.no_bsuf = 1;
3844   else if (i.suffix == WORD_MNEM_SUFFIX)
3845     suffix_check.no_wsuf = 1;
3846   else if (i.suffix == SHORT_MNEM_SUFFIX)
3847     suffix_check.no_ssuf = 1;
3848   else if (i.suffix == LONG_MNEM_SUFFIX)
3849     suffix_check.no_lsuf = 1;
3850   else if (i.suffix == QWORD_MNEM_SUFFIX)
3851     suffix_check.no_qsuf = 1;
3852   else if (i.suffix == LONG_DOUBLE_MNEM_SUFFIX)
3853     suffix_check.no_ldsuf = 1;
3854
3855   /* Must have right number of operands.  */
3856   i.error = number_of_operands_mismatch;
3857
3858   for (t = current_templates->start; t < current_templates->end; t++)
3859     {
3860       addr_prefix_disp = -1;
3861
3862       if (i.operands != t->operands)
3863         continue;
3864
3865       /* Check processor support.  */
3866       i.error = unsupported;
3867       found_cpu_match = (cpu_flags_match (t)
3868                          == CPU_FLAGS_PERFECT_MATCH);
3869       if (!found_cpu_match)
3870         continue;
3871
3872       /* Check old gcc support. */
3873       i.error = old_gcc_only;
3874       if (!old_gcc && t->opcode_modifier.oldgcc)
3875         continue;
3876
3877       /* Check AT&T mnemonic.   */
3878       i.error = unsupported_with_intel_mnemonic;
3879       if (intel_mnemonic && t->opcode_modifier.attmnemonic)
3880         continue;
3881
3882       /* Check AT&T/Intel syntax.   */
3883       i.error = unsupported_syntax;
3884       if ((intel_syntax && t->opcode_modifier.attsyntax)
3885           || (!intel_syntax && t->opcode_modifier.intelsyntax))
3886         continue;
3887
3888       /* Check the suffix, except for some instructions in intel mode.  */
3889       i.error = invalid_instruction_suffix;
3890       if ((!intel_syntax || !t->opcode_modifier.ignoresize)
3891           && ((t->opcode_modifier.no_bsuf && suffix_check.no_bsuf)
3892               || (t->opcode_modifier.no_wsuf && suffix_check.no_wsuf)
3893               || (t->opcode_modifier.no_lsuf && suffix_check.no_lsuf)
3894               || (t->opcode_modifier.no_ssuf && suffix_check.no_ssuf)
3895               || (t->opcode_modifier.no_qsuf && suffix_check.no_qsuf)
3896               || (t->opcode_modifier.no_ldsuf && suffix_check.no_ldsuf)))
3897         continue;
3898
3899       if (!operand_size_match (t))
3900         continue;
3901
3902       for (j = 0; j < MAX_OPERANDS; j++)
3903         operand_types[j] = t->operand_types[j];
3904
3905       /* In general, don't allow 64-bit operands in 32-bit mode.  */
3906       if (i.suffix == QWORD_MNEM_SUFFIX
3907           && flag_code != CODE_64BIT
3908           && (intel_syntax
3909               ? (!t->opcode_modifier.ignoresize
3910                  && !intel_float_operand (t->name))
3911               : intel_float_operand (t->name) != 2)
3912           && ((!operand_types[0].bitfield.regmmx
3913                && !operand_types[0].bitfield.regxmm
3914                && !operand_types[0].bitfield.regymm)
3915               || (!operand_types[t->operands > 1].bitfield.regmmx
3916                   && !!operand_types[t->operands > 1].bitfield.regxmm
3917                   && !!operand_types[t->operands > 1].bitfield.regymm))
3918           && (t->base_opcode != 0x0fc7
3919               || t->extension_opcode != 1 /* cmpxchg8b */))
3920         continue;
3921
3922       /* In general, don't allow 32-bit operands on pre-386.  */
3923       else if (i.suffix == LONG_MNEM_SUFFIX
3924                && !cpu_arch_flags.bitfield.cpui386
3925                && (intel_syntax
3926                    ? (!t->opcode_modifier.ignoresize
3927                       && !intel_float_operand (t->name))
3928                    : intel_float_operand (t->name) != 2)
3929                && ((!operand_types[0].bitfield.regmmx
3930                     && !operand_types[0].bitfield.regxmm)
3931                    || (!operand_types[t->operands > 1].bitfield.regmmx
3932                        && !!operand_types[t->operands > 1].bitfield.regxmm)))
3933         continue;
3934
3935       /* Do not verify operands when there are none.  */
3936       else
3937         {
3938           if (!t->operands)
3939             /* We've found a match; break out of loop.  */
3940             break;
3941         }
3942
3943       /* Address size prefix will turn Disp64/Disp32/Disp16 operand
3944          into Disp32/Disp16/Disp32 operand.  */
3945       if (i.prefix[ADDR_PREFIX] != 0)
3946           {
3947             /* There should be only one Disp operand.  */
3948             switch (flag_code)
3949             {
3950             case CODE_16BIT:
3951               for (j = 0; j < MAX_OPERANDS; j++)
3952                 {
3953                   if (operand_types[j].bitfield.disp16)
3954                     {
3955                       addr_prefix_disp = j;
3956                       operand_types[j].bitfield.disp32 = 1;
3957                       operand_types[j].bitfield.disp16 = 0;
3958                       break;
3959                     }
3960                 }
3961               break;
3962             case CODE_32BIT:
3963               for (j = 0; j < MAX_OPERANDS; j++)
3964                 {
3965                   if (operand_types[j].bitfield.disp32)
3966                     {
3967                       addr_prefix_disp = j;
3968                       operand_types[j].bitfield.disp32 = 0;
3969                       operand_types[j].bitfield.disp16 = 1;
3970                       break;
3971                     }
3972                 }
3973               break;
3974             case CODE_64BIT:
3975               for (j = 0; j < MAX_OPERANDS; j++)
3976                 {
3977                   if (operand_types[j].bitfield.disp64)
3978                     {
3979                       addr_prefix_disp = j;
3980                       operand_types[j].bitfield.disp64 = 0;
3981                       operand_types[j].bitfield.disp32 = 1;
3982                       break;
3983                     }
3984                 }
3985               break;
3986             }
3987           }
3988
3989       /* We check register size only if size of operands can be
3990          encoded the canonical way.  */
3991       check_register = t->opcode_modifier.w;
3992       overlap0 = operand_type_and (i.types[0], operand_types[0]);
3993       switch (t->operands)
3994         {
3995         case 1:
3996           if (!operand_type_match (overlap0, i.types[0]))
3997             continue;
3998           break;
3999         case 2:
4000           /* xchg %eax, %eax is a special case. It is an aliase for nop
4001              only in 32bit mode and we can use opcode 0x90.  In 64bit
4002              mode, we can't use 0x90 for xchg %eax, %eax since it should
4003              zero-extend %eax to %rax.  */
4004           if (flag_code == CODE_64BIT
4005               && t->base_opcode == 0x90
4006               && operand_type_equal (&i.types [0], &acc32)
4007               && operand_type_equal (&i.types [1], &acc32))
4008             continue;
4009           if (i.swap_operand)
4010             {
4011               /* If we swap operand in encoding, we either match
4012                  the next one or reverse direction of operands.  */
4013               if (t->opcode_modifier.s)
4014                 continue;
4015               else if (t->opcode_modifier.d)
4016                 goto check_reverse;
4017             }
4018
4019         case 3:
4020           /* If we swap operand in encoding, we match the next one.  */
4021           if (i.swap_operand && t->opcode_modifier.s)
4022             continue;
4023         case 4:
4024         case 5:
4025           overlap1 = operand_type_and (i.types[1], operand_types[1]);
4026           if (!operand_type_match (overlap0, i.types[0])
4027               || !operand_type_match (overlap1, i.types[1])
4028               || (check_register
4029                   && !operand_type_register_match (overlap0, i.types[0],
4030                                                    operand_types[0],
4031                                                    overlap1, i.types[1],
4032                                                    operand_types[1])))
4033             {
4034               /* Check if other direction is valid ...  */
4035               if (!t->opcode_modifier.d && !t->opcode_modifier.floatd)
4036                 continue;
4037
4038 check_reverse:
4039               /* Try reversing direction of operands.  */
4040               overlap0 = operand_type_and (i.types[0], operand_types[1]);
4041               overlap1 = operand_type_and (i.types[1], operand_types[0]);
4042               if (!operand_type_match (overlap0, i.types[0])
4043                   || !operand_type_match (overlap1, i.types[1])
4044                   || (check_register
4045                       && !operand_type_register_match (overlap0,
4046                                                        i.types[0],
4047                                                        operand_types[1],
4048                                                        overlap1,
4049                                                        i.types[1],
4050                                                        operand_types[0])))
4051                 {
4052                   /* Does not match either direction.  */
4053                   continue;
4054                 }
4055               /* found_reverse_match holds which of D or FloatDR
4056                  we've found.  */
4057               if (t->opcode_modifier.d)
4058                 found_reverse_match = Opcode_D;
4059               else if (t->opcode_modifier.floatd)
4060                 found_reverse_match = Opcode_FloatD;
4061               else
4062                 found_reverse_match = 0;
4063               if (t->opcode_modifier.floatr)
4064                 found_reverse_match |= Opcode_FloatR;
4065             }
4066           else
4067             {
4068               /* Found a forward 2 operand match here.  */
4069               switch (t->operands)
4070                 {
4071                 case 5:
4072                   overlap4 = operand_type_and (i.types[4],
4073                                                operand_types[4]);
4074                 case 4:
4075                   overlap3 = operand_type_and (i.types[3],
4076                                                operand_types[3]);
4077                 case 3:
4078                   overlap2 = operand_type_and (i.types[2],
4079                                                operand_types[2]);
4080                   break;
4081                 }
4082
4083               switch (t->operands)
4084                 {
4085                 case 5:
4086                   if (!operand_type_match (overlap4, i.types[4])
4087                       || !operand_type_register_match (overlap3,
4088                                                        i.types[3],
4089                                                        operand_types[3],
4090                                                        overlap4,
4091                                                        i.types[4],
4092                                                        operand_types[4]))
4093                     continue;
4094                 case 4:
4095                   if (!operand_type_match (overlap3, i.types[3])
4096                       || (check_register
4097                           && !operand_type_register_match (overlap2,
4098                                                            i.types[2],
4099                                                            operand_types[2],
4100                                                            overlap3,
4101                                                            i.types[3],
4102                                                            operand_types[3])))
4103                     continue;
4104                 case 3:
4105                   /* Here we make use of the fact that there are no
4106                      reverse match 3 operand instructions, and all 3
4107                      operand instructions only need to be checked for
4108                      register consistency between operands 2 and 3.  */
4109                   if (!operand_type_match (overlap2, i.types[2])
4110                       || (check_register
4111                           && !operand_type_register_match (overlap1,
4112                                                            i.types[1],
4113                                                            operand_types[1],
4114                                                            overlap2,
4115                                                            i.types[2],
4116                                                            operand_types[2])))
4117                     continue;
4118                   break;
4119                 }
4120             }
4121           /* Found either forward/reverse 2, 3 or 4 operand match here:
4122              slip through to break.  */
4123         }
4124       if (!found_cpu_match)
4125         {
4126           found_reverse_match = 0;
4127           continue;
4128         }
4129
4130       /* Check if VEX operands are valid.  */
4131       if (VEX_check_operands (t))
4132         continue;
4133
4134       /* We've found a match; break out of loop.  */
4135       break;
4136     }
4137
4138   if (t == current_templates->end)
4139     {
4140       /* We found no match.  */
4141       const char *err_msg;
4142       switch (i.error)
4143         {
4144         default:
4145           abort ();
4146         case operand_size_mismatch:
4147           err_msg = _("operand size mismatch");
4148           break;
4149         case operand_type_mismatch:
4150           err_msg = _("operand type mismatch");
4151           break;
4152         case register_type_mismatch:
4153           err_msg = _("register type mismatch");
4154           break;
4155         case number_of_operands_mismatch:
4156           err_msg = _("number of operands mismatch");
4157           break;
4158         case invalid_instruction_suffix:
4159           err_msg = _("invalid instruction suffix");
4160           break;
4161         case bad_imm4:
4162           err_msg = _("Imm4 isn't the first operand");
4163           break;
4164         case old_gcc_only:
4165           err_msg = _("only supported with old gcc");
4166           break;
4167         case unsupported_with_intel_mnemonic:
4168           err_msg = _("unsupported with Intel mnemonic");
4169           break;
4170         case unsupported_syntax:
4171           err_msg = _("unsupported syntax");
4172           break;
4173         case unsupported:
4174           err_msg = _("unsupported");
4175           break;
4176         }
4177       as_bad (_("%s for `%s'"), err_msg,
4178               current_templates->start->name);
4179       return NULL;
4180     }
4181
4182   if (!quiet_warnings)
4183     {
4184       if (!intel_syntax
4185           && (i.types[0].bitfield.jumpabsolute
4186               != operand_types[0].bitfield.jumpabsolute))
4187         {
4188           as_warn (_("indirect %s without `*'"), t->name);
4189         }
4190
4191       if (t->opcode_modifier.isprefix
4192           && t->opcode_modifier.ignoresize)
4193         {
4194           /* Warn them that a data or address size prefix doesn't
4195              affect assembly of the next line of code.  */
4196           as_warn (_("stand-alone `%s' prefix"), t->name);
4197         }
4198     }
4199
4200   /* Copy the template we found.  */
4201   i.tm = *t;
4202
4203   if (addr_prefix_disp != -1)
4204     i.tm.operand_types[addr_prefix_disp]
4205       = operand_types[addr_prefix_disp];
4206
4207   if (found_reverse_match)
4208     {
4209       /* If we found a reverse match we must alter the opcode
4210          direction bit.  found_reverse_match holds bits to change
4211          (different for int & float insns).  */
4212
4213       i.tm.base_opcode ^= found_reverse_match;
4214
4215       i.tm.operand_types[0] = operand_types[1];
4216       i.tm.operand_types[1] = operand_types[0];
4217     }
4218
4219   return t;
4220 }
4221
4222 static int
4223 check_string (void)
4224 {
4225   int mem_op = operand_type_check (i.types[0], anymem) ? 0 : 1;
4226   if (i.tm.operand_types[mem_op].bitfield.esseg)
4227     {
4228       if (i.seg[0] != NULL && i.seg[0] != &es)
4229         {
4230           as_bad (_("`%s' operand %d must use `%ses' segment"),
4231                   i.tm.name,
4232                   mem_op + 1,
4233                   register_prefix);
4234           return 0;
4235         }
4236       /* There's only ever one segment override allowed per instruction.
4237          This instruction possibly has a legal segment override on the
4238          second operand, so copy the segment to where non-string
4239          instructions store it, allowing common code.  */
4240       i.seg[0] = i.seg[1];
4241     }
4242   else if (i.tm.operand_types[mem_op + 1].bitfield.esseg)
4243     {
4244       if (i.seg[1] != NULL && i.seg[1] != &es)
4245         {
4246           as_bad (_("`%s' operand %d must use `%ses' segment"),
4247                   i.tm.name,
4248                   mem_op + 2,
4249                   register_prefix);
4250           return 0;
4251         }
4252     }
4253   return 1;
4254 }
4255
4256 static int
4257 process_suffix (void)
4258 {
4259   /* If matched instruction specifies an explicit instruction mnemonic
4260      suffix, use it.  */
4261   if (i.tm.opcode_modifier.size16)
4262     i.suffix = WORD_MNEM_SUFFIX;
4263   else if (i.tm.opcode_modifier.size32)
4264     i.suffix = LONG_MNEM_SUFFIX;
4265   else if (i.tm.opcode_modifier.size64)
4266     i.suffix = QWORD_MNEM_SUFFIX;
4267   else if (i.reg_operands)
4268     {
4269       /* If there's no instruction mnemonic suffix we try to invent one
4270          based on register operands.  */
4271       if (!i.suffix)
4272         {
4273           /* We take i.suffix from the last register operand specified,
4274              Destination register type is more significant than source
4275              register type.  crc32 in SSE4.2 prefers source register
4276              type. */
4277           if (i.tm.base_opcode == 0xf20f38f1)
4278             {
4279               if (i.types[0].bitfield.reg16)
4280                 i.suffix = WORD_MNEM_SUFFIX;
4281               else if (i.types[0].bitfield.reg32)
4282                 i.suffix = LONG_MNEM_SUFFIX;
4283               else if (i.types[0].bitfield.reg64)
4284                 i.suffix = QWORD_MNEM_SUFFIX;
4285             }
4286           else if (i.tm.base_opcode == 0xf20f38f0)
4287             {
4288               if (i.types[0].bitfield.reg8)
4289                 i.suffix = BYTE_MNEM_SUFFIX;
4290             }
4291
4292           if (!i.suffix)
4293             {
4294               int op;
4295
4296               if (i.tm.base_opcode == 0xf20f38f1
4297                   || i.tm.base_opcode == 0xf20f38f0)
4298                 {
4299                   /* We have to know the operand size for crc32.  */
4300                   as_bad (_("ambiguous memory operand size for `%s`"),
4301                           i.tm.name);
4302                   return 0;
4303                 }
4304
4305               for (op = i.operands; --op >= 0;)
4306                 if (!i.tm.operand_types[op].bitfield.inoutportreg)
4307                   {
4308                     if (i.types[op].bitfield.reg8)
4309                       {
4310                         i.suffix = BYTE_MNEM_SUFFIX;
4311                         break;
4312                       }
4313                     else if (i.types[op].bitfield.reg16)
4314                       {
4315                         i.suffix = WORD_MNEM_SUFFIX;
4316                         break;
4317                       }
4318                     else if (i.types[op].bitfield.reg32)
4319                       {
4320                         i.suffix = LONG_MNEM_SUFFIX;
4321                         break;
4322                       }
4323                     else if (i.types[op].bitfield.reg64)
4324                       {
4325                         i.suffix = QWORD_MNEM_SUFFIX;
4326                         break;
4327                       }
4328                   }
4329             }
4330         }
4331       else if (i.suffix == BYTE_MNEM_SUFFIX)
4332         {
4333           if (intel_syntax
4334               && i.tm.opcode_modifier.ignoresize
4335               && i.tm.opcode_modifier.no_bsuf)
4336             i.suffix = 0;
4337           else if (!check_byte_reg ())
4338             return 0;
4339         }
4340       else if (i.suffix == LONG_MNEM_SUFFIX)
4341         {
4342           if (intel_syntax
4343               && i.tm.opcode_modifier.ignoresize
4344               && i.tm.opcode_modifier.no_lsuf)
4345             i.suffix = 0;
4346           else if (!check_long_reg ())
4347             return 0;
4348         }
4349       else if (i.suffix == QWORD_MNEM_SUFFIX)
4350         {
4351           if (intel_syntax
4352               && i.tm.opcode_modifier.ignoresize
4353               && i.tm.opcode_modifier.no_qsuf)
4354             i.suffix = 0;
4355           else if (!check_qword_reg ())
4356             return 0;
4357         }
4358       else if (i.suffix == WORD_MNEM_SUFFIX)
4359         {
4360           if (intel_syntax
4361               && i.tm.opcode_modifier.ignoresize
4362               && i.tm.opcode_modifier.no_wsuf)
4363             i.suffix = 0;
4364           else if (!check_word_reg ())
4365             return 0;
4366         }
4367       else if (i.suffix == XMMWORD_MNEM_SUFFIX
4368                || i.suffix == YMMWORD_MNEM_SUFFIX)
4369         {
4370           /* Skip if the instruction has x/y suffix.  match_template
4371              should check if it is a valid suffix.  */
4372         }
4373       else if (intel_syntax && i.tm.opcode_modifier.ignoresize)
4374         /* Do nothing if the instruction is going to ignore the prefix.  */
4375         ;
4376       else
4377         abort ();
4378     }
4379   else if (i.tm.opcode_modifier.defaultsize
4380            && !i.suffix
4381            /* exclude fldenv/frstor/fsave/fstenv */
4382            && i.tm.opcode_modifier.no_ssuf)
4383     {
4384       i.suffix = stackop_size;
4385     }
4386   else if (intel_syntax
4387            && !i.suffix
4388            && (i.tm.operand_types[0].bitfield.jumpabsolute
4389                || i.tm.opcode_modifier.jumpbyte
4390                || i.tm.opcode_modifier.jumpintersegment
4391                || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
4392                    && i.tm.extension_opcode <= 3)))
4393     {
4394       switch (flag_code)
4395         {
4396         case CODE_64BIT:
4397           if (!i.tm.opcode_modifier.no_qsuf)
4398             {
4399               i.suffix = QWORD_MNEM_SUFFIX;
4400               break;
4401             }
4402         case CODE_32BIT:
4403           if (!i.tm.opcode_modifier.no_lsuf)
4404             i.suffix = LONG_MNEM_SUFFIX;
4405           break;
4406         case CODE_16BIT:
4407           if (!i.tm.opcode_modifier.no_wsuf)
4408             i.suffix = WORD_MNEM_SUFFIX;
4409           break;
4410         }
4411     }
4412
4413   if (!i.suffix)
4414     {
4415       if (!intel_syntax)
4416         {
4417           if (i.tm.opcode_modifier.w)
4418             {
4419               as_bad (_("no instruction mnemonic suffix given and "
4420                         "no register operands; can't size instruction"));
4421               return 0;
4422             }
4423         }
4424       else
4425         {
4426           unsigned int suffixes;
4427
4428           suffixes = !i.tm.opcode_modifier.no_bsuf;
4429           if (!i.tm.opcode_modifier.no_wsuf)
4430             suffixes |= 1 << 1;
4431           if (!i.tm.opcode_modifier.no_lsuf)
4432             suffixes |= 1 << 2;
4433           if (!i.tm.opcode_modifier.no_ldsuf)
4434             suffixes |= 1 << 3;
4435           if (!i.tm.opcode_modifier.no_ssuf)
4436             suffixes |= 1 << 4;
4437           if (!i.tm.opcode_modifier.no_qsuf)
4438             suffixes |= 1 << 5;
4439
4440           /* There are more than suffix matches.  */
4441           if (i.tm.opcode_modifier.w
4442               || ((suffixes & (suffixes - 1))
4443                   && !i.tm.opcode_modifier.defaultsize
4444                   && !i.tm.opcode_modifier.ignoresize))
4445             {
4446               as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
4447               return 0;
4448             }
4449         }
4450     }
4451
4452   /* Change the opcode based on the operand size given by i.suffix;
4453      We don't need to change things for byte insns.  */
4454
4455   if (i.suffix
4456       && i.suffix != BYTE_MNEM_SUFFIX
4457       && i.suffix != XMMWORD_MNEM_SUFFIX
4458       && i.suffix != YMMWORD_MNEM_SUFFIX)
4459     {
4460       /* It's not a byte, select word/dword operation.  */
4461       if (i.tm.opcode_modifier.w)
4462         {
4463           if (i.tm.opcode_modifier.shortform)
4464             i.tm.base_opcode |= 8;
4465           else
4466             i.tm.base_opcode |= 1;
4467         }
4468
4469       /* Now select between word & dword operations via the operand
4470          size prefix, except for instructions that will ignore this
4471          prefix anyway.  */
4472       if (i.tm.opcode_modifier.addrprefixop0)
4473         {
4474           /* The address size override prefix changes the size of the
4475              first operand.  */
4476           if ((flag_code == CODE_32BIT
4477                && i.op->regs[0].reg_type.bitfield.reg16)
4478               || (flag_code != CODE_32BIT
4479                   && i.op->regs[0].reg_type.bitfield.reg32))
4480             if (!add_prefix (ADDR_PREFIX_OPCODE))
4481               return 0;
4482         }
4483       else if (i.suffix != QWORD_MNEM_SUFFIX
4484                && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
4485                && !i.tm.opcode_modifier.ignoresize
4486                && !i.tm.opcode_modifier.floatmf
4487                && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
4488                    || (flag_code == CODE_64BIT
4489                        && i.tm.opcode_modifier.jumpbyte)))
4490         {
4491           unsigned int prefix = DATA_PREFIX_OPCODE;
4492
4493           if (i.tm.opcode_modifier.jumpbyte) /* jcxz, loop */
4494             prefix = ADDR_PREFIX_OPCODE;
4495
4496           if (!add_prefix (prefix))
4497             return 0;
4498         }
4499
4500       /* Set mode64 for an operand.  */
4501       if (i.suffix == QWORD_MNEM_SUFFIX
4502           && flag_code == CODE_64BIT
4503           && !i.tm.opcode_modifier.norex64)
4504         {
4505           /* Special case for xchg %rax,%rax.  It is NOP and doesn't
4506              need rex64.  cmpxchg8b is also a special case. */
4507           if (! (i.operands == 2
4508                  && i.tm.base_opcode == 0x90
4509                  && i.tm.extension_opcode == None
4510                  && operand_type_equal (&i.types [0], &acc64)
4511                  && operand_type_equal (&i.types [1], &acc64))
4512               && ! (i.operands == 1
4513                     && i.tm.base_opcode == 0xfc7
4514                     && i.tm.extension_opcode == 1
4515                     && !operand_type_check (i.types [0], reg)
4516                     && operand_type_check (i.types [0], anymem)))
4517             i.rex |= REX_W;
4518         }
4519
4520       /* Size floating point instruction.  */
4521       if (i.suffix == LONG_MNEM_SUFFIX)
4522         if (i.tm.opcode_modifier.floatmf)
4523           i.tm.base_opcode ^= 4;
4524     }
4525
4526   return 1;
4527 }
4528
4529 static int
4530 check_byte_reg (void)
4531 {
4532   int op;
4533
4534   for (op = i.operands; --op >= 0;)
4535     {
4536       /* If this is an eight bit register, it's OK.  If it's the 16 or
4537          32 bit version of an eight bit register, we will just use the
4538          low portion, and that's OK too.  */
4539       if (i.types[op].bitfield.reg8)
4540         continue;
4541
4542       /* crc32 doesn't generate this warning.  */
4543       if (i.tm.base_opcode == 0xf20f38f0)
4544         continue;
4545
4546       if ((i.types[op].bitfield.reg16
4547            || i.types[op].bitfield.reg32
4548            || i.types[op].bitfield.reg64)
4549           && i.op[op].regs->reg_num < 4)
4550         {
4551           /* Prohibit these changes in the 64bit mode, since the
4552              lowering is more complicated.  */
4553           if (flag_code == CODE_64BIT
4554               && !i.tm.operand_types[op].bitfield.inoutportreg)
4555             {
4556               as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4557                       register_prefix, i.op[op].regs->reg_name,
4558                       i.suffix);
4559               return 0;
4560             }
4561 #if REGISTER_WARNINGS
4562           if (!quiet_warnings
4563               && !i.tm.operand_types[op].bitfield.inoutportreg)
4564             as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4565                      register_prefix,
4566                      (i.op[op].regs + (i.types[op].bitfield.reg16
4567                                        ? REGNAM_AL - REGNAM_AX
4568                                        : REGNAM_AL - REGNAM_EAX))->reg_name,
4569                      register_prefix,
4570                      i.op[op].regs->reg_name,
4571                      i.suffix);
4572 #endif
4573           continue;
4574         }
4575       /* Any other register is bad.  */
4576       if (i.types[op].bitfield.reg16
4577           || i.types[op].bitfield.reg32
4578           || i.types[op].bitfield.reg64
4579           || i.types[op].bitfield.regmmx
4580           || i.types[op].bitfield.regxmm
4581           || i.types[op].bitfield.regymm
4582           || i.types[op].bitfield.sreg2
4583           || i.types[op].bitfield.sreg3
4584           || i.types[op].bitfield.control
4585           || i.types[op].bitfield.debug
4586           || i.types[op].bitfield.test
4587           || i.types[op].bitfield.floatreg
4588           || i.types[op].bitfield.floatacc)
4589         {
4590           as_bad (_("`%s%s' not allowed with `%s%c'"),
4591                   register_prefix,
4592                   i.op[op].regs->reg_name,
4593                   i.tm.name,
4594                   i.suffix);
4595           return 0;
4596         }
4597     }
4598   return 1;
4599 }
4600
4601 static int
4602 check_long_reg (void)
4603 {
4604   int op;
4605
4606   for (op = i.operands; --op >= 0;)
4607     /* Reject eight bit registers, except where the template requires
4608        them. (eg. movzb)  */
4609     if (i.types[op].bitfield.reg8
4610         && (i.tm.operand_types[op].bitfield.reg16
4611             || i.tm.operand_types[op].bitfield.reg32
4612             || i.tm.operand_types[op].bitfield.acc))
4613       {
4614         as_bad (_("`%s%s' not allowed with `%s%c'"),
4615                 register_prefix,
4616                 i.op[op].regs->reg_name,
4617                 i.tm.name,
4618                 i.suffix);
4619         return 0;
4620       }
4621   /* Warn if the e prefix on a general reg is missing.  */
4622     else if ((!quiet_warnings || flag_code == CODE_64BIT)
4623              && i.types[op].bitfield.reg16
4624              && (i.tm.operand_types[op].bitfield.reg32
4625                  || i.tm.operand_types[op].bitfield.acc))
4626       {
4627         /* Prohibit these changes in the 64bit mode, since the
4628            lowering is more complicated.  */
4629         if (flag_code == CODE_64BIT)
4630           {
4631             as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4632                     register_prefix, i.op[op].regs->reg_name,
4633                     i.suffix);
4634             return 0;
4635           }
4636 #if REGISTER_WARNINGS
4637         else
4638           as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4639                    register_prefix,
4640                    (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
4641                    register_prefix,
4642                    i.op[op].regs->reg_name,
4643                    i.suffix);
4644 #endif
4645       }
4646   /* Warn if the r prefix on a general reg is missing.  */
4647     else if (i.types[op].bitfield.reg64
4648              && (i.tm.operand_types[op].bitfield.reg32
4649                  || i.tm.operand_types[op].bitfield.acc))
4650       {
4651         if (intel_syntax
4652             && i.tm.opcode_modifier.toqword
4653             && !i.types[0].bitfield.regxmm)
4654           {
4655             /* Convert to QWORD.  We want REX byte. */
4656             i.suffix = QWORD_MNEM_SUFFIX;
4657           }
4658         else
4659           {
4660             as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4661                     register_prefix, i.op[op].regs->reg_name,
4662                     i.suffix);
4663             return 0;
4664           }
4665       }
4666   return 1;
4667 }
4668
4669 static int
4670 check_qword_reg (void)
4671 {
4672   int op;
4673
4674   for (op = i.operands; --op >= 0; )
4675     /* Reject eight bit registers, except where the template requires
4676        them. (eg. movzb)  */
4677     if (i.types[op].bitfield.reg8
4678         && (i.tm.operand_types[op].bitfield.reg16
4679             || i.tm.operand_types[op].bitfield.reg32
4680             || i.tm.operand_types[op].bitfield.acc))
4681       {
4682         as_bad (_("`%s%s' not allowed with `%s%c'"),
4683                 register_prefix,
4684                 i.op[op].regs->reg_name,
4685                 i.tm.name,
4686                 i.suffix);
4687         return 0;
4688       }
4689   /* Warn if the e prefix on a general reg is missing.  */
4690     else if ((i.types[op].bitfield.reg16
4691               || i.types[op].bitfield.reg32)
4692              && (i.tm.operand_types[op].bitfield.reg32
4693                  || i.tm.operand_types[op].bitfield.acc))
4694       {
4695         /* Prohibit these changes in the 64bit mode, since the
4696            lowering is more complicated.  */
4697         if (intel_syntax
4698             && i.tm.opcode_modifier.todword
4699             && !i.types[0].bitfield.regxmm)
4700           {
4701             /* Convert to DWORD.  We don't want REX byte. */
4702             i.suffix = LONG_MNEM_SUFFIX;
4703           }
4704         else
4705           {
4706             as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4707                     register_prefix, i.op[op].regs->reg_name,
4708                     i.suffix);
4709             return 0;
4710           }
4711       }
4712   return 1;
4713 }
4714
4715 static int
4716 check_word_reg (void)
4717 {
4718   int op;
4719   for (op = i.operands; --op >= 0;)
4720     /* Reject eight bit registers, except where the template requires
4721        them. (eg. movzb)  */
4722     if (i.types[op].bitfield.reg8
4723         && (i.tm.operand_types[op].bitfield.reg16
4724             || i.tm.operand_types[op].bitfield.reg32
4725             || i.tm.operand_types[op].bitfield.acc))
4726       {
4727         as_bad (_("`%s%s' not allowed with `%s%c'"),
4728                 register_prefix,
4729                 i.op[op].regs->reg_name,
4730                 i.tm.name,
4731                 i.suffix);
4732         return 0;
4733       }
4734   /* Warn if the e prefix on a general reg is present.  */
4735     else if ((!quiet_warnings || flag_code == CODE_64BIT)
4736              && i.types[op].bitfield.reg32
4737              && (i.tm.operand_types[op].bitfield.reg16
4738                  || i.tm.operand_types[op].bitfield.acc))
4739       {
4740         /* Prohibit these changes in the 64bit mode, since the
4741            lowering is more complicated.  */
4742         if (flag_code == CODE_64BIT)
4743           {
4744             as_bad (_("Incorrect register `%s%s' used with `%c' suffix"),
4745                     register_prefix, i.op[op].regs->reg_name,
4746                     i.suffix);
4747             return 0;
4748           }
4749         else
4750 #if REGISTER_WARNINGS
4751           as_warn (_("using `%s%s' instead of `%s%s' due to `%c' suffix"),
4752                    register_prefix,
4753                    (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
4754                    register_prefix,
4755                    i.op[op].regs->reg_name,
4756                    i.suffix);
4757 #endif
4758       }
4759   return 1;
4760 }
4761
4762 static int
4763 update_imm (unsigned int j)
4764 {
4765   i386_operand_type overlap = i.types[j];
4766   if ((overlap.bitfield.imm8
4767        || overlap.bitfield.imm8s
4768        || overlap.bitfield.imm16
4769        || overlap.bitfield.imm32
4770        || overlap.bitfield.imm32s
4771        || overlap.bitfield.imm64)
4772       && !operand_type_equal (&overlap, &imm8)
4773       && !operand_type_equal (&overlap, &imm8s)
4774       && !operand_type_equal (&overlap, &imm16)
4775       && !operand_type_equal (&overlap, &imm32)
4776       && !operand_type_equal (&overlap, &imm32s)
4777       && !operand_type_equal (&overlap, &imm64))
4778     {
4779       if (i.suffix)
4780         {
4781           i386_operand_type temp;
4782
4783           operand_type_set (&temp, 0);
4784           if (i.suffix == BYTE_MNEM_SUFFIX)
4785             {
4786               temp.bitfield.imm8 = overlap.bitfield.imm8;
4787               temp.bitfield.imm8s = overlap.bitfield.imm8s;
4788             }
4789           else if (i.suffix == WORD_MNEM_SUFFIX)
4790             temp.bitfield.imm16 = overlap.bitfield.imm16;
4791           else if (i.suffix == QWORD_MNEM_SUFFIX)
4792             {
4793               temp.bitfield.imm64 = overlap.bitfield.imm64;
4794               temp.bitfield.imm32s = overlap.bitfield.imm32s;
4795             }
4796           else
4797             temp.bitfield.imm32 = overlap.bitfield.imm32;
4798           overlap = temp;
4799         }
4800       else if (operand_type_equal (&overlap, &imm16_32_32s)
4801                || operand_type_equal (&overlap, &imm16_32)
4802                || operand_type_equal (&overlap, &imm16_32s))
4803         {
4804           if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
4805             overlap = imm16;
4806           else
4807             overlap = imm32s;
4808         }
4809       if (!operand_type_equal (&overlap, &imm8)
4810           && !operand_type_equal (&overlap, &imm8s)
4811           && !operand_type_equal (&overlap, &imm16)
4812           && !operand_type_equal (&overlap, &imm32)
4813           && !operand_type_equal (&overlap, &imm32s)
4814           && !operand_type_equal (&overlap, &imm64))
4815         {
4816           as_bad (_("no instruction mnemonic suffix given; "
4817                     "can't determine immediate size"));
4818           return 0;
4819         }
4820     }
4821   i.types[j] = overlap;
4822
4823   return 1;
4824 }
4825
4826 static int
4827 finalize_imm (void)
4828 {
4829   unsigned int j, n;
4830
4831   /* Update the first 2 immediate operands.  */
4832   n = i.operands > 2 ? 2 : i.operands;
4833   if (n)
4834     {
4835       for (j = 0; j < n; j++)
4836         if (update_imm (j) == 0)
4837           return 0;
4838
4839       /* The 3rd operand can't be immediate operand.  */
4840       gas_assert (operand_type_check (i.types[2], imm) == 0);
4841     }
4842
4843   return 1;
4844 }
4845
4846 static int
4847 bad_implicit_operand (int xmm)
4848 {
4849   const char *ireg = xmm ? "xmm0" : "ymm0";
4850
4851   if (intel_syntax)
4852     as_bad (_("the last operand of `%s' must be `%s%s'"),
4853             i.tm.name, register_prefix, ireg);
4854   else
4855     as_bad (_("the first operand of `%s' must be `%s%s'"),
4856             i.tm.name, register_prefix, ireg);
4857   return 0;
4858 }
4859
4860 static int
4861 process_operands (void)
4862 {
4863   /* Default segment register this instruction will use for memory
4864      accesses.  0 means unknown.  This is only for optimizing out
4865      unnecessary segment overrides.  */
4866   const seg_entry *default_seg = 0;
4867
4868   if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
4869     {
4870       unsigned int dupl = i.operands;
4871       unsigned int dest = dupl - 1;
4872       unsigned int j;
4873
4874       /* The destination must be an xmm register.  */
4875       gas_assert (i.reg_operands
4876                   && MAX_OPERANDS > dupl
4877                   && operand_type_equal (&i.types[dest], &regxmm));
4878
4879       if (i.tm.opcode_modifier.firstxmm0)
4880         {
4881           /* The first operand is implicit and must be xmm0.  */
4882           gas_assert (operand_type_equal (&i.types[0], &regxmm));
4883           if (i.op[0].regs->reg_num != 0)
4884             return bad_implicit_operand (1);
4885
4886           if (i.tm.opcode_modifier.vexsources == VEX3SOURCES)
4887             {
4888               /* Keep xmm0 for instructions with VEX prefix and 3
4889                  sources.  */
4890               goto duplicate;
4891             }
4892           else
4893             {
4894               /* We remove the first xmm0 and keep the number of
4895                  operands unchanged, which in fact duplicates the
4896                  destination.  */
4897               for (j = 1; j < i.operands; j++)
4898                 {
4899                   i.op[j - 1] = i.op[j];
4900                   i.types[j - 1] = i.types[j];
4901                   i.tm.operand_types[j - 1] = i.tm.operand_types[j];
4902                 }
4903             }
4904         }
4905       else if (i.tm.opcode_modifier.implicit1stxmm0)
4906         {
4907           gas_assert ((MAX_OPERANDS - 1) > dupl
4908                       && (i.tm.opcode_modifier.vexsources
4909                           == VEX3SOURCES));
4910
4911           /* Add the implicit xmm0 for instructions with VEX prefix
4912              and 3 sources.  */
4913           for (j = i.operands; j > 0; j--)
4914             {
4915               i.op[j] = i.op[j - 1];
4916               i.types[j] = i.types[j - 1];
4917               i.tm.operand_types[j] = i.tm.operand_types[j - 1];
4918             }
4919           i.op[0].regs
4920             = (const reg_entry *) hash_find (reg_hash, "xmm0");
4921           i.types[0] = regxmm;
4922           i.tm.operand_types[0] = regxmm;
4923
4924           i.operands += 2;
4925           i.reg_operands += 2;
4926           i.tm.operands += 2;
4927
4928           dupl++;
4929           dest++;
4930           i.op[dupl] = i.op[dest];
4931           i.types[dupl] = i.types[dest];
4932           i.tm.operand_types[dupl] = i.tm.operand_types[dest];
4933         }
4934       else
4935         {
4936 duplicate:
4937           i.operands++;
4938           i.reg_operands++;
4939           i.tm.operands++;
4940
4941           i.op[dupl] = i.op[dest];
4942           i.types[dupl] = i.types[dest];
4943           i.tm.operand_types[dupl] = i.tm.operand_types[dest];
4944         }
4945
4946        if (i.tm.opcode_modifier.immext)
4947          process_immext ();
4948     }
4949   else if (i.tm.opcode_modifier.firstxmm0)
4950     {
4951       unsigned int j;
4952
4953       /* The first operand is implicit and must be xmm0/ymm0.  */
4954       gas_assert (i.reg_operands
4955                   && (operand_type_equal (&i.types[0], &regxmm)
4956                       || operand_type_equal (&i.types[0], &regymm)));
4957       if (i.op[0].regs->reg_num != 0)
4958         return bad_implicit_operand (i.types[0].bitfield.regxmm);
4959
4960       for (j = 1; j < i.operands; j++)
4961         {
4962           i.op[j - 1] = i.op[j];
4963           i.types[j - 1] = i.types[j];
4964
4965           /* We need to adjust fields in i.tm since they are used by
4966              build_modrm_byte.  */
4967           i.tm.operand_types [j - 1] = i.tm.operand_types [j];
4968         }
4969
4970       i.operands--;
4971       i.reg_operands--;
4972       i.tm.operands--;
4973     }
4974   else if (i.tm.opcode_modifier.regkludge)
4975     {
4976       /* The imul $imm, %reg instruction is converted into
4977          imul $imm, %reg, %reg, and the clr %reg instruction
4978          is converted into xor %reg, %reg.  */
4979
4980       unsigned int first_reg_op;
4981
4982       if (operand_type_check (i.types[0], reg))
4983         first_reg_op = 0;
4984       else
4985         first_reg_op = 1;
4986       /* Pretend we saw the extra register operand.  */
4987       gas_assert (i.reg_operands == 1
4988                   && i.op[first_reg_op + 1].regs == 0);
4989       i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
4990       i.types[first_reg_op + 1] = i.types[first_reg_op];
4991       i.operands++;
4992       i.reg_operands++;
4993     }
4994
4995   if (i.tm.opcode_modifier.shortform)
4996     {
4997       if (i.types[0].bitfield.sreg2
4998           || i.types[0].bitfield.sreg3)
4999         {
5000           if (i.tm.base_opcode == POP_SEG_SHORT
5001               && i.op[0].regs->reg_num == 1)
5002             {
5003               as_bad (_("you can't `pop %scs'"), register_prefix);
5004               return 0;
5005             }
5006           i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
5007           if ((i.op[0].regs->reg_flags & RegRex) != 0)
5008             i.rex |= REX_B;
5009         }
5010       else
5011         {
5012           /* The register or float register operand is in operand
5013              0 or 1.  */
5014           unsigned int op;
5015
5016           if (i.types[0].bitfield.floatreg
5017               || operand_type_check (i.types[0], reg))
5018             op = 0;
5019           else
5020             op = 1;
5021           /* Register goes in low 3 bits of opcode.  */
5022           i.tm.base_opcode |= i.op[op].regs->reg_num;
5023           if ((i.op[op].regs->reg_flags & RegRex) != 0)
5024             i.rex |= REX_B;
5025           if (!quiet_warnings && i.tm.opcode_modifier.ugh)
5026             {
5027               /* Warn about some common errors, but press on regardless.
5028                  The first case can be generated by gcc (<= 2.8.1).  */
5029               if (i.operands == 2)
5030                 {
5031                   /* Reversed arguments on faddp, fsubp, etc.  */
5032                   as_warn (_("translating to `%s %s%s,%s%s'"), i.tm.name,
5033                            register_prefix, i.op[!intel_syntax].regs->reg_name,
5034                            register_prefix, i.op[intel_syntax].regs->reg_name);
5035                 }
5036               else
5037                 {
5038                   /* Extraneous `l' suffix on fp insn.  */
5039                   as_warn (_("translating to `%s %s%s'"), i.tm.name,
5040                            register_prefix, i.op[0].regs->reg_name);
5041                 }
5042             }
5043         }
5044     }
5045   else if (i.tm.opcode_modifier.modrm)
5046     {
5047       /* The opcode is completed (modulo i.tm.extension_opcode which
5048          must be put into the modrm byte).  Now, we make the modrm and
5049          index base bytes based on all the info we've collected.  */
5050
5051       default_seg = build_modrm_byte ();
5052     }
5053   else if ((i.tm.base_opcode & ~0x3) == MOV_AX_DISP32)
5054     {
5055       default_seg = &ds;
5056     }
5057   else if (i.tm.opcode_modifier.isstring)
5058     {
5059       /* For the string instructions that allow a segment override
5060          on one of their operands, the default segment is ds.  */
5061       default_seg = &ds;
5062     }
5063
5064   if (i.tm.base_opcode == 0x8d /* lea */
5065       && i.seg[0]
5066       && !quiet_warnings)
5067     as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
5068
5069   /* If a segment was explicitly specified, and the specified segment
5070      is not the default, use an opcode prefix to select it.  If we
5071      never figured out what the default segment is, then default_seg
5072      will be zero at this point, and the specified segment prefix will
5073      always be used.  */
5074   if ((i.seg[0]) && (i.seg[0] != default_seg))
5075     {
5076       if (!add_prefix (i.seg[0]->seg_prefix))
5077         return 0;
5078     }
5079   return 1;
5080 }
5081
5082 static const seg_entry *
5083 build_modrm_byte (void)
5084 {
5085   const seg_entry *default_seg = 0;
5086   unsigned int source, dest;
5087   int vex_3_sources;
5088
5089   /* The first operand of instructions with VEX prefix and 3 sources
5090      must be VEX_Imm4.  */
5091   vex_3_sources = i.tm.opcode_modifier.vexsources == VEX3SOURCES;
5092   if (vex_3_sources)
5093     {
5094       unsigned int nds, reg_slot;
5095       expressionS *exp;
5096
5097       if (i.tm.opcode_modifier.veximmext
5098           && i.tm.opcode_modifier.immext)
5099         {
5100           dest = i.operands - 2;
5101           gas_assert (dest == 3);
5102         }
5103       else
5104         dest = i.operands - 1;
5105       nds = dest - 1;
5106
5107       /* There are 2 kinds of instructions:
5108          1. 5 operands: 4 register operands or 3 register operands
5109          plus 1 memory operand plus one Vec_Imm4 operand, VexXDS, and
5110          VexW0 or VexW1.  The destination must be either XMM or YMM
5111          register.
5112          2. 4 operands: 4 register operands or 3 register operands
5113          plus 1 memory operand, VexXDS, and VexImmExt  */
5114       gas_assert ((i.reg_operands == 4
5115                    || (i.reg_operands == 3 && i.mem_operands == 1))
5116                   && i.tm.opcode_modifier.vexvvvv == VEXXDS
5117                   && (i.tm.opcode_modifier.veximmext
5118                       || (i.imm_operands == 1
5119                           && i.types[0].bitfield.vec_imm4
5120                           && (i.tm.opcode_modifier.vexw == VEXW0
5121                               || i.tm.opcode_modifier.vexw == VEXW1)
5122                           && (operand_type_equal (&i.tm.operand_types[dest], &regxmm)
5123                               || operand_type_equal (&i.tm.operand_types[dest], &regymm)))));
5124
5125       if (i.imm_operands == 0)
5126         {
5127           /* When there is no immediate operand, generate an 8bit
5128              immediate operand to encode the first operand.  */
5129           exp = &im_expressions[i.imm_operands++];
5130           i.op[i.operands].imms = exp;
5131           i.types[i.operands] = imm8;
5132           i.operands++;
5133           /* If VexW1 is set, the first operand is the source and
5134              the second operand is encoded in the immediate operand.  */
5135           if (i.tm.opcode_modifier.vexw == VEXW1)
5136             {
5137               source = 0;
5138               reg_slot = 1;
5139             }
5140           else
5141             {
5142               source = 1;
5143               reg_slot = 0;
5144             }
5145
5146           /* FMA swaps REG and NDS.  */
5147           if (i.tm.cpu_flags.bitfield.cpufma)
5148             {
5149               unsigned int tmp;
5150               tmp = reg_slot;
5151               reg_slot = nds;
5152               nds = tmp;
5153             }
5154
5155           gas_assert (operand_type_equal (&i.tm.operand_types[reg_slot],
5156                                           &regxmm)
5157                       || operand_type_equal (&i.tm.operand_types[reg_slot],
5158                                              &regymm));
5159           exp->X_op = O_constant;
5160           exp->X_add_number
5161               = ((i.op[reg_slot].regs->reg_num
5162                   + ((i.op[reg_slot].regs->reg_flags & RegRex) ? 8 : 0))
5163                  << 4);
5164         }
5165       else
5166         {
5167           unsigned int imm_slot;
5168
5169           if (i.tm.opcode_modifier.vexw == VEXW0)
5170             {
5171               /* If VexW0 is set, the third operand is the source and
5172                  the second operand is encoded in the immediate
5173                  operand.  */
5174               source = 2;
5175               reg_slot = 1;
5176             }
5177           else
5178             {
5179               /* VexW1 is set, the second operand is the source and
5180                  the third operand is encoded in the immediate
5181                  operand.  */
5182               source = 1;
5183               reg_slot = 2;
5184             }
5185
5186           if (i.tm.opcode_modifier.immext)
5187             {
5188               /* When ImmExt is set, the immdiate byte is the last
5189                  operand.  */
5190               imm_slot = i.operands - 1;
5191               source--;
5192               reg_slot--;
5193             }
5194           else
5195             {
5196               imm_slot = 0;
5197
5198               /* Turn on Imm8 so that output_imm will generate it.  */
5199               i.types[imm_slot].bitfield.imm8 = 1;
5200             }
5201
5202           gas_assert (operand_type_equal (&i.tm.operand_types[reg_slot],
5203                                           &regxmm)
5204                       || operand_type_equal (&i.tm.operand_types[reg_slot],
5205                                              &regymm));
5206           i.op[imm_slot].imms->X_add_number
5207               |= ((i.op[reg_slot].regs->reg_num
5208                    + ((i.op[reg_slot].regs->reg_flags & RegRex) ? 8 : 0))
5209                   << 4);
5210         }
5211
5212       gas_assert (operand_type_equal (&i.tm.operand_types[nds], &regxmm)
5213                   || operand_type_equal (&i.tm.operand_types[nds],
5214                                          &regymm));
5215       i.vex.register_specifier = i.op[nds].regs;
5216     }
5217   else
5218     source = dest = 0;
5219
5220   /* i.reg_operands MUST be the number of real register operands;
5221      implicit registers do not count.  If there are 3 register
5222      operands, it must be a instruction with VexNDS.  For a
5223      instruction with VexNDD, the destination register is encoded
5224      in VEX prefix.  If there are 4 register operands, it must be
5225      a instruction with VEX prefix and 3 sources.  */
5226   if (i.mem_operands == 0
5227       && ((i.reg_operands == 2
5228            && i.tm.opcode_modifier.vexvvvv <= VEXXDS)
5229           || (i.reg_operands == 3
5230               && i.tm.opcode_modifier.vexvvvv == VEXXDS)
5231           || (i.reg_operands == 4 && vex_3_sources)))
5232     {
5233       switch (i.operands)
5234         {
5235         case 2:
5236           source = 0;
5237           break;
5238         case 3:
5239           /* When there are 3 operands, one of them may be immediate,
5240              which may be the first or the last operand.  Otherwise,
5241              the first operand must be shift count register (cl) or it
5242              is an instruction with VexNDS. */
5243           gas_assert (i.imm_operands == 1
5244                       || (i.imm_operands == 0
5245                           && (i.tm.opcode_modifier.vexvvvv == VEXXDS
5246                               || i.types[0].bitfield.shiftcount)));
5247           if (operand_type_check (i.types[0], imm)
5248               || i.types[0].bitfield.shiftcount)
5249             source = 1;
5250           else
5251             source = 0;
5252           break;
5253         case 4:
5254           /* When there are 4 operands, the first two must be 8bit
5255              immediate operands. The source operand will be the 3rd
5256              one.
5257
5258              For instructions with VexNDS, if the first operand
5259              an imm8, the source operand is the 2nd one.  If the last
5260              operand is imm8, the source operand is the first one.  */
5261           gas_assert ((i.imm_operands == 2
5262                        && i.types[0].bitfield.imm8
5263                        && i.types[1].bitfield.imm8)
5264                       || (i.tm.opcode_modifier.vexvvvv == VEXXDS
5265                           && i.imm_operands == 1
5266                           && (i.types[0].bitfield.imm8
5267                               || i.types[i.operands - 1].bitfield.imm8)));
5268           if (i.imm_operands == 2)
5269             source = 2;
5270           else
5271             {
5272               if (i.types[0].bitfield.imm8)
5273                 source = 1;
5274               else
5275                 source = 0;
5276             }
5277           break;
5278         case 5:
5279           break;
5280         default:
5281           abort ();
5282         }
5283
5284       if (!vex_3_sources)
5285         {
5286           dest = source + 1;
5287
5288           if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
5289             {
5290               /* For instructions with VexNDS, the register-only
5291                  source operand must be XMM or YMM register. It is
5292                  encoded in VEX prefix.  We need to clear RegMem bit
5293                  before calling operand_type_equal.  */
5294               i386_operand_type op = i.tm.operand_types[dest];
5295               op.bitfield.regmem = 0;
5296               if ((dest + 1) >= i.operands
5297                   || (!operand_type_equal (&op, &regxmm)
5298                       && !operand_type_equal (&op, &regymm)))
5299                 abort ();
5300               i.vex.register_specifier = i.op[dest].regs;
5301               dest++;
5302             }
5303         }
5304
5305       i.rm.mode = 3;
5306       /* One of the register operands will be encoded in the i.tm.reg
5307          field, the other in the combined i.tm.mode and i.tm.regmem
5308          fields.  If no form of this instruction supports a memory
5309          destination operand, then we assume the source operand may
5310          sometimes be a memory operand and so we need to store the
5311          destination in the i.rm.reg field.  */
5312       if (!i.tm.operand_types[dest].bitfield.regmem
5313           && operand_type_check (i.tm.operand_types[dest], anymem) == 0)
5314         {
5315           i.rm.reg = i.op[dest].regs->reg_num;
5316           i.rm.regmem = i.op[source].regs->reg_num;
5317           if ((i.op[dest].regs->reg_flags & RegRex) != 0)
5318             i.rex |= REX_R;
5319           if ((i.op[source].regs->reg_flags & RegRex) != 0)
5320             i.rex |= REX_B;
5321         }
5322       else
5323         {
5324           i.rm.reg = i.op[source].regs->reg_num;
5325           i.rm.regmem = i.op[dest].regs->reg_num;
5326           if ((i.op[dest].regs->reg_flags & RegRex) != 0)
5327             i.rex |= REX_B;
5328           if ((i.op[source].regs->reg_flags & RegRex) != 0)
5329             i.rex |= REX_R;
5330         }
5331       if (flag_code != CODE_64BIT && (i.rex & (REX_R | REX_B)))
5332         {
5333           if (!i.types[0].bitfield.control
5334               && !i.types[1].bitfield.control)
5335             abort ();
5336           i.rex &= ~(REX_R | REX_B);
5337           add_prefix (LOCK_PREFIX_OPCODE);
5338         }
5339     }
5340   else
5341     {                   /* If it's not 2 reg operands...  */
5342       unsigned int mem;
5343
5344       if (i.mem_operands)
5345         {
5346           unsigned int fake_zero_displacement = 0;
5347           unsigned int op;
5348
5349           for (op = 0; op < i.operands; op++)
5350             if (operand_type_check (i.types[op], anymem))
5351               break;
5352           gas_assert (op < i.operands);
5353
5354           default_seg = &ds;
5355
5356           if (i.base_reg == 0)
5357             {
5358               i.rm.mode = 0;
5359               if (!i.disp_operands)
5360                 fake_zero_displacement = 1;
5361               if (i.index_reg == 0)
5362                 {
5363                   /* Operand is just <disp>  */
5364                   if (flag_code == CODE_64BIT)
5365                     {
5366                       /* 64bit mode overwrites the 32bit absolute
5367                          addressing by RIP relative addressing and
5368                          absolute addressing is encoded by one of the
5369                          redundant SIB forms.  */
5370                       i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5371                       i.sib.base = NO_BASE_REGISTER;
5372                       i.sib.index = NO_INDEX_REGISTER;
5373                       i.types[op] = ((i.prefix[ADDR_PREFIX] == 0)
5374                                      ? disp32s : disp32);
5375                     }
5376                   else if ((flag_code == CODE_16BIT)
5377                            ^ (i.prefix[ADDR_PREFIX] != 0))
5378                     {
5379                       i.rm.regmem = NO_BASE_REGISTER_16;
5380                       i.types[op] = disp16;
5381                     }
5382                   else
5383                     {
5384                       i.rm.regmem = NO_BASE_REGISTER;
5385                       i.types[op] = disp32;
5386                     }
5387                 }
5388               else /* !i.base_reg && i.index_reg  */
5389                 {
5390                   if (i.index_reg->reg_num == RegEiz
5391                       || i.index_reg->reg_num == RegRiz)
5392                     i.sib.index = NO_INDEX_REGISTER;
5393                   else
5394                     i.sib.index = i.index_reg->reg_num;
5395                   i.sib.base = NO_BASE_REGISTER;
5396                   i.sib.scale = i.log2_scale_factor;
5397                   i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5398                   i.types[op].bitfield.disp8 = 0;
5399                   i.types[op].bitfield.disp16 = 0;
5400                   i.types[op].bitfield.disp64 = 0;
5401                   if (flag_code != CODE_64BIT)
5402                     {
5403                       /* Must be 32 bit */
5404                       i.types[op].bitfield.disp32 = 1;
5405                       i.types[op].bitfield.disp32s = 0;
5406                     }
5407                   else
5408                     {
5409                       i.types[op].bitfield.disp32 = 0;
5410                       i.types[op].bitfield.disp32s = 1;
5411                     }
5412                   if ((i.index_reg->reg_flags & RegRex) != 0)
5413                     i.rex |= REX_X;
5414                 }
5415             }
5416           /* RIP addressing for 64bit mode.  */
5417           else if (i.base_reg->reg_num == RegRip ||
5418                    i.base_reg->reg_num == RegEip)
5419             {
5420               i.rm.regmem = NO_BASE_REGISTER;
5421               i.types[op].bitfield.disp8 = 0;
5422               i.types[op].bitfield.disp16 = 0;
5423               i.types[op].bitfield.disp32 = 0;
5424               i.types[op].bitfield.disp32s = 1;
5425               i.types[op].bitfield.disp64 = 0;
5426               i.flags[op] |= Operand_PCrel;
5427               if (! i.disp_operands)
5428                 fake_zero_displacement = 1;
5429             }
5430           else if (i.base_reg->reg_type.bitfield.reg16)
5431             {
5432               switch (i.base_reg->reg_num)
5433                 {
5434                 case 3: /* (%bx)  */
5435                   if (i.index_reg == 0)
5436                     i.rm.regmem = 7;
5437                   else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
5438                     i.rm.regmem = i.index_reg->reg_num - 6;
5439                   break;
5440                 case 5: /* (%bp)  */
5441                   default_seg = &ss;
5442                   if (i.index_reg == 0)
5443                     {
5444                       i.rm.regmem = 6;
5445                       if (operand_type_check (i.types[op], disp) == 0)
5446                         {
5447                           /* fake (%bp) into 0(%bp)  */
5448                           i.types[op].bitfield.disp8 = 1;
5449                           fake_zero_displacement = 1;
5450                         }
5451                     }
5452                   else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
5453                     i.rm.regmem = i.index_reg->reg_num - 6 + 2;
5454                   break;
5455                 default: /* (%si) -> 4 or (%di) -> 5  */
5456                   i.rm.regmem = i.base_reg->reg_num - 6 + 4;
5457                 }
5458               i.rm.mode = mode_from_disp_size (i.types[op]);
5459             }
5460           else /* i.base_reg and 32/64 bit mode  */
5461             {
5462               if (flag_code == CODE_64BIT
5463                   && operand_type_check (i.types[op], disp))
5464                 {
5465                   i386_operand_type temp;
5466                   operand_type_set (&temp, 0);
5467                   temp.bitfield.disp8 = i.types[op].bitfield.disp8;
5468                   i.types[op] = temp;
5469                   if (i.prefix[ADDR_PREFIX] == 0)
5470                     i.types[op].bitfield.disp32s = 1;
5471                   else
5472                     i.types[op].bitfield.disp32 = 1;
5473                 }
5474
5475               i.rm.regmem = i.base_reg->reg_num;
5476               if ((i.base_reg->reg_flags & RegRex) != 0)
5477                 i.rex |= REX_B;
5478               i.sib.base = i.base_reg->reg_num;
5479               /* x86-64 ignores REX prefix bit here to avoid decoder
5480                  complications.  */
5481               if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
5482                 {
5483                   default_seg = &ss;
5484                   if (i.disp_operands == 0)
5485                     {
5486                       fake_zero_displacement = 1;
5487                       i.types[op].bitfield.disp8 = 1;
5488                     }
5489                 }
5490               else if (i.base_reg->reg_num == ESP_REG_NUM)
5491                 {
5492                   default_seg = &ss;
5493                 }
5494               i.sib.scale = i.log2_scale_factor;
5495               if (i.index_reg == 0)
5496                 {
5497                   /* <disp>(%esp) becomes two byte modrm with no index
5498                      register.  We've already stored the code for esp
5499                      in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
5500                      Any base register besides %esp will not use the
5501                      extra modrm byte.  */
5502                   i.sib.index = NO_INDEX_REGISTER;
5503                 }
5504               else
5505                 {
5506                   if (i.index_reg->reg_num == RegEiz
5507                       || i.index_reg->reg_num == RegRiz)
5508                     i.sib.index = NO_INDEX_REGISTER;
5509                   else
5510                     i.sib.index = i.index_reg->reg_num;
5511                   i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
5512                   if ((i.index_reg->reg_flags & RegRex) != 0)
5513                     i.rex |= REX_X;
5514                 }
5515
5516               if (i.disp_operands
5517                   && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
5518                       || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
5519                 i.rm.mode = 0;
5520               else
5521                 i.rm.mode = mode_from_disp_size (i.types[op]);
5522             }
5523
5524           if (fake_zero_displacement)
5525             {
5526               /* Fakes a zero displacement assuming that i.types[op]
5527                  holds the correct displacement size.  */
5528               expressionS *exp;
5529
5530               gas_assert (i.op[op].disps == 0);
5531               exp = &disp_expressions[i.disp_operands++];
5532               i.op[op].disps = exp;
5533               exp->X_op = O_constant;
5534               exp->X_add_number = 0;
5535               exp->X_add_symbol = (symbolS *) 0;
5536               exp->X_op_symbol = (symbolS *) 0;
5537             }
5538
5539           mem = op;
5540         }
5541       else
5542         mem = ~0;
5543
5544       if (i.tm.opcode_modifier.vexsources == XOP2SOURCES)
5545         {
5546           if (operand_type_check (i.types[0], imm))
5547             i.vex.register_specifier = NULL;
5548           else
5549             {
5550               /* VEX.vvvv encodes one of the sources when the first
5551                  operand is not an immediate.  */
5552               if (i.tm.opcode_modifier.vexw == VEXW0)
5553                 i.vex.register_specifier = i.op[0].regs;
5554               else
5555                 i.vex.register_specifier = i.op[1].regs;
5556             }
5557
5558           /* Destination is a XMM register encoded in the ModRM.reg
5559              and VEX.R bit.  */
5560           i.rm.reg = i.op[2].regs->reg_num;
5561           if ((i.op[2].regs->reg_flags & RegRex) != 0)
5562             i.rex |= REX_R;
5563
5564           /* ModRM.rm and VEX.B encodes the other source.  */
5565           if (!i.mem_operands)
5566             {
5567               i.rm.mode = 3;
5568
5569               if (i.tm.opcode_modifier.vexw == VEXW0)
5570                 i.rm.regmem = i.op[1].regs->reg_num;
5571               else
5572                 i.rm.regmem = i.op[0].regs->reg_num;
5573
5574               if ((i.op[1].regs->reg_flags & RegRex) != 0)
5575                 i.rex |= REX_B;
5576             }
5577         }
5578       else if (i.tm.opcode_modifier.vexvvvv == VEXLWP)
5579         {
5580           i.vex.register_specifier = i.op[2].regs;
5581           if (!i.mem_operands)
5582             {
5583               i.rm.mode = 3;
5584               i.rm.regmem = i.op[1].regs->reg_num;
5585               if ((i.op[1].regs->reg_flags & RegRex) != 0)
5586                 i.rex |= REX_B;
5587             }
5588         }
5589       /* Fill in i.rm.reg or i.rm.regmem field with register operand
5590          (if any) based on i.tm.extension_opcode.  Again, we must be
5591          careful to make sure that segment/control/debug/test/MMX
5592          registers are coded into the i.rm.reg field.  */
5593       else if (i.reg_operands)
5594         {
5595           unsigned int op;
5596           unsigned int vex_reg = ~0;
5597
5598           for (op = 0; op < i.operands; op++)
5599             if (i.types[op].bitfield.reg8
5600                 || i.types[op].bitfield.reg16
5601                 || i.types[op].bitfield.reg32
5602                 || i.types[op].bitfield.reg64
5603                 || i.types[op].bitfield.regmmx
5604                 || i.types[op].bitfield.regxmm
5605                 || i.types[op].bitfield.regymm
5606                 || i.types[op].bitfield.sreg2
5607                 || i.types[op].bitfield.sreg3
5608                 || i.types[op].bitfield.control
5609                 || i.types[op].bitfield.debug
5610                 || i.types[op].bitfield.test)
5611               break;
5612
5613           if (vex_3_sources)
5614             op = dest;
5615           else if (i.tm.opcode_modifier.vexvvvv == VEXXDS)
5616             {
5617               /* For instructions with VexNDS, the register-only
5618                  source operand is encoded in VEX prefix. */
5619               gas_assert (mem != (unsigned int) ~0);
5620
5621               if (op > mem)
5622                 {
5623                   vex_reg = op++;
5624                   gas_assert (op < i.operands);
5625                 }
5626               else
5627                 {
5628                   vex_reg = op + 1;
5629                   gas_assert (vex_reg < i.operands);
5630                 }
5631             }
5632           else if (i.tm.opcode_modifier.vexvvvv == VEXNDD)
5633             {
5634               /* For instructions with VexNDD, there should be
5635                  no memory operand and the register destination
5636                  is encoded in VEX prefix.  */
5637               gas_assert (i.mem_operands == 0
5638                           && (op + 2) == i.operands);
5639               vex_reg = op + 1;
5640             }
5641           else
5642             gas_assert (op < i.operands);
5643
5644           if (vex_reg != (unsigned int) ~0)
5645             {
5646               gas_assert (i.reg_operands == 2);
5647
5648               if (!operand_type_equal (&i.tm.operand_types[vex_reg],
5649                                        &regxmm)
5650                   && !operand_type_equal (&i.tm.operand_types[vex_reg],
5651                                           &regymm))
5652                 abort ();
5653
5654               i.vex.register_specifier = i.op[vex_reg].regs;
5655             }
5656
5657           /* Don't set OP operand twice.  */
5658           if (vex_reg != op)
5659             {
5660               /* If there is an extension opcode to put here, the
5661                  register number must be put into the regmem field.  */
5662               if (i.tm.extension_opcode != None)
5663                 {
5664                   i.rm.regmem = i.op[op].regs->reg_num;
5665                   if ((i.op[op].regs->reg_flags & RegRex) != 0)
5666                     i.rex |= REX_B;
5667                 }
5668               else
5669                 {
5670                   i.rm.reg = i.op[op].regs->reg_num;
5671                   if ((i.op[op].regs->reg_flags & RegRex) != 0)
5672                     i.rex |= REX_R;
5673                 }
5674             }
5675
5676           /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
5677              must set it to 3 to indicate this is a register operand
5678              in the regmem field.  */
5679           if (!i.mem_operands)
5680             i.rm.mode = 3;
5681         }
5682
5683       /* Fill in i.rm.reg field with extension opcode (if any).  */
5684       if (i.tm.extension_opcode != None)
5685         i.rm.reg = i.tm.extension_opcode;
5686     }
5687   return default_seg;
5688 }
5689
5690 static void
5691 output_branch (void)
5692 {
5693   char *p;
5694   int code16;
5695   int prefix;
5696   relax_substateT subtype;
5697   symbolS *sym;
5698   offsetT off;
5699
5700   code16 = 0;
5701   if (flag_code == CODE_16BIT)
5702     code16 = CODE16;
5703
5704   prefix = 0;
5705   if (i.prefix[DATA_PREFIX] != 0)
5706     {
5707       prefix = 1;
5708       i.prefixes -= 1;
5709       code16 ^= CODE16;
5710     }
5711   /* Pentium4 branch hints.  */
5712   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
5713       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
5714     {
5715       prefix++;
5716       i.prefixes--;
5717     }
5718   if (i.prefix[REX_PREFIX] != 0)
5719     {
5720       prefix++;
5721       i.prefixes--;
5722     }
5723
5724   if (i.prefixes != 0 && !intel_syntax)
5725     as_warn (_("skipping prefixes on this instruction"));
5726
5727   /* It's always a symbol;  End frag & setup for relax.
5728      Make sure there is enough room in this frag for the largest
5729      instruction we may generate in md_convert_frag.  This is 2
5730      bytes for the opcode and room for the prefix and largest
5731      displacement.  */
5732   frag_grow (prefix + 2 + 4);
5733   /* Prefix and 1 opcode byte go in fr_fix.  */
5734   p = frag_more (prefix + 1);
5735   if (i.prefix[DATA_PREFIX] != 0)
5736     *p++ = DATA_PREFIX_OPCODE;
5737   if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
5738       || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
5739     *p++ = i.prefix[SEG_PREFIX];
5740   if (i.prefix[REX_PREFIX] != 0)
5741     *p++ = i.prefix[REX_PREFIX];
5742   *p = i.tm.base_opcode;
5743
5744   if ((unsigned char) *p == JUMP_PC_RELATIVE)
5745     subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
5746   else if (cpu_arch_flags.bitfield.cpui386)
5747     subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
5748   else
5749     subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
5750   subtype |= code16;
5751
5752   sym = i.op[0].disps->X_add_symbol;
5753   off = i.op[0].disps->X_add_number;
5754
5755   if (i.op[0].disps->X_op != O_constant
5756       && i.op[0].disps->X_op != O_symbol)
5757     {
5758       /* Handle complex expressions.  */
5759       sym = make_expr_symbol (i.op[0].disps);
5760       off = 0;
5761     }
5762
5763   /* 1 possible extra opcode + 4 byte displacement go in var part.
5764      Pass reloc in fr_var.  */
5765   frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
5766 }
5767
5768 static void
5769 output_jump (void)
5770 {
5771   char *p;
5772   int size;
5773   fixS *fixP;
5774
5775   if (i.tm.opcode_modifier.jumpbyte)
5776     {
5777       /* This is a loop or jecxz type instruction.  */
5778       size = 1;
5779       if (i.prefix[ADDR_PREFIX] != 0)
5780         {
5781           FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
5782           i.prefixes -= 1;
5783         }
5784       /* Pentium4 branch hints.  */
5785       if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
5786           || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
5787         {
5788           FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
5789           i.prefixes--;
5790         }
5791     }
5792   else
5793     {
5794       int code16;
5795
5796       code16 = 0;
5797       if (flag_code == CODE_16BIT)
5798         code16 = CODE16;
5799
5800       if (i.prefix[DATA_PREFIX] != 0)
5801         {
5802           FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
5803           i.prefixes -= 1;
5804           code16 ^= CODE16;
5805         }
5806
5807       size = 4;
5808       if (code16)
5809         size = 2;
5810     }
5811
5812   if (i.prefix[REX_PREFIX] != 0)
5813     {
5814       FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
5815       i.prefixes -= 1;
5816     }
5817
5818   if (i.prefixes != 0 && !intel_syntax)
5819     as_warn (_("skipping prefixes on this instruction"));
5820
5821   p = frag_more (1 + size);
5822   *p++ = i.tm.base_opcode;
5823
5824   fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
5825                       i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
5826
5827   /* All jumps handled here are signed, but don't use a signed limit
5828      check for 32 and 16 bit jumps as we want to allow wrap around at
5829      4G and 64k respectively.  */
5830   if (size == 1)
5831     fixP->fx_signed = 1;
5832 }
5833
5834 static void
5835 output_interseg_jump (void)
5836 {
5837   char *p;
5838   int size;
5839   int prefix;
5840   int code16;
5841
5842   code16 = 0;
5843   if (flag_code == CODE_16BIT)
5844     code16 = CODE16;
5845
5846   prefix = 0;
5847   if (i.prefix[DATA_PREFIX] != 0)
5848     {
5849       prefix = 1;
5850       i.prefixes -= 1;
5851       code16 ^= CODE16;
5852     }
5853   if (i.prefix[REX_PREFIX] != 0)
5854     {
5855       prefix++;
5856       i.prefixes -= 1;
5857     }
5858
5859   size = 4;
5860   if (code16)
5861     size = 2;
5862
5863   if (i.prefixes != 0 && !intel_syntax)
5864     as_warn (_("skipping prefixes on this instruction"));
5865
5866   /* 1 opcode; 2 segment; offset  */
5867   p = frag_more (prefix + 1 + 2 + size);
5868
5869   if (i.prefix[DATA_PREFIX] != 0)
5870     *p++ = DATA_PREFIX_OPCODE;
5871
5872   if (i.prefix[REX_PREFIX] != 0)
5873     *p++ = i.prefix[REX_PREFIX];
5874
5875   *p++ = i.tm.base_opcode;
5876   if (i.op[1].imms->X_op == O_constant)
5877     {
5878       offsetT n = i.op[1].imms->X_add_number;
5879
5880       if (size == 2
5881           && !fits_in_unsigned_word (n)
5882           && !fits_in_signed_word (n))
5883         {
5884           as_bad (_("16-bit jump out of range"));
5885           return;
5886         }
5887       md_number_to_chars (p, n, size);
5888     }
5889   else
5890     fix_new_exp (frag_now, p - frag_now->fr_literal, size,
5891                  i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
5892   if (i.op[0].imms->X_op != O_constant)
5893     as_bad (_("can't handle non absolute segment in `%s'"),
5894             i.tm.name);
5895   md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
5896 }
5897
5898 static void
5899 output_insn (void)
5900 {
5901   fragS *insn_start_frag;
5902   offsetT insn_start_off;
5903
5904   /* Tie dwarf2 debug info to the address at the start of the insn.
5905      We can't do this after the insn has been output as the current
5906      frag may have been closed off.  eg. by frag_var.  */
5907   dwarf2_emit_insn (0);
5908
5909   insn_start_frag = frag_now;
5910   insn_start_off = frag_now_fix ();
5911
5912   /* Output jumps.  */
5913   if (i.tm.opcode_modifier.jump)
5914     output_branch ();
5915   else if (i.tm.opcode_modifier.jumpbyte
5916            || i.tm.opcode_modifier.jumpdword)
5917     output_jump ();
5918   else if (i.tm.opcode_modifier.jumpintersegment)
5919     output_interseg_jump ();
5920   else
5921     {
5922       /* Output normal instructions here.  */
5923       char *p;
5924       unsigned char *q;
5925       unsigned int j;
5926       unsigned int prefix;
5927
5928       /* Since the VEX prefix contains the implicit prefix, we don't
5929           need the explicit prefix.  */
5930       if (!i.tm.opcode_modifier.vex)
5931         {
5932           switch (i.tm.opcode_length)
5933             {
5934             case 3:
5935               if (i.tm.base_opcode & 0xff000000)
5936                 {
5937                   prefix = (i.tm.base_opcode >> 24) & 0xff;
5938                   goto check_prefix;
5939                 }
5940               break;
5941             case 2:
5942               if ((i.tm.base_opcode & 0xff0000) != 0)
5943                 {
5944                   prefix = (i.tm.base_opcode >> 16) & 0xff;
5945                   if (i.tm.cpu_flags.bitfield.cpupadlock)
5946                     {
5947 check_prefix:
5948                       if (prefix != REPE_PREFIX_OPCODE
5949                           || (i.prefix[REP_PREFIX]
5950                               != REPE_PREFIX_OPCODE))
5951                         add_prefix (prefix);
5952                     }
5953                   else
5954                     add_prefix (prefix);
5955                 }
5956               break;
5957             case 1:
5958               break;
5959             default:
5960               abort ();
5961             }
5962
5963           /* The prefix bytes.  */
5964           for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
5965             if (*q)
5966               FRAG_APPEND_1_CHAR (*q);
5967         }
5968
5969       if (i.tm.opcode_modifier.vex)
5970         {
5971           for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
5972             if (*q)
5973               switch (j)
5974                 {
5975                 case REX_PREFIX:
5976                   /* REX byte is encoded in VEX prefix.  */
5977                   break;
5978                 case SEG_PREFIX:
5979                 case ADDR_PREFIX:
5980                   FRAG_APPEND_1_CHAR (*q);
5981                   break;
5982                 default:
5983                   /* There should be no other prefixes for instructions
5984                      with VEX prefix.  */
5985                   abort ();
5986                 }
5987
5988           /* Now the VEX prefix.  */
5989           p = frag_more (i.vex.length);
5990           for (j = 0; j < i.vex.length; j++)
5991             p[j] = i.vex.bytes[j];
5992         }
5993
5994       /* Now the opcode; be careful about word order here!  */
5995       if (i.tm.opcode_length == 1)
5996         {
5997           FRAG_APPEND_1_CHAR (i.tm.base_opcode);
5998         }
5999       else
6000         {
6001           switch (i.tm.opcode_length)
6002             {
6003             case 3:
6004               p = frag_more (3);
6005               *p++ = (i.tm.base_opcode >> 16) & 0xff;
6006               break;
6007             case 2:
6008               p = frag_more (2);
6009               break;
6010             default:
6011               abort ();
6012               break;
6013             }
6014
6015           /* Put out high byte first: can't use md_number_to_chars!  */
6016           *p++ = (i.tm.base_opcode >> 8) & 0xff;
6017           *p = i.tm.base_opcode & 0xff;
6018         }
6019
6020       /* Now the modrm byte and sib byte (if present).  */
6021       if (i.tm.opcode_modifier.modrm)
6022         {
6023           FRAG_APPEND_1_CHAR ((i.rm.regmem << 0
6024                                | i.rm.reg << 3
6025                                | i.rm.mode << 6));
6026           /* If i.rm.regmem == ESP (4)
6027              && i.rm.mode != (Register mode)
6028              && not 16 bit
6029              ==> need second modrm byte.  */
6030           if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
6031               && i.rm.mode != 3
6032               && !(i.base_reg && i.base_reg->reg_type.bitfield.reg16))
6033             FRAG_APPEND_1_CHAR ((i.sib.base << 0
6034                                  | i.sib.index << 3
6035                                  | i.sib.scale << 6));
6036         }
6037
6038       if (i.disp_operands)
6039         output_disp (insn_start_frag, insn_start_off);
6040
6041       if (i.imm_operands)
6042         output_imm (insn_start_frag, insn_start_off);
6043     }
6044
6045 #ifdef DEBUG386
6046   if (flag_debug)
6047     {
6048       pi ("" /*line*/, &i);
6049     }
6050 #endif /* DEBUG386  */
6051 }
6052
6053 /* Return the size of the displacement operand N.  */
6054
6055 static int
6056 disp_size (unsigned int n)
6057 {
6058   int size = 4;
6059   if (i.types[n].bitfield.disp64)
6060     size = 8;
6061   else if (i.types[n].bitfield.disp8)
6062     size = 1;
6063   else if (i.types[n].bitfield.disp16)
6064     size = 2;
6065   return size;
6066 }
6067
6068 /* Return the size of the immediate operand N.  */
6069
6070 static int
6071 imm_size (unsigned int n)
6072 {
6073   int size = 4;
6074   if (i.types[n].bitfield.imm64)
6075     size = 8;
6076   else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
6077     size = 1;
6078   else if (i.types[n].bitfield.imm16)
6079     size = 2;
6080   return size;
6081 }
6082
6083 static void
6084 output_disp (fragS *insn_start_frag, offsetT insn_start_off)
6085 {
6086   char *p;
6087   unsigned int n;
6088
6089   for (n = 0; n < i.operands; n++)
6090     {
6091       if (operand_type_check (i.types[n], disp))
6092         {
6093           if (i.op[n].disps->X_op == O_constant)
6094             {
6095               int size = disp_size (n);
6096               offsetT val;
6097
6098               val = offset_in_range (i.op[n].disps->X_add_number,
6099                                      size);
6100               p = frag_more (size);
6101               md_number_to_chars (p, val, size);
6102             }
6103           else
6104             {
6105               enum bfd_reloc_code_real reloc_type;
6106               int size = disp_size (n);
6107               int sign = i.types[n].bitfield.disp32s;
6108               int pcrel = (i.flags[n] & Operand_PCrel) != 0;
6109
6110               /* We can't have 8 bit displacement here.  */
6111               gas_assert (!i.types[n].bitfield.disp8);
6112
6113               /* The PC relative address is computed relative
6114                  to the instruction boundary, so in case immediate
6115                  fields follows, we need to adjust the value.  */
6116               if (pcrel && i.imm_operands)
6117                 {
6118                   unsigned int n1;
6119                   int sz = 0;
6120
6121                   for (n1 = 0; n1 < i.operands; n1++)
6122                     if (operand_type_check (i.types[n1], imm))
6123                       {
6124                         /* Only one immediate is allowed for PC
6125                            relative address.  */
6126                         gas_assert (sz == 0);
6127                         sz = imm_size (n1);
6128                         i.op[n].disps->X_add_number -= sz;
6129                       }
6130                   /* We should find the immediate.  */
6131                   gas_assert (sz != 0);
6132                 }
6133
6134               p = frag_more (size);
6135               reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
6136               if (GOT_symbol
6137                   && GOT_symbol == i.op[n].disps->X_add_symbol
6138                   && (((reloc_type == BFD_RELOC_32
6139                         || reloc_type == BFD_RELOC_X86_64_32S
6140                         || (reloc_type == BFD_RELOC_64
6141                             && object_64bit))
6142                        && (i.op[n].disps->X_op == O_symbol
6143                            || (i.op[n].disps->X_op == O_add
6144                                && ((symbol_get_value_expression
6145                                     (i.op[n].disps->X_op_symbol)->X_op)
6146                                    == O_subtract))))
6147                       || reloc_type == BFD_RELOC_32_PCREL))
6148                 {
6149                   offsetT add;
6150
6151                   if (insn_start_frag == frag_now)
6152                     add = (p - frag_now->fr_literal) - insn_start_off;
6153                   else
6154                     {
6155                       fragS *fr;
6156
6157                       add = insn_start_frag->fr_fix - insn_start_off;
6158                       for (fr = insn_start_frag->fr_next;
6159                            fr && fr != frag_now; fr = fr->fr_next)
6160                         add += fr->fr_fix;
6161                       add += p - frag_now->fr_literal;
6162                     }
6163
6164                   if (!object_64bit)
6165                     {
6166                       reloc_type = BFD_RELOC_386_GOTPC;
6167                       i.op[n].imms->X_add_number += add;
6168                     }
6169                   else if (reloc_type == BFD_RELOC_64)
6170                     reloc_type = BFD_RELOC_X86_64_GOTPC64;
6171                   else
6172                     /* Don't do the adjustment for x86-64, as there
6173                        the pcrel addressing is relative to the _next_
6174                        insn, and that is taken care of in other code.  */
6175                     reloc_type = BFD_RELOC_X86_64_GOTPC32;
6176                 }
6177               fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6178                            i.op[n].disps, pcrel, reloc_type);
6179             }
6180         }
6181     }
6182 }
6183
6184 static void
6185 output_imm (fragS *insn_start_frag, offsetT insn_start_off)
6186 {
6187   char *p;
6188   unsigned int n;
6189
6190   for (n = 0; n < i.operands; n++)
6191     {
6192       if (operand_type_check (i.types[n], imm))
6193         {
6194           if (i.op[n].imms->X_op == O_constant)
6195             {
6196               int size = imm_size (n);
6197               offsetT val;
6198
6199               val = offset_in_range (i.op[n].imms->X_add_number,
6200                                      size);
6201               p = frag_more (size);
6202               md_number_to_chars (p, val, size);
6203             }
6204           else
6205             {
6206               /* Not absolute_section.
6207                  Need a 32-bit fixup (don't support 8bit
6208                  non-absolute imms).  Try to support other
6209                  sizes ...  */
6210               enum bfd_reloc_code_real reloc_type;
6211               int size = imm_size (n);
6212               int sign;
6213
6214               if (i.types[n].bitfield.imm32s
6215                   && (i.suffix == QWORD_MNEM_SUFFIX
6216                       || (!i.suffix && i.tm.opcode_modifier.no_lsuf)))
6217                 sign = 1;
6218               else
6219                 sign = 0;
6220
6221               p = frag_more (size);
6222               reloc_type = reloc (size, 0, sign, i.reloc[n]);
6223
6224               /*   This is tough to explain.  We end up with this one if we
6225                * have operands that look like
6226                * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
6227                * obtain the absolute address of the GOT, and it is strongly
6228                * preferable from a performance point of view to avoid using
6229                * a runtime relocation for this.  The actual sequence of
6230                * instructions often look something like:
6231                *
6232                *        call    .L66
6233                * .L66:
6234                *        popl    %ebx
6235                *        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
6236                *
6237                *   The call and pop essentially return the absolute address
6238                * of the label .L66 and store it in %ebx.  The linker itself
6239                * will ultimately change the first operand of the addl so
6240                * that %ebx points to the GOT, but to keep things simple, the
6241                * .o file must have this operand set so that it generates not
6242                * the absolute address of .L66, but the absolute address of
6243                * itself.  This allows the linker itself simply treat a GOTPC
6244                * relocation as asking for a pcrel offset to the GOT to be
6245                * added in, and the addend of the relocation is stored in the
6246                * operand field for the instruction itself.
6247                *
6248                *   Our job here is to fix the operand so that it would add
6249                * the correct offset so that %ebx would point to itself.  The
6250                * thing that is tricky is that .-.L66 will point to the
6251                * beginning of the instruction, so we need to further modify
6252                * the operand so that it will point to itself.  There are
6253                * other cases where you have something like:
6254                *
6255                *        .long   $_GLOBAL_OFFSET_TABLE_+[.-.L66]
6256                *
6257                * and here no correction would be required.  Internally in
6258                * the assembler we treat operands of this form as not being
6259                * pcrel since the '.' is explicitly mentioned, and I wonder
6260                * whether it would simplify matters to do it this way.  Who
6261                * knows.  In earlier versions of the PIC patches, the
6262                * pcrel_adjust field was used to store the correction, but
6263                * since the expression is not pcrel, I felt it would be
6264                * confusing to do it this way.  */
6265
6266               if ((reloc_type == BFD_RELOC_32
6267                    || reloc_type == BFD_RELOC_X86_64_32S
6268                    || reloc_type == BFD_RELOC_64)
6269                   && GOT_symbol
6270                   && GOT_symbol == i.op[n].imms->X_add_symbol
6271                   && (i.op[n].imms->X_op == O_symbol
6272                       || (i.op[n].imms->X_op == O_add
6273                           && ((symbol_get_value_expression
6274                                (i.op[n].imms->X_op_symbol)->X_op)
6275                               == O_subtract))))
6276                 {
6277                   offsetT add;
6278
6279                   if (insn_start_frag == frag_now)
6280                     add = (p - frag_now->fr_literal) - insn_start_off;
6281                   else
6282                     {
6283                       fragS *fr;
6284
6285                       add = insn_start_frag->fr_fix - insn_start_off;
6286                       for (fr = insn_start_frag->fr_next;
6287                            fr && fr != frag_now; fr = fr->fr_next)
6288                         add += fr->fr_fix;
6289                       add += p - frag_now->fr_literal;
6290                     }
6291
6292                   if (!object_64bit)
6293                     reloc_type = BFD_RELOC_386_GOTPC;
6294                   else if (size == 4)
6295                     reloc_type = BFD_RELOC_X86_64_GOTPC32;
6296                   else if (size == 8)
6297                     reloc_type = BFD_RELOC_X86_64_GOTPC64;
6298                   i.op[n].imms->X_add_number += add;
6299                 }
6300               fix_new_exp (frag_now, p - frag_now->fr_literal, size,
6301                            i.op[n].imms, 0, reloc_type);
6302             }
6303         }
6304     }
6305 }
6306 \f
6307 /* x86_cons_fix_new is called via the expression parsing code when a
6308    reloc is needed.  We use this hook to get the correct .got reloc.  */
6309 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
6310 static int cons_sign = -1;
6311
6312 void
6313 x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
6314                   expressionS *exp)
6315 {
6316   enum bfd_reloc_code_real r = reloc (len, 0, cons_sign, got_reloc);
6317
6318   got_reloc = NO_RELOC;
6319
6320 #ifdef TE_PE
6321   if (exp->X_op == O_secrel)
6322     {
6323       exp->X_op = O_symbol;
6324       r = BFD_RELOC_32_SECREL;
6325     }
6326 #endif
6327
6328   fix_new_exp (frag, off, len, exp, 0, r);
6329 }
6330
6331 #if (!defined (OBJ_ELF) && !defined (OBJ_MAYBE_ELF)) || defined (LEX_AT)
6332 # define lex_got(reloc, adjust, types) NULL
6333 #else
6334 /* Parse operands of the form
6335    <symbol>@GOTOFF+<nnn>
6336    and similar .plt or .got references.
6337
6338    If we find one, set up the correct relocation in RELOC and copy the
6339    input string, minus the `@GOTOFF' into a malloc'd buffer for
6340    parsing by the calling routine.  Return this buffer, and if ADJUST
6341    is non-null set it to the length of the string we removed from the
6342    input line.  Otherwise return NULL.  */
6343 static char *
6344 lex_got (enum bfd_reloc_code_real *rel,
6345          int *adjust,
6346          i386_operand_type *types)
6347 {
6348   /* Some of the relocations depend on the size of what field is to
6349      be relocated.  But in our callers i386_immediate and i386_displacement
6350      we don't yet know the operand size (this will be set by insn
6351      matching).  Hence we record the word32 relocation here,
6352      and adjust the reloc according to the real size in reloc().  */
6353   static const struct {
6354     const char *str;
6355     int len;
6356     const enum bfd_reloc_code_real rel[2];
6357     const i386_operand_type types64;
6358   } gotrel[] = {
6359     { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
6360                                        BFD_RELOC_X86_64_PLTOFF64 },
6361       OPERAND_TYPE_IMM64 },
6362     { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
6363                                        BFD_RELOC_X86_64_PLT32    },
6364       OPERAND_TYPE_IMM32_32S_DISP32 },
6365     { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
6366                                        BFD_RELOC_X86_64_GOTPLT64 },
6367       OPERAND_TYPE_IMM64_DISP64 },
6368     { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_386_GOTOFF,
6369                                        BFD_RELOC_X86_64_GOTOFF64 },
6370       OPERAND_TYPE_IMM64_DISP64 },
6371     { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
6372                                        BFD_RELOC_X86_64_GOTPCREL },
6373       OPERAND_TYPE_IMM32_32S_DISP32 },
6374     { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
6375                                        BFD_RELOC_X86_64_TLSGD    },
6376       OPERAND_TYPE_IMM32_32S_DISP32 },
6377     { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
6378                                        _dummy_first_bfd_reloc_code_real },
6379       OPERAND_TYPE_NONE },
6380     { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
6381                                        BFD_RELOC_X86_64_TLSLD    },
6382       OPERAND_TYPE_IMM32_32S_DISP32 },
6383     { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
6384                                        BFD_RELOC_X86_64_GOTTPOFF },
6385       OPERAND_TYPE_IMM32_32S_DISP32 },
6386     { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
6387                                        BFD_RELOC_X86_64_TPOFF32  },
6388       OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
6389     { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
6390                                        _dummy_first_bfd_reloc_code_real },
6391       OPERAND_TYPE_NONE },
6392     { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
6393                                        BFD_RELOC_X86_64_DTPOFF32 },
6394       OPERAND_TYPE_IMM32_32S_64_DISP32_64 },
6395     { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
6396                                        _dummy_first_bfd_reloc_code_real },
6397       OPERAND_TYPE_NONE },
6398     { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
6399                                        _dummy_first_bfd_reloc_code_real },
6400       OPERAND_TYPE_NONE },
6401     { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
6402                                        BFD_RELOC_X86_64_GOT32    },
6403       OPERAND_TYPE_IMM32_32S_64_DISP32 },
6404     { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
6405                                        BFD_RELOC_X86_64_GOTPC32_TLSDESC },
6406       OPERAND_TYPE_IMM32_32S_DISP32 },
6407     { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
6408                                        BFD_RELOC_X86_64_TLSDESC_CALL },
6409       OPERAND_TYPE_IMM32_32S_DISP32 },
6410   };
6411   char *cp;
6412   unsigned int j;
6413
6414   if (!IS_ELF)
6415     return NULL;
6416
6417   for (cp = input_line_pointer; *cp != '@'; cp++)
6418     if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
6419       return NULL;
6420
6421   for (j = 0; j < ARRAY_SIZE (gotrel); j++)
6422     {
6423       int len = gotrel[j].len;
6424       if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
6425         {
6426           if (gotrel[j].rel[object_64bit] != 0)
6427             {
6428               int first, second;
6429               char *tmpbuf, *past_reloc;
6430
6431               *rel = gotrel[j].rel[object_64bit];
6432               if (adjust)
6433                 *adjust = len;
6434
6435               if (types)
6436                 {
6437                   if (flag_code != CODE_64BIT)
6438                     {
6439                       types->bitfield.imm32 = 1;
6440                       types->bitfield.disp32 = 1;
6441                     }
6442                   else
6443                     *types = gotrel[j].types64;
6444                 }
6445
6446               if (GOT_symbol == NULL)
6447                 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
6448
6449               /* The length of the first part of our input line.  */
6450               first = cp - input_line_pointer;
6451
6452               /* The second part goes from after the reloc token until
6453                  (and including) an end_of_line char or comma.  */
6454               past_reloc = cp + 1 + len;
6455               cp = past_reloc;
6456               while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
6457                 ++cp;
6458               second = cp + 1 - past_reloc;
6459
6460               /* Allocate and copy string.  The trailing NUL shouldn't
6461                  be necessary, but be safe.  */
6462               tmpbuf = (char *) xmalloc (first + second + 2);
6463               memcpy (tmpbuf, input_line_pointer, first);
6464               if (second != 0 && *past_reloc != ' ')
6465                 /* Replace the relocation token with ' ', so that
6466                    errors like foo@GOTOFF1 will be detected.  */
6467                 tmpbuf[first++] = ' ';
6468               memcpy (tmpbuf + first, past_reloc, second);
6469               tmpbuf[first + second] = '\0';
6470               return tmpbuf;
6471             }
6472
6473           as_bad (_("@%s reloc is not supported with %d-bit output format"),
6474                   gotrel[j].str, 1 << (5 + object_64bit));
6475           return NULL;
6476         }
6477     }
6478
6479   /* Might be a symbol version string.  Don't as_bad here.  */
6480   return NULL;
6481 }
6482
6483 void
6484 x86_cons (expressionS *exp, int size)
6485 {
6486   intel_syntax = -intel_syntax;
6487
6488   exp->X_md = 0;
6489   if (size == 4 || (object_64bit && size == 8))
6490     {
6491       /* Handle @GOTOFF and the like in an expression.  */
6492       char *save;
6493       char *gotfree_input_line;
6494       int adjust;
6495
6496       save = input_line_pointer;
6497       gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
6498       if (gotfree_input_line)
6499         input_line_pointer = gotfree_input_line;
6500
6501       expression (exp);
6502
6503       if (gotfree_input_line)
6504         {
6505           /* expression () has merrily parsed up to the end of line,
6506              or a comma - in the wrong buffer.  Transfer how far
6507              input_line_pointer has moved to the right buffer.  */
6508           input_line_pointer = (save
6509                                 + (input_line_pointer - gotfree_input_line)
6510                                 + adjust);
6511           free (gotfree_input_line);
6512           if (exp->X_op == O_constant
6513               || exp->X_op == O_absent
6514               || exp->X_op == O_illegal
6515               || exp->X_op == O_register
6516               || exp->X_op == O_big)
6517             {
6518               char c = *input_line_pointer;
6519               *input_line_pointer = 0;
6520               as_bad (_("missing or invalid expression `%s'"), save);
6521               *input_line_pointer = c;
6522             }
6523         }
6524     }
6525   else
6526     expression (exp);
6527
6528   intel_syntax = -intel_syntax;
6529
6530   if (intel_syntax)
6531     i386_intel_simplify (exp);
6532 }
6533 #endif
6534
6535 static void
6536 signed_cons (int size)
6537 {
6538   if (flag_code == CODE_64BIT)
6539     cons_sign = 1;
6540   cons (size);
6541   cons_sign = -1;
6542 }
6543
6544 #ifdef TE_PE
6545 static void
6546 pe_directive_secrel (dummy)
6547      int dummy ATTRIBUTE_UNUSED;
6548 {
6549   expressionS exp;
6550
6551   do
6552     {
6553       expression (&exp);
6554       if (exp.X_op == O_symbol)
6555         exp.X_op = O_secrel;
6556
6557       emit_expr (&exp, 4);
6558     }
6559   while (*input_line_pointer++ == ',');
6560
6561   input_line_pointer--;
6562   demand_empty_rest_of_line ();
6563 }
6564 #endif
6565
6566 static int
6567 i386_immediate (char *imm_start)
6568 {
6569   char *save_input_line_pointer;
6570   char *gotfree_input_line;
6571   segT exp_seg = 0;
6572   expressionS *exp;
6573   i386_operand_type types;
6574
6575   operand_type_set (&types, ~0);
6576
6577   if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
6578     {
6579       as_bad (_("at most %d immediate operands are allowed"),
6580               MAX_IMMEDIATE_OPERANDS);
6581       return 0;
6582     }
6583
6584   exp = &im_expressions[i.imm_operands++];
6585   i.op[this_operand].imms = exp;
6586
6587   if (is_space_char (*imm_start))
6588     ++imm_start;
6589
6590   save_input_line_pointer = input_line_pointer;
6591   input_line_pointer = imm_start;
6592
6593   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
6594   if (gotfree_input_line)
6595     input_line_pointer = gotfree_input_line;
6596
6597   exp_seg = expression (exp);
6598
6599   SKIP_WHITESPACE ();
6600   if (*input_line_pointer)
6601     as_bad (_("junk `%s' after expression"), input_line_pointer);
6602
6603   input_line_pointer = save_input_line_pointer;
6604   if (gotfree_input_line)
6605     {
6606       free (gotfree_input_line);
6607
6608       if (exp->X_op == O_constant || exp->X_op == O_register)
6609         exp->X_op = O_illegal;
6610     }
6611
6612   return i386_finalize_immediate (exp_seg, exp, types, imm_start);
6613 }
6614
6615 static int
6616 i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
6617                          i386_operand_type types, const char *imm_start)
6618 {
6619   if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
6620     {
6621       if (imm_start)
6622         as_bad (_("missing or invalid immediate expression `%s'"),
6623                 imm_start);
6624       return 0;
6625     }
6626   else if (exp->X_op == O_constant)
6627     {
6628       /* Size it properly later.  */
6629       i.types[this_operand].bitfield.imm64 = 1;
6630       /* If not 64bit, sign extend val.  */
6631       if (flag_code != CODE_64BIT
6632           && (exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
6633         exp->X_add_number
6634           = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
6635     }
6636 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6637   else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
6638            && exp_seg != absolute_section
6639            && exp_seg != text_section
6640            && exp_seg != data_section
6641            && exp_seg != bss_section
6642            && exp_seg != undefined_section
6643            && !bfd_is_com_section (exp_seg))
6644     {
6645       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
6646       return 0;
6647     }
6648 #endif
6649   else if (!intel_syntax && exp->X_op == O_register)
6650     {
6651       if (imm_start)
6652         as_bad (_("illegal immediate register operand %s"), imm_start);
6653       return 0;
6654     }
6655   else
6656     {
6657       /* This is an address.  The size of the address will be
6658          determined later, depending on destination register,
6659          suffix, or the default for the section.  */
6660       i.types[this_operand].bitfield.imm8 = 1;
6661       i.types[this_operand].bitfield.imm16 = 1;
6662       i.types[this_operand].bitfield.imm32 = 1;
6663       i.types[this_operand].bitfield.imm32s = 1;
6664       i.types[this_operand].bitfield.imm64 = 1;
6665       i.types[this_operand] = operand_type_and (i.types[this_operand],
6666                                                 types);
6667     }
6668
6669   return 1;
6670 }
6671
6672 static char *
6673 i386_scale (char *scale)
6674 {
6675   offsetT val;
6676   char *save = input_line_pointer;
6677
6678   input_line_pointer = scale;
6679   val = get_absolute_expression ();
6680
6681   switch (val)
6682     {
6683     case 1:
6684       i.log2_scale_factor = 0;
6685       break;
6686     case 2:
6687       i.log2_scale_factor = 1;
6688       break;
6689     case 4:
6690       i.log2_scale_factor = 2;
6691       break;
6692     case 8:
6693       i.log2_scale_factor = 3;
6694       break;
6695     default:
6696       {
6697         char sep = *input_line_pointer;
6698
6699         *input_line_pointer = '\0';
6700         as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
6701                 scale);
6702         *input_line_pointer = sep;
6703         input_line_pointer = save;
6704         return NULL;
6705       }
6706     }
6707   if (i.log2_scale_factor != 0 && i.index_reg == 0)
6708     {
6709       as_warn (_("scale factor of %d without an index register"),
6710                1 << i.log2_scale_factor);
6711       i.log2_scale_factor = 0;
6712     }
6713   scale = input_line_pointer;
6714   input_line_pointer = save;
6715   return scale;
6716 }
6717
6718 static int
6719 i386_displacement (char *disp_start, char *disp_end)
6720 {
6721   expressionS *exp;
6722   segT exp_seg = 0;
6723   char *save_input_line_pointer;
6724   char *gotfree_input_line;
6725   int override;
6726   i386_operand_type bigdisp, types = anydisp;
6727   int ret;
6728
6729   if (i.disp_operands == MAX_MEMORY_OPERANDS)
6730     {
6731       as_bad (_("at most %d displacement operands are allowed"),
6732               MAX_MEMORY_OPERANDS);
6733       return 0;
6734     }
6735
6736   operand_type_set (&bigdisp, 0);
6737   if ((i.types[this_operand].bitfield.jumpabsolute)
6738       || (!current_templates->start->opcode_modifier.jump
6739           && !current_templates->start->opcode_modifier.jumpdword))
6740     {
6741       bigdisp.bitfield.disp32 = 1;
6742       override = (i.prefix[ADDR_PREFIX] != 0);
6743       if (flag_code == CODE_64BIT)
6744         {
6745           if (!override)
6746             {
6747               bigdisp.bitfield.disp32s = 1;
6748               bigdisp.bitfield.disp64 = 1;
6749             }
6750         }
6751       else if ((flag_code == CODE_16BIT) ^ override)
6752         {
6753           bigdisp.bitfield.disp32 = 0;
6754           bigdisp.bitfield.disp16 = 1;
6755         }
6756     }
6757   else
6758     {
6759       /* For PC-relative branches, the width of the displacement
6760          is dependent upon data size, not address size.  */
6761       override = (i.prefix[DATA_PREFIX] != 0);
6762       if (flag_code == CODE_64BIT)
6763         {
6764           if (override || i.suffix == WORD_MNEM_SUFFIX)
6765             bigdisp.bitfield.disp16 = 1;
6766           else
6767             {
6768               bigdisp.bitfield.disp32 = 1;
6769               bigdisp.bitfield.disp32s = 1;
6770             }
6771         }
6772       else
6773         {
6774           if (!override)
6775             override = (i.suffix == (flag_code != CODE_16BIT
6776                                      ? WORD_MNEM_SUFFIX
6777                                      : LONG_MNEM_SUFFIX));
6778           bigdisp.bitfield.disp32 = 1;
6779           if ((flag_code == CODE_16BIT) ^ override)
6780             {
6781               bigdisp.bitfield.disp32 = 0;
6782               bigdisp.bitfield.disp16 = 1;
6783             }
6784         }
6785     }
6786   i.types[this_operand] = operand_type_or (i.types[this_operand],
6787                                            bigdisp);
6788
6789   exp = &disp_expressions[i.disp_operands];
6790   i.op[this_operand].disps = exp;
6791   i.disp_operands++;
6792   save_input_line_pointer = input_line_pointer;
6793   input_line_pointer = disp_start;
6794   END_STRING_AND_SAVE (disp_end);
6795
6796 #ifndef GCC_ASM_O_HACK
6797 #define GCC_ASM_O_HACK 0
6798 #endif
6799 #if GCC_ASM_O_HACK
6800   END_STRING_AND_SAVE (disp_end + 1);
6801   if (i.types[this_operand].bitfield.baseIndex
6802       && displacement_string_end[-1] == '+')
6803     {
6804       /* This hack is to avoid a warning when using the "o"
6805          constraint within gcc asm statements.
6806          For instance:
6807
6808          #define _set_tssldt_desc(n,addr,limit,type) \
6809          __asm__ __volatile__ ( \
6810          "movw %w2,%0\n\t" \
6811          "movw %w1,2+%0\n\t" \
6812          "rorl $16,%1\n\t" \
6813          "movb %b1,4+%0\n\t" \
6814          "movb %4,5+%0\n\t" \
6815          "movb $0,6+%0\n\t" \
6816          "movb %h1,7+%0\n\t" \
6817          "rorl $16,%1" \
6818          : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
6819
6820          This works great except that the output assembler ends
6821          up looking a bit weird if it turns out that there is
6822          no offset.  You end up producing code that looks like:
6823
6824          #APP
6825          movw $235,(%eax)
6826          movw %dx,2+(%eax)
6827          rorl $16,%edx
6828          movb %dl,4+(%eax)
6829          movb $137,5+(%eax)
6830          movb $0,6+(%eax)
6831          movb %dh,7+(%eax)
6832          rorl $16,%edx
6833          #NO_APP
6834
6835          So here we provide the missing zero.  */
6836
6837       *displacement_string_end = '0';
6838     }
6839 #endif
6840   gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
6841   if (gotfree_input_line)
6842     input_line_pointer = gotfree_input_line;
6843
6844   exp_seg = expression (exp);
6845
6846   SKIP_WHITESPACE ();
6847   if (*input_line_pointer)
6848     as_bad (_("junk `%s' after expression"), input_line_pointer);
6849 #if GCC_ASM_O_HACK
6850   RESTORE_END_STRING (disp_end + 1);
6851 #endif
6852   input_line_pointer = save_input_line_pointer;
6853   if (gotfree_input_line)
6854     {
6855       free (gotfree_input_line);
6856
6857       if (exp->X_op == O_constant || exp->X_op == O_register)
6858         exp->X_op = O_illegal;
6859     }
6860
6861   ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
6862
6863   RESTORE_END_STRING (disp_end);
6864
6865   return ret;
6866 }
6867
6868 static int
6869 i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
6870                             i386_operand_type types, const char *disp_start)
6871 {
6872   i386_operand_type bigdisp;
6873   int ret = 1;
6874
6875   /* We do this to make sure that the section symbol is in
6876      the symbol table.  We will ultimately change the relocation
6877      to be relative to the beginning of the section.  */
6878   if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
6879       || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
6880       || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
6881     {
6882       if (exp->X_op != O_symbol)
6883         goto inv_disp;
6884
6885       if (S_IS_LOCAL (exp->X_add_symbol)
6886           && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
6887           && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
6888         section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
6889       exp->X_op = O_subtract;
6890       exp->X_op_symbol = GOT_symbol;
6891       if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
6892         i.reloc[this_operand] = BFD_RELOC_32_PCREL;
6893       else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
6894         i.reloc[this_operand] = BFD_RELOC_64;
6895       else
6896         i.reloc[this_operand] = BFD_RELOC_32;
6897     }
6898
6899   else if (exp->X_op == O_absent
6900            || exp->X_op == O_illegal
6901            || exp->X_op == O_big)
6902     {
6903     inv_disp:
6904       as_bad (_("missing or invalid displacement expression `%s'"),
6905               disp_start);
6906       ret = 0;
6907     }
6908
6909   else if (flag_code == CODE_64BIT
6910            && !i.prefix[ADDR_PREFIX]
6911            && exp->X_op == O_constant)
6912     {
6913       /* Since displacement is signed extended to 64bit, don't allow
6914          disp32 and turn off disp32s if they are out of range.  */
6915       i.types[this_operand].bitfield.disp32 = 0;
6916       if (!fits_in_signed_long (exp->X_add_number))
6917         {
6918           i.types[this_operand].bitfield.disp32s = 0;
6919           if (i.types[this_operand].bitfield.baseindex)
6920             {
6921               as_bad (_("0x%lx out range of signed 32bit displacement"),
6922                       (long) exp->X_add_number);
6923               ret = 0;
6924             }
6925         }
6926     }
6927
6928 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
6929   else if (exp->X_op != O_constant
6930            && OUTPUT_FLAVOR == bfd_target_aout_flavour
6931            && exp_seg != absolute_section
6932            && exp_seg != text_section
6933            && exp_seg != data_section
6934            && exp_seg != bss_section
6935            && exp_seg != undefined_section
6936            && !bfd_is_com_section (exp_seg))
6937     {
6938       as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
6939       ret = 0;
6940     }
6941 #endif
6942
6943   /* Check if this is a displacement only operand.  */
6944   bigdisp = i.types[this_operand];
6945   bigdisp.bitfield.disp8 = 0;
6946   bigdisp.bitfield.disp16 = 0;
6947   bigdisp.bitfield.disp32 = 0;
6948   bigdisp.bitfield.disp32s = 0;
6949   bigdisp.bitfield.disp64 = 0;
6950   if (operand_type_all_zero (&bigdisp))
6951     i.types[this_operand] = operand_type_and (i.types[this_operand],
6952                                               types);
6953
6954   return ret;
6955 }
6956
6957 /* Make sure the memory operand we've been dealt is valid.
6958    Return 1 on success, 0 on a failure.  */
6959
6960 static int
6961 i386_index_check (const char *operand_string)
6962 {
6963   int ok;
6964   const char *kind = "base/index";
6965 #if INFER_ADDR_PREFIX
6966   int fudged = 0;
6967
6968  tryprefix:
6969 #endif
6970   ok = 1;
6971   if (current_templates->start->opcode_modifier.isstring
6972       && !current_templates->start->opcode_modifier.immext
6973       && (current_templates->end[-1].opcode_modifier.isstring
6974           || i.mem_operands))
6975     {
6976       /* Memory operands of string insns are special in that they only allow
6977          a single register (rDI, rSI, or rBX) as their memory address.  */
6978       unsigned int expected;
6979
6980       kind = "string address";
6981
6982       if (current_templates->start->opcode_modifier.w)
6983         {
6984           i386_operand_type type = current_templates->end[-1].operand_types[0];
6985
6986           if (!type.bitfield.baseindex
6987               || ((!i.mem_operands != !intel_syntax)
6988                   && current_templates->end[-1].operand_types[1]
6989                      .bitfield.baseindex))
6990             type = current_templates->end[-1].operand_types[1];
6991           expected = type.bitfield.esseg ? 7 /* rDI */ : 6 /* rSI */;
6992         }
6993       else
6994         expected = 3 /* rBX */;
6995
6996       if (!i.base_reg || i.index_reg
6997           || operand_type_check (i.types[this_operand], disp))
6998         ok = -1;
6999       else if (!(flag_code == CODE_64BIT
7000                  ? i.prefix[ADDR_PREFIX]
7001                    ? i.base_reg->reg_type.bitfield.reg32
7002                    : i.base_reg->reg_type.bitfield.reg64
7003                  : (flag_code == CODE_16BIT) ^ !i.prefix[ADDR_PREFIX]
7004                    ? i.base_reg->reg_type.bitfield.reg32
7005                    : i.base_reg->reg_type.bitfield.reg16))
7006         ok = 0;
7007       else if (i.base_reg->reg_num != expected)
7008         ok = -1;
7009
7010       if (ok < 0)
7011         {
7012           unsigned int j;
7013
7014           for (j = 0; j < i386_regtab_size; ++j)
7015             if ((flag_code == CODE_64BIT
7016                  ? i.prefix[ADDR_PREFIX]
7017                    ? i386_regtab[j].reg_type.bitfield.reg32
7018                    : i386_regtab[j].reg_type.bitfield.reg64
7019                  : (flag_code == CODE_16BIT) ^ !i.prefix[ADDR_PREFIX]
7020                    ? i386_regtab[j].reg_type.bitfield.reg32
7021                    : i386_regtab[j].reg_type.bitfield.reg16)
7022                 && i386_regtab[j].reg_num == expected)
7023               break;
7024           gas_assert (j < i386_regtab_size);
7025           as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
7026                    operand_string,
7027                    intel_syntax ? '[' : '(',
7028                    register_prefix,
7029                    i386_regtab[j].reg_name,
7030                    intel_syntax ? ']' : ')');
7031           ok = 1;
7032         }
7033     }
7034   else if (flag_code == CODE_64BIT)
7035     {
7036       if ((i.base_reg
7037            && ((i.prefix[ADDR_PREFIX] == 0
7038                 && !i.base_reg->reg_type.bitfield.reg64)
7039                || (i.prefix[ADDR_PREFIX]
7040                    && !i.base_reg->reg_type.bitfield.reg32))
7041            && (i.index_reg
7042                || i.base_reg->reg_num !=
7043                   (i.prefix[ADDR_PREFIX] == 0 ? RegRip : RegEip)))
7044           || (i.index_reg
7045               && (!i.index_reg->reg_type.bitfield.baseindex
7046                   || (i.prefix[ADDR_PREFIX] == 0
7047                       && i.index_reg->reg_num != RegRiz
7048                       && !i.index_reg->reg_type.bitfield.reg64
7049                       )
7050                   || (i.prefix[ADDR_PREFIX]
7051                       && i.index_reg->reg_num != RegEiz
7052                       && !i.index_reg->reg_type.bitfield.reg32))))
7053         ok = 0;
7054     }
7055   else
7056     {
7057       if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
7058         {
7059           /* 16bit checks.  */
7060           if ((i.base_reg
7061                && (!i.base_reg->reg_type.bitfield.reg16
7062                    || !i.base_reg->reg_type.bitfield.baseindex))
7063               || (i.index_reg
7064                   && (!i.index_reg->reg_type.bitfield.reg16
7065                       || !i.index_reg->reg_type.bitfield.baseindex
7066                       || !(i.base_reg
7067                            && i.base_reg->reg_num < 6
7068                            && i.index_reg->reg_num >= 6
7069                            && i.log2_scale_factor == 0))))
7070             ok = 0;
7071         }
7072       else
7073         {
7074           /* 32bit checks.  */
7075           if ((i.base_reg
7076                && !i.base_reg->reg_type.bitfield.reg32)
7077               || (i.index_reg
7078                   && ((!i.index_reg->reg_type.bitfield.reg32
7079                        && i.index_reg->reg_num != RegEiz)
7080                       || !i.index_reg->reg_type.bitfield.baseindex)))
7081             ok = 0;
7082         }
7083     }
7084   if (!ok)
7085     {
7086 #if INFER_ADDR_PREFIX
7087       if (!i.mem_operands && !i.prefix[ADDR_PREFIX])
7088         {
7089           i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
7090           i.prefixes += 1;
7091           /* Change the size of any displacement too.  At most one of
7092              Disp16 or Disp32 is set.
7093              FIXME.  There doesn't seem to be any real need for separate
7094              Disp16 and Disp32 flags.  The same goes for Imm16 and Imm32.
7095              Removing them would probably clean up the code quite a lot.  */
7096           if (flag_code != CODE_64BIT
7097               && (i.types[this_operand].bitfield.disp16
7098                   || i.types[this_operand].bitfield.disp32))
7099             i.types[this_operand]
7100               = operand_type_xor (i.types[this_operand], disp16_32);
7101           fudged = 1;
7102           goto tryprefix;
7103         }
7104       if (fudged)
7105         as_bad (_("`%s' is not a valid %s expression"),
7106                 operand_string,
7107                 kind);
7108       else
7109 #endif
7110         as_bad (_("`%s' is not a valid %s-bit %s expression"),
7111                 operand_string,
7112                 flag_code_names[i.prefix[ADDR_PREFIX]
7113                                          ? flag_code == CODE_32BIT
7114                                            ? CODE_16BIT
7115                                            : CODE_32BIT
7116                                          : flag_code],
7117                 kind);
7118     }
7119   return ok;
7120 }
7121
7122 /* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
7123    on error.  */
7124
7125 static int
7126 i386_att_operand (char *operand_string)
7127 {
7128   const reg_entry *r;
7129   char *end_op;
7130   char *op_string = operand_string;
7131
7132   if (is_space_char (*op_string))
7133     ++op_string;
7134
7135   /* We check for an absolute prefix (differentiating,
7136      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
7137   if (*op_string == ABSOLUTE_PREFIX)
7138     {
7139       ++op_string;
7140       if (is_space_char (*op_string))
7141         ++op_string;
7142       i.types[this_operand].bitfield.jumpabsolute = 1;
7143     }
7144
7145   /* Check if operand is a register.  */
7146   if ((r = parse_register (op_string, &end_op)) != NULL)
7147     {
7148       i386_operand_type temp;
7149
7150       /* Check for a segment override by searching for ':' after a
7151          segment register.  */
7152       op_string = end_op;
7153       if (is_space_char (*op_string))
7154         ++op_string;
7155       if (*op_string == ':'
7156           && (r->reg_type.bitfield.sreg2
7157               || r->reg_type.bitfield.sreg3))
7158         {
7159           switch (r->reg_num)
7160             {
7161             case 0:
7162               i.seg[i.mem_operands] = &es;
7163               break;
7164             case 1:
7165               i.seg[i.mem_operands] = &cs;
7166               break;
7167             case 2:
7168               i.seg[i.mem_operands] = &ss;
7169               break;
7170             case 3:
7171               i.seg[i.mem_operands] = &ds;
7172               break;
7173             case 4:
7174               i.seg[i.mem_operands] = &fs;
7175               break;
7176             case 5:
7177               i.seg[i.mem_operands] = &gs;
7178               break;
7179             }
7180
7181           /* Skip the ':' and whitespace.  */
7182           ++op_string;
7183           if (is_space_char (*op_string))
7184             ++op_string;
7185
7186           if (!is_digit_char (*op_string)
7187               && !is_identifier_char (*op_string)
7188               && *op_string != '('
7189               && *op_string != ABSOLUTE_PREFIX)
7190             {
7191               as_bad (_("bad memory operand `%s'"), op_string);
7192               return 0;
7193             }
7194           /* Handle case of %es:*foo.  */
7195           if (*op_string == ABSOLUTE_PREFIX)
7196             {
7197               ++op_string;
7198               if (is_space_char (*op_string))
7199                 ++op_string;
7200               i.types[this_operand].bitfield.jumpabsolute = 1;
7201             }
7202           goto do_memory_reference;
7203         }
7204       if (*op_string)
7205         {
7206           as_bad (_("junk `%s' after register"), op_string);
7207           return 0;
7208         }
7209       temp = r->reg_type;
7210       temp.bitfield.baseindex = 0;
7211       i.types[this_operand] = operand_type_or (i.types[this_operand],
7212                                                temp);
7213       i.types[this_operand].bitfield.unspecified = 0;
7214       i.op[this_operand].regs = r;
7215       i.reg_operands++;
7216     }
7217   else if (*op_string == REGISTER_PREFIX)
7218     {
7219       as_bad (_("bad register name `%s'"), op_string);
7220       return 0;
7221     }
7222   else if (*op_string == IMMEDIATE_PREFIX)
7223     {
7224       ++op_string;
7225       if (i.types[this_operand].bitfield.jumpabsolute)
7226         {
7227           as_bad (_("immediate operand illegal with absolute jump"));
7228           return 0;
7229         }
7230       if (!i386_immediate (op_string))
7231         return 0;
7232     }
7233   else if (is_digit_char (*op_string)
7234            || is_identifier_char (*op_string)
7235            || *op_string == '(')
7236     {
7237       /* This is a memory reference of some sort.  */
7238       char *base_string;
7239
7240       /* Start and end of displacement string expression (if found).  */
7241       char *displacement_string_start;
7242       char *displacement_string_end;
7243
7244     do_memory_reference:
7245       if ((i.mem_operands == 1
7246            && !current_templates->start->opcode_modifier.isstring)
7247           || i.mem_operands == 2)
7248         {
7249           as_bad (_("too many memory references for `%s'"),
7250                   current_templates->start->name);
7251           return 0;
7252         }
7253
7254       /* Check for base index form.  We detect the base index form by
7255          looking for an ')' at the end of the operand, searching
7256          for the '(' matching it, and finding a REGISTER_PREFIX or ','
7257          after the '('.  */
7258       base_string = op_string + strlen (op_string);
7259
7260       --base_string;
7261       if (is_space_char (*base_string))
7262         --base_string;
7263
7264       /* If we only have a displacement, set-up for it to be parsed later.  */
7265       displacement_string_start = op_string;
7266       displacement_string_end = base_string + 1;
7267
7268       if (*base_string == ')')
7269         {
7270           char *temp_string;
7271           unsigned int parens_balanced = 1;
7272           /* We've already checked that the number of left & right ()'s are
7273              equal, so this loop will not be infinite.  */
7274           do
7275             {
7276               base_string--;
7277               if (*base_string == ')')
7278                 parens_balanced++;
7279               if (*base_string == '(')
7280                 parens_balanced--;
7281             }
7282           while (parens_balanced);
7283
7284           temp_string = base_string;
7285
7286           /* Skip past '(' and whitespace.  */
7287           ++base_string;
7288           if (is_space_char (*base_string))
7289             ++base_string;
7290
7291           if (*base_string == ','
7292               || ((i.base_reg = parse_register (base_string, &end_op))
7293                   != NULL))
7294             {
7295               displacement_string_end = temp_string;
7296
7297               i.types[this_operand].bitfield.baseindex = 1;
7298
7299               if (i.base_reg)
7300                 {
7301                   base_string = end_op;
7302                   if (is_space_char (*base_string))
7303                     ++base_string;
7304                 }
7305
7306               /* There may be an index reg or scale factor here.  */
7307               if (*base_string == ',')
7308                 {
7309                   ++base_string;
7310                   if (is_space_char (*base_string))
7311                     ++base_string;
7312
7313                   if ((i.index_reg = parse_register (base_string, &end_op))
7314                       != NULL)
7315                     {
7316                       base_string = end_op;
7317                       if (is_space_char (*base_string))
7318                         ++base_string;
7319                       if (*base_string == ',')
7320                         {
7321                           ++base_string;
7322                           if (is_space_char (*base_string))
7323                             ++base_string;
7324                         }
7325                       else if (*base_string != ')')
7326                         {
7327                           as_bad (_("expecting `,' or `)' "
7328                                     "after index register in `%s'"),
7329                                   operand_string);
7330                           return 0;
7331                         }
7332                     }
7333                   else if (*base_string == REGISTER_PREFIX)
7334                     {
7335                       as_bad (_("bad register name `%s'"), base_string);
7336                       return 0;
7337                     }
7338
7339                   /* Check for scale factor.  */
7340                   if (*base_string != ')')
7341                     {
7342                       char *end_scale = i386_scale (base_string);
7343
7344                       if (!end_scale)
7345                         return 0;
7346
7347                       base_string = end_scale;
7348                       if (is_space_char (*base_string))
7349                         ++base_string;
7350                       if (*base_string != ')')
7351                         {
7352                           as_bad (_("expecting `)' "
7353                                     "after scale factor in `%s'"),
7354                                   operand_string);
7355                           return 0;
7356                         }
7357                     }
7358                   else if (!i.index_reg)
7359                     {
7360                       as_bad (_("expecting index register or scale factor "
7361                                 "after `,'; got '%c'"),
7362                               *base_string);
7363                       return 0;
7364                     }
7365                 }
7366               else if (*base_string != ')')
7367                 {
7368                   as_bad (_("expecting `,' or `)' "
7369                             "after base register in `%s'"),
7370                           operand_string);
7371                   return 0;
7372                 }
7373             }
7374           else if (*base_string == REGISTER_PREFIX)
7375             {
7376               as_bad (_("bad register name `%s'"), base_string);
7377               return 0;
7378             }
7379         }
7380
7381       /* If there's an expression beginning the operand, parse it,
7382          assuming displacement_string_start and
7383          displacement_string_end are meaningful.  */
7384       if (displacement_string_start != displacement_string_end)
7385         {
7386           if (!i386_displacement (displacement_string_start,
7387                                   displacement_string_end))
7388             return 0;
7389         }
7390
7391       /* Special case for (%dx) while doing input/output op.  */
7392       if (i.base_reg
7393           && operand_type_equal (&i.base_reg->reg_type,
7394                                  &reg16_inoutportreg)
7395           && i.index_reg == 0
7396           && i.log2_scale_factor == 0
7397           && i.seg[i.mem_operands] == 0
7398           && !operand_type_check (i.types[this_operand], disp))
7399         {
7400           i.types[this_operand] = inoutportreg;
7401           return 1;
7402         }
7403
7404       if (i386_index_check (operand_string) == 0)
7405         return 0;
7406       i.types[this_operand].bitfield.mem = 1;
7407       i.mem_operands++;
7408     }
7409   else
7410     {
7411       /* It's not a memory operand; argh!  */
7412       as_bad (_("invalid char %s beginning operand %d `%s'"),
7413               output_invalid (*op_string),
7414               this_operand + 1,
7415               op_string);
7416       return 0;
7417     }
7418   return 1;                     /* Normal return.  */
7419 }
7420 \f
7421 /* md_estimate_size_before_relax()
7422
7423    Called just before relax() for rs_machine_dependent frags.  The x86
7424    assembler uses these frags to handle variable size jump
7425    instructions.
7426
7427    Any symbol that is now undefined will not become defined.
7428    Return the correct fr_subtype in the frag.
7429    Return the initial "guess for variable size of frag" to caller.
7430    The guess is actually the growth beyond the fixed part.  Whatever
7431    we do to grow the fixed or variable part contributes to our
7432    returned value.  */
7433
7434 int
7435 md_estimate_size_before_relax (fragP, segment)
7436      fragS *fragP;
7437      segT segment;
7438 {
7439   /* We've already got fragP->fr_subtype right;  all we have to do is
7440      check for un-relaxable symbols.  On an ELF system, we can't relax
7441      an externally visible symbol, because it may be overridden by a
7442      shared library.  */
7443   if (S_GET_SEGMENT (fragP->fr_symbol) != segment
7444 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7445       || (IS_ELF
7446           && (S_IS_EXTERNAL (fragP->fr_symbol)
7447               || S_IS_WEAK (fragP->fr_symbol)
7448               || ((symbol_get_bfdsym (fragP->fr_symbol)->flags
7449                    & BSF_GNU_INDIRECT_FUNCTION))))
7450 #endif
7451 #if defined (OBJ_COFF) && defined (TE_PE)
7452       || (OUTPUT_FLAVOR == bfd_target_coff_flavour
7453           && S_IS_WEAK (fragP->fr_symbol))
7454 #endif
7455       )
7456     {
7457       /* Symbol is undefined in this segment, or we need to keep a
7458          reloc so that weak symbols can be overridden.  */
7459       int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
7460       enum bfd_reloc_code_real reloc_type;
7461       unsigned char *opcode;
7462       int old_fr_fix;
7463
7464       if (fragP->fr_var != NO_RELOC)
7465         reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
7466       else if (size == 2)
7467         reloc_type = BFD_RELOC_16_PCREL;
7468       else
7469         reloc_type = BFD_RELOC_32_PCREL;
7470
7471       old_fr_fix = fragP->fr_fix;
7472       opcode = (unsigned char *) fragP->fr_opcode;
7473
7474       switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
7475         {
7476         case UNCOND_JUMP:
7477           /* Make jmp (0xeb) a (d)word displacement jump.  */
7478           opcode[0] = 0xe9;
7479           fragP->fr_fix += size;
7480           fix_new (fragP, old_fr_fix, size,
7481                    fragP->fr_symbol,
7482                    fragP->fr_offset, 1,
7483                    reloc_type);
7484           break;
7485
7486         case COND_JUMP86:
7487           if (size == 2
7488               && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
7489             {
7490               /* Negate the condition, and branch past an
7491                  unconditional jump.  */
7492               opcode[0] ^= 1;
7493               opcode[1] = 3;
7494               /* Insert an unconditional jump.  */
7495               opcode[2] = 0xe9;
7496               /* We added two extra opcode bytes, and have a two byte
7497                  offset.  */
7498               fragP->fr_fix += 2 + 2;
7499               fix_new (fragP, old_fr_fix + 2, 2,
7500                        fragP->fr_symbol,
7501                        fragP->fr_offset, 1,
7502                        reloc_type);
7503               break;
7504             }
7505           /* Fall through.  */
7506
7507         case COND_JUMP:
7508           if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
7509             {
7510               fixS *fixP;
7511
7512               fragP->fr_fix += 1;
7513               fixP = fix_new (fragP, old_fr_fix, 1,
7514                               fragP->fr_symbol,
7515                               fragP->fr_offset, 1,
7516                               BFD_RELOC_8_PCREL);
7517               fixP->fx_signed = 1;
7518               break;
7519             }
7520
7521           /* This changes the byte-displacement jump 0x7N
7522              to the (d)word-displacement jump 0x0f,0x8N.  */
7523           opcode[1] = opcode[0] + 0x10;
7524           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7525           /* We've added an opcode byte.  */
7526           fragP->fr_fix += 1 + size;
7527           fix_new (fragP, old_fr_fix + 1, size,
7528                    fragP->fr_symbol,
7529                    fragP->fr_offset, 1,
7530                    reloc_type);
7531           break;
7532
7533         default:
7534           BAD_CASE (fragP->fr_subtype);
7535           break;
7536         }
7537       frag_wane (fragP);
7538       return fragP->fr_fix - old_fr_fix;
7539     }
7540
7541   /* Guess size depending on current relax state.  Initially the relax
7542      state will correspond to a short jump and we return 1, because
7543      the variable part of the frag (the branch offset) is one byte
7544      long.  However, we can relax a section more than once and in that
7545      case we must either set fr_subtype back to the unrelaxed state,
7546      or return the value for the appropriate branch.  */
7547   return md_relax_table[fragP->fr_subtype].rlx_length;
7548 }
7549
7550 /* Called after relax() is finished.
7551
7552    In:  Address of frag.
7553         fr_type == rs_machine_dependent.
7554         fr_subtype is what the address relaxed to.
7555
7556    Out: Any fixSs and constants are set up.
7557         Caller will turn frag into a ".space 0".  */
7558
7559 void
7560 md_convert_frag (abfd, sec, fragP)
7561      bfd *abfd ATTRIBUTE_UNUSED;
7562      segT sec ATTRIBUTE_UNUSED;
7563      fragS *fragP;
7564 {
7565   unsigned char *opcode;
7566   unsigned char *where_to_put_displacement = NULL;
7567   offsetT target_address;
7568   offsetT opcode_address;
7569   unsigned int extension = 0;
7570   offsetT displacement_from_opcode_start;
7571
7572   opcode = (unsigned char *) fragP->fr_opcode;
7573
7574   /* Address we want to reach in file space.  */
7575   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
7576
7577   /* Address opcode resides at in file space.  */
7578   opcode_address = fragP->fr_address + fragP->fr_fix;
7579
7580   /* Displacement from opcode start to fill into instruction.  */
7581   displacement_from_opcode_start = target_address - opcode_address;
7582
7583   if ((fragP->fr_subtype & BIG) == 0)
7584     {
7585       /* Don't have to change opcode.  */
7586       extension = 1;            /* 1 opcode + 1 displacement  */
7587       where_to_put_displacement = &opcode[1];
7588     }
7589   else
7590     {
7591       if (no_cond_jump_promotion
7592           && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
7593         as_warn_where (fragP->fr_file, fragP->fr_line,
7594                        _("long jump required"));
7595
7596       switch (fragP->fr_subtype)
7597         {
7598         case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
7599           extension = 4;                /* 1 opcode + 4 displacement  */
7600           opcode[0] = 0xe9;
7601           where_to_put_displacement = &opcode[1];
7602           break;
7603
7604         case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
7605           extension = 2;                /* 1 opcode + 2 displacement  */
7606           opcode[0] = 0xe9;
7607           where_to_put_displacement = &opcode[1];
7608           break;
7609
7610         case ENCODE_RELAX_STATE (COND_JUMP, BIG):
7611         case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
7612           extension = 5;                /* 2 opcode + 4 displacement  */
7613           opcode[1] = opcode[0] + 0x10;
7614           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7615           where_to_put_displacement = &opcode[2];
7616           break;
7617
7618         case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
7619           extension = 3;                /* 2 opcode + 2 displacement  */
7620           opcode[1] = opcode[0] + 0x10;
7621           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
7622           where_to_put_displacement = &opcode[2];
7623           break;
7624
7625         case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
7626           extension = 4;
7627           opcode[0] ^= 1;
7628           opcode[1] = 3;
7629           opcode[2] = 0xe9;
7630           where_to_put_displacement = &opcode[3];
7631           break;
7632
7633         default:
7634           BAD_CASE (fragP->fr_subtype);
7635           break;
7636         }
7637     }
7638
7639   /* If size if less then four we are sure that the operand fits,
7640      but if it's 4, then it could be that the displacement is larger
7641      then -/+ 2GB.  */
7642   if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
7643       && object_64bit
7644       && ((addressT) (displacement_from_opcode_start - extension
7645                       + ((addressT) 1 << 31))
7646           > (((addressT) 2 << 31) - 1)))
7647     {
7648       as_bad_where (fragP->fr_file, fragP->fr_line,
7649                     _("jump target out of range"));
7650       /* Make us emit 0.  */
7651       displacement_from_opcode_start = extension;
7652     }
7653   /* Now put displacement after opcode.  */
7654   md_number_to_chars ((char *) where_to_put_displacement,
7655                       (valueT) (displacement_from_opcode_start - extension),
7656                       DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
7657   fragP->fr_fix += extension;
7658 }
7659 \f
7660 /* Apply a fixup (fixS) to segment data, once it has been determined
7661    by our caller that we have all the info we need to fix it up.
7662
7663    On the 386, immediates, displacements, and data pointers are all in
7664    the same (little-endian) format, so we don't need to care about which
7665    we are handling.  */
7666
7667 void
7668 md_apply_fix (fixP, valP, seg)
7669      /* The fix we're to put in.  */
7670      fixS *fixP;
7671      /* Pointer to the value of the bits.  */
7672      valueT *valP;
7673      /* Segment fix is from.  */
7674      segT seg ATTRIBUTE_UNUSED;
7675 {
7676   char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
7677   valueT value = *valP;
7678
7679 #if !defined (TE_Mach)
7680   if (fixP->fx_pcrel)
7681     {
7682       switch (fixP->fx_r_type)
7683         {
7684         default:
7685           break;
7686
7687         case BFD_RELOC_64:
7688           fixP->fx_r_type = BFD_RELOC_64_PCREL;
7689           break;
7690         case BFD_RELOC_32:
7691         case BFD_RELOC_X86_64_32S:
7692           fixP->fx_r_type = BFD_RELOC_32_PCREL;
7693           break;
7694         case BFD_RELOC_16:
7695           fixP->fx_r_type = BFD_RELOC_16_PCREL;
7696           break;
7697         case BFD_RELOC_8:
7698           fixP->fx_r_type = BFD_RELOC_8_PCREL;
7699           break;
7700         }
7701     }
7702
7703   if (fixP->fx_addsy != NULL
7704       && (fixP->fx_r_type == BFD_RELOC_32_PCREL
7705           || fixP->fx_r_type == BFD_RELOC_64_PCREL
7706           || fixP->fx_r_type == BFD_RELOC_16_PCREL
7707           || fixP->fx_r_type == BFD_RELOC_8_PCREL)
7708       && !use_rela_relocations)
7709     {
7710       /* This is a hack.  There should be a better way to handle this.
7711          This covers for the fact that bfd_install_relocation will
7712          subtract the current location (for partial_inplace, PC relative
7713          relocations); see more below.  */
7714 #ifndef OBJ_AOUT
7715       if (IS_ELF
7716 #ifdef TE_PE
7717           || OUTPUT_FLAVOR == bfd_target_coff_flavour
7718 #endif
7719           )
7720         value += fixP->fx_where + fixP->fx_frag->fr_address;
7721 #endif
7722 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7723       if (IS_ELF)
7724         {
7725           segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
7726
7727           if ((sym_seg == seg
7728                || (symbol_section_p (fixP->fx_addsy)
7729                    && sym_seg != absolute_section))
7730               && !generic_force_reloc (fixP))
7731             {
7732               /* Yes, we add the values in twice.  This is because
7733                  bfd_install_relocation subtracts them out again.  I think
7734                  bfd_install_relocation is broken, but I don't dare change
7735                  it.  FIXME.  */
7736               value += fixP->fx_where + fixP->fx_frag->fr_address;
7737             }
7738         }
7739 #endif
7740 #if defined (OBJ_COFF) && defined (TE_PE)
7741       /* For some reason, the PE format does not store a
7742          section address offset for a PC relative symbol.  */
7743       if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7744           || S_IS_WEAK (fixP->fx_addsy))
7745         value += md_pcrel_from (fixP);
7746 #endif
7747     }
7748 #if defined (OBJ_COFF) && defined (TE_PE)
7749   if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
7750     {
7751       value -= S_GET_VALUE (fixP->fx_addsy);
7752     }
7753 #endif
7754
7755   /* Fix a few things - the dynamic linker expects certain values here,
7756      and we must not disappoint it.  */
7757 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7758   if (IS_ELF && fixP->fx_addsy)
7759     switch (fixP->fx_r_type)
7760       {
7761       case BFD_RELOC_386_PLT32:
7762       case BFD_RELOC_X86_64_PLT32:
7763         /* Make the jump instruction point to the address of the operand.  At
7764            runtime we merely add the offset to the actual PLT entry.  */
7765         value = -4;
7766         break;
7767
7768       case BFD_RELOC_386_TLS_GD:
7769       case BFD_RELOC_386_TLS_LDM:
7770       case BFD_RELOC_386_TLS_IE_32:
7771       case BFD_RELOC_386_TLS_IE:
7772       case BFD_RELOC_386_TLS_GOTIE:
7773       case BFD_RELOC_386_TLS_GOTDESC:
7774       case BFD_RELOC_X86_64_TLSGD:
7775       case BFD_RELOC_X86_64_TLSLD:
7776       case BFD_RELOC_X86_64_GOTTPOFF:
7777       case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7778         value = 0; /* Fully resolved at runtime.  No addend.  */
7779         /* Fallthrough */
7780       case BFD_RELOC_386_TLS_LE:
7781       case BFD_RELOC_386_TLS_LDO_32:
7782       case BFD_RELOC_386_TLS_LE_32:
7783       case BFD_RELOC_X86_64_DTPOFF32:
7784       case BFD_RELOC_X86_64_DTPOFF64:
7785       case BFD_RELOC_X86_64_TPOFF32:
7786       case BFD_RELOC_X86_64_TPOFF64:
7787         S_SET_THREAD_LOCAL (fixP->fx_addsy);
7788         break;
7789
7790       case BFD_RELOC_386_TLS_DESC_CALL:
7791       case BFD_RELOC_X86_64_TLSDESC_CALL:
7792         value = 0; /* Fully resolved at runtime.  No addend.  */
7793         S_SET_THREAD_LOCAL (fixP->fx_addsy);
7794         fixP->fx_done = 0;
7795         return;
7796
7797       case BFD_RELOC_386_GOT32:
7798       case BFD_RELOC_X86_64_GOT32:
7799         value = 0; /* Fully resolved at runtime.  No addend.  */
7800         break;
7801
7802       case BFD_RELOC_VTABLE_INHERIT:
7803       case BFD_RELOC_VTABLE_ENTRY:
7804         fixP->fx_done = 0;
7805         return;
7806
7807       default:
7808         break;
7809       }
7810 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)  */
7811   *valP = value;
7812 #endif /* !defined (TE_Mach)  */
7813
7814   /* Are we finished with this relocation now?  */
7815   if (fixP->fx_addsy == NULL)
7816     fixP->fx_done = 1;
7817 #if defined (OBJ_COFF) && defined (TE_PE)
7818   else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
7819     {
7820       fixP->fx_done = 0;
7821       /* Remember value for tc_gen_reloc.  */
7822       fixP->fx_addnumber = value;
7823       /* Clear out the frag for now.  */
7824       value = 0;
7825     }
7826 #endif
7827   else if (use_rela_relocations)
7828     {
7829       fixP->fx_no_overflow = 1;
7830       /* Remember value for tc_gen_reloc.  */
7831       fixP->fx_addnumber = value;
7832       value = 0;
7833     }
7834
7835   md_number_to_chars (p, value, fixP->fx_size);
7836 }
7837 \f
7838 char *
7839 md_atof (int type, char *litP, int *sizeP)
7840 {
7841   /* This outputs the LITTLENUMs in REVERSE order;
7842      in accord with the bigendian 386.  */
7843   return ieee_md_atof (type, litP, sizeP, FALSE);
7844 }
7845 \f
7846 static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
7847
7848 static char *
7849 output_invalid (int c)
7850 {
7851   if (ISPRINT (c))
7852     snprintf (output_invalid_buf, sizeof (output_invalid_buf),
7853               "'%c'", c);
7854   else
7855     snprintf (output_invalid_buf, sizeof (output_invalid_buf),
7856               "(0x%x)", (unsigned char) c);
7857   return output_invalid_buf;
7858 }
7859
7860 /* REG_STRING starts *before* REGISTER_PREFIX.  */
7861
7862 static const reg_entry *
7863 parse_real_register (char *reg_string, char **end_op)
7864 {
7865   char *s = reg_string;
7866   char *p;
7867   char reg_name_given[MAX_REG_NAME_SIZE + 1];
7868   const reg_entry *r;
7869
7870   /* Skip possible REGISTER_PREFIX and possible whitespace.  */
7871   if (*s == REGISTER_PREFIX)
7872     ++s;
7873
7874   if (is_space_char (*s))
7875     ++s;
7876
7877   p = reg_name_given;
7878   while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
7879     {
7880       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
7881         return (const reg_entry *) NULL;
7882       s++;
7883     }
7884
7885   /* For naked regs, make sure that we are not dealing with an identifier.
7886      This prevents confusing an identifier like `eax_var' with register
7887      `eax'.  */
7888   if (allow_naked_reg && identifier_chars[(unsigned char) *s])
7889     return (const reg_entry *) NULL;
7890
7891   *end_op = s;
7892
7893   r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
7894
7895   /* Handle floating point regs, allowing spaces in the (i) part.  */
7896   if (r == i386_regtab /* %st is first entry of table  */)
7897     {
7898       if (is_space_char (*s))
7899         ++s;
7900       if (*s == '(')
7901         {
7902           ++s;
7903           if (is_space_char (*s))
7904             ++s;
7905           if (*s >= '0' && *s <= '7')
7906             {
7907               int fpr = *s - '0';
7908               ++s;
7909               if (is_space_char (*s))
7910                 ++s;
7911               if (*s == ')')
7912                 {
7913                   *end_op = s + 1;
7914                   r = (const reg_entry *) hash_find (reg_hash, "st(0)");
7915                   know (r);
7916                   return r + fpr;
7917                 }
7918             }
7919           /* We have "%st(" then garbage.  */
7920           return (const reg_entry *) NULL;
7921         }
7922     }
7923
7924   if (r == NULL || allow_pseudo_reg)
7925     return r;
7926
7927   if (operand_type_all_zero (&r->reg_type))
7928     return (const reg_entry *) NULL;
7929
7930   if ((r->reg_type.bitfield.reg32
7931        || r->reg_type.bitfield.sreg3
7932        || r->reg_type.bitfield.control
7933        || r->reg_type.bitfield.debug
7934        || r->reg_type.bitfield.test)
7935       && !cpu_arch_flags.bitfield.cpui386)
7936     return (const reg_entry *) NULL;
7937
7938   if (r->reg_type.bitfield.floatreg
7939       && !cpu_arch_flags.bitfield.cpu8087
7940       && !cpu_arch_flags.bitfield.cpu287
7941       && !cpu_arch_flags.bitfield.cpu387)
7942     return (const reg_entry *) NULL;
7943
7944   if (r->reg_type.bitfield.regmmx && !cpu_arch_flags.bitfield.cpummx)
7945     return (const reg_entry *) NULL;
7946
7947   if (r->reg_type.bitfield.regxmm && !cpu_arch_flags.bitfield.cpusse)
7948     return (const reg_entry *) NULL;
7949
7950   if (r->reg_type.bitfield.regymm && !cpu_arch_flags.bitfield.cpuavx)
7951     return (const reg_entry *) NULL;
7952
7953   /* Don't allow fake index register unless allow_index_reg isn't 0. */
7954   if (!allow_index_reg
7955       && (r->reg_num == RegEiz || r->reg_num == RegRiz))
7956     return (const reg_entry *) NULL;
7957
7958   if (((r->reg_flags & (RegRex64 | RegRex))
7959        || r->reg_type.bitfield.reg64)
7960       && (!cpu_arch_flags.bitfield.cpulm
7961           || !operand_type_equal (&r->reg_type, &control))
7962       && flag_code != CODE_64BIT)
7963     return (const reg_entry *) NULL;
7964
7965   if (r->reg_type.bitfield.sreg3 && r->reg_num == RegFlat && !intel_syntax)
7966     return (const reg_entry *) NULL;
7967
7968   return r;
7969 }
7970
7971 /* REG_STRING starts *before* REGISTER_PREFIX.  */
7972
7973 static const reg_entry *
7974 parse_register (char *reg_string, char **end_op)
7975 {
7976   const reg_entry *r;
7977
7978   if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
7979     r = parse_real_register (reg_string, end_op);
7980   else
7981     r = NULL;
7982   if (!r)
7983     {
7984       char *save = input_line_pointer;
7985       char c;
7986       symbolS *symbolP;
7987
7988       input_line_pointer = reg_string;
7989       c = get_symbol_end ();
7990       symbolP = symbol_find (reg_string);
7991       if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
7992         {
7993           const expressionS *e = symbol_get_value_expression (symbolP);
7994
7995           know (e->X_op == O_register);
7996           know (e->X_add_number >= 0
7997                 && (valueT) e->X_add_number < i386_regtab_size);
7998           r = i386_regtab + e->X_add_number;
7999           *end_op = input_line_pointer;
8000         }
8001       *input_line_pointer = c;
8002       input_line_pointer = save;
8003     }
8004   return r;
8005 }
8006
8007 int
8008 i386_parse_name (char *name, expressionS *e, char *nextcharP)
8009 {
8010   const reg_entry *r;
8011   char *end = input_line_pointer;
8012
8013   *end = *nextcharP;
8014   r = parse_register (name, &input_line_pointer);
8015   if (r && end <= input_line_pointer)
8016     {
8017       *nextcharP = *input_line_pointer;
8018       *input_line_pointer = 0;
8019       e->X_op = O_register;
8020       e->X_add_number = r - i386_regtab;
8021       return 1;
8022     }
8023   input_line_pointer = end;
8024   *end = 0;
8025   return intel_syntax ? i386_intel_parse_name (name, e) : 0;
8026 }
8027
8028 void
8029 md_operand (expressionS *e)
8030 {
8031   char *end;
8032   const reg_entry *r;
8033
8034   switch (*input_line_pointer)
8035     {
8036     case REGISTER_PREFIX:
8037       r = parse_real_register (input_line_pointer, &end);
8038       if (r)
8039         {
8040           e->X_op = O_register;
8041           e->X_add_number = r - i386_regtab;
8042           input_line_pointer = end;
8043         }
8044       break;
8045
8046     case '[':
8047       gas_assert (intel_syntax);
8048       end = input_line_pointer++;
8049       expression (e);
8050       if (*input_line_pointer == ']')
8051         {
8052           ++input_line_pointer;
8053           e->X_op_symbol = make_expr_symbol (e);
8054           e->X_add_symbol = NULL;
8055           e->X_add_number = 0;
8056           e->X_op = O_index;
8057         }
8058       else
8059         {
8060           e->X_op = O_absent;
8061           input_line_pointer = end;
8062         }
8063       break;
8064     }
8065 }
8066
8067 \f
8068 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8069 const char *md_shortopts = "kVQ:sqn";
8070 #else
8071 const char *md_shortopts = "qn";
8072 #endif
8073
8074 #define OPTION_32 (OPTION_MD_BASE + 0)
8075 #define OPTION_64 (OPTION_MD_BASE + 1)
8076 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
8077 #define OPTION_MARCH (OPTION_MD_BASE + 3)
8078 #define OPTION_MTUNE (OPTION_MD_BASE + 4)
8079 #define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
8080 #define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
8081 #define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
8082 #define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
8083 #define OPTION_MOLD_GCC (OPTION_MD_BASE + 9)
8084 #define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
8085 #define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
8086 #define OPTION_MAVXSCALAR (OPTION_MD_BASE + 12)
8087
8088 struct option md_longopts[] =
8089 {
8090   {"32", no_argument, NULL, OPTION_32},
8091 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
8092      || defined (TE_PE) || defined (TE_PEP))
8093   {"64", no_argument, NULL, OPTION_64},
8094 #endif
8095   {"divide", no_argument, NULL, OPTION_DIVIDE},
8096   {"march", required_argument, NULL, OPTION_MARCH},
8097   {"mtune", required_argument, NULL, OPTION_MTUNE},
8098   {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
8099   {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
8100   {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
8101   {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
8102   {"mold-gcc", no_argument, NULL, OPTION_MOLD_GCC},
8103   {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
8104   {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
8105   {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
8106   {NULL, no_argument, NULL, 0}
8107 };
8108 size_t md_longopts_size = sizeof (md_longopts);
8109
8110 int
8111 md_parse_option (int c, char *arg)
8112 {
8113   unsigned int j;
8114   char *arch, *next;
8115
8116   switch (c)
8117     {
8118     case 'n':
8119       optimize_align_code = 0;
8120       break;
8121
8122     case 'q':
8123       quiet_warnings = 1;
8124       break;
8125
8126 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8127       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
8128          should be emitted or not.  FIXME: Not implemented.  */
8129     case 'Q':
8130       break;
8131
8132       /* -V: SVR4 argument to print version ID.  */
8133     case 'V':
8134       print_version_id ();
8135       break;
8136
8137       /* -k: Ignore for FreeBSD compatibility.  */
8138     case 'k':
8139       break;
8140
8141     case 's':
8142       /* -s: On i386 Solaris, this tells the native assembler to use
8143          .stab instead of .stab.excl.  We always use .stab anyhow.  */
8144       break;
8145 #endif
8146 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
8147      || defined (TE_PE) || defined (TE_PEP))
8148     case OPTION_64:
8149       {
8150         const char **list, **l;
8151
8152         list = bfd_target_list ();
8153         for (l = list; *l != NULL; l++)
8154           if (CONST_STRNEQ (*l, "elf64-x86-64")
8155               || strcmp (*l, "coff-x86-64") == 0
8156               || strcmp (*l, "pe-x86-64") == 0
8157               || strcmp (*l, "pei-x86-64") == 0)
8158             {
8159               default_arch = "x86_64";
8160               break;
8161             }
8162         if (*l == NULL)
8163           as_fatal (_("No compiled in support for x86_64"));
8164         free (list);
8165       }
8166       break;
8167 #endif
8168
8169     case OPTION_32:
8170       default_arch = "i386";
8171       break;
8172
8173     case OPTION_DIVIDE:
8174 #ifdef SVR4_COMMENT_CHARS
8175       {
8176         char *n, *t;
8177         const char *s;
8178
8179         n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
8180         t = n;
8181         for (s = i386_comment_chars; *s != '\0'; s++)
8182           if (*s != '/')
8183             *t++ = *s;
8184         *t = '\0';
8185         i386_comment_chars = n;
8186       }
8187 #endif
8188       break;
8189
8190     case OPTION_MARCH:
8191       arch = xstrdup (arg);
8192       do
8193         {
8194           if (*arch == '.')
8195             as_fatal (_("Invalid -march= option: `%s'"), arg);
8196           next = strchr (arch, '+');
8197           if (next)
8198             *next++ = '\0';
8199           for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
8200             {
8201               if (strcmp (arch, cpu_arch [j].name) == 0)
8202                 {
8203                   /* Processor.  */
8204                   if (! cpu_arch[j].flags.bitfield.cpui386)
8205                     continue;
8206
8207                   cpu_arch_name = cpu_arch[j].name;
8208                   cpu_sub_arch_name = NULL;
8209                   cpu_arch_flags = cpu_arch[j].flags;
8210                   cpu_arch_isa = cpu_arch[j].type;
8211                   cpu_arch_isa_flags = cpu_arch[j].flags;
8212                   if (!cpu_arch_tune_set)
8213                     {
8214                       cpu_arch_tune = cpu_arch_isa;
8215                       cpu_arch_tune_flags = cpu_arch_isa_flags;
8216                     }
8217                   break;
8218                 }
8219               else if (*cpu_arch [j].name == '.'
8220                        && strcmp (arch, cpu_arch [j].name + 1) == 0)
8221                 {
8222                   /* ISA entension.  */
8223                   i386_cpu_flags flags;
8224
8225                   if (!cpu_arch[j].negated)
8226                     flags = cpu_flags_or (cpu_arch_flags,
8227                                           cpu_arch[j].flags);
8228                   else
8229                     flags = cpu_flags_and_not (cpu_arch_flags,
8230                                                cpu_arch[j].flags);
8231                   if (!cpu_flags_equal (&flags, &cpu_arch_flags))
8232                     {
8233                       if (cpu_sub_arch_name)
8234                         {
8235                           char *name = cpu_sub_arch_name;
8236                           cpu_sub_arch_name = concat (name,
8237                                                       cpu_arch[j].name,
8238                                                       (const char *) NULL);
8239                           free (name);
8240                         }
8241                       else
8242                         cpu_sub_arch_name = xstrdup (cpu_arch[j].name);
8243                       cpu_arch_flags = flags;
8244                     }
8245                   break;
8246                 }
8247             }
8248
8249           if (j >= ARRAY_SIZE (cpu_arch))
8250             as_fatal (_("Invalid -march= option: `%s'"), arg);
8251
8252           arch = next;
8253         }
8254       while (next != NULL );
8255       break;
8256
8257     case OPTION_MTUNE:
8258       if (*arg == '.')
8259         as_fatal (_("Invalid -mtune= option: `%s'"), arg);
8260       for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
8261         {
8262           if (strcmp (arg, cpu_arch [j].name) == 0)
8263             {
8264               cpu_arch_tune_set = 1;
8265               cpu_arch_tune = cpu_arch [j].type;
8266               cpu_arch_tune_flags = cpu_arch[j].flags;
8267               break;
8268             }
8269         }
8270       if (j >= ARRAY_SIZE (cpu_arch))
8271         as_fatal (_("Invalid -mtune= option: `%s'"), arg);
8272       break;
8273
8274     case OPTION_MMNEMONIC:
8275       if (strcasecmp (arg, "att") == 0)
8276         intel_mnemonic = 0;
8277       else if (strcasecmp (arg, "intel") == 0)
8278         intel_mnemonic = 1;
8279       else
8280         as_fatal (_("Invalid -mmnemonic= option: `%s'"), arg);
8281       break;
8282
8283     case OPTION_MSYNTAX:
8284       if (strcasecmp (arg, "att") == 0)
8285         intel_syntax = 0;
8286       else if (strcasecmp (arg, "intel") == 0)
8287         intel_syntax = 1;
8288       else
8289         as_fatal (_("Invalid -msyntax= option: `%s'"), arg);
8290       break;
8291
8292     case OPTION_MINDEX_REG:
8293       allow_index_reg = 1;
8294       break;
8295
8296     case OPTION_MNAKED_REG:
8297       allow_naked_reg = 1;
8298       break;
8299
8300     case OPTION_MOLD_GCC:
8301       old_gcc = 1;
8302       break;
8303
8304     case OPTION_MSSE2AVX:
8305       sse2avx = 1;
8306       break;
8307
8308     case OPTION_MSSE_CHECK:
8309       if (strcasecmp (arg, "error") == 0)
8310         sse_check = sse_check_error;
8311       else if (strcasecmp (arg, "warning") == 0)
8312         sse_check = sse_check_warning;
8313       else if (strcasecmp (arg, "none") == 0)
8314         sse_check = sse_check_none;
8315       else
8316         as_fatal (_("Invalid -msse-check= option: `%s'"), arg);
8317       break;
8318
8319     case OPTION_MAVXSCALAR:
8320       if (strcasecmp (arg, "128") == 0)
8321         avxscalar = vex128;
8322       else if (strcasecmp (arg, "256") == 0)
8323         avxscalar = vex256;
8324       else
8325         as_fatal (_("Invalid -mavxscalar= option: `%s'"), arg);
8326       break;
8327
8328     default:
8329       return 0;
8330     }
8331   return 1;
8332 }
8333
8334 #define MESSAGE_TEMPLATE \
8335 "                                                                                "
8336
8337 static void
8338 show_arch (FILE *stream, int ext, int check)
8339 {
8340   static char message[] = MESSAGE_TEMPLATE;
8341   char *start = message + 27;
8342   char *p;
8343   int size = sizeof (MESSAGE_TEMPLATE);
8344   int left;
8345   const char *name;
8346   int len;
8347   unsigned int j;
8348
8349   p = start;
8350   left = size - (start - message);
8351   for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
8352     {
8353       /* Should it be skipped?  */
8354       if (cpu_arch [j].skip)
8355         continue;
8356
8357       name = cpu_arch [j].name;
8358       len = cpu_arch [j].len;
8359       if (*name == '.')
8360         {
8361           /* It is an extension.  Skip if we aren't asked to show it.  */
8362           if (ext)
8363             {
8364               name++;
8365               len--;
8366             }
8367           else
8368             continue;
8369         }
8370       else if (ext)
8371         {
8372           /* It is an processor.  Skip if we show only extension.  */
8373           continue;
8374         }
8375       else if (check && ! cpu_arch[j].flags.bitfield.cpui386)
8376         {
8377           /* It is an impossible processor - skip.  */
8378           continue;
8379         }
8380
8381       /* Reserve 2 spaces for ", " or ",\0" */
8382       left -= len + 2;
8383
8384       /* Check if there is any room.  */
8385       if (left >= 0)
8386         {
8387           if (p != start)
8388             {
8389               *p++ = ',';
8390               *p++ = ' ';
8391             }
8392           p = mempcpy (p, name, len);
8393         }
8394       else
8395         {
8396           /* Output the current message now and start a new one.  */
8397           *p++ = ',';
8398           *p = '\0';
8399           fprintf (stream, "%s\n", message);
8400           p = start;
8401           left = size - (start - message) - len - 2;
8402           
8403           gas_assert (left >= 0);
8404
8405           p = mempcpy (p, name, len);
8406         }
8407     }
8408
8409   *p = '\0';
8410   fprintf (stream, "%s\n", message);
8411 }
8412
8413 void
8414 md_show_usage (FILE *stream)
8415 {
8416 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8417   fprintf (stream, _("\
8418   -Q                      ignored\n\
8419   -V                      print assembler version number\n\
8420   -k                      ignored\n"));
8421 #endif
8422   fprintf (stream, _("\
8423   -n                      Do not optimize code alignment\n\
8424   -q                      quieten some warnings\n"));
8425 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8426   fprintf (stream, _("\
8427   -s                      ignored\n"));
8428 #endif
8429 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
8430      || defined (TE_PE) || defined (TE_PEP))
8431   fprintf (stream, _("\
8432   --32/--64               generate 32bit/64bit code\n"));
8433 #endif
8434 #ifdef SVR4_COMMENT_CHARS
8435   fprintf (stream, _("\
8436   --divide                do not treat `/' as a comment character\n"));
8437 #else
8438   fprintf (stream, _("\
8439   --divide                ignored\n"));
8440 #endif
8441   fprintf (stream, _("\
8442   -march=CPU[,+EXTENSION...]\n\
8443                           generate code for CPU and EXTENSION, CPU is one of:\n"));
8444   show_arch (stream, 0, 1);
8445   fprintf (stream, _("\
8446                           EXTENSION is combination of:\n"));
8447   show_arch (stream, 1, 0);
8448   fprintf (stream, _("\
8449   -mtune=CPU              optimize for CPU, CPU is one of:\n"));
8450   show_arch (stream, 0, 0);
8451   fprintf (stream, _("\
8452   -msse2avx               encode SSE instructions with VEX prefix\n"));
8453   fprintf (stream, _("\
8454   -msse-check=[none|error|warning]\n\
8455                           check SSE instructions\n"));
8456   fprintf (stream, _("\
8457   -mavxscalar=[128|256]   encode scalar AVX instructions with specific vector\n\
8458                            length\n"));
8459   fprintf (stream, _("\
8460   -mmnemonic=[att|intel]  use AT&T/Intel mnemonic\n"));
8461   fprintf (stream, _("\
8462   -msyntax=[att|intel]    use AT&T/Intel syntax\n"));
8463   fprintf (stream, _("\
8464   -mindex-reg             support pseudo index registers\n"));
8465   fprintf (stream, _("\
8466   -mnaked-reg             don't require `%%' prefix for registers\n"));
8467   fprintf (stream, _("\
8468   -mold-gcc               support old (<= 2.8.1) versions of gcc\n"));
8469 }
8470
8471 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
8472      || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
8473      || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
8474
8475 /* Pick the target format to use.  */
8476
8477 const char *
8478 i386_target_format (void)
8479 {
8480   if (!strcmp (default_arch, "x86_64"))
8481     update_code_flag (CODE_64BIT, 1);
8482   else if (!strcmp (default_arch, "i386"))
8483     update_code_flag (CODE_32BIT, 1);
8484   else
8485     as_fatal (_("Unknown architecture"));
8486
8487   if (cpu_flags_all_zero (&cpu_arch_isa_flags))
8488     cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].flags;
8489   if (cpu_flags_all_zero (&cpu_arch_tune_flags))
8490     cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].flags;
8491
8492   switch (OUTPUT_FLAVOR)
8493     {
8494 #if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
8495     case bfd_target_aout_flavour:
8496       return AOUT_TARGET_FORMAT;
8497 #endif
8498 #if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
8499 # if defined (TE_PE) || defined (TE_PEP)
8500     case bfd_target_coff_flavour:
8501       return flag_code == CODE_64BIT ? "pe-x86-64" : "pe-i386";
8502 # elif defined (TE_GO32)
8503     case bfd_target_coff_flavour:
8504       return "coff-go32";
8505 # else
8506     case bfd_target_coff_flavour:
8507       return "coff-i386";
8508 # endif
8509 #endif
8510 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
8511     case bfd_target_elf_flavour:
8512       {
8513         if (flag_code == CODE_64BIT)
8514           {
8515             object_64bit = 1;
8516             use_rela_relocations = 1;
8517           }
8518         if (cpu_arch_isa == PROCESSOR_L1OM)
8519           {
8520             if (flag_code != CODE_64BIT)
8521               as_fatal (_("Intel L1OM is 64bit only"));
8522             return ELF_TARGET_L1OM_FORMAT;
8523           }
8524         else
8525           return (flag_code == CODE_64BIT
8526                   ? ELF_TARGET_FORMAT64 : ELF_TARGET_FORMAT);
8527       }
8528 #endif
8529 #if defined (OBJ_MACH_O)
8530     case bfd_target_mach_o_flavour:
8531       return flag_code == CODE_64BIT ? "mach-o-x86-64" : "mach-o-i386";
8532 #endif
8533     default:
8534       abort ();
8535       return NULL;
8536     }
8537 }
8538
8539 #endif /* OBJ_MAYBE_ more than one  */
8540
8541 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
8542 void
8543 i386_elf_emit_arch_note (void)
8544 {
8545   if (IS_ELF && cpu_arch_name != NULL)
8546     {
8547       char *p;
8548       asection *seg = now_seg;
8549       subsegT subseg = now_subseg;
8550       Elf_Internal_Note i_note;
8551       Elf_External_Note e_note;
8552       asection *note_secp;
8553       int len;
8554
8555       /* Create the .note section.  */
8556       note_secp = subseg_new (".note", 0);
8557       bfd_set_section_flags (stdoutput,
8558                              note_secp,
8559                              SEC_HAS_CONTENTS | SEC_READONLY);
8560
8561       /* Process the arch string.  */
8562       len = strlen (cpu_arch_name);
8563
8564       i_note.namesz = len + 1;
8565       i_note.descsz = 0;
8566       i_note.type = NT_ARCH;
8567       p = frag_more (sizeof (e_note.namesz));
8568       md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
8569       p = frag_more (sizeof (e_note.descsz));
8570       md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
8571       p = frag_more (sizeof (e_note.type));
8572       md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
8573       p = frag_more (len + 1);
8574       strcpy (p, cpu_arch_name);
8575
8576       frag_align (2, 0, 0);
8577
8578       subseg_set (seg, subseg);
8579     }
8580 }
8581 #endif
8582 \f
8583 symbolS *
8584 md_undefined_symbol (name)
8585      char *name;
8586 {
8587   if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
8588       && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
8589       && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
8590       && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
8591     {
8592       if (!GOT_symbol)
8593         {
8594           if (symbol_find (name))
8595             as_bad (_("GOT already in symbol table"));
8596           GOT_symbol = symbol_new (name, undefined_section,
8597                                    (valueT) 0, &zero_address_frag);
8598         };
8599       return GOT_symbol;
8600     }
8601   return 0;
8602 }
8603
8604 /* Round up a section size to the appropriate boundary.  */
8605
8606 valueT
8607 md_section_align (segment, size)
8608      segT segment ATTRIBUTE_UNUSED;
8609      valueT size;
8610 {
8611 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
8612   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
8613     {
8614       /* For a.out, force the section size to be aligned.  If we don't do
8615          this, BFD will align it for us, but it will not write out the
8616          final bytes of the section.  This may be a bug in BFD, but it is
8617          easier to fix it here since that is how the other a.out targets
8618          work.  */
8619       int align;
8620
8621       align = bfd_get_section_alignment (stdoutput, segment);
8622       size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
8623     }
8624 #endif
8625
8626   return size;
8627 }
8628
8629 /* On the i386, PC-relative offsets are relative to the start of the
8630    next instruction.  That is, the address of the offset, plus its
8631    size, since the offset is always the last part of the insn.  */
8632
8633 long
8634 md_pcrel_from (fixS *fixP)
8635 {
8636   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
8637 }
8638
8639 #ifndef I386COFF
8640
8641 static void
8642 s_bss (int ignore ATTRIBUTE_UNUSED)
8643 {
8644   int temp;
8645
8646 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8647   if (IS_ELF)
8648     obj_elf_section_change_hook ();
8649 #endif
8650   temp = get_absolute_expression ();
8651   subseg_set (bss_section, (subsegT) temp);
8652   demand_empty_rest_of_line ();
8653 }
8654
8655 #endif
8656
8657 void
8658 i386_validate_fix (fixS *fixp)
8659 {
8660   if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
8661     {
8662       if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
8663         {
8664           if (!object_64bit)
8665             abort ();
8666           fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
8667         }
8668       else
8669         {
8670           if (!object_64bit)
8671             fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
8672           else
8673             fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
8674         }
8675       fixp->fx_subsy = 0;
8676     }
8677 }
8678
8679 arelent *
8680 tc_gen_reloc (section, fixp)
8681      asection *section ATTRIBUTE_UNUSED;
8682      fixS *fixp;
8683 {
8684   arelent *rel;
8685   bfd_reloc_code_real_type code;
8686
8687   switch (fixp->fx_r_type)
8688     {
8689     case BFD_RELOC_X86_64_PLT32:
8690     case BFD_RELOC_X86_64_GOT32:
8691     case BFD_RELOC_X86_64_GOTPCREL:
8692     case BFD_RELOC_386_PLT32:
8693     case BFD_RELOC_386_GOT32:
8694     case BFD_RELOC_386_GOTOFF:
8695     case BFD_RELOC_386_GOTPC:
8696     case BFD_RELOC_386_TLS_GD:
8697     case BFD_RELOC_386_TLS_LDM:
8698     case BFD_RELOC_386_TLS_LDO_32:
8699     case BFD_RELOC_386_TLS_IE_32:
8700     case BFD_RELOC_386_TLS_IE:
8701     case BFD_RELOC_386_TLS_GOTIE:
8702     case BFD_RELOC_386_TLS_LE_32:
8703     case BFD_RELOC_386_TLS_LE:
8704     case BFD_RELOC_386_TLS_GOTDESC:
8705     case BFD_RELOC_386_TLS_DESC_CALL:
8706     case BFD_RELOC_X86_64_TLSGD:
8707     case BFD_RELOC_X86_64_TLSLD:
8708     case BFD_RELOC_X86_64_DTPOFF32:
8709     case BFD_RELOC_X86_64_DTPOFF64:
8710     case BFD_RELOC_X86_64_GOTTPOFF:
8711     case BFD_RELOC_X86_64_TPOFF32:
8712     case BFD_RELOC_X86_64_TPOFF64:
8713     case BFD_RELOC_X86_64_GOTOFF64:
8714     case BFD_RELOC_X86_64_GOTPC32:
8715     case BFD_RELOC_X86_64_GOT64:
8716     case BFD_RELOC_X86_64_GOTPCREL64:
8717     case BFD_RELOC_X86_64_GOTPC64:
8718     case BFD_RELOC_X86_64_GOTPLT64:
8719     case BFD_RELOC_X86_64_PLTOFF64:
8720     case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
8721     case BFD_RELOC_X86_64_TLSDESC_CALL:
8722     case BFD_RELOC_RVA:
8723     case BFD_RELOC_VTABLE_ENTRY:
8724     case BFD_RELOC_VTABLE_INHERIT:
8725 #ifdef TE_PE
8726     case BFD_RELOC_32_SECREL:
8727 #endif
8728       code = fixp->fx_r_type;
8729       break;
8730     case BFD_RELOC_X86_64_32S:
8731       if (!fixp->fx_pcrel)
8732         {
8733           /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
8734           code = fixp->fx_r_type;
8735           break;
8736         }
8737     default:
8738       if (fixp->fx_pcrel)
8739         {
8740           switch (fixp->fx_size)
8741             {
8742             default:
8743               as_bad_where (fixp->fx_file, fixp->fx_line,
8744                             _("can not do %d byte pc-relative relocation"),
8745                             fixp->fx_size);
8746               code = BFD_RELOC_32_PCREL;
8747               break;
8748             case 1: code = BFD_RELOC_8_PCREL;  break;
8749             case 2: code = BFD_RELOC_16_PCREL; break;
8750             case 4: code = BFD_RELOC_32_PCREL; break;
8751 #ifdef BFD64
8752             case 8: code = BFD_RELOC_64_PCREL; break;
8753 #endif
8754             }
8755         }
8756       else
8757         {
8758           switch (fixp->fx_size)
8759             {
8760             default:
8761               as_bad_where (fixp->fx_file, fixp->fx_line,
8762                             _("can not do %d byte relocation"),
8763                             fixp->fx_size);
8764               code = BFD_RELOC_32;
8765               break;
8766             case 1: code = BFD_RELOC_8;  break;
8767             case 2: code = BFD_RELOC_16; break;
8768             case 4: code = BFD_RELOC_32; break;
8769 #ifdef BFD64
8770             case 8: code = BFD_RELOC_64; break;
8771 #endif
8772             }
8773         }
8774       break;
8775     }
8776
8777   if ((code == BFD_RELOC_32
8778        || code == BFD_RELOC_32_PCREL
8779        || code == BFD_RELOC_X86_64_32S)
8780       && GOT_symbol
8781       && fixp->fx_addsy == GOT_symbol)
8782     {
8783       if (!object_64bit)
8784         code = BFD_RELOC_386_GOTPC;
8785       else
8786         code = BFD_RELOC_X86_64_GOTPC32;
8787     }
8788   if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
8789       && GOT_symbol
8790       && fixp->fx_addsy == GOT_symbol)
8791     {
8792       code = BFD_RELOC_X86_64_GOTPC64;
8793     }
8794
8795   rel = (arelent *) xmalloc (sizeof (arelent));
8796   rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
8797   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
8798
8799   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
8800
8801   if (!use_rela_relocations)
8802     {
8803       /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
8804          vtable entry to be used in the relocation's section offset.  */
8805       if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
8806         rel->address = fixp->fx_offset;
8807 #if defined (OBJ_COFF) && defined (TE_PE)
8808       else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
8809         rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
8810       else
8811 #endif
8812       rel->addend = 0;
8813     }
8814   /* Use the rela in 64bit mode.  */
8815   else
8816     {
8817       if (!fixp->fx_pcrel)
8818         rel->addend = fixp->fx_offset;
8819       else
8820         switch (code)
8821           {
8822           case BFD_RELOC_X86_64_PLT32:
8823           case BFD_RELOC_X86_64_GOT32:
8824           case BFD_RELOC_X86_64_GOTPCREL:
8825           case BFD_RELOC_X86_64_TLSGD:
8826           case BFD_RELOC_X86_64_TLSLD:
8827           case BFD_RELOC_X86_64_GOTTPOFF:
8828           case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
8829           case BFD_RELOC_X86_64_TLSDESC_CALL:
8830             rel->addend = fixp->fx_offset - fixp->fx_size;
8831             break;
8832           default:
8833             rel->addend = (section->vma
8834                            - fixp->fx_size
8835                            + fixp->fx_addnumber
8836                            + md_pcrel_from (fixp));
8837             break;
8838           }
8839     }
8840
8841   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
8842   if (rel->howto == NULL)
8843     {
8844       as_bad_where (fixp->fx_file, fixp->fx_line,
8845                     _("cannot represent relocation type %s"),
8846                     bfd_get_reloc_code_name (code));
8847       /* Set howto to a garbage value so that we can keep going.  */
8848       rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
8849       gas_assert (rel->howto != NULL);
8850     }
8851
8852   return rel;
8853 }
8854
8855 #include "tc-i386-intel.c"
8856
8857 void
8858 tc_x86_parse_to_dw2regnum (expressionS *exp)
8859 {
8860   int saved_naked_reg;
8861   char saved_register_dot;
8862
8863   saved_naked_reg = allow_naked_reg;
8864   allow_naked_reg = 1;
8865   saved_register_dot = register_chars['.'];
8866   register_chars['.'] = '.';
8867   allow_pseudo_reg = 1;
8868   expression_and_evaluate (exp);
8869   allow_pseudo_reg = 0;
8870   register_chars['.'] = saved_register_dot;
8871   allow_naked_reg = saved_naked_reg;
8872
8873   if (exp->X_op == O_register && exp->X_add_number >= 0)
8874     {
8875       if ((addressT) exp->X_add_number < i386_regtab_size)
8876         {
8877           exp->X_op = O_constant;
8878           exp->X_add_number = i386_regtab[exp->X_add_number]
8879                               .dw2_regnum[flag_code >> 1];
8880         }
8881       else
8882         exp->X_op = O_illegal;
8883     }
8884 }
8885
8886 void
8887 tc_x86_frame_initial_instructions (void)
8888 {
8889   static unsigned int sp_regno[2];
8890
8891   if (!sp_regno[flag_code >> 1])
8892     {
8893       char *saved_input = input_line_pointer;
8894       char sp[][4] = {"esp", "rsp"};
8895       expressionS exp;
8896
8897       input_line_pointer = sp[flag_code >> 1];
8898       tc_x86_parse_to_dw2regnum (&exp);
8899       gas_assert (exp.X_op == O_constant);
8900       sp_regno[flag_code >> 1] = exp.X_add_number;
8901       input_line_pointer = saved_input;
8902     }
8903
8904   cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
8905   cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
8906 }
8907
8908 int
8909 i386_elf_section_type (const char *str, size_t len)
8910 {
8911   if (flag_code == CODE_64BIT
8912       && len == sizeof ("unwind") - 1
8913       && strncmp (str, "unwind", 6) == 0)
8914     return SHT_X86_64_UNWIND;
8915
8916   return -1;
8917 }
8918
8919 #ifdef TE_SOLARIS
8920 void
8921 i386_solaris_fix_up_eh_frame (segT sec)
8922 {
8923   if (flag_code == CODE_64BIT)
8924     elf_section_type (sec) = SHT_X86_64_UNWIND;
8925 }
8926 #endif
8927
8928 #ifdef TE_PE
8929 void
8930 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
8931 {
8932   expressionS exp;
8933
8934   exp.X_op = O_secrel;
8935   exp.X_add_symbol = symbol;
8936   exp.X_add_number = 0;
8937   emit_expr (&exp, size);
8938 }
8939 #endif
8940
8941 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8942 /* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
8943
8944 bfd_vma
8945 x86_64_section_letter (int letter, char **ptr_msg)
8946 {
8947   if (flag_code == CODE_64BIT)
8948     {
8949       if (letter == 'l')
8950         return SHF_X86_64_LARGE;
8951
8952       *ptr_msg = _("Bad .section directive: want a,l,w,x,M,S,G,T in string");
8953     }
8954   else
8955     *ptr_msg = _("Bad .section directive: want a,w,x,M,S,G,T in string");
8956   return -1;
8957 }
8958
8959 bfd_vma
8960 x86_64_section_word (char *str, size_t len)
8961 {
8962   if (len == 5 && flag_code == CODE_64BIT && CONST_STRNEQ (str, "large"))
8963     return SHF_X86_64_LARGE;
8964
8965   return -1;
8966 }
8967
8968 static void
8969 handle_large_common (int small ATTRIBUTE_UNUSED)
8970 {
8971   if (flag_code != CODE_64BIT)
8972     {
8973       s_comm_internal (0, elf_common_parse);
8974       as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
8975     }
8976   else
8977     {
8978       static segT lbss_section;
8979       asection *saved_com_section_ptr = elf_com_section_ptr;
8980       asection *saved_bss_section = bss_section;
8981
8982       if (lbss_section == NULL)
8983         {
8984           flagword applicable;
8985           segT seg = now_seg;
8986           subsegT subseg = now_subseg;
8987
8988           /* The .lbss section is for local .largecomm symbols.  */
8989           lbss_section = subseg_new (".lbss", 0);
8990           applicable = bfd_applicable_section_flags (stdoutput);
8991           bfd_set_section_flags (stdoutput, lbss_section,
8992                                  applicable & SEC_ALLOC);
8993           seg_info (lbss_section)->bss = 1;
8994
8995           subseg_set (seg, subseg);
8996         }
8997
8998       elf_com_section_ptr = &_bfd_elf_large_com_section;
8999       bss_section = lbss_section;
9000
9001       s_comm_internal (0, elf_common_parse);
9002
9003       elf_com_section_ptr = saved_com_section_ptr;
9004       bss_section = saved_bss_section;
9005     }
9006 }
9007 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */