| 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 "m32r-desc.h" |
| 35 | #include "m32r-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 | \f |
| 49 | /* -- assembler routines inserted here. */ |
| 50 | |
| 51 | /* -- asm.c */ |
| 52 | static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'"); |
| 53 | |
| 54 | /* Handle '#' prefixes (i.e. skip over them). */ |
| 55 | |
| 56 | static const char * |
| 57 | parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
| 58 | const char **strp, |
| 59 | int opindex ATTRIBUTE_UNUSED, |
| 60 | long *valuep ATTRIBUTE_UNUSED) |
| 61 | { |
| 62 | if (**strp == '#') |
| 63 | ++*strp; |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | /* Handle shigh(), high(). */ |
| 68 | |
| 69 | static const char * |
| 70 | parse_hi16 (CGEN_CPU_DESC cd, |
| 71 | const char **strp, |
| 72 | int opindex, |
| 73 | unsigned long *valuep) |
| 74 | { |
| 75 | const char *errmsg; |
| 76 | enum cgen_parse_operand_result result_type; |
| 77 | bfd_vma value; |
| 78 | |
| 79 | if (**strp == '#') |
| 80 | ++*strp; |
| 81 | |
| 82 | if (strncasecmp (*strp, "high(", 5) == 0) |
| 83 | { |
| 84 | *strp += 5; |
| 85 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO, |
| 86 | & result_type, & value); |
| 87 | if (**strp != ')') |
| 88 | return MISSING_CLOSING_PARENTHESIS; |
| 89 | ++*strp; |
| 90 | if (errmsg == NULL |
| 91 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
| 92 | { |
| 93 | value >>= 16; |
| 94 | value &= 0xffff; |
| 95 | } |
| 96 | *valuep = value; |
| 97 | return errmsg; |
| 98 | } |
| 99 | else if (strncasecmp (*strp, "shigh(", 6) == 0) |
| 100 | { |
| 101 | *strp += 6; |
| 102 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO, |
| 103 | & result_type, & value); |
| 104 | if (**strp != ')') |
| 105 | return MISSING_CLOSING_PARENTHESIS; |
| 106 | ++*strp; |
| 107 | if (errmsg == NULL |
| 108 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
| 109 | { |
| 110 | value += 0x8000; |
| 111 | value >>= 16; |
| 112 | value &= 0xffff; |
| 113 | } |
| 114 | *valuep = value; |
| 115 | return errmsg; |
| 116 | } |
| 117 | |
| 118 | return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
| 119 | } |
| 120 | |
| 121 | /* Handle low() in a signed context. Also handle sda(). |
| 122 | The signedness of the value doesn't matter to low(), but this also |
| 123 | handles the case where low() isn't present. */ |
| 124 | |
| 125 | static const char * |
| 126 | parse_slo16 (CGEN_CPU_DESC cd, |
| 127 | const char ** strp, |
| 128 | int opindex, |
| 129 | long * valuep) |
| 130 | { |
| 131 | const char *errmsg; |
| 132 | enum cgen_parse_operand_result result_type; |
| 133 | bfd_vma value; |
| 134 | |
| 135 | if (**strp == '#') |
| 136 | ++*strp; |
| 137 | |
| 138 | if (strncasecmp (*strp, "low(", 4) == 0) |
| 139 | { |
| 140 | *strp += 4; |
| 141 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, |
| 142 | & result_type, & value); |
| 143 | if (**strp != ')') |
| 144 | return MISSING_CLOSING_PARENTHESIS; |
| 145 | ++*strp; |
| 146 | if (errmsg == NULL |
| 147 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
| 148 | value = ((value & 0xffff) ^ 0x8000) - 0x8000; |
| 149 | *valuep = value; |
| 150 | return errmsg; |
| 151 | } |
| 152 | |
| 153 | if (strncasecmp (*strp, "sda(", 4) == 0) |
| 154 | { |
| 155 | *strp += 4; |
| 156 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16, |
| 157 | NULL, & value); |
| 158 | if (**strp != ')') |
| 159 | return MISSING_CLOSING_PARENTHESIS; |
| 160 | ++*strp; |
| 161 | *valuep = value; |
| 162 | return errmsg; |
| 163 | } |
| 164 | |
| 165 | return cgen_parse_signed_integer (cd, strp, opindex, valuep); |
| 166 | } |
| 167 | |
| 168 | /* Handle low() in an unsigned context. |
| 169 | The signedness of the value doesn't matter to low(), but this also |
| 170 | handles the case where low() isn't present. */ |
| 171 | |
| 172 | static const char * |
| 173 | parse_ulo16 (CGEN_CPU_DESC cd, |
| 174 | const char **strp, |
| 175 | int opindex, |
| 176 | unsigned long *valuep) |
| 177 | { |
| 178 | const char *errmsg; |
| 179 | enum cgen_parse_operand_result result_type; |
| 180 | bfd_vma value; |
| 181 | |
| 182 | if (**strp == '#') |
| 183 | ++*strp; |
| 184 | |
| 185 | if (strncasecmp (*strp, "low(", 4) == 0) |
| 186 | { |
| 187 | *strp += 4; |
| 188 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, |
| 189 | & result_type, & value); |
| 190 | if (**strp != ')') |
| 191 | return MISSING_CLOSING_PARENTHESIS; |
| 192 | ++*strp; |
| 193 | if (errmsg == NULL |
| 194 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
| 195 | value &= 0xffff; |
| 196 | *valuep = value; |
| 197 | return errmsg; |
| 198 | } |
| 199 | |
| 200 | return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
| 201 | } |
| 202 | |
| 203 | /* -- */ |
| 204 | |
| 205 | const char * m32r_cgen_parse_operand |
| 206 | (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); |
| 207 | |
| 208 | /* Main entry point for operand parsing. |
| 209 | |
| 210 | This function is basically just a big switch statement. Earlier versions |
| 211 | used tables to look up the function to use, but |
| 212 | - if the table contains both assembler and disassembler functions then |
| 213 | the disassembler contains much of the assembler and vice-versa, |
| 214 | - there's a lot of inlining possibilities as things grow, |
| 215 | - using a switch statement avoids the function call overhead. |
| 216 | |
| 217 | This function could be moved into `parse_insn_normal', but keeping it |
| 218 | separate makes clear the interface between `parse_insn_normal' and each of |
| 219 | the handlers. */ |
| 220 | |
| 221 | const char * |
| 222 | m32r_cgen_parse_operand (CGEN_CPU_DESC cd, |
| 223 | int opindex, |
| 224 | const char ** strp, |
| 225 | CGEN_FIELDS * fields) |
| 226 | { |
| 227 | const char * errmsg = NULL; |
| 228 | /* Used by scalar operands that still need to be parsed. */ |
| 229 | long junk ATTRIBUTE_UNUSED; |
| 230 | |
| 231 | switch (opindex) |
| 232 | { |
| 233 | case M32R_OPERAND_ACC : |
| 234 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_acc); |
| 235 | break; |
| 236 | case M32R_OPERAND_ACCD : |
| 237 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accd); |
| 238 | break; |
| 239 | case M32R_OPERAND_ACCS : |
| 240 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accs); |
| 241 | break; |
| 242 | case M32R_OPERAND_DCR : |
| 243 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r1); |
| 244 | break; |
| 245 | case M32R_OPERAND_DISP16 : |
| 246 | { |
| 247 | bfd_vma value = 0; |
| 248 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP16, 0, NULL, & value); |
| 249 | fields->f_disp16 = value; |
| 250 | } |
| 251 | break; |
| 252 | case M32R_OPERAND_DISP24 : |
| 253 | { |
| 254 | bfd_vma value = 0; |
| 255 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP24, 0, NULL, & value); |
| 256 | fields->f_disp24 = value; |
| 257 | } |
| 258 | break; |
| 259 | case M32R_OPERAND_DISP8 : |
| 260 | { |
| 261 | bfd_vma value = 0; |
| 262 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP8, 0, NULL, & value); |
| 263 | fields->f_disp8 = value; |
| 264 | } |
| 265 | break; |
| 266 | case M32R_OPERAND_DR : |
| 267 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1); |
| 268 | break; |
| 269 | case M32R_OPERAND_HASH : |
| 270 | errmsg = parse_hash (cd, strp, M32R_OPERAND_HASH, (long *) (& junk)); |
| 271 | break; |
| 272 | case M32R_OPERAND_HI16 : |
| 273 | errmsg = parse_hi16 (cd, strp, M32R_OPERAND_HI16, (unsigned long *) (& fields->f_hi16)); |
| 274 | break; |
| 275 | case M32R_OPERAND_IMM1 : |
| 276 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_IMM1, (unsigned long *) (& fields->f_imm1)); |
| 277 | break; |
| 278 | case M32R_OPERAND_SCR : |
| 279 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r2); |
| 280 | break; |
| 281 | case M32R_OPERAND_SIMM16 : |
| 282 | errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM16, (long *) (& fields->f_simm16)); |
| 283 | break; |
| 284 | case M32R_OPERAND_SIMM8 : |
| 285 | errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM8, (long *) (& fields->f_simm8)); |
| 286 | break; |
| 287 | case M32R_OPERAND_SLO16 : |
| 288 | errmsg = parse_slo16 (cd, strp, M32R_OPERAND_SLO16, (long *) (& fields->f_simm16)); |
| 289 | break; |
| 290 | case M32R_OPERAND_SR : |
| 291 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2); |
| 292 | break; |
| 293 | case M32R_OPERAND_SRC1 : |
| 294 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1); |
| 295 | break; |
| 296 | case M32R_OPERAND_SRC2 : |
| 297 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2); |
| 298 | break; |
| 299 | case M32R_OPERAND_UIMM16 : |
| 300 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16)); |
| 301 | break; |
| 302 | case M32R_OPERAND_UIMM24 : |
| 303 | { |
| 304 | bfd_vma value = 0; |
| 305 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_UIMM24, 0, NULL, & value); |
| 306 | fields->f_uimm24 = value; |
| 307 | } |
| 308 | break; |
| 309 | case M32R_OPERAND_UIMM3 : |
| 310 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM3, (unsigned long *) (& fields->f_uimm3)); |
| 311 | break; |
| 312 | case M32R_OPERAND_UIMM4 : |
| 313 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4)); |
| 314 | break; |
| 315 | case M32R_OPERAND_UIMM5 : |
| 316 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM5, (unsigned long *) (& fields->f_uimm5)); |
| 317 | break; |
| 318 | case M32R_OPERAND_UIMM8 : |
| 319 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8)); |
| 320 | break; |
| 321 | case M32R_OPERAND_ULO16 : |
| 322 | errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, (unsigned long *) (& fields->f_uimm16)); |
| 323 | break; |
| 324 | |
| 325 | default : |
| 326 | /* xgettext:c-format */ |
| 327 | fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); |
| 328 | abort (); |
| 329 | } |
| 330 | |
| 331 | return errmsg; |
| 332 | } |
| 333 | |
| 334 | cgen_parse_fn * const m32r_cgen_parse_handlers[] = |
| 335 | { |
| 336 | parse_insn_normal, |
| 337 | }; |
| 338 | |
| 339 | void |
| 340 | m32r_cgen_init_asm (CGEN_CPU_DESC cd) |
| 341 | { |
| 342 | m32r_cgen_init_opcode_table (cd); |
| 343 | m32r_cgen_init_ibld_table (cd); |
| 344 | cd->parse_handlers = & m32r_cgen_parse_handlers[0]; |
| 345 | cd->parse_operand = m32r_cgen_parse_operand; |
| 346 | #ifdef CGEN_ASM_INIT_HOOK |
| 347 | CGEN_ASM_INIT_HOOK |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | \f |
| 352 | |
| 353 | /* Regex construction routine. |
| 354 | |
| 355 | This translates an opcode syntax string into a regex string, |
| 356 | by replacing any non-character syntax element (such as an |
| 357 | opcode) with the pattern '.*' |
| 358 | |
| 359 | It then compiles the regex and stores it in the opcode, for |
| 360 | later use by m32r_cgen_assemble_insn |
| 361 | |
| 362 | Returns NULL for success, an error message for failure. */ |
| 363 | |
| 364 | char * |
| 365 | m32r_cgen_build_insn_regex (CGEN_INSN *insn) |
| 366 | { |
| 367 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
| 368 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
| 369 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
| 370 | char *rx = rxbuf; |
| 371 | const CGEN_SYNTAX_CHAR_TYPE *syn; |
| 372 | int reg_err; |
| 373 | |
| 374 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); |
| 375 | |
| 376 | /* Mnemonics come first in the syntax string. */ |
| 377 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
| 378 | return _("missing mnemonic in syntax string"); |
| 379 | ++syn; |
| 380 | |
| 381 | /* Generate a case sensitive regular expression that emulates case |
| 382 | insensitive matching in the "C" locale. We cannot generate a case |
| 383 | insensitive regular expression because in Turkish locales, 'i' and 'I' |
| 384 | are not equal modulo case conversion. */ |
| 385 | |
| 386 | /* Copy the literal mnemonic out of the insn. */ |
| 387 | for (; *mnem; mnem++) |
| 388 | { |
| 389 | char c = *mnem; |
| 390 | |
| 391 | if (ISALPHA (c)) |
| 392 | { |
| 393 | *rx++ = '['; |
| 394 | *rx++ = TOLOWER (c); |
| 395 | *rx++ = TOUPPER (c); |
| 396 | *rx++ = ']'; |
| 397 | } |
| 398 | else |
| 399 | *rx++ = c; |
| 400 | } |
| 401 | |
| 402 | /* Copy any remaining literals from the syntax string into the rx. */ |
| 403 | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) |
| 404 | { |
| 405 | if (CGEN_SYNTAX_CHAR_P (* syn)) |
| 406 | { |
| 407 | char c = CGEN_SYNTAX_CHAR (* syn); |
| 408 | |
| 409 | switch (c) |
| 410 | { |
| 411 | /* Escape any regex metacharacters in the syntax. */ |
| 412 | case '.': case '[': case '\\': |
| 413 | case '*': case '^': case '$': |
| 414 | |
| 415 | #ifdef CGEN_ESCAPE_EXTENDED_REGEX |
| 416 | case '?': case '{': case '}': |
| 417 | case '(': case ')': case '*': |
| 418 | case '|': case '+': case ']': |
| 419 | #endif |
| 420 | *rx++ = '\\'; |
| 421 | *rx++ = c; |
| 422 | break; |
| 423 | |
| 424 | default: |
| 425 | if (ISALPHA (c)) |
| 426 | { |
| 427 | *rx++ = '['; |
| 428 | *rx++ = TOLOWER (c); |
| 429 | *rx++ = TOUPPER (c); |
| 430 | *rx++ = ']'; |
| 431 | } |
| 432 | else |
| 433 | *rx++ = c; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | /* Replace non-syntax fields with globs. */ |
| 440 | *rx++ = '.'; |
| 441 | *rx++ = '*'; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /* Trailing whitespace ok. */ |
| 446 | * rx++ = '['; |
| 447 | * rx++ = ' '; |
| 448 | * rx++ = '\t'; |
| 449 | * rx++ = ']'; |
| 450 | * rx++ = '*'; |
| 451 | |
| 452 | /* But anchor it after that. */ |
| 453 | * rx++ = '$'; |
| 454 | * rx = '\0'; |
| 455 | |
| 456 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); |
| 457 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
| 458 | |
| 459 | if (reg_err == 0) |
| 460 | return NULL; |
| 461 | else |
| 462 | { |
| 463 | static char msg[80]; |
| 464 | |
| 465 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
| 466 | regfree ((regex_t *) CGEN_INSN_RX (insn)); |
| 467 | free (CGEN_INSN_RX (insn)); |
| 468 | (CGEN_INSN_RX (insn)) = NULL; |
| 469 | return msg; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | \f |
| 474 | /* Default insn parser. |
| 475 | |
| 476 | The syntax string is scanned and operands are parsed and stored in FIELDS. |
| 477 | Relocs are queued as we go via other callbacks. |
| 478 | |
| 479 | ??? Note that this is currently an all-or-nothing parser. If we fail to |
| 480 | parse the instruction, we return 0 and the caller will start over from |
| 481 | the beginning. Backtracking will be necessary in parsing subexpressions, |
| 482 | but that can be handled there. Not handling backtracking here may get |
| 483 | expensive in the case of the m68k. Deal with later. |
| 484 | |
| 485 | Returns NULL for success, an error message for failure. */ |
| 486 | |
| 487 | static const char * |
| 488 | parse_insn_normal (CGEN_CPU_DESC cd, |
| 489 | const CGEN_INSN *insn, |
| 490 | const char **strp, |
| 491 | CGEN_FIELDS *fields) |
| 492 | { |
| 493 | /* ??? Runtime added insns not handled yet. */ |
| 494 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); |
| 495 | const char *str = *strp; |
| 496 | const char *errmsg; |
| 497 | const char *p; |
| 498 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
| 499 | #ifdef CGEN_MNEMONIC_OPERANDS |
| 500 | /* FIXME: wip */ |
| 501 | int past_opcode_p; |
| 502 | #endif |
| 503 | |
| 504 | /* For now we assume the mnemonic is first (there are no leading operands). |
| 505 | We can parse it without needing to set up operand parsing. |
| 506 | GAS's input scrubber will ensure mnemonics are lowercase, but we may |
| 507 | not be called from GAS. */ |
| 508 | p = CGEN_INSN_MNEMONIC (insn); |
| 509 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
| 510 | ++p, ++str; |
| 511 | |
| 512 | if (* p) |
| 513 | return _("unrecognized instruction"); |
| 514 | |
| 515 | #ifndef CGEN_MNEMONIC_OPERANDS |
| 516 | if (* str && ! ISSPACE (* str)) |
| 517 | return _("unrecognized instruction"); |
| 518 | #endif |
| 519 | |
| 520 | CGEN_INIT_PARSE (cd); |
| 521 | cgen_init_parse_operand (cd); |
| 522 | #ifdef CGEN_MNEMONIC_OPERANDS |
| 523 | past_opcode_p = 0; |
| 524 | #endif |
| 525 | |
| 526 | /* We don't check for (*str != '\0') here because we want to parse |
| 527 | any trailing fake arguments in the syntax string. */ |
| 528 | syn = CGEN_SYNTAX_STRING (syntax); |
| 529 | |
| 530 | /* Mnemonics come first for now, ensure valid string. */ |
| 531 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
| 532 | abort (); |
| 533 | |
| 534 | ++syn; |
| 535 | |
| 536 | while (* syn != 0) |
| 537 | { |
| 538 | /* Non operand chars must match exactly. */ |
| 539 | if (CGEN_SYNTAX_CHAR_P (* syn)) |
| 540 | { |
| 541 | /* FIXME: While we allow for non-GAS callers above, we assume the |
| 542 | first char after the mnemonic part is a space. */ |
| 543 | /* FIXME: We also take inappropriate advantage of the fact that |
| 544 | GAS's input scrubber will remove extraneous blanks. */ |
| 545 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
| 546 | { |
| 547 | #ifdef CGEN_MNEMONIC_OPERANDS |
| 548 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
| 549 | past_opcode_p = 1; |
| 550 | #endif |
| 551 | ++ syn; |
| 552 | ++ str; |
| 553 | } |
| 554 | else if (*str) |
| 555 | { |
| 556 | /* Syntax char didn't match. Can't be this insn. */ |
| 557 | static char msg [80]; |
| 558 | |
| 559 | /* xgettext:c-format */ |
| 560 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), |
| 561 | CGEN_SYNTAX_CHAR(*syn), *str); |
| 562 | return msg; |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | /* Ran out of input. */ |
| 567 | static char msg [80]; |
| 568 | |
| 569 | /* xgettext:c-format */ |
| 570 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), |
| 571 | CGEN_SYNTAX_CHAR(*syn)); |
| 572 | return msg; |
| 573 | } |
| 574 | continue; |
| 575 | } |
| 576 | |
| 577 | #ifdef CGEN_MNEMONIC_OPERANDS |
| 578 | (void) past_opcode_p; |
| 579 | #endif |
| 580 | /* We have an operand of some sort. */ |
| 581 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields); |
| 582 | if (errmsg) |
| 583 | return errmsg; |
| 584 | |
| 585 | /* Done with this operand, continue with next one. */ |
| 586 | ++ syn; |
| 587 | } |
| 588 | |
| 589 | /* If we're at the end of the syntax string, we're done. */ |
| 590 | if (* syn == 0) |
| 591 | { |
| 592 | /* FIXME: For the moment we assume a valid `str' can only contain |
| 593 | blanks now. IE: We needn't try again with a longer version of |
| 594 | the insn and it is assumed that longer versions of insns appear |
| 595 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ |
| 596 | while (ISSPACE (* str)) |
| 597 | ++ str; |
| 598 | |
| 599 | if (* str != '\0') |
| 600 | return _("junk at end of line"); /* FIXME: would like to include `str' */ |
| 601 | |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | /* We couldn't parse it. */ |
| 606 | return _("unrecognized instruction"); |
| 607 | } |
| 608 | \f |
| 609 | /* Main entry point. |
| 610 | This routine is called for each instruction to be assembled. |
| 611 | STR points to the insn to be assembled. |
| 612 | We assume all necessary tables have been initialized. |
| 613 | The assembled instruction, less any fixups, is stored in BUF. |
| 614 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value |
| 615 | still needs to be converted to target byte order, otherwise BUF is an array |
| 616 | of bytes in target byte order. |
| 617 | The result is a pointer to the insn's entry in the opcode table, |
| 618 | or NULL if an error occured (an error message will have already been |
| 619 | printed). |
| 620 | |
| 621 | Note that when processing (non-alias) macro-insns, |
| 622 | this function recurses. |
| 623 | |
| 624 | ??? It's possible to make this cpu-independent. |
| 625 | One would have to deal with a few minor things. |
| 626 | At this point in time doing so would be more of a curiosity than useful |
| 627 | [for example this file isn't _that_ big], but keeping the possibility in |
| 628 | mind helps keep the design clean. */ |
| 629 | |
| 630 | const CGEN_INSN * |
| 631 | m32r_cgen_assemble_insn (CGEN_CPU_DESC cd, |
| 632 | const char *str, |
| 633 | CGEN_FIELDS *fields, |
| 634 | CGEN_INSN_BYTES_PTR buf, |
| 635 | char **errmsg) |
| 636 | { |
| 637 | const char *start; |
| 638 | CGEN_INSN_LIST *ilist; |
| 639 | const char *parse_errmsg = NULL; |
| 640 | const char *insert_errmsg = NULL; |
| 641 | int recognized_mnemonic = 0; |
| 642 | |
| 643 | /* Skip leading white space. */ |
| 644 | while (ISSPACE (* str)) |
| 645 | ++ str; |
| 646 | |
| 647 | /* The instructions are stored in hashed lists. |
| 648 | Get the first in the list. */ |
| 649 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); |
| 650 | |
| 651 | /* Keep looking until we find a match. */ |
| 652 | start = str; |
| 653 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) |
| 654 | { |
| 655 | const CGEN_INSN *insn = ilist->insn; |
| 656 | recognized_mnemonic = 1; |
| 657 | |
| 658 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
| 659 | /* Not usually needed as unsupported opcodes |
| 660 | shouldn't be in the hash lists. */ |
| 661 | /* Is this insn supported by the selected cpu? */ |
| 662 | if (! m32r_cgen_insn_supported (cd, insn)) |
| 663 | continue; |
| 664 | #endif |
| 665 | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
| 666 | chosen immediately. Instead, it is used during assembler/linker |
| 667 | relaxation if possible. */ |
| 668 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
| 669 | continue; |
| 670 | |
| 671 | str = start; |
| 672 | |
| 673 | /* Skip this insn if str doesn't look right lexically. */ |
| 674 | if (CGEN_INSN_RX (insn) != NULL && |
| 675 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) |
| 676 | continue; |
| 677 | |
| 678 | /* Allow parse/insert handlers to obtain length of insn. */ |
| 679 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); |
| 680 | |
| 681 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
| 682 | if (parse_errmsg != NULL) |
| 683 | continue; |
| 684 | |
| 685 | /* ??? 0 is passed for `pc'. */ |
| 686 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
| 687 | (bfd_vma) 0); |
| 688 | if (insert_errmsg != NULL) |
| 689 | continue; |
| 690 | |
| 691 | /* It is up to the caller to actually output the insn and any |
| 692 | queued relocs. */ |
| 693 | return insn; |
| 694 | } |
| 695 | |
| 696 | { |
| 697 | static char errbuf[150]; |
| 698 | const char *tmp_errmsg; |
| 699 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
| 700 | #define be_verbose 1 |
| 701 | #else |
| 702 | #define be_verbose 0 |
| 703 | #endif |
| 704 | |
| 705 | if (be_verbose) |
| 706 | { |
| 707 | /* If requesting verbose error messages, use insert_errmsg. |
| 708 | Failing that, use parse_errmsg. */ |
| 709 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
| 710 | parse_errmsg ? parse_errmsg : |
| 711 | recognized_mnemonic ? |
| 712 | _("unrecognized form of instruction") : |
| 713 | _("unrecognized instruction")); |
| 714 | |
| 715 | if (strlen (start) > 50) |
| 716 | /* xgettext:c-format */ |
| 717 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); |
| 718 | else |
| 719 | /* xgettext:c-format */ |
| 720 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); |
| 721 | } |
| 722 | else |
| 723 | { |
| 724 | if (strlen (start) > 50) |
| 725 | /* xgettext:c-format */ |
| 726 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); |
| 727 | else |
| 728 | /* xgettext:c-format */ |
| 729 | sprintf (errbuf, _("bad instruction `%.50s'"), start); |
| 730 | } |
| 731 | |
| 732 | *errmsg = errbuf; |
| 733 | return NULL; |
| 734 | } |
| 735 | } |