m32r-asm.c 21 KB

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