m32r-asm.c 21 KB

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