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