top level:
[platform/upstream/gcc.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_test *, int, int, struct decision *,
276          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   /* Make sure to reset the last_insn pointer when popping back up.  */
1582   if (old_has_insn >= 0 && new_has_insn < 0)
1583     printf ("%slast_insn = insn;\n", indent);
1584
1585   /* Go down to desired level.  */
1586   while (depth < ndepth)
1587     {
1588       /* It's a different insn from the first one. */
1589       if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1590         {
1591           /* We can only fail if we're moving down the tree.  */
1592           if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1593             {
1594               printf ("%slast_insn = recog_next_insn (insn, %d);\n", 
1595                       indent, newpos[depth] - 'A');
1596             }
1597           else
1598             {
1599               printf ("%stem = recog_next_insn (insn, %d);\n", 
1600                       indent, newpos[depth] - 'A');
1601               printf ("%sif (tem == NULL_RTX)\n", indent);
1602               if (afterward)
1603                 printf ("%s  goto L%d;\n", indent, afterward->number);
1604               else
1605                 printf ("%s  goto ret0;\n", indent);
1606               printf ("%slast_insn = tem;\n", indent);
1607             }
1608           printf ("%sx%d = PATTERN (last_insn);\n", indent, depth + 1);
1609         }
1610       else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1611         printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1612                 indent, depth + 1, depth, newpos[depth] - 'a');
1613       else
1614         printf ("%sx%d = XEXP (x%d, %c);\n",
1615                 indent, depth + 1, depth, newpos[depth]);
1616       ++depth;
1617     }
1618 }
1619 \f
1620 /* Print the enumerator constant for CODE -- the upcase version of
1621    the name.  */
1622
1623 static void
1624 print_code (code)
1625      enum rtx_code code;
1626 {
1627   register const char *p;
1628   for (p = GET_RTX_NAME (code); *p; p++)
1629     putchar (TOUPPER (*p));
1630 }
1631
1632 /* Emit code to cross an afterward link -- change state and branch.  */
1633
1634 static void
1635 write_afterward (start, afterward, indent)
1636      struct decision *start;
1637      struct decision *afterward;
1638      const char *indent;
1639 {
1640   if (!afterward || start->subroutine_number > 0)
1641     printf("%sgoto ret0;\n", indent);
1642   else
1643     {
1644       change_state (start->position, afterward->position, NULL, indent);
1645       printf ("%sgoto L%d;\n", indent, afterward->number);
1646     }
1647 }
1648
1649 /* Emit a switch statement, if possible, for an initial sequence of 
1650    nodes at START.  Return the first node yet untested.  */
1651
1652 static struct decision *
1653 write_switch (start, depth)
1654      struct decision *start;
1655      int depth;
1656 {
1657   struct decision *p = start;
1658   enum decision_type type = p->tests->type;
1659
1660   /* If we have two or more nodes in sequence that test the same one
1661      thing, we may be able to use a switch statement.  */
1662
1663   if (!p->next
1664       || p->tests->next
1665       || p->next->tests->type != type
1666       || p->next->tests->next)
1667     return p;
1668
1669   /* DT_code is special in that we can do interesting things with
1670      known predicates at the same time.  */
1671   if (type == DT_code)
1672     {
1673       char codemap[NUM_RTX_CODE];
1674       struct decision *ret;
1675       RTX_CODE code;
1676
1677       memset (codemap, 0, sizeof(codemap));
1678
1679       printf ("  switch (GET_CODE (x%d))\n    {\n", depth);
1680       code = p->tests->u.code;
1681       do 
1682         {
1683           printf ("    case ");
1684           print_code (code);
1685           printf (":\n      goto L%d;\n", p->success.first->number);
1686           p->success.first->need_label = 1;
1687
1688           codemap[code] = 1;
1689           p = p->next;
1690         }
1691       while (p
1692              && ! p->tests->next
1693              && p->tests->type == DT_code
1694              && ! codemap[code = p->tests->u.code]);
1695
1696       /* If P is testing a predicate that we know about and we haven't
1697          seen any of the codes that are valid for the predicate, we can
1698          write a series of "case" statement, one for each possible code.
1699          Since we are already in a switch, these redundant tests are very
1700          cheap and will reduce the number of predicates called.  */
1701
1702       /* Note that while we write out cases for these predicates here,
1703          we don't actually write the test here, as it gets kinda messy.
1704          It is trivial to leave this to later by telling our caller that
1705          we only processed the CODE tests.  */
1706       ret = p;
1707
1708       while (p && p->tests->type == DT_pred
1709              && p->tests->u.pred.index >= 0)
1710         {
1711           const RTX_CODE *c;
1712
1713           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1714             if (codemap[(int) *c] != 0)
1715               goto pred_done;
1716
1717           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1718             {
1719               printf ("    case ");
1720               print_code (*c);
1721               printf (":\n");
1722               codemap[(int) *c] = 1;
1723             }
1724
1725           printf ("      goto L%d;\n", p->number);
1726           p->need_label = 1;
1727           p = p->next;
1728         }
1729
1730     pred_done:
1731       /* Make the default case skip the predicates we managed to match.  */
1732
1733       printf ("    default:\n");
1734       if (p != ret)
1735         {
1736           if (p)
1737             {
1738               printf ("      goto L%d;\n", p->number);
1739               p->need_label = 1;
1740             }
1741           else
1742             write_afterward (start, start->afterward, "      ");
1743         }
1744       else
1745         printf ("     break;\n");
1746       printf ("   }\n");
1747
1748       return ret;
1749     }
1750   else if (type == DT_mode
1751            || type == DT_veclen
1752            || type == DT_elt_zero_int
1753            || type == DT_elt_one_int
1754            || type == DT_elt_zero_wide)
1755     {
1756       printf ("  switch (");
1757       switch (type)
1758         {
1759         case DT_mode:
1760           printf ("GET_MODE (x%d)", depth);
1761           break;
1762         case DT_veclen:
1763           printf ("XVECLEN (x%d, 0)", depth);
1764           break;
1765         case DT_elt_zero_int:
1766           printf ("XINT (x%d, 0)", depth);
1767           break;
1768         case DT_elt_one_int:
1769           printf ("XINT (x%d, 1)", depth);
1770           break;
1771         case DT_elt_zero_wide:
1772           /* Convert result of XWINT to int for portability since some C
1773              compilers won't do it and some will.  */
1774           printf ("(int) XWINT (x%d, 0)", depth);
1775           break;
1776         default:
1777           abort ();
1778         }
1779       printf (")\n    {\n");
1780
1781       do
1782         {
1783           printf ("    case ");
1784           switch (type)
1785             {
1786             case DT_mode:
1787               printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1788               break;
1789             case DT_veclen:
1790               printf ("%d", p->tests->u.veclen);
1791               break;
1792             case DT_elt_zero_int:
1793             case DT_elt_one_int:
1794             case DT_elt_zero_wide:
1795               printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1796               break;
1797             default:
1798               abort ();
1799             }
1800           printf (":\n      goto L%d;\n", p->success.first->number);
1801           p->success.first->need_label = 1;
1802
1803           p = p->next;
1804         }
1805       while (p && p->tests->type == type && !p->tests->next);
1806       
1807       printf ("    default:\n      break;\n    }\n");
1808
1809       return p;
1810     }
1811   else
1812     {
1813       /* None of the other tests are ameanable.  */
1814       return p;
1815     }
1816 }
1817
1818 /* Emit code for one test.  */
1819
1820 static void
1821 write_cond (p, depth, subroutine_type)
1822      struct decision_test *p;
1823      int depth;
1824      enum routine_type subroutine_type;
1825 {
1826   switch (p->type)
1827     {
1828     case DT_mode:
1829       printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1830       break;
1831
1832     case DT_code:
1833       printf ("GET_CODE (x%d) == ", depth);
1834       print_code (p->u.code);
1835       break;
1836
1837     case DT_veclen:
1838       printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1839       break;
1840
1841     case DT_elt_zero_int:
1842       printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1843       break;
1844
1845     case DT_elt_one_int:
1846       printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1847       break;
1848
1849     case DT_elt_zero_wide:
1850       printf ("XWINT (x%d, 0) == ", depth);
1851       printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1852       break;
1853
1854     case DT_dup:
1855       printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1856       break;
1857
1858     case DT_pred:
1859       printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1860               GET_MODE_NAME (p->u.pred.mode));
1861       break;
1862
1863     case DT_c_test:
1864       printf ("(%s)", p->u.c_test);
1865       break;
1866
1867     case DT_accept_insn:
1868       switch (subroutine_type)
1869         {
1870         case RECOG:
1871           if (p->u.insn.num_clobbers_to_add == 0)
1872             abort ();
1873           printf ("pnum_clobbers != NULL");
1874           break;
1875
1876         default:
1877           abort ();
1878         }
1879       break;
1880
1881     default:
1882       abort ();
1883     }
1884 }
1885
1886 /* Emit code for one action.  The previous tests have succeeded;
1887    TEST is the last of the chain.  In the normal case we simply
1888    perform a state change.  For the `accept' tests we must do more work.  */
1889
1890 static void
1891 write_action (test, depth, uncond, success, subroutine_type)
1892      struct decision_test *test;
1893      int depth, uncond;
1894      struct decision *success;
1895      enum routine_type subroutine_type;
1896 {
1897   const char *indent;
1898   int want_close = 0;
1899
1900   if (uncond)
1901     indent = "  ";
1902   else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1903     {
1904       fputs ("    {\n", stdout);
1905       indent = "      ";
1906       want_close = 1;
1907     }
1908   else
1909     indent = "    ";
1910
1911   if (test->type == DT_accept_op)
1912     {
1913       printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1914
1915       /* Only allow DT_accept_insn to follow.  */
1916       if (test->next)
1917         {
1918           test = test->next;
1919           if (test->type != DT_accept_insn)
1920             abort ();
1921         }
1922     }
1923
1924   /* Sanity check that we're now at the end of the list of tests.  */
1925   if (test->next)
1926     abort ();
1927
1928   if (test->type == DT_accept_insn)
1929     {
1930       switch (subroutine_type)
1931         {
1932         case RECOG:
1933           if (test->u.insn.num_clobbers_to_add != 0)
1934             printf ("%s*pnum_clobbers = %d;\n",
1935                     indent, test->u.insn.num_clobbers_to_add);
1936           printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1937           break;
1938
1939         case SPLIT:
1940           printf ("%sreturn gen_split_%d (operands);\n",
1941                   indent, test->u.insn.code_number);
1942           break;
1943
1944         case PEEPHOLE2:
1945           printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1946                   indent, test->u.insn.code_number);
1947           printf ("%sif (tem != 0)\n%s  goto ret1;\n", indent, indent);
1948           break;
1949
1950         default:
1951           abort ();
1952         }
1953     }
1954   else
1955     {
1956       printf("%sgoto L%d;\n", indent, success->number);
1957       success->need_label = 1;
1958     }
1959
1960   if (want_close)
1961     fputs ("    }\n", stdout);
1962 }
1963
1964 /* Return 1 if the test is always true and has no fallthru path.  Return -1
1965    if the test does have a fallthru path, but requires that the condition be
1966    terminated.  Otherwise return 0 for a normal test.  */
1967 /* ??? is_unconditional is a stupid name for a tri-state function.  */
1968
1969 static int
1970 is_unconditional (t, subroutine_type)
1971      struct decision_test *t;
1972      enum routine_type subroutine_type;
1973 {
1974   if (t->type == DT_accept_op)
1975     return 1;
1976
1977   if (t->type == DT_accept_insn)
1978     {
1979       switch (subroutine_type)
1980         {
1981         case RECOG:
1982           return (t->u.insn.num_clobbers_to_add == 0);
1983         case SPLIT:
1984           return 1;
1985         case PEEPHOLE2:
1986           return -1;
1987         default:
1988           abort ();
1989         }
1990     }
1991
1992   return 0;
1993 }
1994
1995 /* Emit code for one node -- the conditional and the accompanying action.
1996    Return true if there is no fallthru path.  */
1997
1998 static int
1999 write_node (p, depth, subroutine_type)
2000      struct decision *p;
2001      int depth;
2002      enum routine_type subroutine_type;
2003 {
2004   struct decision_test *test, *last_test;
2005   int uncond;
2006
2007   last_test = test = p->tests;
2008   uncond = is_unconditional (test, subroutine_type);
2009   if (uncond == 0)
2010     {
2011       printf ("  if (");
2012       write_cond (test, depth, subroutine_type);
2013
2014       while ((test = test->next) != NULL)
2015         {
2016           int uncond2;
2017
2018           last_test = test;
2019           uncond2 = is_unconditional (test, subroutine_type);
2020           if (uncond2 != 0)
2021             break;
2022
2023           printf ("\n      && ");
2024           write_cond (test, depth, subroutine_type);
2025         }
2026
2027       printf (")\n");
2028     }
2029
2030   write_action (last_test, depth, uncond, p->success.first, subroutine_type);
2031
2032   return uncond > 0;
2033 }
2034
2035 /* Emit code for all of the sibling nodes of HEAD.  */
2036
2037 static void
2038 write_tree_1 (head, depth, subroutine_type)
2039      struct decision_head *head;
2040      int depth;
2041      enum routine_type subroutine_type;
2042 {
2043   struct decision *p, *next;
2044   int uncond = 0;
2045
2046   for (p = head->first; p ; p = next)
2047     {
2048       /* The label for the first element was printed in write_tree.  */
2049       if (p != head->first && p->need_label)
2050         OUTPUT_LABEL (" ", p->number);
2051
2052       /* Attempt to write a switch statement for a whole sequence.  */
2053       next = write_switch (p, depth);
2054       if (p != next)
2055         uncond = 0;
2056       else
2057         {
2058           /* Failed -- fall back and write one node.  */
2059           uncond = write_node (p, depth, subroutine_type);
2060           next = p->next;
2061         }
2062     }
2063
2064   /* Finished with this chain.  Close a fallthru path by branching
2065      to the afterward node.  */
2066   if (! uncond)
2067     write_afterward (head->last, head->last->afterward, "  ");
2068 }
2069
2070 /* Write out the decision tree starting at HEAD.  PREVPOS is the
2071    position at the node that branched to this node.  */
2072
2073 static void
2074 write_tree (head, prevpos, type, initial)
2075      struct decision_head *head;
2076      const char *prevpos;
2077      enum routine_type type;
2078      int initial;
2079 {
2080   register struct decision *p = head->first;
2081
2082   putchar ('\n');
2083   if (p->need_label)
2084     OUTPUT_LABEL (" ", p->number);
2085
2086   if (! initial && p->subroutine_number > 0)
2087     {
2088       static const char * const name_prefix[] = {
2089           "recog", "split", "peephole2"
2090       };
2091
2092       static const char * const call_suffix[] = {
2093           ", pnum_clobbers", "", ", _plast_insn"
2094       };
2095
2096       /* This node has been broken out into a separate subroutine.
2097          Call it, test the result, and branch accordingly.  */
2098
2099       if (p->afterward)
2100         {
2101           printf ("  tem = %s_%d (x0, insn%s);\n",
2102                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2103           if (IS_SPLIT (type))
2104             printf ("  if (tem != 0)\n    return tem;\n");
2105           else
2106             printf ("  if (tem >= 0)\n    return tem;\n");
2107
2108           change_state (p->position, p->afterward->position, NULL, "  ");
2109           printf ("  goto L%d;\n", p->afterward->number);
2110         }
2111       else
2112         {
2113           printf ("  return %s_%d (x0, insn%s);\n",
2114                   name_prefix[type], p->subroutine_number, call_suffix[type]);
2115         }
2116     }
2117   else
2118     {
2119       int depth = strlen (p->position);
2120
2121       change_state (prevpos, p->position, head->last->afterward, "  ");
2122       write_tree_1 (head, depth, type);
2123
2124       for (p = head->first; p; p = p->next)
2125         if (p->success.first)
2126           write_tree (&p->success, p->position, type, 0);
2127     }
2128 }
2129
2130 /* Write out a subroutine of type TYPE to do comparisons starting at
2131    node TREE.  */
2132
2133 static void
2134 write_subroutine (head, type)
2135      struct decision_head *head;
2136      enum routine_type type;
2137 {
2138   int subfunction = head->first ? head->first->subroutine_number : 0;
2139   const char *s_or_e;
2140   char extension[32];
2141   int i;
2142   
2143   s_or_e = subfunction ? "static " : "";
2144
2145   if (subfunction)
2146     sprintf (extension, "_%d", subfunction);
2147   else if (type == RECOG)
2148     extension[0] = '\0';
2149   else
2150     strcpy (extension, "_insns");
2151
2152   switch (type)
2153     {
2154     case RECOG:
2155       printf ("%sint recog%s PARAMS ((rtx, rtx, int *));\n", s_or_e, extension);
2156       printf ("%sint\n\
2157 recog%s (x0, insn, pnum_clobbers)\n\
2158      register rtx x0;\n\
2159      rtx insn ATTRIBUTE_UNUSED;\n\
2160      int *pnum_clobbers ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2161       break;
2162     case SPLIT:
2163       printf ("%srtx split%s PARAMS ((rtx, rtx));\n", s_or_e, extension);
2164       printf ("%srtx\n\
2165 split%s (x0, insn)\n\
2166      register rtx x0;\n\
2167      rtx insn ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2168       break;
2169     case PEEPHOLE2:
2170       printf ("%srtx peephole2%s PARAMS ((rtx, rtx, rtx *));\n", s_or_e, extension);
2171       printf ("%srtx\n\
2172 peephole2%s (x0, insn, _plast_insn)\n\
2173      register rtx x0;\n\
2174      rtx insn ATTRIBUTE_UNUSED;\n\
2175      rtx *_plast_insn ATTRIBUTE_UNUSED;\n", s_or_e, extension);
2176       break;
2177     }
2178
2179   printf ("{\n  register rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n");
2180   for (i = 1; i <= max_depth; i++)
2181     printf ("  register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2182
2183   if (type == PEEPHOLE2)
2184     printf ("  register rtx last_insn = insn;\n");
2185   printf ("  %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2186
2187   if (head->first)
2188     write_tree (head, "", type, 1);
2189   else
2190     printf ("  goto ret0;\n");
2191
2192   if (type == PEEPHOLE2)
2193     printf (" ret1:\n  *_plast_insn = last_insn;\n  return tem;\n");
2194   printf (" ret0:\n  return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2195 }
2196
2197 /* In break_out_subroutines, we discovered the boundaries for the
2198    subroutines, but did not write them out.  Do so now.  */
2199
2200 static void
2201 write_subroutines (head, type)
2202      struct decision_head *head;
2203      enum routine_type type;
2204 {
2205   struct decision *p;
2206
2207   for (p = head->first; p ; p = p->next)
2208     if (p->success.first)
2209       write_subroutines (&p->success, type);
2210
2211   if (head->first->subroutine_number > 0)
2212     write_subroutine (head, type);
2213 }
2214
2215 /* Begin the output file.  */
2216
2217 static void
2218 write_header ()
2219 {
2220   puts ("\
2221 /* Generated automatically by the program `genrecog' from the target\n\
2222    machine description file.  */\n\
2223 \n\
2224 #include \"config.h\"\n\
2225 #include \"system.h\"\n\
2226 #include \"rtl.h\"\n\
2227 #include \"tm_p.h\"\n\
2228 #include \"function.h\"\n\
2229 #include \"insn-config.h\"\n\
2230 #include \"recog.h\"\n\
2231 #include \"real.h\"\n\
2232 #include \"output.h\"\n\
2233 #include \"flags.h\"\n\
2234 #include \"hard-reg-set.h\"\n\
2235 #include \"resource.h\"\n\
2236 \n");
2237
2238   puts ("\n\
2239 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2240    X0 is a valid instruction.\n\
2241 \n\
2242    recog returns -1 if the rtx is not valid.  If the rtx is valid, recog\n\
2243    returns a nonnegative number which is the insn code number for the\n\
2244    pattern that matched.  This is the same as the order in the machine\n\
2245    description of the entry that matched.  This number can be used as an\n\
2246    index into `insn_data' and other tables.\n\
2247 \n\
2248    The third argument to recog is an optional pointer to an int.  If\n\
2249    present, recog will accept a pattern if it matches except for missing\n\
2250    CLOBBER expressions at the end.  In that case, the value pointed to by\n\
2251    the optional pointer will be set to the number of CLOBBERs that need\n\
2252    to be added (it should be initialized to zero by the caller).  If it\n\
2253    is set nonzero, the caller should allocate a PARALLEL of the\n\
2254    appropriate size, copy the initial entries, and call add_clobbers\n\
2255    (found in insn-emit.c) to fill in the CLOBBERs.\n\
2256 ");
2257
2258   puts ("\n\
2259    The function split_insns returns 0 if the rtl could not\n\
2260    be split or the split rtl in a SEQUENCE if it can be.\n\
2261 \n\
2262    The function peephole2_insns returns 0 if the rtl could not\n\
2263    be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2264    and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2265 */\n\n");
2266 }
2267
2268 \f
2269 /* Construct and return a sequence of decisions
2270    that will recognize INSN.
2271
2272    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
2273
2274 static struct decision_head
2275 make_insn_sequence (insn, type)
2276      rtx insn;
2277      enum routine_type type;
2278 {
2279   rtx x;
2280   const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2281   struct decision *last;
2282   struct decision_test *test, **place;
2283   struct decision_head head;
2284   char c_test_pos[2];
2285
2286   record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2287
2288   c_test_pos[0] = '\0';
2289   if (type == PEEPHOLE2)
2290     {
2291       int i, j;
2292
2293       /* peephole2 gets special treatment:
2294          - X always gets an outer parallel even if it's only one entry
2295          - we remove all traces of outer-level match_scratch and match_dup
2296            expressions here.  */
2297       x = rtx_alloc (PARALLEL);
2298       PUT_MODE (x, VOIDmode);
2299       XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2300       for (i = j = 0; i < XVECLEN (insn, 0); i++)
2301         {
2302           rtx tmp = XVECEXP (insn, 0, i);
2303           if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2304             {
2305               XVECEXP (x, 0, j) = tmp;
2306               j++;
2307             }
2308         }
2309       XVECLEN (x, 0) = j;
2310
2311       c_test_pos[0] = 'A' + j - 1;
2312       c_test_pos[1] = '\0';
2313     }
2314   else if (XVECLEN (insn, type == RECOG) == 1)
2315     x = XVECEXP (insn, type == RECOG, 0);
2316   else
2317     {
2318       x = rtx_alloc (PARALLEL);
2319       XVEC (x, 0) = XVEC (insn, type == RECOG);
2320       PUT_MODE (x, VOIDmode);
2321     }
2322
2323   validate_pattern (x, insn, NULL_RTX);
2324
2325   memset(&head, 0, sizeof(head));
2326   last = add_to_sequence (x, &head, "", type, 1);
2327
2328   /* Find the end of the test chain on the last node.  */
2329   for (test = last->tests; test->next; test = test->next)
2330     continue;
2331   place = &test->next;
2332
2333   if (c_test[0])
2334     {
2335       /* Need a new node if we have another test to add.  */
2336       if (test->type == DT_accept_op)
2337         {
2338           last = new_decision (c_test_pos, &last->success);
2339           place = &last->tests;
2340         }
2341       test = new_decision_test (DT_c_test, &place);
2342       test->u.c_test = c_test;
2343     }
2344
2345   test = new_decision_test (DT_accept_insn, &place);
2346   test->u.insn.code_number = next_insn_code;
2347   test->u.insn.lineno = pattern_lineno;
2348   test->u.insn.num_clobbers_to_add = 0;
2349
2350   switch (type)
2351     {
2352     case RECOG:
2353       /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2354          with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2355          If so, set up to recognize the pattern without these CLOBBERs.  */
2356
2357       if (GET_CODE (x) == PARALLEL)
2358         {
2359           int i;
2360
2361           /* Find the last non-clobber in the parallel.  */
2362           for (i = XVECLEN (x, 0); i > 0; i--)
2363             {
2364               rtx y = XVECEXP (x, 0, i - 1);
2365               if (GET_CODE (y) != CLOBBER
2366                   || (GET_CODE (XEXP (y, 0)) != REG
2367                       && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2368                 break;
2369             }
2370
2371           if (i != XVECLEN (x, 0))
2372             {
2373               rtx new;
2374               struct decision_head clobber_head;
2375
2376               /* Build a similar insn without the clobbers.  */
2377               if (i == 1)
2378                 new = XVECEXP (x, 0, 0);
2379               else
2380                 {
2381                   int j;
2382
2383                   new = rtx_alloc (PARALLEL);
2384                   XVEC (new, 0) = rtvec_alloc (i);
2385                   for (j = i - 1; j >= 0; j--)
2386                     XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2387                 }
2388
2389               /* Recognize it.  */
2390               memset (&clobber_head, 0, sizeof(clobber_head));
2391               last = add_to_sequence (new, &clobber_head, "", type, 1);
2392
2393               /* Find the end of the test chain on the last node.  */
2394               for (test = last->tests; test->next; test = test->next)
2395                 continue;
2396
2397               /* We definitely have a new test to add -- create a new
2398                  node if needed.  */
2399               place = &test->next;
2400               if (test->type == DT_accept_op)
2401                 {
2402                   last = new_decision ("", &last->success);
2403                   place = &last->tests;
2404                 }
2405
2406               if (c_test[0])
2407                 {
2408                   test = new_decision_test (DT_c_test, &place);
2409                   test->u.c_test = c_test;
2410                 }
2411
2412               test = new_decision_test (DT_accept_insn, &place);
2413               test->u.insn.code_number = next_insn_code;
2414               test->u.insn.lineno = pattern_lineno;
2415               test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2416
2417               merge_trees (&head, &clobber_head);
2418             }
2419         }
2420       break;
2421
2422     case SPLIT:
2423       /* Define the subroutine we will call below and emit in genemit.  */
2424       printf ("extern rtx gen_split_%d PARAMS ((rtx *));\n", next_insn_code);
2425       break;
2426
2427     case PEEPHOLE2:
2428       /* Define the subroutine we will call below and emit in genemit.  */
2429       printf ("extern rtx gen_peephole2_%d PARAMS ((rtx, rtx *));\n",
2430               next_insn_code);
2431       break;
2432     }
2433
2434   return head;
2435 }
2436
2437 static void
2438 process_tree (head, subroutine_type)
2439      struct decision_head *head;
2440      enum routine_type subroutine_type;
2441 {
2442   if (head->first == NULL)
2443     {
2444       /* We can elide peephole2_insns, but not recog or split_insns.  */
2445       if (subroutine_type == PEEPHOLE2)
2446         return;
2447     }
2448   else
2449     {
2450       factor_tests (head);
2451
2452       next_subroutine_number = 0;
2453       break_out_subroutines (head, 1);
2454       find_afterward (head, NULL);
2455
2456       /* We run this after find_afterward, because find_afterward needs
2457          the redundant DT_mode tests on predicates to determine whether
2458          two tests can both be true or not.  */
2459       simplify_tests(head);
2460
2461       write_subroutines (head, subroutine_type);
2462     }
2463
2464   write_subroutine (head, subroutine_type);
2465 }
2466 \f
2467 extern int main PARAMS ((int, char **));
2468
2469 int
2470 main (argc, argv)
2471      int argc;
2472      char **argv;
2473 {
2474   rtx desc;
2475   struct decision_head recog_tree, split_tree, peephole2_tree, h;
2476
2477   progname = "genrecog";
2478
2479   memset (&recog_tree, 0, sizeof recog_tree);
2480   memset (&split_tree, 0, sizeof split_tree);
2481   memset (&peephole2_tree, 0, sizeof peephole2_tree);
2482
2483   if (argc <= 1)
2484     fatal ("No input file name.");
2485
2486   if (init_md_reader (argv[1]) != SUCCESS_EXIT_CODE)
2487     return (FATAL_EXIT_CODE);
2488
2489   next_insn_code = 0;
2490   next_index = 0;
2491
2492   write_header ();
2493
2494   /* Read the machine description.  */
2495
2496   while (1)
2497     {
2498       desc = read_md_rtx (&pattern_lineno, &next_insn_code);
2499       if (desc == NULL)
2500         break;
2501
2502       if (GET_CODE (desc) == DEFINE_INSN)
2503         {
2504           h = make_insn_sequence (desc, RECOG);
2505           merge_trees (&recog_tree, &h);
2506         }
2507       else if (GET_CODE (desc) == DEFINE_SPLIT)
2508         {
2509           h = make_insn_sequence (desc, SPLIT);
2510           merge_trees (&split_tree, &h);
2511         }
2512       else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2513         {
2514           h = make_insn_sequence (desc, PEEPHOLE2);
2515           merge_trees (&peephole2_tree, &h);
2516         }
2517         
2518       next_index++;
2519     }
2520
2521   if (error_count)
2522     return FATAL_EXIT_CODE;
2523
2524   puts ("\n\n");
2525
2526   process_tree (&recog_tree, RECOG);
2527   process_tree (&split_tree, SPLIT);
2528   process_tree (&peephole2_tree, PEEPHOLE2);
2529
2530   fflush (stdout);
2531   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2532 }
2533 \f
2534 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
2535 const char *
2536 get_insn_name (code)
2537      int code;
2538 {
2539   if (code < insn_name_ptr_size)
2540     return insn_name_ptr[code];
2541   else
2542     return NULL;
2543 }
2544
2545 static void
2546 record_insn_name (code, name)
2547      int code;
2548      const char *name;
2549 {
2550   static const char *last_real_name = "insn";
2551   static int last_real_code = 0;
2552   char *new;
2553
2554   if (insn_name_ptr_size <= code)
2555     {
2556       int new_size;
2557       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2558       insn_name_ptr =
2559         (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2560       memset (insn_name_ptr + insn_name_ptr_size, 0, 
2561               sizeof(char *) * (new_size - insn_name_ptr_size));
2562       insn_name_ptr_size = new_size;
2563     }
2564
2565   if (!name || name[0] == '\0')
2566     {
2567       new = xmalloc (strlen (last_real_name) + 10);
2568       sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2569     }
2570   else
2571     {
2572       last_real_name = new = xstrdup (name);
2573       last_real_code = code;
2574     }
2575   
2576   insn_name_ptr[code] = new;
2577 }  
2578 \f
2579 char *
2580 xstrdup (input)
2581   const char *input;
2582 {
2583   register size_t len = strlen (input) + 1;
2584   register char *output = xmalloc (len);
2585   memcpy (output, input, len);
2586   return output;
2587 }
2588
2589 PTR
2590 xrealloc (old, size)
2591   PTR old;
2592   size_t size;
2593 {
2594   register PTR ptr;
2595   if (old)
2596     ptr = (PTR) realloc (old, size);
2597   else
2598     ptr = (PTR) malloc (size);
2599   if (!ptr)
2600     fatal ("virtual memory exhausted");
2601   return ptr;
2602 }
2603
2604 PTR
2605 xmalloc (size)
2606   size_t size;
2607 {
2608   register PTR val = (PTR) malloc (size);
2609
2610   if (val == 0)
2611     fatal ("virtual memory exhausted");
2612   return val;
2613 }
2614 \f
2615 static void
2616 debug_decision_2 (test)
2617      struct decision_test *test;
2618 {
2619   switch (test->type)
2620     {
2621     case DT_mode:
2622       fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2623       break;
2624     case DT_code:
2625       fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2626       break;
2627     case DT_veclen:
2628       fprintf (stderr, "veclen=%d", test->u.veclen);
2629       break;
2630     case DT_elt_zero_int:
2631       fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2632       break;
2633     case DT_elt_one_int:
2634       fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2635       break;
2636     case DT_elt_zero_wide:
2637       fprintf (stderr, "elt0_w=");
2638       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2639       break;
2640     case DT_dup:
2641       fprintf (stderr, "dup=%d", test->u.dup);
2642       break;
2643     case DT_pred:
2644       fprintf (stderr, "pred=(%s,%s)",
2645                test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2646       break;
2647     case DT_c_test:
2648       {
2649         char sub[16+4];
2650         strncpy (sub, test->u.c_test, sizeof(sub));
2651         memcpy (sub+16, "...", 4);
2652         fprintf (stderr, "c_test=\"%s\"", sub);
2653       }
2654       break;
2655     case DT_accept_op:
2656       fprintf (stderr, "A_op=%d", test->u.opno);
2657       break;
2658     case DT_accept_insn:
2659       fprintf (stderr, "A_insn=(%d,%d)", 
2660                test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2661       break;
2662
2663     default:
2664       abort ();
2665     }
2666 }
2667
2668 static void
2669 debug_decision_1 (d, indent)
2670      struct decision *d;
2671      int indent;
2672 {
2673   int i;
2674   struct decision_test *test;
2675
2676   if (d == NULL)
2677     {
2678       for (i = 0; i < indent; ++i)
2679         putc (' ', stderr);
2680       fputs ("(nil)\n", stderr);
2681       return;
2682     }
2683
2684   for (i = 0; i < indent; ++i)
2685     putc (' ', stderr);
2686
2687   putc ('{', stderr);
2688   test = d->tests;
2689   if (test)
2690     {
2691       debug_decision_2 (test);
2692       while ((test = test->next) != NULL)
2693         {
2694           fputs (" + ", stderr);
2695           debug_decision_2 (test);
2696         }
2697     }
2698   fprintf (stderr, "} %d n %d a %d\n", d->number,
2699            (d->next ? d->next->number : -1),
2700            (d->afterward ? d->afterward->number : -1));
2701 }
2702
2703 static void
2704 debug_decision_0 (d, indent, maxdepth)
2705      struct decision *d;
2706      int indent, maxdepth;
2707 {
2708   struct decision *n;
2709   int i;
2710
2711   if (maxdepth < 0)
2712     return;
2713   if (d == NULL)
2714     {
2715       for (i = 0; i < indent; ++i)
2716         putc (' ', stderr);
2717       fputs ("(nil)\n", stderr);
2718       return;
2719     }
2720
2721   debug_decision_1 (d, indent);
2722   for (n = d->success.first; n ; n = n->next)
2723     debug_decision_0 (n, indent + 2, maxdepth - 1);
2724 }
2725
2726 void
2727 debug_decision (d)
2728      struct decision *d;
2729 {
2730   debug_decision_0 (d, 0, 1000000);
2731 }
2732
2733 void
2734 debug_decision_list (d)
2735      struct decision *d;
2736 {
2737   while (d)
2738     {
2739       debug_decision_0 (d, 0, 0);
2740       d = d->next;
2741     }
2742 }