OSDN Git Service

2000-07-21 Alexandre Petit-Bianco <apbianco@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / genrecog.c
1 /* Generate code from machine description to recognize rtl as insns.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4
5    This file is part of GNU CC.
6
7    GNU CC is free software; you can redistribute it and/or modify
8    it 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    GNU CC is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GNU CC; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22
23 /* This program is used to produce insn-recog.c, which contains a
24    function called `recog' plus its subroutines.  These functions
25    contain a decision tree that recognizes whether an rtx, the
26    argument given to recog, is a valid instruction.
27
28    recog returns -1 if the rtx is not valid.  If the rtx is valid,
29    recog returns a nonnegative number which is the insn code number
30    for the pattern that matched.  This is the same as the order in the
31    machine description of the entry that matched.  This number can be
32    used as an index into various insn_* tables, such as insn_template,
33    insn_outfun, and insn_n_operands (found in insn-output.c).
34
35    The third argument to recog is an optional pointer to an int.  If
36    present, recog will accept a pattern if it matches except for
37    missing CLOBBER expressions at the end.  In that case, the value
38    pointed to by the optional pointer will be set to the number of
39    CLOBBERs that need to be added (it should be initialized to zero by
40    the caller).  If it is set nonzero, the caller should allocate a
41    PARALLEL of the appropriate size, copy the initial entries, and
42    call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
43
44    This program also generates the function `split_insns', which
45    returns 0 if the rtl could not be split, or it returns the split
46    rtl in a SEQUENCE.
47
48    This program also generates the function `peephole2_insns', which
49    returns 0 if the rtl could not be matched.  If there was a match,
50    the new rtl is returned in a SEQUENCE, and LAST_INSN will point
51    to the last recognized insn in the old sequence.  */
52
53 #include "hconfig.h"
54 #include "system.h"
55 #include "rtl.h"
56 #include "errors.h"
57 #include "gensupport.h"
58
59
60 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \
61   printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER))
62
63 /* Holds an array of names indexed by insn_code_number.  */
64 static char **insn_name_ptr = 0;
65 static int insn_name_ptr_size = 0;
66
67 /* A listhead of decision trees.  The alternatives to a node are kept
68    in a doublely-linked list so we can easily add nodes to the proper
69    place when merging.  */
70
71 struct decision_head
72 {
73   struct decision *first;
74   struct decision *last;
75 };
76     
77 /* A single test.  The two accept types aren't tests per-se, but
78    their equality (or lack thereof) does affect tree merging so
79    it is convenient to keep them here.  */
80
81 struct decision_test
82 {
83   /* A linked list through the tests attached to a node.  */
84   struct decision_test *next;
85
86   /* These types are roughly in the order in which we'd like to test them.  */
87   enum decision_type {
88     DT_mode, DT_code, DT_veclen,
89     DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide,
90     DT_dup, DT_pred, DT_c_test, 
91     DT_accept_op, DT_accept_insn
92   } type;
93
94   union
95   {
96     enum machine_mode mode;     /* Machine mode of node.  */
97     RTX_CODE code;              /* Code to test.  */
98
99     struct
100     {
101       const char *name;         /* Predicate to call.  */
102       int index;                /* Index into `preds' or -1.  */
103       enum machine_mode mode;   /* Machine mode for node.  */
104     } pred;
105
106     const char *c_test;         /* Additional test to perform.  */
107     int veclen;                 /* Length of vector.  */
108     int dup;                    /* Number of operand to compare against.  */
109     HOST_WIDE_INT intval;       /* Value for XINT for XWINT.  */
110     int opno;                   /* Operand number matched.  */
111
112     struct {
113       int code_number;          /* Insn number matched.  */
114       int lineno;               /* Line number of the insn.  */
115       int num_clobbers_to_add;  /* Number of CLOBBERs to be added.  */
116     } insn;
117   } u;
118 };
119
120 /* Data structure for decision tree for recognizing legitimate insns.  */
121
122 struct decision
123 {
124   struct decision_head success; /* Nodes to test on success.  */
125   struct decision *next;        /* Node to test on failure.  */
126   struct decision *prev;        /* Node whose failure tests us.  */
127   struct decision *afterward;   /* Node to test on success,
128                                    but failure of successor nodes.  */
129
130   const char *position;         /* String denoting position in pattern.  */
131
132   struct decision_test *tests;  /* The tests for this node.  */
133
134   int number;                   /* Node number, used for labels */
135   int subroutine_number;        /* Number of subroutine this node starts */
136   int need_label;               /* Label needs to be output.  */
137 };
138
139 #define SUBROUTINE_THRESHOLD    100
140
141 static int next_subroutine_number;
142
143 /* We can write three types of subroutines: One for insn recognition,
144    one to split insns, and one for peephole-type optimizations.  This
145    defines which type is being written.  */
146
147 enum routine_type {
148   RECOG, SPLIT, PEEPHOLE2
149 };
150
151 #define IS_SPLIT(X) ((X) != RECOG)
152
153 /* Next available node number for tree nodes.  */
154
155 static int next_number;
156
157 /* Next number to use as an insn_code.  */
158
159 static int next_insn_code;
160
161 /* Similar, but counts all expressions in the MD file; used for
162    error messages.  */
163
164 static int next_index;
165
166 /* Record the highest depth we ever have so we know how many variables to
167    allocate in each subroutine we make.  */
168
169 static int max_depth;
170
171 /* The line number of the start of the pattern currently being processed.  */
172 static int pattern_lineno;
173
174 /* Count of errors.  */
175 static int error_count;
176 \f
177 /* This table contains a list of the rtl codes that can possibly match a
178    predicate defined in recog.c.  The function `maybe_both_true' uses it to
179    deduce that there are no expressions that can be matches by certain pairs
180    of tree nodes.  Also, if a predicate can match only one code, we can
181    hardwire that code into the node testing the predicate.  */
182
183 static struct pred_table
184 {
185   const char *name;
186   RTX_CODE codes[NUM_RTX_CODE];
187 } preds[] = {
188   {"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
189                        LABEL_REF, SUBREG, REG, MEM}},
190 #ifdef PREDICATE_CODES
191   PREDICATE_CODES
192 #endif
193   {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
194                        LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
195   {"register_operand", {SUBREG, REG}},
196   {"pmode_register_operand", {SUBREG, REG}},
197   {"scratch_operand", {SCRATCH, REG}},
198   {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
199                          LABEL_REF}},
200   {"const_int_operand", {CONST_INT}},
201   {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
202   {"nonimmediate_operand", {SUBREG, REG, MEM}},
203   {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
204                          LABEL_REF, SUBREG, REG}},
205   {"push_operand", {MEM}},
206   {"pop_operand", {MEM}},
207   {"memory_operand", {SUBREG, MEM}},
208   {"indirect_operand", {SUBREG, MEM}},
209   {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU,
210                            UNORDERED, ORDERED, UNEQ, UNGE, UNGT, UNLE,
211                            UNLT, LTGT}},
212   {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
213                                 LABEL_REF, SUBREG, REG, MEM}}
214 };
215
216 #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
217
218 static const char * special_mode_pred_table[] = {
219 #ifdef SPECIAL_MODE_PREDICATES
220   SPECIAL_MODE_PREDICATES
221 #endif
222   "pmode_register_operand"
223 };
224
225 #define NUM_SPECIAL_MODE_PREDS \
226   (sizeof (special_mode_pred_table) / sizeof (special_mode_pred_table[0]))
227
228 static struct decision *new_decision
229   PARAMS ((const char *, struct decision_head *));
230 static struct decision_test *new_decision_test
231   PARAMS ((enum decision_type, struct decision_test ***));
232 static rtx find_operand
233   PARAMS ((rtx, int));
234 static void validate_pattern
235   PARAMS ((rtx, rtx, rtx));
236 static struct decision *add_to_sequence
237   PARAMS ((rtx, struct decision_head *, const char *, enum routine_type, int));
238
239 static int maybe_both_true_2
240   PARAMS ((struct decision_test *, struct decision_test *));
241 static int maybe_both_true_1
242   PARAMS ((struct decision_test *, struct decision_test *));
243 static int maybe_both_true
244   PARAMS ((struct decision *, struct decision *, int));
245
246 static int nodes_identical_1
247   PARAMS ((struct decision_test *, struct decision_test *));
248 static int nodes_identical
249   PARAMS ((struct decision *, struct decision *));
250 static void merge_accept_insn
251   PARAMS ((struct decision *, struct decision *));
252 static void merge_trees
253   PARAMS ((struct decision_head *, struct decision_head *));
254
255 static void factor_tests
256   PARAMS ((struct decision_head *));
257 static void simplify_tests
258   PARAMS ((struct decision_head *));
259 static int break_out_subroutines
260   PARAMS ((struct decision_head *, int));
261 static void find_afterward
262   PARAMS ((struct decision_head *, struct decision *));
263
264 static void change_state
265   PARAMS ((const char *, const char *, struct decision *, const char *));
266 static void print_code
267   PARAMS ((enum rtx_code));
268 static void write_afterward
269   PARAMS ((struct decision *, struct decision *, const char *));
270 static struct decision *write_switch
271   PARAMS ((struct decision *, int));
272 static void write_cond
273   PARAMS ((struct decision_test *, int, enum routine_type));
274 static void write_action
275   PARAMS ((struct decision *, struct decision_test *, int, int,
276            struct decision *, enum routine_type));
277 static int is_unconditional
278   PARAMS ((struct decision_test *, enum routine_type));
279 static int write_node
280   PARAMS ((struct decision *, int, enum routine_type));
281 static void write_tree_1
282   PARAMS ((struct decision_head *, int, enum routine_type));
283 static void write_tree
284   PARAMS ((struct decision_head *, const char *, enum routine_type, int));
285 static void write_subroutine
286   PARAMS ((struct decision_head *, enum routine_type));
287 static void write_subroutines
288   PARAMS ((struct decision_head *, enum routine_type));
289 static void write_header
290   PARAMS ((void));
291
292 static struct decision_head make_insn_sequence
293   PARAMS ((rtx, enum routine_type));
294 static void process_tree
295   PARAMS ((struct decision_head *, enum routine_type));
296   
297 static void record_insn_name
298   PARAMS ((int, const char *));
299
300 static void debug_decision_0
301   PARAMS ((struct decision *, int, int));
302 static void debug_decision_1
303   PARAMS ((struct decision *, int));
304 static void debug_decision_2
305   PARAMS ((struct decision_test *));
306 extern void debug_decision
307   PARAMS ((struct decision *));
308 extern void debug_decision_list
309   PARAMS ((struct decision *));
310 \f
311 /* Create a new node in sequence after LAST.  */
312
313 static struct decision *
314 new_decision (position, last)
315      const char *position;
316      struct decision_head *last;
317 {
318   register struct decision *new
319     = (struct decision *) xmalloc (sizeof (struct decision));
320
321   memset (new, 0, sizeof (*new));
322   new->success = *last;
323   new->position = xstrdup (position);
324   new->number = next_number++;
325
326   last->first = last->last = new;
327   return new;
328 }
329
330 /* Create a new test and link it in at PLACE.  */
331
332 static struct decision_test *
333 new_decision_test (type, pplace)
334      enum decision_type type;
335      struct decision_test ***pplace;
336 {
337   struct decision_test **place = *pplace;
338   struct decision_test *test;
339
340   test = (struct decision_test *) xmalloc (sizeof (*test));
341   test->next = *place;
342   test->type = type;
343   *place = test;
344
345   place = &test->next;
346   *pplace = place;
347
348   return test;
349 }
350
351 /* Search for and return operand N.  */
352
353 static rtx
354 find_operand (pattern, n)
355      rtx pattern;
356      int n;
357 {
358   const char *fmt;
359   RTX_CODE code;
360   int i, j, len;
361   rtx r;
362
363   code = GET_CODE (pattern);
364   if ((code == MATCH_SCRATCH
365        || code == MATCH_INSN
366        || code == MATCH_OPERAND
367        || code == MATCH_OPERATOR
368        || code == MATCH_PARALLEL)
369       && XINT (pattern, 0) == n)
370     return pattern;
371
372   fmt = GET_RTX_FORMAT (code);
373   len = GET_RTX_LENGTH (code);
374   for (i = 0; i < len; i++)
375     {
376       switch (fmt[i])
377         {
378         case 'e': case 'u':
379           if ((r = find_operand (XEXP (pattern, i), n)) != NULL_RTX)
380             return r;
381           break;
382
383         case 'E':
384           for (j = 0; j < XVECLEN (pattern, i); j++)
385             if ((r = find_operand (XVECEXP (pattern, i, j), n)) != NULL_RTX)
386               return r;
387           break;
388
389         case 'i': case 'w': case '0': case 's':
390           break;
391
392         default:
393           abort ();
394         }
395     }
396
397   return NULL;
398 }
399
400 /* Check for various errors in patterns.  SET is nonnull for a destination,
401    and is the complete set pattern.  */
402
403 static void
404 validate_pattern (pattern, insn, set)
405      rtx pattern;
406      rtx insn;
407      rtx set;
408 {
409   const char *fmt;
410   RTX_CODE code;
411   size_t i, len;
412   int j;
413
414   code = GET_CODE (pattern);
415   switch (code)
416     {
417     case MATCH_SCRATCH:
418       return;
419
420     case MATCH_INSN:
421     case MATCH_OPERAND:
422     case MATCH_OPERATOR:
423       {
424         const char *pred_name = XSTR (pattern, 1);
425         int allows_non_lvalue = 1, allows_non_const = 1;
426         int special_mode_pred = 0;
427         const char *c_test;
428
429         if (GET_CODE (insn) == DEFINE_INSN)
430           c_test = XSTR (insn, 2);
431         else
432           c_test = XSTR (insn, 1);
433
434         if (pred_name[0] != 0)
435           {
436             for (i = 0; i < NUM_KNOWN_PREDS; i++)
437               if (! strcmp (preds[i].name, pred_name))
438                 break;
439
440             if (i < NUM_KNOWN_PREDS)
441               {
442                 int j;
443
444                 allows_non_lvalue = allows_non_const = 0;
445                 for (j = 0; preds[i].codes[j] != 0; j++)
446                   {
447                     RTX_CODE c = preds[i].codes[j];
448                     if (c != LABEL_REF
449                         && c != SYMBOL_REF
450                         && c != CONST_INT
451                         && c != CONST_DOUBLE
452                         && c != CONST
453                         && c != HIGH
454                         && c != CONSTANT_P_RTX)
455                       allows_non_const = 1;
456
457                     if (c != REG
458                         && c != SUBREG
459                         && c != MEM
460                         && c != CONCAT
461                         && c != PARALLEL
462                         && c != STRICT_LOW_PART)
463                       allows_non_lvalue = 1;
464                   }
465               }
466             else
467               {
468 #ifdef PREDICATE_CODES
469                 /* If the port has a list of the predicates it uses but
470                    omits one, warn.  */
471                 message_with_line (pattern_lineno,
472                                    "warning: `%s' not in PREDICATE_CODES",
473                                    pred_name);
474 #endif
475               }
476
477             for (i = 0; i < NUM_SPECIAL_MODE_PREDS; ++i)
478               if (strcmp (pred_name, special_mode_pred_table[i]) == 0)
479                 {
480                   special_mode_pred = 1;
481                   break;
482                 }
483           }
484
485         /* A MATCH_OPERAND that is a SET should have an output reload.  */
486         if (set
487             && code == MATCH_OPERAND
488             && XSTR (pattern, 2)[0] != '\0'
489             && XSTR (pattern, 2)[0] != '='
490             && XSTR (pattern, 2)[0] != '+')
491           {
492             message_with_line (pattern_lineno,
493                                "operand %d missing output reload", 
494                                XINT (pattern, 0));
495             error_count++;
496           }
497
498         /* Allowing non-lvalues in destinations -- particularly CONST_INT --
499            while not likely to occur at runtime, results in less efficient
500            code from insn-recog.c.  */
501         if (set
502             && pred_name[0] != '\0'
503             && allows_non_lvalue)
504           {
505             message_with_line (pattern_lineno,
506                         "warning: destination operand %d allows non-lvalue",
507                         XINT (pattern, 0));
508           }
509
510         /* A modeless MATCH_OPERAND can be handy when we can
511            check for multiple modes in the c_test.  In most other cases,
512            it is a mistake.  Only DEFINE_INSN is eligible, since SPLIT
513            and PEEP2 can FAIL within the output pattern.  Exclude 
514            address_operand, since its mode is related to the mode of
515            the memory not the operand.  Exclude the SET_DEST of a call
516            instruction, as that is a common idiom.  */
517
518         if (GET_MODE (pattern) == VOIDmode
519             && code == MATCH_OPERAND
520             && GET_CODE (insn) == DEFINE_INSN
521             && allows_non_const
522             && ! special_mode_pred
523             && pred_name[0] != '\0'
524             && strcmp (pred_name, "address_operand") != 0
525             && strstr (c_test, "operands") == NULL
526             && ! (set
527                   && GET_CODE (set) == SET
528                   && GET_CODE (SET_SRC (set)) == CALL))
529           {
530             message_with_line (pattern_lineno,
531                                "warning: operand %d missing mode?",
532                                XINT (pattern, 0));
533           }
534         return;
535       }
536
537     case SET:
538       {
539         enum machine_mode dmode, smode;
540         rtx dest, src;
541
542         dest = SET_DEST (pattern);
543         src = SET_SRC (pattern);
544
545         /* Find the referant for a DUP.  */
546
547         if (GET_CODE (dest) == MATCH_DUP
548             || GET_CODE (dest) == MATCH_OP_DUP
549             || GET_CODE (dest) == MATCH_PAR_DUP)
550           dest = find_operand (insn, XINT (dest, 0));
551
552         if (GET_CODE (src) == MATCH_DUP
553             || GET_CODE (src) == MATCH_OP_DUP
554             || GET_CODE (src) == MATCH_PAR_DUP)
555           src = find_operand (insn, XINT (src, 0));
556
557         /* STRICT_LOW_PART is a wrapper.  Its argument is the real
558            destination, and it's mode should match the source.  */
559         if (GET_CODE (dest) == STRICT_LOW_PART)
560           dest = XEXP (dest, 0);
561
562         dmode = GET_MODE (dest);
563         smode = GET_MODE (src);
564
565         /* The mode of an ADDRESS_OPERAND is the mode of the memory
566            reference, not the mode of the address.  */
567         if (GET_CODE (src) == MATCH_OPERAND
568             && ! strcmp (XSTR (src, 1), "address_operand"))
569           ;
570
571         /* The operands of a SET must have the same mode unless one
572            is VOIDmode.  */
573         else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode)
574           {
575             message_with_line (pattern_lineno,
576                                "mode mismatch in set: %smode vs %smode",
577                                GET_MODE_NAME (dmode), GET_MODE_NAME (smode));
578             error_count++;
579           }
580
581         /* If only one of the operands is VOIDmode, and PC or CC0 is 
582            not involved, it's probably a mistake.  */
583         else if (dmode != smode
584                  && GET_CODE (dest) != PC
585                  && GET_CODE (dest) != CC0
586                  && GET_CODE (src) != PC
587                  && GET_CODE (src) != CC0
588                  && GET_CODE (src) != CONST_INT)
589           {
590             const char *which;
591             which = (dmode == VOIDmode ? "destination" : "source");
592             message_with_line (pattern_lineno,
593                                "warning: %s missing a mode?", which);
594           }
595
596         if (dest != SET_DEST (pattern))
597           validate_pattern (dest, insn, pattern);
598         validate_pattern (SET_DEST (pattern), insn, pattern);
599         validate_pattern (SET_SRC (pattern), insn, NULL_RTX);
600         return;
601       }
602
603     case CLOBBER:
604       validate_pattern (SET_DEST (pattern), insn, pattern);
605       return;
606
607     case LABEL_REF:
608       if (GET_MODE (XEXP (pattern, 0)) != VOIDmode)
609         {
610           message_with_line (pattern_lineno,
611                              "operand to label_ref %smode not VOIDmode",
612                              GET_MODE_NAME (GET_MODE (XEXP (pattern, 0))));
613           error_count++;
614         }
615       break;
616
617     default:
618       break;
619     }
620
621   fmt = GET_RTX_FORMAT (code);
622   len = GET_RTX_LENGTH (code);
623   for (i = 0; i < len; i++)
624     {
625       switch (fmt[i])
626         {
627         case 'e': case 'u':
628           validate_pattern (XEXP (pattern, i), insn, NULL_RTX);
629           break;
630
631         case 'E':
632           for (j = 0; j < XVECLEN (pattern, i); j++)
633             validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX);
634           break;
635
636         case 'i': case 'w': case '0': case 's':
637           break;
638
639         default:
640           abort ();
641         }
642     }
643 }
644
645 /* Create a chain of nodes to verify that an rtl expression matches
646    PATTERN.
647
648    LAST is a pointer to the listhead in the previous node in the chain (or
649    in the calling function, for the first node).
650
651    POSITION is the string representing the current position in the insn.
652
653    INSN_TYPE is the type of insn for which we are emitting code.
654
655    A pointer to the final node in the chain is returned.  */
656
657 static struct decision *
658 add_to_sequence (pattern, last, position, insn_type, top)
659      rtx pattern;
660      struct decision_head *last;
661      const char *position;
662      enum routine_type insn_type;
663      int top;
664 {
665   RTX_CODE code;
666   struct decision *this, *sub;
667   struct decision_test *test;
668   struct decision_test **place;
669   char *subpos;
670   register size_t i;
671   register const char *fmt;
672   int depth = strlen (position);
673   int len;
674   enum machine_mode mode;
675
676   if (depth > max_depth)
677     max_depth = depth;
678
679   subpos = (char *) alloca (depth + 2);
680   strcpy (subpos, position);
681   subpos[depth + 1] = 0;
682
683   sub = this = new_decision (position, last);
684   place = &this->tests;
685
686  restart:
687   mode = GET_MODE (pattern);
688   code = GET_CODE (pattern);
689
690   switch (code)
691     {
692     case PARALLEL:
693       /* Toplevel peephole pattern. */
694       if (insn_type == PEEPHOLE2 && top)
695         {
696           /* We don't need the node we just created -- unlink it.  */
697           last->first = last->last = NULL;
698
699           for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++)
700             {
701               /* Which insn we're looking at is represented by A-Z. We don't
702                  ever use 'A', however; it is always implied. */
703
704               subpos[depth] = (i > 0 ? 'A' + i : 0);
705               sub = add_to_sequence (XVECEXP (pattern, 0, i),
706                                      last, subpos, insn_type, 0);
707               last = &sub->success;
708             }
709           return sub;
710         }
711
712       /* Else nothing special.  */
713       break;
714
715     case MATCH_OPERAND:
716     case MATCH_SCRATCH:
717     case MATCH_OPERATOR:
718     case MATCH_PARALLEL:
719     case MATCH_INSN:
720       {
721         const char *pred_name;
722         RTX_CODE was_code = code;
723         int allows_const_int = 1;
724
725         if (code == MATCH_SCRATCH)
726           {
727             pred_name = "scratch_operand";
728             code = UNKNOWN;
729           }
730         else
731           {
732             pred_name = XSTR (pattern, 1);
733             if (code == MATCH_PARALLEL)
734               code = PARALLEL;
735             else
736               code = UNKNOWN;
737           }
738
739         /* We know exactly what const_int_operand matches -- any CONST_INT.  */
740         if (strcmp ("const_int_operand", pred_name) == 0)
741           {
742             code = CONST_INT;
743             mode = VOIDmode;
744           }
745         else if (pred_name[0] != 0)
746           {
747             test = new_decision_test (DT_pred, &place);
748             test->u.pred.name = pred_name;
749             test->u.pred.mode = mode;
750
751             /* See if we know about this predicate and save its number.  If
752                we do, and it only accepts one code, note that fact.  The
753                predicate `const_int_operand' only tests for a CONST_INT, so
754                if we do so we can avoid calling it at all.
755
756                Finally, if we know that the predicate does not allow
757                CONST_INT, we know that the only way the predicate can match
758                is if the modes match (here we use the kludge of relying on
759                the fact that "address_operand" accepts CONST_INT; otherwise,
760                it would have to be a special case), so we can test the mode
761                (but we need not).  This fact should considerably simplify the
762                generated code.  */
763
764             for (i = 0; i < NUM_KNOWN_PREDS; i++)
765               if (! strcmp (preds[i].name, pred_name))
766                 break;
767
768             if (i < NUM_KNOWN_PREDS)
769               {
770                 int j;
771
772                 test->u.pred.index = i;
773
774                 if (preds[i].codes[1] == 0 && code == UNKNOWN)
775                   code = preds[i].codes[0];
776
777                 allows_const_int = 0;
778                 for (j = 0; preds[i].codes[j] != 0; j++)
779                   if (preds[i].codes[j] == CONST_INT)
780                     {
781                       allows_const_int = 1;
782                       break;
783                     }
784               }
785             else
786               test->u.pred.index = -1;
787           }
788
789         /* Can't enforce a mode if we allow const_int.  */
790         if (allows_const_int)
791           mode = VOIDmode;
792
793         /* Accept the operand, ie. record it in `operands'.  */
794         test = new_decision_test (DT_accept_op, &place);
795         test->u.opno = XINT (pattern, 0);
796
797         if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL)
798           {
799             char base = (was_code == MATCH_OPERATOR ? '0' : 'a');
800             for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++)
801               {
802                 subpos[depth] = i + base;
803                 sub = add_to_sequence (XVECEXP (pattern, 2, i),
804                                        &sub->success, subpos, insn_type, 0);
805               }
806           }
807         goto fini;
808       }
809
810     case MATCH_OP_DUP:
811       code = UNKNOWN;
812
813       test = new_decision_test (DT_dup, &place);
814       test->u.dup = XINT (pattern, 0);
815
816       test = new_decision_test (DT_accept_op, &place);
817       test->u.opno = XINT (pattern, 0);
818
819       for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++)
820         {
821           subpos[depth] = i + '0';
822           sub = add_to_sequence (XVECEXP (pattern, 1, i),
823                                  &sub->success, subpos, insn_type, 0);
824         }
825       goto fini;
826
827     case MATCH_DUP:
828     case MATCH_PAR_DUP:
829       code = UNKNOWN;
830
831       test = new_decision_test (DT_dup, &place);
832       test->u.dup = XINT (pattern, 0);
833       goto fini;
834
835     case ADDRESS:
836       pattern = XEXP (pattern, 0);
837       goto restart;
838
839     default:
840       break;
841     }
842
843   fmt = GET_RTX_FORMAT (code);
844   len = GET_RTX_LENGTH (code);
845
846   /* Do tests against the current node first.  */
847   for (i = 0; i < (size_t) len; i++)
848     {
849       if (fmt[i] == 'i')
850         {
851           if (i == 0)
852             {
853               test = new_decision_test (DT_elt_zero_int, &place);
854               test->u.intval = XINT (pattern, i);
855             }
856           else if (i == 1)
857             {
858               test = new_decision_test (DT_elt_one_int, &place);
859               test->u.intval = XINT (pattern, i);
860             }
861           else
862             abort ();
863         }
864       else if (fmt[i] == 'w')
865         {
866           if (i != 0)
867             abort ();
868
869           test = new_decision_test (DT_elt_zero_wide, &place);
870           test->u.intval = XWINT (pattern, i);
871         }
872       else if (fmt[i] == 'E')
873         {
874           if (i != 0)
875             abort ();
876
877           test = new_decision_test (DT_veclen, &place);
878           test->u.veclen = XVECLEN (pattern, i);
879         }
880     }
881
882   /* Now test our sub-patterns.  */
883   for (i = 0; i < (size_t) len; i++)
884     {
885       switch (fmt[i])
886         {
887         case 'e': case 'u':
888           subpos[depth] = '0' + i;
889           sub = add_to_sequence (XEXP (pattern, i), &sub->success,
890                                  subpos, insn_type, 0);
891           break;
892
893         case 'E':
894           {
895             register int j;
896             for (j = 0; j < XVECLEN (pattern, i); j++)
897               {
898                 subpos[depth] = 'a' + j;
899                 sub = add_to_sequence (XVECEXP (pattern, i, j),
900                                        &sub->success, subpos, insn_type, 0);
901               }
902             break;
903           }
904
905         case 'i': case 'w':
906           /* Handled above.  */
907           break;
908         case '0':
909           break;
910
911         default:
912           abort ();
913         }
914     }
915
916  fini:
917   /* Insert nodes testing mode and code, if they're still relevant,
918      before any of the nodes we may have added above.  */
919   if (code != UNKNOWN)
920     {
921       place = &this->tests;
922       test = new_decision_test (DT_code, &place);
923       test->u.code = code;
924     }
925
926   if (mode != VOIDmode)
927     {
928       place = &this->tests;
929       test = new_decision_test (DT_mode, &place);
930       test->u.mode = mode;
931     }
932
933   /* If we didn't insert any tests or accept nodes, hork.  */
934   if (this->tests == NULL)
935     abort ();
936
937   return sub;
938 }
939 \f
940 /* A subroutine of maybe_both_true; examines only one test.
941    Returns > 0 for "definitely both true" and < 0 for "maybe both true".  */
942
943 static int
944 maybe_both_true_2 (d1, d2)
945      struct decision_test *d1, *d2;
946 {
947   if (d1->type == d2->type)
948     {
949       switch (d1->type)
950         {
951         case DT_mode:
952           return d1->u.mode == d2->u.mode;
953
954         case DT_code:
955           return d1->u.code == d2->u.code;
956
957         case DT_veclen:
958           return d1->u.veclen == d2->u.veclen;
959
960         case DT_elt_zero_int:
961         case DT_elt_one_int:
962         case DT_elt_zero_wide:
963           return d1->u.intval == d2->u.intval;
964
965         default:
966           break;
967         }
968     }
969
970   /* If either has a predicate that we know something about, set
971      things up so that D1 is the one that always has a known
972      predicate.  Then see if they have any codes in common.  */
973
974   if (d1->type == DT_pred || d2->type == DT_pred)
975     {
976       if (d2->type == DT_pred)
977         {
978           struct decision_test *tmp;
979           tmp = d1, d1 = d2, d2 = tmp;
980         }
981
982       /* If D2 tests a mode, see if it matches D1.  */
983       if (d1->u.pred.mode != VOIDmode)
984         {
985           if (d2->type == DT_mode)
986             {
987               if (d1->u.pred.mode != d2->u.mode
988                   /* The mode of an address_operand predicate is the
989                      mode of the memory, not the operand.  It can only
990                      be used for testing the predicate, so we must
991                      ignore it here.  */
992                   && strcmp (d1->u.pred.name, "address_operand") != 0)
993                 return 0;
994             }
995           /* Don't check two predicate modes here, because if both predicates
996              accept CONST_INT, then both can still be true even if the modes
997              are different.  If they don't accept CONST_INT, there will be a
998              separate DT_mode that will make maybe_both_true_1 return 0.  */
999         }
1000
1001       if (d1->u.pred.index >= 0)
1002         {
1003           /* If D2 tests a code, see if it is in the list of valid
1004              codes for D1's predicate.  */
1005           if (d2->type == DT_code)
1006             {
1007               const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
1008               while (*c != 0)
1009                 {
1010                   if (*c == d2->u.code)
1011                     break;
1012                   ++c;
1013                 }
1014               if (*c == 0)
1015                 return 0;
1016             }
1017
1018           /* Otherwise see if the predicates have any codes in common.  */
1019           else if (d2->type == DT_pred && d2->u.pred.index >= 0)
1020             {
1021               const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
1022               int common = 0;
1023
1024               while (*c1 != 0 && !common)
1025                 {
1026                   const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
1027                   while (*c2 != 0 && !common)
1028                     {
1029                       common = (*c1 == *c2);
1030                       ++c2;
1031                     }
1032                   ++c1;
1033                 }
1034
1035               if (!common)
1036                 return 0;
1037             }
1038         }
1039     }
1040
1041   return -1;
1042 }
1043
1044 /* A subroutine of maybe_both_true; examines all the tests for a given node.
1045    Returns > 0 for "definitely both true" and < 0 for "maybe both true".  */
1046
1047 static int
1048 maybe_both_true_1 (d1, d2)
1049      struct decision_test *d1, *d2;
1050 {
1051   struct decision_test *t1, *t2;
1052
1053   /* A match_operand with no predicate can match anything.  Recognize
1054      this by the existance of a lone DT_accept_op test.  */
1055   if (d1->type == DT_accept_op || d2->type == DT_accept_op)
1056     return 1;
1057
1058   /* Eliminate pairs of tests while they can exactly match.  */
1059   while (d1 && d2 && d1->type == d2->type)
1060     {
1061       if (maybe_both_true_2 (d1, d2) == 0)
1062         return 0;
1063       d1 = d1->next, d2 = d2->next;
1064     }
1065
1066   /* After that, consider all pairs.  */
1067   for (t1 = d1; t1 ; t1 = t1->next)
1068     for (t2 = d2; t2 ; t2 = t2->next)
1069       if (maybe_both_true_2 (t1, t2) == 0)
1070         return 0;
1071
1072   return -1;
1073 }
1074
1075 /* Return 0 if we can prove that there is no RTL that can match both
1076    D1 and D2.  Otherwise, return 1 (it may be that there is an RTL that
1077    can match both or just that we couldn't prove there wasn't such an RTL).
1078
1079    TOPLEVEL is non-zero if we are to only look at the top level and not
1080    recursively descend.  */
1081
1082 static int
1083 maybe_both_true (d1, d2, toplevel)
1084      struct decision *d1, *d2;
1085      int toplevel;
1086 {
1087   struct decision *p1, *p2;
1088   int cmp;
1089
1090   /* Don't compare strings on the different positions in insn.  Doing so
1091      is incorrect and results in false matches from constructs like
1092
1093         [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
1094               (subreg:HI (match_operand:SI "register_operand" "r") 0))]
1095      vs
1096         [(set (match_operand:HI "register_operand" "r")
1097               (match_operand:HI "register_operand" "r"))]
1098
1099      If we are presented with such, we are recursing through the remainder
1100      of a node's success nodes (from the loop at the end of this function).
1101      Skip forward until we come to a position that matches.
1102
1103      Due to the way position strings are constructed, we know that iterating
1104      forward from the lexically lower position (e.g. "00") will run into
1105      the lexically higher position (e.g. "1") and not the other way around.
1106      This saves a bit of effort.  */
1107
1108   cmp = strcmp (d1->position, d2->position);
1109   if (cmp != 0)
1110     {
1111       if (toplevel)
1112         abort();
1113
1114       /* If the d2->position was lexically lower, swap.  */
1115       if (cmp > 0)
1116         p1 = d1, d1 = d2, d2 = p1;
1117
1118       if (d1->success.first == 0)
1119         return 0;
1120       for (p1 = d1->success.first; p1; p1 = p1->next)
1121         if (maybe_both_true (p1, d2, 0))
1122           return 1;
1123
1124       return 0;
1125     }
1126
1127   /* Test the current level.  */
1128   cmp = maybe_both_true_1 (d1->tests, d2->tests);
1129   if (cmp >= 0)
1130     return cmp;
1131
1132   /* We can't prove that D1 and D2 cannot both be true.  If we are only
1133      to check the top level, return 1.  Otherwise, see if we can prove
1134      that all choices in both successors are mutually exclusive.  If
1135      either does not have any successors, we can't prove they can't both
1136      be true.  */
1137
1138   if (toplevel || d1->success.first == 0 || d2->success.first == 0)
1139     return 1;
1140
1141   for (p1 = d1->success.first; p1; p1 = p1->next)
1142     for (p2 = d2->success.first; p2; p2 = p2->next)
1143       if (maybe_both_true (p1, p2, 0))
1144         return 1;
1145
1146   return 0;
1147 }
1148
1149 /* A subroutine of nodes_identical.  Examine two tests for equivalence.  */
1150
1151 static int
1152 nodes_identical_1 (d1, d2)
1153      struct decision_test *d1, *d2;
1154 {
1155   switch (d1->type)
1156     {
1157     case DT_mode:
1158       return d1->u.mode == d2->u.mode;
1159
1160     case DT_code:
1161       return d1->u.code == d2->u.code;
1162
1163     case DT_pred:
1164       return (d1->u.pred.mode == d2->u.pred.mode
1165               && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1166
1167     case DT_c_test:
1168       return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1169
1170     case DT_veclen:
1171       return d1->u.veclen == d2->u.veclen;
1172
1173     case DT_dup:
1174       return d1->u.dup == d2->u.dup;
1175
1176     case DT_elt_zero_int:
1177     case DT_elt_one_int:
1178     case DT_elt_zero_wide:
1179       return d1->u.intval == d2->u.intval;
1180
1181     case DT_accept_op:
1182       return d1->u.opno == d2->u.opno;
1183
1184     case DT_accept_insn:
1185       /* Differences will be handled in merge_accept_insn.  */
1186       return 1;
1187
1188     default:
1189       abort ();
1190     }
1191 }
1192
1193 /* True iff the two nodes are identical (on one level only).  Due
1194    to the way these lists are constructed, we shouldn't have to 
1195    consider different orderings on the tests.  */
1196
1197 static int
1198 nodes_identical (d1, d2)
1199      struct decision *d1, *d2;
1200 {
1201   struct decision_test *t1, *t2;
1202
1203   for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1204     {
1205       if (t1->type != t2->type)
1206         return 0;
1207       if (! nodes_identical_1 (t1, t2))
1208         return 0;
1209     }
1210
1211   /* For success, they should now both be null.  */
1212   if (t1 != t2)
1213     return 0;
1214
1215   /* Check that their subnodes are at the same position, as any one set
1216      of sibling decisions must be at the same position.  */
1217   if (d1->success.first
1218       && d2->success.first
1219       && strcmp (d1->success.first->position, d2->success.first->position))
1220     return 0;
1221
1222   return 1;
1223 }
1224
1225 /* A subroutine of merge_trees; given two nodes that have been declared
1226    identical, cope with two insn accept states.  If they differ in the
1227    number of clobbers, then the conflict was created by make_insn_sequence
1228    and we can drop the with-clobbers version on the floor.  If both 
1229    nodes have no additional clobbers, we have found an ambiguity in the
1230    source machine description.  */
1231
1232 static void
1233 merge_accept_insn (oldd, addd)
1234      struct decision *oldd, *addd;
1235 {
1236   struct decision_test *old, *add;
1237
1238   for (old = oldd->tests; old; old = old->next)
1239     if (old->type == DT_accept_insn)
1240       break;
1241   if (old == NULL)
1242     return;
1243
1244   for (add = addd->tests; add; add = add->next)
1245     if (add->type == DT_accept_insn)
1246       break;
1247   if (add == NULL)
1248     return;
1249
1250   /* If one node is for a normal insn and the second is for the base
1251      insn with clobbers stripped off, the second node should be ignored.  */
1252
1253   if (old->u.insn.num_clobbers_to_add == 0
1254       && add->u.insn.num_clobbers_to_add > 0)
1255     {
1256       /* Nothing to do here.  */
1257     }
1258   else if (old->u.insn.num_clobbers_to_add > 0
1259            && add->u.insn.num_clobbers_to_add == 0)
1260     {
1261       /* In this case, replace OLD with ADD.  */
1262       old->u.insn = add->u.insn;
1263     }
1264   else
1265     {
1266       message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1267                          get_insn_name (add->u.insn.code_number),
1268                          get_insn_name (old->u.insn.code_number));
1269       message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1270                          get_insn_name (old->u.insn.code_number));
1271       error_count++;
1272     }
1273 }
1274
1275 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively.  */
1276
1277 static void
1278 merge_trees (oldh, addh)
1279      struct decision_head *oldh, *addh;
1280 {
1281   struct decision *next, *add;
1282
1283   if (addh->first == 0)
1284     return;
1285   if (oldh->first == 0)
1286     {
1287       *oldh = *addh;
1288       return;
1289     }
1290
1291   /* Trying to merge bits at different positions isn't possible.  */
1292   if (strcmp (oldh->first->position, addh->first->position))
1293     abort ();
1294
1295   for (add = addh->first; add ; add = next)
1296     {
1297       struct decision *old, *insert_before = NULL;
1298
1299       next = add->next;
1300
1301       /* The semantics of pattern matching state that the tests are
1302          done in the order given in the MD file so that if an insn
1303          matches two patterns, the first one will be used.  However,
1304          in practice, most, if not all, patterns are unambiguous so
1305          that their order is independent.  In that case, we can merge
1306          identical tests and group all similar modes and codes together.
1307
1308          Scan starting from the end of OLDH until we reach a point
1309          where we reach the head of the list or where we pass a
1310          pattern that could also be true if NEW is true.  If we find
1311          an identical pattern, we can merge them.  Also, record the
1312          last node that tests the same code and mode and the last one
1313          that tests just the same mode.
1314
1315          If we have no match, place NEW after the closest match we found.  */
1316          
1317       for (old = oldh->last; old; old = old->prev)
1318         {
1319           if (nodes_identical (old, add))
1320             {
1321               merge_accept_insn (old, add);
1322               merge_trees (&old->success, &add->success);
1323               goto merged_nodes;
1324             }
1325
1326           if (maybe_both_true (old, add, 0))
1327             break;
1328
1329           /* Insert the nodes in DT test type order, which is roughly
1330              how expensive/important the test is.  Given that the tests
1331              are also ordered within the list, examining the first is
1332              sufficient.  */
1333           if (add->tests->type < old->tests->type)
1334             insert_before = old;
1335         }
1336
1337       if (insert_before == NULL)
1338         {
1339           add->next = NULL;
1340           add->prev = oldh->last;
1341           oldh->last->next = add;
1342           oldh->last = add;
1343         }
1344       else
1345         {
1346           if ((add->prev = insert_before->prev) != NULL)
1347             add->prev->next = add;
1348           else
1349             oldh->first = add;
1350           add->next = insert_before;
1351           insert_before->prev = add;
1352         }
1353
1354     merged_nodes:;
1355     }
1356 }
1357 \f
1358 /* Walk the tree looking for sub-nodes that perform common tests.  
1359    Factor out the common test into a new node.  This enables us
1360    (depending on the test type) to emit switch statements later.  */
1361
1362 static void
1363 factor_tests (head)
1364      struct decision_head *head;
1365 {
1366   struct decision *first, *next;
1367
1368   for (first = head->first; first && first->next; first = next)
1369     {
1370       enum decision_type type;
1371       struct decision *new, *old_last;
1372
1373       type = first->tests->type;
1374       next = first->next;
1375
1376       /* Want at least two compatible sequential nodes.  */
1377       if (next->tests->type != type)
1378         continue;
1379
1380       /* Don't want all node types, just those we can turn into 
1381          switch statements.  */
1382       if (type != DT_mode
1383           && type != DT_code
1384           && type != DT_veclen
1385           && type != DT_elt_zero_int
1386           && type != DT_elt_one_int
1387           && type != DT_elt_zero_wide)
1388         continue;
1389
1390       /* If we'd been performing more than one test, create a new node
1391          below our first test.  */
1392       if (first->tests->next != NULL)
1393         {
1394           new = new_decision (first->position, &first->success);
1395           new->tests = first->tests->next;
1396           first->tests->next = NULL;
1397         }
1398         
1399       /* Crop the node tree off after our first test.  */
1400       first->next = NULL;
1401       old_last = head->last;
1402       head->last = first;
1403
1404       /* For each compatible test, adjust to perform only one test in
1405          the top level node, then merge the node back into the tree.  */
1406       do
1407         {
1408           struct decision_head h;
1409
1410           if (next->tests->next != NULL)
1411             {
1412               new = new_decision (next->position, &next->success);
1413               new->tests = next->tests->next;
1414               next->tests->next = NULL;
1415             }
1416           new = next;
1417           next = next->next;
1418           new->next = NULL;
1419           h.first = h.last = new;
1420
1421           merge_trees (head, &h);
1422         }
1423       while (next && next->tests->type == type);
1424
1425       /* After we run out of compatible tests, graft the remaining nodes
1426          back onto the tree.  */
1427       if (next)
1428         {
1429           next->prev = head->last;
1430           head->last->next = next;
1431           head->last = old_last;
1432         }
1433     }
1434
1435   /* Recurse.  */
1436   for (first = head->first; first; first = first->next)
1437     factor_tests (&first->success);
1438 }
1439
1440 /* After factoring, try to simplify the tests on any one node.
1441    Tests that are useful for switch statements are recognizable
1442    by having only a single test on a node -- we'll be manipulating
1443    nodes with multiple tests:
1444
1445    If we have mode tests or code tests that are redundant with
1446    predicates, remove them.  */
1447
1448 static void
1449 simplify_tests (head)
1450      struct decision_head *head;
1451 {
1452   struct decision *tree;
1453
1454   for (tree = head->first; tree; tree = tree->next)
1455     {
1456       struct decision_test *a, *b;
1457
1458       a = tree->tests;
1459       b = a->next;
1460       if (b == NULL)
1461         continue;
1462
1463       /* Find a predicate node.  */
1464       while (b && b->type != DT_pred)
1465         b = b->next;
1466       if (b)
1467         {
1468           /* Due to how these tests are constructed, we don't even need
1469              to check that the mode and code are compatible -- they were
1470              generated from the predicate in the first place.  */
1471           while (a->type == DT_mode || a->type == DT_code)
1472             a = a->next;
1473           tree->tests = a;
1474         }
1475     }
1476
1477   /* Recurse.  */
1478   for (tree = head->first; tree; tree = tree->next)
1479     simplify_tests (&tree->success);
1480 }
1481
1482 /* Count the number of subnodes of HEAD.  If the number is high enough,
1483    make the first node in HEAD start a separate subroutine in the C code
1484    that is generated.  */
1485
1486 static int
1487 break_out_subroutines (head, initial)
1488      struct decision_head *head;
1489      int initial;
1490 {
1491   int size = 0;
1492   struct decision *sub;
1493
1494   for (sub = head->first; sub; sub = sub->next)
1495     size += 1 + break_out_subroutines (&sub->success, 0);
1496
1497   if (size > SUBROUTINE_THRESHOLD && ! initial)
1498     {
1499       head->first->subroutine_number = ++next_subroutine_number;
1500       size = 1;
1501     }
1502   return size;
1503 }
1504
1505 /* For each node p, find the next alternative that might be true
1506    when p is true.  */
1507
1508 static void
1509 find_afterward (head, real_afterward)
1510      struct decision_head *head;
1511      struct decision *real_afterward;
1512 {
1513   struct decision *p, *q, *afterward;
1514
1515   /* We can't propogate alternatives across subroutine boundaries. 
1516      This is not incorrect, merely a minor optimization loss.  */
1517
1518   p = head->first;
1519   afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1520
1521   for ( ; p ; p = p->next)
1522     {
1523       /* Find the next node that might be true if this one fails.  */
1524       for (q = p->next; q ; q = q->next)
1525         if (maybe_both_true (p, q, 1))
1526           break;
1527
1528       /* If we reached the end of the list without finding one, 
1529          use the incoming afterward position.  */
1530       if (!q)
1531         q = afterward;
1532       p->afterward = q;
1533       if (q)
1534         q->need_label = 1;
1535     }
1536
1537   /* Recurse.  */
1538   for (p = head->first; p ; p = p->next)
1539     if (p->success.first)
1540       find_afterward (&p->success, p->afterward);
1541
1542   /* When we are generating a subroutine, record the real afterward
1543      position in the first node where write_tree can find it, and we
1544      can do the right thing at the subroutine call site.  */
1545   p = head->first;
1546   if (p->subroutine_number > 0)
1547     p->afterward = real_afterward;
1548 }
1549 \f
1550 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1551    actions are necessary to move to NEWPOS.  If we fail to move to the
1552    new state, branch to node AFTERWARD if non-zero, otherwise return.
1553
1554    Failure to move to the new state can only occur if we are trying to
1555    match multiple insns and we try to step past the end of the stream. */
1556
1557 static void
1558 change_state (oldpos, newpos, afterward, indent)
1559      const char *oldpos;
1560      const char *newpos;
1561      struct decision *afterward;
1562      const char *indent;
1563 {
1564   int odepth = strlen (oldpos);
1565   int ndepth = strlen (newpos);
1566   int depth;
1567   int old_has_insn, new_has_insn;
1568
1569   /* Pop up as many levels as necessary.  */
1570   for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1571     continue;
1572
1573   /* Hunt for the last [A-Z] in both strings.  */
1574   for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1575     if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1576       break;
1577   for (new_has_insn = ndepth - 1; new_has_insn >= 0; --new_has_insn)
1578     if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1579       break;
1580
1581   /* Go down to desired level.  */
1582   while (depth < ndepth)
1583     {
1584       /* It's a different insn from the first one. */
1585       if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1586         {
1587           /* We can only fail if we're moving down the tree.  */
1588           if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1589             {
1590               printf ("%stem = peep2_next_insn (%d);\n", 
1591                       indent, newpos[depth] - 'A');
1592             }
1593           else
1594             {
1595               printf ("%stem = peep2_next_insn (%d);\n", 
1596                       indent, newpos[depth] - 'A');
1597               printf ("%sif (tem == NULL_RTX)\n", indent);
1598               if (afterward)
1599                 printf ("%s  goto L%d;\n", indent, afterward->number);
1600               else
1601                 printf ("%s  goto ret0;\n", indent);
1602             }
1603           printf ("%sx%d = PATTERN (tem);\n", indent, depth + 1);
1604         }
1605       else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1606         printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1607                 indent, depth + 1, depth, newpos[depth] - 'a');
1608       else
1609         printf ("%sx%d = XEXP (x%d, %c);\n",
1610                 indent, depth + 1, depth, newpos[depth]);
1611       ++depth;
1612     }
1613 }
1614 \f
1615 /* Print the enumerator constant for CODE -- the upcase version of
1616    the name.  */
1617
1618 static void
1619 print_code (code)
1620      enum rtx_code code;
1621 {
1622   register const char *p;
1623   for (p = GET_RTX_NAME (code); *p; p++)
1624     putchar (TOUPPER (*p));
1625 }
1626
1627 /* Emit code to cross an afterward link -- change state and branch.  */
1628
1629 static void
1630 write_afterward (start, afterward, indent)
1631      struct decision *start;
1632      struct decision *afterward;
1633      const char *indent;
1634 {
1635   if (!afterward || start->subroutine_number > 0)
1636     printf("%sgoto ret0;\n", indent);
1637   else
1638     {
1639       change_state (start->position, afterward->position, NULL, indent);
1640       printf ("%sgoto L%d;\n", indent, afterward->number);
1641     }
1642 }
1643
1644 /* Emit a switch statement, if possible, for an initial sequence of 
1645    nodes at START.  Return the first node yet untested.  */
1646
1647 static struct decision *
1648 write_switch (start, depth)
1649      struct decision *start;
1650      int depth;
1651 {
1652   struct decision *p = start;
1653   enum decision_type type = p->tests->type;
1654
1655   /* If we have two or more nodes in sequence that test the same one
1656      thing, we may be able to use a switch statement.  */
1657
1658   if (!p->next
1659       || p->tests->next
1660       || p->next->tests->type != type
1661       || p->next->tests->next)
1662     return p;
1663
1664   /* DT_code is special in that we can do interesting things with
1665      known predicates at the same time.  */
1666   if (type == DT_code)
1667     {
1668       char codemap[NUM_RTX_CODE];
1669       struct decision *ret;
1670       RTX_CODE code;
1671
1672       memset (codemap, 0, sizeof(codemap));
1673
1674       printf ("  switch (GET_CODE (x%d))\n    {\n", depth);
1675       code = p->tests->u.code;
1676       do 
1677         {
1678           printf ("    case ");
1679           print_code (code);
1680           printf (":\n      goto L%d;\n", p->success.first->number);
1681           p->success.first->need_label = 1;
1682
1683           codemap[code] = 1;
1684           p = p->next;
1685         }
1686       while (p
1687              && ! p->tests->next
1688              && p->tests->type == DT_code
1689              && ! codemap[code = p->tests->u.code]);
1690
1691       /* If P is testing a predicate that we know about and we haven't
1692          seen any of the codes that are valid for the predicate, we can
1693          write a series of "case" statement, one for each possible code.
1694          Since we are already in a switch, these redundant tests are very
1695          cheap and will reduce the number of predicates called.  */
1696
1697       /* Note that while we write out cases for these predicates here,
1698          we don't actually write the test here, as it gets kinda messy.
1699          It is trivial to leave this to later by telling our caller that
1700          we only processed the CODE tests.  */
1701       ret = p;
1702
1703       while (p && p->tests->type == DT_pred
1704              && p->tests->u.pred.index >= 0)
1705         {
1706           const RTX_CODE *c;
1707
1708           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1709             if (codemap[(int) *c] != 0)
1710               goto pred_done;
1711
1712           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1713             {
1714               printf ("    case ");
1715               print_code (*c);
1716               printf (":\n");
1717               codemap[(int) *c] = 1;
1718             }
1719
1720           printf ("      goto L%d;\n", p->number);
1721           p->need_label = 1;
1722           p = p->next;
1723         }
1724
1725     pred_done:
1726       /* Make the default case skip the predicates we managed to match.  */
1727
1728       printf ("    default:\n");
1729       if (p != ret)
1730         {
1731           if (p)
1732             {
1733               printf ("      goto L%d;\n", p->number);
1734               p->need_label = 1;
1735             }
1736           else
1737             write_afterward (start, start->afterward, "      ");
1738         }
1739       else
1740         printf ("     break;\n");
1741       printf ("   }\n");
1742
1743       return ret;
1744     }
1745   else if (type == DT_mode
1746            || type == DT_veclen
1747            || type == DT_elt_zero_int
1748            || type == DT_elt_one_int
1749            || type == DT_elt_zero_wide)
1750     {
1751       printf ("  switch (");
1752       switch (type)
1753         {
1754         case DT_mode:
1755           printf ("GET_MODE (x%d)", depth);
1756           break;
1757         case DT_veclen:
1758           printf ("XVECLEN (x%d, 0)", depth);
1759           break;
1760         case DT_elt_zero_int:
1761           printf ("XINT (x%d, 0)", depth);
1762           break;
1763         case DT_elt_one_int:
1764           printf ("XINT (x%d, 1)", depth);
1765           break;
1766         case DT_elt_zero_wide:
1767           /* Convert result of XWINT to int for portability since some C
1768              compilers won't do it and some will.  */
1769           printf ("(int) XWINT (x%d, 0)", depth);
1770           break;
1771         default:
1772           abort ();
1773         }
1774       printf (")\n    {\n");
1775
1776       do
1777         {
1778           printf ("    case ");
1779           switch (type)
1780             {
1781             case DT_mode:
1782               printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1783               break;
1784             case DT_veclen:
1785               printf ("%d", p->tests->u.veclen);
1786               break;
1787             case DT_elt_zero_int:
1788             case DT_elt_one_int:
1789             case DT_elt_zero_wide:
1790               printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1791               break;
1792             default:
1793               abort ();
1794             }
1795           printf (":\n      goto L%d;\n", p->success.first->number);
1796           p->success.first->need_label = 1;
1797
1798           p = p->next;
1799         }
1800       while (p && p->tests->type == type && !p->tests->next);
1801       
1802       printf ("    default:\n      break;\n    }\n");
1803
1804       return p;
1805     }
1806   else
1807     {
1808       /* None of the other tests are ameanable.  */
1809       return p;
1810     }
1811 }
1812
1813 /* Emit code for one test.  */
1814
1815 static void
1816 write_cond (p, depth, subroutine_type)
1817      struct decision_test *p;
1818      int depth;
1819      enum routine_type subroutine_type;
1820 {
1821   switch (p->type)
1822     {
1823     case DT_mode:
1824       printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1825       break;
1826
1827     case DT_code:
1828       printf ("GET_CODE (x%d) == ", depth);
1829       print_code (p->u.code);
1830       break;
1831
1832     case DT_veclen:
1833       printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1834       break;
1835
1836     case DT_elt_zero_int:
1837       printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1838       break;
1839
1840     case DT_elt_one_int:
1841       printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1842       break;
1843
1844     case DT_elt_zero_wide:
1845       printf ("XWINT (x%d, 0) == ", depth);
1846       printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1847       break;
1848
1849     case DT_dup:
1850       printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1851       break;
1852
1853     case DT_pred:
1854       printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1855               GET_MODE_NAME (p->u.pred.mode));
1856       break;
1857
1858     case DT_c_test:
1859       printf ("(%s)", p->u.c_test);
1860       break;
1861
1862     case DT_accept_insn:
1863       switch (subroutine_type)
1864         {
1865         case RECOG:
1866           if (p->u.insn.num_clobbers_to_add == 0)
1867             abort ();
1868           printf ("pnum_clobbers != NULL");
1869           break;
1870
1871         default:
1872           abort ();
1873         }
1874       break;
1875
1876     default:
1877       abort ();
1878     }
1879 }
1880
1881 /* Emit code for one action.  The previous tests have succeeded;
1882    TEST is the last of the chain.  In the normal case we simply
1883    perform a state change.  For the `accept' tests we must do more work.  */
1884
1885 static void
1886 write_action (p, test, depth, uncond, success, subroutine_type)
1887      struct decision *p;
1888      struct decision_test *test;
1889      int depth, uncond;
1890      struct decision *success;
1891      enum routine_type subroutine_type;
1892 {
1893   const char *indent;
1894   int want_close = 0;
1895
1896   if (uncond)
1897     indent = "  ";
1898   else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1899     {
1900       fputs ("    {\n", stdout);
1901       indent = "      ";
1902       want_close = 1;
1903     }
1904   else
1905     indent = "    ";
1906
1907   if (test->type == DT_accept_op)
1908     {
1909       printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1910
1911       /* Only allow DT_accept_insn to follow.  */
1912       if (test->next)
1913         {
1914           test = test->next;
1915           if (test->type != DT_accept_insn)
1916             abort ();
1917         }
1918     }
1919
1920   /* Sanity check that we're now at the end of the list of tests.  */
1921   if (test->next)
1922     abort ();
1923
1924   if (test->type == DT_accept_insn)
1925     {
1926       switch (subroutine_type)
1927         {
1928         case RECOG:
1929           if (test->u.insn.num_clobbers_to_add != 0)
1930             printf ("%s*pnum_clobbers = %d;\n",
1931                     indent, test->u.insn.num_clobbers_to_add);
1932           printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1933           break;
1934
1935         case SPLIT:
1936           printf ("%sreturn gen_split_%d (operands);\n",
1937                   indent, test->u.insn.code_number);
1938           break;
1939
1940         case PEEPHOLE2:
1941           {
1942             int match_len = 0, i;
1943
1944             for (i = strlen (p->position) - 1; i >= 0; --i)
1945               if (p->position[i] >= 'A' && p->position[i] <= 'Z')
1946                 {
1947                   match_len = p->position[i] - 'A';
1948                   break;
1949                 }
1950             printf ("%s*_pmatch_len = %d;\n", indent, match_len);
1951             printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1952                     indent, test->u.insn.code_number);
1953             printf ("%sif (tem != 0)\n%s  return tem;\n", indent, indent);
1954           }
1955           break;
1956
1957         default:
1958           abort ();
1959         }
1960     }
1961   else
1962     {
1963       printf("%sgoto L%d;\n", indent, success->number);
1964       success->need_label = 1;
1965     }
1966
1967   if (want_close)
1968     fputs ("    }\n", stdout);
1969 }
1970
1971 /* Return 1 if the test is always true and has no fallthru path.  Return -1
1972    if the test does have a fallthru path, but requires that the condition be
1973    terminated.  Otherwise return 0 for a normal test.  */
1974 /* ??? is_unconditional is a stupid name for a tri-state function.  */
1975
1976 static int
1977 is_unconditional (t, subroutine_type)
1978      struct decision_test *t;
1979      enum routine_type subroutine_type;
1980 {
1981   if (t->type == DT_accept_op)
1982     return 1;
1983
1984   if (t->type == DT_accept_insn)
1985     {
1986       switch (subroutine_type)
1987         {
1988         case RECOG:
1989           return (t->u.insn.num_clobbers_to_add == 0);
1990         case SPLIT:
1991           return 1;
1992         case PEEPHOLE2:
1993           return -1;
1994         default:
1995           abort ();
1996         }
1997     }
1998
1999   return 0;
2000 }
2001
2002 /* Emit code for one node -- the conditional and the accompanying action.
2003    Return true if there is no fallthru path.  */
2004
2005 static int
2006 write_node (p, depth, subroutine_type)
2007      struct decision *p;
2008      int depth;
2009      enum routine_type subroutine_type;
2010 {
2011   struct decision_test *test, *last_test;
2012   int uncond;
2013
2014   last_test = test = p->tests;
2015   uncond = is_unconditional (test, subroutine_type);
2016   if (uncond == 0)
2017     {
2018       printf ("  if (");
2019       write_cond (test, depth, subroutine_type);
2020
2021       while ((test = test->next) != NULL)
2022         {
2023           int uncond2;
2024
2025           last_test = test;
2026           uncond2 = is_unconditional (test, subroutine_type);
2027           if (uncond2 != 0)
2028             break;
2029
2030           printf ("\n      && ");
2031           write_cond (test, depth, subroutine_type);
2032         }
2033
2034       printf (")\n");
2035     }
2036
2037   write_action (p, last_test, depth, uncond, p->success.first, subroutine_type);
2038
2039   return uncond > 0;
2040 }
2041
2042 /* Emit code for all of the sibling nodes of HEAD.  */
2043
2044 static void
2045 write_tree_1 (head, depth, subroutine_type)
2046      struct decision_head *head;
2047      int depth;
2048      enum routine_type subroutine_type;
2049 {
2050   struct decision *p, *next;
2051   int uncond = 0;
2052
2053   for (p = head->first; p ; p = next)
2054     {
2055       /* The label for the first element was printed in write_tree.  */
2056       if (p != head->first && p->need_label)
2057         OUTPUT_LABEL (" ", p->number);
2058
2059       /* Attempt to write a switch statement for a whole sequence.  */
2060       next = write_switch (p, depth);
2061       if (p != next)
2062         uncond = 0;
2063       else
2064         {
2065           /* Failed -- fall back and write one node.  */
2066           uncond = write_node (p, depth, subroutine_type);
2067           next = p->next;
2068         }
2069     }
2070
2071   /* Finished with this chain.  Close a fallthru path by branching
2072      to the afterward node.  */
2073   if (! uncond)
2074     write_afterward (head->last, head->last->afterward, "  ");
2075 }
2076
2077 /* Write out the decision tree starting at HEAD.  PREVPOS is the
2078    position at the node that branched to this node.  */
2079
2080 static void
2081 write_tree (head, prevpos, type, initial)
2082      struct decision_head *head;
2083      const char *prevpos;
2084      enum routine_type type;
2085      int initial;
2086 {
2087   register struct decision *p = head->first;
2088
2089   putchar ('\n');
2090   if (p->need_label)
2091     OUTPUT_LABEL (" ", p->number);
2092
2093   if (! initial && p->subroutine_number > 0)
2094     {
2095       static const char * const name_prefix[] = {
2096           "recog", "split", "peephole2"
2097       };
2098
2099       static const char * const call_suffix[] = {
2100           ", pnum_clobbers", "", ", _pmatch_len"
2101       };
2102
2103       /* This node has been broken out into a separate subroutine.
2104          Call it, test the result, and branch accordingly.  */
2105
2106       if (p->afterward)
2107         {
2108           printf ("  tem = %s_%d (x0, insn%s);\n",
2109                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2110           if (IS_SPLIT (type))
2111             printf ("  if (tem != 0)\n    return tem;\n");
2112           else
2113             printf ("  if (tem >= 0)\n    return tem;\n");
2114
2115           change_state (p->position, p->afterward->position, NULL, "  ");
2116           printf ("  goto L%d;\n", p->afterward->number);
2117         }
2118       else
2119         {
2120           printf ("  return %s_%d (x0, insn%s);\n",
2121                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2122         }
2123     }
2124   else
2125     {
2126       int depth = strlen (p->position);
2127
2128       change_state (prevpos, p->position, head->last->afterward, "  ");
2129       write_tree_1 (head, depth, type);
2130
2131       for (p = head->first; p; p = p->next)
2132         if (p->success.first)
2133           write_tree (&p->success, p->position, type, 0);
2134     }
2135 }
2136
2137 /* Write out a subroutine of type TYPE to do comparisons starting at
2138    node TREE.  */
2139
2140 static void
2141 write_subroutine (head, type)
2142      struct decision_head *head;
2143      enum routine_type type;
2144 {
2145   int subfunction = head->first ? head->first->subroutine_number : 0;
2146   const char *s_or_e;
2147   char extension[32];
2148   int i;
2149   
2150   s_or_e = subfunction ? "static " : "";
2151
2152   if (subfunction)
2153     sprintf (extension, "_%d", subfunction);
2154   else if (type == RECOG)
2155     extension[0] = '\0';
2156   else
2157     strcpy (extension, "_insns");
2158
2159   switch (type)
2160     {
2161     case RECOG:
2162       printf ("%sint recog%s PARAMS ((rtx, rtx, int *));\n", s_or_e, extension);
2163       printf ("%sint\n\
2164 recog%s (x0, insn, pnum_clobbers)\n\
2165      register rtx x0;\n\
2166      rtx insn ATTRIBUTE_UNUSED;\n\
2167      int *pnum_clobbers ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2168       break;
2169     case SPLIT:
2170       printf ("%srtx split%s PARAMS ((rtx, rtx));\n", s_or_e, extension);
2171       printf ("%srtx\n\
2172 split%s (x0, insn)\n\
2173      register rtx x0;\n\
2174      rtx insn ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2175       break;
2176     case PEEPHOLE2:
2177       printf ("%srtx peephole2%s PARAMS ((rtx, rtx, int *));\n",
2178               s_or_e, extension);
2179       printf ("%srtx\n\
2180 peephole2%s (x0, insn, _pmatch_len)\n\
2181      register rtx x0;\n\
2182      rtx insn ATTRIBUTE_UNUSED;\n\
2183      int *_pmatch_len ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2184       break;
2185     }
2186
2187   printf ("{\n  register rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2188   for (i = 1; i <= max_depth; i++)
2189     printf ("  register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2190
2191   printf ("  %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2192
2193   if (head->first)
2194     write_tree (head, "", type, 1);
2195   else
2196     printf ("  goto ret0;\n");
2197
2198   printf (" ret0:\n  return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2199 }
2200
2201 /* In break_out_subroutines, we discovered the boundaries for the
2202    subroutines, but did not write them out.  Do so now.  */
2203
2204 static void
2205 write_subroutines (head, type)
2206      struct decision_head *head;
2207      enum routine_type type;
2208 {
2209   struct decision *p;
2210
2211   for (p = head->first; p ; p = p->next)
2212     if (p->success.first)
2213       write_subroutines (&p->success, type);
2214
2215   if (head->first->subroutine_number > 0)
2216     write_subroutine (head, type);
2217 }
2218
2219 /* Begin the output file.  */
2220
2221 static void
2222 write_header ()
2223 {
2224   puts ("\
2225 /* Generated automatically by the program `genrecog' from the target\n\
2226    machine description file.  */\n\
2227 \n\
2228 #include \"config.h\"\n\
2229 #include \"system.h\"\n\
2230 #include \"rtl.h\"\n\
2231 #include \"tm_p.h\"\n\
2232 #include \"function.h\"\n\
2233 #include \"insn-config.h\"\n\
2234 #include \"recog.h\"\n\
2235 #include \"real.h\"\n\
2236 #include \"output.h\"\n\
2237 #include \"flags.h\"\n\
2238 #include \"hard-reg-set.h\"\n\
2239 #include \"resource.h\"\n\
2240 \n");
2241
2242   puts ("\n\
2243 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2244    X0 is a valid instruction.\n\
2245 \n\
2246    recog returns -1 if the rtx is not valid.  If the rtx is valid, recog\n\
2247    returns a nonnegative number which is the insn code number for the\n\
2248    pattern that matched.  This is the same as the order in the machine\n\
2249    description of the entry that matched.  This number can be used as an\n\
2250    index into `insn_data' and other tables.\n");
2251   puts ("\
2252    The third argument to recog is an optional pointer to an int.  If\n\
2253    present, recog will accept a pattern if it matches except for missing\n\
2254    CLOBBER expressions at the end.  In that case, the value pointed to by\n\
2255    the optional pointer will be set to the number of CLOBBERs that need\n\
2256    to be added (it should be initialized to zero by the caller).  If it");
2257   puts ("\
2258    is set nonzero, the caller should allocate a PARALLEL of the\n\
2259    appropriate size, copy the initial entries, and call add_clobbers\n\
2260    (found in insn-emit.c) to fill in the CLOBBERs.\n\
2261 ");
2262
2263   puts ("\n\
2264    The function split_insns returns 0 if the rtl could not\n\
2265    be split or the split rtl in a SEQUENCE if it can be.\n\
2266 \n\
2267    The function peephole2_insns returns 0 if the rtl could not\n\
2268    be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2269    and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2270 */\n\n");
2271 }
2272
2273 \f
2274 /* Construct and return a sequence of decisions
2275    that will recognize INSN.
2276
2277    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
2278
2279 static struct decision_head
2280 make_insn_sequence (insn, type)
2281      rtx insn;
2282      enum routine_type type;
2283 {
2284   rtx x;
2285   const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2286   struct decision *last;
2287   struct decision_test *test, **place;
2288   struct decision_head head;
2289   char c_test_pos[2];
2290
2291   record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2292
2293   c_test_pos[0] = '\0';
2294   if (type == PEEPHOLE2)
2295     {
2296       int i, j;
2297
2298       /* peephole2 gets special treatment:
2299          - X always gets an outer parallel even if it's only one entry
2300          - we remove all traces of outer-level match_scratch and match_dup
2301            expressions here.  */
2302       x = rtx_alloc (PARALLEL);
2303       PUT_MODE (x, VOIDmode);
2304       XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2305       for (i = j = 0; i < XVECLEN (insn, 0); i++)
2306         {
2307           rtx tmp = XVECEXP (insn, 0, i);
2308           if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2309             {
2310               XVECEXP (x, 0, j) = tmp;
2311               j++;
2312             }
2313         }
2314       XVECLEN (x, 0) = j;
2315
2316       c_test_pos[0] = 'A' + j - 1;
2317       c_test_pos[1] = '\0';
2318     }
2319   else if (XVECLEN (insn, type == RECOG) == 1)
2320     x = XVECEXP (insn, type == RECOG, 0);
2321   else
2322     {
2323       x = rtx_alloc (PARALLEL);
2324       XVEC (x, 0) = XVEC (insn, type == RECOG);
2325       PUT_MODE (x, VOIDmode);
2326     }
2327
2328   validate_pattern (x, insn, NULL_RTX);
2329
2330   memset(&head, 0, sizeof(head));
2331   last = add_to_sequence (x, &head, "", type, 1);
2332
2333   /* Find the end of the test chain on the last node.  */
2334   for (test = last->tests; test->next; test = test->next)
2335     continue;
2336   place = &test->next;
2337
2338   if (c_test[0])
2339     {
2340       /* Need a new node if we have another test to add.  */
2341       if (test->type == DT_accept_op)
2342         {
2343           last = new_decision (c_test_pos, &last->success);
2344           place = &last->tests;
2345         }
2346       test = new_decision_test (DT_c_test, &place);
2347       test->u.c_test = c_test;
2348     }
2349
2350   test = new_decision_test (DT_accept_insn, &place);
2351   test->u.insn.code_number = next_insn_code;
2352   test->u.insn.lineno = pattern_lineno;
2353   test->u.insn.num_clobbers_to_add = 0;
2354
2355   switch (type)
2356     {
2357     case RECOG:
2358       /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2359          with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2360          If so, set up to recognize the pattern without these CLOBBERs.  */
2361
2362       if (GET_CODE (x) == PARALLEL)
2363         {
2364           int i;
2365
2366           /* Find the last non-clobber in the parallel.  */
2367           for (i = XVECLEN (x, 0); i > 0; i--)
2368             {
2369               rtx y = XVECEXP (x, 0, i - 1);
2370               if (GET_CODE (y) != CLOBBER
2371                   || (GET_CODE (XEXP (y, 0)) != REG
2372                       && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2373                 break;
2374             }
2375
2376           if (i != XVECLEN (x, 0))
2377             {
2378               rtx new;
2379               struct decision_head clobber_head;
2380
2381               /* Build a similar insn without the clobbers.  */
2382               if (i == 1)
2383                 new = XVECEXP (x, 0, 0);
2384               else
2385                 {
2386                   int j;
2387
2388                   new = rtx_alloc (PARALLEL);
2389                   XVEC (new, 0) = rtvec_alloc (i);
2390                   for (j = i - 1; j >= 0; j--)
2391                     XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2392                 }
2393
2394               /* Recognize it.  */
2395               memset (&clobber_head, 0, sizeof(clobber_head));
2396               last = add_to_sequence (new, &clobber_head, "", type, 1);
2397
2398               /* Find the end of the test chain on the last node.  */
2399               for (test = last->tests; test->next; test = test->next)
2400                 continue;
2401
2402               /* We definitely have a new test to add -- create a new
2403                  node if needed.  */
2404               place = &test->next;
2405               if (test->type == DT_accept_op)
2406                 {
2407                   last = new_decision ("", &last->success);
2408                   place = &last->tests;
2409                 }
2410
2411               if (c_test[0])
2412                 {
2413                   test = new_decision_test (DT_c_test, &place);
2414                   test->u.c_test = c_test;
2415                 }
2416
2417               test = new_decision_test (DT_accept_insn, &place);
2418               test->u.insn.code_number = next_insn_code;
2419               test->u.insn.lineno = pattern_lineno;
2420               test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2421
2422               merge_trees (&head, &clobber_head);
2423             }
2424         }
2425       break;
2426
2427     case SPLIT:
2428       /* Define the subroutine we will call below and emit in genemit.  */
2429       printf ("extern rtx gen_split_%d PARAMS ((rtx *));\n", next_insn_code);
2430       break;
2431
2432     case PEEPHOLE2:
2433       /* Define the subroutine we will call below and emit in genemit.  */
2434       printf ("extern rtx gen_peephole2_%d PARAMS ((rtx, rtx *));\n",
2435               next_insn_code);
2436       break;
2437     }
2438
2439   return head;
2440 }
2441
2442 static void
2443 process_tree (head, subroutine_type)
2444      struct decision_head *head;
2445      enum routine_type subroutine_type;
2446 {
2447   if (head->first == NULL)
2448     {
2449       /* We can elide peephole2_insns, but not recog or split_insns.  */
2450       if (subroutine_type == PEEPHOLE2)
2451         return;
2452     }
2453   else
2454     {
2455       factor_tests (head);
2456
2457       next_subroutine_number = 0;
2458       break_out_subroutines (head, 1);
2459       find_afterward (head, NULL);
2460
2461       /* We run this after find_afterward, because find_afterward needs
2462          the redundant DT_mode tests on predicates to determine whether
2463          two tests can both be true or not.  */
2464       simplify_tests(head);
2465
2466       write_subroutines (head, subroutine_type);
2467     }
2468
2469   write_subroutine (head, subroutine_type);
2470 }
2471 \f
2472 extern int main PARAMS ((int, char **));
2473
2474 int
2475 main (argc, argv)
2476      int argc;
2477      char **argv;
2478 {
2479   rtx desc;
2480   struct decision_head recog_tree, split_tree, peephole2_tree, h;
2481
2482   progname = "genrecog";
2483
2484   memset (&recog_tree, 0, sizeof recog_tree);
2485   memset (&split_tree, 0, sizeof split_tree);
2486   memset (&peephole2_tree, 0, sizeof peephole2_tree);
2487
2488   if (argc <= 1)
2489     fatal ("No input file name.");
2490
2491   if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
2492     return (FATAL_EXIT_CODE);
2493
2494   next_insn_code = 0;
2495   next_index = 0;
2496
2497   write_header ();
2498
2499   /* Read the machine description.  */
2500
2501   while (1)
2502     {
2503       desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2504       if (desc == NULL)
2505         break;
2506
2507       if (GET_CODE (desc) == DEFINE_INSN)
2508         {
2509           h = make_insn_sequence (desc, RECOG);
2510           merge_trees (&recog_tree, &h);
2511         }
2512       else if (GET_CODE (desc) == DEFINE_SPLIT)
2513         {
2514           h = make_insn_sequence (desc, SPLIT);
2515           merge_trees (&split_tree, &h);
2516         }
2517       else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2518         {
2519           h = make_insn_sequence (desc, PEEPHOLE2);
2520           merge_trees (&peephole2_tree, &h);
2521         }
2522         
2523       next_index++;
2524     }
2525
2526   if (error_count)
2527     return FATAL_EXIT_CODE;
2528
2529   puts ("\n\n");
2530
2531   process_tree (&recog_tree, RECOG);
2532   process_tree (&split_tree, SPLIT);
2533   process_tree (&peephole2_tree, PEEPHOLE2);
2534
2535   fflush (stdout);
2536   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2537 }
2538 \f
2539 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
2540 const char *
2541 get_insn_name (code)
2542      int code;
2543 {
2544   if (code < insn_name_ptr_size)
2545     return insn_name_ptr[code];
2546   else
2547     return NULL;
2548 }
2549
2550 static void
2551 record_insn_name (code, name)
2552      int code;
2553      const char *name;
2554 {
2555   static const char *last_real_name = "insn";
2556   static int last_real_code = 0;
2557   char *new;
2558
2559   if (insn_name_ptr_size <= code)
2560     {
2561       int new_size;
2562       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2563       insn_name_ptr =
2564         (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2565       memset (insn_name_ptr + insn_name_ptr_size, 0, 
2566               sizeof(char *) * (new_size - insn_name_ptr_size));
2567       insn_name_ptr_size = new_size;
2568     }
2569
2570   if (!name || name[0] == '\0')
2571     {
2572       new = xmalloc (strlen (last_real_name) + 10);
2573       sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2574     }
2575   else
2576     {
2577       last_real_name = new = xstrdup (name);
2578       last_real_code = code;
2579     }
2580   
2581   insn_name_ptr[code] = new;
2582 }  
2583 \f
2584 static void
2585 debug_decision_2 (test)
2586      struct decision_test *test;
2587 {
2588   switch (test->type)
2589     {
2590     case DT_mode:
2591       fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2592       break;
2593     case DT_code:
2594       fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2595       break;
2596     case DT_veclen:
2597       fprintf (stderr, "veclen=%d", test->u.veclen);
2598       break;
2599     case DT_elt_zero_int:
2600       fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2601       break;
2602     case DT_elt_one_int:
2603       fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2604       break;
2605     case DT_elt_zero_wide:
2606       fprintf (stderr, "elt0_w=");
2607       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2608       break;
2609     case DT_dup:
2610       fprintf (stderr, "dup=%d", test->u.dup);
2611       break;
2612     case DT_pred:
2613       fprintf (stderr, "pred=(%s,%s)",
2614                test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2615       break;
2616     case DT_c_test:
2617       {
2618         char sub[16+4];
2619         strncpy (sub, test->u.c_test, sizeof(sub));
2620         memcpy (sub+16, "...", 4);
2621         fprintf (stderr, "c_test=\"%s\"", sub);
2622       }
2623       break;
2624     case DT_accept_op:
2625       fprintf (stderr, "A_op=%d", test->u.opno);
2626       break;
2627     case DT_accept_insn:
2628       fprintf (stderr, "A_insn=(%d,%d)", 
2629                test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2630       break;
2631
2632     default:
2633       abort ();
2634     }
2635 }
2636
2637 static void
2638 debug_decision_1 (d, indent)
2639      struct decision *d;
2640      int indent;
2641 {
2642   int i;
2643   struct decision_test *test;
2644
2645   if (d == NULL)
2646     {
2647       for (i = 0; i < indent; ++i)
2648         putc (' ', stderr);
2649       fputs ("(nil)\n", stderr);
2650       return;
2651     }
2652
2653   for (i = 0; i < indent; ++i)
2654     putc (' ', stderr);
2655
2656   putc ('{', stderr);
2657   test = d->tests;
2658   if (test)
2659     {
2660       debug_decision_2 (test);
2661       while ((test = test->next) != NULL)
2662         {
2663           fputs (" + ", stderr);
2664           debug_decision_2 (test);
2665         }
2666     }
2667   fprintf (stderr, "} %d n %d a %d\n", d->number,
2668            (d->next ? d->next->number : -1),
2669            (d->afterward ? d->afterward->number : -1));
2670 }
2671
2672 static void
2673 debug_decision_0 (d, indent, maxdepth)
2674      struct decision *d;
2675      int indent, maxdepth;
2676 {
2677   struct decision *n;
2678   int i;
2679
2680   if (maxdepth < 0)
2681     return;
2682   if (d == NULL)
2683     {
2684       for (i = 0; i < indent; ++i)
2685         putc (' ', stderr);
2686       fputs ("(nil)\n", stderr);
2687       return;
2688     }
2689
2690   debug_decision_1 (d, indent);
2691   for (n = d->success.first; n ; n = n->next)
2692     debug_decision_0 (n, indent + 2, maxdepth - 1);
2693 }
2694
2695 void
2696 debug_decision (d)
2697      struct decision *d;
2698 {
2699   debug_decision_0 (d, 0, 1000000);
2700 }
2701
2702 void
2703 debug_decision_list (d)
2704      struct decision *d;
2705 {
2706   while (d)
2707     {
2708       debug_decision_0 (d, 0, 0);
2709       d = d->next;
2710     }
2711 }