genoutput.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Generate code from to output assembler insns as recognized from 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. /* This program reads the machine description for the compiler target machine
  18. and produces a file containing three things:
  19. 1, An array of strings `insn_template' which is indexed by insn code number
  20. and contains the template for output of that insn,
  21. 2. An array of ints `insn_n_operands' which is indexed by insn code number
  22. and contains the number of distinct operands in the pattern for that insn,
  23. 3. An array of ints `insn_n_dups' which is indexed by insn code number
  24. and contains the number of match_dup's that appear in the insn's pattern.
  25. This says how many elements of `recog_dup_loc' are significant
  26. after an insn has been recognized.
  27. 4. An array of arrays of machine modes, `insn_operand_machine_mode',
  28. indexed first by insn code number and second by operand number,
  29. containing the machine mode that that operand is supposed to have.
  30. 5. An array of arrays of operand constraint strings,
  31. `insn_operand_constraint',
  32. indexed first by insn code number and second by operand number,
  33. containing the constraint for that operand.
  34. This array is generated only if register constraints appear in
  35. match_operand rtx's.
  36. 6. An array of arrays of chars which indicate which operands of
  37. which insn patterns appear within ADDRESS rtx's. This array is
  38. called `insn_operand_address_p' and is generated only if there
  39. are *no* register constraints in the match_operand rtx's.
  40. 7. An array of functions `insn_gen_function' which, indexed
  41. by insn code number, gives the function to generate a body
  42. for that patter, given operands as arguments.
  43. 8. A function `output_insn_hairy' which is called with two arguments
  44. (an insn code number and a vector of operand value rtx's)
  45. and returns a template to use for output of that insn.
  46. This is used only in the cases where the template is not constant.
  47. These cases are specified by a * at the beginning of the template string
  48. in the machine description. They are identified for the sake of
  49. other parts of the compiler by a zero element in `insn_template'.
  50. The code number of an insn is simply its position in the machine description;
  51. code numbers are assigned sequentially to entries in the description,
  52. starting with code number 0.
  53. Thus, the following entry in the machine description
  54. (define_insn "clrdf"
  55. [(set (match_operand:DF 0 "general_operand" "")
  56. (const_int 0))]
  57. ""
  58. "clrd %0")
  59. assuming it is the 25th entry present, would cause
  60. insn_template[24] to be "c;rd %0", and insn_n_operands[24] to be 1.
  61. It would not make an case in output_insn_hairy because the template
  62. given in the entry is a constant (it does not start with `*'). */
  63. #include <stdio.h>
  64. #include "rtl.h"
  65. #include <obstack.h>
  66. /* No instruction can have more operands than this.
  67. Sorry for this arbitrary limit, but what machine will
  68. have an instruction with this many operands? */
  69. #define MAX_MAX_OPERANDS 40
  70. struct obstack obstack;
  71. struct obstack *current_obstack = &obstack;
  72. #define obstack_chunk_alloc xmalloc
  73. #define obstack_chunk_free free
  74. extern int xmalloc ();
  75. extern void free ();
  76. void fatal ();
  77. /* insns in the machine description are assigned sequential code numbers
  78. that are used by insn-recog.c (produced by genrecog) to communicate
  79. to insn-output.c (produced by this program). */
  80. int next_code_number;
  81. /* Record in this chain all information that we will output,
  82. associated with the code number of the insn. */
  83. struct data
  84. {
  85. int code_number;
  86. char *name;
  87. char *template; /* string such as "movl %1,%0" */
  88. int n_operands; /* Number of operands this insn recognizes */
  89. int n_dups; /* Number times match_dup appears in pattern */
  90. struct data *next;
  91. char *constraints[MAX_MAX_OPERANDS];
  92. char address_p[MAX_MAX_OPERANDS];
  93. enum machine_mode modes[MAX_MAX_OPERANDS];
  94. };
  95. /* This variable points to the first link in the chain. */
  96. struct data *insn_data;
  97. /* Pointer to the last link in the chain, so new elements
  98. can be added at the end. */
  99. struct data *end_of_insn_data;
  100. /* Nonzero if any match_operand has a constraint string;
  101. implies that REGISTER_CONSTRAINTS will be defined
  102. for this machine description. */
  103. int have_constraints;
  104. void
  105. output_prologue ()
  106. {
  107. printf ("/* Generated automatically by the program `genoutput'\n\
  108. from the machine description file `md'. */\n\n");
  109. printf ("#include \"config.h\"\n");
  110. printf ("#include \"rtl.h\"\n");
  111. printf ("#include \"regs.h\"\n");
  112. printf ("#include \"conditions.h\"\n");
  113. printf ("#include \"insn-flags.h\"\n");
  114. printf ("#include \"insn-config.h\"\n\n");
  115. printf ("extern rtx adj_offsetable_operand();\n\
  116. extern void output_asm_insn();\n\n");
  117. printf ("#include \"aux-output.c\"\n\n");
  118. printf ("char *\noutput_insn_hairy (code_number, operands, insn)\n");
  119. printf (" int code_number;\n");
  120. printf (" rtx *operands;\n");
  121. printf (" rtx insn;\n{\n switch (code_number)\n {\n");
  122. }
  123. void
  124. output_epilogue ()
  125. {
  126. register struct data *d;
  127. printf (" default: abort ();\n");
  128. printf (" }\n}\n");
  129. printf ("\nchar *insn_template[] =\n {\n");
  130. for (d = insn_data; d; d = d->next)
  131. {
  132. if (d->template)
  133. printf (" \"%s\",\n", d->template);
  134. else
  135. printf (" 0,\n");
  136. }
  137. printf (" };\n");
  138. printf ("\nrtx (*insn_gen_function[]) () =\n {\n");
  139. for (d = insn_data; d; d = d->next)
  140. {
  141. if (d->name)
  142. printf (" gen_%s,\n", d->name);
  143. else
  144. printf (" 0,\n");
  145. }
  146. printf (" };\n");
  147. printf ("\nint insn_n_operands[] =\n {\n");
  148. for (d = insn_data; d; d = d->next)
  149. {
  150. printf (" %d,\n", d->n_operands);
  151. }
  152. printf (" };\n");
  153. printf ("\nint insn_n_dups[] =\n {\n");
  154. for (d = insn_data; d; d = d->next)
  155. {
  156. printf (" %d,\n", d->n_dups);
  157. }
  158. printf (" };\n");
  159. if (have_constraints)
  160. {
  161. printf ("\nchar *insn_operand_constraint[][MAX_RECOG_OPERANDS] =\n {\n");
  162. for (d = insn_data; d; d = d->next)
  163. {
  164. register int i;
  165. printf (" {");
  166. for (i = 0; i < d->n_operands; i++)
  167. printf (" \"%s\",", d->constraints[i]);
  168. if (d->n_operands == 0)
  169. printf (" 0");
  170. printf (" },\n");
  171. }
  172. printf (" };\n");
  173. }
  174. else
  175. {
  176. printf ("\nchar insn_operand_address_p[][MAX_RECOG_OPERANDS] =\n {\n");
  177. for (d = insn_data; d; d = d->next)
  178. {
  179. register int i;
  180. printf (" {");
  181. for (i = 0; i < d->n_operands; i++)
  182. printf (" %d,", d->address_p[i]);
  183. if (d->n_operands == 0)
  184. printf (" 0");
  185. printf (" },\n");
  186. }
  187. printf (" };\n");
  188. }
  189. printf ("\nenum machine_mode insn_operand_mode[][MAX_RECOG_OPERANDS] =\n {\n");
  190. for (d = insn_data; d; d = d->next)
  191. {
  192. register int i;
  193. printf (" {");
  194. for (i = 0; i < d->n_operands; i++)
  195. printf (" %smode,", GET_MODE_NAME (d->modes[i]));
  196. if (d->n_operands == 0)
  197. printf (" VOIDmode");
  198. printf (" },\n");
  199. }
  200. printf (" };\n");
  201. }
  202. /* scan_operands (X) stores in max_opno the largest operand
  203. number present in X, if that is larger than the previous
  204. value of max_opno. It stores all the constraints in `constraints'
  205. and all the machine modes in `modes'. */
  206. int max_opno;
  207. int num_dups;
  208. char *constraints[MAX_MAX_OPERANDS];
  209. char address_p[MAX_MAX_OPERANDS];
  210. enum machine_mode modes[MAX_MAX_OPERANDS];
  211. void
  212. scan_operands (part, this_address_p)
  213. rtx part;
  214. int this_address_p;
  215. {
  216. register int i, j;
  217. register RTX_CODE code = GET_CODE (part);
  218. register char *format_ptr;
  219. if (code == MATCH_OPERAND)
  220. {
  221. if (XINT (part, 0) > max_opno)
  222. max_opno = XINT (part, 0);
  223. if (max_opno > MAX_MAX_OPERANDS)
  224. fatal ("Too many operands (%d) in one instruction pattern.\n",
  225. max_opno + 1);
  226. modes[max_opno] = GET_MODE (part);
  227. constraints[max_opno] = XSTR (part, 2);
  228. if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
  229. have_constraints = 1;
  230. address_p[max_opno] = this_address_p;
  231. return;
  232. }
  233. if (code == MATCH_DUP)
  234. {
  235. ++num_dups;
  236. return;
  237. }
  238. if (code == ADDRESS)
  239. {
  240. scan_operands (XEXP (part, 0), 1);
  241. return;
  242. }
  243. format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  244. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  245. switch (*format_ptr++)
  246. {
  247. case 'e':
  248. scan_operands (XEXP (part, i), 0);
  249. break;
  250. case 'E':
  251. if (XVEC (part, i) != NULL)
  252. for (j = 0; j < XVECLEN (part, i); j++)
  253. scan_operands (XVECEXP (part, i, j), 0);
  254. break;
  255. }
  256. }
  257. /* Read the next insn. Assign its code number.
  258. Record on insn_data the template and the number of arguments.
  259. If the insn has a hairy output action, output it now. */
  260. void
  261. gen_insn (insn)
  262. rtx insn;
  263. {
  264. register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  265. register int i;
  266. d->code_number = next_code_number++;
  267. if (XSTR (insn, 0)[0])
  268. d->name = XSTR (insn, 0);
  269. else
  270. d->name = 0;
  271. /* Build up the list in the same order as the insns are seen
  272. in the machine description. */
  273. d->next = 0;
  274. if (end_of_insn_data)
  275. end_of_insn_data->next = d;
  276. else
  277. insn_data = d;
  278. end_of_insn_data = d;
  279. max_opno = -1;
  280. num_dups = 0;
  281. bzero (constraints, sizeof constraints);
  282. bzero (address_p, sizeof address_p);
  283. bzero (modes, sizeof modes);
  284. for (i = 0; i < XVECLEN (insn, 1); i++)
  285. scan_operands (XVECEXP (insn, 1, i), 0);
  286. d->n_operands = max_opno + 1;
  287. d->n_dups = num_dups;
  288. bcopy (constraints, d->constraints, sizeof constraints);
  289. bcopy (address_p, d->address_p, sizeof address_p);
  290. bcopy (modes, d->modes, sizeof modes);
  291. /* We need to consider only the instructions whose assembler code template
  292. starts with a *. These are the ones where the template is really
  293. C code to run to decide on a template to use.
  294. So for all others just return now. */
  295. if (XSTR (insn, 3)[0] != '*')
  296. {
  297. d->template = XSTR (insn, 3);
  298. return;
  299. }
  300. d->template = 0;
  301. printf (" case %d:\n", d->code_number);
  302. printf ("%s\n", &(XSTR (insn, 3)[1]));
  303. }
  304. xmalloc (size)
  305. {
  306. register int val = malloc (size);
  307. if (val == 0)
  308. abort ();
  309. return val;
  310. }
  311. int
  312. xrealloc (ptr, size)
  313. char *ptr;
  314. int size;
  315. {
  316. int result = realloc (ptr, size);
  317. if (!result)
  318. abort ();
  319. return result;
  320. }
  321. void
  322. fatal (s, a1, a2)
  323. {
  324. fprintf (stderr, "genemit: ");
  325. fprintf (stderr, s, a1, a2);
  326. fprintf (stderr, "\n");
  327. exit (1);
  328. }
  329. main (argc, argv)
  330. int argc;
  331. char **argv;
  332. {
  333. rtx desc;
  334. FILE *infile;
  335. extern rtx read_rtx ();
  336. register int c;
  337. obstack_init (current_obstack);
  338. if (argc <= 1)
  339. fatal ("No input file name.");
  340. infile = fopen (argv[1], "r");
  341. if (infile == 0)
  342. {
  343. perror (argv[1]);
  344. exit (1);
  345. }
  346. init_rtl ();
  347. output_prologue ();
  348. next_code_number = 0;
  349. have_constraints = 0;
  350. /* Read the machine description. */
  351. while (1)
  352. {
  353. c = read_skip_spaces (infile);
  354. if (c == EOF)
  355. break;
  356. ungetc (c, infile);
  357. desc = read_rtx (infile);
  358. gen_insn (desc);
  359. }
  360. output_epilogue ();
  361. return 0;
  362. }