Merge branch 'vendor/GCC50' - gcc 5.0 snapshot 1 FEB 2015
[dragonfly.git] / contrib / gcc-5.0 / gcc / genoutput.c
1 /* Generate code from to output assembler insns as recognized from rtl.
2    Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20
21 /* This program reads the machine description for the compiler target machine
22    and produces a file containing these things:
23
24    1. An array of `struct insn_data_d', which is indexed by insn code number,
25    which contains:
26
27      a. `name' is the name for that pattern.  Nameless patterns are
28      given a name.
29
30      b. `output' hold either the output template, an array of output
31      templates, or an output function.
32
33      c. `genfun' is the function to generate a body for that pattern,
34      given operands as arguments.
35
36      d. `n_operands' is the number of distinct operands in the pattern
37      for that insn,
38
39      e. `n_dups' is the number of match_dup's that appear in the insn's
40      pattern.  This says how many elements of `recog_data.dup_loc' are
41      significant after an insn has been recognized.
42
43      f. `n_alternatives' is the number of alternatives in the constraints
44      of each pattern.
45
46      g. `output_format' tells what type of thing `output' is.
47
48      h. `operand' is the base of an array of operand data for the insn.
49
50    2. An array of `struct insn_operand data', used by `operand' above.
51
52      a. `predicate', an int-valued function, is the match_operand predicate
53      for this operand.
54
55      b. `constraint' is the constraint for this operand.
56
57      c. `address_p' indicates that the operand appears within ADDRESS
58      rtx's.
59
60      d. `mode' is the machine mode that that operand is supposed to have.
61
62      e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
63
64      f. `eliminable', is nonzero for operands that are matched normally by
65      MATCH_OPERAND; it is zero for operands that should not be changed during
66      register elimination such as MATCH_OPERATORs.
67
68      g. `allows_mem', is true for operands that accept MEM rtxes.
69
70   The code number of an insn is simply its position in the machine
71   description; code numbers are assigned sequentially to entries in
72   the description, starting with code number 0.
73
74   Thus, the following entry in the machine description
75
76     (define_insn "clrdf"
77       [(set (match_operand:DF 0 "general_operand" "")
78             (const_int 0))]
79       ""
80       "clrd %0")
81
82   assuming it is the 25th entry present, would cause
83   insn_data[24].template to be "clrd %0", and
84   insn_data[24].n_operands to be 1.  */
85 \f
86 #include "bconfig.h"
87 #include "system.h"
88 #include "coretypes.h"
89 #include "tm.h"
90 #include "rtl.h"
91 #include "errors.h"
92 #include "read-md.h"
93 #include "gensupport.h"
94
95 /* No instruction can have more operands than this.  Sorry for this
96    arbitrary limit, but what machine will have an instruction with
97    this many operands?  */
98
99 #define MAX_MAX_OPERANDS 40
100
101 static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
102
103 static int n_occurrences                (int, const char *);
104 static const char *strip_whitespace     (const char *);
105
106 /* insns in the machine description are assigned sequential code numbers
107    that are used by insn-recog.c (produced by genrecog) to communicate
108    to insn-output.c (produced by this program).  */
109
110 static int next_code_number;
111
112 /* This counts all definitions in the md file,
113    for the sake of error messages.  */
114
115 static int next_index_number;
116
117 /* This counts all operands used in the md file.  The first is null.  */
118
119 static int next_operand_number = 1;
120
121 /* Record in this chain all information about the operands we will output.  */
122
123 struct operand_data
124 {
125   struct operand_data *next;
126   int index;
127   const char *predicate;
128   const char *constraint;
129   machine_mode mode;
130   unsigned char n_alternatives;
131   char address_p;
132   char strict_low;
133   char eliminable;
134   char seen;
135 };
136
137 /* Begin with a null operand at index 0.  */
138
139 static struct operand_data null_operand =
140 {
141   0, 0, "", "", VOIDmode, 0, 0, 0, 0, 0
142 };
143
144 static struct operand_data *odata = &null_operand;
145 static struct operand_data **odata_end = &null_operand.next;
146
147 /* Must match the constants in recog.h.  */
148
149 #define INSN_OUTPUT_FORMAT_NONE         0       /* abort */
150 #define INSN_OUTPUT_FORMAT_SINGLE       1       /* const char * */
151 #define INSN_OUTPUT_FORMAT_MULTI        2       /* const char * const * */
152 #define INSN_OUTPUT_FORMAT_FUNCTION     3       /* const char * (*)(...) */
153
154 /* Record in this chain all information that we will output,
155    associated with the code number of the insn.  */
156
157 struct data
158 {
159   struct data *next;
160   const char *name;
161   const char *template_code;
162   int code_number;
163   int index_number;
164   const char *filename;
165   int lineno;
166   int n_generator_args;         /* Number of arguments passed to generator */
167   int n_operands;               /* Number of operands this insn recognizes */
168   int n_dups;                   /* Number times match_dup appears in pattern */
169   int n_alternatives;           /* Number of alternatives in each constraint */
170   int operand_number;           /* Operand index in the big array.  */
171   int output_format;            /* INSN_OUTPUT_FORMAT_*.  */
172   struct operand_data operand[MAX_MAX_OPERANDS];
173 };
174
175 /* A dummy insn, for CODE_FOR_nothing.  */
176 static struct data nothing;
177
178 /* This variable points to the first link in the insn chain.  */
179 static struct data *idata = &nothing;
180
181 /* This variable points to the end of the insn chain.  This is where
182    everything relevant from the machien description is appended to.  */
183 static struct data **idata_end = &nothing.next;
184
185 \f
186 static void output_prologue (void);
187 static void output_operand_data (void);
188 static void output_insn_data (void);
189 static void output_get_insn_name (void);
190 static void scan_operands (struct data *, rtx, int, int);
191 static int compare_operands (struct operand_data *,
192                              struct operand_data *);
193 static void place_operands (struct data *);
194 static void process_template (struct data *, const char *);
195 static void validate_insn_alternatives (struct data *);
196 static void validate_insn_operands (struct data *);
197 static void gen_insn (rtx, int);
198 static void gen_peephole (rtx, int);
199 static void gen_expand (rtx, int);
200 static void gen_split (rtx, int);
201
202 struct constraint_data
203 {
204   struct constraint_data *next_this_letter;
205   int lineno;
206   unsigned int namelen;
207   const char name[1];
208 };
209
210 /* All machine-independent constraint characters (except digits) that
211    are handled outside the define*_constraint mechanism.  */
212 static const char indep_constraints[] = ",=+%*?!^$#&g";
213
214 static struct constraint_data *
215 constraints_by_letter_table[1 << CHAR_BIT];
216
217 static int mdep_constraint_len (const char *, int, int);
218 static void note_constraint (rtx, int);
219 \f
220 static void
221 output_prologue (void)
222 {
223   printf ("/* Generated automatically by the program `genoutput'\n\
224    from the machine description file `md'.  */\n\n");
225
226   printf ("#include \"config.h\"\n");
227   printf ("#include \"system.h\"\n");
228   printf ("#include \"coretypes.h\"\n");
229   printf ("#include \"tm.h\"\n");
230   printf ("#include \"flags.h\"\n");
231   printf ("#include \"ggc.h\"\n");
232   printf ("#include \"hash-set.h\"\n");
233   printf ("#include \"machmode.h\"\n");
234   printf ("#include \"vec.h\"\n");
235   printf ("#include \"double-int.h\"\n");
236   printf ("#include \"input.h\"\n");
237   printf ("#include \"alias.h\"\n");
238   printf ("#include \"symtab.h\"\n");
239   printf ("#include \"wide-int.h\"\n");
240   printf ("#include \"inchash.h\"\n");
241   printf ("#include \"tree.h\"\n");
242   printf ("#include \"varasm.h\"\n");
243   printf ("#include \"stor-layout.h\"\n");
244   printf ("#include \"calls.h\"\n");
245   printf ("#include \"rtl.h\"\n");
246   printf ("#include \"hashtab.h\"\n");
247   printf ("#include \"hard-reg-set.h\"\n");
248   printf ("#include \"function.h\"\n");
249   printf ("#include \"statistics.h\"\n");
250   printf ("#include \"real.h\"\n");
251   printf ("#include \"fixed-value.h\"\n");
252   printf ("#include \"insn-config.h\"\n");
253   printf ("#include \"expmed.h\"\n");
254   printf ("#include \"dojump.h\"\n");
255   printf ("#include \"explow.h\"\n");
256   printf ("#include \"emit-rtl.h\"\n");
257   printf ("#include \"stmt.h\"\n");
258   printf ("#include \"expr.h\"\n");
259   printf ("#include \"insn-codes.h\"\n");
260   printf ("#include \"tm_p.h\"\n");
261   printf ("#include \"regs.h\"\n");
262   printf ("#include \"conditions.h\"\n");
263   printf ("#include \"insn-attr.h\"\n\n");
264   printf ("#include \"recog.h\"\n\n");
265   printf ("#include \"diagnostic-core.h\"\n");
266   printf ("#include \"output.h\"\n");
267   printf ("#include \"target.h\"\n");
268   printf ("#include \"tm-constrs.h\"\n");
269   printf ("#include \"predict.h\"\n");
270 }
271
272 static void
273 output_operand_data (void)
274 {
275   struct operand_data *d;
276
277   printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
278
279   for (d = odata; d; d = d->next)
280     {
281       struct pred_data *pred;
282
283       printf ("  {\n");
284
285       printf ("    %s,\n",
286               d->predicate && d->predicate[0] ? d->predicate : "0");
287
288       printf ("    \"%s\",\n", d->constraint ? d->constraint : "");
289
290       printf ("    %smode,\n", GET_MODE_NAME (d->mode));
291
292       printf ("    %d,\n", d->strict_low);
293
294       printf ("    %d,\n", d->constraint == NULL ? 1 : 0);
295
296       printf ("    %d,\n", d->eliminable);
297
298       pred = NULL;
299       if (d->predicate)
300         pred = lookup_predicate (d->predicate);
301       printf ("    %d\n", pred && pred->codes[MEM]);
302
303       printf ("  },\n");
304     }
305   printf ("};\n\n\n");
306 }
307
308 static void
309 output_insn_data (void)
310 {
311   struct data *d;
312   int name_offset = 0;
313   int next_name_offset;
314   const char * last_name = 0;
315   const char * next_name = 0;
316   struct data *n;
317
318   for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
319     if (n->name)
320       {
321         next_name = n->name;
322         break;
323       }
324
325   printf ("#if GCC_VERSION >= 2007\n__extension__\n#endif\n");
326   printf ("\nconst struct insn_data_d insn_data[] = \n{\n");
327
328   for (d = idata; d; d = d->next)
329     {
330       printf ("  /* %s:%d */\n", d->filename, d->lineno);
331       printf ("  {\n");
332
333       if (d->name)
334         {
335           printf ("    \"%s\",\n", d->name);
336           name_offset = 0;
337           last_name = d->name;
338           next_name = 0;
339           for (n = d->next, next_name_offset = 1; n;
340                n = n->next, next_name_offset++)
341             {
342               if (n->name)
343                 {
344                   next_name = n->name;
345                   break;
346                 }
347             }
348         }
349       else
350         {
351           name_offset++;
352           if (next_name && (last_name == 0
353                             || name_offset > next_name_offset / 2))
354             printf ("    \"%s-%d\",\n", next_name,
355                     next_name_offset - name_offset);
356           else
357             printf ("    \"%s+%d\",\n", last_name, name_offset);
358         }
359
360       switch (d->output_format)
361         {
362         case INSN_OUTPUT_FORMAT_NONE:
363           printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
364           printf ("    { 0 },\n");
365           printf ("#else\n");
366           printf ("    { 0, 0, 0 },\n");
367           printf ("#endif\n");
368           break;
369         case INSN_OUTPUT_FORMAT_SINGLE:
370           {
371             const char *p = d->template_code;
372             char prev = 0;
373
374             printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
375             printf ("    { .single =\n");
376             printf ("#else\n");
377             printf ("    {\n");
378             printf ("#endif\n");
379             printf ("    \"");
380             while (*p)
381               {
382                 if (IS_VSPACE (*p) && prev != '\\')
383                   {
384                     /* Preserve two consecutive \n's or \r's, but treat \r\n
385                        as a single newline.  */
386                     if (*p == '\n' && prev != '\r')
387                       printf ("\\n\\\n");
388                   }
389                 else
390                   putchar (*p);
391                 prev = *p;
392                 ++p;
393               }
394             printf ("\",\n");
395             printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
396             printf ("    },\n");
397             printf ("#else\n");
398             printf ("    0, 0 },\n");
399             printf ("#endif\n");
400           }
401           break;
402         case INSN_OUTPUT_FORMAT_MULTI:
403           printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
404           printf ("    { .multi = output_%d },\n", d->code_number);
405           printf ("#else\n");
406           printf ("    { 0, output_%d, 0 },\n", d->code_number);
407           printf ("#endif\n");
408           break;
409         case INSN_OUTPUT_FORMAT_FUNCTION:
410           printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
411           printf ("    { .function = output_%d },\n", d->code_number);
412           printf ("#else\n");
413           printf ("    { 0, 0, output_%d },\n", d->code_number);
414           printf ("#endif\n");
415           break;
416         default:
417           gcc_unreachable ();
418         }
419
420       if (d->name && d->name[0] != '*')
421         printf ("    { (insn_gen_fn::stored_funcptr) gen_%s },\n", d->name);
422       else
423         printf ("    { 0 },\n");
424
425       printf ("    &operand_data[%d],\n", d->operand_number);
426       printf ("    %d,\n", d->n_generator_args);
427       printf ("    %d,\n", d->n_operands);
428       printf ("    %d,\n", d->n_dups);
429       printf ("    %d,\n", d->n_alternatives);
430       printf ("    %d\n", d->output_format);
431
432       printf ("  },\n");
433     }
434   printf ("};\n\n\n");
435 }
436
437 static void
438 output_get_insn_name (void)
439 {
440   printf ("const char *\n");
441   printf ("get_insn_name (int code)\n");
442   printf ("{\n");
443   printf ("  if (code == NOOP_MOVE_INSN_CODE)\n");
444   printf ("    return \"NOOP_MOVE\";\n");
445   printf ("  else\n");
446   printf ("    return insn_data[code].name;\n");
447   printf ("}\n");
448 }
449
450 \f
451 /* Stores the operand data into `d->operand[i]'.
452
453    THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
454    THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART.  */
455
456 static void
457 scan_operands (struct data *d, rtx part, int this_address_p,
458                int this_strict_low)
459 {
460   int i, j;
461   const char *format_ptr;
462   int opno;
463
464   if (part == 0)
465     return;
466
467   switch (GET_CODE (part))
468     {
469     case MATCH_OPERAND:
470       opno = XINT (part, 0);
471       if (opno >= MAX_MAX_OPERANDS)
472         {
473           error_with_line (d->lineno, "maximum number of operands exceeded");
474           return;
475         }
476       if (d->operand[opno].seen)
477         error_with_line (d->lineno, "repeated operand number %d\n", opno);
478
479       d->operand[opno].seen = 1;
480       d->operand[opno].mode = GET_MODE (part);
481       d->operand[opno].strict_low = this_strict_low;
482       d->operand[opno].predicate = XSTR (part, 1);
483       d->operand[opno].constraint = strip_whitespace (XSTR (part, 2));
484       d->operand[opno].n_alternatives
485         = n_occurrences (',', d->operand[opno].constraint) + 1;
486       d->operand[opno].address_p = this_address_p;
487       d->operand[opno].eliminable = 1;
488       return;
489
490     case MATCH_SCRATCH:
491       opno = XINT (part, 0);
492       if (opno >= MAX_MAX_OPERANDS)
493         {
494           error_with_line (d->lineno, "maximum number of operands exceeded");
495           return;
496         }
497       if (d->operand[opno].seen)
498         error_with_line (d->lineno, "repeated operand number %d\n", opno);
499
500       d->operand[opno].seen = 1;
501       d->operand[opno].mode = GET_MODE (part);
502       d->operand[opno].strict_low = 0;
503       d->operand[opno].predicate = "scratch_operand";
504       d->operand[opno].constraint = strip_whitespace (XSTR (part, 1));
505       d->operand[opno].n_alternatives
506         = n_occurrences (',', d->operand[opno].constraint) + 1;
507       d->operand[opno].address_p = 0;
508       d->operand[opno].eliminable = 0;
509       return;
510
511     case MATCH_OPERATOR:
512     case MATCH_PARALLEL:
513       opno = XINT (part, 0);
514       if (opno >= MAX_MAX_OPERANDS)
515         {
516           error_with_line (d->lineno, "maximum number of operands exceeded");
517           return;
518         }
519       if (d->operand[opno].seen)
520         error_with_line (d->lineno, "repeated operand number %d\n", opno);
521
522       d->operand[opno].seen = 1;
523       d->operand[opno].mode = GET_MODE (part);
524       d->operand[opno].strict_low = 0;
525       d->operand[opno].predicate = XSTR (part, 1);
526       d->operand[opno].constraint = 0;
527       d->operand[opno].address_p = 0;
528       d->operand[opno].eliminable = 0;
529       for (i = 0; i < XVECLEN (part, 2); i++)
530         scan_operands (d, XVECEXP (part, 2, i), 0, 0);
531       return;
532
533     case STRICT_LOW_PART:
534       scan_operands (d, XEXP (part, 0), 0, 1);
535       return;
536
537     default:
538       break;
539     }
540
541   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
542
543   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
544     switch (*format_ptr++)
545       {
546       case 'e':
547       case 'u':
548         scan_operands (d, XEXP (part, i), 0, 0);
549         break;
550       case 'E':
551         if (XVEC (part, i) != NULL)
552           for (j = 0; j < XVECLEN (part, i); j++)
553             scan_operands (d, XVECEXP (part, i, j), 0, 0);
554         break;
555       }
556 }
557
558 /* Compare two operands for content equality.  */
559
560 static int
561 compare_operands (struct operand_data *d0, struct operand_data *d1)
562 {
563   const char *p0, *p1;
564
565   p0 = d0->predicate;
566   if (!p0)
567     p0 = "";
568   p1 = d1->predicate;
569   if (!p1)
570     p1 = "";
571   if (strcmp (p0, p1) != 0)
572     return 0;
573
574   p0 = d0->constraint;
575   if (!p0)
576     p0 = "";
577   p1 = d1->constraint;
578   if (!p1)
579     p1 = "";
580   if (strcmp (p0, p1) != 0)
581     return 0;
582
583   if (d0->mode != d1->mode)
584     return 0;
585
586   if (d0->strict_low != d1->strict_low)
587     return 0;
588
589   if (d0->eliminable != d1->eliminable)
590     return 0;
591
592   return 1;
593 }
594
595 /* Scan the list of operands we've already committed to output and either
596    find a subsequence that is the same, or allocate a new one at the end.  */
597
598 static void
599 place_operands (struct data *d)
600 {
601   struct operand_data *od, *od2;
602   int i;
603
604   if (d->n_operands == 0)
605     {
606       d->operand_number = 0;
607       return;
608     }
609
610   /* Brute force substring search.  */
611   for (od = odata, i = 0; od; od = od->next, i = 0)
612     if (compare_operands (od, &d->operand[0]))
613       {
614         od2 = od->next;
615         i = 1;
616         while (1)
617           {
618             if (i == d->n_operands)
619               goto full_match;
620             if (od2 == NULL)
621               goto partial_match;
622             if (! compare_operands (od2, &d->operand[i]))
623               break;
624             ++i, od2 = od2->next;
625           }
626       }
627
628   /* Either partial match at the end of the list, or no match.  In either
629      case, we tack on what operands are remaining to the end of the list.  */
630  partial_match:
631   d->operand_number = next_operand_number - i;
632   for (; i < d->n_operands; ++i)
633     {
634       od2 = &d->operand[i];
635       *odata_end = od2;
636       odata_end = &od2->next;
637       od2->index = next_operand_number++;
638     }
639   *odata_end = NULL;
640   return;
641
642  full_match:
643   d->operand_number = od->index;
644   return;
645 }
646
647 \f
648 /* Process an assembler template from a define_insn or a define_peephole.
649    It is either the assembler code template, a list of assembler code
650    templates, or C code to generate the assembler code template.  */
651
652 static void
653 process_template (struct data *d, const char *template_code)
654 {
655   const char *cp;
656   int i;
657
658   /* Templates starting with * contain straight code to be run.  */
659   if (template_code[0] == '*')
660     {
661       d->template_code = 0;
662       d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
663
664       puts ("\nstatic const char *");
665       printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n",
666               d->code_number);
667       puts ("{");
668       print_md_ptr_loc (template_code);
669       puts (template_code + 1);
670       puts ("}");
671     }
672
673   /* If the assembler code template starts with a @ it is a newline-separated
674      list of assembler code templates, one for each alternative.  */
675   else if (template_code[0] == '@')
676     {
677       int found_star = 0;
678
679       for (cp = &template_code[1]; *cp; )
680         {
681           while (ISSPACE (*cp))
682             cp++;
683           if (*cp == '*')
684             found_star = 1;
685           while (!IS_VSPACE (*cp) && *cp != '\0')
686             ++cp;
687         }
688       d->template_code = 0;
689       if (found_star)
690         {
691           d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
692           puts ("\nstatic const char *");
693           printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
694                   "rtx_insn *insn ATTRIBUTE_UNUSED)\n", d->code_number);
695           puts ("{");
696           puts ("  switch (which_alternative)\n    {");
697         }
698       else
699         {
700           d->output_format = INSN_OUTPUT_FORMAT_MULTI;
701           printf ("\nstatic const char * const output_%d[] = {\n",
702                   d->code_number);
703         }
704
705       for (i = 0, cp = &template_code[1]; *cp; )
706         {
707           const char *ep, *sp, *bp;
708
709           while (ISSPACE (*cp))
710             cp++;
711
712           bp = cp;
713           if (found_star)
714             {
715               printf ("    case %d:", i);
716               if (*cp == '*')
717                 {
718                   printf ("\n      ");
719                   cp++;
720                 }
721               else
722                 printf (" return \"");
723             }
724           else
725             printf ("  \"");
726
727           for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
728             if (!ISSPACE (*ep))
729               sp = ep + 1;
730
731           if (sp != ep)
732             message_with_line (d->lineno,
733                                "trailing whitespace in output template");
734
735           while (cp < sp)
736             {
737               putchar (*cp);
738               cp++;
739             }
740
741           if (!found_star)
742             puts ("\",");
743           else if (*bp != '*')
744             puts ("\";");
745           else
746             {
747               /* The usual action will end with a return.
748                  If there is neither break or return at the end, this is
749                  assumed to be intentional; this allows to have multiple
750                  consecutive alternatives share some code.  */
751               puts ("");
752             }
753           i++;
754         }
755       if (i == 1)
756         message_with_line (d->lineno,
757                            "'@' is redundant for output template with single alternative");
758       if (i != d->n_alternatives)
759         error_with_line (d->lineno,
760                          "wrong number of alternatives in the output template");
761
762       if (found_star)
763         puts ("      default: gcc_unreachable ();\n    }\n}");
764       else
765         printf ("};\n");
766     }
767   else
768     {
769       d->template_code = template_code;
770       d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
771     }
772 }
773 \f
774 /* Check insn D for consistency in number of constraint alternatives.  */
775
776 static void
777 validate_insn_alternatives (struct data *d)
778 {
779   int n = 0, start;
780
781   /* Make sure all the operands have the same number of alternatives
782      in their constraints.  Let N be that number.  */
783   for (start = 0; start < d->n_operands; start++)
784     if (d->operand[start].n_alternatives > 0)
785       {
786         int len, i;
787         const char *p;
788         char c;
789         int which_alternative = 0;
790         int alternative_count_unsure = 0;
791         bool seen_write = false;
792
793         for (p = d->operand[start].constraint; (c = *p); p += len)
794           {
795             if ((c == '%' || c == '=' || c == '+')
796                 && p != d->operand[start].constraint)
797               error_with_line (d->lineno,
798                                "character '%c' can only be used at the"
799                                " beginning of a constraint string", c);
800
801             if (c == '=' || c == '+')
802               seen_write = true;
803
804             /* Earlyclobber operands must always be marked write-only
805                or read/write.  */
806             if (!seen_write && c == '&')
807               error_with_line (d->lineno,
808                                "earlyclobber operands may not be"
809                                " read-only in alternative %d",
810                                which_alternative);
811
812             if (ISSPACE (c) || strchr (indep_constraints, c))
813               len = 1;
814             else if (ISDIGIT (c))
815               {
816                 const char *q = p;
817                 do
818                   q++;
819                 while (ISDIGIT (*q));
820                 len = q - p;
821               }
822             else
823               len = mdep_constraint_len (p, d->lineno, start);
824
825             if (c == ',')
826               {
827                 which_alternative++;
828                 continue;
829               }
830
831             for (i = 1; i < len; i++)
832               if (p[i] == '\0')
833                 {
834                   error_with_line (d->lineno,
835                                    "NUL in alternative %d of operand %d",
836                                    which_alternative, start);
837                   alternative_count_unsure = 1;
838                   break;
839                 }
840               else if (strchr (",#*", p[i]))
841                 {
842                   error_with_line (d->lineno,
843                                    "'%c' in alternative %d of operand %d",
844                                    p[i], which_alternative, start);
845                   alternative_count_unsure = 1;
846                 }
847           }
848         if (!alternative_count_unsure)
849           {
850             if (n == 0)
851               n = d->operand[start].n_alternatives;
852             else if (n != d->operand[start].n_alternatives)
853               error_with_line (d->lineno,
854                                "wrong number of alternatives in operand %d",
855                                start);
856           }
857       }
858
859   /* Record the insn's overall number of alternatives.  */
860   d->n_alternatives = n;
861 }
862
863 /* Verify that there are no gaps in operand numbers for INSNs.  */
864
865 static void
866 validate_insn_operands (struct data *d)
867 {
868   int i;
869
870   for (i = 0; i < d->n_operands; ++i)
871     if (d->operand[i].seen == 0)
872       error_with_line (d->lineno, "missing operand %d", i);
873 }
874
875 static void
876 validate_optab_operands (struct data *d)
877 {
878   if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
879     return;
880
881   /* Miscellaneous tests.  */
882   if (strncmp (d->name, "cstore", 6) == 0
883       && d->name[strlen (d->name) - 1] == '4'
884       && d->operand[0].mode == VOIDmode)
885     {
886       message_with_line (d->lineno, "missing mode for operand 0 of cstore");
887       have_error = 1;
888     }
889 }
890 \f
891 /* Look at a define_insn just read.  Assign its code number.  Record
892    on idata the template and the number of arguments.  If the insn has
893    a hairy output action, output a function for now.  */
894
895 static void
896 gen_insn (rtx insn, int lineno)
897 {
898   struct pattern_stats stats;
899   struct data *d = XNEW (struct data);
900   int i;
901
902   d->code_number = next_code_number;
903   d->index_number = next_index_number;
904   d->filename = read_md_filename;
905   d->lineno = lineno;
906   if (XSTR (insn, 0)[0])
907     d->name = XSTR (insn, 0);
908   else
909     d->name = 0;
910
911   /* Build up the list in the same order as the insns are seen
912      in the machine description.  */
913   d->next = 0;
914   *idata_end = d;
915   idata_end = &d->next;
916
917   memset (d->operand, 0, sizeof (d->operand));
918
919   for (i = 0; i < XVECLEN (insn, 1); i++)
920     scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
921
922   get_pattern_stats (&stats, XVEC (insn, 1));
923   d->n_generator_args = stats.num_generator_args;
924   d->n_operands = stats.num_insn_operands;
925   d->n_dups = stats.num_dups;
926
927   validate_insn_operands (d);
928   validate_insn_alternatives (d);
929   validate_optab_operands (d);
930   place_operands (d);
931   process_template (d, XTMPL (insn, 3));
932 }
933 \f
934 /* Look at a define_peephole just read.  Assign its code number.
935    Record on idata the template and the number of arguments.
936    If the insn has a hairy output action, output it now.  */
937
938 static void
939 gen_peephole (rtx peep, int lineno)
940 {
941   struct pattern_stats stats;
942   struct data *d = XNEW (struct data);
943   int i;
944
945   d->code_number = next_code_number;
946   d->index_number = next_index_number;
947   d->filename = read_md_filename;
948   d->lineno = lineno;
949   d->name = 0;
950
951   /* Build up the list in the same order as the insns are seen
952      in the machine description.  */
953   d->next = 0;
954   *idata_end = d;
955   idata_end = &d->next;
956
957   memset (d->operand, 0, sizeof (d->operand));
958
959   /* Get the number of operands by scanning all the patterns of the
960      peephole optimizer.  But ignore all the rest of the information
961      thus obtained.  */
962   for (i = 0; i < XVECLEN (peep, 0); i++)
963     scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
964
965   get_pattern_stats (&stats, XVEC (peep, 0));
966   d->n_generator_args = 0;
967   d->n_operands = stats.num_insn_operands;
968   d->n_dups = 0;
969
970   validate_insn_alternatives (d);
971   place_operands (d);
972   process_template (d, XTMPL (peep, 2));
973 }
974 \f
975 /* Process a define_expand just read.  Assign its code number,
976    only for the purposes of `insn_gen_function'.  */
977
978 static void
979 gen_expand (rtx insn, int lineno)
980 {
981   struct pattern_stats stats;
982   struct data *d = XNEW (struct data);
983   int i;
984
985   d->code_number = next_code_number;
986   d->index_number = next_index_number;
987   d->filename = read_md_filename;
988   d->lineno = lineno;
989   if (XSTR (insn, 0)[0])
990     d->name = XSTR (insn, 0);
991   else
992     d->name = 0;
993
994   /* Build up the list in the same order as the insns are seen
995      in the machine description.  */
996   d->next = 0;
997   *idata_end = d;
998   idata_end = &d->next;
999
1000   memset (d->operand, 0, sizeof (d->operand));
1001
1002   /* Scan the operands to get the specified predicates and modes,
1003      since expand_binop needs to know them.  */
1004
1005   if (XVEC (insn, 1))
1006     for (i = 0; i < XVECLEN (insn, 1); i++)
1007       scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
1008
1009   get_pattern_stats (&stats, XVEC (insn, 1));
1010   d->n_generator_args = stats.num_generator_args;
1011   d->n_operands = stats.num_insn_operands;
1012   d->n_dups = stats.num_dups;
1013   d->template_code = 0;
1014   d->output_format = INSN_OUTPUT_FORMAT_NONE;
1015
1016   validate_insn_alternatives (d);
1017   validate_optab_operands (d);
1018   place_operands (d);
1019 }
1020 \f
1021 /* Process a define_split just read.  Assign its code number,
1022    only for reasons of consistency and to simplify genrecog.  */
1023
1024 static void
1025 gen_split (rtx split, int lineno)
1026 {
1027   struct pattern_stats stats;
1028   struct data *d = XNEW (struct data);
1029   int i;
1030
1031   d->code_number = next_code_number;
1032   d->index_number = next_index_number;
1033   d->filename = read_md_filename;
1034   d->lineno = lineno;
1035   d->name = 0;
1036
1037   /* Build up the list in the same order as the insns are seen
1038      in the machine description.  */
1039   d->next = 0;
1040   *idata_end = d;
1041   idata_end = &d->next;
1042
1043   memset (d->operand, 0, sizeof (d->operand));
1044
1045   /* Get the number of operands by scanning all the patterns of the
1046      split patterns.  But ignore all the rest of the information thus
1047      obtained.  */
1048   for (i = 0; i < XVECLEN (split, 0); i++)
1049     scan_operands (d, XVECEXP (split, 0, i), 0, 0);
1050
1051   get_pattern_stats (&stats, XVEC (split, 0));
1052   d->n_generator_args = 0;
1053   d->n_operands = stats.num_insn_operands;
1054   d->n_dups = 0;
1055   d->n_alternatives = 0;
1056   d->template_code = 0;
1057   d->output_format = INSN_OUTPUT_FORMAT_NONE;
1058
1059   place_operands (d);
1060 }
1061
1062 static void
1063 init_insn_for_nothing (void)
1064 {
1065   memset (&nothing, 0, sizeof (nothing));
1066   nothing.name = "*placeholder_for_nothing";
1067   nothing.filename = "<internal>";
1068 }
1069
1070 extern int main (int, char **);
1071
1072 int
1073 main (int argc, char **argv)
1074 {
1075   rtx desc;
1076
1077   progname = "genoutput";
1078
1079   init_insn_for_nothing ();
1080
1081   if (!init_rtx_reader_args (argc, argv))
1082     return (FATAL_EXIT_CODE);
1083
1084   output_prologue ();
1085   next_index_number = 0;
1086
1087   /* Read the machine description.  */
1088
1089   while (1)
1090     {
1091       int line_no;
1092
1093       desc = read_md_rtx (&line_no, &next_code_number);
1094       if (desc == NULL)
1095         break;
1096
1097       switch (GET_CODE (desc))
1098         {
1099         case DEFINE_INSN:
1100           gen_insn (desc, line_no);
1101           break;
1102
1103         case DEFINE_PEEPHOLE:
1104           gen_peephole (desc, line_no);
1105           break;
1106
1107         case DEFINE_EXPAND:
1108           gen_expand (desc, line_no);
1109           break;
1110
1111         case DEFINE_SPLIT:
1112         case DEFINE_PEEPHOLE2:
1113           gen_split (desc, line_no);
1114           break;
1115
1116         case DEFINE_CONSTRAINT:
1117         case DEFINE_REGISTER_CONSTRAINT:
1118         case DEFINE_ADDRESS_CONSTRAINT:
1119         case DEFINE_MEMORY_CONSTRAINT:
1120           note_constraint (desc, line_no);
1121           break;
1122
1123         default:
1124           break;
1125         }
1126       next_index_number++;
1127     }
1128
1129   printf ("\n\n");
1130   output_operand_data ();
1131   output_insn_data ();
1132   output_get_insn_name ();
1133
1134   fflush (stdout);
1135   return (ferror (stdout) != 0 || have_error
1136         ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1137 }
1138
1139 /* Return the number of occurrences of character C in string S or
1140    -1 if S is the null string.  */
1141
1142 static int
1143 n_occurrences (int c, const char *s)
1144 {
1145   int n = 0;
1146
1147   if (s == 0 || *s == '\0')
1148     return -1;
1149
1150   while (*s)
1151     n += (*s++ == c);
1152
1153   return n;
1154 }
1155
1156 /* Remove whitespace in `s' by moving up characters until the end.
1157    Return a new string.  */
1158
1159 static const char *
1160 strip_whitespace (const char *s)
1161 {
1162   char *p, *q;
1163   char ch;
1164
1165   if (s == 0)
1166     return 0;
1167
1168   p = q = XNEWVEC (char, strlen (s) + 1);
1169   while ((ch = *s++) != '\0')
1170     if (! ISSPACE (ch))
1171       *p++ = ch;
1172
1173   *p = '\0';
1174   return q;
1175 }
1176
1177 /* Record just enough information about a constraint to allow checking
1178    of operand constraint strings above, in validate_insn_alternatives.
1179    Does not validate most properties of the constraint itself; does
1180    enforce no duplicate names, no overlap with MI constraints, and no
1181    prefixes.  EXP is the define_*constraint form, LINENO the line number
1182    reported by the reader.  */
1183 static void
1184 note_constraint (rtx exp, int lineno)
1185 {
1186   const char *name = XSTR (exp, 0);
1187   struct constraint_data **iter, **slot, *new_cdata;
1188
1189   if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
1190     name = general_mem;
1191   unsigned int namelen = strlen (name);
1192
1193   if (strchr (indep_constraints, name[0]))
1194     {
1195       if (name[1] == '\0')
1196         error_with_line (lineno, "constraint letter '%s' cannot be "
1197                          "redefined by the machine description", name);
1198       else
1199         error_with_line (lineno, "constraint name '%s' cannot be defined by "
1200                          "the machine description, as it begins with '%c'",
1201                          name, name[0]);
1202       return;
1203     }
1204
1205   slot = &constraints_by_letter_table[(unsigned int)name[0]];
1206   for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
1207     {
1208       /* This causes slot to end up pointing to the
1209          next_this_letter field of the last constraint with a name
1210          of equal or greater length than the new constraint; hence
1211          the new constraint will be inserted after all previous
1212          constraints with names of the same length.  */
1213       if ((*iter)->namelen >= namelen)
1214         slot = iter;
1215
1216       if (!strcmp ((*iter)->name, name))
1217         {
1218           error_with_line (lineno, "redefinition of constraint '%s'", name);
1219           message_with_line ((*iter)->lineno, "previous definition is here");
1220           return;
1221         }
1222       else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
1223         {
1224           error_with_line (lineno, "defining constraint '%s' here", name);
1225           message_with_line ((*iter)->lineno, "renders constraint '%s' "
1226                              "(defined here) a prefix", (*iter)->name);
1227           return;
1228         }
1229       else if (!strncmp ((*iter)->name, name, namelen))
1230         {
1231           error_with_line (lineno, "constraint '%s' is a prefix", name);
1232           message_with_line ((*iter)->lineno, "of constraint '%s' "
1233                              "(defined here)", (*iter)->name);
1234           return;
1235         }
1236     }
1237   new_cdata = XNEWVAR (struct constraint_data, sizeof (struct constraint_data) + namelen);
1238   strcpy (CONST_CAST (char *, new_cdata->name), name);
1239   new_cdata->namelen = namelen;
1240   new_cdata->lineno = lineno;
1241   new_cdata->next_this_letter = *slot;
1242   *slot = new_cdata;
1243 }
1244
1245 /* Return the length of the constraint name beginning at position S
1246    of an operand constraint string, or issue an error message if there
1247    is no such constraint.  Does not expect to be called for generic
1248    constraints.  */
1249 static int
1250 mdep_constraint_len (const char *s, int lineno, int opno)
1251 {
1252   struct constraint_data *p;
1253
1254   p = constraints_by_letter_table[(unsigned int)s[0]];
1255
1256   if (p)
1257     for (; p; p = p->next_this_letter)
1258       if (!strncmp (s, p->name, p->namelen))
1259         return p->namelen;
1260
1261   error_with_line (lineno,
1262                    "error: undefined machine-specific constraint "
1263                    "at this point: \"%s\"", s);
1264   message_with_line (lineno, "note:  in operand %d", opno);
1265   return 1; /* safe */
1266 }