1 /* Support routines for the various generation passes.
2 Copyright (C) 2000, 2001, 2002 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
27 #include "gensupport.h"
30 /* In case some macros used by files we include need it, define this here. */
35 static struct obstack obstack;
36 struct obstack *rtl_obstack = &obstack;
38 static int sequence_num;
41 static int predicable_default;
42 static const char *predicable_true;
43 static const char *predicable_false;
45 static htab_t condition_table;
47 static char *base_dir = NULL;
49 /* We initially queue all patterns, process the define_insn and
50 define_cond_exec patterns, then return them one at a time. */
57 struct queue_elem *next;
60 static struct queue_elem *define_attr_queue;
61 static struct queue_elem **define_attr_tail = &define_attr_queue;
62 static struct queue_elem *define_insn_queue;
63 static struct queue_elem **define_insn_tail = &define_insn_queue;
64 static struct queue_elem *define_cond_exec_queue;
65 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
66 static struct queue_elem *other_queue;
67 static struct queue_elem **other_tail = &other_queue;
69 static void queue_pattern PARAMS ((rtx, struct queue_elem ***,
72 /* Current maximum length of directory names in the search path
73 for include files. (Altered as we get more of them.) */
75 size_t max_include_len;
79 struct file_name_list *next;
83 struct file_name_list *first_dir_md_include = 0; /* First dir to search */
84 /* First dir to search for <file> */
85 struct file_name_list *first_bracket_include = 0;
86 struct file_name_list *last_dir_md_include = 0; /* Last in chain */
88 static void remove_constraints PARAMS ((rtx));
89 static void process_rtx PARAMS ((rtx, int));
91 static int is_predicable PARAMS ((struct queue_elem *));
92 static void identify_predicable_attribute PARAMS ((void));
93 static int n_alternatives PARAMS ((const char *));
94 static void collect_insn_data PARAMS ((rtx, int *, int *));
95 static rtx alter_predicate_for_insn PARAMS ((rtx, int, int, int));
96 static const char *alter_test_for_insn PARAMS ((struct queue_elem *,
97 struct queue_elem *));
98 static char *shift_output_template PARAMS ((char *, const char *, int));
99 static const char *alter_output_for_insn PARAMS ((struct queue_elem *,
102 static void process_one_cond_exec PARAMS ((struct queue_elem *));
103 static void process_define_cond_exec PARAMS ((void));
104 static void process_include PARAMS ((rtx, int));
105 static char *save_string PARAMS ((const char *, int));
108 message_with_line VPARAMS ((int lineno, const char *msg, ...))
111 VA_FIXEDARG (ap, int, lineno);
112 VA_FIXEDARG (ap, const char *, msg);
114 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
115 vfprintf (stderr, msg, ap);
116 fputc ('\n', stderr);
121 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
122 the gensupport programs. */
125 gen_rtx_CONST_INT (mode, arg)
126 enum machine_mode mode ATTRIBUTE_UNUSED;
129 rtx rt = rtx_alloc (CONST_INT);
135 /* Queue PATTERN on LIST_TAIL. */
138 queue_pattern (pattern, list_tail, filename, lineno)
140 struct queue_elem ***list_tail;
141 const char *filename;
144 struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e));
146 e->filename = filename;
150 *list_tail = &e->next;
153 /* Recursively remove constraints from an rtx. */
156 remove_constraints (part)
160 const char *format_ptr;
165 if (GET_CODE (part) == MATCH_OPERAND)
167 else if (GET_CODE (part) == MATCH_SCRATCH)
170 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
172 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
173 switch (*format_ptr++)
177 remove_constraints (XEXP (part, i));
180 if (XVEC (part, i) != NULL)
181 for (j = 0; j < XVECLEN (part, i); j++)
182 remove_constraints (XVECEXP (part, i, j));
187 /* Process an include file assuming that it lives in gcc/config/{target}/
188 if the include looks like (include "file"). */
191 process_include (desc, lineno)
195 const char *filename = XSTR (desc, 0);
196 const char *old_filename;
201 /* If specified file name is absolute, skip the include stack. */
202 if (! IS_ABSOLUTE_PATHNAME (filename))
204 struct file_name_list *stackp;
206 /* Search directory path, trying to open the file. */
207 for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
209 static const char sep[2] = { DIR_SEPARATOR, '\0' };
211 pathname = concat (stackp->fname, sep, filename, NULL);
212 input_file = fopen (pathname, "r");
213 if (input_file != NULL)
220 pathname = concat (base_dir, filename, NULL);
222 pathname = xstrdup (filename);
223 input_file = fopen (pathname, "r");
224 if (input_file == NULL)
227 message_with_line (lineno, "include file `%s' not found", filename);
233 /* Save old cursor; setup new for the new file. Note that "lineno" the
234 argument to this function is the beginning of the include statement,
235 while read_rtx_lineno has already been advanced. */
236 old_filename = read_rtx_filename;
237 old_lineno = read_rtx_lineno;
238 read_rtx_filename = pathname;
241 /* Read the entire file. */
247 c = read_skip_spaces (input_file);
251 ungetc (c, input_file);
252 lineno = read_rtx_lineno;
253 desc = read_rtx (input_file);
254 process_rtx (desc, lineno);
257 /* Do not free pathname. It is attached to the various rtx queue
260 read_rtx_filename = old_filename;
261 read_rtx_lineno = old_lineno;
266 /* Process a top level rtx in some way, queueing as appropriate. */
269 process_rtx (desc, lineno)
273 switch (GET_CODE (desc))
276 queue_pattern (desc, &define_insn_tail, read_rtx_filename, lineno);
279 case DEFINE_COND_EXEC:
280 queue_pattern (desc, &define_cond_exec_tail, read_rtx_filename, lineno);
284 queue_pattern (desc, &define_attr_tail, read_rtx_filename, lineno);
288 process_include (desc, lineno);
291 case DEFINE_INSN_AND_SPLIT:
293 const char *split_cond;
298 /* Create a split with values from the insn_and_split. */
299 split = rtx_alloc (DEFINE_SPLIT);
301 i = XVECLEN (desc, 1);
302 XVEC (split, 0) = rtvec_alloc (i);
305 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
306 remove_constraints (XVECEXP (split, 0, i));
309 /* If the split condition starts with "&&", append it to the
310 insn condition to create the new split condition. */
311 split_cond = XSTR (desc, 4);
312 if (split_cond[0] == '&' && split_cond[1] == '&')
314 const char *insn_cond = XSTR (desc, 2);
315 size_t insn_cond_len = strlen (insn_cond);
316 size_t split_cond_len = strlen (split_cond);
319 combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1);
320 memcpy (combined, insn_cond, insn_cond_len);
321 memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1);
323 split_cond = combined;
325 XSTR (split, 1) = split_cond;
326 XVEC (split, 2) = XVEC (desc, 5);
327 XSTR (split, 3) = XSTR (desc, 6);
329 /* Fix up the DEFINE_INSN. */
330 attr = XVEC (desc, 7);
331 PUT_CODE (desc, DEFINE_INSN);
332 XVEC (desc, 4) = attr;
335 queue_pattern (desc, &define_insn_tail, read_rtx_filename, lineno);
336 queue_pattern (split, &other_tail, read_rtx_filename, lineno);
341 queue_pattern (desc, &other_tail, read_rtx_filename, lineno);
346 /* Return true if attribute PREDICABLE is true for ELEM, which holds
351 struct queue_elem *elem;
353 rtvec vec = XVEC (elem->data, 4);
358 return predicable_default;
360 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
362 rtx sub = RTVEC_ELT (vec, i);
363 switch (GET_CODE (sub))
366 if (strcmp (XSTR (sub, 0), "predicable") == 0)
368 value = XSTR (sub, 1);
373 case SET_ATTR_ALTERNATIVE:
374 if (strcmp (XSTR (sub, 0), "predicable") == 0)
376 message_with_line (elem->lineno,
377 "multiple alternatives for `predicable'");
384 if (GET_CODE (SET_DEST (sub)) != ATTR
385 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
388 if (GET_CODE (sub) == CONST_STRING)
390 value = XSTR (sub, 0);
394 /* ??? It would be possible to handle this if we really tried.
395 It's not easy though, and I'm not going to bother until it
396 really proves necessary. */
397 message_with_line (elem->lineno,
398 "non-constant value for `predicable'");
407 return predicable_default;
410 /* Verify that predicability does not vary on the alternative. */
411 /* ??? It should be possible to handle this by simply eliminating
412 the non-predicable alternatives from the insn. FRV would like
413 to do this. Delay this until we've got the basics solid. */
414 if (strchr (value, ',') != NULL)
416 message_with_line (elem->lineno,
417 "multiple alternatives for `predicable'");
422 /* Find out which value we're looking at. */
423 if (strcmp (value, predicable_true) == 0)
425 if (strcmp (value, predicable_false) == 0)
428 message_with_line (elem->lineno,
429 "unknown value `%s' for `predicable' attribute",
435 /* Examine the attribute "predicable"; discover its boolean values
439 identify_predicable_attribute ()
441 struct queue_elem *elem;
442 char *p_true, *p_false;
446 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
447 for (elem = define_attr_queue; elem ; elem = elem->next)
448 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
451 message_with_line (define_cond_exec_queue->lineno,
452 "attribute `predicable' not defined");
457 value = XSTR (elem->data, 1);
458 len = strlen (value);
459 p_false = (char *) xmalloc (len + 1);
460 memcpy (p_false, value, len + 1);
462 p_true = strchr (p_false, ',');
463 if (p_true == NULL || strchr (++p_true, ',') != NULL)
465 message_with_line (elem->lineno,
466 "attribute `predicable' is not a boolean");
472 predicable_true = p_true;
473 predicable_false = p_false;
475 switch (GET_CODE (XEXP (elem->data, 2)))
478 value = XSTR (XEXP (elem->data, 2), 0);
482 message_with_line (elem->lineno,
483 "attribute `predicable' cannot be const");
488 message_with_line (elem->lineno,
489 "attribute `predicable' must have a constant default");
494 if (strcmp (value, p_true) == 0)
495 predicable_default = 1;
496 else if (strcmp (value, p_false) == 0)
497 predicable_default = 0;
500 message_with_line (elem->lineno,
501 "unknown value `%s' for `predicable' attribute",
507 /* Return the number of alternatives in constraint S. */
522 /* Determine how many alternatives there are in INSN, and how many
526 collect_insn_data (pattern, palt, pmax)
534 code = GET_CODE (pattern);
538 i = n_alternatives (XSTR (pattern, 2));
539 *palt = (i > *palt ? i : *palt);
546 i = XINT (pattern, 0);
555 fmt = GET_RTX_FORMAT (code);
556 len = GET_RTX_LENGTH (code);
557 for (i = 0; i < len; i++)
562 collect_insn_data (XEXP (pattern, i), palt, pmax);
566 if (XVEC (pattern, i) == NULL)
570 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
571 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
574 case 'i': case 'w': case '0': case 's': case 'S': case 'T':
584 alter_predicate_for_insn (pattern, alt, max_op, lineno)
586 int alt, max_op, lineno;
592 code = GET_CODE (pattern);
597 const char *c = XSTR (pattern, 2);
599 if (n_alternatives (c) != 1)
601 message_with_line (lineno,
602 "too many alternatives for operand %d",
608 /* Replicate C as needed to fill out ALT alternatives. */
609 if (c && *c && alt > 1)
611 size_t c_len = strlen (c);
612 size_t len = alt * (c_len + 1);
613 char *new_c = (char *) xmalloc (len);
615 memcpy (new_c, c, c_len);
616 for (i = 1; i < alt; ++i)
618 new_c[i * (c_len + 1) - 1] = ',';
619 memcpy (&new_c[i * (c_len + 1)], c, c_len);
621 new_c[len - 1] = '\0';
622 XSTR (pattern, 2) = new_c;
631 XINT (pattern, 0) += max_op;
638 fmt = GET_RTX_FORMAT (code);
639 len = GET_RTX_LENGTH (code);
640 for (i = 0; i < len; i++)
647 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
654 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
656 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
657 alt, max_op, lineno);
663 case 'i': case 'w': case '0': case 's':
675 alter_test_for_insn (ce_elem, insn_elem)
676 struct queue_elem *ce_elem, *insn_elem;
678 const char *ce_test, *insn_test;
680 size_t len, ce_len, insn_len;
682 ce_test = XSTR (ce_elem->data, 1);
683 insn_test = XSTR (insn_elem->data, 2);
684 if (!ce_test || *ce_test == '\0')
686 if (!insn_test || *insn_test == '\0')
689 ce_len = strlen (ce_test);
690 insn_len = strlen (insn_test);
691 len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1;
692 new_test = (char *) xmalloc (len);
694 sprintf (new_test, "(%s) && (%s)", ce_test, insn_test);
699 /* Adjust all of the operand numbers in OLD to match the shift they'll
700 get from an operand displacement of DISP. Return a pointer after the
704 shift_output_template (new, old, disp)
716 if (ISDIGIT ((unsigned char) c))
718 else if (ISALPHA (c))
731 alter_output_for_insn (ce_elem, insn_elem, alt, max_op)
732 struct queue_elem *ce_elem, *insn_elem;
735 const char *ce_out, *insn_out;
737 size_t len, ce_len, insn_len;
739 /* ??? Could coordinate with genoutput to not duplicate code here. */
741 ce_out = XSTR (ce_elem->data, 2);
742 insn_out = XTMPL (insn_elem->data, 3);
743 if (!ce_out || *ce_out == '\0')
746 ce_len = strlen (ce_out);
747 insn_len = strlen (insn_out);
749 if (*insn_out == '*')
750 /* You must take care of the predicate yourself. */
753 if (*insn_out == '@')
755 len = (ce_len + 1) * alt + insn_len + 1;
756 p = new = xmalloc (len);
762 while (ISSPACE ((unsigned char) *insn_out));
764 if (*insn_out != '#')
766 p = shift_output_template (p, ce_out, max_op);
772 while (*insn_out && *insn_out != '\n');
779 len = ce_len + 1 + insn_len + 1;
782 p = shift_output_template (new, ce_out, max_op);
784 memcpy (p, insn_out, insn_len + 1);
790 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
793 process_one_cond_exec (ce_elem)
794 struct queue_elem *ce_elem;
796 struct queue_elem *insn_elem;
797 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
799 int alternatives, max_operand;
800 rtx pred, insn, pattern;
802 if (! is_predicable (insn_elem))
807 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
810 if (XVECLEN (ce_elem->data, 0) != 1)
812 message_with_line (ce_elem->lineno,
813 "too many patterns in predicate");
818 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
819 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
824 /* Construct a new pattern for the new insn. */
825 insn = copy_rtx (insn_elem->data);
827 pattern = rtx_alloc (COND_EXEC);
828 XEXP (pattern, 0) = pred;
829 if (XVECLEN (insn, 1) == 1)
831 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
832 XVECEXP (insn, 1, 0) = pattern;
833 PUT_NUM_ELEM (XVEC (insn, 1), 1);
837 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
838 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
839 XVEC (insn, 1) = rtvec_alloc (1);
840 XVECEXP (insn, 1, 0) = pattern;
843 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
844 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
845 alternatives, max_operand);
847 /* ??? Set `predicable' to false. Not crucial since it's really
848 only used here, and we won't reprocess this new pattern. */
850 /* Put the new pattern on the `other' list so that it
851 (a) is not reprocessed by other define_cond_exec patterns
852 (b) appears after all normal define_insn patterns.
854 ??? B is debatable. If one has normal insns that match
855 cond_exec patterns, they will be preferred over these
856 generated patterns. Whether this matters in practice, or if
857 it's a good thing, or whether we should thread these new
858 patterns into the define_insn chain just after their generator
859 is something we'll have to experiment with. */
861 queue_pattern (insn, &other_tail, insn_elem->filename,
866 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
867 patterns appropriately. */
870 process_define_cond_exec ()
872 struct queue_elem *elem;
874 identify_predicable_attribute ();
878 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
879 process_one_cond_exec (elem);
887 register char *result = xmalloc (len + 1);
889 memcpy (result, s, len);
895 /* The entry point for initializing the reader. */
898 init_md_reader_args (argc, argv)
903 const char *in_fname;
907 for (i = 1; i < argc; i++)
909 if (argv[i][0] != '-')
911 if (in_fname == NULL)
919 case 'I': /* Add directory to path for includes. */
921 struct file_name_list *dirtmp;
923 dirtmp = (struct file_name_list *)
924 xmalloc (sizeof (struct file_name_list));
925 dirtmp->next = 0; /* New one goes on the end */
926 if (first_dir_md_include == 0)
927 first_dir_md_include = dirtmp;
929 last_dir_md_include->next = dirtmp;
930 last_dir_md_include = dirtmp; /* Tail follows the last one */
931 if (argv[i][1] == 'I' && argv[i][2] != 0)
932 dirtmp->fname = argv[i] + 2;
933 else if (i + 1 == argc)
934 fatal ("directory name missing after -I option");
936 dirtmp->fname = argv[++i];
937 if (strlen (dirtmp->fname) > max_include_len)
938 max_include_len = strlen (dirtmp->fname);
942 fatal ("invalid option `%s'", argv[i]);
947 return init_md_reader (in_fname);
950 /* The entry point for initializing the reader. */
953 init_md_reader (filename)
954 const char *filename;
961 lastsl = strrchr (filename, '/');
963 base_dir = save_string (filename, lastsl - filename + 1 );
965 read_rtx_filename = filename;
966 input_file = fopen (filename, "r");
970 return FATAL_EXIT_CODE;
973 /* Initialize the table of insn conditions. */
974 condition_table = htab_create (n_insn_conditions,
975 hash_c_test, cmp_c_test, NULL);
977 for (i = 0; i < n_insn_conditions; i++)
978 *(htab_find_slot (condition_table, (PTR) &insn_conditions[i], INSERT))
979 = (PTR) &insn_conditions[i];
981 obstack_init (rtl_obstack);
985 /* Read the entire file. */
991 c = read_skip_spaces (input_file);
995 ungetc (c, input_file);
996 lineno = read_rtx_lineno;
997 desc = read_rtx (input_file);
998 process_rtx (desc, lineno);
1000 fclose (input_file);
1002 /* Process define_cond_exec patterns. */
1003 if (define_cond_exec_queue != NULL)
1004 process_define_cond_exec ();
1006 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
1009 /* The entry point for reading a single rtx from an md file. */
1012 read_md_rtx (lineno, seqnr)
1016 struct queue_elem **queue, *elem;
1021 /* Read all patterns from a given queue before moving on to the next. */
1022 if (define_attr_queue != NULL)
1023 queue = &define_attr_queue;
1024 else if (define_insn_queue != NULL)
1025 queue = &define_insn_queue;
1026 else if (other_queue != NULL)
1027 queue = &other_queue;
1032 *queue = elem->next;
1034 read_rtx_filename = elem->filename;
1035 *lineno = elem->lineno;
1036 *seqnr = sequence_num;
1040 /* Discard insn patterns which we know can never match (because
1041 their C test is provably always false). If insn_elision is
1042 false, our caller needs to see all the patterns. Note that the
1043 elided patterns are never counted by the sequence numbering; it
1044 it is the caller's responsibility, when insn_elision is false, not
1045 to use elided pattern numbers for anything. */
1046 switch (GET_CODE (desc))
1050 if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
1052 else if (insn_elision)
1057 case DEFINE_PEEPHOLE:
1058 case DEFINE_PEEPHOLE2:
1059 if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
1061 else if (insn_elision)
1072 /* Helper functions for insn elision. */
1074 /* Compute a hash function of a c_test structure, which is keyed
1075 by its ->expr field. */
1080 const struct c_test *a = (const struct c_test *) x;
1081 const unsigned char *base, *s = (const unsigned char *) a->expr;
1089 while ((c = *s++) != '\0')
1091 hash += c + (c << 17);
1096 hash += len + (len << 17);
1102 /* Compare two c_test expression structures. */
1108 const struct c_test *a = (const struct c_test *) x;
1109 const struct c_test *b = (const struct c_test *) y;
1111 return !strcmp (a->expr, b->expr);
1114 /* Given a string representing a C test expression, look it up in the
1115 condition_table and report whether or not its value is known
1116 at compile time. Returns a tristate: 1 for known true, 0 for
1117 known false, -1 for unknown. */
1119 maybe_eval_c_test (expr)
1122 const struct c_test *test;
1123 struct c_test dummy;
1128 if (insn_elision_unavailable)
1132 test = (const struct c_test *) htab_find (condition_table, &dummy);
1139 /* Given a string, return the number of comma-separated elements in it.
1140 Return 0 for the null string. */
1150 for (n = 1; *s; s++)
1157 /* Given a pointer to a (char *), return a pointer to the beginning of the
1158 next comma-separated element in the string. Advance the pointer given
1159 to the end of that element. Return NULL if at end of string. Caller
1160 is responsible for copying the string if necessary. White space between
1161 a comma and an element is ignored. */
1164 scan_comma_elt (pstr)
1168 const char *p = *pstr;
1180 while (*p != ',' && *p != '\0')