genemit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Generate code from machine description to emit insns as rtl.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include <stdio.h>
  18. #include "config.h"
  19. #include "rtl.h"
  20. #include "obstack.h"
  21. struct obstack obstack;
  22. struct obstack *rtl_obstack = &obstack;
  23. #define obstack_chunk_alloc xmalloc
  24. #define obstack_chunk_free free
  25. extern int xmalloc ();
  26. extern void free ();
  27. void fatal ();
  28. int max_opno;
  29. int max_dup_opno;
  30. int register_constraints;
  31. int insn_code_number;
  32. #define max(a, b) ((a) > (b) ? (a) : (b))
  33. void
  34. max_operand_1 (x)
  35. rtx x;
  36. {
  37. register RTX_CODE code;
  38. register int i;
  39. register int len;
  40. register char *fmt;
  41. if (x == 0)
  42. return;
  43. code = GET_CODE (x);
  44. if (code == MATCH_OPERAND && XSTR (x, 2) != 0)
  45. register_constraints = 1;
  46. if (code == MATCH_OPERAND)
  47. max_opno = max (max_opno, XINT (x, 0));
  48. if (code == MATCH_DUP)
  49. max_dup_opno = max (max_dup_opno, XINT (x, 0));
  50. fmt = GET_RTX_FORMAT (code);
  51. len = GET_RTX_LENGTH (code);
  52. for (i = 0; i < len; i++)
  53. {
  54. if (fmt[i] == 'e' || fmt[i] == 'u')
  55. max_operand_1 (XEXP (x, i));
  56. else if (fmt[i] == 'E')
  57. {
  58. int j;
  59. for (j = 0; j < XVECLEN (x, i); j++)
  60. max_operand_1 (XVECEXP (x, i, j));
  61. }
  62. }
  63. }
  64. int
  65. max_operand_vec (insn, arg)
  66. rtx insn;
  67. int arg;
  68. {
  69. register int len = XVECLEN (insn, arg);
  70. register int i;
  71. max_opno = -1;
  72. max_dup_opno = -1;
  73. for (i = 0; i < len; i++)
  74. max_operand_1 (XVECEXP (insn, arg, i));
  75. return max_opno + 1;
  76. }
  77. void
  78. print_code (code)
  79. RTX_CODE code;
  80. {
  81. register char *p1;
  82. for (p1 = GET_RTX_NAME (code); *p1; p1++)
  83. {
  84. if (*p1 >= 'a' && *p1 <= 'z')
  85. putchar (*p1 + 'A' - 'a');
  86. else
  87. putchar (*p1);
  88. }
  89. }
  90. /* Print a C expression to construct an RTX just like X,
  91. substituting any operand references appearing within. */
  92. void
  93. gen_exp (x)
  94. rtx x;
  95. {
  96. register RTX_CODE code;
  97. register int i;
  98. register int len;
  99. register char *fmt;
  100. if (x == 0)
  101. {
  102. printf ("0");
  103. return;
  104. }
  105. code = GET_CODE (x);
  106. switch (code)
  107. {
  108. case MATCH_OPERAND:
  109. case MATCH_DUP:
  110. printf ("operand%d", XINT (x, 0));
  111. return;
  112. case ADDRESS:
  113. fatal ("ADDRESS expression code used in named instruction pattern");
  114. case PC:
  115. printf ("pc_rtx");
  116. return;
  117. case CC0:
  118. printf ("cc0_rtx");
  119. return;
  120. case CONST_INT:
  121. if (INTVAL (x) == 0)
  122. {
  123. printf ("const0_rtx");
  124. return;
  125. }
  126. if (INTVAL (x) == 1)
  127. {
  128. printf ("const1_rtx");
  129. return;
  130. }
  131. }
  132. printf ("gen_rtx (");
  133. print_code (code);
  134. printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  135. fmt = GET_RTX_FORMAT (code);
  136. len = GET_RTX_LENGTH (code);
  137. for (i = 0; i < len; i++)
  138. {
  139. if (fmt[i] == '0')
  140. break;
  141. printf (", ");
  142. if (fmt[i] == 'e' || fmt[i] == 'u')
  143. gen_exp (XEXP (x, i));
  144. else if (fmt[i] == 'i')
  145. printf ("%d", XINT (x, i));
  146. else if (fmt[i] == 'E')
  147. {
  148. int j;
  149. printf ("gen_rtvec (%d", XVECLEN (x, i));
  150. for (j = 0; j < XVECLEN (x, i); j++)
  151. {
  152. printf (", ");
  153. gen_exp (XVECEXP (x, i, j));
  154. }
  155. printf (")");
  156. }
  157. else
  158. abort ();
  159. }
  160. printf (")");
  161. }
  162. /* Generate the `gen_...' function for a DEFINE_INSN. */
  163. void
  164. gen_insn (insn)
  165. rtx insn;
  166. {
  167. int operands;
  168. register int i;
  169. /* Don't mention instructions whose names are the null string.
  170. They are in the machine description just to be recognized. */
  171. if (strlen (XSTR (insn, 0)) == 0)
  172. return;
  173. /* Find out how many operands this function has,
  174. and also whether any of them have register constraints. */
  175. register_constraints = 0;
  176. operands = max_operand_vec (insn, 1);
  177. if (max_dup_opno >= operands)
  178. fatal ("match_dup operand number has no match_operand");
  179. /* Output the function name and argument declarations. */
  180. printf ("rtx\ngen_%s (", XSTR (insn, 0));
  181. for (i = 0; i < operands; i++)
  182. printf (i ? ", operand%d" : "operand%d", i);
  183. printf (")\n");
  184. for (i = 0; i < operands; i++)
  185. printf (" rtx operand%d;\n", i);
  186. printf ("{\n");
  187. /* Output code to construct and return the rtl for the instruction body */
  188. if (XVECLEN (insn, 1) == 1)
  189. {
  190. printf (" return ");
  191. gen_exp (XVECEXP (insn, 1, 0));
  192. printf (";\n}\n\n");
  193. }
  194. else
  195. {
  196. printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
  197. for (i = 0; i < XVECLEN (insn, 1); i++)
  198. {
  199. printf (",\n\t\t");
  200. gen_exp (XVECEXP (insn, 1, i));
  201. }
  202. printf ("));\n}\n\n");
  203. }
  204. }
  205. /* Generate the `gen_...' function for a DEFINE_EXPAND. */
  206. void
  207. gen_expand (expand)
  208. rtx expand;
  209. {
  210. int operands;
  211. register int i;
  212. if (strlen (XSTR (expand, 0)) == 0)
  213. fatal ("define_expand lacks a name");
  214. if (XVEC (expand, 1) == 0)
  215. fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
  216. /* Find out how many operands this function has,
  217. and also whether any of them have register constraints. */
  218. register_constraints = 0;
  219. operands = max_operand_vec (expand, 1);
  220. /* Output the function name and argument declarations. */
  221. printf ("rtx\ngen_%s (", XSTR (expand, 0));
  222. for (i = 0; i < operands; i++)
  223. printf (i ? ", operand%d" : "operand%d", i);
  224. printf (")\n");
  225. for (i = 0; i < operands; i++)
  226. printf (" rtx operand%d;\n", i);
  227. printf ("{\n");
  228. /* For each operand referred to only with MATCH_DUPs,
  229. make a local variable. */
  230. for (i = operands; i <= max_dup_opno; i++)
  231. printf (" rtx operand%d;\n", i);
  232. printf (" rtx operands[%d];\n\n", max (operands, max_dup_opno + 1));
  233. printf (" extern rtx gen_sequence ();\n");
  234. printf (" extern int emit_to_sequence;\n\n");
  235. printf (" emit_to_sequence++;\n");
  236. /* The fourth operand of DEFINE_EXPAND is some code to be executed
  237. before the actual construction.
  238. This code expects to refer to `operands'
  239. just as the output-code in a DEFINE_INSN does,
  240. but here `operands' is an automatic array.
  241. So copy the operand values there before executing it. */
  242. if (XSTR (expand, 3))
  243. {
  244. /* Output code to copy the arguments into `operands'. */
  245. for (i = 0; i < operands; i++)
  246. printf (" operands[%d] = operand%d;\n", i, i);
  247. /* Output the special code to be executed before the sequence
  248. is generated. */
  249. printf ("%s\n", XSTR (expand, 3));
  250. /* Output code to copy the arguments back out of `operands'
  251. (unless we aren't going to use them at all). */
  252. if (XVEC (expand, 1) != 0)
  253. {
  254. for (i = 0; i < operands; i++)
  255. printf (" operand%d = operands[%d];\n", i, i);
  256. for (; i <= max_dup_opno; i++)
  257. printf (" operand%d = operands[%d];\n", i, i);
  258. }
  259. }
  260. /* Output code to construct the rtl for the instruction bodies.
  261. Use emit_insn to add them to the sequence being accumulated.
  262. But don't do this if the user's code has set `no_more' nonzero. */
  263. for (i = 0; i < XVECLEN (expand, 1); i++)
  264. {
  265. rtx next = XVECEXP (expand, 1, i);
  266. if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
  267. || (GET_CODE (next) == PARALLEL
  268. && GET_CODE (XVECEXP (next, 0, 0)) == SET
  269. && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC))
  270. printf (" emit_jump_insn (");
  271. else if (GET_CODE (next) == CODE_LABEL)
  272. printf (" emit_label (");
  273. else
  274. printf (" emit_insn (");
  275. gen_exp (next);
  276. printf (");\n");
  277. if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
  278. && GET_CODE (SET_SRC (next)) == LABEL_REF)
  279. printf (" emit_barrier ();");
  280. }
  281. printf (" emit_to_sequence--;\n");
  282. /* Call `gen_sequence' to make a SEQUENCE out of all the
  283. insns emitted within this gen_... function. */
  284. printf (" return gen_sequence ();\n}\n\n");
  285. }
  286. int
  287. xmalloc (size)
  288. {
  289. register int val = malloc (size);
  290. if (val == 0)
  291. fatal ("virtual memory exhausted");
  292. return val;
  293. }
  294. int
  295. xrealloc (ptr, size)
  296. char *ptr;
  297. int size;
  298. {
  299. int result = realloc (ptr, size);
  300. if (!result)
  301. fatal ("virtual memory exhausted");
  302. return result;
  303. }
  304. void
  305. fatal (s, a1, a2)
  306. {
  307. fprintf (stderr, "genemit: ");
  308. fprintf (stderr, s, a1, a2);
  309. fprintf (stderr, "\n");
  310. exit (FATAL_EXIT_CODE);
  311. }
  312. int
  313. main (argc, argv)
  314. int argc;
  315. char **argv;
  316. {
  317. rtx desc;
  318. FILE *infile;
  319. extern rtx read_rtx ();
  320. register int c;
  321. obstack_init (rtl_obstack);
  322. if (argc <= 1)
  323. fatal ("No input file name.");
  324. infile = fopen (argv[1], "r");
  325. if (infile == 0)
  326. {
  327. perror (argv[1]);
  328. exit (FATAL_EXIT_CODE);
  329. }
  330. init_rtl ();
  331. /* Assign sequential codes to all entries in the machine description
  332. in parallel with the tables in insn-output.c. */
  333. insn_code_number = 0;
  334. printf ("/* Generated automatically by the program `genemit'\n\
  335. from the machine description file `md'. */\n\n");
  336. printf ("#include \"config.h\"\n");
  337. printf ("#include \"rtl.h\"\n");
  338. printf ("#include \"expr.h\"\n");
  339. printf ("#include \"insn-config.h\"\n\n");
  340. printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
  341. printf ("extern rtx recog_operand[];\n");
  342. printf ("#define operands recog_operand\n\n");
  343. printf ("#define FAIL do { emit_to_sequence--; return 0;} while (0)\n\n");
  344. printf ("#define DONE do { emit_to_sequence--; return gen_sequence ();} while (0)\n\n");
  345. /* Read the machine description. */
  346. while (1)
  347. {
  348. c = read_skip_spaces (infile);
  349. if (c == EOF)
  350. break;
  351. ungetc (c, infile);
  352. desc = read_rtx (infile);
  353. if (GET_CODE (desc) == DEFINE_INSN)
  354. {
  355. gen_insn (desc);
  356. ++insn_code_number;
  357. }
  358. if (GET_CODE (desc) == DEFINE_EXPAND)
  359. {
  360. gen_expand (desc);
  361. ++insn_code_number;
  362. }
  363. if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  364. {
  365. ++insn_code_number;
  366. }
  367. }
  368. fflush (stdout);
  369. exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  370. }