genemit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "rtl.h"
  19. #include <obstack.h>
  20. struct obstack obstack;
  21. struct obstack *current_obstack = &obstack;
  22. #define obstack_chunk_alloc xmalloc
  23. #define obstack_chunk_free free
  24. extern int xmalloc ();
  25. extern void free ();
  26. void fatal ();
  27. int max_opno;
  28. int register_constraints;
  29. int insn_code_number;
  30. #define max(a, b) ((a) > (b) ? (a) : (b))
  31. void
  32. max_operand_1 (x)
  33. rtx x;
  34. {
  35. register RTX_CODE code = GET_CODE (x);
  36. register int i;
  37. register int len;
  38. register char *fmt;
  39. if (code == MATCH_OPERAND && XSTR (x, 2) != 0)
  40. register_constraints = 1;
  41. if (code == MATCH_OPERAND || code == MATCH_DUP)
  42. max_opno = max (max_opno, XINT (x, 0));
  43. fmt = GET_RTX_FORMAT (code);
  44. len = GET_RTX_LENGTH (code);
  45. for (i = 0; i < len; i++)
  46. {
  47. if (fmt[i] == 'e' || fmt[i] == 'u')
  48. max_operand_1 (XEXP (x, i));
  49. }
  50. }
  51. int
  52. max_operand_vec (insn, arg)
  53. rtx insn;
  54. int arg;
  55. {
  56. register int len = XVECLEN (insn, arg);
  57. register int i;
  58. max_opno = -1;
  59. for (i = 0; i < len; i++)
  60. max_operand_1 (XVECEXP (insn, arg, i));
  61. return max_opno + 1;
  62. }
  63. void
  64. print_code (code)
  65. RTX_CODE code;
  66. {
  67. register char *p1;
  68. for (p1 = GET_RTX_NAME (code); *p1; p1++)
  69. {
  70. if (*p1 >= 'a' && *p1 <= 'z')
  71. putchar (*p1 + 'A' - 'a');
  72. else
  73. putchar (*p1);
  74. }
  75. }
  76. void
  77. gen_exp (x)
  78. rtx x;
  79. {
  80. register RTX_CODE code;
  81. register int i;
  82. register int len;
  83. register char *fmt;
  84. if (x == 0)
  85. {
  86. printf ("0");
  87. return;
  88. }
  89. code = GET_CODE (x);
  90. if (code == MATCH_OPERAND)
  91. {
  92. printf ("operand%d", XINT (x, 0));
  93. return;
  94. }
  95. if (code == MATCH_DUP)
  96. {
  97. printf ("copy_rtx (operand%d)", XINT (x, 0));
  98. return;
  99. }
  100. if (code == ADDRESS)
  101. fatal ("ADDRESS expression code used in named instruction pattern");
  102. if (code == PC)
  103. {
  104. printf ("pc_rtx");
  105. return;
  106. }
  107. if (code == CC0)
  108. {
  109. printf ("cc0_rtx");
  110. return;
  111. }
  112. if (code == CONST_INT && INTVAL (x) == 0)
  113. {
  114. printf ("const0_rtx");
  115. return;
  116. }
  117. if (code == CONST_INT && INTVAL (x) == 1)
  118. {
  119. printf ("const1_rtx");
  120. return;
  121. }
  122. printf ("gen_rtx (");
  123. print_code (code);
  124. printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  125. fmt = GET_RTX_FORMAT (code);
  126. len = GET_RTX_LENGTH (code);
  127. for (i = 0; i < len; i++)
  128. {
  129. if (fmt[i] == '0')
  130. break;
  131. printf (", ");
  132. if (fmt[i] == 'e' || fmt[i] == 'u')
  133. gen_exp (XEXP (x, i));
  134. else if (fmt[i] == 'i')
  135. printf ("%d", XINT (x, i));
  136. else
  137. abort ();
  138. }
  139. printf (")");
  140. }
  141. void
  142. gen_insn (insn)
  143. rtx insn;
  144. {
  145. int operands;
  146. register int i;
  147. /* Don't mention instructions whose names are the null string.
  148. They are in the machine description just to be recognized. */
  149. if (strlen (XSTR (insn, 0)) == 0)
  150. return;
  151. /* Find out how many operands this function has,
  152. and also whether any of them have register constraints. */
  153. register_constraints = 0;
  154. operands = max_operand_vec (insn, 1);
  155. /* Output the function name and argument declarations. */
  156. printf ("rtx\ngen_%s (", XSTR (insn, 0));
  157. for (i = 0; i < operands; i++)
  158. printf (i ? ", operand%d" : "operand%d", i);
  159. printf (")\n");
  160. for (i = 0; i < operands; i++)
  161. printf (" rtx operand%d;\n", i);
  162. printf ("{\n");
  163. /* Output code to construct and return the rtl for the instruction body */
  164. if (XVECLEN (insn, 1) == 1)
  165. {
  166. printf (" return ");
  167. gen_exp (XVECEXP (insn, 1, 0));
  168. printf (";\n}\n\n");
  169. }
  170. else
  171. {
  172. printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
  173. for (i = 0; i < XVECLEN (insn, 1); i++)
  174. {
  175. printf (",\n\t\t");
  176. gen_exp (XVECEXP (insn, 1, i));
  177. }
  178. printf ("));\n}\n\n");
  179. }
  180. }
  181. xmalloc (size)
  182. {
  183. register int val = malloc (size);
  184. if (val == 0)
  185. abort ();
  186. return val;
  187. }
  188. int
  189. xrealloc (ptr, size)
  190. char *ptr;
  191. int size;
  192. {
  193. int result = realloc (ptr, size);
  194. if (!result)
  195. abort ();
  196. return result;
  197. }
  198. void
  199. fatal (s, a1, a2)
  200. {
  201. fprintf (stderr, "genemit: ");
  202. fprintf (stderr, s, a1, a2);
  203. fprintf (stderr, "\n");
  204. exit (1);
  205. }
  206. main (argc, argv)
  207. int argc;
  208. char **argv;
  209. {
  210. rtx desc;
  211. FILE *infile;
  212. extern rtx read_rtx ();
  213. register int c;
  214. obstack_begin (current_obstack, 4060);
  215. if (argc <= 1)
  216. fatal ("No input file name.");
  217. infile = fopen (argv[1], "r");
  218. if (infile == 0)
  219. {
  220. perror (argv[1]);
  221. exit (1);
  222. }
  223. init_rtl ();
  224. /* Assign sequential codes to all entries in the machine description
  225. in parallel with the tables in insn-output.c. */
  226. insn_code_number = 0;
  227. printf ("/* Generated automatically by the program `genemit'\n\
  228. from the machine description file `md'. */\n\n");
  229. printf ("#include \"rtl.h\"\n");
  230. printf ("#include \"insn-config.h\"\n");
  231. printf ("\nextern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
  232. /* Read the machine description. */
  233. while (1)
  234. {
  235. c = read_skip_spaces (infile);
  236. if (c == EOF)
  237. break;
  238. ungetc (c, infile);
  239. desc = read_rtx (infile);
  240. gen_insn (desc);
  241. ++insn_code_number;
  242. }
  243. return 0;
  244. }