OSDN Git Service

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