Create startup files from the GCC sources and drop our versions.
[dragonfly.git] / contrib / gcc-4.0 / gcc / gensupport.c
1 /* Support routines for the various generation passes.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, 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 \f
122 void
123 message_with_line (int lineno, const char *msg, ...)
124 {
125   va_list ap;
126
127   va_start (ap, msg);
128
129   fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
130   vfprintf (stderr, msg, ap);
131   fputc ('\n', stderr);
132
133   va_end (ap);
134 }
135
136 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
137    the gensupport programs.  */
138
139 rtx
140 gen_rtx_CONST_INT (enum machine_mode ARG_UNUSED (mode),
141                    HOST_WIDE_INT arg)
142 {
143   rtx rt = rtx_alloc (CONST_INT);
144
145   XWINT (rt, 0) = arg;
146   return rt;
147 }
148 \f
149 /* Queue PATTERN on LIST_TAIL.  Return the address of the new queue
150    element.  */
151
152 static struct queue_elem *
153 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
154                const char *filename, int lineno)
155 {
156   struct queue_elem *e = XNEW(struct queue_elem);
157   e->data = pattern;
158   e->filename = filename;
159   e->lineno = lineno;
160   e->next = NULL;
161   e->split = NULL;
162   **list_tail = e;
163   *list_tail = &e->next;
164   return e;
165 }
166
167 /* Recursively remove constraints from an rtx.  */
168
169 static void
170 remove_constraints (rtx part)
171 {
172   int i, j;
173   const char *format_ptr;
174
175   if (part == 0)
176     return;
177
178   if (GET_CODE (part) == MATCH_OPERAND)
179     XSTR (part, 2) = "";
180   else if (GET_CODE (part) == MATCH_SCRATCH)
181     XSTR (part, 1) = "";
182
183   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
184
185   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
186     switch (*format_ptr++)
187       {
188       case 'e':
189       case 'u':
190         remove_constraints (XEXP (part, i));
191         break;
192       case 'E':
193         if (XVEC (part, i) != NULL)
194           for (j = 0; j < XVECLEN (part, i); j++)
195             remove_constraints (XVECEXP (part, i, j));
196         break;
197       }
198 }
199
200 /* Process an include file assuming that it lives in gcc/config/{target}/
201    if the include looks like (include "file").  */
202
203 static void
204 process_include (rtx desc, int lineno)
205 {
206   const char *filename = XSTR (desc, 0);
207   const char *old_filename;
208   int old_lineno;
209   char *pathname;
210   FILE *input_file;
211
212   /* If specified file name is absolute, skip the include stack.  */
213   if (! IS_ABSOLUTE_PATH (filename))
214     {
215       struct file_name_list *stackp;
216
217       /* Search directory path, trying to open the file.  */
218       for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
219         {
220           static const char sep[2] = { DIR_SEPARATOR, '\0' };
221
222           pathname = concat (stackp->fname, sep, filename, NULL);
223           input_file = fopen (pathname, "r");
224           if (input_file != NULL)
225             goto success;
226           free (pathname);
227         }
228     }
229
230   if (base_dir)
231     pathname = concat (base_dir, filename, NULL);
232   else
233     pathname = xstrdup (filename);
234   input_file = fopen (pathname, "r");
235   if (input_file == NULL)
236     {
237       free (pathname);
238       message_with_line (lineno, "include file `%s' not found", filename);
239       errors = 1;
240       return;
241     }
242  success:
243
244   /* Save old cursor; setup new for the new file.  Note that "lineno" the
245      argument to this function is the beginning of the include statement,
246      while read_rtx_lineno has already been advanced.  */
247   old_filename = read_rtx_filename;
248   old_lineno = read_rtx_lineno;
249   read_rtx_filename = pathname;
250   read_rtx_lineno = 1;
251
252   if (include_callback)
253     include_callback (pathname);
254
255   /* Read the entire file.  */
256   while (read_rtx (input_file, &desc, &lineno))
257     process_rtx (desc, lineno);
258
259   /* Do not free pathname.  It is attached to the various rtx queue
260      elements.  */
261
262   read_rtx_filename = old_filename;
263   read_rtx_lineno = old_lineno;
264
265   fclose (input_file);
266 }
267
268 /* Process a top level rtx in some way, queuing as appropriate.  */
269
270 static void
271 process_rtx (rtx desc, int lineno)
272 {
273   switch (GET_CODE (desc))
274     {
275     case DEFINE_INSN:
276       queue_pattern (desc, &define_insn_tail, read_rtx_filename, lineno);
277       break;
278
279     case DEFINE_COND_EXEC:
280       queue_pattern (desc, &define_cond_exec_tail, read_rtx_filename, lineno);
281       break;
282
283     case DEFINE_ATTR:
284       queue_pattern (desc, &define_attr_tail, read_rtx_filename, lineno);
285       break;
286
287     case DEFINE_PREDICATE:
288     case DEFINE_SPECIAL_PREDICATE:
289       queue_pattern (desc, &define_pred_tail, read_rtx_filename, lineno);
290       break;
291
292     case INCLUDE:
293       process_include (desc, lineno);
294       break;
295
296     case DEFINE_INSN_AND_SPLIT:
297       {
298         const char *split_cond;
299         rtx split;
300         rtvec attr;
301         int i;
302         struct queue_elem *insn_elem;
303         struct queue_elem *split_elem;
304
305         /* Create a split with values from the insn_and_split.  */
306         split = rtx_alloc (DEFINE_SPLIT);
307
308         i = XVECLEN (desc, 1);
309         XVEC (split, 0) = rtvec_alloc (i);
310         while (--i >= 0)
311           {
312             XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
313             remove_constraints (XVECEXP (split, 0, i));
314           }
315
316         /* If the split condition starts with "&&", append it to the
317            insn condition to create the new split condition.  */
318         split_cond = XSTR (desc, 4);
319         if (split_cond[0] == '&' && split_cond[1] == '&')
320           split_cond = concat (XSTR (desc, 2), split_cond, NULL);
321         XSTR (split, 1) = split_cond;
322         XVEC (split, 2) = XVEC (desc, 5);
323         XSTR (split, 3) = XSTR (desc, 6);
324
325         /* Fix up the DEFINE_INSN.  */
326         attr = XVEC (desc, 7);
327         PUT_CODE (desc, DEFINE_INSN);
328         XVEC (desc, 4) = attr;
329
330         /* Queue them.  */
331         insn_elem
332           = queue_pattern (desc, &define_insn_tail, read_rtx_filename, 
333                            lineno);
334         split_elem
335           = queue_pattern (split, &other_tail, read_rtx_filename, lineno);
336         insn_elem->split = split_elem;
337         break;
338       }
339
340     default:
341       queue_pattern (desc, &other_tail, read_rtx_filename, lineno);
342       break;
343     }
344 }
345 \f
346 /* Return true if attribute PREDICABLE is true for ELEM, which holds
347    a DEFINE_INSN.  */
348
349 static int
350 is_predicable (struct queue_elem *elem)
351 {
352   rtvec vec = XVEC (elem->data, 4);
353   const char *value;
354   int i;
355
356   if (! vec)
357     return predicable_default;
358
359   for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
360     {
361       rtx sub = RTVEC_ELT (vec, i);
362       switch (GET_CODE (sub))
363         {
364         case SET_ATTR:
365           if (strcmp (XSTR (sub, 0), "predicable") == 0)
366             {
367               value = XSTR (sub, 1);
368               goto found;
369             }
370           break;
371
372         case SET_ATTR_ALTERNATIVE:
373           if (strcmp (XSTR (sub, 0), "predicable") == 0)
374             {
375               message_with_line (elem->lineno,
376                                  "multiple alternatives for `predicable'");
377               errors = 1;
378               return 0;
379             }
380           break;
381
382         case SET:
383           if (GET_CODE (SET_DEST (sub)) != ATTR
384               || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
385             break;
386           sub = SET_SRC (sub);
387           if (GET_CODE (sub) == CONST_STRING)
388             {
389               value = XSTR (sub, 0);
390               goto found;
391             }
392
393           /* ??? It would be possible to handle this if we really tried.
394              It's not easy though, and I'm not going to bother until it
395              really proves necessary.  */
396           message_with_line (elem->lineno,
397                              "non-constant value for `predicable'");
398           errors = 1;
399           return 0;
400
401         default:
402           gcc_unreachable ();
403         }
404     }
405
406   return predicable_default;
407
408  found:
409   /* Verify that predicability does not vary on the alternative.  */
410   /* ??? It should be possible to handle this by simply eliminating
411      the non-predicable alternatives from the insn.  FRV would like
412      to do this.  Delay this until we've got the basics solid.  */
413   if (strchr (value, ',') != NULL)
414     {
415       message_with_line (elem->lineno,
416                          "multiple alternatives for `predicable'");
417       errors = 1;
418       return 0;
419     }
420
421   /* Find out which value we're looking at.  */
422   if (strcmp (value, predicable_true) == 0)
423     return 1;
424   if (strcmp (value, predicable_false) == 0)
425     return 0;
426
427   message_with_line (elem->lineno,
428                      "unknown value `%s' for `predicable' attribute",
429                      value);
430   errors = 1;
431   return 0;
432 }
433
434 /* Examine the attribute "predicable"; discover its boolean values
435    and its default.  */
436
437 static void
438 identify_predicable_attribute (void)
439 {
440   struct queue_elem *elem;
441   char *p_true, *p_false;
442   const char *value;
443
444   /* Look for the DEFINE_ATTR for `predicable', which must exist.  */
445   for (elem = define_attr_queue; elem ; elem = elem->next)
446     if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
447       goto found;
448
449   message_with_line (define_cond_exec_queue->lineno,
450                      "attribute `predicable' not defined");
451   errors = 1;
452   return;
453
454  found:
455   value = XSTR (elem->data, 1);
456   p_false = xstrdup (value);
457   p_true = strchr (p_false, ',');
458   if (p_true == NULL || strchr (++p_true, ',') != NULL)
459     {
460       message_with_line (elem->lineno,
461                          "attribute `predicable' is not a boolean");
462       errors = 1;
463       return;
464     }
465   p_true[-1] = '\0';
466
467   predicable_true = p_true;
468   predicable_false = p_false;
469
470   switch (GET_CODE (XEXP (elem->data, 2)))
471     {
472     case CONST_STRING:
473       value = XSTR (XEXP (elem->data, 2), 0);
474       break;
475
476     case CONST:
477       message_with_line (elem->lineno,
478                          "attribute `predicable' cannot be const");
479       errors = 1;
480       return;
481
482     default:
483       message_with_line (elem->lineno,
484                          "attribute `predicable' must have a constant default");
485       errors = 1;
486       return;
487     }
488
489   if (strcmp (value, p_true) == 0)
490     predicable_default = 1;
491   else if (strcmp (value, p_false) == 0)
492     predicable_default = 0;
493   else
494     {
495       message_with_line (elem->lineno,
496                          "unknown value `%s' for `predicable' attribute",
497                          value);
498       errors = 1;
499     }
500 }
501
502 /* Return the number of alternatives in constraint S.  */
503
504 static int
505 n_alternatives (const char *s)
506 {
507   int n = 1;
508
509   if (s)
510     while (*s)
511       n += (*s++ == ',');
512
513   return n;
514 }
515
516 /* Determine how many alternatives there are in INSN, and how many
517    operands.  */
518
519 static void
520 collect_insn_data (rtx pattern, int *palt, int *pmax)
521 {
522   const char *fmt;
523   enum rtx_code code;
524   int i, j, len;
525
526   code = GET_CODE (pattern);
527   switch (code)
528     {
529     case MATCH_OPERAND:
530       i = n_alternatives (XSTR (pattern, 2));
531       *palt = (i > *palt ? i : *palt);
532       /* Fall through.  */
533
534     case MATCH_OPERATOR:
535     case MATCH_SCRATCH:
536     case MATCH_PARALLEL:
537       i = XINT (pattern, 0);
538       if (i > *pmax)
539         *pmax = i;
540       break;
541
542     default:
543       break;
544     }
545
546   fmt = GET_RTX_FORMAT (code);
547   len = GET_RTX_LENGTH (code);
548   for (i = 0; i < len; i++)
549     {
550       switch (fmt[i])
551         {
552         case 'e': case 'u':
553           collect_insn_data (XEXP (pattern, i), palt, pmax);
554           break;
555
556         case 'V':
557           if (XVEC (pattern, i) == NULL)
558             break;
559           /* Fall through.  */
560         case 'E':
561           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
562             collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
563           break;
564
565         case 'i': case 'w': case '0': case 's': case 'S': case 'T':
566           break;
567
568         default:
569           gcc_unreachable ();
570         }
571     }
572 }
573
574 static rtx
575 alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
576 {
577   const char *fmt;
578   enum rtx_code code;
579   int i, j, len;
580
581   code = GET_CODE (pattern);
582   switch (code)
583     {
584     case MATCH_OPERAND:
585       {
586         const char *c = XSTR (pattern, 2);
587
588         if (n_alternatives (c) != 1)
589           {
590             message_with_line (lineno,
591                                "too many alternatives for operand %d",
592                                XINT (pattern, 0));
593             errors = 1;
594             return NULL;
595           }
596
597         /* Replicate C as needed to fill out ALT alternatives.  */
598         if (c && *c && alt > 1)
599           {
600             size_t c_len = strlen (c);
601             size_t len = alt * (c_len + 1);
602             char *new_c = XNEWVEC(char, len);
603
604             memcpy (new_c, c, c_len);
605             for (i = 1; i < alt; ++i)
606               {
607                 new_c[i * (c_len + 1) - 1] = ',';
608                 memcpy (&new_c[i * (c_len + 1)], c, c_len);
609               }
610             new_c[len - 1] = '\0';
611             XSTR (pattern, 2) = new_c;
612           }
613       }
614       /* Fall through.  */
615
616     case MATCH_OPERATOR:
617     case MATCH_SCRATCH:
618     case MATCH_PARALLEL:
619       XINT (pattern, 0) += max_op;
620       break;
621
622     default:
623       break;
624     }
625
626   fmt = GET_RTX_FORMAT (code);
627   len = GET_RTX_LENGTH (code);
628   for (i = 0; i < len; i++)
629     {
630       rtx r;
631
632       switch (fmt[i])
633         {
634         case 'e': case 'u':
635           r = alter_predicate_for_insn (XEXP (pattern, i), alt,
636                                         max_op, lineno);
637           if (r == NULL)
638             return r;
639           break;
640
641         case 'E':
642           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
643             {
644               r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
645                                             alt, max_op, lineno);
646               if (r == NULL)
647                 return r;
648             }
649           break;
650
651         case 'i': case 'w': case '0': case 's':
652           break;
653
654         default:
655           gcc_unreachable ();
656         }
657     }
658
659   return pattern;
660 }
661
662 static const char *
663 alter_test_for_insn (struct queue_elem *ce_elem,
664                      struct queue_elem *insn_elem)
665 {
666   const char *ce_test, *insn_test;
667
668   ce_test = XSTR (ce_elem->data, 1);
669   insn_test = XSTR (insn_elem->data, 2);
670   if (!ce_test || *ce_test == '\0')
671     return insn_test;
672   if (!insn_test || *insn_test == '\0')
673     return ce_test;
674
675   return concat ("(", ce_test, ") && (", insn_test, ")", NULL);
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 i, lineno;
910   size_t ix;
911   char *lastsl;
912   rtx desc;
913
914   /* Unlock the stdio streams.  */
915   unlock_std_streams ();
916
917   for (i = 1; i < argc; i++)
918     {
919       if (argv[i][0] != '-')
920         {
921           if (in_fname)
922             fatal ("too many input files");
923
924           in_fname = argv[i];
925         }
926       else
927         {
928           int c = argv[i][1];
929           switch (c)
930             {
931             case 'I':           /* Add directory to path for includes.  */
932               {
933                 struct file_name_list *dirtmp;
934
935                 dirtmp = XNEW (struct file_name_list);
936                 dirtmp->next = 0;       /* New one goes on the end */
937                 if (first_dir_md_include == 0)
938                   first_dir_md_include = dirtmp;
939                 else
940                   last_dir_md_include->next = dirtmp;
941                 last_dir_md_include = dirtmp;   /* Tail follows the last one */
942                 if (argv[i][1] == 'I' && argv[i][2] != 0)
943                   dirtmp->fname = argv[i] + 2;
944                 else if (i + 1 == argc)
945                   fatal ("directory name missing after -I option");
946                 else
947                   dirtmp->fname = argv[++i];
948                 if (strlen (dirtmp->fname) > max_include_len)
949                   max_include_len = strlen (dirtmp->fname);
950               }
951               break;
952             default:
953               /* The program may have provided a callback so it can
954                  accept its own options.  */
955               if (parse_opt && parse_opt (argv[i]))
956                 break;
957
958               fatal ("invalid option `%s'", argv[i]);
959             }
960         }
961     }
962
963   if (!in_fname)
964     fatal ("no input file name");
965
966   lastsl = strrchr (in_fname, '/');
967   if (lastsl != NULL)
968     base_dir = save_string (in_fname, lastsl - in_fname + 1 );
969
970   read_rtx_filename = in_fname;
971   input_file = fopen (in_fname, "r");
972   if (input_file == 0)
973     {
974       perror (in_fname);
975       return FATAL_EXIT_CODE;
976     }
977
978   /* Initialize the table of insn conditions.  */
979   condition_table = htab_create (n_insn_conditions,
980                                  hash_c_test, cmp_c_test, NULL);
981
982   for (ix = 0; ix < n_insn_conditions; ix++)
983     *(htab_find_slot (condition_table, &insn_conditions[ix], INSERT))
984       = (void *) &insn_conditions[ix];
985
986   init_predicate_table ();
987
988   obstack_init (rtl_obstack);
989   errors = 0;
990   sequence_num = 0;
991
992   /* Read the entire file.  */
993   while (read_rtx (input_file, &desc, &lineno))
994     process_rtx (desc, lineno);
995   fclose (input_file);
996
997   /* Process define_cond_exec patterns.  */
998   if (define_cond_exec_queue != NULL)
999     process_define_cond_exec ();
1000
1001   return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
1002 }
1003
1004 /* Programs that don't have their own options can use this entry point
1005    instead.  */
1006 int
1007 init_md_reader_args (int argc, char **argv)
1008 {
1009   return init_md_reader_args_cb (argc, argv, 0);
1010 }
1011 \f
1012 /* The entry point for reading a single rtx from an md file.  */
1013
1014 rtx
1015 read_md_rtx (int *lineno, int *seqnr)
1016 {
1017   struct queue_elem **queue, *elem;
1018   rtx desc;
1019
1020  discard:
1021
1022   /* Read all patterns from a given queue before moving on to the next.  */
1023   if (define_attr_queue != NULL)
1024     queue = &define_attr_queue;
1025   else if (define_pred_queue != NULL)
1026     queue = &define_pred_queue;
1027   else if (define_insn_queue != NULL)
1028     queue = &define_insn_queue;
1029   else if (other_queue != NULL)
1030     queue = &other_queue;
1031   else
1032     return NULL_RTX;
1033
1034   elem = *queue;
1035   *queue = elem->next;
1036   desc = elem->data;
1037   read_rtx_filename = elem->filename;
1038   *lineno = elem->lineno;
1039   *seqnr = sequence_num;
1040
1041   free (elem);
1042
1043   /* Discard insn patterns which we know can never match (because
1044      their C test is provably always false).  If insn_elision is
1045      false, our caller needs to see all the patterns.  Note that the
1046      elided patterns are never counted by the sequence numbering; it
1047      it is the caller's responsibility, when insn_elision is false, not
1048      to use elided pattern numbers for anything.  */
1049   switch (GET_CODE (desc))
1050     {
1051     case DEFINE_INSN:
1052     case DEFINE_EXPAND:
1053       if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
1054         sequence_num++;
1055       else if (insn_elision)
1056         goto discard;
1057       break;
1058
1059     case DEFINE_SPLIT:
1060     case DEFINE_PEEPHOLE:
1061     case DEFINE_PEEPHOLE2:
1062       if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
1063         sequence_num++;
1064       else if (insn_elision)
1065             goto discard;
1066       break;
1067
1068     default:
1069       break;
1070     }
1071
1072   return desc;
1073 }
1074
1075 /* Helper functions for insn elision.  */
1076
1077 /* Compute a hash function of a c_test structure, which is keyed
1078    by its ->expr field.  */
1079 hashval_t
1080 hash_c_test (const void *x)
1081 {
1082   const struct c_test *a = (const struct c_test *) x;
1083   const unsigned char *base, *s = (const unsigned char *) a->expr;
1084   hashval_t hash;
1085   unsigned char c;
1086   unsigned int len;
1087
1088   base = s;
1089   hash = 0;
1090
1091   while ((c = *s++) != '\0')
1092     {
1093       hash += c + (c << 17);
1094       hash ^= hash >> 2;
1095     }
1096
1097   len = s - base;
1098   hash += len + (len << 17);
1099   hash ^= hash >> 2;
1100
1101   return hash;
1102 }
1103
1104 /* Compare two c_test expression structures.  */
1105 int
1106 cmp_c_test (const void *x, const void *y)
1107 {
1108   const struct c_test *a = (const struct c_test *) x;
1109   const struct c_test *b = (const struct c_test *) y;
1110
1111   return !strcmp (a->expr, b->expr);
1112 }
1113
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.  */
1118 int
1119 maybe_eval_c_test (const char *expr)
1120 {
1121   const struct c_test *test;
1122   struct c_test dummy;
1123
1124   if (expr[0] == 0)
1125     return 1;
1126
1127   if (insn_elision_unavailable)
1128     return -1;
1129
1130   dummy.expr = expr;
1131   test = (const struct c_test *)htab_find (condition_table, &dummy);
1132   gcc_assert (test);
1133
1134   return test->value;
1135 }
1136
1137 /* Given a string, return the number of comma-separated elements in it.
1138    Return 0 for the null string.  */
1139 int
1140 n_comma_elts (const char *s)
1141 {
1142   int n;
1143
1144   if (*s == '\0')
1145     return 0;
1146
1147   for (n = 1; *s; s++)
1148     if (*s == ',')
1149       n++;
1150
1151   return n;
1152 }
1153
1154 /* Given a pointer to a (char *), return a pointer to the beginning of the
1155    next comma-separated element in the string.  Advance the pointer given
1156    to the end of that element.  Return NULL if at end of string.  Caller
1157    is responsible for copying the string if necessary.  White space between
1158    a comma and an element is ignored.  */
1159
1160 const char *
1161 scan_comma_elt (const char **pstr)
1162 {
1163   const char *start;
1164   const char *p = *pstr;
1165
1166   if (*p == ',')
1167     p++;
1168   while (ISSPACE(*p))
1169     p++;
1170
1171   if (*p == '\0')
1172     return NULL;
1173
1174   start = p;
1175
1176   while (*p != ',' && *p != '\0')
1177     p++;
1178
1179   *pstr = p;
1180   return start;
1181 }
1182
1183 /* Helper functions for define_predicate and define_special_predicate
1184    processing.  Shared between genrecog.c and genpreds.c.  */
1185
1186 static htab_t predicate_table;
1187 struct pred_data *first_predicate;
1188 static struct pred_data **last_predicate = &first_predicate;
1189
1190 static hashval_t
1191 hash_struct_pred_data (const void *ptr)
1192 {
1193   return htab_hash_string (((const struct pred_data *)ptr)->name);
1194 }
1195
1196 static int
1197 eq_struct_pred_data (const void *a, const void *b)
1198 {
1199   return !strcmp (((const struct pred_data *)a)->name,
1200                   ((const struct pred_data *)b)->name);
1201 }
1202
1203 struct pred_data *
1204 lookup_predicate (const char *name)
1205 {
1206   struct pred_data key;
1207   key.name = name;
1208   return htab_find (predicate_table, &key);
1209 }
1210
1211 void
1212 add_predicate (struct pred_data *pred)
1213 {
1214   void **slot = htab_find_slot (predicate_table, pred, INSERT);
1215   if (*slot)
1216     {
1217       error ("duplicate predicate definition for '%s'", pred->name);
1218       return;
1219     }
1220   *slot = pred;
1221   *last_predicate = pred;
1222   last_predicate = &pred->next;
1223 }
1224
1225 /* This array gives the initial content of the predicate table.  It
1226    has entries for all predicates defined in recog.c.  The back end
1227    can define PREDICATE_CODES to give additional entries for the
1228    table; this is considered an obsolete mechanism (use
1229    define_predicate instead).  */
1230
1231 struct old_pred_table
1232 {
1233   const char *name;
1234   RTX_CODE codes[NUM_RTX_CODE];
1235 };
1236
1237 static const struct old_pred_table old_preds[] = {
1238   {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1239                        LABEL_REF, SUBREG, REG, MEM }},
1240   {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1241                        LABEL_REF, SUBREG, REG, MEM,
1242                        PLUS, MINUS, MULT}},
1243   {"register_operand", {SUBREG, REG}},
1244   {"pmode_register_operand", {SUBREG, REG}},
1245   {"scratch_operand", {SCRATCH, REG}},
1246   {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1247                          LABEL_REF}},
1248   {"const_int_operand", {CONST_INT}},
1249   {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
1250   {"nonimmediate_operand", {SUBREG, REG, MEM}},
1251   {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
1252                          LABEL_REF, SUBREG, REG}},
1253   {"push_operand", {MEM}},
1254   {"pop_operand", {MEM}},
1255   {"memory_operand", {SUBREG, MEM}},
1256   {"indirect_operand", {SUBREG, MEM}},
1257   {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
1258                            UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
1259                            UNLT, LTGT}},
1260 #ifdef PREDICATE_CODES
1261   PREDICATE_CODES
1262 #endif
1263 };
1264 #define NUM_KNOWN_OLD_PREDS ARRAY_SIZE (old_preds)
1265
1266 /* This table gives the initial set of special predicates.  It has
1267    entries for all special predicates defined in recog.c.  The back
1268    end can define SPECIAL_MODE_PREDICATES to give additional entries
1269    for the table; this is considered an obsolete mechanism (use
1270    define_special_predicate instead).  */
1271 static const char *const old_special_pred_table[] = {
1272   "address_operand",
1273   "pmode_register_operand",
1274 #ifdef SPECIAL_MODE_PREDICATES
1275   SPECIAL_MODE_PREDICATES
1276 #endif
1277 };
1278
1279 #define NUM_OLD_SPECIAL_MODE_PREDS ARRAY_SIZE (old_special_pred_table)
1280
1281 /* Initialize the table of predicate definitions, starting with
1282    the information we have on generic predicates, and the old-style
1283    PREDICATE_CODES definitions.  */
1284
1285 static void
1286 init_predicate_table (void)
1287 {
1288   size_t i, j;
1289   struct pred_data *pred;
1290
1291   predicate_table = htab_create_alloc (37, hash_struct_pred_data,
1292                                        eq_struct_pred_data, 0,
1293                                        xcalloc, free);
1294
1295   for (i = 0; i < NUM_KNOWN_OLD_PREDS; i++)
1296     {
1297       pred = xcalloc (sizeof (struct pred_data), 1);
1298       pred->name = old_preds[i].name;
1299
1300       for (j = 0; old_preds[i].codes[j] != 0; j++)
1301         {
1302           enum rtx_code code = old_preds[i].codes[j];
1303
1304           pred->codes[code] = true;
1305           if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
1306             pred->allows_non_const = true;
1307           if (code != REG
1308               && code != SUBREG
1309               && code != MEM
1310               && code != CONCAT
1311               && code != PARALLEL
1312               && code != STRICT_LOW_PART)
1313             pred->allows_non_lvalue = true;
1314         }
1315       if (j == 1)
1316         pred->singleton = old_preds[i].codes[0];
1317       
1318       add_predicate (pred);
1319     }
1320
1321   for (i = 0; i < NUM_OLD_SPECIAL_MODE_PREDS; i++)
1322     {
1323       pred = lookup_predicate (old_special_pred_table[i]);
1324       if (!pred)
1325         {
1326           error ("old-style special predicate list refers "
1327                  "to unknown predicate '%s'", old_special_pred_table[i]);
1328           continue;
1329         }
1330       pred->special = true;
1331     }
1332 }