OSDN Git Service

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