Ignore:
Timestamp:
Aug 16, 2003, 11:33:53 PM (22 years ago)
Author:
bird
Message:

Joined the port of 2.11.2 with 2.14.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/binutils/gas/config/tc-i386.c

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r617 r618  
    11/* i386.c -- Assemble code for the Intel 80386
    22   Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
    3    2000, 2001
     3   2000, 2001, 2002, 2003
    44   Free Software Foundation, Inc.
    55
     
    2727   Please help us make it better.  */
    2828
    29 #include <ctype.h>
    30 
    3129#include "as.h"
     30#include "safe-ctype.h"
    3231#include "subsegs.h"
    3332#include "dwarf2dbg.h"
     
    5049#endif
    5150
    52 #define true 1
    53 #define false 0
    54 
    55 static unsigned int mode_from_disp_size PARAMS ((unsigned int));
    56 static int fits_in_signed_byte PARAMS ((offsetT));
    57 static int fits_in_unsigned_byte PARAMS ((offsetT));
    58 static int fits_in_unsigned_word PARAMS ((offsetT));
    59 static int fits_in_signed_word PARAMS ((offsetT));
    60 static int fits_in_unsigned_long PARAMS ((offsetT));
    61 static int fits_in_signed_long PARAMS ((offsetT));
     51#ifdef BFD_ASSEMBLER
     52#define RELOC_ENUM enum bfd_reloc_code_real
     53#else
     54#define RELOC_ENUM int
     55#endif
     56
     57#ifndef DEFAULT_ARCH
     58#define DEFAULT_ARCH "i386"
     59#endif
     60
     61#ifndef INLINE
     62#if __GNUC__ >= 2
     63#define INLINE __inline__
     64#else
     65#define INLINE
     66#endif
     67#endif
     68
     69static INLINE unsigned int mode_from_disp_size PARAMS ((unsigned int));
     70static INLINE int fits_in_signed_byte PARAMS ((offsetT));
     71static INLINE int fits_in_unsigned_byte PARAMS ((offsetT));
     72static INLINE int fits_in_unsigned_word PARAMS ((offsetT));
     73static INLINE int fits_in_signed_word PARAMS ((offsetT));
     74static INLINE int fits_in_unsigned_long PARAMS ((offsetT));
     75static INLINE int fits_in_signed_long PARAMS ((offsetT));
    6276static int smallest_imm_type PARAMS ((offsetT));
    6377static offsetT offset_in_range PARAMS ((offsetT, int));
     
    6781static void set_intel_syntax PARAMS ((int));
    6882static void set_cpu_arch PARAMS ((int));
    69 
    70 #ifdef BFD_ASSEMBLER
    71 static bfd_reloc_code_real_type reloc
    72   PARAMS ((int, int, int, bfd_reloc_code_real_type));
    73 #define RELOC_ENUM enum bfd_reloc_code_real
    74 #else
    75 #define RELOC_ENUM int
    76 #endif
    77 
    78 #ifndef DEFAULT_ARCH
    79 #define DEFAULT_ARCH "i386"
    80 #endif
    81 static char *default_arch = DEFAULT_ARCH;
     83static char *output_invalid PARAMS ((int c));
     84static int i386_operand PARAMS ((char *operand_string));
     85static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
     86static const reg_entry *parse_register PARAMS ((char *reg_string,
     87                                                char **end_op));
     88static char *parse_insn PARAMS ((char *, char *));
     89static char *parse_operands PARAMS ((char *, const char *));
     90static void swap_operands PARAMS ((void));
     91static void optimize_imm PARAMS ((void));
     92static void optimize_disp PARAMS ((void));
     93static int match_template PARAMS ((void));
     94static int check_string PARAMS ((void));
     95static int process_suffix PARAMS ((void));
     96static int check_byte_reg PARAMS ((void));
     97static int check_long_reg PARAMS ((void));
     98static int check_qword_reg PARAMS ((void));
     99static int check_word_reg PARAMS ((void));
     100static int finalize_imm PARAMS ((void));
     101static int process_operands PARAMS ((void));
     102static const seg_entry *build_modrm_byte PARAMS ((void));
     103static void output_insn PARAMS ((void));
     104static void output_branch PARAMS ((void));
     105static void output_jump PARAMS ((void));
     106static void output_interseg_jump PARAMS ((void));
     107static void output_imm PARAMS ((fragS *insn_start_frag,
     108                                offsetT insn_start_off));
     109static void output_disp PARAMS ((fragS *insn_start_frag,
     110                                 offsetT insn_start_off));
     111#ifndef I386COFF
     112static void s_bss PARAMS ((int));
     113#endif
     114
     115static const char *default_arch = DEFAULT_ARCH;
    82116
    83117/* 'md_assemble ()' gathers together information and puts it into a
     
    151185   operand.  Used to prevent the scrubber eating vital white-space.  */
    152186#ifdef LEX_AT
    153 const char extra_symbol_chars[] = "*%-(@";
     187const char extra_symbol_chars[] = "*%-(@[";
    154188#else
    155 const char extra_symbol_chars[] = "*%-(";
    156 #endif
    157 
     189const char extra_symbol_chars[] = "*%-([";
     190#endif
     191
     192#if (defined (TE_I386AIX)                               \
     193     || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
     194         && !defined (TE_LINUX)                         \
     195         && !defined (TE_FreeBSD)                       \
     196         && !defined (TE_NetBSD)))
    158197/* This array holds the chars that always start a comment.  If the
    159198   pre-processor is disabled, these aren't very useful.  */
    160 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD))
    161 /* Putting '/' here makes it impossible to use the divide operator.
    162    However, we need it for compatibility with SVR4 systems.  */
    163199const char comment_chars[] = "#/";
    164200#define PREFIX_SEPARATOR '\\'
    165 #else
    166 const char comment_chars[] = "#";
    167 #define PREFIX_SEPARATOR '/'
    168 #endif
    169201
    170202/* This array holds the chars that only start a comment at the beginning of
     
    176208   Also note that comments started like this one will always work if
    177209   '/' isn't otherwise defined.  */
    178 #if defined (TE_I386AIX) || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && ! defined (TE_LINUX) && !defined(TE_FreeBSD))
    179 const char line_comment_chars[] = "";
     210const char line_comment_chars[] = "#";
     211
    180212#else
    181 const char line_comment_chars[] = "/";
     213/* Putting '/' here makes it impossible to use the divide operator.
     214   However, we need it for compatibility with SVR4 systems.  */
     215const char comment_chars[] = "#";
     216#define PREFIX_SEPARATOR '/'
     217
     218const char line_comment_chars[] = "/#";
    182219#endif
    183220
     
    273310
    274311/* CPU feature flags.  */
    275 static unsigned int cpu_arch_flags = CpuUnknownFlags|CpuNo64;
     312static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
    276313
    277314/* If set, conditional jumps are not automatically promoted to handle
    278315   larger than a byte offset.  */
    279316static unsigned int no_cond_jump_promotion = 0;
     317
     318/* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
     319symbolS *GOT_symbol;
    280320
    281321/* Interface to relax_segment.
     
    292332#define CODE16  1
    293333#define SMALL   0
    294 #define SMALL16 (SMALL|CODE16)
     334#define SMALL16 (SMALL | CODE16)
    295335#define BIG     2
    296 #define BIG16   (BIG|CODE16)
     336#define BIG16   (BIG | CODE16)
    297337
    298338#ifndef INLINE
     
    374414  {NULL, 0 }
    375415};
     416
     417const pseudo_typeS md_pseudo_table[] =
     418{
     419#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
     420  {"align", s_align_bytes, 0},
     421#else
     422  {"align", s_align_ptwo, 0},
     423#endif
     424  {"arch", set_cpu_arch, 0},
     425#ifndef I386COFF
     426  {"bss", s_bss, 0},
     427#endif
     428  {"ffloat", float_cons, 'f'},
     429  {"dfloat", float_cons, 'd'},
     430  {"tfloat", float_cons, 'x'},
     431  {"value", cons, 2},
     432  {"noopt", s_ignore, 0},
     433  {"optim", s_ignore, 0},
     434  {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
     435  {"code16", set_code_flag, CODE_16BIT},
     436  {"code32", set_code_flag, CODE_32BIT},
     437  {"code64", set_code_flag, CODE_64BIT},
     438  {"intel_syntax", set_intel_syntax, 1},
     439  {"att_syntax", set_intel_syntax, 0},
     440  {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
     441  {"loc", dwarf2_directive_loc, 0},
     442  {0, 0, 0}
     443};
     444
     445/* For interface with expression ().  */
     446extern char *input_line_pointer;
     447
     448/* Hash table for instruction mnemonic lookup.  */
     449static struct hash_control *op_hash;
     450
     451/* Hash table for register lookup.  */
     452static struct hash_control *reg_hash;
     453
    376454
    377455void
     
    447525  };
    448526
    449   /* ??? We can't use these fillers for x86_64, since they often kills the
    450      upper halves.  Solve later.  */
     527  if (count <= 0 || count > 15)
     528    return;
     529
     530  /* The recommended way to pad 64bit code is to use NOPs preceded by
     531     maximally four 0x66 prefixes.  Balance the size of nops.  */
    451532  if (flag_code == CODE_64BIT)
    452     count = 1;
    453 
    454   if (count > 0 && count <= 15)
    455     {
    456       if (flag_code == CODE_16BIT)
    457         {
    458           memcpy (fragP->fr_literal + fragP->fr_fix,
    459                   f16_patt[count - 1], count);
    460           if (count > 8)
    461             /* Adjust jump offset.  */
    462             fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
    463         }
    464       else
     533    {
     534      int i;
     535      int nnops = (count + 3) / 4;
     536      int len = count / nnops;
     537      int remains = count - nnops * len;
     538      int pos = 0;
     539
     540      for (i = 0; i < remains; i++)
     541        {
     542          memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len);
     543          fragP->fr_literal[fragP->fr_fix + pos + len] = 0x90;
     544          pos += len + 1;
     545        }
     546      for (; i < nnops; i++)
     547        {
     548          memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len - 1);
     549          fragP->fr_literal[fragP->fr_fix + pos + len - 1] = 0x90;
     550          pos += len;
     551        }
     552    }
     553  else
     554    if (flag_code == CODE_16BIT)
     555      {
    465556        memcpy (fragP->fr_literal + fragP->fr_fix,
    466                 f32_patt[count - 1], count);
    467       fragP->fr_var = count;
    468     }
    469 }
    470 
    471 static char *output_invalid PARAMS ((int c));
    472 static int i386_operand PARAMS ((char *operand_string));
    473 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
    474 static const reg_entry *parse_register PARAMS ((char *reg_string,
    475                                                 char **end_op));
    476 
    477 #ifndef I386COFF
    478 static void s_bss PARAMS ((int));
    479 #endif
    480 
    481 symbolS *GOT_symbol;            /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
     557                f16_patt[count - 1], count);
     558        if (count > 8)
     559          /* Adjust jump offset.  */
     560          fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
     561      }
     562    else
     563      memcpy (fragP->fr_literal + fragP->fr_fix,
     564              f32_patt[count - 1], count);
     565  fragP->fr_var = count;
     566}
    482567
    483568static INLINE unsigned int
     
    541626     offsetT num;
    542627{
    543   if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64)
    544       && !(cpu_arch_flags & (CpuUnknown)))
     628  if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
    545629    {
    546630      /* This code is disabled on the 486 because all the Imm1 forms
     
    609693  int q;
    610694
    611   if (prefix >= 0x40 && prefix < 0x50 && flag_code == CODE_64BIT)
     695  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
     696      && flag_code == CODE_64BIT)
    612697    q = REX_PREFIX;
    613698  else
     
    647732      }
    648733
    649   if (i.prefix[q])
     734  if (i.prefix[q] != 0)
    650735    {
    651736      as_bad (_("same type of prefix used twice"));
     
    694779
    695780  SKIP_WHITESPACE ();
    696   if (! is_end_of_line[(unsigned char) *input_line_pointer])
     781  if (!is_end_of_line[(unsigned char) *input_line_pointer])
    697782    {
    698783      char *string = input_line_pointer;
     
    731816  SKIP_WHITESPACE ();
    732817
    733   if (! is_end_of_line[(unsigned char) *input_line_pointer])
     818  if (!is_end_of_line[(unsigned char) *input_line_pointer])
    734819    {
    735820      char *string = input_line_pointer;
     
    757842  no_cond_jump_promotion = 0;
    758843  if (*input_line_pointer == ','
    759       && ! is_end_of_line[(unsigned char) input_line_pointer[1]])
     844      && !is_end_of_line[(unsigned char) input_line_pointer[1]])
    760845    {
    761846      char *string = ++input_line_pointer;
     
    775860}
    776861
    777 const pseudo_typeS md_pseudo_table[] =
    778 {
    779 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
    780   {"align", s_align_bytes, 0},
    781 #else
    782   {"align", s_align_ptwo, 0},
    783 #endif
    784   {"arch", set_cpu_arch, 0},
    785 #ifndef I386COFF
    786   {"bss", s_bss, 0},
    787 #endif
    788   {"ffloat", float_cons, 'f'},
    789   {"dfloat", float_cons, 'd'},
    790   {"tfloat", float_cons, 'x'},
    791   {"value", cons, 2},
    792   {"noopt", s_ignore, 0},
    793   {"optim", s_ignore, 0},
    794   {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
    795   {"code16", set_code_flag, CODE_16BIT},
    796   {"code32", set_code_flag, CODE_32BIT},
    797   {"code64", set_code_flag, CODE_64BIT},
    798   {"intel_syntax", set_intel_syntax, 1},
    799   {"att_syntax", set_intel_syntax, 0},
    800   {"file", dwarf2_directive_file, 0},
    801   {"loc", dwarf2_directive_loc, 0},
    802   {0, 0, 0}
    803 };
    804 
    805 /* For interface with expression ().  */
    806 extern char *input_line_pointer;
    807 
    808 /* Hash table for instruction mnemonic lookup.  */
    809 static struct hash_control *op_hash;
    810 
    811 /* Hash table for register lookup.  */
    812 static struct hash_control *reg_hash;
    813 
    814 
    815862#ifdef BFD_ASSEMBLER
    816863unsigned long
     
    836883
    837884  {
    838     register const template *optab;
    839     register templates *core_optab;
     885    const template *optab;
     886    templates *core_optab;
    840887
    841888    /* Setup for loop.  */
     
    873920  reg_hash = hash_new ();
    874921  {
    875     register const reg_entry *regtab;
     922    const reg_entry *regtab;
    876923
    877924    for (regtab = i386_regtab;
     
    889936  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
    890937  {
    891     register int c;
    892     register char *p;
     938    int c;
     939    char *p;
    893940
    894941    for (c = 0; c < 256; c++)
    895942      {
    896         if (isdigit (c))
     943        if (ISDIGIT (c))
    897944          {
    898945            digit_chars[c] = c;
     
    901948            operand_chars[c] = c;
    902949          }
    903         else if (islower (c))
     950        else if (ISLOWER (c))
    904951          {
    905952            mnemonic_chars[c] = c;
     
    907954            operand_chars[c] = c;
    908955          }
    909         else if (isupper (c))
     956        else if (ISUPPER (c))
    910957          {
    911             mnemonic_chars[c] = tolower (c);
     958            mnemonic_chars[c] = TOLOWER (c);
    912959            register_chars[c] = mnemonic_chars[c];
    913960            operand_chars[c] = c;
    914961          }
    915962
    916         if (isalpha (c) || isdigit (c))
     963        if (ISALPHA (c) || ISDIGIT (c))
    917964          identifier_chars[c] = c;
    918965        else if (c >= 128)
     
    9801027           x->sib.base, x->sib.index, x->sib.scale);
    9811028  fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
    982            x->rex.mode64, x->rex.extX, x->rex.extY, x->rex.extZ);
     1029           (x->rex & REX_MODE64) != 0,
     1030           (x->rex & REX_EXTX) != 0,
     1031           (x->rex & REX_EXTY) != 0,
     1032           (x->rex & REX_EXTZ) != 0);
    9831033  for (i = 0; i < x->operands; i++)
    9841034    {
     
    10551105  }
    10561106
    1057 type_names[] =
     1107static const type_names[] =
    10581108{
    10591109  { Reg8, "r8" },
     
    10951145     unsigned int t;
    10961146{
    1097   register struct type_name *ty;
     1147  const struct type_name *ty;
    10981148
    10991149  for (ty = type_names; ty->mask; ty++)
     
    11061156
    11071157
    1108 int
    1109 tc_i386_force_relocation (fixp)
    1110      struct fix *fixp;
    1111 {
    11121158#ifdef BFD_ASSEMBLER
    1113   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
    1114       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
    1115     return 1;
    1116   return 0;
    1117 #else
    1118   /* For COFF.  */
    1119   return fixp->fx_r_type == 7;
    1120 #endif
    1121 }
    1122 
    1123 #ifdef BFD_ASSEMBLER
     1159static bfd_reloc_code_real_type reloc
     1160  PARAMS ((int, int, int, bfd_reloc_code_real_type));
    11241161
    11251162static bfd_reloc_code_real_type
     
    11751212int
    11761213tc_i386_fix_adjustable (fixP)
    1177      fixS *fixP;
     1214     fixS *fixP ATTRIBUTE_UNUSED;
    11781215{
    11791216#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
    1180   /* Prevent all adjustments to global symbols, or else dynamic
    1181      linking will not work correctly.  */
    1182   if (S_IS_EXTERNAL (fixP->fx_addsy)
    1183       || S_IS_WEAK (fixP->fx_addsy))
     1217  if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
     1218    return 1;
     1219
     1220  /* Don't adjust pc-relative references to merge sections in 64-bit
     1221     mode.  */
     1222  if (use_rela_relocations
     1223      && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
     1224      && fixP->fx_pcrel)
    11841225    return 0;
    1185 #endif
     1226
    11861227  /* adjust_reloc_syms doesn't know about the GOT.  */
    11871228  if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
    11881229      || fixP->fx_r_type == BFD_RELOC_386_PLT32
    11891230      || fixP->fx_r_type == BFD_RELOC_386_GOT32
     1231      || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
     1232      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
     1233      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
     1234      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
     1235      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
     1236      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
     1237      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
     1238      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
    11901239      || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
    11911240      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
    11921241      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
     1242      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
     1243      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
     1244      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
     1245      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
     1246      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
    11931247      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
    11941248      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
    11951249    return 0;
     1250#endif
    11961251  return 1;
    11971252}
    11981253#else
    11991254#define reloc(SIZE,PCREL,SIGN,OTHER)    0
     1255#define BFD_RELOC_8                     0
    12001256#define BFD_RELOC_16                    0
    12011257#define BFD_RELOC_32                    0
     1258#define BFD_RELOC_8_PCREL               0
    12021259#define BFD_RELOC_16_PCREL              0
    12031260#define BFD_RELOC_32_PCREL              0
     
    12051262#define BFD_RELOC_386_GOT32             0
    12061263#define BFD_RELOC_386_GOTOFF            0
     1264#define BFD_RELOC_386_TLS_GD            0
     1265#define BFD_RELOC_386_TLS_LDM           0
     1266#define BFD_RELOC_386_TLS_LDO_32        0
     1267#define BFD_RELOC_386_TLS_IE_32         0
     1268#define BFD_RELOC_386_TLS_IE            0
     1269#define BFD_RELOC_386_TLS_GOTIE         0
     1270#define BFD_RELOC_386_TLS_LE_32         0
     1271#define BFD_RELOC_386_TLS_LE            0
    12071272#define BFD_RELOC_X86_64_PLT32          0
    12081273#define BFD_RELOC_X86_64_GOT32          0
    12091274#define BFD_RELOC_X86_64_GOTPCREL       0
    1210 #endif
    1211 
    1212 static int intel_float_operand PARAMS ((char *mnemonic));
     1275#define BFD_RELOC_X86_64_TLSGD          0
     1276#define BFD_RELOC_X86_64_TLSLD          0
     1277#define BFD_RELOC_X86_64_DTPOFF32       0
     1278#define BFD_RELOC_X86_64_GOTTPOFF       0
     1279#define BFD_RELOC_X86_64_TPOFF32        0
     1280#endif
     1281
     1282static int intel_float_operand PARAMS ((const char *mnemonic));
    12131283
    12141284static int
    12151285intel_float_operand (mnemonic)
    1216      char *mnemonic;
     1286     const char *mnemonic;
    12171287{
    12181288  if (mnemonic[0] == 'f' && mnemonic[1] == 'i')
     
    12331303     char *line;
    12341304{
    1235   /* Points to template once we've found it.  */
    1236   const template *t;
    1237 
    12381305  int j;
    1239 
    12401306  char mnemonic[MAX_MNEM_SIZE];
    12411307
     
    12511317     We assume that the scrubber has arranged it so that line[0] is the valid
    12521318     start of a (possibly prefixed) mnemonic.  */
    1253   {
    1254     char *l = line;
    1255     char *token_start = l;
    1256     char *mnem_p;
    1257 
    1258     /* Non-zero if we found a prefix only acceptable with string insns.  */
    1259     const char *expecting_string_instruction = NULL;
    1260 
    1261     while (1)
    1262       {
    1263         mnem_p = mnemonic;
    1264         while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
    1265           {
    1266             mnem_p++;
    1267             if (mnem_p >= mnemonic + sizeof (mnemonic))
    1268               {
    1269                 as_bad (_("no such instruction: `%s'"), token_start);
    1270                 return;
    1271               }
    1272             l++;
    1273           }
    1274         if (!is_space_char (*l)
    1275             && *l != END_OF_INSN
    1276             && *l != PREFIX_SEPARATOR)
    1277           {
    1278             as_bad (_("invalid character %s in mnemonic"),
    1279                     output_invalid (*l));
    1280             return;
    1281           }
    1282         if (token_start == l)
    1283           {
    1284             if (*l == PREFIX_SEPARATOR)
    1285               as_bad (_("expecting prefix; got nothing"));
    1286             else
    1287               as_bad (_("expecting mnemonic; got nothing"));
    1288             return;
    1289           }
    1290 
    1291         /* Look up instruction (or prefix) via hash table.  */
    1292         current_templates = hash_find (op_hash, mnemonic);
    1293 
    1294         if (*l != END_OF_INSN
    1295             && (! is_space_char (*l) || l[1] != END_OF_INSN)
    1296             && current_templates
    1297             && (current_templates->start->opcode_modifier & IsPrefix))
    1298           {
    1299             /* If we are in 16-bit mode, do not allow addr16 or data16.
    1300                Similarly, in 32-bit mode, do not allow addr32 or data32.  */
    1301             if ((current_templates->start->opcode_modifier & (Size16 | Size32))
    1302                 && (((current_templates->start->opcode_modifier & Size32) != 0)
    1303                     ^ (flag_code == CODE_16BIT)))
    1304               {
    1305                 as_bad (_("redundant %s prefix"),
    1306                         current_templates->start->name);
    1307                 return;
    1308               }
    1309             /* Add prefix, checking for repeated prefixes.  */
    1310             switch (add_prefix (current_templates->start->base_opcode))
    1311               {
    1312               case 0:
    1313                 return;
    1314               case 2:
    1315                 expecting_string_instruction = current_templates->start->name;
    1316                 break;
    1317               }
    1318             /* Skip past PREFIX_SEPARATOR and reset token_start.  */
    1319             token_start = ++l;
    1320           }
    1321         else
    1322           break;
    1323       }
    1324 
    1325     if (!current_templates)
    1326       {
    1327         /* See if we can get a match by trimming off a suffix.  */
    1328         switch (mnem_p[-1])
    1329           {
    1330           case WORD_MNEM_SUFFIX:
    1331           case BYTE_MNEM_SUFFIX:
    1332           case QWORD_MNEM_SUFFIX:
    1333             i.suffix = mnem_p[-1];
    1334             mnem_p[-1] = '\0';
    1335             current_templates = hash_find (op_hash, mnemonic);
    1336             break;
    1337           case SHORT_MNEM_SUFFIX:
    1338           case LONG_MNEM_SUFFIX:
    1339             if (!intel_syntax)
    1340               {
    1341                 i.suffix = mnem_p[-1];
    1342                 mnem_p[-1] = '\0';
    1343                 current_templates = hash_find (op_hash, mnemonic);
    1344               }
    1345             break;
    1346 
    1347           /* Intel Syntax.  */
    1348           case 'd':
    1349             if (intel_syntax)
    1350               {
    1351                 if (intel_float_operand (mnemonic))
    1352                   i.suffix = SHORT_MNEM_SUFFIX;
    1353                 else
    1354                   i.suffix = LONG_MNEM_SUFFIX;
    1355                 mnem_p[-1] = '\0';
    1356                 current_templates = hash_find (op_hash, mnemonic);
    1357               }
    1358             break;
    1359           }
    1360         if (!current_templates)
    1361           {
    1362             as_bad (_("no such instruction: `%s'"), token_start);
    1363             return;
    1364           }
    1365       }
    1366 
    1367     /* Check if instruction is supported on specified architecture.  */
    1368     if (cpu_arch_flags != 0)
    1369       {
    1370         if ((current_templates->start->cpu_flags & ~(Cpu64 | CpuNo64))
    1371             & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64)))
    1372           {
    1373             as_warn (_("`%s' is not supported on `%s'"),
    1374                      current_templates->start->name, cpu_arch_name);
    1375           }
    1376         else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
    1377           {
    1378             as_warn (_("use .code16 to ensure correct addressing mode"));
    1379           }
    1380       }
    1381 
    1382     /* Check for rep/repne without a string instruction.  */
    1383     if (expecting_string_instruction
    1384         && !(current_templates->start->opcode_modifier & IsString))
    1385       {
    1386         as_bad (_("expecting string instruction after `%s'"),
    1387                 expecting_string_instruction);
    1388         return;
    1389       }
    1390 
    1391     /* There may be operands to parse.  */
    1392     if (*l != END_OF_INSN)
    1393       {
    1394         /* 1 if operand is pending after ','.  */
    1395         unsigned int expecting_operand = 0;
    1396 
    1397         /* Non-zero if operand parens not balanced.  */
    1398         unsigned int paren_not_balanced;
    1399 
    1400         do
    1401           {
    1402             /* Skip optional white space before operand.  */
    1403             if (is_space_char (*l))
    1404               ++l;
    1405             if (!is_operand_char (*l) && *l != END_OF_INSN)
    1406               {
    1407                 as_bad (_("invalid character %s before operand %d"),
    1408                         output_invalid (*l),
    1409                         i.operands + 1);
    1410                 return;
    1411               }
    1412             token_start = l;    /* after white space */
    1413             paren_not_balanced = 0;
    1414             while (paren_not_balanced || *l != ',')
    1415               {
    1416                 if (*l == END_OF_INSN)
    1417                   {
    1418                     if (paren_not_balanced)
    1419                       {
    1420                         if (!intel_syntax)
    1421                           as_bad (_("unbalanced parenthesis in operand %d."),
    1422                                   i.operands + 1);
    1423                         else
    1424                           as_bad (_("unbalanced brackets in operand %d."),
    1425                                   i.operands + 1);
    1426                         return;
    1427                       }
    1428                     else
    1429                       break;    /* we are done */
    1430                   }
    1431                 else if (!is_operand_char (*l) && !is_space_char (*l))
    1432                   {
    1433                     as_bad (_("invalid character %s in operand %d"),
    1434                             output_invalid (*l),
    1435                             i.operands + 1);
    1436                     return;
    1437                   }
    1438                 if (!intel_syntax)
    1439                   {
    1440                     if (*l == '(')
    1441                       ++paren_not_balanced;
    1442                     if (*l == ')')
    1443                       --paren_not_balanced;
    1444                   }
    1445                 else
    1446                   {
    1447                     if (*l == '[')
    1448                       ++paren_not_balanced;
    1449                     if (*l == ']')
    1450                       --paren_not_balanced;
    1451                   }
    1452                 l++;
    1453               }
    1454             if (l != token_start)
    1455               {                 /* Yes, we've read in another operand.  */
    1456                 unsigned int operand_ok;
    1457                 this_operand = i.operands++;
    1458                 if (i.operands > MAX_OPERANDS)
    1459                   {
    1460                     as_bad (_("spurious operands; (%d operands/instruction max)"),
    1461                             MAX_OPERANDS);
    1462                     return;
    1463                   }
    1464                 /* Now parse operand adding info to 'i' as we go along.  */
    1465                 END_STRING_AND_SAVE (l);
    1466 
    1467                 if (intel_syntax)
    1468                   operand_ok =
    1469                     i386_intel_operand (token_start,
    1470                                         intel_float_operand (mnemonic));
    1471                 else
    1472                   operand_ok = i386_operand (token_start);
    1473 
    1474                 RESTORE_END_STRING (l);
    1475                 if (!operand_ok)
    1476                   return;
    1477               }
    1478             else
    1479               {
    1480                 if (expecting_operand)
    1481                   {
    1482                   expecting_operand_after_comma:
    1483                     as_bad (_("expecting operand after ','; got nothing"));
    1484                     return;
    1485                   }
    1486                 if (*l == ',')
    1487                   {
    1488                     as_bad (_("expecting operand before ','; got nothing"));
    1489                     return;
    1490                   }
    1491               }
    1492 
    1493             /* Now *l must be either ',' or END_OF_INSN.  */
    1494             if (*l == ',')
    1495               {
    1496                 if (*++l == END_OF_INSN)
    1497                   {
    1498                     /* Just skip it, if it's \n complain.  */
    1499                     goto expecting_operand_after_comma;
    1500                   }
    1501                 expecting_operand = 1;
    1502               }
    1503           }
    1504         while (*l != END_OF_INSN);
    1505       }
    1506   }
     1319
     1320  line = parse_insn (line, mnemonic);
     1321  if (line == NULL)
     1322    return;
     1323
     1324  line = parse_operands (line, mnemonic);
     1325  if (line == NULL)
     1326    return;
    15071327
    15081328  /* Now we've parsed the mnemonic into a set of templates, and have the
    1509      operands at hand.
    1510 
    1511      Next, we find a template that matches the given insn,
     1329     operands at hand.  */
     1330
     1331  /* All intel opcodes have reversed operands except for "bound" and
     1332     "enter".  We also don't reverse intersegment "jmp" and "call"
     1333     instructions with 2 immediate operands so that the immediate segment
     1334     precedes the offset, as it does when in AT&T mode.  "enter" and the
     1335     intersegment "jmp" and "call" instructions are the only ones that
     1336     have two immediate operands.  */
     1337  if (intel_syntax && i.operands > 1
     1338      && (strcmp (mnemonic, "bound") != 0)
     1339      && !((i.types[0] & Imm) && (i.types[1] & Imm)))
     1340    swap_operands ();
     1341
     1342  if (i.imm_operands)
     1343    optimize_imm ();
     1344
     1345  if (i.disp_operands)
     1346    optimize_disp ();
     1347
     1348  /* Next, we find a template that matches the given insn,
    15121349     making sure the overlap of the given operands types is consistent
    15131350     with the template operand types.  */
    15141351
    1515 #define MATCH(overlap, given, template) \
    1516   ((overlap & ~JumpAbsolute) \
    1517    && ((given) & (BaseIndex|JumpAbsolute)) == ((overlap) & (BaseIndex|JumpAbsolute)))
    1518 
    1519   /* If given types r0 and r1 are registers they must be of the same type
    1520      unless the expected operand type register overlap is null.
    1521      Note that Acc in a template matches every size of reg.  */
    1522 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
    1523   ( ((g0) & Reg) == 0 || ((g1) & Reg) == 0 || \
    1524     ((g0) & Reg) == ((g1) & Reg) || \
    1525     ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
    1526 
    1527   {
    1528     register unsigned int overlap0, overlap1;
    1529     unsigned int overlap2;
    1530     unsigned int found_reverse_match;
    1531     int suffix_check;
    1532 
    1533     /* All intel opcodes have reversed operands except for "bound" and
    1534        "enter".  We also don't reverse intersegment "jmp" and "call"
    1535        instructions with 2 immediate operands so that the immediate segment
    1536        precedes the offset, as it does when in AT&T mode.  "enter" and the
    1537        intersegment "jmp" and "call" instructions are the only ones that
    1538        have two immediate operands.  */
    1539     if (intel_syntax && i.operands > 1
    1540         && (strcmp (mnemonic, "bound") != 0)
    1541         && !((i.types[0] & Imm) && (i.types[1] & Imm)))
    1542       {
    1543         union i386_op temp_op;
    1544         unsigned int temp_type;
    1545         RELOC_ENUM temp_reloc;
    1546         int xchg1 = 0;
    1547         int xchg2 = 0;
    1548 
    1549         if (i.operands == 2)
    1550           {
    1551             xchg1 = 0;
    1552             xchg2 = 1;
    1553           }
    1554         else if (i.operands == 3)
    1555           {
    1556             xchg1 = 0;
    1557             xchg2 = 2;
    1558           }
    1559         temp_type = i.types[xchg2];
    1560         i.types[xchg2] = i.types[xchg1];
    1561         i.types[xchg1] = temp_type;
    1562         temp_op = i.op[xchg2];
    1563         i.op[xchg2] = i.op[xchg1];
    1564         i.op[xchg1] = temp_op;
    1565         temp_reloc = i.reloc[xchg2];
    1566         i.reloc[xchg2] = i.reloc[xchg1];
    1567         i.reloc[xchg1] = temp_reloc;
    1568 
    1569         if (i.mem_operands == 2)
    1570           {
    1571             const seg_entry *temp_seg;
    1572             temp_seg = i.seg[0];
    1573             i.seg[0] = i.seg[1];
    1574             i.seg[1] = temp_seg;
    1575           }
    1576       }
    1577 
    1578     if (i.imm_operands)
    1579       {
    1580         /* Try to ensure constant immediates are represented in the smallest
    1581            opcode possible.  */
    1582         char guess_suffix = 0;
    1583         int op;
    1584 
    1585         if (i.suffix)
    1586           guess_suffix = i.suffix;
    1587         else if (i.reg_operands)
    1588           {
    1589             /* Figure out a suffix from the last register operand specified.
    1590                We can't do this properly yet, ie. excluding InOutPortReg,
    1591                but the following works for instructions with immediates.
    1592                In any case, we can't set i.suffix yet.  */
    1593             for (op = i.operands; --op >= 0;)
    1594               if (i.types[op] & Reg)
    1595                 {
    1596                   if (i.types[op] & Reg8)
    1597                     guess_suffix = BYTE_MNEM_SUFFIX;
    1598                   else if (i.types[op] & Reg16)
    1599                     guess_suffix = WORD_MNEM_SUFFIX;
    1600                   else if (i.types[op] & Reg32)
    1601                     guess_suffix = LONG_MNEM_SUFFIX;
    1602                   else if (i.types[op] & Reg64)
    1603                     guess_suffix = QWORD_MNEM_SUFFIX;
    1604                   break;
    1605                 }
    1606           }
    1607         else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
    1608           guess_suffix = WORD_MNEM_SUFFIX;
    1609 
    1610         for (op = i.operands; --op >= 0;)
    1611           if (i.types[op] & Imm)
    1612             {
    1613               switch (i.op[op].imms->X_op)
    1614                 {
    1615                   case O_constant:
    1616                     /* If a suffix is given, this operand may be shortened.  */
    1617                     switch (guess_suffix)
    1618                       {
    1619                       case LONG_MNEM_SUFFIX:
    1620                         i.types[op] |= Imm32 | Imm64;
    1621                         break;
    1622                       case WORD_MNEM_SUFFIX:
    1623                         i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
    1624                         break;
    1625                       case BYTE_MNEM_SUFFIX:
    1626                         i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
    1627                         break;
    1628                       }
    1629 
    1630                     /* If this operand is at most 16 bits, convert it
    1631                        to a signed 16 bit number before trying to see
    1632                        whether it will fit in an even smaller size.
    1633                        This allows a 16-bit operand such as $0xffe0 to
    1634                        be recognised as within Imm8S range.  */
    1635                     if ((i.types[op] & Imm16)
    1636                         && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
    1637                       {
    1638                         i.op[op].imms->X_add_number =
    1639                           (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
    1640                       }
    1641                     if ((i.types[op] & Imm32)
    1642                         && (i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1)) == 0)
    1643                       {
    1644                         i.op[op].imms->X_add_number =
    1645                           (i.op[op].imms->X_add_number ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
    1646                       }
    1647                     i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
    1648                     /* We must avoid matching of Imm32 templates when 64bit only immediate is available.  */
    1649                     if (guess_suffix == QWORD_MNEM_SUFFIX)
    1650                       i.types[op] &= ~Imm32;
    1651                     break;
    1652                   case O_absent:
    1653                   case O_register:
    1654                     abort ();
    1655                   /* Symbols and expressions.  */
    1656                   default:
    1657                     /* Convert symbolic operand to proper sizes for matching.  */
    1658                     switch (guess_suffix)
    1659                       {
    1660                         case QWORD_MNEM_SUFFIX:
    1661                           i.types[op] = Imm64 | Imm32S;
    1662                           break;
    1663                         case LONG_MNEM_SUFFIX:
    1664                           i.types[op] = Imm32 | Imm64;
    1665                           break;
    1666                         case WORD_MNEM_SUFFIX:
    1667                           i.types[op] = Imm16 | Imm32 | Imm64;
    1668                           break;
    1669                           break;
    1670                         case BYTE_MNEM_SUFFIX:
    1671                           i.types[op] = Imm8 | Imm8S | Imm16 | Imm32S | Imm32;
    1672                                 break;
    1673                           break;
    1674                       }
    1675                     break;
    1676                 }
    1677             }
    1678       }
    1679 
    1680     if (i.disp_operands)
    1681       {
    1682         /* Try to use the smallest displacement type too.  */
    1683         int op;
    1684 
    1685         for (op = i.operands; --op >= 0;)
    1686           if ((i.types[op] & Disp)
    1687               && i.op[op].disps->X_op == O_constant)
    1688             {
    1689               offsetT disp = i.op[op].disps->X_add_number;
    1690 
    1691               if (i.types[op] & Disp16)
    1692                 {
    1693                   /* We know this operand is at most 16 bits, so
    1694                      convert to a signed 16 bit number before trying
    1695                      to see whether it will fit in an even smaller
    1696                      size.  */
    1697 
    1698                   disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
    1699                 }
    1700               else if (i.types[op] & Disp32)
    1701                 {
    1702                   /* We know this operand is at most 32 bits, so convert to a
    1703                      signed 32 bit number before trying to see whether it will
    1704                      fit in an even smaller size.  */
    1705                   disp &= (((offsetT) 2 << 31) - 1);
    1706                   disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
    1707                 }
    1708               if (flag_code == CODE_64BIT)
    1709                 {
    1710                   if (fits_in_signed_long (disp))
    1711                     i.types[op] |= Disp32S;
    1712                   if (fits_in_unsigned_long (disp))
    1713                     i.types[op] |= Disp32;
    1714                 }
    1715               if ((i.types[op] & (Disp32 | Disp32S | Disp16))
    1716                   && fits_in_signed_byte (disp))
    1717                 i.types[op] |= Disp8;
    1718             }
    1719       }
    1720 
    1721     overlap0 = 0;
    1722     overlap1 = 0;
    1723     overlap2 = 0;
    1724     found_reverse_match = 0;
    1725     suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
    1726                     ? No_bSuf
    1727                     : (i.suffix == WORD_MNEM_SUFFIX
    1728                        ? No_wSuf
    1729                        : (i.suffix == SHORT_MNEM_SUFFIX
    1730                           ? No_sSuf
    1731                           : (i.suffix == LONG_MNEM_SUFFIX
    1732                              ? No_lSuf
    1733                              : (i.suffix == QWORD_MNEM_SUFFIX
    1734                                 ? No_qSuf
    1735                                 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX ? No_xSuf : 0))))));
    1736 
    1737     for (t = current_templates->start;
    1738          t < current_templates->end;
    1739          t++)
    1740       {
    1741         /* Must have right number of operands.  */
    1742         if (i.operands != t->operands)
    1743           continue;
    1744 
    1745         /* Check the suffix, except for some instructions in intel mode.  */
    1746         if ((t->opcode_modifier & suffix_check)
    1747             && !(intel_syntax
    1748                  && (t->opcode_modifier & IgnoreSize))
    1749             && !(intel_syntax
    1750                  && t->base_opcode == 0xd9
    1751                  && (t->extension_opcode == 5        /* 0xd9,5 "fldcw"  */
    1752                      || t->extension_opcode == 7)))  /* 0xd9,7 "f{n}stcw"  */
    1753           continue;
    1754 
    1755         /* Do not verify operands when there are none.  */
    1756         else if (!t->operands)
    1757           {
    1758             if (t->cpu_flags & ~cpu_arch_flags)
    1759               continue;
    1760             /* We've found a match; break out of loop.  */
    1761             break;
    1762           }
    1763 
    1764         overlap0 = i.types[0] & t->operand_types[0];
    1765         switch (t->operands)
    1766           {
    1767           case 1:
    1768             if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
    1769               continue;
    1770             break;
    1771           case 2:
    1772           case 3:
    1773             overlap1 = i.types[1] & t->operand_types[1];
    1774             if (!MATCH (overlap0, i.types[0], t->operand_types[0])
    1775                 || !MATCH (overlap1, i.types[1], t->operand_types[1])
    1776                 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
    1777                                                t->operand_types[0],
    1778                                                overlap1, i.types[1],
    1779                                                t->operand_types[1]))
    1780               {
    1781                 /* Check if other direction is valid ...  */
    1782                 if ((t->opcode_modifier & (D|FloatD)) == 0)
    1783                   continue;
    1784 
    1785                 /* Try reversing direction of operands.  */
    1786                 overlap0 = i.types[0] & t->operand_types[1];
    1787                 overlap1 = i.types[1] & t->operand_types[0];
    1788                 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
    1789                     || !MATCH (overlap1, i.types[1], t->operand_types[0])
    1790                     || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
    1791                                                    t->operand_types[1],
    1792                                                    overlap1, i.types[1],
    1793                                                    t->operand_types[0]))
    1794                   {
    1795                     /* Does not match either direction.  */
    1796                     continue;
    1797                   }
    1798                 /* found_reverse_match holds which of D or FloatDR
    1799                    we've found.  */
    1800                 found_reverse_match = t->opcode_modifier & (D|FloatDR);
    1801               }
    1802             /* Found a forward 2 operand match here.  */
    1803             else if (t->operands == 3)
    1804               {
    1805                 /* Here we make use of the fact that there are no
    1806                    reverse match 3 operand instructions, and all 3
    1807                    operand instructions only need to be checked for
    1808                    register consistency between operands 2 and 3.  */
    1809                 overlap2 = i.types[2] & t->operand_types[2];
    1810                 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
    1811                     || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
    1812                                                    t->operand_types[1],
    1813                                                    overlap2, i.types[2],
    1814                                                    t->operand_types[2]))
    1815 
    1816                   continue;
    1817               }
    1818             /* Found either forward/reverse 2 or 3 operand match here:
    1819                slip through to break.  */
    1820           }
    1821         if (t->cpu_flags & ~cpu_arch_flags)
    1822           {
    1823             found_reverse_match = 0;
    1824             continue;
    1825           }
    1826         /* We've found a match; break out of loop.  */
    1827         break;
    1828       }
    1829     if (t == current_templates->end)
    1830       {
    1831         /* We found no match.  */
    1832         as_bad (_("suffix or operands invalid for `%s'"),
    1833                 current_templates->start->name);
     1352  if (!match_template ())
     1353    return;
     1354
     1355  if (intel_syntax)
     1356    {
     1357      /* Undo SYSV386_COMPAT brokenness when in Intel mode.  See i386.h  */
     1358      if (SYSV386_COMPAT
     1359          && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
     1360        i.tm.base_opcode ^= FloatR;
     1361
     1362      /* Zap movzx and movsx suffix.  The suffix may have been set from
     1363         "word ptr" or "byte ptr" on the source operand, but we'll use
     1364         the suffix later to choose the destination register.  */
     1365      if ((i.tm.base_opcode & ~9) == 0x0fb6)
     1366        i.suffix = 0;
     1367    }
     1368
     1369  if (i.tm.opcode_modifier & FWait)
     1370    if (!add_prefix (FWAIT_OPCODE))
     1371      return;
     1372
     1373  /* Check string instruction segment overrides.  */
     1374  if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
     1375    {
     1376      if (!check_string ())
    18341377        return;
    1835       }
    1836 
    1837     if (!quiet_warnings)
    1838       {
    1839         if (!intel_syntax
    1840             && ((i.types[0] & JumpAbsolute)
    1841                 != (t->operand_types[0] & JumpAbsolute)))
    1842           {
    1843             as_warn (_("indirect %s without `*'"), t->name);
    1844           }
    1845 
    1846         if ((t->opcode_modifier & (IsPrefix|IgnoreSize))
    1847             == (IsPrefix|IgnoreSize))
    1848           {
    1849             /* Warn them that a data or address size prefix doesn't
    1850                affect assembly of the next line of code.  */
    1851             as_warn (_("stand-alone `%s' prefix"), t->name);
    1852           }
    1853       }
    1854 
    1855     /* Copy the template we found.  */
    1856     i.tm = *t;
    1857     if (found_reverse_match)
    1858       {
    1859         /* If we found a reverse match we must alter the opcode
    1860            direction bit.  found_reverse_match holds bits to change
    1861            (different for int & float insns).  */
    1862 
    1863         i.tm.base_opcode ^= found_reverse_match;
    1864 
    1865         i.tm.operand_types[0] = t->operand_types[1];
    1866         i.tm.operand_types[1] = t->operand_types[0];
    1867       }
    1868 
    1869     /* Undo SYSV386_COMPAT brokenness when in Intel mode.  See i386.h  */
    1870     if (SYSV386_COMPAT
    1871         && intel_syntax
    1872         && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
    1873       i.tm.base_opcode ^= FloatR;
    1874 
    1875     if (i.tm.opcode_modifier & FWait)
    1876       if (! add_prefix (FWAIT_OPCODE))
     1378    }
     1379
     1380  if (!process_suffix ())
     1381    return;
     1382
     1383  /* Make still unresolved immediate matches conform to size of immediate
     1384     given in i.suffix.  */
     1385  if (!finalize_imm ())
     1386    return;
     1387
     1388  if (i.types[0] & Imm1)
     1389    i.imm_operands = 0; /* kludge for shift insns.  */
     1390  if (i.types[0] & ImplicitRegister)
     1391    i.reg_operands--;
     1392  if (i.types[1] & ImplicitRegister)
     1393    i.reg_operands--;
     1394  if (i.types[2] & ImplicitRegister)
     1395    i.reg_operands--;
     1396
     1397  if (i.tm.opcode_modifier & ImmExt)
     1398    {
     1399      /* These AMD 3DNow! and Intel Katmai New Instructions have an
     1400         opcode suffix which is coded in the same place as an 8-bit
     1401         immediate field would be.  Here we fake an 8-bit immediate
     1402         operand from the opcode suffix stored in tm.extension_opcode.  */
     1403
     1404      expressionS *exp;
     1405
     1406      assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
     1407
     1408      exp = &im_expressions[i.imm_operands++];
     1409      i.op[i.operands].imms = exp;
     1410      i.types[i.operands++] = Imm8;
     1411      exp->X_op = O_constant;
     1412      exp->X_add_number = i.tm.extension_opcode;
     1413      i.tm.extension_opcode = None;
     1414    }
     1415
     1416  /* For insns with operands there are more diddles to do to the opcode.  */
     1417  if (i.operands)
     1418    {
     1419      if (!process_operands ())
    18771420        return;
    1878 
    1879     /* Check string instruction segment overrides.  */
    1880     if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
    1881       {
    1882         int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
    1883         if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
    1884           {
    1885             if (i.seg[0] != NULL && i.seg[0] != &es)
    1886               {
    1887                 as_bad (_("`%s' operand %d must use `%%es' segment"),
    1888                         i.tm.name,
    1889                         mem_op + 1);
    1890                 return;
    1891               }
    1892             /* There's only ever one segment override allowed per instruction.
    1893                This instruction possibly has a legal segment override on the
    1894                second operand, so copy the segment to where non-string
    1895                instructions store it, allowing common code.  */
    1896             i.seg[0] = i.seg[1];
    1897           }
    1898         else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
    1899           {
    1900             if (i.seg[1] != NULL && i.seg[1] != &es)
    1901               {
    1902                 as_bad (_("`%s' operand %d must use `%%es' segment"),
    1903                         i.tm.name,
    1904                         mem_op + 2);
    1905                 return;
    1906               }
    1907           }
    1908       }
    1909 
    1910     if (i.reg_operands && flag_code < CODE_64BIT)
    1911       {
    1912         int op;
    1913         for (op = i.operands; --op >= 0;)
    1914           if ((i.types[op] & Reg)
    1915               && (i.op[op].regs->reg_flags & (RegRex64|RegRex)))
    1916             {
    1917               as_bad (_("Extended register `%%%s' available only in 64bit mode."),
    1918                       i.op[op].regs->reg_name);
    1919               return;
    1920             }
    1921       }
    1922 
    1923     /* If matched instruction specifies an explicit instruction mnemonic
    1924        suffix, use it.  */
    1925     if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
    1926       {
    1927         if (i.tm.opcode_modifier & Size16)
    1928           i.suffix = WORD_MNEM_SUFFIX;
    1929         else if (i.tm.opcode_modifier & Size64)
    1930           i.suffix = QWORD_MNEM_SUFFIX;
    1931         else
    1932           i.suffix = LONG_MNEM_SUFFIX;
    1933       }
    1934     else if (i.reg_operands)
    1935       {
    1936         /* If there's no instruction mnemonic suffix we try to invent one
    1937            based on register operands.  */
    1938         if (!i.suffix)
    1939           {
    1940             /* We take i.suffix from the last register operand specified,
    1941                Destination register type is more significant than source
    1942                register type.  */
    1943             int op;
    1944             for (op = i.operands; --op >= 0;)
    1945               if ((i.types[op] & Reg)
    1946                   && !(i.tm.operand_types[op] & InOutPortReg))
    1947                 {
    1948                   i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
    1949                               (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
    1950                               (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
    1951                               LONG_MNEM_SUFFIX);
    1952                   break;
    1953                 }
    1954           }
    1955         else if (i.suffix == BYTE_MNEM_SUFFIX)
    1956           {
    1957             int op;
    1958             for (op = i.operands; --op >= 0;)
    1959               {
    1960                 /* If this is an eight bit register, it's OK.  If it's
    1961                    the 16 or 32 bit version of an eight bit register,
    1962                    we will just use the low portion, and that's OK too.  */
    1963                 if (i.types[op] & Reg8)
    1964                   continue;
    1965 
    1966                 /* movzx and movsx should not generate this warning.  */
    1967                 if (intel_syntax
    1968                     && (i.tm.base_opcode == 0xfb7
    1969                         || i.tm.base_opcode == 0xfb6
    1970                         || i.tm.base_opcode == 0x63
    1971                         || i.tm.base_opcode == 0xfbe
    1972                         || i.tm.base_opcode == 0xfbf))
    1973                   continue;
    1974 
    1975                 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4
    1976 #if 0
    1977                     /* Check that the template allows eight bit regs
    1978                        This kills insns such as `orb $1,%edx', which
    1979                        maybe should be allowed.  */
    1980                     && (i.tm.operand_types[op] & (Reg8|InOutPortReg))
    1981 #endif
    1982                     )
    1983                   {
    1984                     /* Prohibit these changes in the 64bit mode, since
    1985                        the lowering is more complicated.  */
    1986                     if (flag_code == CODE_64BIT
    1987                         && (i.tm.operand_types[op] & InOutPortReg) == 0)
    1988                       as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
    1989                               i.op[op].regs->reg_name,
    1990                               i.suffix);
    1991 #if REGISTER_WARNINGS
    1992                     if (!quiet_warnings
    1993                         && (i.tm.operand_types[op] & InOutPortReg) == 0)
    1994                       as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
    1995                                (i.op[op].regs
    1996                                 + (i.types[op] & Reg16
    1997                                    ? REGNAM_AL - REGNAM_AX
    1998                                    : REGNAM_AL - REGNAM_EAX))->reg_name,
    1999                                i.op[op].regs->reg_name,
    2000                                i.suffix);
    2001 #endif
    2002                     continue;
    2003                   }
    2004                 /* Any other register is bad.  */
    2005                 if (i.types[op] & (Reg | RegMMX | RegXMM
    2006                                    | SReg2 | SReg3
    2007                                    | Control | Debug | Test
    2008                                    | FloatReg | FloatAcc))
    2009                   {
    2010                     as_bad (_("`%%%s' not allowed with `%s%c'"),
    2011                             i.op[op].regs->reg_name,
    2012                             i.tm.name,
    2013                             i.suffix);
    2014                     return;
    2015                   }
    2016               }
    2017           }
    2018         else if (i.suffix == LONG_MNEM_SUFFIX)
    2019           {
    2020             int op;
    2021 
    2022             for (op = i.operands; --op >= 0;)
    2023               /* Reject eight bit registers, except where the template
    2024                  requires them. (eg. movzb)  */
    2025               if ((i.types[op] & Reg8) != 0
    2026                   && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
    2027                 {
    2028                   as_bad (_("`%%%s' not allowed with `%s%c'"),
    2029                           i.op[op].regs->reg_name,
    2030                           i.tm.name,
    2031                           i.suffix);
    2032                   return;
    2033                 }
    2034               /* Warn if the e prefix on a general reg is missing.  */
    2035               else if ((!quiet_warnings || flag_code == CODE_64BIT)
    2036                        && (i.types[op] & Reg16) != 0
    2037                        && (i.tm.operand_types[op] & (Reg32|Acc)) != 0)
    2038                 {
    2039                   /* Prohibit these changes in the 64bit mode, since
    2040                      the lowering is more complicated.  */
    2041                   if (flag_code == CODE_64BIT)
    2042                     as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
    2043                             i.op[op].regs->reg_name,
    2044                             i.suffix);
    2045 #if REGISTER_WARNINGS
    2046                   else
    2047                     as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
    2048                              (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
    2049                              i.op[op].regs->reg_name,
    2050                              i.suffix);
    2051 #endif
    2052                 }
    2053               /* Warn if the r prefix on a general reg is missing.  */
    2054               else if ((i.types[op] & Reg64) != 0
    2055                        && (i.tm.operand_types[op] & (Reg32|Acc)) != 0)
    2056                 {
    2057                   as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
    2058                           i.op[op].regs->reg_name,
    2059                           i.suffix);
    2060                 }
    2061           }
    2062         else if (i.suffix == QWORD_MNEM_SUFFIX)
    2063           {
    2064             int op;
    2065 
    2066             for (op = i.operands; --op >= 0; )
    2067               /* Reject eight bit registers, except where the template
    2068                  requires them. (eg. movzb)  */
    2069               if ((i.types[op] & Reg8) != 0
    2070                   && (i.tm.operand_types[op] & (Reg16|Reg32|Acc)) != 0)
    2071                 {
    2072                   as_bad (_("`%%%s' not allowed with `%s%c'"),
    2073                           i.op[op].regs->reg_name,
    2074                           i.tm.name,
    2075                           i.suffix);
    2076                   return;
    2077                 }
    2078               /* Warn if the e prefix on a general reg is missing.  */
    2079               else if (((i.types[op] & Reg16) != 0
    2080                         || (i.types[op] & Reg32) != 0)
    2081                        && (i.tm.operand_types[op] & (Reg32|Acc)) != 0)
    2082                 {
    2083                   /* Prohibit these changes in the 64bit mode, since
    2084                      the lowering is more complicated.  */
    2085                   as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
    2086                           i.op[op].regs->reg_name,
    2087                           i.suffix);
    2088                 }
    2089           }
    2090         else if (i.suffix == WORD_MNEM_SUFFIX)
    2091           {
    2092             int op;
    2093             for (op = i.operands; --op >= 0;)
    2094               /* Reject eight bit registers, except where the template
    2095                  requires them. (eg. movzb)  */
    2096               if ((i.types[op] & Reg8) != 0
    2097                   && (i.tm.operand_types[op] & (Reg16|Reg32|Acc)) != 0)
    2098                 {
    2099                   as_bad (_("`%%%s' not allowed with `%s%c'"),
    2100                           i.op[op].regs->reg_name,
    2101                           i.tm.name,
    2102                           i.suffix);
    2103                   return;
    2104                 }
    2105               /* Warn if the e prefix on a general reg is present.  */
    2106               else if ((!quiet_warnings || flag_code == CODE_64BIT)
    2107                        && (i.types[op] & Reg32) != 0
    2108                        && (i.tm.operand_types[op] & (Reg16|Acc)) != 0)
    2109                 {
    2110                   /* Prohibit these changes in the 64bit mode, since
    2111                      the lowering is more complicated.  */
    2112                   if (flag_code == CODE_64BIT)
    2113                     as_bad (_("Incorrect register `%%%s' used with`%c' suffix"),
    2114                             i.op[op].regs->reg_name,
    2115                             i.suffix);
    2116                   else
    2117 #if REGISTER_WARNINGS
    2118                     as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
    2119                              (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
    2120                              i.op[op].regs->reg_name,
    2121                              i.suffix);
    2122 #endif
    2123                 }
    2124           }
    2125         else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
    2126           /* Do nothing if the instruction is going to ignore the prefix.  */
    2127           ;
    2128         else
    2129           abort ();
    2130       }
    2131     else if ((i.tm.opcode_modifier & DefaultSize) && !i.suffix)
    2132       {
    2133         i.suffix = stackop_size;
    2134       }
    2135     /* Make still unresolved immediate matches conform to size of immediate
    2136        given in i.suffix.  Note: overlap2 cannot be an immediate!  */
    2137     if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S))
    2138         && overlap0 != Imm8 && overlap0 != Imm8S
    2139         && overlap0 != Imm16 && overlap0 != Imm32S
    2140         && overlap0 != Imm32 && overlap0 != Imm64)
    2141       {
    2142         if (i.suffix)
    2143           {
    2144             overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
    2145                         (i.suffix == WORD_MNEM_SUFFIX ? Imm16 :
    2146                         (i.suffix == QWORD_MNEM_SUFFIX ? Imm64 | Imm32S : Imm32)));
    2147           }
    2148         else if (overlap0 == (Imm16 | Imm32S | Imm32)
    2149                  || overlap0 == (Imm16 | Imm32)
    2150                  || overlap0 == (Imm16 | Imm32S))
    2151           {
    2152             overlap0 =
    2153               ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32S;
    2154           }
    2155         if (overlap0 != Imm8 && overlap0 != Imm8S
    2156             && overlap0 != Imm16 && overlap0 != Imm32S
    2157             && overlap0 != Imm32 && overlap0 != Imm64)
    2158           {
    2159             as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
    2160             return;
    2161           }
    2162       }
    2163     if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32))
    2164         && overlap1 != Imm8 && overlap1 != Imm8S
    2165         && overlap1 != Imm16 && overlap1 != Imm32S
    2166         && overlap1 != Imm32 && overlap1 != Imm64)
    2167       {
    2168         if (i.suffix)
    2169           {
    2170             overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX ? (Imm8 | Imm8S) :
    2171                         (i.suffix == WORD_MNEM_SUFFIX ? Imm16 :
    2172                         (i.suffix == QWORD_MNEM_SUFFIX ? Imm64 | Imm32S : Imm32)));
    2173           }
    2174         else if (overlap1 == (Imm16 | Imm32 | Imm32S)
    2175                  || overlap1 == (Imm16 | Imm32)
    2176                  || overlap1 == (Imm16 | Imm32S))
    2177           {
    2178             overlap1 =
    2179               ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)) ? Imm16 : Imm32S;
    2180           }
    2181         if (overlap1 != Imm8 && overlap1 != Imm8S
    2182             && overlap1 != Imm16 && overlap1 != Imm32S
    2183             && overlap1 != Imm32 && overlap1 != Imm64)
    2184           {
    2185             as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
    2186             return;
    2187           }
    2188       }
    2189     assert ((overlap2 & Imm) == 0);
    2190 
    2191     i.types[0] = overlap0;
    2192     if (overlap0 & ImplicitRegister)
    2193       i.reg_operands--;
    2194     if (overlap0 & Imm1)
    2195       i.imm_operands = 0;       /* kludge for shift insns.  */
    2196 
    2197     i.types[1] = overlap1;
    2198     if (overlap1 & ImplicitRegister)
    2199       i.reg_operands--;
    2200 
    2201     i.types[2] = overlap2;
    2202     if (overlap2 & ImplicitRegister)
    2203       i.reg_operands--;
    2204 
    2205     /* Finalize opcode.  First, we change the opcode based on the operand
    2206        size given by i.suffix:  We need not change things for byte insns.  */
    2207 
    2208     if (!i.suffix && (i.tm.opcode_modifier & W))
    2209       {
    2210         as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
    2211         return;
    2212       }
    2213 
    2214     /* For movzx and movsx, need to check the register type.  */
    2215     if (intel_syntax
    2216         && (i.tm.base_opcode == 0xfb6 || i.tm.base_opcode == 0xfbe))
    2217       if (i.suffix && i.suffix == BYTE_MNEM_SUFFIX)
    2218         {
    2219           unsigned int prefix = DATA_PREFIX_OPCODE;
    2220 
    2221           if ((i.op[1].regs->reg_type & Reg16) != 0)
    2222             if (!add_prefix (prefix))
    2223               return;
    2224         }
    2225 
    2226     if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
    2227       {
    2228         /* It's not a byte, select word/dword operation.  */
    2229         if (i.tm.opcode_modifier & W)
    2230           {
    2231             if (i.tm.opcode_modifier & ShortForm)
    2232               i.tm.base_opcode |= 8;
    2233             else
    2234               i.tm.base_opcode |= 1;
    2235           }
    2236         /* Now select between word & dword operations via the operand
    2237            size prefix, except for instructions that will ignore this
    2238            prefix anyway.  */
    2239         if (i.suffix != QWORD_MNEM_SUFFIX
    2240             && (i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
    2241             && !(i.tm.opcode_modifier & IgnoreSize))
    2242           {
    2243             unsigned int prefix = DATA_PREFIX_OPCODE;
    2244             if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
    2245               prefix = ADDR_PREFIX_OPCODE;
    2246 
    2247             if (! add_prefix (prefix))
    2248               return;
    2249           }
    2250 
    2251         /* Set mode64 for an operand.  */
    2252         if (i.suffix == QWORD_MNEM_SUFFIX
    2253             && !(i.tm.opcode_modifier & NoRex64))
    2254           {
    2255             i.rex.mode64 = 1;
    2256             if (flag_code < CODE_64BIT)
    2257               {
    2258                 as_bad (_("64bit operations available only in 64bit modes."));
    2259                 return;
    2260               }
    2261           }
    2262 
    2263         /* Size floating point instruction.  */
    2264         if (i.suffix == LONG_MNEM_SUFFIX)
    2265           {
    2266             if (i.tm.opcode_modifier & FloatMF)
    2267               i.tm.base_opcode ^= 4;
    2268           }
    2269       }
    2270 
    2271     if (i.tm.opcode_modifier & ImmExt)
    2272       {
    2273         /* These AMD 3DNow! and Intel Katmai New Instructions have an
    2274            opcode suffix which is coded in the same place as an 8-bit
    2275            immediate field would be.  Here we fake an 8-bit immediate
    2276            operand from the opcode suffix stored in tm.extension_opcode.  */
    2277 
    2278         expressionS *exp;
    2279 
    2280         assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
    2281 
    2282         exp = &im_expressions[i.imm_operands++];
    2283         i.op[i.operands].imms = exp;
    2284         i.types[i.operands++] = Imm8;
    2285         exp->X_op = O_constant;
    2286         exp->X_add_number = i.tm.extension_opcode;
    2287         i.tm.extension_opcode = None;
    2288       }
    2289 
    2290     /* For insns with operands there are more diddles to do to the opcode.  */
    2291     if (i.operands)
    2292       {
    2293         /* Default segment register this instruction will use
    2294            for memory accesses.  0 means unknown.
    2295            This is only for optimizing out unnecessary segment overrides.  */
    2296         const seg_entry *default_seg = 0;
    2297 
    2298         /* The imul $imm, %reg instruction is converted into
    2299            imul $imm, %reg, %reg, and the clr %reg instruction
    2300            is converted into xor %reg, %reg.  */
    2301         if (i.tm.opcode_modifier & regKludge)
    2302           {
    2303             unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
    2304             /* Pretend we saw the extra register operand.  */
    2305             assert (i.op[first_reg_op + 1].regs == 0);
    2306             i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
    2307             i.types[first_reg_op + 1] = i.types[first_reg_op];
    2308             i.reg_operands = 2;
    2309           }
    2310 
    2311         if (i.tm.opcode_modifier & ShortForm)
    2312           {
    2313             /* The register or float register operand is in operand 0 or 1.  */
    2314             unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
    2315             /* Register goes in low 3 bits of opcode.  */
    2316             i.tm.base_opcode |= i.op[op].regs->reg_num;
    2317             if (i.op[op].regs->reg_flags & RegRex)
    2318               i.rex.extZ = 1;
    2319             if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
    2320               {
    2321                 /* Warn about some common errors, but press on regardless.
    2322                    The first case can be generated by gcc (<= 2.8.1).  */
    2323                 if (i.operands == 2)
    2324                   {
    2325                     /* Reversed arguments on faddp, fsubp, etc.  */
    2326                     as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
    2327                              i.op[1].regs->reg_name,
    2328                              i.op[0].regs->reg_name);
    2329                   }
    2330                 else
    2331                   {
    2332                     /* Extraneous `l' suffix on fp insn.  */
    2333                     as_warn (_("translating to `%s %%%s'"), i.tm.name,
    2334                              i.op[0].regs->reg_name);
    2335                   }
    2336               }
    2337           }
    2338         else if (i.tm.opcode_modifier & Modrm)
    2339           {
    2340             /* The opcode is completed (modulo i.tm.extension_opcode which
    2341                must be put into the modrm byte).
    2342                Now, we make the modrm & index base bytes based on all the
    2343                info we've collected.  */
    2344 
    2345             /* i.reg_operands MUST be the number of real register operands;
    2346                implicit registers do not count.  */
    2347             if (i.reg_operands == 2)
    2348               {
    2349                 unsigned int source, dest;
    2350                 source = ((i.types[0]
    2351                            & (Reg | RegMMX | RegXMM
    2352                               | SReg2 | SReg3
    2353                               | Control | Debug | Test))
    2354                           ? 0 : 1);
    2355                 dest = source + 1;
    2356 
    2357                 i.rm.mode = 3;
    2358                 /* One of the register operands will be encoded in the
    2359                    i.tm.reg field, the other in the combined i.tm.mode
    2360                    and i.tm.regmem fields.  If no form of this
    2361                    instruction supports a memory destination operand,
    2362                    then we assume the source operand may sometimes be
    2363                    a memory operand and so we need to store the
    2364                    destination in the i.rm.reg field.  */
    2365                 if ((i.tm.operand_types[dest] & AnyMem) == 0)
    2366                   {
    2367                     i.rm.reg = i.op[dest].regs->reg_num;
    2368                     i.rm.regmem = i.op[source].regs->reg_num;
    2369                     if (i.op[dest].regs->reg_flags & RegRex)
    2370                       i.rex.extX = 1;
    2371                     if (i.op[source].regs->reg_flags & RegRex)
    2372                       i.rex.extZ = 1;
    2373                   }
    2374                 else
    2375                   {
    2376                     i.rm.reg = i.op[source].regs->reg_num;
    2377                     i.rm.regmem = i.op[dest].regs->reg_num;
    2378                     if (i.op[dest].regs->reg_flags & RegRex)
    2379                       i.rex.extZ = 1;
    2380                     if (i.op[source].regs->reg_flags & RegRex)
    2381                       i.rex.extX = 1;
    2382                   }
    2383               }
    2384             else
    2385               {                 /* If it's not 2 reg operands...  */
    2386                 if (i.mem_operands)
    2387                   {
    2388                     unsigned int fake_zero_displacement = 0;
    2389                     unsigned int op = ((i.types[0] & AnyMem)
    2390                                        ? 0
    2391                                        : (i.types[1] & AnyMem) ? 1 : 2);
    2392 
    2393                     default_seg = &ds;
    2394 
    2395                     if (! i.base_reg)
    2396                       {
    2397                         i.rm.mode = 0;
    2398                         if (! i.disp_operands)
    2399                           fake_zero_displacement = 1;
    2400                         if (! i.index_reg)
    2401                           {
    2402                             /* Operand is just <disp>  */
    2403                             if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
    2404                               {
    2405                                 i.rm.regmem = NO_BASE_REGISTER_16;
    2406                                 i.types[op] &= ~Disp;
    2407                                 i.types[op] |= Disp16;
    2408                               }
    2409                             else if (flag_code != CODE_64BIT)
    2410                               {
    2411                                 i.rm.regmem = NO_BASE_REGISTER;
    2412                                 i.types[op] &= ~Disp;
    2413                                 i.types[op] |= Disp32;
    2414                               }
    2415                             else
    2416                               {
    2417                                 /* 64bit mode overwrites the 32bit
    2418                                    absolute addressing by RIP relative
    2419                                    addressing and absolute addressing
    2420                                    is encoded by one of the redundant
    2421                                    SIB forms.  */
    2422 
    2423                                 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
    2424                                 i.sib.base = NO_BASE_REGISTER;
    2425                                 i.sib.index = NO_INDEX_REGISTER;
    2426                                 i.types[op] &= ~Disp;
    2427                                 i.types[op] |= Disp32S;
    2428                               }
    2429                           }
    2430                         else /* ! i.base_reg && i.index_reg  */
    2431                           {
    2432                             i.sib.index = i.index_reg->reg_num;
    2433                             i.sib.base = NO_BASE_REGISTER;
    2434                             i.sib.scale = i.log2_scale_factor;
    2435                             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
    2436                             i.types[op] &= ~Disp;
    2437                             if (flag_code != CODE_64BIT)
    2438                               i.types[op] |= Disp32;    /* Must be 32 bit */
    2439                             else
    2440                               i.types[op] |= Disp32S;
    2441                             if (i.index_reg->reg_flags & RegRex)
    2442                               i.rex.extY = 1;
    2443                           }
    2444                       }
    2445                     /* RIP addressing for 64bit mode.  */
    2446                     else if (i.base_reg->reg_type == BaseIndex)
    2447                       {
    2448                         i.rm.regmem = NO_BASE_REGISTER;
    2449                         i.types[op] &= ~Disp;
    2450                         i.types[op] |= Disp32S;
    2451                         i.flags[op] = Operand_PCrel;
    2452                       }
    2453                     else if (i.base_reg->reg_type & Reg16)
    2454                       {
    2455                         switch (i.base_reg->reg_num)
    2456                           {
    2457                           case 3: /* (%bx)  */
    2458                             if (! i.index_reg)
    2459                               i.rm.regmem = 7;
    2460                             else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
    2461                               i.rm.regmem = i.index_reg->reg_num - 6;
    2462                             break;
    2463                           case 5: /* (%bp)  */
    2464                             default_seg = &ss;
    2465                             if (! i.index_reg)
    2466                               {
    2467                                 i.rm.regmem = 6;
    2468                                 if ((i.types[op] & Disp) == 0)
    2469                                   {
    2470                                     /* fake (%bp) into 0(%bp)  */
    2471                                     i.types[op] |= Disp8;
    2472                                     fake_zero_displacement = 1;
    2473                                   }
    2474                               }
    2475                             else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
    2476                               i.rm.regmem = i.index_reg->reg_num - 6 + 2;
    2477                             break;
    2478                           default: /* (%si) -> 4 or (%di) -> 5  */
    2479                             i.rm.regmem = i.base_reg->reg_num - 6 + 4;
    2480                           }
    2481                         i.rm.mode = mode_from_disp_size (i.types[op]);
    2482                       }
    2483                     else /* i.base_reg and 32/64 bit mode  */
    2484                       {
    2485                         if (flag_code == CODE_64BIT
    2486                             && (i.types[op] & Disp))
    2487                           {
    2488                             if (i.types[op] & Disp8)
    2489                               i.types[op] = Disp8 | Disp32S;
    2490                             else
    2491                               i.types[op] = Disp32S;
    2492                           }
    2493                         i.rm.regmem = i.base_reg->reg_num;
    2494                         if (i.base_reg->reg_flags & RegRex)
    2495                           i.rex.extZ = 1;
    2496                         i.sib.base = i.base_reg->reg_num;
    2497                         /* x86-64 ignores REX prefix bit here to avoid
    2498                            decoder complications.  */
    2499                         if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
    2500                           {
    2501                             default_seg = &ss;
    2502                             if (i.disp_operands == 0)
    2503                               {
    2504                                 fake_zero_displacement = 1;
    2505                                 i.types[op] |= Disp8;
    2506                               }
    2507                           }
    2508                         else if (i.base_reg->reg_num == ESP_REG_NUM)
    2509                           {
    2510                             default_seg = &ss;
    2511                           }
    2512                         i.sib.scale = i.log2_scale_factor;
    2513                         if (! i.index_reg)
    2514                           {
    2515                             /* <disp>(%esp) becomes two byte modrm
    2516                                with no index register.  We've already
    2517                                stored the code for esp in i.rm.regmem
    2518                                ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.  Any
    2519                                base register besides %esp will not use
    2520                                the extra modrm byte.  */
    2521                             i.sib.index = NO_INDEX_REGISTER;
    2522 #if ! SCALE1_WHEN_NO_INDEX
    2523                             /* Another case where we force the second
    2524                                modrm byte.  */
    2525                             if (i.log2_scale_factor)
    2526                               i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
    2527 #endif
    2528                           }
    2529                         else
    2530                           {
    2531                             i.sib.index = i.index_reg->reg_num;
    2532                             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
    2533                             if (i.index_reg->reg_flags & RegRex)
    2534                               i.rex.extY = 1;
    2535                           }
    2536                         i.rm.mode = mode_from_disp_size (i.types[op]);
    2537                       }
    2538 
    2539                     if (fake_zero_displacement)
    2540                       {
    2541                         /* Fakes a zero displacement assuming that i.types[op]
    2542                            holds the correct displacement size.  */
    2543                         expressionS *exp;
    2544 
    2545                         assert (i.op[op].disps == 0);
    2546                         exp = &disp_expressions[i.disp_operands++];
    2547                         i.op[op].disps = exp;
    2548                         exp->X_op = O_constant;
    2549                         exp->X_add_number = 0;
    2550                         exp->X_add_symbol = (symbolS *) 0;
    2551                         exp->X_op_symbol = (symbolS *) 0;
    2552                       }
    2553                   }
    2554 
    2555                 /* Fill in i.rm.reg or i.rm.regmem field with register
    2556                    operand (if any) based on i.tm.extension_opcode.
    2557                    Again, we must be careful to make sure that
    2558                    segment/control/debug/test/MMX registers are coded
    2559                    into the i.rm.reg field.  */
    2560                 if (i.reg_operands)
    2561                   {
    2562                     unsigned int op =
    2563                       ((i.types[0]
    2564                         & (Reg | RegMMX | RegXMM
    2565                            | SReg2 | SReg3
    2566                            | Control | Debug | Test))
    2567                        ? 0
    2568                        : ((i.types[1]
    2569                            & (Reg | RegMMX | RegXMM
    2570                               | SReg2 | SReg3
    2571                               | Control | Debug | Test))
    2572                           ? 1
    2573                           : 2));
    2574                     /* If there is an extension opcode to put here, the
    2575                        register number must be put into the regmem field.  */
    2576                     if (i.tm.extension_opcode != None)
    2577                       {
    2578                         i.rm.regmem = i.op[op].regs->reg_num;
    2579                         if (i.op[op].regs->reg_flags & RegRex)
    2580                           i.rex.extZ = 1;
    2581                       }
    2582                     else
    2583                       {
    2584                         i.rm.reg = i.op[op].regs->reg_num;
    2585                         if (i.op[op].regs->reg_flags & RegRex)
    2586                           i.rex.extX = 1;
    2587                       }
    2588 
    2589                     /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
    2590                        we must set it to 3 to indicate this is a register
    2591                        operand in the regmem field.  */
    2592                     if (!i.mem_operands)
    2593                       i.rm.mode = 3;
    2594                   }
    2595 
    2596                 /* Fill in i.rm.reg field with extension opcode (if any).  */
    2597                 if (i.tm.extension_opcode != None)
    2598                   i.rm.reg = i.tm.extension_opcode;
    2599               }
    2600           }
    2601         else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
    2602           {
    2603             if (i.tm.base_opcode == POP_SEG_SHORT
    2604                 && i.op[0].regs->reg_num == 1)
    2605               {
    2606                 as_bad (_("you can't `pop %%cs'"));
    2607                 return;
    2608               }
    2609             i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
    2610             if (i.op[0].regs->reg_flags & RegRex)
    2611               i.rex.extZ = 1;
    2612           }
    2613         else if ((i.tm.base_opcode & ~(D|W)) == MOV_AX_DISP32)
    2614           {
    2615             default_seg = &ds;
    2616           }
    2617         else if ((i.tm.opcode_modifier & IsString) != 0)
    2618           {
    2619             /* For the string instructions that allow a segment override
    2620                on one of their operands, the default segment is ds.  */
    2621             default_seg = &ds;
    2622           }
    2623 
    2624         /* If a segment was explicitly specified,
    2625            and the specified segment is not the default,
    2626            use an opcode prefix to select it.
    2627            If we never figured out what the default segment is,
    2628            then default_seg will be zero at this point,
    2629            and the specified segment prefix will always be used.  */
    2630         if ((i.seg[0]) && (i.seg[0] != default_seg))
    2631           {
    2632             if (! add_prefix (i.seg[0]->seg_prefix))
    2633               return;
    2634           }
    2635       }
    2636     else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
    2637       {
    2638         /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
    2639         as_warn (_("translating to `%sp'"), i.tm.name);
    2640       }
    2641   }
     1421    }
     1422  else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
     1423    {
     1424      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
     1425      as_warn (_("translating to `%sp'"), i.tm.name);
     1426    }
    26421427
    26431428  /* Handle conversion of 'int $3' --> special int3 insn.  */
     
    26581443    }
    26591444
    2660   if (i.tm.opcode_modifier & Rex64)
    2661     i.rex.mode64 = 1;
    2662 
    2663   /* For 8bit registers we would need an empty rex prefix.
    2664      Also in the case instruction is already having prefix,
    2665      we need to convert old registers to new ones.  */
    2666 
    2667   if (((i.types[0] & Reg8) && (i.op[0].regs->reg_flags & RegRex64))
    2668       || ((i.types[1] & Reg8) && (i.op[1].regs->reg_flags & RegRex64))
    2669       || ((i.rex.mode64 || i.rex.extX || i.rex.extY || i.rex.extZ || i.rex.empty)
    2670           && ((i.types[0] & Reg8) || (i.types[1] & Reg8))))
     1445  if ((i.tm.opcode_modifier & Rex64) != 0)
     1446    i.rex |= REX_MODE64;
     1447
     1448  /* For 8 bit registers we need an empty rex prefix.  Also if the
     1449     instruction already has a prefix, we need to convert old
     1450     registers to new ones.  */
     1451
     1452  if (((i.types[0] & Reg8) != 0
     1453       && (i.op[0].regs->reg_flags & RegRex64) != 0)
     1454      || ((i.types[1] & Reg8) != 0
     1455          && (i.op[1].regs->reg_flags & RegRex64) != 0)
     1456      || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
     1457          && i.rex != 0))
    26711458    {
    26721459      int x;
    2673       i.rex.empty = 1;
     1460
     1461      i.rex |= REX_OPCODE;
    26741462      for (x = 0; x < 2; x++)
    26751463        {
    2676           /* Look for 8bit operand that does use old registers.  */
    2677           if (i.types[x] & Reg8
    2678               && !(i.op[x].regs->reg_flags & RegRex64))
     1464          /* Look for 8 bit operand that uses old registers.  */
     1465          if ((i.types[x] & Reg8) != 0
     1466              && (i.op[x].regs->reg_flags & RegRex64) == 0)
    26791467            {
    26801468              /* In case it is "hi" register, give up.  */
    26811469              if (i.op[x].regs->reg_num > 3)
    2682                 as_bad (_("Can't encode registers '%%%s' in the instruction requiring REX prefix.\n"),
     1470                as_bad (_("can't encode register '%%%s' in an instruction requiring REX prefix.\n"),
    26831471                        i.op[x].regs->reg_name);
    26841472
    26851473              /* Otherwise it is equivalent to the extended register.
    2686                  Since the encoding don't change this is merely cosmetical
    2687                 cleanup for debug output.  */
     1474                 Since the encoding doesn't change this is merely
     1475                 cosmetic cleanup for debug output.  */
    26881476
    26891477              i.op[x].regs = i.op[x].regs + 8;
     
    26921480    }
    26931481
    2694   if (i.rex.mode64 || i.rex.extX || i.rex.extY || i.rex.extZ || i.rex.empty)
    2695     add_prefix (0x40
    2696                 | (i.rex.mode64 ? 8 : 0)
    2697                 | (i.rex.extX ? 4 : 0)
    2698                 | (i.rex.extY ? 2 : 0)
    2699                 | (i.rex.extZ ? 1 : 0));
     1482  if (i.rex != 0)
     1483    add_prefix (REX_OPCODE | i.rex);
    27001484
    27011485  /* We are ready to output the insn.  */
    2702   {
    2703     register char *p;
    2704 
    2705     /* Tie dwarf2 debug info to the address at the start of the insn.
    2706        We can't do this after the insn has been output as the current
    2707        frag may have been closed off.  eg. by frag_var.  */
    2708     dwarf2_emit_insn (0);
    2709 
    2710     /* Output jumps.  */
    2711     if (i.tm.opcode_modifier & Jump)
     1486  output_insn ();
     1487}
     1488
     1489static char *
     1490parse_insn (line, mnemonic)
     1491     char *line;
     1492     char *mnemonic;
     1493{
     1494  char *l = line;
     1495  char *token_start = l;
     1496  char *mnem_p;
     1497
     1498  /* Non-zero if we found a prefix only acceptable with string insns.  */
     1499  const char *expecting_string_instruction = NULL;
     1500
     1501  while (1)
     1502    {
     1503      mnem_p = mnemonic;
     1504      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
     1505        {
     1506          mnem_p++;
     1507          if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
     1508            {
     1509              as_bad (_("no such instruction: `%s'"), token_start);
     1510              return NULL;
     1511            }
     1512          l++;
     1513        }
     1514      if (!is_space_char (*l)
     1515          && *l != END_OF_INSN
     1516          && *l != PREFIX_SEPARATOR
     1517          && *l != ',')
     1518        {
     1519          as_bad (_("invalid character %s in mnemonic"),
     1520                  output_invalid (*l));
     1521          return NULL;
     1522        }
     1523      if (token_start == l)
     1524        {
     1525          if (*l == PREFIX_SEPARATOR)
     1526            as_bad (_("expecting prefix; got nothing"));
     1527          else
     1528            as_bad (_("expecting mnemonic; got nothing"));
     1529          return NULL;
     1530        }
     1531
     1532      /* Look up instruction (or prefix) via hash table.  */
     1533      current_templates = hash_find (op_hash, mnemonic);
     1534
     1535      if (*l != END_OF_INSN
     1536          && (!is_space_char (*l) || l[1] != END_OF_INSN)
     1537          && current_templates
     1538          && (current_templates->start->opcode_modifier & IsPrefix))
     1539        {
     1540          /* If we are in 16-bit mode, do not allow addr16 or data16.
     1541             Similarly, in 32-bit mode, do not allow addr32 or data32.  */
     1542          if ((current_templates->start->opcode_modifier & (Size16 | Size32))
     1543              && flag_code != CODE_64BIT
     1544              && (((current_templates->start->opcode_modifier & Size32) != 0)
     1545                  ^ (flag_code == CODE_16BIT)))
     1546            {
     1547              as_bad (_("redundant %s prefix"),
     1548                      current_templates->start->name);
     1549              return NULL;
     1550            }
     1551          /* Add prefix, checking for repeated prefixes.  */
     1552          switch (add_prefix (current_templates->start->base_opcode))
     1553            {
     1554            case 0:
     1555              return NULL;
     1556            case 2:
     1557              expecting_string_instruction = current_templates->start->name;
     1558              break;
     1559            }
     1560          /* Skip past PREFIX_SEPARATOR and reset token_start.  */
     1561          token_start = ++l;
     1562        }
     1563      else
     1564        break;
     1565    }
     1566
     1567  if (!current_templates)
     1568    {
     1569      /* See if we can get a match by trimming off a suffix.  */
     1570      switch (mnem_p[-1])
     1571        {
     1572        case WORD_MNEM_SUFFIX:
     1573        case BYTE_MNEM_SUFFIX:
     1574        case QWORD_MNEM_SUFFIX:
     1575          i.suffix = mnem_p[-1];
     1576          mnem_p[-1] = '\0';
     1577          current_templates = hash_find (op_hash, mnemonic);
     1578          break;
     1579        case SHORT_MNEM_SUFFIX:
     1580        case LONG_MNEM_SUFFIX:
     1581          if (!intel_syntax)
     1582            {
     1583              i.suffix = mnem_p[-1];
     1584              mnem_p[-1] = '\0';
     1585              current_templates = hash_find (op_hash, mnemonic);
     1586            }
     1587          break;
     1588
     1589          /* Intel Syntax.  */
     1590        case 'd':
     1591          if (intel_syntax)
     1592            {
     1593              if (intel_float_operand (mnemonic))
     1594                i.suffix = SHORT_MNEM_SUFFIX;
     1595              else
     1596                i.suffix = LONG_MNEM_SUFFIX;
     1597              mnem_p[-1] = '\0';
     1598              current_templates = hash_find (op_hash, mnemonic);
     1599            }
     1600          break;
     1601        }
     1602      if (!current_templates)
     1603        {
     1604          as_bad (_("no such instruction: `%s'"), token_start);
     1605          return NULL;
     1606        }
     1607    }
     1608
     1609  if (current_templates->start->opcode_modifier & (Jump | JumpByte))
     1610    {
     1611      /* Check for a branch hint.  We allow ",pt" and ",pn" for
     1612         predict taken and predict not taken respectively.
     1613         I'm not sure that branch hints actually do anything on loop
     1614         and jcxz insns (JumpByte) for current Pentium4 chips.  They
     1615         may work in the future and it doesn't hurt to accept them
     1616         now.  */
     1617      if (l[0] == ',' && l[1] == 'p')
     1618        {
     1619          if (l[2] == 't')
     1620            {
     1621              if (!add_prefix (DS_PREFIX_OPCODE))
     1622                return NULL;
     1623              l += 3;
     1624            }
     1625          else if (l[2] == 'n')
     1626            {
     1627              if (!add_prefix (CS_PREFIX_OPCODE))
     1628                return NULL;
     1629              l += 3;
     1630            }
     1631        }
     1632    }
     1633  /* Any other comma loses.  */
     1634  if (*l == ',')
     1635    {
     1636      as_bad (_("invalid character %s in mnemonic"),
     1637              output_invalid (*l));
     1638      return NULL;
     1639    }
     1640
     1641  /* Check if instruction is supported on specified architecture.  */
     1642  if ((current_templates->start->cpu_flags & ~(Cpu64 | CpuNo64))
     1643      & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64)))
     1644    {
     1645      as_warn (_("`%s' is not supported on `%s'"),
     1646               current_templates->start->name, cpu_arch_name);
     1647    }
     1648  else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
     1649    {
     1650      as_warn (_("use .code16 to ensure correct addressing mode"));
     1651    }
     1652
     1653  /* Check for rep/repne without a string instruction.  */
     1654  if (expecting_string_instruction
     1655      && !(current_templates->start->opcode_modifier & IsString))
     1656    {
     1657      as_bad (_("expecting string instruction after `%s'"),
     1658              expecting_string_instruction);
     1659      return NULL;
     1660    }
     1661
     1662  return l;
     1663}
     1664
     1665static char *
     1666parse_operands (l, mnemonic)
     1667     char *l;
     1668     const char *mnemonic;
     1669{
     1670  char *token_start;
     1671
     1672  /* 1 if operand is pending after ','.  */
     1673  unsigned int expecting_operand = 0;
     1674
     1675  /* Non-zero if operand parens not balanced.  */
     1676  unsigned int paren_not_balanced;
     1677
     1678  while (*l != END_OF_INSN)
     1679    {
     1680      /* Skip optional white space before operand.  */
     1681      if (is_space_char (*l))
     1682        ++l;
     1683      if (!is_operand_char (*l) && *l != END_OF_INSN)
     1684        {
     1685          as_bad (_("invalid character %s before operand %d"),
     1686                  output_invalid (*l),
     1687                  i.operands + 1);
     1688          return NULL;
     1689        }
     1690      token_start = l;  /* after white space */
     1691      paren_not_balanced = 0;
     1692      while (paren_not_balanced || *l != ',')
     1693        {
     1694          if (*l == END_OF_INSN)
     1695            {
     1696              if (paren_not_balanced)
     1697                {
     1698                  if (!intel_syntax)
     1699                    as_bad (_("unbalanced parenthesis in operand %d."),
     1700                            i.operands + 1);
     1701                  else
     1702                    as_bad (_("unbalanced brackets in operand %d."),
     1703                            i.operands + 1);
     1704                  return NULL;
     1705                }
     1706              else
     1707                break;  /* we are done */
     1708            }
     1709          else if (!is_operand_char (*l) && !is_space_char (*l))
     1710            {
     1711              as_bad (_("invalid character %s in operand %d"),
     1712                      output_invalid (*l),
     1713                      i.operands + 1);
     1714              return NULL;
     1715            }
     1716          if (!intel_syntax)
     1717            {
     1718              if (*l == '(')
     1719                ++paren_not_balanced;
     1720              if (*l == ')')
     1721                --paren_not_balanced;
     1722            }
     1723          else
     1724            {
     1725              if (*l == '[')
     1726                ++paren_not_balanced;
     1727              if (*l == ']')
     1728                --paren_not_balanced;
     1729            }
     1730          l++;
     1731        }
     1732      if (l != token_start)
     1733        {                       /* Yes, we've read in another operand.  */
     1734          unsigned int operand_ok;
     1735          this_operand = i.operands++;
     1736          if (i.operands > MAX_OPERANDS)
     1737            {
     1738              as_bad (_("spurious operands; (%d operands/instruction max)"),
     1739                      MAX_OPERANDS);
     1740              return NULL;
     1741            }
     1742          /* Now parse operand adding info to 'i' as we go along.  */
     1743          END_STRING_AND_SAVE (l);
     1744
     1745          if (intel_syntax)
     1746            operand_ok =
     1747              i386_intel_operand (token_start,
     1748                                  intel_float_operand (mnemonic));
     1749          else
     1750            operand_ok = i386_operand (token_start);
     1751
     1752          RESTORE_END_STRING (l);
     1753          if (!operand_ok)
     1754            return NULL;
     1755        }
     1756      else
     1757        {
     1758          if (expecting_operand)
     1759            {
     1760            expecting_operand_after_comma:
     1761              as_bad (_("expecting operand after ','; got nothing"));
     1762              return NULL;
     1763            }
     1764          if (*l == ',')
     1765            {
     1766              as_bad (_("expecting operand before ','; got nothing"));
     1767              return NULL;
     1768            }
     1769        }
     1770
     1771      /* Now *l must be either ',' or END_OF_INSN.  */
     1772      if (*l == ',')
     1773        {
     1774          if (*++l == END_OF_INSN)
     1775            {
     1776              /* Just skip it, if it's \n complain.  */
     1777              goto expecting_operand_after_comma;
     1778            }
     1779          expecting_operand = 1;
     1780        }
     1781    }
     1782  return l;
     1783}
     1784
     1785static void
     1786swap_operands ()
     1787{
     1788  union i386_op temp_op;
     1789  unsigned int temp_type;
     1790  RELOC_ENUM temp_reloc;
     1791  int xchg1 = 0;
     1792  int xchg2 = 0;
     1793
     1794  if (i.operands == 2)
     1795    {
     1796      xchg1 = 0;
     1797      xchg2 = 1;
     1798    }
     1799  else if (i.operands == 3)
     1800    {
     1801      xchg1 = 0;
     1802      xchg2 = 2;
     1803    }
     1804  temp_type = i.types[xchg2];
     1805  i.types[xchg2] = i.types[xchg1];
     1806  i.types[xchg1] = temp_type;
     1807  temp_op = i.op[xchg2];
     1808  i.op[xchg2] = i.op[xchg1];
     1809  i.op[xchg1] = temp_op;
     1810  temp_reloc = i.reloc[xchg2];
     1811  i.reloc[xchg2] = i.reloc[xchg1];
     1812  i.reloc[xchg1] = temp_reloc;
     1813
     1814  if (i.mem_operands == 2)
     1815    {
     1816      const seg_entry *temp_seg;
     1817      temp_seg = i.seg[0];
     1818      i.seg[0] = i.seg[1];
     1819      i.seg[1] = temp_seg;
     1820    }
     1821}
     1822
     1823/* Try to ensure constant immediates are represented in the smallest
     1824   opcode possible.  */
     1825static void
     1826optimize_imm ()
     1827{
     1828  char guess_suffix = 0;
     1829  int op;
     1830
     1831  if (i.suffix)
     1832    guess_suffix = i.suffix;
     1833  else if (i.reg_operands)
     1834    {
     1835      /* Figure out a suffix from the last register operand specified.
     1836         We can't do this properly yet, ie. excluding InOutPortReg,
     1837         but the following works for instructions with immediates.
     1838         In any case, we can't set i.suffix yet.  */
     1839      for (op = i.operands; --op >= 0;)
     1840        if (i.types[op] & Reg)
     1841          {
     1842            if (i.types[op] & Reg8)
     1843              guess_suffix = BYTE_MNEM_SUFFIX;
     1844            else if (i.types[op] & Reg16)
     1845              guess_suffix = WORD_MNEM_SUFFIX;
     1846            else if (i.types[op] & Reg32)
     1847              guess_suffix = LONG_MNEM_SUFFIX;
     1848            else if (i.types[op] & Reg64)
     1849              guess_suffix = QWORD_MNEM_SUFFIX;
     1850            break;
     1851          }
     1852    }
     1853  else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
     1854    guess_suffix = WORD_MNEM_SUFFIX;
     1855
     1856  for (op = i.operands; --op >= 0;)
     1857    if (i.types[op] & Imm)
    27121858      {
    2713         int code16;
    2714         int prefix;
    2715 
    2716         code16 = 0;
    2717         if (flag_code == CODE_16BIT)
    2718           code16 = CODE16;
    2719 
    2720         prefix = 0;
    2721         if (i.prefix[DATA_PREFIX])
     1859        switch (i.op[op].imms->X_op)
    27221860          {
    2723             prefix = 1;
    2724             i.prefixes -= 1;
    2725             code16 ^= CODE16;
     1861          case O_constant:
     1862            /* If a suffix is given, this operand may be shortened.  */
     1863            switch (guess_suffix)
     1864              {
     1865              case LONG_MNEM_SUFFIX:
     1866                i.types[op] |= Imm32 | Imm64;
     1867                break;
     1868              case WORD_MNEM_SUFFIX:
     1869                i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
     1870                break;
     1871              case BYTE_MNEM_SUFFIX:
     1872                i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
     1873                break;
     1874              }
     1875
     1876            /* If this operand is at most 16 bits, convert it
     1877               to a signed 16 bit number before trying to see
     1878               whether it will fit in an even smaller size.
     1879               This allows a 16-bit operand such as $0xffe0 to
     1880               be recognised as within Imm8S range.  */
     1881            if ((i.types[op] & Imm16)
     1882                && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
     1883              {
     1884                i.op[op].imms->X_add_number =
     1885                  (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
     1886              }
     1887            if ((i.types[op] & Imm32)
     1888                && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
     1889                    == 0))
     1890              {
     1891                i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
     1892                                                ^ ((offsetT) 1 << 31))
     1893                                               - ((offsetT) 1 << 31));
     1894              }
     1895            i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
     1896
     1897            /* We must avoid matching of Imm32 templates when 64bit
     1898               only immediate is available.  */
     1899            if (guess_suffix == QWORD_MNEM_SUFFIX)
     1900              i.types[op] &= ~Imm32;
     1901            break;
     1902
     1903          case O_absent:
     1904          case O_register:
     1905            abort ();
     1906
     1907            /* Symbols and expressions.  */
     1908          default:
     1909            /* Convert symbolic operand to proper sizes for matching.  */
     1910            switch (guess_suffix)
     1911              {
     1912              case QWORD_MNEM_SUFFIX:
     1913                i.types[op] = Imm64 | Imm32S;
     1914                break;
     1915              case LONG_MNEM_SUFFIX:
     1916                i.types[op] = Imm32 | Imm64;
     1917                break;
     1918              case WORD_MNEM_SUFFIX:
     1919                i.types[op] = Imm16 | Imm32 | Imm64;
     1920                break;
     1921                break;
     1922              case BYTE_MNEM_SUFFIX:
     1923                i.types[op] = Imm8 | Imm8S | Imm16 | Imm32S | Imm32;
     1924                break;
     1925                break;
     1926              }
     1927            break;
    27261928          }
    2727         /* Pentium4 branch hints.  */
    2728         if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
    2729             || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
     1929      }
     1930}
     1931
     1932/* Try to use the smallest displacement type too.  */
     1933static void
     1934optimize_disp ()
     1935{
     1936  int op;
     1937
     1938  for (op = i.operands; --op >= 0;)
     1939    if ((i.types[op] & Disp) && i.op[op].disps->X_op == O_constant)
     1940      {
     1941        offsetT disp = i.op[op].disps->X_add_number;
     1942
     1943        if (i.types[op] & Disp16)
    27301944          {
    2731             prefix++;
    2732             i.prefixes--;
     1945            /* We know this operand is at most 16 bits, so
     1946               convert to a signed 16 bit number before trying
     1947               to see whether it will fit in an even smaller
     1948               size.  */
     1949
     1950            disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
    27331951          }
    2734         if (i.prefix[REX_PREFIX])
     1952        else if (i.types[op] & Disp32)
    27351953          {
    2736             prefix++;
    2737             i.prefixes--;
     1954            /* We know this operand is at most 32 bits, so convert to a
     1955               signed 32 bit number before trying to see whether it will
     1956               fit in an even smaller size.  */
     1957            disp &= (((offsetT) 2 << 31) - 1);
     1958            disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
    27381959          }
    2739 
    2740         if (i.prefixes != 0 && !intel_syntax)
    2741           as_warn (_("skipping prefixes on this instruction"));
    2742 
    2743         /* It's always a symbol;  End frag & setup for relax.
    2744            Make sure there is enough room in this frag for the largest
    2745            instruction we may generate in md_convert_frag.  This is 2
    2746            bytes for the opcode and room for the prefix and largest
    2747            displacement.  */
    2748         frag_grow (prefix + 2 + 4);
    2749         /* Prefix and 1 opcode byte go in fr_fix.  */
    2750         p = frag_more (prefix + 1);
    2751         if (i.prefix[DATA_PREFIX])
    2752           *p++ = DATA_PREFIX_OPCODE;
    2753         if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
    2754             || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
    2755           *p++ = i.prefix[SEG_PREFIX];
    2756         if (i.prefix[REX_PREFIX])
    2757           *p++ = i.prefix[REX_PREFIX];
    2758         *p = i.tm.base_opcode;
    2759         /* 1 possible extra opcode + displacement go in var part.
    2760            Pass reloc in fr_var.  */
    2761         frag_var (rs_machine_dependent,
    2762                   1 + 4,
    2763                   i.reloc[0],
    2764                   ((unsigned char) *p == JUMP_PC_RELATIVE
    2765                    ? ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL) | code16
    2766                    : ((cpu_arch_flags & Cpu386) != 0
    2767                       ? ENCODE_RELAX_STATE (COND_JUMP, SMALL) | code16
    2768                       : ENCODE_RELAX_STATE (COND_JUMP86, SMALL) | code16)),
    2769                   i.op[0].disps->X_add_symbol,
    2770                   i.op[0].disps->X_add_number,
    2771                   p);
     1960        if (flag_code == CODE_64BIT)
     1961          {
     1962            if (fits_in_signed_long (disp))
     1963              i.types[op] |= Disp32S;
     1964            if (fits_in_unsigned_long (disp))
     1965              i.types[op] |= Disp32;
     1966          }
     1967        if ((i.types[op] & (Disp32 | Disp32S | Disp16))
     1968            && fits_in_signed_byte (disp))
     1969          i.types[op] |= Disp8;
    27721970      }
    2773     else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
     1971}
     1972
     1973static int
     1974match_template ()
     1975{
     1976  /* Points to template once we've found it.  */
     1977  const template *t;
     1978  unsigned int overlap0, overlap1, overlap2;
     1979  unsigned int found_reverse_match;
     1980  int suffix_check;
     1981
     1982#define MATCH(overlap, given, template)                         \
     1983  ((overlap & ~JumpAbsolute)                                    \
     1984   && (((given) & (BaseIndex | JumpAbsolute))                   \
     1985       == ((overlap) & (BaseIndex | JumpAbsolute))))
     1986
     1987  /* If given types r0 and r1 are registers they must be of the same type
     1988     unless the expected operand type register overlap is null.
     1989     Note that Acc in a template matches every size of reg.  */
     1990#define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1)       \
     1991  (((g0) & Reg) == 0 || ((g1) & Reg) == 0                       \
     1992   || ((g0) & Reg) == ((g1) & Reg)                              \
     1993   || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
     1994
     1995  overlap0 = 0;
     1996  overlap1 = 0;
     1997  overlap2 = 0;
     1998  found_reverse_match = 0;
     1999  suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
     2000                  ? No_bSuf
     2001                  : (i.suffix == WORD_MNEM_SUFFIX
     2002                     ? No_wSuf
     2003                     : (i.suffix == SHORT_MNEM_SUFFIX
     2004                        ? No_sSuf
     2005                        : (i.suffix == LONG_MNEM_SUFFIX
     2006                           ? No_lSuf
     2007                           : (i.suffix == QWORD_MNEM_SUFFIX
     2008                              ? No_qSuf
     2009                              : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
     2010                                 ? No_xSuf : 0))))));
     2011
     2012  for (t = current_templates->start;
     2013       t < current_templates->end;
     2014       t++)
     2015    {
     2016      /* Must have right number of operands.  */
     2017      if (i.operands != t->operands)
     2018        continue;
     2019
     2020      /* Check the suffix, except for some instructions in intel mode.  */
     2021      if ((t->opcode_modifier & suffix_check)
     2022          && !(intel_syntax
     2023               && (t->opcode_modifier & IgnoreSize))
     2024          && !(intel_syntax
     2025               && t->base_opcode == 0xd9
     2026               && (t->extension_opcode == 5          /* 0xd9,5 "fldcw"  */
     2027                   || t->extension_opcode == 7)))  /* 0xd9,7 "f{n}stcw"  */
     2028        continue;
     2029
     2030      /* Do not verify operands when there are none.  */
     2031      else if (!t->operands)
     2032        {
     2033          if (t->cpu_flags & ~cpu_arch_flags)
     2034            continue;
     2035          /* We've found a match; break out of loop.  */
     2036          break;
     2037        }
     2038
     2039      overlap0 = i.types[0] & t->operand_types[0];
     2040      switch (t->operands)
     2041        {
     2042        case 1:
     2043          if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
     2044            continue;
     2045          break;
     2046        case 2:
     2047        case 3:
     2048          overlap1 = i.types[1] & t->operand_types[1];
     2049          if (!MATCH (overlap0, i.types[0], t->operand_types[0])
     2050              || !MATCH (overlap1, i.types[1], t->operand_types[1])
     2051              || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
     2052                                             t->operand_types[0],
     2053                                             overlap1, i.types[1],
     2054                                             t->operand_types[1]))
     2055            {
     2056              /* Check if other direction is valid ...  */
     2057              if ((t->opcode_modifier & (D | FloatD)) == 0)
     2058                continue;
     2059
     2060              /* Try reversing direction of operands.  */
     2061              overlap0 = i.types[0] & t->operand_types[1];
     2062              overlap1 = i.types[1] & t->operand_types[0];
     2063              if (!MATCH (overlap0, i.types[0], t->operand_types[1])
     2064                  || !MATCH (overlap1, i.types[1], t->operand_types[0])
     2065                  || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
     2066                                                 t->operand_types[1],
     2067                                                 overlap1, i.types[1],
     2068                                                 t->operand_types[0]))
     2069                {
     2070                  /* Does not match either direction.  */
     2071                  continue;
     2072                }
     2073              /* found_reverse_match holds which of D or FloatDR
     2074                 we've found.  */
     2075              found_reverse_match = t->opcode_modifier & (D | FloatDR);
     2076            }
     2077          /* Found a forward 2 operand match here.  */
     2078          else if (t->operands == 3)
     2079            {
     2080              /* Here we make use of the fact that there are no
     2081                 reverse match 3 operand instructions, and all 3
     2082                 operand instructions only need to be checked for
     2083                 register consistency between operands 2 and 3.  */
     2084              overlap2 = i.types[2] & t->operand_types[2];
     2085              if (!MATCH (overlap2, i.types[2], t->operand_types[2])
     2086                  || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
     2087                                                 t->operand_types[1],
     2088                                                 overlap2, i.types[2],
     2089                                                 t->operand_types[2]))
     2090
     2091                continue;
     2092            }
     2093          /* Found either forward/reverse 2 or 3 operand match here:
     2094             slip through to break.  */
     2095        }
     2096      if (t->cpu_flags & ~cpu_arch_flags)
     2097        {
     2098          found_reverse_match = 0;
     2099          continue;
     2100        }
     2101      /* We've found a match; break out of loop.  */
     2102      break;
     2103    }
     2104
     2105  if (t == current_templates->end)
     2106    {
     2107      /* We found no match.  */
     2108      as_bad (_("suffix or operands invalid for `%s'"),
     2109              current_templates->start->name);
     2110      return 0;
     2111    }
     2112
     2113  if (!quiet_warnings)
     2114    {
     2115      if (!intel_syntax
     2116          && ((i.types[0] & JumpAbsolute)
     2117              != (t->operand_types[0] & JumpAbsolute)))
     2118        {
     2119          as_warn (_("indirect %s without `*'"), t->name);
     2120        }
     2121
     2122      if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
     2123          == (IsPrefix | IgnoreSize))
     2124        {
     2125          /* Warn them that a data or address size prefix doesn't
     2126             affect assembly of the next line of code.  */
     2127          as_warn (_("stand-alone `%s' prefix"), t->name);
     2128        }
     2129    }
     2130
     2131  /* Copy the template we found.  */
     2132  i.tm = *t;
     2133  if (found_reverse_match)
     2134    {
     2135      /* If we found a reverse match we must alter the opcode
     2136         direction bit.  found_reverse_match holds bits to change
     2137         (different for int & float insns).  */
     2138
     2139      i.tm.base_opcode ^= found_reverse_match;
     2140
     2141      i.tm.operand_types[0] = t->operand_types[1];
     2142      i.tm.operand_types[1] = t->operand_types[0];
     2143    }
     2144
     2145  return 1;
     2146}
     2147
     2148static int
     2149check_string ()
     2150{
     2151  int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
     2152  if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
     2153    {
     2154      if (i.seg[0] != NULL && i.seg[0] != &es)
     2155        {
     2156          as_bad (_("`%s' operand %d must use `%%es' segment"),
     2157                  i.tm.name,
     2158                  mem_op + 1);
     2159          return 0;
     2160        }
     2161      /* There's only ever one segment override allowed per instruction.
     2162         This instruction possibly has a legal segment override on the
     2163         second operand, so copy the segment to where non-string
     2164         instructions store it, allowing common code.  */
     2165      i.seg[0] = i.seg[1];
     2166    }
     2167  else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
     2168    {
     2169      if (i.seg[1] != NULL && i.seg[1] != &es)
     2170        {
     2171          as_bad (_("`%s' operand %d must use `%%es' segment"),
     2172                  i.tm.name,
     2173                  mem_op + 2);
     2174          return 0;
     2175        }
     2176    }
     2177  return 1;
     2178}
     2179
     2180static int
     2181process_suffix ()
     2182{
     2183  /* If matched instruction specifies an explicit instruction mnemonic
     2184     suffix, use it.  */
     2185  if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
     2186    {
     2187      if (i.tm.opcode_modifier & Size16)
     2188        i.suffix = WORD_MNEM_SUFFIX;
     2189      else if (i.tm.opcode_modifier & Size64)
     2190        i.suffix = QWORD_MNEM_SUFFIX;
     2191      else
     2192        i.suffix = LONG_MNEM_SUFFIX;
     2193    }
     2194  else if (i.reg_operands)
     2195    {
     2196      /* If there's no instruction mnemonic suffix we try to invent one
     2197         based on register operands.  */
     2198      if (!i.suffix)
     2199        {
     2200          /* We take i.suffix from the last register operand specified,
     2201             Destination register type is more significant than source
     2202             register type.  */
     2203          int op;
     2204          for (op = i.operands; --op >= 0;)
     2205            if ((i.types[op] & Reg)
     2206                && !(i.tm.operand_types[op] & InOutPortReg))
     2207              {
     2208                i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
     2209                            (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
     2210                            (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
     2211                            LONG_MNEM_SUFFIX);
     2212                break;
     2213              }
     2214        }
     2215      else if (i.suffix == BYTE_MNEM_SUFFIX)
     2216        {
     2217          if (!check_byte_reg ())
     2218            return 0;
     2219        }
     2220      else if (i.suffix == LONG_MNEM_SUFFIX)
     2221        {
     2222          if (!check_long_reg ())
     2223            return 0;
     2224        }
     2225      else if (i.suffix == QWORD_MNEM_SUFFIX)
     2226        {
     2227          if (!check_qword_reg ())
     2228            return 0;
     2229        }
     2230      else if (i.suffix == WORD_MNEM_SUFFIX)
     2231        {
     2232          if (!check_word_reg ())
     2233            return 0;
     2234        }
     2235      else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
     2236        /* Do nothing if the instruction is going to ignore the prefix.  */
     2237        ;
     2238      else
     2239        abort ();
     2240    }
     2241  else if ((i.tm.opcode_modifier & DefaultSize) && !i.suffix)
     2242    {
     2243      i.suffix = stackop_size;
     2244    }
     2245
     2246  /* Change the opcode based on the operand size given by i.suffix;
     2247     We need not change things for byte insns.  */
     2248
     2249  if (!i.suffix && (i.tm.opcode_modifier & W))
     2250    {
     2251      as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
     2252      return 0;
     2253    }
     2254
     2255  if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
     2256    {
     2257      /* It's not a byte, select word/dword operation.  */
     2258      if (i.tm.opcode_modifier & W)
     2259        {
     2260          if (i.tm.opcode_modifier & ShortForm)
     2261            i.tm.base_opcode |= 8;
     2262          else
     2263            i.tm.base_opcode |= 1;
     2264        }
     2265
     2266      /* Now select between word & dword operations via the operand
     2267         size prefix, except for instructions that will ignore this
     2268         prefix anyway.  */
     2269      if (i.suffix != QWORD_MNEM_SUFFIX
     2270          && !(i.tm.opcode_modifier & IgnoreSize)
     2271          && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
     2272              || (flag_code == CODE_64BIT
     2273                  && (i.tm.opcode_modifier & JumpByte))))
     2274        {
     2275          unsigned int prefix = DATA_PREFIX_OPCODE;
     2276          if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
     2277            prefix = ADDR_PREFIX_OPCODE;
     2278
     2279          if (!add_prefix (prefix))
     2280            return 0;
     2281        }
     2282
     2283      /* Set mode64 for an operand.  */
     2284      if (i.suffix == QWORD_MNEM_SUFFIX
     2285          && flag_code == CODE_64BIT
     2286          && (i.tm.opcode_modifier & NoRex64) == 0)
     2287        i.rex |= REX_MODE64;
     2288
     2289      /* Size floating point instruction.  */
     2290      if (i.suffix == LONG_MNEM_SUFFIX)
     2291        {
     2292          if (i.tm.opcode_modifier & FloatMF)
     2293            i.tm.base_opcode ^= 4;
     2294        }
     2295    }
     2296
     2297  return 1;
     2298}
     2299
     2300static int
     2301check_byte_reg ()
     2302{
     2303  int op;
     2304  for (op = i.operands; --op >= 0;)
     2305    {
     2306      /* If this is an eight bit register, it's OK.  If it's the 16 or
     2307         32 bit version of an eight bit register, we will just use the
     2308         low portion, and that's OK too.  */
     2309      if (i.types[op] & Reg8)
     2310        continue;
     2311
     2312      /* movzx and movsx should not generate this warning.  */
     2313      if (intel_syntax
     2314          && (i.tm.base_opcode == 0xfb7
     2315              || i.tm.base_opcode == 0xfb6
     2316              || i.tm.base_opcode == 0x63
     2317              || i.tm.base_opcode == 0xfbe
     2318              || i.tm.base_opcode == 0xfbf))
     2319        continue;
     2320
     2321      if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4
     2322#if 0
     2323          /* Check that the template allows eight bit regs.  This
     2324             kills insns such as `orb $1,%edx', which maybe should be
     2325             allowed.  */
     2326          && (i.tm.operand_types[op] & (Reg8 | InOutPortReg))
     2327#endif
     2328          )
     2329        {
     2330          /* Prohibit these changes in the 64bit mode, since the
     2331             lowering is more complicated.  */
     2332          if (flag_code == CODE_64BIT
     2333              && (i.tm.operand_types[op] & InOutPortReg) == 0)
     2334            {
     2335              as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
     2336                      i.op[op].regs->reg_name,
     2337                      i.suffix);
     2338              return 0;
     2339            }
     2340#if REGISTER_WARNINGS
     2341          if (!quiet_warnings
     2342              && (i.tm.operand_types[op] & InOutPortReg) == 0)
     2343            as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
     2344                     (i.op[op].regs + (i.types[op] & Reg16
     2345                                       ? REGNAM_AL - REGNAM_AX
     2346                                       : REGNAM_AL - REGNAM_EAX))->reg_name,
     2347                     i.op[op].regs->reg_name,
     2348                     i.suffix);
     2349#endif
     2350          continue;
     2351        }
     2352      /* Any other register is bad.  */
     2353      if (i.types[op] & (Reg | RegMMX | RegXMM
     2354                         | SReg2 | SReg3
     2355                         | Control | Debug | Test
     2356                         | FloatReg | FloatAcc))
     2357        {
     2358          as_bad (_("`%%%s' not allowed with `%s%c'"),
     2359                  i.op[op].regs->reg_name,
     2360                  i.tm.name,
     2361                  i.suffix);
     2362          return 0;
     2363        }
     2364    }
     2365  return 1;
     2366}
     2367
     2368static int
     2369check_long_reg ()
     2370{
     2371  int op;
     2372
     2373  for (op = i.operands; --op >= 0;)
     2374    /* Reject eight bit registers, except where the template requires
     2375       them. (eg. movzb)  */
     2376    if ((i.types[op] & Reg8) != 0
     2377        && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
    27742378      {
    2775         int size;
    2776 
    2777         if (i.tm.opcode_modifier & JumpByte)
     2379        as_bad (_("`%%%s' not allowed with `%s%c'"),
     2380                i.op[op].regs->reg_name,
     2381                i.tm.name,
     2382                i.suffix);
     2383        return 0;
     2384      }
     2385  /* Warn if the e prefix on a general reg is missing.  */
     2386    else if ((!quiet_warnings || flag_code == CODE_64BIT)
     2387             && (i.types[op] & Reg16) != 0
     2388             && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
     2389      {
     2390        /* Prohibit these changes in the 64bit mode, since the
     2391           lowering is more complicated.  */
     2392        if (flag_code == CODE_64BIT)
    27782393          {
    2779             /* This is a loop or jecxz type instruction.  */
    2780             size = 1;
    2781             if (i.prefix[ADDR_PREFIX])
    2782               {
    2783                 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
    2784                 i.prefixes -= 1;
    2785               }
    2786             /* Pentium4 branch hints.  */
    2787             if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
    2788                 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
    2789               {
    2790                 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
    2791                 i.prefixes--;
    2792               }
     2394            as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
     2395                    i.op[op].regs->reg_name,
     2396                    i.suffix);
     2397            return 0;
     2398          }
     2399#if REGISTER_WARNINGS
     2400        else
     2401          as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
     2402                   (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
     2403                   i.op[op].regs->reg_name,
     2404                   i.suffix);
     2405#endif
     2406      }
     2407  /* Warn if the r prefix on a general reg is missing.  */
     2408    else if ((i.types[op] & Reg64) != 0
     2409             && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
     2410      {
     2411        as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
     2412                i.op[op].regs->reg_name,
     2413                i.suffix);
     2414        return 0;
     2415      }
     2416  return 1;
     2417}
     2418
     2419static int
     2420check_qword_reg ()
     2421{
     2422  int op;
     2423
     2424  for (op = i.operands; --op >= 0; )
     2425    /* Reject eight bit registers, except where the template requires
     2426       them. (eg. movzb)  */
     2427    if ((i.types[op] & Reg8) != 0
     2428        && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
     2429      {
     2430        as_bad (_("`%%%s' not allowed with `%s%c'"),
     2431                i.op[op].regs->reg_name,
     2432                i.tm.name,
     2433                i.suffix);
     2434        return 0;
     2435      }
     2436  /* Warn if the e prefix on a general reg is missing.  */
     2437    else if (((i.types[op] & Reg16) != 0
     2438              || (i.types[op] & Reg32) != 0)
     2439             && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
     2440      {
     2441        /* Prohibit these changes in the 64bit mode, since the
     2442           lowering is more complicated.  */
     2443        as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
     2444                i.op[op].regs->reg_name,
     2445                i.suffix);
     2446        return 0;
     2447      }
     2448  return 1;
     2449}
     2450
     2451static int
     2452check_word_reg ()
     2453{
     2454  int op;
     2455  for (op = i.operands; --op >= 0;)
     2456    /* Reject eight bit registers, except where the template requires
     2457       them. (eg. movzb)  */
     2458    if ((i.types[op] & Reg8) != 0
     2459        && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
     2460      {
     2461        as_bad (_("`%%%s' not allowed with `%s%c'"),
     2462                i.op[op].regs->reg_name,
     2463                i.tm.name,
     2464                i.suffix);
     2465        return 0;
     2466      }
     2467  /* Warn if the e prefix on a general reg is present.  */
     2468    else if ((!quiet_warnings || flag_code == CODE_64BIT)
     2469             && (i.types[op] & Reg32) != 0
     2470             && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
     2471      {
     2472        /* Prohibit these changes in the 64bit mode, since the
     2473           lowering is more complicated.  */
     2474        if (flag_code == CODE_64BIT)
     2475          {
     2476            as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
     2477                    i.op[op].regs->reg_name,
     2478                    i.suffix);
     2479            return 0;
    27932480          }
    27942481        else
    2795           {
    2796             int code16;
    2797 
    2798             code16 = 0;
    2799             if (flag_code == CODE_16BIT)
    2800               code16 = CODE16;
    2801 
    2802             if (i.prefix[DATA_PREFIX])
    2803               {
    2804                 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
    2805                 i.prefixes -= 1;
    2806                 code16 ^= CODE16;
    2807               }
    2808 
    2809             size = 4;
    2810             if (code16)
    2811               size = 2;
    2812           }
    2813 
    2814         if (i.prefix[REX_PREFIX])
    2815           {
    2816             FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
    2817             i.prefixes -= 1;
    2818           }
    2819 
    2820         if (i.prefixes != 0 && !intel_syntax)
    2821           as_warn (_("skipping prefixes on this instruction"));
    2822 
    2823         p = frag_more (1 + size);
    2824         *p++ = i.tm.base_opcode;
    2825 
    2826         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
    2827                      i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
     2482#if REGISTER_WARNINGS
     2483          as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
     2484                   (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
     2485                   i.op[op].regs->reg_name,
     2486                   i.suffix);
     2487#endif
    28282488      }
    2829     else if (i.tm.opcode_modifier & JumpInterSegment)
    2830       {
    2831         int size;
    2832         int prefix;
    2833         int code16;
    2834 
    2835         code16 = 0;
    2836         if (flag_code == CODE_16BIT)
    2837           code16 = CODE16;
    2838 
    2839         prefix = 0;
    2840         if (i.prefix[DATA_PREFIX])
    2841           {
    2842             prefix = 1;
    2843             i.prefixes -= 1;
    2844             code16 ^= CODE16;
    2845           }
    2846         if (i.prefix[REX_PREFIX])
    2847           {
    2848             prefix++;
    2849             i.prefixes -= 1;
    2850           }
    2851 
    2852         size = 4;
    2853         if (code16)
    2854           size = 2;
    2855 
    2856         if (i.prefixes != 0 && !intel_syntax)
    2857           as_warn (_("skipping prefixes on this instruction"));
    2858 
    2859         /* 1 opcode; 2 segment; offset  */
    2860         p = frag_more (prefix + 1 + 2 + size);
    2861 
    2862         if (i.prefix[DATA_PREFIX])
    2863           *p++ = DATA_PREFIX_OPCODE;
    2864 
    2865         if (i.prefix[REX_PREFIX])
    2866           *p++ = i.prefix[REX_PREFIX];
    2867 
    2868         *p++ = i.tm.base_opcode;
    2869         if (i.op[1].imms->X_op == O_constant)
    2870           {
    2871             offsetT n = i.op[1].imms->X_add_number;
    2872 
    2873             if (size == 2
    2874                 && !fits_in_unsigned_word (n)
    2875                 && !fits_in_signed_word (n))
    2876               {
    2877                 as_bad (_("16-bit jump out of range"));
    2878                 return;
    2879               }
    2880             md_number_to_chars (p, n, size);
    2881           }
    2882         else
    2883           fix_new_exp (frag_now, p - frag_now->fr_literal, size,
    2884                        i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
    2885         if (i.op[0].imms->X_op != O_constant)
    2886           as_bad (_("can't handle non absolute segment in `%s'"),
    2887                   i.tm.name);
    2888         md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
    2889       }
    2890     else
    2891       {
    2892         /* Output normal instructions here.  */
    2893         unsigned char *q;
    2894 
    2895         /* All opcodes on i386 have eighter 1 or 2 bytes.  We may use third
    2896            byte for the SSE instructions to specify prefix they require.  */
    2897         if (i.tm.base_opcode & 0xff0000)
    2898           add_prefix ((i.tm.base_opcode >> 16) & 0xff);
    2899 
    2900         /* The prefix bytes.  */
    2901         for (q = i.prefix;
    2902              q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
    2903              q++)
    2904           {
    2905             if (*q)
    2906               {
    2907                 p = frag_more (1);
    2908                 md_number_to_chars (p, (valueT) *q, 1);
    2909               }
    2910           }
    2911 
    2912         /* Now the opcode; be careful about word order here!  */
    2913         if (fits_in_unsigned_byte (i.tm.base_opcode))
    2914           {
    2915             FRAG_APPEND_1_CHAR (i.tm.base_opcode);
    2916           }
    2917         else
    2918           {
    2919             p = frag_more (2);
    2920             /* Put out high byte first: can't use md_number_to_chars!  */
    2921             *p++ = (i.tm.base_opcode >> 8) & 0xff;
    2922             *p = i.tm.base_opcode & 0xff;
    2923           }
    2924 
    2925         /* Now the modrm byte and sib byte (if present).  */
    2926         if (i.tm.opcode_modifier & Modrm)
    2927           {
    2928             p = frag_more (1);
    2929             md_number_to_chars (p,
    2930                                 (valueT) (i.rm.regmem << 0
    2931                                           | i.rm.reg << 3
    2932                                           | i.rm.mode << 6),
    2933                                 1);
    2934             /* If i.rm.regmem == ESP (4)
    2935                && i.rm.mode != (Register mode)
    2936                && not 16 bit
    2937                ==> need second modrm byte.  */
    2938             if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
    2939                 && i.rm.mode != 3
    2940                 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
    2941               {
    2942                 p = frag_more (1);
    2943                 md_number_to_chars (p,
    2944                                     (valueT) (i.sib.base << 0
    2945                                               | i.sib.index << 3
    2946                                               | i.sib.scale << 6),
    2947                                     1);
    2948               }
    2949           }
    2950 
    2951         if (i.disp_operands)
    2952           {
    2953             register unsigned int n;
    2954 
    2955             for (n = 0; n < i.operands; n++)
    2956               {
    2957                 if (i.types[n] & Disp)
    2958                   {
    2959                     if (i.op[n].disps->X_op == O_constant)
     2489  return 1;
     2490}
     2491
     2492static int
     2493finalize_imm ()
     2494{
     2495  unsigned int overlap0, overlap1, overlap2;
     2496
     2497  overlap0 = i.types[0] & i.tm.operand_types[0];
     2498  if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S))
     2499      && overlap0 != Imm8 && overlap0 != Imm8S
     2500      && overlap0 != Imm16 && overlap0 != Imm32S
     2501      && overlap0 != Imm32 && overlap0 != Imm64)
     2502    {
     2503      if (i.suffix)
     2504        {
     2505          overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
     2506                       ? Imm8 | Imm8S
     2507                       : (i.suffix == WORD_MNEM_SUFFIX
     2508                          ? Imm16
     2509                          : (i.suffix == QWORD_MNEM_SUFFIX
     2510                             ? Imm64 | Imm32S
     2511                             : Imm32)));
     2512        }
     2513      else if (overlap0 == (Imm16 | Imm32S | Imm32)
     2514               || overlap0 == (Imm16 | Imm32)
     2515               || overlap0 == (Imm16 | Imm32S))
     2516        {
     2517          overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
     2518                      ? Imm16 : Imm32S);
     2519        }
     2520      if (overlap0 != Imm8 && overlap0 != Imm8S
     2521          && overlap0 != Imm16 && overlap0 != Imm32S
     2522          && overlap0 != Imm32 && overlap0 != Imm64)
     2523        {
     2524          as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
     2525          return 0;
     2526        }
     2527    }
     2528  i.types[0] = overlap0;
     2529
     2530  overlap1 = i.types[1] & i.tm.operand_types[1];
     2531  if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32))
     2532      && overlap1 != Imm8 && overlap1 != Imm8S
     2533      && overlap1 != Imm16 && overlap1 != Imm32S
     2534      && overlap1 != Imm32 && overlap1 != Imm64)
     2535    {
     2536      if (i.suffix)
     2537        {
     2538          overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
     2539                       ? Imm8 | Imm8S
     2540                       : (i.suffix == WORD_MNEM_SUFFIX
     2541                          ? Imm16
     2542                          : (i.suffix == QWORD_MNEM_SUFFIX
     2543                             ? Imm64 | Imm32S
     2544                             : Imm32)));
     2545        }
     2546      else if (overlap1 == (Imm16 | Imm32 | Imm32S)
     2547               || overlap1 == (Imm16 | Imm32)
     2548               || overlap1 == (Imm16 | Imm32S))
     2549        {
     2550          overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
     2551                      ? Imm16 : Imm32S);
     2552        }
     2553      if (overlap1 != Imm8 && overlap1 != Imm8S
     2554          && overlap1 != Imm16 && overlap1 != Imm32S
     2555          && overlap1 != Imm32 && overlap1 != Imm64)
     2556        {
     2557          as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
     2558          return 0;
     2559        }
     2560    }
     2561  i.types[1] = overlap1;
     2562
     2563  overlap2 = i.types[2] & i.tm.operand_types[2];
     2564  assert ((overlap2 & Imm) == 0);
     2565  i.types[2] = overlap2;
     2566
     2567  return 1;
     2568}
     2569
     2570static int
     2571process_operands ()
     2572{
     2573  /* Default segment register this instruction will use for memory
     2574     accesses.  0 means unknown.  This is only for optimizing out
     2575     unnecessary segment overrides.  */
     2576  const seg_entry *default_seg = 0;
     2577
     2578  /* The imul $imm, %reg instruction is converted into
     2579     imul $imm, %reg, %reg, and the clr %reg instruction
     2580     is converted into xor %reg, %reg.  */
     2581  if (i.tm.opcode_modifier & regKludge)
     2582    {
     2583      unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
     2584      /* Pretend we saw the extra register operand.  */
     2585      assert (i.op[first_reg_op + 1].regs == 0);
     2586      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
     2587      i.types[first_reg_op + 1] = i.types[first_reg_op];
     2588      i.reg_operands = 2;
     2589    }
     2590
     2591  if (i.tm.opcode_modifier & ShortForm)
     2592    {
     2593      /* The register or float register operand is in operand 0 or 1.  */
     2594      unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
     2595      /* Register goes in low 3 bits of opcode.  */
     2596      i.tm.base_opcode |= i.op[op].regs->reg_num;
     2597      if ((i.op[op].regs->reg_flags & RegRex) != 0)
     2598        i.rex |= REX_EXTZ;
     2599      if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
     2600        {
     2601          /* Warn about some common errors, but press on regardless.
     2602             The first case can be generated by gcc (<= 2.8.1).  */
     2603          if (i.operands == 2)
     2604            {
     2605              /* Reversed arguments on faddp, fsubp, etc.  */
     2606              as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
     2607                       i.op[1].regs->reg_name,
     2608                       i.op[0].regs->reg_name);
     2609            }
     2610          else
     2611            {
     2612              /* Extraneous `l' suffix on fp insn.  */
     2613              as_warn (_("translating to `%s %%%s'"), i.tm.name,
     2614                       i.op[0].regs->reg_name);
     2615            }
     2616        }
     2617    }
     2618  else if (i.tm.opcode_modifier & Modrm)
     2619    {
     2620      /* The opcode is completed (modulo i.tm.extension_opcode which
     2621         must be put into the modrm byte).  Now, we make the modrm and
     2622         index base bytes based on all the info we've collected.  */
     2623
     2624      default_seg = build_modrm_byte ();
     2625    }
     2626  else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
     2627    {
     2628      if (i.tm.base_opcode == POP_SEG_SHORT
     2629          && i.op[0].regs->reg_num == 1)
     2630        {
     2631          as_bad (_("you can't `pop %%cs'"));
     2632          return 0;
     2633        }
     2634      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
     2635      if ((i.op[0].regs->reg_flags & RegRex) != 0)
     2636        i.rex |= REX_EXTZ;
     2637    }
     2638  else if ((i.tm.base_opcode & ~(D | W)) == MOV_AX_DISP32)
     2639    {
     2640      default_seg = &ds;
     2641    }
     2642  else if ((i.tm.opcode_modifier & IsString) != 0)
     2643    {
     2644      /* For the string instructions that allow a segment override
     2645         on one of their operands, the default segment is ds.  */
     2646      default_seg = &ds;
     2647    }
     2648
     2649  if (i.tm.base_opcode == 0x8d /* lea */ && i.seg[0] && !quiet_warnings)
     2650    as_warn (_("segment override on `lea' is ineffectual"));
     2651
     2652  /* If a segment was explicitly specified, and the specified segment
     2653     is not the default, use an opcode prefix to select it.  If we
     2654     never figured out what the default segment is, then default_seg
     2655     will be zero at this point, and the specified segment prefix will
     2656     always be used.  */
     2657  if ((i.seg[0]) && (i.seg[0] != default_seg))
     2658    {
     2659      if (!add_prefix (i.seg[0]->seg_prefix))
     2660        return 0;
     2661    }
     2662  return 1;
     2663}
     2664
     2665static const seg_entry *
     2666build_modrm_byte ()
     2667{
     2668  const seg_entry *default_seg = 0;
     2669
     2670  /* i.reg_operands MUST be the number of real register operands;
     2671     implicit registers do not count.  */
     2672  if (i.reg_operands == 2)
     2673    {
     2674      unsigned int source, dest;
     2675      source = ((i.types[0]
     2676                 & (Reg | RegMMX | RegXMM
     2677                    | SReg2 | SReg3
     2678                    | Control | Debug | Test))
     2679                ? 0 : 1);
     2680      dest = source + 1;
     2681
     2682      i.rm.mode = 3;
     2683      /* One of the register operands will be encoded in the i.tm.reg
     2684         field, the other in the combined i.tm.mode and i.tm.regmem
     2685         fields.  If no form of this instruction supports a memory
     2686         destination operand, then we assume the source operand may
     2687         sometimes be a memory operand and so we need to store the
     2688         destination in the i.rm.reg field.  */
     2689      if ((i.tm.operand_types[dest] & AnyMem) == 0)
     2690        {
     2691          i.rm.reg = i.op[dest].regs->reg_num;
     2692          i.rm.regmem = i.op[source].regs->reg_num;
     2693          if ((i.op[dest].regs->reg_flags & RegRex) != 0)
     2694            i.rex |= REX_EXTX;
     2695          if ((i.op[source].regs->reg_flags & RegRex) != 0)
     2696            i.rex |= REX_EXTZ;
     2697        }
     2698      else
     2699        {
     2700          i.rm.reg = i.op[source].regs->reg_num;
     2701          i.rm.regmem = i.op[dest].regs->reg_num;
     2702          if ((i.op[dest].regs->reg_flags & RegRex) != 0)
     2703            i.rex |= REX_EXTZ;
     2704          if ((i.op[source].regs->reg_flags & RegRex) != 0)
     2705            i.rex |= REX_EXTX;
     2706        }
     2707    }
     2708  else
     2709    {                   /* If it's not 2 reg operands...  */
     2710      if (i.mem_operands)
     2711        {
     2712          unsigned int fake_zero_displacement = 0;
     2713          unsigned int op = ((i.types[0] & AnyMem)
     2714                             ? 0
     2715                             : (i.types[1] & AnyMem) ? 1 : 2);
     2716
     2717          default_seg = &ds;
     2718
     2719          if (i.base_reg == 0)
     2720            {
     2721              i.rm.mode = 0;
     2722              if (!i.disp_operands)
     2723                fake_zero_displacement = 1;
     2724              if (i.index_reg == 0)
     2725                {
     2726                  /* Operand is just <disp>  */
     2727                  if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0)
     2728                      && (flag_code != CODE_64BIT))
     2729                    {
     2730                      i.rm.regmem = NO_BASE_REGISTER_16;
     2731                      i.types[op] &= ~Disp;
     2732                      i.types[op] |= Disp16;
     2733                    }
     2734                  else if (flag_code != CODE_64BIT
     2735                           || (i.prefix[ADDR_PREFIX] != 0))
     2736                    {
     2737                      i.rm.regmem = NO_BASE_REGISTER;
     2738                      i.types[op] &= ~Disp;
     2739                      i.types[op] |= Disp32;
     2740                    }
     2741                  else
     2742                    {
     2743                      /* 64bit mode overwrites the 32bit absolute
     2744                         addressing by RIP relative addressing and
     2745                         absolute addressing is encoded by one of the
     2746                         redundant SIB forms.  */
     2747                      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
     2748                      i.sib.base = NO_BASE_REGISTER;
     2749                      i.sib.index = NO_INDEX_REGISTER;
     2750                      i.types[op] &= ~Disp;
     2751                      i.types[op] |= Disp32S;
     2752                    }
     2753                }
     2754              else /* !i.base_reg && i.index_reg  */
     2755                {
     2756                  i.sib.index = i.index_reg->reg_num;
     2757                  i.sib.base = NO_BASE_REGISTER;
     2758                  i.sib.scale = i.log2_scale_factor;
     2759                  i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
     2760                  i.types[op] &= ~Disp;
     2761                  if (flag_code != CODE_64BIT)
     2762                    i.types[op] |= Disp32;      /* Must be 32 bit */
     2763                  else
     2764                    i.types[op] |= Disp32S;
     2765                  if ((i.index_reg->reg_flags & RegRex) != 0)
     2766                    i.rex |= REX_EXTY;
     2767                }
     2768            }
     2769          /* RIP addressing for 64bit mode.  */
     2770          else if (i.base_reg->reg_type == BaseIndex)
     2771            {
     2772              i.rm.regmem = NO_BASE_REGISTER;
     2773              i.types[op] &= ~Disp;
     2774              i.types[op] |= Disp32S;
     2775              i.flags[op] = Operand_PCrel;
     2776            }
     2777          else if (i.base_reg->reg_type & Reg16)
     2778            {
     2779              switch (i.base_reg->reg_num)
     2780                {
     2781                case 3: /* (%bx)  */
     2782                  if (i.index_reg == 0)
     2783                    i.rm.regmem = 7;
     2784                  else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
     2785                    i.rm.regmem = i.index_reg->reg_num - 6;
     2786                  break;
     2787                case 5: /* (%bp)  */
     2788                  default_seg = &ss;
     2789                  if (i.index_reg == 0)
     2790                    {
     2791                      i.rm.regmem = 6;
     2792                      if ((i.types[op] & Disp) == 0)
     2793                        {
     2794                          /* fake (%bp) into 0(%bp)  */
     2795                          i.types[op] |= Disp8;
     2796                          fake_zero_displacement = 1;
     2797                        }
     2798                    }
     2799                  else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
     2800                    i.rm.regmem = i.index_reg->reg_num - 6 + 2;
     2801                  break;
     2802                default: /* (%si) -> 4 or (%di) -> 5  */
     2803                  i.rm.regmem = i.base_reg->reg_num - 6 + 4;
     2804                }
     2805              i.rm.mode = mode_from_disp_size (i.types[op]);
     2806            }
     2807          else /* i.base_reg and 32/64 bit mode  */
     2808            {
     2809              if (flag_code == CODE_64BIT
     2810                  && (i.types[op] & Disp))
     2811                {
     2812                  if (i.types[op] & Disp8)
     2813                    i.types[op] = Disp8 | Disp32S;
     2814                  else
     2815                    i.types[op] = Disp32S;
     2816                }
     2817              i.rm.regmem = i.base_reg->reg_num;
     2818              if ((i.base_reg->reg_flags & RegRex) != 0)
     2819                i.rex |= REX_EXTZ;
     2820              i.sib.base = i.base_reg->reg_num;
     2821              /* x86-64 ignores REX prefix bit here to avoid decoder
     2822                 complications.  */
     2823              if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
     2824                {
     2825                  default_seg = &ss;
     2826                  if (i.disp_operands == 0)
     2827                    {
     2828                      fake_zero_displacement = 1;
     2829                      i.types[op] |= Disp8;
     2830                    }
     2831                }
     2832              else if (i.base_reg->reg_num == ESP_REG_NUM)
     2833                {
     2834                  default_seg = &ss;
     2835                }
     2836              i.sib.scale = i.log2_scale_factor;
     2837              if (i.index_reg == 0)
     2838                {
     2839                  /* <disp>(%esp) becomes two byte modrm with no index
     2840                     register.  We've already stored the code for esp
     2841                     in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
     2842                     Any base register besides %esp will not use the
     2843                     extra modrm byte.  */
     2844                  i.sib.index = NO_INDEX_REGISTER;
     2845#if !SCALE1_WHEN_NO_INDEX
     2846                  /* Another case where we force the second modrm byte.  */
     2847                  if (i.log2_scale_factor)
     2848                    i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
     2849#endif
     2850                }
     2851              else
     2852                {
     2853                  i.sib.index = i.index_reg->reg_num;
     2854                  i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
     2855                  if ((i.index_reg->reg_flags & RegRex) != 0)
     2856                    i.rex |= REX_EXTY;
     2857                }
     2858              i.rm.mode = mode_from_disp_size (i.types[op]);
     2859            }
     2860
     2861          if (fake_zero_displacement)
     2862            {
     2863              /* Fakes a zero displacement assuming that i.types[op]
     2864                 holds the correct displacement size.  */
     2865              expressionS *exp;
     2866
     2867              assert (i.op[op].disps == 0);
     2868              exp = &disp_expressions[i.disp_operands++];
     2869              i.op[op].disps = exp;
     2870              exp->X_op = O_constant;
     2871              exp->X_add_number = 0;
     2872              exp->X_add_symbol = (symbolS *) 0;
     2873              exp->X_op_symbol = (symbolS *) 0;
     2874            }
     2875        }
     2876
     2877      /* Fill in i.rm.reg or i.rm.regmem field with register operand
     2878         (if any) based on i.tm.extension_opcode.  Again, we must be
     2879         careful to make sure that segment/control/debug/test/MMX
     2880         registers are coded into the i.rm.reg field.  */
     2881      if (i.reg_operands)
     2882        {
     2883          unsigned int op =
     2884            ((i.types[0]
     2885              & (Reg | RegMMX | RegXMM
     2886                 | SReg2 | SReg3
     2887                 | Control | Debug | Test))
     2888             ? 0
     2889             : ((i.types[1]
     2890                 & (Reg | RegMMX | RegXMM
     2891                    | SReg2 | SReg3
     2892                    | Control | Debug | Test))
     2893                ? 1
     2894                : 2));
     2895          /* If there is an extension opcode to put here, the register
     2896             number must be put into the regmem field.  */
     2897          if (i.tm.extension_opcode != None)
     2898            {
     2899              i.rm.regmem = i.op[op].regs->reg_num;
     2900              if ((i.op[op].regs->reg_flags & RegRex) != 0)
     2901                i.rex |= REX_EXTZ;
     2902            }
     2903          else
     2904            {
     2905              i.rm.reg = i.op[op].regs->reg_num;
     2906              if ((i.op[op].regs->reg_flags & RegRex) != 0)
     2907                i.rex |= REX_EXTX;
     2908            }
     2909
     2910          /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
     2911             must set it to 3 to indicate this is a register operand
     2912             in the regmem field.  */
     2913          if (!i.mem_operands)
     2914            i.rm.mode = 3;
     2915        }
     2916
     2917      /* Fill in i.rm.reg field with extension opcode (if any).  */
     2918      if (i.tm.extension_opcode != None)
     2919        i.rm.reg = i.tm.extension_opcode;
     2920    }
     2921  return default_seg;
     2922}
     2923
     2924static void
     2925output_branch ()
     2926{
     2927  char *p;
     2928  int code16;
     2929  int prefix;
     2930  relax_substateT subtype;
     2931  symbolS *sym;
     2932  offsetT off;
     2933
     2934  code16 = 0;
     2935  if (flag_code == CODE_16BIT)
     2936    code16 = CODE16;
     2937
     2938  prefix = 0;
     2939  if (i.prefix[DATA_PREFIX] != 0)
     2940    {
     2941      prefix = 1;
     2942      i.prefixes -= 1;
     2943      code16 ^= CODE16;
     2944    }
     2945  /* Pentium4 branch hints.  */
     2946  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
     2947      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
     2948    {
     2949      prefix++;
     2950      i.prefixes--;
     2951    }
     2952  if (i.prefix[REX_PREFIX] != 0)
     2953    {
     2954      prefix++;
     2955      i.prefixes--;
     2956    }
     2957
     2958  if (i.prefixes != 0 && !intel_syntax)
     2959    as_warn (_("skipping prefixes on this instruction"));
     2960
     2961  /* It's always a symbol;  End frag & setup for relax.
     2962     Make sure there is enough room in this frag for the largest
     2963     instruction we may generate in md_convert_frag.  This is 2
     2964     bytes for the opcode and room for the prefix and largest
     2965     displacement.  */
     2966  frag_grow (prefix + 2 + 4);
     2967  /* Prefix and 1 opcode byte go in fr_fix.  */
     2968  p = frag_more (prefix + 1);
     2969  if (i.prefix[DATA_PREFIX] != 0)
     2970    *p++ = DATA_PREFIX_OPCODE;
     2971  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
     2972      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
     2973    *p++ = i.prefix[SEG_PREFIX];
     2974  if (i.prefix[REX_PREFIX] != 0)
     2975    *p++ = i.prefix[REX_PREFIX];
     2976  *p = i.tm.base_opcode;
     2977
     2978  if ((unsigned char) *p == JUMP_PC_RELATIVE)
     2979    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
     2980  else if ((cpu_arch_flags & Cpu386) != 0)
     2981    subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
     2982  else
     2983    subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
     2984  subtype |= code16;
     2985
     2986  sym = i.op[0].disps->X_add_symbol;
     2987  off = i.op[0].disps->X_add_number;
     2988
     2989  if (i.op[0].disps->X_op != O_constant
     2990      && i.op[0].disps->X_op != O_symbol)
     2991    {
     2992      /* Handle complex expressions.  */
     2993      sym = make_expr_symbol (i.op[0].disps);
     2994      off = 0;
     2995    }
     2996
     2997  /* 1 possible extra opcode + 4 byte displacement go in var part.
     2998     Pass reloc in fr_var.  */
     2999  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
     3000}
     3001
     3002static void
     3003output_jump ()
     3004{
     3005  char *p;
     3006  int size;
     3007  fixS *fixP;
     3008
     3009  if (i.tm.opcode_modifier & JumpByte)
     3010    {
     3011      /* This is a loop or jecxz type instruction.  */
     3012      size = 1;
     3013      if (i.prefix[ADDR_PREFIX] != 0)
     3014        {
     3015          FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
     3016          i.prefixes -= 1;
     3017        }
     3018      /* Pentium4 branch hints.  */
     3019      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
     3020          || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
     3021        {
     3022          FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
     3023          i.prefixes--;
     3024        }
     3025    }
     3026  else
     3027    {
     3028      int code16;
     3029
     3030      code16 = 0;
     3031      if (flag_code == CODE_16BIT)
     3032        code16 = CODE16;
     3033
     3034      if (i.prefix[DATA_PREFIX] != 0)
     3035        {
     3036          FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
     3037          i.prefixes -= 1;
     3038          code16 ^= CODE16;
     3039        }
     3040
     3041      size = 4;
     3042      if (code16)
     3043        size = 2;
     3044    }
     3045
     3046  if (i.prefix[REX_PREFIX] != 0)
     3047    {
     3048      FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
     3049      i.prefixes -= 1;
     3050    }
     3051
     3052  if (i.prefixes != 0 && !intel_syntax)
     3053    as_warn (_("skipping prefixes on this instruction"));
     3054
     3055  p = frag_more (1 + size);
     3056  *p++ = i.tm.base_opcode;
     3057
     3058  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
     3059                      i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
     3060
     3061  /* All jumps handled here are signed, but don't use a signed limit
     3062     check for 32 and 16 bit jumps as we want to allow wrap around at
     3063     4G and 64k respectively.  */
     3064  if (size == 1)
     3065    fixP->fx_signed = 1;
     3066}
     3067
     3068static void
     3069output_interseg_jump ()
     3070{
     3071  char *p;
     3072  int size;
     3073  int prefix;
     3074  int code16;
     3075
     3076  code16 = 0;
     3077  if (flag_code == CODE_16BIT)
     3078    code16 = CODE16;
     3079
     3080  prefix = 0;
     3081  if (i.prefix[DATA_PREFIX] != 0)
     3082    {
     3083      prefix = 1;
     3084      i.prefixes -= 1;
     3085      code16 ^= CODE16;
     3086    }
     3087  if (i.prefix[REX_PREFIX] != 0)
     3088    {
     3089      prefix++;
     3090      i.prefixes -= 1;
     3091    }
     3092
     3093  size = 4;
     3094  if (code16)
     3095    size = 2;
     3096
     3097  if (i.prefixes != 0 && !intel_syntax)
     3098    as_warn (_("skipping prefixes on this instruction"));
     3099
     3100  /* 1 opcode; 2 segment; offset  */
     3101  p = frag_more (prefix + 1 + 2 + size);
     3102
     3103  if (i.prefix[DATA_PREFIX] != 0)
     3104    *p++ = DATA_PREFIX_OPCODE;
     3105
     3106  if (i.prefix[REX_PREFIX] != 0)
     3107    *p++ = i.prefix[REX_PREFIX];
     3108
     3109  *p++ = i.tm.base_opcode;
     3110  if (i.op[1].imms->X_op == O_constant)
     3111    {
     3112      offsetT n = i.op[1].imms->X_add_number;
     3113
     3114      if (size == 2
     3115          && !fits_in_unsigned_word (n)
     3116          && !fits_in_signed_word (n))
     3117        {
     3118          as_bad (_("16-bit jump out of range"));
     3119          return;
     3120        }
     3121      md_number_to_chars (p, n, size);
     3122    }
     3123  else
     3124    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
     3125                 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
     3126  if (i.op[0].imms->X_op != O_constant)
     3127    as_bad (_("can't handle non absolute segment in `%s'"),
     3128            i.tm.name);
     3129  md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
     3130}
     3131
     3132
     3133static void
     3134output_insn ()
     3135{
     3136  fragS *insn_start_frag;
     3137  offsetT insn_start_off;
     3138
     3139  /* Tie dwarf2 debug info to the address at the start of the insn.
     3140     We can't do this after the insn has been output as the current
     3141     frag may have been closed off.  eg. by frag_var.  */
     3142  dwarf2_emit_insn (0);
     3143
     3144  insn_start_frag = frag_now;
     3145  insn_start_off = frag_now_fix ();
     3146
     3147  /* Output jumps.  */
     3148  if (i.tm.opcode_modifier & Jump)
     3149    output_branch ();
     3150  else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
     3151    output_jump ();
     3152  else if (i.tm.opcode_modifier & JumpInterSegment)
     3153    output_interseg_jump ();
     3154  else
     3155    {
     3156      /* Output normal instructions here.  */
     3157      char *p;
     3158      unsigned char *q;
     3159
     3160      /* All opcodes on i386 have either 1 or 2 bytes.  We may use third
     3161         byte for the SSE instructions to specify a prefix they require.  */
     3162      if (i.tm.base_opcode & 0xff0000)
     3163        add_prefix ((i.tm.base_opcode >> 16) & 0xff);
     3164
     3165      /* The prefix bytes.  */
     3166      for (q = i.prefix;
     3167           q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
     3168           q++)
     3169        {
     3170          if (*q)
     3171            {
     3172              p = frag_more (1);
     3173              md_number_to_chars (p, (valueT) *q, 1);
     3174            }
     3175        }
     3176
     3177      /* Now the opcode; be careful about word order here!  */
     3178      if (fits_in_unsigned_byte (i.tm.base_opcode))
     3179        {
     3180          FRAG_APPEND_1_CHAR (i.tm.base_opcode);
     3181        }
     3182      else
     3183        {
     3184          p = frag_more (2);
     3185          /* Put out high byte first: can't use md_number_to_chars!  */
     3186          *p++ = (i.tm.base_opcode >> 8) & 0xff;
     3187          *p = i.tm.base_opcode & 0xff;
     3188        }
     3189
     3190      /* Now the modrm byte and sib byte (if present).  */
     3191      if (i.tm.opcode_modifier & Modrm)
     3192        {
     3193          p = frag_more (1);
     3194          md_number_to_chars (p,
     3195                              (valueT) (i.rm.regmem << 0
     3196                                        | i.rm.reg << 3
     3197                                        | i.rm.mode << 6),
     3198                              1);
     3199          /* If i.rm.regmem == ESP (4)
     3200             && i.rm.mode != (Register mode)
     3201             && not 16 bit
     3202             ==> need second modrm byte.  */
     3203          if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
     3204              && i.rm.mode != 3
     3205              && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
     3206            {
     3207              p = frag_more (1);
     3208              md_number_to_chars (p,
     3209                                  (valueT) (i.sib.base << 0
     3210                                            | i.sib.index << 3
     3211                                            | i.sib.scale << 6),
     3212                                  1);
     3213            }
     3214        }
     3215
     3216      if (i.disp_operands)
     3217        output_disp (insn_start_frag, insn_start_off);
     3218
     3219      if (i.imm_operands)
     3220        output_imm (insn_start_frag, insn_start_off);
     3221    }
     3222
     3223#ifdef DEBUG386
     3224  if (flag_debug)
     3225    {
     3226      pi (line, &i);
     3227    }
     3228#endif /* DEBUG386  */
     3229}
     3230
     3231static void
     3232output_disp (insn_start_frag, insn_start_off)
     3233    fragS *insn_start_frag;
     3234    offsetT insn_start_off;
     3235{
     3236  char *p;
     3237  unsigned int n;
     3238
     3239  for (n = 0; n < i.operands; n++)
     3240    {
     3241      if (i.types[n] & Disp)
     3242        {
     3243          if (i.op[n].disps->X_op == O_constant)
     3244            {
     3245              int size;
     3246              offsetT val;
     3247
     3248              size = 4;
     3249              if (i.types[n] & (Disp8 | Disp16 | Disp64))
     3250                {
     3251                  size = 2;
     3252                  if (i.types[n] & Disp8)
     3253                    size = 1;
     3254                  if (i.types[n] & Disp64)
     3255                    size = 8;
     3256                }
     3257              val = offset_in_range (i.op[n].disps->X_add_number,
     3258                                     size);
     3259              p = frag_more (size);
     3260              md_number_to_chars (p, val, size);
     3261            }
     3262          else
     3263            {
     3264              RELOC_ENUM reloc_type;
     3265              int size = 4;
     3266              int sign = 0;
     3267              int pcrel = (i.flags[n] & Operand_PCrel) != 0;
     3268
     3269              /* The PC relative address is computed relative
     3270                 to the instruction boundary, so in case immediate
     3271                 fields follows, we need to adjust the value.  */
     3272              if (pcrel && i.imm_operands)
     3273                {
     3274                  int imm_size = 4;
     3275                  unsigned int n1;
     3276
     3277                  for (n1 = 0; n1 < i.operands; n1++)
     3278                    if (i.types[n1] & Imm)
    29603279                      {
    2961                         int size;
    2962                         offsetT val;
    2963 
    2964                         size = 4;
    2965                         if (i.types[n] & (Disp8 | Disp16 | Disp64))
     3280                        if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
    29663281                          {
    2967                             size = 2;
    2968                             if (i.types[n] & Disp8)
    2969                               size = 1;
    2970                             if (i.types[n] & Disp64)
    2971                               size = 8;
     3282                            imm_size = 2;
     3283                            if (i.types[n1] & (Imm8 | Imm8S))
     3284                              imm_size = 1;
     3285                            if (i.types[n1] & Imm64)
     3286                              imm_size = 8;
    29723287                          }
    2973                         val = offset_in_range (i.op[n].disps->X_add_number,
    2974                                                size);
    2975                         p = frag_more (size);
    2976                         md_number_to_chars (p, val, size);
     3288                        break;
    29773289                      }
    2978                     else
    2979                       {
    2980                         int size = 4;
    2981                         int sign = 0;
    2982                         int pcrel = (i.flags[n] & Operand_PCrel) != 0;
    2983 
    2984                         /* The PC relative address is computed relative
    2985                            to the instruction boundary, so in case immediate
    2986                            fields follows, we need to adjust the value.  */
    2987                         if (pcrel && i.imm_operands)
    2988                           {
    2989                             int imm_size = 4;
    2990                             register unsigned int n1;
    2991 
    2992                             for (n1 = 0; n1 < i.operands; n1++)
    2993                               if (i.types[n1] & Imm)
    2994                                 {
    2995                                   if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
    2996                                     {
    2997                                       imm_size = 2;
    2998                                       if (i.types[n1] & (Imm8 | Imm8S))
    2999                                         imm_size = 1;
    3000                                       if (i.types[n1] & Imm64)
    3001                                         imm_size = 8;
    3002                                     }
    3003                                   break;
    3004                                 }
    3005                             /* We should find the immediate.  */
    3006                             if (n1 == i.operands)
    3007                               abort ();
    3008                             i.op[n].disps->X_add_number -= imm_size;
    3009                           }
    3010 
    3011                         if (i.types[n] & Disp32S)
    3012                           sign = 1;
    3013 
    3014                         if (i.types[n] & (Disp16 | Disp64))
    3015                           {
    3016                             size = 2;
    3017                             if (i.types[n] & Disp64)
    3018                               size = 8;
    3019                           }
    3020 
    3021                         p = frag_more (size);
    3022                         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
    3023                                      i.op[n].disps, pcrel,
    3024                                      reloc (size, pcrel, sign, i.reloc[n]));
    3025                       }
    3026                   }
    3027               }
    3028           }
    3029 
    3030         /* Output immediate.  */
    3031         if (i.imm_operands)
    3032           {
    3033             register unsigned int n;
    3034 
    3035             for (n = 0; n < i.operands; n++)
    3036               {
    3037                 if (i.types[n] & Imm)
    3038                   {
    3039                     if (i.op[n].imms->X_op == O_constant)
    3040                       {
    3041                         int size;
    3042                         offsetT val;
    3043 
    3044                         size = 4;
    3045                         if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
    3046                           {
    3047                             size = 2;
    3048                             if (i.types[n] & (Imm8 | Imm8S))
    3049                               size = 1;
    3050                             else if (i.types[n] & Imm64)
    3051                               size = 8;
    3052                           }
    3053                         val = offset_in_range (i.op[n].imms->X_add_number,
    3054                                                size);
    3055                         p = frag_more (size);
    3056                         md_number_to_chars (p, val, size);
    3057                       }
    3058                     else
    3059                       {
    3060                         /* Not absolute_section.
    3061                            Need a 32-bit fixup (don't support 8bit
    3062                            non-absolute imms).  Try to support other
    3063                            sizes ...  */
    3064                         RELOC_ENUM reloc_type;
    3065                         int size = 4;
    3066                         int sign = 0;
    3067 
    3068                         if ((i.types[n] & (Imm32S))
    3069                             && i.suffix == QWORD_MNEM_SUFFIX)
    3070                           sign = 1;
    3071                         if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
    3072                           {
    3073                             size = 2;
    3074                             if (i.types[n] & (Imm8 | Imm8S))
    3075                               size = 1;
    3076                             if (i.types[n] & Imm64)
    3077                               size = 8;
    3078                           }
    3079 
    3080                         p = frag_more (size);
    3081                         reloc_type = reloc (size, 0, sign, i.reloc[n]);
     3290                  /* We should find the immediate.  */
     3291                  if (n1 == i.operands)
     3292                    abort ();
     3293                  i.op[n].disps->X_add_number -= imm_size;
     3294                }
     3295
     3296              if (i.types[n] & Disp32S)
     3297                sign = 1;
     3298
     3299              if (i.types[n] & (Disp16 | Disp64))
     3300                {
     3301                  size = 2;
     3302                  if (i.types[n] & Disp64)
     3303                    size = 8;
     3304                }
     3305
     3306              p = frag_more (size);
     3307              reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
    30823308#ifdef BFD_ASSEMBLER
    3083                         if (reloc_type == BFD_RELOC_32
    3084                             && GOT_symbol
    3085                             && GOT_symbol == i.op[n].imms->X_add_symbol
    3086                             && (i.op[n].imms->X_op == O_symbol
    3087                                 || (i.op[n].imms->X_op == O_add
    3088                                     && ((symbol_get_value_expression
    3089                                          (i.op[n].imms->X_op_symbol)->X_op)
    3090                                         == O_subtract))))
    3091                           {
    3092                             /* We don't support dynamic linking on x86-64 yet.  */
    3093                             if (flag_code == CODE_64BIT)
    3094                               abort ();
    3095                             reloc_type = BFD_RELOC_386_GOTPC;
    3096                             i.op[n].imms->X_add_number += 3;
    3097                           }
    3098 #endif
    3099                         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
    3100                                      i.op[n].imms, 0, reloc_type);
    3101                       }
    3102                   }
    3103               }
    3104           }
    3105       }
    3106 
    3107 #ifdef DEBUG386
    3108     if (flag_debug)
    3109       {
    3110         pi (line, &i);
    3111       }
    3112 #endif /* DEBUG386  */
    3113   }
     3309              if (reloc_type == BFD_RELOC_32
     3310                  && GOT_symbol
     3311                  && GOT_symbol == i.op[n].disps->X_add_symbol
     3312                  && (i.op[n].disps->X_op == O_symbol
     3313                      || (i.op[n].disps->X_op == O_add
     3314                          && ((symbol_get_value_expression
     3315                               (i.op[n].disps->X_op_symbol)->X_op)
     3316                              == O_subtract))))
     3317                {
     3318                  offsetT add;
     3319
     3320                  if (insn_start_frag == frag_now)
     3321                    add = (p - frag_now->fr_literal) - insn_start_off;
     3322                  else
     3323                    {
     3324                      fragS *fr;
     3325
     3326                      add = insn_start_frag->fr_fix - insn_start_off;
     3327                      for (fr = insn_start_frag->fr_next;
     3328                           fr && fr != frag_now; fr = fr->fr_next)
     3329                        add += fr->fr_fix;
     3330                      add += p - frag_now->fr_literal;
     3331                    }
     3332
     3333                  /* We don't support dynamic linking on x86-64 yet.  */
     3334                  if (flag_code == CODE_64BIT)
     3335                    abort ();
     3336                  reloc_type = BFD_RELOC_386_GOTPC;
     3337                  i.op[n].disps->X_add_number += add;
     3338                }
     3339#endif
     3340              fix_new_exp (frag_now, p - frag_now->fr_literal, size,
     3341                           i.op[n].disps, pcrel, reloc_type);
     3342            }
     3343        }
     3344    }
     3345}
     3346
     3347static void
     3348output_imm (insn_start_frag, insn_start_off)
     3349    fragS *insn_start_frag;
     3350    offsetT insn_start_off;
     3351{
     3352  char *p;
     3353  unsigned int n;
     3354
     3355  for (n = 0; n < i.operands; n++)
     3356    {
     3357      if (i.types[n] & Imm)
     3358        {
     3359          if (i.op[n].imms->X_op == O_constant)
     3360            {
     3361              int size;
     3362              offsetT val;
     3363
     3364              size = 4;
     3365              if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
     3366                {
     3367                  size = 2;
     3368                  if (i.types[n] & (Imm8 | Imm8S))
     3369                    size = 1;
     3370                  else if (i.types[n] & Imm64)
     3371                    size = 8;
     3372                }
     3373              val = offset_in_range (i.op[n].imms->X_add_number,
     3374                                     size);
     3375              p = frag_more (size);
     3376              md_number_to_chars (p, val, size);
     3377            }
     3378          else
     3379            {
     3380              /* Not absolute_section.
     3381                 Need a 32-bit fixup (don't support 8bit
     3382                 non-absolute imms).  Try to support other
     3383                 sizes ...  */
     3384              RELOC_ENUM reloc_type;
     3385              int size = 4;
     3386              int sign = 0;
     3387
     3388              if ((i.types[n] & (Imm32S))
     3389                  && i.suffix == QWORD_MNEM_SUFFIX)
     3390                sign = 1;
     3391              if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
     3392                {
     3393                  size = 2;
     3394                  if (i.types[n] & (Imm8 | Imm8S))
     3395                    size = 1;
     3396                  if (i.types[n] & Imm64)
     3397                    size = 8;
     3398                }
     3399
     3400              p = frag_more (size);
     3401              reloc_type = reloc (size, 0, sign, i.reloc[n]);
     3402#ifdef BFD_ASSEMBLER
     3403              /*   This is tough to explain.  We end up with this one if we
     3404               * have operands that look like
     3405               * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
     3406               * obtain the absolute address of the GOT, and it is strongly
     3407               * preferable from a performance point of view to avoid using
     3408               * a runtime relocation for this.  The actual sequence of
     3409               * instructions often look something like:
     3410               *
     3411               *        call    .L66
     3412               * .L66:
     3413               *        popl    %ebx
     3414               *        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
     3415               *
     3416               *   The call and pop essentially return the absolute address
     3417               * of the label .L66 and store it in %ebx.  The linker itself
     3418               * will ultimately change the first operand of the addl so
     3419               * that %ebx points to the GOT, but to keep things simple, the
     3420               * .o file must have this operand set so that it generates not
     3421               * the absolute address of .L66, but the absolute address of
     3422               * itself.  This allows the linker itself simply treat a GOTPC
     3423               * relocation as asking for a pcrel offset to the GOT to be
     3424               * added in, and the addend of the relocation is stored in the
     3425               * operand field for the instruction itself.
     3426               *
     3427               *   Our job here is to fix the operand so that it would add
     3428               * the correct offset so that %ebx would point to itself.  The
     3429               * thing that is tricky is that .-.L66 will point to the
     3430               * beginning of the instruction, so we need to further modify
     3431               * the operand so that it will point to itself.  There are
     3432               * other cases where you have something like:
     3433               *
     3434               *        .long   $_GLOBAL_OFFSET_TABLE_+[.-.L66]
     3435               *
     3436               * and here no correction would be required.  Internally in
     3437               * the assembler we treat operands of this form as not being
     3438               * pcrel since the '.' is explicitly mentioned, and I wonder
     3439               * whether it would simplify matters to do it this way.  Who
     3440               * knows.  In earlier versions of the PIC patches, the
     3441               * pcrel_adjust field was used to store the correction, but
     3442               * since the expression is not pcrel, I felt it would be
     3443               * confusing to do it this way.  */
     3444
     3445              if (reloc_type == BFD_RELOC_32
     3446                  && GOT_symbol
     3447                  && GOT_symbol == i.op[n].imms->X_add_symbol
     3448                  && (i.op[n].imms->X_op == O_symbol
     3449                      || (i.op[n].imms->X_op == O_add
     3450                          && ((symbol_get_value_expression
     3451                               (i.op[n].imms->X_op_symbol)->X_op)
     3452                              == O_subtract))))
     3453                {
     3454                  offsetT add;
     3455
     3456                  if (insn_start_frag == frag_now)
     3457                    add = (p - frag_now->fr_literal) - insn_start_off;
     3458                  else
     3459                    {
     3460                      fragS *fr;
     3461
     3462                      add = insn_start_frag->fr_fix - insn_start_off;
     3463                      for (fr = insn_start_frag->fr_next;
     3464                           fr && fr != frag_now; fr = fr->fr_next)
     3465                        add += fr->fr_fix;
     3466                      add += p - frag_now->fr_literal;
     3467                    }
     3468
     3469                  /* We don't support dynamic linking on x86-64 yet.  */
     3470                  if (flag_code == CODE_64BIT)
     3471                    abort ();
     3472                  reloc_type = BFD_RELOC_386_GOTPC;
     3473                  i.op[n].imms->X_add_number += add;
     3474                }
     3475#endif
     3476              fix_new_exp (frag_now, p - frag_now->fr_literal, size,
     3477                           i.op[n].imms, 0, reloc_type);
     3478            }
     3479        }
     3480    }
    31143481}
    31153482
     
    31373504    const RELOC_ENUM rel[NUM_FLAG_CODE];
    31383505  } gotrel[] = {
    3139     { "PLT",      { BFD_RELOC_386_PLT32,  0, BFD_RELOC_X86_64_PLT32    } },
    3140     { "GOTOFF",   { BFD_RELOC_386_GOTOFF, 0, 0                         } },
    3141     { "GOTPCREL", { 0,                    0, BFD_RELOC_X86_64_GOTPCREL } },
    3142     { "GOT",      { BFD_RELOC_386_GOT32,  0, BFD_RELOC_X86_64_GOT32    } }
     3506    { "PLT",      { BFD_RELOC_386_PLT32,      0, BFD_RELOC_X86_64_PLT32    } },
     3507    { "GOTOFF",   { BFD_RELOC_386_GOTOFF,     0, 0                         } },
     3508    { "GOTPCREL", { 0,                        0, BFD_RELOC_X86_64_GOTPCREL } },
     3509    { "TLSGD",    { BFD_RELOC_386_TLS_GD,     0, BFD_RELOC_X86_64_TLSGD    } },
     3510    { "TLSLDM",   { BFD_RELOC_386_TLS_LDM,    0, 0                         } },
     3511    { "TLSLD",    { 0,                        0, BFD_RELOC_X86_64_TLSLD    } },
     3512    { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32,  0, BFD_RELOC_X86_64_GOTTPOFF } },
     3513    { "TPOFF",    { BFD_RELOC_386_TLS_LE_32,  0, BFD_RELOC_X86_64_TPOFF32  } },
     3514    { "NTPOFF",   { BFD_RELOC_386_TLS_LE,     0, 0                         } },
     3515    { "DTPOFF",   { BFD_RELOC_386_TLS_LDO_32, 0, BFD_RELOC_X86_64_DTPOFF32 } },
     3516    { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE,  0, 0                         } },
     3517    { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE,     0, 0                         } },
     3518    { "GOT",      { BFD_RELOC_386_GOT32,      0, BFD_RELOC_X86_64_GOT32    } }
    31433519  };
    31443520  char *cp;
     
    31543530
    31553531      len = strlen (gotrel[j].str);
    3156       if (strncmp (cp + 1, gotrel[j].str, len) == 0)
     3532      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
    31573533        {
    31583534          if (gotrel[j].rel[(unsigned int) flag_code] != 0)
    31593535            {
    3160               int first;
    3161               char *tmpbuf;
     3536              int first, second;
     3537              char *tmpbuf, *past_reloc;
    31623538
    31633539              *reloc = gotrel[j].rel[(unsigned int) flag_code];
     3540              if (adjust)
     3541                *adjust = len;
    31643542
    31653543              if (GOT_symbol == NULL)
     
    31683546              /* Replace the relocation token with ' ', so that
    31693547                 errors like foo@GOTOFF1 will be detected.  */
     3548
     3549              /* The length of the first part of our input line.  */
    31703550              first = cp - input_line_pointer;
    3171               tmpbuf = xmalloc (strlen (input_line_pointer));
     3551
     3552              /* The second part goes from after the reloc token until
     3553                 (and including) an end_of_line char.  Don't use strlen
     3554                 here as the end_of_line char may not be a NUL.  */
     3555              past_reloc = cp + 1 + len;
     3556              for (cp = past_reloc; !is_end_of_line[(unsigned char) *cp++]; )
     3557                ;
     3558              second = cp - past_reloc;
     3559
     3560              /* Allocate and copy string.  The trailing NUL shouldn't
     3561                 be necessary, but be safe.  */
     3562              tmpbuf = xmalloc (first + second + 2);
    31723563              memcpy (tmpbuf, input_line_pointer, first);
    31733564              tmpbuf[first] = ' ';
    3174               strcpy (tmpbuf + first + 1, cp + 1 + len);
    3175               if (adjust)
    3176                 *adjust = len;
     3565              memcpy (tmpbuf + first + 1, past_reloc, second);
     3566              tmpbuf[first + second + 1] = '\0';
    31773567              return tmpbuf;
    31783568            }
     
    33093699           && OUTPUT_FLAVOR == bfd_target_aout_flavour
    33103700#endif
     3701           && exp_seg != absolute_section
    33113702           && exp_seg != text_section
    33123703           && exp_seg != data_section
     
    33703761      return NULL;
    33713762    }
    3372   if (i.log2_scale_factor != 0 && ! i.index_reg)
     3763  if (i.log2_scale_factor != 0 && i.index_reg == 0)
    33733764    {
    33743765      as_warn (_("scale factor of %d without an index register"),
     
    33903781     char *disp_end;
    33913782{
    3392   register expressionS *exp;
     3783  expressionS *exp;
    33933784  segT exp_seg = 0;
    33943785  char *save_input_line_pointer;
     
    33983789  int bigdisp = Disp32;
    33993790
    3400   if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
     3791  if (flag_code == CODE_64BIT)
     3792    {
     3793      if (i.prefix[ADDR_PREFIX] == 0)
     3794        bigdisp = Disp64;
     3795    }
     3796  else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
    34013797    bigdisp = Disp16;
    3402   if (flag_code == CODE_64BIT)
    3403     bigdisp = Disp64;
    34043798  i.types[this_operand] |= bigdisp;
    34053799
     
    34983892      exp->X_op_symbol = GOT_symbol;
    34993893      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
    3500         i.reloc[this_operand] = BFD_RELOC_32_PCREL;
     3894        i.reloc[this_operand] = BFD_RELOC_32_PCREL;
    35013895      else
    3502         i.reloc[this_operand] = BFD_RELOC_32;
     3896        i.reloc[this_operand] = BFD_RELOC_32;
    35033897    }
    35043898#endif
     
    35203914      && OUTPUT_FLAVOR == bfd_target_aout_flavour
    35213915#endif
     3916      && exp_seg != absolute_section
    35223917      && exp_seg != text_section
    35233918      && exp_seg != data_section
    35243919      && exp_seg != bss_section
    3525       && exp_seg != undefined_section)
     3920      && exp_seg != undefined_section
     3921#ifdef BFD_ASSEMBLER
     3922      && !bfd_is_com_section (exp_seg)
     3923#endif
     3924      )
    35263925    {
    35273926#ifdef BFD_ASSEMBLER
     
    35563955  if (flag_code == CODE_64BIT)
    35573956    {
    3558       /* 64bit checks.  */
    3559       if ((i.base_reg
    3560            && ((i.base_reg->reg_type & Reg64) == 0)
    3561                && (i.base_reg->reg_type != BaseIndex
    3562                    || i.index_reg))
    3563           || (i.index_reg
    3564               && ((i.index_reg->reg_type & (Reg64|BaseIndex))
    3565                   != (Reg64|BaseIndex))))
    3566         ok = 0;
    3567     }
    3568   else
    3569     {
    3570       if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
    3571         {
    3572           /* 16bit checks.  */
     3957      if (i.prefix[ADDR_PREFIX] == 0)
     3958        {
     3959          /* 64bit checks.  */
    35733960          if ((i.base_reg
    3574                && ((i.base_reg->reg_type & (Reg16|BaseIndex|RegRex))
    3575                    != (Reg16|BaseIndex)))
     3961               && ((i.base_reg->reg_type & Reg64) == 0)
     3962                   && (i.base_reg->reg_type != BaseIndex
     3963                       || i.index_reg))
    35763964              || (i.index_reg
    3577                   && (((i.index_reg->reg_type & (Reg16|BaseIndex))
    3578                        != (Reg16|BaseIndex))
    3579                       || ! (i.base_reg
    3580                             && i.base_reg->reg_num < 6
    3581                             && i.index_reg->reg_num >= 6
    3582                             && i.log2_scale_factor == 0))))
     3965                  && ((i.index_reg->reg_type & (Reg64 | BaseIndex))
     3966                      != (Reg64 | BaseIndex))))
    35833967            ok = 0;
    35843968        }
     
    35893973               && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
    35903974              || (i.index_reg
    3591                   && ((i.index_reg->reg_type & (Reg32|BaseIndex|RegRex))
    3592                       != (Reg32|BaseIndex))))
     3975                  && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
     3976                      != (Reg32 | BaseIndex))))
     3977            ok = 0;
     3978        }
     3979    }
     3980  else
     3981    {
     3982      if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
     3983        {
     3984          /* 16bit checks.  */
     3985          if ((i.base_reg
     3986               && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
     3987                   != (Reg16 | BaseIndex)))
     3988              || (i.index_reg
     3989                  && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
     3990                       != (Reg16 | BaseIndex))
     3991                      || !(i.base_reg
     3992                           && i.base_reg->reg_num < 6
     3993                           && i.index_reg->reg_num >= 6
     3994                           && i.log2_scale_factor == 0))))
     3995            ok = 0;
     3996        }
     3997      else
     3998        {
     3999          /* 32bit checks.  */
     4000          if ((i.base_reg
     4001               && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
     4002              || (i.index_reg
     4003                  && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
     4004                      != (Reg32 | BaseIndex))))
    35934005            ok = 0;
    35944006        }
     
    36074019             Disp16 and Disp32 flags.  The same goes for Imm16 and Imm32.
    36084020             Removing them would probably clean up the code quite a lot.  */
    3609           if (i.types[this_operand] & (Disp16|Disp32))
    3610              i.types[this_operand] ^= (Disp16|Disp32);
     4021          if (i.types[this_operand] & (Disp16 | Disp32))
     4022             i.types[this_operand] ^= (Disp16 | Disp32);
    36114023          fudged = 1;
    36124024          goto tryprefix;
     
    39284340int
    39294341md_estimate_size_before_relax (fragP, segment)
    3930      register fragS *fragP;
    3931      register segT segment;
     4342     fragS *fragP;
     4343     segT segment;
    39324344{
    39334345  /* We've already got fragP->fr_subtype right;  all we have to do is
     
    39374349  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
    39384350#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
    3939       || S_IS_EXTERNAL (fragP->fr_symbol)
    3940       || S_IS_WEAK (fragP->fr_symbol)
     4351      || (OUTPUT_FLAVOR == bfd_target_elf_flavour
     4352          && (S_IS_EXTERNAL (fragP->fr_symbol)
     4353              || S_IS_WEAK (fragP->fr_symbol)))
    39414354#endif
    39424355      )
     
    39724385
    39734386        case COND_JUMP86:
    3974           if (no_cond_jump_promotion)
    3975             goto relax_guess;
    3976 
    3977           if (size == 2)
     4387          if (size == 2
     4388              && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
    39784389            {
    39794390              /* Negate the condition, and branch past an
     
    39954406
    39964407        case COND_JUMP:
    3997           if (no_cond_jump_promotion)
    3998             goto relax_guess;
     4408          if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
     4409            {
     4410              fixS *fixP;
     4411
     4412              fragP->fr_fix += 1;
     4413              fixP = fix_new (fragP, old_fr_fix, 1,
     4414                              fragP->fr_symbol,
     4415                              fragP->fr_offset, 1,
     4416                              BFD_RELOC_8_PCREL);
     4417              fixP->fx_signed = 1;
     4418              break;
     4419            }
    39994420
    40004421          /* This changes the byte-displacement jump 0x7N
     
    40184439    }
    40194440
    4020  relax_guess:
    40214441  /* Guess size depending on current relax state.  Initially the relax
    40224442     state will correspond to a short jump and we return 1, because
     
    40424462     object_headers *headers ATTRIBUTE_UNUSED;
    40434463     segT sec ATTRIBUTE_UNUSED;
    4044      register fragS *fragP;
     4464     fragS *fragP;
    40454465#else
    40464466void
     
    40484468     bfd *abfd ATTRIBUTE_UNUSED;
    40494469     segT sec ATTRIBUTE_UNUSED;
    4050      register fragS *fragP;
    4051 #endif
    4052 {
    4053   register unsigned char *opcode;
     4470     fragS *fragP;
     4471#endif
     4472{
     4473  unsigned char *opcode;
    40544474  unsigned char *where_to_put_displacement = NULL;
    40554475  offsetT target_address;
     
    40624482  /* Address we want to reach in file space.  */
    40634483  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
    4064 #ifdef BFD_ASSEMBLER
    4065   /* Not needed otherwise?  */
    4066   {
    4067     /* Local symbols which have already been resolved have a NULL frag.  */
    4068     fragS *sym_frag = symbol_get_frag (fragP->fr_symbol);
    4069     if (sym_frag)
    4070       target_address += sym_frag->fr_address;
    4071   }
    4072 #endif
    40734484
    40744485  /* Address opcode resides at in file space.  */
     
    41874598   we are handling.  */
    41884599
    4189 int
    4190 md_apply_fix3 (fixP, valp, seg)
     4600void
     4601md_apply_fix3 (fixP, valP, seg)
    41914602     /* The fix we're to put in.  */
    41924603     fixS *fixP;
    4193 
    41944604     /* Pointer to the value of the bits.  */
    4195      valueT *valp;
    4196 
     4605     valueT *valP;
    41974606     /* Segment fix is from.  */
    41984607     segT seg ATTRIBUTE_UNUSED;
    41994608{
    4200   register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
    4201   valueT value = *valp;
     4609  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
     4610  valueT value = *valP;
    42024611
    42034612#if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
     
    42214630    }
    42224631
    4223   /* This is a hack.  There should be a better way to handle this.
    4224      This covers for the fact that bfd_install_relocation will
    4225      subtract the current location (for partial_inplace, PC relative
    4226      relocations); see more below.  */
    4227   if ((fixP->fx_r_type == BFD_RELOC_32_PCREL
    4228        || fixP->fx_r_type == BFD_RELOC_16_PCREL
    4229        || fixP->fx_r_type == BFD_RELOC_8_PCREL)
    4230       && fixP->fx_addsy && !use_rela_relocations)
    4231     {
     4632  if (fixP->fx_addsy != NULL
     4633      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
     4634          || fixP->fx_r_type == BFD_RELOC_16_PCREL
     4635          || fixP->fx_r_type == BFD_RELOC_8_PCREL)
     4636      && !use_rela_relocations)
     4637    {
     4638      /* This is a hack.  There should be a better way to handle this.
     4639         This covers for the fact that bfd_install_relocation will
     4640         subtract the current location (for partial_inplace, PC relative
     4641         relocations); see more below.  */
    42324642#ifndef OBJ_AOUT
    42334643      if (OUTPUT_FLAVOR == bfd_target_elf_flavour
     
    42414651      if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
    42424652        {
    4243           segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
    4244 
    4245           if ((fseg == seg
     4653          segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
     4654
     4655          if ((sym_seg == seg
    42464656               || (symbol_section_p (fixP->fx_addsy)
    4247                    && fseg != absolute_section))
    4248               && ! S_IS_EXTERNAL (fixP->fx_addsy)
    4249               && ! S_IS_WEAK (fixP->fx_addsy)
    4250               && S_IS_DEFINED (fixP->fx_addsy)
    4251               && ! S_IS_COMMON (fixP->fx_addsy))
     4657                   && sym_seg != absolute_section))
     4658              && !generic_force_reloc (fixP))
    42524659            {
    42534660              /* Yes, we add the values in twice.  This is because
    4254                  bfd_perform_relocation subtracts them out again.  I think
    4255                  bfd_perform_relocation is broken, but I don't dare change
     4661                 bfd_install_relocation subtracts them out again.  I think
     4662                 bfd_install_relocation is broken, but I don't dare change
    42564663                 it.  FIXME.  */
    42574664              value += fixP->fx_where + fixP->fx_frag->fr_address;
     
    42804687        value = -4;
    42814688        break;
    4282       case BFD_RELOC_386_GOTPC:
    4283 
    4284 /*   This is tough to explain.  We end up with this one if we have
    4285  * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal
    4286  * here is to obtain the absolute address of the GOT, and it is strongly
    4287  * preferable from a performance point of view to avoid using a runtime
    4288  * relocation for this.  The actual sequence of instructions often look
    4289  * something like:
    4290  *
    4291  *      call    .L66
    4292  * .L66:
    4293  *      popl    %ebx
    4294  *      addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
    4295  *
    4296  *   The call and pop essentially return the absolute address of
    4297  * the label .L66 and store it in %ebx.  The linker itself will
    4298  * ultimately change the first operand of the addl so that %ebx points to
    4299  * the GOT, but to keep things simple, the .o file must have this operand
    4300  * set so that it generates not the absolute address of .L66, but the
    4301  * absolute address of itself.  This allows the linker itself simply
    4302  * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
    4303  * added in, and the addend of the relocation is stored in the operand
    4304  * field for the instruction itself.
    4305  *
    4306  *   Our job here is to fix the operand so that it would add the correct
    4307  * offset so that %ebx would point to itself.  The thing that is tricky is
    4308  * that .-.L66 will point to the beginning of the instruction, so we need
    4309  * to further modify the operand so that it will point to itself.
    4310  * There are other cases where you have something like:
    4311  *
    4312  *      .long   $_GLOBAL_OFFSET_TABLE_+[.-.L66]
    4313  *
    4314  * and here no correction would be required.  Internally in the assembler
    4315  * we treat operands of this form as not being pcrel since the '.' is
    4316  * explicitly mentioned, and I wonder whether it would simplify matters
    4317  * to do it this way.  Who knows.  In earlier versions of the PIC patches,
    4318  * the pcrel_adjust field was used to store the correction, but since the
    4319  * expression is not pcrel, I felt it would be confusing to do it this
    4320  * way.  */
    4321 
    4322         value -= 1;
     4689
     4690      case BFD_RELOC_386_TLS_GD:
     4691      case BFD_RELOC_386_TLS_LDM:
     4692      case BFD_RELOC_386_TLS_IE_32:
     4693      case BFD_RELOC_386_TLS_IE:
     4694      case BFD_RELOC_386_TLS_GOTIE:
     4695      case BFD_RELOC_X86_64_TLSGD:
     4696      case BFD_RELOC_X86_64_TLSLD:
     4697      case BFD_RELOC_X86_64_GOTTPOFF:
     4698        value = 0; /* Fully resolved at runtime.  No addend.  */
     4699        /* Fallthrough */
     4700      case BFD_RELOC_386_TLS_LE:
     4701      case BFD_RELOC_386_TLS_LDO_32:
     4702      case BFD_RELOC_386_TLS_LE_32:
     4703      case BFD_RELOC_X86_64_DTPOFF32:
     4704      case BFD_RELOC_X86_64_TPOFF32:
     4705        S_SET_THREAD_LOCAL (fixP->fx_addsy);
    43234706        break;
     4707
    43244708      case BFD_RELOC_386_GOT32:
    43254709      case BFD_RELOC_X86_64_GOT32:
    43264710        value = 0; /* Fully resolved at runtime.  No addend.  */
    43274711        break;
    4328       case BFD_RELOC_386_GOTOFF:
    4329       case BFD_RELOC_X86_64_GOTPCREL:
    4330         break;
    43314712
    43324713      case BFD_RELOC_VTABLE_INHERIT:
    43334714      case BFD_RELOC_VTABLE_ENTRY:
    43344715        fixP->fx_done = 0;
    4335         return 1;
     4716        return;
    43364717
    43374718      default:
     
    43394720      }
    43404721#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)  */
    4341   *valp = value;
     4722  *valP = value;
    43424723#endif /* defined (BFD_ASSEMBLER) && !defined (TE_Mach)  */
    43434724
    4344 #ifndef BFD_ASSEMBLER
     4725  /* Are we finished with this relocation now?  */
     4726  if (fixP->fx_addsy == NULL)
     4727    fixP->fx_done = 1;
     4728#ifdef BFD_ASSEMBLER
     4729  else if (use_rela_relocations)
     4730    {
     4731      fixP->fx_no_overflow = 1;
     4732      /* Remember value for tc_gen_reloc.  */
     4733      fixP->fx_addnumber = value;
     4734      value = 0;
     4735    }
     4736#endif
    43454737  md_number_to_chars (p, value, fixP->fx_size);
    4346 #else
    4347   /* Are we finished with this relocation now?  */
    4348   if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
    4349     fixP->fx_done = 1;
    4350   else if (use_rela_relocations)
    4351     {
    4352       fixP->fx_no_overflow = 1;
    4353       value = 0;
    4354     }
    4355   md_number_to_chars (p, value, fixP->fx_size);
    4356 #endif
    4357 
    4358   return 1;
    43594738}
    43604739
     
    44214800     int c;
    44224801{
    4423   if (isprint (c))
     4802  if (ISPRINT (c))
    44244803    sprintf (output_invalid_buf, "'%c'", c);
    44254804  else
     
    44924871    }
    44934872
     4873  if (r != NULL
     4874      && (r->reg_flags & (RegRex64 | RegRex)) != 0
     4875      && flag_code != CODE_64BIT)
     4876    {
     4877      return (const reg_entry *) NULL;
     4878    }
     4879
    44944880  return r;
    44954881}
     
    45524938    case 's':
    45534939      /* -s: On i386 Solaris, this tells the native assembler to use
    4554         .stab instead of .stab.excl.  We always use .stab anyhow.  */
     4940        .stab instead of .stab.excl.  We always use .stab anyhow.  */
    45554941      break;
    45564942
     
    46405026        if (flag_code == CODE_64BIT)
    46415027          use_rela_relocations = 1;
    4642         return flag_code == CODE_64BIT ? "elf64-x86-64" : "elf32-i386";
     5028        return flag_code == CODE_64BIT ? "elf64-x86-64" : ELF_TARGET_FORMAT;
    46435029      }
    46445030#endif
     
    46505036
    46515037#endif /* OBJ_MAYBE_ more than one  */
     5038
     5039#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
     5040void i386_elf_emit_arch_note ()
     5041{
     5042  if (OUTPUT_FLAVOR == bfd_target_elf_flavour
     5043      && cpu_arch_name != NULL)
     5044    {
     5045      char *p;
     5046      asection *seg = now_seg;
     5047      subsegT subseg = now_subseg;
     5048      Elf_Internal_Note i_note;
     5049      Elf_External_Note e_note;
     5050      asection *note_secp;
     5051      int len;
     5052
     5053      /* Create the .note section.  */
     5054      note_secp = subseg_new (".note", 0);
     5055      bfd_set_section_flags (stdoutput,
     5056                             note_secp,
     5057                             SEC_HAS_CONTENTS | SEC_READONLY);
     5058
     5059      /* Process the arch string.  */
     5060      len = strlen (cpu_arch_name);
     5061
     5062      i_note.namesz = len + 1;
     5063      i_note.descsz = 0;
     5064      i_note.type = NT_ARCH;
     5065      p = frag_more (sizeof (e_note.namesz));
     5066      md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
     5067      p = frag_more (sizeof (e_note.descsz));
     5068      md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
     5069      p = frag_more (sizeof (e_note.type));
     5070      md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
     5071      p = frag_more (len + 1);
     5072      strcpy (p, cpu_arch_name);
     5073
     5074      frag_align (2, 0, 0);
     5075
     5076      subseg_set (seg, subseg);
     5077    }
     5078}
     5079#endif
    46525080#endif /* BFD_ASSEMBLER  */
    46535081
     
    47185146     int ignore ATTRIBUTE_UNUSED;
    47195147{
    4720   register int temp;
     5148  int temp;
    47215149
    47225150  temp = get_absolute_expression ();
     
    47695197    case BFD_RELOC_386_GOTOFF:
    47705198    case BFD_RELOC_386_GOTPC:
     5199    case BFD_RELOC_386_TLS_GD:
     5200    case BFD_RELOC_386_TLS_LDM:
     5201    case BFD_RELOC_386_TLS_LDO_32:
     5202    case BFD_RELOC_386_TLS_IE_32:
     5203    case BFD_RELOC_386_TLS_IE:
     5204    case BFD_RELOC_386_TLS_GOTIE:
     5205    case BFD_RELOC_386_TLS_LE_32:
     5206    case BFD_RELOC_386_TLS_LE:
    47715207    case BFD_RELOC_X86_64_32S:
     5208    case BFD_RELOC_X86_64_TLSGD:
     5209    case BFD_RELOC_X86_64_TLSLD:
     5210    case BFD_RELOC_X86_64_DTPOFF32:
     5211    case BFD_RELOC_X86_64_GOTTPOFF:
     5212    case BFD_RELOC_X86_64_TPOFF32:
    47725213    case BFD_RELOC_RVA:
    47735214    case BFD_RELOC_VTABLE_ENTRY:
     
    47815222            {
    47825223            default:
    4783               as_bad (_("can not do %d byte pc-relative relocation"),
    4784                       fixp->fx_size);
     5224              as_bad_where (fixp->fx_file, fixp->fx_line,
     5225                            _("can not do %d byte pc-relative relocation"),
     5226                            fixp->fx_size);
    47855227              code = BFD_RELOC_32_PCREL;
    47865228              break;
     
    47955237            {
    47965238            default:
    4797               as_bad (_("can not do %d byte relocation"), fixp->fx_size);
     5239              as_bad_where (fixp->fx_file, fixp->fx_line,
     5240                            _("can not do %d byte relocation"),
     5241                            fixp->fx_size);
    47985242              code = BFD_RELOC_32;
    47995243              break;
     
    48015245            case 2: code = BFD_RELOC_16; break;
    48025246            case 4: code = BFD_RELOC_32; break;
     5247#ifdef BFD64
    48035248            case 8: code = BFD_RELOC_64; break;
     5249#endif
    48045250            }
    48055251        }
     
    48295275        rel->address = fixp->fx_offset;
    48305276
    4831       if (fixp->fx_pcrel)
    4832         rel->addend = fixp->fx_addnumber;
    4833       else
    4834         rel->addend = 0;
     5277      rel->addend = 0;
    48355278    }
    48365279  /* Use the rela in 64bit mode.  */
    48375280  else
    48385281    {
    4839       rel->addend = fixp->fx_offset;
    4840       if (fixp->fx_pcrel)
    4841         rel->addend -= fixp->fx_size;
     5282      if (!fixp->fx_pcrel)
     5283        rel->addend = fixp->fx_offset;
     5284      else
     5285        switch (code)
     5286          {
     5287          case BFD_RELOC_X86_64_PLT32:
     5288          case BFD_RELOC_X86_64_GOT32:
     5289          case BFD_RELOC_X86_64_GOTPCREL:
     5290          case BFD_RELOC_X86_64_TLSGD:
     5291          case BFD_RELOC_X86_64_TLSLD:
     5292          case BFD_RELOC_X86_64_GOTTPOFF:
     5293            rel->addend = fixp->fx_offset - fixp->fx_size;
     5294            break;
     5295          default:
     5296            rel->addend = (section->vma
     5297                           - fixp->fx_size
     5298                           + fixp->fx_addnumber
     5299                           + md_pcrel_from (fixp));
     5300            break;
     5301          }
    48425302    }
    48435303
     
    48565316}
    48575317
    4858 #else /* ! BFD_ASSEMBLER  */
     5318#else /* !BFD_ASSEMBLER  */
    48595319
    48605320#if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
     
    49225382#endif /* I386COFF  */
    49235383
    4924 #endif /* ! BFD_ASSEMBLER  */
     5384#endif /* !BFD_ASSEMBLER  */
    49255385
    49265386
Note: See TracChangeset for help on using the changeset viewer.