OSDN Git Service

Sat Aug 15 20:22:33 1998 H.J. Lu (hjl@gnu.org)
[pf3gnuchains/gcc-fork.git] / gcc / genrecog.c
1 /* Generate code from machine description to recognize rtl as insns.
2    Copyright (C) 1987, 88, 92, 93, 94, 95, 97, 98 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
23    a function called `recog' plus its subroutines.
24    These functions contain a decision tree
25    that recognizes whether an rtx, the argument given to recog,
26    is a valid instruction.
27
28    recog returns -1 if the rtx is not valid.
29    If the rtx is valid, recog returns a nonnegative number
30    which is the insn code number for the pattern that matched.
31    This is the same as the order in the machine description of the
32    entry that matched.  This number can be used as an index into various
33    insn_* tables, such as insn_template, insn_outfun, and insn_n_operands
34    (found in insn-output.c).
35
36    The third argument to recog is an optional pointer to an int.
37    If present, recog will accept a pattern if it matches except for
38    missing CLOBBER expressions at the end.  In that case, the value
39    pointed to by the optional pointer will be set to the number of
40    CLOBBERs that need to be added (it should be initialized to zero by
41    the caller).  If it is set nonzero, the caller should allocate a
42    PARALLEL of the appropriate size, copy the initial entries, and call
43    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
44
45    This program also generates the function `split_insns',
46    which returns 0 if the rtl could not be split, or
47    it returns the split rtl in a SEQUENCE.  */
48
49 #include "hconfig.h"
50 #ifdef __STDC__
51 #include <stdarg.h>
52 #else
53 #include <varargs.h>
54 #endif
55 #include "system.h"
56 #include "rtl.h"
57 #include "obstack.h"
58
59 static struct obstack obstack;
60 struct obstack *rtl_obstack = &obstack;
61
62 #define obstack_chunk_alloc xmalloc
63 #define obstack_chunk_free free
64
65 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
66 char **insn_name_ptr = 0;
67
68 /* Data structure for a listhead of decision trees.  The alternatives
69    to a node are kept in a doublely-linked list so we can easily add nodes
70    to the proper place when merging.  */
71
72 struct decision_head { struct decision *first, *last; };
73
74 /* Data structure for decision tree for recognizing
75    legitimate instructions.  */
76
77 struct decision
78 {
79   int number;                   /* Node number, used for labels */
80   char *position;               /* String denoting position in pattern */
81   RTX_CODE code;                /* Code to test for or UNKNOWN to suppress */
82   char ignore_code;             /* If non-zero, need not test code */
83   char ignore_mode;             /* If non-zero, need not test mode */
84   int veclen;                   /* Length of vector, if nonzero */
85   enum machine_mode mode;       /* Machine mode of node */
86   char enforce_mode;            /* If non-zero, test `mode' */
87   char retest_code, retest_mode; /* See write_tree_1 */
88   int test_elt_zero_int;        /* Nonzero if should test XINT (rtl, 0) */
89   int elt_zero_int;             /* Required value for XINT (rtl, 0) */
90   int test_elt_one_int;         /* Nonzero if should test XINT (rtl, 1) */
91   int elt_one_int;              /* Required value for XINT (rtl, 1) */
92   int test_elt_zero_wide;       /* Nonzero if should test XWINT (rtl, 0) */
93   HOST_WIDE_INT elt_zero_wide;  /* Required value for XWINT (rtl, 0) */
94   char *tests;                  /* If nonzero predicate to call */
95   int pred;                     /* `preds' index of predicate or -1 */
96   char *c_test;                 /* Additional test to perform */
97   struct decision_head success; /* Nodes to test on success */
98   int insn_code_number;         /* Insn number matched, if success */
99   int num_clobbers_to_add;      /* Number of CLOBBERs to be added to pattern */
100   struct decision *next;        /* Node to test on failure */
101   struct decision *prev;        /* Node whose failure tests us */
102   struct decision *afterward;   /* Node to test on success, but failure of
103                                    successor nodes */
104   int opno;                     /* Operand number, if >= 0 */
105   int dupno;                    /* Number of operand to compare against */
106   int label_needed;             /* Nonzero if label needed when writing tree */
107   int subroutine_number;        /* Number of subroutine this node starts */
108 };
109
110 #define SUBROUTINE_THRESHOLD 50
111
112 static int next_subroutine_number;
113
114 /* We can write two types of subroutines: One for insn recognition and
115    one to split insns.  This defines which type is being written.  */
116
117 enum routine_type {RECOG, SPLIT};
118
119 /* Next available node number for tree nodes.  */
120
121 static int next_number;
122
123 /* Next number to use as an insn_code.  */
124
125 static int next_insn_code;
126
127 /* Similar, but counts all expressions in the MD file; used for
128    error messages.  */
129
130 static int next_index;
131
132 /* Record the highest depth we ever have so we know how many variables to
133    allocate in each subroutine we make.  */
134
135 static int max_depth;
136 \f
137 /* This table contains a list of the rtl codes that can possibly match a
138    predicate defined in recog.c.  The function `not_both_true' uses it to
139    deduce that there are no expressions that can be matches by certain pairs
140    of tree nodes.  Also, if a predicate can match only one code, we can
141    hardwire that code into the node testing the predicate.  */
142
143 static struct pred_table
144 {
145   char *name;
146   RTX_CODE codes[NUM_RTX_CODE];
147 } preds[]
148   = {{"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
149                           LABEL_REF, SUBREG, REG, MEM}},
150 #ifdef PREDICATE_CODES
151      PREDICATE_CODES
152 #endif
153      {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
154                           LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
155      {"register_operand", {SUBREG, REG}},
156      {"scratch_operand", {SCRATCH, REG}},
157      {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
158                             LABEL_REF}},
159      {"const_int_operand", {CONST_INT}},
160      {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
161      {"nonimmediate_operand", {SUBREG, REG, MEM}},
162      {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
163                             LABEL_REF, SUBREG, REG}},
164      {"push_operand", {MEM}},
165      {"memory_operand", {SUBREG, MEM}},
166      {"indirect_operand", {SUBREG, MEM}},
167      {"comparison_operator", {EQ, NE, LE, LT, GE, GT, LEU, LTU, GEU, GTU}},
168      {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
169                                    LABEL_REF, SUBREG, REG, MEM}}};
170
171 #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
172
173 static struct decision_head make_insn_sequence PROTO((rtx, enum routine_type));
174 static struct decision *add_to_sequence PROTO((rtx, struct decision_head *,
175                                                char *));
176 static int not_both_true        PROTO((struct decision *, struct decision *,
177                                        int));
178 static int position_merit       PROTO((struct decision *, enum machine_mode,
179                                        enum rtx_code));
180 static struct decision_head merge_trees PROTO((struct decision_head,
181                                                struct decision_head));
182 static int break_out_subroutines PROTO((struct decision_head,
183                                         enum routine_type, int));
184 static void write_subroutine    PROTO((struct decision *, enum routine_type));
185 static void write_tree_1        PROTO((struct decision *, char *,
186                                        struct decision *, enum routine_type));
187 static void print_code          PROTO((enum rtx_code));
188 static int same_codes           PROTO((struct decision *, enum rtx_code));
189 static void clear_codes         PROTO((struct decision *));
190 static int same_modes           PROTO((struct decision *, enum machine_mode));
191 static void clear_modes         PROTO((struct decision *));
192 static void write_tree          PROTO((struct decision *, char *,
193                                        struct decision *, int,
194                                        enum routine_type));
195 static void change_state        PROTO((char *, char *, int));
196 static char *copystr            PROTO((char *));
197 static void mybzero             PROTO((char *, unsigned));
198 static void mybcopy             PROTO((char *, char *, unsigned));
199 static void fatal               PVPROTO((char *, ...)) ATTRIBUTE_PRINTF_1;
200 char *xrealloc                  PROTO((char *, unsigned));
201 char *xmalloc                   PROTO((unsigned));
202 void fancy_abort                PROTO((void));
203 \f
204 /* Construct and return a sequence of decisions
205    that will recognize INSN.
206
207    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
208
209 static struct decision_head
210 make_insn_sequence (insn, type)
211      rtx insn;
212      enum routine_type type;
213 {
214   rtx x;
215   char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
216   struct decision *last;
217   struct decision_head head;
218
219   if (XVECLEN (insn, type == RECOG) == 1)
220     x = XVECEXP (insn, type == RECOG, 0);
221   else
222     {
223       x = rtx_alloc (PARALLEL);
224       XVEC (x, 0) = XVEC (insn, type == RECOG);
225       PUT_MODE (x, VOIDmode);
226     }
227
228   last = add_to_sequence (x, &head, "");
229
230   if (c_test[0])
231     last->c_test = c_test;
232   last->insn_code_number = next_insn_code;
233   last->num_clobbers_to_add = 0;
234
235   /* If this is not a DEFINE_SPLIT and X is a PARALLEL, see if it ends with a
236      group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.  If so, set up
237      to recognize the pattern without these CLOBBERs.  */
238
239   if (type == RECOG && GET_CODE (x) == PARALLEL)
240     {
241       int i;
242
243       for (i = XVECLEN (x, 0); i > 0; i--)
244         if (GET_CODE (XVECEXP (x, 0, i - 1)) != CLOBBER
245             || (GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != REG
246                 && GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != MATCH_SCRATCH))
247           break;
248
249       if (i != XVECLEN (x, 0))
250         {
251           rtx new;
252           struct decision_head clobber_head;
253
254           if (i == 1)
255             new = XVECEXP (x, 0, 0);
256           else
257             {
258               int j;
259
260               new = rtx_alloc (PARALLEL);
261               XVEC (new, 0) = rtvec_alloc (i);
262               for (j = i - 1; j >= 0; j--)
263                 XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
264             }
265
266           last = add_to_sequence (new, &clobber_head, "");
267
268           if (c_test[0])
269             last->c_test = c_test;
270           last->insn_code_number = next_insn_code;
271           last->num_clobbers_to_add = XVECLEN (x, 0) - i;
272
273           head = merge_trees (head, clobber_head);
274         }
275     }
276
277   next_insn_code++;
278
279   if (type == SPLIT)
280     /* Define the subroutine we will call below and emit in genemit.  */
281     printf ("extern rtx gen_split_%d ();\n", last->insn_code_number);
282
283   return head;
284 }
285 \f
286 /* Create a chain of nodes to verify that an rtl expression matches
287    PATTERN.
288
289    LAST is a pointer to the listhead in the previous node in the chain (or
290    in the calling function, for the first node).
291
292    POSITION is the string representing the current position in the insn.
293
294    A pointer to the final node in the chain is returned.  */
295
296 static struct decision *
297 add_to_sequence (pattern, last, position)
298      rtx pattern;
299      struct decision_head *last;
300      char *position;
301 {
302   register RTX_CODE code;
303   register struct decision *new
304     = (struct decision *) xmalloc (sizeof (struct decision));
305   struct decision *this;
306   char *newpos;
307   register char *fmt;
308   register size_t i;
309   int depth = strlen (position);
310   int len;
311
312   if (depth > max_depth)
313     max_depth = depth;
314
315   new->number = next_number++;
316   new->position = copystr (position);
317   new->ignore_code = 0;
318   new->ignore_mode = 0;
319   new->enforce_mode = 1;
320   new->retest_code = new->retest_mode = 0;
321   new->veclen = 0;
322   new->test_elt_zero_int = 0;
323   new->test_elt_one_int = 0;
324   new->test_elt_zero_wide = 0;
325   new->elt_zero_int = 0;
326   new->elt_one_int = 0;
327   new->elt_zero_wide = 0;
328   new->tests = 0;
329   new->pred = -1;
330   new->c_test = 0;
331   new->success.first = new->success.last = 0;
332   new->insn_code_number = -1;
333   new->num_clobbers_to_add = 0;
334   new->next = 0;
335   new->prev = 0;
336   new->afterward = 0;
337   new->opno = -1;
338   new->dupno = -1;
339   new->label_needed = 0;
340   new->subroutine_number = 0;
341
342   this = new;
343
344   last->first = last->last = new;
345
346   newpos = (char *) alloca (depth + 2);
347   strcpy (newpos, position);
348   newpos[depth + 1] = 0;
349
350  restart:
351
352   new->mode = GET_MODE (pattern);
353   new->code = code = GET_CODE (pattern);
354
355   switch (code)
356     {
357     case MATCH_OPERAND:
358     case MATCH_SCRATCH:
359     case MATCH_OPERATOR:
360     case MATCH_PARALLEL:
361     case MATCH_INSN2:
362       new->opno = XINT (pattern, 0);
363       new->code = (code == MATCH_PARALLEL ? PARALLEL : UNKNOWN);
364       new->enforce_mode = 0;
365
366       if (code == MATCH_SCRATCH)
367         new->tests = "scratch_operand";
368       else
369         new->tests = XSTR (pattern, 1);
370
371       if (*new->tests == 0)
372         new->tests = 0;
373
374       /* See if we know about this predicate and save its number.  If we do,
375          and it only accepts one code, note that fact.  The predicate
376          `const_int_operand' only tests for a CONST_INT, so if we do so we
377          can avoid calling it at all.
378
379          Finally, if we know that the predicate does not allow CONST_INT, we
380          know that the only way the predicate can match is if the modes match
381          (here we use the kludge of relying on the fact that "address_operand"
382          accepts CONST_INT; otherwise, it would have to be a special case),
383          so we can test the mode (but we need not).  This fact should
384          considerably simplify the generated code.  */
385
386       if (new->tests)
387         {
388           for (i = 0; i < NUM_KNOWN_PREDS; i++)
389             if (! strcmp (preds[i].name, new->tests))
390               {
391                 int j;
392                 int allows_const_int = 0;
393
394                 new->pred = i;
395
396                 if (preds[i].codes[1] == 0 && new->code == UNKNOWN)
397                   {
398                     new->code = preds[i].codes[0];
399                     if (! strcmp ("const_int_operand", new->tests))
400                       new->tests = 0, new->pred = -1;
401                   }
402
403                 for (j = 0; j < NUM_RTX_CODE && preds[i].codes[j] != 0; j++)
404                   if (preds[i].codes[j] == CONST_INT)
405                     allows_const_int = 1;
406
407                 if (! allows_const_int)
408                   new->enforce_mode = new->ignore_mode= 1;
409
410                 break;
411               }
412
413 #ifdef PREDICATE_CODES
414           /* If the port has a list of the predicates it uses but omits
415              one, warn.  */
416           if (i == NUM_KNOWN_PREDS)
417             fprintf (stderr, "Warning: `%s' not in PREDICATE_CODES\n",
418                      new->tests);
419 #endif
420         }
421
422       if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
423         {
424           for (i = 0; i < XVECLEN (pattern, 2); i++)
425             {
426               newpos[depth] = i + (code == MATCH_OPERATOR ? '0': 'a');
427               new = add_to_sequence (XVECEXP (pattern, 2, i),
428                                      &new->success, newpos);
429             }
430         }
431
432       return new;
433
434     case MATCH_OP_DUP:
435       new->opno = XINT (pattern, 0);
436       new->dupno = XINT (pattern, 0);
437       new->code = UNKNOWN;
438       new->tests = 0;
439       for (i = 0; i < XVECLEN (pattern, 1); i++)
440         {
441           newpos[depth] = i + '0';
442           new = add_to_sequence (XVECEXP (pattern, 1, i),
443                                  &new->success, newpos);
444         }
445       return new;
446
447     case MATCH_DUP:
448     case MATCH_PAR_DUP:
449       new->dupno = XINT (pattern, 0);
450       new->code = UNKNOWN;
451       new->enforce_mode = 0;
452       return new;
453
454     case ADDRESS:
455       pattern = XEXP (pattern, 0);
456       goto restart;
457
458     case SET:
459       /* The operands of a SET must have the same mode unless one is VOIDmode.  */
460       if (GET_MODE (SET_SRC (pattern)) != VOIDmode
461           && GET_MODE (SET_DEST (pattern)) != VOIDmode
462           && GET_MODE (SET_SRC (pattern)) != GET_MODE (SET_DEST (pattern))
463           /* The mode of an ADDRESS_OPERAND is the mode of the memory reference,
464              not the mode of the address.  */
465           && ! (GET_CODE (SET_SRC (pattern)) == MATCH_OPERAND
466                 && ! strcmp (XSTR (SET_SRC (pattern), 1), "address_operand")))
467         {
468           print_rtl (stderr, pattern);
469           fputc ('\n', stderr);
470           fatal ("mode mismatch in SET");
471         }
472       newpos[depth] = '0';
473       new = add_to_sequence (SET_DEST (pattern), &new->success, newpos);
474       this->success.first->enforce_mode = 1;
475       newpos[depth] = '1';
476       new = add_to_sequence (SET_SRC (pattern), &new->success, newpos);
477
478       /* If set are setting CC0 from anything other than a COMPARE, we
479          must enforce the mode so that we do not produce ambiguous insns.  */
480       if (GET_CODE (SET_DEST (pattern)) == CC0
481           && GET_CODE (SET_SRC (pattern)) != COMPARE)
482         this->success.first->enforce_mode = 1;
483       return new;
484
485     case SIGN_EXTEND:
486     case ZERO_EXTEND:
487     case STRICT_LOW_PART:
488       newpos[depth] = '0';
489       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
490       this->success.first->enforce_mode = 1;
491       return new;
492
493     case SUBREG:
494       this->test_elt_one_int = 1;
495       this->elt_one_int = XINT (pattern, 1);
496       newpos[depth] = '0';
497       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
498       this->success.first->enforce_mode = 1;
499       return new;
500
501     case ZERO_EXTRACT:
502     case SIGN_EXTRACT:
503       newpos[depth] = '0';
504       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
505       this->success.first->enforce_mode = 1;
506       newpos[depth] = '1';
507       new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
508       newpos[depth] = '2';
509       new = add_to_sequence (XEXP (pattern, 2), &new->success, newpos);
510       return new;
511
512     case EQ:   case NE:   case LE:   case LT:   case GE:  case GT:
513     case LEU:  case LTU:  case GEU:  case GTU:
514       /* If the first operand is (cc0), we don't have to do anything
515          special.  */
516       if (GET_CODE (XEXP (pattern, 0)) == CC0)
517         break;
518
519       /* ... fall through ...  */
520       
521     case COMPARE:
522       /* Enforce the mode on the first operand to avoid ambiguous insns.  */
523       newpos[depth] = '0';
524       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
525       this->success.first->enforce_mode = 1;
526       newpos[depth] = '1';
527       new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
528       return new;
529       
530     default:
531       break;
532     }
533
534   fmt = GET_RTX_FORMAT (code);
535   len = GET_RTX_LENGTH (code);
536   for (i = 0; i < len; i++)
537     {
538       newpos[depth] = '0' + i;
539       if (fmt[i] == 'e' || fmt[i] == 'u')
540         new = add_to_sequence (XEXP (pattern, i), &new->success, newpos);
541       else if (fmt[i] == 'i' && i == 0)
542         {
543           this->test_elt_zero_int = 1;
544           this->elt_zero_int = XINT (pattern, i);
545         }
546       else if (fmt[i] == 'i' && i == 1)
547         {
548           this->test_elt_one_int = 1;
549           this->elt_one_int = XINT (pattern, i);
550         }
551       else if (fmt[i] == 'w' && i == 0)
552         {
553           this->test_elt_zero_wide = 1;
554           this->elt_zero_wide = XWINT (pattern, i);
555         }
556       else if (fmt[i] == 'E')
557         {
558           register int j;
559           /* We do not handle a vector appearing as other than
560              the first item, just because nothing uses them
561              and by handling only the special case
562              we can use one element in newpos for either
563              the item number of a subexpression
564              or the element number in a vector.  */
565           if (i != 0)
566             abort ();
567           this->veclen = XVECLEN (pattern, i);
568           for (j = 0; j < XVECLEN (pattern, i); j++)
569             {
570               newpos[depth] = 'a' + j;
571               new = add_to_sequence (XVECEXP (pattern, i, j),
572                                      &new->success, newpos);
573             }
574         }
575       else if (fmt[i] != '0')
576         abort ();
577     }
578   return new;
579 }
580 \f
581 /* Return 1 if we can prove that there is no RTL that can match both
582    D1 and D2.  Otherwise, return 0 (it may be that there is an RTL that
583    can match both or just that we couldn't prove there wasn't such an RTL).
584
585    TOPLEVEL is non-zero if we are to only look at the top level and not
586    recursively descend.  */
587
588 static int
589 not_both_true (d1, d2, toplevel)
590      struct decision *d1, *d2;
591      int toplevel;
592 {
593   struct decision *p1, *p2;
594
595   /* If they are both to test modes and the modes are different, they aren't
596      both true.  Similarly for codes, integer elements, and vector lengths.  */
597
598   if ((d1->enforce_mode && d2->enforce_mode
599        && d1->mode != VOIDmode && d2->mode != VOIDmode && d1->mode != d2->mode)
600       || (d1->code != UNKNOWN && d2->code != UNKNOWN && d1->code != d2->code)
601       || (d1->test_elt_zero_int && d2->test_elt_zero_int
602           && d1->elt_zero_int != d2->elt_zero_int)
603       || (d1->test_elt_one_int && d2->test_elt_one_int
604           && d1->elt_one_int != d2->elt_one_int)
605       || (d1->test_elt_zero_wide && d2->test_elt_zero_wide
606           && d1->elt_zero_wide != d2->elt_zero_wide)
607       || (d1->veclen && d2->veclen && d1->veclen != d2->veclen))
608     return 1;
609
610   /* If either is a wild-card MATCH_OPERAND without a predicate, it can match
611      absolutely anything, so we can't say that no intersection is possible.
612      This case is detected by having a zero TESTS field with a code of
613      UNKNOWN.  */
614
615   if ((d1->tests == 0 && d1->code == UNKNOWN)
616       || (d2->tests == 0 && d2->code == UNKNOWN))
617     return 0;
618
619   /* If either has a predicate that we know something about, set things up so
620      that D1 is the one that always has a known predicate.  Then see if they
621      have any codes in common.  */
622
623   if (d1->pred >= 0 || d2->pred >= 0)
624     {
625       int i, j;
626
627       if (d2->pred >= 0)
628         p1 = d1, d1 = d2, d2 = p1;
629
630       /* If D2 tests an explicit code, see if it is in the list of valid codes
631          for D1's predicate.  */
632       if (d2->code != UNKNOWN)
633         {
634           for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
635             if (preds[d1->pred].codes[i] == d2->code)
636               break;
637
638           if (preds[d1->pred].codes[i] == 0)
639             return 1;
640         }
641
642       /* Otherwise see if the predicates have any codes in common.  */
643
644       else if (d2->pred >= 0)
645         {
646           for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
647             {
648               for (j = 0; j < NUM_RTX_CODE; j++)
649                 if (preds[d2->pred].codes[j] == 0
650                     || preds[d2->pred].codes[j] == preds[d1->pred].codes[i])
651                   break;
652
653               if (preds[d2->pred].codes[j] != 0)
654                 break;
655             }
656
657           if (preds[d1->pred].codes[i] == 0)
658             return 1;
659         }
660     }
661
662   /* If we got here, we can't prove that D1 and D2 cannot both be true.
663      If we are only to check the top level, return 0.  Otherwise, see if
664      we can prove that all choices in both successors are mutually
665      exclusive.  If either does not have any successors, we can't prove
666      they can't both be true.  */
667
668   if (toplevel || d1->success.first == 0 || d2->success.first == 0)
669     return 0;
670
671   for (p1 = d1->success.first; p1; p1 = p1->next)
672     for (p2 = d2->success.first; p2; p2 = p2->next)
673       if (! not_both_true (p1, p2, 0))
674         return 0;
675
676   return 1;
677 }
678 \f
679 /* Assuming that we can reorder all the alternatives at a specific point in
680    the tree (see discussion in merge_trees), we would prefer an ordering of
681    nodes where groups of consecutive nodes test the same mode and, within each
682    mode, groups of nodes test the same code.  With this order, we can
683    construct nested switch statements, the inner one to test the code and
684    the outer one to test the mode.
685
686    We would like to list nodes testing for specific codes before those
687    that test predicates to avoid unnecessary function calls.  Similarly,
688    tests for specific modes should precede nodes that allow any mode.
689
690    This function returns the merit (with 0 being the best) of inserting
691    a test involving the specified MODE and CODE after node P.  If P is
692    zero, we are to determine the merit of inserting the test at the front
693    of the list.  */
694
695 static int
696 position_merit (p, mode, code)
697      struct decision *p;
698      enum machine_mode mode;
699      enum rtx_code code;
700 {
701   enum machine_mode p_mode;
702
703   /* The only time the front of the list is anything other than the worst
704      position is if we are testing a mode that isn't VOIDmode.  */
705   if (p == 0)
706     return mode == VOIDmode ? 3 : 2;
707
708   p_mode = p->enforce_mode ? p->mode : VOIDmode;
709
710   /* The best case is if the codes and modes both match.  */
711   if (p_mode == mode && p->code== code)
712     return 0;
713
714   /* If the codes don't match, the next best case is if the modes match.
715      In that case, the best position for this node depends on whether
716      we are testing for a specific code or not.  If we are, the best place
717      is after some other test for an explicit code and our mode or after
718      the last test in the previous mode if every test in our mode is for
719      an unknown code.
720
721      If we are testing for UNKNOWN, then the next best case is at the end of
722      our mode.  */
723
724   if ((code != UNKNOWN
725        && ((p_mode == mode && p->code != UNKNOWN)
726            || (p_mode != mode && p->next
727                && (p->next->enforce_mode ? p->next->mode : VOIDmode) == mode
728                && (p->next->code == UNKNOWN))))
729       || (code == UNKNOWN && p_mode == mode
730           && (p->next == 0
731               || (p->next->enforce_mode ? p->next->mode : VOIDmode) != mode)))
732     return 1;
733
734   /* The third best case occurs when nothing is testing MODE.  If MODE
735      is not VOIDmode, then the third best case is after something of any
736      mode that is not VOIDmode.  If we are testing VOIDmode, the third best
737      place is the end of the list.  */
738
739   if (p_mode != mode
740       && ((mode != VOIDmode && p_mode != VOIDmode)
741           || (mode == VOIDmode && p->next == 0)))
742     return 2;
743
744   /* Otherwise, we have the worst case.  */
745   return 3;
746 }
747 \f
748 /* Merge two decision tree listheads OLDH and ADDH,
749    modifying OLDH destructively, and return the merged tree.  */
750
751 static struct decision_head
752 merge_trees (oldh, addh)
753      register struct decision_head oldh, addh;
754 {
755   struct decision *add, *next;
756
757   if (oldh.first == 0)
758     return addh;
759
760   if (addh.first == 0)
761     return oldh;
762
763   /* If we are adding things at different positions, something is wrong.  */
764   if (strcmp (oldh.first->position, addh.first->position))
765     abort ();
766
767   for (add = addh.first; add; add = next)
768     {
769       enum machine_mode add_mode = add->enforce_mode ? add->mode : VOIDmode;
770       struct decision *best_position = 0;
771       int best_merit = 4;
772       struct decision *old;
773
774       next = add->next;
775
776       /* The semantics of pattern matching state that the tests are done in
777          the order given in the MD file so that if an insn matches two
778          patterns, the first one will be used.  However, in practice, most,
779          if not all, patterns are unambiguous so that their order is 
780          independent.  In that case, we can merge identical tests and
781          group all similar modes and codes together.
782
783          Scan starting from the end of OLDH until we reach a point
784          where we reach the head of the list or where we pass a pattern
785          that could also be true if NEW is true.  If we find an identical
786          pattern, we can merge them.  Also, record the last node that tests
787          the same code and mode and the last one that tests just the same mode.
788
789          If we have no match, place NEW after the closest match we found.  */
790          
791       for (old = oldh.last; old; old = old->prev)
792         {
793           int our_merit;
794
795           /* If we don't have anything to test except an additional test,
796              do not consider the two nodes equal.  If we did, the test below
797              would cause an infinite recursion.  */
798           if (old->tests == 0 && old->test_elt_zero_int == 0
799               && old->test_elt_one_int == 0 && old->veclen == 0
800               && old->test_elt_zero_wide == 0
801               && old->dupno == -1 && old->mode == VOIDmode
802               && old->code == UNKNOWN
803               && (old->c_test != 0 || add->c_test != 0))
804             ;
805
806           else if ((old->tests == add->tests
807                     || (old->pred >= 0 && old->pred == add->pred)
808                     || (old->tests && add->tests
809                         && !strcmp (old->tests, add->tests)))
810                    && old->test_elt_zero_int == add->test_elt_zero_int
811                    && old->elt_zero_int == add->elt_zero_int
812                    && old->test_elt_one_int == add->test_elt_one_int
813                    && old->elt_one_int == add->elt_one_int
814                    && old->test_elt_zero_wide == add->test_elt_zero_wide
815                    && old->elt_zero_wide == add->elt_zero_wide
816                    && old->veclen == add->veclen
817                    && old->dupno == add->dupno
818                    && old->opno == add->opno
819                    && old->code == add->code
820                    && old->enforce_mode == add->enforce_mode
821                    && old->mode == add->mode)
822             {
823               /* If the additional test is not the same, split both nodes
824                  into nodes that just contain all things tested before the
825                  additional test and nodes that contain the additional test
826                  and actions when it is true.  This optimization is important
827                  because of the case where we have almost identical patterns
828                  with different tests on target flags.  */
829
830               if (old->c_test != add->c_test
831                   && ! (old->c_test && add->c_test
832                         && !strcmp (old->c_test, add->c_test)))
833                 {
834                   if (old->insn_code_number >= 0 || old->opno >= 0)
835                     {
836                       struct decision *split
837                         = (struct decision *) xmalloc (sizeof (struct decision));
838
839                       mybcopy ((char *) old, (char *) split,
840                                sizeof (struct decision));
841
842                       old->success.first = old->success.last = split;
843                       old->c_test = 0;
844                       old->opno = -1;
845                       old->insn_code_number = -1;
846                       old->num_clobbers_to_add = 0;
847
848                       split->number = next_number++;
849                       split->next = split->prev = 0;
850                       split->mode = VOIDmode;
851                       split->code = UNKNOWN;
852                       split->veclen = 0;
853                       split->test_elt_zero_int = 0;
854                       split->test_elt_one_int = 0;
855                       split->test_elt_zero_wide = 0;
856                       split->tests = 0;
857                       split->pred = -1;
858                       split->dupno = -1;
859                     }
860
861                   if (add->insn_code_number >= 0 || add->opno >= 0)
862                     {
863                       struct decision *split
864                         = (struct decision *) xmalloc (sizeof (struct decision));
865
866                       mybcopy ((char *) add, (char *) split,
867                                sizeof (struct decision));
868
869                       add->success.first = add->success.last = split;
870                       add->c_test = 0;
871                       add->opno = -1;
872                       add->insn_code_number = -1;
873                       add->num_clobbers_to_add = 0;
874
875                       split->number = next_number++;
876                       split->next = split->prev = 0;
877                       split->mode = VOIDmode;
878                       split->code = UNKNOWN;
879                       split->veclen = 0;
880                       split->test_elt_zero_int = 0;
881                       split->test_elt_one_int = 0;
882                       split->test_elt_zero_wide = 0;
883                       split->tests = 0;
884                       split->pred = -1;
885                       split->dupno = -1;
886                     }
887                 }
888
889               if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
890                 {
891                   /* If one node is for a normal insn and the second is
892                      for the base insn with clobbers stripped off, the
893                      second node should be ignored.  */
894
895                   if (old->num_clobbers_to_add == 0
896                       && add->num_clobbers_to_add > 0)
897                     /* Nothing to do here.  */
898                     ;
899                   else if (old->num_clobbers_to_add > 0
900                            && add->num_clobbers_to_add == 0)
901                     {
902                       /* In this case, replace OLD with ADD.  */
903                       old->insn_code_number = add->insn_code_number;
904                       old->num_clobbers_to_add = 0;
905                     }
906                   else
907                     fatal ("Two actions at one point in tree");
908                 }
909
910               if (old->insn_code_number == -1)
911                 old->insn_code_number = add->insn_code_number;
912               old->success = merge_trees (old->success, add->success);
913               add = 0;
914               break;
915             }
916
917           /* Unless we have already found the best possible insert point,
918              see if this position is better.  If so, record it.  */
919
920           if (best_merit != 0
921               && ((our_merit = position_merit (old, add_mode, add->code))
922                   < best_merit))
923             best_merit = our_merit, best_position = old;
924
925           if (! not_both_true (old, add, 0))
926             break;
927         }
928
929       /* If ADD was duplicate, we are done.  */
930       if (add == 0)
931         continue;
932
933       /* Otherwise, find the best place to insert ADD.  Normally this is
934          BEST_POSITION.  However, if we went all the way to the top of
935          the list, it might be better to insert at the top.  */
936
937       if (best_position == 0)
938         abort ();
939
940       if (old == 0
941           && position_merit (NULL_PTR, add_mode, add->code) < best_merit)
942         {
943           add->prev = 0;
944           add->next = oldh.first;
945           oldh.first->prev = add;
946           oldh.first = add;
947         }
948
949       else
950         {
951           add->prev = best_position;
952           add->next = best_position->next;
953           best_position->next = add;
954           if (best_position == oldh.last)
955             oldh.last = add;
956           else
957             add->next->prev = add;
958         }
959     }
960
961   return oldh;
962 }
963 \f
964 /* Count the number of subnodes of HEAD.  If the number is high enough,
965    make the first node in HEAD start a separate subroutine in the C code
966    that is generated.
967
968    TYPE gives the type of routine we are writing.
969
970    INITIAL is non-zero if this is the highest-level node.  We never write
971    it out here.  */
972
973 static int
974 break_out_subroutines (head, type, initial)
975      struct decision_head head;
976      enum routine_type type;
977      int initial;
978 {
979   int size = 0;
980   struct decision *sub;
981
982   for (sub = head.first; sub; sub = sub->next)
983     size += 1 + break_out_subroutines (sub->success, type, 0);
984
985   if (size > SUBROUTINE_THRESHOLD && ! initial)
986     {
987       head.first->subroutine_number = ++next_subroutine_number;
988       write_subroutine (head.first, type);
989       size = 1;
990     }
991   return size;
992 }
993 \f
994 /* Write out a subroutine of type TYPE to do comparisons starting at node
995    TREE.  */
996
997 static void
998 write_subroutine (tree, type)
999      struct decision *tree;
1000      enum routine_type type;
1001 {
1002   int i;
1003
1004   if (type == SPLIT)
1005     printf ("rtx\nsplit");
1006   else
1007     printf ("int\nrecog");
1008
1009   if (tree != 0 && tree->subroutine_number > 0)
1010     printf ("_%d", tree->subroutine_number);
1011   else if (type == SPLIT)
1012     printf ("_insns");
1013
1014   printf (" (x0, insn");
1015   if (type == RECOG)
1016     printf (", pnum_clobbers");
1017
1018   printf (")\n");
1019   printf ("     register rtx x0;\n     rtx insn ATTRIBUTE_UNUSED;\n");
1020   if (type == RECOG)
1021     printf ("     int *pnum_clobbers ATTRIBUTE_UNUSED;\n");
1022
1023   printf ("{\n");
1024   printf ("  register rtx *ro = &recog_operand[0];\n");
1025
1026   printf ("  register rtx ");
1027   for (i = 1; i < max_depth; i++)
1028     printf ("x%d ATTRIBUTE_UNUSED, ", i);
1029
1030   printf ("x%d ATTRIBUTE_UNUSED;\n", max_depth);
1031   printf ("  %s tem ATTRIBUTE_UNUSED;\n", type == SPLIT ? "rtx" : "int");
1032   write_tree (tree, "", NULL_PTR, 1, type);
1033   printf (" ret0: return %d;\n}\n\n", type == SPLIT ? 0 : -1);
1034 }
1035 \f
1036 /* This table is used to indent the recog_* functions when we are inside
1037    conditions or switch statements.  We only support small indentations
1038    and always indent at least two spaces.  */
1039
1040 static char *indents[]
1041   = {"  ", "  ", "  ", "   ", "    ", "     ", "      ", "       ",
1042      "\t", "\t ", "\t  ", "\t   ", "\t    ", "\t     ", "\t      ",
1043      "\t\t", "\t\t ", "\t\t  ", "\t\t   ", "\t\t    ", "\t\t     "};
1044
1045 /* Write out C code to perform the decisions in TREE for a subroutine of
1046    type TYPE.  If all of the choices fail, branch to node AFTERWARD, if
1047    non-zero, otherwise return.  PREVPOS is the position of the node that
1048    branched to this test.
1049
1050    When we merged all alternatives, we tried to set up a convenient order.
1051    Specifically, tests involving the same mode are all grouped together,
1052    followed by a group that does not contain a mode test.  Within each group
1053    of the same mode, we also group tests with the same code, followed by a
1054    group that does not test a code.
1055
1056    Occasionally, we cannot arbitrarily reorder the tests so that multiple
1057    sequence of groups as described above are present.
1058
1059    We generate two nested switch statements, the outer statement for
1060    testing modes, and the inner switch for testing RTX codes.  It is
1061    not worth optimizing cases when only a small number of modes or 
1062    codes is tested, since the compiler can do that when compiling the
1063    resulting function.   We do check for when every test is the same mode
1064    or code.  */
1065
1066 static void
1067 write_tree_1 (tree, prevpos, afterward, type)
1068      struct decision *tree;
1069      char *prevpos;
1070      struct decision *afterward;
1071      enum routine_type type;
1072 {
1073   register struct decision *p, *p1;
1074   register int depth = tree ? strlen (tree->position) : 0;
1075   enum machine_mode switch_mode = VOIDmode;
1076   RTX_CODE switch_code = UNKNOWN;
1077   int uncond = 0;
1078   char modemap[NUM_MACHINE_MODES];
1079   char codemap[NUM_RTX_CODE];
1080   int indent = 2;
1081   int i;
1082
1083   /* One tricky area is what is the exact state when we branch to a
1084      node's label.  There are two cases where we branch: when looking at
1085      successors to a node, or when a set of tests fails.
1086
1087      In the former case, we are always branching to the first node in a
1088      decision list and we want all required tests to be performed.  We
1089      put the labels for such nodes in front of any switch or test statements.
1090      These branches are done without updating the position to that of the
1091      target node.
1092
1093      In the latter case, we are branching to a node that is not the first
1094      node in a decision list.  We have already checked that it is possible
1095      for both the node we originally tested at this level and the node we
1096      are branching to to both match some pattern.  That means that they
1097      usually will be testing the same mode and code.  So it is normally safe
1098      for such labels to be inside switch statements, since the tests done
1099      by virtue of arriving at that label will usually already have been
1100      done.  The exception is a branch from a node that does not test a
1101      mode or code to one that does.  In such cases, we set the `retest_mode'
1102      or `retest_code' flags.  That will ensure that we start a new switch
1103      at that position and put the label before the switch. 
1104
1105      The branches in the latter case must set the position to that of the
1106      target node.  */
1107
1108
1109   printf ("\n");
1110   if (tree && tree->subroutine_number == 0)
1111     {
1112       printf ("  L%d:\n", tree->number);
1113       tree->label_needed = 0;
1114     }
1115
1116   if (tree)
1117     {
1118       change_state (prevpos, tree->position, 2);
1119       prevpos = tree->position;
1120     }
1121
1122   for (p = tree; p; p = p->next)
1123     {
1124       enum machine_mode mode = p->enforce_mode ? p->mode : VOIDmode;
1125       int need_bracket;
1126       int wrote_bracket = 0;
1127       int inner_indent;
1128
1129       if (p->success.first == 0 && p->insn_code_number < 0)
1130         abort ();
1131
1132       /* Find the next alternative to p that might be true when p is true.
1133          Test that one next if p's successors fail.  */
1134
1135       for (p1 = p->next; p1 && not_both_true (p, p1, 1); p1 = p1->next)
1136         ;
1137       p->afterward = p1;
1138
1139       if (p1)
1140         {
1141           if (mode == VOIDmode && p1->enforce_mode && p1->mode != VOIDmode)
1142             p1->retest_mode = 1;
1143           if (p->code == UNKNOWN && p1->code != UNKNOWN)
1144             p1->retest_code = 1;
1145           p1->label_needed = 1;
1146         }
1147
1148       /* If we have a different code or mode than the last node and
1149          are in a switch on codes, we must either end the switch or
1150          go to another case.  We must also end the switch if this
1151          node needs a label and to retest either the mode or code.  */
1152
1153       if (switch_code != UNKNOWN
1154           && (switch_code != p->code || switch_mode != mode
1155               || (p->label_needed && (p->retest_mode || p->retest_code))))
1156         {
1157           enum rtx_code code = p->code;
1158
1159           /* If P is testing a predicate that we know about and we haven't
1160              seen any of the codes that are valid for the predicate, we
1161              can write a series of "case" statement, one for each possible
1162              code.  Since we are already in a switch, these redundant tests
1163              are very cheap and will reduce the number of predicate called.  */
1164
1165           if (p->pred >= 0)
1166             {
1167               for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
1168                 if (codemap[(int) preds[p->pred].codes[i]])
1169                   break;
1170
1171               if (preds[p->pred].codes[i] == 0)
1172                 code = MATCH_OPERAND;
1173             }
1174
1175           if (code == UNKNOWN || codemap[(int) code]
1176               || switch_mode != mode
1177               || (p->label_needed && (p->retest_mode || p->retest_code)))
1178             {
1179               printf ("%s}\n", indents[indent - 2]);
1180               switch_code = UNKNOWN;
1181               indent -= 4;
1182             }
1183           else
1184             {
1185               if (! uncond)
1186                 printf ("%sbreak;\n", indents[indent]);
1187
1188               if (code == MATCH_OPERAND)
1189                 {
1190                   for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
1191                     {
1192                       printf ("%scase ", indents[indent - 2]);
1193                       print_code (preds[p->pred].codes[i]);
1194                       printf (":\n");
1195                       codemap[(int) preds[p->pred].codes[i]] = 1;
1196                     }
1197                 }
1198               else
1199                 {
1200                   printf ("%scase ", indents[indent - 2]);
1201                   print_code (code);
1202                   printf (":\n");
1203                   codemap[(int) p->code] = 1;
1204                 }
1205
1206               switch_code = code;
1207             }
1208
1209           uncond = 0;
1210         }
1211
1212       /* If we were previously in a switch on modes and now have a different
1213          mode, end at least the case, and maybe end the switch if we are
1214          not testing a mode or testing a mode whose case we already saw.  */
1215
1216       if (switch_mode != VOIDmode
1217           && (switch_mode != mode || (p->label_needed && p->retest_mode)))
1218         {
1219           if (mode == VOIDmode || modemap[(int) mode]
1220               || (p->label_needed && p->retest_mode))
1221             {
1222               printf ("%s}\n", indents[indent - 2]);
1223               switch_mode = VOIDmode;
1224               indent -= 4;
1225             }
1226           else
1227             {
1228               if (! uncond)
1229                 printf ("      break;\n");
1230               printf ("    case %smode:\n", GET_MODE_NAME (mode));
1231               switch_mode = mode;
1232               modemap[(int) mode] = 1;
1233             }
1234
1235           uncond = 0;
1236         }
1237
1238       /* If we are about to write dead code, something went wrong.  */
1239       if (! p->label_needed && uncond)
1240         abort ();
1241
1242       /* If we need a label and we will want to retest the mode or code at
1243          that label, write the label now.  We have already ensured that
1244          things will be valid for the test.  */
1245
1246       if (p->label_needed && (p->retest_mode || p->retest_code))
1247         {
1248           printf ("%sL%d:\n", indents[indent - 2], p->number);
1249           p->label_needed = 0;
1250         }
1251
1252       uncond = 0;
1253
1254       /* If we are not in any switches, see if we can shortcut things
1255          by checking for identical modes and codes.  */
1256
1257       if (switch_mode == VOIDmode && switch_code == UNKNOWN)
1258         {
1259           /* If p and its alternatives all want the same mode,
1260              reject all others at once, first, then ignore the mode.  */
1261
1262           if (mode != VOIDmode && p->next && same_modes (p, mode))
1263             {
1264               printf ("  if (GET_MODE (x%d) != %smode)\n",
1265                       depth, GET_MODE_NAME (p->mode));
1266               if (afterward)
1267                 {
1268                   printf ("    {\n");
1269                   change_state (p->position, afterward->position, 6);
1270                   printf ("      goto L%d;\n    }\n", afterward->number);
1271                 }
1272               else
1273                 printf ("    goto ret0;\n");
1274               clear_modes (p);
1275               mode = VOIDmode;
1276             }
1277
1278           /* If p and its alternatives all want the same code,
1279              reject all others at once, first, then ignore the code.  */
1280
1281           if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
1282             {
1283               printf ("  if (GET_CODE (x%d) != ", depth);
1284               print_code (p->code);
1285               printf (")\n");
1286               if (afterward)
1287                 {
1288                   printf ("    {\n");
1289                   change_state (p->position, afterward->position, indent + 4);
1290                   printf ("    goto L%d;\n    }\n", afterward->number);
1291                 }
1292               else
1293                 printf ("    goto ret0;\n");
1294               clear_codes (p);
1295             }
1296         }
1297
1298       /* If we are not in a mode switch and we are testing for a specific
1299          mode, start a mode switch unless we have just one node or the next
1300          node is not testing a mode (we have already tested for the case of
1301          more than one mode, but all of the same mode).  */
1302
1303       if (switch_mode == VOIDmode && mode != VOIDmode && p->next != 0
1304           && p->next->enforce_mode && p->next->mode != VOIDmode)
1305         {
1306           mybzero (modemap, sizeof modemap);
1307           printf ("%sswitch (GET_MODE (x%d))\n", indents[indent], depth);
1308           printf ("%s{\n", indents[indent + 2]);
1309           indent += 4;
1310           printf ("%sdefault:\n%sbreak;\n", indents[indent - 2],
1311                   indents[indent]);
1312           printf ("%scase %smode:\n", indents[indent - 2],
1313                   GET_MODE_NAME (mode));
1314           modemap[(int) mode] = 1;
1315           switch_mode = mode;
1316         }
1317
1318       /* Similarly for testing codes.  */
1319
1320       if (switch_code == UNKNOWN && p->code != UNKNOWN && ! p->ignore_code
1321           && p->next != 0 && p->next->code != UNKNOWN)
1322         {
1323           mybzero (codemap, sizeof codemap);
1324           printf ("%sswitch (GET_CODE (x%d))\n", indents[indent], depth);
1325           printf ("%s{\n", indents[indent + 2]);
1326           indent += 4;
1327           printf ("%sdefault:\n%sbreak;\n", indents[indent - 2],
1328                   indents[indent]);
1329           printf ("%scase ", indents[indent - 2]);
1330           print_code (p->code);
1331           printf (":\n");
1332           codemap[(int) p->code] = 1;
1333           switch_code = p->code;
1334         }
1335
1336       /* Now that most mode and code tests have been done, we can write out
1337          a label for an inner node, if we haven't already.  */
1338       if (p->label_needed)
1339         printf ("%sL%d:\n", indents[indent - 2], p->number);
1340
1341       inner_indent = indent;
1342
1343       /* The only way we can have to do a mode or code test here is if
1344          this node needs such a test but is the only node to be tested.
1345          In that case, we won't have started a switch.  Note that this is
1346          the only way the switch and test modes can disagree.  */
1347
1348       if ((mode != switch_mode && ! p->ignore_mode)
1349           || (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
1350           || p->test_elt_zero_int || p->test_elt_one_int
1351           || p->test_elt_zero_wide || p->veclen
1352           || p->dupno >= 0 || p->tests || p->num_clobbers_to_add)
1353         {
1354           printf ("%sif (", indents[indent]);
1355
1356           if (mode != switch_mode && ! p->ignore_mode)
1357             printf ("GET_MODE (x%d) == %smode && ",
1358                     depth, GET_MODE_NAME (mode));
1359           if (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
1360             {
1361               printf ("GET_CODE (x%d) == ", depth);
1362               print_code (p->code);
1363               printf (" && ");
1364             }
1365
1366           if (p->test_elt_zero_int)
1367             printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
1368           if (p->test_elt_one_int)
1369             printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
1370           if (p->test_elt_zero_wide)
1371             {
1372               /* Set offset to 1 iff the number might get propagated to
1373                  unsigned long by ANSI C rules, else 0.
1374                  Prospective hosts are required to have at least 32 bit
1375                  ints, and integer constants in machine descriptions
1376                  must fit in 32 bit, thus it suffices to check only
1377                  for 1 << 31 .  */
1378               HOST_WIDE_INT offset = p->elt_zero_wide == -2147483647 - 1;
1379               printf ("XWINT (x%d, 0) == ", depth);
1380               printf (HOST_WIDE_INT_PRINT_DEC, p->elt_zero_wide + offset);
1381               printf ("%s && ", offset ? "-1" : "");
1382             }
1383           if (p->veclen)
1384             printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
1385           if (p->dupno >= 0)
1386             printf ("rtx_equal_p (x%d, ro[%d]) && ", depth, p->dupno);
1387           if (p->num_clobbers_to_add)
1388             printf ("pnum_clobbers != 0 && ");
1389           if (p->tests)
1390             printf ("%s (x%d, %smode)", p->tests, depth,
1391                     GET_MODE_NAME (p->mode));
1392           else
1393             printf ("1");
1394
1395           printf (")\n");
1396           inner_indent += 2;
1397         }
1398       else
1399         uncond = 1;
1400
1401       need_bracket = ! uncond;
1402
1403       if (p->opno >= 0)
1404         {
1405           if (need_bracket)
1406             {
1407               printf ("%s{\n", indents[inner_indent]);
1408               inner_indent += 2;
1409               wrote_bracket = 1;
1410               need_bracket = 0;
1411             }
1412
1413           printf ("%sro[%d] = x%d;\n", indents[inner_indent], p->opno, depth);
1414         }
1415
1416       if (p->c_test)
1417         {
1418           printf ("%sif (%s)\n", indents[inner_indent], p->c_test);
1419           inner_indent += 2;
1420           uncond = 0;
1421           need_bracket = 1;
1422         }
1423
1424       if (p->insn_code_number >= 0)
1425         {
1426           if (type == SPLIT)
1427             printf ("%sreturn gen_split_%d (operands);\n",
1428                     indents[inner_indent], p->insn_code_number);
1429           else
1430             {
1431               if (p->num_clobbers_to_add)
1432                 {
1433                   if (need_bracket)
1434                     {
1435                       printf ("%s{\n", indents[inner_indent]);
1436                       inner_indent += 2;
1437                     }
1438
1439                   printf ("%s*pnum_clobbers = %d;\n",
1440                           indents[inner_indent], p->num_clobbers_to_add);
1441                   printf ("%sreturn %d;\n",
1442                           indents[inner_indent], p->insn_code_number);
1443
1444                   if (need_bracket)
1445                     {
1446                       inner_indent -= 2;
1447                       printf ("%s}\n", indents[inner_indent]);
1448                     }
1449                 }
1450               else
1451                 printf ("%sreturn %d;\n",
1452                         indents[inner_indent], p->insn_code_number);
1453             }
1454         }
1455       else
1456         printf ("%sgoto L%d;\n", indents[inner_indent],
1457                 p->success.first->number);
1458
1459       if (wrote_bracket)
1460         printf ("%s}\n", indents[inner_indent - 2]);
1461     }
1462
1463   /* We have now tested all alternatives.  End any switches we have open
1464      and branch to the alternative node unless we know that we can't fall
1465      through to the branch.  */
1466
1467   if (switch_code != UNKNOWN)
1468     {
1469       printf ("%s}\n", indents[indent - 2]);
1470       indent -= 4;
1471       uncond = 0;
1472     }
1473
1474   if (switch_mode != VOIDmode)
1475     {
1476       printf ("%s}\n", indents[indent - 2]);
1477       indent -= 4;
1478       uncond = 0;
1479     }
1480
1481   if (indent != 2)
1482     abort ();
1483
1484   if (uncond)
1485     return;
1486
1487   if (afterward)
1488     {
1489       change_state (prevpos, afterward->position, 2);
1490       printf ("  goto L%d;\n", afterward->number);
1491     }
1492   else
1493     printf ("  goto ret0;\n");
1494 }
1495
1496 static void
1497 print_code (code)
1498      enum rtx_code code;
1499 {
1500   register char *p1;
1501   for (p1 = GET_RTX_NAME (code); *p1; p1++)
1502     {
1503       if (*p1 >= 'a' && *p1 <= 'z')
1504         putchar (*p1 + 'A' - 'a');
1505       else
1506         putchar (*p1);
1507     }
1508 }
1509
1510 static int
1511 same_codes (p, code)
1512      register struct decision *p;
1513      register enum rtx_code code;
1514 {
1515   for (; p; p = p->next)
1516     if (p->code != code)
1517       return 0;
1518
1519   return 1;
1520 }
1521
1522 static void
1523 clear_codes (p)
1524      register struct decision *p;
1525 {
1526   for (; p; p = p->next)
1527     p->ignore_code = 1;
1528 }
1529
1530 static int
1531 same_modes (p, mode)
1532      register struct decision *p;
1533      register enum machine_mode mode;
1534 {
1535   for (; p; p = p->next)
1536     if ((p->enforce_mode ? p->mode : VOIDmode) != mode)
1537       return 0;
1538
1539   return 1;
1540 }
1541
1542 static void
1543 clear_modes (p)
1544      register struct decision *p;
1545 {
1546   for (; p; p = p->next)
1547     p->enforce_mode = 0;
1548 }
1549 \f
1550 /* Write out the decision tree starting at TREE for a subroutine of type TYPE.
1551
1552    PREVPOS is the position at the node that branched to this node.
1553
1554    INITIAL is nonzero if this is the first node we are writing in a subroutine.
1555
1556    If all nodes are false, branch to the node AFTERWARD.  */
1557
1558 static void
1559 write_tree (tree, prevpos, afterward, initial, type)
1560      struct decision *tree;
1561      char *prevpos;
1562      struct decision *afterward;
1563      int initial;
1564      enum routine_type type;
1565 {
1566   register struct decision *p;
1567   char *name_prefix = (type == SPLIT ? "split" : "recog");
1568   char *call_suffix = (type == SPLIT ? "" : ", pnum_clobbers");
1569
1570   if (! initial && tree->subroutine_number > 0)
1571     {
1572       printf (" L%d:\n", tree->number);
1573
1574       if (afterward)
1575         {
1576           printf ("  tem = %s_%d (x0, insn%s);\n",
1577                   name_prefix, tree->subroutine_number, call_suffix);
1578           if (type == SPLIT)
1579             printf ("  if (tem != 0) return tem;\n");
1580           else
1581             printf ("  if (tem >= 0) return tem;\n");
1582           change_state (tree->position, afterward->position, 2);
1583           printf ("  goto L%d;\n", afterward->number);
1584         }
1585       else
1586         printf ("  return %s_%d (x0, insn%s);\n",
1587                 name_prefix, tree->subroutine_number, call_suffix);
1588       return;
1589     }
1590
1591   write_tree_1 (tree, prevpos, afterward, type);
1592
1593   for (p = tree; p; p = p->next)
1594     if (p->success.first)
1595       write_tree (p->success.first, p->position,
1596                   p->afterward ? p->afterward : afterward, 0, type);
1597 }
1598
1599 \f
1600 /* Assuming that the state of argument is denoted by OLDPOS, take whatever
1601    actions are necessary to move to NEWPOS.
1602
1603    INDENT says how many blanks to place at the front of lines.  */
1604
1605 static void
1606 change_state (oldpos, newpos, indent)
1607      char *oldpos;
1608      char *newpos;
1609      int indent;
1610 {
1611   int odepth = strlen (oldpos);
1612   int depth = odepth;
1613   int ndepth = strlen (newpos);
1614
1615   /* Pop up as many levels as necessary.  */
1616
1617   while (strncmp (oldpos, newpos, depth))
1618     --depth;
1619
1620   /* Go down to desired level.  */
1621
1622   while (depth < ndepth)
1623     {
1624       if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
1625         printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
1626                 indents[indent], depth + 1, depth, newpos[depth] - 'a');
1627       else
1628         printf ("%sx%d = XEXP (x%d, %c);\n",
1629                 indents[indent], depth + 1, depth, newpos[depth]);
1630       ++depth;
1631     }
1632 }
1633 \f
1634 static char *
1635 copystr (s1)
1636      char *s1;
1637 {
1638   register char *tem;
1639
1640   if (s1 == 0)
1641     return 0;
1642
1643   tem = (char *) xmalloc (strlen (s1) + 1);
1644   strcpy (tem, s1);
1645
1646   return tem;
1647 }
1648
1649 static void
1650 mybzero (b, length)
1651      register char *b;
1652      register unsigned length;
1653 {
1654   while (length-- > 0)
1655     *b++ = 0;
1656 }
1657
1658 static void
1659 mybcopy (in, out, length)
1660      register char *in, *out;
1661      register unsigned length;
1662 {
1663   while (length-- > 0)
1664     *out++ = *in++;
1665 }
1666
1667 char *
1668 xrealloc (ptr, size)
1669      char *ptr;
1670      unsigned size;
1671 {
1672   char *result = (char *) realloc (ptr, size);
1673   if (!result)
1674     fatal ("virtual memory exhausted");
1675   return result;
1676 }
1677
1678 char *
1679 xmalloc (size)
1680      unsigned size;
1681 {
1682   register char *val = (char *) malloc (size);
1683
1684   if (val == 0)
1685     fatal ("virtual memory exhausted");
1686   return val;
1687 }
1688
1689 static void
1690 fatal VPROTO ((char *format, ...))
1691 {
1692 #ifndef __STDC__
1693   char *format;
1694 #endif
1695   va_list ap;
1696
1697   VA_START (ap, format);
1698
1699 #ifndef __STDC__
1700   format = va_arg (ap, char *);
1701 #endif
1702
1703   fprintf (stderr, "genrecog: ");
1704   vfprintf (stderr, format, ap);
1705   va_end (ap);
1706   fprintf (stderr, "\n");
1707   fprintf (stderr, "after %d definitions\n", next_index);
1708   exit (FATAL_EXIT_CODE);
1709 }
1710
1711 /* More 'friendly' abort that prints the line and file.
1712    config.h can #define abort fancy_abort if you like that sort of thing.  */
1713
1714 void
1715 fancy_abort ()
1716 {
1717   fatal ("Internal gcc abort.");
1718 }
1719 \f
1720 int
1721 main (argc, argv)
1722      int argc;
1723      char **argv;
1724 {
1725   rtx desc;
1726   struct decision_head recog_tree;
1727   struct decision_head split_tree;
1728   FILE *infile;
1729   register int c;
1730
1731   obstack_init (rtl_obstack);
1732   recog_tree.first = recog_tree.last = split_tree.first = split_tree.last = 0;
1733
1734   if (argc <= 1)
1735     fatal ("No input file name.");
1736
1737   infile = fopen (argv[1], "r");
1738   if (infile == 0)
1739     {
1740       perror (argv[1]);
1741       exit (FATAL_EXIT_CODE);
1742     }
1743
1744   init_rtl ();
1745   next_insn_code = 0;
1746   next_index = 0;
1747
1748   printf ("/* Generated automatically by the program `genrecog'\n\
1749 from the machine description file `md'.  */\n\n");
1750
1751   printf ("#include \"config.h\"\n");
1752   printf ("#include \"system.h\"\n");
1753   printf ("#include \"rtl.h\"\n");
1754   printf ("#include \"insn-config.h\"\n");
1755   printf ("#include \"recog.h\"\n");
1756   printf ("#include \"real.h\"\n");
1757   printf ("#include \"output.h\"\n");
1758   printf ("#include \"flags.h\"\n");
1759   printf ("\n");
1760
1761   /* Read the machine description.  */
1762
1763   while (1)
1764     {
1765       c = read_skip_spaces (infile);
1766       if (c == EOF)
1767         break;
1768       ungetc (c, infile);
1769
1770       desc = read_rtx (infile);
1771       if (GET_CODE (desc) == DEFINE_INSN)
1772         recog_tree = merge_trees (recog_tree,
1773                                   make_insn_sequence (desc, RECOG));
1774       else if (GET_CODE (desc) == DEFINE_SPLIT)
1775         split_tree = merge_trees (split_tree,
1776                                   make_insn_sequence (desc, SPLIT));
1777       if (GET_CODE (desc) == DEFINE_PEEPHOLE
1778           || GET_CODE (desc) == DEFINE_EXPAND)
1779         next_insn_code++;
1780       next_index++;
1781     }
1782
1783   printf ("\n\
1784 /* `recog' contains a decision tree\n\
1785    that recognizes whether the rtx X0 is a valid instruction.\n\
1786 \n\
1787    recog returns -1 if the rtx is not valid.\n\
1788    If the rtx is valid, recog returns a nonnegative number\n\
1789    which is the insn code number for the pattern that matched.\n");
1790   printf ("   This is the same as the order in the machine description of\n\
1791    the entry that matched.  This number can be used as an index into various\n\
1792    insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands\n\
1793    (found in insn-output.c).\n\n");
1794   printf ("   The third argument to recog is an optional pointer to an int.\n\
1795    If present, recog will accept a pattern if it matches except for\n\
1796    missing CLOBBER expressions at the end.  In that case, the value\n\
1797    pointed to by the optional pointer will be set to the number of\n\
1798    CLOBBERs that need to be added (it should be initialized to zero by\n\
1799    the caller).  If it is set nonzero, the caller should allocate a\n\
1800    PARALLEL of the appropriate size, copy the initial entries, and call\n\
1801    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
1802
1803   if (split_tree.first)
1804     printf ("\n\n   The function split_insns returns 0 if the rtl could not\n\
1805    be split or the split rtl in a SEQUENCE if it can be.");
1806
1807   printf ("*/\n\n");
1808
1809   printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
1810   printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
1811   printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
1812   printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
1813   printf ("#define operands recog_operand\n\n");
1814
1815   next_subroutine_number = 0;
1816   break_out_subroutines (recog_tree, RECOG, 1);
1817   write_subroutine (recog_tree.first, RECOG);
1818
1819   next_subroutine_number = 0;
1820   break_out_subroutines (split_tree, SPLIT, 1);
1821   write_subroutine (split_tree.first, SPLIT);
1822
1823   fflush (stdout);
1824   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1825   /* NOTREACHED */
1826   return 0;
1827 }