home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gassrc04.zoo / i386.c < prev    next >
C/C++ Source or Header  |  1991-01-24  |  61KB  |  1,936 lines

  1. /* i386.c -- Assemble code for the Intel 80386
  2.    Copyright (C) 1989, 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 1, 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 <stdio.h>
  28. #include <varargs.h>
  29. #include <ctype.h>
  30.  
  31. #ifdef __GNUC__
  32. #define alloca __builtin_alloca
  33. #else
  34. extern char *alloca();
  35. #endif
  36. #ifdef USG
  37. #define index strchr
  38. #endif
  39.  
  40. #include "as.h"
  41. #include "read.h"
  42. #include "flonum.h"
  43. #include "obstack.h"
  44. #include "frags.h"
  45. #include "struc-symbol.h"
  46. #include "expr.h"
  47. #include "symbols.h"
  48. #include "hash.h"
  49. #include "md.h"
  50. #include "i386.h"
  51. #include "i386-opcode.h"
  52.  
  53. long omagic = OMAGIC;
  54. char FLT_CHARS[] = "fFdDxX";
  55. char EXP_CHARS[] = "eE";
  56. char line_comment_chars[] = "#";
  57. char comment_chars[] = "#/";
  58.  
  59. /* tables for lexical analysis */
  60. static char opcode_chars[256];
  61. static char register_chars[256];
  62. static char operand_chars[256];
  63. static char space_chars[256];
  64. static char identifier_chars[256];
  65. static char digit_chars[256];
  66.  
  67. /* lexical macros */
  68. #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
  69. #define is_operand_char(x) (operand_chars[(unsigned char) x])
  70. #define is_register_char(x) (register_chars[(unsigned char) x])
  71. #define is_space_char(x) (space_chars[(unsigned char) x])
  72. #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
  73. #define is_digit_char(x) (digit_chars[(unsigned char) x])
  74.  
  75. /* put here all non-digit non-letter charcters that may occur in an operand */
  76. static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:";
  77.  
  78. static char *ordinal_names[] = { "first", "second", "third" };    /* for printfs */
  79.  
  80. /* md_assemble() always leaves the strings it's passed unaltered.  To
  81.    effect this we maintain a stack of saved characters that we've smashed
  82.    with '\0's (indicating end of strings for various sub-fields of the
  83.    assembler instruction). */
  84. static char save_stack[32];
  85. static char *save_stack_p;    /* stack pointer */
  86. #define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
  87. #define RESTORE_END_STRING(s)       *s = *--save_stack_p
  88.  
  89. /* The instruction we're assembling. */
  90. static i386_insn i;
  91.  
  92. /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
  93. static expressionS disp_expressions[2], im_expressions[2];
  94.  
  95. /* pointers to ebp & esp entries in reg_hash hash table */
  96. static reg_entry *ebp, *esp;
  97.  
  98. static int this_operand;    /* current operand we are working on */
  99.  
  100. /*
  101. Interface to relax_segment.
  102. There are 2 relax states for 386 jump insns: one for conditional & one
  103. for unconditional jumps.  This is because the these two types of jumps
  104. add different sizes to frags when we're figuring out what sort of jump
  105. to choose to reach a given label.  */
  106.  
  107. /* types */
  108. #define COND_JUMP 1        /* conditional jump */
  109. #define UNCOND_JUMP 2        /* unconditional jump */
  110. /* sizes */
  111. #define BYTE 0
  112. #define WORD 1
  113. #define DWORD 2
  114. #define UNKNOWN_SIZE 3
  115.  
  116. #define ENCODE_RELAX_STATE(type,size) ((type<<2) | (size))
  117. #define SIZE_FROM_RELAX_STATE(s) \
  118.   ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
  119.  
  120. const relax_typeS md_relax_table[] = {
  121. /*
  122.   The fields are:
  123.    1) most positive reach of this state,
  124.    2) most negative reach of this state,
  125.    3) how many bytes this mode will add to the size of the current frag
  126.    4) which index into the table to try if we can't fit into this one.
  127. */
  128.   {1, 1, 0, 0},
  129.   {1, 1, 0, 0},
  130.   {1, 1, 0, 0},
  131.   {1, 1, 0, 0},
  132.  
  133.   /* For now we don't use word displacement jumps:  they may be
  134.      untrustworthy. */
  135.   {127+1, -128+1, 0, ENCODE_RELAX_STATE(COND_JUMP,DWORD) },
  136.   /* word conditionals add 3 bytes to frag:
  137.          2 opcode prefix; 1 displacement bytes */
  138.   {32767+2, -32768+2, 3, ENCODE_RELAX_STATE(COND_JUMP,DWORD) },
  139.   /* dword conditionals adds 4 bytes to frag:
  140.          1 opcode prefix; 3 displacement bytes */
  141.   {0, 0, 4, 0},
  142.   {1, 1, 0, 0},
  143.  
  144.   {127+1, -128+1, 0, ENCODE_RELAX_STATE(UNCOND_JUMP,DWORD) },
  145.   /* word jmp adds 2 bytes to frag:
  146.          1 opcode prefix; 1 displacement bytes */
  147.   {32767+2, -32768+2, 2, ENCODE_RELAX_STATE(UNCOND_JUMP,DWORD) },
  148.   /* dword jmp adds 3 bytes to frag:
  149.          0 opcode prefix; 3 displacement bytes */
  150.   {0, 0, 3, 0},
  151.   {1, 1, 0, 0},
  152.  
  153. };
  154.  
  155. void float_cons (), cons ();
  156.  
  157. /* Ignore certain directives generated by gcc. This probably should
  158.    not be here. */
  159. void dummy ()
  160. {
  161.   while (*input_line_pointer && *input_line_pointer != '\n')
  162.     input_line_pointer++;
  163. }
  164.  
  165. const pseudo_typeS md_pseudo_table[] = {
  166.     { "ffloat",    float_cons,    'f' },
  167.     { "dfloat",    float_cons,    'd' },
  168.     { "tfloat",    float_cons,    'x' },
  169.     { "value",      cons,           2 },
  170.     { "ident",      dummy,          0   }, /* ignore these directives */
  171.     { "def",        dummy,          0   },
  172.     { "optim",    dummy,        0   }, /* For sun386i cc */
  173.     { "version",    dummy,          0   },
  174.     { "ln",    dummy,          0   },
  175.     { 0, 0, 0 }
  176. };
  177.  
  178. /* for interface with expression () */
  179. extern char * input_line_pointer;
  180. char * index ();
  181.  
  182. char * output_invalid ();
  183. reg_entry * parse_register ();
  184.  
  185. /* obstack for constructing various things in md_begin */
  186. struct obstack o;
  187.  
  188. /* hash table for opcode lookup */
  189. static struct hash_control *op_hash = (struct hash_control *) 0;
  190. /* hash table for register lookup */
  191. static struct hash_control *reg_hash = (struct hash_control *) 0;
  192. /* hash table for prefix lookup */
  193. static struct hash_control *prefix_hash = (struct hash_control *) 0;
  194.  
  195.  
  196. void md_begin ()
  197. {
  198.   char * hash_err;
  199.  
  200.   obstack_begin (&o,4096);
  201.  
  202.   /* initialize op_hash hash table */
  203.   op_hash = hash_new();        /* xmalloc handles error */
  204.  
  205.   {
  206.     register template *optab;
  207.     register templates *core_optab;
  208.     char *prev_name;
  209.  
  210.     optab = i386_optab;        /* setup for loop */
  211.     prev_name = optab->name;
  212.     obstack_grow (&o, optab, sizeof(template));
  213.     core_optab = (templates *) xmalloc (sizeof (templates));
  214.  
  215.     for (optab++; optab < i386_optab_end; optab++) {
  216.       if (! strcmp (optab->name, prev_name)) {
  217.     /* same name as before --> append to current template list */
  218.     obstack_grow (&o, optab, sizeof(template));
  219.       } else {
  220.     /* different name --> ship out current template list;
  221.        add to hash table; & begin anew */
  222.     /* Note: end must be set before start! since obstack_next_free changes
  223.        upon opstack_finish */
  224.     core_optab->end = (template *) obstack_next_free(&o);
  225.     core_optab->start = (template *) obstack_finish(&o);
  226.     hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
  227.     if (hash_err && *hash_err) {
  228.     hash_error:
  229.       as_fatal("Internal Error:  Can't hash %s: %s",prev_name, hash_err);
  230.     }
  231.     prev_name = optab->name;
  232.     core_optab = (templates *) xmalloc (sizeof(templates));
  233.     obstack_grow (&o, optab, sizeof(template));
  234.       }
  235.     }
  236.   }
  237.   
  238.   /* initialize reg_hash hash table */
  239.   reg_hash = hash_new();
  240.   {
  241.     register reg_entry *regtab;
  242.  
  243.     for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++) {
  244.       hash_err = hash_insert (reg_hash, regtab->reg_name, regtab);
  245.       if (hash_err && *hash_err) goto hash_error;
  246.     }
  247.   }
  248.  
  249.   esp = (reg_entry *) hash_find (reg_hash, "esp");
  250.   ebp = (reg_entry *) hash_find (reg_hash, "ebp");
  251.   
  252.   /* initialize reg_hash hash table */
  253.   prefix_hash = hash_new();
  254.   {
  255.     register prefix_entry *prefixtab;
  256.  
  257.     for (prefixtab = i386_prefixtab;
  258.      prefixtab < i386_prefixtab_end; prefixtab++) {
  259.       hash_err = hash_insert (prefix_hash, prefixtab->prefix_name, prefixtab);
  260.       if (hash_err && *hash_err) goto hash_error;
  261.     }
  262.   }
  263.  
  264.   /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
  265.   {  
  266.     register unsigned int c;
  267.     
  268.     bzero (opcode_chars, sizeof(opcode_chars));
  269.     bzero (operand_chars, sizeof(operand_chars));
  270.     bzero (space_chars, sizeof(space_chars));
  271.     bzero (identifier_chars, sizeof(identifier_chars));
  272.     bzero (digit_chars, sizeof(digit_chars));
  273.  
  274.     for (c = 0; c < 256; c++) {
  275.       if (islower(c) || isdigit(c)) {
  276.     opcode_chars[c] = c;
  277.     register_chars[c] = c;
  278.       } else if (isupper(c)) {
  279.     opcode_chars[c] = tolower(c);
  280.     register_chars[c] = opcode_chars[c];
  281.       } else if (c == PREFIX_SEPERATOR) {
  282.     opcode_chars[c] = c;
  283.       } else if (c == ')' || c == '(') {
  284.     register_chars[c] = c;
  285.       }
  286.       
  287.       if (isupper(c) || islower(c) || isdigit(c))
  288.     operand_chars[c] = c;
  289.       else if (c && index(operand_special_chars, c))
  290.       operand_chars[c] = c;
  291.       
  292.       if (isdigit(c) || c == '-') digit_chars[c] = c;
  293.  
  294.       if (isalpha(c) || c == '_' || c == '.' || isdigit(c))
  295.     identifier_chars[c] = c;
  296.  
  297.       if (c == ' ' || c == '\t') space_chars[c] = c;
  298.     }
  299.   }
  300. }
  301.  
  302. void md_end() {}        /* not much to do here. */
  303.  
  304.  
  305. #ifdef DEBUG386
  306.  
  307. /* debugging routines for md_assemble */
  308. static void pi (), pte (), pt (), pe (), ps ();
  309.  
  310. static void pi (line, x)
  311.      char * line;
  312.      i386_insn *x;
  313. {
  314.   register template *p;
  315.   int i;
  316.  
  317.   fprintf (stdout, "%s: template ", line);
  318.   pte (&x->tm);
  319.   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
  320.        x->rm.mode, x->rm.reg, x->rm.regmem);
  321.   fprintf (stdout, " base %x  index %x  scale %x\n",
  322.        x->bi.base, x->bi.index, x->bi.scale);
  323.   for (i = 0; i < x->operands; i++) {
  324.     fprintf (stdout, "    #%d:  ", i+1);
  325.     pt (x->types[i]);
  326.     fprintf (stdout, "\n");
  327.     if (x->types[i] & Reg) fprintf (stdout, "%s\n", x->regs[i]->reg_name);
  328.     if (x->types[i] & Imm) pe (x->imms[i]);
  329.     if (x->types[i] & (Disp|Abs)) pe (x->disps[i]);
  330.   }
  331. }
  332.  
  333. static void pte (t)
  334.      template *t;
  335. {
  336.   int i;
  337.   fprintf (stdout, " %d operands ", t->operands);
  338.   fprintf (stdout, "opcode %x ",
  339.        t->base_opcode);
  340.   if (t->extension_opcode != None)
  341.     fprintf (stdout, "ext %x ", t->extension_opcode);
  342.   if (t->opcode_modifier&D)
  343.     fprintf (stdout, "D");
  344.   if (t->opcode_modifier&W)
  345.     fprintf (stdout, "W");
  346.   fprintf (stdout, "\n");
  347.   for (i = 0; i < t->operands; i++) {
  348.     fprintf (stdout, "    #%d type ", i+1);
  349.     pt (t->operand_types[i]);
  350.     fprintf (stdout, "\n");
  351.   }
  352. }
  353.  
  354. char *seg_names[] = {
  355. "SEG_ABSOLUTE", "SEG_TEXT", "SEG_DATA", "SEG_BSS", "SEG_UNKNOWN",
  356. "SEG_NONE", "SEG_PASS1", "SEG_GOOF", "SEG_BIG", "SEG_DIFFERENCE" };
  357.  
  358. static void pe (e)
  359.      expressionS *e;
  360. {
  361.   fprintf (stdout, "    segment       %s\n", seg_names[(int) e->X_seg]);
  362.   fprintf (stdout, "    add_number    %d (%x)\n",
  363.        e->X_add_number, e->X_add_number);
  364.   if (e->X_add_symbol) {
  365.     fprintf (stdout, "    add_symbol    ");
  366.     ps (e->X_add_symbol);
  367.     fprintf (stdout, "\n");
  368.   }
  369.   if (e->X_subtract_symbol) {
  370.     fprintf (stdout, "    sub_symbol    ");
  371.     ps (e->X_subtract_symbol);
  372.     fprintf (stdout, "\n");
  373.   }
  374. }
  375.  
  376. #define SYMBOL_TYPE(t) \
  377.   (((t&N_TYPE) == N_UNDF) ? "UNDEFINED" : \
  378.    (((t&N_TYPE) == N_ABS) ? "ABSOLUTE" : \
  379.     (((t&N_TYPE) == N_TEXT) ? "TEXT" : \
  380.      (((t&N_TYPE) == N_DATA) ? "DATA" : \
  381.       (((t&N_TYPE) == N_BSS) ? "BSS" : "Bad n_type!")))))
  382.  
  383. static void ps (s)
  384.      symbolS *s;
  385. {
  386.   fprintf (stdout, "%s type %s%s",
  387.        s->sy_nlist.n_un.n_name,
  388.        (s->sy_nlist.n_type&N_EXT) ? "EXTERNAL " : "",
  389.        SYMBOL_TYPE (s->sy_nlist.n_type));
  390. }
  391.  
  392. struct type_name {
  393.   uint mask;
  394.   char *tname;
  395. } type_names[] = {
  396.   { Reg8, "r8" }, { Reg16, "r16" }, { Reg32, "r32" }, { Imm8, "i8" },
  397.   { Imm8S, "i8s" },
  398.   { Imm16, "i16" }, { Imm32, "i32" }, { Mem8, "Mem8"}, { Mem16, "Mem16"},
  399.   { Mem32, "Mem32"}, { BaseIndex, "BaseIndex" },
  400.   { Abs8, "Abs8" }, { Abs16, "Abs16" }, { Abs32, "Abs32" },
  401.   { Disp8, "d8" }, { Disp16, "d16" },
  402.   { Disp32, "d32" }, { SReg2, "SReg2" }, { SReg3, "SReg3" }, { Acc, "Acc" },
  403.   { InOutPortReg, "InOutPortReg" }, { ShiftCount, "ShiftCount" },
  404.   { Imm1, "i1" }, { Control, "control reg" }, {Test, "test reg"},
  405.   { FloatReg, "FReg"}, {FloatAcc, "FAcc"},
  406.   { JumpAbsolute, "Jump Absolute"},
  407.   { 0, "" }
  408. };
  409.  
  410. static void pt (t)
  411.      uint t;
  412. {
  413.   register struct type_name *ty;
  414.  
  415.   if (t == Unknown) {
  416.     fprintf (stdout, "Unknown");
  417.   } else {
  418.     for (ty = type_names; ty->mask; ty++)
  419.       if (t & ty->mask) fprintf (stdout, "%s, ", ty->tname);
  420.   }
  421.   fflush (stdout);
  422. }
  423.  
  424. #endif /* DEBUG386 */
  425.  
  426. /*
  427.   This is the guts of the machine-dependent assembler.  LINE points to a
  428.   machine dependent instruction.  This funciton is supposed to emit
  429.   the frags/bytes it assembles to.
  430.  */
  431. void md_assemble (line)
  432.      char *line;
  433. {
  434.   /* Holds temlate once we've found it. */
  435.   register template * t;
  436.  
  437.   /* Possible templates for current insn */
  438.   templates *current_templates = (templates *) 0;
  439.  
  440.   /* Initialize globals. */
  441.   bzero (&i, sizeof(i));
  442.   bzero (disp_expressions, sizeof(disp_expressions));
  443.   bzero (im_expressions, sizeof(im_expressions));
  444.   save_stack_p = save_stack;    /* reset stack pointer */
  445.   
  446.   /* Fist parse an opcode & call i386_operand for the operands.
  447.      We assume that the scrubber has arranged it so that line[0] is the valid 
  448.      start of a (possibly prefixed) opcode. */
  449.   {
  450.     register char *l = line;        /* Fast place to put LINE. */
  451.  
  452.     /* TRUE if operand is pending after ','. */
  453.     uint expecting_operand = 0;
  454.     /* TRUE if we found a prefix only acceptable with string insns. */
  455.     uint expecting_string_instruction = 0;
  456.     /* Non-zero if operand parens not balenced. */
  457.     uint paren_not_balenced;
  458.     char * token_start = l;
  459.  
  460.     while (! is_space_char(*l) && *l != END_OF_INSN) {
  461.       if (! is_opcode_char(*l)) {
  462.     as_bad ("invalid character %s in opcode", output_invalid(*l));
  463.     return;
  464.       } else if (*l != PREFIX_SEPERATOR) {
  465.     *l = opcode_chars[(unsigned char) *l];    /* fold case of opcodes */
  466.     l++;
  467.       } else {      /* this opcode's got a prefix */
  468.     register int q;
  469.     register prefix_entry * prefix;
  470.  
  471.     if (l == token_start) {
  472.       as_bad ("expecting prefix; got nothing");
  473.       return;
  474.     }
  475.     END_STRING_AND_SAVE (l);
  476.     prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
  477.     if (! prefix) {
  478.       as_bad ("no such opcode prefix ('%s')", token_start);
  479.       return;
  480.     }
  481.     RESTORE_END_STRING (l);
  482.     /* check for repeated prefix */
  483.     for (q = 0; q < i.prefixes; q++)
  484.       if (i.prefix[q] == prefix->prefix_code) {
  485.         as_bad ("same prefix used twice; you don't really want this!");
  486.         return;
  487.       }
  488.     if (i.prefixes == MAX_PREFIXES) {
  489.       as_bad ("too many opcode prefixes");
  490.       return;
  491.     }
  492.     i.prefix[i.prefixes++] = prefix->prefix_code;
  493.     if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
  494.       expecting_string_instruction = TRUE;
  495.     /* skip past PREFIX_SEPERATOR and reset token_start */
  496.     token_start = ++l;
  497.       }
  498.     }
  499.     END_STRING_AND_SAVE (l);
  500.     if (token_start == l) {
  501.       as_bad ("expecting opcode; got nothing");
  502.       return;
  503.     }
  504.  
  505.     /* Lookup insn in hash; try intel & att naming conventions if appropriate;
  506.        that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
  507.     current_templates = (templates *) hash_find (op_hash, token_start);
  508.     if (! current_templates) {
  509.       int last_index = strlen(token_start) - 1;
  510.       char last_char = token_start[last_index];
  511.       switch (last_char) {
  512.       case DWORD_OPCODE_SUFFIX:
  513.       case WORD_OPCODE_SUFFIX:
  514.       case BYTE_OPCODE_SUFFIX:
  515.     token_start[last_index] = '\0';
  516.     current_templates = (templates *) hash_find (op_hash, token_start);
  517.     token_start[last_index] = last_char;
  518.     i.suffix = last_char;
  519.       }
  520.       if (!current_templates) {
  521.     as_bad ("no such 386 instruction: `%s'", token_start); return;
  522.       }
  523.     }
  524.     RESTORE_END_STRING (l);
  525.  
  526.     /* check for rep/repne without a string instruction */
  527.     if (expecting_string_instruction &&
  528.     ! IS_STRING_INSTRUCTION (current_templates->
  529.                  start->base_opcode)) {
  530.       as_bad ("expecting string instruction after rep/repne");
  531.       return;
  532.     }
  533.  
  534.     /* There may be operands to parse. */
  535.     if (*l != END_OF_INSN &&
  536.     /* For string instructions, we ignore any operands if given.  This
  537.        kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
  538.        the operands are always going to be the same, and are not really
  539.        encoded in machine code. */
  540.     ! IS_STRING_INSTRUCTION (current_templates->
  541.                  start->base_opcode)) {
  542.       /* parse operands */
  543.       do {
  544.     /* skip optional white space before operand */
  545.     while (! is_operand_char(*l) && *l != END_OF_INSN) {
  546.       if (! is_space_char(*l)) {
  547.         as_bad ("invalid character %s before %s operand",
  548.              output_invalid(*l),
  549.              ordinal_names[i.operands]);
  550.         return;
  551.       }
  552.       l++;
  553.     }
  554.     token_start = l;        /* after white space */
  555.     paren_not_balenced = 0;
  556.     while (paren_not_balenced || *l != ',') {
  557.       if (*l == END_OF_INSN) {
  558.         if (paren_not_balenced) {
  559.           as_bad ("unbalenced parenthesis in %s operand.",
  560.                ordinal_names[i.operands]);
  561.           return;
  562.         } else break;        /* we are done */
  563.       } else if (! is_operand_char(*l)) {
  564.         as_bad ("invalid character %s in %s operand",
  565.              output_invalid(*l),
  566.              ordinal_names[i.operands]);
  567.         return;
  568.       }
  569.       if (*l == '(') ++paren_not_balenced;
  570.       if (*l == ')') --paren_not_balenced;
  571.       l++;
  572.     }
  573.     if (l != token_start) {    /* yes, we've read in another operand */
  574.       uint operand_ok;
  575.       this_operand = i.operands++;
  576.       if (i.operands > MAX_OPERANDS) {
  577.         as_bad ("spurious operands; (%d operands/instruction max)",
  578.              MAX_OPERANDS);
  579.         return;
  580.       }
  581.       /* now parse operand adding info to 'i' as we go along */
  582.       END_STRING_AND_SAVE (l);
  583.       operand_ok = i386_operand (token_start);
  584.       RESTORE_END_STRING (l);    /* restore old contents */
  585.       if (!operand_ok) return;
  586.     } else {
  587.       if (expecting_operand) {
  588.       expecting_operand_after_comma:
  589.         as_bad ("expecting operand after ','; got nothing");
  590.         return;
  591.       }
  592.       if (*l == ',') {
  593.         as_bad ("expecting operand before ','; got nothing");
  594.         return;
  595.       }
  596.     }
  597.       
  598.     /* now *l must be either ',' or END_OF_INSN */
  599.     if (*l == ',') {
  600.       if (*++l == END_OF_INSN) {        /* just skip it, if it's \n complain */
  601.         goto expecting_operand_after_comma;
  602.       }
  603.       expecting_operand = TRUE;
  604.     }
  605.       } while (*l != END_OF_INSN);        /* until we get end of insn */
  606.     }
  607.   }
  608.  
  609.   /* Now we've parsed the opcode into a set of templates, and have the
  610.      operands at hand.
  611.      Next, we find a template that matches the given insn,
  612.      making sure the overlap of the given operands types is consistent
  613.      with the template operand types. */
  614.  
  615. #define MATCH(overlap,given_type) \
  616.   (overlap && \
  617.    (overlap & (JumpAbsolute|BaseIndex|Mem8)) \
  618.    == (given_type & (JumpAbsolute|BaseIndex|Mem8)))
  619.   
  620.     /* If m0 and m1 are register matches they must be consistent
  621.        with the expected operand types t0 and t1.
  622.      That is, if both m0 & m1 are register matches
  623.          i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
  624.      then, either 1. or 2. must be true:
  625.          1. the expected operand type register overlap is null:
  626.                  (t0 & t1 & Reg) == 0
  627.      AND
  628.         the given register overlap is null:
  629.                      (m0 & m1 & Reg) == 0
  630.      2. the expected operand type register overlap == the given
  631.         operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
  632.      */
  633. #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
  634.     ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
  635.       ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
  636.         ((t0 & t1) & (m0 & m1) & (Reg)) \
  637.        ) : 1)
  638.   {
  639.     register uint overlap0, overlap1;
  640.     expressionS * exp;
  641.     uint overlap2;
  642.     uint found_reverse_match;
  643.  
  644.     overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
  645.     for (t = current_templates->start;
  646.      t < current_templates->end;
  647.      t++) {
  648.  
  649.       /* must have right number of operands */
  650.       if (i.operands != t->operands) continue;
  651.       else if (!t->operands) break;    /* 0 operands always matches */
  652.  
  653.       overlap0 = i.types[0] & t->operand_types[0];
  654.       switch (t->operands) {
  655.       case 1:
  656.     if (! MATCH (overlap0,i.types[0])) continue;
  657.     break;
  658.       case 2: case 3:
  659.     overlap1 = i.types[1] & t->operand_types[1];
  660.     if (! MATCH (overlap0,i.types[0]) ||
  661.         ! MATCH (overlap1,i.types[1]) ||
  662.         ! CONSISTENT_REGISTER_MATCH(overlap0, overlap1,
  663.                     t->operand_types[0],
  664.                     t->operand_types[1])) {
  665.  
  666.       /* check if other direction is valid ... */
  667.       if (! (t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
  668.         continue;
  669.       
  670.       /* try reversing direction of operands */
  671.       overlap0 = i.types[0] & t->operand_types[1];
  672.       overlap1 = i.types[1] & t->operand_types[0];
  673.       if (! MATCH (overlap0,i.types[0]) ||
  674.           ! MATCH (overlap1,i.types[1]) ||
  675.           ! CONSISTENT_REGISTER_MATCH (overlap0, overlap1, 
  676.                        t->operand_types[0],
  677.                        t->operand_types[1])) {
  678.         /* does not match either direction */
  679.         continue;
  680.       }
  681.       /* found a reverse match here -- slip through */
  682.       /* found_reverse_match holds which of D or FloatD we've found */
  683.       found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
  684.     }                /* endif: not forward match */
  685.     /* found either forward/reverse 2 operand match here */
  686.     if (t->operands == 3) {
  687.       overlap2 = i.types[2] & t->operand_types[2];
  688.       if (! MATCH (overlap2,i.types[2]) ||
  689.           ! CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
  690.                        t->operand_types[0],
  691.                        t->operand_types[2]) ||
  692.           ! CONSISTENT_REGISTER_MATCH (overlap1, overlap2, 
  693.                        t->operand_types[1],
  694.                        t->operand_types[2]))
  695.         continue;
  696.     }
  697.     /* found either forward/reverse 2 or 3 operand match here:
  698.        slip through to break */
  699.       }
  700.       break;            /* we've found a match; break out of loop */
  701.     }                /* for (t = ... */
  702.     if (t == current_templates->end) { /* we found no match */
  703.       as_bad ("operands given don't match any known 386 instruction");
  704.       return;
  705.     }
  706.  
  707.     /* Copy the template we found (we may change it!). */
  708.     bcopy (t, &i.tm, sizeof (template));
  709.     t = &i.tm;            /* alter new copy of template */
  710.  
  711.     /* If there's no opcode suffix we try to invent one based on register
  712.        operands. */
  713.     if (! i.suffix && i.reg_operands) {
  714.       /* We take i.suffix from the LAST register operand specified.  This
  715.      assumes that the last register operands is the destination register
  716.      operand. */
  717.       int o;
  718.       for (o = 0; o < MAX_OPERANDS; o++)
  719.     if (i.types[o] & Reg) {
  720.       i.suffix = (i.types[o] == Reg8) ? BYTE_OPCODE_SUFFIX :
  721.         (i.types[o] == Reg16) ? WORD_OPCODE_SUFFIX :
  722.           DWORD_OPCODE_SUFFIX;
  723.     }
  724.     }
  725.  
  726.     /* Make still unresolved immediate matches conform to size of immediate
  727.        given in i.suffix. Note:  overlap2 cannot be an immediate!
  728.        We assume this. */
  729.     if ((overlap0 & (Imm8|Imm8S|Imm16|Imm32))
  730.     && overlap0 != Imm8 && overlap0 != Imm8S
  731.     && overlap0 != Imm16 && overlap0 != Imm32) {
  732.       if (! i.suffix) {
  733.     as_bad ("no opcode suffix given; can't determine immediate size");
  734.     return;
  735.       }
  736.       overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8|Imm8S) :
  737.            (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  738.     }
  739.     if ((overlap1 & (Imm8|Imm8S|Imm16|Imm32))
  740.     && overlap1 != Imm8 && overlap1 != Imm8S
  741.     && overlap1 != Imm16 && overlap1 != Imm32) {
  742.       if (! i.suffix) {
  743.     as_bad ("no opcode suffix given; can't determine immediate size");
  744.     return;
  745.       }
  746.       overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8|Imm8S) :
  747.            (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  748.     }
  749.  
  750.     i.types[0] = overlap0;
  751.     i.types[1] = overlap1;
  752.     i.types[2] = overlap2;
  753.  
  754.     if (overlap0 & ImplicitRegister) i.reg_operands--;
  755.     if (overlap1 & ImplicitRegister) i.reg_operands--;
  756.     if (overlap2 & ImplicitRegister) i.reg_operands--;
  757.     if (overlap0 & Imm1) i.imm_operands = 0; /* kludge for shift insns */
  758.  
  759.     if (found_reverse_match) {
  760.       uint save;
  761.       save = t->operand_types[0];
  762.       t->operand_types[0] = t->operand_types[1];
  763.       t->operand_types[1] = save;
  764.     }
  765.  
  766.     /* Finalize opcode.  First, we change the opcode based on the operand
  767.        size given by i.suffix: we never have to change things for byte insns,
  768.        or when no opcode suffix is need to size the operands. */
  769.  
  770.     if (! i.suffix && (t->opcode_modifier & W)) {
  771.       as_bad ("no opcode suffix given and no register operands; can't size instruction");
  772.       return;
  773.     }
  774.  
  775.     if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX) {
  776.       /* Select between byte and word/dword operations. */
  777.       if (t->opcode_modifier & W)
  778.     t->base_opcode |= W;
  779.       /* Now select between word & dword operations via the
  780.      operand size prefix. */
  781.       if (i.suffix == WORD_OPCODE_SUFFIX) {
  782.     if (i.prefixes == MAX_PREFIXES) {
  783.       as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
  784.            MAX_PREFIXES);
  785.       return;
  786.     }
  787.     i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
  788.       }
  789.     }
  790.  
  791.     /* For insns with operands there are more diddles to do to the opcode. */
  792.     if (i.operands) {
  793.       /* If we found a reverse match we must alter the opcode direction bit
  794.      found_reverse_match holds bit to set (different for int &
  795.      float insns). */
  796.  
  797.       if (found_reverse_match) {
  798.     t->base_opcode |= found_reverse_match;
  799.       }
  800.  
  801.       /*
  802.     The imul $imm, %reg instruction is converted into
  803.     imul $imm, %reg, %reg. */
  804.       if (t->opcode_modifier & imulKludge) {
  805.       i.regs[2] = i.regs[1]; /* Pretend we saw the 3 operand case. */
  806.       i.reg_operands = 2;
  807.       }
  808.  
  809.       /* Certain instructions expect the destination to be in the i.rm.reg
  810.      field.  This is by far the exceptional case.  For these instructions,
  811.      if the source operand is a register, we must reverse the i.rm.reg
  812.      and i.rm.regmem fields.  We accomplish this by faking that the
  813.      two register operands were given in the reverse order. */
  814.       if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2) {
  815.     uint first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
  816.     uint second_reg_operand = first_reg_operand + 1;
  817.     reg_entry *tmp = i.regs[first_reg_operand];
  818.     i.regs[first_reg_operand] = i.regs[second_reg_operand];
  819.     i.regs[second_reg_operand] = tmp;
  820.       }
  821.  
  822.       if (t->opcode_modifier & ShortForm) {
  823.     /* The register or float register operand is in operand 0 or 1. */
  824.     uint o = (i.types[0] & (Reg|FloatReg)) ? 0 : 1;
  825.     /* Register goes in low 3 bits of opcode. */
  826.     t->base_opcode |= i.regs[o]->reg_num;
  827.       } else if (t->opcode_modifier & ShortFormW) {
  828.     /* Short form with 0x8 width bit.  Register is always dest. operand */
  829.     t->base_opcode |= i.regs[1]->reg_num;
  830.     if (i.suffix == WORD_OPCODE_SUFFIX ||
  831.         i.suffix == DWORD_OPCODE_SUFFIX)
  832.       t->base_opcode |= 0x8;
  833.       } else if (t->opcode_modifier & Seg2ShortForm) {
  834.     if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1) {
  835.       as_bad ("you can't 'pop cs' on the 386.");
  836.       return;
  837.     }
  838.     t->base_opcode |= (i.regs[0]->reg_num << 3);
  839.       } else if (t->opcode_modifier & Seg3ShortForm) {
  840.     /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
  841.        'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
  842.        So, only if i.regs[0]->reg_num == 5 (%gs) do we need
  843.        to change the opcode. */
  844.     if (i.regs[0]->reg_num == 5)
  845.       t->base_opcode |= 0x08;
  846.       } else if (t->opcode_modifier & Modrm) {
  847.     /* The opcode is completed (modulo t->extension_opcode which must
  848.        be put into the modrm byte.
  849.        Now, we make the modrm & index base bytes based on all the info
  850.        we've collected. */
  851.  
  852.     /* i.reg_operands MUST be the number of real register operands;
  853.        implicit registers do not count. */
  854.     if (i.reg_operands == 2) {
  855.       uint source, dest;
  856.       source = (i.types[0] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 0 : 1;
  857.       dest = source + 1;
  858.       i.rm.mode = 3;
  859.       /* We must be careful to make sure that all segment/control/test/
  860.          debug registers go into the i.rm.reg field (despite the whether
  861.          they are source or destination operands). */
  862.       if (i.regs[dest]->reg_type & (SReg2|SReg3|Control|Debug|Test)) {
  863.         i.rm.reg = i.regs[dest]->reg_num;
  864.         i.rm.regmem = i.regs[source]->reg_num;
  865.       } else {
  866.         i.rm.reg = i.regs[source]->reg_num;
  867.         i.rm.regmem = i.regs[dest]->reg_num;
  868.       }
  869.     } else {        /* if it's not 2 reg operands... */
  870.       if (i.mem_operands) {
  871.         uint fake_zero_displacement = FALSE;
  872.         uint o = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
  873.         
  874.         /* Encode memory operand into modrm byte and base index byte. */
  875.  
  876.         if (i.base_reg == esp && ! i.index_reg) {
  877.           /* <disp>(%esp) becomes two byte modrm with no index register. */
  878.           i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  879.           i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  880.           i.bi.base = ESP_REG_NUM;
  881.           i.bi.index = NO_INDEX_REGISTER;
  882.           i.bi.scale = 0;        /* Must be zero! */
  883.         } else if (i.base_reg == ebp && !i.index_reg) {
  884.           if (! (i.types[o] & Disp)) {
  885.         /* Must fake a zero byte displacement.
  886.            There is no direct way to code '(%ebp)' directly. */
  887.         fake_zero_displacement = TRUE;
  888.         /* fake_zero_displacement code does not set this. */
  889.         i.types[o] |= Disp8;
  890.           }
  891.           i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  892.           i.rm.regmem = EBP_REG_NUM;
  893.         } else if (! i.base_reg && (i.types[o] & BaseIndex)) {
  894.           /* There are three cases here.
  895.          Case 1:  '<32bit disp>(,1)' -- indirect absolute.
  896.          (Same as cases 2 & 3 with NO index register)
  897.          Case 2:  <32bit disp> (,<index>) -- no base register with disp
  898.          Case 3:  (, <index>)       --- no base register;
  899.          no disp (must add 32bit 0 disp). */
  900.           i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  901.           i.rm.mode = 0;        /* 32bit mode */
  902.           i.bi.base = NO_BASE_REGISTER;
  903.           i.types[o] &= ~Disp;
  904.           i.types[o] |= Disp32;    /* Must be 32bit! */
  905.           if (i.index_reg) {        /* case 2 or case 3 */
  906.         i.bi.index = i.index_reg->reg_num;
  907.         i.bi.scale = i.log2_scale_factor;
  908.         if (i.disp_operands == 0)
  909.           fake_zero_displacement = TRUE; /* case 3 */
  910.           } else {
  911.         i.bi.index = NO_INDEX_REGISTER;
  912.         i.bi.scale = 0;
  913.           }
  914.         } else if (i.disp_operands && !i.base_reg && !i.index_reg) {
  915.           /* Operand is just <32bit disp> */
  916.           i.rm.regmem = EBP_REG_NUM;
  917.           i.rm.mode = 0;
  918.           i.types[o] &= ~Disp;
  919.           i.types[o] |= Disp32;
  920.         } else {
  921.           /* It's not a special case; rev'em up. */
  922.           i.rm.regmem = i.base_reg->reg_num;
  923.           i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  924.           if (i.index_reg) {
  925.         i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  926.         i.bi.base = i.base_reg->reg_num;
  927.         i.bi.index = i.index_reg->reg_num;
  928.         i.bi.scale = i.log2_scale_factor;
  929.         if (i.base_reg == ebp && i.disp_operands == 0) { /* pace */
  930.           fake_zero_displacement = TRUE;
  931.           i.types[o] |= Disp8;
  932.           i.rm.mode = MODE_FROM_DISP_SIZE (i.types[o]);
  933.         }
  934.           }
  935.         }
  936.         if (fake_zero_displacement) {
  937.           /* Fakes a zero displacement assuming that i.types[o] holds
  938.          the correct displacement size. */
  939.           exp = &disp_expressions[i.disp_operands++];
  940.           i.disps[o] = exp;
  941.           exp->X_seg = SEG_ABSOLUTE;
  942.           exp->X_add_number = 0;
  943.           exp->X_add_symbol = (symbolS *) 0;
  944.           exp->X_subtract_symbol = (symbolS *) 0;
  945.         }
  946.  
  947.         /* Select the correct segment for the memory operand. */
  948.         if (i.seg) {
  949.           uint seg_index;
  950.           seg_entry * default_seg;
  951.  
  952.           if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING) {
  953.         seg_index = (i.rm.mode<<3) | i.bi.base;
  954.         default_seg = two_byte_segment_defaults [seg_index];
  955.           } else {
  956.         seg_index = (i.rm.mode<<3) | i.rm.regmem;
  957.         default_seg = one_byte_segment_defaults [seg_index];
  958.           }
  959.           /* If the specified segment is not the default, use an
  960.          opcode prefix to select it */
  961.           if (i.seg != default_seg) {
  962.         if (i.prefixes == MAX_PREFIXES) {
  963.           as_bad ("%d prefixes given and %s segment override gives too many prefixes",
  964.                MAX_PREFIXES, i.seg->seg_name);
  965.           return;
  966.         }
  967.         i.prefix[i.prefixes++] = i.seg->seg_prefix;
  968.           }
  969.         }
  970.       }
  971.  
  972.       /* Fill in i.rm.reg or i.rm.regmem field with register operand
  973.          (if any) based on t->extension_opcode. Again, we must be careful
  974.          to make sure that segment/control/debug/test registers are coded
  975.          into the i.rm.reg field. */
  976.       if (i.reg_operands) {
  977.         uint o =
  978.           (i.types[0] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 0 :
  979.         (i.types[1] & (Reg|SReg2|SReg3|Control|Debug|Test)) ? 1 : 2;
  980.         /* If there is an extension opcode to put here, the register number
  981.            must be put into the regmem field. */
  982.         if (t->extension_opcode != None)
  983.           i.rm.regmem = i.regs[o]->reg_num;
  984.         else i.rm.reg = i.regs[o]->reg_num;
  985.  
  986.         /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
  987.            we must set it to 3 to indicate this is a register operand
  988.            int the regmem field */
  989.         if (! i.mem_operands) i.rm.mode = 3;
  990.       }
  991.  
  992.       /* Fill in i.rm.reg field with extension opcode (if any). */
  993.       if (t->extension_opcode != None)
  994.         i.rm.reg = t->extension_opcode;
  995.     }
  996.       }
  997.     }
  998.   }
  999.  
  1000.   /* Handle conversion of 'int $3' --> special int3 insn. */
  1001.   if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3) {
  1002.     t->base_opcode = INT3_OPCODE;
  1003.     i.imm_operands = 0;
  1004.   }
  1005.  
  1006.   /* We are ready to output the insn. */
  1007.   {
  1008.     register char * p;
  1009.     
  1010.     /* Output jumps. */
  1011.     if (t->opcode_modifier & Jump) {
  1012.       int n = i.disps[0]->X_add_number;
  1013.       
  1014.       switch (i.disps[0]->X_seg) {
  1015.       case SEG_ABSOLUTE:
  1016.     if (FITS_IN_SIGNED_BYTE (n)) {
  1017.       p = frag_more (2);
  1018.       p[0] = t->base_opcode;
  1019.       p[1] = n;
  1020. #if 0 /* leave out 16 bit jumps - pace */
  1021.     } else if (FITS_IN_SIGNED_WORD (n)) {
  1022.       p = frag_more (4);
  1023.       p[0] = WORD_PREFIX_OPCODE;
  1024.       p[1] = t->base_opcode;
  1025.       md_number_to_chars (&p[2], n, 2);
  1026. #endif
  1027.     } else {        /* It's an absolute dword displacement. */
  1028.       if (t->base_opcode == JUMP_PC_RELATIVE) { /* pace */
  1029.         /* unconditional jump */
  1030.         p = frag_more (5);
  1031.         p[0] = 0xe9;
  1032.         md_number_to_chars (&p[1], n, 4);
  1033.       } else {
  1034.         /* conditional jump */
  1035.         p = frag_more (6);
  1036.         p[0] = TWO_BYTE_OPCODE_ESCAPE;
  1037.         p[1] = t->base_opcode + 0x10;
  1038.         md_number_to_chars (&p[2], n, 4);
  1039.       }
  1040.     }
  1041.     break;
  1042.       default:
  1043.     /* It's a symbol; end frag & setup for relax.
  1044.        Make sure there are 6 chars left in the current frag; if not
  1045.        we'll have to start a new one. */
  1046.     /* I caught it failing with obstack_room == 6,
  1047.        so I changed to <=   pace */
  1048.     if (obstack_room (&frags) <= 6) {
  1049.         frag_wane(frag_now);
  1050.         frag_new (0);
  1051.     }
  1052.     p = frag_more (1);
  1053.     p[0] = t->base_opcode;
  1054.     frag_var (rs_machine_dependent,
  1055.           6,        /* 2 opcode/prefix + 4 displacement */
  1056.           1,
  1057.           ((uchar) *p == JUMP_PC_RELATIVE
  1058.            ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
  1059.            : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
  1060.           i.disps[0]->X_add_symbol,
  1061.           n, p);
  1062.     break;
  1063.       }
  1064.     } else if (t->opcode_modifier & (JumpByte|JumpDword)) {
  1065.       int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
  1066.       int n = i.disps[0]->X_add_number;
  1067.       
  1068.       if (FITS_IN_UNSIGNED_BYTE(t->base_opcode)) {
  1069.     FRAG_APPEND_1_CHAR (t->base_opcode);
  1070.       } else {
  1071.     p = frag_more (2);    /* opcode can be at most two bytes */
  1072.     /* put out high byte first: can't use md_number_to_chars! */
  1073.     *p++ = (t->base_opcode >> 8) & 0xff;
  1074.     *p = t->base_opcode & 0xff;
  1075.       }
  1076.  
  1077.       p =  frag_more (size);
  1078.       switch (i.disps[0]->X_seg) {
  1079.       case SEG_ABSOLUTE:
  1080.     md_number_to_chars (p, n, size);
  1081.     if (size == 1 && ! FITS_IN_SIGNED_BYTE (n)) {
  1082.       as_bad ("loop/jecx only takes byte displacement; %d shortened to %d",
  1083.            n, *p);
  1084.     }
  1085.     break;
  1086.       default:
  1087.     fix_new (frag_now, p - frag_now->fr_literal, size,
  1088.          i.disps[0]->X_add_symbol, i.disps[0]->X_subtract_symbol,
  1089.          i.disps[0]->X_add_number, 1);
  1090.     break;
  1091.       }
  1092.     } else if (t->opcode_modifier & JumpInterSegment) {
  1093.       p =  frag_more (1 + 2 + 4);    /* 1 opcode; 2 segment; 4 offset */
  1094.       p[0] = t->base_opcode;
  1095.       if (i.imms[1]->X_seg == SEG_ABSOLUTE)
  1096.     md_number_to_chars (p + 1, i.imms[1]->X_add_number, 4);
  1097.       else
  1098.     fix_new (frag_now, p + 1 -  frag_now->fr_literal, 4,
  1099.          i.imms[1]->X_add_symbol,
  1100.          i.imms[1]->X_subtract_symbol,
  1101.          i.imms[1]->X_add_number, 0);
  1102.       if (i.imms[0]->X_seg != SEG_ABSOLUTE)
  1103.     as_bad ("can't handle non absolute segment in long call/jmp");
  1104.       md_number_to_chars (p + 5, i.imms[0]->X_add_number, 2);
  1105.     } else {
  1106.       /* Output normal instructions here. */
  1107.       register char *q;
  1108.       
  1109.       /* First the prefix bytes. */
  1110.       for (q = i.prefix; q < i.prefix + i.prefixes; q++) {
  1111.     p =  frag_more (1);
  1112.     md_number_to_chars (p, (uint) *q, 1);
  1113.       }
  1114.       
  1115.       /* Now the opcode; be careful about word order here! */
  1116.       if (FITS_IN_UNSIGNED_BYTE(t->base_opcode)) {
  1117.     FRAG_APPEND_1_CHAR (t->base_opcode);
  1118.       } else if (FITS_IN_UNSIGNED_WORD(t->base_opcode)) {
  1119.     p =  frag_more (2);
  1120.     /* put out high byte first: can't use md_number_to_chars! */
  1121.     *p++ = (t->base_opcode >> 8) & 0xff;
  1122.     *p = t->base_opcode & 0xff;
  1123.       } else {            /* opcode is either 3 or 4 bytes */
  1124.     if (t->base_opcode & 0xff000000) {
  1125.       p = frag_more (4);
  1126.       *p++ = (t->base_opcode >> 24) & 0xff;
  1127.     } else p = frag_more (3);
  1128.     *p++ = (t->base_opcode >> 16) & 0xff;
  1129.     *p++ = (t->base_opcode >>  8) & 0xff;
  1130.     *p =   (t->base_opcode      ) & 0xff;
  1131.       }
  1132.  
  1133.       /* Now the modrm byte and base index byte (if present). */
  1134.       if (t->opcode_modifier & Modrm) {
  1135.     p =  frag_more (1);
  1136.     /* md_number_to_chars (p, i.rm, 1); */
  1137.     md_number_to_chars (p, (i.rm.regmem<<0 | i.rm.reg<<3 | i.rm.mode<<6), 1);
  1138.     /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
  1139.        ==> need second modrm byte. */
  1140.     if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3) {
  1141.       p =  frag_more (1);
  1142.       /* md_number_to_chars (p, i.bi, 1); */
  1143.       md_number_to_chars (p,(i.bi.base<<0 | i.bi.index<<3 | i.bi.scale<<6), 1);
  1144.     }
  1145.       }
  1146.       
  1147.       if (i.disp_operands) {
  1148.     register int n;
  1149.     
  1150.     for (n = 0; n < i.operands; n++) {
  1151.       if (i.disps[n]) {
  1152.         if (i.disps[n]->X_seg == SEG_ABSOLUTE) {
  1153.           if (i.types[n] & (Disp8|Abs8)) {
  1154.         p =  frag_more (1);
  1155.         md_number_to_chars (p, i.disps[n]->X_add_number, 1);
  1156.           } else if (i.types[n] & (Disp16|Abs16)) {
  1157.         p =  frag_more (2);
  1158.         md_number_to_chars (p, i.disps[n]->X_add_number, 2);
  1159.           } else {        /* Disp32|Abs32 */
  1160.         p =  frag_more (4);
  1161.         md_number_to_chars (p, i.disps[n]->X_add_number, 4);
  1162.           }
  1163.         } else {            /* not SEG_ABSOLUTE */
  1164.           /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
  1165.           p =  frag_more (4);
  1166.           fix_new (frag_now, p -  frag_now->fr_literal, 4,
  1167.                i.disps[n]->X_add_symbol, i.disps[n]->X_subtract_symbol,
  1168.                i.disps[n]->X_add_number, 0);
  1169.         }
  1170.       }
  1171.     }
  1172.       }                /* end displacement output */
  1173.       
  1174.       /* output immediate */
  1175.       if (i.imm_operands) {
  1176.     register int n;
  1177.     
  1178.     for (n = 0; n < i.operands; n++) {
  1179.       if (i.imms[n]) {
  1180.         if (i.imms[n]->X_seg == SEG_ABSOLUTE) {
  1181.           if (i.types[n] & (Imm8|Imm8S)) {
  1182.         p =  frag_more (1);
  1183.         md_number_to_chars (p, i.imms[n]->X_add_number, 1);
  1184.           } else if (i.types[n] & Imm16) {
  1185.         p =  frag_more (2);
  1186.         md_number_to_chars (p, i.imms[n]->X_add_number, 2);
  1187.           } else {
  1188.         p =  frag_more (4);
  1189.         md_number_to_chars (p, i.imms[n]->X_add_number, 4);
  1190.           }
  1191.         } else {            /* not SEG_ABSOLUTE */
  1192.           /* need a 32-bit fixup (don't support 8bit non-absolute ims) */
  1193.           /* try to support other sizes ... */
  1194.           int size;
  1195.           if (i.types[n] & (Imm8|Imm8S))
  1196.         size = 1;
  1197.           else if (i.types[n] & Imm16)
  1198.         size = 2;
  1199.           else
  1200.         size = 4;
  1201.           p = frag_more (size);
  1202.           fix_new (frag_now, p - frag_now->fr_literal, size,
  1203.                i.imms[n]->X_add_symbol, i.imms[n]->X_subtract_symbol,
  1204.                i.imms[n]->X_add_number, 0);
  1205.         }
  1206.       }
  1207.     }
  1208.       }                /* end immediate output */
  1209.     }
  1210.  
  1211. #ifdef DEBUG386
  1212.     if (flagseen ['D']) {
  1213.       pi (line, &i);
  1214.     }
  1215. #endif /* DEBUG386 */
  1216.  
  1217.   }
  1218.   return;
  1219. }
  1220.  
  1221. /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
  1222.    on error. */
  1223.  
  1224. int i386_operand (operand_string)
  1225.      char *operand_string;
  1226. {
  1227.   register char *op_string = operand_string;
  1228.  
  1229.   /* Address of '\0' at end of operand_string. */
  1230.   char * end_of_operand_string = operand_string + strlen(operand_string);
  1231.  
  1232.   /* Start and end of displacement string expression (if found). */
  1233.   char * displacement_string_start = 0;
  1234.   char * displacement_string_end;
  1235.  
  1236.   /* We check for an absolute prefix (differentiating,
  1237.      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
  1238.   if (*op_string == ABSOLUTE_PREFIX) {
  1239.     op_string++;
  1240.     i.types[this_operand] |= JumpAbsolute;
  1241.   }
  1242.  
  1243.   /* Check if operand is a register. */
  1244.   if (*op_string == REGISTER_PREFIX) {
  1245.     register reg_entry * r;
  1246.     if (! (r = parse_register (op_string))) {
  1247.       as_bad ("bad register name ('%s')", op_string);
  1248.       return 0;
  1249.     }
  1250.     /* Check for segment override, rather than segment register by
  1251.        searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
  1252.     if ((r->reg_type & (SReg2|SReg3)) && op_string[3] == ':') {
  1253.       switch (r->reg_num) {
  1254.       case 0:
  1255.     i.seg = &es; break;
  1256.       case 1:
  1257.     i.seg = &cs; break;
  1258.       case 2:
  1259.     i.seg = &ss; break;
  1260.       case 3:
  1261.     i.seg = &ds; break;
  1262.       case 4:
  1263.     i.seg = &fs; break;
  1264.       case 5:
  1265.     i.seg = &gs; break;
  1266.       }
  1267.       op_string += 4;        /* skip % <x> s : */
  1268.       operand_string = op_string; /* Pretend given string starts here. */
  1269.       if (!is_digit_char(*op_string) && !is_identifier_char(*op_string)
  1270.       && *op_string != '(' && *op_string != ABSOLUTE_PREFIX) {
  1271.     as_bad ("bad memory operand after segment override");
  1272.     return 0;
  1273.       }
  1274.       /* Handle case of %es:*foo. */
  1275.       if (*op_string == ABSOLUTE_PREFIX) {
  1276.     op_string++;
  1277.     i.types[this_operand] |= JumpAbsolute;
  1278.       }
  1279.       goto do_memory_reference;
  1280.     }
  1281.     i.types[this_operand] |= r->reg_type;
  1282.     i.regs[this_operand] = r;
  1283.     i.reg_operands++;
  1284.   } else if (*op_string == IMMEDIATE_PREFIX) { /* ... or an immediate */
  1285.     char * save_input_line_pointer;
  1286.     register expressionS *exp;
  1287.     segT exp_seg;
  1288.     if (i.imm_operands == MAX_IMMEDIATE_OPERANDS) {
  1289.       as_bad ("only 1 or 2 immediate operands are allowed");
  1290.       return 0;
  1291.     }
  1292.     exp = &im_expressions[i.imm_operands++];
  1293.     i.imms [this_operand] = exp;
  1294.     save_input_line_pointer = input_line_pointer;
  1295.     input_line_pointer = ++op_string;        /* must advance op_string! */
  1296.     exp_seg = expression (exp);
  1297.     input_line_pointer = save_input_line_pointer;
  1298.     switch (exp_seg) {
  1299.     case SEG_NONE:    /* missing or bad expr becomes absolute 0 */
  1300.       as_bad ("missing or invalid immediate expression '%s' taken as 0",
  1301.            operand_string);
  1302.       exp->X_seg = SEG_ABSOLUTE;
  1303.       exp->X_add_number = 0;
  1304.       exp->X_add_symbol = (symbolS *) 0;
  1305.       exp->X_subtract_symbol = (symbolS *) 0;
  1306.       i.types[this_operand] |= Imm;
  1307.       break;
  1308.     case SEG_ABSOLUTE:
  1309.       i.types[this_operand] |= SMALLEST_IMM_TYPE (exp->X_add_number);
  1310.       break;
  1311.     case SEG_TEXT: case SEG_DATA: case SEG_BSS: case SEG_UNKNOWN:
  1312.       i.types[this_operand] |= Imm32; /* this is an address ==> 32bit */
  1313.       break;
  1314.     default:
  1315. seg_unimplemented:
  1316.       as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
  1317.       return 0;
  1318.     }
  1319.     /* shorten this type of this operand if the instruction wants
  1320.      * fewer bits than are present in the immediate.  The bit field
  1321.      * code can put out 'andb $0xffffff, %al', for example.   pace
  1322.      * also 'movw $foo,(%eax)'
  1323.      */
  1324.     switch (i.suffix) {
  1325.     case WORD_OPCODE_SUFFIX:
  1326.       i.types[this_operand] |= Imm16;
  1327.       break;
  1328.     case BYTE_OPCODE_SUFFIX:
  1329.       i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
  1330.       break;
  1331.     }
  1332.   } else if (is_digit_char(*op_string) || is_identifier_char(*op_string)
  1333.          || *op_string == '(') {
  1334.     /* This is a memory reference of some sort. */
  1335.     register char * base_string;
  1336.     uint found_base_index_form;
  1337.  
  1338.   do_memory_reference:
  1339.     if (i.mem_operands == MAX_MEMORY_OPERANDS) {
  1340.       as_bad ("more than 1 memory reference in instruction");
  1341.       return 0;
  1342.     }
  1343.     i.mem_operands++;
  1344.  
  1345.     /* Determine type of memory operand from opcode_suffix;
  1346.        no opcode suffix implies general memory references. */
  1347.     switch (i.suffix) {
  1348.     case BYTE_OPCODE_SUFFIX:
  1349.       i.types[this_operand] |= Mem8;
  1350.       break;
  1351.     case WORD_OPCODE_SUFFIX:
  1352.       i.types[this_operand] |= Mem16;
  1353.       break;
  1354.     case DWORD_OPCODE_SUFFIX:
  1355.     default:
  1356.       i.types[this_operand] |= Mem32;
  1357.     }
  1358.  
  1359.     /*  Check for base index form.  We detect the base index form by
  1360.     looking for an ')' at the end of the operand, searching
  1361.     for the '(' matching it, and finding a REGISTER_PREFIX or ','
  1362.     after it. */
  1363.     base_string = end_of_operand_string - 1;
  1364.     found_base_index_form = FALSE;
  1365.     if (*base_string == ')') {
  1366.       uint parens_balenced = 1;
  1367.       /* We've already checked that the number of left & right ()'s are equal,
  1368.      so this loop will not be infinite. */
  1369.       do {
  1370.     base_string--;
  1371.     if (*base_string == ')') parens_balenced++;
  1372.     if (*base_string == '(') parens_balenced--;
  1373.       } while (parens_balenced);
  1374.       base_string++;            /* Skip past '('. */
  1375.       if (*base_string == REGISTER_PREFIX || *base_string == ',')
  1376.     found_base_index_form = TRUE;
  1377.     }
  1378.  
  1379.     /* If we can't parse a base index register expression, we've found
  1380.        a pure displacement expression.  We set up displacement_string_start
  1381.        and displacement_string_end for the code below. */
  1382.     if (! found_base_index_form) {
  1383.     displacement_string_start = op_string;
  1384.     displacement_string_end = end_of_operand_string;
  1385.     } else {
  1386.       char *base_reg_name, *index_reg_name, *num_string;
  1387.       int num;
  1388.  
  1389.       i.types[this_operand] |= BaseIndex;
  1390.  
  1391.       /* If there is a displacement set-up for it to be parsed later. */
  1392.       if (base_string != op_string + 1) {
  1393.     displacement_string_start = op_string;
  1394.     displacement_string_end = base_string - 1;
  1395.       }
  1396.  
  1397.       /* Find base register (if any). */
  1398.       if (*base_string != ',') {
  1399.     base_reg_name = base_string++;
  1400.     /* skip past register name & parse it */
  1401.     while (isalpha(*base_string)) base_string++;
  1402.     if (base_string == base_reg_name+1) {
  1403.       as_bad ("can't find base register name after '(%c'",
  1404.            REGISTER_PREFIX);
  1405.       return 0;
  1406.     }
  1407.     END_STRING_AND_SAVE (base_string);
  1408.     if (! (i.base_reg = parse_register (base_reg_name))) {
  1409.       as_bad ("bad base register name ('%s')", base_reg_name);
  1410.       return 0;
  1411.     }
  1412.     RESTORE_END_STRING (base_string);
  1413.       }
  1414.  
  1415.       /* Now check seperator; must be ',' ==> index reg
  1416.      OR num ==> no index reg. just scale factor
  1417.      OR ')' ==> end. (scale factor = 1) */
  1418.       if (*base_string != ',' && *base_string != ')') {
  1419.     as_bad ("expecting ',' or ')' after base register in `%s'",
  1420.          operand_string);
  1421.     return 0;
  1422.       }
  1423.  
  1424.       /* There may index reg here; and there may be a scale factor. */
  1425.       if (*base_string == ',' && *(base_string+1) == REGISTER_PREFIX) {
  1426.     index_reg_name = ++base_string;
  1427.     while (isalpha(*++base_string));
  1428.     END_STRING_AND_SAVE (base_string);
  1429.     if (! (i.index_reg = parse_register(index_reg_name))) {
  1430.       as_bad ("bad index register name ('%s')", index_reg_name);
  1431.       return 0;
  1432.     }
  1433.     RESTORE_END_STRING (base_string);
  1434.       }
  1435.  
  1436.       /* Check for scale factor. */
  1437.       if (*base_string == ',' && isdigit(*(base_string+1))) {
  1438.     num_string = ++base_string;
  1439.     while (is_digit_char(*base_string)) base_string++;
  1440.     if (base_string == num_string) {
  1441.       as_bad ("can't find a scale factor after ','");
  1442.       return 0;
  1443.     }
  1444.     END_STRING_AND_SAVE (base_string);
  1445.     /* We've got a scale factor. */
  1446.     if (! sscanf (num_string, "%d", &num)) {
  1447.       as_bad ("can't parse scale factor from '%s'", num_string);
  1448.       return 0;
  1449.     }
  1450.     RESTORE_END_STRING (base_string);
  1451.     switch (num) {    /* must be 1 digit scale */
  1452.     case 1: i.log2_scale_factor = 0; break;
  1453.     case 2: i.log2_scale_factor = 1; break;
  1454.     case 4: i.log2_scale_factor = 2; break;
  1455.     case 8: i.log2_scale_factor = 3; break;
  1456.     default:
  1457.       as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
  1458.       return 0;
  1459.     }
  1460.       } else {
  1461.     if (! i.index_reg && *base_string == ',') {
  1462.       as_bad ("expecting index register or scale factor after ','; got '%c'",
  1463.            *(base_string+1));
  1464.       return 0;
  1465.     }
  1466.       }
  1467.     }
  1468.  
  1469.     /* If there's an expression begining the operand, parse it,
  1470.        assuming displacement_string_start and displacement_string_end
  1471.        are meaningful. */
  1472.     if (displacement_string_start) {
  1473.       register expressionS * exp;
  1474.       segT exp_seg;
  1475.       char * save_input_line_pointer;
  1476.       exp = &disp_expressions[i.disp_operands];
  1477.       i.disps [this_operand] = exp;
  1478.       i.disp_operands++;
  1479.       save_input_line_pointer = input_line_pointer;
  1480.       input_line_pointer = displacement_string_start;
  1481.       END_STRING_AND_SAVE (displacement_string_end);
  1482.       exp_seg = expression (exp);
  1483.       if(*input_line_pointer)
  1484.     as_bad("Ignoring junk '%s' after expression",input_line_pointer);
  1485.       RESTORE_END_STRING (displacement_string_end);
  1486.       input_line_pointer = save_input_line_pointer;
  1487.       switch (exp_seg) {
  1488.       case SEG_NONE:
  1489.     /* missing expr becomes absolute 0 */
  1490.     as_bad ("missing or invalid displacement '%s' taken as 0",
  1491.          operand_string);
  1492.     i.types[this_operand] |= (Disp|Abs);
  1493.     exp->X_seg = SEG_ABSOLUTE;
  1494.     exp->X_add_number = 0;
  1495.     exp->X_add_symbol = (symbolS *) 0;
  1496.     exp->X_subtract_symbol = (symbolS *) 0;
  1497.     break;
  1498.       case SEG_ABSOLUTE:
  1499.     i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
  1500.     break;
  1501.       case SEG_TEXT: case SEG_DATA: case SEG_BSS:
  1502.       case SEG_UNKNOWN:    /* must be 32 bit displacement (i.e. address) */
  1503.     i.types[this_operand] |= Disp32;
  1504.     break;
  1505.       default:
  1506.     goto seg_unimplemented;
  1507.       }
  1508.     }
  1509.  
  1510.     /* Make sure the memory operand we've been dealt is valid. */
  1511.     if (i.base_reg && i.index_reg &&
  1512.     ! (i.base_reg->reg_type & i.index_reg->reg_type & Reg)) {
  1513.       as_bad ("register size mismatch in (base,index,scale) expression");
  1514.       return 0;
  1515.     }
  1516.     if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
  1517.     (i.index_reg && (i.index_reg->reg_type & Reg32) == 0)) {
  1518.       as_bad ("base/index register must be 32 bit register");
  1519.       return 0;
  1520.     }
  1521.     if (i.index_reg && i.index_reg == esp) {
  1522.       as_bad ("%s may not be used as an index register", esp->reg_name);
  1523.       return 0;
  1524.     }
  1525.   } else {            /* it's not a memory operand; argh! */
  1526.     as_bad ("invalid char %s begining %s operand '%s'",
  1527.          output_invalid(*op_string), ordinal_names[this_operand],
  1528.          op_string);
  1529.     return 0;
  1530.   }
  1531.   return 1;            /* normal return */
  1532. }
  1533.  
  1534. /*
  1535.  *            md_estimate_size_before_relax()
  1536.  *
  1537.  * Called just before relax().
  1538.  * Any symbol that is now undefined will not become defined.
  1539.  * Return the correct fr_subtype in the frag.
  1540.  * Return the initial "guess for fr_var" to caller.
  1541.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  1542.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  1543.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  1544.  * 0 value.
  1545.  */
  1546. int
  1547. md_estimate_size_before_relax (fragP, segment_type)
  1548.      register fragS *    fragP;
  1549.      register int    segment_type; /* N_DATA or N_TEXT. */
  1550. {
  1551.   register uchar *    opcode;
  1552.   register int        old_fr_fix;
  1553.  
  1554.   old_fr_fix = fragP -> fr_fix;
  1555.   opcode = (uchar *) fragP -> fr_opcode;
  1556.   /* We've already got fragP->fr_subtype right;  all we have to do is check
  1557.      for un-relaxable symbols. */
  1558.   if ((fragP -> fr_symbol -> sy_type & N_TYPE) != segment_type) {
  1559.     /* symbol is undefined in this segment */
  1560.     switch (opcode[0]) {
  1561.     case JUMP_PC_RELATIVE:    /* make jmp (0xeb) a dword displacement jump */
  1562.       opcode[0] = 0xe9;        /* dword disp jmp */
  1563.       fragP -> fr_fix += 4;
  1564.       fix_new (fragP, old_fr_fix, 4,
  1565.            fragP -> fr_symbol,
  1566.            (symbolS *) 0,
  1567.            fragP -> fr_offset, 1);
  1568.       break;
  1569.  
  1570.     default:
  1571.       /* This changes the byte-displacement jump 0x7N -->
  1572.      the dword-displacement jump 0x0f8N */
  1573.       opcode[1] = opcode[0] + 0x10;
  1574.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;        /* two-byte escape */
  1575.       fragP -> fr_fix += 1 + 4;    /* we've added an opcode byte */
  1576.       fix_new (fragP, old_fr_fix + 1, 4,
  1577.            fragP -> fr_symbol,
  1578.            (symbolS *) 0,
  1579.            fragP -> fr_offset, 1);
  1580.       break;
  1581.     }
  1582.     frag_wane (fragP);
  1583.   }
  1584.   return (fragP -> fr_var + fragP -> fr_fix - old_fr_fix);
  1585. }                /* md_estimate_size_before_relax() */
  1586.  
  1587. /*
  1588.  *            md_convert_frag();
  1589.  *
  1590.  * Called after relax() is finished.
  1591.  * In:    Address of frag.
  1592.  *    fr_type == rs_machine_dependent.
  1593.  *    fr_subtype is what the address relaxed to.
  1594.  *
  1595.  * Out:    Any fixSs and constants are set up.
  1596.  *    Caller will turn frag into a ".space 0".
  1597.  */
  1598. void
  1599. md_convert_frag (fragP)
  1600.      register fragS *    fragP;
  1601. {
  1602.   register uchar * opcode;
  1603.   uchar * where_to_put_displacement;
  1604.   uint target_address, opcode_address;
  1605.   uint extension;
  1606.   int displacement_from_opcode_start;
  1607.  
  1608.   opcode = (uchar *) fragP -> fr_opcode;
  1609.  
  1610.   /* Address we want to reach in file space. */
  1611.   target_address = fragP->fr_symbol->sy_value + fragP->fr_offset;
  1612.  
  1613.   /* Address opcode resides at in file space. */
  1614.   opcode_address = fragP->fr_address + fragP->fr_fix;
  1615.  
  1616.   /* Displacement from opcode start to fill into instruction. */
  1617.   displacement_from_opcode_start = target_address - opcode_address;
  1618.  
  1619.   switch (fragP->fr_subtype) {
  1620.   case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
  1621.   case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
  1622.     /* don't have to change opcode */
  1623.     extension = 1;        /* 1 opcode + 1 displacement */
  1624.     where_to_put_displacement = &opcode[1];
  1625.     break;
  1626.  
  1627.   case ENCODE_RELAX_STATE (COND_JUMP, WORD):
  1628.     opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
  1629.     opcode[2] = opcode[0] + 0x10;
  1630.     opcode[0] = WORD_PREFIX_OPCODE;
  1631.     extension = 4;        /* 3 opcode + 2 displacement */
  1632.     where_to_put_displacement = &opcode[3];
  1633.     break;
  1634.  
  1635.   case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
  1636.     opcode[1] = 0xe9;
  1637.     opcode[0] = WORD_PREFIX_OPCODE;
  1638.     extension = 3;        /* 2 opcode + 2 displacement */
  1639.     where_to_put_displacement = &opcode[2];
  1640.     break;
  1641.  
  1642.   case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
  1643.     opcode[1] = opcode[0] + 0x10;
  1644.     opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
  1645.     extension = 5;        /* 2 opcode + 4 displacement */
  1646.     where_to_put_displacement = &opcode[2];
  1647.     break;
  1648.  
  1649.   case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
  1650.     opcode[0] = 0xe9;
  1651.     extension = 4;        /* 1 opcode + 4 displacement */
  1652.     where_to_put_displacement = &opcode[1];
  1653.     break;
  1654.  
  1655.   default:
  1656.     BAD_CASE(fragP -> fr_subtype);
  1657.     break;
  1658.   }
  1659.   /* now put displacement after opcode */
  1660.   md_number_to_chars (where_to_put_displacement,
  1661.               displacement_from_opcode_start - extension,
  1662.               SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
  1663.   fragP -> fr_fix += extension;
  1664. }
  1665.  
  1666.  
  1667. int md_short_jump_size = 2;    /* size of byte displacement jmp */
  1668. int md_long_jump_size  = 5;    /* size of dword displacement jmp */
  1669.  
  1670. void md_create_short_jump(ptr, from_addr, to_addr)
  1671.      char    *ptr;
  1672.      long    from_addr, to_addr;
  1673. {
  1674.   long offset;
  1675.  
  1676.   offset = to_addr - (from_addr + 2);
  1677.   md_number_to_chars (ptr, (long) 0xeb, 1); /* opcode for byte-disp jump */
  1678.   md_number_to_chars (ptr + 1, offset, 1);
  1679. }
  1680.  
  1681. void md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1682.      char    *ptr;
  1683.      long    from_addr, to_addr;
  1684.      fragS    *frag;
  1685.      symbolS    *to_symbol;
  1686. {
  1687.   long offset;
  1688.  
  1689.   if (flagseen['m']) {
  1690.     offset = to_addr - to_symbol->sy_value;
  1691.     md_number_to_chars (ptr, 0xe9, 1); /* opcode for long jmp */
  1692.     md_number_to_chars (ptr + 1, offset, 4);
  1693.     fix_new (frag, (ptr+1) - frag->fr_literal, 4,
  1694.          to_symbol, (symbolS *) 0, (long int) 0, 0);
  1695.   } else {
  1696.     offset = to_addr - (from_addr + 5);
  1697.     md_number_to_chars(ptr, (long) 0xe9, 1);
  1698.     md_number_to_chars(ptr + 1, offset, 4);
  1699.   }
  1700. }
  1701.  
  1702. int
  1703. md_parse_option(argP,cntP,vecP)
  1704. char **argP;
  1705. int *cntP;
  1706. char ***vecP;
  1707. {
  1708.     return 1;
  1709. }
  1710.  
  1711. void                /* Knows about order of bytes in address. */
  1712. md_number_to_chars (con, value, nbytes)
  1713.      char    con [];    /* Return 'nbytes' of chars here. */
  1714.      long int    value;        /* The value of the bits. */
  1715.      int    nbytes;        /* Number of bytes in the output. */
  1716. {
  1717.   register char * p = con;
  1718.  
  1719.   switch (nbytes) {
  1720.   case 1:
  1721.     p[0] = value & 0xff;
  1722.     break;
  1723.   case 2:
  1724.     p[0] = value & 0xff;
  1725.     p[1] = (value >> 8) & 0xff;
  1726.     break;
  1727.   case 4:
  1728.     p[0] = value & 0xff;
  1729.     p[1] = (value>>8) & 0xff;
  1730.     p[2] = (value>>16) & 0xff;
  1731.     p[3] = (value>>24) & 0xff;
  1732.     break;
  1733.   default:
  1734.     BAD_CASE (nbytes);
  1735.   }
  1736. }
  1737.  
  1738. void                /* Knows about order of bytes in address. */
  1739. md_number_to_disp (con, value, nbytes)
  1740.      char    con [];    /* Return 'nbytes' of chars here. */
  1741.      long int    value;        /* The value of the bits. */
  1742.      int    nbytes;        /* Number of bytes in the output. */
  1743. {
  1744.   char * answer = alloca (nbytes);
  1745.   register char * p = answer;
  1746.  
  1747.   switch (nbytes) {
  1748.   case 1:
  1749.     *p = value;
  1750.     break;
  1751.   case 2:
  1752.     *p++   = value;
  1753.     *p = (value>>8);
  1754.     break;
  1755.   case 4:
  1756.     *p++ = value;
  1757.     *p++ = (value>>8);
  1758.     *p++ = (value>>16);
  1759.     *p = (value>>24);
  1760.     break;
  1761.   default:
  1762.     BAD_CASE (nbytes);
  1763.   }
  1764.   bcopy (answer, con, nbytes);
  1765. }
  1766.  
  1767. void                /* Knows about order of bytes in address. */
  1768. md_number_to_imm (con, value, nbytes)
  1769.      char    con [];    /* Return 'nbytes' of chars here. */
  1770.      long int    value;        /* The value of the bits. */
  1771.      int    nbytes;        /* Number of bytes in the output. */
  1772. {
  1773.   char * answer = alloca (nbytes);
  1774.   register char * p = answer;
  1775.  
  1776.   switch (nbytes) {
  1777.   case 1:
  1778.     *p = value;
  1779.     break;
  1780.   case 2:
  1781.     *p++   = value;
  1782.     *p = (value>>8);
  1783.     break;
  1784.   case 4:
  1785.     *p++ = value;
  1786.     *p++ = (value>>8);
  1787.     *p++ = (value>>16);
  1788.     *p = (value>>24);
  1789.     break;
  1790.   default:
  1791.     BAD_CASE (nbytes);
  1792.   }
  1793.   bcopy (answer, con, nbytes);
  1794. }
  1795.  
  1796. void                /* Knows about order of bytes in address. */
  1797. md_number_to_field (con, value, nbytes)
  1798.      char    con [];    /* Return 'nbytes' of chars here. */
  1799.      long int    value;        /* The value of the bits. */
  1800.      int    nbytes;        /* Number of bytes in the output. */
  1801. {
  1802.   char * answer = alloca (nbytes);
  1803.   register char * p = answer;
  1804.  
  1805.   switch (nbytes) {
  1806.   case 1:
  1807.     *p = value;
  1808.     break;
  1809.   case 2:
  1810.     *p++   = value;
  1811.     *p = (value>>8);
  1812.     break;
  1813.   case 4:
  1814.     *p++ = value;
  1815.     *p++ = (value>>8);
  1816.     *p++ = (value>>16);
  1817.     *p = (value>>24);
  1818.     break;
  1819.   default:
  1820.     BAD_CASE (nbytes);
  1821.   }
  1822.   bcopy (answer, con, nbytes);
  1823. }
  1824.  
  1825. long int            /* Knows about the byte order in a word. */
  1826. md_chars_to_number (con, nbytes)
  1827. unsigned     char    con[];    /* Low order byte 1st. */
  1828.      int    nbytes;        /* Number of bytes in the input. */
  1829. {
  1830.   long int    retval;
  1831.   for (retval=0, con+=nbytes-1; nbytes--; con--)
  1832.     {
  1833.       retval <<= BITS_PER_CHAR;
  1834.       retval |= *con;
  1835.     }
  1836.   return retval;
  1837. }
  1838.  
  1839. void md_ri_to_chars(ri_p, ri)
  1840.      struct relocation_info *ri_p, ri;
  1841. {
  1842.   unsigned char the_bytes[8];
  1843.   
  1844.   /* this is easy */
  1845.   md_number_to_chars(the_bytes, ri.r_address, sizeof(ri.r_address));
  1846.   /* now the fun stuff */
  1847.   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
  1848.   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
  1849.   the_bytes[4] = ri.r_symbolnum & 0x0ff;
  1850.   the_bytes[7] = (((ri.r_extern << 3)  & 0x08) | ((ri.r_length << 1) & 0x06) | 
  1851.     ((ri.r_pcrel << 0)  & 0x01)) & 0x0F; 
  1852.   /* now put it back where you found it */
  1853.   bcopy (the_bytes, (char *)ri_p, sizeof(struct relocation_info));
  1854. }
  1855.  
  1856.  
  1857. #define MAX_LITTLENUMS 6
  1858.  
  1859. /* Turn the string pointed to by litP into a floating point constant of type
  1860.    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
  1861.    is stored in *sizeP .  An error message is returned, or NULL on OK.
  1862.  */
  1863. char *
  1864. md_atof(type,litP,sizeP)
  1865.      char type;
  1866.      char *litP;
  1867.      int *sizeP;
  1868. {
  1869.   int    prec;
  1870.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  1871.   LITTLENUM_TYPE *wordP;
  1872.   char    *t;
  1873.   char    *atof_ieee();
  1874.  
  1875.   switch(type) {
  1876.   case 'f':
  1877.   case 'F':
  1878.     prec = 2;
  1879.     break;
  1880.  
  1881.   case 'd':
  1882.   case 'D':
  1883.     prec = 4;
  1884.     break;
  1885.  
  1886.   case 'x':
  1887.   case 'X':
  1888.     prec = 5;
  1889.     break;
  1890.  
  1891.   default:
  1892.     *sizeP=0;
  1893.     return "Bad call to md_atof ()";
  1894.   }
  1895.   t = atof_ieee (input_line_pointer,type,words);
  1896.   if(t)
  1897.     input_line_pointer=t;
  1898.  
  1899.   *sizeP = prec * sizeof(LITTLENUM_TYPE);
  1900.   /* this loops outputs the LITTLENUMs in REVERSE order; in accord with
  1901.      the bigendian 386 */
  1902.   for(wordP = words + prec - 1;prec--;) {
  1903.     md_number_to_chars (litP, (long) (*wordP--), sizeof(LITTLENUM_TYPE));
  1904.     litP += sizeof(LITTLENUM_TYPE);
  1905.   }
  1906.   return "";    /* Someone should teach Dean about null pointers */
  1907. }
  1908.  
  1909. char output_invalid_buf[8];
  1910.  
  1911. char * output_invalid (c)
  1912.      char c;
  1913. {
  1914.   if (isprint(c)) sprintf (output_invalid_buf, "'%c'", c);
  1915.   else sprintf (output_invalid_buf, "(0x%x)", c);
  1916.   return output_invalid_buf;
  1917. }
  1918.  
  1919. reg_entry *parse_register (reg_string)
  1920.     char *reg_string;          /* reg_string starts *before* REGISTER_PREFIX */
  1921. {
  1922.   register char *s = reg_string;
  1923.   register char *p;
  1924.   char reg_name_given[MAX_REG_NAME_SIZE];
  1925.  
  1926.   s++;                /* skip REGISTER_PREFIX */
  1927.   for (p = reg_name_given; is_register_char (*s); p++, s++) {
  1928.     *p = register_chars [*s];
  1929.     if (p >= reg_name_given + MAX_REG_NAME_SIZE)
  1930.       return (reg_entry *) 0;
  1931.   }
  1932.   *p = '\0';
  1933.   return (reg_entry *) hash_find (reg_hash, reg_name_given);
  1934. }
  1935.  
  1936.