genextract.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "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 walk_rtx ();
  28. void print_path ();
  29. void fatal ();
  30. /* Number instruction patterns handled, starting at 0 for first one. */
  31. int insn_code_number;
  32. /* Number the occurrences of MATCH_DUP in each instruction,
  33. starting at 0 for the first occurrence. */
  34. int dup_count;
  35. /* While tree-walking an instruction pattern, we keep a chain
  36. of these `struct link's to record how to get down to the
  37. current position. In each one, POS is the operand number,
  38. and if the operand is a vector VEC is the element number.
  39. VEC is -1 if the operand is not a vector. */
  40. struct link
  41. {
  42. struct link *next;
  43. int pos;
  44. int vecelt;
  45. };
  46. void
  47. gen_insn (insn)
  48. rtx insn;
  49. {
  50. register int i;
  51. dup_count = 0;
  52. /* Output the function name and argument declaration. */
  53. /* It would be cleaner to make `int' the return type
  54. but 4.2 vax compiler doesn't accept that in the array
  55. that these functions are supposed to go in. */
  56. printf ("VOID\nextract_%d (insn)\n rtx insn;\n", insn_code_number);
  57. printf ("{\n");
  58. /* Walk the insn's pattern, remembering at all times the path
  59. down to the walking point. */
  60. if (XVECLEN (insn, 1) == 1)
  61. walk_rtx (XVECEXP (insn, 1, 0), 0);
  62. else
  63. for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
  64. {
  65. struct link link;
  66. link.next = 0;
  67. link.pos = 0;
  68. link.vecelt = i;
  69. walk_rtx (XVECEXP (insn, 1, i), &link);
  70. }
  71. printf ("}\n\n");
  72. }
  73. /* Like gen_insn but handles `define_peephole'. */
  74. void
  75. gen_peephole (peep)
  76. rtx peep;
  77. {
  78. /* Output the function name and argument declaration. */
  79. printf ("VOID\nextract_%d (insn)\n rtx insn;\n", insn_code_number);
  80. printf ("{\n");
  81. /* The vector in the insn says how many operands it has.
  82. And all it contains are operands. In fact, the vector was
  83. created just for the sake of this function. */
  84. printf ("\
  85. bcopy (&XVECEXP (insn, 0, 0), recog_operand,\
  86. UNITS_PER_WORD * XVECLEN (insn, 0));\n");
  87. printf ("}\n\n");
  88. }
  89. void
  90. walk_rtx (x, path)
  91. rtx x;
  92. struct link *path;
  93. {
  94. register RTX_CODE code;
  95. register int i;
  96. register int len;
  97. register char *fmt;
  98. struct link link;
  99. if (x == 0)
  100. return;
  101. code = GET_CODE (x);
  102. switch (code)
  103. {
  104. case PC:
  105. case CC0:
  106. case CONST_INT:
  107. case SYMBOL_REF:
  108. return;
  109. case MATCH_OPERAND:
  110. printf (" recog_operand[%d] = *(recog_operand_loc[%d]\n = &",
  111. XINT (x, 0), XINT (x, 0));
  112. print_path (path);
  113. printf (");\n");
  114. break;
  115. case MATCH_DUP:
  116. printf (" recog_dup_loc[%d] = &", dup_count);
  117. print_path (path);
  118. printf (";\n");
  119. printf (" recog_dup_num[%d] = %d;\n", dup_count, XINT (x, 0));
  120. dup_count++;
  121. break;
  122. case ADDRESS:
  123. walk_rtx (XEXP (x, 0), path);
  124. return;
  125. }
  126. link.next = path;
  127. link.vecelt = -1;
  128. fmt = GET_RTX_FORMAT (code);
  129. len = GET_RTX_LENGTH (code);
  130. for (i = 0; i < len; i++)
  131. {
  132. link.pos = i;
  133. if (fmt[i] == 'e' || fmt[i] == 'u')
  134. {
  135. walk_rtx (XEXP (x, i), &link);
  136. }
  137. else if (fmt[i] == 'E')
  138. {
  139. int j;
  140. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  141. {
  142. link.vecelt = j;
  143. walk_rtx (XVECEXP (x, i, j), &link);
  144. }
  145. }
  146. }
  147. }
  148. /* Given a PATH, representing a path down the instruction's
  149. pattern from the root to a certain point, output code to
  150. evaluate to the rtx at that point. */
  151. void
  152. print_path (path)
  153. struct link *path;
  154. {
  155. if (path == 0)
  156. printf ("insn");
  157. else if (path->vecelt >= 0)
  158. {
  159. printf ("XVECEXP (");
  160. print_path (path->next);
  161. printf (", %d, %d)", path->pos, path->vecelt);
  162. }
  163. else
  164. {
  165. printf ("XEXP (");
  166. print_path (path->next);
  167. printf (", %d)", path->pos);
  168. }
  169. }
  170. int
  171. xmalloc (size)
  172. {
  173. register int val = malloc (size);
  174. if (val == 0)
  175. fatal ("virtual memory exhausted");
  176. return val;
  177. }
  178. int
  179. xrealloc (ptr, size)
  180. char *ptr;
  181. int size;
  182. {
  183. int result = realloc (ptr, size);
  184. if (!result)
  185. fatal ("virtual memory exhausted");
  186. return result;
  187. }
  188. void
  189. fatal (s, a1, a2)
  190. {
  191. fprintf (stderr, "genextract: ");
  192. fprintf (stderr, s, a1, a2);
  193. fprintf (stderr, "\n");
  194. exit (FATAL_EXIT_CODE);
  195. }
  196. int
  197. main (argc, argv)
  198. int argc;
  199. char **argv;
  200. {
  201. rtx desc;
  202. FILE *infile;
  203. extern rtx read_rtx ();
  204. register int c, i;
  205. obstack_init (rtl_obstack);
  206. if (argc <= 1)
  207. fatal ("No input file name.");
  208. infile = fopen (argv[1], "r");
  209. if (infile == 0)
  210. {
  211. perror (argv[1]);
  212. exit (FATAL_EXIT_CODE);
  213. }
  214. init_rtl ();
  215. /* Assign sequential codes to all entries in the machine description
  216. in parallel with the tables in insn-output.c. */
  217. insn_code_number = 0;
  218. printf ("/* Generated automatically by the program `genextract'\n\
  219. from the machine description file `md'. */\n\n");
  220. printf ("#include \"config.h\"\n");
  221. printf ("#include \"rtl.h\"\n\n");
  222. printf ("extern rtx recog_operand[];\n");
  223. printf ("extern rtx *recog_operand_loc[];\n");
  224. printf ("extern rtx *recog_dup_loc[];\n");
  225. printf ("extern char recog_dup_num[];\n\n");
  226. /* The extractor functions really should return `void';
  227. but old C compilers don't seem to be able to handle the array
  228. definition if `void' is used. So use `int' in non-ANSI C compilers. */
  229. printf ("#ifdef __STDC__\n#define VOID void\n#else\n#define VOID int\n#endif\n\n");
  230. /* Read the machine description. */
  231. while (1)
  232. {
  233. c = read_skip_spaces (infile);
  234. if (c == EOF)
  235. break;
  236. ungetc (c, infile);
  237. desc = read_rtx (infile);
  238. if (GET_CODE (desc) == DEFINE_INSN)
  239. {
  240. gen_insn (desc);
  241. ++insn_code_number;
  242. }
  243. if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  244. {
  245. gen_peephole (desc);
  246. ++insn_code_number;
  247. }
  248. if (GET_CODE (desc) == DEFINE_EXPAND)
  249. {
  250. printf ("VOID extract_%d () {}\n\n", insn_code_number);
  251. ++insn_code_number;
  252. }
  253. }
  254. printf ("VOID (*insn_extract_fn[]) () =\n{ ");
  255. for (i = 0; i < insn_code_number; i++)
  256. {
  257. if (i % 4 != 0)
  258. printf (", ");
  259. else if (i != 0)
  260. printf (",\n ");
  261. printf ("extract_%d", i);
  262. }
  263. printf ("\n};\n\n");
  264. printf ("void\ninsn_extract (insn)\n");
  265. printf (" rtx insn;\n");
  266. printf ("{\n if (INSN_CODE (insn) == -1) abort ();\n");
  267. printf (" (*insn_extract_fn[INSN_CODE (insn)]) (PATTERN (insn));\n}\n");
  268. fflush (stdout);
  269. exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  270. }