genflags.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Generate from machine description:
  2. - some flags HAVE_... saying which simple standard instructions are
  3. available for this machine.
  4. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "bconfig.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "rtl.h"
  22. #include "obstack.h"
  23. #include "errors.h"
  24. #include "read-md.h"
  25. #include "gensupport.h"
  26. /* Obstack to remember insns with. */
  27. static struct obstack obstack;
  28. /* Max size of names encountered. */
  29. static int max_id_len;
  30. /* Max operand encountered in a scan over some insn. */
  31. static int max_opno;
  32. static void max_operand_1 (rtx);
  33. static int num_operands (rtx);
  34. static void gen_proto (rtx);
  35. static void gen_macro (const char *, int, int);
  36. static void gen_insn (int, rtx);
  37. /* Count the number of match_operand's found. */
  38. static void
  39. max_operand_1 (rtx x)
  40. {
  41. RTX_CODE code;
  42. int i;
  43. int len;
  44. const char *fmt;
  45. if (x == 0)
  46. return;
  47. code = GET_CODE (x);
  48. if (code == MATCH_OPERAND || code == MATCH_OPERATOR
  49. || code == MATCH_PARALLEL)
  50. max_opno = MAX (max_opno, XINT (x, 0));
  51. fmt = GET_RTX_FORMAT (code);
  52. len = GET_RTX_LENGTH (code);
  53. for (i = 0; i < len; i++)
  54. {
  55. if (fmt[i] == 'e' || fmt[i] == 'u')
  56. max_operand_1 (XEXP (x, i));
  57. else if (fmt[i] == 'E')
  58. {
  59. int j;
  60. for (j = 0; j < XVECLEN (x, i); j++)
  61. max_operand_1 (XVECEXP (x, i, j));
  62. }
  63. }
  64. }
  65. static int
  66. num_operands (rtx insn)
  67. {
  68. int len = XVECLEN (insn, 1);
  69. int i;
  70. max_opno = -1;
  71. for (i = 0; i < len; i++)
  72. max_operand_1 (XVECEXP (insn, 1, i));
  73. return max_opno + 1;
  74. }
  75. /* Print out a wrapper macro for a function which corrects the number
  76. of arguments it takes. Any missing arguments are assumed to be at
  77. the end. */
  78. static void
  79. gen_macro (const char *name, int real, int expect)
  80. {
  81. int i;
  82. gcc_assert (real <= expect);
  83. gcc_assert (real);
  84. /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
  85. fputs ("#define GEN_", stdout);
  86. for (i = 0; name[i]; i++)
  87. putchar (TOUPPER (name[i]));
  88. putchar ('(');
  89. for (i = 0; i < expect - 1; i++)
  90. printf ("%c, ", i + 'A');
  91. printf ("%c) gen_%s (", i + 'A', name);
  92. for (i = 0; i < real - 1; i++)
  93. printf ("(%c), ", i + 'A');
  94. printf ("(%c))\n", i + 'A');
  95. }
  96. /* Print out prototype information for a generator function. If the
  97. insn pattern has been elided, print out a dummy generator that
  98. does nothing. */
  99. static void
  100. gen_proto (rtx insn)
  101. {
  102. int num = num_operands (insn);
  103. int i;
  104. const char *name = XSTR (insn, 0);
  105. int truth = maybe_eval_c_test (XSTR (insn, 2));
  106. /* Many md files don't refer to the last two operands passed to the
  107. call patterns. This means their generator functions will be two
  108. arguments too short. Instead of changing every md file to touch
  109. those operands, we wrap the prototypes in macros that take the
  110. correct number of arguments. */
  111. if (name[0] == 'c' || name[0] == 's')
  112. {
  113. if (!strcmp (name, "call")
  114. || !strcmp (name, "call_pop")
  115. || !strcmp (name, "sibcall")
  116. || !strcmp (name, "sibcall_pop"))
  117. gen_macro (name, num, 4);
  118. else if (!strcmp (name, "call_value")
  119. || !strcmp (name, "call_value_pop")
  120. || !strcmp (name, "sibcall_value")
  121. || !strcmp (name, "sibcall_value_pop"))
  122. gen_macro (name, num, 5);
  123. }
  124. if (truth != 0)
  125. printf ("extern rtx gen_%-*s (", max_id_len, name);
  126. else
  127. printf ("static inline rtx gen_%-*s (", max_id_len, name);
  128. if (num == 0)
  129. fputs ("void", stdout);
  130. else
  131. {
  132. for (i = 1; i < num; i++)
  133. fputs ("rtx, ", stdout);
  134. fputs ("rtx", stdout);
  135. }
  136. puts (");");
  137. /* Some back ends want to take the address of generator functions,
  138. so we cannot simply use #define for these dummy definitions. */
  139. if (truth == 0)
  140. {
  141. printf ("static inline rtx\ngen_%s", name);
  142. if (num > 0)
  143. {
  144. putchar ('(');
  145. for (i = 0; i < num-1; i++)
  146. printf ("rtx ARG_UNUSED (%c), ", 'a' + i);
  147. printf ("rtx ARG_UNUSED (%c))\n", 'a' + i);
  148. }
  149. else
  150. puts ("(void)");
  151. puts ("{\n return 0;\n}");
  152. }
  153. }
  154. static void
  155. gen_insn (int line_no, rtx insn)
  156. {
  157. const char *name = XSTR (insn, 0);
  158. const char *p;
  159. const char *lt, *gt;
  160. int len;
  161. int truth = maybe_eval_c_test (XSTR (insn, 2));
  162. lt = strchr (name, '<');
  163. if (lt && strchr (lt + 1, '>'))
  164. {
  165. message_with_line (line_no, "unresolved iterator");
  166. have_error = 1;
  167. return;
  168. }
  169. gt = strchr (name, '>');
  170. if (lt || gt)
  171. {
  172. message_with_line (line_no,
  173. "unmatched angle brackets, likely "
  174. "an error in iterator syntax");
  175. have_error = 1;
  176. return;
  177. }
  178. /* Don't mention instructions whose names are the null string
  179. or begin with '*'. They are in the machine description just
  180. to be recognized. */
  181. if (name[0] == 0 || name[0] == '*')
  182. return;
  183. len = strlen (name);
  184. if (len > max_id_len)
  185. max_id_len = len;
  186. if (truth == 0)
  187. /* Emit nothing. */;
  188. else if (truth == 1)
  189. printf ("#define HAVE_%s 1\n", name);
  190. else
  191. {
  192. /* Write the macro definition, putting \'s at the end of each line,
  193. if more than one. */
  194. printf ("#define HAVE_%s (", name);
  195. for (p = XSTR (insn, 2); *p; p++)
  196. {
  197. if (IS_VSPACE (*p))
  198. fputs (" \\\n", stdout);
  199. else
  200. putchar (*p);
  201. }
  202. fputs (")\n", stdout);
  203. }
  204. obstack_grow (&obstack, &insn, sizeof (rtx));
  205. }
  206. int
  207. main (int argc, char **argv)
  208. {
  209. rtx desc;
  210. rtx dummy;
  211. rtx *insns;
  212. rtx *insn_ptr;
  213. progname = "genflags";
  214. obstack_init (&obstack);
  215. /* We need to see all the possibilities. Elided insns may have
  216. direct calls to their generators in C code. */
  217. insn_elision = 0;
  218. if (!init_rtx_reader_args (argc, argv))
  219. return (FATAL_EXIT_CODE);
  220. puts ("/* Generated automatically by the program `genflags'");
  221. puts (" from the machine description file `md'. */\n");
  222. puts ("#ifndef GCC_INSN_FLAGS_H");
  223. puts ("#define GCC_INSN_FLAGS_H\n");
  224. /* Read the machine description. */
  225. while (1)
  226. {
  227. int line_no, insn_code_number = 0;
  228. desc = read_md_rtx (&line_no, &insn_code_number);
  229. if (desc == NULL)
  230. break;
  231. if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
  232. gen_insn (line_no, desc);
  233. }
  234. /* Print out the prototypes now. */
  235. dummy = (rtx) 0;
  236. obstack_grow (&obstack, &dummy, sizeof (rtx));
  237. insns = XOBFINISH (&obstack, rtx *);
  238. for (insn_ptr = insns; *insn_ptr; insn_ptr++)
  239. gen_proto (*insn_ptr);
  240. puts ("\n#endif /* GCC_INSN_FLAGS_H */");
  241. if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
  242. return FATAL_EXIT_CODE;
  243. return SUCCESS_EXIT_CODE;
  244. }