29c6b501c677699694eda85c16a7ca8cbaa479a5
[platform/upstream/binutils.git] / gas / config / tc-i386.c
1 /* i386.c -- Assemble code for the Intel 80386
2    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /*
21   Intel 80386 machine specific gas.
22   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23   Bugs & suggestions are completely welcome.  This is free software.
24   Please help us make it better.
25   */
26
27 #include <ctype.h>
28
29 #include "as.h"
30 #include "subsegs.h"
31
32 #include "obstack.h"
33 #include "opcode/i386.h"
34
35 #ifndef TC_RELOC
36 #define TC_RELOC(X,Y) (Y)
37 #endif
38
39 /* 'md_assemble ()' gathers together information and puts it into a
40    i386_insn. */
41
42 struct _i386_insn
43   {
44     /* TM holds the template for the insn were currently assembling. */
45     template tm;
46     /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
47     char suffix;
48     /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
49
50     /* OPERANDS gives the number of given operands. */
51     unsigned int operands;
52
53     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
54        of given register, displacement, memory operands and immediate
55        operands. */
56     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
57
58     /* TYPES [i] is the type (see above #defines) which tells us how to
59        search through DISPS [i] & IMMS [i] & REGS [i] for the required
60        operand.  */
61     unsigned int types[MAX_OPERANDS];
62
63     /* Displacements (if given) for each operand. */
64     expressionS *disps[MAX_OPERANDS];
65
66     /* Relocation type for operand */
67 #ifdef BFD_ASSEMBLER
68     enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
69 #else
70     int disp_reloc[MAX_OPERANDS];
71 #endif
72
73     /* Immediate operands (if given) for each operand. */
74     expressionS *imms[MAX_OPERANDS];
75
76     /* Register operands (if given) for each operand. */
77     reg_entry *regs[MAX_OPERANDS];
78
79     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
80        the base index byte below.  */
81     reg_entry *base_reg;
82     reg_entry *index_reg;
83     unsigned int log2_scale_factor;
84
85     /* SEG gives the seg_entry of this insn.  It is equal to zero unless
86        an explicit segment override is given. */
87     const seg_entry *seg;       /* segment for memory operands (if given) */
88
89     /* PREFIX holds all the given prefix opcodes (usually null).
90        PREFIXES is the size of PREFIX. */
91     /* richfix: really unsigned? */
92     unsigned char prefix[MAX_PREFIXES];
93     unsigned int prefixes;
94
95     /* RM and IB are the modrm byte and the base index byte where the
96        addressing modes of this insn are encoded. */
97
98     modrm_byte rm;
99     base_index_byte bi;
100   };
101
102 typedef struct _i386_insn i386_insn;
103
104 /* This array holds the chars that always start a comment.  If the
105    pre-processor is disabled, these aren't very useful */
106 #if defined (TE_I386AIX) || defined (OBJ_ELF)
107 const char comment_chars[] = "#/";
108 #else
109 const char comment_chars[] = "#";
110 #endif
111
112 /* This array holds the chars that only start a comment at the beginning of
113    a line.  If the line seems to have the form '# 123 filename'
114    .line and .file directives will appear in the pre-processed output */
115 /* Note that input_file.c hand checks for '#' at the beginning of the
116    first line of the input file.  This is because the compiler outputs
117    #NO_APP at the beginning of its output. */
118 /* Also note that comments started like this one will always work if
119    '/' isn't otherwise defined.  */
120 #if defined (TE_I386AIX) || defined (OBJ_ELF)
121 const char line_comment_chars[] = "";
122 #else
123 const char line_comment_chars[] = "/";
124 #endif
125 const char line_separator_chars[] = "";
126
127 /* Chars that can be used to separate mant from exp in floating point nums */
128 const char EXP_CHARS[] = "eE";
129
130 /* Chars that mean this number is a floating point constant */
131 /* As in 0f12.456 */
132 /* or    0d1.2345e12 */
133 const char FLT_CHARS[] = "fFdDxX";
134
135 /* tables for lexical analysis */
136 static char opcode_chars[256];
137 static char register_chars[256];
138 static char operand_chars[256];
139 static char space_chars[256];
140 static char identifier_chars[256];
141 static char digit_chars[256];
142
143 /* lexical macros */
144 #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
145 #define is_operand_char(x) (operand_chars[(unsigned char) x])
146 #define is_register_char(x) (register_chars[(unsigned char) x])
147 #define is_space_char(x) (space_chars[(unsigned char) x])
148 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
149 #define is_digit_char(x) (digit_chars[(unsigned char) x])
150
151 /* put here all non-digit non-letter charcters that may occur in an operand */
152 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
153
154 static char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
155
156 /* md_assemble() always leaves the strings it's passed unaltered.  To
157    effect this we maintain a stack of saved characters that we've smashed
158    with '\0's (indicating end of strings for various sub-fields of the
159    assembler instruction). */
160 static char save_stack[32];
161 static char *save_stack_p;      /* stack pointer */
162 #define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
163 #define RESTORE_END_STRING(s)       *s = *--save_stack_p
164
165 /* The instruction we're assembling. */
166 static i386_insn i;
167
168 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
169 static expressionS disp_expressions[2], im_expressions[2];
170
171 /* pointers to ebp & esp entries in reg_hash hash table */
172 static reg_entry *ebp, *esp;
173
174 static int this_operand;        /* current operand we are working on */
175
176 static int flag_do_long_jump;   /* FIXME what does this do? */
177
178 static int flag_16bit_code;     /* 1 if we're writing 16-bit code, 0 if 32-bit */
179
180 /* Interface to relax_segment.
181    There are 2 relax states for 386 jump insns: one for conditional &
182    one for unconditional jumps.  This is because the these two types
183    of jumps add different sizes to frags when we're figuring out what
184    sort of jump to choose to reach a given label.  */
185
186 /* types */
187 #define COND_JUMP 1             /* conditional jump */
188 #define UNCOND_JUMP 2           /* unconditional jump */
189 /* sizes */
190 #define BYTE 0
191 #define WORD 1
192 #define DWORD 2
193 #define UNKNOWN_SIZE 3
194
195 #ifndef INLINE
196 #ifdef __GNUC__
197 #define INLINE __inline__
198 #else
199 #define INLINE
200 #endif
201 #endif
202
203 #define ENCODE_RELAX_STATE(type,size) \
204   ((relax_substateT)((type<<2) | (size)))
205 #define SIZE_FROM_RELAX_STATE(s) \
206     ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
207
208 const relax_typeS md_relax_table[] =
209 {
210 /* The fields are:
211    1) most positive reach of this state,
212    2) most negative reach of this state,
213    3) how many bytes this mode will add to the size of the current frag
214    4) which index into the table to try if we can't fit into this one.
215    */
216   {1, 1, 0, 0},
217   {1, 1, 0, 0},
218   {1, 1, 0, 0},
219   {1, 1, 0, 0},
220
221   /* For now we don't use word displacement jumps; they may be
222      untrustworthy. */
223   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
224   /* word conditionals add 3 bytes to frag:
225      2 opcode prefix; 1 displacement bytes */
226   {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
227   /* dword conditionals adds 4 bytes to frag:
228      1 opcode prefix; 3 displacement bytes */
229   {0, 0, 4, 0},
230   {1, 1, 0, 0},
231
232   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
233   /* word jmp adds 2 bytes to frag:
234      1 opcode prefix; 1 displacement bytes */
235   {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
236   /* dword jmp adds 3 bytes to frag:
237      0 opcode prefix; 3 displacement bytes */
238   {0, 0, 3, 0},
239   {1, 1, 0, 0},
240
241 };
242
243 static char *output_invalid PARAMS ((int c));
244 static int i386_operand PARAMS ((char *operand_string));
245 static reg_entry *parse_register PARAMS ((char *reg_string));
246 #ifndef I386COFF
247 static void s_bss PARAMS ((int));
248 #endif
249
250 symbolS *GOT_symbol;            /* Pre-defined "__GLOBAL_OFFSET_TABLE" */
251
252 static INLINE unsigned long
253 mode_from_disp_size (t)
254      unsigned long t;
255 {
256   return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
257 }
258
259 #if 0
260 /* Not used.  */
261 /* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
262
263 static INLINE unsigned long
264 opcode_suffix_to_type (s)
265      unsigned long s;
266 {
267   return (s == BYTE_OPCODE_SUFFIX
268           ? Byte : (s == WORD_OPCODE_SUFFIX
269                     ? Word : DWord));
270 }                               /* opcode_suffix_to_type() */
271 #endif
272
273 static INLINE int
274 fits_in_signed_byte (num)
275      long num;
276 {
277   return (num >= -128) && (num <= 127);
278 }                               /* fits_in_signed_byte() */
279
280 static INLINE int
281 fits_in_unsigned_byte (num)
282      long num;
283 {
284   return (num & 0xff) == num;
285 }                               /* fits_in_unsigned_byte() */
286
287 static INLINE int
288 fits_in_unsigned_word (num)
289      long num;
290 {
291   return (num & 0xffff) == num;
292 }                               /* fits_in_unsigned_word() */
293
294 static INLINE int
295 fits_in_signed_word (num)
296      long num;
297 {
298   return (-32768 <= num) && (num <= 32767);
299 }                               /* fits_in_signed_word() */
300
301 static int
302 smallest_imm_type (num)
303      long num;
304 {
305 #if 0
306   /* This code is disabled because all the Imm1 forms in the opcode table
307      are slower on the i486, and they're the versions with the implicitly
308      specified single-position displacement, which has another syntax if
309      you really want to use that form.  If you really prefer to have the
310      one-byte-shorter Imm1 form despite these problems, re-enable this
311      code.  */
312   if (num == 1)
313     return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
314 #endif
315   return (fits_in_signed_byte (num)
316           ? (Imm8S | Imm8 | Imm16 | Imm32)
317           : fits_in_unsigned_byte (num)
318           ? (Imm8 | Imm16 | Imm32)
319           : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
320           ? (Imm16 | Imm32)
321           : (Imm32));
322 }                               /* smallest_imm_type() */
323
324 void set_16bit_code_flag(new_16bit_code_flag)
325         int new_16bit_code_flag;
326 {
327   flag_16bit_code = new_16bit_code_flag;
328 }
329
330 const pseudo_typeS md_pseudo_table[] =
331 {
332 #ifndef I386COFF
333   {"bss", s_bss, 0},
334 #endif
335 #ifndef OBJ_AOUT
336   {"align", s_align_bytes, 0},
337 #else
338   {"align", s_align_ptwo, 0},
339 #endif
340   {"ffloat", float_cons, 'f'},
341   {"dfloat", float_cons, 'd'},
342   {"tfloat", float_cons, 'x'},
343   {"value", cons, 2},
344   {"noopt", s_ignore, 0},
345   {"optim", s_ignore, 0},
346   {"code16", set_16bit_code_flag, 1},
347   {"code32", set_16bit_code_flag, 0},
348   {0, 0, 0}
349 };
350
351 /* for interface with expression () */
352 extern char *input_line_pointer;
353
354 /* obstack for constructing various things in md_begin */
355 struct obstack o;
356
357 /* hash table for opcode lookup */
358 static struct hash_control *op_hash;
359 /* hash table for register lookup */
360 static struct hash_control *reg_hash;
361 /* hash table for prefix lookup */
362 static struct hash_control *prefix_hash;
363 \f
364
365 void
366 md_begin ()
367 {
368   const char *hash_err;
369
370   obstack_begin (&o, 4096);
371
372   /* initialize op_hash hash table */
373   op_hash = hash_new ();
374
375   {
376     register const template *optab;
377     register templates *core_optab;
378     char *prev_name;
379
380     optab = i386_optab;         /* setup for loop */
381     prev_name = optab->name;
382     obstack_grow (&o, optab, sizeof (template));
383     core_optab = (templates *) xmalloc (sizeof (templates));
384
385     for (optab++; optab < i386_optab_end; optab++)
386       {
387         if (!strcmp (optab->name, prev_name))
388           {
389             /* same name as before --> append to current template list */
390             obstack_grow (&o, optab, sizeof (template));
391           }
392         else
393           {
394             /* different name --> ship out current template list;
395                add to hash table; & begin anew */
396             /* Note: end must be set before start! since obstack_next_free
397                changes upon opstack_finish */
398             core_optab->end = (template *) obstack_next_free (&o);
399             core_optab->start = (template *) obstack_finish (&o);
400             hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
401             if (hash_err)
402               {
403               hash_error:
404                 as_fatal ("Internal Error:  Can't hash %s: %s", prev_name,
405                           hash_err);
406               }
407             prev_name = optab->name;
408             core_optab = (templates *) xmalloc (sizeof (templates));
409             obstack_grow (&o, optab, sizeof (template));
410           }
411       }
412   }
413
414   /* initialize reg_hash hash table */
415   reg_hash = hash_new ();
416   {
417     register const reg_entry *regtab;
418
419     for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
420       {
421         hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
422         if (hash_err)
423           goto hash_error;
424       }
425   }
426
427   esp = (reg_entry *) hash_find (reg_hash, "esp");
428   ebp = (reg_entry *) hash_find (reg_hash, "ebp");
429
430   /* initialize reg_hash hash table */
431   prefix_hash = hash_new ();
432   {
433     register const prefix_entry *prefixtab;
434
435     for (prefixtab = i386_prefixtab;
436          prefixtab < i386_prefixtab_end; prefixtab++)
437       {
438         hash_err = hash_insert (prefix_hash, prefixtab->prefix_name,
439                                 (PTR) prefixtab);
440         if (hash_err)
441           goto hash_error;
442       }
443   }
444
445   /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
446   {
447     register int c;
448     register char *p;
449
450     for (c = 0; c < 256; c++)
451       {
452         if (islower (c) || isdigit (c))
453           {
454             opcode_chars[c] = c;
455             register_chars[c] = c;
456           }
457         else if (isupper (c))
458           {
459             opcode_chars[c] = tolower (c);
460             register_chars[c] = opcode_chars[c];
461           }
462         else if (c == PREFIX_SEPERATOR)
463           {
464             opcode_chars[c] = c;
465           }
466         else if (c == ')' || c == '(')
467           {
468             register_chars[c] = c;
469           }
470
471         if (isupper (c) || islower (c) || isdigit (c))
472           operand_chars[c] = c;
473
474         if (isdigit (c) || c == '-')
475           digit_chars[c] = c;
476
477         if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
478           identifier_chars[c] = c;
479
480 #ifdef LEX_AT
481         identifier_chars['@'] = '@';
482 #endif
483
484         if (c == ' ' || c == '\t')
485           space_chars[c] = c;
486       }
487
488     for (p = operand_special_chars; *p != '\0'; p++)
489       operand_chars[(unsigned char) *p] = *p;
490   }
491
492 #ifdef OBJ_ELF
493   record_alignment (text_section, 2);
494   record_alignment (data_section, 2);
495   record_alignment (bss_section, 2);
496 #endif
497 }
498 \f
499
500 #ifdef DEBUG386
501
502 /* debugging routines for md_assemble */
503 static void pi PARAMS ((char *, i386_insn *));
504 static void pte PARAMS ((template *));
505 static void pt PARAMS ((unsigned int));
506 static void pe PARAMS ((expressionS *));
507 static void ps PARAMS ((symbolS *));
508
509 static void
510 pi (line, x)
511      char *line;
512      i386_insn *x;
513 {
514   register template *p;
515   int i;
516
517   fprintf (stdout, "%s: template ", line);
518   pte (&x->tm);
519   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
520            x->rm.mode, x->rm.reg, x->rm.regmem);
521   fprintf (stdout, " base %x  index %x  scale %x\n",
522            x->bi.base, x->bi.index, x->bi.scale);
523   for (i = 0; i < x->operands; i++)
524     {
525       fprintf (stdout, "    #%d:  ", i + 1);
526       pt (x->types[i]);
527       fprintf (stdout, "\n");
528       if (x->types[i] & Reg)
529         fprintf (stdout, "%s\n", x->regs[i]->reg_name);
530       if (x->types[i] & Imm)
531         pe (x->imms[i]);
532       if (x->types[i] & (Disp | Abs))
533         pe (x->disps[i]);
534     }
535 }
536
537 static void
538 pte (t)
539      template *t;
540 {
541   int i;
542   fprintf (stdout, " %d operands ", t->operands);
543   fprintf (stdout, "opcode %x ",
544            t->base_opcode);
545   if (t->extension_opcode != None)
546     fprintf (stdout, "ext %x ", t->extension_opcode);
547   if (t->opcode_modifier & D)
548     fprintf (stdout, "D");
549   if (t->opcode_modifier & W)
550     fprintf (stdout, "W");
551   fprintf (stdout, "\n");
552   for (i = 0; i < t->operands; i++)
553     {
554       fprintf (stdout, "    #%d type ", i + 1);
555       pt (t->operand_types[i]);
556       fprintf (stdout, "\n");
557     }
558 }
559
560 static void
561 pe (e)
562      expressionS *e;
563 {
564   fprintf (stdout, "    operation       %d\n", e->X_op);
565   fprintf (stdout, "    add_number    %d (%x)\n",
566            e->X_add_number, e->X_add_number);
567   if (e->X_add_symbol)
568     {
569       fprintf (stdout, "    add_symbol    ");
570       ps (e->X_add_symbol);
571       fprintf (stdout, "\n");
572     }
573   if (e->X_op_symbol)
574     {
575       fprintf (stdout, "    op_symbol    ");
576       ps (e->X_op_symbol);
577       fprintf (stdout, "\n");
578     }
579 }
580
581 static void
582 ps (s)
583      symbolS *s;
584 {
585   fprintf (stdout, "%s type %s%s",
586            S_GET_NAME (s),
587            S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
588            segment_name (S_GET_SEGMENT (s)));
589 }
590
591 struct type_name
592   {
593     unsigned int mask;
594     char *tname;
595   }
596
597 type_names[] =
598 {
599   { Reg8, "r8" },
600   { Reg16, "r16" },
601   { Reg32, "r32" },
602   { Imm8, "i8" },
603   { Imm8S, "i8s" },
604   { Imm16, "i16" },
605   { Imm32, "i32" },
606   { Mem8, "Mem8" },
607   { Mem16, "Mem16" },
608   { Mem32, "Mem32" },
609   { BaseIndex, "BaseIndex" },
610   { Abs8, "Abs8" },
611   { Abs16, "Abs16" },
612   { Abs32, "Abs32" },
613   { Disp8, "d8" },
614   { Disp16, "d16" },
615   { Disp32, "d32" },
616   { SReg2, "SReg2" },
617   { SReg3, "SReg3" },
618   { Acc, "Acc" },
619   { InOutPortReg, "InOutPortReg" },
620   { ShiftCount, "ShiftCount" },
621   { Imm1, "i1" },
622   { Control, "control reg" },
623   { Test, "test reg" },
624   { FloatReg, "FReg" },
625   { FloatAcc, "FAcc" },
626   { JumpAbsolute, "Jump Absolute" },
627   { 0, "" }
628 };
629
630 static void
631 pt (t)
632      unsigned int t;
633 {
634   register struct type_name *ty;
635
636   if (t == Unknown)
637     {
638       fprintf (stdout, "Unknown");
639     }
640   else
641     {
642       for (ty = type_names; ty->mask; ty++)
643         if (t & ty->mask)
644           fprintf (stdout, "%s, ", ty->tname);
645     }
646   fflush (stdout);
647 }
648
649 #endif /* DEBUG386 */
650 \f
651 #ifdef BFD_ASSEMBLER
652 static bfd_reloc_code_real_type
653 reloc (size, pcrel, other)
654      int size;
655      int pcrel;
656      bfd_reloc_code_real_type other;
657 {
658   if (other != NO_RELOC) return other;
659
660   if (pcrel)
661     switch (size)
662       {
663       case 1: return BFD_RELOC_8_PCREL;
664       case 2: return BFD_RELOC_16_PCREL;
665       case 4: return BFD_RELOC_32_PCREL;
666       }
667   else
668     switch (size)
669       {
670       case 1: return BFD_RELOC_8;
671       case 2: return BFD_RELOC_16;
672       case 4: return BFD_RELOC_32;
673       }
674
675   as_bad ("Can not do %d byte %srelocation", size,
676           pcrel ? "pc-relative " : "");
677   return BFD_RELOC_NONE;
678 }
679 #else
680 #define reloc(SIZE,PCREL,OTHER) 0
681 #define BFD_RELOC_32            0
682 #define BFD_RELOC_32_PCREL      0
683 #define BFD_RELOC_386_PLT32     0
684 #define BFD_RELOC_386_GOT32     0
685 #define BFD_RELOC_386_GOTOFF    0
686 #endif
687
688 /*
689  * Here we decide which fixups can be adjusted to make them relative to
690  * the beginning of the section instead of the symbol.  Basically we need
691  * to make sure that the dynamic relocations are done correctly, so in
692  * some cases we force the original symbol to be used.
693  */
694 int
695 tc_i386_fix_adjustable(fixP)
696      fixS * fixP;
697 {
698 #ifndef OBJ_AOUT
699   /* Prevent all adjustments to global symbols. */
700   if (S_IS_EXTERN (fixP->fx_addsy))
701     return 0;
702 #endif
703 #ifdef BFD_ASSEMBLER
704   /* adjust_reloc_syms doesn't know about the GOT */
705   if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
706       || fixP->fx_r_type == BFD_RELOC_386_PLT32
707       || fixP->fx_r_type == BFD_RELOC_386_GOT32)
708     return 0;
709 #endif
710   return 1;
711 }
712
713 /* This is the guts of the machine-dependent assembler.  LINE points to a
714    machine dependent instruction.  This function is supposed to emit
715    the frags/bytes it assembles to.  */
716
717 void
718 md_assemble (line)
719      char *line;
720 {
721   /* Holds template once we've found it. */
722   template *t;
723
724   /* Count the size of the instruction generated.  */
725   int insn_size = 0;
726
727   /* Possible templates for current insn */
728   templates *current_templates = (templates *) 0;
729
730   int j;
731
732   /* Initialize globals. */
733   memset (&i, '\0', sizeof (i));
734   for (j = 0; j < MAX_OPERANDS; j++)
735     i.disp_reloc[j] = NO_RELOC;
736   memset (disp_expressions, '\0', sizeof (disp_expressions));
737   memset (im_expressions, '\0', sizeof (im_expressions));
738   save_stack_p = save_stack;    /* reset stack pointer */
739
740   /* Fist parse an opcode & call i386_operand for the operands.
741      We assume that the scrubber has arranged it so that line[0] is the valid
742      start of a (possibly prefixed) opcode. */
743   {
744     char *l = line;
745
746     /* 1 if operand is pending after ','. */
747     unsigned int expecting_operand = 0;
748     /* 1 if we found a prefix only acceptable with string insns. */
749     unsigned int expecting_string_instruction = 0;
750     /* Non-zero if operand parens not balenced. */
751     unsigned int paren_not_balenced;
752     char *token_start = l;
753
754     while (!is_space_char (*l) && *l != END_OF_INSN)
755       {
756         if (!is_opcode_char (*l))
757           {
758             as_bad ("invalid character %s in opcode", output_invalid (*l));
759             return;
760           }
761         else if (*l != PREFIX_SEPERATOR)
762           {
763             *l = opcode_chars[(unsigned char) *l];      /* fold case of opcodes */
764             l++;
765           }
766         else
767           {
768             /* This opcode's got a prefix.  */
769             unsigned int q;
770             prefix_entry *prefix;
771
772             if (l == token_start)
773               {
774                 as_bad ("expecting prefix; got nothing");
775                 return;
776               }
777             END_STRING_AND_SAVE (l);
778             prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
779             if (!prefix)
780               {
781                 as_bad ("no such opcode prefix ('%s')", token_start);
782                 return;
783               }
784             RESTORE_END_STRING (l);
785             /* check for repeated prefix */
786             for (q = 0; q < i.prefixes; q++)
787               if (i.prefix[q] == prefix->prefix_code)
788                 {
789                   as_bad ("same prefix used twice; you don't really want this!");
790                   return;
791                 }
792             if (i.prefixes == MAX_PREFIXES)
793               {
794                 as_bad ("too many opcode prefixes");
795                 return;
796               }
797             i.prefix[i.prefixes++] = prefix->prefix_code;
798             if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
799               expecting_string_instruction = 1;
800             /* skip past PREFIX_SEPERATOR and reset token_start */
801             token_start = ++l;
802           }
803       }
804     END_STRING_AND_SAVE (l);
805     if (token_start == l)
806       {
807         as_bad ("expecting opcode; got nothing");
808         return;
809       }
810
811     /* Lookup insn in hash; try intel & att naming conventions if appropriate;
812        that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
813     current_templates = (templates *) hash_find (op_hash, token_start);
814     if (!current_templates)
815       {
816         int last_index = strlen (token_start) - 1;
817         char last_char = token_start[last_index];
818         switch (last_char)
819           {
820           case DWORD_OPCODE_SUFFIX:
821           case WORD_OPCODE_SUFFIX:
822           case BYTE_OPCODE_SUFFIX:
823             token_start[last_index] = '\0';
824             current_templates = (templates *) hash_find (op_hash, token_start);
825             token_start[last_index] = last_char;
826             i.suffix = last_char;
827           }
828         if (!current_templates)
829           {
830             as_bad ("no such 386 instruction: `%s'", token_start);
831             return;
832           }
833       }
834     RESTORE_END_STRING (l);
835
836     /* check for rep/repne without a string instruction */
837     if (expecting_string_instruction &&
838         !IS_STRING_INSTRUCTION (current_templates->
839                                 start->base_opcode))
840       {
841         as_bad ("expecting string instruction after rep/repne");
842         return;
843       }
844
845     /* There may be operands to parse. */
846     if (*l != END_OF_INSN &&
847         /* For string instructions, we ignore any operands if given.  This
848            kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
849            the operands are always going to be the same, and are not really
850            encoded in machine code. */
851         !IS_STRING_INSTRUCTION (current_templates->
852                                 start->base_opcode))
853       {
854         /* parse operands */
855         do
856           {
857             /* skip optional white space before operand */
858             while (!is_operand_char (*l) && *l != END_OF_INSN)
859               {
860                 if (!is_space_char (*l))
861                   {
862                     as_bad ("invalid character %s before %s operand",
863                             output_invalid (*l),
864                             ordinal_names[i.operands]);
865                     return;
866                   }
867                 l++;
868               }
869             token_start = l;    /* after white space */
870             paren_not_balenced = 0;
871             while (paren_not_balenced || *l != ',')
872               {
873                 if (*l == END_OF_INSN)
874                   {
875                     if (paren_not_balenced)
876                       {
877                         as_bad ("unbalenced parenthesis in %s operand.",
878                                 ordinal_names[i.operands]);
879                         return;
880                       }
881                     else
882                       break;    /* we are done */
883                   }
884                 else if (!is_operand_char (*l) && !is_space_char (*l))
885                   {
886                     as_bad ("invalid character %s in %s operand",
887                             output_invalid (*l),
888                             ordinal_names[i.operands]);
889                     return;
890                   }
891                 if (*l == '(')
892                   ++paren_not_balenced;
893                 if (*l == ')')
894                   --paren_not_balenced;
895                 l++;
896               }
897             if (l != token_start)
898               {                 /* yes, we've read in another operand */
899                 unsigned int operand_ok;
900                 this_operand = i.operands++;
901                 if (i.operands > MAX_OPERANDS)
902                   {
903                     as_bad ("spurious operands; (%d operands/instruction max)",
904                             MAX_OPERANDS);
905                     return;
906                   }
907                 /* now parse operand adding info to 'i' as we go along */
908                 END_STRING_AND_SAVE (l);
909                 operand_ok = i386_operand (token_start);
910                 RESTORE_END_STRING (l); /* restore old contents */
911                 if (!operand_ok)
912                   return;
913               }
914             else
915               {
916                 if (expecting_operand)
917                   {
918                   expecting_operand_after_comma:
919                     as_bad ("expecting operand after ','; got nothing");
920                     return;
921                   }
922                 if (*l == ',')
923                   {
924                     as_bad ("expecting operand before ','; got nothing");
925                     return;
926                   }
927               }
928
929             /* now *l must be either ',' or END_OF_INSN */
930             if (*l == ',')
931               {
932                 if (*++l == END_OF_INSN)
933                   {             /* just skip it, if it's \n complain */
934                     goto expecting_operand_after_comma;
935                   }
936                 expecting_operand = 1;
937               }
938           }
939         while (*l != END_OF_INSN);      /* until we get end of insn */
940       }
941   }
942
943   /* Now we've parsed the opcode into a set of templates, and have the
944      operands at hand.
945
946      Next, we find a template that matches the given insn,
947      making sure the overlap of the given operands types is consistent
948      with the template operand types. */
949
950 #define MATCH(overlap,given_type) \
951         (overlap && \
952          (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
953            == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
954           || (overlap == InOutPortReg)))
955
956
957   /* If m0 and m1 are register matches they must be consistent
958      with the expected operand types t0 and t1.
959      That is, if both m0 & m1 are register matches
960      i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
961      then, either 1. or 2. must be true:
962      1. the expected operand type register overlap is null:
963      (t0 & t1 & Reg) == 0
964      AND
965      the given register overlap is null:
966      (m0 & m1 & Reg) == 0
967      2. the expected operand type register overlap == the given
968      operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
969      */
970 #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
971             ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
972              ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
973               ((t0 & t1) & (m0 & m1) & (Reg)) \
974               ) : 1)
975   {
976     register unsigned int overlap0, overlap1;
977     expressionS *exp;
978     unsigned int overlap2;
979     unsigned int found_reverse_match;
980
981     overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
982     for (t = current_templates->start;
983          t < current_templates->end;
984          t++)
985       {
986         /* must have right number of operands */
987         if (i.operands != t->operands)
988           continue;
989         else if (!t->operands)
990           break;                /* 0 operands always matches */
991
992         overlap0 = i.types[0] & t->operand_types[0];
993         switch (t->operands)
994           {
995           case 1:
996             if (!MATCH (overlap0, i.types[0]))
997               continue;
998             break;
999           case 2:
1000           case 3:
1001             overlap1 = i.types[1] & t->operand_types[1];
1002             if (!MATCH (overlap0, i.types[0]) ||
1003                 !MATCH (overlap1, i.types[1]) ||
1004                 !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
1005                                             t->operand_types[0],
1006                                             t->operand_types[1]))
1007               {
1008
1009                 /* check if other direction is valid ... */
1010                 if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
1011                   continue;
1012
1013                 /* try reversing direction of operands */
1014                 overlap0 = i.types[0] & t->operand_types[1];
1015                 overlap1 = i.types[1] & t->operand_types[0];
1016                 if (!MATCH (overlap0, i.types[0]) ||
1017                     !MATCH (overlap1, i.types[1]) ||
1018                     !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
1019                                                 t->operand_types[0],
1020                                                 t->operand_types[1]))
1021                   {
1022                     /* does not match either direction */
1023                     continue;
1024                   }
1025                 /* found a reverse match here -- slip through */
1026                 /* found_reverse_match holds which of D or FloatD we've found */
1027                 found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
1028               }                 /* endif: not forward match */
1029             /* found either forward/reverse 2 operand match here */
1030             if (t->operands == 3)
1031               {
1032                 overlap2 = i.types[2] & t->operand_types[2];
1033                 if (!MATCH (overlap2, i.types[2]) ||
1034                     !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
1035                                                 t->operand_types[0],
1036                                                 t->operand_types[2]) ||
1037                     !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
1038                                                 t->operand_types[1],
1039                                                 t->operand_types[2]))
1040                   continue;
1041               }
1042             /* found either forward/reverse 2 or 3 operand match here:
1043                slip through to break */
1044           }
1045         break;                  /* we've found a match; break out of loop */
1046       }                         /* for (t = ... */
1047     if (t == current_templates->end)
1048       {                         /* we found no match */
1049         as_bad ("operands given don't match any known 386 instruction");
1050         return;
1051       }
1052
1053     /* Copy the template we found (we may change it!). */
1054     i.tm = *t;
1055     t = &i.tm;                  /* alter new copy of template */
1056
1057     /* If the matched instruction specifies an explicit opcode suffix,
1058        use it - and make sure none has already been specified.  */
1059     if (t->opcode_modifier & (Data16|Data32))
1060       {
1061         if (i.suffix)
1062           {
1063             as_bad ("extraneous opcode suffix given");
1064             return;
1065           }
1066         if (t->opcode_modifier & Data16)
1067           i.suffix = WORD_OPCODE_SUFFIX;
1068         else
1069           i.suffix = DWORD_OPCODE_SUFFIX;
1070       }
1071
1072     /* If there's no opcode suffix we try to invent one based on register
1073        operands. */
1074     if (!i.suffix && i.reg_operands)
1075       {
1076         /* We take i.suffix from the LAST register operand specified.  This
1077            assumes that the last register operands is the destination register
1078            operand. */
1079         int op;
1080         for (op = 0; op < MAX_OPERANDS; op++)
1081           if (i.types[op] & Reg)
1082             {
1083               i.suffix = ((i.types[op] == Reg8) ? BYTE_OPCODE_SUFFIX :
1084                           (i.types[op] == Reg16) ? WORD_OPCODE_SUFFIX :
1085                           DWORD_OPCODE_SUFFIX);
1086             }
1087       }
1088
1089     /* Make still unresolved immediate matches conform to size of immediate
1090        given in i.suffix. Note:  overlap2 cannot be an immediate!
1091        We assume this. */
1092     if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
1093         && overlap0 != Imm8 && overlap0 != Imm8S
1094         && overlap0 != Imm16 && overlap0 != Imm32)
1095       {
1096         if (!i.suffix)
1097           {
1098             as_bad ("no opcode suffix given; can't determine immediate size");
1099             return;
1100           }
1101         overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1102                      (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1103       }
1104     if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
1105         && overlap1 != Imm8 && overlap1 != Imm8S
1106         && overlap1 != Imm16 && overlap1 != Imm32)
1107       {
1108         if (!i.suffix)
1109           {
1110             as_bad ("no opcode suffix given; can't determine immediate size");
1111             return;
1112           }
1113         overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1114                      (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1115       }
1116
1117     i.types[0] = overlap0;
1118     i.types[1] = overlap1;
1119     i.types[2] = overlap2;
1120
1121     if (overlap0 & ImplicitRegister)
1122       i.reg_operands--;
1123     if (overlap1 & ImplicitRegister)
1124       i.reg_operands--;
1125     if (overlap2 & ImplicitRegister)
1126       i.reg_operands--;
1127     if (overlap0 & Imm1)
1128       i.imm_operands = 0;       /* kludge for shift insns */
1129
1130     if (found_reverse_match)
1131       {
1132         unsigned int save;
1133         save = t->operand_types[0];
1134         t->operand_types[0] = t->operand_types[1];
1135         t->operand_types[1] = save;
1136       }
1137
1138     /* Finalize opcode.  First, we change the opcode based on the operand
1139        size given by i.suffix: we never have to change things for byte insns,
1140        or when no opcode suffix is need to size the operands. */
1141
1142     if (!i.suffix && (t->opcode_modifier & W))
1143       {
1144         as_bad ("no opcode suffix given and no register operands; can't size instruction");
1145         return;
1146       }
1147
1148     if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
1149       {
1150         /* Select between byte and word/dword operations. */
1151         if (t->opcode_modifier & W)
1152           t->base_opcode |= W;
1153         /* Now select between word & dword operations via the
1154                                    operand size prefix. */
1155         if ((i.suffix == WORD_OPCODE_SUFFIX) ^ flag_16bit_code)
1156           {
1157             if (i.prefixes == MAX_PREFIXES)
1158               {
1159                 as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
1160                         MAX_PREFIXES);
1161                 return;
1162               }
1163             i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
1164           }
1165       }
1166
1167     /* For insns with operands there are more diddles to do to the opcode. */
1168     if (i.operands)
1169       {
1170         /* Default segment register this instruction will use
1171            for memory accesses.  0 means unknown.
1172            This is only for optimizing out unnecessary segment overrides.  */
1173         const seg_entry *default_seg = 0;
1174
1175         /* True if this instruction uses a memory addressing mode,
1176            and therefore may need an address-size prefix.  */
1177         int uses_mem_addrmode = 0;
1178
1179
1180         /* If we found a reverse match we must alter the opcode direction bit
1181            found_reverse_match holds bit to set (different for int &
1182            float insns). */
1183
1184         if (found_reverse_match)
1185           {
1186             t->base_opcode |= found_reverse_match;
1187           }
1188
1189         /* The imul $imm, %reg instruction is converted into
1190            imul $imm, %reg, %reg. */
1191         if (t->opcode_modifier & imulKludge)
1192           {
1193             /* Pretend we saw the 3 operand case. */
1194             i.regs[2] = i.regs[1];
1195             i.reg_operands = 2;
1196           }
1197
1198         /* Certain instructions expect the destination to be in the i.rm.reg
1199            field.  This is by far the exceptional case.  For these
1200            instructions, if the source operand is a register, we must reverse
1201            the i.rm.reg and i.rm.regmem fields.  We accomplish this by faking
1202            that the two register operands were given in the reverse order. */
1203         if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
1204           {
1205             unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
1206             unsigned int second_reg_operand = first_reg_operand + 1;
1207             reg_entry *tmp = i.regs[first_reg_operand];
1208             i.regs[first_reg_operand] = i.regs[second_reg_operand];
1209             i.regs[second_reg_operand] = tmp;
1210           }
1211
1212         if (t->opcode_modifier & ShortForm)
1213           {
1214             /* The register or float register operand is in operand 0 or 1. */
1215             unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
1216             /* Register goes in low 3 bits of opcode. */
1217             t->base_opcode |= i.regs[op]->reg_num;
1218           }
1219         else if (t->opcode_modifier & ShortFormW)
1220           {
1221             /* Short form with 0x8 width bit.  Register is always dest. operand */
1222             t->base_opcode |= i.regs[1]->reg_num;
1223             if (i.suffix == WORD_OPCODE_SUFFIX ||
1224                 i.suffix == DWORD_OPCODE_SUFFIX)
1225               t->base_opcode |= 0x8;
1226           }
1227         else if (t->opcode_modifier & Seg2ShortForm)
1228           {
1229             if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
1230               {
1231                 as_bad ("you can't 'pop cs' on the 386.");
1232                 return;
1233               }
1234             t->base_opcode |= (i.regs[0]->reg_num << 3);
1235           }
1236         else if (t->opcode_modifier & Seg3ShortForm)
1237           {
1238             /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
1239                'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
1240                So, only if i.regs[0]->reg_num == 5 (%gs) do we need
1241                to change the opcode. */
1242             if (i.regs[0]->reg_num == 5)
1243               t->base_opcode |= 0x08;
1244           }
1245         else if ((t->base_opcode & ~DW) == MOV_AX_DISP32)
1246           {
1247             /* This is a special non-modrm instruction
1248                that addresses memory with a 32-bit displacement mode anyway,
1249                and thus requires an address-size prefix if in 16-bit mode.  */
1250             uses_mem_addrmode = 1;
1251             default_seg = &ds;
1252           }
1253         else if (t->opcode_modifier & Modrm)
1254           {
1255             /* The opcode is completed (modulo t->extension_opcode which must
1256                be put into the modrm byte.
1257                Now, we make the modrm & index base bytes based on all the info
1258                we've collected. */
1259
1260             /* i.reg_operands MUST be the number of real register operands;
1261                implicit registers do not count. */
1262             if (i.reg_operands == 2)
1263               {
1264                 unsigned int source, dest;
1265                 source = (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 : 1;
1266                 dest = source + 1;
1267                 i.rm.mode = 3;
1268                 /* We must be careful to make sure that all
1269                    segment/control/test/debug registers go into the i.rm.reg
1270                    field (despite the whether they are source or destination
1271                    operands). */
1272                 if (i.regs[dest]->reg_type & (SReg2 | SReg3 | Control | Debug | Test))
1273                   {
1274                     i.rm.reg = i.regs[dest]->reg_num;
1275                     i.rm.regmem = i.regs[source]->reg_num;
1276                   }
1277                 else
1278                   {
1279                     i.rm.reg = i.regs[source]->reg_num;
1280                     i.rm.regmem = i.regs[dest]->reg_num;
1281                   }
1282               }
1283             else
1284               {                 /* if it's not 2 reg operands... */
1285                 if (i.mem_operands)
1286                   {
1287                     unsigned int fake_zero_displacement = 0;
1288                     unsigned int op = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
1289
1290                     /* Encode memory operand into modrm byte and base index
1291                        byte. */
1292
1293                     if (i.base_reg == esp && !i.index_reg)
1294                       {
1295                         /* <disp>(%esp) becomes two byte modrm with no index
1296                            register. */
1297                         i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1298                         i.rm.mode = mode_from_disp_size (i.types[op]);
1299                         i.bi.base = ESP_REG_NUM;
1300                         i.bi.index = NO_INDEX_REGISTER;
1301                         i.bi.scale = 0; /* Must be zero! */
1302                       }
1303                     else if (i.base_reg == ebp && !i.index_reg)
1304                       {
1305                         if (!(i.types[op] & Disp))
1306                           {
1307                             /* Must fake a zero byte displacement.  There is
1308                                no direct way to code '(%ebp)' directly. */
1309                             fake_zero_displacement = 1;
1310                             /* fake_zero_displacement code does not set this. */
1311                             i.types[op] |= Disp8;
1312                           }
1313                         i.rm.mode = mode_from_disp_size (i.types[op]);
1314                         i.rm.regmem = EBP_REG_NUM;
1315                       }
1316                     else if (!i.base_reg && (i.types[op] & BaseIndex))
1317                       {
1318                         /* There are three cases here.
1319                            Case 1:  '<32bit disp>(,1)' -- indirect absolute.
1320                            (Same as cases 2 & 3 with NO index register)
1321                            Case 2:  <32bit disp> (,<index>) -- no base register with disp
1322                            Case 3:  (, <index>)       --- no base register;
1323                            no disp (must add 32bit 0 disp). */
1324                         i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1325                         i.rm.mode = 0;  /* 32bit mode */
1326                         i.bi.base = NO_BASE_REGISTER;
1327                         i.types[op] &= ~Disp;
1328                         i.types[op] |= Disp32;  /* Must be 32bit! */
1329                         if (i.index_reg)
1330                           {     /* case 2 or case 3 */
1331                             i.bi.index = i.index_reg->reg_num;
1332                             i.bi.scale = i.log2_scale_factor;
1333                             if (i.disp_operands == 0)
1334                               fake_zero_displacement = 1;       /* case 3 */
1335                           }
1336                         else
1337                           {
1338                             i.bi.index = NO_INDEX_REGISTER;
1339                             i.bi.scale = 0;
1340                           }
1341                       }
1342                     else if (i.disp_operands && !i.base_reg && !i.index_reg)
1343                       {
1344                         /* Operand is just <32bit disp> */
1345                         i.rm.regmem = EBP_REG_NUM;
1346                         i.rm.mode = 0;
1347                         i.types[op] &= ~Disp;
1348                         i.types[op] |= Disp32;
1349                       }
1350                     else
1351                       {
1352                         /* It's not a special case; rev'em up. */
1353                         i.rm.regmem = i.base_reg->reg_num;
1354                         i.rm.mode = mode_from_disp_size (i.types[op]);
1355                         if (i.index_reg)
1356                           {
1357                             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1358                             i.bi.base = i.base_reg->reg_num;
1359                             i.bi.index = i.index_reg->reg_num;
1360                             i.bi.scale = i.log2_scale_factor;
1361                             if (i.base_reg == ebp && i.disp_operands == 0)
1362                               { /* pace */
1363                                 fake_zero_displacement = 1;
1364                                 i.types[op] |= Disp8;
1365                                 i.rm.mode = mode_from_disp_size (i.types[op]);
1366                               }
1367                           }
1368                       }
1369                     if (fake_zero_displacement)
1370                       {
1371                         /* Fakes a zero displacement assuming that i.types[op]
1372                            holds the correct displacement size. */
1373                         exp = &disp_expressions[i.disp_operands++];
1374                         i.disps[op] = exp;
1375                         exp->X_op = O_constant;
1376                         exp->X_add_number = 0;
1377                         exp->X_add_symbol = (symbolS *) 0;
1378                         exp->X_op_symbol = (symbolS *) 0;
1379                       }
1380
1381                     /* Find the default segment for the memory operand.
1382                        Used to optimize out explicit segment specifications.  */
1383                     if (i.seg)
1384                       {
1385                         unsigned int seg_index;
1386
1387                         if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
1388                           {
1389                             seg_index = (i.rm.mode << 3) | i.bi.base;
1390                             default_seg = two_byte_segment_defaults[seg_index];
1391                           }
1392                         else
1393                           {
1394                             seg_index = (i.rm.mode << 3) | i.rm.regmem;
1395                             default_seg = one_byte_segment_defaults[seg_index];
1396                           }
1397                       }
1398                   }
1399
1400                 /* Fill in i.rm.reg or i.rm.regmem field with register operand
1401                    (if any) based on t->extension_opcode. Again, we must be
1402                    careful to make sure that segment/control/debug/test
1403                    registers are coded into the i.rm.reg field. */
1404                 if (i.reg_operands)
1405                   {
1406                     unsigned int op =
1407                     (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 :
1408                     (i.types[1] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 1 : 2;
1409                     /* If there is an extension opcode to put here, the
1410                        register number must be put into the regmem field. */
1411                     if (t->extension_opcode != None)
1412                       i.rm.regmem = i.regs[op]->reg_num;
1413                     else
1414                       i.rm.reg = i.regs[op]->reg_num;
1415
1416                     /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
1417                        we must set it to 3 to indicate this is a register
1418                        operand int the regmem field */
1419                     if (!i.mem_operands)
1420                       i.rm.mode = 3;
1421                   }
1422
1423                 /* Fill in i.rm.reg field with extension opcode (if any). */
1424                 if (t->extension_opcode != None)
1425                   i.rm.reg = t->extension_opcode;
1426               }
1427
1428             if (i.rm.mode != 3)
1429               uses_mem_addrmode = 1;
1430           }
1431
1432         /* GAS currently doesn't support 16-bit memory addressing modes at all,
1433            so if we're writing 16-bit code and using a memory addressing mode,
1434            always spew out an address size prefix.  */
1435         if (uses_mem_addrmode && flag_16bit_code)
1436           {
1437             if (i.prefixes == MAX_PREFIXES)
1438               {
1439                 as_bad ("%d prefixes given and address size override gives too many prefixes",
1440                         MAX_PREFIXES);
1441                 return;
1442               }
1443             i.prefix[i.prefixes++] = ADDR_PREFIX_OPCODE;
1444           }
1445
1446         /* If a segment was explicitly specified,
1447            and the specified segment is not the default,
1448            use an opcode prefix to select it.
1449            If we never figured out what the default segment is,
1450            then default_seg will be zero at this point,
1451            and the specified segment prefix will always be used.  */
1452         if ((i.seg) && (i.seg != default_seg))
1453           {
1454             if (i.prefixes == MAX_PREFIXES)
1455               {
1456                 as_bad ("%d prefixes given and %s segment override gives too many prefixes",
1457                     MAX_PREFIXES, i.seg->seg_name);
1458                 return;
1459               }
1460             i.prefix[i.prefixes++] = i.seg->seg_prefix;
1461           }
1462       }
1463   }
1464
1465   /* Handle conversion of 'int $3' --> special int3 insn. */
1466   if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
1467     {
1468       t->base_opcode = INT3_OPCODE;
1469       i.imm_operands = 0;
1470     }
1471
1472   /* We are ready to output the insn. */
1473   {
1474     register char *p;
1475
1476     /* Output jumps. */
1477     if (t->opcode_modifier & Jump)
1478       {
1479         unsigned long n = i.disps[0]->X_add_number;
1480
1481         if (i.disps[0]->X_op == O_constant)
1482           {
1483             if (fits_in_signed_byte (n))
1484               {
1485                 p = frag_more (2);
1486                 insn_size += 2;
1487                 p[0] = t->base_opcode;
1488                 p[1] = n;
1489               }
1490             else
1491               { /* It's an absolute word/dword displacement. */
1492
1493                 /* Use only 16-bit jumps for 16-bit code,
1494                    because text segments are limited to 64K anyway;
1495                    use only 32-bit jumps for 32-bit code,
1496                    because they're faster.  */
1497                 int jmp_size = flag_16bit_code ? 2 : 4;
1498                 if (flag_16bit_code && !fits_in_signed_word (n))
1499                   {
1500                     as_bad ("16-bit jump out of range");
1501                     return;
1502                   }
1503
1504                 if (t->base_opcode == JUMP_PC_RELATIVE)
1505                   {             /* pace */
1506                     /* unconditional jump */
1507                     p = frag_more (1 + jmp_size);
1508                     insn_size += 1 + jmp_size;
1509                     p[0] = (char) 0xe9;
1510                     md_number_to_chars (&p[1], (valueT) n, jmp_size);
1511                   }
1512                 else
1513                   {
1514                     /* conditional jump */
1515                     p = frag_more (2 + jmp_size);
1516                     insn_size += 2 + jmp_size;
1517                     p[0] = TWO_BYTE_OPCODE_ESCAPE;
1518                     p[1] = t->base_opcode + 0x10;
1519                     md_number_to_chars (&p[2], (valueT) n, jmp_size);
1520                   }
1521               }
1522           }
1523         else
1524           {
1525             if (flag_16bit_code)
1526               {
1527                 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1528                 insn_size += 1;
1529               }
1530
1531             /* It's a symbol; end frag & setup for relax.
1532                Make sure there are more than 6 chars left in the current frag;
1533                if not we'll have to start a new one. */
1534             if (obstack_room (&frags) <= 6)
1535               {
1536                 frag_wane (frag_now);
1537                 frag_new (0);
1538               }
1539             p = frag_more (1);
1540             insn_size += 1;
1541             p[0] = t->base_opcode;
1542             frag_var (rs_machine_dependent,
1543                       6,        /* 2 opcode/prefix + 4 displacement */
1544                       1,
1545                       ((unsigned char) *p == JUMP_PC_RELATIVE
1546                        ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
1547                        : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
1548                       i.disps[0]->X_add_symbol,
1549                       (long) n, p);
1550           }
1551       }
1552     else if (t->opcode_modifier & (JumpByte | JumpDword))
1553       {
1554         int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
1555         unsigned long n = i.disps[0]->X_add_number;
1556         unsigned char *q;
1557
1558         /* The jcx/jecx instruction might need a data size prefix.  */
1559         for (q = i.prefix; q < i.prefix + i.prefixes; q++)
1560           {
1561             if (*q == WORD_PREFIX_OPCODE)
1562               {
1563                 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1564                 insn_size += 1;
1565                 break;
1566               }
1567           }
1568
1569         if ((size == 4) && (flag_16bit_code))
1570           {
1571             FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1572             insn_size += 1;
1573           }
1574
1575         if (fits_in_unsigned_byte (t->base_opcode))
1576           {
1577             FRAG_APPEND_1_CHAR (t->base_opcode);
1578             insn_size += 1;
1579           }
1580         else
1581           {
1582             p = frag_more (2);  /* opcode can be at most two bytes */
1583             insn_size += 2;
1584             /* put out high byte first: can't use md_number_to_chars! */
1585             *p++ = (t->base_opcode >> 8) & 0xff;
1586             *p = t->base_opcode & 0xff;
1587           }
1588
1589         p = frag_more (size);
1590         insn_size += size;
1591         if (i.disps[0]->X_op == O_constant)
1592           {
1593             md_number_to_chars (p, (valueT) n, size);
1594             if (size == 1 && !fits_in_signed_byte (n))
1595               {
1596                 as_bad ("loop/jecx only takes byte displacement; %lu shortened to %d",
1597                         n, *p);
1598               }
1599           }
1600         else
1601           {
1602             fix_new_exp (frag_now, p - frag_now->fr_literal, size,
1603                          i.disps[0], 1, reloc (size, 1, i.disp_reloc[0]));
1604
1605           }
1606       }
1607     else if (t->opcode_modifier & JumpInterSegment)
1608       {
1609         if (flag_16bit_code)
1610           {
1611             FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1612             insn_size += 1;
1613           }
1614
1615         p = frag_more (1 + 2 + 4);      /* 1 opcode; 2 segment; 4 offset */
1616         insn_size += 1 + 2 + 4;
1617         p[0] = t->base_opcode;
1618         if (i.imms[1]->X_op == O_constant)
1619           md_number_to_chars (p + 1, (valueT) i.imms[1]->X_add_number, 4);
1620         else
1621           fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
1622                        i.imms[1], 0, BFD_RELOC_32);
1623         if (i.imms[0]->X_op != O_constant)
1624           as_bad ("can't handle non absolute segment in long call/jmp");
1625         md_number_to_chars (p + 5, (valueT) i.imms[0]->X_add_number, 2);
1626       }
1627     else
1628       {
1629         /* Output normal instructions here. */
1630         unsigned char *q;
1631
1632         /* First the prefix bytes. */
1633         for (q = i.prefix; q < i.prefix + i.prefixes; q++)
1634           {
1635             p = frag_more (1);
1636             insn_size += 1;
1637             md_number_to_chars (p, (valueT) *q, 1);
1638           }
1639
1640         /* Now the opcode; be careful about word order here! */
1641         if (fits_in_unsigned_byte (t->base_opcode))
1642           {
1643             FRAG_APPEND_1_CHAR (t->base_opcode);
1644             insn_size += 1;
1645           }
1646         else if (fits_in_unsigned_word (t->base_opcode))
1647           {
1648             p = frag_more (2);
1649             insn_size += 2;
1650             /* put out high byte first: can't use md_number_to_chars! */
1651             *p++ = (t->base_opcode >> 8) & 0xff;
1652             *p = t->base_opcode & 0xff;
1653           }
1654         else
1655           {                     /* opcode is either 3 or 4 bytes */
1656             if (t->base_opcode & 0xff000000)
1657               {
1658                 p = frag_more (4);
1659                 insn_size += 4;
1660                 *p++ = (t->base_opcode >> 24) & 0xff;
1661               }
1662             else
1663               {
1664                 p = frag_more (3);
1665                 insn_size += 3;
1666               }
1667             *p++ = (t->base_opcode >> 16) & 0xff;
1668             *p++ = (t->base_opcode >> 8) & 0xff;
1669             *p = (t->base_opcode) & 0xff;
1670           }
1671
1672         /* Now the modrm byte and base index byte (if present). */
1673         if (t->opcode_modifier & Modrm)
1674           {
1675             p = frag_more (1);
1676             insn_size += 1;
1677             /* md_number_to_chars (p, i.rm, 1); */
1678             md_number_to_chars (p,
1679                                 (valueT) (i.rm.regmem << 0
1680                                           | i.rm.reg << 3
1681                                           | i.rm.mode << 6),
1682                                 1);
1683             /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
1684                                    ==> need second modrm byte. */
1685             if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
1686               {
1687                 p = frag_more (1);
1688                 insn_size += 1;
1689                 /* md_number_to_chars (p, i.bi, 1); */
1690                 md_number_to_chars (p, (valueT) (i.bi.base << 0
1691                                                  | i.bi.index << 3
1692                                                  | i.bi.scale << 6),
1693                                     1);
1694               }
1695           }
1696
1697         if (i.disp_operands)
1698           {
1699             register unsigned int n;
1700
1701             for (n = 0; n < i.operands; n++)
1702               {
1703                 if (i.disps[n])
1704                   {
1705                     if (i.disps[n]->X_op == O_constant)
1706                       {
1707                         if (i.types[n] & (Disp8 | Abs8))
1708                           {
1709                             p = frag_more (1);
1710                             insn_size += 1;
1711                             md_number_to_chars (p,
1712                                                 (valueT) i.disps[n]->X_add_number,
1713                                                 1);
1714                           }
1715                         else if (i.types[n] & (Disp16 | Abs16))
1716                           {
1717                             p = frag_more (2);
1718                             insn_size += 2;
1719                             md_number_to_chars (p,
1720                                                 (valueT) i.disps[n]->X_add_number,
1721                                                 2);
1722                           }
1723                         else
1724                           {     /* Disp32|Abs32 */
1725                             p = frag_more (4);
1726                             insn_size += 4;
1727                             md_number_to_chars (p,
1728                                                 (valueT) i.disps[n]->X_add_number,
1729                                                 4);
1730                           }
1731                       }
1732                     else
1733                       {         /* not absolute_section */
1734                         /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
1735                         p = frag_more (4);
1736                         insn_size += 4;
1737                         fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
1738                                             i.disps[n], 0, 
1739                                             TC_RELOC(i.disp_reloc[n], BFD_RELOC_32));
1740                       }
1741                   }
1742               }
1743           }                     /* end displacement output */
1744
1745         /* output immediate */
1746         if (i.imm_operands)
1747           {
1748             register unsigned int n;
1749
1750             for (n = 0; n < i.operands; n++)
1751               {
1752                 if (i.imms[n])
1753                   {
1754                     if (i.imms[n]->X_op == O_constant)
1755                       {
1756                         if (i.types[n] & (Imm8 | Imm8S))
1757                           {
1758                             p = frag_more (1);
1759                             insn_size += 1;
1760                             md_number_to_chars (p,
1761                                                 (valueT) i.imms[n]->X_add_number,
1762                                                 1);
1763                           }
1764                         else if (i.types[n] & Imm16)
1765                           {
1766                             p = frag_more (2);
1767                             insn_size += 2;
1768                             md_number_to_chars (p,
1769                                                 (valueT) i.imms[n]->X_add_number,
1770                                                 2);
1771                           }
1772                         else
1773                           {
1774                             p = frag_more (4);
1775                             insn_size += 4;
1776                             md_number_to_chars (p,
1777                                                 (valueT) i.imms[n]->X_add_number,
1778                                                 4);
1779                           }
1780                       }
1781                     else
1782                       {         /* not absolute_section */
1783                         /* Need a 32-bit fixup (don't support 8bit
1784                            non-absolute ims).  Try to support other
1785                            sizes ... */
1786                         int r_type;
1787                         int size;
1788                         int pcrel = 0;
1789
1790                         if (i.types[n] & (Imm8 | Imm8S))
1791                           size = 1;
1792                         else if (i.types[n] & Imm16)
1793                           size = 2;
1794                         else
1795                           size = 4;
1796                         r_type = reloc (size, 0, i.disp_reloc[0]);
1797                         p = frag_more (size);
1798                         insn_size += size;
1799 #ifdef BFD_ASSEMBLER
1800                         if (r_type == BFD_RELOC_32
1801                             && GOT_symbol
1802                             && GOT_symbol == i.imms[n]->X_add_symbol
1803                             && (i.imms[n]->X_op == O_symbol
1804                                 || (i.imms[n]->X_op == O_add
1805                                     && (i.imms[n]->X_op_symbol->sy_value.X_op
1806                                         == O_subtract))))
1807                           {
1808                             r_type = BFD_RELOC_386_GOTPC;
1809                             i.imms[n]->X_add_number += 3;
1810                           }
1811 #endif
1812                         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
1813                                      i.imms[n], pcrel, r_type);
1814                       }
1815                   }
1816               }
1817           }                     /* end immediate output */
1818       }
1819
1820 #ifdef DEBUG386
1821     if (flag_debug)
1822       {
1823         pi (line, &i);
1824       }
1825 #endif /* DEBUG386 */
1826   }
1827 }
1828 \f
1829 /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
1830    on error. */
1831
1832 static int
1833 i386_operand (operand_string)
1834      char *operand_string;
1835 {
1836   register char *op_string = operand_string;
1837
1838   /* Address of '\0' at end of operand_string. */
1839   char *end_of_operand_string = operand_string + strlen (operand_string);
1840
1841   /* Start and end of displacement string expression (if found). */
1842   char *displacement_string_start = NULL;
1843   char *displacement_string_end = NULL;
1844
1845   /* We check for an absolute prefix (differentiating,
1846      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
1847   if (*op_string == ABSOLUTE_PREFIX)
1848     {
1849       op_string++;
1850       i.types[this_operand] |= JumpAbsolute;
1851     }
1852
1853   /* Check if operand is a register. */
1854   if (*op_string == REGISTER_PREFIX)
1855     {
1856       register reg_entry *r;
1857       if (!(r = parse_register (op_string)))
1858         {
1859           as_bad ("bad register name ('%s')", op_string);
1860           return 0;
1861         }
1862       /* Check for segment override, rather than segment register by
1863          searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
1864       if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
1865         {
1866           switch (r->reg_num)
1867             {
1868             case 0:
1869               i.seg = (seg_entry *) & es;
1870               break;
1871             case 1:
1872               i.seg = (seg_entry *) & cs;
1873               break;
1874             case 2:
1875               i.seg = (seg_entry *) & ss;
1876               break;
1877             case 3:
1878               i.seg = (seg_entry *) & ds;
1879               break;
1880             case 4:
1881               i.seg = (seg_entry *) & fs;
1882               break;
1883             case 5:
1884               i.seg = (seg_entry *) & gs;
1885               break;
1886             }
1887           op_string += 4;       /* skip % <x> s : */
1888           operand_string = op_string;   /* Pretend given string starts here. */
1889           if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
1890               && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
1891             {
1892               as_bad ("bad memory operand after segment override");
1893               return 0;
1894             }
1895           /* Handle case of %es:*foo. */
1896           if (*op_string == ABSOLUTE_PREFIX)
1897             {
1898               op_string++;
1899               i.types[this_operand] |= JumpAbsolute;
1900             }
1901           goto do_memory_reference;
1902         }
1903       i.types[this_operand] |= r->reg_type;
1904       i.regs[this_operand] = r;
1905       i.reg_operands++;
1906     }
1907   else if (*op_string == IMMEDIATE_PREFIX)
1908     {                           /* ... or an immediate */
1909       char *save_input_line_pointer;
1910       segT exp_seg = 0;
1911       expressionS *exp;
1912
1913       if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
1914         {
1915           as_bad ("only 1 or 2 immediate operands are allowed");
1916           return 0;
1917         }
1918
1919       exp = &im_expressions[i.imm_operands++];
1920       i.imms[this_operand] = exp;
1921       save_input_line_pointer = input_line_pointer;
1922       input_line_pointer = ++op_string; /* must advance op_string! */
1923       SKIP_WHITESPACE ();
1924       exp_seg = expression (exp);
1925       input_line_pointer = save_input_line_pointer;
1926
1927       if (exp->X_op == O_absent)
1928         {
1929           /* missing or bad expr becomes absolute 0 */
1930           as_bad ("missing or invalid immediate expression '%s' taken as 0",
1931                   operand_string);
1932           exp->X_op = O_constant;
1933           exp->X_add_number = 0;
1934           exp->X_add_symbol = (symbolS *) 0;
1935           exp->X_op_symbol = (symbolS *) 0;
1936           i.types[this_operand] |= Imm;
1937         }
1938       else if (exp->X_op == O_constant)
1939         {
1940           i.types[this_operand] |=
1941             smallest_imm_type ((unsigned long) exp->X_add_number);
1942         }
1943 #ifdef OBJ_AOUT
1944       else if (exp_seg != text_section
1945                && exp_seg != data_section
1946                && exp_seg != bss_section
1947                && exp_seg != undefined_section
1948 #ifdef BFD_ASSEMBLER
1949                && ! bfd_is_com_section (exp_seg)
1950 #endif
1951                )
1952         {
1953         seg_unimplemented:
1954           as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
1955           return 0;
1956         }
1957 #endif
1958       else
1959         {
1960           /* this is an address ==> 32bit */
1961           i.types[this_operand] |= Imm32;
1962         }
1963       /* shorten this type of this operand if the instruction wants
1964        * fewer bits than are present in the immediate.  The bit field
1965        * code can put out 'andb $0xffffff, %al', for example.   pace
1966        * also 'movw $foo,(%eax)'
1967        */
1968       switch (i.suffix)
1969         {
1970         case WORD_OPCODE_SUFFIX:
1971           i.types[this_operand] |= Imm16;
1972           break;
1973         case BYTE_OPCODE_SUFFIX:
1974           i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
1975           break;
1976         }
1977     }
1978   else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
1979            || *op_string == '(')
1980     {
1981       /* This is a memory reference of some sort. */
1982       register char *base_string;
1983       unsigned int found_base_index_form;
1984
1985     do_memory_reference:
1986       if (i.mem_operands == MAX_MEMORY_OPERANDS)
1987         {
1988           as_bad ("more than 1 memory reference in instruction");
1989           return 0;
1990         }
1991       i.mem_operands++;
1992
1993       /* Determine type of memory operand from opcode_suffix;
1994                    no opcode suffix implies general memory references. */
1995       switch (i.suffix)
1996         {
1997         case BYTE_OPCODE_SUFFIX:
1998           i.types[this_operand] |= Mem8;
1999           break;
2000         case WORD_OPCODE_SUFFIX:
2001           i.types[this_operand] |= Mem16;
2002           break;
2003         case DWORD_OPCODE_SUFFIX:
2004         default:
2005           i.types[this_operand] |= Mem32;
2006         }
2007
2008       /* Check for base index form.  We detect the base index form by
2009          looking for an ')' at the end of the operand, searching
2010          for the '(' matching it, and finding a REGISTER_PREFIX or ','
2011          after it. */
2012       base_string = end_of_operand_string - 1;
2013       found_base_index_form = 0;
2014       if (*base_string == ')')
2015         {
2016           unsigned int parens_balenced = 1;
2017           /* We've already checked that the number of left & right ()'s are
2018              equal, so this loop will not be infinite. */
2019           do
2020             {
2021               base_string--;
2022               if (*base_string == ')')
2023                 parens_balenced++;
2024               if (*base_string == '(')
2025                 parens_balenced--;
2026             }
2027           while (parens_balenced);
2028           base_string++;        /* Skip past '('. */
2029           if (*base_string == REGISTER_PREFIX || *base_string == ',')
2030             found_base_index_form = 1;
2031         }
2032
2033       /* If we can't parse a base index register expression, we've found
2034          a pure displacement expression.  We set up displacement_string_start
2035          and displacement_string_end for the code below. */
2036       if (!found_base_index_form)
2037         {
2038           displacement_string_start = op_string;
2039           displacement_string_end = end_of_operand_string;
2040         }
2041       else
2042         {
2043           char *base_reg_name, *index_reg_name, *num_string;
2044           int num;
2045
2046           i.types[this_operand] |= BaseIndex;
2047
2048           /* If there is a displacement set-up for it to be parsed later. */
2049           if (base_string != op_string + 1)
2050             {
2051               displacement_string_start = op_string;
2052               displacement_string_end = base_string - 1;
2053             }
2054
2055           /* Find base register (if any). */
2056           if (*base_string != ',')
2057             {
2058               base_reg_name = base_string++;
2059               /* skip past register name & parse it */
2060               while (isalpha (*base_string))
2061                 base_string++;
2062               if (base_string == base_reg_name + 1)
2063                 {
2064                   as_bad ("can't find base register name after '(%c'",
2065                           REGISTER_PREFIX);
2066                   return 0;
2067                 }
2068               END_STRING_AND_SAVE (base_string);
2069               if (!(i.base_reg = parse_register (base_reg_name)))
2070                 {
2071                   as_bad ("bad base register name ('%s')", base_reg_name);
2072                   return 0;
2073                 }
2074               RESTORE_END_STRING (base_string);
2075             }
2076
2077           /* Now check seperator; must be ',' ==> index reg
2078                            OR num ==> no index reg. just scale factor
2079                            OR ')' ==> end. (scale factor = 1) */
2080           if (*base_string != ',' && *base_string != ')')
2081             {
2082               as_bad ("expecting ',' or ')' after base register in `%s'",
2083                       operand_string);
2084               return 0;
2085             }
2086
2087           /* There may index reg here; and there may be a scale factor. */
2088           if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
2089             {
2090               index_reg_name = ++base_string;
2091               while (isalpha (*++base_string));
2092               END_STRING_AND_SAVE (base_string);
2093               if (!(i.index_reg = parse_register (index_reg_name)))
2094                 {
2095                   as_bad ("bad index register name ('%s')", index_reg_name);
2096                   return 0;
2097                 }
2098               RESTORE_END_STRING (base_string);
2099             }
2100
2101           /* Check for scale factor. */
2102           if (*base_string == ',' && isdigit (*(base_string + 1)))
2103             {
2104               num_string = ++base_string;
2105               while (is_digit_char (*base_string))
2106                 base_string++;
2107               if (base_string == num_string)
2108                 {
2109                   as_bad ("can't find a scale factor after ','");
2110                   return 0;
2111                 }
2112               END_STRING_AND_SAVE (base_string);
2113               /* We've got a scale factor. */
2114               if (!sscanf (num_string, "%d", &num))
2115                 {
2116                   as_bad ("can't parse scale factor from '%s'", num_string);
2117                   return 0;
2118                 }
2119               RESTORE_END_STRING (base_string);
2120               switch (num)
2121                 {               /* must be 1 digit scale */
2122                 case 1:
2123                   i.log2_scale_factor = 0;
2124                   break;
2125                 case 2:
2126                   i.log2_scale_factor = 1;
2127                   break;
2128                 case 4:
2129                   i.log2_scale_factor = 2;
2130                   break;
2131                 case 8:
2132                   i.log2_scale_factor = 3;
2133                   break;
2134                 default:
2135                   as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
2136                   return 0;
2137                 }
2138             }
2139           else
2140             {
2141               if (!i.index_reg && *base_string == ',')
2142                 {
2143                   as_bad ("expecting index register or scale factor after ','; got '%c'",
2144                           *(base_string + 1));
2145                   return 0;
2146                 }
2147             }
2148         }
2149
2150       /* If there's an expression begining the operand, parse it,
2151          assuming displacement_string_start and displacement_string_end
2152          are meaningful. */
2153       if (displacement_string_start)
2154         {
2155           register expressionS *exp;
2156           segT exp_seg = 0;
2157           char *save_input_line_pointer;
2158           exp = &disp_expressions[i.disp_operands];
2159           i.disps[this_operand] = exp;
2160           i.disp_reloc[this_operand] = NO_RELOC;
2161           i.disp_operands++;
2162           save_input_line_pointer = input_line_pointer;
2163           input_line_pointer = displacement_string_start;
2164           END_STRING_AND_SAVE (displacement_string_end);
2165 #ifndef LEX_AT
2166           {
2167             /*
2168              * We can have operands of the form
2169              *   <symbol>@GOTOFF+<nnn>
2170              * Take the easy way out here and copy everything
2171              * into a temporary buffer...
2172              */
2173             register char *cp;
2174             if ((cp = strchr (input_line_pointer,'@')) != NULL) {
2175               char tmpbuf[BUFSIZ];
2176               
2177               if(!GOT_symbol)
2178                 GOT_symbol = symbol_find_or_make(GLOBAL_OFFSET_TABLE_NAME);
2179
2180               if (strncmp(cp+1, "PLT", 3) == 0) {
2181                 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2182                 *cp = '\0';
2183                 strcpy(tmpbuf, input_line_pointer);
2184                 strcat(tmpbuf, cp+1+3);
2185                 *cp = '@';
2186               } else if (strncmp(cp+1, "GOTOFF", 6) == 0) {
2187                 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2188                 *cp = '\0';
2189                 strcpy(tmpbuf, input_line_pointer);
2190                 strcat(tmpbuf, cp+1+6);
2191                 *cp = '@';
2192               } else if (strncmp(cp+1, "GOT", 3) == 0) {
2193                 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2194                 *cp = '\0';
2195                 strcpy(tmpbuf, input_line_pointer);
2196                 strcat(tmpbuf, cp+1+3);
2197                 *cp = '@';
2198               } else
2199                 as_bad("Bad reloc specifier '%s' in expression", cp+1);
2200               input_line_pointer = tmpbuf;
2201             }
2202           }
2203 #endif
2204           exp_seg = expression (exp);
2205
2206 #ifdef BFD_ASSEMBLER
2207           /* We do this to make sure that the section symbol is in
2208              the symbol table.  We will ultimately change the relocation
2209              to be relative to the beginning of the section */
2210           if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
2211             {
2212               if (S_IS_LOCAL(exp->X_add_symbol)
2213                   && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
2214                 section_symbol(exp->X_add_symbol->bsym->section);
2215               assert (exp->X_op == O_symbol);
2216               exp->X_op = O_subtract;
2217               exp->X_op_symbol = GOT_symbol;
2218               i.disp_reloc[this_operand] = BFD_RELOC_32;
2219             }
2220 #endif
2221
2222           if (*input_line_pointer)
2223             as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
2224           RESTORE_END_STRING (displacement_string_end);
2225           input_line_pointer = save_input_line_pointer;
2226           if (exp->X_op == O_absent)
2227             {
2228               /* missing expr becomes absolute 0 */
2229               as_bad ("missing or invalid displacement '%s' taken as 0",
2230                       operand_string);
2231               i.types[this_operand] |= (Disp | Abs);
2232               exp->X_op = O_constant;
2233               exp->X_add_number = 0;
2234               exp->X_add_symbol = (symbolS *) 0;
2235               exp->X_op_symbol = (symbolS *) 0;
2236             }
2237           else if (exp->X_op == O_constant)
2238             {
2239               i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
2240             }
2241           else if (exp_seg == text_section
2242                    || exp_seg == data_section
2243                    || exp_seg == bss_section
2244                    || exp_seg == undefined_section)
2245             {
2246               i.types[this_operand] |= Disp32;
2247             }
2248           else
2249             {
2250 #ifndef OBJ_AOUT
2251               i.types[this_operand] |= Disp32;
2252 #else
2253               goto seg_unimplemented;
2254 #endif
2255             }
2256         }
2257
2258       /* Make sure the memory operand we've been dealt is valid. */
2259       if (i.base_reg && i.index_reg &&
2260           !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
2261         {
2262           as_bad ("register size mismatch in (base,index,scale) expression");
2263           return 0;
2264         }
2265       /*
2266        * special case for (%dx) while doing input/output op
2267        */
2268       if ((i.base_reg &&
2269            (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
2270            (i.index_reg == 0)))
2271         {
2272           i.types[this_operand] |= InOutPortReg;
2273           return 1;
2274         }
2275       if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
2276           (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
2277         {
2278           as_bad ("base/index register must be 32 bit register");
2279           return 0;
2280         }
2281       if (i.index_reg && i.index_reg == esp)
2282         {
2283           as_bad ("%s may not be used as an index register", esp->reg_name);
2284           return 0;
2285         }
2286     }
2287   else
2288     {                           /* it's not a memory operand; argh! */
2289       as_bad ("invalid char %s begining %s operand '%s'",
2290               output_invalid (*op_string), ordinal_names[this_operand],
2291               op_string);
2292       return 0;
2293     }
2294   return 1;                     /* normal return */
2295 }
2296 \f
2297 /*
2298  *                      md_estimate_size_before_relax()
2299  *
2300  * Called just before relax().
2301  * Any symbol that is now undefined will not become defined.
2302  * Return the correct fr_subtype in the frag.
2303  * Return the initial "guess for fr_var" to caller.
2304  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
2305  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
2306  * Although it may not be explicit in the frag, pretend fr_var starts with a
2307  * 0 value.
2308  */
2309 int
2310 md_estimate_size_before_relax (fragP, segment)
2311      register fragS *fragP;
2312      register segT segment;
2313 {
2314   register unsigned char *opcode;
2315   register int old_fr_fix;
2316
2317   old_fr_fix = fragP->fr_fix;
2318   opcode = (unsigned char *) fragP->fr_opcode;
2319   /* We've already got fragP->fr_subtype right;  all we have to do is check
2320            for un-relaxable symbols. */
2321   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
2322     {
2323       /* symbol is undefined in this segment */
2324       switch (opcode[0])
2325         {
2326         case JUMP_PC_RELATIVE:  /* make jmp (0xeb) a dword displacement jump */
2327           opcode[0] = 0xe9;     /* dword disp jmp */
2328           fragP->fr_fix += 4;
2329           fix_new (fragP, old_fr_fix, 4,
2330                                  fragP->fr_symbol,
2331                    fragP->fr_offset, 1,
2332                    (GOT_symbol && /* Not quite right - we should switch on
2333                                      presence of @PLT, but I cannot see how
2334                                      to get to that from here.  We should have
2335                                      done this in md_assemble to really
2336                                      get it right all of the time, but I
2337                                      think it does not matter that much, as
2338                                      this will be right most of the time. ERY*/
2339                     S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
2340                    BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
2341           break;
2342
2343         default:
2344           /* This changes the byte-displacement jump 0x7N -->
2345                            the dword-displacement jump 0x0f8N */
2346           opcode[1] = opcode[0] + 0x10;
2347           opcode[0] = TWO_BYTE_OPCODE_ESCAPE;   /* two-byte escape */
2348           fragP->fr_fix += 1 + 4;       /* we've added an opcode byte */
2349           fix_new (fragP, old_fr_fix + 1, 4,
2350                    fragP->fr_symbol,
2351                    fragP->fr_offset, 1, 
2352                    (GOT_symbol &&  /* Not quite right - we should switch on
2353                                      presence of @PLT, but I cannot see how
2354                                      to get to that from here.  ERY */
2355                     S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
2356                    BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
2357           break;
2358         }
2359       frag_wane (fragP);
2360     }
2361   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
2362 }                               /* md_estimate_size_before_relax() */
2363 \f
2364 /*
2365  *                      md_convert_frag();
2366  *
2367  * Called after relax() is finished.
2368  * In:  Address of frag.
2369  *      fr_type == rs_machine_dependent.
2370  *      fr_subtype is what the address relaxed to.
2371  *
2372  * Out: Any fixSs and constants are set up.
2373  *      Caller will turn frag into a ".space 0".
2374  */
2375 #ifndef BFD_ASSEMBLER
2376 void
2377 md_convert_frag (headers, fragP)
2378      object_headers *headers;
2379      register fragS *fragP;
2380 #else
2381 void
2382 md_convert_frag (abfd, sec, fragP)
2383      bfd *abfd;
2384      segT sec;
2385      register fragS *fragP;
2386 #endif
2387 {
2388   register unsigned char *opcode;
2389   unsigned char *where_to_put_displacement = NULL;
2390   unsigned int target_address;
2391   unsigned int opcode_address;
2392   unsigned int extension = 0;
2393   int displacement_from_opcode_start;
2394
2395   opcode = (unsigned char *) fragP->fr_opcode;
2396
2397   /* Address we want to reach in file space. */
2398   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
2399 #ifdef BFD_ASSEMBLER /* not needed otherwise? */
2400   target_address += fragP->fr_symbol->sy_frag->fr_address;
2401 #endif
2402
2403   /* Address opcode resides at in file space. */
2404   opcode_address = fragP->fr_address + fragP->fr_fix;
2405
2406   /* Displacement from opcode start to fill into instruction. */
2407   displacement_from_opcode_start = target_address - opcode_address;
2408
2409   switch (fragP->fr_subtype)
2410     {
2411     case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
2412     case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
2413       /* don't have to change opcode */
2414       extension = 1;            /* 1 opcode + 1 displacement */
2415       where_to_put_displacement = &opcode[1];
2416       break;
2417
2418     case ENCODE_RELAX_STATE (COND_JUMP, WORD):
2419       opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
2420       opcode[2] = opcode[0] + 0x10;
2421       opcode[0] = WORD_PREFIX_OPCODE;
2422       extension = 4;            /* 3 opcode + 2 displacement */
2423       where_to_put_displacement = &opcode[3];
2424       break;
2425
2426     case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
2427       opcode[1] = 0xe9;
2428       opcode[0] = WORD_PREFIX_OPCODE;
2429       extension = 3;            /* 2 opcode + 2 displacement */
2430       where_to_put_displacement = &opcode[2];
2431       break;
2432
2433     case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
2434       opcode[1] = opcode[0] + 0x10;
2435       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
2436       extension = 5;            /* 2 opcode + 4 displacement */
2437       where_to_put_displacement = &opcode[2];
2438       break;
2439
2440     case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
2441       opcode[0] = 0xe9;
2442       extension = 4;            /* 1 opcode + 4 displacement */
2443       where_to_put_displacement = &opcode[1];
2444       break;
2445
2446     default:
2447       BAD_CASE (fragP->fr_subtype);
2448       break;
2449     }
2450   /* now put displacement after opcode */
2451   md_number_to_chars ((char *) where_to_put_displacement,
2452                       (valueT) (displacement_from_opcode_start - extension),
2453                       SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
2454   fragP->fr_fix += extension;
2455 }
2456 \f
2457
2458 int md_short_jump_size = 2;     /* size of byte displacement jmp */
2459 int md_long_jump_size = 5;      /* size of dword displacement jmp */
2460 const int md_reloc_size = 8;    /* Size of relocation record */
2461
2462 void
2463 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
2464      char *ptr;
2465      addressT from_addr, to_addr;
2466      fragS *frag;
2467      symbolS *to_symbol;
2468 {
2469   long offset;
2470
2471   offset = to_addr - (from_addr + 2);
2472   md_number_to_chars (ptr, (valueT) 0xeb, 1);   /* opcode for byte-disp jump */
2473   md_number_to_chars (ptr + 1, (valueT) offset, 1);
2474 }
2475
2476 void
2477 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
2478      char *ptr;
2479      addressT from_addr, to_addr;
2480      fragS *frag;
2481      symbolS *to_symbol;
2482 {
2483   long offset;
2484
2485   if (flag_do_long_jump)
2486     {
2487       offset = to_addr - S_GET_VALUE (to_symbol);
2488       md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
2489       md_number_to_chars (ptr + 1, (valueT) offset, 4);
2490       fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
2491                to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
2492     }
2493   else
2494     {
2495       offset = to_addr - (from_addr + 5);
2496       md_number_to_chars (ptr, (valueT) 0xe9, 1);
2497       md_number_to_chars (ptr + 1, (valueT) offset, 4);
2498     }
2499 }
2500 \f
2501 void                            /* Knows about order of bytes in address. */
2502 md_number_to_chars (con, value, nbytes)
2503      char con[];                /* Return 'nbytes' of chars here. */
2504      valueT value;              /* The value of the bits. */
2505      int nbytes;                /* Number of bytes in the output. */
2506 {
2507   number_to_chars_littleendian (con, value, nbytes);
2508 }
2509
2510
2511 /* Apply a fixup (fixS) to segment data, once it has been determined
2512    by our caller that we have all the info we need to fix it up.
2513
2514    On the 386, immediates, displacements, and data pointers are all in
2515    the same (little-endian) format, so we don't need to care about which
2516    we are handling.  */
2517
2518 static void
2519 md_apply_fix_1 (fixP, value)
2520      fixS *fixP;                /* The fix we're to put in */
2521      long value;                /* The value of the bits. */
2522 {
2523   register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
2524
2525 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
2526   /*
2527    * This is a hack.  There should be a better way to
2528    * handle this.
2529    */
2530   if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
2531     {
2532       value += fixP->fx_where + fixP->fx_frag->fr_address;
2533 #ifdef OBJ_ELF
2534       if (S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
2535         {
2536           /* Yes, we add the values in twice.  This is because
2537              bfd_perform_relocation subtracts them out again.  I think
2538              bfd_perform_relocation is broken, but I don't dare change
2539              it.  FIXME.  */
2540           value += fixP->fx_where + fixP->fx_frag->fr_address;
2541         }
2542 #endif
2543     }
2544
2545   /* Fix a few things - the dynamic linker expects certain values here,
2546      and we must not dissappoint it. */
2547 #ifdef OBJ_ELF
2548   if (fixP->fx_addsy)
2549     switch(fixP->fx_r_type) {
2550     case BFD_RELOC_386_PLT32:
2551       /* Make the jump instruction point to the address of the operand.  At
2552          runtime we merely add the offset to the actual PLT entry. */
2553       value = 0xfffffffc;
2554       break;
2555     case BFD_RELOC_386_GOTPC:
2556 /*
2557  *  This is tough to explain.  We end up with this one if we have
2558  * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal
2559  * here is to obtain the absolute address of the GOT, and it is strongly
2560  * preferable from a performance point of view to avoid using a runtime
2561  * relocation for this.  The actual sequence of instructions often look 
2562  * something like:
2563  * 
2564  *      call    .L66
2565  * .L66:
2566  *      popl    %ebx
2567  *      addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
2568  * 
2569  *      The call and pop essentially return the absolute address of
2570  * the label .L66 and store it in %ebx.  The linker itself will
2571  * ultimately change the first operand of the addl so that %ebx points to
2572  * the GOT, but to keep things simple, the .o file must have this operand
2573  * set so that it generates not the absolute address of .L66, but the
2574  * absolute address of itself.  This allows the linker itself simply
2575  * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
2576  * added in, and the addend of the relocation is stored in the operand
2577  * field for the instruction itself.
2578  * 
2579  *      Our job here is to fix the operand so that it would add the correct
2580  * offset so that %ebx would point to itself.  The thing that is tricky is
2581  * that .-.L66 will point to the beginning of the instruction, so we need
2582  * to further modify the operand so that it will point to itself.
2583  * There are other cases where you have something like:
2584  * 
2585  *      .long   $_GLOBAL_OFFSET_TABLE_+[.-.L66]
2586  * 
2587  * and here no correction would be required.  Internally in the assembler
2588  * we treat operands of this form as not being pcrel since the '.' is 
2589  * explicitly mentioned, and I wonder whether it would simplify matters
2590  * to do it this way.  Who knows.  In earlier versions of the PIC patches,
2591  * the pcrel_adjust field was used to store the correction, but since the
2592  * expression is not pcrel, I felt it would be confusing to do it this way.
2593  */
2594       value -= 1;
2595       break;
2596     case BFD_RELOC_386_GOT32:
2597       value = 0; /* Fully resolved at runtime.  No addend. */
2598       break;
2599     case BFD_RELOC_386_GOTOFF:
2600       break;
2601
2602     default:
2603       break;
2604     }
2605 #endif
2606
2607 #endif
2608   md_number_to_chars (p, value, fixP->fx_size);
2609 }
2610
2611 #ifdef BFD_ASSEMBLER
2612 int
2613 md_apply_fix (fixP, valp)
2614      fixS *fixP;
2615      valueT *valp;
2616 {
2617   md_apply_fix_1 (fixP, *valp);
2618   return 1;
2619 }
2620 #else
2621 void
2622 md_apply_fix (fixP, val)
2623      fixS *fixP;
2624      long val;
2625 {
2626   md_apply_fix_1 (fixP, val);
2627 }
2628 #endif
2629
2630 #if 0
2631 /* This is never used.  */
2632 long                            /* Knows about the byte order in a word. */
2633 md_chars_to_number (con, nbytes)
2634      unsigned char con[];       /* Low order byte 1st. */
2635      int nbytes;                /* Number of bytes in the input. */
2636 {
2637   long retval;
2638   for (retval = 0, con += nbytes - 1; nbytes--; con--)
2639     {
2640       retval <<= BITS_PER_CHAR;
2641       retval |= *con;
2642     }
2643   return retval;
2644 }
2645 #endif /* 0 */
2646 \f
2647
2648 #define MAX_LITTLENUMS 6
2649
2650 /* Turn the string pointed to by litP into a floating point constant of type
2651    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
2652    is stored in *sizeP .  An error message is returned, or NULL on OK.  */
2653 char *
2654 md_atof (type, litP, sizeP)
2655      char type;
2656      char *litP;
2657      int *sizeP;
2658 {
2659   int prec;
2660   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2661   LITTLENUM_TYPE *wordP;
2662   char *t;
2663
2664   switch (type)
2665     {
2666     case 'f':
2667     case 'F':
2668       prec = 2;
2669       break;
2670
2671     case 'd':
2672     case 'D':
2673       prec = 4;
2674       break;
2675
2676     case 'x':
2677     case 'X':
2678       prec = 5;
2679       break;
2680
2681     default:
2682       *sizeP = 0;
2683       return "Bad call to md_atof ()";
2684     }
2685   t = atof_ieee (input_line_pointer, type, words);
2686   if (t)
2687     input_line_pointer = t;
2688
2689   *sizeP = prec * sizeof (LITTLENUM_TYPE);
2690   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
2691      the bigendian 386.  */
2692   for (wordP = words + prec - 1; prec--;)
2693     {
2694       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
2695       litP += sizeof (LITTLENUM_TYPE);
2696     }
2697   return 0;
2698 }
2699 \f
2700 char output_invalid_buf[8];
2701
2702 static char *
2703 output_invalid (c)
2704      char c;
2705 {
2706   if (isprint (c))
2707     sprintf (output_invalid_buf, "'%c'", c);
2708   else
2709     sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
2710   return output_invalid_buf;
2711 }
2712
2713 /* reg_string starts *before* REGISTER_PREFIX */
2714 static reg_entry *
2715 parse_register (reg_string)
2716      char *reg_string;
2717 {
2718   register char *s = reg_string;
2719   register char *p;
2720   char reg_name_given[MAX_REG_NAME_SIZE];
2721
2722   s++;                          /* skip REGISTER_PREFIX */
2723   for (p = reg_name_given; is_register_char (*s); p++, s++)
2724     {
2725       *p = register_chars[(unsigned char) *s];
2726       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
2727         return (reg_entry *) 0;
2728     }
2729   *p = '\0';
2730   return (reg_entry *) hash_find (reg_hash, reg_name_given);
2731 }
2732 \f
2733 #ifdef OBJ_ELF
2734 CONST char *md_shortopts = "mVQ:";
2735 #else
2736 CONST char *md_shortopts = "m";
2737 #endif
2738 struct option md_longopts[] = {
2739   {NULL, no_argument, NULL, 0}
2740 };
2741 size_t md_longopts_size = sizeof(md_longopts);
2742
2743 int
2744 md_parse_option (c, arg)
2745      int c;
2746      char *arg;
2747 {
2748   switch (c)
2749     {
2750     case 'm':
2751       flag_do_long_jump = 1;
2752       break;
2753
2754 #ifdef OBJ_ELF
2755       /* -V: SVR4 argument to print version ID.  */
2756     case 'V':
2757       print_version_id ();
2758       break;
2759
2760       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
2761          should be emitted or not.  FIXME: Not implemented.  */
2762     case 'Q':
2763       break;
2764 #endif
2765
2766     default:
2767       return 0;
2768     }
2769   return 1;
2770 }
2771
2772 void
2773 md_show_usage (stream)
2774      FILE *stream;
2775 {
2776   fprintf (stream, "\
2777 -m                      do long jump\n");
2778 }
2779 \f
2780 /* We have no need to default values of symbols.  */
2781
2782 /* ARGSUSED */
2783 symbolS *
2784 md_undefined_symbol (name)
2785      char *name;
2786 {
2787         if (*name == '_' && *(name+1) == 'G'
2788             && strcmp(name, GLOBAL_OFFSET_TABLE_NAME) == 0)
2789           {
2790             if(!GOT_symbol)
2791               {
2792                 if(symbol_find(name)) 
2793                   as_bad("GOT already in symbol table");
2794                 GOT_symbol = symbol_new (name, undefined_section, 
2795                                          (valueT) 0, &zero_address_frag);
2796               };
2797             return GOT_symbol;
2798           }
2799   return 0;
2800 }
2801
2802 /* Round up a section size to the appropriate boundary.  */
2803 valueT
2804 md_section_align (segment, size)
2805      segT segment;
2806      valueT size;
2807 {
2808   return size;                  /* Byte alignment is fine */
2809 }
2810
2811 /* Exactly what point is a PC-relative offset relative TO?  On the
2812    i386, they're relative to the address of the offset, plus its
2813    size. (??? Is this right?  FIXME-SOON!) */
2814 long
2815 md_pcrel_from (fixP)
2816      fixS *fixP;
2817 {
2818   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2819 }
2820
2821 #ifndef I386COFF
2822
2823 static void
2824 s_bss (ignore)
2825      int ignore;
2826 {
2827   register int temp;
2828
2829   temp = get_absolute_expression ();
2830   subseg_set (bss_section, (subsegT) temp);
2831   demand_empty_rest_of_line ();
2832 }
2833
2834 #endif
2835
2836
2837 #ifdef BFD_ASSEMBLER
2838
2839 void
2840 i386_validate_fix (fixp)
2841      fixS *fixp;
2842 {
2843   if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
2844     {
2845       fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
2846       fixp->fx_subsy = 0;
2847     }
2848 }
2849
2850 #define F(SZ,PCREL)             (((SZ) << 1) + (PCREL))
2851 #define MAP(SZ,PCREL,TYPE)      case F(SZ,PCREL): code = (TYPE); break
2852
2853 arelent *
2854 tc_gen_reloc (section, fixp)
2855      asection *section;
2856      fixS *fixp;
2857 {
2858   arelent *rel;
2859   bfd_reloc_code_real_type code;
2860
2861   switch(fixp->fx_r_type)
2862     {
2863     case BFD_RELOC_386_PLT32:
2864     case BFD_RELOC_386_GOT32:
2865     case BFD_RELOC_386_GOTOFF:
2866     case BFD_RELOC_386_GOTPC:
2867       code = fixp->fx_r_type;
2868       break;
2869     default:
2870       switch (F (fixp->fx_size, fixp->fx_pcrel))
2871         {
2872 #ifndef OBJ_ELF
2873           MAP (1, 0, BFD_RELOC_8);
2874           MAP (2, 0, BFD_RELOC_16);
2875 #endif
2876           MAP (4, 0, BFD_RELOC_32);
2877 #ifndef OBJ_ELF
2878           MAP (1, 1, BFD_RELOC_8_PCREL);
2879           MAP (2, 1, BFD_RELOC_16_PCREL);
2880 #endif
2881           MAP (4, 1, BFD_RELOC_32_PCREL);
2882         default:
2883           as_bad ("Can not do %d byte %srelocation", fixp->fx_size,
2884                   fixp->fx_pcrel ? "pc-relative" : "");
2885         }
2886     }
2887 #undef MAP
2888 #undef F
2889
2890   if (code == BFD_RELOC_32
2891       && GOT_symbol
2892       && fixp->fx_addsy == GOT_symbol)
2893     code = BFD_RELOC_386_GOTPC;
2894
2895   rel = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
2896   assert (rel != 0);
2897   rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2898   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2899   if (fixp->fx_pcrel)
2900     rel->addend = fixp->fx_addnumber;
2901   else
2902     rel->addend = 0;
2903
2904   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
2905   if (!rel->howto)
2906     {
2907       const char *name;
2908
2909       name = S_GET_NAME (fixp->fx_addsy);
2910       if (name == NULL)
2911         name = "<unknown>";
2912       as_fatal ("Cannot generate relocation type for symbol %s, code %s",
2913                 name, bfd_get_reloc_code_name (code));
2914     }
2915
2916   return rel;
2917 }
2918
2919 #else /* ! BFD_ASSEMBLER */
2920
2921 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
2922 void
2923 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
2924      char *where;
2925      fixS *fixP;
2926      relax_addressT segment_address_in_file;
2927 {
2928   /*
2929    * In: length of relocation (or of address) in chars: 1, 2 or 4.
2930    * Out: GNU LD relocation length code: 0, 1, or 2.
2931    */
2932
2933   static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
2934   long r_symbolnum;
2935
2936   know (fixP->fx_addsy != NULL);
2937
2938   md_number_to_chars (where,
2939                       (valueT) (fixP->fx_frag->fr_address
2940                                 + fixP->fx_where - segment_address_in_file),
2941                       4);
2942
2943   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
2944                  ? S_GET_TYPE (fixP->fx_addsy)
2945                  : fixP->fx_addsy->sy_number);
2946
2947   where[6] = (r_symbolnum >> 16) & 0x0ff;
2948   where[5] = (r_symbolnum >> 8) & 0x0ff;
2949   where[4] = r_symbolnum & 0x0ff;
2950   where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
2951               | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
2952               | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
2953 }
2954
2955 #endif /* OBJ_AOUT or OBJ_BOUT */
2956
2957 #if defined (I386COFF)
2958
2959 short
2960 tc_coff_fix2rtype (fixP)
2961      fixS *fixP;
2962 {
2963   return (fixP->fx_pcrel ?
2964           (fixP->fx_size == 1 ? R_PCRBYTE :
2965            fixP->fx_size == 2 ? R_PCRWORD :
2966            R_PCRLONG) :
2967           (fixP->fx_size == 1 ? R_RELBYTE :
2968            fixP->fx_size == 2 ? R_RELWORD :
2969            R_DIR32));
2970 }
2971
2972 int
2973 tc_coff_sizemachdep (frag)
2974      fragS *frag;
2975 {
2976   if (frag->fr_next)
2977     return (frag->fr_next->fr_address - frag->fr_address);
2978   else
2979     return 0;
2980 }
2981
2982 #endif /* I386COFF */
2983
2984 #endif /* BFD_ASSEMBLER? */
2985
2986 /* end of tc-i386.c */