Home | History | Annotate | Line # | Download | only in config
tc-vax.c revision 1.1.1.8
      1 /* tc-vax.c - vax-specific -
      2    Copyright (C) 1987-2024 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_subtract (fixP);
    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[0] + 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[0] + 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 htab_t 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 void
    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 
    969   op_hash = str_htab_create ();
    970 
    971   for (vP = votstrs; *vP->vot_name; vP++)
    972     if (str_hash_insert (op_hash, vP->vot_name, &vP->vot_detail, 0) != NULL)
    973       as_fatal (_("duplicate %s"), vP->vot_name);
    974 
    975   if (synthetic_too)
    976     for (vP = synthetic_votstrs; *vP->vot_name; vP++)
    977       if (str_hash_insert (op_hash, vP->vot_name, &vP->vot_detail, 0) != NULL)
    978 	as_fatal (_("duplicate %s"), vP->vot_name);
    979 
    980 #ifndef CONST_TABLE
    981   vip_op_defaults (immediate, indirect, displen);
    982 #endif
    983 }
    984 
    985 /* Take 3 char.s, the last of which may be `\0` (non-existent)
    986    and return the VAX register number that they represent.
    987 
    988    Return -1 if they don't form a register name. Good names return
    989    a number from 0:15 inclusive.
    990 
    991    Case is not important in a name.
    992 
    993    Register names understood are:
    994 
    995   	R0
    996   	R1
    997   	R2
    998   	R3
    999   	R4
   1000   	R5
   1001   	R6
   1002    	R7
   1003   	R8
   1004   	R9
   1005   	R10
   1006   	R11
   1007   	R12	AP
   1008   	R13	FP
   1009   	R14	SP
   1010   	R15	PC  */
   1011 
   1012 #define AP 12
   1013 #define FP 13
   1014 #define SP 14
   1015 #define PC 15
   1016 
   1017 /* Returns the register number of something like '%r15' or 'ap', supplied
   1018    in four single chars. Returns -1 if the register isn't recognized,
   1019    0..15 otherwise.  */
   1020 static int
   1021 vax_reg_parse (char c1, char c2, char c3, char c4)
   1022 {
   1023   int retval = -1;
   1024 
   1025 #ifdef OBJ_ELF
   1026   if (c1 != '%')	/* Register prefixes are mandatory for ELF.  */
   1027     return retval;
   1028   c1 = c2;
   1029   c2 = c3;
   1030   c3 = c4;
   1031 #endif
   1032 #ifdef OBJ_VMS
   1033   if (c4 != 0)		/* Register prefixes are not allowed under VMS.  */
   1034     return retval;
   1035 #endif
   1036 #ifdef OBJ_AOUT
   1037   if (c1 == '%')	/* Register prefixes are optional under a.out.  */
   1038     {
   1039       c1 = c2;
   1040       c2 = c3;
   1041       c3 = c4;
   1042     }
   1043   else if (c3 && c4)	/* Can't be 4 characters long.  */
   1044     return retval;
   1045 #endif
   1046 
   1047   c1 = TOLOWER (c1);
   1048   c2 = TOLOWER (c2);
   1049   if (ISDIGIT (c2) && c1 == 'r')
   1050     {
   1051       retval = c2 - '0';
   1052       if (ISDIGIT (c3))
   1053 	{
   1054 	  retval = retval * 10 + c3 - '0';
   1055 	  retval = (retval > 15) ? -1 : retval;
   1056 	  /* clamp the register value to 1 hex digit */
   1057 	}
   1058       else if (c3)
   1059 	retval = -1;		/* c3 must be '\0' or a digit.  */
   1060     }
   1061   else if (c3)			/* There are no three letter regs.  */
   1062     retval = -1;
   1063   else if (c2 == 'p')
   1064     {
   1065       switch (c1)
   1066 	{
   1067 	case 's':
   1068 	  retval = SP;
   1069 	  break;
   1070 	case 'f':
   1071 	  retval = FP;
   1072 	  break;
   1073 	case 'a':
   1074 	  retval = AP;
   1075 	  break;
   1076 	default:
   1077 	  retval = -1;
   1078 	}
   1079     }
   1080   else if (c1 == 'p' && c2 == 'c')
   1081     retval = PC;
   1082   else
   1083     retval = -1;
   1084   return retval;
   1085 }
   1086 
   1087 /* Parse a vax operand in DEC assembler notation.
   1088    For speed, expect a string of whitespace to be reduced to a single ' '.
   1089    This is the case for GNU AS, and is easy for other DEC-compatible
   1090    assemblers.
   1091 
   1092    Knowledge about DEC VAX assembler operand notation lives here.
   1093    This doesn't even know what a register name is, except it believes
   1094    all register names are 2 or 3 characters, and lets vax_reg_parse() say
   1095    what number each name represents.
   1096    It does, however, know that PC, SP etc are special registers so it can
   1097    detect addressing modes that are silly for those registers.
   1098 
   1099    Where possible, it delivers 1 fatal or 1 warning message if the operand
   1100    is suspect. Exactly what we test for is still evolving.
   1101 
   1102    ---
   1103   	Arg block.
   1104 
   1105    There were a number of 'mismatched argument type' bugs to vip_op.
   1106    The most general solution is to typedef each (of many) arguments.
   1107    We used instead a typedef'd argument block. This is less modular
   1108    than using separate return pointers for each result, but runs faster
   1109    on most engines, and seems to keep programmers happy. It will have
   1110    to be done properly if we ever want to use vip_op as a general-purpose
   1111    module (it was designed to be).
   1112 
   1113  	G^
   1114 
   1115    Doesn't support DEC "G^" format operands. These always take 5 bytes
   1116    to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
   1117    optimising to (say) a "B^" if you are lucky in the way you link.
   1118    When someone builds a linker smart enough to convert "G^" to "B^", "W^"
   1119    whenever possible, then we should implement it.
   1120    If there is some other use for "G^", feel free to code it in!
   1121 
   1122   	speed
   1123 
   1124    If I nested if()s more, I could avoid testing (*err) which would save
   1125    time, space and page faults. I didn't nest all those if()s for clarity
   1126    and because I think the mode testing can be re-arranged 1st to test the
   1127    commoner constructs 1st. Does anybody have statistics on this?
   1128 
   1129   	error messages
   1130 
   1131    In future, we should be able to 'compose' error messages in a scratch area
   1132    and give the user MUCH more informative error messages. Although this takes
   1133    a little more code at run-time, it will make this module much more self-
   1134    documenting. As an example of what sucks now: most error messages have
   1135    hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
   1136    the Un*x characters "$`*", that most users will expect from this AS.
   1137 
   1138    ----
   1139 
   1140    The input is a string, ending with '\0'.
   1141 
   1142    We also require a 'hint' of what kind of operand is expected: so
   1143    we can remind caller not to write into literals for instance.
   1144 
   1145    The output is a skeletal instruction.
   1146 
   1147    The algorithm has two parts.
   1148    1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
   1149    2. express the @^#-()+[] as some parameters suited to further analysis.
   1150 
   1151    2nd step is where we detect the googles of possible invalid combinations
   1152    a human (or compiler) might write. Note that if we do a half-way
   1153    decent assembler, we don't know how long to make (eg) displacement
   1154    fields when we first meet them (because they may not have defined values).
   1155    So we must wait until we know how many bits are needed for each address,
   1156    then we can know both length and opcodes of instructions.
   1157    For reason(s) above, we will pass to our caller a 'broken' instruction
   1158    of these major components, from which our caller can generate instructions:
   1159     -  displacement length      I^ S^ L^ B^ W^ unspecified
   1160     -  mode                     (many)
   1161     -  register                 R0-R15 or absent
   1162     -  index register           R0-R15 or absent
   1163     -  expression text          what we don't parse
   1164     -  error text(s)            why we couldn't understand the operand
   1165 
   1166    ----
   1167 
   1168    To decode output of this, test errtxt. If errtxt[0] == '\0', then
   1169    we had no errors that prevented parsing. Also, if we ever report
   1170    an internal bug, errtxt[0] is set non-zero. So one test tells you
   1171    if the other outputs are to be taken seriously.
   1172 
   1173    ----
   1174 
   1175    Dec defines the semantics of address modes (and values)
   1176    by a two-letter code, explained here.
   1177 
   1178      letter 1:   access type
   1179 
   1180        a         address calculation - no data access, registers forbidden
   1181        b         branch displacement
   1182        m         read - let go of bus - write back    "modify"
   1183        r         read
   1184        v         bit field address: like 'a' but registers are OK
   1185        w         write
   1186        space	 no operator (eg ".long foo") [our convention]
   1187 
   1188      letter 2:   data type (i.e. width, alignment)
   1189 
   1190        b         byte
   1191        d         double precision floating point (D format)
   1192        f         single precision floating point (F format)
   1193        g         G format floating
   1194        h         H format floating
   1195        l         longword
   1196        o         octaword
   1197        q         quadword
   1198        w         word
   1199        ?	 simple synthetic branch operand
   1200        -	 unconditional synthetic JSB/JSR operand
   1201        !	 complex synthetic branch operand
   1202 
   1203    The '-?!' letter 2's are not for external consumption. They are used
   1204    for various assemblers. Generally, all unknown widths are assumed 0.
   1205    We don't limit your choice of width character.
   1206 
   1207    DEC operands are hard work to parse. For example, '@' as the first
   1208    character means indirect (deferred) mode but elsewhere it is a shift
   1209    operator.
   1210    The long-winded explanation of how this is supposed to work is
   1211    cancelled. Read a DEC vax manual.
   1212    We try hard not to parse anything that MIGHT be part of the expression
   1213    buried in that syntax. For example if we see @...(Rn) we don't check
   1214    for '-' before the '(' because mode @-(Rn) does not exist.
   1215 
   1216    After parsing we have:
   1217 
   1218    at                     1 if leading '@' (or Un*x '*')
   1219    len                    takes one value from " bilsw". eg B^ -> 'b'.
   1220    hash                   1 if leading '#' (or Un*x '$')
   1221    expr_begin, expr_end   the expression we did not parse
   1222                           even though we don't interpret it, we make use
   1223                           of its presence or absence.
   1224    sign                   -1: -(Rn)    0: absent    +1: (Rn)+
   1225    paren                  1 if () are around register
   1226    reg                    major register number 0:15    -1 means absent
   1227    ndx                    index register number 0:15    -1 means absent
   1228 
   1229    Again, I dare not explain it: just trace ALL the code!
   1230 
   1231    Summary of vip_op outputs.
   1232 
   1233   mode	reg	len	ndx
   1234   (Rn) => @Rn
   1235   {@}Rn			5+@	n	' '	optional
   1236   branch operand		0	-1	' '	-1
   1237   S^#foo			0	-1	's'	-1
   1238   -(Rn)			7	n	' '	optional
   1239   {@}(Rn)+		8+@	n	' '	optional
   1240   {@}#foo, no S^		8+@	PC	" i"	optional
   1241   {@}{q^}{(Rn)}		10+@+q	option	" bwl"	optional  */
   1242 
   1243 /* Dissect user-input 'optext' (which is something like "@B^foo@bar(AP)[FP]:")
   1244    using the vop in vopP. vopP's vop_access and vop_width. We fill _ndx, _reg,
   1245    _mode, _short, _warn, _error, _expr_begin, _expr_end and _nbytes.  */
   1246 
   1247 static void
   1248 vip_op (char *optext, struct vop *vopP)
   1249 {
   1250   /* Track operand text forward.  */
   1251   char *p;
   1252   /* Track operand text backward.  */
   1253   char *q;
   1254   /* 1 if leading '@' ('*') seen.  */
   1255   int at;
   1256   /* one of " bilsw" */
   1257   char len;
   1258   /* 1 if leading '#' ('$') seen.  */
   1259   int hash;
   1260   /* -1, 0 or +1.  */
   1261   int sign = 0;
   1262   /* 1 if () surround register.  */
   1263   int paren = 0;
   1264   /* Register number, -1:absent.  */
   1265   int reg = 0;
   1266   /* Index register number -1:absent.  */
   1267   int ndx = 0;
   1268   /* Report illegal operand, ""==OK.  */
   1269   /* " " is a FAKE error: means we won.  */
   1270   /* ANY err that begins with ' ' is a fake.  */
   1271   /* " " is converted to "" before return.  */
   1272   const char *err;
   1273   /* Warn about weird modes pf address.  */
   1274   const char *wrn;
   1275   /* Preserve q in case we backup.  */
   1276   char *oldq = NULL;
   1277   /* Build up 4-bit operand mode here.  */
   1278   /* Note: index mode is in ndx, this is.  */
   1279   /* The major mode of operand address.  */
   1280   int mode = 0;
   1281   /* Notice how we move wrong-arg-type bugs INSIDE this module: if we
   1282      get the types wrong below, we lose at compile time rather than at
   1283      lint or run time.  */
   1284   char access_mode;		/* vop_access.  */
   1285 
   1286   access_mode = vopP->vop_access;
   1287   /* None of our code bugs (yet), no user text errors, no warnings
   1288      even.  */
   1289   err = wrn = 0;
   1290 
   1291   p = optext;
   1292 
   1293   if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1294     p++;			/* skip over whitespace */
   1295 
   1296   if ((at = INDIRECTP (*p)) != 0)
   1297     {				/* 1 if *p=='@'(or '*' for Un*x) */
   1298       p++;			/* at is determined */
   1299       if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1300 	p++;			/* skip over whitespace */
   1301     }
   1302 
   1303   /* This code is subtle. It tries to detect all legal (letter)'^'
   1304      but it doesn't waste time explicitly testing for premature '\0' because
   1305      this case is rejected as a mismatch against either (letter) or '^'.  */
   1306   {
   1307     char c;
   1308 
   1309     c = *p;
   1310     c = TOLOWER (c);
   1311     if (DISPLENP (p[1]) && strchr ("bilws", len = c))
   1312       p += 2;			/* Skip (letter) '^'.  */
   1313     else			/* No (letter) '^' seen.  */
   1314       len = ' ';		/* Len is determined.  */
   1315   }
   1316 
   1317   if (*p == ' ')		/* Expect all whitespace reduced to ' '.  */
   1318     p++;
   1319 
   1320   if ((hash = IMMEDIATEP (*p)) != 0)	/* 1 if *p=='#' ('$' for Un*x) */
   1321     p++;			/* Hash is determined.  */
   1322 
   1323   /* p points to what may be the beginning of an expression.
   1324      We have peeled off the front all that is peelable.
   1325      We know at, len, hash.
   1326 
   1327      Lets point q at the end of the text and parse that (backwards).  */
   1328 
   1329   for (q = p; *q; q++)
   1330     ;
   1331   q--;				/* Now q points at last char of text.  */
   1332 
   1333   if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1334     q--;
   1335 
   1336   /* Reverse over whitespace, but don't.  */
   1337   /* Run back over *p.  */
   1338 
   1339   /* As a matter of policy here, we look for [Rn], although both Rn and S^#
   1340      forbid [Rn]. This is because it is easy, and because only a sick
   1341      cyborg would have [...] trailing an expression in a VAX-like assembler.
   1342      A meticulous parser would first check for Rn followed by '(' or '['
   1343      and not parse a trailing ']' if it found another. We just ban expressions
   1344      ending in ']'.  */
   1345   if (*q == ']')
   1346     {
   1347       while (q >= p && *q != '[')
   1348 	q--;
   1349       /* Either q<p or we got matching '['.  */
   1350       if (q < p)
   1351 	err = _("no '[' to match ']'");
   1352       else
   1353 	{
   1354 	  /* Confusers like "[]" will eventually lose with a bad register
   1355 	   * name error. So again we don't need to check for early '\0'.  */
   1356 	  if (q[3] == ']')
   1357 	    ndx = vax_reg_parse (q[1], q[2], 0, 0);
   1358 	  else if (q[4] == ']')
   1359 	    ndx = vax_reg_parse (q[1], q[2], q[3], 0);
   1360 	  else if (q[5] == ']')
   1361 	    ndx = vax_reg_parse (q[1], q[2], q[3], q[4]);
   1362 	  else
   1363 	    ndx = -1;
   1364 	  /* Since we saw a ']' we will demand a register name in the [].
   1365 	   * If luser hasn't given us one: be rude.  */
   1366 	  if (ndx < 0)
   1367 	    err = _("bad register in []");
   1368 	  else if (ndx == PC)
   1369 	    err = _("[PC] index banned");
   1370 	  else
   1371 	    /* Point q just before "[...]".  */
   1372 	    q--;
   1373 	}
   1374     }
   1375   else
   1376     /* No ']', so no iNDeX register.  */
   1377     ndx = -1;
   1378 
   1379   /* If err = "..." then we lost: run away.
   1380      Otherwise ndx == -1 if there was no "[...]".
   1381      Otherwise, ndx is index register number, and q points before "[...]".  */
   1382 
   1383   if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1384     q--;
   1385   /* Reverse over whitespace, but don't.  */
   1386   /* Run back over *p.  */
   1387   if (!err || !*err)
   1388     {
   1389       /* no ()+ or -() seen yet */
   1390       sign = 0;
   1391 
   1392       if (q > p + 3 && *q == '+' && q[-1] == ')')
   1393 	{
   1394 	  sign = 1;		/* we saw a ")+" */
   1395 	  q--;			/* q points to ')' */
   1396 	}
   1397 
   1398       if (*q == ')' && q > p + 2)
   1399 	{
   1400 	  paren = 1;		/* assume we have "(...)" */
   1401 	  while (q >= p && *q != '(')
   1402 	    q--;
   1403 	  /* either q<p or we got matching '(' */
   1404 	  if (q < p)
   1405 	    err = _("no '(' to match ')'");
   1406 	  else
   1407 	    {
   1408 	      /* Confusers like "()" will eventually lose with a bad register
   1409 	         name error. So again we don't need to check for early '\0'.  */
   1410 	      if (q[3] == ')')
   1411 		reg = vax_reg_parse (q[1], q[2], 0, 0);
   1412 	      else if (q[4] == ')')
   1413 		reg = vax_reg_parse (q[1], q[2], q[3], 0);
   1414 	      else if (q[5] == ')')
   1415 		reg = vax_reg_parse (q[1], q[2], q[3], q[4]);
   1416 	      else
   1417 		reg = -1;
   1418 	      /* Since we saw a ')' we will demand a register name in the ')'.
   1419 	         This is nasty: why can't our hypothetical assembler permit
   1420 	         parenthesised expressions? BECAUSE I AM LAZY! That is why.
   1421 	         Abuse luser if we didn't spy a register name.  */
   1422 	      if (reg < 0)
   1423 		{
   1424 		  /* JF allow parenthesized expressions.  I hope this works.  */
   1425 		  paren = 0;
   1426 		  while (*q != ')')
   1427 		    q++;
   1428 		  /* err = "unknown register in ()"; */
   1429 		}
   1430 	      else
   1431 		q--;		/* point just before '(' of "(...)" */
   1432 	      /* If err == "..." then we lost. Run away.
   1433 	         Otherwise if reg >= 0 then we saw (Rn).  */
   1434 	    }
   1435 	  /* If err == "..." then we lost.
   1436 	     Otherwise paren==1 and reg = register in "()".  */
   1437 	}
   1438       else
   1439 	paren = 0;
   1440       /* If err == "..." then we lost.
   1441          Otherwise, q points just before "(Rn)", if any.
   1442          If there was a "(...)" then paren==1, and reg is the register.  */
   1443 
   1444       /* We should only seek '-' of "-(...)" if:
   1445            we saw "(...)"                    paren == 1
   1446            we have no errors so far          ! *err
   1447            we did not see '+' of "(...)+"    sign < 1
   1448          We don't check len. We want a specific error message later if
   1449          user tries "x^...-(Rn)". This is a feature not a bug.  */
   1450       if (!err || !*err)
   1451 	{
   1452 	  if (paren && sign < 1)/* !sign is adequate test */
   1453 	    {
   1454 	      if (*q == '-')
   1455 		{
   1456 		  sign = -1;
   1457 		  q--;
   1458 		}
   1459 	    }
   1460 	  /* We have back-tracked over most
   1461 	     of the crud at the end of an operand.
   1462 	     Unless err, we know: sign, paren. If paren, we know reg.
   1463 	     The last case is of an expression "Rn".
   1464 	     This is worth hunting for if !err, !paren.
   1465 	     We wouldn't be here if err.
   1466 	     We remember to save q, in case we didn't want "Rn" anyway.  */
   1467 	  if (!paren)
   1468 	    {
   1469 	      if (*q == ' ' && q >= p)	/* Expect all whitespace reduced to ' '.  */
   1470 		q--;
   1471 	      /* Reverse over whitespace, but don't.  */
   1472 	      /* Run back over *p.  */
   1473 	      /* Room for Rn or Rnn (include prefix) exactly?  */
   1474 	      if (q > p && q < p + 4)
   1475 		reg = vax_reg_parse (p[0], p[1],
   1476 		  q < p + 2 ? 0 : p[2],
   1477 		  q < p + 3 ? 0 : p[3]);
   1478 	      else
   1479 		reg = -1;	/* Always comes here if no register at all.  */
   1480 	      /* Here with a definitive reg value.  */
   1481 	      if (reg >= 0)
   1482 		{
   1483 		  oldq = q;
   1484 		  q = p - 1;
   1485 		}
   1486 	    }
   1487 	}
   1488     }
   1489   /* have reg. -1:absent; else 0:15.  */
   1490 
   1491   /* We have:  err, at, len, hash, ndx, sign, paren, reg.
   1492      Also, any remaining expression is from *p through *q inclusive.
   1493      Should there be no expression, q==p-1. So expression length = q-p+1.
   1494      This completes the first part: parsing the operand text.  */
   1495 
   1496   /* We now want to boil the data down, checking consistency on the way.
   1498      We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
   1499      We will deliver a 4-bit reg, and a 4-bit mode.  */
   1500 
   1501   /* Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
   1502 
   1503      in:  at	?
   1504           len	?
   1505           hash	?
   1506           p:q	?
   1507           sign  ?
   1508           paren	?
   1509           reg   ?
   1510           ndx   ?
   1511 
   1512      out: mode  0
   1513           reg   -1
   1514           len	' '
   1515           p:q	whatever was input
   1516           ndx	-1
   1517           err	" "		 or error message, and other outputs trashed.  */
   1518   /* Branch operands have restricted forms.  */
   1519   if ((!err || !*err) && access_mode == 'b')
   1520     {
   1521       if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
   1522 	err = _("invalid branch operand");
   1523       else
   1524 	err = " ";
   1525     }
   1526 
   1527   /* Since nobody seems to use it: comment this 'feature'(?) out for now.  */
   1528 #ifdef NEVER
   1529   /* Case of stand-alone operand. e.g. ".long foo"
   1530 
   1531      in:  at	?
   1532           len	?
   1533           hash	?
   1534           p:q	?
   1535           sign  ?
   1536           paren	?
   1537           reg   ?
   1538           ndx   ?
   1539 
   1540      out: mode  0
   1541           reg   -1
   1542           len	' '
   1543           p:q	whatever was input
   1544           ndx	-1
   1545           err	" "		 or error message, and other outputs trashed.  */
   1546   if ((!err || !*err) && access_mode == ' ')
   1547     {
   1548       if (at)
   1549 	err = _("address prohibits @");
   1550       else if (hash)
   1551 	err = _("address prohibits #");
   1552       else if (sign)
   1553 	{
   1554 	  if (sign < 0)
   1555 	    err = _("address prohibits -()");
   1556 	  else
   1557 	    err = _("address prohibits ()+");
   1558 	}
   1559       else if (paren)
   1560 	err = _("address prohibits ()");
   1561       else if (ndx >= 0)
   1562 	err = _("address prohibits []");
   1563       else if (reg >= 0)
   1564 	err = _("address prohibits register");
   1565       else if (len != ' ')
   1566 	err = _("address prohibits displacement length specifier");
   1567       else
   1568 	{
   1569 	  err = " ";	/* succeed */
   1570 	  mode = 0;
   1571 	}
   1572     }
   1573 #endif
   1574 
   1575   /* Case of S^#.
   1576 
   1577      in:  at       0
   1578           len      's'               definition
   1579           hash     1              demand
   1580           p:q                        demand not empty
   1581           sign     0                 by paren==0
   1582           paren    0             by "()" scan logic because "S^" seen
   1583           reg      -1                or nn by mistake
   1584           ndx      -1
   1585 
   1586      out: mode     0
   1587           reg      -1
   1588           len      's'
   1589           exp
   1590           ndx      -1  */
   1591   if ((!err || !*err) && len == 's')
   1592     {
   1593       if (!hash || paren || at || ndx >= 0)
   1594 	err = _("invalid operand of S^#");
   1595       else
   1596 	{
   1597 	  if (reg >= 0)
   1598 	    {
   1599 	      /* Darn! we saw S^#Rnn ! put the Rnn back in
   1600 	         expression. KLUDGE! Use oldq so we don't
   1601 	         need to know exact length of reg name.  */
   1602 	      q = oldq;
   1603 	      reg = 0;
   1604 	    }
   1605 	  /* We have all the expression we will ever get.  */
   1606 	  if (p > q)
   1607 	    err = _("S^# needs expression");
   1608 	  else if (access_mode == 'r')
   1609 	    {
   1610 	      err = " ";	/* WIN! */
   1611 	      mode = 0;
   1612 	    }
   1613 	  else
   1614 	    err = _("S^# may only read-access");
   1615 	}
   1616     }
   1617 
   1618   /* Case of -(Rn), which is weird case.
   1619 
   1620      in:  at       0
   1621           len      '
   1622           hash     0
   1623           p:q      q<p
   1624           sign     -1                by definition
   1625           paren    1              by definition
   1626           reg      present           by definition
   1627           ndx      optional
   1628 
   1629      out: mode     7
   1630           reg      present
   1631           len      ' '
   1632           exp      ""                enforce empty expression
   1633           ndx      optional          warn if same as reg.  */
   1634   if ((!err || !*err) && sign < 0)
   1635     {
   1636       if (len != ' ' || hash || at || p <= q)
   1637 	err = _("invalid operand of -()");
   1638       else
   1639 	{
   1640 	  err = " ";		/* win */
   1641 	  mode = 7;
   1642 	  if (reg == PC)
   1643 	    wrn = _("-(PC) unpredictable");
   1644 	  else if (reg == ndx)
   1645 	    wrn = _("[]index same as -()register: unpredictable");
   1646 	}
   1647     }
   1648 
   1649   /* We convert "(Rn)" to "@Rn" for our convenience.
   1650      (I hope this is convenient: has someone got a better way to parse this?)
   1651      A side-effect of this is that "@Rn" is a valid operand.  */
   1652   if (paren && !sign && !hash && !at && len == ' ' && p > q)
   1653     {
   1654       at = 1;
   1655       paren = 0;
   1656     }
   1657 
   1658   /* Case of (Rn)+, which is slightly different.
   1659 
   1660      in:  at
   1661           len      ' '
   1662           hash     0
   1663           p:q      q<p
   1664           sign     +1                by definition
   1665           paren    1              by definition
   1666           reg      present           by definition
   1667           ndx      optional
   1668 
   1669      out: mode     8+@
   1670           reg      present
   1671           len      ' '
   1672           exp      ""                enforce empty expression
   1673           ndx      optional          warn if same as reg.  */
   1674   if ((!err || !*err) && sign > 0)
   1675     {
   1676       if (len != ' ' || hash || p <= q)
   1677 	err = _("invalid operand of ()+");
   1678       else
   1679 	{
   1680 	  err = " ";		/* win */
   1681 	  mode = 8 + (at ? 1 : 0);
   1682 	  if (reg == PC)
   1683 	    wrn = _("(PC)+ unpredictable");
   1684 	  else if (reg == ndx)
   1685 	    wrn = _("[]index same as ()+register: unpredictable");
   1686 	}
   1687     }
   1688 
   1689   /* Case of #, without S^.
   1690 
   1691      in:  at
   1692           len      ' ' or 'i'
   1693           hash     1              by definition
   1694           p:q
   1695           sign     0
   1696           paren    0
   1697           reg      absent
   1698           ndx      optional
   1699 
   1700      out: mode     8+@
   1701           reg      PC
   1702           len      ' ' or 'i'
   1703           exp
   1704           ndx      optional.  */
   1705   if ((!err || !*err) && hash)
   1706     {
   1707       if (len != 'i' && len != ' ')
   1708 	err = _("# conflicts length");
   1709       else if (paren)
   1710 	err = _("# bars register");
   1711       else
   1712 	{
   1713 	  if (reg >= 0)
   1714 	    {
   1715 	      /* Darn! we saw #Rnn! Put the Rnn back into the expression.
   1716 	         By using oldq, we don't need to know how long Rnn was.
   1717 	         KLUDGE!  */
   1718 	      q = oldq;
   1719 	      reg = -1;		/* No register any more.  */
   1720 	    }
   1721 	  err = " ";		/* Win.  */
   1722 
   1723 	  /* JF a bugfix, I think!  */
   1724 	  if (at && access_mode == 'a')
   1725 	    vopP->vop_nbytes = 4;
   1726 
   1727 	  mode = (at ? 9 : 8);
   1728 	  reg = PC;
   1729 	  if ((access_mode == 'm' || access_mode == 'w') && !at)
   1730 	    wrn = _("writing or modifying # is unpredictable");
   1731 	}
   1732     }
   1733   /* If !*err, then       sign == 0
   1734                           hash == 0 */
   1735 
   1736   /* Case of Rn. We separate this one because it has a few special
   1737      errors the remaining modes lack.
   1738 
   1739      in:  at       optional
   1740           len      ' '
   1741           hash     0             by program logic
   1742           p:q      empty
   1743           sign     0                 by program logic
   1744           paren    0             by definition
   1745           reg      present           by definition
   1746           ndx      optional
   1747 
   1748      out: mode     5+@
   1749           reg      present
   1750           len      ' '               enforce no length
   1751           exp      ""                enforce empty expression
   1752           ndx      optional          warn if same as reg.  */
   1753   if ((!err || !*err) && !paren && reg >= 0)
   1754     {
   1755       if (len != ' ')
   1756 	err = _("length not needed");
   1757       else if (at)
   1758 	{
   1759 	  err = " ";		/* win */
   1760 	  mode = 6;		/* @Rn */
   1761 	}
   1762       else if (ndx >= 0)
   1763 	err = _("can't []index a register, because it has no address");
   1764       else if (access_mode == 'a')
   1765 	err = _("a register has no address");
   1766       else
   1767 	{
   1768 	  /* Idea here is to detect from length of datum
   1769 	     and from register number if we will touch PC.
   1770 	     Warn if we do.
   1771 	     vop_nbytes is number of bytes in operand.
   1772 	     Compute highest byte affected, compare to PC0.  */
   1773 	  if ((vopP->vop_nbytes + reg * 4) > 60)
   1774 	    wrn = _("PC part of operand unpredictable");
   1775 	  err = " ";		/* win */
   1776 	  mode = 5;		/* Rn */
   1777 	}
   1778     }
   1779   /* If !*err,        sign  == 0
   1780                       hash  == 0
   1781                       paren == 1  OR reg==-1  */
   1782 
   1783   /* Rest of cases fit into one bunch.
   1784 
   1785      in:  at       optional
   1786           len      ' ' or 'b' or 'w' or 'l'
   1787           hash     0             by program logic
   1788           p:q      expected          (empty is not an error)
   1789           sign     0                 by program logic
   1790           paren    optional
   1791           reg      optional
   1792           ndx      optional
   1793 
   1794      out: mode     10 + @ + len
   1795           reg      optional
   1796           len      ' ' or 'b' or 'w' or 'l'
   1797           exp                        maybe empty
   1798           ndx      optional          warn if same as reg.  */
   1799   if (!err || !*err)
   1800     {
   1801       err = " ";		/* win (always) */
   1802       mode = 10 + (at ? 1 : 0);
   1803       switch (len)
   1804 	{
   1805 	case 'l':
   1806 	  mode += 2;
   1807 	  /* Fall through.  */
   1808 	case 'w':
   1809 	  mode += 2;
   1810 	  /* Fall through.  */
   1811 	case ' ':	/* Assumed B^ until our caller changes it.  */
   1812 	case 'b':
   1813 	  break;
   1814 	}
   1815     }
   1816 
   1817   /* here with completely specified     mode
   1818     					len
   1819     					reg
   1820     					expression   p,q
   1821     					ndx.  */
   1822 
   1823   if (*err == ' ')
   1824     err = 0;			/* " " is no longer an error.  */
   1825 
   1826   vopP->vop_mode = mode;
   1827   vopP->vop_reg = reg;
   1828   vopP->vop_short = len;
   1829   vopP->vop_expr_begin = p;
   1830   vopP->vop_expr_end = q;
   1831   vopP->vop_ndx = ndx;
   1832   vopP->vop_error = err;
   1833   vopP->vop_warn = wrn;
   1834 }
   1835 
   1836 /* This converts a string into a vax instruction.
   1837    The string must be a bare single instruction in dec-vax (with BSD4 frobs)
   1838    format.
   1839    It provides some error messages: at most one fatal error message (which
   1840    stops the scan) and at most one warning message for each operand.
   1841    The vax instruction is returned in exploded form, since we have no
   1842    knowledge of how you parse (or evaluate) your expressions.
   1843    We do however strip off and decode addressing modes and operation
   1844    mnemonic.
   1845 
   1846    The exploded instruction is returned to a struct vit of your choice.
   1847    #include "vax-inst.h" to know what a struct vit is.
   1848 
   1849    This function's value is a string. If it is not "" then an internal
   1850    logic error was found: read this code to assign meaning to the string.
   1851    No argument string should generate such an error string:
   1852    it means a bug in our code, not in the user's text.
   1853 
   1854    You MUST have called vip_begin() once before using this function.  */
   1855 
   1856 static void
   1857 vip (struct vit *vitP,		/* We build an exploded instruction here.  */
   1858      char *instring)		/* Text of a vax instruction: we modify.  */
   1859 {
   1860   /* How to bit-encode this opcode.  */
   1861   struct vot_wot *vwP;
   1862   /* 1/skip whitespace.2/scan vot_how */
   1863   char *p;
   1864   char *q;
   1865   /* counts number of operands seen */
   1866   unsigned char count;
   1867   /* scan operands in struct vit */
   1868   struct vop *operandp;
   1869   /* error over all operands */
   1870   const char *alloperr;
   1871   /* Remember char, (we clobber it with '\0' temporarily).  */
   1872   char c;
   1873   /* Op-code of this instruction.  */
   1874   vax_opcodeT oc;
   1875 
   1876   if (*instring == ' ')
   1877     ++instring;
   1878 
   1879   /* MUST end in end-of-string or exactly 1 space.  */
   1880   for (p = instring; *p && *p != ' '; p++)
   1881     ;
   1882 
   1883   /* Scanned up to end of operation-code.  */
   1884   /* Operation-code is ended with whitespace.  */
   1885   if (p - instring == 0)
   1886     {
   1887       vitP->vit_error = _("No operator");
   1888       count = 0;
   1889       memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
   1890     }
   1891   else
   1892     {
   1893       c = *p;
   1894       *p = '\0';
   1895       /* Here with instring pointing to what better be an op-name, and p
   1896          pointing to character just past that.
   1897          We trust instring points to an op-name, with no whitespace.  */
   1898       vwP = (struct vot_wot *) str_hash_find (op_hash, instring);
   1899       /* Restore char after op-code.  */
   1900       *p = c;
   1901       if (vwP == 0)
   1902 	{
   1903 	  vitP->vit_error = _("Unknown operator");
   1904 	  count = 0;
   1905 	  memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
   1906 	}
   1907       else
   1908 	{
   1909 	  /* We found a match! So let's pick up as many operands as the
   1910 	     instruction wants, and even gripe if there are too many.
   1911 	     We expect comma to separate each operand.
   1912 	     We let instring track the text, while p tracks a part of the
   1913 	     struct vot.  */
   1914 	  const char *howp;
   1915 	  /* The lines below know about 2-byte opcodes starting FD,FE or FF.
   1916 	     They also understand synthetic opcodes. Note:
   1917 	     we return 32 bits of opcode, including bucky bits, BUT
   1918 	     an opcode length is either 8 or 16 bits for vit_opcode_nbytes.  */
   1919 	  oc = vwP->vot_code;	/* The op-code.  */
   1920 	  vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
   1921 	  md_number_to_chars (vitP->vit_opcode, oc, 4);
   1922 	  count = 0;		/* No operands seen yet.  */
   1923 	  instring = p;		/* Point just past operation code.  */
   1924 	  alloperr = "";
   1925 	  for (howp = vwP->vot_how, operandp = vitP->vit_operand;
   1926 	       !(alloperr && *alloperr) && *howp;
   1927 	       operandp++, howp += 2)
   1928 	    {
   1929 	      /* Here to parse one operand. Leave instring pointing just
   1930 	         past any one ',' that marks the end of this operand.  */
   1931 	      if (!howp[1])
   1932 		as_fatal (_("odd number of bytes in operand description"));
   1933 	      else if (*instring)
   1934 		{
   1935 		  for (q = instring; (c = *q) && c != ','; q++)
   1936 		    ;
   1937 		  /* Q points to ',' or '\0' that ends argument. C is that
   1938 		     character.  */
   1939 		  *q = 0;
   1940 		  operandp->vop_width = howp[1];
   1941 		  operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
   1942 		  operandp->vop_access = howp[0];
   1943 		  vip_op (instring, operandp);
   1944 		  *q = c;	/* Restore input text.  */
   1945 		  if (operandp->vop_error)
   1946 		    alloperr = _("Bad operand");
   1947 		  instring = q + (c ? 1 : 0);	/* Next operand (if any).  */
   1948 		  count++;	/*  Won another argument, may have an operr.  */
   1949 		}
   1950 	      else
   1951 		alloperr = _("Not enough operands");
   1952 	    }
   1953 	  if (!*alloperr)
   1954 	    {
   1955 	      if (*instring == ' ')
   1956 		instring++;
   1957 	      if (*instring)
   1958 		alloperr = _("Too many operands");
   1959 	    }
   1960 	  vitP->vit_error = alloperr;
   1961 	}
   1962     }
   1963   vitP->vit_operands = count;
   1964 }
   1965 
   1966 #ifdef test
   1968 
   1969 /* Test program for above.  */
   1970 
   1971 struct vit myvit;		/* Build an exploded vax instruction here.  */
   1972 char answer[100];		/* Human types a line of vax assembler here.  */
   1973 char *mybug;			/* "" or an internal logic diagnostic.  */
   1974 int mycount;			/* Number of operands.  */
   1975 struct vop *myvop;		/* Scan operands from myvit.  */
   1976 int mysynth;			/* 1 means want synthetic opcodes.  */
   1977 char my_immediate[200];
   1978 char my_indirect[200];
   1979 char my_displen[200];
   1980 
   1981 int
   1982 main (void)
   1983 {
   1984   char *p;
   1985 
   1986   printf ("0 means no synthetic instructions.   ");
   1987   printf ("Value for vip_begin?  ");
   1988   gets (answer);
   1989   sscanf (answer, "%d", &mysynth);
   1990   printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
   1991   printf ("enter immediate symbols eg enter #   ");
   1992   gets (my_immediate);
   1993   printf ("enter indirect symbols  eg enter @   ");
   1994   gets (my_indirect);
   1995   printf ("enter displen symbols   eg enter ^   ");
   1996   gets (my_displen);
   1997 
   1998   vip_begin (mysynth, my_immediate, my_indirect, my_displen)
   1999 
   2000   printf ("An empty input line will quit you from the vax instruction parser\n");
   2001   for (;;)
   2002     {
   2003       printf ("vax instruction: ");
   2004       fflush (stdout);
   2005       gets (answer);
   2006       if (!*answer)
   2007 	break;		/* Out of for each input text loop.  */
   2008 
   2009       vip (& myvit, answer);
   2010       if (*myvit.vit_error)
   2011 	printf ("ERR:\"%s\"\n", myvit.vit_error);
   2012 
   2013       printf ("opcode=");
   2014       for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
   2015 	   mycount;
   2016 	   mycount--, p++)
   2017 	printf ("%02x ", *p & 0xFF);
   2018 
   2019       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
   2020       for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
   2021 	{
   2022 	  printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
   2023 		  myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
   2024 		  myvop->vop_short, myvop->vop_access, myvop->vop_width,
   2025 		  myvop->vop_nbytes);
   2026 	  for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
   2027 	    putchar (*p);
   2028 
   2029 	  printf ("\"\n");
   2030 	  if (myvop->vop_error)
   2031 	    printf ("  err:\"%s\"\n", myvop->vop_error);
   2032 
   2033 	  if (myvop->vop_warn)
   2034 	    printf ("  wrn:\"%s\"\n", myvop->vop_warn);
   2035 	}
   2036     }
   2037   vip_end ();
   2038   exit (EXIT_SUCCESS);
   2039 }
   2040 
   2041 #endif
   2042 
   2043 #ifdef TEST			/* #Define to use this testbed.  */
   2045 
   2046 /* Follows a test program for this function.
   2047    We declare arrays non-local in case some of our tiny-minded machines
   2048    default to small stacks. Also, helps with some debuggers.  */
   2049 
   2050 char answer[100];		/* Human types into here.  */
   2051 char *p;			/*  */
   2052 char *myerr;
   2053 char *mywrn;
   2054 char *mybug;
   2055 char myaccess;
   2056 char mywidth;
   2057 char mymode;
   2058 char myreg;
   2059 char mylen;
   2060 char *myleft;
   2061 char *myright;
   2062 char myndx;
   2063 int my_operand_length;
   2064 char my_immediate[200];
   2065 char my_indirect[200];
   2066 char my_displen[200];
   2067 
   2068 int
   2069 main (void)
   2070 {
   2071   printf ("enter immediate symbols eg enter #   ");
   2072   gets (my_immediate);
   2073   printf ("enter indirect symbols  eg enter @   ");
   2074   gets (my_indirect);
   2075   printf ("enter displen symbols   eg enter ^   ");
   2076   gets (my_displen);
   2077   vip_op_defaults (my_immediate, my_indirect, my_displen);
   2078 
   2079   for (;;)
   2080     {
   2081       printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
   2082       fflush (stdout);
   2083       gets (answer);
   2084       if (!answer[0])
   2085 	exit (EXIT_SUCCESS);
   2086       myaccess = answer[0];
   2087       mywidth = answer[1];
   2088       switch (mywidth)
   2089 	{
   2090 	case 'b':
   2091 	  my_operand_length = 1;
   2092 	  break;
   2093 	case 'd':
   2094 	  my_operand_length = 8;
   2095 	  break;
   2096 	case 'f':
   2097 	  my_operand_length = 4;
   2098 	  break;
   2099 	case 'g':
   2100 	  my_operand_length = 16;
   2101 	  break;
   2102 	case 'h':
   2103 	  my_operand_length = 32;
   2104 	  break;
   2105 	case 'l':
   2106 	  my_operand_length = 4;
   2107 	  break;
   2108 	case 'o':
   2109 	  my_operand_length = 16;
   2110 	  break;
   2111 	case 'q':
   2112 	  my_operand_length = 8;
   2113 	  break;
   2114 	case 'w':
   2115 	  my_operand_length = 2;
   2116 	  break;
   2117 	case '!':
   2118 	case '?':
   2119 	case '-':
   2120 	  my_operand_length = 0;
   2121 	  break;
   2122 
   2123 	default:
   2124 	  my_operand_length = 2;
   2125 	  printf ("I don't understand access width %c\n", mywidth);
   2126 	  break;
   2127 	}
   2128       printf ("VAX assembler instruction operand: ");
   2129       fflush (stdout);
   2130       gets (answer);
   2131       mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
   2132 		      &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
   2133 		      &myerr, &mywrn);
   2134       if (*myerr)
   2135 	{
   2136 	  printf ("error: \"%s\"\n", myerr);
   2137 	  if (*mybug)
   2138 	    printf (" bug: \"%s\"\n", mybug);
   2139 	}
   2140       else
   2141 	{
   2142 	  if (*mywrn)
   2143 	    printf ("warning: \"%s\"\n", mywrn);
   2144 	  mumble ("mode", mymode);
   2145 	  mumble ("register", myreg);
   2146 	  mumble ("index", myndx);
   2147 	  printf ("width:'%c'  ", mylen);
   2148 	  printf ("expression: \"");
   2149 	  while (myleft <= myright)
   2150 	    putchar (*myleft++);
   2151 	  printf ("\"\n");
   2152 	}
   2153     }
   2154 }
   2155 
   2156 void
   2157 mumble (char *text, int value)
   2158 {
   2159   printf ("%s:", text);
   2160   if (value >= 0)
   2161     printf ("%xx", value);
   2162   else
   2163     printf ("ABSENT");
   2164   printf ("  ");
   2165 }
   2166 
   2167 #endif
   2168 
   2169 int md_short_jump_size = 3;
   2170 int md_long_jump_size = 6;
   2171 
   2172 void
   2173 md_create_short_jump (char *ptr,
   2174 		      addressT from_addr,
   2175 		      addressT to_addr ATTRIBUTE_UNUSED,
   2176 		      fragS *frag ATTRIBUTE_UNUSED,
   2177 		      symbolS *to_symbol ATTRIBUTE_UNUSED)
   2178 {
   2179   valueT offset;
   2180 
   2181   /* This former calculation was off by two:
   2182       offset = to_addr - (from_addr + 1);
   2183      We need to account for the one byte instruction and also its
   2184      two byte operand.  */
   2185   offset = to_addr - (from_addr + 1 + 2);
   2186   *ptr++ = VAX_BRW;		/* Branch with word (16 bit) offset.  */
   2187   md_number_to_chars (ptr, offset, 2);
   2188 }
   2189 
   2190 void
   2191 md_create_long_jump (char *ptr,
   2192 		     addressT from_addr,
   2193 		     addressT to_addr,
   2194 		     fragS *frag ATTRIBUTE_UNUSED,
   2195 		     symbolS *to_symbol ATTRIBUTE_UNUSED)
   2196 {
   2197   valueT offset;
   2198 
   2199   /* Account for 1 byte instruction, 1 byte of address specifier and
   2200      4 bytes of offset from PC.  */
   2201   offset = to_addr - (from_addr + 1 + 1 + 4);
   2202   *ptr++ = VAX_JMP;
   2203   *ptr++ = VAX_PC_RELATIVE_MODE;
   2204   md_number_to_chars (ptr, offset, 4);
   2205 }
   2206 
   2207 #ifdef OBJ_VMS
   2209 const char *md_shortopts = "d:STt:V+1h:Hv::";
   2210 #elif defined(OBJ_ELF)
   2211 const char *md_shortopts = "d:STt:VkKQ:";
   2212 #else
   2213 const char *md_shortopts = "d:STt:V";
   2214 #endif
   2215 struct option md_longopts[] =
   2216 {
   2217 #ifdef OBJ_ELF
   2218 #define OPTION_PIC (OPTION_MD_BASE)
   2219   { "pic", no_argument, NULL, OPTION_PIC },
   2220 #endif
   2221   { NULL, no_argument, NULL, 0 }
   2222 };
   2223 size_t md_longopts_size = sizeof (md_longopts);
   2224 
   2225 int
   2226 md_parse_option (int c, const char *arg)
   2227 {
   2228   switch (c)
   2229     {
   2230     case 'S':
   2231       as_warn (_("SYMBOL TABLE not implemented"));
   2232       break;
   2233 
   2234     case 'T':
   2235       as_warn (_("TOKEN TRACE not implemented"));
   2236       break;
   2237 
   2238     case 'd':
   2239       as_warn (_("Displacement length %s ignored!"), arg);
   2240       break;
   2241 
   2242     case 't':
   2243       as_warn (_("I don't need or use temp. file \"%s\"."), arg);
   2244       break;
   2245 
   2246     case 'V':
   2247       as_warn (_("I don't use an interpass file! -V ignored"));
   2248       break;
   2249 
   2250 #ifdef OBJ_VMS
   2251     case '+':			/* For g++.  Hash any name > 31 chars long.  */
   2252       flag_hash_long_names = 1;
   2253       break;
   2254 
   2255     case '1':			/* For backward compatibility.  */
   2256       flag_one = 1;
   2257       break;
   2258 
   2259     case 'H':			/* Show new symbol after hash truncation.  */
   2260       flag_show_after_trunc = 1;
   2261       break;
   2262 
   2263     case 'h':			/* No hashing of mixed-case names.  */
   2264       {
   2265 	extern char vms_name_mapping;
   2266 	vms_name_mapping = atoi (arg);
   2267 	flag_no_hash_mixed_case = 1;
   2268       }
   2269       break;
   2270 
   2271     case 'v':
   2272       {
   2273 	extern char *compiler_version_string;
   2274 
   2275 	if (!arg || !*arg || access (arg, 0) == 0)
   2276 	  return 0;		/* Have caller show the assembler version.  */
   2277 	compiler_version_string = arg;
   2278       }
   2279       break;
   2280 #endif
   2281 
   2282 #ifdef OBJ_ELF
   2283     case OPTION_PIC:
   2284     case 'k':
   2285       flag_want_pic = 1;
   2286       break;			/* -pic, Position Independent Code.  */
   2287 
   2288      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment
   2289 	section should be emitted or not.  FIXME: Not implemented.  */
   2290     case 'Q':
   2291       break;
   2292 #endif
   2293 
   2294     default:
   2295       return 0;
   2296     }
   2297 
   2298   return 1;
   2299 }
   2300 
   2301 void
   2302 md_show_usage (FILE *stream)
   2303 {
   2304   fprintf (stream, _("\
   2305 VAX options:\n\
   2306 -d LENGTH		ignored\n\
   2307 -J			ignored\n\
   2308 -S			ignored\n\
   2309 -t FILE			ignored\n\
   2310 -T			ignored\n\
   2311 -V			ignored\n"));
   2312 #ifdef OBJ_VMS
   2313   fprintf (stream, _("\
   2314 VMS options:\n\
   2315 -+			hash encode names longer than 31 characters\n\
   2316 -1			`const' handling compatible with gcc 1.x\n\
   2317 -H			show new symbol after hash truncation\n\
   2318 -h NUM			don't hash mixed-case names, and adjust case:\n\
   2319 			0 = upper, 2 = lower, 3 = preserve case\n\
   2320 -v\"VERSION\"		code being assembled was produced by compiler \"VERSION\"\n"));
   2321 #endif
   2322 }
   2323 
   2324 /* We have no need to default values of symbols.  */
   2326 
   2327 symbolS *
   2328 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
   2329 {
   2330   return NULL;
   2331 }
   2332 
   2333 /* Round up a section size to the appropriate boundary.  */
   2334 valueT
   2335 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
   2336 {
   2337   /* Byte alignment is fine */
   2338   return size;
   2339 }
   2340 
   2341 /* Exactly what point is a PC-relative offset relative TO?
   2342    On the vax, they're relative to the address of the offset, plus
   2343    its size. */
   2344 long
   2345 md_pcrel_from (fixS *fixP)
   2346 {
   2347   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
   2348 }
   2349 
   2350 arelent *
   2351 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
   2352 {
   2353   arelent *reloc;
   2354   bfd_reloc_code_real_type code;
   2355 
   2356   if (fixp->fx_tcbit)
   2357     abort ();
   2358 
   2359   if (fixp->fx_r_type != NO_RELOC)
   2360     {
   2361       code = fixp->fx_r_type;
   2362 
   2363       if (fixp->fx_pcrel)
   2364 	{
   2365 	  switch (code)
   2366 	    {
   2367 	    case BFD_RELOC_8_PCREL:
   2368 	    case BFD_RELOC_16_PCREL:
   2369 	    case BFD_RELOC_32_PCREL:
   2370 #ifdef OBJ_ELF
   2371 	    case BFD_RELOC_8_GOT_PCREL:
   2372 	    case BFD_RELOC_16_GOT_PCREL:
   2373 	    case BFD_RELOC_32_GOT_PCREL:
   2374 	    case BFD_RELOC_8_PLT_PCREL:
   2375 	    case BFD_RELOC_16_PLT_PCREL:
   2376 	    case BFD_RELOC_32_PLT_PCREL:
   2377 #endif
   2378 	      break;
   2379 	    default:
   2380 	      as_bad_where (fixp->fx_file, fixp->fx_line,
   2381 			    _("Cannot make %s relocation PC relative"),
   2382 			    bfd_get_reloc_code_name (code));
   2383 	    }
   2384 	}
   2385     }
   2386   else
   2387     {
   2388 #define F(SZ,PCREL)		(((SZ) << 1) + (PCREL))
   2389       switch (F (fixp->fx_size, fixp->fx_pcrel))
   2390 	{
   2391 #define MAP(SZ,PCREL,TYPE)	case F(SZ,PCREL): code = (TYPE); break
   2392 	  MAP (1, 0, BFD_RELOC_8);
   2393 	  MAP (2, 0, BFD_RELOC_16);
   2394 	  MAP (4, 0, BFD_RELOC_32);
   2395 	  MAP (1, 1, BFD_RELOC_8_PCREL);
   2396 	  MAP (2, 1, BFD_RELOC_16_PCREL);
   2397 	  MAP (4, 1, BFD_RELOC_32_PCREL);
   2398 	default:
   2399 	  abort ();
   2400 	}
   2401     }
   2402 #undef F
   2403 #undef MAP
   2404 
   2405   reloc = XNEW (arelent);
   2406   reloc->sym_ptr_ptr = XNEW (asymbol *);
   2407   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   2408   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   2409 #ifndef OBJ_ELF
   2410   if (fixp->fx_pcrel)
   2411     reloc->addend = fixp->fx_addnumber;
   2412   else
   2413     reloc->addend = 0;
   2414 #else
   2415   reloc->addend = fixp->fx_offset;
   2416 #endif
   2417 
   2418   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
   2419   gas_assert (reloc->howto != 0);
   2420 
   2421   return reloc;
   2422 }
   2423 
   2424 /* vax:md_assemble() emit frags for 1 instruction given in textual form.  */
   2425 void
   2426 md_assemble (char *instruction_string)
   2427 {
   2428   /* Non-zero if operand expression's segment is not known yet.  */
   2429   int is_undefined;
   2430   /* Non-zero if operand expression's segment is absolute.  */
   2431   int is_absolute;
   2432   int length_code;
   2433   char *p;
   2434   /* An operand. Scans all operands.  */
   2435   struct vop *operandP;
   2436   char *save_input_line_pointer;
   2437 			/* What used to live after an expression.  */
   2438   char c_save;
   2439   /* 1: instruction_string bad for all passes.  */
   2440   int goofed;
   2441   /* Points to slot just after last operand.  */
   2442   struct vop *end_operandP;
   2443   /* Points to expression values for this operand.  */
   2444   expressionS *expP;
   2445   segT *segP;
   2446 
   2447   /* These refer to an instruction operand expression.  */
   2448   /* Target segment of the address.	 */
   2449   segT to_seg;
   2450   valueT this_add_number;
   2451   /* Positive (minuend) symbol.  */
   2452   symbolS *this_add_symbol;
   2453   /* As a number.  */
   2454   long opcode_as_number;
   2455   /* Least significant byte 1st.  */
   2456   char *opcode_as_chars;
   2457   /* As an array of characters.  */
   2458   /* Least significant byte 1st */
   2459   char *opcode_low_byteP;
   2460   /* length (bytes) meant by vop_short.  */
   2461   int length;
   2462   /* 0, or 1 if '@' is in addressing mode.  */
   2463   int at;
   2464   /* From vop_nbytes: vax_operand_width (in bytes) */
   2465   int nbytes;
   2466   FLONUM_TYPE *floatP;
   2467   LITTLENUM_TYPE literal_float[8];
   2468   /* Big enough for any floating point literal.  */
   2469 
   2470   vip (&v, instruction_string);
   2471 
   2472   /* Now we try to find as many as_warn()s as we can. If we do any as_warn()s
   2473      then goofed=1. Notice that we don't make any frags yet.
   2474      Should goofed be 1, then this instruction will wedge in any pass,
   2475      and we can safely flush it, without causing interpass symbol phase
   2476      errors. That is, without changing label values in different passes.  */
   2477   if ((goofed = (*v.vit_error)) != 0)
   2478     {
   2479       as_fatal (_("Ignoring statement due to \"%s\""), v.vit_error);
   2480     }
   2481   /* We need to use expression() and friends, which require us to diddle
   2482      input_line_pointer. So we save it and restore it later.  */
   2483   save_input_line_pointer = input_line_pointer;
   2484   for (operandP = v.vit_operand,
   2485        expP = exp_of_operand,
   2486        segP = seg_of_operand,
   2487        floatP = float_operand,
   2488        end_operandP = v.vit_operand + v.vit_operands;
   2489 
   2490        operandP < end_operandP;
   2491 
   2492        operandP++, expP++, segP++, floatP++)
   2493     {
   2494       if (operandP->vop_error)
   2495 	{
   2496 	  as_fatal (_("Aborting because statement has \"%s\""), operandP->vop_error);
   2497 	  goofed = 1;
   2498 	}
   2499       else
   2500 	{
   2501 	  /* Statement has no syntax goofs: let's sniff the expression.  */
   2502 	  int can_be_short = 0;	/* 1 if a bignum can be reduced to a short literal.  */
   2503 
   2504 	  input_line_pointer = operandP->vop_expr_begin;
   2505 	  c_save = operandP->vop_expr_end[1];
   2506 	  operandP->vop_expr_end[1] = '\0';
   2507 	  /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1.  */
   2508 	  *segP = expression (expP);
   2509 	  switch (expP->X_op)
   2510 	    {
   2511 	    case O_absent:
   2512 	      /* for BSD4.2 compatibility, missing expression is absolute 0 */
   2513 	      expP->X_op = O_constant;
   2514 	      expP->X_add_number = 0;
   2515 	      /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
   2516 		 X_add_symbol to any particular value.  But, we will program
   2517 		 defensively. Since this situation occurs rarely so it costs
   2518 		 us little to do, and stops Dean worrying about the origin of
   2519 		 random bits in expressionS's.  */
   2520 	      expP->X_add_symbol = NULL;
   2521 	      expP->X_op_symbol = NULL;
   2522 	      break;
   2523 
   2524 	    case O_symbol:
   2525 	    case O_constant:
   2526 	      break;
   2527 
   2528 	    default:
   2529 	      /* Major bug. We can't handle the case of a
   2530 	         SEG_OP expression in a VIT_OPCODE_SYNTHETIC
   2531 	         variable-length instruction.
   2532 	         We don't have a frag type that is smart enough to
   2533 	         relax a SEG_OP, and so we just force all
   2534 	         SEG_OPs to behave like SEG_PASS1s.
   2535 	         Clearly, if there is a demand we can invent a new or
   2536 	         modified frag type and then coding up a frag for this
   2537 	         case will be easy. SEG_OP was invented for the
   2538 	         .words after a CASE opcode, and was never intended for
   2539 	         instruction operands.  */
   2540 	      need_pass_2 = 1;
   2541 	      as_fatal (_("Can't relocate expression"));
   2542 	      break;
   2543 
   2544 	    case O_big:
   2545 	      /* Preserve the bits.  */
   2546 	      if (expP->X_add_number > 0)
   2547 		{
   2548 		  bignum_copy (generic_bignum, expP->X_add_number,
   2549 			       floatP->low, SIZE_OF_LARGE_NUMBER);
   2550 		}
   2551 	      else
   2552 		{
   2553 		  know (expP->X_add_number < 0);
   2554 		  flonum_copy (&generic_floating_point_number,
   2555 			       floatP);
   2556 		  if (strchr ("s i", operandP->vop_short))
   2557 		    {
   2558 		      /* Could possibly become S^# */
   2559 		      flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
   2560 		      switch (-expP->X_add_number)
   2561 			{
   2562 			case 'f':
   2563 			  can_be_short =
   2564 			    (literal_float[0] & 0xFC0F) == 0x4000
   2565 			    && literal_float[1] == 0;
   2566 			  break;
   2567 
   2568 			case 'd':
   2569 			  can_be_short =
   2570 			    (literal_float[0] & 0xFC0F) == 0x4000
   2571 			    && literal_float[1] == 0
   2572 			    && literal_float[2] == 0
   2573 			    && literal_float[3] == 0;
   2574 			  break;
   2575 
   2576 			case 'g':
   2577 			  can_be_short =
   2578 			    (literal_float[0] & 0xFF81) == 0x4000
   2579 			    && literal_float[1] == 0
   2580 			    && literal_float[2] == 0
   2581 			    && literal_float[3] == 0;
   2582 			  break;
   2583 
   2584 			case 'h':
   2585 			  can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
   2586 					  && (literal_float[1] & 0xE000) == 0
   2587 					  && literal_float[2] == 0
   2588 					  && literal_float[3] == 0
   2589 					  && literal_float[4] == 0
   2590 					  && literal_float[5] == 0
   2591 					  && literal_float[6] == 0
   2592 					  && literal_float[7] == 0);
   2593 			  break;
   2594 
   2595 			default:
   2596 			  BAD_CASE (-expP->X_add_number);
   2597 			  break;
   2598 			}
   2599 		    }
   2600 		}
   2601 
   2602 	      if (operandP->vop_short == 's'
   2603 		  || operandP->vop_short == 'i'
   2604 		  || (operandP->vop_short == ' '
   2605 		      && operandP->vop_reg == 0xF
   2606 		      && (operandP->vop_mode & 0xE) == 0x8))
   2607 		{
   2608 		  /* Saw a '#'.  */
   2609 		  if (operandP->vop_short == ' ')
   2610 		    {
   2611 		      /* We must chose S^ or I^.  */
   2612 		      if (expP->X_add_number > 0)
   2613 			{
   2614 			  /* Bignum: Short literal impossible.  */
   2615 			  operandP->vop_short = 'i';
   2616 			  operandP->vop_mode = 8;
   2617 			  operandP->vop_reg = 0xF;	/* VAX PC.  */
   2618 			}
   2619 		      else
   2620 			{
   2621 			  /* Flonum: Try to do it.  */
   2622 			  if (can_be_short)
   2623 			    {
   2624 			      operandP->vop_short = 's';
   2625 			      operandP->vop_mode = 0;
   2626 			      operandP->vop_ndx = -1;
   2627 			      operandP->vop_reg = -1;
   2628 			      expP->X_op = O_constant;
   2629 			    }
   2630 			  else
   2631 			    {
   2632 			      operandP->vop_short = 'i';
   2633 			      operandP->vop_mode = 8;
   2634 			      operandP->vop_reg = 0xF;	/* VAX PC */
   2635 			    }
   2636 			}	/* bignum or flonum ? */
   2637 		    }		/*  if #, but no S^ or I^ seen.  */
   2638 		  /* No more ' ' case: either 's' or 'i'.  */
   2639 		  if (operandP->vop_short == 's')
   2640 		    {
   2641 		      /* Wants to be a short literal.  */
   2642 		      if (expP->X_add_number > 0)
   2643 			{
   2644 			  as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
   2645 			  operandP->vop_short = 'i';
   2646 			  operandP->vop_mode = 8;
   2647 			  operandP->vop_reg = 0xF;	/* VAX PC.  */
   2648 			}
   2649 		      else
   2650 			{
   2651 			  if (!can_be_short)
   2652 			    {
   2653 			      as_warn (_("Can't do flonum short literal: immediate mode used."));
   2654 			      operandP->vop_short = 'i';
   2655 			      operandP->vop_mode = 8;
   2656 			      operandP->vop_reg = 0xF;	/* VAX PC.  */
   2657 			    }
   2658 			  else
   2659 			    {
   2660 			      /* Encode short literal now.  */
   2661 			      int temp = 0;
   2662 
   2663 			      switch (-expP->X_add_number)
   2664 				{
   2665 				case 'f':
   2666 				case 'd':
   2667 				  temp = literal_float[0] >> 4;
   2668 				  break;
   2669 
   2670 				case 'g':
   2671 				  temp = literal_float[0] >> 1;
   2672 				  break;
   2673 
   2674 				case 'h':
   2675 				  temp = ((literal_float[0] << 3) & 070)
   2676 				    | ((literal_float[1] >> 13) & 07);
   2677 				  break;
   2678 
   2679 				default:
   2680 				  BAD_CASE (-expP->X_add_number);
   2681 				  break;
   2682 				}
   2683 
   2684 			      floatP->low[0] = temp & 077;
   2685 			      floatP->low[1] = 0;
   2686 			    }
   2687 			}
   2688 		    }
   2689 		  else
   2690 		    {
   2691 		      /* I^# seen: set it up if float.  */
   2692 		      if (expP->X_add_number < 0)
   2693 			{
   2694 			  memcpy (floatP->low, literal_float, sizeof (literal_float));
   2695 			}
   2696 		    }		/* if S^# seen.  */
   2697 		}
   2698 	      else
   2699 		{
   2700 		  as_warn (_("A bignum/flonum may not be a displacement: 0x%"
   2701 			     PRIx64 " used"),
   2702 			   (uint64_t) (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   FLONUM_TYPE *fP;
   3273   int i;
   3274 
   3275   vip_begin (1, "$", "*", "`");
   3276 
   3277   for (i = 0, fP = float_operand;
   3278        fP < float_operand + VIT_MAX_OPERANDS;
   3279        i++, fP++)
   3280     {
   3281       fP->low = &big_operand_bits[i][0];
   3282       fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
   3283     }
   3284 }
   3285 
   3286 bfd_reloc_code_real_type
   3287 vax_cons (expressionS *exp, int size)
   3288 {
   3289   char *save;
   3290   const char *vax_cons_special_reloc;
   3291 
   3292   SKIP_WHITESPACE ();
   3293   vax_cons_special_reloc = NULL;
   3294   save = input_line_pointer;
   3295   if (input_line_pointer[0] == '%')
   3296     {
   3297       if (startswith (input_line_pointer + 1, "pcrel"))
   3298 	{
   3299 	  input_line_pointer += 6;
   3300 	  vax_cons_special_reloc = "pcrel";
   3301 	}
   3302       if (vax_cons_special_reloc)
   3303 	{
   3304 	  int bad = 0;
   3305 
   3306 	  switch (size)
   3307 	    {
   3308 	    case 1:
   3309 	      if (*input_line_pointer != '8')
   3310 		bad = 1;
   3311 	      input_line_pointer--;
   3312 	      break;
   3313 	    case 2:
   3314 	      if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
   3315 		bad = 1;
   3316 	      break;
   3317 	    case 4:
   3318 	      if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
   3319 		bad = 1;
   3320 	      break;
   3321 	    default:
   3322 	      bad = 1;
   3323 	      break;
   3324 	    }
   3325 
   3326 	  if (bad)
   3327 	    {
   3328 	      as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
   3329 		      vax_cons_special_reloc, size * 8, size);
   3330 	    }
   3331 	  else
   3332 	    {
   3333 	      input_line_pointer += 2;
   3334 	      if (*input_line_pointer != '(')
   3335 		{
   3336 		  as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3337 			  vax_cons_special_reloc, size * 8);
   3338 		  bad = 1;
   3339 		}
   3340 	    }
   3341 
   3342 	  if (bad)
   3343 	    {
   3344 	      input_line_pointer = save;
   3345 	      vax_cons_special_reloc = NULL;
   3346 	    }
   3347 	  else
   3348 	    {
   3349 	      int c;
   3350 	      char *end = ++input_line_pointer;
   3351 	      int npar = 0;
   3352 
   3353 	      while (! is_end_of_line[(c = *end)])
   3354 		{
   3355 		  if (c == '(')
   3356 	  	    npar++;
   3357 		  else if (c == ')')
   3358 	  	    {
   3359 		      if (!npar)
   3360 	      		break;
   3361 		      npar--;
   3362 		    }
   3363 	    	  end++;
   3364 		}
   3365 
   3366 	      if (c != ')')
   3367 		as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3368 			vax_cons_special_reloc, size * 8);
   3369 	      else
   3370 		{
   3371 		  *end = '\0';
   3372 		  expression (exp);
   3373 		  *end = c;
   3374 		  if (input_line_pointer != end)
   3375 		    {
   3376 		      as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
   3377 			      vax_cons_special_reloc, size * 8);
   3378 		    }
   3379 		  else
   3380 		    {
   3381 		      input_line_pointer++;
   3382 		      SKIP_WHITESPACE ();
   3383 		      c = *input_line_pointer;
   3384 		      if (! is_end_of_line[c] && c != ',')
   3385 			as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
   3386 			        vax_cons_special_reloc, size * 8);
   3387 		    }
   3388 		}
   3389 	    }
   3390 	}
   3391     }
   3392   if (vax_cons_special_reloc == NULL)
   3393     expression (exp);
   3394   else
   3395     switch (size)
   3396       {
   3397       case 1: return BFD_RELOC_8_PCREL;
   3398       case 2: return BFD_RELOC_16_PCREL;
   3399       case 4: return BFD_RELOC_32_PCREL;
   3400       }
   3401   return NO_RELOC;
   3402 }
   3403 
   3404 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
   3405    reloc for a cons.  */
   3406 
   3407 void
   3408 vax_cons_fix_new (fragS *frag, int where, unsigned int nbytes, expressionS *exp,
   3409 		  bfd_reloc_code_real_type r)
   3410 {
   3411   if (r == NO_RELOC)
   3412     r = (nbytes == 1 ? BFD_RELOC_8
   3413 	 : nbytes == 2 ? BFD_RELOC_16
   3414 	 : BFD_RELOC_32);
   3415 
   3416   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
   3417 }
   3418 
   3419 const char *
   3420 md_atof (int type, char * litP, int * sizeP)
   3421 {
   3422   return vax_md_atof (type, litP, sizeP);
   3423 }
   3424