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