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