Home | History | Annotate | Line # | Download | only in config
tc-vax.c revision 1.1
      1 /* tc-vax.c - vax-specific -
      2    Copyright 1987, 1991, 1992, 1993, 1994, 1995, 1998, 2000, 2001, 2002,
      3    2003, 2004, 2005, 2006, 2007, 2008
      4    Free Software Foundation, Inc.
      5 
      6    This file is part of GAS, the GNU Assembler.
      7 
      8    GAS is free software; you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation; either version 3, or (at your option)
     11    any later version.
     12 
     13    GAS is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with GAS; see the file COPYING.  If not, write to the Free
     20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     21    02110-1301, USA.  */
     22 
     23 #include "as.h"
     24 
     25 #include "vax-inst.h"
     26 #include "obstack.h"		/* For FRAG_APPEND_1_CHAR macro in "frags.h" */
     27 #include "subsegs.h"
     28 #include "safe-ctype.h"
     29 
     30 #ifdef OBJ_ELF
     31 #include "elf/vax.h"
     32 #endif
     33 
     34 /* These chars start a comment anywhere in a source file (except inside
     35    another comment */
     36 const char comment_chars[] = "#";
     37 
     38 /* These chars only start a comment at the beginning of a line.  */
     39 /* Note that for the VAX the are the same as comment_chars above.  */
     40 const char line_comment_chars[] = "#";
     41 
     42 const char line_separator_chars[] = ";";
     43 
     44 /* Chars that can be used to separate mant from exp in floating point nums.  */
     45 const char EXP_CHARS[] = "eE";
     46 
     47 /* Chars that mean this number is a floating point constant
     48    as in 0f123.456
     49    or    0H1.234E-12 (see exp chars above).  */
     50 const char FLT_CHARS[] = "dDfFgGhH";
     51 
     52 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
     53    changed in read.c .  Ideally it shouldn't have to know about it at all,
     54    but nothing is ideal around here.  */
     55 
     56 /* Hold details of an operand expression.  */
     57 static expressionS exp_of_operand[VIT_MAX_OPERANDS];
     58 static segT seg_of_operand[VIT_MAX_OPERANDS];
     59 
     60 /* A vax instruction after decoding.  */
     61 static struct vit v;
     62 
     63 /* Hold details of big operands.  */
     64 LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER];
     65 FLONUM_TYPE float_operand[VIT_MAX_OPERANDS];
     66 /* Above is made to point into big_operand_bits by md_begin().  */
     67 
     68 #ifdef OBJ_ELF
     69 #define GLOBAL_OFFSET_TABLE_NAME	"_GLOBAL_OFFSET_TABLE_"
     70 #define PROCEDURE_LINKAGE_TABLE_NAME	"_PROCEDURE_LINKAGE_TABLE_"
     71 symbolS *GOT_symbol;		/* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
     72 symbolS *PLT_symbol;		/* Pre-defined "_PROCEDURE_LINKAGE_TABLE_".  */
     73 #endif
     74 
     75 int flag_hash_long_names;	/* -+ */
     76 int flag_one;			/* -1 */
     77 int flag_show_after_trunc;	/* -H */
     78 int flag_no_hash_mixed_case;	/* -h NUM */
     79 #ifdef OBJ_ELF
     80 int flag_want_pic;		/* -k */
     81 #endif
     82 
     83 /* For VAX, relative addresses of "just the right length" are easy.
     85    The branch displacement is always the last operand, even in
     86    synthetic instructions.
     87    For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
     88 
     89   		    4       3       2       1       0	     bit number
     90   	---/ /--+-------+-------+-------+-------+-------+
     91   		|     what state ?	|  how long ?	|
     92   	---/ /--+-------+-------+-------+-------+-------+
     93 
     94    The "how long" bits are 00=byte, 01=word, 10=long.
     95    This is a Un*x convention.
     96    Not all lengths are legit for a given value of (what state).
     97    The "how long" refers merely to the displacement length.
     98    The address usually has some constant bytes in it as well.
     99 
    100  groups for VAX address relaxing.
    101 
    102  1.	"foo" pc-relative.
    103  length of byte, word, long
    104 
    105  2a.	J<cond> where <cond> is a simple flag test.
    106  length of byte, word, long.
    107  VAX opcodes are:	(Hex)
    108  bneq/bnequ	12
    109  beql/beqlu	13
    110  bgtr		14
    111  bleq		15
    112  bgeq		18
    113  blss		19
    114  bgtru		1a
    115  blequ		1b
    116  bvc		1c
    117  bvs		1d
    118  bgequ/bcc	1e
    119  blssu/bcs	1f
    120  Always, you complement 0th bit to reverse condition.
    121  Always, 1-byte opcode, then 1-byte displacement.
    122 
    123  2b.	J<cond> where cond tests a memory bit.
    124  length of byte, word, long.
    125  Vax opcodes are:	(Hex)
    126  bbs		e0
    127  bbc		e1
    128  bbss		e2
    129  bbcs		e3
    130  bbsc		e4
    131  bbcc		e5
    132  Always, you complement 0th bit to reverse condition.
    133  Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
    134 
    135  2c.	J<cond> where cond tests low-order memory bit
    136  length of byte,word,long.
    137  Vax opcodes are:	(Hex)
    138  blbs		e8
    139  blbc		e9
    140  Always, you complement 0th bit to reverse condition.
    141  Always, 1-byte opcode, longword-address, 1-byte displacement.
    142 
    143  3.	Jbs/Jbr.
    144  length of byte,word,long.
    145  Vax opcodes are:	(Hex)
    146  bsbb		10
    147  brb		11
    148  These are like (2) but there is no condition to reverse.
    149  Always, 1 byte opcode, then displacement/absolute.
    150 
    151  4a.	JacbX
    152  length of word, long.
    153  Vax opcodes are:	(Hex)
    154  acbw		3d
    155  acbf		4f
    156  acbd		6f
    157  abcb		9d
    158  acbl		f1
    159  acbg	      4ffd
    160  acbh	      6ffd
    161  Always, we cannot reverse the sense of the branch; we have a word
    162  displacement.
    163  The double-byte op-codes don't hurt: we never want to modify the
    164  opcode, so we don't care how many bytes are between the opcode and
    165  the operand.
    166 
    167  4b.	JXobXXX
    168  length of long, long, byte.
    169  Vax opcodes are:	(Hex)
    170  aoblss		f2
    171  aobleq		f3
    172  sobgeq		f4
    173  sobgtr		f5
    174  Always, we cannot reverse the sense of the branch; we have a byte
    175  displacement.
    176 
    177  The only time we need to modify the opcode is for class 2 instructions.
    178  After relax() we may complement the lowest order bit of such instruction
    179  to reverse sense of branch.
    180 
    181  For class 2 instructions, we store context of "where is the opcode literal".
    182  We can change an opcode's lowest order bit without breaking anything else.
    183 
    184  We sometimes store context in the operand literal. This way we can figure out
    185  after relax() what the original addressing mode was.  */
    186 
    187 /* These displacements are relative to the start address of the
    189    displacement.  The first letter is Byte, Word.  2nd letter is
    190    Forward, Backward.  */
    191 #define BF (1+ 127)
    192 #define BB (1+-128)
    193 #define WF (2+ 32767)
    194 #define WB (2+-32768)
    195 /* Dont need LF, LB because they always reach. [They are coded as 0.]  */
    196 
    197 #define C(a,b) ENCODE_RELAX(a,b)
    198 /* This macro has no side-effects.  */
    199 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
    200 #define RELAX_STATE(s) ((s) >> 2)
    201 #define RELAX_LENGTH(s) ((s) & 3)
    202 
    203 const relax_typeS md_relax_table[] =
    204 {
    205   {1, 1, 0, 0},			/* error sentinel   0,0	*/
    206   {1, 1, 0, 0},			/* unused	    0,1	*/
    207   {1, 1, 0, 0},			/* unused	    0,2	*/
    208   {1, 1, 0, 0},			/* unused	    0,3	*/
    209 
    210   {BF + 1, BB + 1, 2, C (1, 1)},/* B^"foo"	    1,0 */
    211   {WF + 1, WB + 1, 3, C (1, 2)},/* W^"foo"	    1,1 */
    212   {0, 0, 5, 0},			/* L^"foo"	    1,2 */
    213   {1, 1, 0, 0},			/* unused	    1,3 */
    214 
    215   {BF, BB, 1, C (2, 1)},	/* b<cond> B^"foo"  2,0 */
    216   {WF + 2, WB + 2, 4, C (2, 2)},/* br.+? brw X	    2,1 */
    217   {0, 0, 7, 0},			/* br.+? jmp X	    2,2 */
    218   {1, 1, 0, 0},			/* unused	    2,3 */
    219 
    220   {BF, BB, 1, C (3, 1)},	/* brb B^foo	    3,0 */
    221   {WF, WB, 2, C (3, 2)},	/* brw W^foo	    3,1 */
    222   {0, 0, 5, 0},			/* Jmp L^foo	    3,2 */
    223   {1, 1, 0, 0},			/* unused	    3,3 */
    224 
    225   {1, 1, 0, 0},			/* unused	    4,0 */
    226   {WF, WB, 2, C (4, 2)},	/* acb_ ^Wfoo	    4,1 */
    227   {0, 0, 10, 0},		/* acb_,br,jmp L^foo4,2 */
    228   {1, 1, 0, 0},			/* unused	    4,3 */
    229 
    230   {BF, BB, 1, C (5, 1)},	/* Xob___,,foo      5,0 */
    231   {WF + 4, WB + 4, 6, C (5, 2)},/* Xob.+2,brb.+3,brw5,1 */
    232   {0, 0, 9, 0},			/* Xob.+2,brb.+6,jmp5,2 */
    233   {1, 1, 0, 0},			/* unused	    5,3 */
    234 };
    235 
    236 #undef C
    237 #undef BF
    238 #undef BB
    239 #undef WF
    240 #undef WB
    241 
    242 void float_cons (int);
    243 int flonum_gen2vax (char, FLONUM_TYPE *, LITTLENUM_TYPE *);
    244 
    245 const pseudo_typeS md_pseudo_table[] =
    246 {
    247   {"dfloat", float_cons, 'd'},
    248   {"ffloat", float_cons, 'f'},
    249   {"gfloat", float_cons, 'g'},
    250   {"hfloat", float_cons, 'h'},
    251   {"d_floating", float_cons, 'd'},
    252   {"f_floating", float_cons, 'f'},
    253   {"g_floating", float_cons, 'g'},
    254   {"h_floating", float_cons, 'h'},
    255   {NULL, NULL, 0},
    256 };
    257 
    258 #define STATE_PC_RELATIVE		(1)
    259 #define STATE_CONDITIONAL_BRANCH	(2)
    260 #define STATE_ALWAYS_BRANCH		(3)	/* includes BSB...  */
    261 #define STATE_COMPLEX_BRANCH	        (4)
    262 #define STATE_COMPLEX_HOP		(5)
    263 
    264 #define STATE_BYTE			(0)
    265 #define STATE_WORD			(1)
    266 #define STATE_LONG			(2)
    267 #define STATE_UNDF			(3)	/* Symbol undefined in pass1.  */
    268 
    269 #define min(a, b)	((a) < (b) ? (a) : (b))
    270 
    271 void
    273 md_number_to_chars (char con[], valueT value, int nbytes)
    274 {
    275   number_to_chars_littleendian (con, value, nbytes);
    276 }
    277 
    278 /* Fix up some data or instructions after we find out the value of a symbol
    279    that they reference.  */
    280 
    281 void				/* Knows about order of bytes in address.  */
    282 md_apply_fix (fixS *fixP, valueT *valueP, segT seg ATTRIBUTE_UNUSED)
    283 {
    284   valueT value = * valueP;
    285 
    286   if (((fixP->fx_addsy == NULL && fixP->fx_subsy == NULL)
    287        && fixP->fx_r_type != BFD_RELOC_32_PLT_PCREL
    288        && fixP->fx_r_type != BFD_RELOC_32_GOT_PCREL)
    289       || fixP->fx_r_type == NO_RELOC)
    290     number_to_chars_littleendian (fixP->fx_where + fixP->fx_frag->fr_literal,
    291 				  value, fixP->fx_size);
    292 
    293   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
    294     fixP->fx_done = 1;
    295 }
    296 
    297 /* Convert a number from VAX byte order (little endian)
    298    into host byte order.
    299    con		is the buffer to convert,
    300    nbytes	is the length of the given buffer.  */
    301 static long
    302 md_chars_to_number (unsigned char con[], int nbytes)
    303 {
    304   long retval;
    305 
    306   for (retval = 0, con += nbytes - 1; nbytes--; con--)
    307     {
    308       retval <<= BITS_PER_CHAR;
    309       retval |= *con;
    310     }
    311   return retval;
    312 }
    313 
    314 /* Copy a bignum from in to out.
    315    If the output is shorter than the input, copy lower-order
    316    littlenums.  Return 0 or the number of significant littlenums
    317    dropped.  Assumes littlenum arrays are densely packed: no unused
    318    chars between the littlenums. Uses memcpy() to move littlenums, and
    319    wants to know length (in chars) of the input bignum.  */
    320 
    321 static int
    322 bignum_copy (LITTLENUM_TYPE *in,
    323 	     int in_length,	/* in sizeof(littlenum)s */
    324 	     LITTLENUM_TYPE *out,
    325 	     int out_length	/* in sizeof(littlenum)s */)
    326 {
    327   int significant_littlenums_dropped;
    328 
    329   if (out_length < in_length)
    330     {
    331       LITTLENUM_TYPE *p;	/* -> most significant (non-zero) input
    332 				      littlenum.  */
    333 
    334       memcpy ((void *) out, (void *) in,
    335 	      (unsigned int) out_length << LITTLENUM_SHIFT);
    336       for (p = in + in_length - 1; p >= in; --p)
    337 	{
    338 	  if (*p)
    339 	    break;
    340 	}
    341       significant_littlenums_dropped = p - in - in_length + 1;
    342 
    343       if (significant_littlenums_dropped < 0)
    344 	significant_littlenums_dropped = 0;
    345     }
    346   else
    347     {
    348       memcpy ((char *) out, (char *) in,
    349 	      (unsigned int) in_length << LITTLENUM_SHIFT);
    350 
    351       if (out_length > in_length)
    352 	memset ((char *) (out + in_length), '\0',
    353 		(unsigned int) (out_length - in_length) << LITTLENUM_SHIFT);
    354 
    355       significant_littlenums_dropped = 0;
    356     }
    357 
    358   return significant_littlenums_dropped;
    359 }
    360 
    361 /* md_estimate_size_before_relax(), called just before relax().
    363    Any symbol that is now undefined will not become defined.
    364    Return the correct fr_subtype in the frag and the growth beyond
    365    fr_fix.  */
    366 int
    367 md_estimate_size_before_relax (fragS *fragP, segT segment)
    368 {
    369   if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
    370     {
    371       if (S_GET_SEGMENT (fragP->fr_symbol) != segment
    372 #ifdef OBJ_ELF
    373 	  || S_IS_WEAK (fragP->fr_symbol)
    374 	  || S_IS_EXTERNAL (fragP->fr_symbol)
    375 #endif
    376 	  )
    377 	{
    378 	  /* Non-relaxable cases.  */
    379 	  int reloc_type = NO_RELOC;
    380 	  char *p;
    381 	  int old_fr_fix;
    382 
    383 	  old_fr_fix = fragP->fr_fix;
    384 	  p = fragP->fr_literal + old_fr_fix;
    385 #ifdef OBJ_ELF
    386 	  /* If this is to an undefined symbol, then if it's an indirect
    387 	     reference indicate that is can mutated into a GLOB_DAT or
    388 	     JUMP_SLOT by the loader.  We restrict ourselves to no offset
    389 	     due to a limitation in the NetBSD linker.  */
    390 
    391 	  if (GOT_symbol == NULL)
    392 	    GOT_symbol = symbol_find (GLOBAL_OFFSET_TABLE_NAME);
    393 	  if (PLT_symbol == NULL)
    394 	    PLT_symbol = symbol_find (PROCEDURE_LINKAGE_TABLE_NAME);
    395 	  if ((GOT_symbol == NULL || fragP->fr_symbol != GOT_symbol)
    396 	      && (PLT_symbol == NULL || fragP->fr_symbol != PLT_symbol)
    397 	      && fragP->fr_symbol != NULL
    398 	      && flag_want_pic
    399 	      && (!S_IS_DEFINED (fragP->fr_symbol)
    400 	          || S_IS_WEAK (fragP->fr_symbol)
    401 	          || S_IS_EXTERNAL (fragP->fr_symbol)))
    402 	    {
    403 	      if (p[0] & 0x10)
    404 		{
    405 		  if (flag_want_pic)
    406 		    as_fatal ("PIC reference to %s is indirect.\n",
    407 			      S_GET_NAME (fragP->fr_symbol));
    408 		}
    409 	      else
    410 		{
    411 		  if (((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLS
    412 		      || ((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLG
    413 		      || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JSB
    414 		      || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JMP
    415 		      || S_IS_FUNCTION (fragP->fr_symbol))
    416 		    reloc_type = BFD_RELOC_32_PLT_PCREL;
    417 		  else
    418 		    reloc_type = BFD_RELOC_32_GOT_PCREL;
    419 		}
    420 	    }
    421 #endif
    422 	  switch (RELAX_STATE (fragP->fr_subtype))
    423 	    {
    424 	    case STATE_PC_RELATIVE:
    425 	      p[0] |= VAX_PC_RELATIVE_MODE;	/* Preserve @ bit.  */
    426 	      fragP->fr_fix += 1 + 4;
    427 	      fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
    428 		       fragP->fr_offset, 1, reloc_type);
    429 	      break;
    430 
    431 	    case STATE_CONDITIONAL_BRANCH:
    432 	      *fragP->fr_opcode ^= 1;		/* Reverse sense of branch.  */
    433 	      p[0] = 6;
    434 	      p[1] = VAX_JMP;
    435 	      p[2] = VAX_PC_RELATIVE_MODE;	/* ...(PC) */
    436 	      fragP->fr_fix += 1 + 1 + 1 + 4;
    437 	      fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol,
    438 		       fragP->fr_offset, 1, NO_RELOC);
    439 	      break;
    440 
    441 	    case STATE_COMPLEX_BRANCH:
    442 	      p[0] = 2;
    443 	      p[1] = 0;
    444 	      p[2] = VAX_BRB;
    445 	      p[3] = 6;
    446 	      p[4] = VAX_JMP;
    447 	      p[5] = VAX_PC_RELATIVE_MODE;	/* ...(pc) */
    448 	      fragP->fr_fix += 2 + 2 + 1 + 1 + 4;
    449 	      fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol,
    450 		       fragP->fr_offset, 1, NO_RELOC);
    451 	      break;
    452 
    453 	    case STATE_COMPLEX_HOP:
    454 	      p[0] = 2;
    455 	      p[1] = VAX_BRB;
    456 	      p[2] = 6;
    457 	      p[3] = VAX_JMP;
    458 	      p[4] = VAX_PC_RELATIVE_MODE;	/* ...(pc) */
    459 	      fragP->fr_fix += 1 + 2 + 1 + 1 + 4;
    460 	      fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol,
    461 		       fragP->fr_offset, 1, NO_RELOC);
    462 	      break;
    463 
    464 	    case STATE_ALWAYS_BRANCH:
    465 	      *fragP->fr_opcode += VAX_WIDEN_LONG;
    466 	      p[0] = VAX_PC_RELATIVE_MODE;	/* ...(PC) */
    467 	      fragP->fr_fix += 1 + 4;
    468 	      fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
    469 		       fragP->fr_offset, 1, NO_RELOC);
    470 	      break;
    471 
    472 	    default:
    473 	      abort ();
    474 	    }
    475 	  frag_wane (fragP);
    476 
    477 	  /* Return the growth in the fixed part of the frag.  */
    478 	  return fragP->fr_fix - old_fr_fix;
    479 	}
    480 
    481       /* Relaxable cases.  Set up the initial guess for the variable
    482 	 part of the frag.  */
    483       switch (RELAX_STATE (fragP->fr_subtype))
    484 	{
    485 	case STATE_PC_RELATIVE:
    486 	  fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
    487 	  break;
    488 	case STATE_CONDITIONAL_BRANCH:
    489 	  fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
    490 	  break;
    491 	case STATE_COMPLEX_BRANCH:
    492 	  fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
    493 	  break;
    494 	case STATE_COMPLEX_HOP:
    495 	  fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
    496 	  break;
    497 	case STATE_ALWAYS_BRANCH:
    498 	  fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
    499 	  break;
    500 	}
    501     }
    502 
    503   if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
    504     abort ();
    505 
    506   /* Return the size of the variable part of the frag.  */
    507   return md_relax_table[fragP->fr_subtype].rlx_length;
    508 }
    509 
    510 /* Called after relax() is finished.
    512    In:	Address of frag.
    513   	fr_type == rs_machine_dependent.
    514   	fr_subtype is what the address relaxed to.
    515 
    516    Out:	Any fixSs and constants are set up.
    517   	Caller will turn frag into a ".space 0".  */
    518 void
    519 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED,
    520 		 segT seg ATTRIBUTE_UNUSED,
    521 		 fragS *fragP)
    522 {
    523   char *addressP;		/* -> _var to change.  */
    524   char *opcodeP;		/* -> opcode char(s) to change.  */
    525   short int extension = 0;	/* Size of relaxed address.  */
    526   /* Added to fr_fix: incl. ALL var chars.  */
    527   symbolS *symbolP;
    528   long where;
    529 
    530   know (fragP->fr_type == rs_machine_dependent);
    531   where = fragP->fr_fix;
    532   addressP = fragP->fr_literal + where;
    533   opcodeP = fragP->fr_opcode;
    534   symbolP = fragP->fr_symbol;
    535   know (symbolP);
    536 
    537   switch (fragP->fr_subtype)
    538     {
    539     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
    540       know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
    541       addressP[0] |= 0xAF;	/* Byte displacement. */
    542       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
    543 	       fragP->fr_offset, 1, NO_RELOC);
    544       extension = 2;
    545       break;
    546 
    547     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
    548       know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
    549       addressP[0] |= 0xCF;	/* Word displacement. */
    550       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
    551 	       fragP->fr_offset, 1, NO_RELOC);
    552       extension = 3;
    553       break;
    554 
    555     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
    556       know (*addressP == 0 || *addressP == 0x10);	/* '@' bit.  */
    557       addressP[0] |= 0xEF;	/* Long word displacement. */
    558       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
    559 	       fragP->fr_offset, 1, NO_RELOC);
    560       extension = 5;
    561       break;
    562 
    563     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
    564       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
    565 	       fragP->fr_offset, 1, NO_RELOC);
    566       extension = 1;
    567       break;
    568 
    569     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
    570       opcodeP[0] ^= 1;		/* Reverse sense of test.  */
    571       addressP[0] = 3;
    572       addressP[1] = VAX_BRW;
    573       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
    574 	       fragP->fr_offset, 1, NO_RELOC);
    575       extension = 4;
    576       break;
    577 
    578     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
    579       opcodeP[0] ^= 1;		/* Reverse sense of test.  */
    580       addressP[0] = 6;
    581       addressP[1] = VAX_JMP;
    582       addressP[2] = VAX_PC_RELATIVE_MODE;
    583       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
    584 	       fragP->fr_offset, 1, NO_RELOC);
    585       extension = 7;
    586       break;
    587 
    588     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
    589       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
    590 	       fragP->fr_offset, 1, NO_RELOC);
    591       extension = 1;
    592       break;
    593 
    594     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
    595       opcodeP[0] += VAX_WIDEN_WORD;	/* brb -> brw, bsbb -> bsbw */
    596       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
    597 	       1, NO_RELOC);
    598       extension = 2;
    599       break;
    600 
    601     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
    602       opcodeP[0] += VAX_WIDEN_LONG;	/* brb -> jmp, bsbb -> jsb */
    603       addressP[0] = VAX_PC_RELATIVE_MODE;
    604       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
    605 	       fragP->fr_offset, 1, NO_RELOC);
    606       extension = 5;
    607       break;
    608 
    609     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
    610       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
    611 	       fragP->fr_offset, 1, NO_RELOC);
    612       extension = 2;
    613       break;
    614 
    615     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
    616       addressP[0] = 2;
    617       addressP[1] = 0;
    618       addressP[2] = VAX_BRB;
    619       addressP[3] = 6;
    620       addressP[4] = VAX_JMP;
    621       addressP[5] = VAX_PC_RELATIVE_MODE;
    622       fix_new (fragP, fragP->fr_fix + 6, 4, fragP->fr_symbol,
    623 	       fragP->fr_offset, 1, NO_RELOC);
    624       extension = 10;
    625       break;
    626 
    627     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
    628       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
    629 	       fragP->fr_offset, 1, NO_RELOC);
    630       extension = 1;
    631       break;
    632 
    633     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
    634       addressP[0] = 2;
    635       addressP[1] = VAX_BRB;
    636       addressP[2] = 3;
    637       addressP[3] = VAX_BRW;
    638       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
    639 	       fragP->fr_offset, 1, NO_RELOC);
    640       extension = 6;
    641       break;
    642 
    643     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
    644       addressP[0] = 2;
    645       addressP[1] = VAX_BRB;
    646       addressP[2] = 6;
    647       addressP[3] = VAX_JMP;
    648       addressP[4] = VAX_PC_RELATIVE_MODE;
    649       fix_new (fragP, fragP->fr_fix + 5, 4, fragP->fr_symbol,
    650 	       fragP->fr_offset, 1, NO_RELOC);
    651       extension = 9;
    652       break;
    653 
    654     default:
    655       BAD_CASE (fragP->fr_subtype);
    656       break;
    657     }
    658   fragP->fr_fix += extension;
    659 }
    660 
    661 /* Translate internal format of relocation info into target format.
    662 
    663    On vax: first 4 bytes are normal unsigned long, next three bytes
    664    are symbolnum, least sig. byte first.  Last byte is broken up with
    665    the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
    666    bit 0 as pcrel.  */
    667 #ifdef comment
    668 void
    669 md_ri_to_chars (char *the_bytes, struct reloc_info_generic ri)
    670 {
    671   /* This is easy.  */
    672   md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
    673   /* Now the fun stuff.  */
    674   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
    675   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
    676   the_bytes[4] = ri.r_symbolnum & 0x0ff;
    677   the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06)
    678 		  | ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
    679 }
    680 
    681 #endif /* comment */
    682 
    683 /*       BUGS, GRIPES,  APOLOGIA, etc.
    684 
    685    The opcode table 'votstrs' needs to be sorted on opcode frequency.
    686    That is, AFTER we hash it with hash_...(), we want most-used opcodes
    687    to come out of the hash table faster.
    688 
    689    I am sorry to inflict yet another VAX assembler on the world, but
    690    RMS says we must do everything from scratch, to prevent pin-heads
    691    restricting this software.
    692 
    693    This is a vaguely modular set of routines in C to parse VAX
    694    assembly code using DEC mnemonics. It is NOT un*x specific.
    695 
    696    The idea here is that the assembler has taken care of all:
    697      labels
    698      macros
    699      listing
    700      pseudo-ops
    701      line continuation
    702      comments
    703      condensing any whitespace down to exactly one space
    704    and all we have to do is parse 1 line into a vax instruction
    705    partially formed. We will accept a line, and deliver:
    706      an error message (hopefully empty)
    707      a skeleton VAX instruction (tree structure)
    708      textual pointers to all the operand expressions
    709      a warning message that notes a silly operand (hopefully empty)
    710 
    711   		E D I T   H I S T O R Y
    712 
    713    17may86 Dean Elsner. Bug if line ends immediately after opcode.
    714    30apr86 Dean Elsner. New vip_op() uses arg block so change call.
    715     6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
    716     2jan86 Dean Elsner. Invent synthetic opcodes.
    717   	Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
    718   	which means this is not a real opcode, it is like a macro; it will
    719   	be relax()ed into 1 or more instructions.
    720   	Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
    721   	like a regular branch instruction. Option added to vip_begin():
    722   	exclude	synthetic opcodes. Invent synthetic_votstrs[].
    723    31dec85 Dean Elsner. Invent vit_opcode_nbytes.
    724   	Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
    725   	so caller's don't have to know the difference between a 1-byte & a
    726   	2-byte op-code. Still need vax_opcodeT concept, so we know how
    727   	big an object must be to hold an op.code.
    728    30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
    729   	because vax opcodes may be 16 bits. Our crufty C compiler was
    730   	happily initialising 8-bit vot_codes with 16-bit numbers!
    731   	(Wouldn't the 'phone company like to compress data so easily!)
    732    29dec85 Dean Elsner. New static table vax_operand_width_size[].
    733   	Invented so we know hw many bytes a "I^#42" needs in its immediate
    734   	operand. Revised struct vop in "vax-inst.h": explicitly include
    735   	byte length of each operand, and it's letter-code datum type.
    736    17nov85 Dean Elsner. Name Change.
    737   	Due to ar(1) truncating names, we learned the hard way that
    738   	"vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
    739   	the archived object name. SO... we shortened the name of this
    740   	source file, and changed the makefile.  */
    741 
    742 /* Handle of the OPCODE hash table.  */
    743 static struct hash_control *op_hash;
    744 
    745 /* In:	1 character, from "bdfghloqpw" being the data-type of an operand
    746   	of a vax instruction.
    747 
    748    Out:	the length of an operand of that type, in bytes.
    749   	Special branch operands types "-?!" have length 0.  */
    750 
    751 static const short int vax_operand_width_size[256] =
    752 {
    753   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    754   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    755   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    756   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    757   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,	/* ..b.d.fgh...l..o  */
    758   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,	/* .q.....w........  */
    759   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,	/* ..b.d.fgh...l..o  */
    760   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,	/* .q.....w........  */
    761   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    762   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    763   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    764   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    765   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    766   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    767   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    768   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
    769 };
    770 
    771 /* This perversion encodes all the vax opcodes as a bunch of strings.
    773    RMS says we should build our hash-table at run-time. Hmm.
    774    Please would someone arrange these in decreasing frequency of opcode?
    775    Because of the way hash_...() works, the most frequently used opcode
    776    should be textually first and so on.
    777 
    778    Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
    779    So change 'vax.opcodes', then re-generate this table.  */
    780 
    781 #include "opcode/vax.h"
    782 
    783 /* This is a table of optional op-codes. All of them represent
    785    'synthetic' instructions that seem popular.
    786 
    787    Here we make some pseudo op-codes. Every code has a bit set to say
    788    it is synthetic. This lets you catch them if you want to
    789    ban these opcodes. They are mnemonics for "elastic" instructions
    790    that are supposed to assemble into the fewest bytes needed to do a
    791    branch, or to do a conditional branch, or whatever.
    792 
    793    The opcode is in the usual place [low-order n*8 bits]. This means
    794    that if you mask off the bucky bits, the usual rules apply about
    795    how long the opcode is.
    796 
    797    All VAX branch displacements come at the end of the instruction.
    798    For simple branches (1-byte opcode + 1-byte displacement) the last
    799    operand is coded 'b?' where the "data type" '?' is a clue that we
    800    may reverse the sense of the branch (complement lowest order bit)
    801    and branch around a jump. This is by far the most common case.
    802    That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
    803    a 0-byte op-code followed by 2 or more bytes of operand address.
    804 
    805    If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
    806    case.
    807 
    808    For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
    809    option before (2) we can directly JSB/JMP because there is no condition.
    810    These operands have 'b-' as their access/data type.
    811 
    812    That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
    813    cases, we do the same idea. JACBxxx are all marked with a 'b!'
    814    JAOBxxx & JSOBxxx are marked with a 'b:'.  */
    815 #if (VIT_OPCODE_SYNTHETIC != 0x80000000)
    816 #error "You have just broken the encoding below, which assumes the sign bit means 'I am an imaginary instruction'."
    817 #endif
    818 
    819 #if (VIT_OPCODE_SPECIAL != 0x40000000)
    820 #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'."
    821 #endif
    822 
    823 static const struct vot
    824   synthetic_votstrs[] =
    825 {
    826   {"jbsb",	{"b-", 0xC0000010}},		/* BSD 4.2 */
    827 /* jsb used already */
    828   {"jbr",	{"b-", 0xC0000011}},		/* BSD 4.2 */
    829   {"jr",	{"b-", 0xC0000011}},		/* consistent */
    830   {"jneq",	{"b?", 0x80000012}},
    831   {"jnequ",	{"b?", 0x80000012}},
    832   {"jeql",	{"b?", 0x80000013}},
    833   {"jeqlu",	{"b?", 0x80000013}},
    834   {"jgtr",	{"b?", 0x80000014}},
    835   {"jleq",	{"b?", 0x80000015}},
    836 /* un-used opcodes here */
    837   {"jgeq",	{"b?", 0x80000018}},
    838   {"jlss",	{"b?", 0x80000019}},
    839   {"jgtru",	{"b?", 0x8000001a}},
    840   {"jlequ",	{"b?", 0x8000001b}},
    841   {"jvc",	{"b?", 0x8000001c}},
    842   {"jvs",	{"b?", 0x8000001d}},
    843   {"jgequ",	{"b?", 0x8000001e}},
    844   {"jcc",	{"b?", 0x8000001e}},
    845   {"jlssu",	{"b?", 0x8000001f}},
    846   {"jcs",	{"b?", 0x8000001f}},
    847 
    848   {"jacbw",	{"rwrwmwb!", 0xC000003d}},
    849   {"jacbf",	{"rfrfmfb!", 0xC000004f}},
    850   {"jacbd",	{"rdrdmdb!", 0xC000006f}},
    851   {"jacbb",	{"rbrbmbb!", 0xC000009d}},
    852   {"jacbl",	{"rlrlmlb!", 0xC00000f1}},
    853   {"jacbg",	{"rgrgmgb!", 0xC0004ffd}},
    854   {"jacbh",	{"rhrhmhb!", 0xC0006ffd}},
    855 
    856   {"jbs",	{"rlvbb?", 0x800000e0}},
    857   {"jbc",	{"rlvbb?", 0x800000e1}},
    858   {"jbss",	{"rlvbb?", 0x800000e2}},
    859   {"jbcs",	{"rlvbb?", 0x800000e3}},
    860   {"jbsc",	{"rlvbb?", 0x800000e4}},
    861   {"jbcc",	{"rlvbb?", 0x800000e5}},
    862   {"jlbs",	{"rlb?", 0x800000e8}},
    863   {"jlbc",	{"rlb?", 0x800000e9}},
    864 
    865   {"jaoblss",	{"rlmlb:", 0xC00000f2}},
    866   {"jaobleq",	{"rlmlb:", 0xC00000f3}},
    867   {"jsobgeq",	{"mlb:", 0xC00000f4}},
    868   {"jsobgtr",	{"mlb:", 0xC00000f5}},
    869 
    870 /* CASEx has no branch addresses in our conception of it.  */
    871 /* You should use ".word ..." statements after the "case ...".  */
    872 
    873   {"",		{"", 0}}	/* Empty is end sentinel.  */
    874 };
    875 
    876 /* Because this module is useful for both VMS and UN*X style assemblers
    878    and because of the variety of UN*X assemblers we must recognise
    879    the different conventions for assembler operand notation. For example
    880    VMS says "#42" for immediate mode, while most UN*X say "$42".
    881    We permit arbitrary sets of (single) characters to represent the
    882    3 concepts that DEC writes '#', '@', '^'.  */
    883 
    884 /* Character tests.  */
    885 #define VIP_IMMEDIATE 01	/* Character is like DEC # */
    886 #define VIP_INDIRECT  02	/* Char is like DEC @ */
    887 #define VIP_DISPLEN   04	/* Char is like DEC ^ */
    888 
    889 #define IMMEDIATEP(c)	(vip_metacharacters [(c) & 0xff] & VIP_IMMEDIATE)
    890 #define INDIRECTP(c)	(vip_metacharacters [(c) & 0xff] & VIP_INDIRECT)
    891 #define DISPLENP(c)	(vip_metacharacters [(c) & 0xff] & VIP_DISPLEN)
    892 
    893 /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
    894    are ever called.  */
    895 
    896 #if defined(CONST_TABLE)
    897 #define _ 0,
    898 #define I VIP_IMMEDIATE,
    899 #define S VIP_INDIRECT,
    900 #define D VIP_DISPLEN,
    901 static const char
    902 vip_metacharacters[256] =
    903 {
    904   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/* ^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/
    905   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/* ^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
    906   _ _ _ _ I _ _ _ _ _ S _ _ _ _ _	/* sp !  "  #  $  %  & '  (  )  *  +  ,  -  .  / */
    907   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?*/
    908   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O*/
    909   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _*/
    910   D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o*/
    911   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _	/*p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  ^?*/
    912 
    913   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    914   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    915   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    916   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    917   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    918   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    919   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    920   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    921 };
    922 #undef _
    923 #undef I
    924 #undef S
    925 #undef D
    926 
    927 #else
    928 
    929 static char vip_metacharacters[256];
    930 
    931 static void
    932 vip_op_1 (int bit, const char *syms)
    933 {
    934   unsigned char t;
    935 
    936   while ((t = *syms++) != 0)
    937     vip_metacharacters[t] |= bit;
    938 }
    939 
    940 /* Can be called any time.  More arguments may appear in future.  */
    941 static void
    942 vip_op_defaults (const char *immediate, const char *indirect, const char *displen)
    943 {
    944   vip_op_1 (VIP_IMMEDIATE, immediate);
    945   vip_op_1 (VIP_INDIRECT, indirect);
    946   vip_op_1 (VIP_DISPLEN, displen);
    947 }
    948 
    949 #endif
    950 
    951 /* Call me once before you decode any lines.
    952    I decode votstrs into a hash table at op_hash (which I create).
    953    I return an error text or null.
    954    If you want, I will include the 'synthetic' jXXX instructions in the
    955    instruction table.
    956    You must nominate metacharacters for eg DEC's "#", "@", "^".  */
    957 
    958 static const char *
    959 vip_begin (int synthetic_too,		/* 1 means include jXXX op-codes.  */
    960 	   const char *immediate,
    961 	   const char *indirect,
    962 	   const char *displen)
    963 {
    964   const struct vot *vP;		/* scan votstrs */
    965   const char *retval = 0;	/* error text */
    966 
    967   op_hash = hash_new ();
    968 
    969   for (vP = votstrs; *vP->vot_name && !retval; vP++)
    970     retval = hash_insert (op_hash, vP->vot_name, (void *) &vP->vot_detail);
    971 
    972   if (synthetic_too)
    973     for (vP = synthetic_votstrs; *vP->vot_name && !retval; vP++)
    974       retval = hash_insert (op_hash, vP->vot_name, (void *) &vP->vot_detail);
    975 
    976 #ifndef CONST_TABLE
    977   vip_op_defaults (immediate, indirect, displen);
    978 #endif
    979 
    980   return retval;
    981 }
    982 
    983 /* Take 3 char.s, the last of which may be `\0` (non-existent)
    984    and return the VAX register number that they represent.
    985 
    986    Return -1 if they don't form a register name. Good names return
    987    a number from 0:15 inclusive.
    988 
    989    Case is not important in a name.
    990 
    991    Register names understood are:
    992 
    993   	R0
    994   	R1
    995   	R2
    996   	R3
    997   	R4
    998   	R5
    999   	R6
   1000    	R7
   1001   	R8
   1002   	R9
   1003   	R10
   1004   	R11
   1005   	R12	AP
   1006   	R13	FP
   1007   	R14	SP
   1008   	R15	PC  */
   1009 
   1010 #define AP 12
   1011 #define FP 13
   1012 #define SP 14
   1013 #define PC 15
   1014 
   1015 /* Returns the register number of something like '%r15' or 'ap', supplied
   1016    in four single chars. Returns -1 if the register isn't recognized,
   1017    0..15 otherwise.  */
   1018 static int
   1019 vax_reg_parse (char c1, char c2, char c3, char c4)
   1020 {
   1021   int retval = -1;
   1022 
   1023 #ifdef OBJ_ELF
   1024   if (c1 != '%')	/* Register prefixes are mandatory for ELF.  */
   1025     return retval;
   1026   c1 = c2;
   1027   c2 = c3;
   1028   c3 = c4;
   1029 #endif
   1030 #ifdef OBJ_VMS
   1031   if (c4 != 0)		/* Register prefixes are not allowed under VMS.  */
   1032     return retval;
   1033 #endif
   1034 #ifdef OBJ_AOUT
   1035   if (c1 == '%')	/* Register prefixes are optional under a.out.  */
   1036     {
   1037       c1 = c2;
   1038       c2 = c3;
   1039       c3 = c4;
   1040     }
   1041   else if (c3 && c4)	/* Can't be 4 characters long.  */
   1042     return retval;
   1043 #endif
   1044 
   1045   c1 = TOLOWER (c1);
   1046   c2 = TOLOWER (c2);
   1047   if (ISDIGIT (c2) && c1 == 'r')
   1048     {
   1049       retval = c2 - '0';
   1050       if (ISDIGIT (c3))
   1051 	{
   1052 	  retval = retval * 10 + c3 - '0';
   1053 	  retval = (retval > 15) ? -1 : retval;
   1054 	  /* clamp the register value to 1 hex digit */
   1055 	}
   1056       else if (c3)
   1057 	retval = -1;		/* c3 must be '\0' or a digit.  */
   1058     }
   1059   else if (c3)			/* There are no three letter regs.  */
   1060     retval = -1;
   1061   else if (c2 == 'p')
   1062     {
   1063       switch (c1)
   1064 	{
   1065 	case 's':
   1066 	  retval = SP;
   1067 	  break;
   1068 	case 'f':
   1069 	  retval = FP;
   1070 	  break;
   1071 	case 'a':
   1072 	  retval = AP;
   1073 	  break;
   1074 	default:
   1075 	  retval = -1;
   1076 	}
   1077     }
   1078   else if (c1 == 'p' && c2 == 'c')
   1079     retval = PC;
   1080   else
   1081     retval = -1;
   1082   return retval;
   1083 }
   1084 
   1085 /* Parse a vax operand in DEC assembler notation.
   1086    For speed, expect a string of whitespace to be reduced to a single ' '.
   1087    This is the case for GNU AS, and is easy for other DEC-compatible
   1088    assemblers.
   1089 
   1090    Knowledge about DEC VAX assembler operand notation lives here.
   1091    This doesn't even know what a register name is, except it believes
   1092    all register names are 2 or 3 characters, and lets vax_reg_parse() say
   1093    what number each name represents.
   1094    It does, however, know that PC, SP etc are special registers so it can
   1095    detect addressing modes that are silly for those registers.
   1096 
   1097    Where possible, it delivers 1 fatal or 1 warning message if the operand
   1098    is suspect. Exactly what we test for is still evolving.
   1099 
   1100    ---
   1101   	Arg block.
   1102 
   1103    There were a number of 'mismatched argument type' bugs to vip_op.
   1104    The most general solution is to typedef each (of many) arguments.
   1105    We used instead a typedef'd argument block. This is less modular
   1106    than using separate return pointers for each result, but runs faster
   1107    on most engines, and seems to keep programmers happy. It will have
   1108    to be done properly if we ever want to use vip_op as a general-purpose
   1109    module (it was designed to be).
   1110 
   1111  	G^
   1112 
   1113    Doesn't support DEC "G^" format operands. These always take 5 bytes
   1114    to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
   1115    optimising to (say) a "B^" if you are lucky in the way you link.
   1116    When someone builds a linker smart enough to convert "G^" to "B^", "W^"
   1117    whenever possible, then we should implement it.
   1118    If there is some other use for "G^", feel free to code it in!
   1119 
   1120   	speed
   1121 
   1122    If I nested if()s more, I could avoid testing (*err) which would save
   1123    time, space and page faults. I didn't nest all those if()s for clarity
   1124    and because I think the mode testing can be re-arranged 1st to test the
   1125    commoner constructs 1st. Does anybody have statistics on this?
   1126 
   1127   	error messages
   1128 
   1129    In future, we should be able to 'compose' error messages in a scratch area
   1130    and give the user MUCH more informative error messages. Although this takes
   1131    a little more code at run-time, it will make this module much more self-
   1132    documenting. As an example of what sucks now: most error messages have
   1133    hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
   1134    the Un*x characters "$`*", that most users will expect from this AS.
   1135 
   1136    ----
   1137 
   1138    The input is a string, ending with '\0'.
   1139 
   1140    We also require a 'hint' of what kind of operand is expected: so
   1141    we can remind caller not to write into literals for instance.
   1142 
   1143    The output is a skeletal instruction.
   1144 
   1145    The algorithm has two parts.
   1146    1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
   1147    2. express the @^#-()+[] as some parameters suited to further analysis.
   1148 
   1149    2nd step is where we detect the googles of possible invalid combinations
   1150    a human (or compiler) might write. Note that if we do a half-way
   1151    decent assembler, we don't know how long to make (eg) displacement
   1152    fields when we first meet them (because they may not have defined values).
   1153    So we must wait until we know how many bits are needed for each address,
   1154    then we can know both length and opcodes of instructions.
   1155    For reason(s) above, we will pass to our caller a 'broken' instruction
   1156    of these major components, from which our caller can generate instructions:
   1157     -  displacement length      I^ S^ L^ B^ W^ unspecified
   1158     -  mode                     (many)
   1159     -  register                 R0-R15 or absent
   1160     -  index register           R0-R15 or absent
   1161     -  expression text          what we don't parse
   1162     -  error text(s)            why we couldn't understand the operand
   1163 
   1164    ----
   1165 
   1166    To decode output of this, test errtxt. If errtxt[0] == '\0', then
   1167    we had no errors that prevented parsing. Also, if we ever report
   1168    an internal bug, errtxt[0] is set non-zero. So one test tells you
   1169    if the other outputs are to be taken seriously.
   1170 
   1171    ----
   1172 
   1173    Dec defines the semantics of address modes (and values)
   1174    by a two-letter code, explained here.
   1175 
   1176      letter 1:   access type
   1177 
   1178        a         address calculation - no data access, registers forbidden
   1179        b         branch displacement
   1180        m         read - let go of bus - write back    "modify"
   1181        r         read
   1182        v         bit field address: like 'a' but registers are OK
   1183        w         write
   1184        space	 no operator (eg ".long foo") [our convention]
   1185 
   1186      letter 2:   data type (i.e. width, alignment)
   1187 
   1188        b         byte
   1189        d         double precision floating point (D format)
   1190        f         single precision floating point (F format)
   1191        g         G format floating
   1192        h         H format floating
   1193        l         longword
   1194        o         octaword
   1195        q         quadword
   1196        w         word
   1197        ?	 simple synthetic branch operand
   1198        -	 unconditional synthetic JSB/JSR operand
   1199        !	 complex synthetic branch operand
   1200 
   1201    The '-?!' letter 2's are not for external consumption. They are used
   1202    for various assemblers. Generally, all unknown widths are assumed 0.
   1203    We don't limit your choice of width character.
   1204 
   1205    DEC operands are hard work to parse. For example, '@' as the first
   1206    character means indirect (deferred) mode but elsewhere it is a shift
   1207    operator.
   1208    The long-winded explanation of how this is supposed to work is
   1209    cancelled. Read a DEC vax manual.
   1210    We try hard not to parse anything that MIGHT be part of the expression
   1211    buried in that syntax. For example if we see @...(Rn) we don't check
   1212    for '-' before the '(' because mode @-(Rn) does not exist.
   1213 
   1214    After parsing we have:
   1215 
   1216    at                     1 if leading '@' (or Un*x '*')
   1217    len                    takes one value from " bilsw". eg B^ -> 'b'.
   1218    hash                   1 if leading '#' (or Un*x '$')
   1219    expr_begin, expr_end   the expression we did not parse
   1220                           even though we don't interpret it, we make use
   1221                           of its presence or absence.
   1222    sign                   -1: -(Rn)    0: absent    +1: (Rn)+
   1223    paren                  1 if () are around register
   1224    reg                    major register number 0:15    -1 means absent
   1225    ndx                    index register number 0:15    -1 means absent
   1226 
   1227    Again, I dare not explain it: just trace ALL the code!
   1228 
   1229    Summary of vip_op outputs.
   1230 
   1231   mode	reg	len	ndx
   1232   (Rn) => @Rn
   1233   {@}Rn			5+@	n	' '	optional
   1234   branch operand		0	-1	' '	-1
   1235   S^#foo			0	-1	's'	-1
   1236   -(Rn)			7	n	' '	optional
   1237   {@}(Rn)+		8+@	n	' '	optional
   1238   {@}#foo, no S^		8+@	PC	" i"	optional
   1239   {@}{q^}{(Rn)}		10+@+q	option	" bwl"	optional  */
   1240 
   1241 /* Dissect user-input 'optext' (which is something like "@B^foo@bar(AP)[FP]:")
   1242    using the vop in vopP. vopP's vop_access and vop_width. We fill _ndx, _reg,
   1243    _mode, _short, _warn, _error, _expr_begin, _expr_end and _nbytes.  */
   1244 
   1245 static void
   1246 vip_op (char *optext, struct vop *vopP)
   1247 {
   1248   /* Track operand text forward.  */
   1249   char *p;
   1250   /* Track operand text backward.  */
   1251   char *q;
   1252   /* 1 if leading '@' ('*') seen.  */
   1253   int at;
   1254   /* one of " bilsw" */
   1255   char len;
   1256   /* 1 if leading '#' ('$') seen.  */
   1257   int hash;
   1258   /* -1, 0 or +1.  */
   1259   int sign = 0;
   1260   /* 1 if () surround register.  */
   1261   int paren = 0;
   1262   /* Register number, -1:absent.  */
   1263   int reg = 0;
   1264   /* Index register number -1:absent.  */
   1265   int ndx = 0;
   1266   /* Report illegal operand, ""==OK.  */
   1267   /* " " is a FAKE error: means we won.  */
   1268   /* ANY err that begins with ' ' is a fake.  */
   1269   /* " " is converted to "" before return.  */
   1270   const char *err;
   1271   /* Warn about weird modes pf address.  */
   1272   const char *wrn;
   1273   /* Preserve q in case we backup.  */
   1274   char *oldq = NULL;
   1275   /* Build up 4-bit operand mode here.  */
   1276   /* Note: index mode is in ndx, this is.  */
   1277   /* The major mode of operand address.  */
   1278   int mode = 0;
   1279   /* Notice how we move wrong-arg-type bugs INSIDE this module: if we
   1280      get the types wrong below, we lose at compile time rather than at
   1281      lint or run time.  */
   1282   char access_mode;		/* vop_access.  */
   1283   char width;			/* vop_width.  */
   1284 
   1285   access_mode = vopP->vop_access;
   1286   width = vopP->vop_width;
   1287   /* None of our code bugs (yet), no user text errors, no warnings
   1288      even.  */
   1289   err = wrn = 0;
   1290 
   1291   p = optext;
   1292 
   1293   if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1294     p++;			/* skip over whitespace */
   1295 
   1296   if ((at = INDIRECTP (*p)) != 0)
   1297     {				/* 1 if *p=='@'(or '*' for Un*x) */
   1298       p++;			/* at is determined */
   1299       if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1300 	p++;			/* skip over whitespace */
   1301     }
   1302 
   1303   /* This code is subtle. It tries to detect all legal (letter)'^'
   1304      but it doesn't waste time explicitly testing for premature '\0' because
   1305      this case is rejected as a mismatch against either (letter) or '^'.  */
   1306   {
   1307     char c;
   1308 
   1309     c = *p;
   1310     c = TOLOWER (c);
   1311     if (DISPLENP (p[1]) && strchr ("bilws", len = c))
   1312       p += 2;			/* Skip (letter) '^'.  */
   1313     else			/* No (letter) '^' seen.  */
   1314       len = ' ';		/* Len is determined.  */
   1315   }
   1316 
   1317   if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1318     p++;
   1319 
   1320   if ((hash = IMMEDIATEP (*p)) != 0)	/* 1 if *p=='#' ('$' for Un*x) */
   1321     p++;			/* Hash is determined.  */
   1322 
   1323   /* p points to what may be the beginning of an expression.
   1324      We have peeled off the front all that is peelable.
   1325      We know at, len, hash.
   1326 
   1327      Lets point q at the end of the text and parse that (backwards).  */
   1328 
   1329   for (q = p; *q; q++)
   1330     ;
   1331   q--;				/* Now q points at last char of text.  */
   1332 
   1333   if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1334     q--;
   1335 
   1336   /* Reverse over whitespace, but don't.  */
   1337   /* Run back over *p.  */
   1338 
   1339   /* As a matter of policy here, we look for [Rn], although both Rn and S^#
   1340      forbid [Rn]. This is because it is easy, and because only a sick
   1341      cyborg would have [...] trailing an expression in a VAX-like assembler.
   1342      A meticulous parser would first check for Rn followed by '(' or '['
   1343      and not parse a trailing ']' if it found another. We just ban expressions
   1344      ending in ']'.  */
   1345   if (*q == ']')
   1346     {
   1347       while (q >= p && *q != '[')
   1348 	q--;
   1349       /* Either q<p or we got matching '['.  */
   1350       if (q < p)
   1351 	err = _("no '[' to match ']'");
   1352       else
   1353 	{
   1354 	  /* Confusers like "[]" will eventually lose with a bad register
   1355 	   * name error. So again we don't need to check for early '\0'.  */
   1356 	  if (q[3] == ']')
   1357 	    ndx = vax_reg_parse (q[1], q[2], 0, 0);
   1358 	  else if (q[4] == ']')
   1359 	    ndx = vax_reg_parse (q[1], q[2], q[3], 0);
   1360 	  else if (q[5] == ']')
   1361 	    ndx = vax_reg_parse (q[1], q[2], q[3], q[4]);
   1362 	  else
   1363 	    ndx = -1;
   1364 	  /* Since we saw a ']' we will demand a register name in the [].
   1365 	   * If luser hasn't given us one: be rude.  */
   1366 	  if (ndx < 0)
   1367 	    err = _("bad register in []");
   1368 	  else if (ndx == PC)
   1369 	    err = _("[PC] index banned");
   1370 	  else
   1371 	    /* Point q just before "[...]".  */
   1372 	    q--;
   1373 	}
   1374     }
   1375   else
   1376     /* No ']', so no iNDeX register.  */
   1377     ndx = -1;
   1378 
   1379   /* If err = "..." then we lost: run away.
   1380      Otherwise ndx == -1 if there was no "[...]".
   1381      Otherwise, ndx is index register number, and q points before "[...]".  */
   1382 
   1383   if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1384     q--;
   1385   /* Reverse over whitespace, but don't.  */
   1386   /* Run back over *p.  */
   1387   if (!err || !*err)
   1388     {
   1389       /* no ()+ or -() seen yet */
   1390       sign = 0;
   1391 
   1392       if (q > p + 3 && *q == '+' && q[-1] == ')')
   1393 	{
   1394 	  sign = 1;		/* we saw a ")+" */
   1395 	  q--;			/* q points to ')' */
   1396 	}
   1397 
   1398       if (*q == ')' && q > p + 2)
   1399 	{
   1400 	  paren = 1;		/* assume we have "(...)" */
   1401 	  while (q >= p && *q != '(')
   1402 	    q--;
   1403 	  /* either q<p or we got matching '(' */
   1404 	  if (q < p)
   1405 	    err = _("no '(' to match ')'");
   1406 	  else
   1407 	    {
   1408 	      /* Confusers like "()" will eventually lose with a bad register
   1409 	         name error. So again we don't need to check for early '\0'.  */
   1410 	      if (q[3] == ')')
   1411 		reg = vax_reg_parse (q[1], q[2], 0, 0);
   1412 	      else if (q[4] == ')')
   1413 		reg = vax_reg_parse (q[1], q[2], q[3], 0);
   1414 	      else if (q[5] == ')')
   1415 		reg = vax_reg_parse (q[1], q[2], q[3], q[4]);
   1416 	      else
   1417 		reg = -1;
   1418 	      /* Since we saw a ')' we will demand a register name in the ')'.
   1419 	         This is nasty: why can't our hypothetical assembler permit
   1420 	         parenthesised expressions? BECAUSE I AM LAZY! That is why.
   1421 	         Abuse luser if we didn't spy a register name.  */
   1422 	      if (reg < 0)
   1423 		{
   1424 		  /* JF allow parenthesized expressions.  I hope this works.  */
   1425 		  paren = 0;
   1426 		  while (*q != ')')
   1427 		    q++;
   1428 		  /* err = "unknown register in ()"; */
   1429 		}
   1430 	      else
   1431 		q--;		/* point just before '(' of "(...)" */
   1432 	      /* If err == "..." then we lost. Run away.
   1433 	         Otherwise if reg >= 0 then we saw (Rn).  */
   1434 	    }
   1435 	  /* If err == "..." then we lost.
   1436 	     Otherwise paren==1 and reg = register in "()".  */
   1437 	}
   1438       else
   1439 	paren = 0;
   1440       /* If err == "..." then we lost.
   1441          Otherwise, q points just before "(Rn)", if any.
   1442          If there was a "(...)" then paren==1, and reg is the register.  */
   1443 
   1444       /* We should only seek '-' of "-(...)" if:
   1445            we saw "(...)"                    paren == 1
   1446            we have no errors so far          ! *err
   1447            we did not see '+' of "(...)+"    sign < 1
   1448          We don't check len. We want a specific error message later if
   1449          user tries "x^...-(Rn)". This is a feature not a bug.  */
   1450       if (!err || !*err)
   1451 	{
   1452 	  if (paren && sign < 1)/* !sign is adequate test */
   1453 	    {
   1454 	      if (*q == '-')
   1455 		{
   1456 		  sign = -1;
   1457 		  q--;
   1458 		}
   1459 	    }
   1460 	  /* We have back-tracked over most
   1461 	     of the crud at the end of an operand.
   1462 	     Unless err, we know: sign, paren. If paren, we know reg.
   1463 	     The last case is of an expression "Rn".
   1464 	     This is worth hunting for if !err, !paren.
   1465 	     We wouldn't be here if err.
   1466 	     We remember to save q, in case we didn't want "Rn" anyway.  */
   1467 	  if (!paren)
   1468 	    {
   1469 	      if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1470 		q--;
   1471 	      /* Reverse over whitespace, but don't.  */
   1472 	      /* Run back over *p.  */
   1473 	      /* Room for Rn or Rnn (include prefix) exactly?  */
   1474 	      if (q > p && q < p + 4)
   1475 		reg = vax_reg_parse (p[0], p[1],
   1476 		  q < p + 2 ? 0 : p[2],
   1477 		  q < p + 3 ? 0 : p[3]);
   1478 	      else
   1479 		reg = -1;	/* Always comes here if no register at all.  */
   1480 	      /* Here with a definitive reg value.  */
   1481 	      if (reg >= 0)
   1482 		{
   1483 		  oldq = q;
   1484 		  q = p - 1;
   1485 		}
   1486 	    }
   1487 	}
   1488     }
   1489   /* have reg. -1:absent; else 0:15.  */
   1490 
   1491   /* We have:  err, at, len, hash, ndx, sign, paren, reg.
   1492      Also, any remaining expression is from *p through *q inclusive.
   1493      Should there be no expression, q==p-1. So expression length = q-p+1.
   1494      This completes the first part: parsing the operand text.  */
   1495 
   1496   /* We now want to boil the data down, checking consistency on the way.
   1498      We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
   1499      We will deliver a 4-bit reg, and a 4-bit mode.  */
   1500 
   1501   /* Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
   1502 
   1503      in:  at	?
   1504           len	?
   1505           hash	?
   1506           p:q	?
   1507           sign  ?
   1508           paren	?
   1509           reg   ?
   1510           ndx   ?
   1511 
   1512      out: mode  0
   1513           reg   -1
   1514           len	' '
   1515           p:q	whatever was input
   1516           ndx	-1
   1517           err	" "		 or error message, and other outputs trashed.  */
   1518   /* Branch operands have restricted forms.  */
   1519   if ((!err || !*err) && access_mode == 'b')
   1520     {
   1521       if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
   1522 	err = _("invalid branch operand");
   1523       else
   1524 	err = " ";
   1525     }
   1526 
   1527   /* Since nobody seems to use it: comment this 'feature'(?) out for now.  */
   1528 #ifdef NEVER
   1529   /* Case of stand-alone operand. e.g. ".long foo"
   1530 
   1531      in:  at	?
   1532           len	?
   1533           hash	?
   1534           p:q	?
   1535           sign  ?
   1536           paren	?
   1537           reg   ?
   1538           ndx   ?
   1539 
   1540      out: mode  0
   1541           reg   -1
   1542           len	' '
   1543           p:q	whatever was input
   1544           ndx	-1
   1545           err	" "		 or error message, and other outputs trashed.  */
   1546   if ((!err || !*err) && access_mode == ' ')
   1547     {
   1548       if (at)
   1549 	err = _("address prohibits @");
   1550       else if (hash)
   1551 	err = _("address prohibits #");
   1552       else if (sign)
   1553 	{
   1554 	  if (sign < 0)
   1555 	    err = _("address prohibits -()");
   1556 	  else
   1557 	    err = _("address prohibits ()+");
   1558 	}
   1559       else if (paren)
   1560 	err = _("address prohibits ()");
   1561       else if (ndx >= 0)
   1562 	err = _("address prohibits []");
   1563       else if (reg >= 0)
   1564 	err = _("address prohibits register");
   1565       else if (len != ' ')
   1566 	err = _("address prohibits displacement length specifier");
   1567       else
   1568 	{
   1569 	  err = " ";	/* succeed */
   1570 	  mode = 0;
   1571 	}
   1572     }
   1573 #endif
   1574 
   1575   /* Case of S^#.
   1576 
   1577      in:  at       0
   1578           len      's'               definition
   1579           hash     1              demand
   1580           p:q                        demand not empty
   1581           sign     0                 by paren==0
   1582           paren    0             by "()" scan logic because "S^" seen
   1583           reg      -1                or nn by mistake
   1584           ndx      -1
   1585 
   1586      out: mode     0
   1587           reg      -1
   1588           len      's'
   1589           exp
   1590           ndx      -1  */
   1591   if ((!err || !*err) && len == 's')
   1592     {
   1593       if (!hash || paren || at || ndx >= 0)
   1594 	err = _("invalid operand of S^#");
   1595       else
   1596 	{
   1597 	  if (reg >= 0)
   1598 	    {
   1599 	      /* Darn! we saw S^#Rnn ! put the Rnn back in
   1600 	         expression. KLUDGE! Use oldq so we don't
   1601 	         need to know exact length of reg name.  */
   1602 	      q = oldq;
   1603 	      reg = 0;
   1604 	    }
   1605 	  /* We have all the expression we will ever get.  */
   1606 	  if (p > q)
   1607 	    err = _("S^# needs expression");
   1608 	  else if (access_mode == 'r')
   1609 	    {
   1610 	      err = " ";	/* WIN! */
   1611 	      mode = 0;
   1612 	    }
   1613 	  else
   1614 	    err = _("S^# may only read-access");
   1615 	}
   1616     }
   1617 
   1618   /* Case of -(Rn), which is weird case.
   1619 
   1620      in:  at       0
   1621           len      '
   1622           hash     0
   1623           p:q      q<p
   1624           sign     -1                by definition
   1625           paren    1              by definition
   1626           reg      present           by definition
   1627           ndx      optional
   1628 
   1629      out: mode     7
   1630           reg      present
   1631           len      ' '
   1632           exp      ""                enforce empty expression
   1633           ndx      optional          warn if same as reg.  */
   1634   if ((!err || !*err) && sign < 0)
   1635     {
   1636       if (len != ' ' || hash || at || p <= q)
   1637 	err = _("invalid operand of -()");
   1638       else
   1639 	{
   1640 	  err = " ";		/* win */
   1641 	  mode = 7;
   1642 	  if (reg == PC)
   1643 	    wrn = _("-(PC) unpredictable");
   1644 	  else if (reg == ndx)
   1645 	    wrn = _("[]index same as -()register: unpredictable");
   1646 	}
   1647     }
   1648 
   1649   /* We convert "(Rn)" to "@Rn" for our convenience.
   1650      (I hope this is convenient: has someone got a better way to parse this?)
   1651      A side-effect of this is that "@Rn" is a valid operand.  */
   1652   if (paren && !sign && !hash && !at && len == ' ' && p > q)
   1653     {
   1654       at = 1;
   1655       paren = 0;
   1656     }
   1657 
   1658   /* Case of (Rn)+, which is slightly different.
   1659 
   1660      in:  at
   1661           len      ' '
   1662           hash     0
   1663           p:q      q<p
   1664           sign     +1                by definition
   1665           paren    1              by definition
   1666           reg      present           by definition
   1667           ndx      optional
   1668 
   1669      out: mode     8+@
   1670           reg      present
   1671           len      ' '
   1672           exp      ""                enforce empty expression
   1673           ndx      optional          warn if same as reg.  */
   1674   if ((!err || !*err) && sign > 0)
   1675     {
   1676       if (len != ' ' || hash || p <= q)
   1677 	err = _("invalid operand of ()+");
   1678       else
   1679 	{
   1680 	  err = " ";		/* win */
   1681 	  mode = 8 + (at ? 1 : 0);
   1682 	  if (reg == PC)
   1683 	    wrn = _("(PC)+ unpredictable");
   1684 	  else if (reg == ndx)
   1685 	    wrn = _("[]index same as ()+register: unpredictable");
   1686 	}
   1687     }
   1688 
   1689   /* Case of #, without S^.
   1690 
   1691      in:  at
   1692           len      ' ' or 'i'
   1693           hash     1              by definition
   1694           p:q
   1695           sign     0
   1696           paren    0
   1697           reg      absent
   1698           ndx      optional
   1699 
   1700      out: mode     8+@
   1701           reg      PC
   1702           len      ' ' or 'i'
   1703           exp
   1704           ndx      optional.  */
   1705   if ((!err || !*err) && hash)
   1706     {
   1707       if (len != 'i' && len != ' ')
   1708 	err = _("# conflicts length");
   1709       else if (paren)
   1710 	err = _("# bars register");
   1711       else
   1712 	{
   1713 	  if (reg >= 0)
   1714 	    {
   1715 	      /* Darn! we saw #Rnn! Put the Rnn back into the expression.
   1716 	         By using oldq, we don't need to know how long Rnn was.
   1717 	         KLUDGE!  */
   1718 	      q = oldq;
   1719 	      reg = -1;		/* No register any more.  */
   1720 	    }
   1721 	  err = " ";		/* Win.  */
   1722 
   1723 	  /* JF a bugfix, I think!  */
   1724 	  if (at && access_mode == 'a')
   1725 	    vopP->vop_nbytes = 4;
   1726 
   1727 	  mode = (at ? 9 : 8);
   1728 	  reg = PC;
   1729 	  if ((access_mode == 'm' || access_mode == 'w') && !at)
   1730 	    wrn = _("writing or modifying # is unpredictable");
   1731 	}
   1732     }
   1733   /* If !*err, then       sign == 0
   1734                           hash == 0 */
   1735 
   1736   /* Case of Rn. We separate this one because it has a few special
   1737      errors the remaining modes lack.
   1738 
   1739      in:  at       optional
   1740           len      ' '
   1741           hash     0             by program logic
   1742           p:q      empty
   1743           sign     0                 by program logic
   1744           paren    0             by definition
   1745           reg      present           by definition
   1746           ndx      optional
   1747 
   1748      out: mode     5+@
   1749           reg      present
   1750           len      ' '               enforce no length
   1751           exp      ""                enforce empty expression
   1752           ndx      optional          warn if same as reg.  */
   1753   if ((!err || !*err) && !paren && reg >= 0)
   1754     {
   1755       if (len != ' ')
   1756 	err = _("length not needed");
   1757       else if (at)
   1758 	{
   1759 	  err = " ";		/* win */
   1760 	  mode = 6;		/* @Rn */
   1761 	}
   1762       else if (ndx >= 0)
   1763 	err = _("can't []index a register, because it has no address");
   1764       else if (access_mode == 'a')
   1765 	err = _("a register has no address");
   1766       else
   1767 	{
   1768 	  /* Idea here is to detect from length of datum
   1769 	     and from register number if we will touch PC.
   1770 	     Warn if we do.
   1771 	     vop_nbytes is number of bytes in operand.
   1772 	     Compute highest byte affected, compare to PC0.  */
   1773 	  if ((vopP->vop_nbytes + reg * 4) > 60)
   1774 	    wrn = _("PC part of operand unpredictable");
   1775 	  err = " ";		/* win */
   1776 	  mode = 5;		/* Rn */
   1777 	}
   1778     }
   1779   /* If !*err,        sign  == 0
   1780                       hash  == 0
   1781                       paren == 1  OR reg==-1  */
   1782 
   1783   /* Rest of cases fit into one bunch.
   1784 
   1785      in:  at       optional
   1786           len      ' ' or 'b' or 'w' or 'l'
   1787           hash     0             by program logic
   1788           p:q      expected          (empty is not an error)
   1789           sign     0                 by program logic
   1790           paren    optional
   1791           reg      optional
   1792           ndx      optional
   1793 
   1794      out: mode     10 + @ + len
   1795           reg      optional
   1796           len      ' ' or 'b' or 'w' or 'l'
   1797           exp                        maybe empty
   1798           ndx      optional          warn if same as reg.  */
   1799   if (!err || !*err)
   1800     {
   1801       err = " ";		/* win (always) */
   1802       mode = 10 + (at ? 1 : 0);
   1803       switch (len)
   1804 	{
   1805 	case 'l':
   1806 	  mode += 2;
   1807 	case 'w':
   1808 	  mode += 2;
   1809 	case ' ':	/* Assumed B^ until our caller changes it.  */
   1810 	case 'b':
   1811 	  break;
   1812 	}
   1813     }
   1814 
   1815   /* here with completely specified     mode
   1816     					len
   1817     					reg
   1818     					expression   p,q
   1819     					ndx.  */
   1820 
   1821   if (*err == ' ')
   1822     err = 0;			/* " " is no longer an error.  */
   1823 
   1824   vopP->vop_mode = mode;
   1825   vopP->vop_reg = reg;
   1826   vopP->vop_short = len;
   1827   vopP->vop_expr_begin = p;
   1828   vopP->vop_expr_end = q;
   1829   vopP->vop_ndx = ndx;
   1830   vopP->vop_error = err;
   1831   vopP->vop_warn = wrn;
   1832 }
   1833 
   1834 /* This converts a string into a vax instruction.
   1835    The string must be a bare single instruction in dec-vax (with BSD4 frobs)
   1836    format.
   1837    It provides some error messages: at most one fatal error message (which
   1838    stops the scan) and at most one warning message for each operand.
   1839    The vax instruction is returned in exploded form, since we have no
   1840    knowledge of how you parse (or evaluate) your expressions.
   1841    We do however strip off and decode addressing modes and operation
   1842    mnemonic.
   1843 
   1844    The exploded instruction is returned to a struct vit of your choice.
   1845    #include "vax-inst.h" to know what a struct vit is.
   1846 
   1847    This function's value is a string. If it is not "" then an internal
   1848    logic error was found: read this code to assign meaning to the string.
   1849    No argument string should generate such an error string:
   1850    it means a bug in our code, not in the user's text.
   1851 
   1852    You MUST have called vip_begin() once before using this function.  */
   1853 
   1854 static void
   1855 vip (struct vit *vitP,		/* We build an exploded instruction here.  */
   1856      char *instring)		/* Text of a vax instruction: we modify.  */
   1857 {
   1858   /* How to bit-encode this opcode.  */
   1859   struct vot_wot *vwP;
   1860   /* 1/skip whitespace.2/scan vot_how */
   1861   char *p;
   1862   char *q;
   1863   /* counts number of operands seen */
   1864   unsigned char count;
   1865   /* scan operands in struct vit */
   1866   struct vop *operandp;
   1867   /* error over all operands */
   1868   const char *alloperr;
   1869   /* Remember char, (we clobber it with '\0' temporarily).  */
   1870   char c;
   1871   /* Op-code of this instruction.  */
   1872   vax_opcodeT oc;
   1873 
   1874   if (*instring == ' ')
   1875     ++instring;
   1876 
   1877   /* MUST end in end-of-string or exactly 1 space.  */
   1878   for (p = instring; *p && *p != ' '; p++)
   1879     ;
   1880 
   1881   /* Scanned up to end of operation-code.  */
   1882   /* Operation-code is ended with whitespace.  */
   1883   if (p - instring == 0)
   1884     {
   1885       vitP->vit_error = _("No operator");
   1886       count = 0;
   1887       memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
   1888     }
   1889   else
   1890     {
   1891       c = *p;
   1892       *p = '\0';
   1893       /* Here with instring pointing to what better be an op-name, and p
   1894          pointing to character just past that.
   1895          We trust instring points to an op-name, with no whitespace.  */
   1896       vwP = (struct vot_wot *) hash_find (op_hash, instring);
   1897       /* Restore char after op-code.  */
   1898       *p = c;
   1899       if (vwP == 0)
   1900 	{
   1901 	  vitP->vit_error = _("Unknown operator");
   1902 	  count = 0;
   1903 	  memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
   1904 	}
   1905       else
   1906 	{
   1907 	  /* We found a match! So let's pick up as many operands as the
   1908 	     instruction wants, and even gripe if there are too many.
   1909 	     We expect comma to separate each operand.
   1910 	     We let instring track the text, while p tracks a part of the
   1911 	     struct vot.  */
   1912 	  const char *howp;
   1913 	  /* The lines below know about 2-byte opcodes starting FD,FE or FF.
   1914 	     They also understand synthetic opcodes. Note:
   1915 	     we return 32 bits of opcode, including bucky bits, BUT
   1916 	     an opcode length is either 8 or 16 bits for vit_opcode_nbytes.  */
   1917 	  oc = vwP->vot_code;	/* The op-code.  */
   1918 	  vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
   1919 	  md_number_to_chars (vitP->vit_opcode, oc, 4);
   1920 	  count = 0;		/* No operands seen yet.  */
   1921 	  instring = p;		/* Point just past operation code.  */
   1922 	  alloperr = "";
   1923 	  for (howp = vwP->vot_how, operandp = vitP->vit_operand;
   1924 	       !(alloperr && *alloperr) && *howp;
   1925 	       operandp++, howp += 2)
   1926 	    {
   1927 	      /* Here to parse one operand. Leave instring pointing just
   1928 	         past any one ',' that marks the end of this operand.  */
   1929 	      if (!howp[1])
   1930 		as_fatal (_("odd number of bytes in operand description"));
   1931 	      else if (*instring)
   1932 		{
   1933 		  for (q = instring; (c = *q) && c != ','; q++)
   1934 		    ;
   1935 		  /* Q points to ',' or '\0' that ends argument. C is that
   1936 		     character.  */
   1937 		  *q = 0;
   1938 		  operandp->vop_width = howp[1];
   1939 		  operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
   1940 		  operandp->vop_access = howp[0];
   1941 		  vip_op (instring, operandp);
   1942 		  *q = c;	/* Restore input text.  */
   1943 		  if (operandp->vop_error)
   1944 		    alloperr = _("Bad operand");
   1945 		  instring = q + (c ? 1 : 0);	/* Next operand (if any).  */
   1946 		  count++;	/*  Won another argument, may have an operr.  */
   1947 		}
   1948 	      else
   1949 		alloperr = _("Not enough operands");
   1950 	    }
   1951 	  if (!*alloperr)
   1952 	    {
   1953 	      if (*instring == ' ')
   1954 		instring++;
   1955 	      if (*instring)
   1956 		alloperr = _("Too many operands");
   1957 	    }
   1958 	  vitP->vit_error = alloperr;
   1959 	}
   1960     }
   1961   vitP->vit_operands = count;
   1962 }
   1963 
   1964 #ifdef test
   1966 
   1967 /* Test program for above.  */
   1968 
   1969 struct vit myvit;		/* Build an exploded vax instruction here.  */
   1970 char answer[100];		/* Human types a line of vax assembler here.  */
   1971 char *mybug;			/* "" or an internal logic diagnostic.  */
   1972 int mycount;			/* Number of operands.  */
   1973 struct vop *myvop;		/* Scan operands from myvit.  */
   1974 int mysynth;			/* 1 means want synthetic opcodes.  */
   1975 char my_immediate[200];
   1976 char my_indirect[200];
   1977 char my_displen[200];
   1978 
   1979 int
   1980 main (void)
   1981 {
   1982   char *p;
   1983 
   1984   printf ("0 means no synthetic instructions.   ");
   1985   printf ("Value for vip_begin?  ");
   1986   gets (answer);
   1987   sscanf (answer, "%d", &mysynth);
   1988   printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
   1989   printf ("enter immediate symbols eg enter #   ");
   1990   gets (my_immediate);
   1991   printf ("enter indirect symbols  eg enter @   ");
   1992   gets (my_indirect);
   1993   printf ("enter displen symbols   eg enter ^   ");
   1994   gets (my_displen);
   1995 
   1996   if (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))
   1997     error ("vip_begin=%s", p);
   1998 
   1999   printf ("An empty input line will quit you from the vax instruction parser\n");
   2000   for (;;)
   2001     {
   2002       printf ("vax instruction: ");
   2003       fflush (stdout);
   2004       gets (answer);
   2005       if (!*answer)
   2006 	break;		/* Out of for each input text loop.  */
   2007 
   2008       vip (& myvit, answer);
   2009       if (*myvit.vit_error)
   2010 	printf ("ERR:\"%s\"\n", myvit.vit_error);
   2011 
   2012       printf ("opcode=");
   2013       for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
   2014 	   mycount;
   2015 	   mycount--, p++)
   2016 	printf ("%02x ", *p & 0xFF);
   2017 
   2018       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
   2019       for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
   2020 	{
   2021 	  printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
   2022 		  myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
   2023 		  myvop->vop_short, myvop->vop_access, myvop->vop_width,
   2024 		  myvop->vop_nbytes);
   2025 	  for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
   2026 	    putchar (*p);
   2027 
   2028 	  printf ("\"\n");
   2029 	  if (myvop->vop_error)
   2030 	    printf ("  err:\"%s\"\n", myvop->vop_error);
   2031 
   2032 	  if (myvop->vop_warn)
   2033 	    printf ("  wrn:\"%s\"\n", myvop->vop_warn);
   2034 	}
   2035     }
   2036   vip_end ();
   2037   exit (EXIT_SUCCESS);
   2038 }
   2039 
   2040 #endif
   2041 
   2042 #ifdef TEST			/* #Define to use this testbed.  */
   2044 
   2045 /* Follows a test program for this function.
   2046    We declare arrays non-local in case some of our tiny-minded machines
   2047    default to small stacks. Also, helps with some debuggers.  */
   2048 
   2049 char answer[100];		/* Human types into here.  */
   2050 char *p;			/*  */
   2051 char *myerr;
   2052 char *mywrn;
   2053 char *mybug;
   2054 char myaccess;
   2055 char mywidth;
   2056 char mymode;
   2057 char myreg;
   2058 char mylen;
   2059 char *myleft;
   2060 char *myright;
   2061 char myndx;
   2062 int my_operand_length;
   2063 char my_immediate[200];
   2064 char my_indirect[200];
   2065 char my_displen[200];
   2066 
   2067 int
   2068 main (void)
   2069 {
   2070   printf ("enter immediate symbols eg enter #   ");
   2071   gets (my_immediate);
   2072   printf ("enter indirect symbols  eg enter @   ");
   2073   gets (my_indirect);
   2074   printf ("enter displen symbols   eg enter ^   ");
   2075   gets (my_displen);
   2076   vip_op_defaults (my_immediate, my_indirect, my_displen);
   2077 
   2078   for (;;)
   2079     {
   2080       printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
   2081       fflush (stdout);
   2082       gets (answer);
   2083       if (!answer[0])
   2084 	exit (EXIT_SUCCESS);
   2085       myaccess = answer[0];
   2086       mywidth = answer[1];
   2087       switch (mywidth)
   2088 	{
   2089 	case 'b':
   2090 	  my_operand_length = 1;
   2091 	  break;
   2092 	case 'd':
   2093 	  my_operand_length = 8;
   2094 	  break;
   2095 	case 'f':
   2096 	  my_operand_length = 4;
   2097 	  break;
   2098 	case 'g':
   2099 	  my_operand_length = 16;
   2100 	  break;
   2101 	case 'h':
   2102 	  my_operand_length = 32;
   2103 	  break;
   2104 	case 'l':
   2105 	  my_operand_length = 4;
   2106 	  break;
   2107 	case 'o':
   2108 	  my_operand_length = 16;
   2109 	  break;
   2110 	case 'q':
   2111 	  my_operand_length = 8;
   2112 	  break;
   2113 	case 'w':
   2114 	  my_operand_length = 2;
   2115 	  break;
   2116 	case '!':
   2117 	case '?':
   2118 	case '-':
   2119 	  my_operand_length = 0;
   2120 	  break;
   2121 
   2122 	default:
   2123 	  my_operand_length = 2;
   2124 	  printf ("I dn't understand access width %c\n", mywidth);
   2125 	  break;
   2126 	}
   2127       printf ("VAX assembler instruction operand: ");
   2128       fflush (stdout);
   2129       gets (answer);
   2130       mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
   2131 		      &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
   2132 		      &myerr, &mywrn);
   2133       if (*myerr)
   2134 	{
   2135 	  printf ("error: \"%s\"\n", myerr);
   2136 	  if (*mybug)
   2137 	    printf (" bug: \"%s\"\n", mybug);
   2138 	}
   2139       else
   2140 	{
   2141 	  if (*mywrn)
   2142 	    printf ("warning: \"%s\"\n", mywrn);
   2143 	  mumble ("mode", mymode);
   2144 	  mumble ("register", myreg);
   2145 	  mumble ("index", myndx);
   2146 	  printf ("width:'%c'  ", mylen);
   2147 	  printf ("expression: \"");
   2148 	  while (myleft <= myright)
   2149 	    putchar (*myleft++);
   2150 	  printf ("\"\n");
   2151 	}
   2152     }
   2153 }
   2154 
   2155 void
   2156 mumble (char *text, int value)
   2157 {
   2158   printf ("%s:", text);
   2159   if (value >= 0)
   2160     printf ("%xx", value);
   2161   else
   2162     printf ("ABSENT");
   2163   printf ("  ");
   2164 }
   2165 
   2166 #endif
   2167 
   2168 int md_short_jump_size = 3;
   2169 int md_long_jump_size = 6;
   2170 
   2171 void
   2172 md_create_short_jump (char *ptr,
   2173 		      addressT from_addr,
   2174 		      addressT to_addr ATTRIBUTE_UNUSED,
   2175 		      fragS *frag ATTRIBUTE_UNUSED,
   2176 		      symbolS *to_symbol ATTRIBUTE_UNUSED)
   2177 {
   2178   valueT offset;
   2179 
   2180   /* This former calculation was off by two:
   2181       offset = to_addr - (from_addr + 1);
   2182      We need to account for the one byte instruction and also its
   2183      two byte operand.  */
   2184   offset = to_addr - (from_addr + 1 + 2);
   2185   *ptr++ = VAX_BRW;		/* Branch with word (16 bit) offset.  */
   2186   md_number_to_chars (ptr, offset, 2);
   2187 }
   2188 
   2189 void
   2190 md_create_long_jump (char *ptr,
   2191 		     addressT from_addr ATTRIBUTE_UNUSED,
   2192 		     addressT to_addr,
   2193 		     fragS *frag,
   2194 		     symbolS *to_symbol)
   2195 {
   2196   valueT offset;
   2197 
   2198   offset = to_addr - S_GET_VALUE (to_symbol);
   2199   *ptr++ = VAX_JMP;		/* Arbitrary jump.  */
   2200   *ptr++ = VAX_ABSOLUTE_MODE;
   2201   md_number_to_chars (ptr, offset, 4);
   2202   fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (long) 0, 0, NO_RELOC);
   2203 }
   2204 
   2205 #ifdef OBJ_VMS
   2207 const char *md_shortopts = "d:STt:V+1h:Hv::";
   2208 #elif defined(OBJ_ELF)
   2209 const char *md_shortopts = "d:STt:VkKQ:";
   2210 #else
   2211 const char *md_shortopts = "d:STt:V";
   2212 #endif
   2213 struct option md_longopts[] =
   2214 {
   2215 #ifdef OBJ_ELF
   2216 #define OPTION_PIC (OPTION_MD_BASE)
   2217   { "pic", no_argument, NULL, OPTION_PIC },
   2218 #endif
   2219   { NULL, no_argument, NULL, 0 }
   2220 };
   2221 size_t md_longopts_size = sizeof (md_longopts);
   2222 
   2223 int
   2224 md_parse_option (int c, char *arg)
   2225 {
   2226   switch (c)
   2227     {
   2228     case 'S':
   2229       as_warn (_("SYMBOL TABLE not implemented"));
   2230       break;
   2231 
   2232     case 'T':
   2233       as_warn (_("TOKEN TRACE not implemented"));
   2234       break;
   2235 
   2236     case 'd':
   2237       as_warn (_("Displacement length %s ignored!"), arg);
   2238       break;
   2239 
   2240     case 't':
   2241       as_warn (_("I don't need or use temp. file \"%s\"."), arg);
   2242       break;
   2243 
   2244     case 'V':
   2245       as_warn (_("I don't use an interpass file! -V ignored"));
   2246       break;
   2247 
   2248 #ifdef OBJ_VMS
   2249     case '+':			/* For g++.  Hash any name > 31 chars long.  */
   2250       flag_hash_long_names = 1;
   2251       break;
   2252 
   2253     case '1':			/* For backward compatibility.  */
   2254       flag_one = 1;
   2255       break;
   2256 
   2257     case 'H':			/* Show new symbol after hash truncation.  */
   2258       flag_show_after_trunc = 1;
   2259       break;
   2260 
   2261     case 'h':			/* No hashing of mixed-case names.  */
   2262       {
   2263 	extern char vms_name_mapping;
   2264 	vms_name_mapping = atoi (arg);
   2265 	flag_no_hash_mixed_case = 1;
   2266       }
   2267       break;
   2268 
   2269     case 'v':
   2270       {
   2271 	extern char *compiler_version_string;
   2272 
   2273 	if (!arg || !*arg || access (arg, 0) == 0)
   2274 	  return 0;		/* Have caller show the assembler version.  */
   2275 	compiler_version_string = arg;
   2276       }
   2277       break;
   2278 #endif
   2279 
   2280 #ifdef OBJ_ELF
   2281     case OPTION_PIC:
   2282     case 'k':
   2283       flag_want_pic = 1;
   2284       break;			/* -pic, Position Independent Code.  */
   2285 
   2286      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment
   2287 	section should be emitted or not.  FIXME: Not implemented.  */
   2288     case 'Q':
   2289       break;
   2290 #endif
   2291 
   2292     default:
   2293       return 0;
   2294     }
   2295 
   2296   return 1;
   2297 }
   2298 
   2299 void
   2300 md_show_usage (FILE *stream)
   2301 {
   2302   fprintf (stream, _("\
   2303 VAX options:\n\
   2304 -d LENGTH		ignored\n\
   2305 -J			ignored\n\
   2306 -S			ignored\n\
   2307 -t FILE			ignored\n\
   2308 -T			ignored\n\
   2309 -V			ignored\n"));
   2310 #ifdef OBJ_VMS
   2311   fprintf (stream, _("\
   2312 VMS options:\n\
   2313 -+			hash encode names longer than 31 characters\n\
   2314 -1			`const' handling compatible with gcc 1.x\n\
   2315 -H			show new symbol after hash truncation\n\
   2316 -h NUM			don't hash mixed-case names, and adjust case:\n\
   2317 			0 = upper, 2 = lower, 3 = preserve case\n\
   2318 -v\"VERSION\"		code being assembled was produced by compiler \"VERSION\"\n"));
   2319 #endif
   2320 }
   2321 
   2322 /* We have no need to default values of symbols.  */
   2324 
   2325 symbolS *
   2326 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
   2327 {
   2328   return NULL;
   2329 }
   2330 
   2331 /* Round up a section size to the appropriate boundary.  */
   2332 valueT
   2333 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
   2334 {
   2335   /* Byte alignment is fine */
   2336   return size;
   2337 }
   2338 
   2339 /* Exactly what point is a PC-relative offset relative TO?
   2340    On the vax, they're relative to the address of the offset, plus
   2341    its size. */
   2342 long
   2343 md_pcrel_from (fixS *fixP)
   2344 {
   2345   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
   2346 }
   2347 
   2348 arelent *
   2349 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
   2350 {
   2351   arelent *reloc;
   2352   bfd_reloc_code_real_type code;
   2353 
   2354   if (fixp->fx_tcbit)
   2355     abort ();
   2356 
   2357   if (fixp->fx_r_type != BFD_RELOC_NONE)
   2358     {
   2359       code = fixp->fx_r_type;
   2360 
   2361       if (fixp->fx_pcrel)
   2362 	{
   2363 	  switch (code)
   2364 	    {
   2365 	    case BFD_RELOC_8_PCREL:
   2366 	    case BFD_RELOC_16_PCREL:
   2367 	    case BFD_RELOC_32_PCREL:
   2368 #ifdef OBJ_ELF
   2369 	    case BFD_RELOC_8_GOT_PCREL:
   2370 	    case BFD_RELOC_16_GOT_PCREL:
   2371 	    case BFD_RELOC_32_GOT_PCREL:
   2372 	    case BFD_RELOC_8_PLT_PCREL:
   2373 	    case BFD_RELOC_16_PLT_PCREL:
   2374 	    case BFD_RELOC_32_PLT_PCREL:
   2375 #endif
   2376 	      break;
   2377 	    default:
   2378 	      as_bad_where (fixp->fx_file, fixp->fx_line,
   2379 			    _("Cannot make %s relocation PC relative"),
   2380 			    bfd_get_reloc_code_name (code));
   2381 	    }
   2382 	}
   2383     }
   2384   else
   2385     {
   2386 #define F(SZ,PCREL)		(((SZ) << 1) + (PCREL))
   2387       switch (F (fixp->fx_size, fixp->fx_pcrel))
   2388 	{
   2389 #define MAP(SZ,PCREL,TYPE)	case F(SZ,PCREL): code = (TYPE); break
   2390 	  MAP (1, 0, BFD_RELOC_8);
   2391 	  MAP (2, 0, BFD_RELOC_16);
   2392 	  MAP (4, 0, BFD_RELOC_32);
   2393 	  MAP (1, 1, BFD_RELOC_8_PCREL);
   2394 	  MAP (2, 1, BFD_RELOC_16_PCREL);
   2395 	  MAP (4, 1, BFD_RELOC_32_PCREL);
   2396 	default:
   2397 	  abort ();
   2398 	}
   2399     }
   2400 #undef F
   2401 #undef MAP
   2402 
   2403   reloc = xmalloc (sizeof (arelent));
   2404   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
   2405   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   2406   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   2407 #ifndef OBJ_ELF
   2408   if (fixp->fx_pcrel)
   2409     reloc->addend = fixp->fx_addnumber;
   2410   else
   2411     reloc->addend = 0;
   2412 #else
   2413   reloc->addend = fixp->fx_offset;
   2414 #endif
   2415 
   2416   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
   2417   assert (reloc->howto != 0);
   2418 
   2419   return reloc;
   2420 }
   2421 
   2422 /* vax:md_assemble() emit frags for 1 instruction given in textual form.  */
   2423 void
   2424 md_assemble (char *instruction_string)
   2425 {
   2426   /* Non-zero if operand expression's segment is not known yet.  */
   2427   int is_undefined;
   2428   /* Non-zero if operand expression's segment is absolute.  */
   2429   int is_absolute;
   2430   int length_code;
   2431   char *p;
   2432   /* An operand. Scans all operands.  */
   2433   struct vop *operandP;
   2434   char *save_input_line_pointer;
   2435 			/* What used to live after an expression.  */
   2436   char c_save;
   2437   /* 1: instruction_string bad for all passes.  */
   2438   int goofed;
   2439   /* Points to slot just after last operand.  */
   2440   struct vop *end_operandP;
   2441   /* Points to expression values for this operand.  */
   2442   expressionS *expP;
   2443   segT *segP;
   2444 
   2445   /* These refer to an instruction operand expression.  */
   2446   /* Target segment of the address.	 */
   2447   segT to_seg;
   2448   valueT this_add_number;
   2449   /* Positive (minuend) symbol.  */
   2450   symbolS *this_add_symbol;
   2451   /* As a number.  */
   2452   long opcode_as_number;
   2453   /* Least significant byte 1st.  */
   2454   char *opcode_as_chars;
   2455   /* As an array of characters.  */
   2456   /* Least significant byte 1st */
   2457   char *opcode_low_byteP;
   2458   /* length (bytes) meant by vop_short.  */
   2459   int length;
   2460   /* 0, or 1 if '@' is in addressing mode.  */
   2461   int at;
   2462   /* From vop_nbytes: vax_operand_width (in bytes) */
   2463   int nbytes;
   2464   FLONUM_TYPE *floatP;
   2465   LITTLENUM_TYPE literal_float[8];
   2466   /* Big enough for any floating point literal.  */
   2467 
   2468   vip (&v, instruction_string);
   2469 
   2470   /* Now we try to find as many as_warn()s as we can. If we do any as_warn()s
   2471      then goofed=1. Notice that we don't make any frags yet.
   2472      Should goofed be 1, then this instruction will wedge in any pass,
   2473      and we can safely flush it, without causing interpass symbol phase
   2474      errors. That is, without changing label values in different passes.  */
   2475   if ((goofed = (*v.vit_error)) != 0)
   2476     {
   2477       as_fatal (_("Ignoring statement due to \"%s\""), v.vit_error);
   2478     }
   2479   /* We need to use expression() and friends, which require us to diddle
   2480      input_line_pointer. So we save it and restore it later.  */
   2481   save_input_line_pointer = input_line_pointer;
   2482   for (operandP = v.vit_operand,
   2483        expP = exp_of_operand,
   2484        segP = seg_of_operand,
   2485        floatP = float_operand,
   2486        end_operandP = v.vit_operand + v.vit_operands;
   2487 
   2488        operandP < end_operandP;
   2489 
   2490        operandP++, expP++, segP++, floatP++)
   2491     {
   2492       if (operandP->vop_error)
   2493 	{
   2494 	  as_fatal (_("Aborting because statement has \"%s\""), operandP->vop_error);
   2495 	  goofed = 1;
   2496 	}
   2497       else
   2498 	{
   2499 	  /* Statement has no syntax goofs: let's sniff the expression.  */
   2500 	  int can_be_short = 0;	/* 1 if a bignum can be reduced to a short literal.  */
   2501 
   2502 	  input_line_pointer = operandP->vop_expr_begin;
   2503 	  c_save = operandP->vop_expr_end[1];
   2504 	  operandP->vop_expr_end[1] = '\0';
   2505 	  /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1.  */
   2506 	  *segP = expression (expP);
   2507 	  switch (expP->X_op)
   2508 	    {
   2509 	    case O_absent:
   2510 	      /* for BSD4.2 compatibility, missing expression is absolute 0 */
   2511 	      expP->X_op = O_constant;
   2512 	      expP->X_add_number = 0;
   2513 	      /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
   2514 		 X_add_symbol to any particular value.  But, we will program
   2515 		 defensively. Since this situation occurs rarely so it costs
   2516 		 us little to do, and stops Dean worrying about the origin of
   2517 		 random bits in expressionS's.  */
   2518 	      expP->X_add_symbol = NULL;
   2519 	      expP->X_op_symbol = NULL;
   2520 	      break;
   2521 
   2522 	    case O_symbol:
   2523 	    case O_constant:
   2524 	      break;
   2525 
   2526 	    default:
   2527 	      /* Major bug. We can't handle the case of a
   2528 	         SEG_OP expression in a VIT_OPCODE_SYNTHETIC
   2529 	         variable-length instruction.
   2530 	         We don't have a frag type that is smart enough to
   2531 	         relax a SEG_OP, and so we just force all
   2532 	         SEG_OPs to behave like SEG_PASS1s.
   2533 	         Clearly, if there is a demand we can invent a new or
   2534 	         modified frag type and then coding up a frag for this
   2535 	         case will be easy. SEG_OP was invented for the
   2536 	         .words after a CASE opcode, and was never intended for
   2537 	         instruction operands.  */
   2538 	      need_pass_2 = 1;
   2539 	      as_fatal (_("Can't relocate expression"));
   2540 	      break;
   2541 
   2542 	    case O_big:
   2543 	      /* Preserve the bits.  */
   2544 	      if (expP->X_add_number > 0)
   2545 		{
   2546 		  bignum_copy (generic_bignum, expP->X_add_number,
   2547 			       floatP->low, SIZE_OF_LARGE_NUMBER);
   2548 		}
   2549 	      else
   2550 		{
   2551 		  know (expP->X_add_number < 0);
   2552 		  flonum_copy (&generic_floating_point_number,
   2553 			       floatP);
   2554 		  if (strchr ("s i", operandP->vop_short))
   2555 		    {
   2556 		      /* Could possibly become S^# */
   2557 		      flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
   2558 		      switch (-expP->X_add_number)
   2559 			{
   2560 			case 'f':
   2561 			  can_be_short =
   2562 			    (literal_float[0] & 0xFC0F) == 0x4000
   2563 			    && literal_float[1] == 0;
   2564 			  break;
   2565 
   2566 			case 'd':
   2567 			  can_be_short =
   2568 			    (literal_float[0] & 0xFC0F) == 0x4000
   2569 			    && literal_float[1] == 0
   2570 			    && literal_float[2] == 0
   2571 			    && literal_float[3] == 0;
   2572 			  break;
   2573 
   2574 			case 'g':
   2575 			  can_be_short =
   2576 			    (literal_float[0] & 0xFF81) == 0x4000
   2577 			    && literal_float[1] == 0
   2578 			    && literal_float[2] == 0
   2579 			    && literal_float[3] == 0;
   2580 			  break;
   2581 
   2582 			case 'h':
   2583 			  can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
   2584 					  && (literal_float[1] & 0xE000) == 0
   2585 					  && literal_float[2] == 0
   2586 					  && literal_float[3] == 0
   2587 					  && literal_float[4] == 0
   2588 					  && literal_float[5] == 0
   2589 					  && literal_float[6] == 0
   2590 					  && literal_float[7] == 0);
   2591 			  break;
   2592 
   2593 			default:
   2594 			  BAD_CASE (-expP->X_add_number);
   2595 			  break;
   2596 			}
   2597 		    }
   2598 		}
   2599 
   2600 	      if (operandP->vop_short == 's'
   2601 		  || operandP->vop_short == 'i'
   2602 		  || (operandP->vop_short == ' '
   2603 		      && operandP->vop_reg == 0xF
   2604 		      && (operandP->vop_mode & 0xE) == 0x8))
   2605 		{
   2606 		  /* Saw a '#'.  */
   2607 		  if (operandP->vop_short == ' ')
   2608 		    {
   2609 		      /* We must chose S^ or I^.  */
   2610 		      if (expP->X_add_number > 0)
   2611 			{
   2612 			  /* Bignum: Short literal impossible.  */
   2613 			  operandP->vop_short = 'i';
   2614 			  operandP->vop_mode = 8;
   2615 			  operandP->vop_reg = 0xF;	/* VAX PC.  */
   2616 			}
   2617 		      else
   2618 			{
   2619 			  /* Flonum: Try to do it.  */
   2620 			  if (can_be_short)
   2621 			    {
   2622 			      operandP->vop_short = 's';
   2623 			      operandP->vop_mode = 0;
   2624 			      operandP->vop_ndx = -1;
   2625 			      operandP->vop_reg = -1;
   2626 			      expP->X_op = O_constant;
   2627 			    }
   2628 			  else
   2629 			    {
   2630 			      operandP->vop_short = 'i';
   2631 			      operandP->vop_mode = 8;
   2632 			      operandP->vop_reg = 0xF;	/* VAX PC */
   2633 			    }
   2634 			}	/* bignum or flonum ? */
   2635 		    }		/*  if #, but no S^ or I^ seen.  */
   2636 		  /* No more ' ' case: either 's' or 'i'.  */
   2637 		  if (operandP->vop_short == 's')
   2638 		    {
   2639 		      /* Wants to be a short literal.  */
   2640 		      if (expP->X_add_number > 0)
   2641 			{
   2642 			  as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
   2643 			  operandP->vop_short = 'i';
   2644 			  operandP->vop_mode = 8;
   2645 			  operandP->vop_reg = 0xF;	/* VAX PC.  */
   2646 			}
   2647 		      else
   2648 			{
   2649 			  if (!can_be_short)
   2650 			    {
   2651 			      as_warn (_("Can't do flonum short literal: immediate mode used."));
   2652 			      operandP->vop_short = 'i';
   2653 			      operandP->vop_mode = 8;
   2654 			      operandP->vop_reg = 0xF;	/* VAX PC.  */
   2655 			    }
   2656 			  else
   2657 			    {
   2658 			      /* Encode short literal now.  */
   2659 			      int temp = 0;
   2660 
   2661 			      switch (-expP->X_add_number)
   2662 				{
   2663 				case 'f':
   2664 				case 'd':
   2665 				  temp = literal_float[0] >> 4;
   2666 				  break;
   2667 
   2668 				case 'g':
   2669 				  temp = literal_float[0] >> 1;
   2670 				  break;
   2671 
   2672 				case 'h':
   2673 				  temp = ((literal_float[0] << 3) & 070)
   2674 				    | ((literal_float[1] >> 13) & 07);
   2675 				  break;
   2676 
   2677 				default:
   2678 				  BAD_CASE (-expP->X_add_number);
   2679 				  break;
   2680 				}
   2681 
   2682 			      floatP->low[0] = temp & 077;
   2683 			      floatP->low[1] = 0;
   2684 			    }
   2685 			}
   2686 		    }
   2687 		  else
   2688 		    {
   2689 		      /* I^# seen: set it up if float.  */
   2690 		      if (expP->X_add_number < 0)
   2691 			{
   2692 			  memcpy (floatP->low, literal_float, sizeof (literal_float));
   2693 			}
   2694 		    }		/* if S^# seen.  */
   2695 		}
   2696 	      else
   2697 		{
   2698 		  as_warn (_("A bignum/flonum may not be a displacement: 0x%lx used"),
   2699 			   (expP->X_add_number = 0x80000000L));
   2700 		  /* Chosen so luser gets the most offset bits to patch later.  */
   2701 		}
   2702 	      expP->X_add_number = floatP->low[0]
   2703 		| ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
   2704 
   2705 	      /* For the O_big case we have:
   2706 	         If vop_short == 's' then a short floating literal is in the
   2707 	        	lowest 6 bits of floatP -> low [0], which is
   2708 	        	big_operand_bits [---] [0].
   2709 	         If vop_short == 'i' then the appropriate number of elements
   2710 	        	of big_operand_bits [---] [...] are set up with the correct
   2711 	        	bits.
   2712 	         Also, just in case width is byte word or long, we copy the lowest
   2713 	         32 bits of the number to X_add_number.  */
   2714 	      break;
   2715 	    }
   2716 	  if (input_line_pointer != operandP->vop_expr_end + 1)
   2717 	    {
   2718 	      as_fatal ("Junk at end of expression \"%s\"", input_line_pointer);
   2719 	      goofed = 1;
   2720 	    }
   2721 	  operandP->vop_expr_end[1] = c_save;
   2722 	}
   2723     }
   2724 
   2725   input_line_pointer = save_input_line_pointer;
   2726 
   2727   if (need_pass_2 || goofed)
   2728     return;
   2729 
   2730   /* Emit op-code.  */
   2731   /* Remember where it is, in case we want to modify the op-code later.  */
   2732   opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
   2733   memcpy (opcode_low_byteP, v.vit_opcode, v.vit_opcode_nbytes);
   2734   opcode_as_chars = v.vit_opcode;
   2735   opcode_as_number = md_chars_to_number ((unsigned char *) opcode_as_chars, 4);
   2736   for (operandP = v.vit_operand,
   2737        expP = exp_of_operand,
   2738        segP = seg_of_operand,
   2739        floatP = float_operand,
   2740        end_operandP = v.vit_operand + v.vit_operands;
   2741 
   2742        operandP < end_operandP;
   2743 
   2744        operandP++,
   2745        floatP++,
   2746        segP++,
   2747        expP++)
   2748     {
   2749       if (operandP->vop_ndx >= 0)
   2750 	{
   2751 	  /* Indexed addressing byte.  */
   2752 	  /* Legality of indexed mode already checked: it is OK.  */
   2753 	  FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
   2754 	}			/* if(vop_ndx>=0) */
   2755 
   2756       /* Here to make main operand frag(s).  */
   2757       this_add_number = expP->X_add_number;
   2758       this_add_symbol = expP->X_add_symbol;
   2759       to_seg = *segP;
   2760       is_undefined = (to_seg == undefined_section);
   2761       is_absolute = (to_seg == absolute_section);
   2762       at = operandP->vop_mode & 1;
   2763       length = (operandP->vop_short == 'b'
   2764 		? 1 : (operandP->vop_short == 'w'
   2765 		       ? 2 : (operandP->vop_short == 'l'
   2766 			      ? 4 : 0)));
   2767       nbytes = operandP->vop_nbytes;
   2768       if (operandP->vop_access == 'b')
   2769 	{
   2770 	  if (to_seg == now_seg || is_undefined)
   2771 	    {
   2772 	      /* If is_undefined, then it might BECOME now_seg.  */
   2773 	      if (nbytes)
   2774 		{
   2775 		  p = frag_more (nbytes);
   2776 		  fix_new (frag_now, p - frag_now->fr_literal, nbytes,
   2777 			   this_add_symbol, this_add_number, 1, NO_RELOC);
   2778 		}
   2779 	      else
   2780 		{
   2781 		  /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
   2782 		  /* nbytes==0 */
   2783 		  length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
   2784 		  if (opcode_as_number & VIT_OPCODE_SPECIAL)
   2785 		    {
   2786 		      if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
   2787 			{
   2788 			  /* br or jsb */
   2789 			  frag_var (rs_machine_dependent, 5, 1,
   2790 			    ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
   2791 				    this_add_symbol, this_add_number,
   2792 				    opcode_low_byteP);
   2793 			}
   2794 		      else
   2795 			{
   2796 			  if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
   2797 			    {
   2798 			      length_code = STATE_WORD;
   2799 			      /* JF: There is no state_byte for this one! */
   2800 			      frag_var (rs_machine_dependent, 10, 2,
   2801 					ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
   2802 					this_add_symbol, this_add_number,
   2803 					opcode_low_byteP);
   2804 			    }
   2805 			  else
   2806 			    {
   2807 			      know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
   2808 			      frag_var (rs_machine_dependent, 9, 1,
   2809 			      ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
   2810 					this_add_symbol, this_add_number,
   2811 					opcode_low_byteP);
   2812 			    }
   2813 			}
   2814 		    }
   2815 		  else
   2816 		    {
   2817 		      know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
   2818 		      frag_var (rs_machine_dependent, 7, 1,
   2819 		       ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
   2820 				this_add_symbol, this_add_number,
   2821 				opcode_low_byteP);
   2822 		    }
   2823 		}
   2824 	    }
   2825 	  else
   2826 	    {
   2827 	      /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
   2828 	      /* --- SEG FLOAT MAY APPEAR HERE ---  */
   2829 	      if (is_absolute)
   2830 		{
   2831 		  if (nbytes)
   2832 		    {
   2833 		      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
   2834 		      p = frag_more (nbytes);
   2835 		      /* Conventional relocation.  */
   2836 		      fix_new (frag_now, p - frag_now->fr_literal, nbytes,
   2837 			       section_symbol (absolute_section),
   2838 			       this_add_number, 1, NO_RELOC);
   2839 		    }
   2840 		  else
   2841 		    {
   2842 		      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
   2843 		      if (opcode_as_number & VIT_OPCODE_SPECIAL)
   2844 			{
   2845 			  if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
   2846 			    {
   2847 			      /* br or jsb */
   2848 			      *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
   2849 			      know (opcode_as_chars[1] == 0);
   2850 			      p = frag_more (5);
   2851 			      p[0] = VAX_ABSOLUTE_MODE;	/* @#...  */
   2852 			      md_number_to_chars (p + 1, this_add_number, 4);
   2853 			      /* Now (eg) JMP @#foo or JSB @#foo.  */
   2854 			    }
   2855 			  else
   2856 			    {
   2857 			      if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
   2858 				{
   2859 				  p = frag_more (10);
   2860 				  p[0] = 2;
   2861 				  p[1] = 0;
   2862 				  p[2] = VAX_BRB;
   2863 				  p[3] = 6;
   2864 				  p[4] = VAX_JMP;
   2865 				  p[5] = VAX_ABSOLUTE_MODE;	/* @#...  */
   2866 				  md_number_to_chars (p + 6, this_add_number, 4);
   2867 				  /* Now (eg)	ACBx	1f
   2868 				    		BRB	2f
   2869 				    	1:	JMP	@#foo
   2870 				    	2:  */
   2871 				}
   2872 			      else
   2873 				{
   2874 				  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
   2875 				  p = frag_more (9);
   2876 				  p[0] = 2;
   2877 				  p[1] = VAX_BRB;
   2878 				  p[2] = 6;
   2879 				  p[3] = VAX_JMP;
   2880                                   p[4] = VAX_ABSOLUTE_MODE;     /* @#...  */
   2881 				  md_number_to_chars (p + 5, this_add_number, 4);
   2882 				  /* Now (eg)	xOBxxx	1f
   2883 				   		BRB	2f
   2884 				   	1:	JMP	@#foo
   2885 				   	2:  */
   2886 				}
   2887 			    }
   2888 			}
   2889 		      else
   2890 			{
   2891 			  /* b<cond> */
   2892 			  *opcode_low_byteP ^= 1;
   2893 			  /* To reverse the condition in a VAX branch,
   2894 			     complement the lowest order bit.  */
   2895 			  p = frag_more (7);
   2896 			  p[0] = 6;
   2897 			  p[1] = VAX_JMP;
   2898 			  p[2] = VAX_ABSOLUTE_MODE;	/* @#...  */
   2899 			  md_number_to_chars (p + 3, this_add_number, 4);
   2900 			  /* Now (eg)	BLEQ	1f
   2901 			   		JMP	@#foo
   2902 			   	1:  */
   2903 			}
   2904 		    }
   2905 		}
   2906 	      else
   2907 		{
   2908 		  /* to_seg != now_seg && !is_undefinfed && !is_absolute */
   2909 		  if (nbytes > 0)
   2910 		    {
   2911 		      /* Pc-relative. Conventional relocation.  */
   2912 		      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
   2913 		      p = frag_more (nbytes);
   2914 		      fix_new (frag_now, p - frag_now->fr_literal, nbytes,
   2915 			       section_symbol (absolute_section),
   2916 			       this_add_number, 1, NO_RELOC);
   2917 		    }
   2918 		  else
   2919 		    {
   2920 		      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
   2921 		      if (opcode_as_number & VIT_OPCODE_SPECIAL)
   2922 			{
   2923 			  if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
   2924 			    {
   2925 			      /* br or jsb */
   2926 			      know (opcode_as_chars[1] == 0);
   2927 			      *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
   2928 			      p = frag_more (5);
   2929 			      p[0] = VAX_PC_RELATIVE_MODE;
   2930 			      fix_new (frag_now,
   2931 				       p + 1 - frag_now->fr_literal, 4,
   2932 				       this_add_symbol,
   2933 				       this_add_number, 1, NO_RELOC);
   2934 			      /* Now eg JMP foo or JSB foo.  */
   2935 			    }
   2936 			  else
   2937 			    {
   2938 			      if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
   2939 				{
   2940 				  p = frag_more (10);
   2941 				  p[0] = 0;
   2942 				  p[1] = 2;
   2943 				  p[2] = VAX_BRB;
   2944 				  p[3] = 6;
   2945 				  p[4] = VAX_JMP;
   2946 				  p[5] = VAX_PC_RELATIVE_MODE;
   2947 				  fix_new (frag_now,
   2948 					   p + 6 - frag_now->fr_literal, 4,
   2949 					   this_add_symbol,
   2950 					   this_add_number, 1, NO_RELOC);
   2951 				  /* Now (eg)	ACBx	1f
   2952 				   		BRB	2f
   2953 				   	1:	JMP	foo
   2954 				   	2:  */
   2955 				}
   2956 			      else
   2957 				{
   2958 				  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
   2959 				  p = frag_more (10);
   2960 				  p[0] = 2;
   2961 				  p[1] = VAX_BRB;
   2962 				  p[2] = 6;
   2963 				  p[3] = VAX_JMP;
   2964 				  p[4] = VAX_PC_RELATIVE_MODE;
   2965 				  fix_new (frag_now,
   2966 					   p + 5 - frag_now->fr_literal,
   2967 					   4, this_add_symbol,
   2968 					   this_add_number, 1, NO_RELOC);
   2969 				  /* Now (eg)	xOBxxx	1f
   2970 				   		BRB	2f
   2971 				   	1:	JMP	foo
   2972 				   	2:  */
   2973 				}
   2974 			    }
   2975 			}
   2976 		      else
   2977 			{
   2978 			  know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
   2979 			  *opcode_low_byteP ^= 1;	/* Reverse branch condition.  */
   2980 			  p = frag_more (7);
   2981 			  p[0] = 6;
   2982 			  p[1] = VAX_JMP;
   2983 			  p[2] = VAX_PC_RELATIVE_MODE;
   2984 			  fix_new (frag_now, p + 3 - frag_now->fr_literal,
   2985 				   4, this_add_symbol,
   2986 				   this_add_number, 1, NO_RELOC);
   2987 			}
   2988 		    }
   2989 		}
   2990 	    }
   2991 	}
   2992       else
   2993 	{
   2994 	  /* So it is ordinary operand.  */
   2995 	  know (operandP->vop_access != 'b');
   2996 	  /* ' ' target-independent: elsewhere.  */
   2997 	  know (operandP->vop_access != ' ');
   2998 	  know (operandP->vop_access == 'a'
   2999 		|| operandP->vop_access == 'm'
   3000 		|| operandP->vop_access == 'r'
   3001 		|| operandP->vop_access == 'v'
   3002 		|| operandP->vop_access == 'w');
   3003 	  if (operandP->vop_short == 's')
   3004 	    {
   3005 	      if (is_absolute)
   3006 		{
   3007 		  if (this_add_number >= 64)
   3008 		    {
   3009 		      as_warn (_("Short literal overflow(%ld.), immediate mode assumed."),
   3010 			       (long) this_add_number);
   3011 		      operandP->vop_short = 'i';
   3012 		      operandP->vop_mode = 8;
   3013 		      operandP->vop_reg = 0xF;
   3014 		    }
   3015 		}
   3016 	      else
   3017 		{
   3018 		  as_warn (_("Forced short literal to immediate mode. now_seg=%s to_seg=%s"),
   3019 			   segment_name (now_seg), segment_name (to_seg));
   3020 		  operandP->vop_short = 'i';
   3021 		  operandP->vop_mode = 8;
   3022 		  operandP->vop_reg = 0xF;
   3023 		}
   3024 	    }
   3025 	  if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
   3026 		  || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
   3027 	    {
   3028 	      /* One byte operand.  */
   3029 	      know (operandP->vop_mode > 3);
   3030 	      FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
   3031 	      /* All 1-bytes except S^# happen here.  */
   3032 	    }
   3033 	  else
   3034 	    {
   3035 	      /* {@}{q^}foo{(Rn)} or S^#foo */
   3036 	      if (operandP->vop_reg == -1 && operandP->vop_short != 's')
   3037 		{
   3038 		  /* "{@}{q^}foo" */
   3039 		  if (to_seg == now_seg)
   3040 		    {
   3041 		      if (length == 0)
   3042 			{
   3043 			  know (operandP->vop_short == ' ');
   3044 			  length_code = STATE_BYTE;
   3045 #ifdef OBJ_ELF
   3046 			  if (S_IS_EXTERNAL (this_add_symbol)
   3047 			      || S_IS_WEAK (this_add_symbol))
   3048 			    length_code = STATE_UNDF;
   3049 #endif
   3050 			  p = frag_var (rs_machine_dependent, 10, 2,
   3051 			       ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
   3052 					this_add_symbol, this_add_number,
   3053 					opcode_low_byteP);
   3054 			  know (operandP->vop_mode == 10 + at);
   3055 			  *p = at << 4;
   3056 			  /* At is the only context we need to carry
   3057 			     to other side of relax() process.  Must
   3058 			     be in the correct bit position of VAX
   3059 			     operand spec. byte.  */
   3060 			}
   3061 		      else
   3062 			{
   3063 			  know (length);
   3064 			  know (operandP->vop_short != ' ');
   3065 			  p = frag_more (length + 1);
   3066 			  p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
   3067 			  fix_new (frag_now, p + 1 - frag_now->fr_literal,
   3068 				   length, this_add_symbol,
   3069 				   this_add_number, 1, NO_RELOC);
   3070 			}
   3071 		    }
   3072 		  else
   3073 		    {
   3074 		      /* to_seg != now_seg */
   3075 		      if (this_add_symbol == NULL)
   3076 			{
   3077 			  know (is_absolute);
   3078 			  /* Do @#foo: simpler relocation than foo-.(pc) anyway.  */
   3079 			  p = frag_more (5);
   3080 			  p[0] = VAX_ABSOLUTE_MODE;	/* @#...  */
   3081 			  md_number_to_chars (p + 1, this_add_number, 4);
   3082 			  if (length && length != 4)
   3083 			    as_warn (_("Length specification ignored. Address mode 9F used"));
   3084 			}
   3085 		      else
   3086 			{
   3087 			  /* {@}{q^}other_seg */
   3088 			  know ((length == 0 && operandP->vop_short == ' ')
   3089 			     || (length > 0 && operandP->vop_short != ' '));
   3090 			  if (is_undefined
   3091 #ifdef OBJ_ELF
   3092 			      || S_IS_WEAK(this_add_symbol)
   3093 			      || S_IS_EXTERNAL(this_add_symbol)
   3094 #endif
   3095 			      )
   3096 			    {
   3097 			      switch (length)
   3098 				{
   3099 				default: length_code = STATE_UNDF; break;
   3100 				case 1: length_code = STATE_BYTE; break;
   3101 				case 2: length_code = STATE_WORD; break;
   3102 				case 4: length_code = STATE_LONG; break;
   3103 				}
   3104 			      /* We have a SEG_UNKNOWN symbol. It might
   3105 			         turn out to be in the same segment as
   3106 			         the instruction, permitting relaxation.  */
   3107 			      p = frag_var (rs_machine_dependent, 5, 2,
   3108 			       ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
   3109 					    this_add_symbol, this_add_number,
   3110 					    opcode_low_byteP);
   3111 			      p[0] = at << 4;
   3112 			    }
   3113 			  else
   3114 			    {
   3115 			      if (length == 0)
   3116 				{
   3117 				  know (operandP->vop_short == ' ');
   3118 				  length = 4;	/* Longest possible.  */
   3119 				}
   3120 			      p = frag_more (length + 1);
   3121 			      p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
   3122 			      md_number_to_chars (p + 1, this_add_number, length);
   3123 			      fix_new (frag_now,
   3124 				       p + 1 - frag_now->fr_literal,
   3125 				       length, this_add_symbol,
   3126 				       this_add_number, 1, NO_RELOC);
   3127 			    }
   3128 			}
   3129 		    }
   3130 		}
   3131 	      else
   3132 		{
   3133 		  /* {@}{q^}foo(Rn) or S^# or I^# or # */
   3134 		  if (operandP->vop_mode < 0xA)
   3135 		    {
   3136 		      /* # or S^# or I^# */
   3137 		      if (operandP->vop_access == 'v'
   3138 			  || operandP->vop_access == 'a')
   3139 			{
   3140 			  if (operandP->vop_access == 'v')
   3141 			    as_warn (_("Invalid operand:  immediate value used as base address."));
   3142 			  else
   3143 			    as_warn (_("Invalid operand:  immediate value used as address."));
   3144 			  /* gcc 2.6.3 is known to generate these in at least
   3145 			     one case.  */
   3146 			}
   3147 		      if (length == 0
   3148 			  && is_absolute && (expP->X_op != O_big)
   3149 			  && operandP->vop_mode == 8	/* No '@'.  */
   3150 			  && this_add_number < 64)
   3151 			{
   3152 			  operandP->vop_short = 's';
   3153 			}
   3154 		      if (operandP->vop_short == 's')
   3155 			{
   3156 			  FRAG_APPEND_1_CHAR (this_add_number);
   3157 			}
   3158 		      else
   3159 			{
   3160 			  /* I^#...  */
   3161 			  know (nbytes);
   3162 			  p = frag_more (nbytes + 1);
   3163 			  know (operandP->vop_reg == 0xF);
   3164 #ifdef OBJ_ELF
   3165 			  if (flag_want_pic && operandP->vop_mode == 8
   3166 				&& this_add_symbol != NULL)
   3167 			    {
   3168 			      as_warn (_("Symbol used as immediate operand in PIC mode."));
   3169 			    }
   3170 #endif
   3171 			  p[0] = (operandP->vop_mode << 4) | 0xF;
   3172 			  if ((is_absolute) && (expP->X_op != O_big))
   3173 			    {
   3174 			      /* If nbytes > 4, then we are scrod. We
   3175 			         don't know if the high order bytes
   3176 			         are to be 0xFF or 0x00.  BSD4.2 & RMS
   3177 			         say use 0x00. OK --- but this
   3178 			         assembler needs ANOTHER rewrite to
   3179 			         cope properly with this bug.  */
   3180 			      md_number_to_chars (p + 1, this_add_number,
   3181 						  min (sizeof (valueT),
   3182 						       (size_t) nbytes));
   3183 			      if ((size_t) nbytes > sizeof (valueT))
   3184 				memset (p + 5, '\0', nbytes - sizeof (valueT));
   3185 			    }
   3186 			  else
   3187 			    {
   3188 			      if (expP->X_op == O_big)
   3189 				{
   3190 				  /* Problem here is to get the bytes
   3191 				     in the right order.  We stored
   3192 				     our constant as LITTLENUMs, not
   3193 				     bytes.  */
   3194 				  LITTLENUM_TYPE *lP;
   3195 
   3196 				  lP = floatP->low;
   3197 				  if (nbytes & 1)
   3198 				    {
   3199 				      know (nbytes == 1);
   3200 				      p[1] = *lP;
   3201 				    }
   3202 				  else
   3203 				    {
   3204 				      for (p++; nbytes; nbytes -= 2, p += 2, lP++)
   3205 					md_number_to_chars (p, *lP, 2);
   3206 				    }
   3207 				}
   3208 			      else
   3209 				{
   3210 				  fix_new (frag_now, p + 1 - frag_now->fr_literal,
   3211 					   nbytes, this_add_symbol,
   3212 					   this_add_number, 0, NO_RELOC);
   3213 				}
   3214 			    }
   3215 			}
   3216 		    }
   3217 		  else
   3218 		    {
   3219 		      /* {@}{q^}foo(Rn) */
   3220 		      know ((length == 0 && operandP->vop_short == ' ')
   3221 			    || (length > 0 && operandP->vop_short != ' '));
   3222 		      if (length == 0)
   3223 			{
   3224 			  if (is_absolute)
   3225 			    {
   3226 			      long test;
   3227 
   3228 			      test = this_add_number;
   3229 
   3230 			      if (test < 0)
   3231 				test = ~test;
   3232 
   3233 			      length = test & 0xffff8000 ? 4
   3234 				: test & 0xffffff80 ? 2
   3235 				: 1;
   3236 			    }
   3237 			  else
   3238 			    {
   3239 			      length = 4;
   3240 			    }
   3241 			}
   3242 		      p = frag_more (1 + length);
   3243 		      know (operandP->vop_reg >= 0);
   3244 		      p[0] = operandP->vop_reg
   3245 			| ((at | "?\12\14?\16"[length]) << 4);
   3246 		      if (is_absolute)
   3247 			{
   3248 			  md_number_to_chars (p + 1, this_add_number, length);
   3249 			}
   3250 		      else
   3251 			{
   3252 			  fix_new (frag_now, p + 1 - frag_now->fr_literal,
   3253 				   length, this_add_symbol,
   3254 				   this_add_number, 0, NO_RELOC);
   3255 			}
   3256 		    }
   3257 		}
   3258 	    }
   3259 	}
   3260     }
   3261 }
   3262 
   3263 void
   3264 md_begin (void)
   3265 {
   3266   const char *errtxt;
   3267   FLONUM_TYPE *fP;
   3268   int i;
   3269 
   3270   if ((errtxt = vip_begin (1, "$", "*", "`")) != 0)
   3271     as_fatal (_("VIP_BEGIN error:%s"), errtxt);
   3272 
   3273   for (i = 0, fP = float_operand;
   3274        fP < float_operand + VIT_MAX_OPERANDS;
   3275        i++, fP++)
   3276     {
   3277       fP->low = &big_operand_bits[i][0];
   3278       fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
   3279     }
   3280 }
   3281 
   3282 static char *vax_cons_special_reloc;
   3283 
   3284 void
   3285 vax_cons (expressionS *exp, int size)
   3286 {
   3287   char *save;
   3288 
   3289   SKIP_WHITESPACE ();
   3290   vax_cons_special_reloc = NULL;
   3291   save = input_line_pointer;
   3292   if (input_line_pointer[0] == '%')
   3293     {
   3294       if (strncmp (input_line_pointer + 1, "pcrel", 5) == 0)
   3295 	{
   3296 	  input_line_pointer += 6;
   3297 	  vax_cons_special_reloc = "pcrel";
   3298 	}
   3299       if (vax_cons_special_reloc)
   3300 	{
   3301 	  int bad = 0;
   3302 
   3303 	  switch (size)
   3304 	    {
   3305 	    case 1:
   3306 	      if (*input_line_pointer != '8')
   3307 		bad = 1;
   3308 	      input_line_pointer--;
   3309 	      break;
   3310 	    case 2:
   3311 	      if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
   3312 		bad = 1;
   3313 	      break;
   3314 	    case 4:
   3315 	      if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
   3316 		bad = 1;
   3317 	      break;
   3318 	    default:
   3319 	      bad = 1;
   3320 	      break;
   3321 	    }
   3322 
   3323 	  if (bad)
   3324 	    {
   3325 	      as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
   3326 		      vax_cons_special_reloc, size * 8, size);
   3327 	    }
   3328 	  else
   3329 	    {
   3330 	      input_line_pointer += 2;
   3331 	      if (*input_line_pointer != '(')
   3332 		{
   3333 		  as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3334 			  vax_cons_special_reloc, size * 8);
   3335 		  bad = 1;
   3336 		}
   3337 	    }
   3338 
   3339 	  if (bad)
   3340 	    {
   3341 	      input_line_pointer = save;
   3342 	      vax_cons_special_reloc = NULL;
   3343 	    }
   3344 	  else
   3345 	    {
   3346 	      int c;
   3347 	      char *end = ++input_line_pointer;
   3348 	      int npar = 0;
   3349 
   3350 	      while (! is_end_of_line[(c = *end)])
   3351 		{
   3352 		  if (c == '(')
   3353 	  	    npar++;
   3354 		  else if (c == ')')
   3355 	  	    {
   3356 		      if (!npar)
   3357 	      		break;
   3358 		      npar--;
   3359 		    }
   3360 	    	  end++;
   3361 		}
   3362 
   3363 	      if (c != ')')
   3364 		as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3365 			vax_cons_special_reloc, size * 8);
   3366 	      else
   3367 		{
   3368 		  *end = '\0';
   3369 		  expression (exp);
   3370 		  *end = c;
   3371 		  if (input_line_pointer != end)
   3372 		    {
   3373 		      as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3374 			      vax_cons_special_reloc, size * 8);
   3375 		    }
   3376 		  else
   3377 		    {
   3378 		      input_line_pointer++;
   3379 		      SKIP_WHITESPACE ();
   3380 		      c = *input_line_pointer;
   3381 		      if (! is_end_of_line[c] && c != ',')
   3382 			as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
   3383 			        vax_cons_special_reloc, size * 8);
   3384 		    }
   3385 		}
   3386 	    }
   3387 	}
   3388     }
   3389   if (vax_cons_special_reloc == NULL)
   3390     expression (exp);
   3391 }
   3392 
   3393 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
   3394    reloc for a cons.  */
   3395 
   3396 void
   3397 vax_cons_fix_new (fragS *frag, int where, unsigned int nbytes, expressionS *exp)
   3398 {
   3399   bfd_reloc_code_real_type r;
   3400 
   3401   r = (nbytes == 1 ? BFD_RELOC_8 :
   3402        (nbytes == 2 ? BFD_RELOC_16 : BFD_RELOC_32));
   3403 
   3404   if (vax_cons_special_reloc)
   3405     {
   3406       if (*vax_cons_special_reloc == 'p')
   3407 	{
   3408 	  switch (nbytes)
   3409 	    {
   3410 	    case 1: r = BFD_RELOC_8_PCREL; break;
   3411 	    case 2: r = BFD_RELOC_16_PCREL; break;
   3412 	    case 4: r = BFD_RELOC_32_PCREL; break;
   3413 	    default: abort ();
   3414 	    }
   3415 	}
   3416     }
   3417 
   3418   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
   3419   vax_cons_special_reloc = NULL;
   3420 }
   3421 
   3422 char *
   3423 md_atof (int type, char * litP, int * sizeP)
   3424 {
   3425   return vax_md_atof (type, litP, sizeP);
   3426 }
   3427