1 /* Support routines for the various generation passes.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "gensupport.h"
29 /* In case some macros used by files we include need it, define this here. */
32 static struct obstack obstack;
33 struct obstack *rtl_obstack = &obstack;
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 static int sequence_num;
41 static int predicable_default;
42 static const char *predicable_true;
43 static const char *predicable_false;
45 static char *base_dir = NULL;
47 /* We initially queue all patterns, process the define_insn and
48 define_cond_exec patterns, then return them one at a time. */
54 struct queue_elem *next;
57 static struct queue_elem *define_attr_queue;
58 static struct queue_elem **define_attr_tail = &define_attr_queue;
59 static struct queue_elem *define_insn_queue;
60 static struct queue_elem **define_insn_tail = &define_insn_queue;
61 static struct queue_elem *define_cond_exec_queue;
62 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
63 static struct queue_elem *other_queue;
64 static struct queue_elem **other_tail = &other_queue;
66 static void queue_pattern PARAMS ((rtx, struct queue_elem ***, int));
68 /* Current maximum length of directory names in the search path
69 for include files. (Altered as we get more of them.) */
71 size_t max_include_len;
75 struct file_name_list *next;
79 struct file_name_list *include = 0; /* First dir to search */
80 /* First dir to search for <file> */
81 struct file_name_list *first_bracket_include = 0;
82 struct file_name_list *last_include = 0; /* Last in chain */
84 static void remove_constraints PARAMS ((rtx));
85 static void process_rtx PARAMS ((rtx, int));
87 static int is_predicable PARAMS ((struct queue_elem *));
88 static void identify_predicable_attribute PARAMS ((void));
89 static int n_alternatives PARAMS ((const char *));
90 static void collect_insn_data PARAMS ((rtx, int *, int *));
91 static rtx alter_predicate_for_insn PARAMS ((rtx, int, int, int));
92 static const char *alter_test_for_insn PARAMS ((struct queue_elem *,
93 struct queue_elem *));
94 static char *shift_output_template PARAMS ((char *, const char *, int));
95 static const char *alter_output_for_insn PARAMS ((struct queue_elem *,
98 static void process_one_cond_exec PARAMS ((struct queue_elem *));
99 static void process_define_cond_exec PARAMS ((void));
100 static int process_include PARAMS ((rtx, int));
101 static char *save_string PARAMS ((const char *, int));
102 static int init_include_reader PARAMS ((FILE *));
105 message_with_line VPARAMS ((int lineno, const char *msg, ...))
108 VA_FIXEDARG (ap, int, lineno);
109 VA_FIXEDARG (ap, const char *, msg);
111 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
112 vfprintf (stderr, msg, ap);
113 fputc ('\n', stderr);
118 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
119 the gensupport programs. */
122 gen_rtx_CONST_INT (mode, arg)
123 enum machine_mode mode ATTRIBUTE_UNUSED;
126 rtx rt = rtx_alloc (CONST_INT);
132 /* Queue PATTERN on LIST_TAIL. */
135 queue_pattern (pattern, list_tail, lineno)
137 struct queue_elem ***list_tail;
140 struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e));
145 *list_tail = &e->next;
148 /* Recursively remove constraints from an rtx. */
151 remove_constraints (part)
155 const char *format_ptr;
160 if (GET_CODE (part) == MATCH_OPERAND)
162 else if (GET_CODE (part) == MATCH_SCRATCH)
165 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
167 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
168 switch (*format_ptr++)
172 remove_constraints (XEXP (part, i));
175 if (XVEC (part, i) != NULL)
176 for (j = 0; j < XVECLEN (part, i); j++)
177 remove_constraints (XVECEXP (part, i, j));
182 /* The entry point for initializing the reader. */
185 init_include_reader (inf)
192 /* Read the entire file. */
198 c = read_skip_spaces (inf);
203 lineno = read_rtx_lineno;
204 desc = read_rtx (inf);
205 process_rtx (desc, lineno);
209 /* Process define_cond_exec patterns. */
210 if (define_cond_exec_queue != NULL)
211 process_define_cond_exec ();
213 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
216 /* Process an include file assuming that it lives in gcc/config/{target}/
217 if the include looks line (include "file" ) */
219 process_include (desc, lineno)
223 const char *filename = XSTR (desc, 0);
224 char *pathname = NULL;
227 struct file_name_list *stackp;
232 /* If specified file name is absolute, just open it. */
233 if (IS_ABSOLUTE_PATHNAME (filename) || !stackp)
237 pathname = xmalloc (strlen (base_dir) + strlen (filename) + 1);
238 pathname = strcpy (pathname, base_dir);
239 strcat (pathname, filename);
240 strcat (pathname, "\0");
244 pathname = xstrdup (filename);
246 read_rtx_filename = pathname;
247 input_file = fopen (pathname, "r");
252 return FATAL_EXIT_CODE;
258 flen = strlen (filename);
260 fname = (char *) alloca (max_include_len + flen + 2);
262 /* + 2 above for slash and terminating null. */
264 /* Search directory path, trying to open the file.
265 Copy each filename tried into FNAME. */
267 for (; stackp; stackp = stackp->next)
271 strcpy (fname, stackp->fname);
273 fname[strlen (fname) + flen] = 0;
279 strncat (fname, (const char *) filename, flen);
280 read_rtx_filename = fname;
281 input_file = fopen (fname, "r");
282 if (input_file != NULL)
287 if (strchr (fname, '/') == NULL || strchr (fname, '\\' ) || base_dir)
292 xmalloc (strlen (base_dir) + strlen (filename) + 1);
293 pathname = strcpy (pathname, base_dir);
294 strcat (pathname, filename);
295 strcat (pathname, "\0");
298 pathname = xstrdup (filename);
300 read_rtx_filename = pathname;
301 input_file = fopen (pathname, "r");
306 return FATAL_EXIT_CODE;
312 if (init_include_reader (input_file) == FATAL_EXIT_CODE)
313 message_with_line (lineno, "read errors found in include file %s\n", pathname);
315 return SUCCESS_EXIT_CODE;
318 /* Process a top level rtx in some way, queueing as appropriate. */
321 process_rtx (desc, lineno)
325 const char *filename = XSTR (desc, 0);
327 switch (GET_CODE (desc))
330 queue_pattern (desc, &define_insn_tail, lineno);
333 case DEFINE_COND_EXEC:
334 queue_pattern (desc, &define_cond_exec_tail, lineno);
338 queue_pattern (desc, &define_attr_tail, lineno);
342 if (process_include (desc, lineno) == FATAL_EXIT_CODE)
343 message_with_line (lineno, "include file at %s not found\n", filename);
346 case DEFINE_INSN_AND_SPLIT:
348 const char *split_cond;
353 /* Create a split with values from the insn_and_split. */
354 split = rtx_alloc (DEFINE_SPLIT);
356 i = XVECLEN (desc, 1);
357 XVEC (split, 0) = rtvec_alloc (i);
360 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
361 remove_constraints (XVECEXP (split, 0, i));
364 /* If the split condition starts with "&&", append it to the
365 insn condition to create the new split condition. */
366 split_cond = XSTR (desc, 4);
367 if (split_cond[0] == '&' && split_cond[1] == '&')
369 const char *insn_cond = XSTR (desc, 2);
370 size_t insn_cond_len = strlen (insn_cond);
371 size_t split_cond_len = strlen (split_cond);
374 combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1);
375 memcpy (combined, insn_cond, insn_cond_len);
376 memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1);
378 split_cond = combined;
380 XSTR (split, 1) = split_cond;
381 XVEC (split, 2) = XVEC (desc, 5);
382 XSTR (split, 3) = XSTR (desc, 6);
384 /* Fix up the DEFINE_INSN. */
385 attr = XVEC (desc, 7);
386 PUT_CODE (desc, DEFINE_INSN);
387 XVEC (desc, 4) = attr;
390 queue_pattern (desc, &define_insn_tail, lineno);
391 queue_pattern (split, &other_tail, lineno);
396 queue_pattern (desc, &other_tail, lineno);
401 /* Return true if attribute PREDICABLE is true for ELEM, which holds
406 struct queue_elem *elem;
408 rtvec vec = XVEC (elem->data, 4);
413 return predicable_default;
415 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
417 rtx sub = RTVEC_ELT (vec, i);
418 switch (GET_CODE (sub))
421 if (strcmp (XSTR (sub, 0), "predicable") == 0)
423 value = XSTR (sub, 1);
428 case SET_ATTR_ALTERNATIVE:
429 if (strcmp (XSTR (sub, 0), "predicable") == 0)
431 message_with_line (elem->lineno,
432 "multiple alternatives for `predicable'");
439 if (GET_CODE (SET_DEST (sub)) != ATTR
440 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
443 if (GET_CODE (sub) == CONST_STRING)
445 value = XSTR (sub, 0);
449 /* ??? It would be possible to handle this if we really tried.
450 It's not easy though, and I'm not going to bother until it
451 really proves necessary. */
452 message_with_line (elem->lineno,
453 "non-constant value for `predicable'");
462 return predicable_default;
465 /* Verify that predicability does not vary on the alternative. */
466 /* ??? It should be possible to handle this by simply eliminating
467 the non-predicable alternatives from the insn. FRV would like
468 to do this. Delay this until we've got the basics solid. */
469 if (strchr (value, ',') != NULL)
471 message_with_line (elem->lineno,
472 "multiple alternatives for `predicable'");
477 /* Find out which value we're looking at. */
478 if (strcmp (value, predicable_true) == 0)
480 if (strcmp (value, predicable_false) == 0)
483 message_with_line (elem->lineno,
484 "Unknown value `%s' for `predicable' attribute",
490 /* Examine the attribute "predicable"; discover its boolean values
494 identify_predicable_attribute ()
496 struct queue_elem *elem;
497 char *p_true, *p_false;
501 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
502 for (elem = define_attr_queue; elem ; elem = elem->next)
503 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
506 message_with_line (define_cond_exec_queue->lineno,
507 "Attribute `predicable' not defined");
512 value = XSTR (elem->data, 1);
513 len = strlen (value);
514 p_false = (char *) xmalloc (len + 1);
515 memcpy (p_false, value, len + 1);
517 p_true = strchr (p_false, ',');
518 if (p_true == NULL || strchr (++p_true, ',') != NULL)
520 message_with_line (elem->lineno,
521 "Attribute `predicable' is not a boolean");
527 predicable_true = p_true;
528 predicable_false = p_false;
530 switch (GET_CODE (XEXP (elem->data, 2)))
533 value = XSTR (XEXP (elem->data, 2), 0);
537 message_with_line (elem->lineno,
538 "Attribute `predicable' cannot be const");
543 message_with_line (elem->lineno,
544 "Attribute `predicable' must have a constant default");
549 if (strcmp (value, p_true) == 0)
550 predicable_default = 1;
551 else if (strcmp (value, p_false) == 0)
552 predicable_default = 0;
555 message_with_line (elem->lineno,
556 "Unknown value `%s' for `predicable' attribute",
562 /* Return the number of alternatives in constraint S. */
577 /* Determine how many alternatives there are in INSN, and how many
581 collect_insn_data (pattern, palt, pmax)
589 code = GET_CODE (pattern);
593 i = n_alternatives (XSTR (pattern, 2));
594 *palt = (i > *palt ? i : *palt);
601 i = XINT (pattern, 0);
610 fmt = GET_RTX_FORMAT (code);
611 len = GET_RTX_LENGTH (code);
612 for (i = 0; i < len; i++)
617 collect_insn_data (XEXP (pattern, i), palt, pmax);
621 if (XVEC (pattern, i) == NULL)
625 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
626 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
629 case 'i': case 'w': case '0': case 's': case 'S': case 'T':
639 alter_predicate_for_insn (pattern, alt, max_op, lineno)
641 int alt, max_op, lineno;
647 code = GET_CODE (pattern);
652 const char *c = XSTR (pattern, 2);
654 if (n_alternatives (c) != 1)
656 message_with_line (lineno,
657 "Too many alternatives for operand %d",
663 /* Replicate C as needed to fill out ALT alternatives. */
664 if (c && *c && alt > 1)
666 size_t c_len = strlen (c);
667 size_t len = alt * (c_len + 1);
668 char *new_c = (char *) xmalloc (len);
670 memcpy (new_c, c, c_len);
671 for (i = 1; i < alt; ++i)
673 new_c[i * (c_len + 1) - 1] = ',';
674 memcpy (&new_c[i * (c_len + 1)], c, c_len);
676 new_c[len - 1] = '\0';
677 XSTR (pattern, 2) = new_c;
686 XINT (pattern, 0) += max_op;
693 fmt = GET_RTX_FORMAT (code);
694 len = GET_RTX_LENGTH (code);
695 for (i = 0; i < len; i++)
702 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
709 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
711 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
712 alt, max_op, lineno);
718 case 'i': case 'w': case '0': case 's':
730 alter_test_for_insn (ce_elem, insn_elem)
731 struct queue_elem *ce_elem, *insn_elem;
733 const char *ce_test, *insn_test;
735 size_t len, ce_len, insn_len;
737 ce_test = XSTR (ce_elem->data, 1);
738 insn_test = XSTR (insn_elem->data, 2);
739 if (!ce_test || *ce_test == '\0')
741 if (!insn_test || *insn_test == '\0')
744 ce_len = strlen (ce_test);
745 insn_len = strlen (insn_test);
746 len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1;
747 new_test = (char *) xmalloc (len);
749 sprintf (new_test, "(%s) && (%s)", ce_test, insn_test);
754 /* Adjust all of the operand numbers in OLD to match the shift they'll
755 get from an operand displacement of DISP. Return a pointer after the
759 shift_output_template (new, old, disp)
771 if (ISDIGIT ((unsigned char) c))
773 else if (ISALPHA (c))
786 alter_output_for_insn (ce_elem, insn_elem, alt, max_op)
787 struct queue_elem *ce_elem, *insn_elem;
790 const char *ce_out, *insn_out;
792 size_t len, ce_len, insn_len;
794 /* ??? Could coordinate with genoutput to not duplicate code here. */
796 ce_out = XSTR (ce_elem->data, 2);
797 insn_out = XTMPL (insn_elem->data, 3);
798 if (!ce_out || *ce_out == '\0')
801 ce_len = strlen (ce_out);
802 insn_len = strlen (insn_out);
804 if (*insn_out == '*')
805 /* You must take care of the predicate yourself. */
808 if (*insn_out == '@')
810 len = (ce_len + 1) * alt + insn_len + 1;
811 p = new = xmalloc (len);
817 while (ISSPACE ((unsigned char) *insn_out));
819 if (*insn_out != '#')
821 p = shift_output_template (p, ce_out, max_op);
827 while (*insn_out && *insn_out != '\n');
834 len = ce_len + 1 + insn_len + 1;
837 p = shift_output_template (new, ce_out, max_op);
839 memcpy (p, insn_out, insn_len + 1);
845 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
848 process_one_cond_exec (ce_elem)
849 struct queue_elem *ce_elem;
851 struct queue_elem *insn_elem;
852 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
854 int alternatives, max_operand;
855 rtx pred, insn, pattern;
857 if (! is_predicable (insn_elem))
862 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
865 if (XVECLEN (ce_elem->data, 0) != 1)
867 message_with_line (ce_elem->lineno,
868 "too many patterns in predicate");
873 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
874 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
879 /* Construct a new pattern for the new insn. */
880 insn = copy_rtx (insn_elem->data);
882 pattern = rtx_alloc (COND_EXEC);
883 XEXP (pattern, 0) = pred;
884 if (XVECLEN (insn, 1) == 1)
886 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
887 XVECEXP (insn, 1, 0) = pattern;
888 PUT_NUM_ELEM (XVEC (insn, 1), 1);
892 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
893 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
894 XVEC (insn, 1) = rtvec_alloc (1);
895 XVECEXP (insn, 1, 0) = pattern;
898 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
899 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
900 alternatives, max_operand);
902 /* ??? Set `predicable' to false. Not crucial since it's really
903 only used here, and we won't reprocess this new pattern. */
905 /* Put the new pattern on the `other' list so that it
906 (a) is not reprocessed by other define_cond_exec patterns
907 (b) appears after all normal define_insn patterns.
909 ??? B is debatable. If one has normal insns that match
910 cond_exec patterns, they will be preferred over these
911 generated patterns. Whether this matters in practice, or if
912 it's a good thing, or whether we should thread these new
913 patterns into the define_insn chain just after their generator
914 is something we'll have to experiment with. */
916 queue_pattern (insn, &other_tail, insn_elem->lineno);
920 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
921 patterns appropriately. */
924 process_define_cond_exec ()
926 struct queue_elem *elem;
928 identify_predicable_attribute ();
932 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
933 process_one_cond_exec (elem);
941 register char *result = xmalloc (len + 1);
943 memcpy (result, s, len);
949 /* The entry point for initializing the reader. */
952 init_md_reader_args (argc, argv)
957 const char *in_fname;
961 for (i = 1; i < argc; i++)
963 if (argv[i][0] != '-')
965 if (in_fname == NULL)
973 case 'I': /* Add directory to path for includes. */
975 struct file_name_list *dirtmp;
977 dirtmp = (struct file_name_list *)
978 xmalloc (sizeof (struct file_name_list));
979 dirtmp->next = 0; /* New one goes on the end */
983 last_include->next = dirtmp;
984 last_include = dirtmp; /* Tail follows the last one */
985 if (argv[i][1] == 'I' && argv[i][2] != 0)
986 dirtmp->fname = argv[i] + 2;
987 else if (i + 1 == argc)
988 fatal ("Directory name missing after -I option");
990 dirtmp->fname = argv[++i];
991 if (strlen (dirtmp->fname) > max_include_len)
992 max_include_len = strlen (dirtmp->fname);
996 fatal ("Invalid option `%s'", argv[i]);
1001 return init_md_reader (in_fname);
1004 /* The entry point for initializing the reader. */
1007 init_md_reader (filename)
1008 const char *filename;
1014 if (!IS_ABSOLUTE_PATHNAME (filename))
1016 lastsl = strrchr (filename, '/');
1018 base_dir = save_string (filename, lastsl - filename + 1 );
1021 read_rtx_filename = filename;
1022 input_file = fopen (filename, "r");
1023 if (input_file == 0)
1026 return FATAL_EXIT_CODE;
1029 obstack_init (rtl_obstack);
1033 /* Read the entire file. */
1039 c = read_skip_spaces (input_file);
1043 ungetc (c, input_file);
1044 lineno = read_rtx_lineno;
1045 desc = read_rtx (input_file);
1046 process_rtx (desc, lineno);
1048 fclose (input_file);
1050 /* Process define_cond_exec patterns. */
1051 if (define_cond_exec_queue != NULL)
1052 process_define_cond_exec ();
1054 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
1057 /* The entry point for reading a single rtx from an md file. */
1060 read_md_rtx (lineno, seqnr)
1064 struct queue_elem **queue, *elem;
1067 /* Read all patterns from a given queue before moving on to the next. */
1068 if (define_attr_queue != NULL)
1069 queue = &define_attr_queue;
1070 else if (define_insn_queue != NULL)
1071 queue = &define_insn_queue;
1072 else if (other_queue != NULL)
1073 queue = &other_queue;
1078 *queue = elem->next;
1080 *lineno = elem->lineno;
1081 *seqnr = sequence_num;
1085 switch (GET_CODE (desc))
1090 case DEFINE_PEEPHOLE:
1091 case DEFINE_PEEPHOLE2:
1102 /* Until we can use the versions in libiberty. */
1107 size_t len = strlen (input) + 1;
1108 char *output = xmalloc (len);
1109 memcpy (output, input, len);
1114 xcalloc (nelem, elsize)
1115 size_t nelem, elsize;
1119 if (nelem == 0 || elsize == 0)
1122 newmem = really_call_calloc (nelem, elsize);
1124 fatal ("virtual memory exhausted");
1129 xrealloc (old, size)
1135 ptr = (PTR) really_call_realloc (old, size);
1137 ptr = (PTR) really_call_malloc (size);
1139 fatal ("virtual memory exhausted");
1147 PTR val = (PTR) really_call_malloc (size);
1150 fatal ("virtual memory exhausted");