tc-i386.c revision 38891
133965Sjdp/* i386.c -- Assemble code for the Intel 80386
238891Sjdp   Copyright (C) 1989, 91, 92, 93, 94, 95, 96, 97, 1998
338891Sjdp   Free Software Foundation.
433965Sjdp
533965Sjdp   This file is part of GAS, the GNU Assembler.
633965Sjdp
733965Sjdp   GAS is free software; you can redistribute it and/or modify
833965Sjdp   it under the terms of the GNU General Public License as published by
933965Sjdp   the Free Software Foundation; either version 2, or (at your option)
1033965Sjdp   any later version.
1133965Sjdp
1233965Sjdp   GAS is distributed in the hope that it will be useful,
1333965Sjdp   but WITHOUT ANY WARRANTY; without even the implied warranty of
1433965Sjdp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1533965Sjdp   GNU General Public License for more details.
1633965Sjdp
1733965Sjdp   You should have received a copy of the GNU General Public License
1833965Sjdp   along with GAS; see the file COPYING.  If not, write to the Free
1933965Sjdp   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2033965Sjdp   02111-1307, USA.  */
2133965Sjdp
2233965Sjdp/*
2333965Sjdp  Intel 80386 machine specific gas.
2433965Sjdp  Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
2533965Sjdp  Bugs & suggestions are completely welcome.  This is free software.
2633965Sjdp  Please help us make it better.
2733965Sjdp  */
2833965Sjdp
2933965Sjdp#include <ctype.h>
3033965Sjdp
3133965Sjdp#include "as.h"
3233965Sjdp#include "subsegs.h"
3333965Sjdp
3433965Sjdp#include "obstack.h"
3533965Sjdp#include "opcode/i386.h"
3633965Sjdp
3733965Sjdp#ifndef TC_RELOC
3833965Sjdp#define TC_RELOC(X,Y) (Y)
3933965Sjdp#endif
4033965Sjdp
4133965Sjdpstatic unsigned long mode_from_disp_size PARAMS ((unsigned long));
4233965Sjdpstatic int fits_in_signed_byte PARAMS ((long));
4333965Sjdpstatic int fits_in_unsigned_byte PARAMS ((long));
4433965Sjdpstatic int fits_in_unsigned_word PARAMS ((long));
4533965Sjdpstatic int fits_in_signed_word PARAMS ((long));
4633965Sjdpstatic int smallest_imm_type PARAMS ((long));
4733965Sjdpstatic void set_16bit_code_flag PARAMS ((int));
4833965Sjdp#ifdef BFD_ASSEMBLER
4933965Sjdpstatic bfd_reloc_code_real_type reloc
5033965Sjdp  PARAMS ((int, int, bfd_reloc_code_real_type));
5133965Sjdp#endif
5233965Sjdp
5333965Sjdp/* 'md_assemble ()' gathers together information and puts it into a
5433965Sjdp   i386_insn. */
5533965Sjdp
5633965Sjdpstruct _i386_insn
5733965Sjdp  {
5833965Sjdp    /* TM holds the template for the insn were currently assembling. */
5933965Sjdp    template tm;
6033965Sjdp    /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
6133965Sjdp    char suffix;
6233965Sjdp    /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
6333965Sjdp
6433965Sjdp    /* OPERANDS gives the number of given operands. */
6533965Sjdp    unsigned int operands;
6633965Sjdp
6733965Sjdp    /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
6833965Sjdp       of given register, displacement, memory operands and immediate
6933965Sjdp       operands. */
7033965Sjdp    unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
7133965Sjdp
7233965Sjdp    /* TYPES [i] is the type (see above #defines) which tells us how to
7333965Sjdp       search through DISPS [i] & IMMS [i] & REGS [i] for the required
7433965Sjdp       operand.  */
7533965Sjdp    unsigned int types[MAX_OPERANDS];
7633965Sjdp
7733965Sjdp    /* Displacements (if given) for each operand. */
7833965Sjdp    expressionS *disps[MAX_OPERANDS];
7933965Sjdp
8033965Sjdp    /* Relocation type for operand */
8133965Sjdp#ifdef BFD_ASSEMBLER
8233965Sjdp    enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
8333965Sjdp#else
8433965Sjdp    int disp_reloc[MAX_OPERANDS];
8533965Sjdp#endif
8633965Sjdp
8733965Sjdp    /* Immediate operands (if given) for each operand. */
8833965Sjdp    expressionS *imms[MAX_OPERANDS];
8933965Sjdp
9033965Sjdp    /* Register operands (if given) for each operand. */
9133965Sjdp    reg_entry *regs[MAX_OPERANDS];
9233965Sjdp
9333965Sjdp    /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
9433965Sjdp       the base index byte below.  */
9533965Sjdp    reg_entry *base_reg;
9633965Sjdp    reg_entry *index_reg;
9733965Sjdp    unsigned int log2_scale_factor;
9833965Sjdp
9933965Sjdp    /* SEG gives the seg_entry of this insn.  It is equal to zero unless
10033965Sjdp       an explicit segment override is given. */
10133965Sjdp    const seg_entry *seg;	/* segment for memory operands (if given) */
10233965Sjdp
10333965Sjdp    /* PREFIX holds all the given prefix opcodes (usually null).
10433965Sjdp       PREFIXES is the size of PREFIX. */
10533965Sjdp    /* richfix: really unsigned? */
10633965Sjdp    unsigned char prefix[MAX_PREFIXES];
10733965Sjdp    unsigned int prefixes;
10833965Sjdp
10938891Sjdp    /* RM and BI are the modrm byte and the base index byte where the
11033965Sjdp       addressing modes of this insn are encoded. */
11133965Sjdp
11233965Sjdp    modrm_byte rm;
11333965Sjdp    base_index_byte bi;
11433965Sjdp  };
11533965Sjdp
11633965Sjdptypedef struct _i386_insn i386_insn;
11733965Sjdp
11833965Sjdp/* This array holds the chars that always start a comment.  If the
11933965Sjdp   pre-processor is disabled, these aren't very useful */
12033972Sjdp#if defined (TE_I386AIX) || (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && !defined(TE_FreeBSD)
12133965Sjdpconst char comment_chars[] = "#/";
12233965Sjdp#else
12333965Sjdpconst char comment_chars[] = "#";
12433965Sjdp#endif
12533965Sjdp
12633965Sjdp/* This array holds the chars that only start a comment at the beginning of
12733965Sjdp   a line.  If the line seems to have the form '# 123 filename'
12833965Sjdp   .line and .file directives will appear in the pre-processed output */
12933965Sjdp/* Note that input_file.c hand checks for '#' at the beginning of the
13033965Sjdp   first line of the input file.  This is because the compiler outputs
13133965Sjdp   #NO_APP at the beginning of its output. */
13233965Sjdp/* Also note that comments started like this one will always work if
13333965Sjdp   '/' isn't otherwise defined.  */
13433972Sjdp#if defined (TE_I386AIX) || (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) && !defined(TE_FreeBSD)
13533965Sjdpconst char line_comment_chars[] = "";
13633965Sjdp#else
13733965Sjdpconst char line_comment_chars[] = "/";
13833965Sjdp#endif
13933965Sjdpconst char line_separator_chars[] = "";
14033965Sjdp
14133965Sjdp/* Chars that can be used to separate mant from exp in floating point nums */
14233965Sjdpconst char EXP_CHARS[] = "eE";
14333965Sjdp
14433965Sjdp/* Chars that mean this number is a floating point constant */
14533965Sjdp/* As in 0f12.456 */
14633965Sjdp/* or    0d1.2345e12 */
14733965Sjdpconst char FLT_CHARS[] = "fFdDxX";
14833965Sjdp
14933965Sjdp/* tables for lexical analysis */
15033965Sjdpstatic char opcode_chars[256];
15133965Sjdpstatic char register_chars[256];
15233965Sjdpstatic char operand_chars[256];
15333965Sjdpstatic char space_chars[256];
15433965Sjdpstatic char identifier_chars[256];
15533965Sjdpstatic char digit_chars[256];
15633965Sjdp
15733965Sjdp/* lexical macros */
15833965Sjdp#define is_opcode_char(x) (opcode_chars[(unsigned char) x])
15933965Sjdp#define is_operand_char(x) (operand_chars[(unsigned char) x])
16033965Sjdp#define is_register_char(x) (register_chars[(unsigned char) x])
16133965Sjdp#define is_space_char(x) (space_chars[(unsigned char) x])
16233965Sjdp#define is_identifier_char(x) (identifier_chars[(unsigned char) x])
16333965Sjdp#define is_digit_char(x) (digit_chars[(unsigned char) x])
16433965Sjdp
16533965Sjdp/* put here all non-digit non-letter charcters that may occur in an operand */
16633965Sjdpstatic char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
16733965Sjdp
16833965Sjdpstatic char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
16933965Sjdp
17033965Sjdp/* md_assemble() always leaves the strings it's passed unaltered.  To
17133965Sjdp   effect this we maintain a stack of saved characters that we've smashed
17233965Sjdp   with '\0's (indicating end of strings for various sub-fields of the
17333965Sjdp   assembler instruction). */
17433965Sjdpstatic char save_stack[32];
17533965Sjdpstatic char *save_stack_p;	/* stack pointer */
17633965Sjdp#define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
17733965Sjdp#define RESTORE_END_STRING(s)       *s = *--save_stack_p
17833965Sjdp
17933965Sjdp/* The instruction we're assembling. */
18033965Sjdpstatic i386_insn i;
18133965Sjdp
18233965Sjdp/* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
18333965Sjdpstatic expressionS disp_expressions[2], im_expressions[2];
18433965Sjdp
18533965Sjdp/* pointers to ebp & esp entries in reg_hash hash table */
18633965Sjdpstatic reg_entry *ebp, *esp;
18733965Sjdp
18833965Sjdpstatic int this_operand;	/* current operand we are working on */
18933965Sjdp
19033965Sjdpstatic int flag_do_long_jump;	/* FIXME what does this do? */
19133965Sjdp
19233965Sjdpstatic int flag_16bit_code;	/* 1 if we're writing 16-bit code, 0 if 32-bit */
19333965Sjdp
19433965Sjdp/* Interface to relax_segment.
19533965Sjdp   There are 2 relax states for 386 jump insns: one for conditional &
19633965Sjdp   one for unconditional jumps.  This is because the these two types
19733965Sjdp   of jumps add different sizes to frags when we're figuring out what
19833965Sjdp   sort of jump to choose to reach a given label.  */
19933965Sjdp
20033965Sjdp/* types */
20133965Sjdp#define COND_JUMP 1		/* conditional jump */
20233965Sjdp#define UNCOND_JUMP 2		/* unconditional jump */
20333965Sjdp/* sizes */
20433965Sjdp#define BYTE 0
20533965Sjdp#define WORD 1
20633965Sjdp#define DWORD 2
20733965Sjdp#define UNKNOWN_SIZE 3
20833965Sjdp
20933965Sjdp#ifndef INLINE
21033965Sjdp#ifdef __GNUC__
21133965Sjdp#define INLINE __inline__
21233965Sjdp#else
21333965Sjdp#define INLINE
21433965Sjdp#endif
21533965Sjdp#endif
21633965Sjdp
21733965Sjdp#define ENCODE_RELAX_STATE(type,size) \
21833965Sjdp  ((relax_substateT)((type<<2) | (size)))
21933965Sjdp#define SIZE_FROM_RELAX_STATE(s) \
22033965Sjdp    ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
22133965Sjdp
22233965Sjdpconst relax_typeS md_relax_table[] =
22333965Sjdp{
22433965Sjdp/* The fields are:
22533965Sjdp   1) most positive reach of this state,
22633965Sjdp   2) most negative reach of this state,
22733965Sjdp   3) how many bytes this mode will add to the size of the current frag
22833965Sjdp   4) which index into the table to try if we can't fit into this one.
22933965Sjdp   */
23033965Sjdp  {1, 1, 0, 0},
23133965Sjdp  {1, 1, 0, 0},
23233965Sjdp  {1, 1, 0, 0},
23333965Sjdp  {1, 1, 0, 0},
23433965Sjdp
23533965Sjdp  /* For now we don't use word displacement jumps; they may be
23633965Sjdp     untrustworthy. */
23733965Sjdp  {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
23833965Sjdp  /* word conditionals add 3 bytes to frag:
23933965Sjdp     2 opcode prefix; 1 displacement bytes */
24033965Sjdp  {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
24133965Sjdp  /* dword conditionals adds 4 bytes to frag:
24233965Sjdp     1 opcode prefix; 3 displacement bytes */
24333965Sjdp  {0, 0, 4, 0},
24433965Sjdp  {1, 1, 0, 0},
24533965Sjdp
24633965Sjdp  {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
24733965Sjdp  /* word jmp adds 2 bytes to frag:
24833965Sjdp     1 opcode prefix; 1 displacement bytes */
24933965Sjdp  {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
25033965Sjdp  /* dword jmp adds 3 bytes to frag:
25133965Sjdp     0 opcode prefix; 3 displacement bytes */
25233965Sjdp  {0, 0, 3, 0},
25333965Sjdp  {1, 1, 0, 0},
25433965Sjdp
25533965Sjdp};
25633965Sjdp
25733965Sjdp
25833965Sjdpvoid
25933965Sjdpi386_align_code (fragP, count)
26033965Sjdp     fragS *fragP;
26133965Sjdp     int count;
26233965Sjdp{
26333965Sjdp  /* Various efficient no-op patterns for aligning code labels.  */
26433965Sjdp  /* Note: Don't try to assemble the instructions in the comments. */
26533965Sjdp  /*       0L and 0w are not legal */
26633965Sjdp  static const char f32_1[] =
26733965Sjdp    {0x90};					/* nop			*/
26833965Sjdp  static const char f32_2[] =
26933965Sjdp    {0x89,0xf6};				/* movl %esi,%esi	*/
27033965Sjdp  static const char f32_3[] =
27133965Sjdp    {0x8d,0x76,0x00};				/* leal 0(%esi),%esi	*/
27233965Sjdp  static const char f32_4[] =
27333965Sjdp    {0x8d,0x74,0x26,0x00};			/* leal 0(%esi,1),%esi	*/
27433965Sjdp  static const char f32_5[] =
27533965Sjdp    {0x90,					/* nop			*/
27633965Sjdp     0x8d,0x74,0x26,0x00};			/* leal 0(%esi,1),%esi	*/
27733965Sjdp  static const char f32_6[] =
27833965Sjdp    {0x8d,0xb6,0x00,0x00,0x00,0x00};		/* leal 0L(%esi),%esi	*/
27933965Sjdp  static const char f32_7[] =
28033965Sjdp    {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};	/* leal 0L(%esi,1),%esi */
28133965Sjdp  static const char f32_8[] =
28233965Sjdp    {0x90,					/* nop			*/
28333965Sjdp     0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};	/* leal 0L(%esi,1),%esi */
28433965Sjdp  static const char f32_9[] =
28533965Sjdp    {0x89,0xf6,					/* movl %esi,%esi	*/
28633965Sjdp     0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};	/* leal 0L(%edi,1),%edi */
28733965Sjdp  static const char f32_10[] =
28833965Sjdp    {0x8d,0x76,0x00,				/* leal 0(%esi),%esi	*/
28933965Sjdp     0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};	/* leal 0L(%edi,1),%edi */
29033965Sjdp  static const char f32_11[] =
29133965Sjdp    {0x8d,0x74,0x26,0x00,			/* leal 0(%esi,1),%esi	*/
29233965Sjdp     0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};	/* leal 0L(%edi,1),%edi */
29333965Sjdp  static const char f32_12[] =
29433965Sjdp    {0x8d,0xb6,0x00,0x00,0x00,0x00,		/* leal 0L(%esi),%esi	*/
29533965Sjdp     0x8d,0xbf,0x00,0x00,0x00,0x00};		/* leal 0L(%edi),%edi	*/
29633965Sjdp  static const char f32_13[] =
29733965Sjdp    {0x8d,0xb6,0x00,0x00,0x00,0x00,		/* leal 0L(%esi),%esi	*/
29833965Sjdp     0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};	/* leal 0L(%edi,1),%edi */
29933965Sjdp  static const char f32_14[] =
30033965Sjdp    {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00,	/* leal 0L(%esi,1),%esi */
30133965Sjdp     0x8d,0xbc,0x27,0x00,0x00,0x00,0x00};	/* leal 0L(%edi,1),%edi */
30233965Sjdp  static const char f32_15[] =
30333965Sjdp    {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90,	/* jmp .+15; lotsa nops	*/
30433965Sjdp     0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
30533965Sjdp  static const char f16_4[] =
30638891Sjdp    {0x8d,0xb4,0x00,0x00};			/* lea 0w(%si),%si	*/
30733965Sjdp  static const char f16_5[] =
30833965Sjdp    {0x90,					/* nop			*/
30938891Sjdp     0x8d,0xb4,0x00,0x00};			/* lea 0w(%si),%si	*/
31033965Sjdp  static const char f16_6[] =
31133965Sjdp    {0x89,0xf6,					/* mov %si,%si		*/
31233965Sjdp     0x8d,0xbd,0x00,0x00};			/* lea 0w(%di),%di	*/
31333965Sjdp  static const char f16_7[] =
31438891Sjdp    {0x8d,0x74,0x00,				/* lea 0(%si),%si	*/
31533965Sjdp     0x8d,0xbd,0x00,0x00};			/* lea 0w(%di),%di	*/
31633965Sjdp  static const char f16_8[] =
31738891Sjdp    {0x8d,0xb4,0x00,0x00,			/* lea 0w(%si),%si	*/
31833965Sjdp     0x8d,0xbd,0x00,0x00};			/* lea 0w(%di),%di	*/
31933965Sjdp  static const char *const f32_patt[] = {
32033965Sjdp    f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
32133965Sjdp    f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
32233965Sjdp  };
32333965Sjdp  static const char *const f16_patt[] = {
32433965Sjdp    f32_1, f32_2, f32_3, f16_4, f16_5, f16_6, f16_7, f16_8,
32533965Sjdp    f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
32633965Sjdp  };
32733965Sjdp
32833965Sjdp  if (count > 0 && count <= 15)
32933965Sjdp    {
33033965Sjdp      if (flag_16bit_code)
33133965Sjdp	{
33233965Sjdp	  memcpy(fragP->fr_literal + fragP->fr_fix,
33333965Sjdp		 f16_patt[count - 1], count);
33433965Sjdp	  if (count > 8) /* adjust jump offset */
33533965Sjdp	    fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
33633965Sjdp	}
33733965Sjdp      else
33833965Sjdp	memcpy(fragP->fr_literal + fragP->fr_fix,
33933965Sjdp	       f32_patt[count - 1], count);
34033965Sjdp      fragP->fr_var = count;
34133965Sjdp    }
34233965Sjdp}
34333965Sjdp
34433965Sjdpstatic char *output_invalid PARAMS ((int c));
34533965Sjdpstatic int i386_operand PARAMS ((char *operand_string));
34633965Sjdpstatic reg_entry *parse_register PARAMS ((char *reg_string));
34733965Sjdp#ifndef I386COFF
34833965Sjdpstatic void s_bss PARAMS ((int));
34933965Sjdp#endif
35033965Sjdp
35133965SjdpsymbolS *GOT_symbol;		/* Pre-defined "__GLOBAL_OFFSET_TABLE" */
35233965Sjdp
35333965Sjdpstatic INLINE unsigned long
35433965Sjdpmode_from_disp_size (t)
35533965Sjdp     unsigned long t;
35633965Sjdp{
35733965Sjdp  return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
35833965Sjdp}
35933965Sjdp
36033965Sjdp#if 0
36133965Sjdp/* Not used.  */
36233965Sjdp/* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
36333965Sjdp
36433965Sjdpstatic INLINE unsigned long
36533965Sjdpopcode_suffix_to_type (s)
36633965Sjdp     unsigned long s;
36733965Sjdp{
36833965Sjdp  return (s == BYTE_OPCODE_SUFFIX
36933965Sjdp	  ? Byte : (s == WORD_OPCODE_SUFFIX
37033965Sjdp		    ? Word : DWord));
37133965Sjdp}				/* opcode_suffix_to_type() */
37233965Sjdp#endif
37333965Sjdp
37433965Sjdpstatic INLINE int
37533965Sjdpfits_in_signed_byte (num)
37633965Sjdp     long num;
37733965Sjdp{
37833965Sjdp  return (num >= -128) && (num <= 127);
37933965Sjdp}				/* fits_in_signed_byte() */
38033965Sjdp
38133965Sjdpstatic INLINE int
38233965Sjdpfits_in_unsigned_byte (num)
38333965Sjdp     long num;
38433965Sjdp{
38533965Sjdp  return (num & 0xff) == num;
38633965Sjdp}				/* fits_in_unsigned_byte() */
38733965Sjdp
38833965Sjdpstatic INLINE int
38933965Sjdpfits_in_unsigned_word (num)
39033965Sjdp     long num;
39133965Sjdp{
39233965Sjdp  return (num & 0xffff) == num;
39333965Sjdp}				/* fits_in_unsigned_word() */
39433965Sjdp
39533965Sjdpstatic INLINE int
39633965Sjdpfits_in_signed_word (num)
39733965Sjdp     long num;
39833965Sjdp{
39933965Sjdp  return (-32768 <= num) && (num <= 32767);
40033965Sjdp}				/* fits_in_signed_word() */
40133965Sjdp
40233965Sjdpstatic int
40333965Sjdpsmallest_imm_type (num)
40433965Sjdp     long num;
40533965Sjdp{
40633965Sjdp#if 0
40733965Sjdp  /* This code is disabled because all the Imm1 forms in the opcode table
40833965Sjdp     are slower on the i486, and they're the versions with the implicitly
40933965Sjdp     specified single-position displacement, which has another syntax if
41033965Sjdp     you really want to use that form.  If you really prefer to have the
41133965Sjdp     one-byte-shorter Imm1 form despite these problems, re-enable this
41233965Sjdp     code.  */
41333965Sjdp  if (num == 1)
41433965Sjdp    return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
41533965Sjdp#endif
41633965Sjdp  return (fits_in_signed_byte (num)
41733965Sjdp	  ? (Imm8S | Imm8 | Imm16 | Imm32)
41833965Sjdp	  : fits_in_unsigned_byte (num)
41933965Sjdp	  ? (Imm8 | Imm16 | Imm32)
42033965Sjdp	  : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
42133965Sjdp	  ? (Imm16 | Imm32)
42233965Sjdp	  : (Imm32));
42333965Sjdp}				/* smallest_imm_type() */
42433965Sjdp
42533965Sjdpstatic void
42633965Sjdpset_16bit_code_flag (new_16bit_code_flag)
42733965Sjdp	int new_16bit_code_flag;
42833965Sjdp{
42933965Sjdp  flag_16bit_code = new_16bit_code_flag;
43033965Sjdp}
43133965Sjdp
43233965Sjdpconst pseudo_typeS md_pseudo_table[] =
43333965Sjdp{
43433965Sjdp#ifndef I386COFF
43533965Sjdp  {"bss", s_bss, 0},
43633965Sjdp#endif
43738891Sjdp#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
43833965Sjdp  {"align", s_align_bytes, 0},
43933965Sjdp#else
44033965Sjdp  {"align", s_align_ptwo, 0},
44133965Sjdp#endif
44233965Sjdp  {"ffloat", float_cons, 'f'},
44333965Sjdp  {"dfloat", float_cons, 'd'},
44433965Sjdp  {"tfloat", float_cons, 'x'},
44533965Sjdp  {"value", cons, 2},
44633965Sjdp  {"noopt", s_ignore, 0},
44733965Sjdp  {"optim", s_ignore, 0},
44833965Sjdp  {"code16", set_16bit_code_flag, 1},
44933965Sjdp  {"code32", set_16bit_code_flag, 0},
45033965Sjdp  {0, 0, 0}
45133965Sjdp};
45233965Sjdp
45333965Sjdp/* for interface with expression () */
45433965Sjdpextern char *input_line_pointer;
45533965Sjdp
45633965Sjdp/* obstack for constructing various things in md_begin */
45733965Sjdpstruct obstack o;
45833965Sjdp
45933965Sjdp/* hash table for opcode lookup */
46033965Sjdpstatic struct hash_control *op_hash;
46133965Sjdp/* hash table for register lookup */
46233965Sjdpstatic struct hash_control *reg_hash;
46333965Sjdp/* hash table for prefix lookup */
46433965Sjdpstatic struct hash_control *prefix_hash;
46533965Sjdp
46633965Sjdp
46733965Sjdpvoid
46833965Sjdpmd_begin ()
46933965Sjdp{
47033965Sjdp  const char *hash_err;
47133965Sjdp
47233965Sjdp  obstack_begin (&o, 4096);
47333965Sjdp
47433965Sjdp  /* initialize op_hash hash table */
47533965Sjdp  op_hash = hash_new ();
47633965Sjdp
47733965Sjdp  {
47833965Sjdp    register const template *optab;
47933965Sjdp    register templates *core_optab;
48033965Sjdp    char *prev_name;
48133965Sjdp
48233965Sjdp    optab = i386_optab;		/* setup for loop */
48333965Sjdp    prev_name = optab->name;
48433965Sjdp    obstack_grow (&o, optab, sizeof (template));
48533965Sjdp    core_optab = (templates *) xmalloc (sizeof (templates));
48633965Sjdp
48733965Sjdp    for (optab++; optab < i386_optab_end; optab++)
48833965Sjdp      {
48933965Sjdp	if (!strcmp (optab->name, prev_name))
49033965Sjdp	  {
49133965Sjdp	    /* same name as before --> append to current template list */
49233965Sjdp	    obstack_grow (&o, optab, sizeof (template));
49333965Sjdp	  }
49433965Sjdp	else
49533965Sjdp	  {
49633965Sjdp	    /* different name --> ship out current template list;
49733965Sjdp	       add to hash table; & begin anew */
49833965Sjdp	    /* Note: end must be set before start! since obstack_next_free
49933965Sjdp	       changes upon opstack_finish */
50033965Sjdp	    core_optab->end = (template *) obstack_next_free (&o);
50133965Sjdp	    core_optab->start = (template *) obstack_finish (&o);
50233965Sjdp	    hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
50333965Sjdp	    if (hash_err)
50433965Sjdp	      {
50533965Sjdp	      hash_error:
50633965Sjdp		as_fatal ("Internal Error:  Can't hash %s: %s", prev_name,
50733965Sjdp			  hash_err);
50833965Sjdp	      }
50933965Sjdp	    prev_name = optab->name;
51033965Sjdp	    core_optab = (templates *) xmalloc (sizeof (templates));
51133965Sjdp	    obstack_grow (&o, optab, sizeof (template));
51233965Sjdp	  }
51333965Sjdp      }
51433965Sjdp  }
51533965Sjdp
51633965Sjdp  /* initialize reg_hash hash table */
51733965Sjdp  reg_hash = hash_new ();
51833965Sjdp  {
51933965Sjdp    register const reg_entry *regtab;
52033965Sjdp
52133965Sjdp    for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
52233965Sjdp      {
52333965Sjdp	hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
52433965Sjdp	if (hash_err)
52533965Sjdp	  goto hash_error;
52633965Sjdp      }
52733965Sjdp  }
52833965Sjdp
52933965Sjdp  esp = (reg_entry *) hash_find (reg_hash, "esp");
53033965Sjdp  ebp = (reg_entry *) hash_find (reg_hash, "ebp");
53133965Sjdp
53233965Sjdp  /* initialize reg_hash hash table */
53333965Sjdp  prefix_hash = hash_new ();
53433965Sjdp  {
53533965Sjdp    register const prefix_entry *prefixtab;
53633965Sjdp
53733965Sjdp    for (prefixtab = i386_prefixtab;
53833965Sjdp	 prefixtab < i386_prefixtab_end; prefixtab++)
53933965Sjdp      {
54033965Sjdp	hash_err = hash_insert (prefix_hash, prefixtab->prefix_name,
54133965Sjdp				(PTR) prefixtab);
54233965Sjdp	if (hash_err)
54333965Sjdp	  goto hash_error;
54433965Sjdp      }
54533965Sjdp  }
54633965Sjdp
54733965Sjdp  /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
54833965Sjdp  {
54933965Sjdp    register int c;
55033965Sjdp    register char *p;
55133965Sjdp
55233965Sjdp    for (c = 0; c < 256; c++)
55333965Sjdp      {
55433965Sjdp	if (islower (c) || isdigit (c))
55533965Sjdp	  {
55633965Sjdp	    opcode_chars[c] = c;
55733965Sjdp	    register_chars[c] = c;
55833965Sjdp	  }
55933965Sjdp	else if (isupper (c))
56033965Sjdp	  {
56133965Sjdp	    opcode_chars[c] = tolower (c);
56233965Sjdp	    register_chars[c] = opcode_chars[c];
56333965Sjdp	  }
56433965Sjdp	else if (c == PREFIX_SEPERATOR)
56533965Sjdp	  {
56633965Sjdp	    opcode_chars[c] = c;
56733965Sjdp	  }
56833965Sjdp	else if (c == ')' || c == '(')
56933965Sjdp	  {
57033965Sjdp	    register_chars[c] = c;
57133965Sjdp	  }
57233965Sjdp
57333965Sjdp	if (isupper (c) || islower (c) || isdigit (c))
57433965Sjdp	  operand_chars[c] = c;
57533965Sjdp
57633965Sjdp	if (isdigit (c) || c == '-')
57733965Sjdp	  digit_chars[c] = c;
57833965Sjdp
57933965Sjdp	if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
58033965Sjdp	  identifier_chars[c] = c;
58133965Sjdp
58233965Sjdp#ifdef LEX_AT
58333965Sjdp	identifier_chars['@'] = '@';
58433965Sjdp#endif
58533965Sjdp
58633965Sjdp	if (c == ' ' || c == '\t')
58733965Sjdp	  space_chars[c] = c;
58833965Sjdp      }
58933965Sjdp
59033965Sjdp    for (p = operand_special_chars; *p != '\0'; p++)
59133965Sjdp      operand_chars[(unsigned char) *p] = *p;
59233965Sjdp  }
59333965Sjdp
59433965Sjdp#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
59533965Sjdp  if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
59633965Sjdp    {
59733965Sjdp      record_alignment (text_section, 2);
59833965Sjdp      record_alignment (data_section, 2);
59933965Sjdp      record_alignment (bss_section, 2);
60033965Sjdp    }
60133965Sjdp#endif
60233965Sjdp}
60333965Sjdp
60433965Sjdpvoid
60533965Sjdpi386_print_statistics (file)
60633965Sjdp     FILE *file;
60733965Sjdp{
60833965Sjdp  hash_print_statistics (file, "i386 opcode", op_hash);
60933965Sjdp  hash_print_statistics (file, "i386 register", reg_hash);
61033965Sjdp  hash_print_statistics (file, "i386 prefix", prefix_hash);
61133965Sjdp}
61233965Sjdp
61333965Sjdp
61433965Sjdp#ifdef DEBUG386
61533965Sjdp
61633965Sjdp/* debugging routines for md_assemble */
61733965Sjdpstatic void pi PARAMS ((char *, i386_insn *));
61833965Sjdpstatic void pte PARAMS ((template *));
61933965Sjdpstatic void pt PARAMS ((unsigned int));
62033965Sjdpstatic void pe PARAMS ((expressionS *));
62133965Sjdpstatic void ps PARAMS ((symbolS *));
62233965Sjdp
62333965Sjdpstatic void
62433965Sjdppi (line, x)
62533965Sjdp     char *line;
62633965Sjdp     i386_insn *x;
62733965Sjdp{
62833965Sjdp  register template *p;
62933965Sjdp  int i;
63033965Sjdp
63133965Sjdp  fprintf (stdout, "%s: template ", line);
63233965Sjdp  pte (&x->tm);
63333965Sjdp  fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
63433965Sjdp	   x->rm.mode, x->rm.reg, x->rm.regmem);
63533965Sjdp  fprintf (stdout, " base %x  index %x  scale %x\n",
63633965Sjdp	   x->bi.base, x->bi.index, x->bi.scale);
63733965Sjdp  for (i = 0; i < x->operands; i++)
63833965Sjdp    {
63933965Sjdp      fprintf (stdout, "    #%d:  ", i + 1);
64033965Sjdp      pt (x->types[i]);
64133965Sjdp      fprintf (stdout, "\n");
64233965Sjdp      if (x->types[i]
64333965Sjdp	  & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX))
64433965Sjdp	fprintf (stdout, "%s\n", x->regs[i]->reg_name);
64533965Sjdp      if (x->types[i] & Imm)
64633965Sjdp	pe (x->imms[i]);
64733965Sjdp      if (x->types[i] & (Disp | Abs))
64833965Sjdp	pe (x->disps[i]);
64933965Sjdp    }
65033965Sjdp}
65133965Sjdp
65233965Sjdpstatic void
65333965Sjdppte (t)
65433965Sjdp     template *t;
65533965Sjdp{
65633965Sjdp  int i;
65733965Sjdp  fprintf (stdout, " %d operands ", t->operands);
65833965Sjdp  fprintf (stdout, "opcode %x ",
65933965Sjdp	   t->base_opcode);
66033965Sjdp  if (t->extension_opcode != None)
66133965Sjdp    fprintf (stdout, "ext %x ", t->extension_opcode);
66233965Sjdp  if (t->opcode_modifier & D)
66333965Sjdp    fprintf (stdout, "D");
66433965Sjdp  if (t->opcode_modifier & W)
66533965Sjdp    fprintf (stdout, "W");
66633965Sjdp  fprintf (stdout, "\n");
66733965Sjdp  for (i = 0; i < t->operands; i++)
66833965Sjdp    {
66933965Sjdp      fprintf (stdout, "    #%d type ", i + 1);
67033965Sjdp      pt (t->operand_types[i]);
67133965Sjdp      fprintf (stdout, "\n");
67233965Sjdp    }
67333965Sjdp}
67433965Sjdp
67533965Sjdpstatic void
67633965Sjdppe (e)
67733965Sjdp     expressionS *e;
67833965Sjdp{
67933965Sjdp  fprintf (stdout, "    operation       %d\n", e->X_op);
68033965Sjdp  fprintf (stdout, "    add_number    %d (%x)\n",
68133965Sjdp	   e->X_add_number, e->X_add_number);
68233965Sjdp  if (e->X_add_symbol)
68333965Sjdp    {
68433965Sjdp      fprintf (stdout, "    add_symbol    ");
68533965Sjdp      ps (e->X_add_symbol);
68633965Sjdp      fprintf (stdout, "\n");
68733965Sjdp    }
68833965Sjdp  if (e->X_op_symbol)
68933965Sjdp    {
69033965Sjdp      fprintf (stdout, "    op_symbol    ");
69133965Sjdp      ps (e->X_op_symbol);
69233965Sjdp      fprintf (stdout, "\n");
69333965Sjdp    }
69433965Sjdp}
69533965Sjdp
69633965Sjdpstatic void
69733965Sjdpps (s)
69833965Sjdp     symbolS *s;
69933965Sjdp{
70033965Sjdp  fprintf (stdout, "%s type %s%s",
70133965Sjdp	   S_GET_NAME (s),
70233965Sjdp	   S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
70333965Sjdp	   segment_name (S_GET_SEGMENT (s)));
70433965Sjdp}
70533965Sjdp
70633965Sjdpstruct type_name
70733965Sjdp  {
70833965Sjdp    unsigned int mask;
70933965Sjdp    char *tname;
71033965Sjdp  }
71133965Sjdp
71233965Sjdptype_names[] =
71333965Sjdp{
71433965Sjdp  { Reg8, "r8" },
71533965Sjdp  { Reg16, "r16" },
71633965Sjdp  { Reg32, "r32" },
71733965Sjdp  { Imm8, "i8" },
71833965Sjdp  { Imm8S, "i8s" },
71933965Sjdp  { Imm16, "i16" },
72033965Sjdp  { Imm32, "i32" },
72133965Sjdp  { Mem8, "Mem8" },
72233965Sjdp  { Mem16, "Mem16" },
72333965Sjdp  { Mem32, "Mem32" },
72433965Sjdp  { BaseIndex, "BaseIndex" },
72533965Sjdp  { Abs8, "Abs8" },
72633965Sjdp  { Abs16, "Abs16" },
72733965Sjdp  { Abs32, "Abs32" },
72833965Sjdp  { Disp8, "d8" },
72933965Sjdp  { Disp16, "d16" },
73033965Sjdp  { Disp32, "d32" },
73133965Sjdp  { SReg2, "SReg2" },
73233965Sjdp  { SReg3, "SReg3" },
73333965Sjdp  { Acc, "Acc" },
73433965Sjdp  { InOutPortReg, "InOutPortReg" },
73533965Sjdp  { ShiftCount, "ShiftCount" },
73633965Sjdp  { Imm1, "i1" },
73733965Sjdp  { Control, "control reg" },
73833965Sjdp  { Test, "test reg" },
73933965Sjdp  { FloatReg, "FReg" },
74033965Sjdp  { FloatAcc, "FAcc" },
74133965Sjdp  { JumpAbsolute, "Jump Absolute" },
74233965Sjdp  { RegMMX, "rMMX" },
74333965Sjdp  { 0, "" }
74433965Sjdp};
74533965Sjdp
74633965Sjdpstatic void
74733965Sjdppt (t)
74833965Sjdp     unsigned int t;
74933965Sjdp{
75033965Sjdp  register struct type_name *ty;
75133965Sjdp
75233965Sjdp  if (t == Unknown)
75333965Sjdp    {
75433965Sjdp      fprintf (stdout, "Unknown");
75533965Sjdp    }
75633965Sjdp  else
75733965Sjdp    {
75833965Sjdp      for (ty = type_names; ty->mask; ty++)
75933965Sjdp	if (t & ty->mask)
76033965Sjdp	  fprintf (stdout, "%s, ", ty->tname);
76133965Sjdp    }
76233965Sjdp  fflush (stdout);
76333965Sjdp}
76433965Sjdp
76533965Sjdp#endif /* DEBUG386 */
76633965Sjdp
76733965Sjdp#ifdef BFD_ASSEMBLER
76833965Sjdpstatic bfd_reloc_code_real_type
76933965Sjdpreloc (size, pcrel, other)
77033965Sjdp     int size;
77133965Sjdp     int pcrel;
77233965Sjdp     bfd_reloc_code_real_type other;
77333965Sjdp{
77433965Sjdp  if (other != NO_RELOC) return other;
77533965Sjdp
77633965Sjdp  if (pcrel)
77733965Sjdp    switch (size)
77833965Sjdp      {
77933965Sjdp      case 1: return BFD_RELOC_8_PCREL;
78033965Sjdp      case 2: return BFD_RELOC_16_PCREL;
78133965Sjdp      case 4: return BFD_RELOC_32_PCREL;
78233965Sjdp      }
78333965Sjdp  else
78433965Sjdp    switch (size)
78533965Sjdp      {
78633965Sjdp      case 1: return BFD_RELOC_8;
78733965Sjdp      case 2: return BFD_RELOC_16;
78833965Sjdp      case 4: return BFD_RELOC_32;
78933965Sjdp      }
79033965Sjdp
79133965Sjdp  as_bad ("Can not do %d byte %srelocation", size,
79233965Sjdp	  pcrel ? "pc-relative " : "");
79333965Sjdp  return BFD_RELOC_NONE;
79433965Sjdp}
79533965Sjdp
79633965Sjdp/*
79733965Sjdp * Here we decide which fixups can be adjusted to make them relative to
79833965Sjdp * the beginning of the section instead of the symbol.  Basically we need
79933965Sjdp * to make sure that the dynamic relocations are done correctly, so in
80033965Sjdp * some cases we force the original symbol to be used.
80133965Sjdp */
80233965Sjdpint
80333965Sjdptc_i386_fix_adjustable(fixP)
80433965Sjdp     fixS * fixP;
80533965Sjdp{
80638891Sjdp#ifdef OBJ_ELF
80733965Sjdp  /* Prevent all adjustments to global symbols. */
80833965Sjdp  if (S_IS_EXTERN (fixP->fx_addsy))
80933965Sjdp    return 0;
81033965Sjdp  if (S_IS_WEAK (fixP->fx_addsy))
81133965Sjdp    return 0;
81233965Sjdp#endif /* ! defined (OBJ_AOUT) */
81333965Sjdp  /* adjust_reloc_syms doesn't know about the GOT */
81433965Sjdp  if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
81533965Sjdp      || fixP->fx_r_type == BFD_RELOC_386_PLT32
81633965Sjdp      || fixP->fx_r_type == BFD_RELOC_386_GOT32)
81733965Sjdp    return 0;
81833965Sjdp  return 1;
81933965Sjdp}
82033965Sjdp#else
82133965Sjdp#define reloc(SIZE,PCREL,OTHER)	0
82233965Sjdp#define BFD_RELOC_32		0
82333965Sjdp#define BFD_RELOC_32_PCREL	0
82433965Sjdp#define BFD_RELOC_386_PLT32	0
82533965Sjdp#define BFD_RELOC_386_GOT32	0
82633965Sjdp#define BFD_RELOC_386_GOTOFF	0
82733965Sjdp#endif
82833965Sjdp
82933965Sjdp/* This is the guts of the machine-dependent assembler.  LINE points to a
83033965Sjdp   machine dependent instruction.  This function is supposed to emit
83133965Sjdp   the frags/bytes it assembles to.  */
83233965Sjdp
83333965Sjdpvoid
83433965Sjdpmd_assemble (line)
83533965Sjdp     char *line;
83633965Sjdp{
83738891Sjdp  /* Points to template once we've found it. */
83838891Sjdp  const template *t;
83933965Sjdp
84033965Sjdp  /* Count the size of the instruction generated.  */
84133965Sjdp  int insn_size = 0;
84233965Sjdp
84333965Sjdp  /* Possible templates for current insn */
84433965Sjdp  templates *current_templates = (templates *) 0;
84533965Sjdp
84633965Sjdp  int j;
84733965Sjdp
84838891Sjdp  /* Wait prefix needs to come before any other prefixes, so handle it
84938891Sjdp     specially.  wait_prefix will hold the opcode modifier flag FWait
85038891Sjdp     if a wait prefix is given.  */
85138891Sjdp  int wait_prefix = 0;
85238891Sjdp
85333965Sjdp  /* Initialize globals. */
85433965Sjdp  memset (&i, '\0', sizeof (i));
85533965Sjdp  for (j = 0; j < MAX_OPERANDS; j++)
85633965Sjdp    i.disp_reloc[j] = NO_RELOC;
85733965Sjdp  memset (disp_expressions, '\0', sizeof (disp_expressions));
85833965Sjdp  memset (im_expressions, '\0', sizeof (im_expressions));
85933965Sjdp  save_stack_p = save_stack;	/* reset stack pointer */
86033965Sjdp
86138891Sjdp  /* First parse an opcode & call i386_operand for the operands.
86233965Sjdp     We assume that the scrubber has arranged it so that line[0] is the valid
86333965Sjdp     start of a (possibly prefixed) opcode. */
86433965Sjdp  {
86533965Sjdp    char *l = line;
86633965Sjdp
86733965Sjdp    /* 1 if operand is pending after ','. */
86833965Sjdp    unsigned int expecting_operand = 0;
86933965Sjdp    /* 1 if we found a prefix only acceptable with string insns. */
87033965Sjdp    unsigned int expecting_string_instruction = 0;
87133965Sjdp    /* Non-zero if operand parens not balanced. */
87233965Sjdp    unsigned int paren_not_balanced;
87333965Sjdp    char *token_start = l;
87433965Sjdp
87533965Sjdp    while (!is_space_char (*l) && *l != END_OF_INSN)
87633965Sjdp      {
87733965Sjdp	if (!is_opcode_char (*l))
87833965Sjdp	  {
87933965Sjdp	    as_bad ("invalid character %s in opcode", output_invalid (*l));
88033965Sjdp	    return;
88133965Sjdp	  }
88233965Sjdp	else if (*l != PREFIX_SEPERATOR)
88333965Sjdp	  {
88433965Sjdp	    *l = opcode_chars[(unsigned char) *l];	/* fold case of opcodes */
88533965Sjdp	    l++;
88633965Sjdp	  }
88733965Sjdp	else
88833965Sjdp	  {
88933965Sjdp	    /* This opcode's got a prefix.  */
89033965Sjdp	    unsigned int q;
89133965Sjdp	    prefix_entry *prefix;
89233965Sjdp
89333965Sjdp	    if (l == token_start)
89433965Sjdp	      {
89533965Sjdp		as_bad ("expecting prefix; got nothing");
89633965Sjdp		return;
89733965Sjdp	      }
89833965Sjdp	    END_STRING_AND_SAVE (l);
89933965Sjdp	    prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
90033965Sjdp	    if (!prefix)
90133965Sjdp	      {
90233965Sjdp		as_bad ("no such opcode prefix ('%s')", token_start);
90333965Sjdp		return;
90433965Sjdp	      }
90533965Sjdp	    RESTORE_END_STRING (l);
90633965Sjdp	    /* check for repeated prefix */
90733965Sjdp	    for (q = 0; q < i.prefixes; q++)
90833965Sjdp	      if (i.prefix[q] == prefix->prefix_code)
90933965Sjdp		{
91033965Sjdp		  as_bad ("same prefix used twice; you don't really want this!");
91133965Sjdp		  return;
91233965Sjdp		}
91338891Sjdp	    if (prefix->prefix_code == FWAIT_OPCODE)
91433965Sjdp	      {
91538891Sjdp		if (wait_prefix != 0)
91638891Sjdp		  {
91738891Sjdp		    as_bad ("same prefix used twice; you don't really want this!");
91838891Sjdp		    return;
91938891Sjdp		  }
92038891Sjdp		wait_prefix = FWait;
92133965Sjdp	      }
92238891Sjdp	    else
92338891Sjdp	      {
92438891Sjdp		if (i.prefixes == MAX_PREFIXES)
92538891Sjdp		  {
92638891Sjdp		    as_bad ("too many opcode prefixes");
92738891Sjdp		    return;
92838891Sjdp		  }
92938891Sjdp		i.prefix[i.prefixes++] = prefix->prefix_code;
93038891Sjdp		if (prefix->prefix_code == REPE
93138891Sjdp		    || prefix->prefix_code == REPNE)
93238891Sjdp		  expecting_string_instruction = 1;
93338891Sjdp	      }
93433965Sjdp	    /* skip past PREFIX_SEPERATOR and reset token_start */
93533965Sjdp	    token_start = ++l;
93633965Sjdp	  }
93733965Sjdp      }
93833965Sjdp    END_STRING_AND_SAVE (l);
93933965Sjdp    if (token_start == l)
94033965Sjdp      {
94133965Sjdp	as_bad ("expecting opcode; got nothing");
94233965Sjdp	return;
94333965Sjdp      }
94433965Sjdp
94533965Sjdp    /* Lookup insn in hash; try intel & att naming conventions if appropriate;
94633965Sjdp       that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
94733965Sjdp    current_templates = (templates *) hash_find (op_hash, token_start);
94833965Sjdp    if (!current_templates)
94933965Sjdp      {
95033965Sjdp	int last_index = strlen (token_start) - 1;
95133965Sjdp	char last_char = token_start[last_index];
95233965Sjdp	switch (last_char)
95333965Sjdp	  {
95433965Sjdp	  case DWORD_OPCODE_SUFFIX:
95533965Sjdp	  case WORD_OPCODE_SUFFIX:
95633965Sjdp	  case BYTE_OPCODE_SUFFIX:
95733965Sjdp	    token_start[last_index] = '\0';
95833965Sjdp	    current_templates = (templates *) hash_find (op_hash, token_start);
95933965Sjdp	    token_start[last_index] = last_char;
96033965Sjdp	    i.suffix = last_char;
96133965Sjdp	  }
96233965Sjdp	if (!current_templates)
96333965Sjdp	  {
96433965Sjdp	    as_bad ("no such 386 instruction: `%s'", token_start);
96533965Sjdp	    return;
96633965Sjdp	  }
96733965Sjdp      }
96833965Sjdp    RESTORE_END_STRING (l);
96933965Sjdp
97033965Sjdp    /* check for rep/repne without a string instruction */
97133965Sjdp    if (expecting_string_instruction &&
97233965Sjdp	!IS_STRING_INSTRUCTION (current_templates->
97333965Sjdp				start->base_opcode))
97433965Sjdp      {
97533965Sjdp	as_bad ("expecting string instruction after rep/repne");
97633965Sjdp	return;
97733965Sjdp      }
97833965Sjdp
97933965Sjdp    /* There may be operands to parse. */
98033965Sjdp    if (*l != END_OF_INSN &&
98133965Sjdp	/* For string instructions, we ignore any operands if given.  This
98233965Sjdp	   kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
98333965Sjdp	   the operands are always going to be the same, and are not really
98433965Sjdp	   encoded in machine code. */
98533965Sjdp	!IS_STRING_INSTRUCTION (current_templates->
98633965Sjdp				start->base_opcode))
98733965Sjdp      {
98833965Sjdp	/* parse operands */
98933965Sjdp	do
99033965Sjdp	  {
99133965Sjdp	    /* skip optional white space before operand */
99233965Sjdp	    while (!is_operand_char (*l) && *l != END_OF_INSN)
99333965Sjdp	      {
99433965Sjdp		if (!is_space_char (*l))
99533965Sjdp		  {
99633965Sjdp		    as_bad ("invalid character %s before %s operand",
99733965Sjdp			    output_invalid (*l),
99833965Sjdp			    ordinal_names[i.operands]);
99933965Sjdp		    return;
100033965Sjdp		  }
100133965Sjdp		l++;
100233965Sjdp	      }
100333965Sjdp	    token_start = l;	/* after white space */
100433965Sjdp	    paren_not_balanced = 0;
100533965Sjdp	    while (paren_not_balanced || *l != ',')
100633965Sjdp	      {
100733965Sjdp		if (*l == END_OF_INSN)
100833965Sjdp		  {
100933965Sjdp		    if (paren_not_balanced)
101033965Sjdp		      {
101133965Sjdp			as_bad ("unbalanced parenthesis in %s operand.",
101233965Sjdp				ordinal_names[i.operands]);
101333965Sjdp			return;
101433965Sjdp		      }
101533965Sjdp		    else
101633965Sjdp		      break;	/* we are done */
101733965Sjdp		  }
101833965Sjdp		else if (!is_operand_char (*l) && !is_space_char (*l))
101933965Sjdp		  {
102033965Sjdp		    as_bad ("invalid character %s in %s operand",
102133965Sjdp			    output_invalid (*l),
102233965Sjdp			    ordinal_names[i.operands]);
102333965Sjdp		    return;
102433965Sjdp		  }
102533965Sjdp		if (*l == '(')
102633965Sjdp		  ++paren_not_balanced;
102733965Sjdp		if (*l == ')')
102833965Sjdp		  --paren_not_balanced;
102933965Sjdp		l++;
103033965Sjdp	      }
103133965Sjdp	    if (l != token_start)
103233965Sjdp	      {			/* yes, we've read in another operand */
103333965Sjdp		unsigned int operand_ok;
103433965Sjdp		this_operand = i.operands++;
103533965Sjdp		if (i.operands > MAX_OPERANDS)
103633965Sjdp		  {
103733965Sjdp		    as_bad ("spurious operands; (%d operands/instruction max)",
103833965Sjdp			    MAX_OPERANDS);
103933965Sjdp		    return;
104033965Sjdp		  }
104133965Sjdp		/* now parse operand adding info to 'i' as we go along */
104233965Sjdp		END_STRING_AND_SAVE (l);
104333965Sjdp		operand_ok = i386_operand (token_start);
104433965Sjdp		RESTORE_END_STRING (l);	/* restore old contents */
104533965Sjdp		if (!operand_ok)
104633965Sjdp		  return;
104733965Sjdp	      }
104833965Sjdp	    else
104933965Sjdp	      {
105033965Sjdp		if (expecting_operand)
105133965Sjdp		  {
105233965Sjdp		  expecting_operand_after_comma:
105333965Sjdp		    as_bad ("expecting operand after ','; got nothing");
105433965Sjdp		    return;
105533965Sjdp		  }
105633965Sjdp		if (*l == ',')
105733965Sjdp		  {
105833965Sjdp		    as_bad ("expecting operand before ','; got nothing");
105933965Sjdp		    return;
106033965Sjdp		  }
106133965Sjdp	      }
106233965Sjdp
106333965Sjdp	    /* now *l must be either ',' or END_OF_INSN */
106433965Sjdp	    if (*l == ',')
106533965Sjdp	      {
106633965Sjdp		if (*++l == END_OF_INSN)
106733965Sjdp		  {		/* just skip it, if it's \n complain */
106833965Sjdp		    goto expecting_operand_after_comma;
106933965Sjdp		  }
107033965Sjdp		expecting_operand = 1;
107133965Sjdp	      }
107233965Sjdp	  }
107333965Sjdp	while (*l != END_OF_INSN);	/* until we get end of insn */
107433965Sjdp      }
107533965Sjdp  }
107633965Sjdp
107733965Sjdp  /* Now we've parsed the opcode into a set of templates, and have the
107833965Sjdp     operands at hand.
107933965Sjdp
108033965Sjdp     Next, we find a template that matches the given insn,
108133965Sjdp     making sure the overlap of the given operands types is consistent
108233965Sjdp     with the template operand types. */
108333965Sjdp
108433965Sjdp#define MATCH(overlap,given_type) \
108533965Sjdp	(overlap && \
108633965Sjdp	 (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
108733965Sjdp	   == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
108833965Sjdp	  || (overlap == InOutPortReg)))
108933965Sjdp
109033965Sjdp
109133965Sjdp  /* If m0 and m1 are register matches they must be consistent
109233965Sjdp     with the expected operand types t0 and t1.
109333965Sjdp     That is, if both m0 & m1 are register matches
109433965Sjdp     i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
109533965Sjdp     then, either 1. or 2. must be true:
109633965Sjdp     1. the expected operand type register overlap is null:
109733965Sjdp     (t0 & t1 & Reg) == 0
109833965Sjdp     AND
109933965Sjdp     the given register overlap is null:
110033965Sjdp     (m0 & m1 & Reg) == 0
110133965Sjdp     2. the expected operand type register overlap == the given
110233965Sjdp     operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
110333965Sjdp     */
110433965Sjdp#define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
110533965Sjdp	    ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
110633965Sjdp	     ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
110733965Sjdp	      ((t0 & t1) & (m0 & m1) & (Reg)) \
110833965Sjdp	      ) : 1)
110933965Sjdp  {
111033965Sjdp    register unsigned int overlap0, overlap1;
111133965Sjdp    expressionS *exp;
111233965Sjdp    unsigned int overlap2;
111333965Sjdp    unsigned int found_reverse_match;
111433965Sjdp
111533965Sjdp    overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
111633965Sjdp    for (t = current_templates->start;
111733965Sjdp	 t < current_templates->end;
111833965Sjdp	 t++)
111933965Sjdp      {
112033965Sjdp	/* must have right number of operands */
112133965Sjdp	if (i.operands != t->operands)
112233965Sjdp	  continue;
112333965Sjdp	else if (!t->operands)
112433965Sjdp	  break;		/* 0 operands always matches */
112533965Sjdp
112633965Sjdp	overlap0 = i.types[0] & t->operand_types[0];
112733965Sjdp	switch (t->operands)
112833965Sjdp	  {
112933965Sjdp	  case 1:
113033965Sjdp	    if (!MATCH (overlap0, i.types[0]))
113133965Sjdp	      continue;
113233965Sjdp	    break;
113333965Sjdp	  case 2:
113433965Sjdp	  case 3:
113533965Sjdp	    overlap1 = i.types[1] & t->operand_types[1];
113633965Sjdp	    if (!MATCH (overlap0, i.types[0]) ||
113733965Sjdp		!MATCH (overlap1, i.types[1]) ||
113833965Sjdp		!CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
113933965Sjdp					    t->operand_types[0],
114033965Sjdp					    t->operand_types[1]))
114133965Sjdp	      {
114233965Sjdp
114333965Sjdp		/* check if other direction is valid ... */
114433965Sjdp		if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
114533965Sjdp		  continue;
114633965Sjdp
114733965Sjdp		/* try reversing direction of operands */
114833965Sjdp		overlap0 = i.types[0] & t->operand_types[1];
114933965Sjdp		overlap1 = i.types[1] & t->operand_types[0];
115033965Sjdp		if (!MATCH (overlap0, i.types[0]) ||
115133965Sjdp		    !MATCH (overlap1, i.types[1]) ||
115233965Sjdp		    !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
115338891Sjdp						t->operand_types[1],
115438891Sjdp						t->operand_types[0]))
115533965Sjdp		  {
115633965Sjdp		    /* does not match either direction */
115733965Sjdp		    continue;
115833965Sjdp		  }
115933965Sjdp		/* found a reverse match here -- slip through */
116033965Sjdp		/* found_reverse_match holds which of D or FloatD we've found */
116133965Sjdp		found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
116233965Sjdp	      }			/* endif: not forward match */
116333965Sjdp	    /* found either forward/reverse 2 operand match here */
116433965Sjdp	    if (t->operands == 3)
116533965Sjdp	      {
116633965Sjdp		overlap2 = i.types[2] & t->operand_types[2];
116733965Sjdp		if (!MATCH (overlap2, i.types[2]) ||
116833965Sjdp		    !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
116933965Sjdp						t->operand_types[0],
117033965Sjdp						t->operand_types[2]) ||
117133965Sjdp		    !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
117233965Sjdp						t->operand_types[1],
117333965Sjdp						t->operand_types[2]))
117433965Sjdp		  continue;
117533965Sjdp	      }
117633965Sjdp	    /* found either forward/reverse 2 or 3 operand match here:
117733965Sjdp	       slip through to break */
117833965Sjdp	  }
117933965Sjdp	break;			/* we've found a match; break out of loop */
118033965Sjdp      }				/* for (t = ... */
118133965Sjdp    if (t == current_templates->end)
118233965Sjdp      {				/* we found no match */
118333965Sjdp	as_bad ("operands given don't match any known 386 instruction");
118433965Sjdp	return;
118533965Sjdp      }
118633965Sjdp
118738891Sjdp    /* Copy the template we found.  */
118833965Sjdp    i.tm = *t;
118938891Sjdp    i.tm.opcode_modifier |= wait_prefix;
119033965Sjdp
119138891Sjdp    if (found_reverse_match)
119238891Sjdp      {
119338891Sjdp	i.tm.operand_types[0] = t->operand_types[1];
119438891Sjdp	i.tm.operand_types[1] = t->operand_types[0];
119538891Sjdp      }
119638891Sjdp
119733965Sjdp    /* If the matched instruction specifies an explicit opcode suffix,
119833965Sjdp       use it - and make sure none has already been specified.  */
119938891Sjdp    if (i.tm.opcode_modifier & (Data16|Data32))
120033965Sjdp      {
120133965Sjdp	if (i.suffix)
120233965Sjdp	  {
120333965Sjdp	    as_bad ("extraneous opcode suffix given");
120433965Sjdp	    return;
120533965Sjdp	  }
120638891Sjdp	if (i.tm.opcode_modifier & Data16)
120733965Sjdp	  i.suffix = WORD_OPCODE_SUFFIX;
120833965Sjdp	else
120933965Sjdp	  i.suffix = DWORD_OPCODE_SUFFIX;
121033965Sjdp      }
121133965Sjdp
121233965Sjdp    /* If there's no opcode suffix we try to invent one based on register
121333965Sjdp       operands. */
121433965Sjdp    if (!i.suffix && i.reg_operands)
121533965Sjdp      {
121633965Sjdp	/* We take i.suffix from the LAST register operand specified.  This
121733965Sjdp	   assumes that the last register operands is the destination register
121833965Sjdp	   operand. */
121933965Sjdp	int op;
122033965Sjdp	for (op = 0; op < MAX_OPERANDS; op++)
122133965Sjdp	  if (i.types[op] & Reg)
122233965Sjdp	    {
122333965Sjdp	      i.suffix = ((i.types[op] & Reg8) ? BYTE_OPCODE_SUFFIX :
122433965Sjdp			  (i.types[op] & Reg16) ? WORD_OPCODE_SUFFIX :
122533965Sjdp			  DWORD_OPCODE_SUFFIX);
122633965Sjdp	    }
122733965Sjdp      }
122833965Sjdp    else if (i.suffix != 0
122933965Sjdp	     && i.reg_operands != 0
123033965Sjdp	     && (i.types[i.operands - 1] & Reg) != 0)
123133965Sjdp      {
123233965Sjdp	int bad;
123333965Sjdp
123433965Sjdp	/* If the last operand is a register, make sure it is
123533965Sjdp           compatible with the suffix.  */
123633965Sjdp
123733965Sjdp	bad = 0;
123833965Sjdp	switch (i.suffix)
123933965Sjdp	  {
124033965Sjdp	  default:
124133965Sjdp	    abort ();
124233965Sjdp	  case BYTE_OPCODE_SUFFIX:
124333965Sjdp	    /* If this is an eight bit register, it's OK.  If it's the
124433965Sjdp               16 or 32 bit version of an eight bit register, we will
124533965Sjdp               just use the low portion, and that's OK too.  */
124633965Sjdp	    if ((i.types[i.operands - 1] & Reg8) == 0
124733965Sjdp		&& i.regs[i.operands - 1]->reg_num >= 4)
124833965Sjdp	      bad = 1;
124933965Sjdp	    break;
125033965Sjdp	  case WORD_OPCODE_SUFFIX:
125133965Sjdp	  case DWORD_OPCODE_SUFFIX:
125233965Sjdp	    /* We don't insist on the presence or absence of the e
125333965Sjdp               prefix on the register, but we reject eight bit
125433965Sjdp               registers.  */
125533965Sjdp	    if ((i.types[i.operands - 1] & Reg8) != 0)
125633965Sjdp	      bad = 1;
125733965Sjdp	  }
125833965Sjdp	if (bad)
125933965Sjdp	  as_bad ("register does not match opcode suffix");
126033965Sjdp      }
126133965Sjdp
126233965Sjdp    /* Make still unresolved immediate matches conform to size of immediate
126333965Sjdp       given in i.suffix. Note:  overlap2 cannot be an immediate!
126433965Sjdp       We assume this. */
126533965Sjdp    if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
126633965Sjdp	&& overlap0 != Imm8 && overlap0 != Imm8S
126733965Sjdp	&& overlap0 != Imm16 && overlap0 != Imm32)
126833965Sjdp      {
126933965Sjdp	if (!i.suffix)
127033965Sjdp	  {
127133965Sjdp	    as_bad ("no opcode suffix given; can't determine immediate size");
127233965Sjdp	    return;
127333965Sjdp	  }
127433965Sjdp	overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
127533965Sjdp		     (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
127633965Sjdp      }
127733965Sjdp    if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
127833965Sjdp	&& overlap1 != Imm8 && overlap1 != Imm8S
127933965Sjdp	&& overlap1 != Imm16 && overlap1 != Imm32)
128033965Sjdp      {
128133965Sjdp	if (!i.suffix)
128233965Sjdp	  {
128333965Sjdp	    as_bad ("no opcode suffix given; can't determine immediate size");
128433965Sjdp	    return;
128533965Sjdp	  }
128633965Sjdp	overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
128733965Sjdp		     (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
128833965Sjdp      }
128933965Sjdp
129033965Sjdp    i.types[0] = overlap0;
129133965Sjdp    i.types[1] = overlap1;
129233965Sjdp    i.types[2] = overlap2;
129333965Sjdp
129433965Sjdp    if (overlap0 & ImplicitRegister)
129533965Sjdp      i.reg_operands--;
129633965Sjdp    if (overlap1 & ImplicitRegister)
129733965Sjdp      i.reg_operands--;
129833965Sjdp    if (overlap2 & ImplicitRegister)
129933965Sjdp      i.reg_operands--;
130033965Sjdp    if (overlap0 & Imm1)
130133965Sjdp      i.imm_operands = 0;	/* kludge for shift insns */
130233965Sjdp
130333965Sjdp    /* Finalize opcode.  First, we change the opcode based on the operand
130433965Sjdp       size given by i.suffix: we never have to change things for byte insns,
130533965Sjdp       or when no opcode suffix is need to size the operands. */
130633965Sjdp
130738891Sjdp    if (!i.suffix && (i.tm.opcode_modifier & W))
130833965Sjdp      {
130933965Sjdp	as_bad ("no opcode suffix given and no register operands; can't size instruction");
131033965Sjdp	return;
131133965Sjdp      }
131233965Sjdp
131333965Sjdp    if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
131433965Sjdp      {
131533965Sjdp	/* Select between byte and word/dword operations. */
131638891Sjdp	if (i.tm.opcode_modifier & W)
131738891Sjdp	  i.tm.base_opcode |= W;
131833965Sjdp	/* Now select between word & dword operations via the
131933965Sjdp				   operand size prefix. */
132033965Sjdp	if ((i.suffix == WORD_OPCODE_SUFFIX) ^ flag_16bit_code)
132133965Sjdp	  {
132233965Sjdp	    if (i.prefixes == MAX_PREFIXES)
132333965Sjdp	      {
132438891Sjdp		as_bad ("%d prefixes given and data size prefix gives too many prefixes",
132533965Sjdp			MAX_PREFIXES);
132633965Sjdp		return;
132733965Sjdp	      }
132833965Sjdp	    i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
132933965Sjdp	  }
133033965Sjdp      }
133133965Sjdp
133233965Sjdp    /* For insns with operands there are more diddles to do to the opcode. */
133333965Sjdp    if (i.operands)
133433965Sjdp      {
133533965Sjdp        /* Default segment register this instruction will use
133633965Sjdp	   for memory accesses.  0 means unknown.
133733965Sjdp	   This is only for optimizing out unnecessary segment overrides.  */
133833965Sjdp	const seg_entry *default_seg = 0;
133933965Sjdp
134033965Sjdp	/* True if this instruction uses a memory addressing mode,
134133965Sjdp	   and therefore may need an address-size prefix.  */
134233965Sjdp	int uses_mem_addrmode = 0;
134333965Sjdp
134433965Sjdp
134533965Sjdp	/* If we found a reverse match we must alter the opcode direction bit
134633965Sjdp	   found_reverse_match holds bit to set (different for int &
134733965Sjdp	   float insns). */
134833965Sjdp
134933965Sjdp	if (found_reverse_match)
135033965Sjdp	  {
135138891Sjdp	    i.tm.base_opcode |= found_reverse_match;
135233965Sjdp	  }
135333965Sjdp
135433965Sjdp	/* The imul $imm, %reg instruction is converted into
135533965Sjdp	   imul $imm, %reg, %reg. */
135638891Sjdp	if (i.tm.opcode_modifier & imulKludge)
135733965Sjdp	  {
135833965Sjdp	    /* Pretend we saw the 3 operand case. */
135933965Sjdp	    i.regs[2] = i.regs[1];
136033965Sjdp	    i.reg_operands = 2;
136133965Sjdp	  }
136233965Sjdp
136333965Sjdp	/* The clr %reg instruction is converted into xor %reg, %reg.  */
136438891Sjdp	if (i.tm.opcode_modifier & iclrKludge)
136533965Sjdp	  {
136633965Sjdp	    i.regs[1] = i.regs[0];
136733965Sjdp	    i.reg_operands = 2;
136833965Sjdp	  }
136933965Sjdp
137033965Sjdp	/* Certain instructions expect the destination to be in the i.rm.reg
137133965Sjdp	   field.  This is by far the exceptional case.  For these
137233965Sjdp	   instructions, if the source operand is a register, we must reverse
137333965Sjdp	   the i.rm.reg and i.rm.regmem fields.  We accomplish this by faking
137433965Sjdp	   that the two register operands were given in the reverse order. */
137538891Sjdp	if ((i.tm.opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
137633965Sjdp	  {
137733965Sjdp	    unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
137833965Sjdp	    unsigned int second_reg_operand = first_reg_operand + 1;
137933965Sjdp	    reg_entry *tmp = i.regs[first_reg_operand];
138033965Sjdp	    i.regs[first_reg_operand] = i.regs[second_reg_operand];
138133965Sjdp	    i.regs[second_reg_operand] = tmp;
138233965Sjdp	  }
138333965Sjdp
138438891Sjdp	if (i.tm.opcode_modifier & ShortForm)
138533965Sjdp	  {
138633965Sjdp	    /* The register or float register operand is in operand 0 or 1. */
138733965Sjdp	    unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
138833965Sjdp	    /* Register goes in low 3 bits of opcode. */
138938891Sjdp	    i.tm.base_opcode |= i.regs[op]->reg_num;
139033965Sjdp	  }
139138891Sjdp	else if (i.tm.opcode_modifier & ShortFormW)
139233965Sjdp	  {
139333965Sjdp	    /* Short form with 0x8 width bit.  Register is always dest. operand */
139438891Sjdp	    i.tm.base_opcode |= i.regs[1]->reg_num;
139533965Sjdp	    if (i.suffix == WORD_OPCODE_SUFFIX ||
139633965Sjdp		i.suffix == DWORD_OPCODE_SUFFIX)
139738891Sjdp	      i.tm.base_opcode |= 0x8;
139833965Sjdp	  }
139938891Sjdp	else if (i.tm.opcode_modifier & Seg2ShortForm)
140033965Sjdp	  {
140138891Sjdp	    if (i.tm.base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
140233965Sjdp	      {
140333965Sjdp		as_bad ("you can't 'pop cs' on the 386.");
140433965Sjdp		return;
140533965Sjdp	      }
140638891Sjdp	    i.tm.base_opcode |= (i.regs[0]->reg_num << 3);
140733965Sjdp	  }
140838891Sjdp	else if (i.tm.opcode_modifier & Seg3ShortForm)
140933965Sjdp	  {
141033965Sjdp	    /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
141133965Sjdp	       'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
141233965Sjdp	       So, only if i.regs[0]->reg_num == 5 (%gs) do we need
141333965Sjdp	       to change the opcode. */
141433965Sjdp	    if (i.regs[0]->reg_num == 5)
141538891Sjdp	      i.tm.base_opcode |= 0x08;
141633965Sjdp	  }
141738891Sjdp	else if ((i.tm.base_opcode & ~DW) == MOV_AX_DISP32)
141833965Sjdp	  {
141933965Sjdp	    /* This is a special non-modrm instruction
142033965Sjdp	       that addresses memory with a 32-bit displacement mode anyway,
142133965Sjdp	       and thus requires an address-size prefix if in 16-bit mode.  */
142233965Sjdp	    uses_mem_addrmode = 1;
142333965Sjdp	    default_seg = &ds;
142433965Sjdp	  }
142538891Sjdp	else if (i.tm.opcode_modifier & Modrm)
142633965Sjdp	  {
142738891Sjdp	    /* The opcode is completed (modulo i.tm.extension_opcode which
142838891Sjdp	       must be put into the modrm byte).
142938891Sjdp	       Now, we make the modrm & index base bytes based on all the
143038891Sjdp	       info we've collected. */
143133965Sjdp
143233965Sjdp	    /* i.reg_operands MUST be the number of real register operands;
143333965Sjdp	       implicit registers do not count. */
143433965Sjdp	    if (i.reg_operands == 2)
143533965Sjdp	      {
143633965Sjdp		unsigned int source, dest;
143733965Sjdp		source = ((i.types[0]
143833965Sjdp			   & (Reg
143933965Sjdp			      | SReg2
144033965Sjdp			      | SReg3
144133965Sjdp			      | Control
144233965Sjdp			      | Debug
144333965Sjdp			      | Test
144433965Sjdp			      | RegMMX))
144533965Sjdp			  ? 0 : 1);
144633965Sjdp		dest = source + 1;
144733965Sjdp		i.rm.mode = 3;
144833965Sjdp		/* We must be careful to make sure that all
144933965Sjdp		   segment/control/test/debug/MMX registers go into
145033965Sjdp		   the i.rm.reg field (despite the whether they are
145133965Sjdp		   source or destination operands). */
145233965Sjdp		if (i.regs[dest]->reg_type
145333965Sjdp		    & (SReg2 | SReg3 | Control | Debug | Test | RegMMX))
145433965Sjdp		  {
145533965Sjdp		    i.rm.reg = i.regs[dest]->reg_num;
145633965Sjdp		    i.rm.regmem = i.regs[source]->reg_num;
145733965Sjdp		  }
145833965Sjdp		else
145933965Sjdp		  {
146033965Sjdp		    i.rm.reg = i.regs[source]->reg_num;
146133965Sjdp		    i.rm.regmem = i.regs[dest]->reg_num;
146233965Sjdp		  }
146333965Sjdp	      }
146433965Sjdp	    else
146533965Sjdp	      {			/* if it's not 2 reg operands... */
146633965Sjdp		if (i.mem_operands)
146733965Sjdp		  {
146833965Sjdp		    unsigned int fake_zero_displacement = 0;
146933965Sjdp		    unsigned int op = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
147033965Sjdp
147133965Sjdp		    /* Encode memory operand into modrm byte and base index
147233965Sjdp		       byte. */
147333965Sjdp
147433965Sjdp		    if (i.base_reg == esp && !i.index_reg)
147533965Sjdp		      {
147633965Sjdp			/* <disp>(%esp) becomes two byte modrm with no index
147733965Sjdp			   register. */
147833965Sjdp			i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
147933965Sjdp			i.rm.mode = mode_from_disp_size (i.types[op]);
148033965Sjdp			i.bi.base = ESP_REG_NUM;
148133965Sjdp			i.bi.index = NO_INDEX_REGISTER;
148233965Sjdp			i.bi.scale = 0;	/* Must be zero! */
148333965Sjdp		      }
148433965Sjdp		    else if (i.base_reg == ebp && !i.index_reg)
148533965Sjdp		      {
148633965Sjdp			if (!(i.types[op] & Disp))
148733965Sjdp			  {
148833965Sjdp			    /* Must fake a zero byte displacement.  There is
148933965Sjdp			       no direct way to code '(%ebp)' directly. */
149033965Sjdp			    fake_zero_displacement = 1;
149133965Sjdp			    /* fake_zero_displacement code does not set this. */
149233965Sjdp			    i.types[op] |= Disp8;
149333965Sjdp			  }
149433965Sjdp			i.rm.mode = mode_from_disp_size (i.types[op]);
149533965Sjdp			i.rm.regmem = EBP_REG_NUM;
149633965Sjdp		      }
149733965Sjdp		    else if (!i.base_reg && (i.types[op] & BaseIndex))
149833965Sjdp		      {
149933965Sjdp			/* There are three cases here.
150033965Sjdp			   Case 1:  '<32bit disp>(,1)' -- indirect absolute.
150133965Sjdp			   (Same as cases 2 & 3 with NO index register)
150233965Sjdp			   Case 2:  <32bit disp> (,<index>) -- no base register with disp
150333965Sjdp			   Case 3:  (, <index>)       --- no base register;
150433965Sjdp			   no disp (must add 32bit 0 disp). */
150533965Sjdp			i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
150633965Sjdp			i.rm.mode = 0;	/* 32bit mode */
150733965Sjdp			i.bi.base = NO_BASE_REGISTER;
150833965Sjdp			i.types[op] &= ~Disp;
150933965Sjdp			i.types[op] |= Disp32;	/* Must be 32bit! */
151033965Sjdp			if (i.index_reg)
151133965Sjdp			  {	/* case 2 or case 3 */
151233965Sjdp			    i.bi.index = i.index_reg->reg_num;
151333965Sjdp			    i.bi.scale = i.log2_scale_factor;
151433965Sjdp			    if (i.disp_operands == 0)
151533965Sjdp			      fake_zero_displacement = 1;	/* case 3 */
151633965Sjdp			  }
151733965Sjdp			else
151833965Sjdp			  {
151933965Sjdp			    i.bi.index = NO_INDEX_REGISTER;
152033965Sjdp			    i.bi.scale = 0;
152133965Sjdp			  }
152233965Sjdp		      }
152333965Sjdp		    else if (i.disp_operands && !i.base_reg && !i.index_reg)
152433965Sjdp		      {
152533965Sjdp			/* Operand is just <32bit disp> */
152633965Sjdp			i.rm.regmem = EBP_REG_NUM;
152733965Sjdp			i.rm.mode = 0;
152833965Sjdp			i.types[op] &= ~Disp;
152933965Sjdp			i.types[op] |= Disp32;
153033965Sjdp		      }
153133965Sjdp		    else
153233965Sjdp		      {
153333965Sjdp			/* It's not a special case; rev'em up. */
153433965Sjdp			i.rm.regmem = i.base_reg->reg_num;
153533965Sjdp			i.rm.mode = mode_from_disp_size (i.types[op]);
153633965Sjdp			if (i.index_reg)
153733965Sjdp			  {
153833965Sjdp			    i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
153933965Sjdp			    i.bi.base = i.base_reg->reg_num;
154033965Sjdp			    i.bi.index = i.index_reg->reg_num;
154133965Sjdp			    i.bi.scale = i.log2_scale_factor;
154233965Sjdp			    if (i.base_reg == ebp && i.disp_operands == 0)
154333965Sjdp			      {	/* pace */
154433965Sjdp				fake_zero_displacement = 1;
154533965Sjdp				i.types[op] |= Disp8;
154633965Sjdp				i.rm.mode = mode_from_disp_size (i.types[op]);
154733965Sjdp			      }
154833965Sjdp			  }
154933965Sjdp		      }
155033965Sjdp		    if (fake_zero_displacement)
155133965Sjdp		      {
155233965Sjdp			/* Fakes a zero displacement assuming that i.types[op]
155333965Sjdp			   holds the correct displacement size. */
155433965Sjdp			exp = &disp_expressions[i.disp_operands++];
155533965Sjdp			i.disps[op] = exp;
155633965Sjdp			exp->X_op = O_constant;
155733965Sjdp			exp->X_add_number = 0;
155833965Sjdp			exp->X_add_symbol = (symbolS *) 0;
155933965Sjdp			exp->X_op_symbol = (symbolS *) 0;
156033965Sjdp		      }
156133965Sjdp
156233965Sjdp		    /* Find the default segment for the memory operand.
156333965Sjdp		       Used to optimize out explicit segment specifications.  */
156433965Sjdp		    if (i.seg)
156533965Sjdp		      {
156633965Sjdp			unsigned int seg_index;
156733965Sjdp
156833965Sjdp			if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
156933965Sjdp			  {
157033965Sjdp			    seg_index = (i.rm.mode << 3) | i.bi.base;
157133965Sjdp			    default_seg = two_byte_segment_defaults[seg_index];
157233965Sjdp			  }
157333965Sjdp			else
157433965Sjdp			  {
157533965Sjdp			    seg_index = (i.rm.mode << 3) | i.rm.regmem;
157633965Sjdp			    default_seg = one_byte_segment_defaults[seg_index];
157733965Sjdp			  }
157833965Sjdp		      }
157933965Sjdp		  }
158033965Sjdp
158133965Sjdp		/* Fill in i.rm.reg or i.rm.regmem field with register
158238891Sjdp		   operand (if any) based on i.tm.extension_opcode.
158338891Sjdp		   Again, we must be careful to make sure that
158438891Sjdp		   segment/control/debug/test/MMX registers are coded
158538891Sjdp		   into the i.rm.reg field. */
158633965Sjdp		if (i.reg_operands)
158733965Sjdp		  {
158833965Sjdp		    unsigned int op =
158933965Sjdp		      ((i.types[0]
159033965Sjdp			& (Reg | SReg2 | SReg3 | Control | Debug
159133965Sjdp			   | Test | RegMMX))
159233965Sjdp		       ? 0
159333965Sjdp		       : ((i.types[1]
159433965Sjdp			   & (Reg | SReg2 | SReg3 | Control | Debug
159533965Sjdp			      | Test | RegMMX))
159633965Sjdp			  ? 1
159733965Sjdp			  : 2));
159833965Sjdp		    /* If there is an extension opcode to put here, the
159933965Sjdp		       register number must be put into the regmem field. */
160038891Sjdp		    if (i.tm.extension_opcode != None)
160133965Sjdp		      i.rm.regmem = i.regs[op]->reg_num;
160233965Sjdp		    else
160333965Sjdp		      i.rm.reg = i.regs[op]->reg_num;
160433965Sjdp
160533965Sjdp		    /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
160633965Sjdp		       we must set it to 3 to indicate this is a register
160733965Sjdp		       operand int the regmem field */
160833965Sjdp		    if (!i.mem_operands)
160933965Sjdp		      i.rm.mode = 3;
161033965Sjdp		  }
161133965Sjdp
161233965Sjdp		/* Fill in i.rm.reg field with extension opcode (if any). */
161338891Sjdp		if (i.tm.extension_opcode != None)
161438891Sjdp		  i.rm.reg = i.tm.extension_opcode;
161533965Sjdp	      }
161633965Sjdp
161733965Sjdp	    if (i.rm.mode != 3)
161833965Sjdp	      uses_mem_addrmode = 1;
161933965Sjdp	  }
162033965Sjdp
162133965Sjdp	/* GAS currently doesn't support 16-bit memory addressing modes at all,
162233965Sjdp	   so if we're writing 16-bit code and using a memory addressing mode,
162333965Sjdp	   always spew out an address size prefix.  */
162433965Sjdp	if (uses_mem_addrmode && flag_16bit_code)
162533965Sjdp	  {
162633965Sjdp	    if (i.prefixes == MAX_PREFIXES)
162733965Sjdp	      {
162833965Sjdp	        as_bad ("%d prefixes given and address size override gives too many prefixes",
162933965Sjdp	        	MAX_PREFIXES);
163033965Sjdp	        return;
163133965Sjdp	      }
163233965Sjdp	    i.prefix[i.prefixes++] = ADDR_PREFIX_OPCODE;
163333965Sjdp	  }
163433965Sjdp
163533965Sjdp	/* If a segment was explicitly specified,
163633965Sjdp	   and the specified segment is not the default,
163733965Sjdp	   use an opcode prefix to select it.
163833965Sjdp	   If we never figured out what the default segment is,
163933965Sjdp	   then default_seg will be zero at this point,
164033965Sjdp	   and the specified segment prefix will always be used.  */
164133965Sjdp	if ((i.seg) && (i.seg != default_seg))
164233965Sjdp	  {
164333965Sjdp	    if (i.prefixes == MAX_PREFIXES)
164433965Sjdp	      {
164533965Sjdp	        as_bad ("%d prefixes given and %s segment override gives too many prefixes",
164633965Sjdp	    	    MAX_PREFIXES, i.seg->seg_name);
164733965Sjdp	        return;
164833965Sjdp	      }
164933965Sjdp	    i.prefix[i.prefixes++] = i.seg->seg_prefix;
165033965Sjdp	  }
165133965Sjdp      }
165233965Sjdp  }
165333965Sjdp
165433965Sjdp  /* Handle conversion of 'int $3' --> special int3 insn. */
165538891Sjdp  if (i.tm.base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
165633965Sjdp    {
165738891Sjdp      i.tm.base_opcode = INT3_OPCODE;
165833965Sjdp      i.imm_operands = 0;
165933965Sjdp    }
166033965Sjdp
166133965Sjdp  /* We are ready to output the insn. */
166233965Sjdp  {
166333965Sjdp    register char *p;
166433965Sjdp
166533965Sjdp    /* Output jumps. */
166638891Sjdp    if (i.tm.opcode_modifier & Jump)
166733965Sjdp      {
166833965Sjdp	unsigned long n = i.disps[0]->X_add_number;
166933965Sjdp
167033965Sjdp	if (i.disps[0]->X_op == O_constant)
167133965Sjdp	  {
167233965Sjdp	    if (fits_in_signed_byte (n))
167333965Sjdp	      {
167433965Sjdp		p = frag_more (2);
167533965Sjdp		insn_size += 2;
167638891Sjdp		p[0] = i.tm.base_opcode;
167733965Sjdp		p[1] = n;
167833965Sjdp	      }
167933965Sjdp	    else
168033965Sjdp	      {	/* It's an absolute word/dword displacement. */
168133965Sjdp
168233965Sjdp	        /* Use only 16-bit jumps for 16-bit code,
168333965Sjdp		   because text segments are limited to 64K anyway;
168433965Sjdp	           use only 32-bit jumps for 32-bit code,
168533965Sjdp		   because they're faster.  */
168633965Sjdp		int jmp_size = flag_16bit_code ? 2 : 4;
168733965Sjdp	      	if (flag_16bit_code && !fits_in_signed_word (n))
168833965Sjdp		  {
168933965Sjdp		    as_bad ("16-bit jump out of range");
169033965Sjdp		    return;
169133965Sjdp		  }
169233965Sjdp
169338891Sjdp		if (i.tm.base_opcode == JUMP_PC_RELATIVE)
169433965Sjdp		  {		/* pace */
169533965Sjdp		    /* unconditional jump */
169633965Sjdp		    p = frag_more (1 + jmp_size);
169733965Sjdp		    insn_size += 1 + jmp_size;
169833965Sjdp		    p[0] = (char) 0xe9;
169933965Sjdp		    md_number_to_chars (&p[1], (valueT) n, jmp_size);
170033965Sjdp		  }
170133965Sjdp		else
170233965Sjdp		  {
170333965Sjdp		    /* conditional jump */
170433965Sjdp		    p = frag_more (2 + jmp_size);
170533965Sjdp		    insn_size += 2 + jmp_size;
170633965Sjdp		    p[0] = TWO_BYTE_OPCODE_ESCAPE;
170738891Sjdp		    p[1] = i.tm.base_opcode + 0x10;
170833965Sjdp		    md_number_to_chars (&p[2], (valueT) n, jmp_size);
170933965Sjdp		  }
171033965Sjdp	      }
171133965Sjdp	  }
171233965Sjdp	else
171333965Sjdp	  {
171433965Sjdp	    if (flag_16bit_code)
171533965Sjdp	      {
171633965Sjdp	        FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
171733965Sjdp		insn_size += 1;
171833965Sjdp	      }
171933965Sjdp
172033965Sjdp	    /* It's a symbol; end frag & setup for relax.
172133965Sjdp	       Make sure there are more than 6 chars left in the current frag;
172233965Sjdp	       if not we'll have to start a new one. */
172333965Sjdp	    frag_grow (7);
172433965Sjdp	    p = frag_more (1);
172533965Sjdp	    insn_size += 1;
172638891Sjdp	    p[0] = i.tm.base_opcode;
172733965Sjdp	    frag_var (rs_machine_dependent,
172833965Sjdp		      6,	/* 2 opcode/prefix + 4 displacement */
172933965Sjdp		      1,
173033965Sjdp		      ((unsigned char) *p == JUMP_PC_RELATIVE
173133965Sjdp		       ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
173233965Sjdp		       : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
173333965Sjdp		      i.disps[0]->X_add_symbol,
173433965Sjdp		      (offsetT) n, p);
173533965Sjdp	  }
173633965Sjdp      }
173738891Sjdp    else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
173833965Sjdp      {
173938891Sjdp	int size = (i.tm.opcode_modifier & JumpByte) ? 1 : 4;
174033965Sjdp	unsigned long n = i.disps[0]->X_add_number;
174133965Sjdp	unsigned char *q;
174233965Sjdp
174333965Sjdp	/* The jcx/jecx instruction might need a data size prefix.  */
174433965Sjdp	for (q = i.prefix; q < i.prefix + i.prefixes; q++)
174533965Sjdp	  {
174633965Sjdp	    if (*q == WORD_PREFIX_OPCODE)
174733965Sjdp	      {
174838891Sjdp		/* The jcxz/jecxz instructions are marked with Data16
174938891Sjdp		   and Data32, which means that they may get
175038891Sjdp		   WORD_PREFIX_OPCODE added to the list of prefixes.
175138891Sjdp		   However, the are correctly distinguished using
175238891Sjdp		   ADDR_PREFIX_OPCODE.  Here we look for
175338891Sjdp		   WORD_PREFIX_OPCODE, and actually emit
175438891Sjdp		   ADDR_PREFIX_OPCODE.  This is a hack, but, then, so
175538891Sjdp		   is the instruction itself.
175638891Sjdp
175738891Sjdp		   If an explicit suffix is used for the loop
175838891Sjdp		   instruction, that actually controls whether we use
175938891Sjdp		   cx vs. ecx.  This is also controlled by
176038891Sjdp		   ADDR_PREFIX_OPCODE.
176138891Sjdp
176238891Sjdp		   I don't know if there is any valid case in which we
176338891Sjdp		   want to emit WORD_PREFIX_OPCODE, but I am keeping
176438891Sjdp		   the old behaviour for safety.  */
176538891Sjdp
176638891Sjdp		if (IS_JUMP_ON_CX_ZERO (i.tm.base_opcode)
176738891Sjdp		    || IS_LOOP_ECX_TIMES (i.tm.base_opcode))
176838891Sjdp		  FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
176938891Sjdp		else
177038891Sjdp		  FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
177133965Sjdp	        insn_size += 1;
177233965Sjdp		break;
177333965Sjdp	      }
177433965Sjdp	  }
177533965Sjdp
177633965Sjdp	if ((size == 4) && (flag_16bit_code))
177733965Sjdp	  {
177833965Sjdp	    FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
177933965Sjdp	    insn_size += 1;
178033965Sjdp	  }
178133965Sjdp
178238891Sjdp	if (fits_in_unsigned_byte (i.tm.base_opcode))
178333965Sjdp	  {
178438891Sjdp	    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
178533965Sjdp	    insn_size += 1;
178633965Sjdp	  }
178733965Sjdp	else
178833965Sjdp	  {
178933965Sjdp	    p = frag_more (2);	/* opcode can be at most two bytes */
179033965Sjdp	    insn_size += 2;
179133965Sjdp	    /* put out high byte first: can't use md_number_to_chars! */
179238891Sjdp	    *p++ = (i.tm.base_opcode >> 8) & 0xff;
179338891Sjdp	    *p = i.tm.base_opcode & 0xff;
179433965Sjdp	  }
179533965Sjdp
179633965Sjdp	p = frag_more (size);
179733965Sjdp	insn_size += size;
179833965Sjdp	if (i.disps[0]->X_op == O_constant)
179933965Sjdp	  {
180033965Sjdp	    md_number_to_chars (p, (valueT) n, size);
180133965Sjdp	    if (size == 1 && !fits_in_signed_byte (n))
180233965Sjdp	      {
180333965Sjdp		as_bad ("loop/jecx only takes byte displacement; %lu shortened to %d",
180433965Sjdp			n, *p);
180533965Sjdp	      }
180633965Sjdp	  }
180733965Sjdp	else
180833965Sjdp	  {
180933965Sjdp	    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
181033965Sjdp			 i.disps[0], 1, reloc (size, 1, i.disp_reloc[0]));
181133965Sjdp
181233965Sjdp	  }
181333965Sjdp      }
181438891Sjdp    else if (i.tm.opcode_modifier & JumpInterSegment)
181533965Sjdp      {
181633965Sjdp	if (flag_16bit_code)
181733965Sjdp	  {
181833965Sjdp	    FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
181933965Sjdp	    insn_size += 1;
182033965Sjdp	  }
182133965Sjdp
182233965Sjdp	p = frag_more (1 + 2 + 4);	/* 1 opcode; 2 segment; 4 offset */
182333965Sjdp	insn_size += 1 + 2 + 4;
182438891Sjdp	p[0] = i.tm.base_opcode;
182533965Sjdp	if (i.imms[1]->X_op == O_constant)
182633965Sjdp	  md_number_to_chars (p + 1, (valueT) i.imms[1]->X_add_number, 4);
182733965Sjdp	else
182833965Sjdp	  fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
182933965Sjdp		       i.imms[1], 0, BFD_RELOC_32);
183033965Sjdp	if (i.imms[0]->X_op != O_constant)
183133965Sjdp	  as_bad ("can't handle non absolute segment in long call/jmp");
183233965Sjdp	md_number_to_chars (p + 5, (valueT) i.imms[0]->X_add_number, 2);
183333965Sjdp      }
183433965Sjdp    else
183533965Sjdp      {
183633965Sjdp	/* Output normal instructions here. */
183733965Sjdp	unsigned char *q;
183833965Sjdp
183938891Sjdp	/* Hack for fwait.  It must come before any prefixes, as it
184038891Sjdp	   really is an instruction rather than a prefix. */
184138891Sjdp	if ((i.tm.opcode_modifier & FWait) != 0)
184238891Sjdp	  {
184338891Sjdp	    p = frag_more (1);
184438891Sjdp	    insn_size += 1;
184538891Sjdp	    md_number_to_chars (p, (valueT) FWAIT_OPCODE, 1);
184638891Sjdp	  }
184738891Sjdp
184838891Sjdp	/* The prefix bytes. */
184933965Sjdp	for (q = i.prefix; q < i.prefix + i.prefixes; q++)
185033965Sjdp	  {
185133965Sjdp	    p = frag_more (1);
185233965Sjdp	    insn_size += 1;
185333965Sjdp	    md_number_to_chars (p, (valueT) *q, 1);
185433965Sjdp	  }
185533965Sjdp
185633965Sjdp	/* Now the opcode; be careful about word order here! */
185738891Sjdp	if (fits_in_unsigned_byte (i.tm.base_opcode))
185833965Sjdp	  {
185938891Sjdp	    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
186033965Sjdp	    insn_size += 1;
186133965Sjdp	  }
186238891Sjdp	else if (fits_in_unsigned_word (i.tm.base_opcode))
186333965Sjdp	  {
186433965Sjdp	    p = frag_more (2);
186533965Sjdp	    insn_size += 2;
186633965Sjdp	    /* put out high byte first: can't use md_number_to_chars! */
186738891Sjdp	    *p++ = (i.tm.base_opcode >> 8) & 0xff;
186838891Sjdp	    *p = i.tm.base_opcode & 0xff;
186933965Sjdp	  }
187033965Sjdp	else
187133965Sjdp	  {			/* opcode is either 3 or 4 bytes */
187238891Sjdp	    if (i.tm.base_opcode & 0xff000000)
187333965Sjdp	      {
187433965Sjdp		p = frag_more (4);
187533965Sjdp		insn_size += 4;
187638891Sjdp		*p++ = (i.tm.base_opcode >> 24) & 0xff;
187733965Sjdp	      }
187833965Sjdp	    else
187933965Sjdp	      {
188033965Sjdp		p = frag_more (3);
188133965Sjdp		insn_size += 3;
188233965Sjdp	      }
188338891Sjdp	    *p++ = (i.tm.base_opcode >> 16) & 0xff;
188438891Sjdp	    *p++ = (i.tm.base_opcode >> 8) & 0xff;
188538891Sjdp	    *p = (i.tm.base_opcode) & 0xff;
188633965Sjdp	  }
188733965Sjdp
188833965Sjdp	/* Now the modrm byte and base index byte (if present). */
188938891Sjdp	if (i.tm.opcode_modifier & Modrm)
189033965Sjdp	  {
189133965Sjdp	    p = frag_more (1);
189233965Sjdp	    insn_size += 1;
189333965Sjdp	    /* md_number_to_chars (p, i.rm, 1); */
189433965Sjdp	    md_number_to_chars (p,
189533965Sjdp				(valueT) (i.rm.regmem << 0
189633965Sjdp					  | i.rm.reg << 3
189733965Sjdp					  | i.rm.mode << 6),
189833965Sjdp				1);
189933965Sjdp	    /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
190033965Sjdp				   ==> need second modrm byte. */
190133965Sjdp	    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
190233965Sjdp	      {
190333965Sjdp		p = frag_more (1);
190433965Sjdp		insn_size += 1;
190533965Sjdp		/* md_number_to_chars (p, i.bi, 1); */
190633965Sjdp		md_number_to_chars (p, (valueT) (i.bi.base << 0
190733965Sjdp						 | i.bi.index << 3
190833965Sjdp						 | i.bi.scale << 6),
190933965Sjdp				    1);
191033965Sjdp	      }
191133965Sjdp	  }
191233965Sjdp
191333965Sjdp	if (i.disp_operands)
191433965Sjdp	  {
191533965Sjdp	    register unsigned int n;
191633965Sjdp
191733965Sjdp	    for (n = 0; n < i.operands; n++)
191833965Sjdp	      {
191933965Sjdp		if (i.disps[n])
192033965Sjdp		  {
192133965Sjdp		    if (i.disps[n]->X_op == O_constant)
192233965Sjdp		      {
192333965Sjdp			if (i.types[n] & (Disp8 | Abs8))
192433965Sjdp			  {
192533965Sjdp			    p = frag_more (1);
192633965Sjdp			    insn_size += 1;
192733965Sjdp			    md_number_to_chars (p,
192833965Sjdp						(valueT) i.disps[n]->X_add_number,
192933965Sjdp						1);
193033965Sjdp			  }
193133965Sjdp			else if (i.types[n] & (Disp16 | Abs16))
193233965Sjdp			  {
193333965Sjdp			    p = frag_more (2);
193433965Sjdp			    insn_size += 2;
193533965Sjdp			    md_number_to_chars (p,
193633965Sjdp						(valueT) i.disps[n]->X_add_number,
193733965Sjdp						2);
193833965Sjdp			  }
193933965Sjdp			else
194033965Sjdp			  {	/* Disp32|Abs32 */
194133965Sjdp			    p = frag_more (4);
194233965Sjdp			    insn_size += 4;
194333965Sjdp			    md_number_to_chars (p,
194433965Sjdp						(valueT) i.disps[n]->X_add_number,
194533965Sjdp						4);
194633965Sjdp			  }
194733965Sjdp		      }
194833965Sjdp		    else
194933965Sjdp		      {		/* not absolute_section */
195033965Sjdp			/* need a 32-bit fixup (don't support 8bit non-absolute disps) */
195133965Sjdp			p = frag_more (4);
195233965Sjdp			insn_size += 4;
195333965Sjdp			fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
195433965Sjdp					    i.disps[n], 0,
195533965Sjdp					    TC_RELOC(i.disp_reloc[n], BFD_RELOC_32));
195633965Sjdp		      }
195733965Sjdp		  }
195833965Sjdp	      }
195933965Sjdp	  }			/* end displacement output */
196033965Sjdp
196133965Sjdp	/* output immediate */
196233965Sjdp	if (i.imm_operands)
196333965Sjdp	  {
196433965Sjdp	    register unsigned int n;
196533965Sjdp
196633965Sjdp	    for (n = 0; n < i.operands; n++)
196733965Sjdp	      {
196833965Sjdp		if (i.imms[n])
196933965Sjdp		  {
197033965Sjdp		    if (i.imms[n]->X_op == O_constant)
197133965Sjdp		      {
197233965Sjdp			if (i.types[n] & (Imm8 | Imm8S))
197333965Sjdp			  {
197433965Sjdp			    p = frag_more (1);
197533965Sjdp			    insn_size += 1;
197633965Sjdp			    md_number_to_chars (p,
197733965Sjdp						(valueT) i.imms[n]->X_add_number,
197833965Sjdp						1);
197933965Sjdp			  }
198033965Sjdp			else if (i.types[n] & Imm16)
198133965Sjdp			  {
198233965Sjdp			    p = frag_more (2);
198333965Sjdp			    insn_size += 2;
198433965Sjdp			    md_number_to_chars (p,
198533965Sjdp						(valueT) i.imms[n]->X_add_number,
198633965Sjdp						2);
198733965Sjdp			  }
198833965Sjdp			else
198933965Sjdp			  {
199033965Sjdp			    p = frag_more (4);
199133965Sjdp			    insn_size += 4;
199233965Sjdp			    md_number_to_chars (p,
199333965Sjdp						(valueT) i.imms[n]->X_add_number,
199433965Sjdp						4);
199533965Sjdp			  }
199633965Sjdp		      }
199733965Sjdp		    else
199833965Sjdp		      {		/* not absolute_section */
199933965Sjdp			/* Need a 32-bit fixup (don't support 8bit
200033965Sjdp			   non-absolute ims).  Try to support other
200133965Sjdp			   sizes ... */
200233965Sjdp			int r_type;
200333965Sjdp			int size;
200433965Sjdp			int pcrel = 0;
200533965Sjdp
200633965Sjdp			if (i.types[n] & (Imm8 | Imm8S))
200733965Sjdp			  size = 1;
200833965Sjdp			else if (i.types[n] & Imm16)
200933965Sjdp			  size = 2;
201033965Sjdp			else
201133965Sjdp			  size = 4;
201233965Sjdp			r_type = reloc (size, 0, i.disp_reloc[0]);
201333965Sjdp			p = frag_more (size);
201433965Sjdp			insn_size += size;
201533965Sjdp#ifdef BFD_ASSEMBLER
201633965Sjdp			if (r_type == BFD_RELOC_32
201733965Sjdp			    && GOT_symbol
201833965Sjdp			    && GOT_symbol == i.imms[n]->X_add_symbol
201933965Sjdp			    && (i.imms[n]->X_op == O_symbol
202033965Sjdp				|| (i.imms[n]->X_op == O_add
202133965Sjdp				    && (i.imms[n]->X_op_symbol->sy_value.X_op
202233965Sjdp					== O_subtract))))
202333965Sjdp			  {
202433965Sjdp			    r_type = BFD_RELOC_386_GOTPC;
202533965Sjdp			    i.imms[n]->X_add_number += 3;
202633965Sjdp			  }
202733965Sjdp#endif
202833965Sjdp			fix_new_exp (frag_now, p - frag_now->fr_literal, size,
202933965Sjdp				     i.imms[n], pcrel, r_type);
203033965Sjdp		      }
203133965Sjdp		  }
203233965Sjdp	      }
203333965Sjdp	  }			/* end immediate output */
203433965Sjdp      }
203533965Sjdp
203633965Sjdp#ifdef DEBUG386
203733965Sjdp    if (flag_debug)
203833965Sjdp      {
203933965Sjdp	pi (line, &i);
204033965Sjdp      }
204133965Sjdp#endif /* DEBUG386 */
204233965Sjdp  }
204333965Sjdp}
204433965Sjdp
204533965Sjdp/* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
204633965Sjdp   on error. */
204733965Sjdp
204833965Sjdpstatic int
204933965Sjdpi386_operand (operand_string)
205033965Sjdp     char *operand_string;
205133965Sjdp{
205233965Sjdp  register char *op_string = operand_string;
205333965Sjdp
205433965Sjdp  /* Address of '\0' at end of operand_string. */
205533965Sjdp  char *end_of_operand_string = operand_string + strlen (operand_string);
205633965Sjdp
205733965Sjdp  /* Start and end of displacement string expression (if found). */
205833965Sjdp  char *displacement_string_start = NULL;
205933965Sjdp  char *displacement_string_end = NULL;
206033965Sjdp
206133965Sjdp  /* We check for an absolute prefix (differentiating,
206233965Sjdp     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
206333965Sjdp  if (*op_string == ABSOLUTE_PREFIX)
206433965Sjdp    {
206533965Sjdp      op_string++;
206633965Sjdp      i.types[this_operand] |= JumpAbsolute;
206733965Sjdp    }
206833965Sjdp
206933965Sjdp  /* Check if operand is a register. */
207033965Sjdp  if (*op_string == REGISTER_PREFIX)
207133965Sjdp    {
207233965Sjdp      register reg_entry *r;
207333965Sjdp      if (!(r = parse_register (op_string)))
207433965Sjdp	{
207533965Sjdp	  as_bad ("bad register name ('%s')", op_string);
207633965Sjdp	  return 0;
207733965Sjdp	}
207833965Sjdp      /* Check for segment override, rather than segment register by
207933965Sjdp	 searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
208033965Sjdp      if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
208133965Sjdp	{
208233965Sjdp	  switch (r->reg_num)
208333965Sjdp	    {
208433965Sjdp	    case 0:
208533965Sjdp	      i.seg = (seg_entry *) & es;
208633965Sjdp	      break;
208733965Sjdp	    case 1:
208833965Sjdp	      i.seg = (seg_entry *) & cs;
208933965Sjdp	      break;
209033965Sjdp	    case 2:
209133965Sjdp	      i.seg = (seg_entry *) & ss;
209233965Sjdp	      break;
209333965Sjdp	    case 3:
209433965Sjdp	      i.seg = (seg_entry *) & ds;
209533965Sjdp	      break;
209633965Sjdp	    case 4:
209733965Sjdp	      i.seg = (seg_entry *) & fs;
209833965Sjdp	      break;
209933965Sjdp	    case 5:
210033965Sjdp	      i.seg = (seg_entry *) & gs;
210133965Sjdp	      break;
210233965Sjdp	    }
210333965Sjdp	  op_string += 4;	/* skip % <x> s : */
210433965Sjdp	  operand_string = op_string;	/* Pretend given string starts here. */
210533965Sjdp	  if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
210633965Sjdp	      && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
210733965Sjdp	    {
210833965Sjdp	      as_bad ("bad memory operand after segment override");
210933965Sjdp	      return 0;
211033965Sjdp	    }
211133965Sjdp	  /* Handle case of %es:*foo. */
211233965Sjdp	  if (*op_string == ABSOLUTE_PREFIX)
211333965Sjdp	    {
211433965Sjdp	      op_string++;
211533965Sjdp	      i.types[this_operand] |= JumpAbsolute;
211633965Sjdp	    }
211733965Sjdp	  goto do_memory_reference;
211833965Sjdp	}
211933965Sjdp      i.types[this_operand] |= r->reg_type;
212033965Sjdp      i.regs[this_operand] = r;
212133965Sjdp      i.reg_operands++;
212233965Sjdp    }
212333965Sjdp  else if (*op_string == IMMEDIATE_PREFIX)
212433965Sjdp    {				/* ... or an immediate */
212533965Sjdp      char *save_input_line_pointer;
212633965Sjdp      segT exp_seg = 0;
212733965Sjdp      expressionS *exp;
212833965Sjdp
212933965Sjdp      if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
213033965Sjdp	{
213133965Sjdp	  as_bad ("only 1 or 2 immediate operands are allowed");
213233965Sjdp	  return 0;
213333965Sjdp	}
213433965Sjdp
213533965Sjdp      exp = &im_expressions[i.imm_operands++];
213633965Sjdp      i.imms[this_operand] = exp;
213733965Sjdp      save_input_line_pointer = input_line_pointer;
213833965Sjdp      input_line_pointer = ++op_string;	/* must advance op_string! */
213933965Sjdp      SKIP_WHITESPACE ();
214033965Sjdp      exp_seg = expression (exp);
214138891Sjdp      if (*input_line_pointer != '\0')
214238891Sjdp	{
214338891Sjdp	  /* This should be as_bad, but some versions of gcc, up to
214438891Sjdp             about 2.8 and egcs 1.01, generate a bogus @GOTOFF(%ebx)
214538891Sjdp             in certain cases.  Oddly, the code in question turns out
214638891Sjdp             to work correctly anyhow, so we make this just a warning
214738891Sjdp             until those versions of gcc are obsolete.  */
214838891Sjdp	  as_warn ("warning: unrecognized characters `%s' in expression",
214938891Sjdp		   input_line_pointer);
215038891Sjdp	}
215133965Sjdp      input_line_pointer = save_input_line_pointer;
215233965Sjdp
215333965Sjdp      if (exp->X_op == O_absent)
215433965Sjdp	{
215533965Sjdp	  /* missing or bad expr becomes absolute 0 */
215633965Sjdp	  as_bad ("missing or invalid immediate expression '%s' taken as 0",
215733965Sjdp		  operand_string);
215833965Sjdp	  exp->X_op = O_constant;
215933965Sjdp	  exp->X_add_number = 0;
216033965Sjdp	  exp->X_add_symbol = (symbolS *) 0;
216133965Sjdp	  exp->X_op_symbol = (symbolS *) 0;
216233965Sjdp	  i.types[this_operand] |= Imm;
216333965Sjdp	}
216433965Sjdp      else if (exp->X_op == O_constant)
216533965Sjdp	{
216633965Sjdp	  i.types[this_operand] |=
216733965Sjdp	    smallest_imm_type ((unsigned long) exp->X_add_number);
216833965Sjdp	}
216933965Sjdp#ifdef OBJ_AOUT
217033965Sjdp      else if (exp_seg != text_section
217133965Sjdp	       && exp_seg != data_section
217233965Sjdp	       && exp_seg != bss_section
217333965Sjdp	       && exp_seg != undefined_section
217433965Sjdp#ifdef BFD_ASSEMBLER
217533965Sjdp	       && ! bfd_is_com_section (exp_seg)
217633965Sjdp#endif
217733965Sjdp	       )
217833965Sjdp	{
217933965Sjdp	seg_unimplemented:
218033965Sjdp	  as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
218133965Sjdp	  return 0;
218233965Sjdp	}
218333965Sjdp#endif
218433965Sjdp      else
218533965Sjdp	{
218633965Sjdp	  /* this is an address ==> 32bit */
218733965Sjdp	  i.types[this_operand] |= Imm32;
218833965Sjdp	}
218933965Sjdp      /* shorten this type of this operand if the instruction wants
219033965Sjdp       * fewer bits than are present in the immediate.  The bit field
219133965Sjdp       * code can put out 'andb $0xffffff, %al', for example.   pace
219233965Sjdp       * also 'movw $foo,(%eax)'
219333965Sjdp       */
219433965Sjdp      switch (i.suffix)
219533965Sjdp	{
219633965Sjdp	case WORD_OPCODE_SUFFIX:
219733965Sjdp	  i.types[this_operand] |= Imm16;
219833965Sjdp	  break;
219933965Sjdp	case BYTE_OPCODE_SUFFIX:
220033965Sjdp	  i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
220133965Sjdp	  break;
220233965Sjdp	}
220333965Sjdp    }
220433965Sjdp  else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
220533965Sjdp	   || *op_string == '(')
220633965Sjdp    {
220733965Sjdp      /* This is a memory reference of some sort. */
220833965Sjdp      register char *base_string;
220933965Sjdp      unsigned int found_base_index_form;
221033965Sjdp
221133965Sjdp    do_memory_reference:
221233965Sjdp      if (i.mem_operands == MAX_MEMORY_OPERANDS)
221333965Sjdp	{
221433965Sjdp	  as_bad ("more than 1 memory reference in instruction");
221533965Sjdp	  return 0;
221633965Sjdp	}
221733965Sjdp      i.mem_operands++;
221833965Sjdp
221933965Sjdp      /* Determine type of memory operand from opcode_suffix;
222038891Sjdp	 no opcode suffix implies general memory references. */
222133965Sjdp      switch (i.suffix)
222233965Sjdp	{
222333965Sjdp	case BYTE_OPCODE_SUFFIX:
222433965Sjdp	  i.types[this_operand] |= Mem8;
222533965Sjdp	  break;
222633965Sjdp	case WORD_OPCODE_SUFFIX:
222733965Sjdp	  i.types[this_operand] |= Mem16;
222833965Sjdp	  break;
222933965Sjdp	case DWORD_OPCODE_SUFFIX:
223033965Sjdp	default:
223133965Sjdp	  i.types[this_operand] |= Mem32;
223233965Sjdp	}
223333965Sjdp
223433965Sjdp      /* Check for base index form.  We detect the base index form by
223533965Sjdp	 looking for an ')' at the end of the operand, searching
223633965Sjdp	 for the '(' matching it, and finding a REGISTER_PREFIX or ','
223733965Sjdp	 after it. */
223833965Sjdp      base_string = end_of_operand_string - 1;
223933965Sjdp      found_base_index_form = 0;
224033965Sjdp      if (*base_string == ')')
224133965Sjdp	{
224233965Sjdp	  unsigned int parens_balanced = 1;
224333965Sjdp	  /* We've already checked that the number of left & right ()'s are
224433965Sjdp	     equal, so this loop will not be infinite. */
224533965Sjdp	  do
224633965Sjdp	    {
224733965Sjdp	      base_string--;
224833965Sjdp	      if (*base_string == ')')
224933965Sjdp		parens_balanced++;
225033965Sjdp	      if (*base_string == '(')
225133965Sjdp		parens_balanced--;
225233965Sjdp	    }
225333965Sjdp	  while (parens_balanced);
225433965Sjdp	  base_string++;	/* Skip past '('. */
225533965Sjdp	  if (*base_string == REGISTER_PREFIX || *base_string == ',')
225633965Sjdp	    found_base_index_form = 1;
225733965Sjdp	}
225833965Sjdp
225933965Sjdp      /* If we can't parse a base index register expression, we've found
226033965Sjdp	 a pure displacement expression.  We set up displacement_string_start
226133965Sjdp	 and displacement_string_end for the code below. */
226233965Sjdp      if (!found_base_index_form)
226333965Sjdp	{
226433965Sjdp	  displacement_string_start = op_string;
226533965Sjdp	  displacement_string_end = end_of_operand_string;
226633965Sjdp	}
226733965Sjdp      else
226833965Sjdp	{
226933965Sjdp	  char *base_reg_name, *index_reg_name, *num_string;
227033965Sjdp	  int num;
227133965Sjdp
227233965Sjdp	  i.types[this_operand] |= BaseIndex;
227333965Sjdp
227433965Sjdp	  /* If there is a displacement set-up for it to be parsed later. */
227533965Sjdp	  if (base_string != op_string + 1)
227633965Sjdp	    {
227733965Sjdp	      displacement_string_start = op_string;
227833965Sjdp	      displacement_string_end = base_string - 1;
227933965Sjdp	    }
228033965Sjdp
228133965Sjdp	  /* Find base register (if any). */
228233965Sjdp	  if (*base_string != ',')
228333965Sjdp	    {
228433965Sjdp	      base_reg_name = base_string++;
228533965Sjdp	      /* skip past register name & parse it */
228633965Sjdp	      while (isalpha (*base_string))
228733965Sjdp		base_string++;
228833965Sjdp	      if (base_string == base_reg_name + 1)
228933965Sjdp		{
229033965Sjdp		  as_bad ("can't find base register name after '(%c'",
229133965Sjdp			  REGISTER_PREFIX);
229233965Sjdp		  return 0;
229333965Sjdp		}
229433965Sjdp	      END_STRING_AND_SAVE (base_string);
229533965Sjdp	      if (!(i.base_reg = parse_register (base_reg_name)))
229633965Sjdp		{
229733965Sjdp		  as_bad ("bad base register name ('%s')", base_reg_name);
229833965Sjdp		  return 0;
229933965Sjdp		}
230033965Sjdp	      RESTORE_END_STRING (base_string);
230133965Sjdp	    }
230233965Sjdp
230333965Sjdp	  /* Now check seperator; must be ',' ==> index reg
230433965Sjdp			   OR num ==> no index reg. just scale factor
230533965Sjdp			   OR ')' ==> end. (scale factor = 1) */
230633965Sjdp	  if (*base_string != ',' && *base_string != ')')
230733965Sjdp	    {
230833965Sjdp	      as_bad ("expecting ',' or ')' after base register in `%s'",
230933965Sjdp		      operand_string);
231033965Sjdp	      return 0;
231133965Sjdp	    }
231233965Sjdp
231333965Sjdp	  /* There may index reg here; and there may be a scale factor. */
231433965Sjdp	  if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
231533965Sjdp	    {
231633965Sjdp	      index_reg_name = ++base_string;
231733965Sjdp	      while (isalpha (*++base_string));
231833965Sjdp	      END_STRING_AND_SAVE (base_string);
231933965Sjdp	      if (!(i.index_reg = parse_register (index_reg_name)))
232033965Sjdp		{
232133965Sjdp		  as_bad ("bad index register name ('%s')", index_reg_name);
232233965Sjdp		  return 0;
232333965Sjdp		}
232433965Sjdp	      RESTORE_END_STRING (base_string);
232533965Sjdp	    }
232633965Sjdp
232733965Sjdp	  /* Check for scale factor. */
232833965Sjdp	  if (*base_string == ',' && isdigit (*(base_string + 1)))
232933965Sjdp	    {
233033965Sjdp	      num_string = ++base_string;
233133965Sjdp	      while (is_digit_char (*base_string))
233233965Sjdp		base_string++;
233333965Sjdp	      if (base_string == num_string)
233433965Sjdp		{
233533965Sjdp		  as_bad ("can't find a scale factor after ','");
233633965Sjdp		  return 0;
233733965Sjdp		}
233833965Sjdp	      END_STRING_AND_SAVE (base_string);
233933965Sjdp	      /* We've got a scale factor. */
234033965Sjdp	      if (!sscanf (num_string, "%d", &num))
234133965Sjdp		{
234233965Sjdp		  as_bad ("can't parse scale factor from '%s'", num_string);
234333965Sjdp		  return 0;
234433965Sjdp		}
234533965Sjdp	      RESTORE_END_STRING (base_string);
234633965Sjdp	      switch (num)
234733965Sjdp		{		/* must be 1 digit scale */
234833965Sjdp		case 1:
234933965Sjdp		  i.log2_scale_factor = 0;
235033965Sjdp		  break;
235133965Sjdp		case 2:
235233965Sjdp		  i.log2_scale_factor = 1;
235333965Sjdp		  break;
235433965Sjdp		case 4:
235533965Sjdp		  i.log2_scale_factor = 2;
235633965Sjdp		  break;
235733965Sjdp		case 8:
235833965Sjdp		  i.log2_scale_factor = 3;
235933965Sjdp		  break;
236033965Sjdp		default:
236133965Sjdp		  as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
236233965Sjdp		  return 0;
236333965Sjdp		}
236433965Sjdp	    }
236533965Sjdp	  else
236633965Sjdp	    {
236733965Sjdp	      if (!i.index_reg && *base_string == ',')
236833965Sjdp		{
236933965Sjdp		  as_bad ("expecting index register or scale factor after ','; got '%c'",
237033965Sjdp			  *(base_string + 1));
237133965Sjdp		  return 0;
237233965Sjdp		}
237333965Sjdp	    }
237433965Sjdp	}
237533965Sjdp
237633965Sjdp      /* If there's an expression begining the operand, parse it,
237733965Sjdp	 assuming displacement_string_start and displacement_string_end
237833965Sjdp	 are meaningful. */
237933965Sjdp      if (displacement_string_start)
238033965Sjdp	{
238133965Sjdp	  register expressionS *exp;
238233965Sjdp	  segT exp_seg = 0;
238333965Sjdp	  char *save_input_line_pointer;
238433965Sjdp	  exp = &disp_expressions[i.disp_operands];
238533965Sjdp	  i.disps[this_operand] = exp;
238633965Sjdp	  i.disp_reloc[this_operand] = NO_RELOC;
238733965Sjdp	  i.disp_operands++;
238833965Sjdp	  save_input_line_pointer = input_line_pointer;
238933965Sjdp	  input_line_pointer = displacement_string_start;
239033965Sjdp	  END_STRING_AND_SAVE (displacement_string_end);
239138891Sjdp
239233965Sjdp#ifndef LEX_AT
239333965Sjdp	  {
239433965Sjdp	    /*
239533965Sjdp	     * We can have operands of the form
239633965Sjdp	     *   <symbol>@GOTOFF+<nnn>
239733965Sjdp	     * Take the easy way out here and copy everything
239833965Sjdp	     * into a temporary buffer...
239933965Sjdp	     */
240033965Sjdp	    register char *cp;
240133965Sjdp
240238891Sjdp	    cp = strchr (input_line_pointer, '@');
240338891Sjdp	    if (cp != NULL)
240438891Sjdp	      {
240538891Sjdp		char *tmpbuf;
240638891Sjdp
240738891Sjdp		if (GOT_symbol == NULL)
240838891Sjdp		  GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
240938891Sjdp
241038891Sjdp		tmpbuf = (char *) alloca ((cp - input_line_pointer) + 20);
241138891Sjdp
241238891Sjdp		if (strncmp (cp + 1, "PLT", 3) == 0)
241338891Sjdp		  {
241438891Sjdp		    i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
241538891Sjdp		    *cp = '\0';
241638891Sjdp		    strcpy (tmpbuf, input_line_pointer);
241738891Sjdp		    strcat (tmpbuf, cp + 1 + 3);
241838891Sjdp		    *cp = '@';
241938891Sjdp		  }
242038891Sjdp		else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
242138891Sjdp		  {
242238891Sjdp		    i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
242338891Sjdp		    *cp = '\0';
242438891Sjdp		    strcpy (tmpbuf, input_line_pointer);
242538891Sjdp		    strcat (tmpbuf, cp + 1 + 6);
242638891Sjdp		    *cp = '@';
242738891Sjdp		  }
242838891Sjdp		else if (strncmp (cp + 1, "GOT", 3) == 0)
242938891Sjdp		  {
243038891Sjdp		    i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
243138891Sjdp		    *cp = '\0';
243238891Sjdp		    strcpy (tmpbuf, input_line_pointer);
243338891Sjdp		    strcat (tmpbuf, cp + 1 + 3);
243438891Sjdp		    *cp = '@';
243538891Sjdp		  }
243638891Sjdp		else
243738891Sjdp		  as_bad ("Bad reloc specifier '%s' in expression", cp + 1);
243838891Sjdp
243938891Sjdp		input_line_pointer = tmpbuf;
244038891Sjdp	      }
244133965Sjdp	  }
244233965Sjdp#endif
244338891Sjdp
244433965Sjdp	  exp_seg = expression (exp);
244533965Sjdp
244633965Sjdp#ifdef BFD_ASSEMBLER
244733965Sjdp	  /* We do this to make sure that the section symbol is in
244833965Sjdp	     the symbol table.  We will ultimately change the relocation
244933965Sjdp	     to be relative to the beginning of the section */
245033965Sjdp	  if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
245133965Sjdp	    {
245233965Sjdp	      if (S_IS_LOCAL(exp->X_add_symbol)
245333965Sjdp		  && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
245433965Sjdp		section_symbol(exp->X_add_symbol->bsym->section);
245533965Sjdp	      assert (exp->X_op == O_symbol);
245633965Sjdp	      exp->X_op = O_subtract;
245733965Sjdp	      exp->X_op_symbol = GOT_symbol;
245833965Sjdp	      i.disp_reloc[this_operand] = BFD_RELOC_32;
245933965Sjdp	    }
246033965Sjdp#endif
246133965Sjdp
246233965Sjdp	  if (*input_line_pointer)
246333965Sjdp	    as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
246433965Sjdp	  RESTORE_END_STRING (displacement_string_end);
246533965Sjdp	  input_line_pointer = save_input_line_pointer;
246633965Sjdp	  if (exp->X_op == O_absent)
246733965Sjdp	    {
246833965Sjdp	      /* missing expr becomes absolute 0 */
246933965Sjdp	      as_bad ("missing or invalid displacement '%s' taken as 0",
247033965Sjdp		      operand_string);
247133965Sjdp	      i.types[this_operand] |= (Disp | Abs);
247233965Sjdp	      exp->X_op = O_constant;
247333965Sjdp	      exp->X_add_number = 0;
247433965Sjdp	      exp->X_add_symbol = (symbolS *) 0;
247533965Sjdp	      exp->X_op_symbol = (symbolS *) 0;
247633965Sjdp	    }
247733965Sjdp	  else if (exp->X_op == O_constant)
247833965Sjdp	    {
247933965Sjdp	      i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
248033965Sjdp	    }
248133965Sjdp	  else if (exp_seg == text_section
248233965Sjdp		   || exp_seg == data_section
248333965Sjdp		   || exp_seg == bss_section
248433965Sjdp		   || exp_seg == undefined_section)
248533965Sjdp	    {
248633965Sjdp	      i.types[this_operand] |= Disp32;
248733965Sjdp	    }
248833965Sjdp	  else
248933965Sjdp	    {
249033965Sjdp#ifndef OBJ_AOUT
249133965Sjdp	      i.types[this_operand] |= Disp32;
249233965Sjdp#else
249333965Sjdp	      goto seg_unimplemented;
249433965Sjdp#endif
249533965Sjdp	    }
249633965Sjdp	}
249733965Sjdp
249833965Sjdp      /* Make sure the memory operand we've been dealt is valid. */
249933965Sjdp      if (i.base_reg && i.index_reg &&
250033965Sjdp	  !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
250133965Sjdp	{
250233965Sjdp	  as_bad ("register size mismatch in (base,index,scale) expression");
250333965Sjdp	  return 0;
250433965Sjdp	}
250533965Sjdp      /*
250633965Sjdp       * special case for (%dx) while doing input/output op
250733965Sjdp       */
250833965Sjdp      if ((i.base_reg &&
250933965Sjdp	   (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
251033965Sjdp	   (i.index_reg == 0)))
251133965Sjdp	{
251233965Sjdp	  i.types[this_operand] |= InOutPortReg;
251333965Sjdp	  return 1;
251433965Sjdp	}
251533965Sjdp      if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
251633965Sjdp	  (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
251733965Sjdp	{
251833965Sjdp	  as_bad ("base/index register must be 32 bit register");
251933965Sjdp	  return 0;
252033965Sjdp	}
252133965Sjdp      if (i.index_reg && i.index_reg == esp)
252233965Sjdp	{
252333965Sjdp	  as_bad ("%s may not be used as an index register", esp->reg_name);
252433965Sjdp	  return 0;
252533965Sjdp	}
252633965Sjdp    }
252733965Sjdp  else
252833965Sjdp    {				/* it's not a memory operand; argh! */
252933965Sjdp      as_bad ("invalid char %s begining %s operand '%s'",
253033965Sjdp	      output_invalid (*op_string), ordinal_names[this_operand],
253133965Sjdp	      op_string);
253233965Sjdp      return 0;
253333965Sjdp    }
253433965Sjdp  return 1;			/* normal return */
253533965Sjdp}
253633965Sjdp
253733965Sjdp/*
253833965Sjdp *			md_estimate_size_before_relax()
253933965Sjdp *
254033965Sjdp * Called just before relax().
254133965Sjdp * Any symbol that is now undefined will not become defined.
254233965Sjdp * Return the correct fr_subtype in the frag.
254333965Sjdp * Return the initial "guess for fr_var" to caller.
254433965Sjdp * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
254533965Sjdp * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
254633965Sjdp * Although it may not be explicit in the frag, pretend fr_var starts with a
254733965Sjdp * 0 value.
254833965Sjdp */
254933965Sjdpint
255033965Sjdpmd_estimate_size_before_relax (fragP, segment)
255133965Sjdp     register fragS *fragP;
255233965Sjdp     register segT segment;
255333965Sjdp{
255433965Sjdp  register unsigned char *opcode;
255533965Sjdp  register int old_fr_fix;
255633965Sjdp
255733965Sjdp  old_fr_fix = fragP->fr_fix;
255833965Sjdp  opcode = (unsigned char *) fragP->fr_opcode;
255933965Sjdp  /* We've already got fragP->fr_subtype right;  all we have to do is check
256033965Sjdp	   for un-relaxable symbols. */
256133965Sjdp  if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
256233965Sjdp    {
256333965Sjdp      /* symbol is undefined in this segment */
256433965Sjdp      switch (opcode[0])
256533965Sjdp	{
256633965Sjdp	case JUMP_PC_RELATIVE:	/* make jmp (0xeb) a dword displacement jump */
256733965Sjdp	  opcode[0] = 0xe9;	/* dword disp jmp */
256833965Sjdp	  fragP->fr_fix += 4;
256933965Sjdp	  fix_new (fragP, old_fr_fix, 4,
257033965Sjdp				 fragP->fr_symbol,
257133965Sjdp		   fragP->fr_offset, 1,
257233965Sjdp		   (GOT_symbol && /* Not quite right - we should switch on
257333965Sjdp				     presence of @PLT, but I cannot see how
257433965Sjdp				     to get to that from here.  We should have
257533965Sjdp				     done this in md_assemble to really
257633965Sjdp				     get it right all of the time, but I
257733965Sjdp				     think it does not matter that much, as
257833965Sjdp				     this will be right most of the time. ERY*/
257933965Sjdp		    S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
258033965Sjdp		   BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
258133965Sjdp	  break;
258233965Sjdp
258333965Sjdp	default:
258433965Sjdp	  /* This changes the byte-displacement jump 0x7N -->
258533965Sjdp			   the dword-displacement jump 0x0f8N */
258633965Sjdp	  opcode[1] = opcode[0] + 0x10;
258733965Sjdp	  opcode[0] = TWO_BYTE_OPCODE_ESCAPE;	/* two-byte escape */
258833965Sjdp	  fragP->fr_fix += 1 + 4;	/* we've added an opcode byte */
258933965Sjdp	  fix_new (fragP, old_fr_fix + 1, 4,
259033965Sjdp		   fragP->fr_symbol,
259133965Sjdp		   fragP->fr_offset, 1,
259233965Sjdp		   (GOT_symbol &&  /* Not quite right - we should switch on
259333965Sjdp				     presence of @PLT, but I cannot see how
259433965Sjdp				     to get to that from here.  ERY */
259533965Sjdp		    S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
259633965Sjdp		   BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
259733965Sjdp	  break;
259833965Sjdp	}
259933965Sjdp      frag_wane (fragP);
260033965Sjdp    }
260133965Sjdp  return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
260233965Sjdp}				/* md_estimate_size_before_relax() */
260333965Sjdp
260433965Sjdp/*
260533965Sjdp *			md_convert_frag();
260633965Sjdp *
260733965Sjdp * Called after relax() is finished.
260833965Sjdp * In:	Address of frag.
260933965Sjdp *	fr_type == rs_machine_dependent.
261033965Sjdp *	fr_subtype is what the address relaxed to.
261133965Sjdp *
261233965Sjdp * Out:	Any fixSs and constants are set up.
261333965Sjdp *	Caller will turn frag into a ".space 0".
261433965Sjdp */
261533965Sjdp#ifndef BFD_ASSEMBLER
261633965Sjdpvoid
261733965Sjdpmd_convert_frag (headers, sec, fragP)
261833965Sjdp     object_headers *headers;
261933965Sjdp     segT sec;
262033965Sjdp     register fragS *fragP;
262133965Sjdp#else
262233965Sjdpvoid
262333965Sjdpmd_convert_frag (abfd, sec, fragP)
262433965Sjdp     bfd *abfd;
262533965Sjdp     segT sec;
262633965Sjdp     register fragS *fragP;
262733965Sjdp#endif
262833965Sjdp{
262933965Sjdp  register unsigned char *opcode;
263033965Sjdp  unsigned char *where_to_put_displacement = NULL;
263133965Sjdp  unsigned int target_address;
263233965Sjdp  unsigned int opcode_address;
263333965Sjdp  unsigned int extension = 0;
263433965Sjdp  int displacement_from_opcode_start;
263533965Sjdp
263633965Sjdp  opcode = (unsigned char *) fragP->fr_opcode;
263733965Sjdp
263833965Sjdp  /* Address we want to reach in file space. */
263933965Sjdp  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
264033965Sjdp#ifdef BFD_ASSEMBLER /* not needed otherwise? */
264133965Sjdp  target_address += fragP->fr_symbol->sy_frag->fr_address;
264233965Sjdp#endif
264333965Sjdp
264433965Sjdp  /* Address opcode resides at in file space. */
264533965Sjdp  opcode_address = fragP->fr_address + fragP->fr_fix;
264633965Sjdp
264733965Sjdp  /* Displacement from opcode start to fill into instruction. */
264833965Sjdp  displacement_from_opcode_start = target_address - opcode_address;
264933965Sjdp
265033965Sjdp  switch (fragP->fr_subtype)
265133965Sjdp    {
265233965Sjdp    case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
265333965Sjdp    case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
265433965Sjdp      /* don't have to change opcode */
265533965Sjdp      extension = 1;		/* 1 opcode + 1 displacement */
265633965Sjdp      where_to_put_displacement = &opcode[1];
265733965Sjdp      break;
265833965Sjdp
265933965Sjdp    case ENCODE_RELAX_STATE (COND_JUMP, WORD):
266033965Sjdp      opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
266133965Sjdp      opcode[2] = opcode[0] + 0x10;
266233965Sjdp      opcode[0] = WORD_PREFIX_OPCODE;
266333965Sjdp      extension = 4;		/* 3 opcode + 2 displacement */
266433965Sjdp      where_to_put_displacement = &opcode[3];
266533965Sjdp      break;
266633965Sjdp
266733965Sjdp    case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
266833965Sjdp      opcode[1] = 0xe9;
266933965Sjdp      opcode[0] = WORD_PREFIX_OPCODE;
267033965Sjdp      extension = 3;		/* 2 opcode + 2 displacement */
267133965Sjdp      where_to_put_displacement = &opcode[2];
267233965Sjdp      break;
267333965Sjdp
267433965Sjdp    case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
267533965Sjdp      opcode[1] = opcode[0] + 0x10;
267633965Sjdp      opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
267733965Sjdp      extension = 5;		/* 2 opcode + 4 displacement */
267833965Sjdp      where_to_put_displacement = &opcode[2];
267933965Sjdp      break;
268033965Sjdp
268133965Sjdp    case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
268233965Sjdp      opcode[0] = 0xe9;
268333965Sjdp      extension = 4;		/* 1 opcode + 4 displacement */
268433965Sjdp      where_to_put_displacement = &opcode[1];
268533965Sjdp      break;
268633965Sjdp
268733965Sjdp    default:
268833965Sjdp      BAD_CASE (fragP->fr_subtype);
268933965Sjdp      break;
269033965Sjdp    }
269133965Sjdp  /* now put displacement after opcode */
269233965Sjdp  md_number_to_chars ((char *) where_to_put_displacement,
269333965Sjdp		      (valueT) (displacement_from_opcode_start - extension),
269433965Sjdp		      SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
269533965Sjdp  fragP->fr_fix += extension;
269633965Sjdp}
269733965Sjdp
269833965Sjdp
269933965Sjdpint md_short_jump_size = 2;	/* size of byte displacement jmp */
270033965Sjdpint md_long_jump_size = 5;	/* size of dword displacement jmp */
270133965Sjdpconst int md_reloc_size = 8;	/* Size of relocation record */
270233965Sjdp
270333965Sjdpvoid
270433965Sjdpmd_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
270533965Sjdp     char *ptr;
270633965Sjdp     addressT from_addr, to_addr;
270733965Sjdp     fragS *frag;
270833965Sjdp     symbolS *to_symbol;
270933965Sjdp{
271033965Sjdp  long offset;
271133965Sjdp
271233965Sjdp  offset = to_addr - (from_addr + 2);
271333965Sjdp  md_number_to_chars (ptr, (valueT) 0xeb, 1);	/* opcode for byte-disp jump */
271433965Sjdp  md_number_to_chars (ptr + 1, (valueT) offset, 1);
271533965Sjdp}
271633965Sjdp
271733965Sjdpvoid
271833965Sjdpmd_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
271933965Sjdp     char *ptr;
272033965Sjdp     addressT from_addr, to_addr;
272133965Sjdp     fragS *frag;
272233965Sjdp     symbolS *to_symbol;
272333965Sjdp{
272433965Sjdp  long offset;
272533965Sjdp
272633965Sjdp  if (flag_do_long_jump)
272733965Sjdp    {
272833965Sjdp      offset = to_addr - S_GET_VALUE (to_symbol);
272933965Sjdp      md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
273033965Sjdp      md_number_to_chars (ptr + 1, (valueT) offset, 4);
273133965Sjdp      fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
273233965Sjdp	       to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
273333965Sjdp    }
273433965Sjdp  else
273533965Sjdp    {
273633965Sjdp      offset = to_addr - (from_addr + 5);
273733965Sjdp      md_number_to_chars (ptr, (valueT) 0xe9, 1);
273833965Sjdp      md_number_to_chars (ptr + 1, (valueT) offset, 4);
273933965Sjdp    }
274033965Sjdp}
274133965Sjdp
274233965Sjdp/* Apply a fixup (fixS) to segment data, once it has been determined
274333965Sjdp   by our caller that we have all the info we need to fix it up.
274433965Sjdp
274533965Sjdp   On the 386, immediates, displacements, and data pointers are all in
274633965Sjdp   the same (little-endian) format, so we don't need to care about which
274733965Sjdp   we are handling.  */
274833965Sjdp
274933965Sjdpint
275033965Sjdpmd_apply_fix3 (fixP, valp, seg)
275133965Sjdp     fixS *fixP;		/* The fix we're to put in.  */
275233965Sjdp     valueT *valp;		/* Pointer to the value of the bits.  */
275333965Sjdp     segT seg;			/* Segment fix is from.  */
275433965Sjdp{
275533965Sjdp  register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
275633965Sjdp  valueT value = *valp;
275733965Sjdp
275838891Sjdp  if (fixP->fx_r_type == BFD_RELOC_32 && fixP->fx_pcrel)
275938891Sjdp     fixP->fx_r_type = BFD_RELOC_32_PCREL;
276038891Sjdp
276133965Sjdp#if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
276233965Sjdp  /*
276333965Sjdp   * This is a hack.  There should be a better way to
276433965Sjdp   * handle this.
276533965Sjdp   */
276633965Sjdp  if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
276733965Sjdp    {
276833965Sjdp#ifndef OBJ_AOUT
276938891Sjdp      if (OUTPUT_FLAVOR == bfd_target_elf_flavour
277038891Sjdp	  || OUTPUT_FLAVOR == bfd_target_coff_flavour)
277133965Sjdp	value += fixP->fx_where + fixP->fx_frag->fr_address;
277233965Sjdp#endif
277333965Sjdp#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
277433965Sjdp      if (OUTPUT_FLAVOR == bfd_target_elf_flavour
277533965Sjdp	  && (S_GET_SEGMENT (fixP->fx_addsy) == seg
277633965Sjdp	      || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0))
277733965Sjdp	{
277833965Sjdp	  /* Yes, we add the values in twice.  This is because
277933965Sjdp	     bfd_perform_relocation subtracts them out again.  I think
278033965Sjdp	     bfd_perform_relocation is broken, but I don't dare change
278133965Sjdp	     it.  FIXME.  */
278233965Sjdp	  value += fixP->fx_where + fixP->fx_frag->fr_address;
278333965Sjdp	}
278433965Sjdp#endif
278538891Sjdp#if defined (OBJ_COFF) && defined (TE_PE)
278638891Sjdp      /* For some reason, the PE format does not store a section
278738891Sjdp         address offset for a PC relative symbol.  */
278838891Sjdp      if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
278938891Sjdp	value += md_pcrel_from (fixP);
279038891Sjdp#endif
279133965Sjdp    }
279233965Sjdp
279333965Sjdp  /* Fix a few things - the dynamic linker expects certain values here,
279433965Sjdp     and we must not dissappoint it. */
279533965Sjdp#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
279633965Sjdp  if (OUTPUT_FLAVOR == bfd_target_elf_flavour
279733965Sjdp      && fixP->fx_addsy)
279833965Sjdp    switch(fixP->fx_r_type) {
279933965Sjdp    case BFD_RELOC_386_PLT32:
280033965Sjdp      /* Make the jump instruction point to the address of the operand.  At
280133965Sjdp	 runtime we merely add the offset to the actual PLT entry. */
280233965Sjdp      value = 0xfffffffc;
280333965Sjdp      break;
280433965Sjdp    case BFD_RELOC_386_GOTPC:
280533965Sjdp/*
280633965Sjdp *  This is tough to explain.  We end up with this one if we have
280733965Sjdp * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal
280833965Sjdp * here is to obtain the absolute address of the GOT, and it is strongly
280933965Sjdp * preferable from a performance point of view to avoid using a runtime
281033965Sjdp * relocation for this.  The actual sequence of instructions often look
281133965Sjdp * something like:
281233965Sjdp *
281333965Sjdp * 	call	.L66
281433965Sjdp * .L66:
281533965Sjdp * 	popl	%ebx
281633965Sjdp * 	addl	$_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
281733965Sjdp *
281833965Sjdp * 	The call and pop essentially return the absolute address of
281933965Sjdp * the label .L66 and store it in %ebx.  The linker itself will
282033965Sjdp * ultimately change the first operand of the addl so that %ebx points to
282133965Sjdp * the GOT, but to keep things simple, the .o file must have this operand
282233965Sjdp * set so that it generates not the absolute address of .L66, but the
282333965Sjdp * absolute address of itself.  This allows the linker itself simply
282433965Sjdp * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
282533965Sjdp * added in, and the addend of the relocation is stored in the operand
282633965Sjdp * field for the instruction itself.
282733965Sjdp *
282833965Sjdp * 	Our job here is to fix the operand so that it would add the correct
282933965Sjdp * offset so that %ebx would point to itself.  The thing that is tricky is
283033965Sjdp * that .-.L66 will point to the beginning of the instruction, so we need
283133965Sjdp * to further modify the operand so that it will point to itself.
283233965Sjdp * There are other cases where you have something like:
283333965Sjdp *
283433965Sjdp * 	.long	$_GLOBAL_OFFSET_TABLE_+[.-.L66]
283533965Sjdp *
283633965Sjdp * and here no correction would be required.  Internally in the assembler
283733965Sjdp * we treat operands of this form as not being pcrel since the '.' is
283833965Sjdp * explicitly mentioned, and I wonder whether it would simplify matters
283933965Sjdp * to do it this way.  Who knows.  In earlier versions of the PIC patches,
284033965Sjdp * the pcrel_adjust field was used to store the correction, but since the
284133965Sjdp * expression is not pcrel, I felt it would be confusing to do it this way.
284233965Sjdp */
284333965Sjdp      value -= 1;
284433965Sjdp      break;
284533965Sjdp    case BFD_RELOC_386_GOT32:
284633965Sjdp      value = 0; /* Fully resolved at runtime.  No addend. */
284733965Sjdp      break;
284833965Sjdp    case BFD_RELOC_386_GOTOFF:
284933965Sjdp      break;
285033965Sjdp
285133965Sjdp    default:
285233965Sjdp      break;
285333965Sjdp    }
285433965Sjdp#endif
285533965Sjdp
285633965Sjdp#endif
285733965Sjdp  md_number_to_chars (p, value, fixP->fx_size);
285833965Sjdp
285933965Sjdp  return 1;
286033965Sjdp}
286133965Sjdp
286233965Sjdp#if 0
286333965Sjdp/* This is never used.  */
286433965Sjdplong				/* Knows about the byte order in a word. */
286533965Sjdpmd_chars_to_number (con, nbytes)
286633965Sjdp     unsigned char con[];	/* Low order byte 1st. */
286733965Sjdp     int nbytes;		/* Number of bytes in the input. */
286833965Sjdp{
286933965Sjdp  long retval;
287033965Sjdp  for (retval = 0, con += nbytes - 1; nbytes--; con--)
287133965Sjdp    {
287233965Sjdp      retval <<= BITS_PER_CHAR;
287333965Sjdp      retval |= *con;
287433965Sjdp    }
287533965Sjdp  return retval;
287633965Sjdp}
287733965Sjdp#endif /* 0 */
287833965Sjdp
287933965Sjdp
288033965Sjdp#define MAX_LITTLENUMS 6
288133965Sjdp
288233965Sjdp/* Turn the string pointed to by litP into a floating point constant of type
288333965Sjdp   type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
288433965Sjdp   is stored in *sizeP .  An error message is returned, or NULL on OK.  */
288533965Sjdpchar *
288633965Sjdpmd_atof (type, litP, sizeP)
288733965Sjdp     char type;
288833965Sjdp     char *litP;
288933965Sjdp     int *sizeP;
289033965Sjdp{
289133965Sjdp  int prec;
289233965Sjdp  LITTLENUM_TYPE words[MAX_LITTLENUMS];
289333965Sjdp  LITTLENUM_TYPE *wordP;
289433965Sjdp  char *t;
289533965Sjdp
289633965Sjdp  switch (type)
289733965Sjdp    {
289833965Sjdp    case 'f':
289933965Sjdp    case 'F':
290033965Sjdp      prec = 2;
290133965Sjdp      break;
290233965Sjdp
290333965Sjdp    case 'd':
290433965Sjdp    case 'D':
290533965Sjdp      prec = 4;
290633965Sjdp      break;
290733965Sjdp
290833965Sjdp    case 'x':
290933965Sjdp    case 'X':
291033965Sjdp      prec = 5;
291133965Sjdp      break;
291233965Sjdp
291333965Sjdp    default:
291433965Sjdp      *sizeP = 0;
291533965Sjdp      return "Bad call to md_atof ()";
291633965Sjdp    }
291733965Sjdp  t = atof_ieee (input_line_pointer, type, words);
291833965Sjdp  if (t)
291933965Sjdp    input_line_pointer = t;
292033965Sjdp
292133965Sjdp  *sizeP = prec * sizeof (LITTLENUM_TYPE);
292233965Sjdp  /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
292333965Sjdp     the bigendian 386.  */
292433965Sjdp  for (wordP = words + prec - 1; prec--;)
292533965Sjdp    {
292633965Sjdp      md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
292733965Sjdp      litP += sizeof (LITTLENUM_TYPE);
292833965Sjdp    }
292933965Sjdp  return 0;
293033965Sjdp}
293133965Sjdp
293233965Sjdpchar output_invalid_buf[8];
293333965Sjdp
293433965Sjdpstatic char *
293533965Sjdpoutput_invalid (c)
293633965Sjdp     char c;
293733965Sjdp{
293833965Sjdp  if (isprint (c))
293933965Sjdp    sprintf (output_invalid_buf, "'%c'", c);
294033965Sjdp  else
294133965Sjdp    sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
294233965Sjdp  return output_invalid_buf;
294333965Sjdp}
294433965Sjdp
294533965Sjdp/* reg_string starts *before* REGISTER_PREFIX */
294633965Sjdpstatic reg_entry *
294733965Sjdpparse_register (reg_string)
294833965Sjdp     char *reg_string;
294933965Sjdp{
295033965Sjdp  register char *s = reg_string;
295133965Sjdp  register char *p;
295233965Sjdp  char reg_name_given[MAX_REG_NAME_SIZE];
295333965Sjdp
295433965Sjdp  s++;				/* skip REGISTER_PREFIX */
295533965Sjdp  for (p = reg_name_given; is_register_char (*s); p++, s++)
295633965Sjdp    {
295733965Sjdp      *p = register_chars[(unsigned char) *s];
295833965Sjdp      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
295933965Sjdp	return (reg_entry *) 0;
296033965Sjdp    }
296133965Sjdp  *p = '\0';
296233965Sjdp  return (reg_entry *) hash_find (reg_hash, reg_name_given);
296333965Sjdp}
296433965Sjdp
296533965Sjdp#ifdef OBJ_ELF
296633965SjdpCONST char *md_shortopts = "kmVQ:";
296733965Sjdp#else
296833965SjdpCONST char *md_shortopts = "m";
296933965Sjdp#endif
297033965Sjdpstruct option md_longopts[] = {
297133965Sjdp  {NULL, no_argument, NULL, 0}
297233965Sjdp};
297333965Sjdpsize_t md_longopts_size = sizeof(md_longopts);
297433965Sjdp
297533965Sjdpint
297633965Sjdpmd_parse_option (c, arg)
297733965Sjdp     int c;
297833965Sjdp     char *arg;
297933965Sjdp{
298033965Sjdp  switch (c)
298133965Sjdp    {
298233965Sjdp    case 'm':
298333965Sjdp      flag_do_long_jump = 1;
298433965Sjdp      break;
298533965Sjdp
298633965Sjdp#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
298733965Sjdp      /* -k: Ignore for FreeBSD compatibility.  */
298833965Sjdp    case 'k':
298933965Sjdp      break;
299033965Sjdp
299133965Sjdp      /* -V: SVR4 argument to print version ID.  */
299233965Sjdp    case 'V':
299333965Sjdp      print_version_id ();
299433965Sjdp      break;
299533965Sjdp
299633965Sjdp      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
299733965Sjdp	 should be emitted or not.  FIXME: Not implemented.  */
299833965Sjdp    case 'Q':
299933965Sjdp      break;
300033965Sjdp#endif
300133965Sjdp
300233965Sjdp    default:
300333965Sjdp      return 0;
300433965Sjdp    }
300533965Sjdp  return 1;
300633965Sjdp}
300733965Sjdp
300833965Sjdpvoid
300933965Sjdpmd_show_usage (stream)
301033965Sjdp     FILE *stream;
301133965Sjdp{
301233965Sjdp  fprintf (stream, "\
301333965Sjdp-m			do long jump\n");
301433965Sjdp}
301533965Sjdp
301633965Sjdp#ifdef BFD_ASSEMBLER
301733965Sjdp#ifdef OBJ_MAYBE_ELF
301833965Sjdp#ifdef OBJ_MAYBE_COFF
301933965Sjdp
302033965Sjdp/* Pick the target format to use.  */
302133965Sjdp
302233965Sjdpconst char  *
302333965Sjdpi386_target_format ()
302433965Sjdp{
302533965Sjdp  switch (OUTPUT_FLAVOR)
302633965Sjdp    {
302733965Sjdp    case bfd_target_coff_flavour:
302833965Sjdp      return "coff-i386";
302933965Sjdp    case bfd_target_elf_flavour:
303033965Sjdp      return "elf32-i386";
303133965Sjdp    default:
303233965Sjdp      abort ();
303333965Sjdp      return NULL;
303433965Sjdp    }
303533965Sjdp}
303633965Sjdp
303733965Sjdp#endif /* OBJ_MAYBE_COFF */
303833965Sjdp#endif /* OBJ_MAYBE_ELF */
303933965Sjdp#endif /* BFD_ASSEMBLER */
304033965Sjdp
304133965Sjdp/* ARGSUSED */
304233965SjdpsymbolS *
304333965Sjdpmd_undefined_symbol (name)
304433965Sjdp     char *name;
304533965Sjdp{
304633965Sjdp	if (*name == '_' && *(name+1) == 'G'
304733965Sjdp	    && strcmp(name, GLOBAL_OFFSET_TABLE_NAME) == 0)
304833965Sjdp	  {
304933965Sjdp	    if(!GOT_symbol)
305033965Sjdp	      {
305133965Sjdp		if(symbol_find(name))
305233965Sjdp		  as_bad("GOT already in symbol table");
305333965Sjdp		GOT_symbol = symbol_new (name, undefined_section,
305433965Sjdp					 (valueT) 0, &zero_address_frag);
305533965Sjdp	      };
305633965Sjdp	    return GOT_symbol;
305733965Sjdp	  }
305833965Sjdp  return 0;
305933965Sjdp}
306033965Sjdp
306133965Sjdp/* Round up a section size to the appropriate boundary.  */
306233965SjdpvalueT
306333965Sjdpmd_section_align (segment, size)
306433965Sjdp     segT segment;
306533965Sjdp     valueT size;
306633965Sjdp{
306733965Sjdp#ifdef OBJ_AOUT
306833965Sjdp#ifdef BFD_ASSEMBLER
306933965Sjdp  /* For a.out, force the section size to be aligned.  If we don't do
307033965Sjdp     this, BFD will align it for us, but it will not write out the
307133965Sjdp     final bytes of the section.  This may be a bug in BFD, but it is
307233965Sjdp     easier to fix it here since that is how the other a.out targets
307333965Sjdp     work.  */
307433965Sjdp  int align;
307533965Sjdp
307633965Sjdp  align = bfd_get_section_alignment (stdoutput, segment);
307733965Sjdp  size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
307833965Sjdp#endif
307933965Sjdp#endif
308033965Sjdp
308133965Sjdp  return size;
308233965Sjdp}
308333965Sjdp
308433965Sjdp/* Exactly what point is a PC-relative offset relative TO?  On the
308533965Sjdp   i386, they're relative to the address of the offset, plus its
308633965Sjdp   size. (??? Is this right?  FIXME-SOON!) */
308733965Sjdplong
308833965Sjdpmd_pcrel_from (fixP)
308933965Sjdp     fixS *fixP;
309033965Sjdp{
309133965Sjdp  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
309233965Sjdp}
309333965Sjdp
309433965Sjdp#ifndef I386COFF
309533965Sjdp
309633965Sjdpstatic void
309733965Sjdps_bss (ignore)
309833965Sjdp     int ignore;
309933965Sjdp{
310033965Sjdp  register int temp;
310133965Sjdp
310233965Sjdp  temp = get_absolute_expression ();
310333965Sjdp  subseg_set (bss_section, (subsegT) temp);
310433965Sjdp  demand_empty_rest_of_line ();
310533965Sjdp}
310633965Sjdp
310733965Sjdp#endif
310833965Sjdp
310933965Sjdp
311033965Sjdp#ifdef BFD_ASSEMBLER
311133965Sjdp
311233965Sjdpvoid
311333965Sjdpi386_validate_fix (fixp)
311433965Sjdp     fixS *fixp;
311533965Sjdp{
311633965Sjdp  if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
311733965Sjdp    {
311833965Sjdp      fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
311933965Sjdp      fixp->fx_subsy = 0;
312033965Sjdp    }
312133965Sjdp}
312233965Sjdp
312333965Sjdp#define F(SZ,PCREL)		(((SZ) << 1) + (PCREL))
312433965Sjdp#define MAP(SZ,PCREL,TYPE)	case F(SZ,PCREL): code = (TYPE); break
312533965Sjdp
312633965Sjdparelent *
312733965Sjdptc_gen_reloc (section, fixp)
312833965Sjdp     asection *section;
312933965Sjdp     fixS *fixp;
313033965Sjdp{
313133965Sjdp  arelent *rel;
313233965Sjdp  bfd_reloc_code_real_type code;
313333965Sjdp
313433965Sjdp  switch(fixp->fx_r_type)
313533965Sjdp    {
313633965Sjdp    case BFD_RELOC_386_PLT32:
313733965Sjdp    case BFD_RELOC_386_GOT32:
313833965Sjdp    case BFD_RELOC_386_GOTOFF:
313933965Sjdp    case BFD_RELOC_386_GOTPC:
314038891Sjdp    case BFD_RELOC_RVA:
314133965Sjdp      code = fixp->fx_r_type;
314233965Sjdp      break;
314333965Sjdp    default:
314433965Sjdp      switch (F (fixp->fx_size, fixp->fx_pcrel))
314533965Sjdp	{
314633965Sjdp	  MAP (1, 0, BFD_RELOC_8);
314733965Sjdp	  MAP (2, 0, BFD_RELOC_16);
314833965Sjdp	  MAP (4, 0, BFD_RELOC_32);
314933965Sjdp	  MAP (1, 1, BFD_RELOC_8_PCREL);
315033965Sjdp	  MAP (2, 1, BFD_RELOC_16_PCREL);
315133965Sjdp	  MAP (4, 1, BFD_RELOC_32_PCREL);
315233965Sjdp	default:
315333965Sjdp	  as_bad ("Can not do %d byte %srelocation", fixp->fx_size,
315433965Sjdp		  fixp->fx_pcrel ? "pc-relative " : "");
315533965Sjdp	}
315633965Sjdp    }
315733965Sjdp#undef MAP
315833965Sjdp#undef F
315933965Sjdp
316033965Sjdp  if (code == BFD_RELOC_32
316133965Sjdp      && GOT_symbol
316233965Sjdp      && fixp->fx_addsy == GOT_symbol)
316333965Sjdp    code = BFD_RELOC_386_GOTPC;
316433965Sjdp
316533965Sjdp  rel = (arelent *) xmalloc (sizeof (arelent));
316633965Sjdp  rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
316733965Sjdp  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
316833965Sjdp  if (fixp->fx_pcrel)
316933965Sjdp    rel->addend = fixp->fx_addnumber;
317033965Sjdp  else
317133965Sjdp    rel->addend = 0;
317233965Sjdp
317333965Sjdp  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
317433965Sjdp  if (rel->howto == NULL)
317533965Sjdp    {
317633965Sjdp      as_bad_where (fixp->fx_file, fixp->fx_line,
317733965Sjdp		    "Cannot represent relocation type %s",
317833965Sjdp		    bfd_get_reloc_code_name (code));
317933965Sjdp      /* Set howto to a garbage value so that we can keep going.  */
318033965Sjdp      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
318133965Sjdp      assert (rel->howto != NULL);
318233965Sjdp    }
318333965Sjdp
318433965Sjdp  return rel;
318533965Sjdp}
318633965Sjdp
318733965Sjdp#else /* ! BFD_ASSEMBLER */
318833965Sjdp
318933965Sjdp#if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
319033965Sjdpvoid
319133965Sjdptc_aout_fix_to_chars (where, fixP, segment_address_in_file)
319233965Sjdp     char *where;
319333965Sjdp     fixS *fixP;
319433965Sjdp     relax_addressT segment_address_in_file;
319533965Sjdp{
319633965Sjdp  /*
319733965Sjdp   * In: length of relocation (or of address) in chars: 1, 2 or 4.
319833965Sjdp   * Out: GNU LD relocation length code: 0, 1, or 2.
319933965Sjdp   */
320033965Sjdp
320133965Sjdp  static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
320233965Sjdp  long r_symbolnum;
320333965Sjdp
320433965Sjdp  know (fixP->fx_addsy != NULL);
320533965Sjdp
320633965Sjdp  md_number_to_chars (where,
320733965Sjdp		      (valueT) (fixP->fx_frag->fr_address
320833965Sjdp				+ fixP->fx_where - segment_address_in_file),
320933965Sjdp		      4);
321033965Sjdp
321133965Sjdp  r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
321233965Sjdp		 ? S_GET_TYPE (fixP->fx_addsy)
321333965Sjdp		 : fixP->fx_addsy->sy_number);
321433965Sjdp
321533965Sjdp  where[6] = (r_symbolnum >> 16) & 0x0ff;
321633965Sjdp  where[5] = (r_symbolnum >> 8) & 0x0ff;
321733965Sjdp  where[4] = r_symbolnum & 0x0ff;
321833965Sjdp  where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
321933965Sjdp	      | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
322033965Sjdp	      | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
322133965Sjdp}
322233965Sjdp
322333965Sjdp#endif /* OBJ_AOUT or OBJ_BOUT */
322433965Sjdp
322533965Sjdp#if defined (I386COFF)
322633965Sjdp
322733965Sjdpshort
322833965Sjdptc_coff_fix2rtype (fixP)
322933965Sjdp     fixS *fixP;
323033965Sjdp{
323133965Sjdp  if (fixP->fx_r_type == R_IMAGEBASE)
323233965Sjdp    return R_IMAGEBASE;
323333965Sjdp
323433965Sjdp  return (fixP->fx_pcrel ?
323533965Sjdp	  (fixP->fx_size == 1 ? R_PCRBYTE :
323633965Sjdp	   fixP->fx_size == 2 ? R_PCRWORD :
323733965Sjdp	   R_PCRLONG) :
323833965Sjdp	  (fixP->fx_size == 1 ? R_RELBYTE :
323933965Sjdp	   fixP->fx_size == 2 ? R_RELWORD :
324033965Sjdp	   R_DIR32));
324133965Sjdp}
324233965Sjdp
324333965Sjdpint
324433965Sjdptc_coff_sizemachdep (frag)
324533965Sjdp     fragS *frag;
324633965Sjdp{
324733965Sjdp  if (frag->fr_next)
324833965Sjdp    return (frag->fr_next->fr_address - frag->fr_address);
324933965Sjdp  else
325033965Sjdp    return 0;
325133965Sjdp}
325233965Sjdp
325333965Sjdp#endif /* I386COFF */
325433965Sjdp
325533965Sjdp#endif /* BFD_ASSEMBLER? */
325633965Sjdp
325733965Sjdp/* end of tc-i386.c */
3258