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