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