fr30-asm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 "fr30-desc.h"
  27. #include "fr30-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. /* Handle register lists for LDMx and STMx. */
  41. static int
  42. parse_register_number (const char **strp)
  43. {
  44. int regno;
  45. if (**strp < '0' || **strp > '9')
  46. return -1; /* Error. */
  47. regno = **strp - '0';
  48. ++*strp;
  49. if (**strp >= '0' && **strp <= '9')
  50. {
  51. regno = regno * 10 + (**strp - '0');
  52. ++*strp;
  53. }
  54. return regno;
  55. }
  56. static const char *
  57. parse_register_list (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  58. const char **strp,
  59. int opindex ATTRIBUTE_UNUSED,
  60. unsigned long *valuep,
  61. int high_low, /* 0 == high, 1 == low. */
  62. int load_store) /* 0 == load, 1 == store. */
  63. {
  64. *valuep = 0;
  65. while (**strp && **strp != ')')
  66. {
  67. int regno;
  68. if (**strp != 'R' && **strp != 'r')
  69. break;
  70. ++*strp;
  71. regno = parse_register_number (strp);
  72. if (regno == -1)
  73. return _("Register number is not valid");
  74. if (regno > 7 && !high_low)
  75. return _("Register must be between r0 and r7");
  76. if (regno < 8 && high_low)
  77. return _("Register must be between r8 and r15");
  78. if (high_low)
  79. regno -= 8;
  80. if (load_store) /* Mask is reversed for store. */
  81. *valuep |= 0x80 >> regno;
  82. else
  83. *valuep |= 1 << regno;
  84. if (**strp == ',')
  85. {
  86. if (*(*strp + 1) == ')')
  87. break;
  88. ++*strp;
  89. }
  90. }
  91. if (!*strp || **strp != ')')
  92. return _("Register list is not valid");
  93. return NULL;
  94. }
  95. static const char *
  96. parse_low_register_list_ld (CGEN_CPU_DESC cd,
  97. const char **strp,
  98. int opindex,
  99. unsigned long *valuep)
  100. {
  101. return parse_register_list (cd, strp, opindex, valuep,
  102. 0 /* Low. */, 0 /* Load. */);
  103. }
  104. static const char *
  105. parse_hi_register_list_ld (CGEN_CPU_DESC cd,
  106. const char **strp,
  107. int opindex,
  108. unsigned long *valuep)
  109. {
  110. return parse_register_list (cd, strp, opindex, valuep,
  111. 1 /* High. */, 0 /* Load. */);
  112. }
  113. static const char *
  114. parse_low_register_list_st (CGEN_CPU_DESC cd,
  115. const char **strp,
  116. int opindex,
  117. unsigned long *valuep)
  118. {
  119. return parse_register_list (cd, strp, opindex, valuep,
  120. 0 /* Low. */, 1 /* Store. */);
  121. }
  122. static const char *
  123. parse_hi_register_list_st (CGEN_CPU_DESC cd,
  124. const char **strp,
  125. int opindex,
  126. unsigned long *valuep)
  127. {
  128. return parse_register_list (cd, strp, opindex, valuep,
  129. 1 /* High. */, 1 /* Store. */);
  130. }
  131. /* -- */
  132. const char * fr30_cgen_parse_operand
  133. (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
  134. /* Main entry point for operand parsing.
  135. This function is basically just a big switch statement. Earlier versions
  136. used tables to look up the function to use, but
  137. - if the table contains both assembler and disassembler functions then
  138. the disassembler contains much of the assembler and vice-versa,
  139. - there's a lot of inlining possibilities as things grow,
  140. - using a switch statement avoids the function call overhead.
  141. This function could be moved into `parse_insn_normal', but keeping it
  142. separate makes clear the interface between `parse_insn_normal' and each of
  143. the handlers. */
  144. const char *
  145. fr30_cgen_parse_operand (CGEN_CPU_DESC cd,
  146. int opindex,
  147. const char ** strp,
  148. CGEN_FIELDS * fields)
  149. {
  150. const char * errmsg = NULL;
  151. /* Used by scalar operands that still need to be parsed. */
  152. long junk ATTRIBUTE_UNUSED;
  153. switch (opindex)
  154. {
  155. case FR30_OPERAND_CRI :
  156. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi);
  157. break;
  158. case FR30_OPERAND_CRJ :
  159. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj);
  160. break;
  161. case FR30_OPERAND_R13 :
  162. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk);
  163. break;
  164. case FR30_OPERAND_R14 :
  165. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk);
  166. break;
  167. case FR30_OPERAND_R15 :
  168. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk);
  169. break;
  170. case FR30_OPERAND_RI :
  171. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri);
  172. break;
  173. case FR30_OPERAND_RIC :
  174. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric);
  175. break;
  176. case FR30_OPERAND_RJ :
  177. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj);
  178. break;
  179. case FR30_OPERAND_RJC :
  180. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc);
  181. break;
  182. case FR30_OPERAND_RS1 :
  183. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1);
  184. break;
  185. case FR30_OPERAND_RS2 :
  186. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2);
  187. break;
  188. case FR30_OPERAND_CC :
  189. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, (unsigned long *) (& fields->f_cc));
  190. break;
  191. case FR30_OPERAND_CCC :
  192. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, (unsigned long *) (& fields->f_ccc));
  193. break;
  194. case FR30_OPERAND_DIR10 :
  195. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, (unsigned long *) (& fields->f_dir10));
  196. break;
  197. case FR30_OPERAND_DIR8 :
  198. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, (unsigned long *) (& fields->f_dir8));
  199. break;
  200. case FR30_OPERAND_DIR9 :
  201. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, (unsigned long *) (& fields->f_dir9));
  202. break;
  203. case FR30_OPERAND_DISP10 :
  204. errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, (long *) (& fields->f_disp10));
  205. break;
  206. case FR30_OPERAND_DISP8 :
  207. errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, (long *) (& fields->f_disp8));
  208. break;
  209. case FR30_OPERAND_DISP9 :
  210. errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, (long *) (& fields->f_disp9));
  211. break;
  212. case FR30_OPERAND_I20 :
  213. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, (unsigned long *) (& fields->f_i20));
  214. break;
  215. case FR30_OPERAND_I32 :
  216. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, (unsigned long *) (& fields->f_i32));
  217. break;
  218. case FR30_OPERAND_I8 :
  219. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, (unsigned long *) (& fields->f_i8));
  220. break;
  221. case FR30_OPERAND_LABEL12 :
  222. {
  223. bfd_vma value = 0;
  224. errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL, & value);
  225. fields->f_rel12 = value;
  226. }
  227. break;
  228. case FR30_OPERAND_LABEL9 :
  229. {
  230. bfd_vma value = 0;
  231. errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL, & value);
  232. fields->f_rel9 = value;
  233. }
  234. break;
  235. case FR30_OPERAND_M4 :
  236. errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, (long *) (& fields->f_m4));
  237. break;
  238. case FR30_OPERAND_PS :
  239. errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk);
  240. break;
  241. case FR30_OPERAND_REGLIST_HI_LD :
  242. errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, (unsigned long *) (& fields->f_reglist_hi_ld));
  243. break;
  244. case FR30_OPERAND_REGLIST_HI_ST :
  245. errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, (unsigned long *) (& fields->f_reglist_hi_st));
  246. break;
  247. case FR30_OPERAND_REGLIST_LOW_LD :
  248. errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, (unsigned long *) (& fields->f_reglist_low_ld));
  249. break;
  250. case FR30_OPERAND_REGLIST_LOW_ST :
  251. errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, (unsigned long *) (& fields->f_reglist_low_st));
  252. break;
  253. case FR30_OPERAND_S10 :
  254. errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, (long *) (& fields->f_s10));
  255. break;
  256. case FR30_OPERAND_U10 :
  257. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, (unsigned long *) (& fields->f_u10));
  258. break;
  259. case FR30_OPERAND_U4 :
  260. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, (unsigned long *) (& fields->f_u4));
  261. break;
  262. case FR30_OPERAND_U4C :
  263. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, (unsigned long *) (& fields->f_u4c));
  264. break;
  265. case FR30_OPERAND_U8 :
  266. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, (unsigned long *) (& fields->f_u8));
  267. break;
  268. case FR30_OPERAND_UDISP6 :
  269. errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, (unsigned long *) (& fields->f_udisp6));
  270. break;
  271. default :
  272. /* xgettext:c-format */
  273. opcodes_error_handler
  274. (_("internal error: unrecognized field %d while parsing"),
  275. opindex);
  276. abort ();
  277. }
  278. return errmsg;
  279. }
  280. cgen_parse_fn * const fr30_cgen_parse_handlers[] =
  281. {
  282. parse_insn_normal,
  283. };
  284. void
  285. fr30_cgen_init_asm (CGEN_CPU_DESC cd)
  286. {
  287. fr30_cgen_init_opcode_table (cd);
  288. fr30_cgen_init_ibld_table (cd);
  289. cd->parse_handlers = & fr30_cgen_parse_handlers[0];
  290. cd->parse_operand = fr30_cgen_parse_operand;
  291. #ifdef CGEN_ASM_INIT_HOOK
  292. CGEN_ASM_INIT_HOOK
  293. #endif
  294. }
  295. /* Regex construction routine.
  296. This translates an opcode syntax string into a regex string,
  297. by replacing any non-character syntax element (such as an
  298. opcode) with the pattern '.*'
  299. It then compiles the regex and stores it in the opcode, for
  300. later use by fr30_cgen_assemble_insn
  301. Returns NULL for success, an error message for failure. */
  302. char *
  303. fr30_cgen_build_insn_regex (CGEN_INSN *insn)
  304. {
  305. CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
  306. const char *mnem = CGEN_INSN_MNEMONIC (insn);
  307. char rxbuf[CGEN_MAX_RX_ELEMENTS];
  308. char *rx = rxbuf;
  309. const CGEN_SYNTAX_CHAR_TYPE *syn;
  310. int reg_err;
  311. syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
  312. /* Mnemonics come first in the syntax string. */
  313. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  314. return _("missing mnemonic in syntax string");
  315. ++syn;
  316. /* Generate a case sensitive regular expression that emulates case
  317. insensitive matching in the "C" locale. We cannot generate a case
  318. insensitive regular expression because in Turkish locales, 'i' and 'I'
  319. are not equal modulo case conversion. */
  320. /* Copy the literal mnemonic out of the insn. */
  321. for (; *mnem; mnem++)
  322. {
  323. char c = *mnem;
  324. if (ISALPHA (c))
  325. {
  326. *rx++ = '[';
  327. *rx++ = TOLOWER (c);
  328. *rx++ = TOUPPER (c);
  329. *rx++ = ']';
  330. }
  331. else
  332. *rx++ = c;
  333. }
  334. /* Copy any remaining literals from the syntax string into the rx. */
  335. for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
  336. {
  337. if (CGEN_SYNTAX_CHAR_P (* syn))
  338. {
  339. char c = CGEN_SYNTAX_CHAR (* syn);
  340. switch (c)
  341. {
  342. /* Escape any regex metacharacters in the syntax. */
  343. case '.': case '[': case '\\':
  344. case '*': case '^': case '$':
  345. #ifdef CGEN_ESCAPE_EXTENDED_REGEX
  346. case '?': case '{': case '}':
  347. case '(': case ')': case '*':
  348. case '|': case '+': case ']':
  349. #endif
  350. *rx++ = '\\';
  351. *rx++ = c;
  352. break;
  353. default:
  354. if (ISALPHA (c))
  355. {
  356. *rx++ = '[';
  357. *rx++ = TOLOWER (c);
  358. *rx++ = TOUPPER (c);
  359. *rx++ = ']';
  360. }
  361. else
  362. *rx++ = c;
  363. break;
  364. }
  365. }
  366. else
  367. {
  368. /* Replace non-syntax fields with globs. */
  369. *rx++ = '.';
  370. *rx++ = '*';
  371. }
  372. }
  373. /* Trailing whitespace ok. */
  374. * rx++ = '[';
  375. * rx++ = ' ';
  376. * rx++ = '\t';
  377. * rx++ = ']';
  378. * rx++ = '*';
  379. /* But anchor it after that. */
  380. * rx++ = '$';
  381. * rx = '\0';
  382. CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
  383. reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
  384. if (reg_err == 0)
  385. return NULL;
  386. else
  387. {
  388. static char msg[80];
  389. regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
  390. regfree ((regex_t *) CGEN_INSN_RX (insn));
  391. free (CGEN_INSN_RX (insn));
  392. (CGEN_INSN_RX (insn)) = NULL;
  393. return msg;
  394. }
  395. }
  396. /* Default insn parser.
  397. The syntax string is scanned and operands are parsed and stored in FIELDS.
  398. Relocs are queued as we go via other callbacks.
  399. ??? Note that this is currently an all-or-nothing parser. If we fail to
  400. parse the instruction, we return 0 and the caller will start over from
  401. the beginning. Backtracking will be necessary in parsing subexpressions,
  402. but that can be handled there. Not handling backtracking here may get
  403. expensive in the case of the m68k. Deal with later.
  404. Returns NULL for success, an error message for failure. */
  405. static const char *
  406. parse_insn_normal (CGEN_CPU_DESC cd,
  407. const CGEN_INSN *insn,
  408. const char **strp,
  409. CGEN_FIELDS *fields)
  410. {
  411. /* ??? Runtime added insns not handled yet. */
  412. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  413. const char *str = *strp;
  414. const char *errmsg;
  415. const char *p;
  416. const CGEN_SYNTAX_CHAR_TYPE * syn;
  417. #ifdef CGEN_MNEMONIC_OPERANDS
  418. /* FIXME: wip */
  419. int past_opcode_p;
  420. #endif
  421. /* For now we assume the mnemonic is first (there are no leading operands).
  422. We can parse it without needing to set up operand parsing.
  423. GAS's input scrubber will ensure mnemonics are lowercase, but we may
  424. not be called from GAS. */
  425. p = CGEN_INSN_MNEMONIC (insn);
  426. while (*p && TOLOWER (*p) == TOLOWER (*str))
  427. ++p, ++str;
  428. if (* p)
  429. return _("unrecognized instruction");
  430. #ifndef CGEN_MNEMONIC_OPERANDS
  431. if (* str && ! ISSPACE (* str))
  432. return _("unrecognized instruction");
  433. #endif
  434. CGEN_INIT_PARSE (cd);
  435. cgen_init_parse_operand (cd);
  436. #ifdef CGEN_MNEMONIC_OPERANDS
  437. past_opcode_p = 0;
  438. #endif
  439. /* We don't check for (*str != '\0') here because we want to parse
  440. any trailing fake arguments in the syntax string. */
  441. syn = CGEN_SYNTAX_STRING (syntax);
  442. /* Mnemonics come first for now, ensure valid string. */
  443. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  444. abort ();
  445. ++syn;
  446. while (* syn != 0)
  447. {
  448. /* Non operand chars must match exactly. */
  449. if (CGEN_SYNTAX_CHAR_P (* syn))
  450. {
  451. /* FIXME: While we allow for non-GAS callers above, we assume the
  452. first char after the mnemonic part is a space. */
  453. /* FIXME: We also take inappropriate advantage of the fact that
  454. GAS's input scrubber will remove extraneous blanks. */
  455. if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
  456. {
  457. #ifdef CGEN_MNEMONIC_OPERANDS
  458. if (CGEN_SYNTAX_CHAR(* syn) == ' ')
  459. past_opcode_p = 1;
  460. #endif
  461. ++ syn;
  462. ++ str;
  463. }
  464. else if (*str)
  465. {
  466. /* Syntax char didn't match. Can't be this insn. */
  467. static char msg [80];
  468. /* xgettext:c-format */
  469. sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
  470. CGEN_SYNTAX_CHAR(*syn), *str);
  471. return msg;
  472. }
  473. else
  474. {
  475. /* Ran out of input. */
  476. static char msg [80];
  477. /* xgettext:c-format */
  478. sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
  479. CGEN_SYNTAX_CHAR(*syn));
  480. return msg;
  481. }
  482. continue;
  483. }
  484. #ifdef CGEN_MNEMONIC_OPERANDS
  485. (void) past_opcode_p;
  486. #endif
  487. /* We have an operand of some sort. */
  488. errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
  489. if (errmsg)
  490. return errmsg;
  491. /* Done with this operand, continue with next one. */
  492. ++ syn;
  493. }
  494. /* If we're at the end of the syntax string, we're done. */
  495. if (* syn == 0)
  496. {
  497. /* FIXME: For the moment we assume a valid `str' can only contain
  498. blanks now. IE: We needn't try again with a longer version of
  499. the insn and it is assumed that longer versions of insns appear
  500. before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
  501. while (ISSPACE (* str))
  502. ++ str;
  503. if (* str != '\0')
  504. return _("junk at end of line"); /* FIXME: would like to include `str' */
  505. return NULL;
  506. }
  507. /* We couldn't parse it. */
  508. return _("unrecognized instruction");
  509. }
  510. /* Main entry point.
  511. This routine is called for each instruction to be assembled.
  512. STR points to the insn to be assembled.
  513. We assume all necessary tables have been initialized.
  514. The assembled instruction, less any fixups, is stored in BUF.
  515. Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
  516. still needs to be converted to target byte order, otherwise BUF is an array
  517. of bytes in target byte order.
  518. The result is a pointer to the insn's entry in the opcode table,
  519. or NULL if an error occured (an error message will have already been
  520. printed).
  521. Note that when processing (non-alias) macro-insns,
  522. this function recurses.
  523. ??? It's possible to make this cpu-independent.
  524. One would have to deal with a few minor things.
  525. At this point in time doing so would be more of a curiosity than useful
  526. [for example this file isn't _that_ big], but keeping the possibility in
  527. mind helps keep the design clean. */
  528. const CGEN_INSN *
  529. fr30_cgen_assemble_insn (CGEN_CPU_DESC cd,
  530. const char *str,
  531. CGEN_FIELDS *fields,
  532. CGEN_INSN_BYTES_PTR buf,
  533. char **errmsg)
  534. {
  535. const char *start;
  536. CGEN_INSN_LIST *ilist;
  537. const char *parse_errmsg = NULL;
  538. const char *insert_errmsg = NULL;
  539. int recognized_mnemonic = 0;
  540. /* Skip leading white space. */
  541. while (ISSPACE (* str))
  542. ++ str;
  543. /* The instructions are stored in hashed lists.
  544. Get the first in the list. */
  545. ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
  546. /* Keep looking until we find a match. */
  547. start = str;
  548. for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
  549. {
  550. const CGEN_INSN *insn = ilist->insn;
  551. recognized_mnemonic = 1;
  552. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  553. /* Not usually needed as unsupported opcodes
  554. shouldn't be in the hash lists. */
  555. /* Is this insn supported by the selected cpu? */
  556. if (! fr30_cgen_insn_supported (cd, insn))
  557. continue;
  558. #endif
  559. /* If the RELAXED attribute is set, this is an insn that shouldn't be
  560. chosen immediately. Instead, it is used during assembler/linker
  561. relaxation if possible. */
  562. if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
  563. continue;
  564. str = start;
  565. /* Skip this insn if str doesn't look right lexically. */
  566. if (CGEN_INSN_RX (insn) != NULL &&
  567. regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
  568. continue;
  569. /* Allow parse/insert handlers to obtain length of insn. */
  570. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  571. parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
  572. if (parse_errmsg != NULL)
  573. continue;
  574. /* ??? 0 is passed for `pc'. */
  575. insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
  576. (bfd_vma) 0);
  577. if (insert_errmsg != NULL)
  578. continue;
  579. /* It is up to the caller to actually output the insn and any
  580. queued relocs. */
  581. return insn;
  582. }
  583. {
  584. static char errbuf[150];
  585. const char *tmp_errmsg;
  586. #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
  587. #define be_verbose 1
  588. #else
  589. #define be_verbose 0
  590. #endif
  591. if (be_verbose)
  592. {
  593. /* If requesting verbose error messages, use insert_errmsg.
  594. Failing that, use parse_errmsg. */
  595. tmp_errmsg = (insert_errmsg ? insert_errmsg :
  596. parse_errmsg ? parse_errmsg :
  597. recognized_mnemonic ?
  598. _("unrecognized form of instruction") :
  599. _("unrecognized instruction"));
  600. if (strlen (start) > 50)
  601. /* xgettext:c-format */
  602. sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
  603. else
  604. /* xgettext:c-format */
  605. sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
  606. }
  607. else
  608. {
  609. if (strlen (start) > 50)
  610. /* xgettext:c-format */
  611. sprintf (errbuf, _("bad instruction `%.50s...'"), start);
  612. else
  613. /* xgettext:c-format */
  614. sprintf (errbuf, _("bad instruction `%.50s'"), start);
  615. }
  616. *errmsg = errbuf;
  617. return NULL;
  618. }
  619. }