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