Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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