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