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