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