OSDN Git Service

* rtl.c (dump_and_abort): Remove.
[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           else if (d2->type == DT_pred)
841             {
842               if (d2->u.pred.mode != VOIDmode
843                   && d1->u.pred.mode != d2->u.pred.mode)
844                 return 0;
845             }
846         }
847
848       if (d1->u.pred.index >= 0)
849         {
850           /* If D2 tests a code, see if it is in the list of valid
851              codes for D1's predicate.  */
852           if (d2->type == DT_code)
853             {
854               const RTX_CODE *c = &preds[d1->u.pred.index].codes[0];
855               while (*c != 0)
856                 {
857                   if (*c == d2->u.code)
858                     break;
859                   ++c;
860                 }
861               if (*c == 0)
862                 return 0;
863             }
864
865           /* Otherwise see if the predicates have any codes in common.  */
866           else if (d2->type == DT_pred && d2->u.pred.index >= 0)
867             {
868               const RTX_CODE *c1 = &preds[d1->u.pred.index].codes[0];
869               int common = 0;
870
871               while (*c1 != 0 && !common)
872                 {
873                   const RTX_CODE *c2 = &preds[d2->u.pred.index].codes[0];
874                   while (*c2 != 0 && !common)
875                     {
876                       common = (*c1 == *c2);
877                       ++c2;
878                     }
879                   ++c1;
880                 }
881
882               if (!common)
883                 return 0;
884             }
885         }
886     }
887
888   return -1;
889 }
890
891 /* A subroutine of maybe_both_true; examines all the tests for a given node.
892    Returns > 0 for "definitely both true" and < 0 for "maybe both true".  */
893
894 static int
895 maybe_both_true_1 (d1, d2)
896      struct decision_test *d1, *d2;
897 {
898   struct decision_test *t1, *t2;
899
900   /* A match_operand with no predicate can match anything.  Recognize
901      this by the existance of a lone DT_accept_op test.  */
902   if (d1->type == DT_accept_op || d2->type == DT_accept_op)
903     return 1;
904
905   /* Eliminate pairs of tests while they can exactly match.  */
906   while (d1 && d2 && d1->type == d2->type)
907     {
908       if (maybe_both_true_2 (d1, d2) == 0)
909         return 0;
910       d1 = d1->next, d2 = d2->next;
911     }
912
913   /* After that, consider all pairs.  */
914   for (t1 = d1; t1 ; t1 = t1->next)
915     for (t2 = d2; t2 ; t2 = t2->next)
916       if (maybe_both_true_2 (t1, t2) == 0)
917         return 0;
918
919   return -1;
920 }
921
922 /* Return 0 if we can prove that there is no RTL that can match both
923    D1 and D2.  Otherwise, return 1 (it may be that there is an RTL that
924    can match both or just that we couldn't prove there wasn't such an RTL).
925
926    TOPLEVEL is non-zero if we are to only look at the top level and not
927    recursively descend.  */
928
929 static int
930 maybe_both_true (d1, d2, toplevel)
931      struct decision *d1, *d2;
932      int toplevel;
933 {
934   struct decision *p1, *p2;
935   int cmp;
936
937   /* Don't compare strings on the different positions in insn.  Doing so
938      is incorrect and results in false matches from constructs like
939
940         [(set (subreg:HI (match_operand:SI "register_operand" "r") 0)
941               (subreg:HI (match_operand:SI "register_operand" "r") 0))]
942      vs
943         [(set (match_operand:HI "register_operand" "r")
944               (match_operand:HI "register_operand" "r"))]
945
946      If we are presented with such, we are recursing through the remainder
947      of a node's success nodes (from the loop at the end of this function).
948      Skip forward until we come to a position that matches.
949
950      Due to the way position strings are constructed, we know that iterating
951      forward from the lexically lower position (e.g. "00") will run into
952      the lexically higher position (e.g. "1") and not the other way around.
953      This saves a bit of effort.  */
954
955   cmp = strcmp (d1->position, d2->position);
956   if (cmp != 0)
957     {
958       if (toplevel)
959         abort();
960
961       /* If the d2->position was lexically lower, swap.  */
962       if (cmp > 0)
963         p1 = d1, d1 = d2, d2 = p1;
964
965       if (d1->success.first == 0)
966         return 0;
967       for (p1 = d1->success.first; p1; p1 = p1->next)
968         if (maybe_both_true (p1, d2, 0))
969           return 1;
970
971       return 0;
972     }
973
974   /* Test the current level.  */
975   cmp = maybe_both_true_1 (d1->tests, d2->tests);
976   if (cmp >= 0)
977     return cmp;
978
979   /* We can't prove that D1 and D2 cannot both be true.  If we are only
980      to check the top level, return 1.  Otherwise, see if we can prove
981      that all choices in both successors are mutually exclusive.  If
982      either does not have any successors, we can't prove they can't both
983      be true.  */
984
985   if (toplevel || d1->success.first == 0 || d2->success.first == 0)
986     return 1;
987
988   for (p1 = d1->success.first; p1; p1 = p1->next)
989     for (p2 = d2->success.first; p2; p2 = p2->next)
990       if (maybe_both_true (p1, p2, 0))
991         return 1;
992
993   return 0;
994 }
995
996 /* A subroutine of nodes_identical.  Examine two tests for equivalence.  */
997
998 static int
999 nodes_identical_1 (d1, d2)
1000      struct decision_test *d1, *d2;
1001 {
1002   switch (d1->type)
1003     {
1004     case DT_mode:
1005       return d1->u.mode == d2->u.mode;
1006
1007     case DT_code:
1008       return d1->u.code == d2->u.code;
1009
1010     case DT_pred:
1011       return (d1->u.pred.mode == d2->u.pred.mode
1012               && strcmp (d1->u.pred.name, d2->u.pred.name) == 0);
1013
1014     case DT_c_test:
1015       return strcmp (d1->u.c_test, d2->u.c_test) == 0;
1016
1017     case DT_veclen:
1018       return d1->u.veclen == d2->u.veclen;
1019
1020     case DT_dup:
1021       return d1->u.dup == d2->u.dup;
1022
1023     case DT_elt_zero_int:
1024     case DT_elt_one_int:
1025     case DT_elt_zero_wide:
1026       return d1->u.intval == d2->u.intval;
1027
1028     case DT_accept_op:
1029       return d1->u.opno == d2->u.opno;
1030
1031     case DT_accept_insn:
1032       /* Differences will be handled in merge_accept_insn.  */
1033       return 1;
1034
1035     default:
1036       abort ();
1037     }
1038 }
1039
1040 /* True iff the two nodes are identical (on one level only).  Due
1041    to the way these lists are constructed, we shouldn't have to 
1042    consider different orderings on the tests.  */
1043
1044 static int
1045 nodes_identical (d1, d2)
1046      struct decision *d1, *d2;
1047 {
1048   struct decision_test *t1, *t2;
1049
1050   for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next)
1051     {
1052       if (t1->type != t2->type)
1053         return 0;
1054       if (! nodes_identical_1 (t1, t2))
1055         return 0;
1056     }
1057
1058   /* For success, they should now both be null.  */
1059   return t1 == t2;
1060 }
1061
1062 /* A subroutine of merge_trees; given two nodes that have been declared
1063    identical, cope with two insn accept states.  If they differ in the
1064    number of clobbers, then the conflict was created by make_insn_sequence
1065    and we can drop the with-clobbers version on the floor.  If both 
1066    nodes have no additional clobbers, we have found an ambiguity in the
1067    source machine description.  */
1068
1069 static void
1070 merge_accept_insn (oldd, addd)
1071      struct decision *oldd, *addd;
1072 {
1073   struct decision_test *old, *add;
1074
1075   for (old = oldd->tests; old; old = old->next)
1076     if (old->type == DT_accept_insn)
1077       break;
1078   if (old == NULL)
1079     return;
1080
1081   for (add = addd->tests; add; add = add->next)
1082     if (add->type == DT_accept_insn)
1083       break;
1084   if (add == NULL)
1085     return;
1086
1087   /* If one node is for a normal insn and the second is for the base
1088      insn with clobbers stripped off, the second node should be ignored.  */
1089
1090   if (old->u.insn.num_clobbers_to_add == 0
1091       && add->u.insn.num_clobbers_to_add > 0)
1092     {
1093       /* Nothing to do here.  */
1094     }
1095   else if (old->u.insn.num_clobbers_to_add > 0
1096            && add->u.insn.num_clobbers_to_add == 0)
1097     {
1098       /* In this case, replace OLD with ADD.  */
1099       old->u.insn = add->u.insn;
1100     }
1101   else
1102     {
1103       message_with_line (add->u.insn.lineno, "`%s' matches `%s'",
1104                          get_insn_name (add->u.insn.code_number),
1105                          get_insn_name (old->u.insn.code_number));
1106       message_with_line (old->u.insn.lineno, "previous definition of `%s'",
1107                          get_insn_name (old->u.insn.code_number));
1108       error_count++;
1109     }
1110 }
1111
1112 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively.  */
1113
1114 static void
1115 merge_trees (oldh, addh)
1116      struct decision_head *oldh, *addh;
1117 {
1118   struct decision *next, *add;
1119
1120   if (addh->first == 0)
1121     return;
1122   if (oldh->first == 0)
1123     {
1124       *oldh = *addh;
1125       return;
1126     }
1127
1128   /* Trying to merge bits at different positions isn't possible.  */
1129   if (strcmp (oldh->first->position, addh->first->position))
1130     abort ();
1131
1132   for (add = addh->first; add ; add = next)
1133     {
1134       struct decision *old, *insert_before = NULL;
1135
1136       next = add->next;
1137
1138       /* The semantics of pattern matching state that the tests are
1139          done in the order given in the MD file so that if an insn
1140          matches two patterns, the first one will be used.  However,
1141          in practice, most, if not all, patterns are unambiguous so
1142          that their order is independent.  In that case, we can merge
1143          identical tests and group all similar modes and codes together.
1144
1145          Scan starting from the end of OLDH until we reach a point
1146          where we reach the head of the list or where we pass a
1147          pattern that could also be true if NEW is true.  If we find
1148          an identical pattern, we can merge them.  Also, record the
1149          last node that tests the same code and mode and the last one
1150          that tests just the same mode.
1151
1152          If we have no match, place NEW after the closest match we found.  */
1153          
1154       for (old = oldh->last; old; old = old->prev)
1155         {
1156           if (nodes_identical (old, add))
1157             {
1158               merge_accept_insn (old, add);
1159               merge_trees (&old->success, &add->success);
1160               goto merged_nodes;
1161             }
1162
1163           if (maybe_both_true (old, add, 0))
1164             break;
1165
1166           /* Insert the nodes in DT test type order, which is roughly
1167              how expensive/important the test is.  Given that the tests
1168              are also ordered within the list, examining the first is
1169              sufficient.  */
1170           if (add->tests->type < old->tests->type)
1171             insert_before = old;
1172         }
1173
1174       if (insert_before == NULL)
1175         {
1176           add->next = NULL;
1177           add->prev = oldh->last;
1178           oldh->last->next = add;
1179           oldh->last = add;
1180         }
1181       else
1182         {
1183           if ((add->prev = insert_before->prev) != NULL)
1184             add->prev->next = add;
1185           else
1186             oldh->first = add;
1187           add->next = insert_before;
1188           insert_before->prev = add;
1189         }
1190
1191     merged_nodes:;
1192     }
1193 }
1194 \f
1195 /* Walk the tree looking for sub-nodes that perform common tests.  
1196    Factor out the common test into a new node.  This enables us
1197    (depending on the test type) to emit switch statements later.  */
1198
1199 static void
1200 factor_tests (head)
1201      struct decision_head *head;
1202 {
1203   struct decision *first, *next;
1204
1205   for (first = head->first; first && first->next; first = next)
1206     {
1207       enum decision_type type;
1208       struct decision *new, *old_last;
1209
1210       type = first->tests->type;
1211       next = first->next;
1212
1213       /* Want at least two compatible sequential nodes.  */
1214       if (next->tests->type != type)
1215         continue;
1216
1217       /* Don't want all node types, just those we can turn into 
1218          switch statements.  */
1219       if (type != DT_mode
1220           && type != DT_code
1221           && type != DT_veclen
1222           && type != DT_elt_zero_int
1223           && type != DT_elt_one_int
1224           && type != DT_elt_zero_wide)
1225         continue;
1226
1227       /* If we'd been performing more than one test, create a new node
1228          below our first test.  */
1229       if (first->tests->next != NULL)
1230         {
1231           new = new_decision (first->position, &first->success);
1232           new->tests = first->tests->next;
1233           first->tests->next = NULL;
1234         }
1235         
1236       /* Crop the node tree off after our first test.  */
1237       first->next = NULL;
1238       old_last = head->last;
1239       head->last = first;
1240
1241       /* For each compatible test, adjust to perform only one test in
1242          the top level node, then merge the node back into the tree.  */
1243       do
1244         {
1245           struct decision_head h;
1246
1247           if (next->tests->next != NULL)
1248             {
1249               new = new_decision (next->position, &next->success);
1250               new->tests = next->tests->next;
1251               next->tests->next = NULL;
1252             }
1253           new = next;
1254           next = next->next;
1255           new->next = NULL;
1256           h.first = h.last = new;
1257
1258           merge_trees (head, &h);
1259         }
1260       while (next && next->tests->type == type);
1261
1262       /* After we run out of compatible tests, graft the remaining nodes
1263          back onto the tree.  */
1264       if (next)
1265         {
1266           next->prev = head->last;
1267           head->last->next = next;
1268           head->last = old_last;
1269         }
1270     }
1271
1272   /* Recurse.  */
1273   for (first = head->first; first; first = first->next)
1274     factor_tests (&first->success);
1275 }
1276
1277 /* After factoring, try to simplify the tests on any one node.
1278    Tests that are useful for switch statements are recognizable
1279    by having only a single test on a node -- we'll be manipulating
1280    nodes with multiple tests:
1281
1282    If we have mode tests or code tests that are redundant with
1283    predicates, remove them.  */
1284
1285 static void
1286 simplify_tests (head)
1287      struct decision_head *head;
1288 {
1289   struct decision *tree;
1290
1291   for (tree = head->first; tree; tree = tree->next)
1292     {
1293       struct decision_test *a, *b;
1294
1295       a = tree->tests;
1296       b = a->next;
1297       if (b == NULL)
1298         continue;
1299
1300       /* Find a predicate node.  */
1301       while (b && b->type != DT_pred)
1302         b = b->next;
1303       if (b)
1304         {
1305           /* Due to how these tests are constructed, we don't even need
1306              to check that the mode and code are compatible -- they were
1307              generated from the predicate in the first place.  */
1308           while (a->type == DT_mode || a->type == DT_code)
1309             a = a->next;
1310           tree->tests = a;
1311         }
1312     }
1313
1314   /* Recurse.  */
1315   for (tree = head->first; tree; tree = tree->next)
1316     simplify_tests (&tree->success);
1317 }
1318
1319 /* Count the number of subnodes of HEAD.  If the number is high enough,
1320    make the first node in HEAD start a separate subroutine in the C code
1321    that is generated.  */
1322
1323 static int
1324 break_out_subroutines (head, initial)
1325      struct decision_head *head;
1326      int initial;
1327 {
1328   int size = 0;
1329   struct decision *sub;
1330
1331   for (sub = head->first; sub; sub = sub->next)
1332     size += 1 + break_out_subroutines (&sub->success, 0);
1333
1334   if (size > SUBROUTINE_THRESHOLD && ! initial)
1335     {
1336       head->first->subroutine_number = ++next_subroutine_number;
1337       size = 1;
1338     }
1339   return size;
1340 }
1341
1342 /* For each node p, find the next alternative that might be true
1343    when p is true.  */
1344
1345 static void
1346 find_afterward (head, real_afterward)
1347      struct decision_head *head;
1348      struct decision *real_afterward;
1349 {
1350   struct decision *p, *q, *afterward;
1351
1352   /* We can't propogate alternatives across subroutine boundaries. 
1353      This is not incorrect, merely a minor optimization loss.  */
1354
1355   p = head->first;
1356   afterward = (p->subroutine_number > 0 ? NULL : real_afterward);
1357
1358   for ( ; p ; p = p->next)
1359     {
1360       /* Find the next node that might be true if this one fails.  */
1361       for (q = p->next; q ; q = q->next)
1362         if (maybe_both_true (p, q, 1))
1363           break;
1364
1365       /* If we reached the end of the list without finding one, 
1366          use the incoming afterward position.  */
1367       if (!q)
1368         q = afterward;
1369       p->afterward = q;
1370       if (q)
1371         q->need_label = 1;
1372     }
1373
1374   /* Recurse.  */
1375   for (p = head->first; p ; p = p->next)
1376     if (p->success.first)
1377       find_afterward (&p->success, p->afterward);
1378
1379   /* When we are generating a subroutine, record the real afterward
1380      position in the first node where write_tree can find it, and we
1381      can do the right thing at the subroutine call site.  */
1382   p = head->first;
1383   if (p->subroutine_number > 0)
1384     p->afterward = real_afterward;
1385 }
1386 \f
1387 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1388    actions are necessary to move to NEWPOS.  If we fail to move to the
1389    new state, branch to node AFTERWARD if non-zero, otherwise return.
1390
1391    Failure to move to the new state can only occur if we are trying to
1392    match multiple insns and we try to step past the end of the stream. */
1393
1394 static void
1395 change_state (oldpos, newpos, afterward, indent)
1396      const char *oldpos;
1397      const char *newpos;
1398      struct decision *afterward;
1399      const char *indent;
1400 {
1401   int odepth = strlen (oldpos);
1402   int ndepth = strlen (newpos);
1403   int depth;
1404   int old_has_insn, new_has_insn;
1405
1406   /* Pop up as many levels as necessary.  */
1407   for (depth = odepth; strncmp (oldpos, newpos, depth) != 0; --depth)
1408     continue;
1409
1410   /* Hunt for the last [A-Z] in both strings.  */
1411   for (old_has_insn = odepth - 1; old_has_insn >= 0; --old_has_insn)
1412     if (oldpos[old_has_insn] >= 'A' && oldpos[old_has_insn] <= 'Z')
1413       break;
1414   for (new_has_insn = odepth - 1; new_has_insn >= 0; --new_has_insn)
1415     if (newpos[new_has_insn] >= 'A' && newpos[new_has_insn] <= 'Z')
1416       break;
1417
1418   /* Make sure to reset the _last_insn pointer when popping back up.  */
1419   if (old_has_insn >= 0 && new_has_insn < 0)
1420     printf ("%s_last_insn = insn;\n", indent);
1421
1422   /* Go down to desired level.  */
1423   while (depth < ndepth)
1424     {
1425       /* It's a different insn from the first one. */
1426       if (newpos[depth] >= 'A' && newpos[depth] <= 'Z')
1427         {
1428           /* We can only fail if we're moving down the tree.  */
1429           if (old_has_insn >= 0 && oldpos[old_has_insn] >= newpos[depth])
1430             {
1431               printf ("%s_last_insn = recog_next_insn (insn, %d);\n", 
1432                       indent, newpos[depth] - 'A');
1433             }
1434           else
1435             {
1436               printf ("%stem = recog_next_insn (insn, %d);\n", 
1437                       indent, newpos[depth] - 'A');
1438               printf ("%sif (tem == NULL_RTX)\n", indent);
1439               if (afterward)
1440                 printf ("%s  goto L%d;\n", indent, afterward->number);
1441               else
1442                 printf ("%s  goto ret0;\n", indent);
1443               printf ("%s_last_insn = tem;\n", indent);
1444             }
1445           printf ("%sx%d = PATTERN (_last_insn);\n", indent, depth + 1);
1446         }
1447       else if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1448         printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1449                 indent, depth + 1, depth, newpos[depth] - 'a');
1450       else
1451         printf ("%sx%d = XEXP (x%d, %c);\n",
1452                 indent, depth + 1, depth, newpos[depth]);
1453       ++depth;
1454     }
1455 }
1456 \f
1457 /* Print the enumerator constant for CODE -- the upcase version of
1458    the name.  */
1459
1460 static void
1461 print_code (code)
1462      enum rtx_code code;
1463 {
1464   register const char *p;
1465   for (p = GET_RTX_NAME (code); *p; p++)
1466     putchar (TOUPPER (*p));
1467 }
1468
1469 /* Emit code to cross an afterward link -- change state and branch.  */
1470
1471 static void
1472 write_afterward (start, afterward, indent)
1473      struct decision *start;
1474      struct decision *afterward;
1475      const char *indent;
1476 {
1477   if (!afterward || start->subroutine_number > 0)
1478     printf("%sgoto ret0;\n", indent);
1479   else
1480     {
1481       change_state (start->position, afterward->position, NULL, indent);
1482       printf ("%sgoto L%d;\n", indent, afterward->number);
1483     }
1484 }
1485
1486 /* Emit a switch statement, if possible, for an initial sequence of 
1487    nodes at START.  Return the first node yet untested.  */
1488
1489 static struct decision *
1490 write_switch (start, depth)
1491      struct decision *start;
1492      int depth;
1493 {
1494   struct decision *p = start;
1495   enum decision_type type = p->tests->type;
1496
1497   /* If we have two or more nodes in sequence that test the same one
1498      thing, we may be able to use a switch statement.  */
1499
1500   if (!p->next
1501       || p->tests->next
1502       || p->next->tests->type != type
1503       || p->next->tests->next)
1504     return p;
1505
1506   /* DT_code is special in that we can do interesting things with
1507      known predicates at the same time.  */
1508   if (type == DT_code)
1509     {
1510       char codemap[NUM_RTX_CODE];
1511       struct decision *ret;
1512
1513       memset (codemap, 0, sizeof(codemap));
1514
1515       printf ("  switch (GET_CODE (x%d))\n    {\n", depth);
1516       do 
1517         {
1518           RTX_CODE code = p->tests->u.code;
1519           printf ("    case ");
1520           print_code (code);
1521           printf (":\n      goto L%d;\n", p->success.first->number);
1522           p->success.first->need_label = 1;
1523
1524           codemap[code] = 1;
1525           p = p->next;
1526         }
1527       while (p && p->tests->type == DT_code && !p->tests->next);
1528
1529       /* If P is testing a predicate that we know about and we haven't
1530          seen any of the codes that are valid for the predicate, we can
1531          write a series of "case" statement, one for each possible code.
1532          Since we are already in a switch, these redundant tests are very
1533          cheap and will reduce the number of predicates called.  */
1534
1535       /* Note that while we write out cases for these predicates here,
1536          we don't actually write the test here, as it gets kinda messy.
1537          It is trivial to leave this to later by telling our caller that
1538          we only processed the CODE tests.  */
1539       ret = p;
1540
1541       while (p && p->tests->type == DT_pred
1542              && p->tests->u.pred.index >= 0)
1543         {
1544           const RTX_CODE *c;
1545
1546           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1547             if (codemap[(int) *c] != 0)
1548               goto pred_done;
1549
1550           for (c = &preds[p->tests->u.pred.index].codes[0]; *c ; ++c)
1551             {
1552               printf ("    case ");
1553               print_code (*c);
1554               printf (":\n");
1555               codemap[(int) *c] = 1;
1556             }
1557
1558           printf ("      goto L%d;\n", p->number);
1559           p->need_label = 1;
1560           p = p->next;
1561         }
1562
1563     pred_done:
1564       /* Make the default case skip the predicates we managed to match.  */
1565
1566       printf ("    default:\n");
1567       if (p != ret)
1568         {
1569           if (p)
1570             {
1571               printf ("      goto L%d;\n", p->number);
1572               p->need_label = 1;
1573             }
1574           else
1575             write_afterward (start, start->afterward, "      ");
1576         }
1577       else
1578         printf ("     break;\n");
1579       printf ("   }\n");
1580
1581       return ret;
1582     }
1583   else if (type == DT_mode
1584            || type == DT_veclen
1585            || type == DT_elt_zero_int
1586            || type == DT_elt_one_int
1587            || type == DT_elt_zero_wide)
1588     {
1589       const char *str;
1590
1591       printf ("  switch (");
1592       switch (type)
1593         {
1594         case DT_mode:
1595           str = "GET_MODE (x%d)";
1596           break;
1597         case DT_veclen:
1598           str = "XVECLEN (x%d, 0)";
1599           break;
1600         case DT_elt_zero_int:
1601           str = "XINT (x%d, 0)";
1602           break;
1603         case DT_elt_one_int:
1604           str = "XINT (x%d, 1)";
1605           break;
1606         case DT_elt_zero_wide:
1607           str = "XWINT (x%d, 0)";
1608           break;
1609         default:
1610           abort ();
1611         }
1612       printf (str, depth);
1613       printf (")\n    {\n");
1614
1615       do
1616         {
1617           printf ("    case ");
1618           switch (type)
1619             {
1620             case DT_mode:
1621               printf ("%smode", GET_MODE_NAME (p->tests->u.mode));
1622               break;
1623             case DT_veclen:
1624               printf ("%d", p->tests->u.veclen);
1625               break;
1626             case DT_elt_zero_int:
1627             case DT_elt_one_int:
1628             case DT_elt_zero_wide:
1629               printf (HOST_WIDE_INT_PRINT_DEC, p->tests->u.intval);
1630               break;
1631             default:
1632               abort ();
1633             }
1634           printf (":\n      goto L%d;\n", p->success.first->number);
1635           p->success.first->need_label = 1;
1636
1637           p = p->next;
1638         }
1639       while (p && p->tests->type == type && !p->tests->next);
1640       
1641       printf ("    default:\n      break;\n    }\n");
1642
1643       return p;
1644     }
1645   else
1646     {
1647       /* None of the other tests are ameanable.  */
1648       return p;
1649     }
1650 }
1651
1652 /* Emit code for one test.  */
1653
1654 static void
1655 write_cond (p, depth, subroutine_type)
1656      struct decision_test *p;
1657      int depth;
1658      enum routine_type subroutine_type;
1659 {
1660   switch (p->type)
1661     {
1662     case DT_mode:
1663       printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode));
1664       break;
1665
1666     case DT_code:
1667       printf ("GET_CODE (x%d) == ", depth);
1668       print_code (p->u.code);
1669       break;
1670
1671     case DT_veclen:
1672       printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen);
1673       break;
1674
1675     case DT_elt_zero_int:
1676       printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval);
1677       break;
1678
1679     case DT_elt_one_int:
1680       printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval);
1681       break;
1682
1683     case DT_elt_zero_wide:
1684       printf ("XWINT (x%d, 0) == ", depth);
1685       printf (HOST_WIDE_INT_PRINT_DEC, p->u.intval);
1686       break;
1687
1688     case DT_dup:
1689       printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup);
1690       break;
1691
1692     case DT_pred:
1693       printf ("%s (x%d, %smode)", p->u.pred.name, depth,
1694               GET_MODE_NAME (p->u.pred.mode));
1695       break;
1696
1697     case DT_c_test:
1698       printf ("(%s)", p->u.c_test);
1699       break;
1700
1701     case DT_accept_insn:
1702       switch (subroutine_type)
1703         {
1704         case RECOG:
1705           if (p->u.insn.num_clobbers_to_add == 0)
1706             abort ();
1707           printf ("pnum_clobbers != NULL");
1708           break;
1709
1710         default:
1711           abort ();
1712         }
1713       break;
1714
1715     default:
1716       abort ();
1717     }
1718 }
1719
1720 /* Emit code for one action.  The previous tests have succeeded;
1721    TEST is the last of the chain.  In the normal case we simply
1722    perform a state change.  For the `accept' tests we must do more work.  */
1723
1724 static void
1725 write_action (test, depth, uncond, success, subroutine_type)
1726      struct decision_test *test;
1727      int depth, uncond;
1728      struct decision *success;
1729      enum routine_type subroutine_type;
1730 {
1731   const char *indent;
1732   int want_close = 0;
1733
1734   if (uncond)
1735     indent = "  ";
1736   else if (test->type == DT_accept_op || test->type == DT_accept_insn)
1737     {
1738       fputs ("    {\n", stdout);
1739       indent = "      ";
1740       want_close = 1;
1741     }
1742   else
1743     indent = "    ";
1744
1745   if (test->type == DT_accept_op)
1746     {
1747       printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth);
1748
1749       /* Only allow DT_accept_insn to follow.  */
1750       if (test->next)
1751         {
1752           test = test->next;
1753           if (test->type != DT_accept_insn)
1754             abort ();
1755         }
1756     }
1757
1758   /* Sanity check that we're now at the end of the list of tests.  */
1759   if (test->next)
1760     abort ();
1761
1762   if (test->type == DT_accept_insn)
1763     {
1764       switch (subroutine_type)
1765         {
1766         case RECOG:
1767           if (test->u.insn.num_clobbers_to_add != 0)
1768             printf ("%s*pnum_clobbers = %d;\n",
1769                     indent, test->u.insn.num_clobbers_to_add);
1770           printf ("%sreturn %d;\n", indent, test->u.insn.code_number);
1771           break;
1772
1773         case SPLIT:
1774           printf ("%sreturn gen_split_%d (operands);\n",
1775                   indent, test->u.insn.code_number);
1776           break;
1777
1778         case PEEPHOLE2:
1779           printf ("%stem = gen_peephole2_%d (insn, operands);\n",
1780                   indent, test->u.insn.code_number);
1781           printf ("%sif (tem != 0)\n%s  goto ret1;\n", indent, indent);
1782           break;
1783
1784         default:
1785           abort ();
1786         }
1787     }
1788   else
1789     {
1790       printf("%sgoto L%d;\n", indent, success->number);
1791       success->need_label = 1;
1792     }
1793
1794   if (want_close)
1795     fputs ("    }\n", stdout);
1796 }
1797
1798 /* Return 1 if the test is always true and has no fallthru path.  Return -1
1799    if the test does have a fallthru path, but requires that the condition be
1800    terminated.  Otherwise return 0 for a normal test.  */
1801 /* ??? is_unconditional is a stupid name for a tri-state function.  */
1802
1803 static int
1804 is_unconditional (t, subroutine_type)
1805      struct decision_test *t;
1806      enum routine_type subroutine_type;
1807 {
1808   if (t->type == DT_accept_op)
1809     return 1;
1810
1811   if (t->type == DT_accept_insn)
1812     {
1813       switch (subroutine_type)
1814         {
1815         case RECOG:
1816           return (t->u.insn.num_clobbers_to_add == 0);
1817         case SPLIT:
1818           return 1;
1819         case PEEPHOLE2:
1820           return -1;
1821         default:
1822           abort ();
1823         }
1824     }
1825
1826   return 0;
1827 }
1828
1829 /* Emit code for one node -- the conditional and the accompanying action.
1830    Return true if there is no fallthru path.  */
1831
1832 static int
1833 write_node (p, depth, subroutine_type)
1834      struct decision *p;
1835      int depth;
1836      enum routine_type subroutine_type;
1837 {
1838   struct decision_test *test, *last_test;
1839   int uncond;
1840
1841   last_test = test = p->tests;
1842   uncond = is_unconditional (test, subroutine_type);
1843   if (uncond == 0)
1844     {
1845       printf ("  if (");
1846       write_cond (test, depth, subroutine_type);
1847
1848       while ((test = test->next) != NULL)
1849         {
1850           int uncond2;
1851
1852           last_test = test;
1853           uncond2 = is_unconditional (test, subroutine_type);
1854           if (uncond2 != 0)
1855             break;
1856
1857           printf ("\n      && ");
1858           write_cond (test, depth, subroutine_type);
1859         }
1860
1861       printf (")\n");
1862     }
1863
1864   write_action (last_test, depth, uncond, p->success.first, subroutine_type);
1865
1866   return uncond > 0;
1867 }
1868
1869 /* Emit code for all of the sibling nodes of HEAD.  */
1870
1871 static void
1872 write_tree_1 (head, depth, subroutine_type)
1873      struct decision_head *head;
1874      int depth;
1875      enum routine_type subroutine_type;
1876 {
1877   struct decision *p, *next;
1878   int uncond = 0;
1879
1880   for (p = head->first; p ; p = next)
1881     {
1882       /* The label for the first element was printed in write_tree.  */
1883       if (p != head->first && p->need_label)
1884         OUTPUT_LABEL (" ", p->number);
1885
1886       /* Attempt to write a switch statement for a whole sequence.  */
1887       next = write_switch (p, depth);
1888       if (p != next)
1889         uncond = 0;
1890       else
1891         {
1892           /* Failed -- fall back and write one node.  */
1893           uncond = write_node (p, depth, subroutine_type);
1894           next = p->next;
1895         }
1896     }
1897
1898   /* Finished with this chain.  Close a fallthru path by branching
1899      to the afterward node.  */
1900   if (! uncond)
1901     write_afterward (head->last, head->last->afterward, "  ");
1902 }
1903
1904 /* Write out the decision tree starting at HEAD.  PREVPOS is the
1905    position at the node that branched to this node.  */
1906
1907 static void
1908 write_tree (head, prevpos, type, initial)
1909      struct decision_head *head;
1910      const char *prevpos;
1911      enum routine_type type;
1912      int initial;
1913 {
1914   register struct decision *p = head->first;
1915
1916   putchar ('\n');
1917   if (p->need_label)
1918     OUTPUT_LABEL (" ", p->number);
1919
1920   if (! initial && p->subroutine_number > 0)
1921     {
1922       static const char * const name_prefix[] = {
1923           "recog", "split", "peephole2"
1924       };
1925
1926       static const char * const call_suffix[] = {
1927           ", pnum_clobbers", "", ", _plast_insn"
1928       };
1929
1930       /* This node has been broken out into a separate subroutine.
1931          Call it, test the result, and branch accordingly.  */
1932
1933       if (p->afterward)
1934         {
1935           printf ("  tem = %s_%d (x0, insn%s);\n",
1936                   name_prefix[type], p->subroutine_number, call_suffix[type]);
1937           if (IS_SPLIT (type))
1938             printf ("  if (tem != 0)\n    return tem;\n");
1939           else
1940             printf ("  if (tem >= 0)\n    return tem;\n");
1941
1942           change_state (p->position, p->afterward->position, NULL, "  ");
1943           printf ("  goto L%d;\n", p->afterward->number);
1944         }
1945       else
1946         {
1947           printf ("  return %s_%d (x0, insn%s);\n",
1948                   name_prefix[type], p->subroutine_number, call_suffix[type]);
1949         }
1950     }
1951   else
1952     {
1953       int depth = strlen (p->position);
1954
1955       change_state (prevpos, p->position, head->last->afterward, "  ");
1956       write_tree_1 (head, depth, type);
1957
1958       for (p = head->first; p; p = p->next)
1959         if (p->success.first)
1960           write_tree (&p->success, p->position, type, 0);
1961     }
1962 }
1963
1964 /* Write out a subroutine of type TYPE to do comparisons starting at
1965    node TREE.  */
1966
1967 static void
1968 write_subroutine (head, type)
1969      struct decision_head *head;
1970      enum routine_type type;
1971 {
1972   static const char * const proto_pattern[] = {
1973     "%sint recog%s PROTO ((rtx, rtx, int *));\n",
1974     "%srtx split%s PROTO ((rtx, rtx));\n",
1975     "%srtx peephole2%s PROTO ((rtx, rtx, rtx *));\n"
1976   };
1977
1978   static const char * const decl_pattern[] = {
1979 "%sint\n\
1980 recog%s (x0, insn, pnum_clobbers)\n\
1981      register rtx x0;\n\
1982      rtx insn ATTRIBUTE_UNUSED;\n\
1983      int *pnum_clobbers ATTRIBUTE_UNUSED;\n",
1984
1985 "%srtx\n\
1986 split%s (x0, insn)\n\
1987      register rtx x0;\n\
1988      rtx insn ATTRIBUTE_UNUSED;\n",
1989
1990 "%srtx\n\
1991 peephole2%s (x0, insn, _plast_insn)\n\
1992      register rtx x0;\n\
1993      rtx insn ATTRIBUTE_UNUSED;\n\
1994      rtx *_plast_insn ATTRIBUTE_UNUSED;\n"
1995   };
1996      
1997   int subfunction = head->first ? head->first->subroutine_number : 0;
1998   const char *s_or_e;
1999   char extension[32];
2000   int i;
2001   
2002   s_or_e = subfunction ? "static " : "";
2003
2004   if (subfunction)
2005     sprintf (extension, "_%d", subfunction);
2006   else if (type == RECOG)
2007     extension[0] = '\0';
2008   else
2009     strcpy (extension, "_insns");
2010
2011   printf (proto_pattern[type], s_or_e, extension);
2012   printf (decl_pattern[type], s_or_e, extension);
2013
2014   printf ("{\n  register rtx * const operands = &recog_data.operand[0];\n");
2015   for (i = 1; i <= max_depth; i++)
2016     printf ("  register rtx x%d ATTRIBUTE_UNUSED;\n", i);
2017
2018   if (type == PEEPHOLE2)
2019     printf ("  register rtx _last_insn = insn;\n");
2020   printf ("  %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int");
2021
2022   if (head->first)
2023     write_tree (head, "", type, 1);
2024   else
2025     printf ("  goto ret0;\n");
2026
2027   if (type == PEEPHOLE2)
2028     printf (" ret1:\n  *_plast_insn = _last_insn;\n  return tem;\n");
2029   printf (" ret0:\n  return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1);
2030 }
2031
2032 /* In break_out_subroutines, we discovered the boundaries for the
2033    subroutines, but did not write them out.  Do so now.  */
2034
2035 static void
2036 write_subroutines (head, type)
2037      struct decision_head *head;
2038      enum routine_type type;
2039 {
2040   struct decision *p;
2041
2042   for (p = head->first; p ; p = p->next)
2043     if (p->success.first)
2044       write_subroutines (&p->success, type);
2045
2046   if (head->first->subroutine_number > 0)
2047     write_subroutine (head, type);
2048 }
2049
2050 /* Begin the output file.  */
2051
2052 static void
2053 write_header ()
2054 {
2055   puts ("\
2056 /* Generated automatically by the program `genrecog' from the target\n\
2057    machine description file.  */\n\
2058 \n\
2059 #include \"config.h\"\n\
2060 #include \"system.h\"\n\
2061 #include \"rtl.h\"\n\
2062 #include \"tm_p.h\"\n\
2063 #include \"function.h\"\n\
2064 #include \"insn-config.h\"\n\
2065 #include \"recog.h\"\n\
2066 #include \"real.h\"\n\
2067 #include \"output.h\"\n\
2068 #include \"flags.h\"\n\
2069 #include \"hard-reg-set.h\"\n\
2070 #include \"resource.h\"\n\
2071 \n");
2072
2073   puts ("\n\
2074 /* `recog' contains a decision tree that recognizes whether the rtx\n\
2075    X0 is a valid instruction.\n\
2076 \n\
2077    recog returns -1 if the rtx is not valid.  If the rtx is valid, recog\n\
2078    returns a nonnegative number which is the insn code number for the\n\
2079    pattern that matched.  This is the same as the order in the machine\n\
2080    description of the entry that matched.  This number can be used as an\n\
2081    index into `insn_data' and other tables.\n\
2082 \n\
2083    The third argument to recog is an optional pointer to an int.  If\n\
2084    present, recog will accept a pattern if it matches except for missing\n\
2085    CLOBBER expressions at the end.  In that case, the value pointed to by\n\
2086    the optional pointer will be set to the number of CLOBBERs that need\n\
2087    to be added (it should be initialized to zero by the caller).  If it\n\
2088    is set nonzero, the caller should allocate a PARALLEL of the\n\
2089    appropriate size, copy the initial entries, and call add_clobbers\n\
2090    (found in insn-emit.c) to fill in the CLOBBERs.\n\
2091 ");
2092
2093   puts ("\n\
2094    The function split_insns returns 0 if the rtl could not\n\
2095    be split or the split rtl in a SEQUENCE if it can be.\n\
2096 \n\
2097    The function peephole2_insns returns 0 if the rtl could not\n\
2098    be matched. If there was a match, the new rtl is returned in a SEQUENCE,\n\
2099    and LAST_INSN will point to the last recognized insn in the old sequence.\n\
2100 */\n\n");
2101 }
2102
2103 \f
2104 /* Construct and return a sequence of decisions
2105    that will recognize INSN.
2106
2107    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
2108
2109 static struct decision_head
2110 make_insn_sequence (insn, type)
2111      rtx insn;
2112      enum routine_type type;
2113 {
2114   rtx x;
2115   const char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
2116   struct decision *last;
2117   struct decision_test *test, **place;
2118   struct decision_head head;
2119
2120   record_insn_name (next_insn_code, (type == RECOG ? XSTR (insn, 0) : NULL));
2121
2122   if (type == PEEPHOLE2)
2123     {
2124       int i, j;
2125
2126       /* peephole2 gets special treatment:
2127          - X always gets an outer parallel even if it's only one entry
2128          - we remove all traces of outer-level match_scratch and match_dup
2129            expressions here.  */
2130       x = rtx_alloc (PARALLEL);
2131       PUT_MODE (x, VOIDmode);
2132       XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0));
2133       for (i = j = 0; i < XVECLEN (insn, 0); i++)
2134         {
2135           rtx tmp = XVECEXP (insn, 0, i);
2136           if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP)
2137             {
2138               XVECEXP (x, 0, j) = tmp;
2139               j++;
2140             }
2141         }
2142       XVECLEN (x, 0) = j;
2143     }
2144   else if (XVECLEN (insn, type == RECOG) == 1)
2145     x = XVECEXP (insn, type == RECOG, 0);
2146   else
2147     {
2148       x = rtx_alloc (PARALLEL);
2149       XVEC (x, 0) = XVEC (insn, type == RECOG);
2150       PUT_MODE (x, VOIDmode);
2151     }
2152
2153   validate_pattern (x, 0);
2154
2155   memset(&head, 0, sizeof(head));
2156   last = add_to_sequence (x, &head, "", type, 1);
2157
2158   /* Find the end of the test chain on the last node.  */
2159   for (test = last->tests; test->next; test = test->next)
2160     continue;
2161   place = &test->next;
2162
2163   if (c_test[0])
2164     {
2165       /* Need a new node if we have another test to add.  */
2166       if (test->type == DT_accept_op)
2167         {
2168           last = new_decision ("", &last->success);
2169           place = &last->tests;
2170         }
2171       test = new_decision_test (DT_c_test, &place);
2172       test->u.c_test = c_test;
2173     }
2174
2175   test = new_decision_test (DT_accept_insn, &place);
2176   test->u.insn.code_number = next_insn_code;
2177   test->u.insn.lineno = pattern_lineno;
2178   test->u.insn.num_clobbers_to_add = 0;
2179
2180   switch (type)
2181     {
2182     case RECOG:
2183       /* If this is an DEFINE_INSN and X is a PARALLEL, see if it ends
2184          with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.
2185          If so, set up to recognize the pattern without these CLOBBERs.  */
2186
2187       if (GET_CODE (x) == PARALLEL)
2188         {
2189           int i;
2190
2191           /* Find the last non-clobber in the parallel.  */
2192           for (i = XVECLEN (x, 0); i > 0; i--)
2193             {
2194               rtx y = XVECEXP (x, 0, i - 1);
2195               if (GET_CODE (y) != CLOBBER
2196                   || (GET_CODE (XEXP (y, 0)) != REG
2197                       && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH))
2198                 break;
2199             }
2200
2201           if (i != XVECLEN (x, 0))
2202             {
2203               rtx new;
2204               struct decision_head clobber_head;
2205
2206               /* Build a similar insn without the clobbers.  */
2207               if (i == 1)
2208                 new = XVECEXP (x, 0, 0);
2209               else
2210                 {
2211                   int j;
2212
2213                   new = rtx_alloc (PARALLEL);
2214                   XVEC (new, 0) = rtvec_alloc (i);
2215                   for (j = i - 1; j >= 0; j--)
2216                     XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
2217                 }
2218
2219               /* Recognize it.  */
2220               memset (&clobber_head, 0, sizeof(clobber_head));
2221               last = add_to_sequence (new, &clobber_head, "", type, 1);
2222
2223               /* Find the end of the test chain on the last node.  */
2224               for (test = last->tests; test->next; test = test->next)
2225                 continue;
2226
2227               /* We definitely have a new test to add -- create a new
2228                  node if needed.  */
2229               place = &test->next;
2230               if (test->type == DT_accept_op)
2231                 {
2232                   last = new_decision ("", &last->success);
2233                   place = &last->tests;
2234                 }
2235
2236               if (c_test[0])
2237                 {
2238                   test = new_decision_test (DT_c_test, &place);
2239                   test->u.c_test = c_test;
2240                 }
2241
2242               test = new_decision_test (DT_accept_insn, &place);
2243               test->u.insn.code_number = next_insn_code;
2244               test->u.insn.lineno = pattern_lineno;
2245               test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i;
2246
2247               merge_trees (&head, &clobber_head);
2248             }
2249         }
2250       break;
2251
2252     case SPLIT:
2253       /* Define the subroutine we will call below and emit in genemit.  */
2254       printf ("extern rtx gen_split_%d PROTO ((rtx *));\n", next_insn_code);
2255       break;
2256
2257     case PEEPHOLE2:
2258       /* Define the subroutine we will call below and emit in genemit.  */
2259       printf ("extern rtx gen_peephole2_%d PROTO ((rtx, rtx *));\n",
2260               next_insn_code);
2261       break;
2262     }
2263   next_insn_code++;
2264
2265   return head;
2266 }
2267
2268 static void
2269 process_tree (head, subroutine_type)
2270      struct decision_head *head;
2271      enum routine_type subroutine_type;
2272 {
2273   if (head->first != NULL)
2274     {
2275       factor_tests (head);
2276       simplify_tests (head);
2277
2278       next_subroutine_number = 0;
2279       break_out_subroutines (head, 1);
2280       find_afterward (head, NULL);
2281
2282       write_subroutines (head, subroutine_type);
2283     }
2284   write_subroutine (head, subroutine_type);
2285 }
2286 \f
2287 int
2288 main (argc, argv)
2289      int argc;
2290      char **argv;
2291 {
2292   rtx desc;
2293   struct decision_head recog_tree, split_tree, peephole2_tree, h;
2294   FILE *infile;
2295   register int c;
2296
2297   progname = "genrecog";
2298   obstack_init (rtl_obstack);
2299
2300   memset (&recog_tree, 0, sizeof recog_tree);
2301   memset (&split_tree, 0, sizeof split_tree);
2302   memset (&peephole2_tree, 0, sizeof peephole2_tree);
2303
2304   if (argc <= 1)
2305     fatal ("No input file name.");
2306
2307   infile = fopen (argv[1], "r");
2308   if (infile == 0)
2309     {
2310       perror (argv[1]);
2311       return FATAL_EXIT_CODE;
2312     }
2313   read_rtx_filename = argv[1];
2314
2315   next_insn_code = 0;
2316   next_index = 0;
2317
2318   write_header ();
2319
2320   /* Read the machine description.  */
2321
2322   while (1)
2323     {
2324       c = read_skip_spaces (infile);
2325       if (c == EOF)
2326         break;
2327       ungetc (c, infile);
2328       pattern_lineno = read_rtx_lineno;
2329
2330       desc = read_rtx (infile);
2331       if (GET_CODE (desc) == DEFINE_INSN)
2332         {
2333           h = make_insn_sequence (desc, RECOG);
2334           merge_trees (&recog_tree, &h);
2335         }
2336       else if (GET_CODE (desc) == DEFINE_SPLIT)
2337         {
2338           h = make_insn_sequence (desc, SPLIT);
2339           merge_trees (&split_tree, &h);
2340         }
2341       else if (GET_CODE (desc) == DEFINE_PEEPHOLE2)
2342         {
2343           h = make_insn_sequence (desc, PEEPHOLE2);
2344           merge_trees (&peephole2_tree, &h);
2345         }
2346         
2347       if (GET_CODE (desc) == DEFINE_PEEPHOLE
2348           || GET_CODE (desc) == DEFINE_EXPAND)
2349         next_insn_code++;
2350       next_index++;
2351     }
2352
2353   if (error_count)
2354     return FATAL_EXIT_CODE;
2355
2356   puts ("\n\n");
2357
2358   process_tree (&recog_tree, RECOG);
2359   process_tree (&split_tree, SPLIT);
2360   process_tree (&peephole2_tree, PEEPHOLE2);
2361
2362   fflush (stdout);
2363   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
2364 }
2365 \f
2366 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
2367 const char *
2368 get_insn_name (code)
2369      int code;
2370 {
2371   if (code < insn_name_ptr_size)
2372     return insn_name_ptr[code];
2373   else
2374     return NULL;
2375 }
2376
2377 static void
2378 record_insn_name (code, name)
2379      int code;
2380      const char *name;
2381 {
2382   static const char *last_real_name = "insn";
2383   static int last_real_code = 0;
2384   char *new;
2385
2386   if (insn_name_ptr_size <= code)
2387     {
2388       int new_size;
2389       new_size = (insn_name_ptr_size ? insn_name_ptr_size * 2 : 512);
2390       insn_name_ptr =
2391         (char **) xrealloc (insn_name_ptr, sizeof(char *) * new_size);
2392       memset (insn_name_ptr + insn_name_ptr_size, 0, 
2393               sizeof(char *) * (new_size - insn_name_ptr_size));
2394       insn_name_ptr_size = new_size;
2395     }
2396
2397   if (!name || name[0] == '\0')
2398     {
2399       new = xmalloc (strlen (last_real_name) + 10);
2400       sprintf (new, "%s+%d", last_real_name, code - last_real_code);
2401     }
2402   else
2403     {
2404       last_real_name = new = xstrdup (name);
2405       last_real_code = code;
2406     }
2407   
2408   insn_name_ptr[code] = new;
2409 }  
2410 \f
2411 char *
2412 xstrdup (input)
2413   const char *input;
2414 {
2415   register size_t len = strlen (input) + 1;
2416   register char *output = xmalloc (len);
2417   memcpy (output, input, len);
2418   return output;
2419 }
2420
2421 PTR
2422 xrealloc (old, size)
2423   PTR old;
2424   size_t size;
2425 {
2426   register PTR ptr;
2427   if (old)
2428     ptr = (PTR) realloc (old, size);
2429   else
2430     ptr = (PTR) malloc (size);
2431   if (!ptr)
2432     fatal ("virtual memory exhausted");
2433   return ptr;
2434 }
2435
2436 PTR
2437 xmalloc (size)
2438   size_t size;
2439 {
2440   register PTR val = (PTR) malloc (size);
2441
2442   if (val == 0)
2443     fatal ("virtual memory exhausted");
2444   return val;
2445 }
2446 \f
2447 static void
2448 debug_decision_2 (test)
2449      struct decision_test *test;
2450 {
2451   switch (test->type)
2452     {
2453     case DT_mode:
2454       fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode));
2455       break;
2456     case DT_code:
2457       fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code));
2458       break;
2459     case DT_veclen:
2460       fprintf (stderr, "veclen=%d", test->u.veclen);
2461       break;
2462     case DT_elt_zero_int:
2463       fprintf (stderr, "elt0_i=%d", (int) test->u.intval);
2464       break;
2465     case DT_elt_one_int:
2466       fprintf (stderr, "elt1_i=%d", (int) test->u.intval);
2467       break;
2468     case DT_elt_zero_wide:
2469       fprintf (stderr, "elt0_w=");
2470       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, test->u.intval);
2471       break;
2472     case DT_dup:
2473       fprintf (stderr, "dup=%d", test->u.dup);
2474       break;
2475     case DT_pred:
2476       fprintf (stderr, "pred=(%s,%s)",
2477                test->u.pred.name, GET_MODE_NAME(test->u.pred.mode));
2478       break;
2479     case DT_c_test:
2480       {
2481         char sub[16+4];
2482         strncpy (sub, test->u.c_test, sizeof(sub));
2483         memcpy (sub+16, "...", 4);
2484         fprintf (stderr, "c_test=\"%s\"", sub);
2485       }
2486       break;
2487     case DT_accept_op:
2488       fprintf (stderr, "A_op=%d", test->u.opno);
2489       break;
2490     case DT_accept_insn:
2491       fprintf (stderr, "A_insn=(%d,%d)", 
2492                test->u.insn.code_number, test->u.insn.num_clobbers_to_add);
2493       break;
2494
2495     default:
2496       abort ();
2497     }
2498 }
2499
2500 static void
2501 debug_decision_1 (d, indent)
2502      struct decision *d;
2503      int indent;
2504 {
2505   int i;
2506   struct decision_test *test;
2507
2508   if (d == NULL)
2509     {
2510       for (i = 0; i < indent; ++i)
2511         putc (' ', stderr);
2512       fputs ("(nil)\n", stderr);
2513       return;
2514     }
2515
2516   for (i = 0; i < indent; ++i)
2517     putc (' ', stderr);
2518
2519   putc ('{', stderr);
2520   test = d->tests;
2521   if (test)
2522     {
2523       debug_decision_2 (test);
2524       while ((test = test->next) != NULL)
2525         {
2526           fputs (" + ", stderr);
2527           debug_decision_2 (test);
2528         }
2529     }
2530   fprintf (stderr, "} %d\n", d->number);
2531 }
2532
2533 static void
2534 debug_decision_0 (d, indent, maxdepth)
2535      struct decision *d;
2536      int indent, maxdepth;
2537 {
2538   struct decision *n;
2539   int i;
2540
2541   if (maxdepth < 0)
2542     return;
2543   if (d == NULL)
2544     {
2545       for (i = 0; i < indent; ++i)
2546         putc (' ', stderr);
2547       fputs ("(nil)\n", stderr);
2548       return;
2549     }
2550
2551   debug_decision_1 (d, indent);
2552   for (n = d->success.first; n ; n = n->next)
2553     debug_decision_0 (n, indent + 2, maxdepth - 1);
2554 }
2555
2556 void
2557 debug_decision (d)
2558      struct decision *d;
2559 {
2560   debug_decision_0 (d, 0, 1000000);
2561 }
2562
2563 void
2564 debug_decision_list (d)
2565      struct decision *d;
2566 {
2567   while (d)
2568     {
2569       debug_decision_0 (d, 0, 0);
2570       d = d->next;
2571     }
2572 }