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