genextract.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Generate code from machine description to extract operands from insn 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. /* Number instruction patterns handled, starting at 0 for first one. */
  28. int insn_code_number;
  29. /* Number the occurrences of MATCH_DUP in each instruction,
  30. starting at 0 for the first occurrence. */
  31. int dup_count;
  32. /* While tree-walking an instruction pattern, we keep a chain
  33. of these `struct link's to record how to get down to the
  34. current position. In each one, POS is the operand number
  35. or vector element number, and VEC is nonzero for a vector. */
  36. struct link
  37. {
  38. struct link *next;
  39. int pos;
  40. int vec;
  41. };
  42. void
  43. gen_insn (insn)
  44. rtx insn;
  45. {
  46. register int i;
  47. dup_count = 0;
  48. /* Output the function name and argument declaration. */
  49. /* It would be cleaner to make `int' the return type
  50. but 4.2 vax compiler doesn't accept that in the array
  51. that these functions are supposed to go in. */
  52. printf ("int\nextract_%d (insn)\n rtx insn;\n", insn_code_number);
  53. printf ("{\n");
  54. /* Walk the insn's pattern, remembering at all times the path
  55. down to the walking point. */
  56. if (XVECLEN (insn, 1) == 1)
  57. walk_rtx (XVECEXP (insn, 1, 0), 0);
  58. else
  59. for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
  60. {
  61. struct link link;
  62. link.next = 0;
  63. link.pos = i;
  64. link.vec = 1;
  65. walk_rtx (XVECEXP (insn, 1, i), &link);
  66. }
  67. printf ("}\n\n");
  68. }
  69. walk_rtx (x, path)
  70. rtx x;
  71. struct link *path;
  72. {
  73. register RTX_CODE code = GET_CODE (x);
  74. register int i;
  75. register int len;
  76. register char *fmt;
  77. struct link link;
  78. switch (code)
  79. {
  80. case PC:
  81. case CC0:
  82. case CONST_INT:
  83. case SYMBOL_REF:
  84. return;
  85. case MATCH_OPERAND:
  86. printf (" recog_operand[%d] = *(recog_operand_loc[%d]\n = &",
  87. XINT (x, 0), XINT (x, 0));
  88. print_path (path);
  89. printf (");\n");
  90. break;
  91. case MATCH_DUP:
  92. printf (" recog_dup_loc[%d] = &", dup_count);
  93. print_path (path);
  94. printf (";\n");
  95. printf (" recog_dup_num[%d] = %d;\n", dup_count, XINT (x, 0));
  96. dup_count++;
  97. break;
  98. case ADDRESS:
  99. walk_rtx (XEXP (x, 0), path);
  100. return;
  101. }
  102. link.next = path;
  103. link.vec = 0;
  104. fmt = GET_RTX_FORMAT (code);
  105. len = GET_RTX_LENGTH (code);
  106. for (i = 0; i < len; i++)
  107. if (fmt[i] == 'e' || fmt[i] == 'u')
  108. {
  109. link.pos = i;
  110. walk_rtx (XEXP (x, i), &link);
  111. }
  112. }
  113. /* Given a PATH, representing a path down the instruction's
  114. pattern from the root to a certain point, output code to
  115. evaluate to the rtx at that point. */
  116. print_path (path)
  117. struct link *path;
  118. {
  119. if (path == 0)
  120. printf ("insn");
  121. else if (path->vec)
  122. {
  123. printf ("XVECEXP (");
  124. print_path (path->next);
  125. printf (", 0, %d)", path->pos);
  126. }
  127. else
  128. {
  129. printf ("XEXP (");
  130. print_path (path->next);
  131. printf (", %d)", path->pos);
  132. }
  133. }
  134. xmalloc (size)
  135. {
  136. register int val = malloc (size);
  137. if (val == 0)
  138. abort ();
  139. return val;
  140. }
  141. int
  142. xrealloc (ptr, size)
  143. char *ptr;
  144. int size;
  145. {
  146. int result = realloc (ptr, size);
  147. if (!result)
  148. abort ();
  149. return result;
  150. }
  151. void
  152. fatal (s, a1, a2)
  153. {
  154. fprintf (stderr, "genemit: ");
  155. fprintf (stderr, s, a1, a2);
  156. fprintf (stderr, "\n");
  157. exit (1);
  158. }
  159. main (argc, argv)
  160. int argc;
  161. char **argv;
  162. {
  163. rtx desc;
  164. FILE *infile;
  165. extern rtx read_rtx ();
  166. register int c, i;
  167. obstack_begin (current_obstack, 4060);
  168. if (argc <= 1)
  169. fatal ("No input file name.");
  170. infile = fopen (argv[1], "r");
  171. if (infile == 0)
  172. {
  173. perror (argv[1]);
  174. exit (1);
  175. }
  176. init_rtl ();
  177. /* Assign sequential codes to all entries in the machine description
  178. in parallel with the tables in insn-output.c. */
  179. insn_code_number = 0;
  180. printf ("/* Generated automatically by the program `genextract'\n\
  181. from the machine description file `md'. */\n\n");
  182. printf ("#include \"rtl.h\"\n\n");
  183. printf ("extern rtx recog_operand[];\n");
  184. printf ("extern rtx *recog_operand_loc[];\n");
  185. printf ("extern rtx *recog_dup_loc[];\n");
  186. printf ("extern char recog_dup_num[];\n\n");
  187. /* Read the machine description. */
  188. while (1)
  189. {
  190. c = read_skip_spaces (infile);
  191. if (c == EOF)
  192. break;
  193. ungetc (c, infile);
  194. desc = read_rtx (infile);
  195. gen_insn (desc);
  196. ++insn_code_number;
  197. }
  198. printf ("int (*insn_extract_fn[]) () =\n{ ");
  199. for (i = 0; i < insn_code_number; i++)
  200. {
  201. if (i % 4 != 0)
  202. printf (", ");
  203. else if (i != 0)
  204. printf (",\n ");
  205. printf ("extract_%d", i);
  206. }
  207. printf ("\n};\n\n");
  208. printf ("void\ninsn_extract (insn)\n");
  209. printf (" rtx insn;\n");
  210. printf ("{\n if (INSN_CODE (insn) == -1) abort ();\n");
  211. printf (" (*insn_extract_fn[INSN_CODE (insn)]) (PATTERN (insn));\n}\n");
  212. return 0;
  213. }