OSDN Git Service

* tree-ssa-threadupdate.c (rediscover_loops_after_threading):
[pf3gnuchains/gcc-fork.git] / gcc / gensupport.c
1 /* Support routines for the various generation passes.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
31
32
33 /* In case some macros used by files we include need it, define this here.  */
34 int target_flags;
35
36 int insn_elision = 1;
37
38 const char *in_fname;
39
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 *);
43
44 static struct obstack obstack;
45 struct obstack *rtl_obstack = &obstack;
46
47 static int sequence_num;
48 static int errors;
49
50 static int predicable_default;
51 static const char *predicable_true;
52 static const char *predicable_false;
53
54 static htab_t condition_table;
55
56 static char *base_dir = NULL;
57
58 /* We initially queue all patterns, process the define_insn and
59    define_cond_exec patterns, then return them one at a time.  */
60
61 struct queue_elem
62 {
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;
70 };
71
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;
82
83 static struct queue_elem *queue_pattern (rtx, struct queue_elem ***,
84                                          const char *, int);
85
86 /* Current maximum length of directory names in the search path
87    for include files.  (Altered as we get more of them.)  */
88
89 size_t max_include_len;
90
91 struct file_name_list
92   {
93     struct file_name_list *next;
94     const char *fname;
95   };
96
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 */
101
102 static void remove_constraints (rtx);
103 static void process_rtx (rtx, int);
104
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 *);
122 \f
123 void
124 message_with_line (int lineno, const char *msg, ...)
125 {
126   va_list ap;
127
128   va_start (ap, msg);
129
130   fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
131   vfprintf (stderr, msg, ap);
132   fputc ('\n', stderr);
133
134   va_end (ap);
135 }
136
137 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
138    the gensupport programs.  */
139
140 rtx
141 gen_rtx_CONST_INT (enum machine_mode ARG_UNUSED (mode),
142                    HOST_WIDE_INT arg)
143 {
144   rtx rt = rtx_alloc (CONST_INT);
145
146   XWINT (rt, 0) = arg;
147   return rt;
148 }
149 \f
150 /* Queue PATTERN on LIST_TAIL.  Return the address of the new queue
151    element.  */
152
153 static struct queue_elem *
154 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
155                const char *filename, int lineno)
156 {
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;
166 }
167
168 /* Recursively remove constraints from an rtx.  */
169
170 static void
171 remove_constraints (rtx part)
172 {
173   int i, j;
174   const char *format_ptr;
175
176   if (part == 0)
177     return;
178
179   if (GET_CODE (part) == MATCH_OPERAND)
180     XSTR (part, 2) = "";
181   else if (GET_CODE (part) == MATCH_SCRATCH)
182     XSTR (part, 1) = "";
183
184   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
185
186   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
187     switch (*format_ptr++)
188       {
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;
198       }
199 }
200
201 /* Process an include file assuming that it lives in gcc/config/{target}/
202    if the include looks like (include "file").  */
203
204 static void
205 process_include (rtx desc, int lineno)
206 {
207   const char *filename = XSTR (desc, 0);
208   const char *old_filename;
209   int old_lineno;
210   char *pathname;
211   FILE *input_file;
212
213   /* If specified file name is absolute, skip the include stack.  */
214   if (! IS_ABSOLUTE_PATH (filename))
215     {
216       struct file_name_list *stackp;
217
218       /* Search directory path, trying to open the file.  */
219       for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
220         {
221           static const char sep[2] = { DIR_SEPARATOR, '\0' };
222
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);
228         }
229     }
230
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)
237     {
238       free (pathname);
239       message_with_line (lineno, "include file `%s' not found", filename);
240       errors = 1;
241       return;
242     }
243  success:
244
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;
252
253   if (include_callback)
254     include_callback (pathname);
255
256   /* Read the entire file.  */
257   while (read_rtx (input_file, &desc, &lineno))
258     process_rtx (desc, lineno);
259
260   /* Do not free pathname.  It is attached to the various rtx queue
261      elements.  */
262
263   read_rtx_filename = old_filename;
264   read_rtx_lineno = old_lineno;
265
266   fclose (input_file);
267 }
268
269 /* Process a top level rtx in some way, queuing as appropriate.  */
270
271 static void
272 process_rtx (rtx desc, int lineno)
273 {
274   switch (GET_CODE (desc))
275     {
276     case DEFINE_INSN:
277       queue_pattern (desc, &define_insn_tail, read_rtx_filename, lineno);
278       break;
279
280     case DEFINE_COND_EXEC:
281       queue_pattern (desc, &define_cond_exec_tail, read_rtx_filename, lineno);
282       break;
283
284     case DEFINE_ATTR:
285       queue_pattern (desc, &define_attr_tail, read_rtx_filename, lineno);
286       break;
287
288     case DEFINE_PREDICATE:
289     case DEFINE_SPECIAL_PREDICATE:
290     case DEFINE_CONSTRAINT:
291     case DEFINE_REGISTER_CONSTRAINT:
292     case DEFINE_MEMORY_CONSTRAINT:
293     case DEFINE_ADDRESS_CONSTRAINT:
294       queue_pattern (desc, &define_pred_tail, read_rtx_filename, lineno);
295       break;
296
297     case INCLUDE:
298       process_include (desc, lineno);
299       break;
300
301     case DEFINE_INSN_AND_SPLIT:
302       {
303         const char *split_cond;
304         rtx split;
305         rtvec attr;
306         int i;
307         struct queue_elem *insn_elem;
308         struct queue_elem *split_elem;
309
310         /* Create a split with values from the insn_and_split.  */
311         split = rtx_alloc (DEFINE_SPLIT);
312
313         i = XVECLEN (desc, 1);
314         XVEC (split, 0) = rtvec_alloc (i);
315         while (--i >= 0)
316           {
317             XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
318             remove_constraints (XVECEXP (split, 0, i));
319           }
320
321         /* If the split condition starts with "&&", append it to the
322            insn condition to create the new split condition.  */
323         split_cond = XSTR (desc, 4);
324         if (split_cond[0] == '&' && split_cond[1] == '&')
325           {
326             copy_rtx_ptr_loc (split_cond + 2, split_cond);
327             split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
328           }
329         XSTR (split, 1) = split_cond;
330         XVEC (split, 2) = XVEC (desc, 5);
331         XSTR (split, 3) = XSTR (desc, 6);
332
333         /* Fix up the DEFINE_INSN.  */
334         attr = XVEC (desc, 7);
335         PUT_CODE (desc, DEFINE_INSN);
336         XVEC (desc, 4) = attr;
337
338         /* Queue them.  */
339         insn_elem
340           = queue_pattern (desc, &define_insn_tail, read_rtx_filename, 
341                            lineno);
342         split_elem
343           = queue_pattern (split, &other_tail, read_rtx_filename, lineno);
344         insn_elem->split = split_elem;
345         break;
346       }
347
348     default:
349       queue_pattern (desc, &other_tail, read_rtx_filename, lineno);
350       break;
351     }
352 }
353 \f
354 /* Return true if attribute PREDICABLE is true for ELEM, which holds
355    a DEFINE_INSN.  */
356
357 static int
358 is_predicable (struct queue_elem *elem)
359 {
360   rtvec vec = XVEC (elem->data, 4);
361   const char *value;
362   int i;
363
364   if (! vec)
365     return predicable_default;
366
367   for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
368     {
369       rtx sub = RTVEC_ELT (vec, i);
370       switch (GET_CODE (sub))
371         {
372         case SET_ATTR:
373           if (strcmp (XSTR (sub, 0), "predicable") == 0)
374             {
375               value = XSTR (sub, 1);
376               goto found;
377             }
378           break;
379
380         case SET_ATTR_ALTERNATIVE:
381           if (strcmp (XSTR (sub, 0), "predicable") == 0)
382             {
383               message_with_line (elem->lineno,
384                                  "multiple alternatives for `predicable'");
385               errors = 1;
386               return 0;
387             }
388           break;
389
390         case SET:
391           if (GET_CODE (SET_DEST (sub)) != ATTR
392               || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
393             break;
394           sub = SET_SRC (sub);
395           if (GET_CODE (sub) == CONST_STRING)
396             {
397               value = XSTR (sub, 0);
398               goto found;
399             }
400
401           /* ??? It would be possible to handle this if we really tried.
402              It's not easy though, and I'm not going to bother until it
403              really proves necessary.  */
404           message_with_line (elem->lineno,
405                              "non-constant value for `predicable'");
406           errors = 1;
407           return 0;
408
409         default:
410           gcc_unreachable ();
411         }
412     }
413
414   return predicable_default;
415
416  found:
417   /* Verify that predicability does not vary on the alternative.  */
418   /* ??? It should be possible to handle this by simply eliminating
419      the non-predicable alternatives from the insn.  FRV would like
420      to do this.  Delay this until we've got the basics solid.  */
421   if (strchr (value, ',') != NULL)
422     {
423       message_with_line (elem->lineno,
424                          "multiple alternatives for `predicable'");
425       errors = 1;
426       return 0;
427     }
428
429   /* Find out which value we're looking at.  */
430   if (strcmp (value, predicable_true) == 0)
431     return 1;
432   if (strcmp (value, predicable_false) == 0)
433     return 0;
434
435   message_with_line (elem->lineno,
436                      "unknown value `%s' for `predicable' attribute",
437                      value);
438   errors = 1;
439   return 0;
440 }
441
442 /* Examine the attribute "predicable"; discover its boolean values
443    and its default.  */
444
445 static void
446 identify_predicable_attribute (void)
447 {
448   struct queue_elem *elem;
449   char *p_true, *p_false;
450   const char *value;
451
452   /* Look for the DEFINE_ATTR for `predicable', which must exist.  */
453   for (elem = define_attr_queue; elem ; elem = elem->next)
454     if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
455       goto found;
456
457   message_with_line (define_cond_exec_queue->lineno,
458                      "attribute `predicable' not defined");
459   errors = 1;
460   return;
461
462  found:
463   value = XSTR (elem->data, 1);
464   p_false = xstrdup (value);
465   p_true = strchr (p_false, ',');
466   if (p_true == NULL || strchr (++p_true, ',') != NULL)
467     {
468       message_with_line (elem->lineno,
469                          "attribute `predicable' is not a boolean");
470       errors = 1;
471       return;
472     }
473   p_true[-1] = '\0';
474
475   predicable_true = p_true;
476   predicable_false = p_false;
477
478   switch (GET_CODE (XEXP (elem->data, 2)))
479     {
480     case CONST_STRING:
481       value = XSTR (XEXP (elem->data, 2), 0);
482       break;
483
484     case CONST:
485       message_with_line (elem->lineno,
486                          "attribute `predicable' cannot be const");
487       errors = 1;
488       return;
489
490     default:
491       message_with_line (elem->lineno,
492                          "attribute `predicable' must have a constant default");
493       errors = 1;
494       return;
495     }
496
497   if (strcmp (value, p_true) == 0)
498     predicable_default = 1;
499   else if (strcmp (value, p_false) == 0)
500     predicable_default = 0;
501   else
502     {
503       message_with_line (elem->lineno,
504                          "unknown value `%s' for `predicable' attribute",
505                          value);
506       errors = 1;
507     }
508 }
509
510 /* Return the number of alternatives in constraint S.  */
511
512 static int
513 n_alternatives (const char *s)
514 {
515   int n = 1;
516
517   if (s)
518     while (*s)
519       n += (*s++ == ',');
520
521   return n;
522 }
523
524 /* Determine how many alternatives there are in INSN, and how many
525    operands.  */
526
527 static void
528 collect_insn_data (rtx pattern, int *palt, int *pmax)
529 {
530   const char *fmt;
531   enum rtx_code code;
532   int i, j, len;
533
534   code = GET_CODE (pattern);
535   switch (code)
536     {
537     case MATCH_OPERAND:
538       i = n_alternatives (XSTR (pattern, 2));
539       *palt = (i > *palt ? i : *palt);
540       /* Fall through.  */
541
542     case MATCH_OPERATOR:
543     case MATCH_SCRATCH:
544     case MATCH_PARALLEL:
545       i = XINT (pattern, 0);
546       if (i > *pmax)
547         *pmax = i;
548       break;
549
550     default:
551       break;
552     }
553
554   fmt = GET_RTX_FORMAT (code);
555   len = GET_RTX_LENGTH (code);
556   for (i = 0; i < len; i++)
557     {
558       switch (fmt[i])
559         {
560         case 'e': case 'u':
561           collect_insn_data (XEXP (pattern, i), palt, pmax);
562           break;
563
564         case 'V':
565           if (XVEC (pattern, i) == NULL)
566             break;
567           /* Fall through.  */
568         case 'E':
569           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
570             collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
571           break;
572
573         case 'i': case 'w': case '0': case 's': case 'S': case 'T':
574           break;
575
576         default:
577           gcc_unreachable ();
578         }
579     }
580 }
581
582 static rtx
583 alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
584 {
585   const char *fmt;
586   enum rtx_code code;
587   int i, j, len;
588
589   code = GET_CODE (pattern);
590   switch (code)
591     {
592     case MATCH_OPERAND:
593       {
594         const char *c = XSTR (pattern, 2);
595
596         if (n_alternatives (c) != 1)
597           {
598             message_with_line (lineno,
599                                "too many alternatives for operand %d",
600                                XINT (pattern, 0));
601             errors = 1;
602             return NULL;
603           }
604
605         /* Replicate C as needed to fill out ALT alternatives.  */
606         if (c && *c && alt > 1)
607           {
608             size_t c_len = strlen (c);
609             size_t len = alt * (c_len + 1);
610             char *new_c = XNEWVEC(char, len);
611
612             memcpy (new_c, c, c_len);
613             for (i = 1; i < alt; ++i)
614               {
615                 new_c[i * (c_len + 1) - 1] = ',';
616                 memcpy (&new_c[i * (c_len + 1)], c, c_len);
617               }
618             new_c[len - 1] = '\0';
619             XSTR (pattern, 2) = new_c;
620           }
621       }
622       /* Fall through.  */
623
624     case MATCH_OPERATOR:
625     case MATCH_SCRATCH:
626     case MATCH_PARALLEL:
627       XINT (pattern, 0) += max_op;
628       break;
629
630     default:
631       break;
632     }
633
634   fmt = GET_RTX_FORMAT (code);
635   len = GET_RTX_LENGTH (code);
636   for (i = 0; i < len; i++)
637     {
638       rtx r;
639
640       switch (fmt[i])
641         {
642         case 'e': case 'u':
643           r = alter_predicate_for_insn (XEXP (pattern, i), alt,
644                                         max_op, lineno);
645           if (r == NULL)
646             return r;
647           break;
648
649         case 'E':
650           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
651             {
652               r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
653                                             alt, max_op, lineno);
654               if (r == NULL)
655                 return r;
656             }
657           break;
658
659         case 'i': case 'w': case '0': case 's':
660           break;
661
662         default:
663           gcc_unreachable ();
664         }
665     }
666
667   return pattern;
668 }
669
670 static const char *
671 alter_test_for_insn (struct queue_elem *ce_elem,
672                      struct queue_elem *insn_elem)
673 {
674   return join_c_conditions (XSTR (ce_elem->data, 1),
675                             XSTR (insn_elem->data, 2));
676 }
677
678 /* Adjust all of the operand numbers in SRC to match the shift they'll
679    get from an operand displacement of DISP.  Return a pointer after the
680    adjusted string.  */
681
682 static char *
683 shift_output_template (char *dest, const char *src, int disp)
684 {
685   while (*src)
686     {
687       char c = *src++;
688       *dest++ = c;
689       if (c == '%')
690         {
691           c = *src++;
692           if (ISDIGIT ((unsigned char) c))
693             c += disp;
694           else if (ISALPHA (c))
695             {
696               *dest++ = c;
697               c = *src++ + disp;
698             }
699           *dest++ = c;
700         }
701     }
702
703   return dest;
704 }
705
706 static const char *
707 alter_output_for_insn (struct queue_elem *ce_elem,
708                        struct queue_elem *insn_elem,
709                        int alt, int max_op)
710 {
711   const char *ce_out, *insn_out;
712   char *result, *p;
713   size_t len, ce_len, insn_len;
714
715   /* ??? Could coordinate with genoutput to not duplicate code here.  */
716
717   ce_out = XSTR (ce_elem->data, 2);
718   insn_out = XTMPL (insn_elem->data, 3);
719   if (!ce_out || *ce_out == '\0')
720     return insn_out;
721
722   ce_len = strlen (ce_out);
723   insn_len = strlen (insn_out);
724
725   if (*insn_out == '*')
726     /* You must take care of the predicate yourself.  */
727     return insn_out;
728
729   if (*insn_out == '@')
730     {
731       len = (ce_len + 1) * alt + insn_len + 1;
732       p = result = XNEWVEC(char, len);
733
734       do
735         {
736           do
737             *p++ = *insn_out++;
738           while (ISSPACE ((unsigned char) *insn_out));
739
740           if (*insn_out != '#')
741             {
742               p = shift_output_template (p, ce_out, max_op);
743               *p++ = ' ';
744             }
745
746           do
747             *p++ = *insn_out++;
748           while (*insn_out && *insn_out != '\n');
749         }
750       while (*insn_out);
751       *p = '\0';
752     }
753   else
754     {
755       len = ce_len + 1 + insn_len + 1;
756       result = XNEWVEC (char, len);
757
758       p = shift_output_template (result, ce_out, max_op);
759       *p++ = ' ';
760       memcpy (p, insn_out, insn_len + 1);
761     }
762
763   return result;
764 }
765
766 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC.  */
767
768 static void
769 process_one_cond_exec (struct queue_elem *ce_elem)
770 {
771   struct queue_elem *insn_elem;
772   for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
773     {
774       int alternatives, max_operand;
775       rtx pred, insn, pattern, split;
776       int i;
777
778       if (! is_predicable (insn_elem))
779         continue;
780
781       alternatives = 1;
782       max_operand = -1;
783       collect_insn_data (insn_elem->data, &alternatives, &max_operand);
784       max_operand += 1;
785
786       if (XVECLEN (ce_elem->data, 0) != 1)
787         {
788           message_with_line (ce_elem->lineno,
789                              "too many patterns in predicate");
790           errors = 1;
791           return;
792         }
793
794       pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
795       pred = alter_predicate_for_insn (pred, alternatives, max_operand,
796                                        ce_elem->lineno);
797       if (pred == NULL)
798         return;
799
800       /* Construct a new pattern for the new insn.  */
801       insn = copy_rtx (insn_elem->data);
802       XSTR (insn, 0) = "";
803       pattern = rtx_alloc (COND_EXEC);
804       XEXP (pattern, 0) = pred;
805       if (XVECLEN (insn, 1) == 1)
806         {
807           XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
808           XVECEXP (insn, 1, 0) = pattern;
809           PUT_NUM_ELEM (XVEC (insn, 1), 1);
810         }
811       else
812         {
813           XEXP (pattern, 1) = rtx_alloc (PARALLEL);
814           XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
815           XVEC (insn, 1) = rtvec_alloc (1);
816           XVECEXP (insn, 1, 0) = pattern;
817         }
818
819       XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
820       XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
821                                               alternatives, max_operand);
822
823       /* ??? Set `predicable' to false.  Not crucial since it's really
824          only used here, and we won't reprocess this new pattern.  */
825
826       /* Put the new pattern on the `other' list so that it
827          (a) is not reprocessed by other define_cond_exec patterns
828          (b) appears after all normal define_insn patterns.
829
830          ??? B is debatable.  If one has normal insns that match
831          cond_exec patterns, they will be preferred over these
832          generated patterns.  Whether this matters in practice, or if
833          it's a good thing, or whether we should thread these new
834          patterns into the define_insn chain just after their generator
835          is something we'll have to experiment with.  */
836
837       queue_pattern (insn, &other_tail, insn_elem->filename,
838                      insn_elem->lineno);
839
840       if (!insn_elem->split)
841         continue;
842
843       /* If the original insn came from a define_insn_and_split,
844          generate a new split to handle the predicated insn.  */
845       split = copy_rtx (insn_elem->split->data);
846       /* Predicate the pattern matched by the split.  */
847       pattern = rtx_alloc (COND_EXEC);
848       XEXP (pattern, 0) = pred;
849       if (XVECLEN (split, 0) == 1)
850         {
851           XEXP (pattern, 1) = XVECEXP (split, 0, 0);
852           XVECEXP (split, 0, 0) = pattern;
853           PUT_NUM_ELEM (XVEC (split, 0), 1);
854         }
855       else
856         {
857           XEXP (pattern, 1) = rtx_alloc (PARALLEL);
858           XVEC (XEXP (pattern, 1), 0) = XVEC (split, 0);
859           XVEC (split, 0) = rtvec_alloc (1);
860           XVECEXP (split, 0, 0) = pattern;
861         }
862       /* Predicate all of the insns generated by the split.  */
863       for (i = 0; i < XVECLEN (split, 2); i++)
864         {
865           pattern = rtx_alloc (COND_EXEC);
866           XEXP (pattern, 0) = pred;
867           XEXP (pattern, 1) = XVECEXP (split, 2, i);
868           XVECEXP (split, 2, i) = pattern;
869         }
870       /* Add the new split to the queue.  */
871       queue_pattern (split, &other_tail, read_rtx_filename, 
872                      insn_elem->split->lineno);
873     }
874 }
875
876 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
877    patterns appropriately.  */
878
879 static void
880 process_define_cond_exec (void)
881 {
882   struct queue_elem *elem;
883
884   identify_predicable_attribute ();
885   if (errors)
886     return;
887
888   for (elem = define_cond_exec_queue; elem ; elem = elem->next)
889     process_one_cond_exec (elem);
890 }
891
892 static char *
893 save_string (const char *s, int len)
894 {
895   char *result = XNEWVEC (char, len + 1);
896
897   memcpy (result, s, len);
898   result[len] = 0;
899   return result;
900 }
901
902 \f
903 /* The entry point for initializing the reader.  */
904
905 int
906 init_md_reader_args_cb (int argc, char **argv, bool (*parse_opt)(const char *))
907 {
908   FILE *input_file;
909   int c, i, lineno;
910   char *lastsl;
911   rtx desc;
912   bool no_more_options;
913   bool already_read_stdin;
914
915   /* Unlock the stdio streams.  */
916   unlock_std_streams ();
917
918   /* First we loop over all the options.  */
919   for (i = 1; i < argc; i++)
920     {
921       if (argv[i][0] != '-')
922         continue;
923       
924       c = argv[i][1];
925       switch (c)
926         {
927         case 'I':               /* Add directory to path for includes.  */
928           {
929             struct file_name_list *dirtmp;
930
931             dirtmp = XNEW (struct file_name_list);
932             dirtmp->next = 0;   /* New one goes on the end */
933             if (first_dir_md_include == 0)
934               first_dir_md_include = dirtmp;
935             else
936               last_dir_md_include->next = dirtmp;
937             last_dir_md_include = dirtmp;       /* Tail follows the last one */
938             if (argv[i][1] == 'I' && argv[i][2] != 0)
939               dirtmp->fname = argv[i] + 2;
940             else if (i + 1 == argc)
941               fatal ("directory name missing after -I option");
942             else
943               dirtmp->fname = argv[++i];
944             if (strlen (dirtmp->fname) > max_include_len)
945               max_include_len = strlen (dirtmp->fname);
946           }
947           break;
948
949         case '\0':
950           /* An argument consisting of exactly one dash is a request to
951              read stdin.  This will be handled in the second loop.  */
952           continue;
953
954         case '-':
955           /* An argument consisting of just two dashes causes option
956              parsing to cease.  */
957           if (argv[i][2] == '\0')
958             goto stop_parsing_options;
959
960         default:
961           /* The program may have provided a callback so it can
962              accept its own options.  */
963           if (parse_opt && parse_opt (argv[i]))
964             break;
965
966           fatal ("invalid option `%s'", argv[i]);
967         }
968     }
969
970  stop_parsing_options:
971
972   /* Prepare to read input.  */
973   condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
974   init_predicate_table ();
975   obstack_init (rtl_obstack);
976   errors = 0;
977   sequence_num = 0;
978   no_more_options = false;
979   already_read_stdin = false;
980
981
982   /* Now loop over all input files.  */
983   for (i = 1; i < argc; i++)
984     {
985       if (argv[i][0] == '-')
986         {
987           if (argv[i][1] == '\0')
988             {
989               /* Read stdin.  */
990               if (already_read_stdin)
991                 fatal ("cannot read standard input twice");
992               
993               base_dir = NULL;
994               read_rtx_filename = in_fname = "<stdin>";
995               read_rtx_lineno = 1;
996               input_file = stdin;
997               already_read_stdin = true;
998
999               while (read_rtx (input_file, &desc, &lineno))
1000                 process_rtx (desc, lineno);
1001               fclose (input_file);
1002               continue;
1003             }
1004           else if (argv[i][1] == '-' && argv[i][2] == '\0')
1005             {
1006               /* No further arguments are to be treated as options.  */
1007               no_more_options = true;
1008               continue;
1009             }
1010           else if (!no_more_options)
1011             continue;
1012         }
1013
1014       /* If we get here we are looking at a non-option argument, i.e.
1015          a file to be processed.  */
1016
1017       in_fname = argv[i];
1018       lastsl = strrchr (in_fname, '/');
1019       if (lastsl != NULL)
1020         base_dir = save_string (in_fname, lastsl - in_fname + 1 );
1021       else
1022         base_dir = NULL;
1023
1024       read_rtx_filename = in_fname;
1025       read_rtx_lineno = 1;
1026       input_file = fopen (in_fname, "r");
1027       if (input_file == 0)
1028         {
1029           perror (in_fname);
1030           return FATAL_EXIT_CODE;
1031         }
1032
1033       while (read_rtx (input_file, &desc, &lineno))
1034         process_rtx (desc, lineno);
1035       fclose (input_file);
1036     }
1037
1038   /* If we get to this point without having seen any files to process,
1039      read standard input now.  */
1040   if (!in_fname)
1041     {
1042       base_dir = NULL;
1043       read_rtx_filename = in_fname = "<stdin>";
1044       read_rtx_lineno = 1;
1045       input_file = stdin;
1046
1047       while (read_rtx (input_file, &desc, &lineno))
1048         process_rtx (desc, lineno);
1049       fclose (input_file);
1050     }
1051
1052   /* Process define_cond_exec patterns.  */
1053   if (define_cond_exec_queue != NULL)
1054     process_define_cond_exec ();
1055
1056   return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
1057 }
1058
1059 /* Programs that don't have their own options can use this entry point
1060    instead.  */
1061 int
1062 init_md_reader_args (int argc, char **argv)
1063 {
1064   return init_md_reader_args_cb (argc, argv, 0);
1065 }
1066 \f
1067 /* The entry point for reading a single rtx from an md file.  */
1068
1069 rtx
1070 read_md_rtx (int *lineno, int *seqnr)
1071 {
1072   struct queue_elem **queue, *elem;
1073   rtx desc;
1074
1075  discard:
1076
1077   /* Read all patterns from a given queue before moving on to the next.  */
1078   if (define_attr_queue != NULL)
1079     queue = &define_attr_queue;
1080   else if (define_pred_queue != NULL)
1081     queue = &define_pred_queue;
1082   else if (define_insn_queue != NULL)
1083     queue = &define_insn_queue;
1084   else if (other_queue != NULL)
1085     queue = &other_queue;
1086   else
1087     return NULL_RTX;
1088
1089   elem = *queue;
1090   *queue = elem->next;
1091   desc = elem->data;
1092   read_rtx_filename = elem->filename;
1093   *lineno = elem->lineno;
1094   *seqnr = sequence_num;
1095
1096   free (elem);
1097
1098   /* Discard insn patterns which we know can never match (because
1099      their C test is provably always false).  If insn_elision is
1100      false, our caller needs to see all the patterns.  Note that the
1101      elided patterns are never counted by the sequence numbering; it
1102      it is the caller's responsibility, when insn_elision is false, not
1103      to use elided pattern numbers for anything.  */
1104   switch (GET_CODE (desc))
1105     {
1106     case DEFINE_INSN:
1107     case DEFINE_EXPAND:
1108       if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
1109         sequence_num++;
1110       else if (insn_elision)
1111         goto discard;
1112
1113       /* *seqnr is used here so the name table will match caller's
1114          idea of insn numbering, whether or not elision is active.  */
1115       record_insn_name (*seqnr, XSTR (desc, 0));
1116       break;
1117
1118     case DEFINE_SPLIT:
1119     case DEFINE_PEEPHOLE:
1120     case DEFINE_PEEPHOLE2:
1121       if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
1122         sequence_num++;
1123       else if (insn_elision)
1124             goto discard;
1125       break;
1126
1127     default:
1128       break;
1129     }
1130
1131   return desc;
1132 }
1133
1134 /* Helper functions for insn elision.  */
1135
1136 /* Compute a hash function of a c_test structure, which is keyed
1137    by its ->expr field.  */
1138 hashval_t
1139 hash_c_test (const void *x)
1140 {
1141   const struct c_test *a = (const struct c_test *) x;
1142   const unsigned char *base, *s = (const unsigned char *) a->expr;
1143   hashval_t hash;
1144   unsigned char c;
1145   unsigned int len;
1146
1147   base = s;
1148   hash = 0;
1149
1150   while ((c = *s++) != '\0')
1151     {
1152       hash += c + (c << 17);
1153       hash ^= hash >> 2;
1154     }
1155
1156   len = s - base;
1157   hash += len + (len << 17);
1158   hash ^= hash >> 2;
1159
1160   return hash;
1161 }
1162
1163 /* Compare two c_test expression structures.  */
1164 int
1165 cmp_c_test (const void *x, const void *y)
1166 {
1167   const struct c_test *a = (const struct c_test *) x;
1168   const struct c_test *b = (const struct c_test *) y;
1169
1170   return !strcmp (a->expr, b->expr);
1171 }
1172
1173 /* Given a string representing a C test expression, look it up in the
1174    condition_table and report whether or not its value is known
1175    at compile time.  Returns a tristate: 1 for known true, 0 for
1176    known false, -1 for unknown.  */
1177 int
1178 maybe_eval_c_test (const char *expr)
1179 {
1180   const struct c_test *test;
1181   struct c_test dummy;
1182
1183   if (expr[0] == 0)
1184     return 1;
1185
1186   dummy.expr = expr;
1187   test = (const struct c_test *)htab_find (condition_table, &dummy);
1188   if (!test)
1189     return -1;
1190   return test->value;
1191 }
1192
1193 /* Record the C test expression EXPR in the condition_table, with
1194    value VAL.  Duplicates clobber previous entries.  */
1195
1196 void
1197 add_c_test (const char *expr, int value)
1198 {
1199   struct c_test *test;
1200
1201   if (expr[0] == 0)
1202     return;
1203
1204   test = XNEW (struct c_test);
1205   test->expr = expr;
1206   test->value = value;
1207
1208   *(htab_find_slot (condition_table, test, INSERT)) = test;
1209 }
1210
1211 /* For every C test, call CALLBACK with two arguments: a pointer to
1212    the condition structure and INFO.  Stops when CALLBACK returns zero.  */
1213 void
1214 traverse_c_tests (htab_trav callback, void *info)
1215 {
1216   if (condition_table)
1217     htab_traverse (condition_table, callback, info);
1218 }
1219
1220
1221 /* Given a string, return the number of comma-separated elements in it.
1222    Return 0 for the null string.  */
1223 int
1224 n_comma_elts (const char *s)
1225 {
1226   int n;
1227
1228   if (*s == '\0')
1229     return 0;
1230
1231   for (n = 1; *s; s++)
1232     if (*s == ',')
1233       n++;
1234
1235   return n;
1236 }
1237
1238 /* Given a pointer to a (char *), return a pointer to the beginning of the
1239    next comma-separated element in the string.  Advance the pointer given
1240    to the end of that element.  Return NULL if at end of string.  Caller
1241    is responsible for copying the string if necessary.  White space between
1242    a comma and an element is ignored.  */
1243
1244 const char *
1245 scan_comma_elt (const char **pstr)
1246 {
1247   const char *start;
1248   const char *p = *pstr;
1249
1250   if (*p == ',')
1251     p++;
1252   while (ISSPACE(*p))
1253     p++;
1254
1255   if (*p == '\0')
1256     return NULL;
1257
1258   start = p;
1259
1260   while (*p != ',' && *p != '\0')
1261     p++;
1262
1263   *pstr = p;
1264   return start;
1265 }
1266
1267 /* Helper functions for define_predicate and define_special_predicate
1268    processing.  Shared between genrecog.c and genpreds.c.  */
1269
1270 static htab_t predicate_table;
1271 struct pred_data *first_predicate;
1272 static struct pred_data **last_predicate = &first_predicate;
1273
1274 static hashval_t
1275 hash_struct_pred_data (const void *ptr)
1276 {
1277   return htab_hash_string (((const struct pred_data *)ptr)->name);
1278 }
1279
1280 static int
1281 eq_struct_pred_data (const void *a, const void *b)
1282 {
1283   return !strcmp (((const struct pred_data *)a)->name,
1284                   ((const struct pred_data *)b)->name);
1285 }
1286
1287 struct pred_data *
1288 lookup_predicate (const char *name)
1289 {
1290   struct pred_data key;
1291   key.name = name;
1292   return (struct pred_data *) htab_find (predicate_table, &key);
1293 }
1294
1295 void
1296 add_predicate (struct pred_data *pred)
1297 {
1298   void **slot = htab_find_slot (predicate_table, pred, INSERT);
1299   if (*slot)
1300     {
1301       error ("duplicate predicate definition for '%s'", pred->name);
1302       return;
1303     }
1304   *slot = pred;
1305   *last_predicate = pred;
1306   last_predicate = &pred->next;
1307 }
1308
1309 /* This array gives the initial content of the predicate table.  It
1310    has entries for all predicates defined in recog.c.  */
1311
1312 struct std_pred_table
1313 {
1314   const char *name;
1315   bool special;
1316   RTX_CODE codes[NUM_RTX_CODE];
1317 };
1318
1319 static const struct std_pred_table std_preds[] = {
1320   {"general_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1321                               LABEL_REF, SUBREG, REG, MEM }},
1322   {"address_operand", true, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1323                              LABEL_REF, SUBREG, REG, MEM,
1324                              PLUS, MINUS, MULT}},
1325   {"register_operand", false, {SUBREG, REG}},
1326   {"pmode_register_operand", true, {SUBREG, REG}},
1327   {"scratch_operand", false, {SCRATCH, REG}},
1328   {"immediate_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1329                                 LABEL_REF}},
1330   {"const_int_operand", false, {CONST_INT}},
1331   {"const_double_operand", false, {CONST_INT, CONST_DOUBLE}},
1332   {"nonimmediate_operand", false, {SUBREG, REG, MEM}},
1333   {"nonmemory_operand", false, {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1334                                 LABEL_REF, SUBREG, REG}},
1335   {"push_operand", false, {MEM}},
1336   {"pop_operand", false, {MEM}},
1337   {"memory_operand", false, {SUBREG, MEM}},
1338   {"indirect_operand", false, {SUBREG, MEM}},
1339   {"comparison_operator", false, {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
1340                                   UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
1341                                   UNLT, LTGT}}
1342 };
1343 #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
1344
1345 /* Initialize the table of predicate definitions, starting with
1346    the information we have on generic predicates.  */
1347
1348 static void
1349 init_predicate_table (void)
1350 {
1351   size_t i, j;
1352   struct pred_data *pred;
1353
1354   predicate_table = htab_create_alloc (37, hash_struct_pred_data,
1355                                        eq_struct_pred_data, 0,
1356                                        xcalloc, free);
1357
1358   for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
1359     {
1360       pred = XCNEW (struct pred_data);
1361       pred->name = std_preds[i].name;
1362       pred->special = std_preds[i].special;
1363
1364       for (j = 0; std_preds[i].codes[j] != 0; j++)
1365         {
1366           enum rtx_code code = std_preds[i].codes[j];
1367
1368           pred->codes[code] = true;
1369           if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
1370             pred->allows_non_const = true;
1371           if (code != REG
1372               && code != SUBREG
1373               && code != MEM
1374               && code != CONCAT
1375               && code != PARALLEL
1376               && code != STRICT_LOW_PART)
1377             pred->allows_non_lvalue = true;
1378         }
1379       if (j == 1)
1380         pred->singleton = std_preds[i].codes[0];
1381       
1382       add_predicate (pred);
1383     }
1384 }
1385 \f
1386 /* These functions allow linkage with print-rtl.c.  Also, some generators
1387    like to annotate their output with insn names.  */
1388
1389 /* Holds an array of names indexed by insn_code_number.  */
1390 static char **insn_name_ptr = 0;
1391 static int insn_name_ptr_size = 0;
1392
1393 const char *
1394 get_insn_name (int code)
1395 {
1396   if (code < insn_name_ptr_size)
1397     return insn_name_ptr[code];
1398   else
1399     return NULL;
1400 }
1401
1402 static void
1403 record_insn_name (int code, const char *name)
1404 {
1405   static const char *last_real_name = "insn";
1406   static int last_real_code = 0;
1407   char *new;
1408
1409   if (insn_name_ptr_size <= code)
1410     {
1411       int new_size;
1412       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
1413       insn_name_ptr = xrealloc (insn_name_ptr, sizeof(char *) * new_size);
1414       memset (insn_name_ptr + insn_name_ptr_size, 0,
1415               sizeof(char *) * (new_size - insn_name_ptr_size));
1416       insn_name_ptr_size = new_size;
1417     }
1418
1419   if (!name || name[0] == '\0')
1420     {
1421       new = xmalloc (strlen (last_real_name) + 10);
1422       sprintf (new, "%s+%d", last_real_name, code - last_real_code);
1423     }
1424   else
1425     {
1426       last_real_name = new = xstrdup (name);
1427       last_real_code = code;
1428     }
1429
1430   insn_name_ptr[code] = new;
1431 }