OSDN Git Service

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