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