or1k-asm.c 25 KB

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