1 /* tc-vax.c - vax-specific -
2 Copyright 1987, 1991, 1992, 1993, 1994, 1995, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
25 #include "obstack.h" /* For FRAG_APPEND_1_CHAR macro in "frags.h" */
27 /* These chars start a comment anywhere in a source file (except inside
29 const char comment_chars[] = "#";
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[] = "#";
35 const char line_separator_chars[] = ";";
37 /* Chars that can be used to separate mant from exp in floating point nums */
38 const char EXP_CHARS[] = "eE";
40 /* Chars that mean this number is a floating point constant */
42 /* or 0H1.234E-12 (see exp chars above) */
43 const char FLT_CHARS[] = "dDfFgGhH";
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. */
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];
53 /* A vax instruction after decoding. */
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(). */
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 */
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:
72 * 4 3 2 1 0 bit number
73 * ---/ /--+-------+-------+-------+-------+-------+
74 * | what state ? | how long ? |
75 * ---/ /--+-------+-------+-------+-------+-------+
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.
84 groups for VAX address relaxing.
87 length of byte, word, long
89 2a. J<cond> where <cond> is a simple flag test.
90 length of byte, word, long.
91 VAX opcodes are: (Hex)
104 Always, you complement 0th bit to reverse condition.
105 Always, 1-byte opcode, then 1-byte displacement.
107 2b. J<cond> where cond tests a memory bit.
108 length of byte, word, long.
109 Vax opcodes are: (Hex)
116 Always, you complement 0th bit to reverse condition.
117 Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
119 2c. J<cond> where cond tests low-order memory bit
120 length of byte,word,long.
121 Vax opcodes are: (Hex)
124 Always, you complement 0th bit to reverse condition.
125 Always, 1-byte opcode, longword-address, 1-byte displacement.
128 length of byte,word,long.
129 Vax opcodes are: (Hex)
132 These are like (2) but there is no condition to reverse.
133 Always, 1 byte opcode, then displacement/absolute.
136 length of word, long.
137 Vax opcodes are: (Hex)
145 Always, we cannot reverse the sense of the branch; we have a word
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
152 length of long, long, byte.
153 Vax opcodes are: (Hex)
158 Always, we cannot reverse the sense of the branch; we have a byte
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.
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.
168 We sometimes store context in the operand literal. This way we can figure out
169 after relax() what the original addressing mode was.
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. */
177 #define WF (2+ 32767)
178 #define WB (2+-32768)
179 /* Dont need LF, LB because they always reach. [They are coded as 0.] */
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))
185 const relax_typeS md_relax_table[] =
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 */
220 const pseudo_typeS md_pseudo_table[] =
222 {"dfloat", float_cons, 'd'},
223 {"ffloat", float_cons, 'f'},
224 {"gfloat", float_cons, 'g'},
225 {"hfloat", float_cons, 'h'},
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)
235 #define STATE_BYTE (0)
236 #define STATE_WORD (1)
237 #define STATE_LONG (2)
238 #define STATE_UNDF (3) /* Symbol undefined in pass1 */
240 #define min(a, b) ((a) < (b) ? (a) : (b))
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 *,
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 *));
257 if ((errtxt = vip_begin (1, "$", "*", "`")) != 0)
259 as_fatal (_("VIP_BEGIN error:%s"), errtxt);
262 for (i = 0, fP = float_operand;
263 fP < float_operand + VIT_MAX_OPERANDS;
266 fP->low = &big_operand_bits[i][0];
267 fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
272 md_number_to_chars (con, value, nbytes)
277 number_to_chars_littleendian (con, value, nbytes);
280 /* Fix up some data or instructions after we find out the value of a symbol
281 that they reference. */
283 void /* Knows about order of bytes in address. */
284 md_apply_fix (fixP, value)
288 number_to_chars_littleendian (fixP->fx_where + fixP->fx_frag->fr_literal,
289 (valueT) value, fixP->fx_size);
293 md_chars_to_number (con, nbytes)
294 unsigned char con[]; /* Low order byte 1st. */
295 int nbytes; /* Number of bytes in the input. */
298 for (retval = 0, con += nbytes - 1; nbytes--; con--)
300 retval <<= BITS_PER_CHAR;
306 /* vax:md_assemble() emit frags for 1 instruction */
309 md_assemble (instruction_string)
310 char *instruction_string; /* A string: assemble 1 instruction. */
312 /* Non-zero if operand expression's segment is not known yet. */
317 /* An operand. Scans all operands. */
318 struct vop *operandP;
319 char *save_input_line_pointer;
320 /* What used to live after an expression. */
322 /* 1: instruction_string bad for all passes. */
324 /* Points to slot just after last operand. */
325 struct vop *end_operandP;
326 /* Points to expression values for this operand. */
330 /* These refer to an instruction operand expression. */
331 /* Target segment of the address. */
333 valueT this_add_number;
334 /* Positive (minuend) symbol. */
335 symbolS *this_add_symbol;
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. */
345 /* 0, or 1 if '@' is in addressing mode. */
347 /* From vop_nbytes: vax_operand_width (in bytes) */
350 LITTLENUM_TYPE literal_float[8];
351 /* Big enough for any floating point literal. */
353 vip (&v, instruction_string);
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.
362 if ((goofed = (*v.vit_error)) != 0)
364 as_warn (_("Ignoring statement due to \"%s\""), v.vit_error);
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.
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;
377 operandP < end_operandP;
379 operandP++, expP++, segP++, floatP++)
380 { /* for each operand */
381 if (operandP->vop_error)
383 as_warn (_("Ignoring statement because \"%s\""), operandP->vop_error);
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. */
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);
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;
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.
430 as_warn (_("Can't relocate expression"));
434 /* Preserve the bits. */
435 if (expP->X_add_number > 0)
437 bignum_copy (generic_bignum, expP->X_add_number,
438 floatP->low, SIZE_OF_LARGE_NUMBER);
442 know (expP->X_add_number < 0);
443 flonum_copy (&generic_floating_point_number,
445 if (strchr ("s i", operandP->vop_short))
447 /* Could possibly become S^# */
448 flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
449 switch (-expP->X_add_number)
453 (literal_float[0] & 0xFC0F) == 0x4000
454 && literal_float[1] == 0;
459 (literal_float[0] & 0xFC0F) == 0x4000
460 && literal_float[1] == 0
461 && literal_float[2] == 0
462 && literal_float[3] == 0;
467 (literal_float[0] & 0xFF81) == 0x4000
468 && literal_float[1] == 0
469 && literal_float[2] == 0
470 && literal_float[3] == 0;
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);
485 BAD_CASE (-expP->X_add_number);
487 } /* switch (float type) */
488 } /* if (could want to become S^#...) */
489 } /* bignum or flonum ? */
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))
498 if (operandP->vop_short == ' ')
500 /* We must chose S^ or I^. */
501 if (expP->X_add_number > 0)
503 /* Bignum: Short literal impossible. */
504 operandP->vop_short = 'i';
505 operandP->vop_mode = 8;
506 operandP->vop_reg = 0xF; /* VAX PC. */
510 /* Flonum: Try to do it. */
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;
521 operandP->vop_short = 'i';
522 operandP->vop_mode = 8;
523 operandP->vop_reg = 0xF; /* VAX PC */
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')
530 /* Wants to be a short literal. */
531 if (expP->X_add_number > 0)
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. */
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. */
548 { /* Encode short literal now. */
551 switch (-expP->X_add_number)
555 temp = literal_float[0] >> 4;
559 temp = literal_float[0] >> 1;
563 temp = ((literal_float[0] << 3) & 070)
564 | ((literal_float[1] >> 13) & 07);
568 BAD_CASE (-expP->X_add_number);
572 floatP->low[0] = temp & 077;
574 } /* if can be short literal float */
575 } /* flonum or bignum ? */
578 { /* I^# seen: set it up if float. */
579 if (expP->X_add_number < 0)
581 memcpy (floatP->low, literal_float, sizeof (literal_float));
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. */
591 expP->X_add_number = floatP->low[0]
592 | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
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
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.
606 if (input_line_pointer != operandP->vop_expr_end + 1)
608 as_warn ("Junk at end of expression \"%s\"", input_line_pointer);
611 operandP->vop_expr_end[1] = c_save;
613 } /* for(each operand) */
615 input_line_pointer = save_input_line_pointer;
617 if (need_pass_2 || goofed)
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;
633 operandP < end_operandP;
640 if (operandP->vop_ndx >= 0)
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) */
647 /* Here to make main operand frag(s). */
648 this_add_number = expP->X_add_number;
649 this_add_symbol = expP->X_add_symbol;
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'
657 nbytes = operandP->vop_nbytes;
658 if (operandP->vop_access == 'b')
660 if (to_seg == now_seg || is_undefined)
662 /* If is_undefined, then it might BECOME now_seg. */
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);
670 { /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
672 length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
673 if (opcode_as_number & VIT_OPCODE_SPECIAL)
675 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
678 frag_var (rs_machine_dependent, 5, 1,
679 ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
680 this_add_symbol, this_add_number,
685 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
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,
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,
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,
716 /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
718 * --- SEG FLOAT MAY APPEAR HERE ----
720 if (to_seg == SEG_ABSOLUTE)
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,
733 know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
734 if (opcode_as_number & VIT_OPCODE_SPECIAL)
736 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
739 *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
740 know (opcode_as_chars[1] == 0);
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. */
748 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
756 p[5] = VAX_ABSOLUTE_MODE; /* @#... */
757 md_number_to_chars (p + 6, this_add_number, 4);
767 know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
773 p[4] = VAX_ABSOLUTE_MODE; /* @#... */
774 md_number_to_chars (p + 5, this_add_number, 4);
787 *opcode_low_byteP ^= 1;
788 /* To reverse the condition in a VAX branch,
789 complement the lowest order bit. */
793 p[2] = VAX_ABSOLUTE_MODE; /* @#... */
794 md_number_to_chars (p + 3, this_add_number, 4);
805 /* to_seg != now_seg && to_seg != SEG_UNKNOWN && to_Seg != SEG_ABSOLUTE */
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,
817 know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
818 if (opcode_as_number & VIT_OPCODE_SPECIAL)
820 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
823 know (opcode_as_chars[1] == 0);
824 *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
826 p[0] = VAX_PC_RELATIVE_MODE;
828 p + 1 - frag_now->fr_literal, 4,
830 this_add_number, 1, NO_RELOC);
831 /* Now eg JMP foo or JSB foo. */
835 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
843 p[5] = VAX_PC_RELATIVE_MODE;
845 p + 6 - frag_now->fr_literal, 4,
847 this_add_number, 1, NO_RELOC);
857 know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
863 p[4] = VAX_PC_RELATIVE_MODE;
865 p + 5 - frag_now->fr_literal,
867 this_add_number, 1, NO_RELOC);
879 know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
880 *opcode_low_byteP ^= 1; /* Reverse branch condition. */
884 p[2] = VAX_PC_RELATIVE_MODE;
885 fix_new (frag_now, p + 3 - frag_now->fr_literal,
887 this_add_number, 1, NO_RELOC);
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')
904 if (to_seg == SEG_ABSOLUTE)
906 if (this_add_number >= 64)
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;
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;
924 if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
925 || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
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. */
934 /* {@}{q^}foo{(Rn)} or S^#foo */
935 if (operandP->vop_reg == -1 && operandP->vop_short != 's')
938 if (to_seg == now_seg)
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,
947 know (operandP->vop_mode == 10 + at);
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. */
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);
966 { /* to_seg != now_seg */
967 if (this_add_symbol == NULL)
969 know (to_seg == SEG_ABSOLUTE);
970 /* Do @#foo: simpler relocation than foo-.(pc) anyway. */
972 p[0] = VAX_ABSOLUTE_MODE; /* @#... */
973 md_number_to_chars (p + 1, this_add_number, 4);
974 if (length && length != 4)
976 as_warn (_("Length specification ignored. Address mode 9F used"));
981 /* {@}{q^}other_seg */
982 know ((length == 0 && operandP->vop_short == ' ')
983 || (length > 0 && operandP->vop_short != ' '));
987 * We have a SEG_UNKNOWN symbol. It might
988 * turn out to be in the same segment as
989 * the instruction, permitting relaxation.
991 p = frag_var (rs_machine_dependent, 5, 2,
992 ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
993 this_add_symbol, this_add_number,
1001 know (operandP->vop_short == ' ');
1002 length = 4; /* Longest possible. */
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);
1008 p + 1 - frag_now->fr_literal,
1009 length, this_add_symbol,
1010 this_add_number, 1, NO_RELOC);
1017 /* {@}{q^}foo(Rn) or S^# or I^# or # */
1018 if (operandP->vop_mode < 0xA)
1020 /* # or S^# or I^# */
1021 if (operandP->vop_access == 'v'
1022 || operandP->vop_access == 'a')
1024 if (operandP->vop_access == 'v')
1025 as_warn (_("Invalid operand: immediate value used as base address."));
1027 as_warn (_("Invalid operand: immediate value used as address."));
1028 /* gcc 2.6.3 is known to generate these in at least
1032 && to_seg == SEG_ABSOLUTE && (expP->X_op != O_big)
1033 && operandP->vop_mode == 8 /* No '@'. */
1034 && this_add_number < 64)
1036 operandP->vop_short = 's';
1038 if (operandP->vop_short == 's')
1040 FRAG_APPEND_1_CHAR (this_add_number);
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))
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));
1061 memset (p + 5, '\0', nbytes - 4);
1066 if (expP->X_op == O_big)
1069 * Problem here is to get the bytes
1070 * in the right order. We stored
1071 * our constant as LITTLENUMs, not
1083 for (p++; nbytes; nbytes -= 2, p += 2, lP++)
1085 md_number_to_chars (p, *lP, 2);
1091 fix_new (frag_now, p + 1 - frag_now->fr_literal,
1092 nbytes, this_add_symbol,
1093 this_add_number, 0, NO_RELOC);
1099 { /* {@}{q^}foo(Rn) */
1100 know ((length == 0 && operandP->vop_short == ' ')
1101 || (length > 0 && operandP->vop_short != ' '));
1104 if (to_seg == SEG_ABSOLUTE)
1108 test = this_add_number;
1113 length = test & 0xffff8000 ? 4
1114 : test & 0xffffff80 ? 2
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)
1128 md_number_to_chars (p + 1, this_add_number, length);
1132 fix_new (frag_now, p + 1 - frag_now->fr_literal,
1133 length, this_add_symbol,
1134 this_add_number, 0, NO_RELOC);
1138 } /* if(single-byte-operand) */
1140 } /* for(operandP) */
1141 } /* vax_assemble() */
1144 * md_estimate_size_before_relax()
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
1156 md_estimate_size_before_relax (fragP, segment)
1163 old_fr_fix = fragP->fr_fix;
1164 switch (fragP->fr_subtype)
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);
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);
1182 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF):
1183 if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
1185 fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
1189 p = fragP->fr_literal + old_fr_fix;
1190 *fragP->fr_opcode ^= 1; /* Reverse sense of branch. */
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);
1201 case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_UNDF):
1202 if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
1204 fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
1208 p = fragP->fr_literal + old_fr_fix;
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);
1222 case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_UNDF):
1223 if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
1225 fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
1229 p = fragP->fr_literal + old_fr_fix;
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);
1242 case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_UNDF):
1243 if (S_GET_SEGMENT (fragP->fr_symbol) == segment)
1245 fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
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);
1262 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
1263 } /* md_estimate_size_before_relax() */
1266 * md_convert_frag();
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.
1273 * Out: Any fixSs and constants are set up.
1274 * Caller will turn frag into a ".space 0".
1277 md_convert_frag (headers, seg, fragP)
1278 object_headers *headers;
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. */
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? */
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;
1302 target_address = S_GET_VALUE (symbolP) + fragP->fr_offset;
1303 address_of_var = fragP->fr_address + where;
1305 switch (fragP->fr_subtype)
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);
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);
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);
1329 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
1330 addressP[0] = target_address - (address_of_var + 1);
1334 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
1335 opcodeP[0] ^= 1; /* Reverse sense of test. */
1337 addressP[1] = VAX_BRB + VAX_WIDEN_WORD;
1338 md_number_to_chars (addressP + 2, target_address - (address_of_var + 4), 2);
1342 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
1343 opcodeP[0] ^= 1; /* Reverse sense of test. */
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);
1351 case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
1352 addressP[0] = target_address - (address_of_var + 1);
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);
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);
1369 case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
1370 md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
1374 case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
1377 addressP[2] = VAX_BRB;
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);
1385 case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
1386 addressP[0] = target_address - (address_of_var + 1);
1390 case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
1392 addressP[1] = VAX_BRB;
1394 addressP[3] = VAX_BRW;
1395 md_number_to_chars (addressP + 4, target_address - (address_of_var + 6), 2);
1399 case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
1401 addressP[1] = VAX_BRB;
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);
1410 BAD_CASE (fragP->fr_subtype);
1413 fragP->fr_fix += extension;
1414 } /* md_convert_frag() */
1416 /* Translate internal format of relocation info into target format.
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
1424 md_ri_to_chars (the_bytes, ri)
1426 struct reloc_info_generic ri;
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;
1438 #endif /* comment */
1441 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
1444 relax_addressT segment_address_in_file;
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.
1451 static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
1454 know (fixP->fx_addsy != NULL);
1456 md_number_to_chars (where,
1457 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
1460 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
1461 ? S_GET_TYPE (fixP->fx_addsy)
1462 : fixP->fx_addsy->sy_number);
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));
1473 * BUGS, GRIPES, APOLOGIA, etc.
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.
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.
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.
1488 * The idea here is that the assembler has taken care of all:
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)
1505 * E D I T H I S T O R Y
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.
1537 /* handle of the OPCODE hash table */
1538 static struct hash_control *op_hash;
1541 * In: 1 character, from "bdfghloqpw" being the data-type of an operand
1542 * of a vax instruction.
1544 * Out: the length of an operand of that type, in bytes.
1545 * Special branch operands types "-?!" have length 0.
1548 static const short int vax_operand_width_size[256] =
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,
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.
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.
1579 #include "opcode/vax.h"
1582 * This is a table of optional op-codes. All of them represent
1583 * 'synthetic' instructions that seem popular.
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.
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.
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.
1603 * If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
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.
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:'.
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'.
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'.
1625 static const struct vot
1626 synthetic_votstrs[] =
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}},
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}},
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}},
1667 {"jaoblss", {"rlmlb:", 0xC00000f2}},
1668 {"jaobleq", {"rlmlb:", 0xC00000f3}},
1669 {"jsobgeq", {"mlb:", 0xC00000f4}},
1670 {"jsobgtr", {"mlb:", 0xC00000f5}},
1672 /* CASEx has no branch addresses in our conception of it. */
1673 /* You should use ".word ..." statements after the "case ...". */
1675 {"", {"", 0}} /* empty is end sentinel */
1677 }; /* synthetic_votstrs */
1680 * v i p _ b e g i n ( )
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 "#", "@", "^".
1691 vip_begin (synthetic_too, immediate, indirect, displen)
1692 int synthetic_too; /* 1 means include jXXX op-codes. */
1693 const char *immediate, *indirect, *displen;
1695 const struct vot *vP; /* scan votstrs */
1696 const char *retval = 0; /* error text */
1698 op_hash = hash_new ();
1700 for (vP = votstrs; *vP->vot_name && !retval; vP++)
1701 retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);
1704 for (vP = synthetic_votstrs; *vP->vot_name && !retval; vP++)
1705 retval = hash_insert (op_hash, vP->vot_name, (PTR) &vP->vot_detail);
1708 vip_op_defaults (immediate, indirect, displen);
1717 * This converts a string into a vax instruction.
1718 * The string must be a bare single instruction in dec-vax (with BSD4 frobs)
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
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.
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.
1735 * You MUST have called vip_begin() once before using this function.
1739 vip (vitP, instring)
1740 struct vit *vitP; /* We build an exploded instruction here. */
1741 char *instring; /* Text of a vax instruction: we modify. */
1743 /* How to bit-encode this opcode. */
1744 struct vot_wot *vwP;
1745 /* 1/skip whitespace.2/scan vot_how */
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). */
1756 /* Op-code of this instruction. */
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)
1766 vitP->vit_error = _("No operator");
1768 memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
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.
1779 vwP = (struct vot_wot *) hash_find (op_hash, instring);
1780 *p = c; /* Restore char after op-code. */
1783 vitP->vit_error = _("Unknown operator");
1785 memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
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
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.
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 */
1809 for (howp = vwP->vot_how, operandp = vitP->vit_operand;
1810 !(alloperr && *alloperr) && *howp;
1811 operandp++, howp += 2)
1814 * Here to parse one operand. Leave instring pointing just
1815 * past any one ',' that marks the end of this operand.
1818 as_fatal (_("odd number of bytes in operand description"));
1821 for (q = instring; (c = *q) && c != ','; q++)
1824 * Q points to ',' or '\0' that ends argument. C is that
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 */
1839 alloperr = _("Not enough operands");
1843 if (*instring == ' ')
1844 instring++; /* Skip whitespace. */
1846 alloperr = _("Too many operands");
1848 vitP->vit_error = alloperr;
1851 vitP->vit_operands = count;
1857 * Test program for above.
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];
1874 printf ("0 means no synthetic instructions. ");
1875 printf ("Value for vip_begin? ");
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 @ ");
1883 printf ("enter displen symbols eg enter ^ ");
1885 if (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))
1887 error ("vip_begin=%s", p);
1889 printf ("An empty input line will quit you from the vax instruction parser\n");
1892 printf ("vax instruction: ");
1897 break; /* out of for each input text loop */
1899 vip (&myvit, answer);
1900 if (*myvit.vit_error)
1902 printf ("ERR:\"%s\"\n", myvit.vit_error);
1905 for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
1910 printf ("%02x ", *p & 0xFF);
1912 printf (" operand count=%d.\n", mycount = myvit.vit_operands);
1913 for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
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,
1919 for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
1924 if (myvop->vop_error)
1926 printf (" err:\"%s\"\n", myvop->vop_error);
1928 if (myvop->vop_warn)
1930 printf (" wrn:\"%s\"\n", myvop->vop_warn);
1935 exit (EXIT_SUCCESS);
1938 #endif /* #ifdef test */
1940 /* end of vax_ins_parse.c */
1942 /* vax_reg_parse.c - convert a VAX register name to a number */
1944 /* Copyright (C) 1987 Free Software Foundation, Inc. A part of GNU. */
1947 * v a x _ r e g _ p a r s e ( )
1949 * Take 3 char.s, the last of which may be `\0` (non-existent)
1950 * and return the VAX register number that they represent.
1952 * Return -1 if they don't form a register name. Good names return
1953 * a number from 0:15 inclusive.
1955 * Case is not important in a name.
1957 * Register names understood are:
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 */
1988 int retval; /* return -1:15 */
1996 if (isdigit (c2) && c1 == 'r')
2001 retval = retval * 10 + c3 - '0';
2002 retval = (retval > 15) ? -1 : retval;
2003 /* clamp the register value to 1 hex digit */
2006 retval = -1; /* c3 must be '\0' or a digit */
2008 else if (c3) /* There are no three letter regs */
2027 else if (c1 == 'p' && c2 == 'c')
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
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.
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.
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).
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!
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?
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.
2096 * The input is a string, ending with '\0'.
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.
2101 * The output is a skeletal instruction.
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.
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
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
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.
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 '#', '@', '^'.
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 ^ */
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)
2148 /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
2152 #if defined(CONST_TABLE)
2154 #define I VIP_IMMEDIATE,
2155 #define S VIP_INDIRECT,
2156 #define D VIP_DISPLEN,
2158 vip_metacharacters[256] =
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 { | } ~ ^?*/
2169 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2170 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2171 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2172 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2173 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2174 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2175 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2176 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2183 static char vip_metacharacters[256];
2186 vip_op_1 (bit, syms)
2192 while ((t = *syms++) != 0)
2193 vip_metacharacters[t] |= bit;
2196 /* Can be called any time. More arguments may appear in future. */
2198 vip_op_defaults (immediate, indirect, displen)
2199 const char *immediate;
2200 const char *indirect;
2201 const char *displen;
2203 vip_op_1 (VIP_IMMEDIATE, immediate);
2204 vip_op_1 (VIP_INDIRECT, indirect);
2205 vip_op_1 (VIP_DISPLEN, displen);
2212 * Dec defines the semantics of address modes (and values)
2213 * by a two-letter code, explained here.
2215 * letter 1: access type
2217 * a address calculation - no data access, registers forbidden
2218 * b branch displacement
2219 * m read - let go of bus - write back "modify"
2221 * v bit field address: like 'a' but registers are OK
2223 * space no operator (eg ".long foo") [our convention]
2225 * letter 2: data type (i.e. width, alignment)
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
2236 * ? simple synthetic branch operand
2237 * - unconditional synthetic JSB/JSR operand
2238 * ! complex synthetic branch operand
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.
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
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.
2253 * After parsing we have:
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
2266 * Again, I dare not explain it: just trace ALL the code!
2270 vip_op (optext, vopP)
2271 /* user's input string e.g.: "@B^foo@bar(AP)[FP]:" */
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. */
2279 /* track operand text forward */
2281 /* track operand text backward */
2283 /* 1 if leading '@' ('*') seen */
2285 /* one of " bilsw" */
2287 /* 1 if leading '#' ('$') seen */
2291 /* 1 if () surround register */
2293 /* register number, -1:absent */
2295 /* index register number -1:absent */
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 */
2302 /* warn about weird modes pf address */
2304 /* preserve q in case we backup */
2306 /* build up 4-bit operand mode here */
2307 /* note: index mode is in ndx, this is */
2308 /* the major mode of operand address */
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
2315 char access_mode; /* vop_access. */
2316 char width; /* vop_width. */
2318 access_mode = vopP->vop_access;
2319 width = vopP->vop_width;
2320 /* None of our code bugs (yet), no user text errors, no warnings
2326 if (*p == ' ') /* Expect all whitespace reduced to ' '. */
2327 p++; /* skip over whitespace */
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 */
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 '^'.
2347 if (DISPLENP (p[1]) && strchr ("bilws", len = c))
2348 p += 2; /* skip (letter) '^' */
2349 else /* no (letter) '^' seen */
2350 len = ' '; /* len is determined */
2353 if (*p == ' ') /* Expect all whitespace reduced to ' '. */
2354 p++; /* skip over whitespace */
2356 if ((hash = IMMEDIATEP (*p)) != 0) /* 1 if *p=='#' ('$' for Un*x) */
2357 p++; /* hash is determined */
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.
2364 * Lets point q at the end of the text and parse that (backwards).
2367 for (q = p; *q; q++)
2369 q--; /* now q points at last char of text */
2371 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
2373 /* reverse over whitespace, but don't */
2374 /* run back over *p */
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
2386 while (q >= p && *q != '[')
2388 /* either q<p or we got matching '[' */
2390 err = _("no '[' to match ']'");
2394 * Confusers like "[]" will eventually lose with a bad register
2395 * name error. So again we don't need to check for early '\0'.
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]);
2404 * Since we saw a ']' we will demand a register name in the [].
2405 * If luser hasn't given us one: be rude.
2408 err = _("bad register in []");
2410 err = _("[PC] index banned");
2412 q--; /* point q just before "[...]" */
2416 ndx = -1; /* no ']', so no iNDeX register */
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 "[...]".
2424 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
2426 /* reverse over whitespace, but don't */
2427 /* run back over *p */
2430 sign = 0; /* no ()+ or -() seen yet */
2432 if (q > p + 3 && *q == '+' && q[-1] == ')')
2434 sign = 1; /* we saw a ")+" */
2435 q--; /* q points to ')' */
2438 if (*q == ')' && q > p + 2)
2440 paren = 1; /* assume we have "(...)" */
2441 while (q >= p && *q != '(')
2443 /* either q<p or we got matching '(' */
2445 err = _("no '(' to match ')'");
2449 * Confusers like "()" will eventually lose with a bad register
2450 * name error. So again we don't need to check for early '\0'.
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]);
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.
2466 /* JF allow parenthasized expressions. I hope this works */
2470 /* err = "unknown register in ()"; */
2473 q--; /* point just before '(' of "(...)" */
2475 * If err == "..." then we lost. Run away.
2476 * Otherwise if reg >= 0 then we saw (Rn).
2480 * If err == "..." then we lost.
2481 * Otherwise paren==1 and reg = register in "()".
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.
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.
2502 if (paren && sign < 1)/* !sign is adequate test */
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.
2521 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
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]);
2528 reg = -1; /* always comes here if no register at all */
2530 * Here with a definitive reg value.
2541 * have reg. -1:absent; else 0:15
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.
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.
2558 * Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
2572 * p:q whatever was input
2574 * err " " or error message, and other outputs trashed
2576 /* branch operands have restricted forms */
2577 if ((!err || !*err) && access_mode == 'b')
2579 if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
2580 err = _("invalid branch operand");
2585 /* Since nobody seems to use it: comment this 'feature'(?) out for now. */
2588 * Case of stand-alone operand. e.g. ".long foo"
2602 * p:q whatever was input
2604 * err " " or error message, and other outputs trashed
2606 if ((!err || !*err) && access_mode == ' ')
2609 err = _("address prohibits @");
2611 err = _("address prohibits #");
2615 err = _("address prohibits -()");
2617 err = _("address prohibits ()+");
2620 err = _("address prohibits ()");
2622 err = _("address prohibits []");
2624 err = _("address prohibits register");
2625 else if (len != ' ')
2626 err = _("address prohibits displacement length specifier");
2629 err = " "; /* succeed */
2633 #endif /*#Ifdef NEVER*/
2639 * len 's' definition
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
2653 if ((!err || !*err) && len == 's')
2655 if (!hash || paren || at || ndx >= 0)
2656 err = _("invalid operand of S^#");
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.
2670 * We have all the expression we will ever get.
2673 err = _("S^# needs expression");
2674 else if (access_mode == 'r')
2676 err = " "; /* WIN! */
2680 err = _("S^# may only read-access");
2685 * Case of -(Rn), which is weird case.
2691 * sign -1 by definition
2692 * paren 1 by definition
2693 * reg present by definition
2699 * exp "" enforce empty expression
2700 * ndx optional warn if same as reg
2702 if ((!err || !*err) && sign < 0)
2704 if (len != ' ' || hash || at || p <= q)
2705 err = _("invalid operand of -()");
2708 err = " "; /* win */
2711 wrn = _("-(PC) unpredictable");
2712 else if (reg == ndx)
2713 wrn = _("[]index same as -()register: unpredictable");
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.
2722 if (paren && !sign && !hash && !at && len == ' ' && p > q)
2729 * Case of (Rn)+, which is slightly different.
2735 * sign +1 by definition
2736 * paren 1 by definition
2737 * reg present by definition
2743 * exp "" enforce empty expression
2744 * ndx optional warn if same as reg
2746 if ((!err || !*err) && sign > 0)
2748 if (len != ' ' || hash || p <= q)
2749 err = _("invalid operand of ()+");
2752 err = " "; /* win */
2753 mode = 8 + (at ? 1 : 0);
2755 wrn = _("(PC)+ unpredictable");
2756 else if (reg == ndx)
2757 wrn = _("[]index same as ()+register: unpredictable");
2762 * Case of #, without S^.
2766 * hash 1 by definition
2779 if ((!err || !*err) && hash)
2781 if (len != 'i' && len != ' ')
2782 err = _("# conflicts length");
2784 err = _("# bars register");
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.
2795 reg = -1; /* no register any more */
2797 err = " "; /* win */
2799 /* JF a bugfix, I think! */
2800 if (at && access_mode == 'a')
2801 vopP->vop_nbytes = 4;
2803 mode = (at ? 9 : 8);
2805 if ((access_mode == 'm' || access_mode == 'w') && !at)
2806 wrn = _("writing or modifying # is unpredictable");
2810 * If !*err, then sign == 0
2815 * Case of Rn. We seperate this one because it has a few special
2816 * errors the remaining modes lack.
2820 * hash 0 by program logic
2822 * sign 0 by program logic
2823 * paren 0 by definition
2824 * reg present by definition
2829 * len ' ' enforce no length
2830 * exp "" enforce empty expression
2831 * ndx optional warn if same as reg
2833 if ((!err || !*err) && !paren && reg >= 0)
2836 err = _("length not needed");
2839 err = " "; /* win */
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");
2849 * Idea here is to detect from length of datum
2850 * and from register number if we will touch PC.
2852 * vop_nbytes is number of bytes in operand.
2853 * Compute highest byte affected, compare to PC0.
2855 if ((vopP->vop_nbytes + reg * 4) > 60)
2856 wrn = _("PC part of operand unpredictable");
2857 err = " "; /* win */
2862 * If !*err, sign == 0
2864 * paren == 1 OR reg==-1
2868 * Rest of cases fit into one bunch.
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
2879 * out: mode 10 + @ + len
2881 * len ' ' or 'b' or 'w' or 'l'
2883 * ndx optional warn if same as reg
2887 err = " "; /* win (always) */
2888 mode = 10 + (at ? 1 : 0);
2895 case ' ': /* assumed B^ until our caller changes it */
2902 * here with completely specified mode
2910 err = 0; /* " " is no longer an error */
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;
2924 Summary of vip_op outputs.
2928 {@}Rn 5+@ n ' ' optional
2929 branch operand 0 -1 ' ' -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
2938 #ifdef TEST /* #Define to use this testbed. */
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.
2948 char answer[100]; /* human types into here */
2961 int my_operand_length;
2962 char my_immediate[200];
2963 char my_indirect[200];
2964 char my_displen[200];
2968 printf ("enter immediate symbols eg enter # ");
2969 gets (my_immediate);
2970 printf ("enter indirect symbols eg enter @ ");
2972 printf ("enter displen symbols eg enter ^ ");
2974 vip_op_defaults (my_immediate, my_indirect, my_displen);
2977 printf ("access,width (eg 'ab' or 'wh') [empty line to quit] : ");
2981 exit (EXIT_SUCCESS);
2982 myaccess = answer[0];
2983 mywidth = answer[1];
2987 my_operand_length = 1;
2990 my_operand_length = 8;
2993 my_operand_length = 4;
2996 my_operand_length = 16;
2999 my_operand_length = 32;
3002 my_operand_length = 4;
3005 my_operand_length = 16;
3008 my_operand_length = 8;
3011 my_operand_length = 2;
3016 my_operand_length = 0;
3020 my_operand_length = 2;
3021 printf ("I dn't understand access width %c\n", mywidth);
3024 printf ("VAX assembler instruction operand: ");
3027 mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
3028 &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
3032 printf ("error: \"%s\"\n", myerr);
3034 printf (" bug: \"%s\"\n", mybug);
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++);
3052 mumble (text, value)
3056 printf ("%s:", text);
3058 printf ("%xx", value);
3064 #endif /* ifdef TEST */
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 */
3073 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
3075 addressT from_addr, to_addr;
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);
3091 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
3093 addressT from_addr, to_addr;
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);
3107 CONST char *md_shortopts = "d:STt:V+1h:Hv::";
3109 CONST char *md_shortopts = "d:STt:V";
3111 struct option md_longopts[] = {
3112 {NULL, no_argument, NULL, 0}
3114 size_t md_longopts_size = sizeof (md_longopts);
3117 md_parse_option (c, arg)
3124 as_warn (_("SYMBOL TABLE not implemented"));
3128 as_warn (_("TOKEN TRACE not implemented"));
3132 as_warn (_("Displacement length %s ignored!"), arg);
3136 as_warn (_("I don't need or use temp. file \"%s\"."), arg);
3140 as_warn (_("I don't use an interpass file! -V ignored"));
3144 case '+': /* For g++. Hash any name > 31 chars long. */
3145 flag_hash_long_names = 1;
3148 case '1': /* For backward compatibility */
3152 case 'H': /* Show new symbol after hash truncation */
3153 flag_show_after_trunc = 1;
3156 case 'h': /* No hashing of mixed-case names */
3158 extern char vms_name_mapping;
3159 vms_name_mapping = atoi (arg);
3160 flag_no_hash_mixed_case = 1;
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;
3182 md_show_usage (stream)
3185 fprintf (stream, _("\
3187 -d LENGTH ignored\n\
3194 fprintf (stream, _("\
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"));
3205 /* We have no need to default values of symbols. */
3208 md_undefined_symbol (name)
3214 /* Round up a section size to the appropriate boundary. */
3216 md_section_align (segment, size)
3220 return size; /* Byte alignment is fine */
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) */
3227 md_pcrel_from (fixP)
3230 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
3233 /* end of tc-vax.c */