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