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