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