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