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