OSDN Git Service

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