genextract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* Generate code from machine description to extract operands from insn as rtl.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "bconfig.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "errors.h"
  21. #include "read-md.h"
  22. #include "gensupport.h"
  23. #include "vec.h"
  24. /* This structure contains all the information needed to describe one
  25. set of extractions methods. Each method may be used by more than
  26. one pattern if the operands are in the same place.
  27. The string for each operand describes that path to the operand and
  28. contains `0' through `9' when going into an expression and `a' through
  29. `z' when going into a vector. We assume here that only the first operand
  30. of an rtl expression is a vector. genrecog.c makes the same assumption
  31. (and uses the same representation) and it is currently true. */
  32. typedef char *locstr;
  33. struct extraction
  34. {
  35. unsigned int op_count;
  36. unsigned int dup_count;
  37. locstr *oplocs;
  38. locstr *duplocs;
  39. int *dupnums;
  40. struct code_ptr *insns;
  41. struct extraction *next;
  42. };
  43. /* Holds a single insn code that uses an extraction method. */
  44. struct code_ptr
  45. {
  46. int insn_code;
  47. struct code_ptr *next;
  48. };
  49. /* All extractions needed for this machine description. */
  50. static struct extraction *extractions;
  51. /* All insn codes for old-style peepholes. */
  52. static struct code_ptr *peepholes;
  53. /* This structure is used by gen_insn and walk_rtx to accumulate the
  54. data that will be used to produce an extractions structure. */
  55. struct accum_extract
  56. {
  57. vec<locstr> oplocs;
  58. vec<locstr> duplocs;
  59. vec<int> dupnums;
  60. vec<char> pathstr;
  61. };
  62. int line_no;
  63. /* Forward declarations. */
  64. static void walk_rtx (rtx, struct accum_extract *);
  65. static void
  66. gen_insn (rtx insn, int insn_code_number)
  67. {
  68. int i;
  69. unsigned int op_count, dup_count, j;
  70. struct extraction *p;
  71. struct code_ptr *link;
  72. struct accum_extract acc;
  73. acc.oplocs.create (10);
  74. acc.duplocs.create (10);
  75. acc.dupnums.create (10);
  76. acc.pathstr.create (20);
  77. /* Walk the insn's pattern, remembering at all times the path
  78. down to the walking point. */
  79. if (XVECLEN (insn, 1) == 1)
  80. walk_rtx (XVECEXP (insn, 1, 0), &acc);
  81. else
  82. for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
  83. {
  84. acc.pathstr.safe_push ('a' + i);
  85. walk_rtx (XVECEXP (insn, 1, i), &acc);
  86. acc.pathstr.pop ();
  87. }
  88. link = XNEW (struct code_ptr);
  89. link->insn_code = insn_code_number;
  90. /* See if we find something that already had this extraction method. */
  91. op_count = acc.oplocs.length ();
  92. dup_count = acc.duplocs.length ();
  93. gcc_assert (dup_count == acc.dupnums.length ());
  94. for (p = extractions; p; p = p->next)
  95. {
  96. if (p->op_count != op_count || p->dup_count != dup_count)
  97. continue;
  98. for (j = 0; j < op_count; j++)
  99. {
  100. char *a = p->oplocs[j];
  101. char *b = acc.oplocs[j];
  102. if (a != b && (!a || !b || strcmp (a, b)))
  103. break;
  104. }
  105. if (j != op_count)
  106. continue;
  107. for (j = 0; j < dup_count; j++)
  108. if (p->dupnums[j] != acc.dupnums[j]
  109. || strcmp (p->duplocs[j], acc.duplocs[j]))
  110. break;
  111. if (j != dup_count)
  112. continue;
  113. /* This extraction is the same as ours. Just link us in. */
  114. link->next = p->insns;
  115. p->insns = link;
  116. goto done;
  117. }
  118. /* Otherwise, make a new extraction method. We stash the arrays
  119. after the extraction structure in memory. */
  120. p = XNEWVAR (struct extraction, sizeof (struct extraction)
  121. + op_count*sizeof (char *)
  122. + dup_count*sizeof (char *)
  123. + dup_count*sizeof (int));
  124. p->op_count = op_count;
  125. p->dup_count = dup_count;
  126. p->next = extractions;
  127. extractions = p;
  128. p->insns = link;
  129. link->next = 0;
  130. p->oplocs = (char **)((char *)p + sizeof (struct extraction));
  131. p->duplocs = p->oplocs + op_count;
  132. p->dupnums = (int *)(p->duplocs + dup_count);
  133. memcpy (p->oplocs, acc.oplocs.address (), op_count * sizeof (locstr));
  134. memcpy (p->duplocs, acc.duplocs.address (), dup_count * sizeof (locstr));
  135. memcpy (p->dupnums, acc.dupnums.address (), dup_count * sizeof (int));
  136. done:
  137. acc.oplocs.release ();
  138. acc.duplocs.release ();
  139. acc.dupnums.release ();
  140. acc.pathstr.release ();
  141. }
  142. /* Helper subroutine of walk_rtx: given a vec<locstr>, an index, and a
  143. string, insert the string at the index, which should either already
  144. exist and be NULL, or not yet exist within the vector. In the latter
  145. case the vector is enlarged as appropriate. */
  146. static void
  147. VEC_safe_set_locstr (vec<locstr> *vp, unsigned int ix, char *str)
  148. {
  149. if (ix < (*vp).length ())
  150. {
  151. if ((*vp)[ix])
  152. {
  153. message_with_line (line_no, "repeated operand number %d", ix);
  154. have_error = 1;
  155. }
  156. else
  157. (*vp)[ix] = str;
  158. }
  159. else
  160. {
  161. while (ix > (*vp).length ())
  162. vp->safe_push (NULL);
  163. vp->safe_push (str);
  164. }
  165. }
  166. /* Another helper subroutine of walk_rtx: given a vec<char>, convert it
  167. to a NUL-terminated string in malloc memory. */
  168. static char *
  169. VEC_char_to_string (vec<char> v)
  170. {
  171. size_t n = v.length ();
  172. char *s = XNEWVEC (char, n + 1);
  173. memcpy (s, v.address (), n);
  174. s[n] = '\0';
  175. return s;
  176. }
  177. static void
  178. walk_rtx (rtx x, struct accum_extract *acc)
  179. {
  180. RTX_CODE code;
  181. int i, len, base;
  182. const char *fmt;
  183. if (x == 0)
  184. return;
  185. code = GET_CODE (x);
  186. switch (code)
  187. {
  188. case PC:
  189. case CC0:
  190. case CONST_INT:
  191. case SYMBOL_REF:
  192. return;
  193. case MATCH_OPERAND:
  194. case MATCH_SCRATCH:
  195. VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
  196. VEC_char_to_string (acc->pathstr));
  197. break;
  198. case MATCH_OPERATOR:
  199. case MATCH_PARALLEL:
  200. VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
  201. VEC_char_to_string (acc->pathstr));
  202. base = (code == MATCH_OPERATOR ? '0' : 'a');
  203. for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
  204. {
  205. acc->pathstr.safe_push (base + i);
  206. walk_rtx (XVECEXP (x, 2, i), acc);
  207. acc->pathstr.pop ();
  208. }
  209. return;
  210. case MATCH_DUP:
  211. case MATCH_PAR_DUP:
  212. case MATCH_OP_DUP:
  213. acc->duplocs.safe_push (VEC_char_to_string (acc->pathstr));
  214. acc->dupnums.safe_push (XINT (x, 0));
  215. if (code == MATCH_DUP)
  216. break;
  217. base = (code == MATCH_OP_DUP ? '0' : 'a');
  218. for (i = XVECLEN (x, 1) - 1; i >= 0; i--)
  219. {
  220. acc->pathstr.safe_push (base + i);
  221. walk_rtx (XVECEXP (x, 1, i), acc);
  222. acc->pathstr.pop ();
  223. }
  224. return;
  225. default:
  226. break;
  227. }
  228. fmt = GET_RTX_FORMAT (code);
  229. len = GET_RTX_LENGTH (code);
  230. for (i = 0; i < len; i++)
  231. {
  232. if (fmt[i] == 'e' || fmt[i] == 'u')
  233. {
  234. acc->pathstr.safe_push ('0' + i);
  235. walk_rtx (XEXP (x, i), acc);
  236. acc->pathstr.pop ();
  237. }
  238. else if (fmt[i] == 'E')
  239. {
  240. int j;
  241. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  242. {
  243. acc->pathstr.safe_push ('a' + j);
  244. walk_rtx (XVECEXP (x, i, j), acc);
  245. acc->pathstr.pop ();
  246. }
  247. }
  248. }
  249. }
  250. /* Given a PATH, representing a path down the instruction's
  251. pattern from the root to a certain point, output code to
  252. evaluate to the rtx at that point. */
  253. static void
  254. print_path (const char *path)
  255. {
  256. int len = strlen (path);
  257. int i;
  258. if (len == 0)
  259. {
  260. /* Don't emit "pat", since we may try to take the address of it,
  261. which isn't what is intended. */
  262. fputs ("PATTERN (insn)", stdout);
  263. return;
  264. }
  265. /* We first write out the operations (XEXP or XVECEXP) in reverse
  266. order, then write "pat", then the indices in forward order. */
  267. for (i = len - 1; i >= 0 ; i--)
  268. {
  269. if (ISLOWER (path[i]))
  270. fputs ("XVECEXP (", stdout);
  271. else if (ISDIGIT (path[i]))
  272. fputs ("XEXP (", stdout);
  273. else
  274. gcc_unreachable ();
  275. }
  276. fputs ("pat", stdout);
  277. for (i = 0; i < len; i++)
  278. {
  279. if (ISLOWER (path[i]))
  280. printf (", 0, %d)", path[i] - 'a');
  281. else if (ISDIGIT (path[i]))
  282. printf (", %d)", path[i] - '0');
  283. else
  284. gcc_unreachable ();
  285. }
  286. }
  287. static void
  288. print_header (void)
  289. {
  290. /* N.B. Code below avoids putting squiggle braces in column 1 inside
  291. a string, because this confuses some editors' syntax highlighting
  292. engines. */
  293. puts ("\
  294. /* Generated automatically by the program `genextract'\n\
  295. from the machine description file `md'. */\n\
  296. \n\
  297. #include \"config.h\"\n\
  298. #include \"system.h\"\n\
  299. #include \"coretypes.h\"\n\
  300. #include \"tm.h\"\n\
  301. #include \"rtl.h\"\n\
  302. #include \"insn-config.h\"\n\
  303. #include \"recog.h\"\n\
  304. #include \"diagnostic-core.h\"\n\
  305. \n\
  306. /* This variable is used as the \"location\" of any missing operand\n\
  307. whose numbers are skipped by a given pattern. */\n\
  308. static rtx junk ATTRIBUTE_UNUSED;\n");
  309. puts ("\
  310. void\n\
  311. insn_extract (rtx_insn *insn)\n{\n\
  312. rtx *ro = recog_data.operand;\n\
  313. rtx **ro_loc = recog_data.operand_loc;\n\
  314. rtx pat = PATTERN (insn);\n\
  315. int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
  316. \n\
  317. #ifdef ENABLE_CHECKING\n\
  318. memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
  319. memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
  320. #endif\n");
  321. puts ("\
  322. switch (INSN_CODE (insn))\n\
  323. {\n\
  324. default:\n\
  325. /* Control reaches here if insn_extract has been called with an\n\
  326. unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
  327. corresponds to a DEFINE_EXPAND in the machine description;\n\
  328. either way, a bug. */\n\
  329. if (INSN_CODE (insn) < 0)\n\
  330. fatal_insn (\"unrecognizable insn:\", insn);\n\
  331. else\n\
  332. fatal_insn (\"insn with invalid code number:\", insn);\n");
  333. }
  334. int
  335. main (int argc, char **argv)
  336. {
  337. rtx desc;
  338. unsigned int i;
  339. struct extraction *p;
  340. struct code_ptr *link;
  341. const char *name;
  342. int insn_code_number;
  343. progname = "genextract";
  344. if (!init_rtx_reader_args (argc, argv))
  345. return (FATAL_EXIT_CODE);
  346. /* Read the machine description. */
  347. while ((desc = read_md_rtx (&line_no, &insn_code_number)) != NULL)
  348. {
  349. if (GET_CODE (desc) == DEFINE_INSN)
  350. gen_insn (desc, insn_code_number);
  351. else if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  352. {
  353. struct code_ptr *link = XNEW (struct code_ptr);
  354. link->insn_code = insn_code_number;
  355. link->next = peepholes;
  356. peepholes = link;
  357. }
  358. }
  359. if (have_error)
  360. return FATAL_EXIT_CODE;
  361. print_header ();
  362. /* Write out code to handle peepholes and the insn_codes that it should
  363. be called for. */
  364. if (peepholes)
  365. {
  366. for (link = peepholes; link; link = link->next)
  367. printf (" case %d:\n", link->insn_code);
  368. /* The vector in the insn says how many operands it has.
  369. And all it contains are operands. In fact, the vector was
  370. created just for the sake of this function. We need to set the
  371. location of the operands for sake of simplifications after
  372. extraction, like eliminating subregs. */
  373. puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
  374. " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
  375. " break;\n");
  376. }
  377. /* Write out all the ways to extract insn operands. */
  378. for (p = extractions; p; p = p->next)
  379. {
  380. for (link = p->insns; link; link = link->next)
  381. {
  382. i = link->insn_code;
  383. name = get_insn_name (i);
  384. if (name)
  385. printf (" case %d: /* %s */\n", i, name);
  386. else
  387. printf (" case %d:\n", i);
  388. }
  389. for (i = 0; i < p->op_count; i++)
  390. {
  391. if (p->oplocs[i] == 0)
  392. {
  393. printf (" ro[%d] = const0_rtx;\n", i);
  394. printf (" ro_loc[%d] = &junk;\n", i);
  395. }
  396. else
  397. {
  398. printf (" ro[%d] = *(ro_loc[%d] = &", i, i);
  399. print_path (p->oplocs[i]);
  400. puts (");");
  401. }
  402. }
  403. for (i = 0; i < p->dup_count; i++)
  404. {
  405. printf (" recog_data.dup_loc[%d] = &", i);
  406. print_path (p->duplocs[i]);
  407. puts (";");
  408. printf (" recog_data.dup_num[%d] = %d;\n", i, p->dupnums[i]);
  409. }
  410. puts (" break;\n");
  411. }
  412. puts (" }\n}");
  413. fflush (stdout);
  414. return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  415. }