Back to index

cell-binutils  2.17cvs20070401
tc-vax.c
Go to the documentation of this file.
00001 /* tc-vax.c - vax-specific -
00002    Copyright 1987, 1991, 1992, 1993, 1994, 1995, 1998, 2000, 2001, 2002,
00003    2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005 
00006    This file is part of GAS, the GNU Assembler.
00007 
00008    GAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GAS; see the file COPYING.  If not, write to the Free
00020    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 #include "as.h"
00024 
00025 #include "vax-inst.h"
00026 #include "obstack.h"        /* For FRAG_APPEND_1_CHAR macro in "frags.h" */
00027 #include "subsegs.h"
00028 #include "safe-ctype.h"
00029 
00030 #ifdef OBJ_ELF
00031 #include "elf/vax.h"
00032 #endif
00033 
00034 /* These chars start a comment anywhere in a source file (except inside
00035    another comment */
00036 const char comment_chars[] = "#";
00037 
00038 /* These chars only start a comment at the beginning of a line.  */
00039 /* Note that for the VAX the are the same as comment_chars above.  */
00040 const char line_comment_chars[] = "#";
00041 
00042 const char line_separator_chars[] = ";";
00043 
00044 /* Chars that can be used to separate mant from exp in floating point nums.  */
00045 const char EXP_CHARS[] = "eE";
00046 
00047 /* Chars that mean this number is a floating point constant
00048    as in 0f123.456
00049    or    0H1.234E-12 (see exp chars above).  */
00050 const char FLT_CHARS[] = "dDfFgGhH";
00051 
00052 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
00053    changed in read.c .  Ideally it shouldn't have to know about it at all,
00054    but nothing is ideal around here.  */
00055 
00056 /* Hold details of an operand expression.  */
00057 static expressionS exp_of_operand[VIT_MAX_OPERANDS];
00058 static segT seg_of_operand[VIT_MAX_OPERANDS];
00059 
00060 /* A vax instruction after decoding.  */
00061 static struct vit v;
00062 
00063 /* Hold details of big operands.  */
00064 LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER];
00065 FLONUM_TYPE float_operand[VIT_MAX_OPERANDS];
00066 /* Above is made to point into big_operand_bits by md_begin().  */
00067 
00068 #ifdef OBJ_ELF
00069 #define GLOBAL_OFFSET_TABLE_NAME   "_GLOBAL_OFFSET_TABLE_"
00070 #define PROCEDURE_LINKAGE_TABLE_NAME      "_PROCEDURE_LINKAGE_TABLE_"
00071 symbolS *GOT_symbol;        /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
00072 symbolS *PLT_symbol;        /* Pre-defined "_PROCEDURE_LINKAGE_TABLE_".  */
00073 #endif
00074 
00075 int flag_hash_long_names;   /* -+ */
00076 int flag_one;               /* -1 */
00077 int flag_show_after_trunc;  /* -H */
00078 int flag_no_hash_mixed_case;       /* -h NUM */
00079 #ifdef OBJ_ELF
00080 int flag_want_pic;          /* -k */
00081 #endif
00082 
00083 /* For VAX, relative addresses of "just the right length" are easy.
00084    The branch displacement is always the last operand, even in
00085    synthetic instructions.
00086    For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
00087 
00088                   4       3       2       1       0          bit number
00089        ---/ /--+-------+-------+-------+-------+-------+
00090               |     what state ?   |  how long ? |
00091        ---/ /--+-------+-------+-------+-------+-------+
00092 
00093    The "how long" bits are 00=byte, 01=word, 10=long.
00094    This is a Un*x convention.
00095    Not all lengths are legit for a given value of (what state).
00096    The "how long" refers merely to the displacement length.
00097    The address usually has some constant bytes in it as well.
00098 
00099  groups for VAX address relaxing.
00100 
00101  1.    "foo" pc-relative.
00102  length of byte, word, long
00103 
00104  2a.   J<cond> where <cond> is a simple flag test.
00105  length of byte, word, long.
00106  VAX opcodes are:    (Hex)
00107  bneq/bnequ   12
00108  beql/beqlu   13
00109  bgtr         14
00110  bleq         15
00111  bgeq         18
00112  blss         19
00113  bgtru        1a
00114  blequ        1b
00115  bvc          1c
00116  bvs          1d
00117  bgequ/bcc    1e
00118  blssu/bcs    1f
00119  Always, you complement 0th bit to reverse condition.
00120  Always, 1-byte opcode, then 1-byte displacement.
00121 
00122  2b.   J<cond> where cond tests a memory bit.
00123  length of byte, word, long.
00124  Vax opcodes are:    (Hex)
00125  bbs          e0
00126  bbc          e1
00127  bbss         e2
00128  bbcs         e3
00129  bbsc         e4
00130  bbcc         e5
00131  Always, you complement 0th bit to reverse condition.
00132  Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
00133 
00134  2c.   J<cond> where cond tests low-order memory bit
00135  length of byte,word,long.
00136  Vax opcodes are:    (Hex)
00137  blbs         e8
00138  blbc         e9
00139  Always, you complement 0th bit to reverse condition.
00140  Always, 1-byte opcode, longword-address, 1-byte displacement.
00141 
00142  3.    Jbs/Jbr.
00143  length of byte,word,long.
00144  Vax opcodes are:    (Hex)
00145  bsbb         10
00146  brb          11
00147  These are like (2) but there is no condition to reverse.
00148  Always, 1 byte opcode, then displacement/absolute.
00149 
00150  4a.   JacbX
00151  length of word, long.
00152  Vax opcodes are:    (Hex)
00153  acbw         3d
00154  acbf         4f
00155  acbd         6f
00156  abcb         9d
00157  acbl         f1
00158  acbg        4ffd
00159  acbh        6ffd
00160  Always, we cannot reverse the sense of the branch; we have a word
00161  displacement.
00162  The double-byte op-codes don't hurt: we never want to modify the
00163  opcode, so we don't care how many bytes are between the opcode and
00164  the operand.
00165 
00166  4b.   JXobXXX
00167  length of long, long, byte.
00168  Vax opcodes are:    (Hex)
00169  aoblss              f2
00170  aobleq              f3
00171  sobgeq              f4
00172  sobgtr              f5
00173  Always, we cannot reverse the sense of the branch; we have a byte
00174  displacement.
00175 
00176  The only time we need to modify the opcode is for class 2 instructions.
00177  After relax() we may complement the lowest order bit of such instruction
00178  to reverse sense of branch.
00179 
00180  For class 2 instructions, we store context of "where is the opcode literal".
00181  We can change an opcode's lowest order bit without breaking anything else.
00182 
00183  We sometimes store context in the operand literal. This way we can figure out
00184  after relax() what the original addressing mode was.  */
00185 
00186 /* These displacements are relative to the start address of the
00187    displacement.  The first letter is Byte, Word.  2nd letter is
00188    Forward, Backward.  */
00189 #define BF (1+ 127)
00190 #define BB (1+-128)
00191 #define WF (2+ 32767)
00192 #define WB (2+-32768)
00193 /* Dont need LF, LB because they always reach. [They are coded as 0.]  */
00194 
00195 #define C(a,b) ENCODE_RELAX(a,b)
00196 /* This macro has no side-effects.  */
00197 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
00198 #define RELAX_STATE(s) ((s) >> 2)
00199 #define RELAX_LENGTH(s) ((s) & 3)
00200 
00201 const relax_typeS md_relax_table[] =
00202 {
00203   {1, 1, 0, 0},                    /* error sentinel   0,0     */
00204   {1, 1, 0, 0},                    /* unused         0,1       */
00205   {1, 1, 0, 0},                    /* unused         0,2       */
00206   {1, 1, 0, 0},                    /* unused         0,3       */
00207 
00208   {BF + 1, BB + 1, 2, C (1, 1)},/* B^"foo"           1,0 */
00209   {WF + 1, WB + 1, 3, C (1, 2)},/* W^"foo"           1,1 */
00210   {0, 0, 5, 0},                    /* L^"foo"        1,2 */
00211   {1, 1, 0, 0},                    /* unused         1,3 */
00212 
00213   {BF, BB, 1, C (2, 1)},    /* b<cond> B^"foo"  2,0 */
00214   {WF + 2, WB + 2, 4, C (2, 2)},/* br.+? brw X       2,1 */
00215   {0, 0, 7, 0},                    /* br.+? jmp X           2,2 */
00216   {1, 1, 0, 0},                    /* unused         2,3 */
00217 
00218   {BF, BB, 1, C (3, 1)},    /* brb B^foo      3,0 */
00219   {WF, WB, 2, C (3, 2)},    /* brw W^foo      3,1 */
00220   {0, 0, 5, 0},                    /* Jmp L^foo      3,2 */
00221   {1, 1, 0, 0},                    /* unused         3,3 */
00222 
00223   {1, 1, 0, 0},                    /* unused         4,0 */
00224   {WF, WB, 2, C (4, 2)},    /* acb_ ^Wfoo     4,1 */
00225   {0, 0, 10, 0},            /* acb_,br,jmp L^foo4,2 */
00226   {1, 1, 0, 0},                    /* unused         4,3 */
00227 
00228   {BF, BB, 1, C (5, 1)},    /* Xob___,,foo      5,0 */
00229   {WF + 4, WB + 4, 6, C (5, 2)},/* Xob.+2,brb.+3,brw5,1 */
00230   {0, 0, 9, 0},                    /* Xob.+2,brb.+6,jmp5,2 */
00231   {1, 1, 0, 0},                    /* unused         5,3 */
00232 };
00233 
00234 #undef C
00235 #undef BF
00236 #undef BB
00237 #undef WF
00238 #undef WB
00239 
00240 void float_cons (int);
00241 int flonum_gen2vax (char, FLONUM_TYPE *, LITTLENUM_TYPE *);
00242 
00243 const pseudo_typeS md_pseudo_table[] =
00244 {
00245   {"dfloat", float_cons, 'd'},
00246   {"ffloat", float_cons, 'f'},
00247   {"gfloat", float_cons, 'g'},
00248   {"hfloat", float_cons, 'h'},
00249   {"d_floating", float_cons, 'd'},
00250   {"f_floating", float_cons, 'f'},
00251   {"g_floating", float_cons, 'g'},
00252   {"h_floating", float_cons, 'h'},
00253   {NULL, NULL, 0},
00254 };
00255 
00256 #define STATE_PC_RELATIVE          (1)
00257 #define STATE_CONDITIONAL_BRANCH   (2)
00258 #define STATE_ALWAYS_BRANCH        (3)    /* includes BSB...  */
00259 #define STATE_COMPLEX_BRANCH               (4)
00260 #define STATE_COMPLEX_HOP          (5)
00261 
00262 #define STATE_BYTE                 (0)
00263 #define STATE_WORD                 (1)
00264 #define STATE_LONG                 (2)
00265 #define STATE_UNDF                 (3)    /* Symbol undefined in pass1.  */
00266 
00267 #define min(a, b)    ((a) < (b) ? (a) : (b))
00268 
00269 void
00270 md_number_to_chars (char con[], valueT value, int nbytes)
00271 {
00272   number_to_chars_littleendian (con, value, nbytes);
00273 }
00274 
00275 /* Fix up some data or instructions after we find out the value of a symbol
00276    that they reference.  */
00277 
00278 void                        /* Knows about order of bytes in address.  */
00279 md_apply_fix (fixS *fixP, valueT *valueP, segT seg ATTRIBUTE_UNUSED)
00280 {
00281   valueT value = * valueP;
00282 
00283   if (((fixP->fx_addsy == NULL && fixP->fx_subsy == NULL)
00284        && fixP->fx_r_type != BFD_RELOC_32_PLT_PCREL
00285        && fixP->fx_r_type != BFD_RELOC_32_GOT_PCREL)
00286       || fixP->fx_r_type == NO_RELOC)
00287     number_to_chars_littleendian (fixP->fx_where + fixP->fx_frag->fr_literal,
00288                               value, fixP->fx_size);
00289 
00290   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
00291     fixP->fx_done = 1;
00292 }
00293 
00294 /* Convert a number from VAX byte order (little endian)
00295    into host byte order.
00296    con        is the buffer to convert,
00297    nbytes     is the length of the given buffer.  */
00298 static long
00299 md_chars_to_number (unsigned char con[], int nbytes)
00300 {
00301   long retval;
00302 
00303   for (retval = 0, con += nbytes - 1; nbytes--; con--)
00304     {
00305       retval <<= BITS_PER_CHAR;
00306       retval |= *con;
00307     }
00308   return retval;
00309 }
00310 
00311 /* Copy a bignum from in to out.
00312    If the output is shorter than the input, copy lower-order
00313    littlenums.  Return 0 or the number of significant littlenums
00314    dropped.  Assumes littlenum arrays are densely packed: no unused
00315    chars between the littlenums. Uses memcpy() to move littlenums, and
00316    wants to know length (in chars) of the input bignum.  */
00317 
00318 static int
00319 bignum_copy (LITTLENUM_TYPE *in,
00320             int in_length,  /* in sizeof(littlenum)s */
00321             LITTLENUM_TYPE *out,
00322             int out_length  /* in sizeof(littlenum)s */)
00323 {
00324   int significant_littlenums_dropped;
00325 
00326   if (out_length < in_length)
00327     {
00328       LITTLENUM_TYPE *p;    /* -> most significant (non-zero) input
00329                                   littlenum.  */
00330 
00331       memcpy ((void *) out, (void *) in,
00332              (unsigned int) out_length << LITTLENUM_SHIFT);
00333       for (p = in + in_length - 1; p >= in; --p)
00334        {
00335          if (*p)
00336            break;
00337        }
00338       significant_littlenums_dropped = p - in - in_length + 1;
00339 
00340       if (significant_littlenums_dropped < 0)
00341        significant_littlenums_dropped = 0;
00342     }
00343   else
00344     {
00345       memcpy ((char *) out, (char *) in,
00346              (unsigned int) in_length << LITTLENUM_SHIFT);
00347 
00348       if (out_length > in_length)
00349        memset ((char *) (out + in_length), '\0',
00350               (unsigned int) (out_length - in_length) << LITTLENUM_SHIFT);
00351 
00352       significant_littlenums_dropped = 0;
00353     }
00354 
00355   return significant_littlenums_dropped;
00356 }
00357 
00358 /* md_estimate_size_before_relax(), called just before relax().
00359    Any symbol that is now undefined will not become defined.
00360    Return the correct fr_subtype in the frag and the growth beyond
00361    fr_fix.  */
00362 int
00363 md_estimate_size_before_relax (fragS *fragP, segT segment)
00364 {
00365   if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
00366     {
00367       if (S_GET_SEGMENT (fragP->fr_symbol) != segment
00368 #ifdef OBJ_ELF
00369          || S_IS_WEAK (fragP->fr_symbol)
00370          || S_IS_EXTERNAL (fragP->fr_symbol)
00371 #endif
00372          )
00373        {
00374          /* Non-relaxable cases.  */
00375          int reloc_type = NO_RELOC;
00376          char *p;
00377          int old_fr_fix;
00378 
00379          old_fr_fix = fragP->fr_fix;
00380          p = fragP->fr_literal + old_fr_fix;
00381 #ifdef OBJ_ELF
00382          /* If this is to an undefined symbol, then if it's an indirect
00383             reference indicate that is can mutated into a GLOB_DAT or
00384             JUMP_SLOT by the loader.  We restrict ourselves to no offset
00385             due to a limitation in the NetBSD linker.  */
00386 
00387          if (GOT_symbol == NULL)
00388            GOT_symbol = symbol_find (GLOBAL_OFFSET_TABLE_NAME);
00389          if (PLT_symbol == NULL)
00390            PLT_symbol = symbol_find (PROCEDURE_LINKAGE_TABLE_NAME);
00391          if ((GOT_symbol == NULL || fragP->fr_symbol != GOT_symbol)
00392              && (PLT_symbol == NULL || fragP->fr_symbol != PLT_symbol)
00393              && fragP->fr_symbol != NULL
00394              && flag_want_pic
00395              && (!S_IS_DEFINED (fragP->fr_symbol)
00396                  || S_IS_WEAK (fragP->fr_symbol)
00397                  || S_IS_EXTERNAL (fragP->fr_symbol)))
00398            {
00399              if (p[0] & 0x10)
00400               {
00401                 if (flag_want_pic)
00402                   as_fatal ("PIC reference to %s is indirect.\n",
00403                            S_GET_NAME (fragP->fr_symbol));
00404               }
00405              else
00406               {
00407                 if (((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLS
00408                     || ((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLG
00409                     || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JSB
00410                     || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JMP
00411                     || S_IS_FUNCTION (fragP->fr_symbol))
00412                   reloc_type = BFD_RELOC_32_PLT_PCREL;
00413                 else
00414                   reloc_type = BFD_RELOC_32_GOT_PCREL;
00415               }
00416            }
00417 #endif
00418          switch (RELAX_STATE (fragP->fr_subtype))
00419            {
00420            case STATE_PC_RELATIVE:
00421              p[0] |= VAX_PC_RELATIVE_MODE;       /* Preserve @ bit.  */
00422              fragP->fr_fix += 1 + 4;
00423              fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
00424                      fragP->fr_offset, 1, reloc_type);
00425              break;
00426 
00427            case STATE_CONDITIONAL_BRANCH:
00428              *fragP->fr_opcode ^= 1;             /* Reverse sense of branch.  */
00429              p[0] = 6;
00430              p[1] = VAX_JMP;
00431              p[2] = VAX_PC_RELATIVE_MODE; /* ...(PC) */
00432              fragP->fr_fix += 1 + 1 + 1 + 4;
00433              fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol,
00434                      fragP->fr_offset, 1, NO_RELOC);
00435              break;
00436 
00437            case STATE_COMPLEX_BRANCH:
00438              p[0] = 2;
00439              p[1] = 0;
00440              p[2] = VAX_BRB;
00441              p[3] = 6;
00442              p[4] = VAX_JMP;
00443              p[5] = VAX_PC_RELATIVE_MODE; /* ...(pc) */
00444              fragP->fr_fix += 2 + 2 + 1 + 1 + 4;
00445              fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol,
00446                      fragP->fr_offset, 1, NO_RELOC);
00447              break;
00448 
00449            case STATE_COMPLEX_HOP:
00450              p[0] = 2;
00451              p[1] = VAX_BRB;
00452              p[2] = 6;
00453              p[3] = VAX_JMP;
00454              p[4] = VAX_PC_RELATIVE_MODE; /* ...(pc) */
00455              fragP->fr_fix += 1 + 2 + 1 + 1 + 4;
00456              fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol,
00457                      fragP->fr_offset, 1, NO_RELOC);
00458              break;
00459 
00460            case STATE_ALWAYS_BRANCH:
00461              *fragP->fr_opcode += VAX_WIDEN_LONG;
00462              p[0] = VAX_PC_RELATIVE_MODE; /* ...(PC) */
00463              fragP->fr_fix += 1 + 4;
00464              fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
00465                      fragP->fr_offset, 1, NO_RELOC);
00466              break;
00467 
00468            default:
00469              abort ();
00470            }
00471          frag_wane (fragP);
00472 
00473          /* Return the growth in the fixed part of the frag.  */
00474          return fragP->fr_fix - old_fr_fix;
00475        }
00476 
00477       /* Relaxable cases.  Set up the initial guess for the variable
00478         part of the frag.  */
00479       switch (RELAX_STATE (fragP->fr_subtype))
00480        {
00481        case STATE_PC_RELATIVE:
00482          fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
00483          break;
00484        case STATE_CONDITIONAL_BRANCH:
00485          fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
00486          break;
00487        case STATE_COMPLEX_BRANCH:
00488          fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
00489          break;
00490        case STATE_COMPLEX_HOP:
00491          fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
00492          break;
00493        case STATE_ALWAYS_BRANCH:
00494          fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
00495          break;
00496        }
00497     }
00498 
00499   if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
00500     abort ();
00501 
00502   /* Return the size of the variable part of the frag.  */
00503   return md_relax_table[fragP->fr_subtype].rlx_length;
00504 }
00505 
00506 /* Called after relax() is finished.
00507    In: Address of frag.
00508        fr_type == rs_machine_dependent.
00509        fr_subtype is what the address relaxed to.
00510 
00511    Out:       Any fixSs and constants are set up.
00512        Caller will turn frag into a ".space 0".  */
00513 void
00514 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED,
00515                segT seg ATTRIBUTE_UNUSED,
00516                fragS *fragP)
00517 {
00518   char *addressP;           /* -> _var to change.  */
00519   char *opcodeP;            /* -> opcode char(s) to change.  */
00520   short int extension = 0;  /* Size of relaxed address.  */
00521   /* Added to fr_fix: incl. ALL var chars.  */
00522   symbolS *symbolP;
00523   long where;
00524 
00525   know (fragP->fr_type == rs_machine_dependent);
00526   where = fragP->fr_fix;
00527   addressP = fragP->fr_literal + where;
00528   opcodeP = fragP->fr_opcode;
00529   symbolP = fragP->fr_symbol;
00530   know (symbolP);
00531 
00532   switch (fragP->fr_subtype)
00533     {
00534     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
00535       know (*addressP == 0 || *addressP == 0x10);       /* '@' bit.  */
00536       addressP[0] |= 0xAF;  /* Byte displacement. */
00537       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
00538               fragP->fr_offset, 1, NO_RELOC);
00539       extension = 2;
00540       break;
00541 
00542     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
00543       know (*addressP == 0 || *addressP == 0x10);       /* '@' bit.  */
00544       addressP[0] |= 0xCF;  /* Word displacement. */
00545       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
00546               fragP->fr_offset, 1, NO_RELOC);
00547       extension = 3;
00548       break;
00549 
00550     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
00551       know (*addressP == 0 || *addressP == 0x10);       /* '@' bit.  */
00552       addressP[0] |= 0xEF;  /* Long word displacement. */
00553       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
00554               fragP->fr_offset, 1, NO_RELOC);
00555       extension = 5;
00556       break;
00557 
00558     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
00559       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
00560               fragP->fr_offset, 1, NO_RELOC);
00561       extension = 1;
00562       break;
00563 
00564     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
00565       opcodeP[0] ^= 1;             /* Reverse sense of test.  */
00566       addressP[0] = 3;
00567       addressP[1] = VAX_BRW;
00568       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
00569               fragP->fr_offset, 1, NO_RELOC);
00570       extension = 4;
00571       break;
00572 
00573     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
00574       opcodeP[0] ^= 1;             /* Reverse sense of test.  */
00575       addressP[0] = 6;
00576       addressP[1] = VAX_JMP;
00577       addressP[2] = VAX_PC_RELATIVE_MODE;
00578       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
00579               fragP->fr_offset, 1, NO_RELOC);
00580       extension = 7;
00581       break;
00582 
00583     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
00584       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
00585               fragP->fr_offset, 1, NO_RELOC);
00586       extension = 1;
00587       break;
00588 
00589     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
00590       opcodeP[0] += VAX_WIDEN_WORD;       /* brb -> brw, bsbb -> bsbw */
00591       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
00592               1, NO_RELOC);
00593       extension = 2;
00594       break;
00595 
00596     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
00597       opcodeP[0] += VAX_WIDEN_LONG;       /* brb -> jmp, bsbb -> jsb */
00598       addressP[0] = VAX_PC_RELATIVE_MODE;
00599       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
00600               fragP->fr_offset, 1, NO_RELOC);
00601       extension = 5;
00602       break;
00603 
00604     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
00605       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
00606               fragP->fr_offset, 1, NO_RELOC);
00607       extension = 2;
00608       break;
00609 
00610     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
00611       addressP[0] = 2;
00612       addressP[1] = 0;
00613       addressP[2] = VAX_BRB;
00614       addressP[3] = 6;
00615       addressP[4] = VAX_JMP;
00616       addressP[5] = VAX_PC_RELATIVE_MODE;
00617       fix_new (fragP, fragP->fr_fix + 6, 4, fragP->fr_symbol,
00618               fragP->fr_offset, 1, NO_RELOC);
00619       extension = 10;
00620       break;
00621 
00622     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
00623       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
00624               fragP->fr_offset, 1, NO_RELOC);
00625       extension = 1;
00626       break;
00627 
00628     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
00629       addressP[0] = 2;
00630       addressP[1] = VAX_BRB;
00631       addressP[2] = 3;
00632       addressP[3] = VAX_BRW;
00633       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
00634               fragP->fr_offset, 1, NO_RELOC);
00635       extension = 6;
00636       break;
00637 
00638     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
00639       addressP[0] = 2;
00640       addressP[1] = VAX_BRB;
00641       addressP[2] = 6;
00642       addressP[3] = VAX_JMP;
00643       addressP[4] = VAX_PC_RELATIVE_MODE;
00644       fix_new (fragP, fragP->fr_fix + 5, 4, fragP->fr_symbol,
00645               fragP->fr_offset, 1, NO_RELOC);
00646       extension = 9;
00647       break;
00648 
00649     default:
00650       BAD_CASE (fragP->fr_subtype);
00651       break;
00652     }
00653   fragP->fr_fix += extension;
00654 }
00655 
00656 /* Translate internal format of relocation info into target format.
00657 
00658    On vax: first 4 bytes are normal unsigned long, next three bytes
00659    are symbolnum, least sig. byte first.  Last byte is broken up with
00660    the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
00661    bit 0 as pcrel.  */
00662 #ifdef comment
00663 void
00664 md_ri_to_chars (char *the_bytes, struct reloc_info_generic ri)
00665 {
00666   /* This is easy.  */
00667   md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
00668   /* Now the fun stuff.  */
00669   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
00670   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
00671   the_bytes[4] = ri.r_symbolnum & 0x0ff;
00672   the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06)
00673                 | ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
00674 }
00675 
00676 #endif /* comment */
00677 
00678 /*       BUGS, GRIPES,  APOLOGIA, etc.
00679 
00680    The opcode table 'votstrs' needs to be sorted on opcode frequency.
00681    That is, AFTER we hash it with hash_...(), we want most-used opcodes
00682    to come out of the hash table faster.
00683 
00684    I am sorry to inflict yet another VAX assembler on the world, but
00685    RMS says we must do everything from scratch, to prevent pin-heads
00686    restricting this software.
00687 
00688    This is a vaguely modular set of routines in C to parse VAX
00689    assembly code using DEC mnemonics. It is NOT un*x specific.
00690 
00691    The idea here is that the assembler has taken care of all:
00692      labels
00693      macros
00694      listing
00695      pseudo-ops
00696      line continuation
00697      comments
00698      condensing any whitespace down to exactly one space
00699    and all we have to do is parse 1 line into a vax instruction
00700    partially formed. We will accept a line, and deliver:
00701      an error message (hopefully empty)
00702      a skeleton VAX instruction (tree structure)
00703      textual pointers to all the operand expressions
00704      a warning message that notes a silly operand (hopefully empty)
00705 
00706               E D I T   H I S T O R Y
00707 
00708    17may86 Dean Elsner. Bug if line ends immediately after opcode.
00709    30apr86 Dean Elsner. New vip_op() uses arg block so change call.
00710     6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
00711     2jan86 Dean Elsner. Invent synthetic opcodes.
00712        Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
00713        which means this is not a real opcode, it is like a macro; it will
00714        be relax()ed into 1 or more instructions.
00715        Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
00716        like a regular branch instruction. Option added to vip_begin():
00717        exclude       synthetic opcodes. Invent synthetic_votstrs[].
00718    31dec85 Dean Elsner. Invent vit_opcode_nbytes.
00719        Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
00720        so caller's don't have to know the difference between a 1-byte & a
00721        2-byte op-code. Still need vax_opcodeT concept, so we know how
00722        big an object must be to hold an op.code.
00723    30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
00724        because vax opcodes may be 16 bits. Our crufty C compiler was
00725        happily initialising 8-bit vot_codes with 16-bit numbers!
00726        (Wouldn't the 'phone company like to compress data so easily!)
00727    29dec85 Dean Elsner. New static table vax_operand_width_size[].
00728        Invented so we know hw many bytes a "I^#42" needs in its immediate
00729        operand. Revised struct vop in "vax-inst.h": explicitly include
00730        byte length of each operand, and it's letter-code datum type.
00731    17nov85 Dean Elsner. Name Change.
00732        Due to ar(1) truncating names, we learned the hard way that
00733        "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
00734        the archived object name. SO... we shortened the name of this
00735        source file, and changed the makefile.  */
00736 
00737 /* Handle of the OPCODE hash table.  */
00738 static struct hash_control *op_hash;
00739 
00740 /* In: 1 character, from "bdfghloqpw" being the data-type of an operand
00741        of a vax instruction.
00742 
00743    Out:       the length of an operand of that type, in bytes.
00744        Special branch operands types "-?!" have length 0.  */
00745 
00746 static const short int vax_operand_width_size[256] =
00747 {
00748   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00749   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00750   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00751   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00752   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,      /* ..b.d.fgh...l..o  */
00753   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,      /* .q.....w........  */
00754   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,      /* ..b.d.fgh...l..o  */
00755   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,      /* .q.....w........  */
00756   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00757   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00758   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00759   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00760   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00761   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00762   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00763   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
00764 };
00765 
00766 /* This perversion encodes all the vax opcodes as a bunch of strings.
00767    RMS says we should build our hash-table at run-time. Hmm.
00768    Please would someone arrange these in decreasing frequency of opcode?
00769    Because of the way hash_...() works, the most frequently used opcode
00770    should be textually first and so on.
00771 
00772    Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
00773    So change 'vax.opcodes', then re-generate this table.  */
00774 
00775 #include "opcode/vax.h"
00776 
00777 /* This is a table of optional op-codes. All of them represent
00778    'synthetic' instructions that seem popular.
00779 
00780    Here we make some pseudo op-codes. Every code has a bit set to say
00781    it is synthetic. This lets you catch them if you want to
00782    ban these opcodes. They are mnemonics for "elastic" instructions
00783    that are supposed to assemble into the fewest bytes needed to do a
00784    branch, or to do a conditional branch, or whatever.
00785   
00786    The opcode is in the usual place [low-order n*8 bits]. This means
00787    that if you mask off the bucky bits, the usual rules apply about
00788    how long the opcode is.
00789   
00790    All VAX branch displacements come at the end of the instruction.
00791    For simple branches (1-byte opcode + 1-byte displacement) the last
00792    operand is coded 'b?' where the "data type" '?' is a clue that we
00793    may reverse the sense of the branch (complement lowest order bit)
00794    and branch around a jump. This is by far the most common case.
00795    That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
00796    a 0-byte op-code followed by 2 or more bytes of operand address.
00797   
00798    If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
00799    case.
00800   
00801    For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
00802    option before (2) we can directly JSB/JMP because there is no condition.
00803    These operands have 'b-' as their access/data type.
00804   
00805    That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
00806    cases, we do the same idea. JACBxxx are all marked with a 'b!'
00807    JAOBxxx & JSOBxxx are marked with a 'b:'.  */
00808 #if (VIT_OPCODE_SYNTHETIC != 0x80000000)
00809 #error "You have just broken the encoding below, which assumes the sign bit means 'I am an imaginary instruction'."
00810 #endif
00811 
00812 #if (VIT_OPCODE_SPECIAL != 0x40000000)
00813 #error "You have just broken the encoding below, which assumes the 0x40 M bit means 'I am not to be "optimised" the way normal branches are'."
00814 #endif
00815 
00816 static const struct vot
00817   synthetic_votstrs[] =
00818 {
00819   {"jbsb",    {"b-", 0xC0000010}},        /* BSD 4.2 */
00820 /* jsb used already */
00821   {"jbr",     {"b-", 0xC0000011}},        /* BSD 4.2 */
00822   {"jr",      {"b-", 0xC0000011}},        /* consistent */
00823   {"jneq",    {"b?", 0x80000012}},
00824   {"jnequ",   {"b?", 0x80000012}},
00825   {"jeql",    {"b?", 0x80000013}},
00826   {"jeqlu",   {"b?", 0x80000013}},
00827   {"jgtr",    {"b?", 0x80000014}},
00828   {"jleq",    {"b?", 0x80000015}},
00829 /* un-used opcodes here */
00830   {"jgeq",    {"b?", 0x80000018}},
00831   {"jlss",    {"b?", 0x80000019}},
00832   {"jgtru",   {"b?", 0x8000001a}},
00833   {"jlequ",   {"b?", 0x8000001b}},
00834   {"jvc",     {"b?", 0x8000001c}},
00835   {"jvs",     {"b?", 0x8000001d}},
00836   {"jgequ",   {"b?", 0x8000001e}},
00837   {"jcc",     {"b?", 0x8000001e}},
00838   {"jlssu",   {"b?", 0x8000001f}},
00839   {"jcs",     {"b?", 0x8000001f}},
00840 
00841   {"jacbw",   {"rwrwmwb!", 0xC000003d}},
00842   {"jacbf",   {"rfrfmfb!", 0xC000004f}},
00843   {"jacbd",   {"rdrdmdb!", 0xC000006f}},
00844   {"jacbb",   {"rbrbmbb!", 0xC000009d}},
00845   {"jacbl",   {"rlrlmlb!", 0xC00000f1}},
00846   {"jacbg",   {"rgrgmgb!", 0xC0004ffd}},
00847   {"jacbh",   {"rhrhmhb!", 0xC0006ffd}},
00848 
00849   {"jbs",     {"rlvbb?", 0x800000e0}},
00850   {"jbc",     {"rlvbb?", 0x800000e1}},
00851   {"jbss",    {"rlvbb?", 0x800000e2}},
00852   {"jbcs",    {"rlvbb?", 0x800000e3}},
00853   {"jbsc",    {"rlvbb?", 0x800000e4}},
00854   {"jbcc",    {"rlvbb?", 0x800000e5}},
00855   {"jlbs",    {"rlb?", 0x800000e8}},
00856   {"jlbc",    {"rlb?", 0x800000e9}},
00857 
00858   {"jaoblss", {"rlmlb:", 0xC00000f2}},
00859   {"jaobleq", {"rlmlb:", 0xC00000f3}},
00860   {"jsobgeq", {"mlb:", 0xC00000f4}},
00861   {"jsobgtr", {"mlb:", 0xC00000f5}},
00862 
00863 /* CASEx has no branch addresses in our conception of it.  */
00864 /* You should use ".word ..." statements after the "case ...".  */
00865 
00866   {"",        {"", 0}}      /* Empty is end sentinel.  */
00867 };
00868 
00869 /* Because this module is useful for both VMS and UN*X style assemblers
00870    and because of the variety of UN*X assemblers we must recognise
00871    the different conventions for assembler operand notation. For example
00872    VMS says "#42" for immediate mode, while most UN*X say "$42".
00873    We permit arbitrary sets of (single) characters to represent the
00874    3 concepts that DEC writes '#', '@', '^'.  */
00875 
00876 /* Character tests.  */
00877 #define VIP_IMMEDIATE 01    /* Character is like DEC # */
00878 #define VIP_INDIRECT  02    /* Char is like DEC @ */
00879 #define VIP_DISPLEN   04    /* Char is like DEC ^ */
00880 
00881 #define IMMEDIATEP(c)       (vip_metacharacters [(c) & 0xff] & VIP_IMMEDIATE)
00882 #define INDIRECTP(c) (vip_metacharacters [(c) & 0xff] & VIP_INDIRECT)
00883 #define DISPLENP(c)  (vip_metacharacters [(c) & 0xff] & VIP_DISPLEN)
00884 
00885 /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
00886    are ever called.  */
00887 
00888 #if defined(CONST_TABLE)
00889 #define _ 0,
00890 #define I VIP_IMMEDIATE,
00891 #define S VIP_INDIRECT,
00892 #define D VIP_DISPLEN,
00893 static const char
00894 vip_metacharacters[256] =
00895 {
00896   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /* ^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/
00897   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /* ^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
00898   _ _ _ _ I _ _ _ _ _ S _ _ _ _ _  /* sp !  "  #  $  %  & '  (  )  *  +  ,  -  .  / */
00899   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /*0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?*/
00900   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /*@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O*/
00901   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /*P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _*/
00902   D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /*`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o*/
00903   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  /*p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  ^?*/
00904 
00905   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00906   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00907   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00908   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00909   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00910   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00911   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00912   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00913 };
00914 #undef _
00915 #undef I
00916 #undef S
00917 #undef D
00918 
00919 #else
00920 
00921 static char vip_metacharacters[256];
00922 
00923 static void
00924 vip_op_1 (int bit, const char *syms)
00925 {
00926   unsigned char t;
00927 
00928   while ((t = *syms++) != 0)
00929     vip_metacharacters[t] |= bit;
00930 }
00931 
00932 /* Can be called any time.  More arguments may appear in future.  */
00933 static void
00934 vip_op_defaults (const char *immediate, const char *indirect, const char *displen)
00935 {
00936   vip_op_1 (VIP_IMMEDIATE, immediate);
00937   vip_op_1 (VIP_INDIRECT, indirect);
00938   vip_op_1 (VIP_DISPLEN, displen);
00939 }
00940 
00941 #endif
00942 
00943 /* Call me once before you decode any lines.
00944    I decode votstrs into a hash table at op_hash (which I create).
00945    I return an error text or null.
00946    If you want, I will include the 'synthetic' jXXX instructions in the
00947    instruction table.
00948    You must nominate metacharacters for eg DEC's "#", "@", "^".  */
00949 
00950 static const char *
00951 vip_begin (int synthetic_too,             /* 1 means include jXXX op-codes.  */
00952           const char *immediate,
00953           const char *indirect,
00954           const char *displen)
00955 {
00956   const struct vot *vP;            /* scan votstrs */
00957   const char *retval = 0;   /* error text */
00958 
00959   op_hash = hash_new ();
00960 
00961   for (vP = votstrs; *vP->vot_name && !retval; vP++)
00962     retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);
00963 
00964   if (synthetic_too)
00965     for (vP = synthetic_votstrs; *vP->vot_name && !retval; vP++)
00966       retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);
00967 
00968 #ifndef CONST_TABLE
00969   vip_op_defaults (immediate, indirect, displen);
00970 #endif
00971 
00972   return retval;
00973 }
00974 
00975 /* Take 3 char.s, the last of which may be `\0` (non-existent)
00976    and return the VAX register number that they represent.
00977   
00978    Return -1 if they don't form a register name. Good names return
00979    a number from 0:15 inclusive.
00980   
00981    Case is not important in a name.
00982   
00983    Register names understood are:
00984   
00985        R0
00986        R1
00987        R2
00988        R3
00989        R4
00990        R5
00991        R6
00992        R7
00993        R8
00994        R9
00995        R10
00996        R11
00997        R12    AP
00998        R13    FP
00999        R14    SP
01000        R15    PC  */
01001 
01002 #define AP 12
01003 #define FP 13
01004 #define SP 14
01005 #define PC 15
01006 
01007 /* Returns the register number of something like '%r15' or 'ap', supplied
01008    in four single chars. Returns -1 if the register isn't recognized,
01009    0..15 otherwise.  */
01010 static int
01011 vax_reg_parse (char c1, char c2, char c3, char c4)
01012 {
01013   int retval = -1;
01014 
01015 #ifdef OBJ_ELF
01016   if (c1 != '%')     /* Register prefixes are mandatory for ELF.  */
01017     return retval;
01018   c1 = c2;
01019   c2 = c3;
01020   c3 = c4;
01021 #endif
01022 #ifdef OBJ_VMS
01023   if (c4 != 0)              /* Register prefixes are not allowed under VMS.  */
01024     return retval;
01025 #endif
01026 #ifdef OBJ_AOUT
01027   if (c1 == '%')     /* Register prefixes are optional under a.out.  */
01028     {
01029       c1 = c2;
01030       c2 = c3;
01031       c3 = c4;
01032     }
01033   else if (c3 && c4) /* Can't be 4 characters long.  */
01034     return retval;
01035 #endif
01036 
01037   c1 = TOLOWER (c1);
01038   c2 = TOLOWER (c2);
01039   if (ISDIGIT (c2) && c1 == 'r')
01040     {
01041       retval = c2 - '0';
01042       if (ISDIGIT (c3))
01043        {
01044          retval = retval * 10 + c3 - '0';
01045          retval = (retval > 15) ? -1 : retval;
01046          /* clamp the register value to 1 hex digit */
01047        }
01048       else if (c3)
01049        retval = -1;         /* c3 must be '\0' or a digit.  */
01050     }
01051   else if (c3)                     /* There are no three letter regs.  */
01052     retval = -1;
01053   else if (c2 == 'p')
01054     {
01055       switch (c1)
01056        {
01057        case 's':
01058          retval = SP;
01059          break;
01060        case 'f':
01061          retval = FP;
01062          break;
01063        case 'a':
01064          retval = AP;
01065          break;
01066        default:
01067          retval = -1;
01068        }
01069     }
01070   else if (c1 == 'p' && c2 == 'c')
01071     retval = PC;
01072   else
01073     retval = -1;
01074   return retval;
01075 }
01076 
01077 /* Parse a vax operand in DEC assembler notation.
01078    For speed, expect a string of whitespace to be reduced to a single ' '.
01079    This is the case for GNU AS, and is easy for other DEC-compatible
01080    assemblers.
01081   
01082    Knowledge about DEC VAX assembler operand notation lives here.
01083    This doesn't even know what a register name is, except it believes
01084    all register names are 2 or 3 characters, and lets vax_reg_parse() say
01085    what number each name represents.
01086    It does, however, know that PC, SP etc are special registers so it can
01087    detect addressing modes that are silly for those registers.
01088   
01089    Where possible, it delivers 1 fatal or 1 warning message if the operand
01090    is suspect. Exactly what we test for is still evolving.
01091 
01092    ---
01093        Arg block.
01094   
01095    There were a number of 'mismatched argument type' bugs to vip_op.
01096    The most general solution is to typedef each (of many) arguments.
01097    We used instead a typedef'd argument block. This is less modular
01098    than using separate return pointers for each result, but runs faster
01099    on most engines, and seems to keep programmers happy. It will have
01100    to be done properly if we ever want to use vip_op as a general-purpose
01101    module (it was designed to be).
01102   
01103        G^
01104 
01105    Doesn't support DEC "G^" format operands. These always take 5 bytes
01106    to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
01107    optimising to (say) a "B^" if you are lucky in the way you link.
01108    When someone builds a linker smart enough to convert "G^" to "B^", "W^"
01109    whenever possible, then we should implement it.
01110    If there is some other use for "G^", feel free to code it in!
01111 
01112        speed
01113   
01114    If I nested if()s more, I could avoid testing (*err) which would save
01115    time, space and page faults. I didn't nest all those if()s for clarity
01116    and because I think the mode testing can be re-arranged 1st to test the
01117    commoner constructs 1st. Does anybody have statistics on this?  
01118   
01119        error messages
01120   
01121    In future, we should be able to 'compose' error messages in a scratch area
01122    and give the user MUCH more informative error messages. Although this takes
01123    a little more code at run-time, it will make this module much more self-
01124    documenting. As an example of what sucks now: most error messages have
01125    hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
01126    the Un*x characters "$`*", that most users will expect from this AS.
01127 
01128    ----
01129    
01130    The input is a string, ending with '\0'.
01131   
01132    We also require a 'hint' of what kind of operand is expected: so
01133    we can remind caller not to write into literals for instance.
01134   
01135    The output is a skeletal instruction.
01136   
01137    The algorithm has two parts.
01138    1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
01139    2. express the @^#-()+[] as some parameters suited to further analysis.
01140   
01141    2nd step is where we detect the googles of possible invalid combinations
01142    a human (or compiler) might write. Note that if we do a half-way
01143    decent assembler, we don't know how long to make (eg) displacement
01144    fields when we first meet them (because they may not have defined values).
01145    So we must wait until we know how many bits are needed for each address,
01146    then we can know both length and opcodes of instructions.
01147    For reason(s) above, we will pass to our caller a 'broken' instruction
01148    of these major components, from which our caller can generate instructions:
01149     -  displacement length      I^ S^ L^ B^ W^ unspecified
01150     -  mode                     (many)
01151     -  register                 R0-R15 or absent
01152     -  index register           R0-R15 or absent
01153     -  expression text          what we don't parse
01154     -  error text(s)            why we couldn't understand the operand
01155 
01156    ----
01157     
01158    To decode output of this, test errtxt. If errtxt[0] == '\0', then
01159    we had no errors that prevented parsing. Also, if we ever report
01160    an internal bug, errtxt[0] is set non-zero. So one test tells you
01161    if the other outputs are to be taken seriously.
01162 
01163    ----
01164    
01165    Dec defines the semantics of address modes (and values)
01166    by a two-letter code, explained here.
01167   
01168      letter 1:   access type
01169   
01170        a         address calculation - no data access, registers forbidden
01171        b         branch displacement
01172        m         read - let go of bus - write back    "modify"
01173        r         read
01174        v         bit field address: like 'a' but registers are OK
01175        w         write
01176        space   no operator (eg ".long foo") [our convention]
01177   
01178      letter 2:   data type (i.e. width, alignment)
01179   
01180        b         byte
01181        d         double precision floating point (D format)
01182        f         single precision floating point (F format)
01183        g         G format floating
01184        h         H format floating
01185        l         longword
01186        o         octaword
01187        q         quadword
01188        w         word
01189        ?       simple synthetic branch operand
01190        -       unconditional synthetic JSB/JSR operand
01191        !       complex synthetic branch operand
01192   
01193    The '-?!' letter 2's are not for external consumption. They are used
01194    for various assemblers. Generally, all unknown widths are assumed 0.
01195    We don't limit your choice of width character.
01196   
01197    DEC operands are hard work to parse. For example, '@' as the first
01198    character means indirect (deferred) mode but elsewhere it is a shift
01199    operator.
01200    The long-winded explanation of how this is supposed to work is
01201    cancelled. Read a DEC vax manual.
01202    We try hard not to parse anything that MIGHT be part of the expression
01203    buried in that syntax. For example if we see @...(Rn) we don't check
01204    for '-' before the '(' because mode @-(Rn) does not exist.
01205   
01206    After parsing we have:
01207   
01208    at                     1 if leading '@' (or Un*x '*')
01209    len                    takes one value from " bilsw". eg B^ -> 'b'.
01210    hash                   1 if leading '#' (or Un*x '$')
01211    expr_begin, expr_end   the expression we did not parse
01212                           even though we don't interpret it, we make use
01213                           of its presence or absence.
01214    sign                   -1: -(Rn)    0: absent    +1: (Rn)+
01215    paren                  1 if () are around register
01216    reg                    major register number 0:15    -1 means absent
01217    ndx                    index register number 0:15    -1 means absent
01218   
01219    Again, I dare not explain it: just trace ALL the code!
01220 
01221    Summary of vip_op outputs.
01222 
01223   mode reg    len    ndx
01224   (Rn) => @Rn
01225   {@}Rn                     5+@    n      ' '    optional
01226   branch operand            0      -1     ' '    -1
01227   S^#foo                    0      -1     's'    -1
01228   -(Rn)                     7      n      ' '    optional
01229   {@}(Rn)+           8+@    n      ' '    optional
01230   {@}#foo, no S^            8+@    PC     " i"   optional
01231   {@}{q^}{(Rn)}             10+@+q option " bwl" optional  */
01232 
01233 /* Dissect user-input 'optext' (which is something like "@B^foo@bar(AP)[FP]:")
01234    using the vop in vopP. vopP's vop_access and vop_width. We fill _ndx, _reg,
01235    _mode, _short, _warn, _error, _expr_begin, _expr_end and _nbytes.  */
01236 
01237 static void
01238 vip_op (char *optext, struct vop *vopP)
01239 {
01240   /* Track operand text forward.  */
01241   char *p;
01242   /* Track operand text backward.  */
01243   char *q;
01244   /* 1 if leading '@' ('*') seen.  */
01245   int at;
01246   /* one of " bilsw" */
01247   char len;
01248   /* 1 if leading '#' ('$') seen.  */
01249   int hash;
01250   /* -1, 0 or +1.  */
01251   int sign = 0;
01252   /* 1 if () surround register.  */
01253   int paren = 0;
01254   /* Register number, -1:absent.  */
01255   int reg = 0;
01256   /* Index register number -1:absent.  */
01257   int ndx = 0;
01258   /* Report illegal operand, ""==OK.  */
01259   /* " " is a FAKE error: means we won.  */
01260   /* ANY err that begins with ' ' is a fake.  */
01261   /* " " is converted to "" before return.  */
01262   const char *err;
01263   /* Warn about weird modes pf address.  */
01264   const char *wrn;
01265   /* Preserve q in case we backup.  */
01266   char *oldq = NULL;
01267   /* Build up 4-bit operand mode here.  */
01268   /* Note: index mode is in ndx, this is.  */
01269   /* The major mode of operand address.  */
01270   int mode = 0;
01271   /* Notice how we move wrong-arg-type bugs INSIDE this module: if we
01272      get the types wrong below, we lose at compile time rather than at
01273      lint or run time.  */
01274   char access_mode;         /* vop_access.  */
01275   char width;               /* vop_width.  */
01276 
01277   access_mode = vopP->vop_access;
01278   width = vopP->vop_width;
01279   /* None of our code bugs (yet), no user text errors, no warnings
01280      even.  */
01281   err = wrn = 0;
01282 
01283   p = optext;
01284 
01285   if (*p == ' ')            /* Expect all whitespace reduced to ' '.  */
01286     p++;                    /* skip over whitespace */
01287 
01288   if ((at = INDIRECTP (*p)) != 0)
01289     {                       /* 1 if *p=='@'(or '*' for Un*x) */
01290       p++;                  /* at is determined */
01291       if (*p == ' ')        /* Expect all whitespace reduced to ' '.  */
01292        p++;                 /* skip over whitespace */
01293     }
01294 
01295   /* This code is subtle. It tries to detect all legal (letter)'^'
01296      but it doesn't waste time explicitly testing for premature '\0' because
01297      this case is rejected as a mismatch against either (letter) or '^'.  */
01298   {
01299     char c;
01300 
01301     c = *p;
01302     c = TOLOWER (c);
01303     if (DISPLENP (p[1]) && strchr ("bilws", len = c))
01304       p += 2;               /* Skip (letter) '^'.  */
01305     else                    /* No (letter) '^' seen.  */
01306       len = ' ';            /* Len is determined.  */
01307   }
01308 
01309   if (*p == ' ')            /* Expect all whitespace reduced to ' '.  */
01310     p++;
01311 
01312   if ((hash = IMMEDIATEP (*p)) != 0)      /* 1 if *p=='#' ('$' for Un*x) */
01313     p++;                    /* Hash is determined.  */
01314 
01315   /* p points to what may be the beginning of an expression.
01316      We have peeled off the front all that is peelable.
01317      We know at, len, hash.
01318     
01319      Lets point q at the end of the text and parse that (backwards).  */
01320 
01321   for (q = p; *q; q++)
01322     ;
01323   q--;                      /* Now q points at last char of text.  */
01324 
01325   if (*q == ' ' && q >= p)  /* Expect all whitespace reduced to ' '.  */
01326     q--;
01327 
01328   /* Reverse over whitespace, but don't.  */
01329   /* Run back over *p.  */
01330 
01331   /* As a matter of policy here, we look for [Rn], although both Rn and S^#
01332      forbid [Rn]. This is because it is easy, and because only a sick
01333      cyborg would have [...] trailing an expression in a VAX-like assembler.
01334      A meticulous parser would first check for Rn followed by '(' or '['
01335      and not parse a trailing ']' if it found another. We just ban expressions
01336      ending in ']'.  */
01337   if (*q == ']')
01338     {
01339       while (q >= p && *q != '[')
01340        q--;
01341       /* Either q<p or we got matching '['.  */
01342       if (q < p)
01343        err = _("no '[' to match ']'");
01344       else
01345        {
01346          /* Confusers like "[]" will eventually lose with a bad register
01347           * name error. So again we don't need to check for early '\0'.  */
01348          if (q[3] == ']')
01349            ndx = vax_reg_parse (q[1], q[2], 0, 0);
01350          else if (q[4] == ']')
01351            ndx = vax_reg_parse (q[1], q[2], q[3], 0);
01352          else if (q[5] == ']')
01353            ndx = vax_reg_parse (q[1], q[2], q[3], q[4]);
01354          else
01355            ndx = -1;
01356          /* Since we saw a ']' we will demand a register name in the [].
01357           * If luser hasn't given us one: be rude.  */
01358          if (ndx < 0)
01359            err = _("bad register in []");
01360          else if (ndx == PC)
01361            err = _("[PC] index banned");
01362          else
01363            /* Point q just before "[...]".  */
01364            q--;
01365        }
01366     }
01367   else
01368     /* No ']', so no iNDeX register.  */
01369     ndx = -1;
01370 
01371   /* If err = "..." then we lost: run away.
01372      Otherwise ndx == -1 if there was no "[...]".
01373      Otherwise, ndx is index register number, and q points before "[...]".  */
01374 
01375   if (*q == ' ' && q >= p)  /* Expect all whitespace reduced to ' '.  */
01376     q--;
01377   /* Reverse over whitespace, but don't.  */
01378   /* Run back over *p.  */
01379   if (!err || !*err)
01380     {
01381       /* no ()+ or -() seen yet */
01382       sign = 0;
01383 
01384       if (q > p + 3 && *q == '+' && q[-1] == ')')
01385        {
01386          sign = 1;          /* we saw a ")+" */
01387          q--;               /* q points to ')' */
01388        }
01389 
01390       if (*q == ')' && q > p + 2)
01391        {
01392          paren = 1;         /* assume we have "(...)" */
01393          while (q >= p && *q != '(')
01394            q--;
01395          /* either q<p or we got matching '(' */
01396          if (q < p)
01397            err = _("no '(' to match ')'");
01398          else
01399            {
01400              /* Confusers like "()" will eventually lose with a bad register
01401                 name error. So again we don't need to check for early '\0'.  */
01402              if (q[3] == ')')
01403               reg = vax_reg_parse (q[1], q[2], 0, 0);
01404              else if (q[4] == ')')
01405               reg = vax_reg_parse (q[1], q[2], q[3], 0);
01406              else if (q[5] == ')')
01407               reg = vax_reg_parse (q[1], q[2], q[3], q[4]);
01408              else
01409               reg = -1;
01410              /* Since we saw a ')' we will demand a register name in the ')'.
01411                 This is nasty: why can't our hypothetical assembler permit
01412                 parenthesised expressions? BECAUSE I AM LAZY! That is why.
01413                 Abuse luser if we didn't spy a register name.  */
01414              if (reg < 0)
01415               {
01416                 /* JF allow parenthesized expressions.  I hope this works.  */
01417                 paren = 0;
01418                 while (*q != ')')
01419                   q++;
01420                 /* err = "unknown register in ()"; */
01421               }
01422              else
01423               q--;          /* point just before '(' of "(...)" */
01424              /* If err == "..." then we lost. Run away.
01425                 Otherwise if reg >= 0 then we saw (Rn).  */
01426            }
01427          /* If err == "..." then we lost.
01428             Otherwise paren==1 and reg = register in "()".  */
01429        }
01430       else
01431        paren = 0;
01432       /* If err == "..." then we lost.
01433          Otherwise, q points just before "(Rn)", if any.
01434          If there was a "(...)" then paren==1, and reg is the register.  */
01435 
01436       /* We should only seek '-' of "-(...)" if:
01437            we saw "(...)"                    paren == 1
01438            we have no errors so far          ! *err
01439            we did not see '+' of "(...)+"    sign < 1
01440          We don't check len. We want a specific error message later if
01441          user tries "x^...-(Rn)". This is a feature not a bug.  */
01442       if (!err || !*err)
01443        {
01444          if (paren && sign < 1)/* !sign is adequate test */
01445            {
01446              if (*q == '-')
01447               {
01448                 sign = -1;
01449                 q--;
01450               }
01451            }
01452          /* We have back-tracked over most
01453             of the crud at the end of an operand.
01454             Unless err, we know: sign, paren. If paren, we know reg.
01455             The last case is of an expression "Rn".
01456             This is worth hunting for if !err, !paren.
01457             We wouldn't be here if err.
01458             We remember to save q, in case we didn't want "Rn" anyway.  */
01459          if (!paren)
01460            {
01461              if (*q == ' ' && q >= p)     /* Expect all whitespace reduced to ' '.  */
01462               q--;
01463              /* Reverse over whitespace, but don't.  */
01464              /* Run back over *p.  */
01465              /* Room for Rn or Rnn (include prefix) exactly?  */
01466              if (q > p && q < p + 4)
01467               reg = vax_reg_parse (p[0], p[1],
01468                 q < p + 2 ? 0 : p[2],
01469                 q < p + 3 ? 0 : p[3]);
01470              else
01471               reg = -1;     /* Always comes here if no register at all.  */
01472              /* Here with a definitive reg value.  */
01473              if (reg >= 0)
01474               {
01475                 oldq = q;
01476                 q = p - 1;
01477               }
01478            }
01479        }
01480     }
01481   /* have reg. -1:absent; else 0:15.  */
01482 
01483   /* We have:  err, at, len, hash, ndx, sign, paren, reg.
01484      Also, any remaining expression is from *p through *q inclusive.
01485      Should there be no expression, q==p-1. So expression length = q-p+1.
01486      This completes the first part: parsing the operand text.  */
01487 
01488   /* We now want to boil the data down, checking consistency on the way.
01489      We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
01490      We will deliver a 4-bit reg, and a 4-bit mode.  */
01491 
01492   /* Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
01493     
01494      in:  at  ?
01495           len ?
01496           hash       ?
01497           p:q ?
01498           sign  ?
01499           paren      ?
01500           reg   ?
01501           ndx   ?
01502     
01503      out: mode  0
01504           reg   -1
01505           len ' '
01506           p:q whatever was input
01507           ndx -1
01508           err " "            or error message, and other outputs trashed.  */
01509   /* Branch operands have restricted forms.  */
01510   if ((!err || !*err) && access_mode == 'b')
01511     {
01512       if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
01513        err = _("invalid branch operand");
01514       else
01515        err = " ";
01516     }
01517 
01518   /* Since nobody seems to use it: comment this 'feature'(?) out for now.  */
01519 #ifdef NEVER
01520   /* Case of stand-alone operand. e.g. ".long foo"
01521     
01522      in:  at  ?
01523           len ?
01524           hash       ?
01525           p:q ?
01526           sign  ?
01527           paren      ?
01528           reg   ?
01529           ndx   ?
01530     
01531      out: mode  0
01532           reg   -1
01533           len ' '
01534           p:q whatever was input
01535           ndx -1
01536           err " "            or error message, and other outputs trashed.  */
01537   if ((!err || !*err) && access_mode == ' ')
01538     {
01539       if (at)
01540        err = _("address prohibits @");
01541       else if (hash)
01542        err = _("address prohibits #");
01543       else if (sign)
01544        {
01545          if (sign < 0)
01546            err = _("address prohibits -()");
01547          else
01548            err = _("address prohibits ()+");
01549        }
01550       else if (paren)
01551        err = _("address prohibits ()");
01552       else if (ndx >= 0)
01553        err = _("address prohibits []");
01554       else if (reg >= 0)
01555        err = _("address prohibits register");
01556       else if (len != ' ')
01557        err = _("address prohibits displacement length specifier");
01558       else
01559        {
01560          err = " ";  /* succeed */
01561          mode = 0;
01562        }
01563     }
01564 #endif
01565 
01566   /* Case of S^#.
01567     
01568      in:  at       0
01569           len      's'               definition
01570           hash     1              demand
01571           p:q                        demand not empty
01572           sign     0                 by paren==0
01573           paren    0             by "()" scan logic because "S^" seen
01574           reg      -1                or nn by mistake
01575           ndx      -1
01576     
01577      out: mode     0
01578           reg      -1
01579           len      's'
01580           exp
01581           ndx      -1  */
01582   if ((!err || !*err) && len == 's')
01583     {
01584       if (!hash || paren || at || ndx >= 0)
01585        err = _("invalid operand of S^#");
01586       else
01587        {
01588          if (reg >= 0)
01589            {
01590              /* Darn! we saw S^#Rnn ! put the Rnn back in
01591                 expression. KLUDGE! Use oldq so we don't
01592                 need to know exact length of reg name.  */
01593              q = oldq;
01594              reg = 0;
01595            }
01596          /* We have all the expression we will ever get.  */
01597          if (p > q)
01598            err = _("S^# needs expression");
01599          else if (access_mode == 'r')
01600            {
01601              err = " ";     /* WIN! */
01602              mode = 0;
01603            }
01604          else
01605            err = _("S^# may only read-access");
01606        }
01607     }
01608   
01609   /* Case of -(Rn), which is weird case.
01610     
01611      in:  at       0
01612           len      '
01613           hash     0
01614           p:q      q<p
01615           sign     -1                by definition
01616           paren    1              by definition
01617           reg      present           by definition
01618           ndx      optional
01619     
01620      out: mode     7
01621           reg      present
01622           len      ' '
01623           exp      ""                enforce empty expression
01624           ndx      optional          warn if same as reg.  */
01625   if ((!err || !*err) && sign < 0)
01626     {
01627       if (len != ' ' || hash || at || p <= q)
01628        err = _("invalid operand of -()");
01629       else
01630        {
01631          err = " ";         /* win */
01632          mode = 7;
01633          if (reg == PC)
01634            wrn = _("-(PC) unpredictable");
01635          else if (reg == ndx)
01636            wrn = _("[]index same as -()register: unpredictable");
01637        }
01638     }
01639 
01640   /* We convert "(Rn)" to "@Rn" for our convenience.
01641      (I hope this is convenient: has someone got a better way to parse this?)
01642      A side-effect of this is that "@Rn" is a valid operand.  */
01643   if (paren && !sign && !hash && !at && len == ' ' && p > q)
01644     {
01645       at = 1;
01646       paren = 0;
01647     }
01648 
01649   /* Case of (Rn)+, which is slightly different.
01650     
01651      in:  at
01652           len      ' '
01653           hash     0
01654           p:q      q<p
01655           sign     +1                by definition
01656           paren    1              by definition
01657           reg      present           by definition
01658           ndx      optional
01659     
01660      out: mode     8+@
01661           reg      present
01662           len      ' '
01663           exp      ""                enforce empty expression
01664           ndx      optional          warn if same as reg.  */
01665   if ((!err || !*err) && sign > 0)
01666     {
01667       if (len != ' ' || hash || p <= q)
01668        err = _("invalid operand of ()+");
01669       else
01670        {
01671          err = " ";         /* win */
01672          mode = 8 + (at ? 1 : 0);
01673          if (reg == PC)
01674            wrn = _("(PC)+ unpredictable");
01675          else if (reg == ndx)
01676            wrn = _("[]index same as ()+register: unpredictable");
01677        }
01678     }
01679 
01680   /* Case of #, without S^.
01681     
01682      in:  at
01683           len      ' ' or 'i'
01684           hash     1              by definition
01685           p:q
01686           sign     0
01687           paren    0
01688           reg      absent
01689           ndx      optional
01690     
01691      out: mode     8+@
01692           reg      PC
01693           len      ' ' or 'i'
01694           exp
01695           ndx      optional.  */
01696   if ((!err || !*err) && hash)
01697     {
01698       if (len != 'i' && len != ' ')
01699        err = _("# conflicts length");
01700       else if (paren)
01701        err = _("# bars register");
01702       else
01703        {
01704          if (reg >= 0)
01705            {
01706              /* Darn! we saw #Rnn! Put the Rnn back into the expression.
01707                 By using oldq, we don't need to know how long Rnn was.
01708                 KLUDGE!  */
01709              q = oldq;
01710              reg = -1;             /* No register any more.  */
01711            }
01712          err = " ";         /* Win.  */
01713 
01714          /* JF a bugfix, I think!  */
01715          if (at && access_mode == 'a')
01716            vopP->vop_nbytes = 4;
01717 
01718          mode = (at ? 9 : 8);
01719          reg = PC;
01720          if ((access_mode == 'm' || access_mode == 'w') && !at)
01721            wrn = _("writing or modifying # is unpredictable");
01722        }
01723     }
01724   /* If !*err, then       sign == 0
01725                           hash == 0 */
01726 
01727   /* Case of Rn. We separate this one because it has a few special
01728      errors the remaining modes lack.
01729     
01730      in:  at       optional
01731           len      ' '
01732           hash     0             by program logic
01733           p:q      empty
01734           sign     0                 by program logic
01735           paren    0             by definition
01736           reg      present           by definition
01737           ndx      optional
01738     
01739      out: mode     5+@
01740           reg      present
01741           len      ' '               enforce no length
01742           exp      ""                enforce empty expression
01743           ndx      optional          warn if same as reg.  */
01744   if ((!err || !*err) && !paren && reg >= 0)
01745     {
01746       if (len != ' ')
01747        err = _("length not needed");
01748       else if (at)
01749        {
01750          err = " ";         /* win */
01751          mode = 6;          /* @Rn */
01752        }
01753       else if (ndx >= 0)
01754        err = _("can't []index a register, because it has no address");
01755       else if (access_mode == 'a')
01756        err = _("a register has no address");
01757       else
01758        {
01759          /* Idea here is to detect from length of datum
01760             and from register number if we will touch PC.
01761             Warn if we do.
01762             vop_nbytes is number of bytes in operand.
01763             Compute highest byte affected, compare to PC0.  */
01764          if ((vopP->vop_nbytes + reg * 4) > 60)
01765            wrn = _("PC part of operand unpredictable");
01766          err = " ";         /* win */
01767          mode = 5;          /* Rn */
01768        }
01769     }
01770   /* If !*err,        sign  == 0
01771                       hash  == 0
01772                       paren == 1  OR reg==-1  */
01773 
01774   /* Rest of cases fit into one bunch.
01775     
01776      in:  at       optional
01777           len      ' ' or 'b' or 'w' or 'l'
01778           hash     0             by program logic
01779           p:q      expected          (empty is not an error)
01780           sign     0                 by program logic
01781           paren    optional
01782           reg      optional
01783           ndx      optional
01784     
01785      out: mode     10 + @ + len
01786           reg      optional
01787           len      ' ' or 'b' or 'w' or 'l'
01788           exp                        maybe empty
01789           ndx      optional          warn if same as reg.  */
01790   if (!err || !*err)
01791     {
01792       err = " ";            /* win (always) */
01793       mode = 10 + (at ? 1 : 0);
01794       switch (len)
01795        {
01796        case 'l':
01797          mode += 2;
01798        case 'w':
01799          mode += 2;
01800        case ' ':     /* Assumed B^ until our caller changes it.  */
01801        case 'b':
01802          break;
01803        }
01804     }
01805 
01806   /* here with completely specified     mode
01807                                    len
01808                                    reg
01809                                    expression   p,q
01810                                    ndx.  */
01811 
01812   if (*err == ' ')
01813     err = 0;                /* " " is no longer an error.  */
01814 
01815   vopP->vop_mode = mode;
01816   vopP->vop_reg = reg;
01817   vopP->vop_short = len;
01818   vopP->vop_expr_begin = p;
01819   vopP->vop_expr_end = q;
01820   vopP->vop_ndx = ndx;
01821   vopP->vop_error = err;
01822   vopP->vop_warn = wrn;
01823 }
01824 
01825 /* This converts a string into a vax instruction.
01826    The string must be a bare single instruction in dec-vax (with BSD4 frobs)
01827    format.
01828    It provides some error messages: at most one fatal error message (which
01829    stops the scan) and at most one warning message for each operand.
01830    The vax instruction is returned in exploded form, since we have no
01831    knowledge of how you parse (or evaluate) your expressions.
01832    We do however strip off and decode addressing modes and operation
01833    mnemonic.
01834   
01835    The exploded instruction is returned to a struct vit of your choice.
01836    #include "vax-inst.h" to know what a struct vit is.
01837   
01838    This function's value is a string. If it is not "" then an internal
01839    logic error was found: read this code to assign meaning to the string.
01840    No argument string should generate such an error string:
01841    it means a bug in our code, not in the user's text.
01842   
01843    You MUST have called vip_begin() once before using this function.  */
01844 
01845 static void
01846 vip (struct vit *vitP,             /* We build an exploded instruction here.  */
01847      char *instring)        /* Text of a vax instruction: we modify.  */
01848 {
01849   /* How to bit-encode this opcode.  */
01850   struct vot_wot *vwP;
01851   /* 1/skip whitespace.2/scan vot_how */
01852   char *p;
01853   char *q;
01854   /* counts number of operands seen */
01855   unsigned char count;
01856   /* scan operands in struct vit */
01857   struct vop *operandp;
01858   /* error over all operands */
01859   const char *alloperr;
01860   /* Remember char, (we clobber it with '\0' temporarily).  */
01861   char c;
01862   /* Op-code of this instruction.  */
01863   vax_opcodeT oc;
01864 
01865   if (*instring == ' ')
01866     ++instring;
01867   
01868   /* MUST end in end-of-string or exactly 1 space.  */
01869   for (p = instring; *p && *p != ' '; p++)
01870     ;
01871 
01872   /* Scanned up to end of operation-code.  */
01873   /* Operation-code is ended with whitespace.  */
01874   if (p - instring == 0)
01875     {
01876       vitP->vit_error = _("No operator");
01877       count = 0;
01878       memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
01879     }
01880   else
01881     {
01882       c = *p;
01883       *p = '\0';
01884       /* Here with instring pointing to what better be an op-name, and p
01885          pointing to character just past that.
01886          We trust instring points to an op-name, with no whitespace.  */
01887       vwP = (struct vot_wot *) hash_find (op_hash, instring);
01888       /* Restore char after op-code.  */
01889       *p = c;
01890       if (vwP == 0)
01891        {
01892          vitP->vit_error = _("Unknown operator");
01893          count = 0;
01894          memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
01895        }
01896       else
01897        {
01898          /* We found a match! So let's pick up as many operands as the
01899             instruction wants, and even gripe if there are too many.
01900             We expect comma to separate each operand.
01901             We let instring track the text, while p tracks a part of the
01902             struct vot.  */
01903          const char *howp;
01904          /* The lines below know about 2-byte opcodes starting FD,FE or FF.
01905             They also understand synthetic opcodes. Note:
01906             we return 32 bits of opcode, including bucky bits, BUT
01907             an opcode length is either 8 or 16 bits for vit_opcode_nbytes.  */
01908          oc = vwP->vot_code;       /* The op-code.  */
01909          vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
01910          md_number_to_chars (vitP->vit_opcode, oc, 4);
01911          count = 0;         /* No operands seen yet.  */
01912          instring = p;             /* Point just past operation code.  */
01913          alloperr = "";
01914          for (howp = vwP->vot_how, operandp = vitP->vit_operand;
01915               !(alloperr && *alloperr) && *howp;
01916               operandp++, howp += 2)
01917            {
01918              /* Here to parse one operand. Leave instring pointing just
01919                 past any one ',' that marks the end of this operand.  */
01920              if (!howp[1])
01921               as_fatal (_("odd number of bytes in operand description"));
01922              else if (*instring)
01923               {
01924                 for (q = instring; (c = *q) && c != ','; q++)
01925                   ;
01926                 /* Q points to ',' or '\0' that ends argument. C is that
01927                    character.  */
01928                 *q = 0;
01929                 operandp->vop_width = howp[1];
01930                 operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
01931                 operandp->vop_access = howp[0];
01932                 vip_op (instring, operandp);
01933                 *q = c;     /* Restore input text.  */
01934                 if (operandp->vop_error)
01935                   alloperr = _("Bad operand");
01936                 instring = q + (c ? 1 : 0);      /* Next operand (if any).  */
01937                 count++;    /*  Won another argument, may have an operr.  */
01938               }
01939              else
01940               alloperr = _("Not enough operands");
01941            }
01942          if (!*alloperr)
01943            {
01944              if (*instring == ' ')
01945               instring++;
01946              if (*instring)
01947               alloperr = _("Too many operands");
01948            }
01949          vitP->vit_error = alloperr;
01950        }
01951     }
01952   vitP->vit_operands = count;
01953 }
01954 
01955 #ifdef test
01956 
01957 /* Test program for above.  */
01958 
01959 struct vit myvit;           /* Build an exploded vax instruction here.  */
01960 char answer[100];           /* Human types a line of vax assembler here.  */
01961 char *mybug;                /* "" or an internal logic diagnostic.  */
01962 int mycount;                /* Number of operands.  */
01963 struct vop *myvop;          /* Scan operands from myvit.  */
01964 int mysynth;                /* 1 means want synthetic opcodes.  */
01965 char my_immediate[200];
01966 char my_indirect[200];
01967 char my_displen[200];
01968 
01969 int
01970 main (void)
01971 {
01972   char *p;
01973 
01974   printf ("0 means no synthetic instructions.   ");
01975   printf ("Value for vip_begin?  ");
01976   gets (answer);
01977   sscanf (answer, "%d", &mysynth);
01978   printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
01979   printf ("enter immediate symbols eg enter #   ");
01980   gets (my_immediate);
01981   printf ("enter indirect symbols  eg enter @   ");
01982   gets (my_indirect);
01983   printf ("enter displen symbols   eg enter ^   ");
01984   gets (my_displen);
01985 
01986   if (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))
01987     error ("vip_begin=%s", p);
01988 
01989   printf ("An empty input line will quit you from the vax instruction parser\n");
01990   for (;;)
01991     {
01992       printf ("vax instruction: ");
01993       fflush (stdout);
01994       gets (answer);
01995       if (!*answer)
01996        break;        /* Out of for each input text loop.  */
01997 
01998       vip (& myvit, answer);
01999       if (*myvit.vit_error)
02000        printf ("ERR:\"%s\"\n", myvit.vit_error);
02001 
02002       printf ("opcode=");
02003       for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
02004           mycount;
02005           mycount--, p++)
02006        printf ("%02x ", *p & 0xFF);
02007 
02008       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
02009       for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
02010        {
02011          printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
02012                 myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
02013                 myvop->vop_short, myvop->vop_access, myvop->vop_width,
02014                 myvop->vop_nbytes);
02015          for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
02016            putchar (*p);
02017 
02018          printf ("\"\n");
02019          if (myvop->vop_error)
02020            printf ("  err:\"%s\"\n", myvop->vop_error);
02021 
02022          if (myvop->vop_warn)
02023            printf ("  wrn:\"%s\"\n", myvop->vop_warn);
02024        }
02025     }
02026   vip_end ();
02027   exit (EXIT_SUCCESS);
02028 }
02029 
02030 #endif
02031 
02032 #ifdef TEST                 /* #Define to use this testbed.  */
02033 
02034 /* Follows a test program for this function.
02035    We declare arrays non-local in case some of our tiny-minded machines
02036    default to small stacks. Also, helps with some debuggers.  */
02037 
02038 char answer[100];           /* Human types into here.  */
02039 char *p;                    /*  */
02040 char *myerr;
02041 char *mywrn;
02042 char *mybug;
02043 char myaccess;
02044 char mywidth;
02045 char mymode;
02046 char myreg;
02047 char mylen;
02048 char *myleft;
02049 char *myright;
02050 char myndx;
02051 int my_operand_length;
02052 char my_immediate[200];
02053 char my_indirect[200];
02054 char my_displen[200];
02055 
02056 int
02057 main (void)
02058 {
02059   printf ("enter immediate symbols eg enter #   ");
02060   gets (my_immediate);
02061   printf ("enter indirect symbols  eg enter @   ");
02062   gets (my_indirect);
02063   printf ("enter displen symbols   eg enter ^   ");
02064   gets (my_displen);
02065   vip_op_defaults (my_immediate, my_indirect, my_displen);
02066 
02067   for (;;)
02068     {
02069       printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
02070       fflush (stdout);
02071       gets (answer);
02072       if (!answer[0])
02073        exit (EXIT_SUCCESS);
02074       myaccess = answer[0];
02075       mywidth = answer[1];
02076       switch (mywidth)
02077        {
02078        case 'b':
02079          my_operand_length = 1;
02080          break;
02081        case 'd':
02082          my_operand_length = 8;
02083          break;
02084        case 'f':
02085          my_operand_length = 4;
02086          break;
02087        case 'g':
02088          my_operand_length = 16;
02089          break;
02090        case 'h':
02091          my_operand_length = 32;
02092          break;
02093        case 'l':
02094          my_operand_length = 4;
02095          break;
02096        case 'o':
02097          my_operand_length = 16;
02098          break;
02099        case 'q':
02100          my_operand_length = 8;
02101          break;
02102        case 'w':
02103          my_operand_length = 2;
02104          break;
02105        case '!':
02106        case '?':
02107        case '-':
02108          my_operand_length = 0;
02109          break;
02110 
02111        default:
02112          my_operand_length = 2;
02113          printf ("I dn't understand access width %c\n", mywidth);
02114          break;
02115        }
02116       printf ("VAX assembler instruction operand: ");
02117       fflush (stdout);
02118       gets (answer);
02119       mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
02120                     &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
02121                     &myerr, &mywrn);
02122       if (*myerr)
02123        {
02124          printf ("error: \"%s\"\n", myerr);
02125          if (*mybug)
02126            printf (" bug: \"%s\"\n", mybug);
02127        }
02128       else
02129        {
02130          if (*mywrn)
02131            printf ("warning: \"%s\"\n", mywrn);
02132          mumble ("mode", mymode);
02133          mumble ("register", myreg);
02134          mumble ("index", myndx);
02135          printf ("width:'%c'  ", mylen);
02136          printf ("expression: \"");
02137          while (myleft <= myright)
02138            putchar (*myleft++);
02139          printf ("\"\n");
02140        }
02141     }
02142 }
02143 
02144 void
02145 mumble (char *text, int value)
02146 {
02147   printf ("%s:", text);
02148   if (value >= 0)
02149     printf ("%xx", value);
02150   else
02151     printf ("ABSENT");
02152   printf ("  ");
02153 }
02154 
02155 #endif
02156 
02157 int md_short_jump_size = 3;
02158 int md_long_jump_size = 6;
02159 
02160 void
02161 md_create_short_jump (char *ptr,
02162                     addressT from_addr,
02163                     addressT to_addr ATTRIBUTE_UNUSED,
02164                     fragS *frag ATTRIBUTE_UNUSED,
02165                     symbolS *to_symbol ATTRIBUTE_UNUSED)
02166 {
02167   valueT offset;
02168 
02169   /* This former calculation was off by two:
02170       offset = to_addr - (from_addr + 1);
02171      We need to account for the one byte instruction and also its
02172      two byte operand.  */
02173   offset = to_addr - (from_addr + 1 + 2);
02174   *ptr++ = VAX_BRW;         /* Branch with word (16 bit) offset.  */
02175   md_number_to_chars (ptr, offset, 2);
02176 }
02177 
02178 void
02179 md_create_long_jump (char *ptr,
02180                    addressT from_addr ATTRIBUTE_UNUSED,
02181                    addressT to_addr,
02182                    fragS *frag,
02183                    symbolS *to_symbol)
02184 {
02185   valueT offset;
02186 
02187   offset = to_addr - S_GET_VALUE (to_symbol);
02188   *ptr++ = VAX_JMP;         /* Arbitrary jump.  */
02189   *ptr++ = VAX_ABSOLUTE_MODE;
02190   md_number_to_chars (ptr, offset, 4);
02191   fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (long) 0, 0, NO_RELOC);
02192 }
02193 
02194 #ifdef OBJ_VMS
02195 const char *md_shortopts = "d:STt:V+1h:Hv::";
02196 #elif defined(OBJ_ELF)
02197 const char *md_shortopts = "d:STt:VkKQ:";
02198 #else
02199 const char *md_shortopts = "d:STt:V";
02200 #endif
02201 struct option md_longopts[] =
02202 {
02203 #ifdef OBJ_ELF
02204 #define OPTION_PIC (OPTION_MD_BASE)
02205   { "pic", no_argument, NULL, OPTION_PIC },
02206 #endif
02207   { NULL, no_argument, NULL, 0 }
02208 };
02209 size_t md_longopts_size = sizeof (md_longopts);
02210 
02211 int
02212 md_parse_option (int c, char *arg)
02213 {
02214   switch (c)
02215     {
02216     case 'S':
02217       as_warn (_("SYMBOL TABLE not implemented"));
02218       break;
02219 
02220     case 'T':
02221       as_warn (_("TOKEN TRACE not implemented"));
02222       break;
02223 
02224     case 'd':
02225       as_warn (_("Displacement length %s ignored!"), arg);
02226       break;
02227 
02228     case 't':
02229       as_warn (_("I don't need or use temp. file \"%s\"."), arg);
02230       break;
02231 
02232     case 'V':
02233       as_warn (_("I don't use an interpass file! -V ignored"));
02234       break;
02235 
02236 #ifdef OBJ_VMS
02237     case '+':               /* For g++.  Hash any name > 31 chars long.  */
02238       flag_hash_long_names = 1;
02239       break;
02240 
02241     case '1':               /* For backward compatibility.  */
02242       flag_one = 1;
02243       break;
02244 
02245     case 'H':               /* Show new symbol after hash truncation.  */
02246       flag_show_after_trunc = 1;
02247       break;
02248 
02249     case 'h':               /* No hashing of mixed-case names.  */
02250       {
02251        extern char vms_name_mapping;
02252        vms_name_mapping = atoi (arg);
02253        flag_no_hash_mixed_case = 1;
02254       }
02255       break;
02256 
02257     case 'v':
02258       {
02259        extern char *compiler_version_string;
02260 
02261        if (!arg || !*arg || access (arg, 0) == 0)
02262          return 0;          /* Have caller show the assembler version.  */
02263        compiler_version_string = arg;
02264       }
02265       break;
02266 #endif
02267 
02268 #ifdef OBJ_ELF
02269     case OPTION_PIC:
02270     case 'k':
02271       flag_want_pic = 1;
02272       break;                /* -pic, Position Independent Code.  */
02273 
02274      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment
02275        section should be emitted or not.  FIXME: Not implemented.  */
02276     case 'Q':
02277       break;
02278 #endif
02279 
02280     default:
02281       return 0;
02282     }
02283 
02284   return 1;
02285 }
02286 
02287 void
02288 md_show_usage (FILE *stream)
02289 {
02290   fprintf (stream, _("\
02291 VAX options:\n\
02292 -d LENGTH            ignored\n\
02293 -J                   ignored\n\
02294 -S                   ignored\n\
02295 -t FILE                     ignored\n\
02296 -T                   ignored\n\
02297 -V                   ignored\n"));
02298 #ifdef OBJ_VMS
02299   fprintf (stream, _("\
02300 VMS options:\n\
02301 -+                   hash encode names longer than 31 characters\n\
02302 -1                   `const' handling compatible with gcc 1.x\n\
02303 -H                   show new symbol after hash truncation\n\
02304 -h NUM               don't hash mixed-case names, and adjust case:\n\
02305                      0 = upper, 2 = lower, 3 = preserve case\n\
02306 -v\"VERSION\"        code being assembled was produced by compiler \"VERSION\"\n"));
02307 #endif
02308 }
02309 
02310 /* We have no need to default values of symbols.  */
02311 
02312 symbolS *
02313 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
02314 {
02315   return NULL;
02316 }
02317 
02318 /* Round up a section size to the appropriate boundary.  */
02319 valueT
02320 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
02321 {
02322   /* Byte alignment is fine */
02323   return size;
02324 }
02325 
02326 /* Exactly what point is a PC-relative offset relative TO?
02327    On the vax, they're relative to the address of the offset, plus
02328    its size. */
02329 long
02330 md_pcrel_from (fixS *fixP)
02331 {
02332   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
02333 }
02334 
02335 arelent *
02336 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
02337 {
02338   arelent *reloc;
02339   bfd_reloc_code_real_type code;
02340 
02341   if (fixp->fx_tcbit)
02342     abort ();
02343 
02344   if (fixp->fx_r_type != BFD_RELOC_NONE)
02345     {
02346       code = fixp->fx_r_type;
02347 
02348       if (fixp->fx_pcrel)
02349        {
02350          switch (code)
02351            {
02352            case BFD_RELOC_8_PCREL:
02353            case BFD_RELOC_16_PCREL:
02354            case BFD_RELOC_32_PCREL:
02355 #ifdef OBJ_ELF
02356            case BFD_RELOC_8_GOT_PCREL:
02357            case BFD_RELOC_16_GOT_PCREL:
02358            case BFD_RELOC_32_GOT_PCREL:
02359            case BFD_RELOC_8_PLT_PCREL:
02360            case BFD_RELOC_16_PLT_PCREL:
02361            case BFD_RELOC_32_PLT_PCREL:
02362 #endif
02363              break;
02364            default:
02365              as_bad_where (fixp->fx_file, fixp->fx_line,
02366                          _("Cannot make %s relocation PC relative"),
02367                          bfd_get_reloc_code_name (code));
02368            }
02369        }
02370     }
02371   else
02372     {
02373 #define F(SZ,PCREL)         (((SZ) << 1) + (PCREL))
02374       switch (F (fixp->fx_size, fixp->fx_pcrel))
02375        {
02376 #define MAP(SZ,PCREL,TYPE)  case F(SZ,PCREL): code = (TYPE); break
02377          MAP (1, 0, BFD_RELOC_8);
02378          MAP (2, 0, BFD_RELOC_16);
02379          MAP (4, 0, BFD_RELOC_32);
02380          MAP (1, 1, BFD_RELOC_8_PCREL);
02381          MAP (2, 1, BFD_RELOC_16_PCREL);
02382          MAP (4, 1, BFD_RELOC_32_PCREL);
02383        default:
02384          abort ();
02385        }
02386     }
02387 #undef F
02388 #undef MAP
02389 
02390   reloc = xmalloc (sizeof (arelent));
02391   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
02392   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
02393   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
02394 #ifndef OBJ_ELF
02395   if (fixp->fx_pcrel)
02396     reloc->addend = fixp->fx_addnumber;
02397   else
02398     reloc->addend = 0;
02399 #else
02400   reloc->addend = fixp->fx_offset;
02401 #endif
02402 
02403   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
02404   assert (reloc->howto != 0);
02405 
02406   return reloc;
02407 }
02408 
02409 /* vax:md_assemble() emit frags for 1 instruction given in textual form.  */
02410 void
02411 md_assemble (char *instruction_string)
02412 {
02413   /* Non-zero if operand expression's segment is not known yet.  */
02414   int is_undefined;
02415   /* Non-zero if operand expression's segment is absolute.  */
02416   int is_absolute;
02417   int length_code;
02418   char *p;
02419   /* An operand. Scans all operands.  */
02420   struct vop *operandP;
02421   char *save_input_line_pointer;
02422                      /* What used to live after an expression.  */
02423   char c_save;
02424   /* 1: instruction_string bad for all passes.  */
02425   int goofed;
02426   /* Points to slot just after last operand.  */
02427   struct vop *end_operandP;
02428   /* Points to expression values for this operand.  */
02429   expressionS *expP;
02430   segT *segP;
02431 
02432   /* These refer to an instruction operand expression.  */
02433   /* Target segment of the address.        */
02434   segT to_seg;
02435   valueT this_add_number;
02436   /* Positive (minuend) symbol.  */
02437   symbolS *this_add_symbol;
02438   /* As a number.  */
02439   long opcode_as_number;
02440   /* Least significant byte 1st.  */
02441   char *opcode_as_chars;
02442   /* As an array of characters.  */
02443   /* Least significant byte 1st */
02444   char *opcode_low_byteP;
02445   /* length (bytes) meant by vop_short.  */
02446   int length;
02447   /* 0, or 1 if '@' is in addressing mode.  */
02448   int at;
02449   /* From vop_nbytes: vax_operand_width (in bytes) */
02450   int nbytes;
02451   FLONUM_TYPE *floatP;
02452   LITTLENUM_TYPE literal_float[8];
02453   /* Big enough for any floating point literal.  */
02454 
02455   vip (&v, instruction_string);
02456 
02457   /* Now we try to find as many as_warn()s as we can. If we do any as_warn()s
02458      then goofed=1. Notice that we don't make any frags yet.
02459      Should goofed be 1, then this instruction will wedge in any pass,
02460      and we can safely flush it, without causing interpass symbol phase
02461      errors. That is, without changing label values in different passes.  */
02462   if ((goofed = (*v.vit_error)) != 0)
02463     {
02464       as_fatal (_("Ignoring statement due to \"%s\""), v.vit_error);
02465     }
02466   /* We need to use expression() and friends, which require us to diddle
02467      input_line_pointer. So we save it and restore it later.  */
02468   save_input_line_pointer = input_line_pointer;
02469   for (operandP = v.vit_operand,
02470        expP = exp_of_operand,
02471        segP = seg_of_operand,
02472        floatP = float_operand,
02473        end_operandP = v.vit_operand + v.vit_operands;
02474 
02475        operandP < end_operandP;
02476 
02477        operandP++, expP++, segP++, floatP++)
02478     {
02479       if (operandP->vop_error)
02480        {
02481          as_fatal (_("Aborting because statement has \"%s\""), operandP->vop_error);
02482          goofed = 1;
02483        }
02484       else
02485        {
02486          /* Statement has no syntax goofs: let's sniff the expression.  */
02487          int can_be_short = 0;     /* 1 if a bignum can be reduced to a short literal.  */
02488 
02489          input_line_pointer = operandP->vop_expr_begin;
02490          c_save = operandP->vop_expr_end[1];
02491          operandP->vop_expr_end[1] = '\0';
02492          /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1.  */
02493          *segP = expression (expP);
02494          switch (expP->X_op)
02495            {
02496            case O_absent:
02497              /* for BSD4.2 compatibility, missing expression is absolute 0 */
02498              expP->X_op = O_constant;
02499              expP->X_add_number = 0;
02500              /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
02501                X_add_symbol to any particular value.  But, we will program
02502                defensively. Since this situation occurs rarely so it costs
02503                us little to do, and stops Dean worrying about the origin of
02504                random bits in expressionS's.  */
02505              expP->X_add_symbol = NULL;
02506              expP->X_op_symbol = NULL;
02507              break;
02508 
02509            case O_symbol:
02510            case O_constant:
02511              break;
02512 
02513            default:
02514              /* Major bug. We can't handle the case of a
02515                 SEG_OP expression in a VIT_OPCODE_SYNTHETIC
02516                 variable-length instruction.
02517                 We don't have a frag type that is smart enough to
02518                 relax a SEG_OP, and so we just force all
02519                 SEG_OPs to behave like SEG_PASS1s.
02520                 Clearly, if there is a demand we can invent a new or
02521                 modified frag type and then coding up a frag for this
02522                 case will be easy. SEG_OP was invented for the
02523                 .words after a CASE opcode, and was never intended for
02524                 instruction operands.  */
02525              need_pass_2 = 1;
02526              as_fatal (_("Can't relocate expression"));
02527              break;
02528 
02529            case O_big:
02530              /* Preserve the bits.  */
02531              if (expP->X_add_number > 0)
02532               {
02533                 bignum_copy (generic_bignum, expP->X_add_number,
02534                             floatP->low, SIZE_OF_LARGE_NUMBER);
02535               }
02536              else
02537               {
02538                 know (expP->X_add_number < 0);
02539                 flonum_copy (&generic_floating_point_number,
02540                             floatP);
02541                 if (strchr ("s i", operandP->vop_short))
02542                   {
02543                     /* Could possibly become S^# */
02544                     flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
02545                     switch (-expP->X_add_number)
02546                      {
02547                      case 'f':
02548                        can_be_short =
02549                          (literal_float[0] & 0xFC0F) == 0x4000
02550                          && literal_float[1] == 0;
02551                        break;
02552 
02553                      case 'd':
02554                        can_be_short =
02555                          (literal_float[0] & 0xFC0F) == 0x4000
02556                          && literal_float[1] == 0
02557                          && literal_float[2] == 0
02558                          && literal_float[3] == 0;
02559                        break;
02560 
02561                      case 'g':
02562                        can_be_short =
02563                          (literal_float[0] & 0xFF81) == 0x4000
02564                          && literal_float[1] == 0
02565                          && literal_float[2] == 0
02566                          && literal_float[3] == 0;
02567                        break;
02568 
02569                      case 'h':
02570                        can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
02571                                      && (literal_float[1] & 0xE000) == 0
02572                                      && literal_float[2] == 0
02573                                      && literal_float[3] == 0
02574                                      && literal_float[4] == 0
02575                                      && literal_float[5] == 0
02576                                      && literal_float[6] == 0
02577                                      && literal_float[7] == 0);
02578                        break;
02579 
02580                      default:
02581                        BAD_CASE (-expP->X_add_number);
02582                        break;
02583                      }
02584                   }
02585               }
02586 
02587              if (operandP->vop_short == 's'
02588                 || operandP->vop_short == 'i'
02589                 || (operandP->vop_short == ' '
02590                     && operandP->vop_reg == 0xF
02591                     && (operandP->vop_mode & 0xE) == 0x8))
02592               {
02593                 /* Saw a '#'.  */
02594                 if (operandP->vop_short == ' ')
02595                   {
02596                     /* We must chose S^ or I^.  */
02597                     if (expP->X_add_number > 0)
02598                      {
02599                        /* Bignum: Short literal impossible.  */
02600                        operandP->vop_short = 'i';
02601                        operandP->vop_mode = 8;
02602                        operandP->vop_reg = 0xF;  /* VAX PC.  */
02603                      }
02604                     else
02605                      {
02606                        /* Flonum: Try to do it.  */
02607                        if (can_be_short)
02608                          {
02609                            operandP->vop_short = 's';
02610                            operandP->vop_mode = 0;
02611                            operandP->vop_ndx = -1;
02612                            operandP->vop_reg = -1;
02613                            expP->X_op = O_constant;
02614                          }
02615                        else
02616                          {
02617                            operandP->vop_short = 'i';
02618                            operandP->vop_mode = 8;
02619                            operandP->vop_reg = 0xF;     /* VAX PC */
02620                          }
02621                      }      /* bignum or flonum ? */
02622                   }         /*  if #, but no S^ or I^ seen.  */
02623                 /* No more ' ' case: either 's' or 'i'.  */
02624                 if (operandP->vop_short == 's')
02625                   {
02626                     /* Wants to be a short literal.  */
02627                     if (expP->X_add_number > 0)
02628                      {
02629                        as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
02630                        operandP->vop_short = 'i';
02631                        operandP->vop_mode = 8;
02632                        operandP->vop_reg = 0xF;  /* VAX PC.  */
02633                      }
02634                     else
02635                      {
02636                        if (!can_be_short)
02637                          {
02638                            as_warn (_("Can't do flonum short literal: immediate mode used."));
02639                            operandP->vop_short = 'i';
02640                            operandP->vop_mode = 8;
02641                            operandP->vop_reg = 0xF;     /* VAX PC.  */
02642                          }
02643                        else
02644                          {
02645                            /* Encode short literal now.  */
02646                            int temp = 0;
02647 
02648                            switch (-expP->X_add_number)
02649                             {
02650                             case 'f':
02651                             case 'd':
02652                               temp = literal_float[0] >> 4;
02653                               break;
02654 
02655                             case 'g':
02656                               temp = literal_float[0] >> 1;
02657                               break;
02658 
02659                             case 'h':
02660                               temp = ((literal_float[0] << 3) & 070)
02661                                 | ((literal_float[1] >> 13) & 07);
02662                               break;
02663 
02664                             default:
02665                               BAD_CASE (-expP->X_add_number);
02666                               break;
02667                             }
02668 
02669                            floatP->low[0] = temp & 077;
02670                            floatP->low[1] = 0;
02671                          }
02672                      }
02673                   }
02674                 else
02675                   {
02676                     /* I^# seen: set it up if float.  */
02677                     if (expP->X_add_number < 0)
02678                      {
02679                        memcpy (floatP->low, literal_float, sizeof (literal_float));
02680                      }
02681                   }         /* if S^# seen.  */
02682               }
02683              else
02684               {
02685                 as_warn (_("A bignum/flonum may not be a displacement: 0x%lx used"),
02686                         (expP->X_add_number = 0x80000000L));
02687                 /* Chosen so luser gets the most offset bits to patch later.  */
02688               }
02689              expP->X_add_number = floatP->low[0]
02690               | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
02691 
02692              /* For the O_big case we have:
02693                 If vop_short == 's' then a short floating literal is in the
02694                      lowest 6 bits of floatP -> low [0], which is
02695                      big_operand_bits [---] [0].
02696                 If vop_short == 'i' then the appropriate number of elements
02697                      of big_operand_bits [---] [...] are set up with the correct
02698                      bits.
02699                 Also, just in case width is byte word or long, we copy the lowest
02700                 32 bits of the number to X_add_number.  */
02701              break;
02702            }
02703          if (input_line_pointer != operandP->vop_expr_end + 1)
02704            {
02705              as_fatal ("Junk at end of expression \"%s\"", input_line_pointer);
02706              goofed = 1;
02707            }
02708          operandP->vop_expr_end[1] = c_save;
02709        }
02710     }
02711 
02712   input_line_pointer = save_input_line_pointer;
02713 
02714   if (need_pass_2 || goofed)
02715     return;
02716 
02717   /* Emit op-code.  */
02718   /* Remember where it is, in case we want to modify the op-code later.  */
02719   opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
02720   memcpy (opcode_low_byteP, v.vit_opcode, v.vit_opcode_nbytes);
02721   opcode_as_chars = v.vit_opcode;
02722   opcode_as_number = md_chars_to_number ((unsigned char *) opcode_as_chars, 4);
02723   for (operandP = v.vit_operand,
02724        expP = exp_of_operand,
02725        segP = seg_of_operand,
02726        floatP = float_operand,
02727        end_operandP = v.vit_operand + v.vit_operands;
02728 
02729        operandP < end_operandP;
02730 
02731        operandP++,
02732        floatP++,
02733        segP++,
02734        expP++)
02735     {
02736       if (operandP->vop_ndx >= 0)
02737        {
02738          /* Indexed addressing byte.  */
02739          /* Legality of indexed mode already checked: it is OK.  */
02740          FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
02741        }                    /* if(vop_ndx>=0) */
02742 
02743       /* Here to make main operand frag(s).  */
02744       this_add_number = expP->X_add_number;
02745       this_add_symbol = expP->X_add_symbol;
02746       to_seg = *segP;
02747       is_undefined = (to_seg == undefined_section);
02748       is_absolute = (to_seg == absolute_section);
02749       at = operandP->vop_mode & 1;
02750       length = (operandP->vop_short == 'b'
02751               ? 1 : (operandP->vop_short == 'w'
02752                      ? 2 : (operandP->vop_short == 'l'
02753                            ? 4 : 0)));
02754       nbytes = operandP->vop_nbytes;
02755       if (operandP->vop_access == 'b')
02756        {
02757          if (to_seg == now_seg || is_undefined)
02758            {
02759              /* If is_undefined, then it might BECOME now_seg.  */
02760              if (nbytes)
02761               {
02762                 p = frag_more (nbytes);
02763                 fix_new (frag_now, p - frag_now->fr_literal, nbytes,
02764                         this_add_symbol, this_add_number, 1, NO_RELOC);
02765               }
02766              else
02767               {
02768                 /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
02769                 /* nbytes==0 */
02770                 length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
02771                 if (opcode_as_number & VIT_OPCODE_SPECIAL)
02772                   {
02773                     if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
02774                      {
02775                        /* br or jsb */
02776                        frag_var (rs_machine_dependent, 5, 1,
02777                          ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
02778                                 this_add_symbol, this_add_number,
02779                                 opcode_low_byteP);
02780                      }
02781                     else
02782                      {
02783                        if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
02784                          {
02785                            length_code = STATE_WORD;
02786                            /* JF: There is no state_byte for this one! */
02787                            frag_var (rs_machine_dependent, 10, 2,
02788                                    ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
02789                                    this_add_symbol, this_add_number,
02790                                    opcode_low_byteP);
02791                          }
02792                        else
02793                          {
02794                            know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
02795                            frag_var (rs_machine_dependent, 9, 1,
02796                            ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
02797                                    this_add_symbol, this_add_number,
02798                                    opcode_low_byteP);
02799                          }
02800                      }
02801                   }
02802                 else
02803                   {
02804                     know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
02805                     frag_var (rs_machine_dependent, 7, 1,
02806                      ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
02807                             this_add_symbol, this_add_number,
02808                             opcode_low_byteP);
02809                   }
02810               }
02811            }
02812          else
02813            {
02814              /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
02815              /* --- SEG FLOAT MAY APPEAR HERE ---  */
02816              if (is_absolute)
02817               {
02818                 if (nbytes)
02819                   {
02820                     know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
02821                     p = frag_more (nbytes);
02822                     /* Conventional relocation.  */
02823                     fix_new (frag_now, p - frag_now->fr_literal, nbytes,
02824                             section_symbol (absolute_section),
02825                             this_add_number, 1, NO_RELOC);
02826                   }
02827                 else
02828                   {
02829                     know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
02830                     if (opcode_as_number & VIT_OPCODE_SPECIAL)
02831                      {
02832                        if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
02833                          {
02834                            /* br or jsb */
02835                            *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
02836                            know (opcode_as_chars[1] == 0);
02837                            p = frag_more (5);
02838                            p[0] = VAX_ABSOLUTE_MODE;    /* @#...  */
02839                            md_number_to_chars (p + 1, this_add_number, 4);
02840                            /* Now (eg) JMP @#foo or JSB @#foo.  */
02841                          }
02842                        else
02843                          {
02844                            if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
02845                             {
02846                               p = frag_more (10);
02847                               p[0] = 2;
02848                               p[1] = 0;
02849                               p[2] = VAX_BRB;
02850                               p[3] = 6;
02851                               p[4] = VAX_JMP;
02852                               p[5] = VAX_ABSOLUTE_MODE; /* @#...  */
02853                               md_number_to_chars (p + 6, this_add_number, 4);
02854                               /* Now (eg) ACBx   1f
02855                                           BRB    2f
02856                                    1:     JMP    @#foo
02857                                    2:  */
02858                             }
02859                            else
02860                             {
02861                               know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
02862                               p = frag_more (9);
02863                               p[0] = 2;
02864                               p[1] = VAX_BRB;
02865                               p[2] = 6;
02866                               p[3] = VAX_JMP;
02867                                   p[4] = VAX_ABSOLUTE_MODE;     /* @#...  */
02868                               md_number_to_chars (p + 5, this_add_number, 4);
02869                               /* Now (eg) xOBxxx 1f
02870                                           BRB    2f
02871                                    1:     JMP    @#foo
02872                                    2:  */
02873                             }
02874                          }
02875                      }
02876                     else
02877                      {
02878                        /* b<cond> */
02879                        *opcode_low_byteP ^= 1;
02880                        /* To reverse the condition in a VAX branch,
02881                           complement the lowest order bit.  */
02882                        p = frag_more (7);
02883                        p[0] = 6;
02884                        p[1] = VAX_JMP;
02885                        p[2] = VAX_ABSOLUTE_MODE; /* @#...  */
02886                        md_number_to_chars (p + 3, this_add_number, 4);
02887                        /* Now (eg) BLEQ   1f
02888                                    JMP    @#foo
02889                             1:  */
02890                      }
02891                   }
02892               }
02893              else
02894               {
02895                 /* to_seg != now_seg && !is_undefinfed && !is_absolute */
02896                 if (nbytes > 0)
02897                   {
02898                     /* Pc-relative. Conventional relocation.  */
02899                     know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
02900                     p = frag_more (nbytes);
02901                     fix_new (frag_now, p - frag_now->fr_literal, nbytes,
02902                             section_symbol (absolute_section),
02903                             this_add_number, 1, NO_RELOC);
02904                   }
02905                 else
02906                   {
02907                     know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
02908                     if (opcode_as_number & VIT_OPCODE_SPECIAL)
02909                      {
02910                        if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
02911                          {
02912                            /* br or jsb */
02913                            know (opcode_as_chars[1] == 0);
02914                            *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
02915                            p = frag_more (5);
02916                            p[0] = VAX_PC_RELATIVE_MODE;
02917                            fix_new (frag_now,
02918                                    p + 1 - frag_now->fr_literal, 4,
02919                                    this_add_symbol,
02920                                    this_add_number, 1, NO_RELOC);
02921                            /* Now eg JMP foo or JSB foo.  */
02922                          }
02923                        else
02924                          {
02925                            if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
02926                             {
02927                               p = frag_more (10);
02928                               p[0] = 0;
02929                               p[1] = 2;
02930                               p[2] = VAX_BRB;
02931                               p[3] = 6;
02932                               p[4] = VAX_JMP;
02933                               p[5] = VAX_PC_RELATIVE_MODE;
02934                               fix_new (frag_now,
02935                                       p + 6 - frag_now->fr_literal, 4,
02936                                       this_add_symbol,
02937                                       this_add_number, 1, NO_RELOC);
02938                               /* Now (eg) ACBx   1f
02939                                           BRB    2f
02940                                    1:     JMP    foo
02941                                    2:  */
02942                             }
02943                            else
02944                             {
02945                               know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
02946                               p = frag_more (10);
02947                               p[0] = 2;
02948                               p[1] = VAX_BRB;
02949                               p[2] = 6;
02950                               p[3] = VAX_JMP;
02951                               p[4] = VAX_PC_RELATIVE_MODE;
02952                               fix_new (frag_now,
02953                                       p + 5 - frag_now->fr_literal,
02954                                       4, this_add_symbol,
02955                                       this_add_number, 1, NO_RELOC);
02956                               /* Now (eg) xOBxxx 1f
02957                                           BRB    2f
02958                                    1:     JMP    foo
02959                                    2:  */
02960                             }
02961                          }
02962                      }
02963                     else
02964                      {
02965                        know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
02966                        *opcode_low_byteP ^= 1;   /* Reverse branch condition.  */
02967                        p = frag_more (7);
02968                        p[0] = 6;
02969                        p[1] = VAX_JMP;
02970                        p[2] = VAX_PC_RELATIVE_MODE;
02971                        fix_new (frag_now, p + 3 - frag_now->fr_literal,
02972                                4, this_add_symbol,
02973                                this_add_number, 1, NO_RELOC);
02974                      }
02975                   }
02976               }
02977            }
02978        }
02979       else
02980        {
02981          /* So it is ordinary operand.  */
02982          know (operandP->vop_access != 'b');
02983          /* ' ' target-independent: elsewhere.  */
02984          know (operandP->vop_access != ' ');
02985          know (operandP->vop_access == 'a'
02986               || operandP->vop_access == 'm'
02987               || operandP->vop_access == 'r'
02988               || operandP->vop_access == 'v'
02989               || operandP->vop_access == 'w');
02990          if (operandP->vop_short == 's')
02991            {
02992              if (is_absolute)
02993               {
02994                 if (this_add_number >= 64)
02995                   {
02996                     as_warn (_("Short literal overflow(%ld.), immediate mode assumed."),
02997                             (long) this_add_number);
02998                     operandP->vop_short = 'i';
02999                     operandP->vop_mode = 8;
03000                     operandP->vop_reg = 0xF;
03001                   }
03002               }
03003              else
03004               {
03005                 as_warn (_("Forced short literal to immediate mode. now_seg=%s to_seg=%s"),
03006                         segment_name (now_seg), segment_name (to_seg));
03007                 operandP->vop_short = 'i';
03008                 operandP->vop_mode = 8;
03009                 operandP->vop_reg = 0xF;
03010               }
03011            }
03012          if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
03013                 || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
03014            {
03015              /* One byte operand.  */
03016              know (operandP->vop_mode > 3);
03017              FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
03018              /* All 1-bytes except S^# happen here.  */
03019            }
03020          else
03021            {
03022              /* {@}{q^}foo{(Rn)} or S^#foo */
03023              if (operandP->vop_reg == -1 && operandP->vop_short != 's')
03024               {
03025                 /* "{@}{q^}foo" */
03026                 if (to_seg == now_seg)
03027                   {
03028                     if (length == 0)
03029                      {
03030                        know (operandP->vop_short == ' ');
03031                        length_code = STATE_BYTE;
03032 #ifdef OBJ_ELF
03033                        if (S_IS_EXTERNAL (this_add_symbol)
03034                            || S_IS_WEAK (this_add_symbol))
03035                          length_code = STATE_UNDF;
03036 #endif
03037                        p = frag_var (rs_machine_dependent, 10, 2,
03038                             ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
03039                                    this_add_symbol, this_add_number,
03040                                    opcode_low_byteP);
03041                        know (operandP->vop_mode == 10 + at);
03042                        *p = at << 4;
03043                        /* At is the only context we need to carry
03044                           to other side of relax() process.  Must
03045                           be in the correct bit position of VAX
03046                           operand spec. byte.  */
03047                      }
03048                     else
03049                      {
03050                        know (length);
03051                        know (operandP->vop_short != ' ');
03052                        p = frag_more (length + 1);
03053                        p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
03054                        fix_new (frag_now, p + 1 - frag_now->fr_literal,
03055                                length, this_add_symbol,
03056                                this_add_number, 1, NO_RELOC);
03057                      }
03058                   }
03059                 else
03060                   {
03061                     /* to_seg != now_seg */
03062                     if (this_add_symbol == NULL)
03063                      {
03064                        know (is_absolute);
03065                        /* Do @#foo: simpler relocation than foo-.(pc) anyway.  */
03066                        p = frag_more (5);
03067                        p[0] = VAX_ABSOLUTE_MODE; /* @#...  */
03068                        md_number_to_chars (p + 1, this_add_number, 4);
03069                        if (length && length != 4)
03070                          as_warn (_("Length specification ignored. Address mode 9F used"));
03071                      }
03072                     else
03073                      {
03074                        /* {@}{q^}other_seg */
03075                        know ((length == 0 && operandP->vop_short == ' ')
03076                           || (length > 0 && operandP->vop_short != ' '));
03077                        if (is_undefined
03078 #ifdef OBJ_ELF
03079                            || S_IS_WEAK(this_add_symbol)
03080                            || S_IS_EXTERNAL(this_add_symbol)
03081 #endif
03082                            )
03083                          {
03084                            switch (length)
03085                             {
03086                             default: length_code = STATE_UNDF; break;
03087                             case 1: length_code = STATE_BYTE; break;
03088                             case 2: length_code = STATE_WORD; break;
03089                             case 4: length_code = STATE_LONG; break;
03090                             }
03091                            /* We have a SEG_UNKNOWN symbol. It might
03092                               turn out to be in the same segment as
03093                               the instruction, permitting relaxation.  */
03094                            p = frag_var (rs_machine_dependent, 5, 2,
03095                             ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
03096                                        this_add_symbol, this_add_number,
03097                                        opcode_low_byteP);
03098                            p[0] = at << 4;
03099                          }
03100                        else
03101                          {
03102                            if (length == 0)
03103                             {
03104                               know (operandP->vop_short == ' ');
03105                               length = 4; /* Longest possible.  */
03106                             }
03107                            p = frag_more (length + 1);
03108                            p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
03109                            md_number_to_chars (p + 1, this_add_number, length);
03110                            fix_new (frag_now,
03111                                    p + 1 - frag_now->fr_literal,
03112                                    length, this_add_symbol,
03113                                    this_add_number, 1, NO_RELOC);
03114                          }
03115                      }
03116                   }
03117               }
03118              else
03119               {
03120                 /* {@}{q^}foo(Rn) or S^# or I^# or # */
03121                 if (operandP->vop_mode < 0xA)
03122                   {
03123                     /* # or S^# or I^# */
03124                     if (operandP->vop_access == 'v'
03125                        || operandP->vop_access == 'a')
03126                      {
03127                        if (operandP->vop_access == 'v')
03128                          as_warn (_("Invalid operand:  immediate value used as base address."));
03129                        else
03130                          as_warn (_("Invalid operand:  immediate value used as address."));
03131                        /* gcc 2.6.3 is known to generate these in at least
03132                           one case.  */
03133                      }
03134                     if (length == 0
03135                        && is_absolute && (expP->X_op != O_big)
03136                        && operandP->vop_mode == 8       /* No '@'.  */
03137                        && this_add_number < 64)
03138                      {
03139                        operandP->vop_short = 's';
03140                      }
03141                     if (operandP->vop_short == 's')
03142                      {
03143                        FRAG_APPEND_1_CHAR (this_add_number);
03144                      }
03145                     else
03146                      {
03147                        /* I^#...  */
03148                        know (nbytes);
03149                        p = frag_more (nbytes + 1);
03150                        know (operandP->vop_reg == 0xF);
03151 #ifdef OBJ_ELF
03152                        if (flag_want_pic && operandP->vop_mode == 8
03153                             && this_add_symbol != NULL)
03154                          {
03155                            as_warn (_("Symbol used as immediate operand in PIC mode."));
03156                          }
03157 #endif
03158                        p[0] = (operandP->vop_mode << 4) | 0xF;
03159                        if ((is_absolute) && (expP->X_op != O_big))
03160                          {
03161                            /* If nbytes > 4, then we are scrod. We
03162                               don't know if the high order bytes
03163                               are to be 0xFF or 0x00.  BSD4.2 & RMS
03164                               say use 0x00. OK --- but this
03165                               assembler needs ANOTHER rewrite to
03166                               cope properly with this bug.  */
03167                            md_number_to_chars (p + 1, this_add_number,
03168                                             min (sizeof (valueT),
03169                                                  (size_t) nbytes));
03170                            if ((size_t) nbytes > sizeof (valueT))
03171                             memset (p + 5, '\0', nbytes - sizeof (valueT));
03172                          }
03173                        else
03174                          {
03175                            if (expP->X_op == O_big)
03176                             {
03177                               /* Problem here is to get the bytes
03178                                  in the right order.  We stored
03179                                  our constant as LITTLENUMs, not
03180                                  bytes.  */
03181                               LITTLENUM_TYPE *lP;
03182 
03183                               lP = floatP->low;
03184                               if (nbytes & 1)
03185                                 {
03186                                   know (nbytes == 1);
03187                                   p[1] = *lP;
03188                                 }
03189                               else
03190                                 {
03191                                   for (p++; nbytes; nbytes -= 2, p += 2, lP++)
03192                                    md_number_to_chars (p, *lP, 2);
03193                                 }
03194                             }
03195                            else
03196                             {
03197                               fix_new (frag_now, p + 1 - frag_now->fr_literal,
03198                                       nbytes, this_add_symbol,
03199                                       this_add_number, 0, NO_RELOC);
03200                             }
03201                          }
03202                      }
03203                   }
03204                 else
03205                   {
03206                     /* {@}{q^}foo(Rn) */
03207                     know ((length == 0 && operandP->vop_short == ' ')
03208                          || (length > 0 && operandP->vop_short != ' '));
03209                     if (length == 0)
03210                      {
03211                        if (is_absolute)
03212                          {
03213                            long test;
03214 
03215                            test = this_add_number;
03216 
03217                            if (test < 0)
03218                             test = ~test;
03219 
03220                            length = test & 0xffff8000 ? 4
03221                             : test & 0xffffff80 ? 2
03222                             : 1;
03223                          }
03224                        else
03225                          {
03226                            length = 4;
03227                          }
03228                      }
03229                     p = frag_more (1 + length);
03230                     know (operandP->vop_reg >= 0);
03231                     p[0] = operandP->vop_reg
03232                      | ((at | "?\12\14?\16"[length]) << 4);
03233                     if (is_absolute)
03234                      {
03235                        md_number_to_chars (p + 1, this_add_number, length);
03236                      }
03237                     else
03238                      {
03239                        fix_new (frag_now, p + 1 - frag_now->fr_literal,
03240                                length, this_add_symbol,
03241                                this_add_number, 0, NO_RELOC);
03242                      }
03243                   }
03244               }
03245            }
03246        }
03247     }
03248 }
03249 
03250 void
03251 md_begin (void)
03252 {
03253   const char *errtxt;
03254   FLONUM_TYPE *fP;
03255   int i;
03256 
03257   if ((errtxt = vip_begin (1, "$", "*", "`")) != 0)
03258     as_fatal (_("VIP_BEGIN error:%s"), errtxt);
03259 
03260   for (i = 0, fP = float_operand;
03261        fP < float_operand + VIT_MAX_OPERANDS;
03262        i++, fP++)
03263     {
03264       fP->low = &big_operand_bits[i][0];
03265       fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
03266     }
03267 }