]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genoutput.c
genoutput.c (process_template): Process '*' in '@' alternatives.
[thirdparty/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, 2011, 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 int found_star = 0;
666
667 for (cp = &template_code[1]; *cp; )
668 {
669 while (ISSPACE (*cp))
670 cp++;
671 if (*cp == '*')
672 found_star = 1;
673 while (!IS_VSPACE (*cp) && *cp != '\0')
674 ++cp;
675 }
676 d->template_code = 0;
677 if (found_star)
678 {
679 d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
680 puts ("\nstatic const char *");
681 printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
682 "rtx insn ATTRIBUTE_UNUSED)\n", d->code_number);
683 puts ("{");
684 puts (" switch (which_alternative)\n {");
685 }
686 else
687 {
688 d->output_format = INSN_OUTPUT_FORMAT_MULTI;
689 printf ("\nstatic const char * const output_%d[] = {\n",
690 d->code_number);
691 }
692
693 for (i = 0, cp = &template_code[1]; *cp; )
694 {
695 const char *ep, *sp, *bp;
696
697 while (ISSPACE (*cp))
698 cp++;
699
700 bp = cp;
701 if (found_star)
702 {
703 printf (" case %d:", i);
704 if (*cp == '*')
705 {
706 printf ("\n ");
707 cp++;
708 }
709 else
710 printf (" return \"");
711 }
712 else
713 printf (" \"");
714
715 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
716 if (!ISSPACE (*ep))
717 sp = ep + 1;
718
719 if (sp != ep)
720 message_with_line (d->lineno,
721 "trailing whitespace in output template");
722
723 while (cp < sp)
724 {
725 putchar (*cp);
726 cp++;
727 }
728
729 if (!found_star)
730 puts ("\",");
731 else if (*bp != '*')
732 puts ("\";");
733 else
734 {
735 /* The usual action will end with a return.
736 If there is neither break or return at the end, this is
737 assumed to be intentional; this allows to have multiple
738 consecutive alternatives share some code. */
739 puts ("");
740 }
741 i++;
742 }
743 if (i == 1)
744 message_with_line (d->lineno,
745 "'@' is redundant for output template with single alternative");
746 if (i != d->n_alternatives)
747 error_with_line (d->lineno,
748 "wrong number of alternatives in the output template");
749
750 if (found_star)
751 puts (" default: gcc_unreachable ();\n }\n}");
752 else
753 printf ("};\n");
754 }
755 else
756 {
757 d->template_code = template_code;
758 d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
759 }
760 }
761 \f
762 /* Check insn D for consistency in number of constraint alternatives. */
763
764 static void
765 validate_insn_alternatives (struct data *d)
766 {
767 int n = 0, start;
768
769 /* Make sure all the operands have the same number of alternatives
770 in their constraints. Let N be that number. */
771 for (start = 0; start < d->n_operands; start++)
772 if (d->operand[start].n_alternatives > 0)
773 {
774 int len, i;
775 const char *p;
776 char c;
777 int which_alternative = 0;
778 int alternative_count_unsure = 0;
779
780 for (p = d->operand[start].constraint; (c = *p); p += len)
781 {
782 #ifdef USE_MD_CONSTRAINTS
783 if (ISSPACE (c) || strchr (indep_constraints, c))
784 len = 1;
785 else if (ISDIGIT (c))
786 {
787 const char *q = p;
788 do
789 q++;
790 while (ISDIGIT (*q));
791 len = q - p;
792 }
793 else
794 len = mdep_constraint_len (p, d->lineno, start);
795 #else
796 len = CONSTRAINT_LEN (c, p);
797
798 if (len < 1 || (len > 1 && strchr (",#*+=&%!0123456789", c)))
799 {
800 error_with_line (d->lineno,
801 "invalid length %d for char '%c' in"
802 " alternative %d of operand %d",
803 len, c, which_alternative, start);
804 len = 1;
805 }
806 #endif
807
808 if (c == ',')
809 {
810 which_alternative++;
811 continue;
812 }
813
814 for (i = 1; i < len; i++)
815 if (p[i] == '\0')
816 {
817 error_with_line (d->lineno,
818 "NUL in alternative %d of operand %d",
819 which_alternative, start);
820 alternative_count_unsure = 1;
821 break;
822 }
823 else if (strchr (",#*", p[i]))
824 {
825 error_with_line (d->lineno,
826 "'%c' in alternative %d of operand %d",
827 p[i], which_alternative, start);
828 alternative_count_unsure = 1;
829 }
830 }
831 if (!alternative_count_unsure)
832 {
833 if (n == 0)
834 n = d->operand[start].n_alternatives;
835 else if (n != d->operand[start].n_alternatives)
836 error_with_line (d->lineno,
837 "wrong number of alternatives in operand %d",
838 start);
839 }
840 }
841
842 /* Record the insn's overall number of alternatives. */
843 d->n_alternatives = n;
844 }
845
846 /* Verify that there are no gaps in operand numbers for INSNs. */
847
848 static void
849 validate_insn_operands (struct data *d)
850 {
851 int i;
852
853 for (i = 0; i < d->n_operands; ++i)
854 if (d->operand[i].seen == 0)
855 error_with_line (d->lineno, "missing operand %d", i);
856 }
857
858 static void
859 validate_optab_operands (struct data *d)
860 {
861 if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
862 return;
863
864 /* Miscellaneous tests. */
865 if (strncmp (d->name, "cstore", 6) == 0
866 && d->name[strlen (d->name) - 1] == '4'
867 && d->operand[0].mode == VOIDmode)
868 {
869 message_with_line (d->lineno, "missing mode for operand 0 of cstore");
870 have_error = 1;
871 }
872 }
873 \f
874 /* Look at a define_insn just read. Assign its code number. Record
875 on idata the template and the number of arguments. If the insn has
876 a hairy output action, output a function for now. */
877
878 static void
879 gen_insn (rtx insn, int lineno)
880 {
881 struct pattern_stats stats;
882 struct data *d = XNEW (struct data);
883 int i;
884
885 d->code_number = next_code_number;
886 d->index_number = next_index_number;
887 d->filename = read_md_filename;
888 d->lineno = lineno;
889 if (XSTR (insn, 0)[0])
890 d->name = XSTR (insn, 0);
891 else
892 d->name = 0;
893
894 /* Build up the list in the same order as the insns are seen
895 in the machine description. */
896 d->next = 0;
897 *idata_end = d;
898 idata_end = &d->next;
899
900 memset (d->operand, 0, sizeof (d->operand));
901
902 for (i = 0; i < XVECLEN (insn, 1); i++)
903 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
904
905 get_pattern_stats (&stats, XVEC (insn, 1));
906 d->n_generator_args = stats.num_generator_args;
907 d->n_operands = stats.num_insn_operands;
908 d->n_dups = stats.num_dups;
909
910 #ifndef USE_MD_CONSTRAINTS
911 check_constraint_len ();
912 #endif
913 validate_insn_operands (d);
914 validate_insn_alternatives (d);
915 validate_optab_operands (d);
916 place_operands (d);
917 process_template (d, XTMPL (insn, 3));
918 }
919 \f
920 /* Look at a define_peephole just read. Assign its code number.
921 Record on idata the template and the number of arguments.
922 If the insn has a hairy output action, output it now. */
923
924 static void
925 gen_peephole (rtx peep, int lineno)
926 {
927 struct pattern_stats stats;
928 struct data *d = XNEW (struct data);
929 int i;
930
931 d->code_number = next_code_number;
932 d->index_number = next_index_number;
933 d->filename = read_md_filename;
934 d->lineno = lineno;
935 d->name = 0;
936
937 /* Build up the list in the same order as the insns are seen
938 in the machine description. */
939 d->next = 0;
940 *idata_end = d;
941 idata_end = &d->next;
942
943 memset (d->operand, 0, sizeof (d->operand));
944
945 /* Get the number of operands by scanning all the patterns of the
946 peephole optimizer. But ignore all the rest of the information
947 thus obtained. */
948 for (i = 0; i < XVECLEN (peep, 0); i++)
949 scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
950
951 get_pattern_stats (&stats, XVEC (peep, 0));
952 d->n_generator_args = 0;
953 d->n_operands = stats.num_insn_operands;
954 d->n_dups = 0;
955
956 validate_insn_alternatives (d);
957 place_operands (d);
958 process_template (d, XTMPL (peep, 2));
959 }
960 \f
961 /* Process a define_expand just read. Assign its code number,
962 only for the purposes of `insn_gen_function'. */
963
964 static void
965 gen_expand (rtx insn, int lineno)
966 {
967 struct pattern_stats stats;
968 struct data *d = XNEW (struct data);
969 int i;
970
971 d->code_number = next_code_number;
972 d->index_number = next_index_number;
973 d->filename = read_md_filename;
974 d->lineno = lineno;
975 if (XSTR (insn, 0)[0])
976 d->name = XSTR (insn, 0);
977 else
978 d->name = 0;
979
980 /* Build up the list in the same order as the insns are seen
981 in the machine description. */
982 d->next = 0;
983 *idata_end = d;
984 idata_end = &d->next;
985
986 memset (d->operand, 0, sizeof (d->operand));
987
988 /* Scan the operands to get the specified predicates and modes,
989 since expand_binop needs to know them. */
990
991 if (XVEC (insn, 1))
992 for (i = 0; i < XVECLEN (insn, 1); i++)
993 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
994
995 get_pattern_stats (&stats, XVEC (insn, 1));
996 d->n_generator_args = stats.num_generator_args;
997 d->n_operands = stats.num_insn_operands;
998 d->n_dups = stats.num_dups;
999 d->template_code = 0;
1000 d->output_format = INSN_OUTPUT_FORMAT_NONE;
1001
1002 validate_insn_alternatives (d);
1003 validate_optab_operands (d);
1004 place_operands (d);
1005 }
1006 \f
1007 /* Process a define_split just read. Assign its code number,
1008 only for reasons of consistency and to simplify genrecog. */
1009
1010 static void
1011 gen_split (rtx split, int lineno)
1012 {
1013 struct pattern_stats stats;
1014 struct data *d = XNEW (struct data);
1015 int i;
1016
1017 d->code_number = next_code_number;
1018 d->index_number = next_index_number;
1019 d->filename = read_md_filename;
1020 d->lineno = lineno;
1021 d->name = 0;
1022
1023 /* Build up the list in the same order as the insns are seen
1024 in the machine description. */
1025 d->next = 0;
1026 *idata_end = d;
1027 idata_end = &d->next;
1028
1029 memset (d->operand, 0, sizeof (d->operand));
1030
1031 /* Get the number of operands by scanning all the patterns of the
1032 split patterns. But ignore all the rest of the information thus
1033 obtained. */
1034 for (i = 0; i < XVECLEN (split, 0); i++)
1035 scan_operands (d, XVECEXP (split, 0, i), 0, 0);
1036
1037 get_pattern_stats (&stats, XVEC (split, 0));
1038 d->n_generator_args = 0;
1039 d->n_operands = stats.num_insn_operands;
1040 d->n_dups = 0;
1041 d->n_alternatives = 0;
1042 d->template_code = 0;
1043 d->output_format = INSN_OUTPUT_FORMAT_NONE;
1044
1045 place_operands (d);
1046 }
1047
1048 static void
1049 init_insn_for_nothing (void)
1050 {
1051 memset (&nothing, 0, sizeof (nothing));
1052 nothing.name = "*placeholder_for_nothing";
1053 nothing.filename = "<internal>";
1054 }
1055
1056 extern int main (int, char **);
1057
1058 int
1059 main (int argc, char **argv)
1060 {
1061 rtx desc;
1062
1063 progname = "genoutput";
1064
1065 init_insn_for_nothing ();
1066
1067 if (!init_rtx_reader_args (argc, argv))
1068 return (FATAL_EXIT_CODE);
1069
1070 output_prologue ();
1071 next_index_number = 0;
1072
1073 /* Read the machine description. */
1074
1075 while (1)
1076 {
1077 int line_no;
1078
1079 desc = read_md_rtx (&line_no, &next_code_number);
1080 if (desc == NULL)
1081 break;
1082
1083 switch (GET_CODE (desc))
1084 {
1085 case DEFINE_INSN:
1086 gen_insn (desc, line_no);
1087 break;
1088
1089 case DEFINE_PEEPHOLE:
1090 gen_peephole (desc, line_no);
1091 break;
1092
1093 case DEFINE_EXPAND:
1094 gen_expand (desc, line_no);
1095 break;
1096
1097 case DEFINE_SPLIT:
1098 case DEFINE_PEEPHOLE2:
1099 gen_split (desc, line_no);
1100 break;
1101
1102 #ifdef USE_MD_CONSTRAINTS
1103 case DEFINE_CONSTRAINT:
1104 case DEFINE_REGISTER_CONSTRAINT:
1105 case DEFINE_ADDRESS_CONSTRAINT:
1106 case DEFINE_MEMORY_CONSTRAINT:
1107 note_constraint (desc, line_no);
1108 break;
1109 #endif
1110
1111 default:
1112 break;
1113 }
1114 next_index_number++;
1115 }
1116
1117 printf("\n\n");
1118 output_operand_data ();
1119 output_insn_data ();
1120 output_get_insn_name ();
1121
1122 fflush (stdout);
1123 return (ferror (stdout) != 0 || have_error
1124 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1125 }
1126
1127 /* Return the number of occurrences of character C in string S or
1128 -1 if S is the null string. */
1129
1130 static int
1131 n_occurrences (int c, const char *s)
1132 {
1133 int n = 0;
1134
1135 if (s == 0 || *s == '\0')
1136 return -1;
1137
1138 while (*s)
1139 n += (*s++ == c);
1140
1141 return n;
1142 }
1143
1144 /* Remove whitespace in `s' by moving up characters until the end.
1145 Return a new string. */
1146
1147 static const char *
1148 strip_whitespace (const char *s)
1149 {
1150 char *p, *q;
1151 char ch;
1152
1153 if (s == 0)
1154 return 0;
1155
1156 p = q = XNEWVEC (char, strlen (s) + 1);
1157 while ((ch = *s++) != '\0')
1158 if (! ISSPACE (ch))
1159 *p++ = ch;
1160
1161 *p = '\0';
1162 return q;
1163 }
1164
1165 #ifdef USE_MD_CONSTRAINTS
1166
1167 /* Record just enough information about a constraint to allow checking
1168 of operand constraint strings above, in validate_insn_alternatives.
1169 Does not validate most properties of the constraint itself; does
1170 enforce no duplicate names, no overlap with MI constraints, and no
1171 prefixes. EXP is the define_*constraint form, LINENO the line number
1172 reported by the reader. */
1173 static void
1174 note_constraint (rtx exp, int lineno)
1175 {
1176 const char *name = XSTR (exp, 0);
1177 unsigned int namelen = strlen (name);
1178 struct constraint_data **iter, **slot, *new_cdata;
1179
1180 /* The 'm' constraint is special here since that constraint letter
1181 can be overridden by the back end by defining the
1182 TARGET_MEM_CONSTRAINT macro. */
1183 if (strchr (indep_constraints, name[0]) && name[0] != 'm')
1184 {
1185 if (name[1] == '\0')
1186 error_with_line (lineno, "constraint letter '%s' cannot be "
1187 "redefined by the machine description", name);
1188 else
1189 error_with_line (lineno, "constraint name '%s' cannot be defined by "
1190 "the machine description, as it begins with '%c'",
1191 name, name[0]);
1192 return;
1193 }
1194
1195 slot = &constraints_by_letter_table[(unsigned int)name[0]];
1196 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
1197 {
1198 /* This causes slot to end up pointing to the
1199 next_this_letter field of the last constraint with a name
1200 of equal or greater length than the new constraint; hence
1201 the new constraint will be inserted after all previous
1202 constraints with names of the same length. */
1203 if ((*iter)->namelen >= namelen)
1204 slot = iter;
1205
1206 if (!strcmp ((*iter)->name, name))
1207 {
1208 error_with_line (lineno, "redefinition of constraint '%s'", name);
1209 message_with_line ((*iter)->lineno, "previous definition is here");
1210 return;
1211 }
1212 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
1213 {
1214 error_with_line (lineno, "defining constraint '%s' here", name);
1215 message_with_line ((*iter)->lineno, "renders constraint '%s' "
1216 "(defined here) a prefix", (*iter)->name);
1217 return;
1218 }
1219 else if (!strncmp ((*iter)->name, name, namelen))
1220 {
1221 error_with_line (lineno, "constraint '%s' is a prefix", name);
1222 message_with_line ((*iter)->lineno, "of constraint '%s' "
1223 "(defined here)", (*iter)->name);
1224 return;
1225 }
1226 }
1227 new_cdata = XNEWVAR (struct constraint_data, sizeof (struct constraint_data) + namelen);
1228 strcpy (CONST_CAST(char *, new_cdata->name), name);
1229 new_cdata->namelen = namelen;
1230 new_cdata->lineno = lineno;
1231 new_cdata->next_this_letter = *slot;
1232 *slot = new_cdata;
1233 }
1234
1235 /* Return the length of the constraint name beginning at position S
1236 of an operand constraint string, or issue an error message if there
1237 is no such constraint. Does not expect to be called for generic
1238 constraints. */
1239 static int
1240 mdep_constraint_len (const char *s, int lineno, int opno)
1241 {
1242 struct constraint_data *p;
1243
1244 p = constraints_by_letter_table[(unsigned int)s[0]];
1245
1246 if (p)
1247 for (; p; p = p->next_this_letter)
1248 if (!strncmp (s, p->name, p->namelen))
1249 return p->namelen;
1250
1251 error_with_line (lineno,
1252 "error: undefined machine-specific constraint "
1253 "at this point: \"%s\"", s);
1254 message_with_line (lineno, "note: in operand %d", opno);
1255 return 1; /* safe */
1256 }
1257
1258 #else
1259 /* Verify that DEFAULT_CONSTRAINT_LEN is used properly and not
1260 tampered with. This isn't bullet-proof, but it should catch
1261 most genuine mistakes. */
1262 static void
1263 check_constraint_len (void)
1264 {
1265 const char *p;
1266 int d;
1267
1268 for (p = ",#*+=&%!1234567890"; *p; p++)
1269 for (d = -9; d < 9; d++)
1270 gcc_assert (constraint_len (p, d) == d);
1271 }
1272
1273 static int
1274 constraint_len (const char *p, int genoutput_default_constraint_len)
1275 {
1276 /* Check that we still match defaults.h . First we do a generation-time
1277 check that fails if the value is not the expected one... */
1278 gcc_assert (DEFAULT_CONSTRAINT_LEN (*p, p) == 1);
1279 /* And now a compile-time check that should give a diagnostic if the
1280 definition doesn't exactly match. */
1281 #define DEFAULT_CONSTRAINT_LEN(C,STR) 1
1282 /* Now re-define DEFAULT_CONSTRAINT_LEN so that we can verify it is
1283 being used. */
1284 #undef DEFAULT_CONSTRAINT_LEN
1285 #define DEFAULT_CONSTRAINT_LEN(C,STR) \
1286 ((C) != *p || STR != p ? -1 : genoutput_default_constraint_len)
1287 return CONSTRAINT_LEN (*p, p);
1288 /* And set it back. */
1289 #undef DEFAULT_CONSTRAINT_LEN
1290 #define DEFAULT_CONSTRAINT_LEN(C,STR) 1
1291 }
1292 #endif