iq2000-asm.c 25 KB

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