OSDN Git Service

2012-10-08 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / gensupport.c
1 /* Support routines for the various generation passes.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3    2010, 2012  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 3, 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 COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20
21 #include "bconfig.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "obstack.h"
27 #include "errors.h"
28 #include "hashtab.h"
29 #include "read-md.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 static struct obstack obstack;
39 struct obstack *rtl_obstack = &obstack;
40
41 /* Counter for patterns that generate code: define_insn, define_expand,
42    define_split, define_peephole, and define_peephole2.  See read_md_rtx().
43    Any define_insn_and_splits are already in separate queues so that the
44    insn and the splitter get a unique number also.  */
45 static int sequence_num;
46
47 static int predicable_default;
48 static const char *predicable_true;
49 static const char *predicable_false;
50
51 static htab_t condition_table;
52
53 /* We initially queue all patterns, process the define_insn and
54    define_cond_exec patterns, then return them one at a time.  */
55
56 struct queue_elem
57 {
58   rtx data;
59   const char *filename;
60   int lineno;
61   struct queue_elem *next;
62   /* In a DEFINE_INSN that came from a DEFINE_INSN_AND_SPLIT, SPLIT
63      points to the generated DEFINE_SPLIT.  */
64   struct queue_elem *split;
65 };
66
67 #define MNEMONIC_ATTR_NAME "mnemonic"
68 #define MNEMONIC_HTAB_SIZE 1024
69
70 static struct queue_elem *define_attr_queue;
71 static struct queue_elem **define_attr_tail = &define_attr_queue;
72 static struct queue_elem *define_pred_queue;
73 static struct queue_elem **define_pred_tail = &define_pred_queue;
74 static struct queue_elem *define_insn_queue;
75 static struct queue_elem **define_insn_tail = &define_insn_queue;
76 static struct queue_elem *define_cond_exec_queue;
77 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
78 static struct queue_elem *other_queue;
79 static struct queue_elem **other_tail = &other_queue;
80
81 static struct queue_elem *queue_pattern (rtx, struct queue_elem ***,
82                                          const char *, int);
83
84 static void remove_constraints (rtx);
85 static void process_rtx (rtx, int);
86
87 static int is_predicable (struct queue_elem *);
88 static void identify_predicable_attribute (void);
89 static int n_alternatives (const char *);
90 static void collect_insn_data (rtx, int *, int *);
91 static rtx alter_predicate_for_insn (rtx, int, int, int);
92 static const char *alter_test_for_insn (struct queue_elem *,
93                                         struct queue_elem *);
94 static char *shift_output_template (char *, const char *, int);
95 static const char *alter_output_for_insn (struct queue_elem *,
96                                           struct queue_elem *,
97                                           int, int);
98 static void process_one_cond_exec (struct queue_elem *);
99 static void process_define_cond_exec (void);
100 static void init_predicate_table (void);
101 static void record_insn_name (int, const char *);
102 \f
103 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
104    the gensupport programs.  */
105
106 rtx
107 gen_rtx_CONST_INT (enum machine_mode ARG_UNUSED (mode),
108                    HOST_WIDE_INT arg)
109 {
110   rtx rt = rtx_alloc (CONST_INT);
111
112   XWINT (rt, 0) = arg;
113   return rt;
114 }
115 \f
116 /* Predicate handling.
117
118    We construct from the machine description a table mapping each
119    predicate to a list of the rtl codes it can possibly match.  The
120    function 'maybe_both_true' uses it to deduce that there are no
121    expressions that can be matches by certain pairs of tree nodes.
122    Also, if a predicate can match only one code, we can hardwire that
123    code into the node testing the predicate.
124
125    Some predicates are flagged as special.  validate_pattern will not
126    warn about modeless match_operand expressions if they have a
127    special predicate.  Predicates that allow only constants are also
128    treated as special, for this purpose.
129
130    validate_pattern will warn about predicates that allow non-lvalues
131    when they appear in destination operands.
132
133    Calculating the set of rtx codes that can possibly be accepted by a
134    predicate expression EXP requires a three-state logic: any given
135    subexpression may definitively accept a code C (Y), definitively
136    reject a code C (N), or may have an indeterminate effect (I).  N
137    and I is N; Y or I is Y; Y and I, N or I are both I.  Here are full
138    truth tables.
139
140      a b  a&b  a|b
141      Y Y   Y    Y
142      N Y   N    Y
143      N N   N    N
144      I Y   I    Y
145      I N   N    I
146      I I   I    I
147
148    We represent Y with 1, N with 0, I with 2.  If any code is left in
149    an I state by the complete expression, we must assume that that
150    code can be accepted.  */
151
152 #define N 0
153 #define Y 1
154 #define I 2
155
156 #define TRISTATE_AND(a,b)                       \
157   ((a) == I ? ((b) == N ? N : I) :              \
158    (b) == I ? ((a) == N ? N : I) :              \
159    (a) && (b))
160
161 #define TRISTATE_OR(a,b)                        \
162   ((a) == I ? ((b) == Y ? Y : I) :              \
163    (b) == I ? ((a) == Y ? Y : I) :              \
164    (a) || (b))
165
166 #define TRISTATE_NOT(a)                         \
167   ((a) == I ? I : !(a))
168
169 /* 0 means no warning about that code yet, 1 means warned.  */
170 static char did_you_mean_codes[NUM_RTX_CODE];
171
172 /* Recursively calculate the set of rtx codes accepted by the
173    predicate expression EXP, writing the result to CODES.  LINENO is
174    the line number on which the directive containing EXP appeared.  */
175
176 static void
177 compute_predicate_codes (rtx exp, int lineno, char codes[NUM_RTX_CODE])
178 {
179   char op0_codes[NUM_RTX_CODE];
180   char op1_codes[NUM_RTX_CODE];
181   char op2_codes[NUM_RTX_CODE];
182   int i;
183
184   switch (GET_CODE (exp))
185     {
186     case AND:
187       compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
188       compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
189       for (i = 0; i < NUM_RTX_CODE; i++)
190         codes[i] = TRISTATE_AND (op0_codes[i], op1_codes[i]);
191       break;
192
193     case IOR:
194       compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
195       compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
196       for (i = 0; i < NUM_RTX_CODE; i++)
197         codes[i] = TRISTATE_OR (op0_codes[i], op1_codes[i]);
198       break;
199     case NOT:
200       compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
201       for (i = 0; i < NUM_RTX_CODE; i++)
202         codes[i] = TRISTATE_NOT (op0_codes[i]);
203       break;
204
205     case IF_THEN_ELSE:
206       /* a ? b : c  accepts the same codes as (a & b) | (!a & c).  */
207       compute_predicate_codes (XEXP (exp, 0), lineno, op0_codes);
208       compute_predicate_codes (XEXP (exp, 1), lineno, op1_codes);
209       compute_predicate_codes (XEXP (exp, 2), lineno, op2_codes);
210       for (i = 0; i < NUM_RTX_CODE; i++)
211         codes[i] = TRISTATE_OR (TRISTATE_AND (op0_codes[i], op1_codes[i]),
212                                 TRISTATE_AND (TRISTATE_NOT (op0_codes[i]),
213                                               op2_codes[i]));
214       break;
215
216     case MATCH_CODE:
217       /* MATCH_CODE allows a specified list of codes.  However, if it
218          does not apply to the top level of the expression, it does not
219          constrain the set of codes for the top level.  */
220       if (XSTR (exp, 1)[0] != '\0')
221         {
222           memset (codes, Y, NUM_RTX_CODE);
223           break;
224         }
225
226       memset (codes, N, NUM_RTX_CODE);
227       {
228         const char *next_code = XSTR (exp, 0);
229         const char *code;
230
231         if (*next_code == '\0')
232           {
233             error_with_line (lineno, "empty match_code expression");
234             break;
235           }
236
237         while ((code = scan_comma_elt (&next_code)) != 0)
238           {
239             size_t n = next_code - code;
240             int found_it = 0;
241
242             for (i = 0; i < NUM_RTX_CODE; i++)
243               if (!strncmp (code, GET_RTX_NAME (i), n)
244                   && GET_RTX_NAME (i)[n] == '\0')
245                 {
246                   codes[i] = Y;
247                   found_it = 1;
248                   break;
249                 }
250             if (!found_it)
251               {
252                 error_with_line (lineno,
253                                  "match_code \"%.*s\" matches nothing",
254                                  (int) n, code);
255                 for (i = 0; i < NUM_RTX_CODE; i++)
256                   if (!strncasecmp (code, GET_RTX_NAME (i), n)
257                       && GET_RTX_NAME (i)[n] == '\0'
258                       && !did_you_mean_codes[i])
259                     {
260                       did_you_mean_codes[i] = 1;
261                       message_with_line (lineno, "(did you mean \"%s\"?)",
262                                          GET_RTX_NAME (i));
263                     }
264               }
265           }
266       }
267       break;
268
269     case MATCH_OPERAND:
270       /* MATCH_OPERAND disallows the set of codes that the named predicate
271          disallows, and is indeterminate for the codes that it does allow.  */
272       {
273         struct pred_data *p = lookup_predicate (XSTR (exp, 1));
274         if (!p)
275           {
276             error_with_line (lineno, "reference to unknown predicate '%s'",
277                              XSTR (exp, 1));
278             break;
279           }
280         for (i = 0; i < NUM_RTX_CODE; i++)
281           codes[i] = p->codes[i] ? I : N;
282       }
283       break;
284
285
286     case MATCH_TEST:
287       /* (match_test WHATEVER) is completely indeterminate.  */
288       memset (codes, I, NUM_RTX_CODE);
289       break;
290
291     default:
292       error_with_line (lineno,
293                        "'%s' cannot be used in a define_predicate expression",
294                        GET_RTX_NAME (GET_CODE (exp)));
295       memset (codes, I, NUM_RTX_CODE);
296       break;
297     }
298 }
299
300 #undef TRISTATE_OR
301 #undef TRISTATE_AND
302 #undef TRISTATE_NOT
303
304 /* Return true if NAME is a valid predicate name.  */
305
306 static bool
307 valid_predicate_name_p (const char *name)
308 {
309   const char *p;
310
311   if (!ISALPHA (name[0]) && name[0] != '_')
312     return false;
313   for (p = name + 1; *p; p++)
314     if (!ISALNUM (*p) && *p != '_')
315       return false;
316   return true;
317 }
318
319 /* Process define_predicate directive DESC, which appears on line number
320    LINENO.  Compute the set of codes that can be matched, and record this
321    as a known predicate.  */
322
323 static void
324 process_define_predicate (rtx desc, int lineno)
325 {
326   struct pred_data *pred;
327   char codes[NUM_RTX_CODE];
328   int i;
329
330   if (!valid_predicate_name_p (XSTR (desc, 0)))
331     {
332       error_with_line (lineno,
333                        "%s: predicate name must be a valid C function name",
334                        XSTR (desc, 0));
335       return;
336     }
337
338   pred = XCNEW (struct pred_data);
339   pred->name = XSTR (desc, 0);
340   pred->exp = XEXP (desc, 1);
341   pred->c_block = XSTR (desc, 2);
342   if (GET_CODE (desc) == DEFINE_SPECIAL_PREDICATE)
343     pred->special = true;
344
345   compute_predicate_codes (XEXP (desc, 1), lineno, codes);
346
347   for (i = 0; i < NUM_RTX_CODE; i++)
348     if (codes[i] != N)
349       add_predicate_code (pred, (enum rtx_code) i);
350
351   add_predicate (pred);
352 }
353 #undef I
354 #undef N
355 #undef Y
356 \f
357 /* Queue PATTERN on LIST_TAIL.  Return the address of the new queue
358    element.  */
359
360 static struct queue_elem *
361 queue_pattern (rtx pattern, struct queue_elem ***list_tail,
362                const char *filename, int lineno)
363 {
364   struct queue_elem *e = XNEW(struct queue_elem);
365   e->data = pattern;
366   e->filename = filename;
367   e->lineno = lineno;
368   e->next = NULL;
369   e->split = NULL;
370   **list_tail = e;
371   *list_tail = &e->next;
372   return e;
373 }
374
375 /* Build a define_attr for an binary attribute with name NAME and
376    possible values "yes" and "no", and queue it.  */
377 static void
378 add_define_attr (const char *name)
379 {
380   struct queue_elem *e = XNEW(struct queue_elem);
381   rtx t1 = rtx_alloc (DEFINE_ATTR);
382   XSTR (t1, 0) = name;
383   XSTR (t1, 1) = "no,yes";
384   XEXP (t1, 2) = rtx_alloc (CONST_STRING);
385   XSTR (XEXP (t1, 2), 0) = "yes";
386   e->data = t1;
387   e->filename = "built-in";
388   e->lineno = -1;
389   e->next = define_attr_queue;
390   define_attr_queue = e;
391
392 }
393
394 /* Recursively remove constraints from an rtx.  */
395
396 static void
397 remove_constraints (rtx part)
398 {
399   int i, j;
400   const char *format_ptr;
401
402   if (part == 0)
403     return;
404
405   if (GET_CODE (part) == MATCH_OPERAND)
406     XSTR (part, 2) = "";
407   else if (GET_CODE (part) == MATCH_SCRATCH)
408     XSTR (part, 1) = "";
409
410   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
411
412   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
413     switch (*format_ptr++)
414       {
415       case 'e':
416       case 'u':
417         remove_constraints (XEXP (part, i));
418         break;
419       case 'E':
420         if (XVEC (part, i) != NULL)
421           for (j = 0; j < XVECLEN (part, i); j++)
422             remove_constraints (XVECEXP (part, i, j));
423         break;
424       }
425 }
426
427 /* Process a top level rtx in some way, queuing as appropriate.  */
428
429 static void
430 process_rtx (rtx desc, int lineno)
431 {
432   switch (GET_CODE (desc))
433     {
434     case DEFINE_INSN:
435       queue_pattern (desc, &define_insn_tail, read_md_filename, lineno);
436       break;
437
438     case DEFINE_COND_EXEC:
439       queue_pattern (desc, &define_cond_exec_tail, read_md_filename, lineno);
440       break;
441
442     case DEFINE_ATTR:
443     case DEFINE_ENUM_ATTR:
444       queue_pattern (desc, &define_attr_tail, read_md_filename, lineno);
445       break;
446
447     case DEFINE_PREDICATE:
448     case DEFINE_SPECIAL_PREDICATE:
449       process_define_predicate (desc, lineno);
450       /* Fall through.  */
451
452     case DEFINE_CONSTRAINT:
453     case DEFINE_REGISTER_CONSTRAINT:
454     case DEFINE_MEMORY_CONSTRAINT:
455     case DEFINE_ADDRESS_CONSTRAINT:
456       queue_pattern (desc, &define_pred_tail, read_md_filename, lineno);
457       break;
458
459     case DEFINE_INSN_AND_SPLIT:
460       {
461         const char *split_cond;
462         rtx split;
463         rtvec attr;
464         int i;
465         struct queue_elem *insn_elem;
466         struct queue_elem *split_elem;
467
468         /* Create a split with values from the insn_and_split.  */
469         split = rtx_alloc (DEFINE_SPLIT);
470
471         i = XVECLEN (desc, 1);
472         XVEC (split, 0) = rtvec_alloc (i);
473         while (--i >= 0)
474           {
475             XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
476             remove_constraints (XVECEXP (split, 0, i));
477           }
478
479         /* If the split condition starts with "&&", append it to the
480            insn condition to create the new split condition.  */
481         split_cond = XSTR (desc, 4);
482         if (split_cond[0] == '&' && split_cond[1] == '&')
483           {
484             copy_md_ptr_loc (split_cond + 2, split_cond);
485             split_cond = join_c_conditions (XSTR (desc, 2), split_cond + 2);
486           }
487         XSTR (split, 1) = split_cond;
488         XVEC (split, 2) = XVEC (desc, 5);
489         XSTR (split, 3) = XSTR (desc, 6);
490
491         /* Fix up the DEFINE_INSN.  */
492         attr = XVEC (desc, 7);
493         PUT_CODE (desc, DEFINE_INSN);
494         XVEC (desc, 4) = attr;
495
496         /* Queue them.  */
497         insn_elem
498           = queue_pattern (desc, &define_insn_tail, read_md_filename,
499                            lineno);
500         split_elem
501           = queue_pattern (split, &other_tail, read_md_filename, lineno);
502         insn_elem->split = split_elem;
503         break;
504       }
505
506     default:
507       queue_pattern (desc, &other_tail, read_md_filename, lineno);
508       break;
509     }
510 }
511 \f
512 /* Return true if attribute PREDICABLE is true for ELEM, which holds
513    a DEFINE_INSN.  */
514
515 static int
516 is_predicable (struct queue_elem *elem)
517 {
518   rtvec vec = XVEC (elem->data, 4);
519   const char *value;
520   int i;
521
522   if (! vec)
523     return predicable_default;
524
525   for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
526     {
527       rtx sub = RTVEC_ELT (vec, i);
528       switch (GET_CODE (sub))
529         {
530         case SET_ATTR:
531           if (strcmp (XSTR (sub, 0), "predicable") == 0)
532             {
533               value = XSTR (sub, 1);
534               goto found;
535             }
536           break;
537
538         case SET_ATTR_ALTERNATIVE:
539           if (strcmp (XSTR (sub, 0), "predicable") == 0)
540             {
541               error_with_line (elem->lineno,
542                                "multiple alternatives for `predicable'");
543               return 0;
544             }
545           break;
546
547         case SET:
548           if (GET_CODE (SET_DEST (sub)) != ATTR
549               || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
550             break;
551           sub = SET_SRC (sub);
552           if (GET_CODE (sub) == CONST_STRING)
553             {
554               value = XSTR (sub, 0);
555               goto found;
556             }
557
558           /* ??? It would be possible to handle this if we really tried.
559              It's not easy though, and I'm not going to bother until it
560              really proves necessary.  */
561           error_with_line (elem->lineno,
562                            "non-constant value for `predicable'");
563           return 0;
564
565         default:
566           gcc_unreachable ();
567         }
568     }
569
570   return predicable_default;
571
572  found:
573   /* Find out which value we're looking at.  Multiple alternatives means at
574      least one is predicable.  */
575   if (strchr (value, ',') != NULL)
576     return 1;
577   if (strcmp (value, predicable_true) == 0)
578     return 1;
579   if (strcmp (value, predicable_false) == 0)
580     return 0;
581
582   error_with_line (elem->lineno,
583                    "unknown value `%s' for `predicable' attribute", value);
584   return 0;
585 }
586
587 /* Examine the attribute "predicable"; discover its boolean values
588    and its default.  */
589
590 static void
591 identify_predicable_attribute (void)
592 {
593   struct queue_elem *elem;
594   char *p_true, *p_false;
595   const char *value;
596
597   /* Look for the DEFINE_ATTR for `predicable', which must exist.  */
598   for (elem = define_attr_queue; elem ; elem = elem->next)
599     if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
600       goto found;
601
602   error_with_line (define_cond_exec_queue->lineno,
603                    "attribute `predicable' not defined");
604   return;
605
606  found:
607   value = XSTR (elem->data, 1);
608   p_false = xstrdup (value);
609   p_true = strchr (p_false, ',');
610   if (p_true == NULL || strchr (++p_true, ',') != NULL)
611     {
612       error_with_line (elem->lineno, "attribute `predicable' is not a boolean");
613       free (p_false);
614       return;
615     }
616   p_true[-1] = '\0';
617
618   predicable_true = p_true;
619   predicable_false = p_false;
620
621   switch (GET_CODE (XEXP (elem->data, 2)))
622     {
623     case CONST_STRING:
624       value = XSTR (XEXP (elem->data, 2), 0);
625       break;
626
627     case CONST:
628       error_with_line (elem->lineno, "attribute `predicable' cannot be const");
629       free (p_false);
630       return;
631
632     default:
633       error_with_line (elem->lineno,
634                        "attribute `predicable' must have a constant default");
635       free (p_false);
636       return;
637     }
638
639   if (strcmp (value, p_true) == 0)
640     predicable_default = 1;
641   else if (strcmp (value, p_false) == 0)
642     predicable_default = 0;
643   else
644     {
645       error_with_line (elem->lineno,
646                        "unknown value `%s' for `predicable' attribute", value);
647       free (p_false);
648     }
649 }
650
651 /* Return the number of alternatives in constraint S.  */
652
653 static int
654 n_alternatives (const char *s)
655 {
656   int n = 1;
657
658   if (s)
659     while (*s)
660       n += (*s++ == ',');
661
662   return n;
663 }
664
665 /* Determine how many alternatives there are in INSN, and how many
666    operands.  */
667
668 static void
669 collect_insn_data (rtx pattern, int *palt, int *pmax)
670 {
671   const char *fmt;
672   enum rtx_code code;
673   int i, j, len;
674
675   code = GET_CODE (pattern);
676   switch (code)
677     {
678     case MATCH_OPERAND:
679       i = n_alternatives (XSTR (pattern, 2));
680       *palt = (i > *palt ? i : *palt);
681       /* Fall through.  */
682
683     case MATCH_OPERATOR:
684     case MATCH_SCRATCH:
685     case MATCH_PARALLEL:
686       i = XINT (pattern, 0);
687       if (i > *pmax)
688         *pmax = i;
689       break;
690
691     default:
692       break;
693     }
694
695   fmt = GET_RTX_FORMAT (code);
696   len = GET_RTX_LENGTH (code);
697   for (i = 0; i < len; i++)
698     {
699       switch (fmt[i])
700         {
701         case 'e': case 'u':
702           collect_insn_data (XEXP (pattern, i), palt, pmax);
703           break;
704
705         case 'V':
706           if (XVEC (pattern, i) == NULL)
707             break;
708           /* Fall through.  */
709         case 'E':
710           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
711             collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
712           break;
713
714         case 'i': case 'w': case '0': case 's': case 'S': case 'T':
715           break;
716
717         default:
718           gcc_unreachable ();
719         }
720     }
721 }
722
723 static rtx
724 alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
725 {
726   const char *fmt;
727   enum rtx_code code;
728   int i, j, len;
729
730   code = GET_CODE (pattern);
731   switch (code)
732     {
733     case MATCH_OPERAND:
734       {
735         const char *c = XSTR (pattern, 2);
736
737         if (n_alternatives (c) != 1)
738           {
739             error_with_line (lineno, "too many alternatives for operand %d",
740                              XINT (pattern, 0));
741             return NULL;
742           }
743
744         /* Replicate C as needed to fill out ALT alternatives.  */
745         if (c && *c && alt > 1)
746           {
747             size_t c_len = strlen (c);
748             size_t len = alt * (c_len + 1);
749             char *new_c = XNEWVEC(char, len);
750
751             memcpy (new_c, c, c_len);
752             for (i = 1; i < alt; ++i)
753               {
754                 new_c[i * (c_len + 1) - 1] = ',';
755                 memcpy (&new_c[i * (c_len + 1)], c, c_len);
756               }
757             new_c[len - 1] = '\0';
758             XSTR (pattern, 2) = new_c;
759           }
760       }
761       /* Fall through.  */
762
763     case MATCH_OPERATOR:
764     case MATCH_SCRATCH:
765     case MATCH_PARALLEL:
766       XINT (pattern, 0) += max_op;
767       break;
768
769     default:
770       break;
771     }
772
773   fmt = GET_RTX_FORMAT (code);
774   len = GET_RTX_LENGTH (code);
775   for (i = 0; i < len; i++)
776     {
777       rtx r;
778
779       switch (fmt[i])
780         {
781         case 'e': case 'u':
782           r = alter_predicate_for_insn (XEXP (pattern, i), alt,
783                                         max_op, lineno);
784           if (r == NULL)
785             return r;
786           break;
787
788         case 'E':
789           for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
790             {
791               r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
792                                             alt, max_op, lineno);
793               if (r == NULL)
794                 return r;
795             }
796           break;
797
798         case 'i': case 'w': case '0': case 's':
799           break;
800
801         default:
802           gcc_unreachable ();
803         }
804     }
805
806   return pattern;
807 }
808
809 static const char *
810 alter_test_for_insn (struct queue_elem *ce_elem,
811                      struct queue_elem *insn_elem)
812 {
813   return join_c_conditions (XSTR (ce_elem->data, 1),
814                             XSTR (insn_elem->data, 2));
815 }
816
817 /* Modify VAL, which is an attribute expression for the "enabled" attribute,
818    to take "ce_enabled" into account.  Return the new expression.  */
819 static rtx
820 modify_attr_enabled_ce (rtx val)
821 {
822   rtx eq_attr, str;
823   rtx ite;
824   eq_attr = rtx_alloc (EQ_ATTR);
825   ite = rtx_alloc (IF_THEN_ELSE);
826   str = rtx_alloc (CONST_STRING);
827
828   XSTR (eq_attr, 0) = "ce_enabled";
829   XSTR (eq_attr, 1) = "yes";
830   XSTR (str, 0) = "no";
831   XEXP (ite, 0) = eq_attr;
832   XEXP (ite, 1) = val;
833   XEXP (ite, 2) = str;
834
835   return ite;
836 }
837
838 /* Alter the attribute vector of INSN, which is a COND_EXEC variant created
839    from a define_insn pattern.  We must modify the "predicable" attribute
840    to be named "ce_enabled", and also change any "enabled" attribute that's
841    present so that it takes ce_enabled into account.
842    We rely on the fact that INSN was created with copy_rtx, and modify data
843    in-place.  */
844
845 static void
846 alter_attrs_for_insn (rtx insn)
847 {
848   static bool global_changes_made = false;
849   rtvec vec = XVEC (insn, 4);
850   rtvec new_vec;
851   rtx val, set;
852   int num_elem;
853   int predicable_idx = -1;
854   int enabled_idx = -1;
855   int i;
856
857   if (! vec)
858     return;
859
860   num_elem = GET_NUM_ELEM (vec);
861   for (i = num_elem - 1; i >= 0; --i)
862     {
863       rtx sub = RTVEC_ELT (vec, i);
864       switch (GET_CODE (sub))
865         {
866         case SET_ATTR:
867           if (strcmp (XSTR (sub, 0), "predicable") == 0)
868             {
869               predicable_idx = i;
870               XSTR (sub, 0) = "ce_enabled";
871             }
872           else if (strcmp (XSTR (sub, 0), "enabled") == 0)
873             {
874               enabled_idx = i;
875               XSTR (sub, 0) = "nonce_enabled";
876             }
877           break;
878
879         case SET_ATTR_ALTERNATIVE:
880           if (strcmp (XSTR (sub, 0), "predicable") == 0)
881             /* We already give an error elsewhere.  */
882             return;
883           else if (strcmp (XSTR (sub, 0), "enabled") == 0)
884             {
885               enabled_idx = i;
886               XSTR (sub, 0) = "nonce_enabled";
887             }
888           break;
889
890         case SET:
891           if (GET_CODE (SET_DEST (sub)) != ATTR)
892             break;
893           if (strcmp (XSTR (SET_DEST (sub), 0), "predicable") == 0)
894             {
895               sub = SET_SRC (sub);
896               if (GET_CODE (sub) == CONST_STRING)
897                 {
898                   predicable_idx = i;
899                   XSTR (sub, 0) = "ce_enabled";
900                 }
901               else
902                 /* We already give an error elsewhere.  */
903                 return;
904               break;
905             }
906           if (strcmp (XSTR (SET_DEST (sub), 0), "enabled") == 0)
907             {
908               enabled_idx = i;
909               XSTR (SET_DEST (sub), 0) = "nonce_enabled";
910             }
911           break;
912
913         default:
914           gcc_unreachable ();
915         }
916     }
917   if (predicable_idx == -1)
918     return;
919
920   if (!global_changes_made)
921     {
922       struct queue_elem *elem;
923       
924       global_changes_made = true;
925       add_define_attr ("ce_enabled");
926       add_define_attr ("nonce_enabled");
927
928       for (elem = define_attr_queue; elem ; elem = elem->next)
929         if (strcmp (XSTR (elem->data, 0), "enabled") == 0)
930           {
931             XEXP (elem->data, 2)
932               = modify_attr_enabled_ce (XEXP (elem->data, 2));
933           }
934     }
935   if (enabled_idx == -1)
936     return;
937
938   new_vec = rtvec_alloc (num_elem + 1);
939   for (i = 0; i < num_elem; i++)
940     RTVEC_ELT (new_vec, i) = RTVEC_ELT (vec, i);
941   val = rtx_alloc (IF_THEN_ELSE);
942   XEXP (val, 0) = rtx_alloc (EQ_ATTR);
943   XEXP (val, 1) = rtx_alloc (CONST_STRING);
944   XEXP (val, 2) = rtx_alloc (CONST_STRING);
945   XSTR (XEXP (val, 0), 0) = "nonce_enabled";
946   XSTR (XEXP (val, 0), 1) = "yes";
947   XSTR (XEXP (val, 1), 0) = "yes";
948   XSTR (XEXP (val, 2), 0) = "no";
949   set = rtx_alloc (SET);
950   SET_DEST (set) = rtx_alloc (ATTR);
951   XSTR (SET_DEST (set), 0) = "enabled";
952   SET_SRC (set) = modify_attr_enabled_ce (val);
953   RTVEC_ELT (new_vec, i) = set;
954   XVEC (insn, 4) = new_vec;
955 }
956
957 /* Adjust all of the operand numbers in SRC to match the shift they'll
958    get from an operand displacement of DISP.  Return a pointer after the
959    adjusted string.  */
960
961 static char *
962 shift_output_template (char *dest, const char *src, int disp)
963 {
964   while (*src)
965     {
966       char c = *src++;
967       *dest++ = c;
968       if (c == '%')
969         {
970           c = *src++;
971           if (ISDIGIT ((unsigned char) c))
972             c += disp;
973           else if (ISALPHA (c))
974             {
975               *dest++ = c;
976               c = *src++ + disp;
977             }
978           *dest++ = c;
979         }
980     }
981
982   return dest;
983 }
984
985 static const char *
986 alter_output_for_insn (struct queue_elem *ce_elem,
987                        struct queue_elem *insn_elem,
988                        int alt, int max_op)
989 {
990   const char *ce_out, *insn_out;
991   char *result, *p;
992   size_t len, ce_len, insn_len;
993
994   /* ??? Could coordinate with genoutput to not duplicate code here.  */
995
996   ce_out = XSTR (ce_elem->data, 2);
997   insn_out = XTMPL (insn_elem->data, 3);
998   if (!ce_out || *ce_out == '\0')
999     return insn_out;
1000
1001   ce_len = strlen (ce_out);
1002   insn_len = strlen (insn_out);
1003
1004   if (*insn_out == '*')
1005     /* You must take care of the predicate yourself.  */
1006     return insn_out;
1007
1008   if (*insn_out == '@')
1009     {
1010       len = (ce_len + 1) * alt + insn_len + 1;
1011       p = result = XNEWVEC(char, len);
1012
1013       do
1014         {
1015           do
1016             *p++ = *insn_out++;
1017           while (ISSPACE ((unsigned char) *insn_out));
1018
1019           if (*insn_out != '#')
1020             {
1021               p = shift_output_template (p, ce_out, max_op);
1022               *p++ = ' ';
1023             }
1024
1025           do
1026             *p++ = *insn_out++;
1027           while (*insn_out && *insn_out != '\n');
1028         }
1029       while (*insn_out);
1030       *p = '\0';
1031     }
1032   else
1033     {
1034       len = ce_len + 1 + insn_len + 1;
1035       result = XNEWVEC (char, len);
1036
1037       p = shift_output_template (result, ce_out, max_op);
1038       *p++ = ' ';
1039       memcpy (p, insn_out, insn_len + 1);
1040     }
1041
1042   return result;
1043 }
1044
1045 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC.  */
1046
1047 static void
1048 process_one_cond_exec (struct queue_elem *ce_elem)
1049 {
1050   struct queue_elem *insn_elem;
1051   for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
1052     {
1053       int alternatives, max_operand;
1054       rtx pred, insn, pattern, split;
1055       char *new_name;
1056       int i;
1057
1058       if (! is_predicable (insn_elem))
1059         continue;
1060
1061       alternatives = 1;
1062       max_operand = -1;
1063       collect_insn_data (insn_elem->data, &alternatives, &max_operand);
1064       max_operand += 1;
1065
1066       if (XVECLEN (ce_elem->data, 0) != 1)
1067         {
1068           error_with_line (ce_elem->lineno, "too many patterns in predicate");
1069           return;
1070         }
1071
1072       pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
1073       pred = alter_predicate_for_insn (pred, alternatives, max_operand,
1074                                        ce_elem->lineno);
1075       if (pred == NULL)
1076         return;
1077
1078       /* Construct a new pattern for the new insn.  */
1079       insn = copy_rtx (insn_elem->data);
1080       new_name = XNEWVAR (char, strlen XSTR (insn_elem->data, 0) + 4);
1081       sprintf (new_name, "*p %s", XSTR (insn_elem->data, 0));
1082       XSTR (insn, 0) = new_name;
1083       pattern = rtx_alloc (COND_EXEC);
1084       XEXP (pattern, 0) = pred;
1085       if (XVECLEN (insn, 1) == 1)
1086         {
1087           XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
1088           XVECEXP (insn, 1, 0) = pattern;
1089           PUT_NUM_ELEM (XVEC (insn, 1), 1);
1090         }
1091       else
1092         {
1093           XEXP (pattern, 1) = rtx_alloc (PARALLEL);
1094           XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
1095           XVEC (insn, 1) = rtvec_alloc (1);
1096           XVECEXP (insn, 1, 0) = pattern;
1097         }
1098
1099       XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
1100       XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
1101                                               alternatives, max_operand);
1102       alter_attrs_for_insn (insn);
1103
1104       /* Put the new pattern on the `other' list so that it
1105          (a) is not reprocessed by other define_cond_exec patterns
1106          (b) appears after all normal define_insn patterns.
1107
1108          ??? B is debatable.  If one has normal insns that match
1109          cond_exec patterns, they will be preferred over these
1110          generated patterns.  Whether this matters in practice, or if
1111          it's a good thing, or whether we should thread these new
1112          patterns into the define_insn chain just after their generator
1113          is something we'll have to experiment with.  */
1114
1115       queue_pattern (insn, &other_tail, insn_elem->filename,
1116                      insn_elem->lineno);
1117
1118       if (!insn_elem->split)
1119         continue;
1120
1121       /* If the original insn came from a define_insn_and_split,
1122          generate a new split to handle the predicated insn.  */
1123       split = copy_rtx (insn_elem->split->data);
1124       /* Predicate the pattern matched by the split.  */
1125       pattern = rtx_alloc (COND_EXEC);
1126       XEXP (pattern, 0) = pred;
1127       if (XVECLEN (split, 0) == 1)
1128         {
1129           XEXP (pattern, 1) = XVECEXP (split, 0, 0);
1130           XVECEXP (split, 0, 0) = pattern;
1131           PUT_NUM_ELEM (XVEC (split, 0), 1);
1132         }
1133       else
1134         {
1135           XEXP (pattern, 1) = rtx_alloc (PARALLEL);
1136           XVEC (XEXP (pattern, 1), 0) = XVEC (split, 0);
1137           XVEC (split, 0) = rtvec_alloc (1);
1138           XVECEXP (split, 0, 0) = pattern;
1139         }
1140       /* Predicate all of the insns generated by the split.  */
1141       for (i = 0; i < XVECLEN (split, 2); i++)
1142         {
1143           pattern = rtx_alloc (COND_EXEC);
1144           XEXP (pattern, 0) = pred;
1145           XEXP (pattern, 1) = XVECEXP (split, 2, i);
1146           XVECEXP (split, 2, i) = pattern;
1147         }
1148       /* Add the new split to the queue.  */
1149       queue_pattern (split, &other_tail, read_md_filename,
1150                      insn_elem->split->lineno);
1151     }
1152 }
1153
1154 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
1155    patterns appropriately.  */
1156
1157 static void
1158 process_define_cond_exec (void)
1159 {
1160   struct queue_elem *elem;
1161
1162   identify_predicable_attribute ();
1163   if (have_error)
1164     return;
1165
1166   for (elem = define_cond_exec_queue; elem ; elem = elem->next)
1167     process_one_cond_exec (elem);
1168 }
1169 \f
1170 /* A read_md_files callback for reading an rtx.  */
1171
1172 static void
1173 rtx_handle_directive (int lineno, const char *rtx_name)
1174 {
1175   rtx queue, x;
1176
1177   if (read_rtx (rtx_name, &queue))
1178     for (x = queue; x; x = XEXP (x, 1))
1179       process_rtx (XEXP (x, 0), lineno);
1180 }
1181
1182 /* Comparison function for the mnemonic hash table.  */
1183
1184 static int
1185 htab_eq_string (const void *s1, const void *s2)
1186 {
1187   return strcmp ((const char*)s1, (const char*)s2) == 0;
1188 }
1189
1190 /* Add mnemonic STR with length LEN to the mnemonic hash table
1191    MNEMONIC_HTAB.  A trailing zero end character is appendend to STR
1192    and a permanent heap copy of STR is created.  */
1193
1194 static void
1195 add_mnemonic_string (htab_t mnemonic_htab, const char *str, int len)
1196 {
1197   char *new_str;
1198   void **slot;
1199   char *str_zero = (char*)alloca (len + 1);
1200
1201   memcpy (str_zero, str, len);
1202   str_zero[len] = '\0';
1203
1204   slot = htab_find_slot (mnemonic_htab, str_zero, INSERT);
1205
1206   if (*slot)
1207     return;
1208
1209   /* Not found; create a permanent copy and add it to the hash table.  */
1210   new_str = XNEWVAR (char, len + 1);
1211   memcpy (new_str, str_zero, len + 1);
1212   *slot = new_str;
1213 }
1214
1215 /* Scan INSN for mnemonic strings and add them to the mnemonic hash
1216    table in MNEMONIC_HTAB.
1217
1218    The mnemonics cannot be found if they are emitted using C code.
1219
1220    If a mnemonic string contains ';' or a newline the string assumed
1221    to consist of more than a single instruction.  The attribute value
1222    will then be set to the user defined default value.  */
1223
1224 static void
1225 gen_mnemonic_setattr (htab_t mnemonic_htab, rtx insn)
1226 {
1227   const char *template_code, *cp;
1228   int i;
1229   int vec_len;
1230   rtx set_attr;
1231   char *attr_name;
1232   rtvec new_vec;
1233
1234   template_code = XTMPL (insn, 3);
1235
1236   /* Skip patterns which use C code to emit the template.  */
1237   if (template_code[0] == '*')
1238     return;
1239
1240   if (template_code[0] == '@')
1241     cp = &template_code[1];
1242   else
1243     cp = &template_code[0];
1244
1245   for (i = 0; *cp; )
1246     {
1247       const char *ep, *sp;
1248       int size = 0;
1249
1250       while (ISSPACE (*cp))
1251         cp++;
1252
1253       for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
1254         if (!ISSPACE (*ep))
1255           sp = ep + 1;
1256
1257       if (i > 0)
1258         obstack_1grow (&string_obstack, ',');
1259
1260       while (cp < sp && ((*cp >= '0' && *cp <= '9')
1261                          || (*cp >= 'a' && *cp <= 'z')))
1262
1263         {
1264           obstack_1grow (&string_obstack, *cp);
1265           cp++;
1266           size++;
1267         }
1268
1269       while (cp < sp)
1270         {
1271           if (*cp == ';' || (*cp == '\\' && cp[1] == 'n'))
1272             {
1273               /* Don't set a value if there are more than one
1274                  instruction in the string.  */
1275               obstack_next_free (&string_obstack) =
1276                 obstack_next_free (&string_obstack) - size;
1277               size = 0;
1278
1279               cp = sp;
1280               break;
1281             }
1282           cp++;
1283         }
1284       if (size == 0)
1285         obstack_1grow (&string_obstack, '*');
1286       else
1287         add_mnemonic_string (mnemonic_htab,
1288                              obstack_next_free (&string_obstack) - size,
1289                              size);
1290       i++;
1291     }
1292
1293   /* An insn definition might emit an empty string.  */
1294   if (obstack_object_size (&string_obstack) == 0)
1295     return;
1296
1297   obstack_1grow (&string_obstack, '\0');
1298
1299   set_attr = rtx_alloc (SET_ATTR);
1300   XSTR (set_attr, 1) = XOBFINISH (&string_obstack, char *);
1301   attr_name = XNEWVAR (char, strlen (MNEMONIC_ATTR_NAME) + 1);
1302   strcpy (attr_name, MNEMONIC_ATTR_NAME);
1303   XSTR (set_attr, 0) = attr_name;
1304
1305   if (!XVEC (insn, 4))
1306     vec_len = 0;
1307   else
1308     vec_len = XVECLEN (insn, 4);
1309
1310   new_vec = rtvec_alloc (vec_len + 1);
1311   for (i = 0; i < vec_len; i++)
1312     RTVEC_ELT (new_vec, i) = XVECEXP (insn, 4, i);
1313   RTVEC_ELT (new_vec, vec_len) = set_attr;
1314   XVEC (insn, 4) = new_vec;
1315 }
1316
1317 /* This function is called for the elements in the mnemonic hashtable
1318    and generates a comma separated list of the mnemonics.  */
1319
1320 static int
1321 mnemonic_htab_callback (void **slot, void *info ATTRIBUTE_UNUSED)
1322 {
1323   obstack_grow (&string_obstack, (char*)*slot, strlen ((char*)*slot));
1324   obstack_1grow (&string_obstack, ',');
1325   return 1;
1326 }
1327
1328 /* Generate (set_attr "mnemonic" "..") RTXs and append them to every
1329    insn definition in case the back end requests it by defining the
1330    mnemonic attribute.  The values for the attribute will be extracted
1331    from the output patterns of the insn definitions as far as
1332    possible.  */
1333
1334 static void
1335 gen_mnemonic_attr (void)
1336 {
1337   struct queue_elem *elem;
1338   rtx mnemonic_attr = NULL;
1339   htab_t mnemonic_htab;
1340   const char *str, *p;
1341   int i;
1342
1343   if (have_error)
1344     return;
1345
1346   /* Look for the DEFINE_ATTR for `mnemonic'.  */
1347   for (elem = define_attr_queue; elem != *define_attr_tail; elem = elem->next)
1348     if (GET_CODE (elem->data) == DEFINE_ATTR
1349         && strcmp (XSTR (elem->data, 0), MNEMONIC_ATTR_NAME) == 0)
1350       {
1351         mnemonic_attr = elem->data;
1352         break;
1353       }
1354
1355   /* A (define_attr "mnemonic" "...") indicates that the back-end
1356      wants a mnemonic attribute to be generated.  */
1357   if (!mnemonic_attr)
1358     return;
1359
1360   mnemonic_htab = htab_create_alloc (MNEMONIC_HTAB_SIZE, htab_hash_string,
1361                                      htab_eq_string, 0, xcalloc, free);
1362
1363   for (elem = define_insn_queue; elem; elem = elem->next)
1364     {
1365       rtx insn = elem->data;
1366       bool found = false;
1367
1368       /* Check if the insn definition already has
1369          (set_attr "mnemonic" ...).  */
1370       if (XVEC (insn, 4))
1371         for (i = 0; i < XVECLEN (insn, 4); i++)
1372           if (strcmp (XSTR (XVECEXP (insn, 4, i), 0), MNEMONIC_ATTR_NAME) == 0)
1373             {
1374               found = true;
1375               break;
1376             }
1377
1378       if (!found)
1379         gen_mnemonic_setattr (mnemonic_htab, insn);
1380     }
1381
1382   /* Add the user defined values to the hash table.  */
1383   str = XSTR (mnemonic_attr, 1);
1384   while ((p = scan_comma_elt (&str)) != NULL)
1385     add_mnemonic_string (mnemonic_htab, p, str - p);
1386
1387   htab_traverse (mnemonic_htab, mnemonic_htab_callback, NULL);
1388
1389   /* Replace the last ',' with the zero end character.  */
1390   *((char *)obstack_next_free (&string_obstack) - 1) = '\0';
1391   XSTR (mnemonic_attr, 1) = XOBFINISH (&string_obstack, char *);
1392 }
1393
1394 /* The entry point for initializing the reader.  */
1395
1396 bool
1397 init_rtx_reader_args_cb (int argc, char **argv,
1398                          bool (*parse_opt) (const char *))
1399 {
1400   /* Prepare to read input.  */
1401   condition_table = htab_create (500, hash_c_test, cmp_c_test, NULL);
1402   init_predicate_table ();
1403   obstack_init (rtl_obstack);
1404
1405   /* Start at 1, to make 0 available for CODE_FOR_nothing.  */
1406   sequence_num = 1;
1407
1408   read_md_files (argc, argv, parse_opt, rtx_handle_directive);
1409
1410   /* Process define_cond_exec patterns.  */
1411   if (define_cond_exec_queue != NULL)
1412     process_define_cond_exec ();
1413
1414   if (define_attr_queue != NULL)
1415     gen_mnemonic_attr ();
1416
1417   return !have_error;
1418 }
1419
1420 /* Programs that don't have their own options can use this entry point
1421    instead.  */
1422 bool
1423 init_rtx_reader_args (int argc, char **argv)
1424 {
1425   return init_rtx_reader_args_cb (argc, argv, 0);
1426 }
1427 \f
1428 /* The entry point for reading a single rtx from an md file.  Return
1429    the rtx, or NULL if the md file has been fully processed.
1430    Return the line where the rtx was found in LINENO.
1431    Return the number of code generating rtx'en read since the start
1432    of the md file in SEQNR.  */
1433
1434 rtx
1435 read_md_rtx (int *lineno, int *seqnr)
1436 {
1437   struct queue_elem **queue, *elem;
1438   rtx desc;
1439
1440  discard:
1441
1442   /* Read all patterns from a given queue before moving on to the next.  */
1443   if (define_attr_queue != NULL)
1444     queue = &define_attr_queue;
1445   else if (define_pred_queue != NULL)
1446     queue = &define_pred_queue;
1447   else if (define_insn_queue != NULL)
1448     queue = &define_insn_queue;
1449   else if (other_queue != NULL)
1450     queue = &other_queue;
1451   else
1452     return NULL_RTX;
1453
1454   elem = *queue;
1455   *queue = elem->next;
1456   desc = elem->data;
1457   read_md_filename = elem->filename;
1458   *lineno = elem->lineno;
1459   *seqnr = sequence_num;
1460
1461   free (elem);
1462
1463   /* Discard insn patterns which we know can never match (because
1464      their C test is provably always false).  If insn_elision is
1465      false, our caller needs to see all the patterns.  Note that the
1466      elided patterns are never counted by the sequence numbering; it
1467      is the caller's responsibility, when insn_elision is false, not
1468      to use elided pattern numbers for anything.  */
1469   switch (GET_CODE (desc))
1470     {
1471     case DEFINE_INSN:
1472     case DEFINE_EXPAND:
1473       if (maybe_eval_c_test (XSTR (desc, 2)) != 0)
1474         sequence_num++;
1475       else if (insn_elision)
1476         goto discard;
1477
1478       /* *seqnr is used here so the name table will match caller's
1479          idea of insn numbering, whether or not elision is active.  */
1480       record_insn_name (*seqnr, XSTR (desc, 0));
1481       break;
1482
1483     case DEFINE_SPLIT:
1484     case DEFINE_PEEPHOLE:
1485     case DEFINE_PEEPHOLE2:
1486       if (maybe_eval_c_test (XSTR (desc, 1)) != 0)
1487         sequence_num++;
1488       else if (insn_elision)
1489             goto discard;
1490       break;
1491
1492     default:
1493       break;
1494     }
1495
1496   return desc;
1497 }
1498
1499 /* Helper functions for insn elision.  */
1500
1501 /* Compute a hash function of a c_test structure, which is keyed
1502    by its ->expr field.  */
1503 hashval_t
1504 hash_c_test (const void *x)
1505 {
1506   const struct c_test *a = (const struct c_test *) x;
1507   const unsigned char *base, *s = (const unsigned char *) a->expr;
1508   hashval_t hash;
1509   unsigned char c;
1510   unsigned int len;
1511
1512   base = s;
1513   hash = 0;
1514
1515   while ((c = *s++) != '\0')
1516     {
1517       hash += c + (c << 17);
1518       hash ^= hash >> 2;
1519     }
1520
1521   len = s - base;
1522   hash += len + (len << 17);
1523   hash ^= hash >> 2;
1524
1525   return hash;
1526 }
1527
1528 /* Compare two c_test expression structures.  */
1529 int
1530 cmp_c_test (const void *x, const void *y)
1531 {
1532   const struct c_test *a = (const struct c_test *) x;
1533   const struct c_test *b = (const struct c_test *) y;
1534
1535   return !strcmp (a->expr, b->expr);
1536 }
1537
1538 /* Given a string representing a C test expression, look it up in the
1539    condition_table and report whether or not its value is known
1540    at compile time.  Returns a tristate: 1 for known true, 0 for
1541    known false, -1 for unknown.  */
1542 int
1543 maybe_eval_c_test (const char *expr)
1544 {
1545   const struct c_test *test;
1546   struct c_test dummy;
1547
1548   if (expr[0] == 0)
1549     return 1;
1550
1551   dummy.expr = expr;
1552   test = (const struct c_test *)htab_find (condition_table, &dummy);
1553   if (!test)
1554     return -1;
1555   return test->value;
1556 }
1557
1558 /* Record the C test expression EXPR in the condition_table, with
1559    value VAL.  Duplicates clobber previous entries.  */
1560
1561 void
1562 add_c_test (const char *expr, int value)
1563 {
1564   struct c_test *test;
1565
1566   if (expr[0] == 0)
1567     return;
1568
1569   test = XNEW (struct c_test);
1570   test->expr = expr;
1571   test->value = value;
1572
1573   *(htab_find_slot (condition_table, test, INSERT)) = test;
1574 }
1575
1576 /* For every C test, call CALLBACK with two arguments: a pointer to
1577    the condition structure and INFO.  Stops when CALLBACK returns zero.  */
1578 void
1579 traverse_c_tests (htab_trav callback, void *info)
1580 {
1581   if (condition_table)
1582     htab_traverse (condition_table, callback, info);
1583 }
1584
1585 /* Helper functions for define_predicate and define_special_predicate
1586    processing.  Shared between genrecog.c and genpreds.c.  */
1587
1588 static htab_t predicate_table;
1589 struct pred_data *first_predicate;
1590 static struct pred_data **last_predicate = &first_predicate;
1591
1592 static hashval_t
1593 hash_struct_pred_data (const void *ptr)
1594 {
1595   return htab_hash_string (((const struct pred_data *)ptr)->name);
1596 }
1597
1598 static int
1599 eq_struct_pred_data (const void *a, const void *b)
1600 {
1601   return !strcmp (((const struct pred_data *)a)->name,
1602                   ((const struct pred_data *)b)->name);
1603 }
1604
1605 struct pred_data *
1606 lookup_predicate (const char *name)
1607 {
1608   struct pred_data key;
1609   key.name = name;
1610   return (struct pred_data *) htab_find (predicate_table, &key);
1611 }
1612
1613 /* Record that predicate PRED can accept CODE.  */
1614
1615 void
1616 add_predicate_code (struct pred_data *pred, enum rtx_code code)
1617 {
1618   if (!pred->codes[code])
1619     {
1620       pred->num_codes++;
1621       pred->codes[code] = true;
1622
1623       if (GET_RTX_CLASS (code) != RTX_CONST_OBJ)
1624         pred->allows_non_const = true;
1625
1626       if (code != REG
1627           && code != SUBREG
1628           && code != MEM
1629           && code != CONCAT
1630           && code != PARALLEL
1631           && code != STRICT_LOW_PART)
1632         pred->allows_non_lvalue = true;
1633
1634       if (pred->num_codes == 1)
1635         pred->singleton = code;
1636       else if (pred->num_codes == 2)
1637         pred->singleton = UNKNOWN;
1638     }
1639 }
1640
1641 void
1642 add_predicate (struct pred_data *pred)
1643 {
1644   void **slot = htab_find_slot (predicate_table, pred, INSERT);
1645   if (*slot)
1646     {
1647       error ("duplicate predicate definition for '%s'", pred->name);
1648       return;
1649     }
1650   *slot = pred;
1651   *last_predicate = pred;
1652   last_predicate = &pred->next;
1653 }
1654
1655 /* This array gives the initial content of the predicate table.  It
1656    has entries for all predicates defined in recog.c.  */
1657
1658 struct std_pred_table
1659 {
1660   const char *name;
1661   bool special;
1662   bool allows_const_p;
1663   RTX_CODE codes[NUM_RTX_CODE];
1664 };
1665
1666 static const struct std_pred_table std_preds[] = {
1667   {"general_operand", false, true, {SUBREG, REG, MEM}},
1668   {"address_operand", true, true, {SUBREG, REG, MEM, PLUS, MINUS, MULT}},
1669   {"register_operand", false, false, {SUBREG, REG}},
1670   {"pmode_register_operand", true, false, {SUBREG, REG}},
1671   {"scratch_operand", false, false, {SCRATCH, REG}},
1672   {"immediate_operand", false, true, {UNKNOWN}},
1673   {"const_int_operand", false, false, {CONST_INT}},
1674   {"const_double_operand", false, false, {CONST_INT, CONST_DOUBLE}},
1675   {"nonimmediate_operand", false, false, {SUBREG, REG, MEM}},
1676   {"nonmemory_operand", false, true, {SUBREG, REG}},
1677   {"push_operand", false, false, {MEM}},
1678   {"pop_operand", false, false, {MEM}},
1679   {"memory_operand", false, false, {SUBREG, MEM}},
1680   {"indirect_operand", false, false, {SUBREG, MEM}},
1681   {"ordered_comparison_operator", false, false, {EQ, NE,
1682                                                  LE, LT, GE, GT,
1683                                                  LEU, LTU, GEU, GTU}},
1684   {"comparison_operator", false, false, {EQ, NE,
1685                                          LE, LT, GE, GT,
1686                                          LEU, LTU, GEU, GTU,
1687                                          UNORDERED, ORDERED,
1688                                          UNEQ, UNGE, UNGT,
1689                                          UNLE, UNLT, LTGT}}
1690 };
1691 #define NUM_KNOWN_STD_PREDS ARRAY_SIZE (std_preds)
1692
1693 /* Initialize the table of predicate definitions, starting with
1694    the information we have on generic predicates.  */
1695
1696 static void
1697 init_predicate_table (void)
1698 {
1699   size_t i, j;
1700   struct pred_data *pred;
1701
1702   predicate_table = htab_create_alloc (37, hash_struct_pred_data,
1703                                        eq_struct_pred_data, 0,
1704                                        xcalloc, free);
1705
1706   for (i = 0; i < NUM_KNOWN_STD_PREDS; i++)
1707     {
1708       pred = XCNEW (struct pred_data);
1709       pred->name = std_preds[i].name;
1710       pred->special = std_preds[i].special;
1711
1712       for (j = 0; std_preds[i].codes[j] != 0; j++)
1713         add_predicate_code (pred, std_preds[i].codes[j]);
1714
1715       if (std_preds[i].allows_const_p)
1716         for (j = 0; j < NUM_RTX_CODE; j++)
1717           if (GET_RTX_CLASS (j) == RTX_CONST_OBJ)
1718             add_predicate_code (pred, (enum rtx_code) j);
1719
1720       add_predicate (pred);
1721     }
1722 }
1723 \f
1724 /* These functions allow linkage with print-rtl.c.  Also, some generators
1725    like to annotate their output with insn names.  */
1726
1727 /* Holds an array of names indexed by insn_code_number.  */
1728 static char **insn_name_ptr = 0;
1729 static int insn_name_ptr_size = 0;
1730
1731 const char *
1732 get_insn_name (int code)
1733 {
1734   if (code < insn_name_ptr_size)
1735     return insn_name_ptr[code];
1736   else
1737     return NULL;
1738 }
1739
1740 static void
1741 record_insn_name (int code, const char *name)
1742 {
1743   static const char *last_real_name = "insn";
1744   static int last_real_code = 0;
1745   char *new_name;
1746
1747   if (insn_name_ptr_size <= code)
1748     {
1749       int new_size;
1750       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
1751       insn_name_ptr = XRESIZEVEC (char *, insn_name_ptr, new_size);
1752       memset (insn_name_ptr + insn_name_ptr_size, 0,
1753               sizeof(char *) * (new_size - insn_name_ptr_size));
1754       insn_name_ptr_size = new_size;
1755     }
1756
1757   if (!name || name[0] == '\0')
1758     {
1759       new_name = XNEWVAR (char, strlen (last_real_name) + 10);
1760       sprintf (new_name, "%s+%d", last_real_name, code - last_real_code);
1761     }
1762   else
1763     {
1764       last_real_name = new_name = xstrdup (name);
1765       last_real_code = code;
1766     }
1767
1768   insn_name_ptr[code] = new_name;
1769 }
1770 \f
1771 /* Make STATS describe the operands that appear in rtx X.  */
1772
1773 static void
1774 get_pattern_stats_1 (struct pattern_stats *stats, rtx x)
1775 {
1776   RTX_CODE code;
1777   int i;
1778   int len;
1779   const char *fmt;
1780
1781   if (x == NULL_RTX)
1782     return;
1783
1784   code = GET_CODE (x);
1785   switch (code)
1786     {
1787     case MATCH_OPERAND:
1788     case MATCH_OPERATOR:
1789     case MATCH_PARALLEL:
1790       stats->max_opno = MAX (stats->max_opno, XINT (x, 0));
1791       break;
1792
1793     case MATCH_DUP:
1794     case MATCH_OP_DUP:
1795     case MATCH_PAR_DUP:
1796       stats->num_dups++;
1797       stats->max_dup_opno = MAX (stats->max_dup_opno, XINT (x, 0));
1798       break;
1799
1800     case MATCH_SCRATCH:
1801       stats->max_scratch_opno = MAX (stats->max_scratch_opno, XINT (x, 0));
1802       break;
1803
1804     default:
1805       break;
1806     }
1807
1808   fmt = GET_RTX_FORMAT (code);
1809   len = GET_RTX_LENGTH (code);
1810   for (i = 0; i < len; i++)
1811     {
1812       if (fmt[i] == 'e' || fmt[i] == 'u')
1813         get_pattern_stats_1 (stats, XEXP (x, i));
1814       else if (fmt[i] == 'E')
1815         {
1816           int j;
1817           for (j = 0; j < XVECLEN (x, i); j++)
1818             get_pattern_stats_1 (stats, XVECEXP (x, i, j));
1819         }
1820     }
1821 }
1822
1823 /* Make STATS describe the operands that appear in instruction pattern
1824    PATTERN.  */
1825
1826 void
1827 get_pattern_stats (struct pattern_stats *stats, rtvec pattern)
1828 {
1829   int i, len;
1830
1831   stats->max_opno = -1;
1832   stats->max_dup_opno = -1;
1833   stats->max_scratch_opno = -1;
1834   stats->num_dups = 0;
1835
1836   len = GET_NUM_ELEM (pattern);
1837   for (i = 0; i < len; i++)
1838     get_pattern_stats_1 (stats, RTVEC_ELT (pattern, i));
1839
1840   stats->num_generator_args = stats->max_opno + 1;
1841   stats->num_insn_operands = MAX (stats->max_opno,
1842                                   stats->max_scratch_opno) + 1;
1843   stats->num_operand_vars = MAX (stats->max_opno,
1844                                   MAX (stats->max_dup_opno,
1845                                        stats->max_scratch_opno)) + 1;
1846 }