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