epiphany-asm.c 25 KB

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