2006-02-09 Kaz Kojima <kkojima@gcc.gnu.org>
[official-gcc.git] / gcc / gensupport.c
blobbbac33a2c8863d496d827f23418ff10c5eb9d907
1 /* Support routines for the various generation passes.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "bconfig.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "obstack.h"
28 #include "errors.h"
29 #include "hashtab.h"
30 #include "gensupport.h"
33 /* In case some macros used by files we include need it, define this here. */
34 int target_flags;
36 int insn_elision = 1;
38 const char *in_fname;
40 /* This callback will be invoked whenever an rtl include directive is
41 processed. To be used for creation of the dependency file. */
42 void (*include_callback) (const char *);
44 static struct obstack obstack;
45 struct obstack *rtl_obstack = &obstack;
47 static int sequence_num;
48 static int errors;
50 static int predicable_default;
51 static const char *predicable_true;
52 static const char *predicable_false;
54 static htab_t condition_table;
56 static char *base_dir = NULL;
58 /* We initially queue all patterns, process the define_insn and
59 define_cond_exec patterns, then return them one at a time. */
61 struct queue_elem
63 rtx data;
64 const char *filename;
65 int lineno;
66 struct queue_elem *next;
67 /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT, SPLIT
68 points to the generated DEFINE_SPLIT. */
69 struct queue_elem *split;
72 static struct queue_elem *define_attr_queue;
73 static struct queue_elem **define_attr_tail = &define_attr_queue;
74 static struct queue_elem *define_pred_queue;
75 static struct queue_elem **define_pred_tail = &define_pred_queue;
76 static struct queue_elem *define_insn_queue;
77 static struct queue_elem **define_insn_tail = &define_insn_queue;
78 static struct queue_elem *define_cond_exec_queue;
79 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
80 static struct queue_elem *other_queue;
81 static struct queue_elem **other_tail = &other_queue;
83 static struct queue_elem *queue_pattern (rtx, struct queue_elem ***,
84 const char *, int);
86 /* Current maximum length of directory names in the search path
87 for include files. (Altered as we get more of them.) */
89 size_t max_include_len;
91 struct file_name_list
93 struct file_name_list *next;
94 const char *fname;
97 struct file_name_list *first_dir_md_include = 0; /* First dir to search */
98 /* First dir to search for <file> */
99 struct file_name_list *first_bracket_include = 0;
100 struct file_name_list *last_dir_md_include = 0; /* Last in chain */
102 static void remove_constraints (rtx);
103 static void process_rtx (rtx, int);
105 static int is_predicable (struct queue_elem *);
106 static void identify_predicable_attribute (void);
107 static int n_alternatives (const char *);
108 static void collect_insn_data (rtx, int *, int *);
109 static rtx alter_predicate_for_insn (rtx, int, int, int);
110 static const char *alter_test_for_insn (struct queue_elem *,
111 struct queue_elem *);
112 static char *shift_output_template (char *, const char *, int);
113 static const char *alter_output_for_insn (struct queue_elem *,
114 struct queue_elem *,
115 int, int);
116 static void process_one_cond_exec (struct queue_elem *);
117 static void process_define_cond_exec (void);
118 static void process_include (rtx, int);
119 static char *save_string (const char *, int);
120 static void init_predicate_table (void);
121 static void record_insn_name (int, const char *);
123 void
124 message_with_line (int lineno, const char *msg, ...)
126 va_list ap;
128 va_start (ap, msg);
130 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
131 vfprintf (stderr, msg, ap);
132 fputc ('\n', stderr);
134 va_end (ap);
137 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
138 the gensupport programs. */
141 gen_rtx_CONST_INT (enum machine_mode ARG_UNUSED (mode),
142 HOST_WIDE_INT arg)
144 rtx rt = rtx_alloc (CONST_INT);
146 XWINT (rt, 0) = arg;
147 return rt;
150 /* Queue PATTERN on LIST_TAIL. Return the address of the new queue
151 element. */
153 static struct queue_elem *
154 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
155 const char *filename, int lineno)
157 struct queue_elem *e = XNEW(struct queue_elem);
158 e->data = pattern;
159 e->filename = filename;
160 e->lineno = lineno;
161 e->next = NULL;
162 e->split = NULL;
163 **list_tail = e;
164 *list_tail = &e->next;
165 return e;
168 /* Recursively remove constraints from an rtx. */
170 static void
171 remove_constraints (rtx part)
173 int i, j;
174 const char *format_ptr;
176 if (part == 0)
177 return;
179 if (GET_CODE (part) == MATCH_OPERAND)
180 XSTR (part, 2) = "";
181 else if (GET_CODE (part) == MATCH_SCRATCH)
182 XSTR (part, 1) = "";
184 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
186 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
187 switch (*format_ptr++)
189 case 'e':
190 case 'u':
191 remove_constraints (XEXP (part, i));
192 break;
193 case 'E':
194 if (XVEC (part, i) != NULL)
195 for (j = 0; j < XVECLEN (part, i); j++)
196 remove_constraints (XVECEXP (part, i, j));
197 break;
201 /* Process an include file assuming that it lives in gcc/config/{target}/
202 if the include looks like (include "file"). */
204 static void
205 process_include (rtx desc, int lineno)
207 const char *filename = XSTR (desc, 0);
208 const char *old_filename;
209 int old_lineno;
210 char *pathname;
211 FILE *input_file;
213 /* If specified file name is absolute, skip the include stack. */
214 if (! IS_ABSOLUTE_PATH (filename))
216 struct file_name_list *stackp;
218 /* Search directory path, trying to open the file. */
219 for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
221 static const char sep[2] = { DIR_SEPARATOR, '\0' };
223 pathname = concat (stackp->fname, sep, filename, NULL);
224 input_file = fopen (pathname, "r");
225 if (input_file != NULL)
226 goto success;
227 free (pathname);
231 if (base_dir)
232 pathname = concat (base_dir, filename, NULL);
233 else
234 pathname = xstrdup (filename);
235 input_file = fopen (pathname, "r");
236 if (input_file == NULL)
238 free (pathname);
239 message_with_line (lineno, "include file `%s' not found", filename);
240 errors = 1;
241 return;
243 success:
245 /* Save old cursor; setup new for the new file. Note that "lineno" the
246 argument to this function is the beginning of the include statement,
247 while read_rtx_lineno has already been advanced. */
248 old_filename = read_rtx_filename;
249 old_lineno = read_rtx_lineno;
250 read_rtx_filename = pathname;
251 read_rtx_lineno = 1;
253 if (include_callback)
254 include_callback (pathname);
256 /* Read the entire file. */
257 while (read_rtx (input_file, &desc, &lineno))
258 process_rtx (desc, lineno);
260 /* Do not free pathname. It is attached to the various rtx queue
261 elements. */
263 read_rtx_filename = old_filename;
264 read_rtx_lineno = old_lineno;
266 fclose (input_file);
269 /* Process a top level rtx in some way, queuing as appropriate. */
271 static void
272 process_rtx (rtx desc, int lineno)
274 switch (GET_CODE (desc))
276 case DEFINE_INSN:
277 queue_pattern (desc, &define_insn_tail, read_rtx_filename, lineno);
278 break;
280 case DEFINE_COND_EXEC:
281 queue_pattern (desc, &define_cond_exec_tail, read_rtx_filename, lineno);
282 break;
284 case DEFINE_ATTR:
285 queue_pattern (desc, &define_attr_tail, read_rtx_filename, lineno);
286 break;
288 case DEFINE_PREDICATE:
289 case DEFINE_SPECIAL_PREDICATE:
290 queue_pattern (desc, &define_pred_tail, read_rtx_filename, lineno);
291 break;
293 case INCLUDE:
294 process_include (desc, lineno);
295 break;
297 case DEFINE_INSN_AND_SPLIT:
299 const char *split_cond;
300 rtx split;
301 rtvec attr;
302 int i;
303 struct queue_elem *insn_elem;
304 struct queue_elem *split_elem;
306 /* Create a split with values from the insn_and_split. */
307 split = rtx_alloc (DEFINE_SPLIT);
309 i = XVECLEN (desc, 1);
310 XVEC (split, 0) = rtvec_alloc (i);
311 while (--i >= 0)
313 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
314 remove_constraints (XVECEXP (split, 0, i));
317 /* If the split condition starts with "&&", append it to the
318 insn condition to create the new split condition. */
319 split_cond = XSTR (desc, 4);
320 if (split_cond[0] == '&' && split_cond[1] == '&')
322 copy_rtx_ptr_loc (split_cond + 2, split_cond);
323 split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
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;
334 /* Queue them. */
335 insn_elem
336 = queue_pattern (desc, &define_insn_tail, read_rtx_filename,
337 lineno);
338 split_elem
339 = queue_pattern (split, &other_tail, read_rtx_filename, lineno);
340 insn_elem->split = split_elem;
341 break;
344 default:
345 queue_pattern (desc, &other_tail, read_rtx_filename, lineno);
346 break;
350 /* Return true if attribute PREDICABLE is true for ELEM, which holds
351 a DEFINE_INSN. */
353 static int
354 is_predicable (struct queue_elem *elem)
356 rtvec vec = XVEC (elem->data, 4);
357 const char *value;
358 int i;
360 if (! vec)
361 return predicable_default;
363 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
365 rtx sub = RTVEC_ELT (vec, i);
366 switch (GET_CODE (sub))
368 case SET_ATTR:
369 if (strcmp (XSTR (sub, 0), "predicable") == 0)
371 value = XSTR (sub, 1);
372 goto found;
374 break;
376 case SET_ATTR_ALTERNATIVE:
377 if (strcmp (XSTR (sub, 0), "predicable") == 0)
379 message_with_line (elem->lineno,
380 "multiple alternatives for `predicable'");
381 errors = 1;
382 return 0;
384 break;
386 case SET:
387 if (GET_CODE (SET_DEST (sub)) != ATTR
388 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
389 break;
390 sub = SET_SRC (sub);
391 if (GET_CODE (sub) == CONST_STRING)
393 value = XSTR (sub, 0);
394 goto found;
397 /* ??? It would be possible to handle this if we really tried.
398 It's not easy though, and I'm not going to bother until it
399 really proves necessary. */
400 message_with_line (elem->lineno,
401 "non-constant value for `predicable'");
402 errors = 1;
403 return 0;
405 default:
406 gcc_unreachable ();
410 return predicable_default;
412 found:
413 /* Verify that predicability does not vary on the alternative. */
414 /* ??? It should be possible to handle this by simply eliminating
415 the non-predicable alternatives from the insn. FRV would like
416 to do this. Delay this until we've got the basics solid. */
417 if (strchr (value, ',') != NULL)
419 message_with_line (elem->lineno,
420 "multiple alternatives for `predicable'");
421 errors = 1;
422 return 0;
425 /* Find out which value we're looking at. */
426 if (strcmp (value, predicable_true) == 0)
427 return 1;
428 if (strcmp (value, predicable_false) == 0)
429 return 0;
431 message_with_line (elem->lineno,
432 "unknown value `%s' for `predicable' attribute",
433 value);
434 errors = 1;
435 return 0;
438 /* Examine the attribute "predicable"; discover its boolean values
439 and its default. */
441 static void
442 identify_predicable_attribute (void)
444 struct queue_elem *elem;
445 char *p_true, *p_false;
446 const char *value;
448 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
449 for (elem = define_attr_queue; elem ; elem = elem->next)
450 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
451 goto found;
453 message_with_line (define_cond_exec_queue->lineno,
454 "attribute `predicable' not defined");
455 errors = 1;
456 return;
458 found:
459 value = XSTR (elem->data, 1);
460 p_false = xstrdup (value);
461 p_true = strchr (p_false, ',');
462 if (p_true == NULL || strchr (++p_true, ',') != NULL)
464 message_with_line (elem->lineno,
465 "attribute `predicable' is not a boolean");
466 errors = 1;
467 return;
469 p_true[-1] = '\0';
471 predicable_true = p_true;
472 predicable_false = p_false;
474 switch (GET_CODE (XEXP (elem->data, 2)))
476 case CONST_STRING:
477 value = XSTR (XEXP (elem->data, 2), 0);
478 break;
480 case CONST:
481 message_with_line (elem->lineno,
482 "attribute `predicable' cannot be const");
483 errors = 1;
484 return;
486 default:
487 message_with_line (elem->lineno,
488 "attribute `predicable' must have a constant default");
489 errors = 1;
490 return;
493 if (strcmp (value, p_true) == 0)
494 predicable_default = 1;
495 else if (strcmp (value, p_false) == 0)
496 predicable_default = 0;
497 else
499 message_with_line (elem->lineno,
500 "unknown value `%s' for `predicable' attribute",
501 value);
502 errors = 1;
506 /* Return the number of alternatives in constraint S. */
508 static int
509 n_alternatives (const char *s)
511 int n = 1;
513 if (s)
514 while (*s)
515 n += (*s++ == ',');
517 return n;
520 /* Determine how many alternatives there are in INSN, and how many
521 operands. */
523 static void
524 collect_insn_data (rtx pattern, int *palt, int *pmax)
526 const char *fmt;
527 enum rtx_code code;
528 int i, j, len;
530 code = GET_CODE (pattern);
531 switch (code)
533 case MATCH_OPERAND:
534 i = n_alternatives (XSTR (pattern, 2));
535 *palt = (i > *palt ? i : *palt);
536 /* Fall through. */
538 case MATCH_OPERATOR:
539 case MATCH_SCRATCH:
540 case MATCH_PARALLEL:
541 i = XINT (pattern, 0);
542 if (i > *pmax)
543 *pmax = i;
544 break;
546 default:
547 break;
550 fmt = GET_RTX_FORMAT (code);
551 len = GET_RTX_LENGTH (code);
552 for (i = 0; i < len; i++)
554 switch (fmt[i])
556 case 'e': case 'u':
557 collect_insn_data (XEXP (pattern, i), palt, pmax);
558 break;
560 case 'V':
561 if (XVEC (pattern, i) == NULL)
562 break;
563 /* Fall through. */
564 case 'E':
565 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
566 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
567 break;
569 case 'i': case 'w': case '0': case 's': case 'S': case 'T':
570 break;
572 default:
573 gcc_unreachable ();
578 static rtx
579 alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
581 const char *fmt;
582 enum rtx_code code;
583 int i, j, len;
585 code = GET_CODE (pattern);
586 switch (code)
588 case MATCH_OPERAND:
590 const char *c = XSTR (pattern, 2);
592 if (n_alternatives (c) != 1)
594 message_with_line (lineno,
595 "too many alternatives for operand %d",
596 XINT (pattern, 0));
597 errors = 1;
598 return NULL;
601 /* Replicate C as needed to fill out ALT alternatives. */
602 if (c && *c && alt > 1)
604 size_t c_len = strlen (c);
605 size_t len = alt * (c_len + 1);
606 char *new_c = XNEWVEC(char, len);
608 memcpy (new_c, c, c_len);
609 for (i = 1; i < alt; ++i)
611 new_c[i * (c_len + 1) - 1] = ',';
612 memcpy (&new_c[i * (c_len + 1)], c, c_len);
614 new_c[len - 1] = '\0';
615 XSTR (pattern, 2) = new_c;
618 /* Fall through. */
620 case MATCH_OPERATOR:
621 case MATCH_SCRATCH:
622 case MATCH_PARALLEL:
623 XINT (pattern, 0) += max_op;
624 break;
626 default:
627 break;
630 fmt = GET_RTX_FORMAT (code);
631 len = GET_RTX_LENGTH (code);
632 for (i = 0; i < len; i++)
634 rtx r;
636 switch (fmt[i])
638 case 'e': case 'u':
639 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
640 max_op, lineno);
641 if (r == NULL)
642 return r;
643 break;
645 case 'E':
646 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
648 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
649 alt, max_op, lineno);
650 if (r == NULL)
651 return r;
653 break;
655 case 'i': case 'w': case '0': case 's':
656 break;
658 default:
659 gcc_unreachable ();
663 return pattern;
666 static const char *
667 alter_test_for_insn (struct queue_elem *ce_elem,
668 struct queue_elem *insn_elem)
670 return join_c_conditions (XSTR (ce_elem->data, 1),
671 XSTR (insn_elem->data, 2));
674 /* Adjust all of the operand numbers in SRC to match the shift they'll
675 get from an operand displacement of DISP. Return a pointer after the
676 adjusted string. */
678 static char *
679 shift_output_template (char *dest, const char *src, int disp)
681 while (*src)
683 char c = *src++;
684 *dest++ = c;
685 if (c == '%')
687 c = *src++;
688 if (ISDIGIT ((unsigned char) c))
689 c += disp;
690 else if (ISALPHA (c))
692 *dest++ = c;
693 c = *src++ + disp;
695 *dest++ = c;
699 return dest;
702 static const char *
703 alter_output_for_insn (struct queue_elem *ce_elem,
704 struct queue_elem *insn_elem,
705 int alt, int max_op)
707 const char *ce_out, *insn_out;
708 char *result, *p;
709 size_t len, ce_len, insn_len;
711 /* ??? Could coordinate with genoutput to not duplicate code here. */
713 ce_out = XSTR (ce_elem->data, 2);
714 insn_out = XTMPL (insn_elem->data, 3);
715 if (!ce_out || *ce_out == '\0')
716 return insn_out;
718 ce_len = strlen (ce_out);
719 insn_len = strlen (insn_out);
721 if (*insn_out == '*')
722 /* You must take care of the predicate yourself. */
723 return insn_out;
725 if (*insn_out == '@')
727 len = (ce_len + 1) * alt + insn_len + 1;
728 p = result = XNEWVEC(char, len);
733 *p++ = *insn_out++;
734 while (ISSPACE ((unsigned char) *insn_out));
736 if (*insn_out != '#')
738 p = shift_output_template (p, ce_out, max_op);
739 *p++ = ' ';
743 *p++ = *insn_out++;
744 while (*insn_out && *insn_out != '\n');
746 while (*insn_out);
747 *p = '\0';
749 else
751 len = ce_len + 1 + insn_len + 1;
752 result = XNEWVEC (char, len);
754 p = shift_output_template (result, ce_out, max_op);
755 *p++ = ' ';
756 memcpy (p, insn_out, insn_len + 1);
759 return result;
762 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
764 static void
765 process_one_cond_exec (struct queue_elem *ce_elem)
767 struct queue_elem *insn_elem;
768 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
770 int alternatives, max_operand;
771 rtx pred, insn, pattern, split;
772 int i;
774 if (! is_predicable (insn_elem))
775 continue;
777 alternatives = 1;
778 max_operand = -1;
779 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
780 max_operand += 1;
782 if (XVECLEN (ce_elem->data, 0) != 1)
784 message_with_line (ce_elem->lineno,
785 "too many patterns in predicate");
786 errors = 1;
787 return;
790 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
791 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
792 ce_elem->lineno);
793 if (pred == NULL)
794 return;
796 /* Construct a new pattern for the new insn. */
797 insn = copy_rtx (insn_elem->data);
798 XSTR (insn, 0) = "";
799 pattern = rtx_alloc (COND_EXEC);
800 XEXP (pattern, 0) = pred;
801 if (XVECLEN (insn, 1) == 1)
803 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
804 XVECEXP (insn, 1, 0) = pattern;
805 PUT_NUM_ELEM (XVEC (insn, 1), 1);
807 else
809 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
810 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
811 XVEC (insn, 1) = rtvec_alloc (1);
812 XVECEXP (insn, 1, 0) = pattern;
815 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
816 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
817 alternatives, max_operand);
819 /* ??? Set `predicable' to false. Not crucial since it's really
820 only used here, and we won't reprocess this new pattern. */
822 /* Put the new pattern on the `other' list so that it
823 (a) is not reprocessed by other define_cond_exec patterns
824 (b) appears after all normal define_insn patterns.
826 ??? B is debatable. If one has normal insns that match
827 cond_exec patterns, they will be preferred over these
828 generated patterns. Whether this matters in practice, or if
829 it's a good thing, or whether we should thread these new
830 patterns into the define_insn chain just after their generator
831 is something we'll have to experiment with. */
833 queue_pattern (insn, &other_tail, insn_elem->filename,
834 insn_elem->lineno);
836 if (!insn_elem->split)
837 continue;
839 /* If the original insn came from a define_insn_and_split,
840 generate a new split to handle the predicated insn. */
841 split = copy_rtx (insn_elem->split->data);
842 /* Predicate the pattern matched by the split. */
843 pattern = rtx_alloc (COND_EXEC);
844 XEXP (pattern, 0) = pred;
845 if (XVECLEN (split, 0) == 1)
847 XEXP (pattern, 1) = XVECEXP (split, 0, 0);
848 XVECEXP (split, 0, 0) = pattern;
849 PUT_NUM_ELEM (XVEC (split, 0), 1);
851 else
853 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
854 XVEC (XEXP (pattern, 1), 0) = XVEC (split, 0);
855 XVEC (split, 0) = rtvec_alloc (1);
856 XVECEXP (split, 0, 0) = pattern;
858 /* Predicate all of the insns generated by the split. */
859 for (i = 0; i < XVECLEN (split, 2); i++)
861 pattern = rtx_alloc (COND_EXEC);
862 XEXP (pattern, 0) = pred;
863 XEXP (pattern, 1) = XVECEXP (split, 2, i);
864 XVECEXP (split, 2, i) = pattern;
866 /* Add the new split to the queue. */
867 queue_pattern (split, &other_tail, read_rtx_filename,
868 insn_elem->split->lineno);
872 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
873 patterns appropriately. */
875 static void
876 process_define_cond_exec (void)
878 struct queue_elem *elem;
880 identify_predicable_attribute ();
881 if (errors)
882 return;
884 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
885 process_one_cond_exec (elem);
888 static char *
889 save_string (const char *s, int len)
891 char *result = XNEWVEC (char, len + 1);
893 memcpy (result, s, len);
894 result[len] = 0;
895 return result;
899 /* The entry point for initializing the reader. */
902 init_md_reader_args_cb (int argc, char **argv, bool (*parse_opt)(const char *))
904 FILE *input_file;
905 int c, i, lineno;
906 char *lastsl;
907 rtx desc;
908 bool no_more_options;
909 bool already_read_stdin;
911 /* Unlock the stdio streams. */
912 unlock_std_streams ();
914 /* First we loop over all the options. */
915 for (i = 1; i < argc; i++)
917 if (argv[i][0] != '-')
918 continue;
920 c = argv[i][1];
921 switch (c)
923 case 'I': /* Add directory to path for includes. */
925 struct file_name_list *dirtmp;
927 dirtmp = XNEW (struct file_name_list);
928 dirtmp->next = 0; /* New one goes on the end */
929 if (first_dir_md_include == 0)
930 first_dir_md_include = dirtmp;
931 else
932 last_dir_md_include->next = dirtmp;
933 last_dir_md_include = dirtmp; /* Tail follows the last one */
934 if (argv[i][1] == 'I' && argv[i][2] != 0)
935 dirtmp->fname = argv[i] + 2;
936 else if (i + 1 == argc)
937 fatal ("directory name missing after -I option");
938 else
939 dirtmp->fname = argv[++i];
940 if (strlen (dirtmp->fname) > max_include_len)
941 max_include_len = strlen (dirtmp->fname);
943 break;
945 case '\0':
946 /* An argument consisting of exactly one dash is a request to
947 read stdin. This will be handled in the second loop. */
948 continue;
950 case '-':
951 /* An argument consisting of just two dashes causes option
952 parsing to cease. */
953 if (argv[i][2] == '\0')
954 goto stop_parsing_options;
956 default:
957 /* The program may have provided a callback so it can
958 accept its own options. */
959 if (parse_opt && parse_opt (argv[i]))
960 break;
962 fatal ("invalid option `%s'", argv[i]);
966 stop_parsing_options:
968 /* Prepare to read input. */
969 condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
970 init_predicate_table ();
971 obstack_init (rtl_obstack);
972 errors = 0;
973 sequence_num = 0;
974 no_more_options = false;
975 already_read_stdin = false;
978 /* Now loop over all input files. */
979 for (i = 1; i < argc; i++)
981 if (argv[i][0] == '-')
983 if (argv[i][1] == '\0')
985 /* Read stdin. */
986 if (already_read_stdin)
987 fatal ("cannot read standard input twice");
989 base_dir = NULL;
990 read_rtx_filename = in_fname = "<stdin>";
991 read_rtx_lineno = 1;
992 input_file = stdin;
993 already_read_stdin = true;
995 while (read_rtx (input_file, &desc, &lineno))
996 process_rtx (desc, lineno);
997 fclose (input_file);
998 continue;
1000 else if (argv[i][1] == '-' && argv[i][2] == '\0')
1002 /* No further arguments are to be treated as options. */
1003 no_more_options = true;
1004 continue;
1006 else if (!no_more_options)
1007 continue;
1010 /* If we get here we are looking at a non-option argument, i.e.
1011 a file to be processed. */
1013 in_fname = argv[i];
1014 lastsl = strrchr (in_fname, '/');
1015 if (lastsl != NULL)
1016 base_dir = save_string (in_fname, lastsl - in_fname + 1 );
1017 else
1018 base_dir = NULL;
1020 read_rtx_filename = in_fname;
1021 read_rtx_lineno = 1;
1022 input_file = fopen (in_fname, "r");
1023 if (input_file == 0)
1025 perror (in_fname);
1026 return FATAL_EXIT_CODE;
1029 while (read_rtx (input_file, &desc, &lineno))
1030 process_rtx (desc, lineno);
1031 fclose (input_file);
1034 /* If we get to this point without having seen any files to process,
1035 read standard input now. */
1036 if (!in_fname)
1038 base_dir = NULL;
1039 read_rtx_filename = in_fname = "<stdin>";
1040 read_rtx_lineno = 1;
1041 input_file = stdin;
1043 while (read_rtx (input_file, &desc, &lineno))
1044 process_rtx (desc, lineno);
1045 fclose (input_file);
1048 /* Process define_cond_exec patterns. */
1049 if (define_cond_exec_queue != NULL)
1050 process_define_cond_exec ();
1052 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
1055 /* Programs that don't have their own options can use this entry point
1056 instead. */
1058 init_md_reader_args (int argc, char **argv)
1060 return init_md_reader_args_cb (argc, argv, 0);
1063 /* The entry point for reading a single rtx from an md file. */
1066 read_md_rtx (int *lineno, int *seqnr)
1068 struct queue_elem **queue, *elem;
1069 rtx desc;
1071 discard:
1073 /* Read all patterns from a given queue before moving on to the next. */
1074 if (define_attr_queue != NULL)
1075 queue = &define_attr_queue;
1076 else if (define_pred_queue != NULL)
1077 queue = &define_pred_queue;
1078 else if (define_insn_queue != NULL)
1079 queue = &define_insn_queue;
1080 else if (other_queue != NULL)
1081 queue = &other_queue;
1082 else
1083 return NULL_RTX;
1085 elem = *queue;
1086 *queue = elem->next;
1087 desc = elem->data;
1088 read_rtx_filename = elem->filename;
1089 *lineno = elem->lineno;
1090 *seqnr = sequence_num;
1092 free (elem);
1094 /* Discard insn patterns which we know can never match (because
1095 their C test is provably always false). If insn_elision is
1096 false, our caller needs to see all the patterns. Note that the
1097 elided patterns are never counted by the sequence numbering; it
1098 it is the caller's responsibility, when insn_elision is false, not
1099 to use elided pattern numbers for anything. */
1100 switch (GET_CODE (desc))
1102 case DEFINE_INSN:
1103 case DEFINE_EXPAND:
1104 if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
1105 sequence_num++;
1106 else if (insn_elision)
1107 goto discard;
1109 /* *seqnr is used here so the name table will match caller's
1110 idea of insn numbering, whether or not elision is active. */
1111 record_insn_name (*seqnr, XSTR (desc, 0));
1112 break;
1114 case DEFINE_SPLIT:
1115 case DEFINE_PEEPHOLE:
1116 case DEFINE_PEEPHOLE2:
1117 if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
1118 sequence_num++;
1119 else if (insn_elision)
1120 goto discard;
1121 break;
1123 default:
1124 break;
1127 return desc;
1130 /* Helper functions for insn elision. */
1132 /* Compute a hash function of a c_test structure, which is keyed
1133 by its ->expr field. */
1134 hashval_t
1135 hash_c_test (const void *x)
1137 const struct c_test *a = (const struct c_test *) x;
1138 const unsigned char *base, *s = (const unsigned char *) a->expr;
1139 hashval_t hash;
1140 unsigned char c;
1141 unsigned int len;
1143 base = s;
1144 hash = 0;
1146 while ((c = *s++) != '\0')
1148 hash += c + (c << 17);
1149 hash ^= hash >> 2;
1152 len = s - base;
1153 hash += len + (len << 17);
1154 hash ^= hash >> 2;
1156 return hash;
1159 /* Compare two c_test expression structures. */
1161 cmp_c_test (const void *x, const void *y)
1163 const struct c_test *a = (const struct c_test *) x;
1164 const struct c_test *b = (const struct c_test *) y;
1166 return !strcmp (a->expr, b->expr);
1169 /* Given a string representing a C test expression, look it up in the
1170 condition_table and report whether or not its value is known
1171 at compile time. Returns a tristate: 1 for known true, 0 for
1172 known false, -1 for unknown. */
1174 maybe_eval_c_test (const char *expr)
1176 const struct c_test *test;
1177 struct c_test dummy;
1179 if (expr[0] == 0)
1180 return 1;
1182 dummy.expr = expr;
1183 test = (const struct c_test *)htab_find (condition_table, &dummy);
1184 if (!test)
1185 return -1;
1186 return test->value;
1189 /* Record the C test expression EXPR in the condition_table, with
1190 value VAL. Duplicates clobber previous entries. */
1192 void
1193 add_c_test (const char *expr, int value)
1195 struct c_test *test;
1197 if (expr[0] == 0)
1198 return;
1200 test = XNEW (struct c_test);
1201 test->expr = expr;
1202 test->value = value;
1204 *(htab_find_slot (condition_table, test, INSERT)) = test;
1207 /* For every C test, call CALLBACK with two arguments: a pointer to
1208 the condition structure and INFO. Stops when CALLBACK returns zero. */
1209 void
1210 traverse_c_tests (htab_trav callback, void *info)
1212 if (condition_table)
1213 htab_traverse (condition_table, callback, info);
1217 /* Given a string, return the number of comma-separated elements in it.
1218 Return 0 for the null string. */
1220 n_comma_elts (const char *s)
1222 int n;
1224 if (*s == '\0')
1225 return 0;
1227 for (n = 1; *s; s++)
1228 if (*s == ',')
1229 n++;
1231 return n;
1234 /* Given a pointer to a (char *), return a pointer to the beginning of the
1235 next comma-separated element in the string. Advance the pointer given
1236 to the end of that element. Return NULL if at end of string. Caller
1237 is responsible for copying the string if necessary. White space between
1238 a comma and an element is ignored. */
1240 const char *
1241 scan_comma_elt (const char **pstr)
1243 const char *start;
1244 const char *p = *pstr;
1246 if (*p == ',')
1247 p++;
1248 while (ISSPACE(*p))
1249 p++;
1251 if (*p == '\0')
1252 return NULL;
1254 start = p;
1256 while (*p != ',' && *p != '\0')
1257 p++;
1259 *pstr = p;
1260 return start;
1263 /* Helper functions for define_predicate and define_special_predicate
1264 processing. Shared between genrecog.c and genpreds.c. */
1266 static htab_t predicate_table;
1267 struct pred_data *first_predicate;
1268 static struct pred_data **last_predicate = &first_predicate;
1270 static hashval_t
1271 hash_struct_pred_data (const void *ptr)
1273 return htab_hash_string (((const struct pred_data *)ptr)->name);
1276 static int
1277 eq_struct_pred_data (const void *a, const void *b)
1279 return !strcmp (((const struct pred_data *)a)->name,
1280 ((const struct pred_data *)b)->name);
1283 struct pred_data *
1284 lookup_predicate (const char *name)
1286 struct pred_data key;
1287 key.name = name;
1288 return (struct pred_data *) htab_find (predicate_table, &key);
1291 void
1292 add_predicate (struct pred_data *pred)
1294 void **slot = htab_find_slot (predicate_table, pred, INSERT);
1295 if (*slot)
1297 error ("duplicate predicate definition for '%s'", pred->name);
1298 return;
1300 *slot = pred;
1301 *last_predicate = pred;
1302 last_predicate = &pred->next;
1305 /* This array gives the initial content of the predicate table. It
1306 has entries for all predicates defined in recog.c. */
1308 struct std_pred_table
1310 const char *name;
1311 bool special;
1312 RTX_CODE codes[NUM_RTX_CODE];
1315 static const struct std_pred_table std_preds[] = {
1316 {"general_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1317 LABEL_REF, SUBREG, REG, MEM }},
1318 {"address_operand", true, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1319 LABEL_REF, SUBREG, REG, MEM,
1320 PLUS, MINUS, MULT}},
1321 {"register_operand", false, {SUBREG, REG}},
1322 {"pmode_register_operand", true, {SUBREG, REG}},
1323 {"scratch_operand", false, {SCRATCH, REG}},
1324 {"immediate_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1325 LABEL_REF}},
1326 {"const_int_operand", false, {CONST_INT}},
1327 {"const_double_operand", false, {CONST_INT, CONST_DOUBLE}},
1328 {"nonimmediate_operand", false, {SUBREG, REG, MEM}},
1329 {"nonmemory_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1330 LABEL_REF, SUBREG, REG}},
1331 {"push_operand", false, {MEM}},
1332 {"pop_operand", false, {MEM}},
1333 {"memory_operand", false, {SUBREG, MEM}},
1334 {"indirect_operand", false, {SUBREG, MEM}},
1335 {"comparison_operator", false, {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
1336 UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
1337 UNLT, LTGT}}
1339 #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
1341 /* Initialize the table of predicate definitions, starting with
1342 the information we have on generic predicates. */
1344 static void
1345 init_predicate_table (void)
1347 size_t i, j;
1348 struct pred_data *pred;
1350 predicate_table = htab_create_alloc (37, hash_struct_pred_data,
1351 eq_struct_pred_data, 0,
1352 xcalloc, free);
1354 for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
1356 pred = XCNEW (struct pred_data);
1357 pred->name = std_preds[i].name;
1358 pred->special = std_preds[i].special;
1360 for (j = 0; std_preds[i].codes[j] != 0; j++)
1362 enum rtx_code code = std_preds[i].codes[j];
1364 pred->codes[code] = true;
1365 if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
1366 pred->allows_non_const = true;
1367 if (code != REG
1368 && code != SUBREG
1369 && code != MEM
1370 && code != CONCAT
1371 && code != PARALLEL
1372 && code != STRICT_LOW_PART)
1373 pred->allows_non_lvalue = true;
1375 if (j == 1)
1376 pred->singleton = std_preds[i].codes[0];
1378 add_predicate (pred);
1382 /* These functions allow linkage with print-rtl.c. Also, some generators
1383 like to annotate their output with insn names. */
1385 /* Holds an array of names indexed by insn_code_number. */
1386 static char **insn_name_ptr = 0;
1387 static int insn_name_ptr_size = 0;
1389 const char *
1390 get_insn_name (int code)
1392 if (code < insn_name_ptr_size)
1393 return insn_name_ptr[code];
1394 else
1395 return NULL;
1398 static void
1399 record_insn_name (int code, const char *name)
1401 static const char *last_real_name = "insn";
1402 static int last_real_code = 0;
1403 char *new;
1405 if (insn_name_ptr_size <= code)
1407 int new_size;
1408 new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
1409 insn_name_ptr = xrealloc (insn_name_ptr, sizeof(char *) * new_size);
1410 memset (insn_name_ptr + insn_name_ptr_size, 0,
1411 sizeof(char *) * (new_size - insn_name_ptr_size));
1412 insn_name_ptr_size = new_size;
1415 if (!name || name[0] == '\0')
1417 new = xmalloc (strlen (last_real_name) + 10);
1418 sprintf (new, "%s+%d", last_real_name, code - last_real_code);
1420 else
1422 last_real_name = new = xstrdup (name);
1423 last_real_code = code;
1426 insn_name_ptr[code] = new;