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