1 /* Assembler interface for targets using CGEN. -*- C -*- 2 CGEN: Cpu tools GENerator 3 4 THIS FILE IS MACHINE GENERATED WITH CGEN. 5 - the resultant file is machine generated, cgen-asm.in isn't 6 7 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005 8 Free Software Foundation, Inc. 9 10 This file is part of the GNU Binutils and GDB, the GNU debugger. 11 12 This program is free software; you can redistribute it and/or modify 13 it under the terms of the GNU General Public License as published by 14 the Free Software Foundation; either version 2, or (at your option) 15 any later version. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to the Free Software Foundation, Inc., 24 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 25 26 /* ??? Eventually more and more of this stuff can go to cpu-independent files. 27 Keep that in mind. */ 28 29 #include "sysdep.h" 30 #include <stdio.h> 31 #include "ansidecl.h" 32 #include "bfd.h" 33 #include "symcat.h" 34 #include "ip2k-desc.h" 35 #include "ip2k-opc.h" 36 #include "opintl.h" 37 #include "xregex.h" 38 #include "libiberty.h" 39 #include "safe-ctype.h" 40 41 #undef min 42 #define min(a,b) ((a) < (b) ? (a) : (b)) 43 #undef max 44 #define max(a,b) ((a) > (b) ? (a) : (b)) 45 46 static const char * parse_insn_normal 47 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); 48 49 /* -- assembler routines inserted here. */ 50 51 /* -- asm.c */ 52 53 static const char * 54 parse_fr (CGEN_CPU_DESC cd, 55 const char **strp, 56 int opindex, 57 unsigned long *valuep) 58 { 59 const char *errmsg; 60 const char *old_strp; 61 char *afteroffset; 62 enum cgen_parse_operand_result result_type; 63 bfd_vma value; 64 extern CGEN_KEYWORD ip2k_cgen_opval_register_names; 65 bfd_vma tempvalue; 66 67 old_strp = *strp; 68 afteroffset = NULL; 69 70 /* Check here to see if you're about to try parsing a w as the first arg 71 and return an error if you are. */ 72 if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0)) 73 { 74 (*strp)++; 75 76 if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp)) 77 { 78 /* We've been passed a w. Return with an error message so that 79 cgen will try the next parsing option. */ 80 errmsg = _("W keyword invalid in FR operand slot."); 81 return errmsg; 82 } 83 *strp = old_strp; 84 } 85 86 /* Attempt parse as register keyword. */ 87 errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names, 88 (long *) valuep); 89 if (*strp != NULL 90 && errmsg == NULL) 91 return errmsg; 92 93 /* Attempt to parse for "(IP)". */ 94 afteroffset = strstr (*strp, "(IP)"); 95 96 if (afteroffset == NULL) 97 /* Make sure it's not in lower case. */ 98 afteroffset = strstr (*strp, "(ip)"); 99 100 if (afteroffset != NULL) 101 { 102 if (afteroffset != *strp) 103 { 104 /* Invalid offset present. */ 105 errmsg = _("offset(IP) is not a valid form"); 106 return errmsg; 107 } 108 else 109 { 110 *strp += 4; 111 *valuep = 0; 112 errmsg = NULL; 113 return errmsg; 114 } 115 } 116 117 /* Attempt to parse for DP. ex: mov w, offset(DP) 118 mov offset(DP),w */ 119 120 /* Try parsing it as an address and see what comes back. */ 121 afteroffset = strstr (*strp, "(DP)"); 122 123 if (afteroffset == NULL) 124 /* Maybe it's in lower case. */ 125 afteroffset = strstr (*strp, "(dp)"); 126 127 if (afteroffset != NULL) 128 { 129 if (afteroffset == *strp) 130 { 131 /* No offset present. Use 0 by default. */ 132 tempvalue = 0; 133 errmsg = NULL; 134 } 135 else 136 errmsg = cgen_parse_address (cd, strp, opindex, 137 BFD_RELOC_IP2K_FR_OFFSET, 138 & result_type, & tempvalue); 139 140 if (errmsg == NULL) 141 { 142 if (tempvalue <= 127) 143 { 144 /* Value is ok. Fix up the first 2 bits and return. */ 145 *valuep = 0x0100 | tempvalue; 146 *strp += 4; /* Skip over the (DP) in *strp. */ 147 return errmsg; 148 } 149 else 150 { 151 /* Found something there in front of (DP) but it's out 152 of range. */ 153 errmsg = _("(DP) offset out of range."); 154 return errmsg; 155 } 156 } 157 } 158 159 160 /* Attempt to parse for SP. ex: mov w, offset(SP) 161 mov offset(SP), w. */ 162 afteroffset = strstr (*strp, "(SP)"); 163 164 if (afteroffset == NULL) 165 /* Maybe it's in lower case. */ 166 afteroffset = strstr (*strp, "(sp)"); 167 168 if (afteroffset != NULL) 169 { 170 if (afteroffset == *strp) 171 { 172 /* No offset present. Use 0 by default. */ 173 tempvalue = 0; 174 errmsg = NULL; 175 } 176 else 177 errmsg = cgen_parse_address (cd, strp, opindex, 178 BFD_RELOC_IP2K_FR_OFFSET, 179 & result_type, & tempvalue); 180 181 if (errmsg == NULL) 182 { 183 if (tempvalue <= 127) 184 { 185 /* Value is ok. Fix up the first 2 bits and return. */ 186 *valuep = 0x0180 | tempvalue; 187 *strp += 4; /* Skip over the (SP) in *strp. */ 188 return errmsg; 189 } 190 else 191 { 192 /* Found something there in front of (SP) but it's out 193 of range. */ 194 errmsg = _("(SP) offset out of range."); 195 return errmsg; 196 } 197 } 198 } 199 200 /* Attempt to parse as an address. */ 201 *strp = old_strp; 202 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9, 203 & result_type, & value); 204 if (errmsg == NULL) 205 { 206 *valuep = value; 207 208 /* If a parenthesis is found, warn about invalid form. */ 209 if (**strp == '(') 210 errmsg = _("illegal use of parentheses"); 211 212 /* If a numeric value is specified, ensure that it is between 213 1 and 255. */ 214 else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 215 { 216 if (value < 0x1 || value > 0xff) 217 errmsg = _("operand out of range (not between 1 and 255)"); 218 } 219 } 220 return errmsg; 221 } 222 223 static const char * 224 parse_addr16 (CGEN_CPU_DESC cd, 225 const char **strp, 226 int opindex, 227 unsigned long *valuep) 228 { 229 const char *errmsg; 230 enum cgen_parse_operand_result result_type; 231 bfd_reloc_code_real_type code = BFD_RELOC_NONE; 232 bfd_vma value; 233 234 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H) 235 code = BFD_RELOC_IP2K_HI8DATA; 236 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L) 237 code = BFD_RELOC_IP2K_LO8DATA; 238 else 239 { 240 /* Something is very wrong. opindex has to be one of the above. */ 241 errmsg = _("parse_addr16: invalid opindex."); 242 return errmsg; 243 } 244 245 errmsg = cgen_parse_address (cd, strp, opindex, code, 246 & result_type, & value); 247 if (errmsg == NULL) 248 { 249 /* We either have a relocation or a number now. */ 250 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 251 { 252 /* We got a number back. */ 253 if (code == BFD_RELOC_IP2K_HI8DATA) 254 value >>= 8; 255 else 256 /* code = BFD_RELOC_IP2K_LOW8DATA. */ 257 value &= 0x00FF; 258 } 259 *valuep = value; 260 } 261 262 return errmsg; 263 } 264 265 static const char * 266 parse_addr16_cjp (CGEN_CPU_DESC cd, 267 const char **strp, 268 int opindex, 269 unsigned long *valuep) 270 { 271 const char *errmsg; 272 enum cgen_parse_operand_result result_type; 273 bfd_reloc_code_real_type code = BFD_RELOC_NONE; 274 bfd_vma value; 275 276 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP) 277 code = BFD_RELOC_IP2K_ADDR16CJP; 278 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P) 279 code = BFD_RELOC_IP2K_PAGE3; 280 281 errmsg = cgen_parse_address (cd, strp, opindex, code, 282 & result_type, & value); 283 if (errmsg == NULL) 284 { 285 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 286 { 287 if ((value & 0x1) == 0) /* If the address is even .... */ 288 { 289 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP) 290 *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */ 291 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P) 292 *valuep = (value >> 14) & 0x7; 293 } 294 else 295 errmsg = _("Byte address required. - must be even."); 296 } 297 else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED) 298 { 299 /* This will happen for things like (s2-s1) where s2 and s1 300 are labels. */ 301 *valuep = value; 302 } 303 else 304 errmsg = _("cgen_parse_address returned a symbol. Literal required."); 305 } 306 return errmsg; 307 } 308 309 static const char * 310 parse_lit8 (CGEN_CPU_DESC cd, 311 const char **strp, 312 int opindex, 313 long *valuep) 314 { 315 const char *errmsg; 316 enum cgen_parse_operand_result result_type; 317 bfd_reloc_code_real_type code = BFD_RELOC_NONE; 318 bfd_vma value; 319 320 /* Parse %OP relocating operators. */ 321 if (strncmp (*strp, "%bank", 5) == 0) 322 { 323 *strp += 5; 324 code = BFD_RELOC_IP2K_BANK; 325 } 326 else if (strncmp (*strp, "%lo8data", 8) == 0) 327 { 328 *strp += 8; 329 code = BFD_RELOC_IP2K_LO8DATA; 330 } 331 else if (strncmp (*strp, "%hi8data", 8) == 0) 332 { 333 *strp += 8; 334 code = BFD_RELOC_IP2K_HI8DATA; 335 } 336 else if (strncmp (*strp, "%ex8data", 8) == 0) 337 { 338 *strp += 8; 339 code = BFD_RELOC_IP2K_EX8DATA; 340 } 341 else if (strncmp (*strp, "%lo8insn", 8) == 0) 342 { 343 *strp += 8; 344 code = BFD_RELOC_IP2K_LO8INSN; 345 } 346 else if (strncmp (*strp, "%hi8insn", 8) == 0) 347 { 348 *strp += 8; 349 code = BFD_RELOC_IP2K_HI8INSN; 350 } 351 352 /* Parse %op operand. */ 353 if (code != BFD_RELOC_NONE) 354 { 355 errmsg = cgen_parse_address (cd, strp, opindex, code, 356 & result_type, & value); 357 if ((errmsg == NULL) && 358 (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED)) 359 errmsg = _("percent-operator operand is not a symbol"); 360 361 *valuep = value; 362 } 363 /* Parse as a number. */ 364 else 365 { 366 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep); 367 368 /* Truncate to eight bits to accept both signed and unsigned input. */ 369 if (errmsg == NULL) 370 *valuep &= 0xFF; 371 } 372 373 return errmsg; 374 } 375 376 static const char * 377 parse_bit3 (CGEN_CPU_DESC cd, 378 const char **strp, 379 int opindex, 380 unsigned long *valuep) 381 { 382 const char *errmsg; 383 char mode = 0; 384 long count = 0; 385 unsigned long value; 386 387 if (strncmp (*strp, "%bit", 4) == 0) 388 { 389 *strp += 4; 390 mode = 1; 391 } 392 else if (strncmp (*strp, "%msbbit", 7) == 0) 393 { 394 *strp += 7; 395 mode = 1; 396 } 397 else if (strncmp (*strp, "%lsbbit", 7) == 0) 398 { 399 *strp += 7; 400 mode = 2; 401 } 402 403 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep); 404 if (errmsg) 405 return errmsg; 406 407 if (mode) 408 { 409 value = * valuep; 410 if (value == 0) 411 { 412 errmsg = _("Attempt to find bit index of 0"); 413 return errmsg; 414 } 415 416 if (mode == 1) 417 { 418 count = 31; 419 while ((value & 0x80000000) == 0) 420 { 421 count--; 422 value <<= 1; 423 } 424 } 425 else if (mode == 2) 426 { 427 count = 0; 428 while ((value & 0x00000001) == 0) 429 { 430 count++; 431 value >>= 1; 432 } 433 } 434 435 *valuep = count; 436 } 437 438 return errmsg; 439 } 440 441 /* -- dis.c */ 442 443 const char * ip2k_cgen_parse_operand 444 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); 445 446 /* Main entry point for operand parsing. 447 448 This function is basically just a big switch statement. Earlier versions 449 used tables to look up the function to use, but 450 - if the table contains both assembler and disassembler functions then 451 the disassembler contains much of the assembler and vice-versa, 452 - there's a lot of inlining possibilities as things grow, 453 - using a switch statement avoids the function call overhead. 454 455 This function could be moved into `parse_insn_normal', but keeping it 456 separate makes clear the interface between `parse_insn_normal' and each of 457 the handlers. */ 458 459 const char * 460 ip2k_cgen_parse_operand (CGEN_CPU_DESC cd, 461 int opindex, 462 const char ** strp, 463 CGEN_FIELDS * fields) 464 { 465 const char * errmsg = NULL; 466 /* Used by scalar operands that still need to be parsed. */ 467 long junk ATTRIBUTE_UNUSED; 468 469 switch (opindex) 470 { 471 case IP2K_OPERAND_ADDR16CJP : 472 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, (unsigned long *) (& fields->f_addr16cjp)); 473 break; 474 case IP2K_OPERAND_ADDR16H : 475 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, (unsigned long *) (& fields->f_imm8)); 476 break; 477 case IP2K_OPERAND_ADDR16L : 478 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, (unsigned long *) (& fields->f_imm8)); 479 break; 480 case IP2K_OPERAND_ADDR16P : 481 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, (unsigned long *) (& fields->f_page3)); 482 break; 483 case IP2K_OPERAND_BITNO : 484 errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, (unsigned long *) (& fields->f_bitno)); 485 break; 486 case IP2K_OPERAND_CBIT : 487 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, (unsigned long *) (& junk)); 488 break; 489 case IP2K_OPERAND_DCBIT : 490 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, (unsigned long *) (& junk)); 491 break; 492 case IP2K_OPERAND_FR : 493 errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, (unsigned long *) (& fields->f_reg)); 494 break; 495 case IP2K_OPERAND_LIT8 : 496 errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, (long *) (& fields->f_imm8)); 497 break; 498 case IP2K_OPERAND_PABITS : 499 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, (unsigned long *) (& junk)); 500 break; 501 case IP2K_OPERAND_RETI3 : 502 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, (unsigned long *) (& fields->f_reti3)); 503 break; 504 case IP2K_OPERAND_ZBIT : 505 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, (unsigned long *) (& junk)); 506 break; 507 508 default : 509 /* xgettext:c-format */ 510 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); 511 abort (); 512 } 513 514 return errmsg; 515 } 516 517 cgen_parse_fn * const ip2k_cgen_parse_handlers[] = 518 { 519 parse_insn_normal, 520 }; 521 522 void 523 ip2k_cgen_init_asm (CGEN_CPU_DESC cd) 524 { 525 ip2k_cgen_init_opcode_table (cd); 526 ip2k_cgen_init_ibld_table (cd); 527 cd->parse_handlers = & ip2k_cgen_parse_handlers[0]; 528 cd->parse_operand = ip2k_cgen_parse_operand; 529 } 530 531 532 533 /* Regex construction routine. 534 535 This translates an opcode syntax string into a regex string, 536 by replacing any non-character syntax element (such as an 537 opcode) with the pattern '.*' 538 539 It then compiles the regex and stores it in the opcode, for 540 later use by ip2k_cgen_assemble_insn 541 542 Returns NULL for success, an error message for failure. */ 543 544 char * 545 ip2k_cgen_build_insn_regex (CGEN_INSN *insn) 546 { 547 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); 548 const char *mnem = CGEN_INSN_MNEMONIC (insn); 549 char rxbuf[CGEN_MAX_RX_ELEMENTS]; 550 char *rx = rxbuf; 551 const CGEN_SYNTAX_CHAR_TYPE *syn; 552 int reg_err; 553 554 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); 555 556 /* Mnemonics come first in the syntax string. */ 557 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 558 return _("missing mnemonic in syntax string"); 559 ++syn; 560 561 /* Generate a case sensitive regular expression that emulates case 562 insensitive matching in the "C" locale. We cannot generate a case 563 insensitive regular expression because in Turkish locales, 'i' and 'I' 564 are not equal modulo case conversion. */ 565 566 /* Copy the literal mnemonic out of the insn. */ 567 for (; *mnem; mnem++) 568 { 569 char c = *mnem; 570 571 if (ISALPHA (c)) 572 { 573 *rx++ = '['; 574 *rx++ = TOLOWER (c); 575 *rx++ = TOUPPER (c); 576 *rx++ = ']'; 577 } 578 else 579 *rx++ = c; 580 } 581 582 /* Copy any remaining literals from the syntax string into the rx. */ 583 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) 584 { 585 if (CGEN_SYNTAX_CHAR_P (* syn)) 586 { 587 char c = CGEN_SYNTAX_CHAR (* syn); 588 589 switch (c) 590 { 591 /* Escape any regex metacharacters in the syntax. */ 592 case '.': case '[': case '\\': 593 case '*': case '^': case '$': 594 595 #ifdef CGEN_ESCAPE_EXTENDED_REGEX 596 case '?': case '{': case '}': 597 case '(': case ')': case '*': 598 case '|': case '+': case ']': 599 #endif 600 *rx++ = '\\'; 601 *rx++ = c; 602 break; 603 604 default: 605 if (ISALPHA (c)) 606 { 607 *rx++ = '['; 608 *rx++ = TOLOWER (c); 609 *rx++ = TOUPPER (c); 610 *rx++ = ']'; 611 } 612 else 613 *rx++ = c; 614 break; 615 } 616 } 617 else 618 { 619 /* Replace non-syntax fields with globs. */ 620 *rx++ = '.'; 621 *rx++ = '*'; 622 } 623 } 624 625 /* Trailing whitespace ok. */ 626 * rx++ = '['; 627 * rx++ = ' '; 628 * rx++ = '\t'; 629 * rx++ = ']'; 630 * rx++ = '*'; 631 632 /* But anchor it after that. */ 633 * rx++ = '$'; 634 * rx = '\0'; 635 636 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); 637 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); 638 639 if (reg_err == 0) 640 return NULL; 641 else 642 { 643 static char msg[80]; 644 645 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); 646 regfree ((regex_t *) CGEN_INSN_RX (insn)); 647 free (CGEN_INSN_RX (insn)); 648 (CGEN_INSN_RX (insn)) = NULL; 649 return msg; 650 } 651 } 652 653 654 /* Default insn parser. 655 656 The syntax string is scanned and operands are parsed and stored in FIELDS. 657 Relocs are queued as we go via other callbacks. 658 659 ??? Note that this is currently an all-or-nothing parser. If we fail to 660 parse the instruction, we return 0 and the caller will start over from 661 the beginning. Backtracking will be necessary in parsing subexpressions, 662 but that can be handled there. Not handling backtracking here may get 663 expensive in the case of the m68k. Deal with later. 664 665 Returns NULL for success, an error message for failure. */ 666 667 static const char * 668 parse_insn_normal (CGEN_CPU_DESC cd, 669 const CGEN_INSN *insn, 670 const char **strp, 671 CGEN_FIELDS *fields) 672 { 673 /* ??? Runtime added insns not handled yet. */ 674 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); 675 const char *str = *strp; 676 const char *errmsg; 677 const char *p; 678 const CGEN_SYNTAX_CHAR_TYPE * syn; 679 #ifdef CGEN_MNEMONIC_OPERANDS 680 /* FIXME: wip */ 681 int past_opcode_p; 682 #endif 683 684 /* For now we assume the mnemonic is first (there are no leading operands). 685 We can parse it without needing to set up operand parsing. 686 GAS's input scrubber will ensure mnemonics are lowercase, but we may 687 not be called from GAS. */ 688 p = CGEN_INSN_MNEMONIC (insn); 689 while (*p && TOLOWER (*p) == TOLOWER (*str)) 690 ++p, ++str; 691 692 if (* p) 693 return _("unrecognized instruction"); 694 695 #ifndef CGEN_MNEMONIC_OPERANDS 696 if (* str && ! ISSPACE (* str)) 697 return _("unrecognized instruction"); 698 #endif 699 700 CGEN_INIT_PARSE (cd); 701 cgen_init_parse_operand (cd); 702 #ifdef CGEN_MNEMONIC_OPERANDS 703 past_opcode_p = 0; 704 #endif 705 706 /* We don't check for (*str != '\0') here because we want to parse 707 any trailing fake arguments in the syntax string. */ 708 syn = CGEN_SYNTAX_STRING (syntax); 709 710 /* Mnemonics come first for now, ensure valid string. */ 711 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 712 abort (); 713 714 ++syn; 715 716 while (* syn != 0) 717 { 718 /* Non operand chars must match exactly. */ 719 if (CGEN_SYNTAX_CHAR_P (* syn)) 720 { 721 /* FIXME: While we allow for non-GAS callers above, we assume the 722 first char after the mnemonic part is a space. */ 723 /* FIXME: We also take inappropriate advantage of the fact that 724 GAS's input scrubber will remove extraneous blanks. */ 725 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) 726 { 727 #ifdef CGEN_MNEMONIC_OPERANDS 728 if (CGEN_SYNTAX_CHAR(* syn) == ' ') 729 past_opcode_p = 1; 730 #endif 731 ++ syn; 732 ++ str; 733 } 734 else if (*str) 735 { 736 /* Syntax char didn't match. Can't be this insn. */ 737 static char msg [80]; 738 739 /* xgettext:c-format */ 740 sprintf (msg, _("syntax error (expected char `%c', found `%c')"), 741 CGEN_SYNTAX_CHAR(*syn), *str); 742 return msg; 743 } 744 else 745 { 746 /* Ran out of input. */ 747 static char msg [80]; 748 749 /* xgettext:c-format */ 750 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), 751 CGEN_SYNTAX_CHAR(*syn)); 752 return msg; 753 } 754 continue; 755 } 756 757 /* We have an operand of some sort. */ 758 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), 759 &str, fields); 760 if (errmsg) 761 return errmsg; 762 763 /* Done with this operand, continue with next one. */ 764 ++ syn; 765 } 766 767 /* If we're at the end of the syntax string, we're done. */ 768 if (* syn == 0) 769 { 770 /* FIXME: For the moment we assume a valid `str' can only contain 771 blanks now. IE: We needn't try again with a longer version of 772 the insn and it is assumed that longer versions of insns appear 773 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ 774 while (ISSPACE (* str)) 775 ++ str; 776 777 if (* str != '\0') 778 return _("junk at end of line"); /* FIXME: would like to include `str' */ 779 780 return NULL; 781 } 782 783 /* We couldn't parse it. */ 784 return _("unrecognized instruction"); 785 } 786 787 /* Main entry point. 788 This routine is called for each instruction to be assembled. 789 STR points to the insn to be assembled. 790 We assume all necessary tables have been initialized. 791 The assembled instruction, less any fixups, is stored in BUF. 792 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value 793 still needs to be converted to target byte order, otherwise BUF is an array 794 of bytes in target byte order. 795 The result is a pointer to the insn's entry in the opcode table, 796 or NULL if an error occured (an error message will have already been 797 printed). 798 799 Note that when processing (non-alias) macro-insns, 800 this function recurses. 801 802 ??? It's possible to make this cpu-independent. 803 One would have to deal with a few minor things. 804 At this point in time doing so would be more of a curiosity than useful 805 [for example this file isn't _that_ big], but keeping the possibility in 806 mind helps keep the design clean. */ 807 808 const CGEN_INSN * 809 ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd, 810 const char *str, 811 CGEN_FIELDS *fields, 812 CGEN_INSN_BYTES_PTR buf, 813 char **errmsg) 814 { 815 const char *start; 816 CGEN_INSN_LIST *ilist; 817 const char *parse_errmsg = NULL; 818 const char *insert_errmsg = NULL; 819 int recognized_mnemonic = 0; 820 821 /* Skip leading white space. */ 822 while (ISSPACE (* str)) 823 ++ str; 824 825 /* The instructions are stored in hashed lists. 826 Get the first in the list. */ 827 ilist = CGEN_ASM_LOOKUP_INSN (cd, str); 828 829 /* Keep looking until we find a match. */ 830 start = str; 831 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) 832 { 833 const CGEN_INSN *insn = ilist->insn; 834 recognized_mnemonic = 1; 835 836 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 837 /* Not usually needed as unsupported opcodes 838 shouldn't be in the hash lists. */ 839 /* Is this insn supported by the selected cpu? */ 840 if (! ip2k_cgen_insn_supported (cd, insn)) 841 continue; 842 #endif 843 /* If the RELAXED attribute is set, this is an insn that shouldn't be 844 chosen immediately. Instead, it is used during assembler/linker 845 relaxation if possible. */ 846 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) 847 continue; 848 849 str = start; 850 851 /* Skip this insn if str doesn't look right lexically. */ 852 if (CGEN_INSN_RX (insn) != NULL && 853 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) 854 continue; 855 856 /* Allow parse/insert handlers to obtain length of insn. */ 857 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); 858 859 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); 860 if (parse_errmsg != NULL) 861 continue; 862 863 /* ??? 0 is passed for `pc'. */ 864 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, 865 (bfd_vma) 0); 866 if (insert_errmsg != NULL) 867 continue; 868 869 /* It is up to the caller to actually output the insn and any 870 queued relocs. */ 871 return insn; 872 } 873 874 { 875 static char errbuf[150]; 876 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS 877 const char *tmp_errmsg; 878 879 /* If requesting verbose error messages, use insert_errmsg. 880 Failing that, use parse_errmsg. */ 881 tmp_errmsg = (insert_errmsg ? insert_errmsg : 882 parse_errmsg ? parse_errmsg : 883 recognized_mnemonic ? 884 _("unrecognized form of instruction") : 885 _("unrecognized instruction")); 886 887 if (strlen (start) > 50) 888 /* xgettext:c-format */ 889 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); 890 else 891 /* xgettext:c-format */ 892 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); 893 #else 894 if (strlen (start) > 50) 895 /* xgettext:c-format */ 896 sprintf (errbuf, _("bad instruction `%.50s...'"), start); 897 else 898 /* xgettext:c-format */ 899 sprintf (errbuf, _("bad instruction `%.50s'"), start); 900 #endif 901 902 *errmsg = errbuf; 903 return NULL; 904 } 905 } 906