genoutput.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 operand constraint strings,
  28. `insn_operand_constraint',
  29. indexed first by insn code number and second by operand number,
  30. containing the constraint for that operand.
  31. This array is generated only if register constraints appear in
  32. match_operand rtx's.
  33. 5. An array of arrays of chars which indicate which operands of
  34. which insn patterns appear within ADDRESS rtx's. This array is
  35. called `insn_operand_address_p' and is generated only if there
  36. are *no* register constraints in the match_operand rtx's.
  37. 6. An array of arrays of machine modes, `insn_operand_mode',
  38. indexed first by insn code number and second by operand number,
  39. containing the machine mode that that operand is supposed to have.
  40. Also `insn_operand_strict_low', which is nonzero for operands
  41. contained in a STRICT_LOW_PART.
  42. 7. An array of arrays of int-valued functions, `insn_operand_predicate',
  43. indexed first by insn code number and second by operand number,
  44. containing the match_operand predicate for this operand.
  45. 8. An array of functions `insn_gen_function' which, indexed
  46. by insn code number, gives the function to generate a body
  47. for that patter, given operands as arguments.
  48. 9. A function `output_insn_hairy' which is called with two arguments
  49. (an insn code number and a vector of operand value rtx's)
  50. and returns a template to use for output of that insn.
  51. This is used only in the cases where the template is not constant.
  52. These cases are specified by a * at the beginning of the template string
  53. in the machine description. They are identified for the sake of
  54. other parts of the compiler by a zero element in `insn_template'.
  55. The code number of an insn is simply its position in the machine description;
  56. code numbers are assigned sequentially to entries in the description,
  57. starting with code number 0.
  58. Thus, the following entry in the machine description
  59. (define_insn "clrdf"
  60. [(set (match_operand:DF 0 "general_operand" "")
  61. (const_int 0))]
  62. ""
  63. "clrd %0")
  64. assuming it is the 25th entry present, would cause
  65. insn_template[24] to be "clrd %0", and insn_n_operands[24] to be 1.
  66. It would not make an case in output_insn_hairy because the template
  67. given in the entry is a constant (it does not start with `*'). */
  68. #include <stdio.h>
  69. #include "config.h"
  70. #include "rtl.h"
  71. #include "obstack.h"
  72. /* No instruction can have more operands than this.
  73. Sorry for this arbitrary limit, but what machine will
  74. have an instruction with this many operands? */
  75. #define MAX_MAX_OPERANDS 40
  76. struct obstack obstack;
  77. struct obstack *rtl_obstack = &obstack;
  78. #define obstack_chunk_alloc xmalloc
  79. #define obstack_chunk_free free
  80. extern int xmalloc ();
  81. extern void free ();
  82. void fatal ();
  83. void mybcopy ();
  84. void mybzero ();
  85. /* insns in the machine description are assigned sequential code numbers
  86. that are used by insn-recog.c (produced by genrecog) to communicate
  87. to insn-output.c (produced by this program). */
  88. int next_code_number;
  89. /* Record in this chain all information that we will output,
  90. associated with the code number of the insn. */
  91. struct data
  92. {
  93. int code_number;
  94. char *name;
  95. char *template; /* string such as "movl %1,%0" */
  96. int n_operands; /* Number of operands this insn recognizes */
  97. int n_dups; /* Number times match_dup appears in pattern */
  98. struct data *next;
  99. char *constraints[MAX_MAX_OPERANDS];
  100. char *predicates[MAX_MAX_OPERANDS];
  101. char address_p[MAX_MAX_OPERANDS];
  102. enum machine_mode modes[MAX_MAX_OPERANDS];
  103. char strict_low[MAX_MAX_OPERANDS];
  104. char outfun; /* Nonzero means this has an output function */
  105. };
  106. /* This variable points to the first link in the chain. */
  107. struct data *insn_data;
  108. /* Pointer to the last link in the chain, so new elements
  109. can be added at the end. */
  110. struct data *end_of_insn_data;
  111. /* Nonzero if any match_operand has a constraint string;
  112. implies that REGISTER_CONSTRAINTS will be defined
  113. for this machine description. */
  114. int have_constraints;
  115. void
  116. output_prologue ()
  117. {
  118. printf ("/* Generated automatically by the program `genoutput'\n\
  119. from the machine description file `md'. */\n\n");
  120. printf ("#include \"config.h\"\n");
  121. printf ("#include \"rtl.h\"\n");
  122. printf ("#include \"regs.h\"\n");
  123. printf ("#include \"conditions.h\"\n");
  124. printf ("#include \"insn-flags.h\"\n");
  125. printf ("#include \"insn-config.h\"\n\n");
  126. printf ("#include \"output.h\"\n");
  127. printf ("#include \"aux-output.c\"\n\n");
  128. }
  129. void
  130. output_epilogue ()
  131. {
  132. register struct data *d;
  133. printf ("\nchar *insn_template[] =\n {\n");
  134. for (d = insn_data; d; d = d->next)
  135. {
  136. if (d->template)
  137. printf (" \"%s\",\n", d->template);
  138. else
  139. printf (" 0,\n");
  140. }
  141. printf (" };\n");
  142. printf ("\nchar *(*insn_outfun[])() =\n {\n");
  143. for (d = insn_data; d; d = d->next)
  144. {
  145. if (d->outfun)
  146. printf (" output_%d,", d->code_number);
  147. else
  148. printf (" 0,\n");
  149. }
  150. printf (" };\n");
  151. printf ("\nrtx (*insn_gen_function[]) () =\n {\n");
  152. for (d = insn_data; d; d = d->next)
  153. {
  154. if (d->name)
  155. printf (" gen_%s,\n", d->name);
  156. else
  157. printf (" 0,\n");
  158. }
  159. printf (" };\n");
  160. printf ("\nint insn_n_operands[] =\n {\n");
  161. for (d = insn_data; d; d = d->next)
  162. {
  163. printf (" %d,\n", d->n_operands);
  164. }
  165. printf (" };\n");
  166. printf ("\nint insn_n_dups[] =\n {\n");
  167. for (d = insn_data; d; d = d->next)
  168. {
  169. printf (" %d,\n", d->n_dups);
  170. }
  171. printf (" };\n");
  172. if (have_constraints)
  173. {
  174. printf ("\nchar *insn_operand_constraint[][MAX_RECOG_OPERANDS] =\n {\n");
  175. for (d = insn_data; d; d = d->next)
  176. {
  177. register int i;
  178. printf (" {");
  179. for (i = 0; i < d->n_operands; i++)
  180. {
  181. if (d->constraints[i] == 0)
  182. printf (" \"\",");
  183. else
  184. printf (" \"%s\",", d->constraints[i]);
  185. }
  186. if (d->n_operands == 0)
  187. printf (" 0");
  188. printf (" },\n");
  189. }
  190. printf (" };\n");
  191. }
  192. else
  193. {
  194. printf ("\nchar insn_operand_address_p[][MAX_RECOG_OPERANDS] =\n {\n");
  195. for (d = insn_data; d; d = d->next)
  196. {
  197. register int i;
  198. printf (" {");
  199. for (i = 0; i < d->n_operands; i++)
  200. printf (" %d,", d->address_p[i]);
  201. if (d->n_operands == 0)
  202. printf (" 0");
  203. printf (" },\n");
  204. }
  205. printf (" };\n");
  206. }
  207. printf ("\nenum machine_mode insn_operand_mode[][MAX_RECOG_OPERANDS] =\n {\n");
  208. for (d = insn_data; d; d = d->next)
  209. {
  210. register int i;
  211. printf (" {");
  212. for (i = 0; i < d->n_operands; i++)
  213. printf (" %smode,", GET_MODE_NAME (d->modes[i]));
  214. if (d->n_operands == 0)
  215. printf (" VOIDmode");
  216. printf (" },\n");
  217. }
  218. printf (" };\n");
  219. printf ("\nchar insn_operand_strict_low[][MAX_RECOG_OPERANDS] =\n {\n");
  220. for (d = insn_data; d; d = d->next)
  221. {
  222. register int i;
  223. printf (" {");
  224. for (i = 0; i < d->n_operands; i++)
  225. printf (" %d,", d->strict_low[i]);
  226. if (d->n_operands == 0)
  227. printf (" 0");
  228. printf (" },\n");
  229. }
  230. printf (" };\n");
  231. printf ("\nint (*insn_operand_predicate[][MAX_RECOG_OPERANDS])() =\n {\n");
  232. for (d = insn_data; d; d = d->next)
  233. {
  234. register int i;
  235. printf (" {");
  236. for (i = 0; i < d->n_operands; i++)
  237. printf (" %s,", ((d->predicates[i] && d->predicates[i][0])
  238. ? d->predicates[i] : "0"));
  239. if (d->n_operands == 0)
  240. printf (" 0");
  241. printf (" },\n");
  242. }
  243. printf (" };\n");
  244. }
  245. /* scan_operands (X) stores in max_opno the largest operand
  246. number present in X, if that is larger than the previous
  247. value of max_opno. It stores all the constraints in `constraints'
  248. and all the machine modes in `modes'.
  249. THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
  250. THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
  251. int max_opno;
  252. int num_dups;
  253. char *constraints[MAX_MAX_OPERANDS];
  254. char *predicates[MAX_MAX_OPERANDS];
  255. char address_p[MAX_MAX_OPERANDS];
  256. enum machine_mode modes[MAX_MAX_OPERANDS];
  257. char strict_low[MAX_MAX_OPERANDS];
  258. void
  259. scan_operands (part, this_address_p, this_strict_low)
  260. rtx part;
  261. int this_address_p;
  262. int this_strict_low;
  263. {
  264. register int i, j;
  265. register RTX_CODE code;
  266. register char *format_ptr;
  267. if (part == 0)
  268. return;
  269. code = GET_CODE (part);
  270. if (code == MATCH_OPERAND)
  271. {
  272. int opno = XINT (part, 0);
  273. if (opno > max_opno)
  274. max_opno = opno;
  275. if (max_opno >= MAX_MAX_OPERANDS)
  276. fatal ("Too many operands (%d) in one instruction pattern.\n",
  277. max_opno + 1);
  278. modes[opno] = GET_MODE (part);
  279. strict_low[opno] = this_strict_low;
  280. predicates[opno] = XSTR (part, 1);
  281. constraints[opno] = XSTR (part, 2);
  282. if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
  283. have_constraints = 1;
  284. address_p[opno] = this_address_p;
  285. return;
  286. }
  287. if (code == MATCH_DUP)
  288. {
  289. ++num_dups;
  290. return;
  291. }
  292. if (code == ADDRESS)
  293. {
  294. scan_operands (XEXP (part, 0), 1, 0);
  295. return;
  296. }
  297. if (code == STRICT_LOW_PART)
  298. {
  299. scan_operands (XEXP (part, 0), 0, 1);
  300. return;
  301. }
  302. format_ptr = GET_RTX_FORMAT (GET_CODE (part));
  303. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
  304. switch (*format_ptr++)
  305. {
  306. case 'e':
  307. scan_operands (XEXP (part, i), 0, 0);
  308. break;
  309. case 'E':
  310. if (XVEC (part, i) != NULL)
  311. for (j = 0; j < XVECLEN (part, i); j++)
  312. scan_operands (XVECEXP (part, i, j), 0, 0);
  313. break;
  314. }
  315. }
  316. /* Look at a define_insn just read. Assign its code number.
  317. Record on insn_data the template and the number of arguments.
  318. If the insn has a hairy output action, output a function for now. */
  319. void
  320. gen_insn (insn)
  321. rtx insn;
  322. {
  323. register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  324. register int i;
  325. d->code_number = next_code_number++;
  326. if (XSTR (insn, 0)[0])
  327. d->name = XSTR (insn, 0);
  328. else
  329. d->name = 0;
  330. /* Build up the list in the same order as the insns are seen
  331. in the machine description. */
  332. d->next = 0;
  333. if (end_of_insn_data)
  334. end_of_insn_data->next = d;
  335. else
  336. insn_data = d;
  337. end_of_insn_data = d;
  338. max_opno = -1;
  339. num_dups = 0;
  340. mybzero (constraints, sizeof constraints);
  341. mybzero (predicates, sizeof predicates);
  342. mybzero (address_p, sizeof address_p);
  343. mybzero (modes, sizeof modes);
  344. mybzero (strict_low, sizeof strict_low);
  345. for (i = 0; i < XVECLEN (insn, 1); i++)
  346. scan_operands (XVECEXP (insn, 1, i), 0, 0);
  347. d->n_operands = max_opno + 1;
  348. d->n_dups = num_dups;
  349. mybcopy (constraints, d->constraints, sizeof constraints);
  350. mybcopy (predicates, d->predicates, sizeof predicates);
  351. mybcopy (address_p, d->address_p, sizeof address_p);
  352. mybcopy (modes, d->modes, sizeof modes);
  353. mybcopy (strict_low, d->strict_low, sizeof strict_low);
  354. /* We need to consider only the instructions whose assembler code template
  355. starts with a *. These are the ones where the template is really
  356. C code to run to decide on a template to use.
  357. So for all others just return now. */
  358. if (XSTR (insn, 3)[0] != '*')
  359. {
  360. d->template = XSTR (insn, 3);
  361. d->outfun = 0;
  362. return;
  363. }
  364. d->template = 0;
  365. d->outfun = 1;
  366. printf ("\nchar *\n");
  367. printf ("output_%d (operands, insn)\n", d->code_number);
  368. printf (" rtx *operands;\n");
  369. printf (" rtx insn;\n");
  370. printf ("{\n");
  371. /* The following is done in a funny way to get around problems in
  372. VAX-11 "C" on VMS. It is the equivalent of:
  373. printf ("%s\n", &(XSTR (insn, 3)[1])); */
  374. {
  375. register char *cp = &(XSTR (insn, 3)[1]);
  376. while (*cp) putchar (*cp++);
  377. putchar ('\n');
  378. }
  379. printf ("}\n");
  380. }
  381. /* Look at a define_peephole just read. Assign its code number.
  382. Record on insn_data the template and the number of arguments.
  383. If the insn has a hairy output action, output it now. */
  384. void
  385. gen_peephole (peep)
  386. rtx peep;
  387. {
  388. register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  389. register int i;
  390. d->code_number = next_code_number++;
  391. d->name = 0;
  392. /* Build up the list in the same order as the insns are seen
  393. in the machine description. */
  394. d->next = 0;
  395. if (end_of_insn_data)
  396. end_of_insn_data->next = d;
  397. else
  398. insn_data = d;
  399. end_of_insn_data = d;
  400. max_opno = -1;
  401. mybzero (constraints, sizeof constraints);
  402. /* Get the number of operands by scanning all the
  403. patterns of the peephole optimizer.
  404. But ignore all the rest of the information thus obtained. */
  405. for (i = 0; i < XVECLEN (peep, 0); i++)
  406. scan_operands (XVECEXP (peep, 0, i), 0, 0);
  407. d->n_operands = max_opno + 1;
  408. d->n_dups = 0;
  409. mybcopy (constraints, d->constraints, sizeof constraints);
  410. mybzero (d->predicates, sizeof predicates);
  411. mybzero (d->address_p, sizeof address_p);
  412. mybzero (d->modes, sizeof modes);
  413. mybzero (d->strict_low, sizeof strict_low);
  414. /* We need to consider only the instructions whose assembler code template
  415. starts with a *. These are the ones where the template is really
  416. C code to run to decide on a template to use.
  417. So for all others just return now. */
  418. if (XSTR (peep, 2)[0] != '*')
  419. {
  420. d->template = XSTR (peep, 2);
  421. d->outfun = 0;
  422. return;
  423. }
  424. d->template = 0;
  425. d->outfun = 1;
  426. printf ("\nchar *\n");
  427. printf ("output_%d (operands, insn)\n", d->code_number);
  428. printf (" rtx *operands;\n");
  429. printf (" rtx insn;\n");
  430. printf ("{\n");
  431. printf ("%s\n", &(XSTR (peep, 2)[1]));
  432. printf ("}\n");
  433. }
  434. /* Process a define_expand just read. Assign its code number,
  435. only for the purposes of `insn_gen_function'. */
  436. void
  437. gen_expand (insn)
  438. rtx insn;
  439. {
  440. register struct data *d = (struct data *) xmalloc (sizeof (struct data));
  441. register int i;
  442. d->code_number = next_code_number++;
  443. if (XSTR (insn, 0)[0])
  444. d->name = XSTR (insn, 0);
  445. else
  446. d->name = 0;
  447. /* Build up the list in the same order as the insns are seen
  448. in the machine description. */
  449. d->next = 0;
  450. if (end_of_insn_data)
  451. end_of_insn_data->next = d;
  452. else
  453. insn_data = d;
  454. end_of_insn_data = d;
  455. max_opno = -1;
  456. num_dups = 0;
  457. /* Scan the operands to get the specified predicates and modes,
  458. since expand_binop needs to know them. */
  459. mybzero (predicates, sizeof predicates);
  460. mybzero (modes, sizeof modes);
  461. if (XVEC (insn, 1))
  462. for (i = 0; i < XVECLEN (insn, 1); i++)
  463. scan_operands (XVECEXP (insn, 1, i), 0, 0);
  464. d->n_operands = max_opno + 1;
  465. mybcopy (predicates, d->predicates, sizeof predicates);
  466. mybcopy (modes, d->modes, sizeof modes);
  467. mybzero (d->constraints, sizeof constraints);
  468. mybzero (d->address_p, sizeof address_p);
  469. mybzero (d->strict_low, sizeof strict_low);
  470. d->n_dups = 0;
  471. d->template = 0;
  472. d->outfun = 0;
  473. }
  474. int
  475. xmalloc (size)
  476. {
  477. register int val = malloc (size);
  478. if (val == 0)
  479. fatal ("virtual memory exhausted");
  480. return val;
  481. }
  482. int
  483. xrealloc (ptr, size)
  484. char *ptr;
  485. int size;
  486. {
  487. int result = realloc (ptr, size);
  488. if (!result)
  489. fatal ("virtual memory exhausted");
  490. return result;
  491. }
  492. void
  493. mybzero (b, length)
  494. register char *b;
  495. register int length;
  496. {
  497. while (length-- > 0)
  498. *b++ = 0;
  499. }
  500. void
  501. mybcopy (b1, b2, length)
  502. register char *b1;
  503. register char *b2;
  504. register int length;
  505. {
  506. while (length-- > 0)
  507. *b2++ = *b1++;
  508. }
  509. void
  510. fatal (s, a1, a2)
  511. {
  512. fprintf (stderr, "genoutput: ");
  513. fprintf (stderr, s, a1, a2);
  514. fprintf (stderr, "\n");
  515. exit (FATAL_EXIT_CODE);
  516. }
  517. int
  518. main (argc, argv)
  519. int argc;
  520. char **argv;
  521. {
  522. rtx desc;
  523. FILE *infile;
  524. extern rtx read_rtx ();
  525. register int c;
  526. obstack_init (rtl_obstack);
  527. if (argc <= 1)
  528. fatal ("No input file name.");
  529. infile = fopen (argv[1], "r");
  530. if (infile == 0)
  531. {
  532. perror (argv[1]);
  533. exit (FATAL_EXIT_CODE);
  534. }
  535. init_rtl ();
  536. output_prologue ();
  537. next_code_number = 0;
  538. have_constraints = 0;
  539. /* Read the machine description. */
  540. while (1)
  541. {
  542. c = read_skip_spaces (infile);
  543. if (c == EOF)
  544. break;
  545. ungetc (c, infile);
  546. desc = read_rtx (infile);
  547. if (GET_CODE (desc) == DEFINE_INSN)
  548. gen_insn (desc);
  549. if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  550. gen_peephole (desc);
  551. if (GET_CODE (desc) == DEFINE_EXPAND)
  552. gen_expand (desc);
  553. }
  554. output_epilogue ();
  555. fflush (stdout);
  556. exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  557. }