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