tc-vax.c revision 1.16 1 /* tc-vax.c - vax-specific -
2 Copyright (C) 1987-2022 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 opcode, 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 /* Don't 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 (int, 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_subtract (fixP);
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[0] + 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[0] + 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 htab_t 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 void
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
973 op_hash = str_htab_create ();
974
975 for (vP = votstrs; *vP->vot_name; vP++)
976 if (str_hash_insert (op_hash, vP->vot_name, &vP->vot_detail, 0) != NULL)
977 as_fatal (_("duplicate %s"), vP->vot_name);
978
979 if (synthetic_too)
980 for (vP = synthetic_votstrs; *vP->vot_name; vP++)
981 if (str_hash_insert (op_hash, vP->vot_name, &vP->vot_detail, 0) != NULL)
982 as_fatal (_("duplicate %s"), vP->vot_name);
983
984 #ifndef CONST_TABLE
985 vip_op_defaults (immediate, indirect, displen);
986 #endif
987 }
988
989 /* Take 3 char.s, the last of which may be `\0` (non-existent)
990 and return the VAX register number that they represent.
991
992 Return -1 if they don't form a register name. Good names return
993 a number from 0:15 inclusive.
994
995 Case is not important in a name.
996
997 Register names understood are:
998
999 R0
1000 R1
1001 R2
1002 R3
1003 R4
1004 R5
1005 R6
1006 R7
1007 R8
1008 R9
1009 R10
1010 R11
1011 R12 AP
1012 R13 FP
1013 R14 SP
1014 R15 PC */
1015
1016 #define AP 12
1017 #define FP 13
1018 #define SP 14
1019 #define PC 15
1020
1021 /* Returns the register number of something like '%r15' or 'ap', supplied
1022 in four single chars. Returns -1 if the register isn't recognized,
1023 0..15 otherwise. */
1024 static int
1025 vax_reg_parse (char c1, char c2, char c3, char c4)
1026 {
1027 int retval = -1;
1028
1029 #ifdef OBJ_ELF
1030 if (c1 != '%') /* Register prefixes are mandatory for ELF. */
1031 return retval;
1032 c1 = c2;
1033 c2 = c3;
1034 c3 = c4;
1035 #endif
1036 #ifdef OBJ_VMS
1037 if (c4 != 0) /* Register prefixes are not allowed under VMS. */
1038 return retval;
1039 #endif
1040 #ifdef OBJ_AOUT
1041 if (c1 == '%') /* Register prefixes are optional under a.out. */
1042 {
1043 c1 = c2;
1044 c2 = c3;
1045 c3 = c4;
1046 }
1047 else if (c3 && c4) /* Can't be 4 characters long. */
1048 return retval;
1049 #endif
1050
1051 c1 = TOLOWER (c1);
1052 c2 = TOLOWER (c2);
1053 if (ISDIGIT (c2) && c1 == 'r')
1054 {
1055 retval = c2 - '0';
1056 if (ISDIGIT (c3))
1057 {
1058 retval = retval * 10 + c3 - '0';
1059 retval = (retval > 15) ? -1 : retval;
1060 /* clamp the register value to 1 hex digit */
1061 }
1062 else if (c3)
1063 retval = -1; /* c3 must be '\0' or a digit. */
1064 }
1065 else if (c3) /* There are no three letter regs. */
1066 retval = -1;
1067 else if (c2 == 'p')
1068 {
1069 switch (c1)
1070 {
1071 case 's':
1072 retval = SP;
1073 break;
1074 case 'f':
1075 retval = FP;
1076 break;
1077 case 'a':
1078 retval = AP;
1079 break;
1080 default:
1081 retval = -1;
1082 }
1083 }
1084 else if (c1 == 'p' && c2 == 'c')
1085 retval = PC;
1086 else
1087 retval = -1;
1088 return retval;
1089 }
1090
1091 #ifdef OBJ_AOUT
1092 #ifndef BFD_ASSEMBLER
1093 void
1094 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
1095 char *where;
1096 fixS *fixP;
1097 relax_addressT segment_address_in_file;
1098 {
1099 /*
1100 * In: length of relocation (or of address) in chars: 1, 2 or 4.
1101 * Out: GNU LD relocation length code: 0, 1, or 2.
1102 */
1103
1104 static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
1105 int r_symbolnum;
1106 int r_flags;
1107
1108 know (fixP->fx_addsy != NULL);
1109
1110 md_number_to_chars (where,
1111 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
1112 4);
1113
1114 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
1115 ? S_GET_TYPE (fixP->fx_addsy)
1116 : fixP->fx_addsy->sy_number);
1117 r_flags = (fixP->fx_pcrel ? 1 : 0)
1118 | (!S_IS_DEFINED (fixP->fx_addsy) ? 8 : 0) /* extern */
1119 | ((nbytes_r_length[fixP->fx_size] & 3) << 1);
1120
1121 switch (fixP->fx_r_type) {
1122 case NO_RELOC:
1123 break;
1124 case NO_RELOC2:
1125 if (r_flags & 8)
1126 r_flags |= 0x80; /* setting the copy bit */
1127 /* says we can convert */
1128 /* to gotslot if needed */
1129 break;
1130 case RELOC_32:
1131 if (flag_want_pic && S_IS_EXTERNAL(fixP->fx_addsy)) {
1132 r_symbolnum = fixP->fx_addsy->sy_number;
1133 r_flags |= 8; /* set extern bit */
1134 }
1135 break;
1136 case RELOC_JMP_SLOT:
1137 if (flag_want_pic) {
1138 r_flags |= 0x20; /* set jmptable */
1139 r_flags &= ~0x08; /* clear extern bit */
1140 }
1141 break;
1142 case RELOC_JMP_TBL:
1143 if (flag_want_pic) {
1144 r_flags |= 0x20; /* set jmptable */
1145 r_flags |= 0x08; /* set extern bit */
1146 }
1147 break;
1148 case RELOC_GLOB_DAT:
1149 if (flag_want_pic) {
1150 r_flags |= 0x10; /* set baserel bit */
1151 r_symbolnum = fixP->fx_addsy->sy_number;
1152 if (S_IS_EXTERNAL(fixP->fx_addsy))
1153 r_flags |= 8; /* set extern bit */
1154 }
1155 break;
1156 }
1157
1158 where[4] = (r_symbolnum >> 0) & 0xff;
1159 where[5] = (r_symbolnum >> 8) & 0xff;
1160 where[6] = (r_symbolnum >> 16) & 0xff;
1161 where[7] = r_flags;
1162 }
1163 #endif /* !BFD_ASSEMBLER */
1164 #endif /* OBJ_AOUT */
1165
1166 /* Parse a vax operand in DEC assembler notation.
1167 For speed, expect a string of whitespace to be reduced to a single ' '.
1168 This is the case for GNU AS, and is easy for other DEC-compatible
1169 assemblers.
1170
1171 Knowledge about DEC VAX assembler operand notation lives here.
1172 This doesn't even know what a register name is, except it believes
1173 all register names are 2 or 3 characters, and lets vax_reg_parse() say
1174 what number each name represents.
1175 It does, however, know that PC, SP etc are special registers so it can
1176 detect addressing modes that are silly for those registers.
1177
1178 Where possible, it delivers 1 fatal or 1 warning message if the operand
1179 is suspect. Exactly what we test for is still evolving.
1180
1181 ---
1182 Arg block.
1183
1184 There were a number of 'mismatched argument type' bugs to vip_op.
1185 The most general solution is to typedef each (of many) arguments.
1186 We used instead a typedef'd argument block. This is less modular
1187 than using separate return pointers for each result, but runs faster
1188 on most engines, and seems to keep programmers happy. It will have
1189 to be done properly if we ever want to use vip_op as a general-purpose
1190 module (it was designed to be).
1191
1192 G^
1193
1194 Doesn't support DEC "G^" format operands. These always take 5 bytes
1195 to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
1196 optimising to (say) a "B^" if you are lucky in the way you link.
1197 When someone builds a linker smart enough to convert "G^" to "B^", "W^"
1198 whenever possible, then we should implement it.
1199 If there is some other use for "G^", feel free to code it in!
1200
1201 speed
1202
1203 If I nested if()s more, I could avoid testing (*err) which would save
1204 time, space and page faults. I didn't nest all those if()s for clarity
1205 and because I think the mode testing can be re-arranged 1st to test the
1206 commoner constructs 1st. Does anybody have statistics on this?
1207
1208 error messages
1209
1210 In future, we should be able to 'compose' error messages in a scratch area
1211 and give the user MUCH more informative error messages. Although this takes
1212 a little more code at run-time, it will make this module much more self-
1213 documenting. As an example of what sucks now: most error messages have
1214 hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
1215 the Un*x characters "$`*", that most users will expect from this AS.
1216
1217 ----
1218
1219 The input is a string, ending with '\0'.
1220
1221 We also require a 'hint' of what kind of operand is expected: so
1222 we can remind caller not to write into literals for instance.
1223
1224 The output is a skeletal instruction.
1225
1226 The algorithm has two parts.
1227 1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
1228 2. express the @^#-()+[] as some parameters suited to further analysis.
1229
1230 2nd step is where we detect the googles of possible invalid combinations
1231 a human (or compiler) might write. Note that if we do a half-way
1232 decent assembler, we don't know how long to make (eg) displacement
1233 fields when we first meet them (because they may not have defined values).
1234 So we must wait until we know how many bits are needed for each address,
1235 then we can know both length and opcodes of instructions.
1236 For reason(s) above, we will pass to our caller a 'broken' instruction
1237 of these major components, from which our caller can generate instructions:
1238 - displacement length I^ S^ L^ B^ W^ unspecified
1239 - mode (many)
1240 - register R0-R15 or absent
1241 - index register R0-R15 or absent
1242 - expression text what we don't parse
1243 - error text(s) why we couldn't understand the operand
1244
1245 ----
1246
1247 To decode output of this, test errtxt. If errtxt[0] == '\0', then
1248 we had no errors that prevented parsing. Also, if we ever report
1249 an internal bug, errtxt[0] is set non-zero. So one test tells you
1250 if the other outputs are to be taken seriously.
1251
1252 ----
1253
1254 Dec defines the semantics of address modes (and values)
1255 by a two-letter code, explained here.
1256
1257 letter 1: access type
1258
1259 a address calculation - no data access, registers forbidden
1260 b branch displacement
1261 m read - let go of bus - write back "modify"
1262 r read
1263 v bit field address: like 'a' but registers are OK
1264 w write
1265 space no operator (eg ".long foo") [our convention]
1266
1267 letter 2: data type (i.e. width, alignment)
1268
1269 b byte
1270 d double precision floating point (D format)
1271 f single precision floating point (F format)
1272 g G format floating
1273 h H format floating
1274 l longword
1275 o octaword
1276 q quadword
1277 w word
1278 ? simple synthetic branch operand
1279 - unconditional synthetic JSB/JSR operand
1280 ! complex synthetic branch operand
1281
1282 The '-?!' letter 2's are not for external consumption. They are used
1283 for various assemblers. Generally, all unknown widths are assumed 0.
1284 We don't limit your choice of width character.
1285
1286 DEC operands are hard work to parse. For example, '@' as the first
1287 character means indirect (deferred) mode but elsewhere it is a shift
1288 operator.
1289 The long-winded explanation of how this is supposed to work is
1290 cancelled. Read a DEC vax manual.
1291 We try hard not to parse anything that MIGHT be part of the expression
1292 buried in that syntax. For example if we see @...(Rn) we don't check
1293 for '-' before the '(' because mode @-(Rn) does not exist.
1294
1295 After parsing we have:
1296
1297 at 1 if leading '@' (or Un*x '*')
1298 len takes one value from " bilsw". eg B^ -> 'b'.
1299 hash 1 if leading '#' (or Un*x '$')
1300 expr_begin, expr_end the expression we did not parse
1301 even though we don't interpret it, we make use
1302 of its presence or absence.
1303 sign -1: -(Rn) 0: absent +1: (Rn)+
1304 paren 1 if () are around register
1305 reg major register number 0:15 -1 means absent
1306 ndx index register number 0:15 -1 means absent
1307
1308 Again, I dare not explain it: just trace ALL the code!
1309
1310 Summary of vip_op outputs.
1311
1312 mode reg len ndx
1313 (Rn) => @Rn
1314 {@}Rn 5+@ n ' ' optional
1315 branch operand 0 -1 ' ' -1
1316 S^#foo 0 -1 's' -1
1317 -(Rn) 7 n ' ' optional
1318 {@}(Rn)+ 8+@ n ' ' optional
1319 {@}#foo, no S^ 8+@ PC " i" optional
1320 {@}{q^}{(Rn)} 10+@+q option " bwl" optional */
1321
1322 /* Dissect user-input 'optext' (which is something like "@B^foo@bar(AP)[FP]:")
1323 using the vop in vopP. vopP's vop_access and vop_width. We fill _ndx, _reg,
1324 _mode, _short, _warn, _error, _expr_begin, _expr_end and _nbytes. */
1325
1326 static void
1327 vip_op (char *optext, struct vop *vopP)
1328 {
1329 /* Track operand text forward. */
1330 char *p;
1331 /* Track operand text backward. */
1332 char *q;
1333 /* 1 if leading '@' ('*') seen. */
1334 int at;
1335 /* one of " bilsw" */
1336 char len;
1337 /* 1 if leading '#' ('$') seen. */
1338 int hash;
1339 /* -1, 0 or +1. */
1340 int sign = 0;
1341 /* 1 if () surround register. */
1342 int paren = 0;
1343 /* Register number, -1:absent. */
1344 int reg = 0;
1345 /* Index register number -1:absent. */
1346 int ndx = 0;
1347 /* Report illegal operand, ""==OK. */
1348 /* " " is a FAKE error: means we won. */
1349 /* ANY err that begins with ' ' is a fake. */
1350 /* " " is converted to "" before return. */
1351 const char *err;
1352 /* Warn about weird modes pf address. */
1353 const char *wrn;
1354 /* Preserve q in case we backup. */
1355 char *oldq = NULL;
1356 /* Build up 4-bit operand mode here. */
1357 /* Note: index mode is in ndx, this is. */
1358 /* The major mode of operand address. */
1359 int mode = 0;
1360 /* Notice how we move wrong-arg-type bugs INSIDE this module: if we
1361 get the types wrong below, we lose at compile time rather than at
1362 lint or run time. */
1363 char access_mode; /* vop_access. */
1364
1365 access_mode = vopP->vop_access;
1366 /* None of our code bugs (yet), no user text errors, no warnings
1367 even. */
1368 err = wrn = 0;
1369
1370 p = optext;
1371
1372 if (*p == ' ') /* Expect all whitespace reduced to ' '. */
1373 p++; /* skip over whitespace */
1374
1375 if ((at = INDIRECTP (*p)) != 0)
1376 { /* 1 if *p=='@'(or '*' for Un*x) */
1377 p++; /* at is determined */
1378 if (*p == ' ') /* Expect all whitespace reduced to ' '. */
1379 p++; /* skip over whitespace */
1380 }
1381
1382 /* This code is subtle. It tries to detect all legal (letter)'^'
1383 but it doesn't waste time explicitly testing for premature '\0' because
1384 this case is rejected as a mismatch against either (letter) or '^'. */
1385 {
1386 char c;
1387
1388 c = *p;
1389 c = TOLOWER (c);
1390 if (DISPLENP (p[1]) && strchr ("bilws", len = c))
1391 p += 2; /* Skip (letter) '^'. */
1392 else /* No (letter) '^' seen. */
1393 len = ' '; /* Len is determined. */
1394 }
1395
1396 if (*p == ' ') /* Expect all whitespace reduced to ' '. */
1397 p++;
1398
1399 if ((hash = IMMEDIATEP (*p)) != 0) /* 1 if *p=='#' ('$' for Un*x) */
1400 p++; /* Hash is determined. */
1401
1402 /* p points to what may be the beginning of an expression.
1403 We have peeled off the front all that is peelable.
1404 We know at, len, hash.
1405
1406 Lets point q at the end of the text and parse that (backwards). */
1407
1408 for (q = p; *q; q++)
1409 ;
1410 q--; /* Now q points at last char of text. */
1411
1412 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
1413 q--;
1414
1415 /* Reverse over whitespace, but don't. */
1416 /* Run back over *p. */
1417
1418 /* As a matter of policy here, we look for [Rn], although both Rn and S^#
1419 forbid [Rn]. This is because it is easy, and because only a sick
1420 cyborg would have [...] trailing an expression in a VAX-like assembler.
1421 A meticulous parser would first check for Rn followed by '(' or '['
1422 and not parse a trailing ']' if it found another. We just ban expressions
1423 ending in ']'. */
1424 if (*q == ']')
1425 {
1426 while (q >= p && *q != '[')
1427 q--;
1428 /* Either q<p or we got matching '['. */
1429 if (q < p)
1430 err = _("no '[' to match ']'");
1431 else
1432 {
1433 /* Confusers like "[]" will eventually lose with a bad register
1434 * name error. So again we don't need to check for early '\0'. */
1435 if (q[3] == ']')
1436 ndx = vax_reg_parse (q[1], q[2], 0, 0);
1437 else if (q[4] == ']')
1438 ndx = vax_reg_parse (q[1], q[2], q[3], 0);
1439 else if (q[5] == ']')
1440 ndx = vax_reg_parse (q[1], q[2], q[3], q[4]);
1441 else
1442 ndx = -1;
1443 /* Since we saw a ']' we will demand a register name in the [].
1444 * If luser hasn't given us one: be rude. */
1445 if (ndx < 0)
1446 err = _("bad register in []");
1447 else if (ndx == PC)
1448 err = _("[PC] index banned");
1449 else
1450 /* Point q just before "[...]". */
1451 q--;
1452 }
1453 }
1454 else
1455 /* No ']', so no iNDeX register. */
1456 ndx = -1;
1457
1458 /* If err = "..." then we lost: run away.
1459 Otherwise ndx == -1 if there was no "[...]".
1460 Otherwise, ndx is index register number, and q points before "[...]". */
1461
1462 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
1463 q--;
1464 /* Reverse over whitespace, but don't. */
1465 /* Run back over *p. */
1466 if (!err || !*err)
1467 {
1468 /* no ()+ or -() seen yet */
1469 sign = 0;
1470
1471 if (q > p + 3 && *q == '+' && q[-1] == ')')
1472 {
1473 sign = 1; /* we saw a ")+" */
1474 q--; /* q points to ')' */
1475 }
1476
1477 if (*q == ')' && q > p + 2)
1478 {
1479 paren = 1; /* assume we have "(...)" */
1480 while (q >= p && *q != '(')
1481 q--;
1482 /* either q<p or we got matching '(' */
1483 if (q < p)
1484 err = _("no '(' to match ')'");
1485 else
1486 {
1487 /* Confusers like "()" will eventually lose with a bad register
1488 name error. So again we don't need to check for early '\0'. */
1489 if (q[3] == ')')
1490 reg = vax_reg_parse (q[1], q[2], 0, 0);
1491 else if (q[4] == ')')
1492 reg = vax_reg_parse (q[1], q[2], q[3], 0);
1493 else if (q[5] == ')')
1494 reg = vax_reg_parse (q[1], q[2], q[3], q[4]);
1495 else
1496 reg = -1;
1497 /* Since we saw a ')' we will demand a register name in the ')'.
1498 This is nasty: why can't our hypothetical assembler permit
1499 parenthesised expressions? BECAUSE I AM LAZY! That is why.
1500 Abuse luser if we didn't spy a register name. */
1501 if (reg < 0)
1502 {
1503 /* JF allow parenthesized expressions. I hope this works. */
1504 paren = 0;
1505 while (*q != ')')
1506 q++;
1507 /* err = "unknown register in ()"; */
1508 }
1509 else
1510 q--; /* point just before '(' of "(...)" */
1511 /* If err == "..." then we lost. Run away.
1512 Otherwise if reg >= 0 then we saw (Rn). */
1513 }
1514 /* If err == "..." then we lost.
1515 Otherwise paren==1 and reg = register in "()". */
1516 }
1517 else
1518 paren = 0;
1519 /* If err == "..." then we lost.
1520 Otherwise, q points just before "(Rn)", if any.
1521 If there was a "(...)" then paren==1, and reg is the register. */
1522
1523 /* We should only seek '-' of "-(...)" if:
1524 we saw "(...)" paren == 1
1525 we have no errors so far ! *err
1526 we did not see '+' of "(...)+" sign < 1
1527 We don't check len. We want a specific error message later if
1528 user tries "x^...-(Rn)". This is a feature not a bug. */
1529 if (!err || !*err)
1530 {
1531 if (paren && sign < 1)/* !sign is adequate test */
1532 {
1533 if (*q == '-')
1534 {
1535 sign = -1;
1536 q--;
1537 }
1538 }
1539 /* We have back-tracked over most
1540 of the crud at the end of an operand.
1541 Unless err, we know: sign, paren. If paren, we know reg.
1542 The last case is of an expression "Rn".
1543 This is worth hunting for if !err, !paren.
1544 We wouldn't be here if err.
1545 We remember to save q, in case we didn't want "Rn" anyway. */
1546 if (!paren)
1547 {
1548 if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
1549 q--;
1550 /* Reverse over whitespace, but don't. */
1551 /* Run back over *p. */
1552 /* Room for Rn or Rnn (include prefix) exactly? */
1553 if (q > p && q < p + 4)
1554 reg = vax_reg_parse (p[0], p[1],
1555 q < p + 2 ? 0 : p[2],
1556 q < p + 3 ? 0 : p[3]);
1557 else
1558 reg = -1; /* Always comes here if no register at all. */
1559 /* Here with a definitive reg value. */
1560 if (reg >= 0)
1561 {
1562 oldq = q;
1563 q = p - 1;
1564 }
1565 }
1566 }
1567 }
1568 /* have reg. -1:absent; else 0:15. */
1569
1570 /* We have: err, at, len, hash, ndx, sign, paren, reg.
1571 Also, any remaining expression is from *p through *q inclusive.
1572 Should there be no expression, q==p-1. So expression length = q-p+1.
1573 This completes the first part: parsing the operand text. */
1574
1575 /* We now want to boil the data down, checking consistency on the way.
1577 We want: len, mode, reg, ndx, err, p, q, wrn, bug.
1578 We will deliver a 4-bit reg, and a 4-bit mode. */
1579
1580 /* Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
1581
1582 in: at ?
1583 len ?
1584 hash ?
1585 p:q ?
1586 sign ?
1587 paren ?
1588 reg ?
1589 ndx ?
1590
1591 out: mode 0
1592 reg -1
1593 len ' '
1594 p:q whatever was input
1595 ndx -1
1596 err " " or error message, and other outputs trashed. */
1597 /* Branch operands have restricted forms. */
1598 if ((!err || !*err) && access_mode == 'b')
1599 {
1600 if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
1601 err = _("invalid branch operand");
1602 else
1603 err = " ";
1604 }
1605
1606 /* Since nobody seems to use it: comment this 'feature'(?) out for now. */
1607 #ifdef NEVER
1608 /* Case of stand-alone operand. e.g. ".long foo"
1609
1610 in: at ?
1611 len ?
1612 hash ?
1613 p:q ?
1614 sign ?
1615 paren ?
1616 reg ?
1617 ndx ?
1618
1619 out: mode 0
1620 reg -1
1621 len ' '
1622 p:q whatever was input
1623 ndx -1
1624 err " " or error message, and other outputs trashed. */
1625 if ((!err || !*err) && access_mode == ' ')
1626 {
1627 if (at)
1628 err = _("address prohibits @");
1629 else if (hash)
1630 err = _("address prohibits #");
1631 else if (sign)
1632 {
1633 if (sign < 0)
1634 err = _("address prohibits -()");
1635 else
1636 err = _("address prohibits ()+");
1637 }
1638 else if (paren)
1639 err = _("address prohibits ()");
1640 else if (ndx >= 0)
1641 err = _("address prohibits []");
1642 else if (reg >= 0)
1643 err = _("address prohibits register");
1644 else if (len != ' ')
1645 err = _("address prohibits displacement length specifier");
1646 else
1647 {
1648 err = " "; /* succeed */
1649 mode = 0;
1650 }
1651 }
1652 #endif
1653
1654 /* Case of S^#.
1655
1656 in: at 0
1657 len 's' definition
1658 hash 1 demand
1659 p:q demand not empty
1660 sign 0 by paren==0
1661 paren 0 by "()" scan logic because "S^" seen
1662 reg -1 or nn by mistake
1663 ndx -1
1664
1665 out: mode 0
1666 reg -1
1667 len 's'
1668 exp
1669 ndx -1 */
1670 if ((!err || !*err) && len == 's')
1671 {
1672 if (!hash || paren || at || ndx >= 0)
1673 err = _("invalid operand of S^#");
1674 else
1675 {
1676 if (reg >= 0)
1677 {
1678 /* Darn! we saw S^#Rnn ! put the Rnn back in
1679 expression. KLUDGE! Use oldq so we don't
1680 need to know exact length of reg name. */
1681 q = oldq;
1682 reg = 0;
1683 }
1684 /* We have all the expression we will ever get. */
1685 if (p > q)
1686 err = _("S^# needs expression");
1687 else if (access_mode == 'r')
1688 {
1689 err = " "; /* WIN! */
1690 mode = 0;
1691 }
1692 else
1693 err = _("S^# may only read-access");
1694 }
1695 }
1696
1697 /* Case of -(Rn), which is weird case.
1698
1699 in: at 0
1700 len '
1701 hash 0
1702 p:q q<p
1703 sign -1 by definition
1704 paren 1 by definition
1705 reg present by definition
1706 ndx optional
1707
1708 out: mode 7
1709 reg present
1710 len ' '
1711 exp "" enforce empty expression
1712 ndx optional warn if same as reg. */
1713 if ((!err || !*err) && sign < 0)
1714 {
1715 if (len != ' ' || hash || at || p <= q)
1716 err = _("invalid operand of -()");
1717 else
1718 {
1719 err = " "; /* win */
1720 mode = 7;
1721 if (reg == PC)
1722 wrn = _("-(PC) unpredictable");
1723 else if (reg == ndx)
1724 wrn = _("[]index same as -()register: unpredictable");
1725 }
1726 }
1727
1728 /* We convert "(Rn)" to "@Rn" for our convenience.
1729 (I hope this is convenient: has someone got a better way to parse this?)
1730 A side-effect of this is that "@Rn" is a valid operand. */
1731 if (paren && !sign && !hash && !at && len == ' ' && p > q)
1732 {
1733 at = 1;
1734 paren = 0;
1735 }
1736
1737 /* Case of (Rn)+, which is slightly different.
1738
1739 in: at
1740 len ' '
1741 hash 0
1742 p:q q<p
1743 sign +1 by definition
1744 paren 1 by definition
1745 reg present by definition
1746 ndx optional
1747
1748 out: mode 8+@
1749 reg present
1750 len ' '
1751 exp "" enforce empty expression
1752 ndx optional warn if same as reg. */
1753 if ((!err || !*err) && sign > 0)
1754 {
1755 if (len != ' ' || hash || p <= q)
1756 err = _("invalid operand of ()+");
1757 else
1758 {
1759 err = " "; /* win */
1760 mode = 8 + (at ? 1 : 0);
1761 if (reg == PC)
1762 wrn = _("(PC)+ unpredictable");
1763 else if (reg == ndx)
1764 wrn = _("[]index same as ()+register: unpredictable");
1765 }
1766 }
1767
1768 /* Case of #, without S^.
1769
1770 in: at
1771 len ' ' or 'i'
1772 hash 1 by definition
1773 p:q
1774 sign 0
1775 paren 0
1776 reg absent
1777 ndx optional
1778
1779 out: mode 8+@
1780 reg PC
1781 len ' ' or 'i'
1782 exp
1783 ndx optional. */
1784 if ((!err || !*err) && hash)
1785 {
1786 if (len != 'i' && len != ' ')
1787 err = _("# conflicts length");
1788 else if (paren)
1789 err = _("# bars register");
1790 else
1791 {
1792 if (reg >= 0)
1793 {
1794 /* Darn! we saw #Rnn! Put the Rnn back into the expression.
1795 By using oldq, we don't need to know how long Rnn was.
1796 KLUDGE! */
1797 q = oldq;
1798 reg = -1; /* No register any more. */
1799 }
1800 err = " "; /* Win. */
1801
1802 /* JF a bugfix, I think! */
1803 if (at && access_mode == 'a')
1804 vopP->vop_nbytes = 4;
1805
1806 mode = (at ? 9 : 8);
1807 reg = PC;
1808 if ((access_mode == 'm' || access_mode == 'w') && !at)
1809 wrn = _("writing or modifying # is unpredictable");
1810 }
1811 }
1812 /* If !*err, then sign == 0
1813 hash == 0 */
1814
1815 /* Case of Rn. We separate this one because it has a few special
1816 errors the remaining modes lack.
1817
1818 in: at optional
1819 len ' '
1820 hash 0 by program logic
1821 p:q empty
1822 sign 0 by program logic
1823 paren 0 by definition
1824 reg present by definition
1825 ndx optional
1826
1827 out: mode 5+@
1828 reg present
1829 len ' ' enforce no length
1830 exp "" enforce empty expression
1831 ndx optional warn if same as reg. */
1832 if ((!err || !*err) && !paren && reg >= 0)
1833 {
1834 if (len != ' ')
1835 err = _("length not needed");
1836 else if (at)
1837 {
1838 err = " "; /* win */
1839 mode = 6; /* @Rn */
1840 }
1841 else if (ndx >= 0)
1842 err = _("can't []index a register, because it has no address");
1843 else if (access_mode == 'a')
1844 err = _("a register has no address");
1845 else
1846 {
1847 /* Idea here is to detect from length of datum
1848 and from register number if we will touch PC.
1849 Warn if we do.
1850 vop_nbytes is number of bytes in operand.
1851 Compute highest byte affected, compare to PC0. */
1852 if ((vopP->vop_nbytes + reg * 4) > 60)
1853 wrn = _("PC part of operand unpredictable");
1854 err = " "; /* win */
1855 mode = 5; /* Rn */
1856 }
1857 }
1858 /* If !*err, sign == 0
1859 hash == 0
1860 paren == 1 OR reg==-1 */
1861
1862 /* Rest of cases fit into one bunch.
1863
1864 in: at optional
1865 len ' ' or 'b' or 'w' or 'l'
1866 hash 0 by program logic
1867 p:q expected (empty is not an error)
1868 sign 0 by program logic
1869 paren optional
1870 reg optional
1871 ndx optional
1872
1873 out: mode 10 + @ + len
1874 reg optional
1875 len ' ' or 'b' or 'w' or 'l'
1876 exp maybe empty
1877 ndx optional warn if same as reg. */
1878 if (!err || !*err)
1879 {
1880 err = " "; /* win (always) */
1881 mode = 10 + (at ? 1 : 0);
1882 switch (len)
1883 {
1884 case 'l':
1885 mode += 2;
1886 /* Fall through. */
1887 case 'w':
1888 mode += 2;
1889 /* Fall through. */
1890 case ' ': /* Assumed B^ until our caller changes it. */
1891 case 'b':
1892 break;
1893 }
1894 }
1895
1896 /* here with completely specified mode
1897 len
1898 reg
1899 expression p,q
1900 ndx. */
1901
1902 if (*err == ' ')
1903 err = 0; /* " " is no longer an error. */
1904
1905 vopP->vop_mode = mode;
1906 vopP->vop_reg = reg;
1907 vopP->vop_short = len;
1908 vopP->vop_expr_begin = p;
1909 vopP->vop_expr_end = q;
1910 vopP->vop_ndx = ndx;
1911 vopP->vop_error = err;
1912 vopP->vop_warn = wrn;
1913 }
1914
1915 /* This converts a string into a vax instruction.
1916 The string must be a bare single instruction in dec-vax (with BSD4 frobs)
1917 format.
1918 It provides some error messages: at most one fatal error message (which
1919 stops the scan) and at most one warning message for each operand.
1920 The vax instruction is returned in exploded form, since we have no
1921 knowledge of how you parse (or evaluate) your expressions.
1922 We do however strip off and decode addressing modes and operation
1923 mnemonic.
1924
1925 The exploded instruction is returned to a struct vit of your choice.
1926 #include "vax-inst.h" to know what a struct vit is.
1927
1928 This function's value is a string. If it is not "" then an internal
1929 logic error was found: read this code to assign meaning to the string.
1930 No argument string should generate such an error string:
1931 it means a bug in our code, not in the user's text.
1932
1933 You MUST have called vip_begin() once before using this function. */
1934
1935 static void
1936 vip (struct vit *vitP, /* We build an exploded instruction here. */
1937 char *instring) /* Text of a vax instruction: we modify. */
1938 {
1939 /* How to bit-encode this opcode. */
1940 struct vot_wot *vwP;
1941 /* 1/skip whitespace.2/scan vot_how */
1942 char *p;
1943 char *q;
1944 /* counts number of operands seen */
1945 unsigned char count;
1946 /* scan operands in struct vit */
1947 struct vop *operandp;
1948 /* error over all operands */
1949 const char *alloperr;
1950 /* Remember char, (we clobber it with '\0' temporarily). */
1951 char c;
1952 /* Op-code of this instruction. */
1953 vax_opcodeT oc;
1954
1955 if (*instring == ' ')
1956 ++instring;
1957
1958 /* MUST end in end-of-string or exactly 1 space. */
1959 for (p = instring; *p && *p != ' '; p++)
1960 ;
1961
1962 /* Scanned up to end of operation-code. */
1963 /* Operation-code is ended with whitespace. */
1964 if (p - instring == 0)
1965 {
1966 vitP->vit_error = _("No operator");
1967 count = 0;
1968 memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
1969 }
1970 else
1971 {
1972 c = *p;
1973 *p = '\0';
1974 /* Here with instring pointing to what better be an op-name, and p
1975 pointing to character just past that.
1976 We trust instring points to an op-name, with no whitespace. */
1977 vwP = (struct vot_wot *) str_hash_find (op_hash, instring);
1978 /* Restore char after op-code. */
1979 *p = c;
1980 if (vwP == 0)
1981 {
1982 vitP->vit_error = _("Unknown operator");
1983 count = 0;
1984 memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
1985 }
1986 else
1987 {
1988 /* We found a match! So let's pick up as many operands as the
1989 instruction wants, and even gripe if there are too many.
1990 We expect comma to separate each operand.
1991 We let instring track the text, while p tracks a part of the
1992 struct vot. */
1993 const char *howp;
1994 /* The lines below know about 2-byte opcodes starting FD,FE or FF.
1995 They also understand synthetic opcodes. Note:
1996 we return 32 bits of opcode, including bucky bits, BUT
1997 an opcode length is either 8 or 16 bits for vit_opcode_nbytes. */
1998 oc = vwP->vot_code; /* The op-code. */
1999 vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
2000 md_number_to_chars (vitP->vit_opcode, oc, 4);
2001 count = 0; /* No operands seen yet. */
2002 instring = p; /* Point just past operation code. */
2003 alloperr = "";
2004 for (howp = vwP->vot_how, operandp = vitP->vit_operand;
2005 !(alloperr && *alloperr) && *howp;
2006 operandp++, howp += 2)
2007 {
2008 /* Here to parse one operand. Leave instring pointing just
2009 past any one ',' that marks the end of this operand. */
2010 if (!howp[1])
2011 as_fatal (_("odd number of bytes in operand description"));
2012 else if (*instring)
2013 {
2014 for (q = instring; (c = *q) && c != ','; q++)
2015 ;
2016 /* Q points to ',' or '\0' that ends argument. C is that
2017 character. */
2018 *q = 0;
2019 operandp->vop_width = howp[1];
2020 operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
2021 operandp->vop_access = howp[0];
2022 vip_op (instring, operandp);
2023 *q = c; /* Restore input text. */
2024 if (operandp->vop_error)
2025 alloperr = _("Bad operand");
2026 instring = q + (c ? 1 : 0); /* Next operand (if any). */
2027 count++; /* Won another argument, may have an operr. */
2028 }
2029 else
2030 alloperr = _("Not enough operands");
2031 }
2032 if (!*alloperr)
2033 {
2034 if (*instring == ' ')
2035 instring++;
2036 if (*instring)
2037 alloperr = _("Too many operands");
2038 }
2039 vitP->vit_error = alloperr;
2040 }
2041 }
2042 vitP->vit_operands = count;
2043 }
2044
2045 #ifdef test
2047
2048 /* Test program for above. */
2049
2050 struct vit myvit; /* Build an exploded vax instruction here. */
2051 char answer[100]; /* Human types a line of vax assembler here. */
2052 char *mybug; /* "" or an internal logic diagnostic. */
2053 int mycount; /* Number of operands. */
2054 struct vop *myvop; /* Scan operands from myvit. */
2055 int mysynth; /* 1 means want synthetic opcodes. */
2056 char my_immediate[200];
2057 char my_indirect[200];
2058 char my_displen[200];
2059
2060 int
2061 main (void)
2062 {
2063 char *p;
2064
2065 printf ("0 means no synthetic instructions. ");
2066 printf ("Value for vip_begin? ");
2067 gets (answer);
2068 sscanf (answer, "%d", &mysynth);
2069 printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
2070 printf ("enter immediate symbols eg enter # ");
2071 gets (my_immediate);
2072 printf ("enter indirect symbols eg enter @ ");
2073 gets (my_indirect);
2074 printf ("enter displen symbols eg enter ^ ");
2075 gets (my_displen);
2076
2077 vip_begin (mysynth, my_immediate, my_indirect, my_displen)
2078
2079 printf ("An empty input line will quit you from the vax instruction parser\n");
2080 for (;;)
2081 {
2082 printf ("vax instruction: ");
2083 fflush (stdout);
2084 gets (answer);
2085 if (!*answer)
2086 break; /* Out of for each input text loop. */
2087
2088 vip (& myvit, answer);
2089 if (*myvit.vit_error)
2090 printf ("ERR:\"%s\"\n", myvit.vit_error);
2091
2092 printf ("opcode=");
2093 for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
2094 mycount;
2095 mycount--, p++)
2096 printf ("%02x ", *p & 0xFF);
2097
2098 printf (" operand count=%d.\n", mycount = myvit.vit_operands);
2099 for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
2100 {
2101 printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
2102 myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
2103 myvop->vop_short, myvop->vop_access, myvop->vop_width,
2104 myvop->vop_nbytes);
2105 for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
2106 putchar (*p);
2107
2108 printf ("\"\n");
2109 if (myvop->vop_error)
2110 printf (" err:\"%s\"\n", myvop->vop_error);
2111
2112 if (myvop->vop_warn)
2113 printf (" wrn:\"%s\"\n", myvop->vop_warn);
2114 }
2115 }
2116 vip_end ();
2117 exit (EXIT_SUCCESS);
2118 }
2119
2120 #endif
2121
2122 #ifdef TEST /* #Define to use this testbed. */
2124
2125 /* Follows a test program for this function.
2126 We declare arrays non-local in case some of our tiny-minded machines
2127 default to small stacks. Also, helps with some debuggers. */
2128
2129 char answer[100]; /* Human types into here. */
2130 char *p; /* */
2131 char *myerr;
2132 char *mywrn;
2133 char *mybug;
2134 char myaccess;
2135 char mywidth;
2136 char mymode;
2137 char myreg;
2138 char mylen;
2139 char *myleft;
2140 char *myright;
2141 char myndx;
2142 int my_operand_length;
2143 char my_immediate[200];
2144 char my_indirect[200];
2145 char my_displen[200];
2146
2147 int
2148 main (void)
2149 {
2150 printf ("enter immediate symbols eg enter # ");
2151 gets (my_immediate);
2152 printf ("enter indirect symbols eg enter @ ");
2153 gets (my_indirect);
2154 printf ("enter displen symbols eg enter ^ ");
2155 gets (my_displen);
2156 vip_op_defaults (my_immediate, my_indirect, my_displen);
2157
2158 for (;;)
2159 {
2160 printf ("access,width (eg 'ab' or 'wh') [empty line to quit] : ");
2161 fflush (stdout);
2162 gets (answer);
2163 if (!answer[0])
2164 exit (EXIT_SUCCESS);
2165 myaccess = answer[0];
2166 mywidth = answer[1];
2167 switch (mywidth)
2168 {
2169 case 'b':
2170 my_operand_length = 1;
2171 break;
2172 case 'd':
2173 my_operand_length = 8;
2174 break;
2175 case 'f':
2176 my_operand_length = 4;
2177 break;
2178 case 'g':
2179 my_operand_length = 16;
2180 break;
2181 case 'h':
2182 my_operand_length = 32;
2183 break;
2184 case 'l':
2185 my_operand_length = 4;
2186 break;
2187 case 'o':
2188 my_operand_length = 16;
2189 break;
2190 case 'q':
2191 my_operand_length = 8;
2192 break;
2193 case 'w':
2194 my_operand_length = 2;
2195 break;
2196 case '!':
2197 case '?':
2198 case '-':
2199 my_operand_length = 0;
2200 break;
2201
2202 default:
2203 my_operand_length = 2;
2204 printf ("I don't understand access width %c\n", mywidth);
2205 break;
2206 }
2207 printf ("VAX assembler instruction operand: ");
2208 fflush (stdout);
2209 gets (answer);
2210 mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
2211 &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
2212 &myerr, &mywrn);
2213 if (*myerr)
2214 {
2215 printf ("error: \"%s\"\n", myerr);
2216 if (*mybug)
2217 printf (" bug: \"%s\"\n", mybug);
2218 }
2219 else
2220 {
2221 if (*mywrn)
2222 printf ("warning: \"%s\"\n", mywrn);
2223 mumble ("mode", mymode);
2224 mumble ("register", myreg);
2225 mumble ("index", myndx);
2226 printf ("width:'%c' ", mylen);
2227 printf ("expression: \"");
2228 while (myleft <= myright)
2229 putchar (*myleft++);
2230 printf ("\"\n");
2231 }
2232 }
2233 }
2234
2235 void
2236 mumble (char *text, int value)
2237 {
2238 printf ("%s:", text);
2239 if (value >= 0)
2240 printf ("%xx", value);
2241 else
2242 printf ("ABSENT");
2243 printf (" ");
2244 }
2245
2246 #endif
2247
2248 int md_short_jump_size = 3;
2249 int md_long_jump_size = 6;
2250
2251 void
2252 md_create_short_jump (char *ptr,
2253 addressT from_addr,
2254 addressT to_addr ATTRIBUTE_UNUSED,
2255 fragS *frag ATTRIBUTE_UNUSED,
2256 symbolS *to_symbol ATTRIBUTE_UNUSED)
2257 {
2258 valueT offset;
2259
2260 /* This former calculation was off by two:
2261 offset = to_addr - (from_addr + 1);
2262 We need to account for the one byte instruction and also its
2263 two byte operand. */
2264 offset = to_addr - (from_addr + 1 + 2);
2265 *ptr++ = VAX_BRW; /* Branch with word (16 bit) offset. */
2266 md_number_to_chars (ptr, offset, 2);
2267 }
2268
2269 void
2270 md_create_long_jump (char *ptr,
2271 addressT from_addr,
2272 addressT to_addr,
2273 fragS *frag ATTRIBUTE_UNUSED,
2274 symbolS *to_symbol ATTRIBUTE_UNUSED)
2275 {
2276 valueT offset;
2277
2278 /* Account for 1 byte instruction, 1 byte of address specifier and
2279 4 bytes of offset from PC. */
2280 offset = to_addr - (from_addr + 1 + 1 + 4);
2281 *ptr++ = VAX_JMP;
2282 *ptr++ = VAX_PC_RELATIVE_MODE;
2283 md_number_to_chars (ptr, offset, 4);
2284 }
2285
2286 #ifdef OBJ_VMS
2288 const char *md_shortopts = "d:STt:V+1h:Hv::";
2289 #elif defined(OBJ_ELF)
2290 const char *md_shortopts = "d:STt:VkKQ:";
2291 #else
2292 const char *md_shortopts = "d:STt:V";
2293 #endif
2294 struct option md_longopts[] =
2295 {
2296 #ifdef OBJ_ELF
2297 #define OPTION_PIC (OPTION_MD_BASE)
2298 { "pic", no_argument, NULL, OPTION_PIC },
2299 #endif
2300 { NULL, no_argument, NULL, 0 }
2301 };
2302 size_t md_longopts_size = sizeof (md_longopts);
2303
2304 int
2305 md_parse_option (int c, const char *arg)
2306 {
2307 switch (c)
2308 {
2309 case 'S':
2310 as_warn (_("SYMBOL TABLE not implemented"));
2311 break;
2312
2313 case 'T':
2314 as_warn (_("TOKEN TRACE not implemented"));
2315 break;
2316
2317 case 'd':
2318 as_warn (_("Displacement length %s ignored!"), arg);
2319 break;
2320
2321 case 't':
2322 as_warn (_("I don't need or use temp. file \"%s\"."), arg);
2323 break;
2324
2325 case 'V':
2326 as_warn (_("I don't use an interpass file! -V ignored"));
2327 break;
2328
2329 #ifdef OBJ_VMS
2330 case '+': /* For g++. Hash any name > 31 chars long. */
2331 flag_hash_long_names = 1;
2332 break;
2333
2334 case '1': /* For backward compatibility. */
2335 flag_one = 1;
2336 break;
2337
2338 case 'H': /* Show new symbol after hash truncation. */
2339 flag_show_after_trunc = 1;
2340 break;
2341
2342 case 'h': /* No hashing of mixed-case names. */
2343 {
2344 extern char vms_name_mapping;
2345 vms_name_mapping = atoi (arg);
2346 flag_no_hash_mixed_case = 1;
2347 }
2348 break;
2349
2350 case 'v':
2351 {
2352 extern char *compiler_version_string;
2353
2354 if (!arg || !*arg || access (arg, 0) == 0)
2355 return 0; /* Have caller show the assembler version. */
2356 compiler_version_string = arg;
2357 }
2358 break;
2359 #endif
2360
2361 #ifdef OBJ_ELF
2362 case OPTION_PIC:
2363 case 'k':
2364 flag_want_pic = 1;
2365 break; /* -pic, Position Independent Code. */
2366
2367 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment
2368 section should be emitted or not. FIXME: Not implemented. */
2369 case 'Q':
2370 break;
2371 #endif
2372
2373 default:
2374 return 0;
2375 }
2376
2377 return 1;
2378 }
2379
2380 void
2381 md_show_usage (FILE *stream)
2382 {
2383 fprintf (stream, _("\
2384 VAX options:\n\
2385 -d LENGTH ignored\n\
2386 -J ignored\n\
2387 -S ignored\n\
2388 -t FILE ignored\n\
2389 -T ignored\n\
2390 -V ignored\n"));
2391 #ifdef OBJ_VMS
2392 fprintf (stream, _("\
2393 VMS options:\n\
2394 -+ hash encode names longer than 31 characters\n\
2395 -1 `const' handling compatible with gcc 1.x\n\
2396 -H show new symbol after hash truncation\n\
2397 -h NUM don't hash mixed-case names, and adjust case:\n\
2398 0 = upper, 2 = lower, 3 = preserve case\n\
2399 -v\"VERSION\" code being assembled was produced by compiler \"VERSION\"\n"));
2400 #endif
2401 }
2402
2403 /* We have no need to default values of symbols. */
2405
2406 symbolS *
2407 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2408 {
2409 return NULL;
2410 }
2411
2412 /* Round up a section size to the appropriate boundary. */
2413 valueT
2414 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
2415 {
2416 /* Byte alignment is fine */
2417 return size;
2418 }
2419
2420 /* Exactly what point is a PC-relative offset relative TO?
2421 On the vax, they're relative to the address of the offset, plus
2422 its size. */
2423 long
2424 md_pcrel_from (fixS *fixP)
2425 {
2426 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2427 }
2428
2429 arelent *
2430 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2431 {
2432 arelent *reloc;
2433 bfd_reloc_code_real_type code;
2434
2435 if (fixp->fx_tcbit)
2436 abort ();
2437
2438 if (fixp->fx_r_type != NO_RELOC)
2439 {
2440 code = fixp->fx_r_type;
2441
2442 if (fixp->fx_pcrel)
2443 {
2444 switch (code)
2445 {
2446 case BFD_RELOC_8_PCREL:
2447 case BFD_RELOC_16_PCREL:
2448 case BFD_RELOC_32_PCREL:
2449 #ifdef OBJ_ELF
2450 case BFD_RELOC_8_GOT_PCREL:
2451 case BFD_RELOC_16_GOT_PCREL:
2452 case BFD_RELOC_32_GOT_PCREL:
2453 case BFD_RELOC_8_PLT_PCREL:
2454 case BFD_RELOC_16_PLT_PCREL:
2455 case BFD_RELOC_32_PLT_PCREL:
2456 #endif
2457 break;
2458 default:
2459 as_bad_where (fixp->fx_file, fixp->fx_line,
2460 _("Cannot make %s relocation PC relative"),
2461 bfd_get_reloc_code_name (code));
2462 }
2463 }
2464 }
2465 else
2466 {
2467 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
2468 switch (F (fixp->fx_size, fixp->fx_pcrel))
2469 {
2470 #define MAP(SZ,PCREL,TYPE) case F(SZ,PCREL): code = (TYPE); break
2471 MAP (1, 0, BFD_RELOC_8);
2472 MAP (2, 0, BFD_RELOC_16);
2473 MAP (4, 0, BFD_RELOC_32);
2474 MAP (1, 1, BFD_RELOC_8_PCREL);
2475 MAP (2, 1, BFD_RELOC_16_PCREL);
2476 MAP (4, 1, BFD_RELOC_32_PCREL);
2477 default:
2478 abort ();
2479 }
2480 }
2481 #undef F
2482 #undef MAP
2483
2484 reloc = XNEW (arelent);
2485 reloc->sym_ptr_ptr = XNEW (asymbol *);
2486 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2487 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2488 #ifndef OBJ_ELF
2489 if (fixp->fx_pcrel)
2490 reloc->addend = fixp->fx_addnumber;
2491 else
2492 reloc->addend = 0;
2493 #else
2494 reloc->addend = fixp->fx_offset;
2495 #endif
2496
2497 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2498 gas_assert (reloc->howto != 0);
2499
2500 return reloc;
2501 }
2502
2503 /* vax:md_assemble() emit frags for 1 instruction given in textual form. */
2504 void
2505 md_assemble (char *instruction_string)
2506 {
2507 /* Non-zero if operand expression's segment is not known yet. */
2508 int is_undefined;
2509 /* Non-zero if operand expression's segment is absolute. */
2510 int is_absolute;
2511 int length_code;
2512 char *p;
2513 /* An operand. Scans all operands. */
2514 struct vop *operandP;
2515 char *save_input_line_pointer;
2516 /* What used to live after an expression. */
2517 char c_save;
2518 /* 1: instruction_string bad for all passes. */
2519 int goofed;
2520 /* Points to slot just after last operand. */
2521 struct vop *end_operandP;
2522 /* Points to expression values for this operand. */
2523 expressionS *expP;
2524 segT *segP;
2525
2526 /* These refer to an instruction operand expression. */
2527 /* Target segment of the address. */
2528 segT to_seg;
2529 valueT this_add_number;
2530 /* Positive (minuend) symbol. */
2531 symbolS *this_add_symbol;
2532 /* As a number. */
2533 long opcode_as_number;
2534 /* Least significant byte 1st. */
2535 char *opcode_as_chars;
2536 /* As an array of characters. */
2537 /* Least significant byte 1st */
2538 char *opcode_low_byteP;
2539 /* length (bytes) meant by vop_short. */
2540 int length;
2541 /* 0, or 1 if '@' is in addressing mode. */
2542 int at;
2543 /* From vop_nbytes: vax_operand_width (in bytes) */
2544 int nbytes;
2545 FLONUM_TYPE *floatP;
2546 LITTLENUM_TYPE literal_float[8];
2547 /* Big enough for any floating point literal. */
2548
2549 vip (&v, instruction_string);
2550
2551 /* Now we try to find as many as_warn()s as we can. If we do any as_warn()s
2552 then goofed=1. Notice that we don't make any frags yet.
2553 Should goofed be 1, then this instruction will wedge in any pass,
2554 and we can safely flush it, without causing interpass symbol phase
2555 errors. That is, without changing label values in different passes. */
2556 if ((goofed = (*v.vit_error)) != 0)
2557 {
2558 as_fatal (_("Ignoring statement due to \"%s\""), v.vit_error);
2559 }
2560 /* We need to use expression() and friends, which require us to diddle
2561 input_line_pointer. So we save it and restore it later. */
2562 save_input_line_pointer = input_line_pointer;
2563 for (operandP = v.vit_operand,
2564 expP = exp_of_operand,
2565 segP = seg_of_operand,
2566 floatP = float_operand,
2567 end_operandP = v.vit_operand + v.vit_operands;
2568
2569 operandP < end_operandP;
2570
2571 operandP++, expP++, segP++, floatP++)
2572 {
2573 if (operandP->vop_error)
2574 {
2575 as_fatal (_("Aborting because statement has \"%s\""), operandP->vop_error);
2576 goofed = 1;
2577 }
2578 else
2579 {
2580 /* Statement has no syntax goofs: let's sniff the expression. */
2581 int can_be_short = 0; /* 1 if a bignum can be reduced to a short literal. */
2582
2583 input_line_pointer = operandP->vop_expr_begin;
2584 c_save = operandP->vop_expr_end[1];
2585 operandP->vop_expr_end[1] = '\0';
2586 /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1. */
2587 *segP = expression (expP);
2588 switch (expP->X_op)
2589 {
2590 case O_absent:
2591 /* for BSD4.2 compatibility, missing expression is absolute 0 */
2592 expP->X_op = O_constant;
2593 expP->X_add_number = 0;
2594 /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
2595 X_add_symbol to any particular value. But, we will program
2596 defensively. Since this situation occurs rarely so it costs
2597 us little to do, and stops Dean worrying about the origin of
2598 random bits in expressionS's. */
2599 expP->X_add_symbol = NULL;
2600 expP->X_op_symbol = NULL;
2601 break;
2602
2603 case O_symbol:
2604 case O_constant:
2605 break;
2606
2607 default:
2608 /* Major bug. We can't handle the case of a
2609 SEG_OP expression in a VIT_OPCODE_SYNTHETIC
2610 variable-length instruction.
2611 We don't have a frag type that is smart enough to
2612 relax a SEG_OP, and so we just force all
2613 SEG_OPs to behave like SEG_PASS1s.
2614 Clearly, if there is a demand we can invent a new or
2615 modified frag type and then coding up a frag for this
2616 case will be easy. SEG_OP was invented for the
2617 .words after a CASE opcode, and was never intended for
2618 instruction operands. */
2619 need_pass_2 = 1;
2620 as_fatal (_("Can't relocate expression"));
2621 break;
2622
2623 case O_big:
2624 /* Preserve the bits. */
2625 if (expP->X_add_number > 0)
2626 {
2627 bignum_copy (generic_bignum, expP->X_add_number,
2628 floatP->low, SIZE_OF_LARGE_NUMBER);
2629 }
2630 else
2631 {
2632 know (expP->X_add_number < 0);
2633 flonum_copy (&generic_floating_point_number,
2634 floatP);
2635 if (strchr ("s i", operandP->vop_short))
2636 {
2637 /* Could possibly become S^# */
2638 flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
2639 switch (-expP->X_add_number)
2640 {
2641 case 'f':
2642 can_be_short =
2643 (literal_float[0] & 0xFC0F) == 0x4000
2644 && literal_float[1] == 0;
2645 break;
2646
2647 case 'd':
2648 can_be_short =
2649 (literal_float[0] & 0xFC0F) == 0x4000
2650 && literal_float[1] == 0
2651 && literal_float[2] == 0
2652 && literal_float[3] == 0;
2653 break;
2654
2655 case 'g':
2656 can_be_short =
2657 (literal_float[0] & 0xFF81) == 0x4000
2658 && literal_float[1] == 0
2659 && literal_float[2] == 0
2660 && literal_float[3] == 0;
2661 break;
2662
2663 case 'h':
2664 can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
2665 && (literal_float[1] & 0xE000) == 0
2666 && literal_float[2] == 0
2667 && literal_float[3] == 0
2668 && literal_float[4] == 0
2669 && literal_float[5] == 0
2670 && literal_float[6] == 0
2671 && literal_float[7] == 0);
2672 break;
2673
2674 default:
2675 BAD_CASE (-expP->X_add_number);
2676 break;
2677 }
2678 }
2679 }
2680
2681 if (operandP->vop_short == 's'
2682 || operandP->vop_short == 'i'
2683 || (operandP->vop_short == ' '
2684 && operandP->vop_reg == 0xF
2685 && (operandP->vop_mode & 0xE) == 0x8))
2686 {
2687 /* Saw a '#'. */
2688 if (operandP->vop_short == ' ')
2689 {
2690 /* We must chose S^ or I^. */
2691 if (expP->X_add_number > 0)
2692 {
2693 /* Bignum: Short literal impossible. */
2694 operandP->vop_short = 'i';
2695 operandP->vop_mode = 8;
2696 operandP->vop_reg = 0xF; /* VAX PC. */
2697 }
2698 else
2699 {
2700 /* Flonum: Try to do it. */
2701 if (can_be_short)
2702 {
2703 operandP->vop_short = 's';
2704 operandP->vop_mode = 0;
2705 operandP->vop_ndx = -1;
2706 operandP->vop_reg = -1;
2707 expP->X_op = O_constant;
2708 }
2709 else
2710 {
2711 operandP->vop_short = 'i';
2712 operandP->vop_mode = 8;
2713 operandP->vop_reg = 0xF; /* VAX PC */
2714 }
2715 } /* bignum or flonum ? */
2716 } /* if #, but no S^ or I^ seen. */
2717 /* No more ' ' case: either 's' or 'i'. */
2718 if (operandP->vop_short == 's')
2719 {
2720 /* Wants to be a short literal. */
2721 if (expP->X_add_number > 0)
2722 {
2723 as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
2724 operandP->vop_short = 'i';
2725 operandP->vop_mode = 8;
2726 operandP->vop_reg = 0xF; /* VAX PC. */
2727 }
2728 else
2729 {
2730 if (!can_be_short)
2731 {
2732 as_warn (_("Can't do flonum short literal: immediate mode used."));
2733 operandP->vop_short = 'i';
2734 operandP->vop_mode = 8;
2735 operandP->vop_reg = 0xF; /* VAX PC. */
2736 }
2737 else
2738 {
2739 /* Encode short literal now. */
2740 int temp = 0;
2741
2742 switch (-expP->X_add_number)
2743 {
2744 case 'f':
2745 case 'd':
2746 temp = literal_float[0] >> 4;
2747 break;
2748
2749 case 'g':
2750 temp = literal_float[0] >> 1;
2751 break;
2752
2753 case 'h':
2754 temp = ((literal_float[0] << 3) & 070)
2755 | ((literal_float[1] >> 13) & 07);
2756 break;
2757
2758 default:
2759 BAD_CASE (-expP->X_add_number);
2760 break;
2761 }
2762
2763 floatP->low[0] = temp & 077;
2764 floatP->low[1] = 0;
2765 }
2766 }
2767 }
2768 else
2769 {
2770 /* I^# seen: set it up if float. */
2771 if (expP->X_add_number < 0)
2772 {
2773 memcpy (floatP->low, literal_float, sizeof (literal_float));
2774 }
2775 } /* if S^# seen. */
2776 }
2777 else
2778 {
2779 as_warn (_("A bignum/flonum may not be a displacement: 0x%lx used"),
2780 (expP->X_add_number = 0x80000000L));
2781 /* Chosen so luser gets the most offset bits to patch later. */
2782 }
2783 expP->X_add_number = floatP->low[0]
2784 | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
2785
2786 /* For the O_big case we have:
2787 If vop_short == 's' then a short floating literal is in the
2788 lowest 6 bits of floatP -> low [0], which is
2789 big_operand_bits [---] [0].
2790 If vop_short == 'i' then the appropriate number of elements
2791 of big_operand_bits [---] [...] are set up with the correct
2792 bits.
2793 Also, just in case width is byte word or long, we copy the lowest
2794 32 bits of the number to X_add_number. */
2795 break;
2796 }
2797 if (input_line_pointer != operandP->vop_expr_end + 1)
2798 {
2799 as_fatal ("Junk at end of expression \"%s\"", input_line_pointer);
2800 goofed = 1;
2801 }
2802 operandP->vop_expr_end[1] = c_save;
2803 }
2804 }
2805
2806 input_line_pointer = save_input_line_pointer;
2807
2808 if (need_pass_2 || goofed)
2809 return;
2810
2811 dwarf2_emit_insn (0);
2812 /* Emit op-code. */
2813 /* Remember where it is, in case we want to modify the op-code later. */
2814 opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
2815 memcpy (opcode_low_byteP, v.vit_opcode, v.vit_opcode_nbytes);
2816 opcode_as_chars = v.vit_opcode;
2817 opcode_as_number = md_chars_to_number ((unsigned char *) opcode_as_chars, 4);
2818 for (operandP = v.vit_operand,
2819 expP = exp_of_operand,
2820 segP = seg_of_operand,
2821 floatP = float_operand,
2822 end_operandP = v.vit_operand + v.vit_operands;
2823
2824 operandP < end_operandP;
2825
2826 operandP++,
2827 floatP++,
2828 segP++,
2829 expP++)
2830 {
2831 if (operandP->vop_ndx >= 0)
2832 {
2833 /* Indexed addressing byte. */
2834 /* Legality of indexed mode already checked: it is OK. */
2835 FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
2836 } /* if(vop_ndx>=0) */
2837
2838 /* Here to make main operand frag(s). */
2839 this_add_number = expP->X_add_number;
2840 this_add_symbol = expP->X_add_symbol;
2841 to_seg = *segP;
2842 is_undefined = (to_seg == undefined_section);
2843 is_absolute = (to_seg == absolute_section);
2844 at = operandP->vop_mode & 1;
2845 length = (operandP->vop_short == 'b'
2846 ? 1 : (operandP->vop_short == 'w'
2847 ? 2 : (operandP->vop_short == 'l'
2848 ? 4 : 0)));
2849 nbytes = operandP->vop_nbytes;
2850 if (operandP->vop_access == 'b')
2851 {
2852 if (to_seg == now_seg || is_undefined)
2853 {
2854 /* If is_undefined, then it might BECOME now_seg. */
2855 if (nbytes)
2856 {
2857 p = frag_more (nbytes);
2858 fix_new (frag_now, p - frag_now->fr_literal, nbytes,
2859 this_add_symbol, this_add_number, 1, NO_RELOC);
2860 }
2861 else
2862 {
2863 /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
2864 /* nbytes==0 */
2865 length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
2866 if (opcode_as_number & VIT_OPCODE_SPECIAL)
2867 {
2868 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
2869 {
2870 /* br or jsb */
2871 frag_var (rs_machine_dependent, 5, 1,
2872 ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
2873 this_add_symbol, this_add_number,
2874 opcode_low_byteP);
2875 }
2876 else
2877 {
2878 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
2879 {
2880 length_code = STATE_WORD;
2881 /* JF: There is no state_byte for this one! */
2882 frag_var (rs_machine_dependent, 10, 2,
2883 ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
2884 this_add_symbol, this_add_number,
2885 opcode_low_byteP);
2886 }
2887 else
2888 {
2889 know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
2890 frag_var (rs_machine_dependent, 9, 1,
2891 ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
2892 this_add_symbol, this_add_number,
2893 opcode_low_byteP);
2894 }
2895 }
2896 }
2897 else
2898 {
2899 know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
2900 frag_var (rs_machine_dependent, 7, 1,
2901 ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
2902 this_add_symbol, this_add_number,
2903 opcode_low_byteP);
2904 }
2905 }
2906 }
2907 else
2908 {
2909 /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
2910 /* --- SEG FLOAT MAY APPEAR HERE --- */
2911 if (is_absolute)
2912 {
2913 if (nbytes)
2914 {
2915 know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
2916 p = frag_more (nbytes);
2917 /* Conventional relocation. */
2918 fix_new (frag_now, p - frag_now->fr_literal, nbytes,
2919 section_symbol (absolute_section),
2920 this_add_number, 1, NO_RELOC);
2921 }
2922 else
2923 {
2924 know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
2925 if (opcode_as_number & VIT_OPCODE_SPECIAL)
2926 {
2927 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
2928 {
2929 /* br or jsb */
2930 *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
2931 know (opcode_as_chars[1] == 0);
2932 p = frag_more (5);
2933 p[0] = VAX_ABSOLUTE_MODE; /* @#... */
2934 md_number_to_chars (p + 1, this_add_number, 4);
2935 /* Now (eg) JMP @#foo or JSB @#foo. */
2936 }
2937 else
2938 {
2939 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
2940 {
2941 p = frag_more (10);
2942 p[0] = 2;
2943 p[1] = 0;
2944 p[2] = VAX_BRB;
2945 p[3] = 6;
2946 p[4] = VAX_JMP;
2947 p[5] = VAX_ABSOLUTE_MODE; /* @#... */
2948 md_number_to_chars (p + 6, this_add_number, 4);
2949 /* Now (eg) ACBx 1f
2950 BRB 2f
2951 1: JMP @#foo
2952 2: */
2953 }
2954 else
2955 {
2956 know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
2957 p = frag_more (9);
2958 p[0] = 2;
2959 p[1] = VAX_BRB;
2960 p[2] = 6;
2961 p[3] = VAX_JMP;
2962 p[4] = VAX_ABSOLUTE_MODE; /* @#... */
2963 md_number_to_chars (p + 5, this_add_number, 4);
2964 /* Now (eg) xOBxxx 1f
2965 BRB 2f
2966 1: JMP @#foo
2967 2: */
2968 }
2969 }
2970 }
2971 else
2972 {
2973 /* b<cond> */
2974 *opcode_low_byteP ^= 1;
2975 /* To reverse the condition in a VAX branch,
2976 complement the lowest order bit. */
2977 p = frag_more (7);
2978 p[0] = 6;
2979 p[1] = VAX_JMP;
2980 p[2] = VAX_ABSOLUTE_MODE; /* @#... */
2981 md_number_to_chars (p + 3, this_add_number, 4);
2982 /* Now (eg) BLEQ 1f
2983 JMP @#foo
2984 1: */
2985 }
2986 }
2987 }
2988 else
2989 {
2990 /* to_seg != now_seg && !is_undefinfed && !is_absolute */
2991 if (nbytes > 0)
2992 {
2993 /* Pc-relative. Conventional relocation. */
2994 know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
2995 p = frag_more (nbytes);
2996 fix_new (frag_now, p - frag_now->fr_literal, nbytes,
2997 section_symbol (absolute_section),
2998 this_add_number, 1, NO_RELOC);
2999 }
3000 else
3001 {
3002 know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
3003 if (opcode_as_number & VIT_OPCODE_SPECIAL)
3004 {
3005 if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
3006 {
3007 /* br or jsb */
3008 know (opcode_as_chars[1] == 0);
3009 *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
3010 p = frag_more (5);
3011 p[0] = VAX_PC_RELATIVE_MODE;
3012 fix_new (frag_now,
3013 p + 1 - frag_now->fr_literal, 4,
3014 this_add_symbol,
3015 this_add_number, 1, NO_RELOC);
3016 /* Now eg JMP foo or JSB foo. */
3017 }
3018 else
3019 {
3020 if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
3021 {
3022 p = frag_more (10);
3023 p[0] = 0;
3024 p[1] = 2;
3025 p[2] = VAX_BRB;
3026 p[3] = 6;
3027 p[4] = VAX_JMP;
3028 p[5] = VAX_PC_RELATIVE_MODE;
3029 fix_new (frag_now,
3030 p + 6 - frag_now->fr_literal, 4,
3031 this_add_symbol,
3032 this_add_number, 1, NO_RELOC);
3033 /* Now (eg) ACBx 1f
3034 BRB 2f
3035 1: JMP foo
3036 2: */
3037 }
3038 else
3039 {
3040 know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
3041 p = frag_more (10);
3042 p[0] = 2;
3043 p[1] = VAX_BRB;
3044 p[2] = 6;
3045 p[3] = VAX_JMP;
3046 p[4] = VAX_PC_RELATIVE_MODE;
3047 fix_new (frag_now,
3048 p + 5 - frag_now->fr_literal,
3049 4, this_add_symbol,
3050 this_add_number, 1, NO_RELOC);
3051 /* Now (eg) xOBxxx 1f
3052 BRB 2f
3053 1: JMP foo
3054 2: */
3055 }
3056 }
3057 }
3058 else
3059 {
3060 know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
3061 *opcode_low_byteP ^= 1; /* Reverse branch condition. */
3062 p = frag_more (7);
3063 p[0] = 6;
3064 p[1] = VAX_JMP;
3065 p[2] = VAX_PC_RELATIVE_MODE;
3066 fix_new (frag_now, p + 3 - frag_now->fr_literal,
3067 4, this_add_symbol,
3068 this_add_number, 1, NO_RELOC);
3069 }
3070 }
3071 }
3072 }
3073 }
3074 else
3075 {
3076 /* So it is ordinary operand. */
3077 know (operandP->vop_access != 'b');
3078 /* ' ' target-independent: elsewhere. */
3079 know (operandP->vop_access != ' ');
3080 know (operandP->vop_access == 'a'
3081 || operandP->vop_access == 'm'
3082 || operandP->vop_access == 'r'
3083 || operandP->vop_access == 'v'
3084 || operandP->vop_access == 'w');
3085 if (operandP->vop_short == 's')
3086 {
3087 if (is_absolute)
3088 {
3089 if (this_add_number >= 64)
3090 {
3091 as_warn (_("Short literal overflow(%ld.), immediate mode assumed."),
3092 (long) this_add_number);
3093 operandP->vop_short = 'i';
3094 operandP->vop_mode = 8;
3095 operandP->vop_reg = 0xF;
3096 }
3097 }
3098 else
3099 {
3100 as_warn (_("Forced short literal to immediate mode. now_seg=%s to_seg=%s"),
3101 segment_name (now_seg), segment_name (to_seg));
3102 operandP->vop_short = 'i';
3103 operandP->vop_mode = 8;
3104 operandP->vop_reg = 0xF;
3105 }
3106 }
3107 if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
3108 || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
3109 {
3110 /* One byte operand. */
3111 know (operandP->vop_mode > 3);
3112 FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
3113 /* All 1-bytes except S^# happen here. */
3114 }
3115 else
3116 {
3117 /* {@}{q^}foo{(Rn)} or S^#foo */
3118 if (operandP->vop_reg == -1 && operandP->vop_short != 's')
3119 {
3120 /* "{@}{q^}foo" */
3121 if (to_seg == now_seg)
3122 {
3123 if (length == 0)
3124 {
3125 know (operandP->vop_short == ' ');
3126 length_code = STATE_BYTE;
3127 #ifdef OBJ_ELF
3128 if (S_IS_EXTERNAL (this_add_symbol)
3129 || S_IS_WEAK (this_add_symbol))
3130 length_code = STATE_UNDF;
3131 #endif
3132 p = frag_var (rs_machine_dependent, 10, 2,
3133 ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
3134 this_add_symbol, this_add_number,
3135 opcode_low_byteP);
3136 know (operandP->vop_mode == 10 + at);
3137 *p = at << 4;
3138 /* At is the only context we need to carry
3139 to other side of relax() process. Must
3140 be in the correct bit position of VAX
3141 operand spec. byte. */
3142 }
3143 else
3144 {
3145 know (length);
3146 know (operandP->vop_short != ' ');
3147 p = frag_more (length + 1);
3148 p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
3149 fix_new (frag_now, p + 1 - frag_now->fr_literal,
3150 length, this_add_symbol,
3151 this_add_number, 1, NO_RELOC);
3152 }
3153 }
3154 else
3155 {
3156 /* to_seg != now_seg */
3157 if (this_add_symbol == NULL)
3158 {
3159 know (is_absolute);
3160 /* Do @#foo: simpler relocation than foo-.(pc) anyway. */
3161 p = frag_more (5);
3162 p[0] = VAX_ABSOLUTE_MODE; /* @#... */
3163 md_number_to_chars (p + 1, this_add_number, 4);
3164 if (length && length != 4)
3165 as_warn (_("Length specification ignored. Address mode 9F used"));
3166 }
3167 else
3168 {
3169 /* {@}{q^}other_seg */
3170 know ((length == 0 && operandP->vop_short == ' ')
3171 || (length > 0 && operandP->vop_short != ' '));
3172 if (is_undefined
3173 #ifdef OBJ_ELF
3174 || S_IS_WEAK(this_add_symbol)
3175 || S_IS_EXTERNAL(this_add_symbol)
3176 #endif
3177 )
3178 {
3179 switch (length)
3180 {
3181 default: length_code = STATE_UNDF; break;
3182 case 1: length_code = STATE_BYTE; break;
3183 case 2: length_code = STATE_WORD; break;
3184 case 4: length_code = STATE_LONG; break;
3185 }
3186 /* We have a SEG_UNKNOWN symbol. It might
3187 turn out to be in the same segment as
3188 the instruction, permitting relaxation. */
3189 p = frag_var (rs_machine_dependent, 5, 2,
3190 ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
3191 this_add_symbol, this_add_number,
3192 opcode_low_byteP);
3193 p[0] = at << 4;
3194 }
3195 else
3196 {
3197 if (length == 0)
3198 {
3199 know (operandP->vop_short == ' ');
3200 length = 4; /* Longest possible. */
3201 }
3202 p = frag_more (length + 1);
3203 p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
3204 md_number_to_chars (p + 1, this_add_number, length);
3205 fix_new (frag_now,
3206 p + 1 - frag_now->fr_literal,
3207 length, this_add_symbol,
3208 this_add_number, 1, NO_RELOC);
3209 }
3210 }
3211 }
3212 }
3213 else
3214 {
3215 /* {@}{q^}foo(Rn) or S^# or I^# or # */
3216 if (operandP->vop_mode < 0xA)
3217 {
3218 /* # or S^# or I^# */
3219 if (operandP->vop_access == 'v'
3220 || operandP->vop_access == 'a')
3221 {
3222 if (operandP->vop_access == 'v')
3223 as_warn (_("Invalid operand: immediate value used as base address."));
3224 else
3225 as_warn (_("Invalid operand: immediate value used as address."));
3226 /* gcc 2.6.3 is known to generate these in at least
3227 one case. */
3228 }
3229 if (length == 0
3230 && is_absolute && (expP->X_op != O_big)
3231 && operandP->vop_mode == 8 /* No '@'. */
3232 && this_add_number < 64)
3233 {
3234 operandP->vop_short = 's';
3235 }
3236 if (operandP->vop_short == 's')
3237 {
3238 FRAG_APPEND_1_CHAR (this_add_number);
3239 }
3240 else
3241 {
3242 /* I^#... */
3243 know (nbytes);
3244 p = frag_more (nbytes + 1);
3245 know (operandP->vop_reg == 0xF);
3246 #ifdef OBJ_ELF
3247 if (flag_want_pic && operandP->vop_mode == 8
3248 && this_add_symbol != NULL)
3249 {
3250 as_warn (_("Symbol '%s' used as immediate operand in PIC mode."),
3251 S_GET_NAME (this_add_symbol));
3252 }
3253 #endif
3254 p[0] = (operandP->vop_mode << 4) | 0xF;
3255 if ((is_absolute) && (expP->X_op != O_big))
3256 {
3257 /* If nbytes > 4, then we are scrod. We
3258 don't know if the high order bytes
3259 are to be 0xFF or 0x00. BSD4.2 & RMS
3260 say use 0x00. OK --- but this
3261 assembler needs ANOTHER rewrite to
3262 cope properly with this bug. */
3263 md_number_to_chars (p + 1, this_add_number,
3264 min (sizeof (valueT),
3265 (size_t) nbytes));
3266 if ((size_t) nbytes > sizeof (valueT))
3267 memset (p + 1 + sizeof (valueT),
3268 '\0', nbytes - sizeof (valueT));
3269 }
3270 else
3271 {
3272 if (expP->X_op == O_big)
3273 {
3274 /* Problem here is to get the bytes
3275 in the right order. We stored
3276 our constant as LITTLENUMs, not
3277 bytes. */
3278 LITTLENUM_TYPE *lP;
3279
3280 lP = floatP->low;
3281 if (nbytes & 1)
3282 {
3283 know (nbytes == 1);
3284 p[1] = *lP;
3285 }
3286 else
3287 {
3288 for (p++; nbytes; nbytes -= 2, p += 2, lP++)
3289 md_number_to_chars (p, *lP, 2);
3290 }
3291 }
3292 else
3293 {
3294 fix_new (frag_now, p + 1 - frag_now->fr_literal,
3295 nbytes, this_add_symbol,
3296 this_add_number, 0, NO_RELOC);
3297 }
3298 }
3299 }
3300 }
3301 else
3302 {
3303 /* {@}{q^}foo(Rn) */
3304 know ((length == 0 && operandP->vop_short == ' ')
3305 || (length > 0 && operandP->vop_short != ' '));
3306 if (length == 0)
3307 {
3308 if (is_absolute)
3309 {
3310 long test;
3311
3312 test = this_add_number;
3313
3314 if (test < 0)
3315 test = ~test;
3316
3317 length = test & 0xffff8000 ? 4
3318 : test & 0xffffff80 ? 2
3319 : 1;
3320 }
3321 else
3322 {
3323 length = 4;
3324 }
3325 }
3326 #ifdef OBJ_ELF
3327 if (flag_want_pic && this_add_symbol != NULL)
3328 {
3329 as_warn (_("Symbol '%s' used as displacement in PIC mode."),
3330 S_GET_NAME (this_add_symbol));
3331 }
3332 #endif
3333 p = frag_more (1 + length);
3334 know (operandP->vop_reg != 0xf);
3335 know (operandP->vop_reg >= 0);
3336 p[0] = operandP->vop_reg
3337 | ((at | "?\12\14?\16"[length]) << 4);
3338 if (is_absolute)
3339 {
3340 md_number_to_chars (p + 1, this_add_number, length);
3341 }
3342 else
3343 {
3344 fix_new (frag_now, p + 1 - frag_now->fr_literal,
3345 length, this_add_symbol,
3346 this_add_number, 0, NO_RELOC);
3347 }
3348 }
3349 }
3350 }
3351 }
3352 }
3353 }
3354
3355 void
3356 md_begin (void)
3357 {
3358 FLONUM_TYPE *fP;
3359 int i;
3360
3361 vip_begin (1, "$", "*", "`");
3362
3363 for (i = 0, fP = float_operand;
3364 fP < float_operand + VIT_MAX_OPERANDS;
3365 i++, fP++)
3366 {
3367 fP->low = &big_operand_bits[i][0];
3368 fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
3369 }
3370 }
3371
3372 bfd_reloc_code_real_type
3373 vax_cons (expressionS *exp, int size)
3374 {
3375 char *save;
3376 const char *vax_cons_special_reloc;
3377
3378 SKIP_WHITESPACE ();
3379 vax_cons_special_reloc = NULL;
3380 save = input_line_pointer;
3381 if (input_line_pointer[0] == '%')
3382 {
3383 if (startswith (input_line_pointer + 1, "pcrel"))
3384 {
3385 input_line_pointer += 6;
3386 vax_cons_special_reloc = "pcrel";
3387 }
3388 if (vax_cons_special_reloc)
3389 {
3390 int bad = 0;
3391
3392 switch (size)
3393 {
3394 case 1:
3395 if (*input_line_pointer != '8')
3396 bad = 1;
3397 input_line_pointer--;
3398 break;
3399 case 2:
3400 if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
3401 bad = 1;
3402 break;
3403 case 4:
3404 if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
3405 bad = 1;
3406 break;
3407 default:
3408 bad = 1;
3409 break;
3410 }
3411
3412 if (bad)
3413 {
3414 as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
3415 vax_cons_special_reloc, size * 8, size);
3416 }
3417 else
3418 {
3419 input_line_pointer += 2;
3420 if (*input_line_pointer != '(')
3421 {
3422 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
3423 vax_cons_special_reloc, size * 8);
3424 bad = 1;
3425 }
3426 }
3427
3428 if (bad)
3429 {
3430 input_line_pointer = save;
3431 vax_cons_special_reloc = NULL;
3432 }
3433 else
3434 {
3435 int c;
3436 char *end = ++input_line_pointer;
3437 int npar = 0;
3438
3439 while (! is_end_of_line[(c = *end)])
3440 {
3441 if (c == '(')
3442 npar++;
3443 else if (c == ')')
3444 {
3445 if (!npar)
3446 break;
3447 npar--;
3448 }
3449 end++;
3450 }
3451
3452 if (c != ')')
3453 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
3454 vax_cons_special_reloc, size * 8);
3455 else
3456 {
3457 *end = '\0';
3458 expression (exp);
3459 *end = c;
3460 if (input_line_pointer != end)
3461 {
3462 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
3463 vax_cons_special_reloc, size * 8);
3464 }
3465 else
3466 {
3467 input_line_pointer++;
3468 SKIP_WHITESPACE ();
3469 c = *input_line_pointer;
3470 if (! is_end_of_line[c] && c != ',')
3471 as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
3472 vax_cons_special_reloc, size * 8);
3473 }
3474 }
3475 }
3476 }
3477 }
3478 if (vax_cons_special_reloc == NULL)
3479 expression (exp);
3480 else
3481 switch (size)
3482 {
3483 case 1: return BFD_RELOC_8_PCREL;
3484 case 2: return BFD_RELOC_16_PCREL;
3485 case 4: return BFD_RELOC_32_PCREL;
3486 }
3487 return NO_RELOC;
3488 }
3489
3490 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
3491 reloc for a cons. */
3492
3493 void
3494 vax_cons_fix_new (fragS *frag, int where, unsigned int nbytes, expressionS *exp,
3495 bfd_reloc_code_real_type r)
3496 {
3497 if (r == NO_RELOC)
3498 r = (nbytes == 1 ? BFD_RELOC_8
3499 : nbytes == 2 ? BFD_RELOC_16
3500 : BFD_RELOC_32);
3501
3502 fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
3503 }
3504
3505 const char *
3506 md_atof (int type, char * litP, int * sizeP)
3507 {
3508 return vax_md_atof (type, litP, sizeP);
3509 }
3510
3511 void
3512 vax_cfi_frame_initial_instructions (void)
3513 {
3514 cfi_add_CFA_def_cfa (14, 0);
3515 }
3516
3517 int
3518 tc_vax_regname_to_dw2regnum (char *regname)
3519 {
3520 unsigned int i;
3521 static const struct { char *name; int dw2regnum; } regnames[] =
3522 {
3523 { "r0", 0 }, { "r1", 1 }, { "r2", 2 }, { "r3", 3 },
3524 { "r4", 4 }, { "r5", 5 }, { "r6", 6 }, { "r7", 7 },
3525 { "r8", 8 }, { "r9", 9 }, { "r10", 10 }, { "r11", 11 },
3526 { "ap", 12 }, { "fp", 13 }, { "sp", 14 }, { "pc", 15 },
3527 { "psw", 16 },
3528 };
3529
3530 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
3531 if (strcmp (regnames[i].name, regname) == 0)
3532 return regnames[i].dw2regnum;
3533
3534 return -1;
3535 }
3536
3537 void
3538 vax_cfi_emit_pcrel_expr (expressionS *expP, unsigned int nbytes)
3539 {
3540 expP->X_add_number += nbytes;
3541 emit_expr (expP, nbytes);
3542 }
3543