cgen-asm.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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-2022 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 "@prefix@-desc.h"
  26. #include "@prefix@-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. /* Regex construction routine.
  39. This translates an opcode syntax string into a regex string,
  40. by replacing any non-character syntax element (such as an
  41. opcode) with the pattern '.*'
  42. It then compiles the regex and stores it in the opcode, for
  43. later use by @arch@_cgen_assemble_insn
  44. Returns NULL for success, an error message for failure. */
  45. char *
  46. @arch@_cgen_build_insn_regex (CGEN_INSN *insn)
  47. {
  48. CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
  49. const char *mnem = CGEN_INSN_MNEMONIC (insn);
  50. char rxbuf[CGEN_MAX_RX_ELEMENTS];
  51. char *rx = rxbuf;
  52. const CGEN_SYNTAX_CHAR_TYPE *syn;
  53. int reg_err;
  54. syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
  55. /* Mnemonics come first in the syntax string. */
  56. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  57. return _("missing mnemonic in syntax string");
  58. ++syn;
  59. /* Generate a case sensitive regular expression that emulates case
  60. insensitive matching in the "C" locale. We cannot generate a case
  61. insensitive regular expression because in Turkish locales, 'i' and 'I'
  62. are not equal modulo case conversion. */
  63. /* Copy the literal mnemonic out of the insn. */
  64. for (; *mnem; mnem++)
  65. {
  66. char c = *mnem;
  67. if (ISALPHA (c))
  68. {
  69. *rx++ = '[';
  70. *rx++ = TOLOWER (c);
  71. *rx++ = TOUPPER (c);
  72. *rx++ = ']';
  73. }
  74. else
  75. *rx++ = c;
  76. }
  77. /* Copy any remaining literals from the syntax string into the rx. */
  78. for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
  79. {
  80. if (CGEN_SYNTAX_CHAR_P (* syn))
  81. {
  82. char c = CGEN_SYNTAX_CHAR (* syn);
  83. switch (c)
  84. {
  85. /* Escape any regex metacharacters in the syntax. */
  86. case '.': case '[': case '\\':
  87. case '*': case '^': case '$':
  88. #ifdef CGEN_ESCAPE_EXTENDED_REGEX
  89. case '?': case '{': case '}':
  90. case '(': case ')': case '*':
  91. case '|': case '+': case ']':
  92. #endif
  93. *rx++ = '\\';
  94. *rx++ = c;
  95. break;
  96. default:
  97. if (ISALPHA (c))
  98. {
  99. *rx++ = '[';
  100. *rx++ = TOLOWER (c);
  101. *rx++ = TOUPPER (c);
  102. *rx++ = ']';
  103. }
  104. else
  105. *rx++ = c;
  106. break;
  107. }
  108. }
  109. else
  110. {
  111. /* Replace non-syntax fields with globs. */
  112. *rx++ = '.';
  113. *rx++ = '*';
  114. }
  115. }
  116. /* Trailing whitespace ok. */
  117. * rx++ = '[';
  118. * rx++ = ' ';
  119. * rx++ = '\t';
  120. * rx++ = ']';
  121. * rx++ = '*';
  122. /* But anchor it after that. */
  123. * rx++ = '$';
  124. * rx = '\0';
  125. CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
  126. reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
  127. if (reg_err == 0)
  128. return NULL;
  129. else
  130. {
  131. static char msg[80];
  132. regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
  133. regfree ((regex_t *) CGEN_INSN_RX (insn));
  134. free (CGEN_INSN_RX (insn));
  135. (CGEN_INSN_RX (insn)) = NULL;
  136. return msg;
  137. }
  138. }
  139. /* Default insn parser.
  140. The syntax string is scanned and operands are parsed and stored in FIELDS.
  141. Relocs are queued as we go via other callbacks.
  142. ??? Note that this is currently an all-or-nothing parser. If we fail to
  143. parse the instruction, we return 0 and the caller will start over from
  144. the beginning. Backtracking will be necessary in parsing subexpressions,
  145. but that can be handled there. Not handling backtracking here may get
  146. expensive in the case of the m68k. Deal with later.
  147. Returns NULL for success, an error message for failure. */
  148. static const char *
  149. parse_insn_normal (CGEN_CPU_DESC cd,
  150. const CGEN_INSN *insn,
  151. const char **strp,
  152. CGEN_FIELDS *fields)
  153. {
  154. /* ??? Runtime added insns not handled yet. */
  155. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  156. const char *str = *strp;
  157. const char *errmsg;
  158. const char *p;
  159. const CGEN_SYNTAX_CHAR_TYPE * syn;
  160. #ifdef CGEN_MNEMONIC_OPERANDS
  161. /* FIXME: wip */
  162. int past_opcode_p;
  163. #endif
  164. /* For now we assume the mnemonic is first (there are no leading operands).
  165. We can parse it without needing to set up operand parsing.
  166. GAS's input scrubber will ensure mnemonics are lowercase, but we may
  167. not be called from GAS. */
  168. p = CGEN_INSN_MNEMONIC (insn);
  169. while (*p && TOLOWER (*p) == TOLOWER (*str))
  170. ++p, ++str;
  171. if (* p)
  172. return _("unrecognized instruction");
  173. #ifndef CGEN_MNEMONIC_OPERANDS
  174. if (* str && ! ISSPACE (* str))
  175. return _("unrecognized instruction");
  176. #endif
  177. CGEN_INIT_PARSE (cd);
  178. cgen_init_parse_operand (cd);
  179. #ifdef CGEN_MNEMONIC_OPERANDS
  180. past_opcode_p = 0;
  181. #endif
  182. /* We don't check for (*str != '\0') here because we want to parse
  183. any trailing fake arguments in the syntax string. */
  184. syn = CGEN_SYNTAX_STRING (syntax);
  185. /* Mnemonics come first for now, ensure valid string. */
  186. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  187. abort ();
  188. ++syn;
  189. while (* syn != 0)
  190. {
  191. /* Non operand chars must match exactly. */
  192. if (CGEN_SYNTAX_CHAR_P (* syn))
  193. {
  194. /* FIXME: While we allow for non-GAS callers above, we assume the
  195. first char after the mnemonic part is a space. */
  196. /* FIXME: We also take inappropriate advantage of the fact that
  197. GAS's input scrubber will remove extraneous blanks. */
  198. if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
  199. {
  200. #ifdef CGEN_MNEMONIC_OPERANDS
  201. if (CGEN_SYNTAX_CHAR(* syn) == ' ')
  202. past_opcode_p = 1;
  203. #endif
  204. ++ syn;
  205. ++ str;
  206. }
  207. else if (*str)
  208. {
  209. /* Syntax char didn't match. Can't be this insn. */
  210. static char msg [80];
  211. /* xgettext:c-format */
  212. sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
  213. CGEN_SYNTAX_CHAR(*syn), *str);
  214. return msg;
  215. }
  216. else
  217. {
  218. /* Ran out of input. */
  219. static char msg [80];
  220. /* xgettext:c-format */
  221. sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
  222. CGEN_SYNTAX_CHAR(*syn));
  223. return msg;
  224. }
  225. continue;
  226. }
  227. #ifdef CGEN_MNEMONIC_OPERANDS
  228. (void) past_opcode_p;
  229. #endif
  230. /* We have an operand of some sort. */
  231. errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
  232. if (errmsg)
  233. return errmsg;
  234. /* Done with this operand, continue with next one. */
  235. ++ syn;
  236. }
  237. /* If we're at the end of the syntax string, we're done. */
  238. if (* syn == 0)
  239. {
  240. /* FIXME: For the moment we assume a valid `str' can only contain
  241. blanks now. IE: We needn't try again with a longer version of
  242. the insn and it is assumed that longer versions of insns appear
  243. before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
  244. while (ISSPACE (* str))
  245. ++ str;
  246. if (* str != '\0')
  247. return _("junk at end of line"); /* FIXME: would like to include `str' */
  248. return NULL;
  249. }
  250. /* We couldn't parse it. */
  251. return _("unrecognized instruction");
  252. }
  253. /* Main entry point.
  254. This routine is called for each instruction to be assembled.
  255. STR points to the insn to be assembled.
  256. We assume all necessary tables have been initialized.
  257. The assembled instruction, less any fixups, is stored in BUF.
  258. Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
  259. still needs to be converted to target byte order, otherwise BUF is an array
  260. of bytes in target byte order.
  261. The result is a pointer to the insn's entry in the opcode table,
  262. or NULL if an error occured (an error message will have already been
  263. printed).
  264. Note that when processing (non-alias) macro-insns,
  265. this function recurses.
  266. ??? It's possible to make this cpu-independent.
  267. One would have to deal with a few minor things.
  268. At this point in time doing so would be more of a curiosity than useful
  269. [for example this file isn't _that_ big], but keeping the possibility in
  270. mind helps keep the design clean. */
  271. const CGEN_INSN *
  272. @arch@_cgen_assemble_insn (CGEN_CPU_DESC cd,
  273. const char *str,
  274. CGEN_FIELDS *fields,
  275. CGEN_INSN_BYTES_PTR buf,
  276. char **errmsg)
  277. {
  278. const char *start;
  279. CGEN_INSN_LIST *ilist;
  280. const char *parse_errmsg = NULL;
  281. const char *insert_errmsg = NULL;
  282. int recognized_mnemonic = 0;
  283. /* Skip leading white space. */
  284. while (ISSPACE (* str))
  285. ++ str;
  286. /* The instructions are stored in hashed lists.
  287. Get the first in the list. */
  288. ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
  289. /* Keep looking until we find a match. */
  290. start = str;
  291. for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
  292. {
  293. const CGEN_INSN *insn = ilist->insn;
  294. recognized_mnemonic = 1;
  295. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  296. /* Not usually needed as unsupported opcodes
  297. shouldn't be in the hash lists. */
  298. /* Is this insn supported by the selected cpu? */
  299. if (! @arch@_cgen_insn_supported (cd, insn))
  300. continue;
  301. #endif
  302. /* If the RELAXED attribute is set, this is an insn that shouldn't be
  303. chosen immediately. Instead, it is used during assembler/linker
  304. relaxation if possible. */
  305. if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
  306. continue;
  307. str = start;
  308. /* Skip this insn if str doesn't look right lexically. */
  309. if (CGEN_INSN_RX (insn) != NULL &&
  310. regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
  311. continue;
  312. /* Allow parse/insert handlers to obtain length of insn. */
  313. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  314. parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
  315. if (parse_errmsg != NULL)
  316. continue;
  317. /* ??? 0 is passed for `pc'. */
  318. insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
  319. (bfd_vma) 0);
  320. if (insert_errmsg != NULL)
  321. continue;
  322. /* It is up to the caller to actually output the insn and any
  323. queued relocs. */
  324. return insn;
  325. }
  326. {
  327. static char errbuf[150];
  328. const char *tmp_errmsg;
  329. #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
  330. #define be_verbose 1
  331. #else
  332. #define be_verbose 0
  333. #endif
  334. if (be_verbose)
  335. {
  336. /* If requesting verbose error messages, use insert_errmsg.
  337. Failing that, use parse_errmsg. */
  338. tmp_errmsg = (insert_errmsg ? insert_errmsg :
  339. parse_errmsg ? parse_errmsg :
  340. recognized_mnemonic ?
  341. _("unrecognized form of instruction") :
  342. _("unrecognized instruction"));
  343. if (strlen (start) > 50)
  344. /* xgettext:c-format */
  345. sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
  346. else
  347. /* xgettext:c-format */
  348. sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
  349. }
  350. else
  351. {
  352. if (strlen (start) > 50)
  353. /* xgettext:c-format */
  354. sprintf (errbuf, _("bad instruction `%.50s...'"), start);
  355. else
  356. /* xgettext:c-format */
  357. sprintf (errbuf, _("bad instruction `%.50s'"), start);
  358. }
  359. *errmsg = errbuf;
  360. return NULL;
  361. }
  362. }