OSDN Git Service

c266342b180a054e6b2990080013c142ae785ffc
[pf3gnuchains/gcc-fork.git] / gcc / genoutput.c
1 /* Generate code from to output assembler insns as recognized from rtl.
2    Copyright (C) 1987, 88, 92, 94-95, 97-98, 1999
3    Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* This program reads the machine description for the compiler target machine
24    and produces a file containing these things:
25
26    1. An array of `struct insn_data', which is indexed by insn code number,
27    which contains:
28
29      a. `name' is the name for that pattern.  Nameless patterns are
30      given a name.
31
32      b. `output' hold either the output template, an array of output
33      templates, or an output function.
34
35      c. `genfun' is the function to generate a body for that pattern,
36      given operands as arguments.
37
38      d. `n_operands' is the number of distinct operands in the pattern
39      for that insn,
40
41      e. `n_dups' is the number of match_dup's that appear in the insn's
42      pattern.  This says how many elements of `recog_data.dup_loc' are
43      significant after an insn has been recognized.
44
45      f. `n_alternatives' is the number of alternatives in the constraints
46      of each pattern.
47
48      g. `output_format' tells what type of thing `output' is.
49
50      h. `operand' is the base of an array of operand data for the insn.
51
52    2. An array of `struct insn_operand data', used by `operand' above.
53
54      a. `predicate', an int-valued function, is the match_operand predicate
55      for this operand.
56
57      b. `constraint' is the constraint for this operand.  This exists
58      only if register constraints appear in match_operand rtx's.
59
60      c. `address_p' indicates that the operand appears within ADDRESS
61      rtx's.  This exists only if there are *no* register constraints
62      in the match_operand rtx's.
63
64      d. `mode' is the machine mode that that operand is supposed to have.
65
66      e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
67
68   The code number of an insn is simply its position in the machine
69   description; code numbers are assigned sequentially to entries in
70   the description, starting with code number 0.
71
72   Thus, the following entry in the machine description
73
74     (define_insn "clrdf"
75       [(set (match_operand:DF 0 "general_operand" "")
76             (const_int 0))]
77       ""
78       "clrd %0")
79
80   assuming it is the 25th entry present, would cause
81   insn_data[24].template to be "clrd %0", and
82   insn_data[24].n_operands to be 1.  */
83 \f
84 #include "hconfig.h"
85 #include "system.h"
86 #include "rtl.h"
87 #include "obstack.h"
88 #include "errors.h"
89
90 /* No instruction can have more operands than this.  Sorry for this
91    arbitrary limit, but what machine will have an instruction with
92    this many operands?  */
93
94 #define MAX_MAX_OPERANDS 40
95
96 static struct obstack obstack;
97 struct obstack *rtl_obstack = &obstack;
98
99 #define obstack_chunk_alloc xmalloc
100 #define obstack_chunk_free free
101
102 static int n_occurrences PROTO((int, char *));
103
104 /* insns in the machine description are assigned sequential code numbers
105    that are used by insn-recog.c (produced by genrecog) to communicate
106    to insn-output.c (produced by this program).  */
107
108 static int next_code_number;
109
110 /* This counts all definitions in the md file,
111    for the sake of error messages.  */
112
113 static int next_index_number;
114
115 /* This counts all operands used in the md file.  The first is null.  */
116
117 static int next_operand_number = 1;
118
119 /* Record in this chain all information about the operands we will output.  */
120
121 struct operand_data
122 {
123   struct operand_data *next;
124   int index;
125   const char *predicate;
126   const char *constraint;
127   enum machine_mode mode;
128   unsigned char n_alternatives;
129   char address_p;
130   char strict_low;
131   char seen;
132 };
133
134 /* Begin with a null operand at index 0.  */
135
136 static struct operand_data null_operand =
137 {
138   0, 0, "", "", VOIDmode, 0, 0, 0, 0
139 };
140
141 static struct operand_data *odata = &null_operand;
142 static struct operand_data **odata_end = &null_operand.next;
143
144 /* Must match the constants in recog.h.  */
145
146 #define INSN_OUTPUT_FORMAT_NONE         0       /* abort */
147 #define INSN_OUTPUT_FORMAT_SINGLE       1       /* const char * */
148 #define INSN_OUTPUT_FORMAT_MULTI        2       /* const char * const * */
149 #define INSN_OUTPUT_FORMAT_FUNCTION     3       /* const char * (*)(...) */
150
151 /* Record in this chain all information that we will output,
152    associated with the code number of the insn.  */
153
154 struct data
155 {
156   struct data *next;
157   const char *name;
158   const char *template;
159   int code_number;
160   int index_number;
161   int n_operands;               /* Number of operands this insn recognizes */
162   int n_dups;                   /* Number times match_dup appears in pattern */
163   int n_alternatives;           /* Number of alternatives in each constraint */
164   int operand_number;           /* Operand index in the big array.  */
165   int output_format;            /* INSN_OUTPUT_FORMAT_*.  */
166   struct operand_data operand[MAX_MAX_OPERANDS];
167 };
168
169 /* This variable points to the first link in the insn chain.  */
170
171 static struct data *idata, **idata_end = &idata;
172
173 /* Nonzero if any match_operand has a constraint string; implies that
174    REGISTER_CONSTRAINTS will be defined for this machine description.  */
175
176 static int have_constraints;
177
178 \f
179 static void output_prologue PROTO((void));
180 static void output_predicate_decls PROTO((void));
181 static void output_operand_data PROTO((void));
182 static void output_insn_data PROTO((void));
183 static void output_get_insn_name PROTO((void));
184 static void scan_operands PROTO((struct data *, rtx, int, int));
185 static int compare_operands PROTO((struct operand_data *,
186                                    struct operand_data *));
187 static void place_operands PROTO((struct data *));
188 static void process_template PROTO((struct data *, char *));
189 static void validate_insn_alternatives PROTO((struct data *));
190 static void gen_insn PROTO((rtx));
191 static void gen_peephole PROTO((rtx));
192 static void gen_expand PROTO((rtx));
193 static void gen_split PROTO((rtx));
194 static int n_occurrences PROTO((int, char *));
195 \f
196 const char *
197 get_insn_name (index)
198      int index;
199 {
200   static char buf[100];
201
202   struct data *i, *last_named = NULL;
203   for (i = idata; i ; i = i->next)
204     {
205       if (i->index_number == index)
206         return i->name;
207       if (i->name)
208         last_named = i;
209     }
210
211   if (last_named)
212     sprintf(buf, "%s+%d", last_named->name, index - last_named->index_number);
213   else
214     sprintf(buf, "insn %d", index);
215
216   return buf;
217 }
218
219 static void
220 output_prologue ()
221 {
222   printf ("/* Generated automatically by the program `genoutput'\n\
223 from the machine description file `md'.  */\n\n");
224
225   printf ("#define NO_MD_PROTOTYPES\n");
226   printf ("#include \"config.h\"\n");
227   printf ("#include \"system.h\"\n");
228   printf ("#include \"flags.h\"\n");
229   printf ("#include \"rtl.h\"\n");
230   printf ("#include \"function.h\"\n");
231   printf ("#include \"regs.h\"\n");
232   printf ("#include \"hard-reg-set.h\"\n");
233   printf ("#include \"real.h\"\n");
234   printf ("#include \"insn-config.h\"\n\n");
235   printf ("#include \"conditions.h\"\n");
236   printf ("#include \"insn-flags.h\"\n");
237   printf ("#include \"insn-attr.h\"\n\n");
238   printf ("#include \"insn-codes.h\"\n\n");
239   printf ("#include \"recog.h\"\n\n");
240
241   printf ("#include \"output.h\"\n");
242 }
243
244
245 /* We need to define all predicates used.  Keep a list of those we
246    have defined so far.  There normally aren't very many predicates
247    used, so a linked list should be fast enough.  */
248
249 static void
250 output_predicate_decls ()
251 {
252   struct predicate { const char *name; struct predicate *next; } *predicates = 0;
253   register struct operand_data *d;
254   struct predicate *p;
255
256   for (d = odata; d; d = d->next)
257     if (d->predicate && d->predicate[0])
258       {
259         for (p = predicates; p; p = p->next)
260           if (strcmp (p->name, d->predicate) == 0)
261             break;
262
263         if (p == 0)
264           {
265             printf ("extern int %s PROTO ((rtx, enum machine_mode));\n",
266                     d->predicate);
267             p = (struct predicate *) alloca (sizeof (struct predicate));
268             p->name = d->predicate;
269             p->next = predicates;
270             predicates = p;
271           }
272       }
273
274   printf ("\n\n");
275 }
276
277 static void
278 output_operand_data ()
279 {
280   register struct operand_data *d;
281
282   printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
283
284   for (d = odata; d; d = d->next)
285     {
286       printf ("  {\n");
287
288       printf ("    %s,\n",
289               d->predicate && d->predicate[0] ? d->predicate : "0");
290
291       if (have_constraints)
292         {
293           printf ("    \"%s\",\n",
294                   d->constraint ? d->constraint : "");
295         }
296
297       printf ("    %smode,\n", GET_MODE_NAME (d->mode));
298
299       if (! have_constraints)
300         printf ("    %d,\n", d->address_p);
301
302       printf ("    %d\n", d->strict_low);
303
304       printf("  },\n");
305     }
306   printf("};\n\n\n");
307 }
308
309 static void
310 output_insn_data ()
311 {
312   register struct data *d;
313   int name_offset = 0;
314   int next_name_offset;
315   const char * last_name = 0;
316   const char * next_name = 0;
317   register struct data *n;
318
319   for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
320     if (n->name)
321       {
322         next_name = n->name;
323         break;
324       }
325
326   printf ("\nconst struct insn_data insn_data[] = \n{\n");
327
328   for (d = idata; d; d = d->next)
329     {
330       printf ("  {\n");
331
332       if (d->name)
333         {
334           printf ("    \"%s\",\n", d->name);
335           name_offset = 0;
336           last_name = d->name;
337           next_name = 0;
338           for (n = d->next, next_name_offset = 1; n;
339                n = n->next, next_name_offset++)
340             {
341               if (n->name)
342                 {
343                   next_name = n->name;
344                   break;
345                 }
346             }
347         }
348       else
349         {
350           name_offset++;
351           if (next_name && (last_name == 0
352                             || name_offset > next_name_offset / 2))
353             printf ("    \"%s-%d\",\n", next_name,
354                     next_name_offset - name_offset);
355           else
356             printf ("    \"%s+%d\",\n", last_name, name_offset);
357         }
358
359       switch (d->output_format)
360         {
361         case INSN_OUTPUT_FORMAT_NONE:
362           printf ("    0,\n");
363           break;
364         case INSN_OUTPUT_FORMAT_SINGLE:
365           printf ("    \"%s\",\n", d->template);
366           break;
367         case INSN_OUTPUT_FORMAT_MULTI:
368         case INSN_OUTPUT_FORMAT_FUNCTION:
369           printf ("    output_%d,\n", d->code_number);
370           break;
371         default:
372           abort ();
373         }
374
375       if (d->name && d->name[0] != '*')
376         printf ("    gen_%s,\n", d->name);
377       else
378         printf ("    0,\n");
379
380       printf ("    &operand_data[%d],\n", d->operand_number);
381       printf ("    %d,\n", d->n_operands);
382       printf ("    %d,\n", d->n_dups);
383       printf ("    %d,\n", d->n_alternatives);
384       printf ("    %d\n", d->output_format);
385
386       printf("  },\n");
387     }
388   printf ("};\n\n\n");
389 }
390
391 static void
392 output_get_insn_name ()
393 {
394   printf ("const char *\n");
395   printf ("get_insn_name (code)\n");
396   printf ("     int code;\n");
397   printf ("{\n");
398   printf ("  return insn_data[code].name;\n");
399   printf ("}\n");
400 }
401
402 \f
403 /* Stores in max_opno the largest operand number present in `part', if
404    that is larger than the previous value of max_opno, and the rest of
405    the operand data into `d->operand[i]'.
406
407    THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
408    THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART.  */
409
410 static int max_opno;
411 static int num_dups;
412
413 static void
414 scan_operands (d, part, this_address_p, this_strict_low)
415      struct data *d;
416      rtx part;
417      int this_address_p;
418      int this_strict_low;
419 {
420   register int i, j;
421   register const char *format_ptr;
422   int opno;
423
424   if (part == 0)
425     return;
426
427   switch (GET_CODE (part))
428     {
429     case MATCH_OPERAND:
430       opno = XINT (part, 0);
431       if (opno > max_opno)
432         max_opno = opno;
433       if (max_opno >= MAX_MAX_OPERANDS)
434         {
435           error ("Too many operands (%d) in definition %s.\n",
436                  max_opno + 1, get_insn_name (next_index_number));
437           return;
438         }
439       if (d->operand[opno].seen)
440         error ("Definition %s specified operand number %d more than once.\n",
441                get_insn_name (next_index_number), opno);
442       d->operand[opno].seen = 1;
443       d->operand[opno].mode = GET_MODE (part);
444       d->operand[opno].strict_low = this_strict_low;
445       d->operand[opno].predicate = XSTR (part, 1);
446       d->operand[opno].constraint = XSTR (part, 2);
447       if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
448         {
449           d->operand[opno].n_alternatives
450             = n_occurrences (',', XSTR (part, 2)) + 1;
451           have_constraints = 1;
452         }
453       d->operand[opno].address_p = this_address_p;
454       return;
455
456     case MATCH_SCRATCH:
457       opno = XINT (part, 0);
458       if (opno > max_opno)
459         max_opno = opno;
460       if (max_opno >= MAX_MAX_OPERANDS)
461         {
462           error ("Too many operands (%d) in definition %s.\n",
463                  max_opno + 1, get_insn_name (next_index_number));
464           return;
465         }
466       if (d->operand[opno].seen)
467         error ("Definition %s specified operand number %d more than once.\n",
468                get_insn_name (next_index_number), opno);
469       d->operand[opno].seen = 1;
470       d->operand[opno].mode = GET_MODE (part);
471       d->operand[opno].strict_low = 0;
472       d->operand[opno].predicate = "scratch_operand";
473       d->operand[opno].constraint = XSTR (part, 1);
474       if (XSTR (part, 1) != 0 && *XSTR (part, 1) != 0)
475         {
476           d->operand[opno].n_alternatives
477             = n_occurrences (',', XSTR (part, 1)) + 1;
478           have_constraints = 1;
479         }
480       d->operand[opno].address_p = 0;
481       return;
482
483     case MATCH_OPERATOR:
484     case MATCH_PARALLEL:
485       opno = XINT (part, 0);
486       if (opno > max_opno)
487         max_opno = opno;
488       if (max_opno >= MAX_MAX_OPERANDS)
489         {
490           error ("Too many operands (%d) in definition %s.\n",
491                  max_opno + 1, get_insn_name (next_index_number));
492           return;
493         }
494       if (d->operand[opno].seen)
495         error ("Definition %s specified operand number %d more than once.\n",
496                get_insn_name (next_index_number), opno);
497       d->operand[opno].seen = 1;
498       d->operand[opno].mode = GET_MODE (part);
499       d->operand[opno].strict_low = 0;
500       d->operand[opno].predicate = XSTR (part, 1);
501       d->operand[opno].constraint = 0;
502       d->operand[opno].address_p = 0;
503       for (i = 0; i < XVECLEN (part, 2); i++)
504         scan_operands (d, XVECEXP (part, 2, i), 0, 0);
505       return;
506
507     case MATCH_DUP:
508     case MATCH_OP_DUP:
509     case MATCH_PAR_DUP:
510       ++num_dups;
511       return;
512
513     case ADDRESS:
514       scan_operands (d, XEXP (part, 0), 1, 0);
515       return;
516
517     case STRICT_LOW_PART:
518       scan_operands (d, XEXP (part, 0), 0, 1);
519       return;
520       
521     default:
522       break;
523     }
524
525   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
526
527   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
528     switch (*format_ptr++)
529       {
530       case 'e':
531       case 'u':
532         scan_operands (d, XEXP (part, i), 0, 0);
533         break;
534       case 'E':
535         if (XVEC (part, i) != NULL)
536           for (j = 0; j < XVECLEN (part, i); j++)
537             scan_operands (d, XVECEXP (part, i, j), 0, 0);
538         break;
539       }
540 }
541
542 /* Compare two operands for content equality.  */
543
544 static int
545 compare_operands (d0, d1)
546      struct operand_data *d0, *d1;
547 {
548   const char *p0, *p1;
549
550   p0 = d0->predicate;
551   if (!p0)
552     p0 = "";
553   p1 = d1->predicate;
554   if (!p1)
555     p1 = "";
556   if (strcmp (p0, p1) != 0)
557     return 0;
558
559   if (have_constraints)
560     {
561       p0 = d0->constraint;
562       if (!p0)
563         p0 = "";
564       p1 = d1->constraint;
565       if (!p1)
566         p1 = "";
567       if (strcmp (p0, p1) != 0)
568         return 0;
569     }
570
571   if (d0->mode != d1->mode)
572     return 0;
573
574   if (!have_constraints)
575     if (d0->address_p != d1->address_p)
576       return 0;
577
578   if (d0->strict_low != d1->strict_low)
579     return 0;
580
581   return 1;
582 }
583
584 /* Scan the list of operands we've already committed to output and either
585    find a subsequence that is the same, or allocate a new one at the end.  */
586
587 static void
588 place_operands (d)
589      struct data *d;
590 {
591   struct operand_data *od, *od2;
592   int i;
593
594   if (d->n_operands == 0)
595     {
596       d->operand_number = 0;
597       return;
598     }
599
600   /* Brute force substring search.  */
601   for (od = odata, i = 0; od; od = od->next, i = 0)
602     if (compare_operands (od, &d->operand[0]))
603       {
604         od2 = od->next;
605         i = 1;
606         while (1)
607           {
608             if (i == d->n_operands)
609               goto full_match;
610             if (od2 == NULL)
611               goto partial_match;
612             if (! compare_operands (od2, &d->operand[i]))
613               break;
614             ++i, od2 = od2->next;
615           }
616       }
617
618   /* Either partial match at the end of the list, or no match.  In either
619      case, we tack on what operands are remaining to the end of the list.  */
620  partial_match:
621   d->operand_number = next_operand_number - i;
622   for (; i < d->n_operands; ++i)
623     {
624       od2 = &d->operand[i];
625       *odata_end = od2;
626       odata_end = &od2->next;
627       od2->index = next_operand_number++;
628     }
629   *odata_end = NULL;
630   return;
631
632  full_match:
633   d->operand_number = od->index;
634   return;
635 }
636
637 \f
638 /* Process an assembler template from a define_insn or a define_peephole.
639    It is either the assembler code template, a list of assembler code
640    templates, or C code to generate the assembler code template.  */
641
642 static void
643 process_template (d, template)
644     struct data *d;
645     char *template;
646 {
647   register char *cp;
648   register int i;
649
650   /* Templates starting with * contain straight code to be run.  */
651   if (template[0] == '*')
652     {
653       d->template = 0;
654       d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
655
656       printf ("\nstatic const char *output_%d PROTO ((rtx *, rtx));\n",
657               d->code_number);
658       puts ("\nstatic const char *");
659       printf ("output_%d (operands, insn)\n", d->code_number);
660       puts ("     rtx *operands ATTRIBUTE_UNUSED;");
661       puts ("     rtx insn ATTRIBUTE_UNUSED;");
662       puts ("{");
663
664       puts (template + 1);
665       puts ("}");
666     }
667
668   /* If the assembler code template starts with a @ it is a newline-separated
669      list of assembler code templates, one for each alternative.  */
670   else if (template[0] == '@')
671     {
672       d->template = 0;
673       d->output_format = INSN_OUTPUT_FORMAT_MULTI;
674
675       printf ("\nstatic const char * const output_%d[] = {\n", d->code_number);
676
677       for (i = 0, cp = &template[1]; *cp; )
678         {
679           while (*cp == '\n' || *cp == ' ' || *cp== '\t')
680             cp++;
681
682           printf ("  \"");
683           while (*cp != '\n' && *cp != '\0')
684             {
685               putchar (*cp);
686               cp++;
687             }
688
689           printf ("\",\n");
690           i++;
691         }
692
693       printf ("};\n");
694     }
695   else
696     {
697       d->template = template;
698       d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
699     }
700 }
701 \f
702 /* Check insn D for consistency in number of constraint alternatives.  */
703
704 static void
705 validate_insn_alternatives (d)
706      struct data *d;
707 {
708   register int n = 0, start;
709
710   /* Make sure all the operands have the same number of alternatives
711      in their constraints.  Let N be that number.  */
712   for (start = 0; start < d->n_operands; start++)
713     if (d->operand[start].n_alternatives > 0)
714       {
715         if (n == 0)
716           n = d->operand[start].n_alternatives;
717         else if (n != d->operand[start].n_alternatives)
718           error ("wrong number of alternatives in operand %d of insn %s",
719                  start, get_insn_name (d->index_number));
720       }
721
722   /* Record the insn's overall number of alternatives.  */
723   d->n_alternatives = n;
724 }
725 \f
726 /* Look at a define_insn just read.  Assign its code number.  Record
727    on idata the template and the number of arguments.  If the insn has
728    a hairy output action, output a function for now.  */
729
730 static void
731 gen_insn (insn)
732      rtx insn;
733 {
734   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
735   register int i;
736
737   d->code_number = next_code_number++;
738   d->index_number = next_index_number;
739   if (XSTR (insn, 0)[0])
740     d->name = XSTR (insn, 0);
741   else
742     d->name = 0;
743
744   /* Build up the list in the same order as the insns are seen
745      in the machine description.  */
746   d->next = 0;
747   *idata_end = d;
748   idata_end = &d->next;
749
750   max_opno = -1;
751   num_dups = 0;
752   memset (d->operand, 0, sizeof (d->operand));
753
754   for (i = 0; i < XVECLEN (insn, 1); i++)
755     scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
756
757   d->n_operands = max_opno + 1;
758   d->n_dups = num_dups;
759
760   validate_insn_alternatives (d);
761   place_operands (d);
762   process_template (d, XSTR (insn, 3));
763 }
764 \f
765 /* Look at a define_peephole just read.  Assign its code number.
766    Record on idata the template and the number of arguments.
767    If the insn has a hairy output action, output it now.  */
768
769 static void
770 gen_peephole (peep)
771      rtx peep;
772 {
773   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
774   register int i;
775
776   d->code_number = next_code_number++;
777   d->index_number = next_index_number;
778   d->name = 0;
779
780   /* Build up the list in the same order as the insns are seen
781      in the machine description.  */
782   d->next = 0;
783   *idata_end = d;
784   idata_end = &d->next;
785
786   max_opno = -1;
787   num_dups = 0;
788   memset (d->operand, 0, sizeof (d->operand));
789
790   /* Get the number of operands by scanning all the patterns of the
791      peephole optimizer.  But ignore all the rest of the information
792      thus obtained.  */
793   for (i = 0; i < XVECLEN (peep, 0); i++)
794     scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
795
796   d->n_operands = max_opno + 1;
797   d->n_dups = 0;
798
799   validate_insn_alternatives (d);
800   place_operands (d);
801   process_template (d, XSTR (peep, 2));
802 }
803 \f
804 /* Process a define_expand just read.  Assign its code number,
805    only for the purposes of `insn_gen_function'.  */
806
807 static void
808 gen_expand (insn)
809      rtx insn;
810 {
811   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
812   register int i;
813
814   d->code_number = next_code_number++;
815   d->index_number = next_index_number;
816   if (XSTR (insn, 0)[0])
817     d->name = XSTR (insn, 0);
818   else
819     d->name = 0;
820
821   /* Build up the list in the same order as the insns are seen
822      in the machine description.  */
823   d->next = 0;
824   *idata_end = d;
825   idata_end = &d->next;
826
827   max_opno = -1;
828   num_dups = 0;
829   memset (d->operand, 0, sizeof (d->operand));
830
831   /* Scan the operands to get the specified predicates and modes,
832      since expand_binop needs to know them.  */
833
834   if (XVEC (insn, 1))
835     for (i = 0; i < XVECLEN (insn, 1); i++)
836       scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
837
838   d->n_operands = max_opno + 1;
839   d->n_dups = num_dups;
840   d->template = 0;
841   d->output_format = INSN_OUTPUT_FORMAT_NONE;
842
843   validate_insn_alternatives (d);
844   place_operands (d);
845 }
846 \f
847 /* Process a define_split just read.  Assign its code number,
848    only for reasons of consistency and to simplify genrecog.  */
849
850 static void
851 gen_split (split)
852      rtx split;
853 {
854   register struct data *d = (struct data *) xmalloc (sizeof (struct data));
855   register int i;
856
857   d->code_number = next_code_number++;
858   d->index_number = next_index_number;
859   d->name = 0;
860
861   /* Build up the list in the same order as the insns are seen
862      in the machine description.  */
863   d->next = 0;
864   *idata_end = d;
865   idata_end = &d->next;
866
867   max_opno = -1;
868   num_dups = 0;
869   memset (d->operand, 0, sizeof (d->operand));
870
871   /* Get the number of operands by scanning all the patterns of the
872      split patterns.  But ignore all the rest of the information thus
873      obtained.  */
874   for (i = 0; i < XVECLEN (split, 0); i++)
875     scan_operands (d, XVECEXP (split, 0, i), 0, 0);
876
877   d->n_operands = max_opno + 1;
878   d->n_dups = 0;
879   d->n_alternatives = 0;
880   d->template = 0;
881   d->output_format = INSN_OUTPUT_FORMAT_NONE;
882
883   place_operands (d);
884 }
885 \f
886 PTR
887 xmalloc (size)
888   size_t size;
889 {
890   register PTR val = (PTR) malloc (size);
891
892   if (val == 0)
893     fatal ("virtual memory exhausted");
894   return val;
895 }
896
897 PTR
898 xrealloc (old, size)
899   PTR old;
900   size_t size;
901 {
902   register PTR ptr;
903   if (old)
904     ptr = (PTR) realloc (old, size);
905   else
906     ptr = (PTR) malloc (size);
907   if (!ptr)
908     fatal ("virtual memory exhausted");
909   return ptr;
910 }
911
912 extern int main PROTO ((int, char **));
913
914 int
915 main (argc, argv)
916      int argc;
917      char **argv;
918 {
919   rtx desc;
920   FILE *infile;
921   register int c;
922
923   progname = "genoutput";
924   obstack_init (rtl_obstack);
925
926   if (argc <= 1)
927     fatal ("No input file name.");
928
929   infile = fopen (argv[1], "r");
930   if (infile == 0)
931     {
932       perror (argv[1]);
933       return (FATAL_EXIT_CODE);
934     }
935
936   output_prologue ();
937   next_code_number = 0;
938   next_index_number = 0;
939   have_constraints = 0;
940
941   /* Read the machine description.  */
942
943   while (1)
944     {
945       c = read_skip_spaces (infile);
946       if (c == EOF)
947         break;
948       ungetc (c, infile);
949
950       desc = read_rtx (infile);
951       if (GET_CODE (desc) == DEFINE_INSN)
952         gen_insn (desc);
953       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
954         gen_peephole (desc);
955       if (GET_CODE (desc) == DEFINE_EXPAND)
956         gen_expand (desc);
957       if (GET_CODE (desc) == DEFINE_SPLIT
958           || GET_CODE (desc) == DEFINE_PEEPHOLE2)
959         gen_split (desc);
960       next_index_number++;
961     }
962
963   printf("\n\n");
964   output_predicate_decls ();
965   output_operand_data ();
966   output_insn_data ();
967   output_get_insn_name ();
968
969   fflush (stdout);
970   return (ferror (stdout) != 0 || have_error
971         ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
972 }
973
974 static int
975 n_occurrences (c, s)
976      int c;
977      char *s;
978 {
979   int n = 0;
980   while (*s)
981     n += (*s++ == c);
982   return n;
983 }