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 (C) 1996-2014 Free Software Foundation, Inc. 8 9 This file is part of libopcodes. 10 11 This library is free software; you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation; either version 3, or (at your option) 14 any later version. 15 16 It is distributed in the hope that it will be useful, but WITHOUT 17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 19 License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software Foundation, Inc., 23 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 24 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 "xc16x-desc.h" 35 #include "xc16x-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. */ 51 52 /* -- asm.c */ 53 /* Handle '#' prefixes (i.e. skip over them). */ 54 55 static const char * 56 parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 57 const char **strp, 58 int opindex ATTRIBUTE_UNUSED, 59 long *valuep ATTRIBUTE_UNUSED) 60 { 61 if (**strp == '#') 62 { 63 ++*strp; 64 return NULL; 65 } 66 return _("Missing '#' prefix"); 67 } 68 69 /* Handle '.' prefixes (i.e. skip over them). */ 70 71 static const char * 72 parse_dot (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 73 const char **strp, 74 int opindex ATTRIBUTE_UNUSED, 75 long *valuep ATTRIBUTE_UNUSED) 76 { 77 if (**strp == '.') 78 { 79 ++*strp; 80 return NULL; 81 } 82 return _("Missing '.' prefix"); 83 } 84 85 /* Handle 'pof:' prefixes (i.e. skip over them). */ 86 87 static const char * 88 parse_pof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 89 const char **strp, 90 int opindex ATTRIBUTE_UNUSED, 91 long *valuep ATTRIBUTE_UNUSED) 92 { 93 if (strncasecmp (*strp, "pof:", 4) == 0) 94 { 95 *strp += 4; 96 return NULL; 97 } 98 return _("Missing 'pof:' prefix"); 99 } 100 101 /* Handle 'pag:' prefixes (i.e. skip over them). */ 102 103 static const char * 104 parse_pag (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 105 const char **strp, 106 int opindex ATTRIBUTE_UNUSED, 107 long *valuep ATTRIBUTE_UNUSED) 108 { 109 if (strncasecmp (*strp, "pag:", 4) == 0) 110 { 111 *strp += 4; 112 return NULL; 113 } 114 return _("Missing 'pag:' prefix"); 115 } 116 117 /* Handle 'sof' prefixes (i.e. skip over them). */ 118 119 static const char * 120 parse_sof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 121 const char **strp, 122 int opindex ATTRIBUTE_UNUSED, 123 long *valuep ATTRIBUTE_UNUSED) 124 { 125 if (strncasecmp (*strp, "sof:", 4) == 0) 126 { 127 *strp += 4; 128 return NULL; 129 } 130 return _("Missing 'sof:' prefix"); 131 } 132 133 /* Handle 'seg' prefixes (i.e. skip over them). */ 134 135 static const char * 136 parse_seg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 137 const char **strp, 138 int opindex ATTRIBUTE_UNUSED, 139 long *valuep ATTRIBUTE_UNUSED) 140 { 141 if (strncasecmp (*strp, "seg:", 4) == 0) 142 { 143 *strp += 4; 144 return NULL; 145 } 146 return _("Missing 'seg:' prefix"); 147 } 148 /* -- */ 149 150 const char * xc16x_cgen_parse_operand 151 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); 152 153 /* Main entry point for operand parsing. 154 155 This function is basically just a big switch statement. Earlier versions 156 used tables to look up the function to use, but 157 - if the table contains both assembler and disassembler functions then 158 the disassembler contains much of the assembler and vice-versa, 159 - there's a lot of inlining possibilities as things grow, 160 - using a switch statement avoids the function call overhead. 161 162 This function could be moved into `parse_insn_normal', but keeping it 163 separate makes clear the interface between `parse_insn_normal' and each of 164 the handlers. */ 165 166 const char * 167 xc16x_cgen_parse_operand (CGEN_CPU_DESC cd, 168 int opindex, 169 const char ** strp, 170 CGEN_FIELDS * fields) 171 { 172 const char * errmsg = NULL; 173 /* Used by scalar operands that still need to be parsed. */ 174 long junk ATTRIBUTE_UNUSED; 175 176 switch (opindex) 177 { 178 case XC16X_OPERAND_REGNAM : 179 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_psw_names, & fields->f_reg8); 180 break; 181 case XC16X_OPERAND_BIT01 : 182 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT01, (unsigned long *) (& fields->f_op_1bit)); 183 break; 184 case XC16X_OPERAND_BIT1 : 185 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT1, (unsigned long *) (& fields->f_op_bit1)); 186 break; 187 case XC16X_OPERAND_BIT2 : 188 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT2, (unsigned long *) (& fields->f_op_bit2)); 189 break; 190 case XC16X_OPERAND_BIT4 : 191 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT4, (unsigned long *) (& fields->f_op_bit4)); 192 break; 193 case XC16X_OPERAND_BIT8 : 194 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT8, (unsigned long *) (& fields->f_op_bit8)); 195 break; 196 case XC16X_OPERAND_BITONE : 197 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BITONE, (unsigned long *) (& fields->f_op_onebit)); 198 break; 199 case XC16X_OPERAND_CADDR : 200 { 201 bfd_vma value = 0; 202 errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_CADDR, 0, NULL, & value); 203 fields->f_offset16 = value; 204 } 205 break; 206 case XC16X_OPERAND_COND : 207 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_conditioncode_names, & fields->f_condcode); 208 break; 209 case XC16X_OPERAND_DATA8 : 210 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_DATA8, (unsigned long *) (& fields->f_data8)); 211 break; 212 case XC16X_OPERAND_DATAHI8 : 213 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_DATAHI8, (unsigned long *) (& fields->f_datahi8)); 214 break; 215 case XC16X_OPERAND_DOT : 216 errmsg = parse_dot (cd, strp, XC16X_OPERAND_DOT, (long *) (& junk)); 217 break; 218 case XC16X_OPERAND_DR : 219 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r1); 220 break; 221 case XC16X_OPERAND_DRB : 222 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb_names, & fields->f_r1); 223 break; 224 case XC16X_OPERAND_DRI : 225 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r4); 226 break; 227 case XC16X_OPERAND_EXTCOND : 228 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_extconditioncode_names, & fields->f_extccode); 229 break; 230 case XC16X_OPERAND_GENREG : 231 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_regb8); 232 break; 233 case XC16X_OPERAND_HASH : 234 errmsg = parse_hash (cd, strp, XC16X_OPERAND_HASH, (long *) (& junk)); 235 break; 236 case XC16X_OPERAND_ICOND : 237 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_conditioncode_names, & fields->f_icondcode); 238 break; 239 case XC16X_OPERAND_LBIT2 : 240 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_LBIT2, (unsigned long *) (& fields->f_op_lbit2)); 241 break; 242 case XC16X_OPERAND_LBIT4 : 243 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_LBIT4, (unsigned long *) (& fields->f_op_lbit4)); 244 break; 245 case XC16X_OPERAND_MASK8 : 246 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_MASK8, (unsigned long *) (& fields->f_mask8)); 247 break; 248 case XC16X_OPERAND_MASKLO8 : 249 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_MASKLO8, (unsigned long *) (& fields->f_datahi8)); 250 break; 251 case XC16X_OPERAND_MEMGR8 : 252 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_memgr8_names, & fields->f_memgr8); 253 break; 254 case XC16X_OPERAND_MEMORY : 255 { 256 bfd_vma value = 0; 257 errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_MEMORY, 0, NULL, & value); 258 fields->f_memory = value; 259 } 260 break; 261 case XC16X_OPERAND_PAG : 262 errmsg = parse_pag (cd, strp, XC16X_OPERAND_PAG, (long *) (& junk)); 263 break; 264 case XC16X_OPERAND_PAGENUM : 265 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_PAGENUM, (unsigned long *) (& fields->f_pagenum)); 266 break; 267 case XC16X_OPERAND_POF : 268 errmsg = parse_pof (cd, strp, XC16X_OPERAND_POF, (long *) (& junk)); 269 break; 270 case XC16X_OPERAND_QBIT : 271 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QBIT, (unsigned long *) (& fields->f_qbit)); 272 break; 273 case XC16X_OPERAND_QHIBIT : 274 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QHIBIT, (unsigned long *) (& fields->f_qhibit)); 275 break; 276 case XC16X_OPERAND_QLOBIT : 277 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QLOBIT, (unsigned long *) (& fields->f_qlobit)); 278 break; 279 case XC16X_OPERAND_REG8 : 280 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_reg8); 281 break; 282 case XC16X_OPERAND_REGB8 : 283 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb8_names, & fields->f_regb8); 284 break; 285 case XC16X_OPERAND_REGBMEM8 : 286 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regbmem8_names, & fields->f_regmem8); 287 break; 288 case XC16X_OPERAND_REGHI8 : 289 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_reghi8); 290 break; 291 case XC16X_OPERAND_REGMEM8 : 292 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regmem8_names, & fields->f_regmem8); 293 break; 294 case XC16X_OPERAND_REGOFF8 : 295 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_regoff8); 296 break; 297 case XC16X_OPERAND_REL : 298 errmsg = cgen_parse_signed_integer (cd, strp, XC16X_OPERAND_REL, (long *) (& fields->f_rel8)); 299 break; 300 case XC16X_OPERAND_RELHI : 301 errmsg = cgen_parse_signed_integer (cd, strp, XC16X_OPERAND_RELHI, (long *) (& fields->f_relhi8)); 302 break; 303 case XC16X_OPERAND_SEG : 304 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_SEG, (unsigned long *) (& fields->f_seg8)); 305 break; 306 case XC16X_OPERAND_SEGHI8 : 307 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_SEGHI8, (unsigned long *) (& fields->f_segnum8)); 308 break; 309 case XC16X_OPERAND_SEGM : 310 errmsg = parse_seg (cd, strp, XC16X_OPERAND_SEGM, (long *) (& junk)); 311 break; 312 case XC16X_OPERAND_SOF : 313 errmsg = parse_sof (cd, strp, XC16X_OPERAND_SOF, (long *) (& junk)); 314 break; 315 case XC16X_OPERAND_SR : 316 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r2); 317 break; 318 case XC16X_OPERAND_SR2 : 319 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r0); 320 break; 321 case XC16X_OPERAND_SRB : 322 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb_names, & fields->f_r2); 323 break; 324 case XC16X_OPERAND_SRC1 : 325 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r1); 326 break; 327 case XC16X_OPERAND_SRC2 : 328 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r2); 329 break; 330 case XC16X_OPERAND_SRDIV : 331 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regdiv8_names, & fields->f_reg8); 332 break; 333 case XC16X_OPERAND_U4 : 334 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_reg0_name, & fields->f_uimm4); 335 break; 336 case XC16X_OPERAND_UIMM16 : 337 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16)); 338 break; 339 case XC16X_OPERAND_UIMM2 : 340 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_ext_names, & fields->f_uimm2); 341 break; 342 case XC16X_OPERAND_UIMM3 : 343 errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_reg0_name1, & fields->f_uimm3); 344 break; 345 case XC16X_OPERAND_UIMM4 : 346 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4)); 347 break; 348 case XC16X_OPERAND_UIMM7 : 349 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM7, (unsigned long *) (& fields->f_uimm7)); 350 break; 351 case XC16X_OPERAND_UIMM8 : 352 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8)); 353 break; 354 case XC16X_OPERAND_UPAG16 : 355 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UPAG16, (unsigned long *) (& fields->f_uimm16)); 356 break; 357 case XC16X_OPERAND_UPOF16 : 358 { 359 bfd_vma value = 0; 360 errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_UPOF16, 0, NULL, & value); 361 fields->f_memory = value; 362 } 363 break; 364 case XC16X_OPERAND_USEG16 : 365 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USEG16, (unsigned long *) (& fields->f_offset16)); 366 break; 367 case XC16X_OPERAND_USEG8 : 368 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USEG8, (unsigned long *) (& fields->f_seg8)); 369 break; 370 case XC16X_OPERAND_USOF16 : 371 errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USOF16, (unsigned long *) (& fields->f_offset16)); 372 break; 373 374 default : 375 /* xgettext:c-format */ 376 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); 377 abort (); 378 } 379 380 return errmsg; 381 } 382 383 cgen_parse_fn * const xc16x_cgen_parse_handlers[] = 384 { 385 parse_insn_normal, 386 }; 387 388 void 389 xc16x_cgen_init_asm (CGEN_CPU_DESC cd) 390 { 391 xc16x_cgen_init_opcode_table (cd); 392 xc16x_cgen_init_ibld_table (cd); 393 cd->parse_handlers = & xc16x_cgen_parse_handlers[0]; 394 cd->parse_operand = xc16x_cgen_parse_operand; 395 #ifdef CGEN_ASM_INIT_HOOK 396 CGEN_ASM_INIT_HOOK 397 #endif 398 } 399 400 401 403 /* Regex construction routine. 404 405 This translates an opcode syntax string into a regex string, 406 by replacing any non-character syntax element (such as an 407 opcode) with the pattern '.*' 408 409 It then compiles the regex and stores it in the opcode, for 410 later use by xc16x_cgen_assemble_insn 411 412 Returns NULL for success, an error message for failure. */ 413 414 char * 415 xc16x_cgen_build_insn_regex (CGEN_INSN *insn) 416 { 417 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); 418 const char *mnem = CGEN_INSN_MNEMONIC (insn); 419 char rxbuf[CGEN_MAX_RX_ELEMENTS]; 420 char *rx = rxbuf; 421 const CGEN_SYNTAX_CHAR_TYPE *syn; 422 int reg_err; 423 424 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); 425 426 /* Mnemonics come first in the syntax string. */ 427 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 428 return _("missing mnemonic in syntax string"); 429 ++syn; 430 431 /* Generate a case sensitive regular expression that emulates case 432 insensitive matching in the "C" locale. We cannot generate a case 433 insensitive regular expression because in Turkish locales, 'i' and 'I' 434 are not equal modulo case conversion. */ 435 436 /* Copy the literal mnemonic out of the insn. */ 437 for (; *mnem; mnem++) 438 { 439 char c = *mnem; 440 441 if (ISALPHA (c)) 442 { 443 *rx++ = '['; 444 *rx++ = TOLOWER (c); 445 *rx++ = TOUPPER (c); 446 *rx++ = ']'; 447 } 448 else 449 *rx++ = c; 450 } 451 452 /* Copy any remaining literals from the syntax string into the rx. */ 453 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) 454 { 455 if (CGEN_SYNTAX_CHAR_P (* syn)) 456 { 457 char c = CGEN_SYNTAX_CHAR (* syn); 458 459 switch (c) 460 { 461 /* Escape any regex metacharacters in the syntax. */ 462 case '.': case '[': case '\\': 463 case '*': case '^': case '$': 464 465 #ifdef CGEN_ESCAPE_EXTENDED_REGEX 466 case '?': case '{': case '}': 467 case '(': case ')': case '*': 468 case '|': case '+': case ']': 469 #endif 470 *rx++ = '\\'; 471 *rx++ = c; 472 break; 473 474 default: 475 if (ISALPHA (c)) 476 { 477 *rx++ = '['; 478 *rx++ = TOLOWER (c); 479 *rx++ = TOUPPER (c); 480 *rx++ = ']'; 481 } 482 else 483 *rx++ = c; 484 break; 485 } 486 } 487 else 488 { 489 /* Replace non-syntax fields with globs. */ 490 *rx++ = '.'; 491 *rx++ = '*'; 492 } 493 } 494 495 /* Trailing whitespace ok. */ 496 * rx++ = '['; 497 * rx++ = ' '; 498 * rx++ = '\t'; 499 * rx++ = ']'; 500 * rx++ = '*'; 501 502 /* But anchor it after that. */ 503 * rx++ = '$'; 504 * rx = '\0'; 505 506 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); 507 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); 508 509 if (reg_err == 0) 510 return NULL; 511 else 512 { 513 static char msg[80]; 514 515 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); 516 regfree ((regex_t *) CGEN_INSN_RX (insn)); 517 free (CGEN_INSN_RX (insn)); 518 (CGEN_INSN_RX (insn)) = NULL; 519 return msg; 520 } 521 } 522 523 524 /* Default insn parser. 526 527 The syntax string is scanned and operands are parsed and stored in FIELDS. 528 Relocs are queued as we go via other callbacks. 529 530 ??? Note that this is currently an all-or-nothing parser. If we fail to 531 parse the instruction, we return 0 and the caller will start over from 532 the beginning. Backtracking will be necessary in parsing subexpressions, 533 but that can be handled there. Not handling backtracking here may get 534 expensive in the case of the m68k. Deal with later. 535 536 Returns NULL for success, an error message for failure. */ 537 538 static const char * 539 parse_insn_normal (CGEN_CPU_DESC cd, 540 const CGEN_INSN *insn, 541 const char **strp, 542 CGEN_FIELDS *fields) 543 { 544 /* ??? Runtime added insns not handled yet. */ 545 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); 546 const char *str = *strp; 547 const char *errmsg; 548 const char *p; 549 const CGEN_SYNTAX_CHAR_TYPE * syn; 550 #ifdef CGEN_MNEMONIC_OPERANDS 551 /* FIXME: wip */ 552 int past_opcode_p; 553 #endif 554 555 /* For now we assume the mnemonic is first (there are no leading operands). 556 We can parse it without needing to set up operand parsing. 557 GAS's input scrubber will ensure mnemonics are lowercase, but we may 558 not be called from GAS. */ 559 p = CGEN_INSN_MNEMONIC (insn); 560 while (*p && TOLOWER (*p) == TOLOWER (*str)) 561 ++p, ++str; 562 563 if (* p) 564 return _("unrecognized instruction"); 565 566 #ifndef CGEN_MNEMONIC_OPERANDS 567 if (* str && ! ISSPACE (* str)) 568 return _("unrecognized instruction"); 569 #endif 570 571 CGEN_INIT_PARSE (cd); 572 cgen_init_parse_operand (cd); 573 #ifdef CGEN_MNEMONIC_OPERANDS 574 past_opcode_p = 0; 575 #endif 576 577 /* We don't check for (*str != '\0') here because we want to parse 578 any trailing fake arguments in the syntax string. */ 579 syn = CGEN_SYNTAX_STRING (syntax); 580 581 /* Mnemonics come first for now, ensure valid string. */ 582 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) 583 abort (); 584 585 ++syn; 586 587 while (* syn != 0) 588 { 589 /* Non operand chars must match exactly. */ 590 if (CGEN_SYNTAX_CHAR_P (* syn)) 591 { 592 /* FIXME: While we allow for non-GAS callers above, we assume the 593 first char after the mnemonic part is a space. */ 594 /* FIXME: We also take inappropriate advantage of the fact that 595 GAS's input scrubber will remove extraneous blanks. */ 596 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) 597 { 598 #ifdef CGEN_MNEMONIC_OPERANDS 599 if (CGEN_SYNTAX_CHAR(* syn) == ' ') 600 past_opcode_p = 1; 601 #endif 602 ++ syn; 603 ++ str; 604 } 605 else if (*str) 606 { 607 /* Syntax char didn't match. Can't be this insn. */ 608 static char msg [80]; 609 610 /* xgettext:c-format */ 611 sprintf (msg, _("syntax error (expected char `%c', found `%c')"), 612 CGEN_SYNTAX_CHAR(*syn), *str); 613 return msg; 614 } 615 else 616 { 617 /* Ran out of input. */ 618 static char msg [80]; 619 620 /* xgettext:c-format */ 621 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), 622 CGEN_SYNTAX_CHAR(*syn)); 623 return msg; 624 } 625 continue; 626 } 627 628 #ifdef CGEN_MNEMONIC_OPERANDS 629 (void) past_opcode_p; 630 #endif 631 /* We have an operand of some sort. */ 632 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields); 633 if (errmsg) 634 return errmsg; 635 636 /* Done with this operand, continue with next one. */ 637 ++ syn; 638 } 639 640 /* If we're at the end of the syntax string, we're done. */ 641 if (* syn == 0) 642 { 643 /* FIXME: For the moment we assume a valid `str' can only contain 644 blanks now. IE: We needn't try again with a longer version of 645 the insn and it is assumed that longer versions of insns appear 646 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ 647 while (ISSPACE (* str)) 648 ++ str; 649 650 if (* str != '\0') 651 return _("junk at end of line"); /* FIXME: would like to include `str' */ 652 653 return NULL; 654 } 655 656 /* We couldn't parse it. */ 657 return _("unrecognized instruction"); 658 } 659 660 /* Main entry point. 662 This routine is called for each instruction to be assembled. 663 STR points to the insn to be assembled. 664 We assume all necessary tables have been initialized. 665 The assembled instruction, less any fixups, is stored in BUF. 666 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value 667 still needs to be converted to target byte order, otherwise BUF is an array 668 of bytes in target byte order. 669 The result is a pointer to the insn's entry in the opcode table, 670 or NULL if an error occured (an error message will have already been 671 printed). 672 673 Note that when processing (non-alias) macro-insns, 674 this function recurses. 675 676 ??? It's possible to make this cpu-independent. 677 One would have to deal with a few minor things. 678 At this point in time doing so would be more of a curiosity than useful 679 [for example this file isn't _that_ big], but keeping the possibility in 680 mind helps keep the design clean. */ 681 682 const CGEN_INSN * 683 xc16x_cgen_assemble_insn (CGEN_CPU_DESC cd, 684 const char *str, 685 CGEN_FIELDS *fields, 686 CGEN_INSN_BYTES_PTR buf, 687 char **errmsg) 688 { 689 const char *start; 690 CGEN_INSN_LIST *ilist; 691 const char *parse_errmsg = NULL; 692 const char *insert_errmsg = NULL; 693 int recognized_mnemonic = 0; 694 695 /* Skip leading white space. */ 696 while (ISSPACE (* str)) 697 ++ str; 698 699 /* The instructions are stored in hashed lists. 700 Get the first in the list. */ 701 ilist = CGEN_ASM_LOOKUP_INSN (cd, str); 702 703 /* Keep looking until we find a match. */ 704 start = str; 705 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) 706 { 707 const CGEN_INSN *insn = ilist->insn; 708 recognized_mnemonic = 1; 709 710 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 711 /* Not usually needed as unsupported opcodes 712 shouldn't be in the hash lists. */ 713 /* Is this insn supported by the selected cpu? */ 714 if (! xc16x_cgen_insn_supported (cd, insn)) 715 continue; 716 #endif 717 /* If the RELAXED attribute is set, this is an insn that shouldn't be 718 chosen immediately. Instead, it is used during assembler/linker 719 relaxation if possible. */ 720 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) 721 continue; 722 723 str = start; 724 725 /* Skip this insn if str doesn't look right lexically. */ 726 if (CGEN_INSN_RX (insn) != NULL && 727 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) 728 continue; 729 730 /* Allow parse/insert handlers to obtain length of insn. */ 731 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); 732 733 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); 734 if (parse_errmsg != NULL) 735 continue; 736 737 /* ??? 0 is passed for `pc'. */ 738 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, 739 (bfd_vma) 0); 740 if (insert_errmsg != NULL) 741 continue; 742 743 /* It is up to the caller to actually output the insn and any 744 queued relocs. */ 745 return insn; 746 } 747 748 { 749 static char errbuf[150]; 750 const char *tmp_errmsg; 751 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS 752 #define be_verbose 1 753 #else 754 #define be_verbose 0 755 #endif 756 757 if (be_verbose) 758 { 759 /* If requesting verbose error messages, use insert_errmsg. 760 Failing that, use parse_errmsg. */ 761 tmp_errmsg = (insert_errmsg ? insert_errmsg : 762 parse_errmsg ? parse_errmsg : 763 recognized_mnemonic ? 764 _("unrecognized form of instruction") : 765 _("unrecognized instruction")); 766 767 if (strlen (start) > 50) 768 /* xgettext:c-format */ 769 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); 770 else 771 /* xgettext:c-format */ 772 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); 773 } 774 else 775 { 776 if (strlen (start) > 50) 777 /* xgettext:c-format */ 778 sprintf (errbuf, _("bad instruction `%.50s...'"), start); 779 else 780 /* xgettext:c-format */ 781 sprintf (errbuf, _("bad instruction `%.50s'"), start); 782 } 783 784 *errmsg = errbuf; 785 return NULL; 786 } 787 } 788