* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / genoutput.c
blobd667b94b37bde42c89e76ee4804ef888eb90f92f
1 /* Generate code from to output assembler insns as recognized from rtl.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
21 /* This program reads the machine description for the compiler target machine
22 and produces a file containing these things:
24 1. An array of `struct insn_data_d', which is indexed by insn code number,
25 which contains:
27 a. `name' is the name for that pattern. Nameless patterns are
28 given a name.
30 b. `output' hold either the output template, an array of output
31 templates, or an output function.
33 c. `genfun' is the function to generate a body for that pattern,
34 given operands as arguments.
36 d. `n_operands' is the number of distinct operands in the pattern
37 for that insn,
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.
43 f. `n_alternatives' is the number of alternatives in the constraints
44 of each pattern.
46 g. `output_format' tells what type of thing `output' is.
48 h. `operand' is the base of an array of operand data for the insn.
50 2. An array of `struct insn_operand data', used by `operand' above.
52 a. `predicate', an int-valued function, is the match_operand predicate
53 for this operand.
55 b. `constraint' is the constraint for this operand.
57 c. `address_p' indicates that the operand appears within ADDRESS
58 rtx's.
60 d. `mode' is the machine mode that that operand is supposed to have.
62 e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
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.
68 g. `allows_mem', is true for operands that accept MEM rtxes.
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.
74 Thus, the following entry in the machine description
76 (define_insn "clrdf"
77 [(set (match_operand:DF 0 "general_operand" "")
78 (const_int 0))]
80 "clrd %0")
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. */
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"
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? */
99 #define MAX_MAX_OPERANDS 40
101 static char general_mem[] = { TARGET_MEM_CONSTRAINT, 0 };
103 static int n_occurrences (int, const char *);
104 static const char *strip_whitespace (const char *);
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). */
110 static int next_code_number;
112 /* This counts all definitions in the md file,
113 for the sake of error messages. */
115 static int next_index_number;
117 /* This counts all operands used in the md file. The first is null. */
119 static int next_operand_number = 1;
121 /* Record in this chain all information about the operands we will output. */
123 struct operand_data
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;
137 /* Begin with a null operand at index 0. */
139 static struct operand_data null_operand =
141 0, 0, "", "", VOIDmode, 0, 0, 0, 0, 0
144 static struct operand_data *odata = &null_operand;
145 static struct operand_data **odata_end = &null_operand.next;
147 /* Must match the constants in recog.h. */
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 * (*)(...) */
154 /* Record in this chain all information that we will output,
155 associated with the code number of the insn. */
157 struct data
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];
175 /* A dummy insn, for CODE_FOR_nothing. */
176 static struct data nothing;
178 /* This variable points to the first link in the insn chain. */
179 static struct data *idata = &nothing;
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;
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);
202 struct constraint_data
204 struct constraint_data *next_this_letter;
205 int lineno;
206 unsigned int namelen;
207 const char name[1];
210 /* All machine-independent constraint characters (except digits) that
211 are handled outside the define*_constraint mechanism. */
212 static const char indep_constraints[] = ",=+%*?!#&g";
214 static struct constraint_data *
215 constraints_by_letter_table[1 << CHAR_BIT];
217 static int mdep_constraint_len (const char *, int, int);
218 static void note_constraint (rtx, int);
220 static void
221 output_prologue (void)
223 printf ("/* Generated automatically by the program `genoutput'\n\
224 from the machine description file `md'. */\n\n");
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 \"tree.h\"\n");
233 printf ("#include \"varasm.h\"\n");
234 printf ("#include \"stor-layout.h\"\n");
235 printf ("#include \"calls.h\"\n");
236 printf ("#include \"rtl.h\"\n");
237 printf ("#include \"expr.h\"\n");
238 printf ("#include \"insn-codes.h\"\n");
239 printf ("#include \"tm_p.h\"\n");
240 printf ("#include \"function.h\"\n");
241 printf ("#include \"regs.h\"\n");
242 printf ("#include \"hard-reg-set.h\"\n");
243 printf ("#include \"insn-config.h\"\n\n");
244 printf ("#include \"conditions.h\"\n");
245 printf ("#include \"insn-attr.h\"\n\n");
246 printf ("#include \"recog.h\"\n\n");
247 printf ("#include \"diagnostic-core.h\"\n");
248 printf ("#include \"output.h\"\n");
249 printf ("#include \"target.h\"\n");
250 printf ("#include \"tm-constrs.h\"\n");
251 printf ("#include \"predict.h\"\n");
254 static void
255 output_operand_data (void)
257 struct operand_data *d;
259 printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
261 for (d = odata; d; d = d->next)
263 struct pred_data *pred;
265 printf (" {\n");
267 printf (" %s,\n",
268 d->predicate && d->predicate[0] ? d->predicate : "0");
270 printf (" \"%s\",\n", d->constraint ? d->constraint : "");
272 printf (" %smode,\n", GET_MODE_NAME (d->mode));
274 printf (" %d,\n", d->strict_low);
276 printf (" %d,\n", d->constraint == NULL ? 1 : 0);
278 printf (" %d,\n", d->eliminable);
280 pred = NULL;
281 if (d->predicate)
282 pred = lookup_predicate (d->predicate);
283 printf (" %d\n", pred && pred->codes[MEM]);
285 printf (" },\n");
287 printf ("};\n\n\n");
290 static void
291 output_insn_data (void)
293 struct data *d;
294 int name_offset = 0;
295 int next_name_offset;
296 const char * last_name = 0;
297 const char * next_name = 0;
298 struct data *n;
300 for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
301 if (n->name)
303 next_name = n->name;
304 break;
307 printf ("#if GCC_VERSION >= 2007\n__extension__\n#endif\n");
308 printf ("\nconst struct insn_data_d insn_data[] = \n{\n");
310 for (d = idata; d; d = d->next)
312 printf (" /* %s:%d */\n", d->filename, d->lineno);
313 printf (" {\n");
315 if (d->name)
317 printf (" \"%s\",\n", d->name);
318 name_offset = 0;
319 last_name = d->name;
320 next_name = 0;
321 for (n = d->next, next_name_offset = 1; n;
322 n = n->next, next_name_offset++)
324 if (n->name)
326 next_name = n->name;
327 break;
331 else
333 name_offset++;
334 if (next_name && (last_name == 0
335 || name_offset > next_name_offset / 2))
336 printf (" \"%s-%d\",\n", next_name,
337 next_name_offset - name_offset);
338 else
339 printf (" \"%s+%d\",\n", last_name, name_offset);
342 switch (d->output_format)
344 case INSN_OUTPUT_FORMAT_NONE:
345 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
346 printf (" { 0 },\n");
347 printf ("#else\n");
348 printf (" { 0, 0, 0 },\n");
349 printf ("#endif\n");
350 break;
351 case INSN_OUTPUT_FORMAT_SINGLE:
353 const char *p = d->template_code;
354 char prev = 0;
356 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
357 printf (" { .single =\n");
358 printf ("#else\n");
359 printf (" {\n");
360 printf ("#endif\n");
361 printf (" \"");
362 while (*p)
364 if (IS_VSPACE (*p) && prev != '\\')
366 /* Preserve two consecutive \n's or \r's, but treat \r\n
367 as a single newline. */
368 if (*p == '\n' && prev != '\r')
369 printf ("\\n\\\n");
371 else
372 putchar (*p);
373 prev = *p;
374 ++p;
376 printf ("\",\n");
377 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
378 printf (" },\n");
379 printf ("#else\n");
380 printf (" 0, 0 },\n");
381 printf ("#endif\n");
383 break;
384 case INSN_OUTPUT_FORMAT_MULTI:
385 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
386 printf (" { .multi = output_%d },\n", d->code_number);
387 printf ("#else\n");
388 printf (" { 0, output_%d, 0 },\n", d->code_number);
389 printf ("#endif\n");
390 break;
391 case INSN_OUTPUT_FORMAT_FUNCTION:
392 printf ("#if HAVE_DESIGNATED_UNION_INITIALIZERS\n");
393 printf (" { .function = output_%d },\n", d->code_number);
394 printf ("#else\n");
395 printf (" { 0, 0, output_%d },\n", d->code_number);
396 printf ("#endif\n");
397 break;
398 default:
399 gcc_unreachable ();
402 if (d->name && d->name[0] != '*')
403 printf (" { (insn_gen_fn::stored_funcptr) gen_%s },\n", d->name);
404 else
405 printf (" { 0 },\n");
407 printf (" &operand_data[%d],\n", d->operand_number);
408 printf (" %d,\n", d->n_generator_args);
409 printf (" %d,\n", d->n_operands);
410 printf (" %d,\n", d->n_dups);
411 printf (" %d,\n", d->n_alternatives);
412 printf (" %d\n", d->output_format);
414 printf (" },\n");
416 printf ("};\n\n\n");
419 static void
420 output_get_insn_name (void)
422 printf ("const char *\n");
423 printf ("get_insn_name (int code)\n");
424 printf ("{\n");
425 printf (" if (code == NOOP_MOVE_INSN_CODE)\n");
426 printf (" return \"NOOP_MOVE\";\n");
427 printf (" else\n");
428 printf (" return insn_data[code].name;\n");
429 printf ("}\n");
433 /* Stores the operand data into `d->operand[i]'.
435 THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
436 THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
438 static void
439 scan_operands (struct data *d, rtx part, int this_address_p,
440 int this_strict_low)
442 int i, j;
443 const char *format_ptr;
444 int opno;
446 if (part == 0)
447 return;
449 switch (GET_CODE (part))
451 case MATCH_OPERAND:
452 opno = XINT (part, 0);
453 if (opno >= MAX_MAX_OPERANDS)
455 error_with_line (d->lineno, "maximum number of operands exceeded");
456 return;
458 if (d->operand[opno].seen)
459 error_with_line (d->lineno, "repeated operand number %d\n", opno);
461 d->operand[opno].seen = 1;
462 d->operand[opno].mode = GET_MODE (part);
463 d->operand[opno].strict_low = this_strict_low;
464 d->operand[opno].predicate = XSTR (part, 1);
465 d->operand[opno].constraint = strip_whitespace (XSTR (part, 2));
466 d->operand[opno].n_alternatives
467 = n_occurrences (',', d->operand[opno].constraint) + 1;
468 d->operand[opno].address_p = this_address_p;
469 d->operand[opno].eliminable = 1;
470 return;
472 case MATCH_SCRATCH:
473 opno = XINT (part, 0);
474 if (opno >= MAX_MAX_OPERANDS)
476 error_with_line (d->lineno, "maximum number of operands exceeded");
477 return;
479 if (d->operand[opno].seen)
480 error_with_line (d->lineno, "repeated operand number %d\n", opno);
482 d->operand[opno].seen = 1;
483 d->operand[opno].mode = GET_MODE (part);
484 d->operand[opno].strict_low = 0;
485 d->operand[opno].predicate = "scratch_operand";
486 d->operand[opno].constraint = strip_whitespace (XSTR (part, 1));
487 d->operand[opno].n_alternatives
488 = n_occurrences (',', d->operand[opno].constraint) + 1;
489 d->operand[opno].address_p = 0;
490 d->operand[opno].eliminable = 0;
491 return;
493 case MATCH_OPERATOR:
494 case MATCH_PARALLEL:
495 opno = XINT (part, 0);
496 if (opno >= MAX_MAX_OPERANDS)
498 error_with_line (d->lineno, "maximum number of operands exceeded");
499 return;
501 if (d->operand[opno].seen)
502 error_with_line (d->lineno, "repeated operand number %d\n", opno);
504 d->operand[opno].seen = 1;
505 d->operand[opno].mode = GET_MODE (part);
506 d->operand[opno].strict_low = 0;
507 d->operand[opno].predicate = XSTR (part, 1);
508 d->operand[opno].constraint = 0;
509 d->operand[opno].address_p = 0;
510 d->operand[opno].eliminable = 0;
511 for (i = 0; i < XVECLEN (part, 2); i++)
512 scan_operands (d, XVECEXP (part, 2, i), 0, 0);
513 return;
515 case STRICT_LOW_PART:
516 scan_operands (d, XEXP (part, 0), 0, 1);
517 return;
519 default:
520 break;
523 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
525 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
526 switch (*format_ptr++)
528 case 'e':
529 case 'u':
530 scan_operands (d, XEXP (part, i), 0, 0);
531 break;
532 case 'E':
533 if (XVEC (part, i) != NULL)
534 for (j = 0; j < XVECLEN (part, i); j++)
535 scan_operands (d, XVECEXP (part, i, j), 0, 0);
536 break;
540 /* Compare two operands for content equality. */
542 static int
543 compare_operands (struct operand_data *d0, struct operand_data *d1)
545 const char *p0, *p1;
547 p0 = d0->predicate;
548 if (!p0)
549 p0 = "";
550 p1 = d1->predicate;
551 if (!p1)
552 p1 = "";
553 if (strcmp (p0, p1) != 0)
554 return 0;
556 p0 = d0->constraint;
557 if (!p0)
558 p0 = "";
559 p1 = d1->constraint;
560 if (!p1)
561 p1 = "";
562 if (strcmp (p0, p1) != 0)
563 return 0;
565 if (d0->mode != d1->mode)
566 return 0;
568 if (d0->strict_low != d1->strict_low)
569 return 0;
571 if (d0->eliminable != d1->eliminable)
572 return 0;
574 return 1;
577 /* Scan the list of operands we've already committed to output and either
578 find a subsequence that is the same, or allocate a new one at the end. */
580 static void
581 place_operands (struct data *d)
583 struct operand_data *od, *od2;
584 int i;
586 if (d->n_operands == 0)
588 d->operand_number = 0;
589 return;
592 /* Brute force substring search. */
593 for (od = odata, i = 0; od; od = od->next, i = 0)
594 if (compare_operands (od, &d->operand[0]))
596 od2 = od->next;
597 i = 1;
598 while (1)
600 if (i == d->n_operands)
601 goto full_match;
602 if (od2 == NULL)
603 goto partial_match;
604 if (! compare_operands (od2, &d->operand[i]))
605 break;
606 ++i, od2 = od2->next;
610 /* Either partial match at the end of the list, or no match. In either
611 case, we tack on what operands are remaining to the end of the list. */
612 partial_match:
613 d->operand_number = next_operand_number - i;
614 for (; i < d->n_operands; ++i)
616 od2 = &d->operand[i];
617 *odata_end = od2;
618 odata_end = &od2->next;
619 od2->index = next_operand_number++;
621 *odata_end = NULL;
622 return;
624 full_match:
625 d->operand_number = od->index;
626 return;
630 /* Process an assembler template from a define_insn or a define_peephole.
631 It is either the assembler code template, a list of assembler code
632 templates, or C code to generate the assembler code template. */
634 static void
635 process_template (struct data *d, const char *template_code)
637 const char *cp;
638 int i;
640 /* Templates starting with * contain straight code to be run. */
641 if (template_code[0] == '*')
643 d->template_code = 0;
644 d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
646 puts ("\nstatic const char *");
647 printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n",
648 d->code_number);
649 puts ("{");
650 print_md_ptr_loc (template_code);
651 puts (template_code + 1);
652 puts ("}");
655 /* If the assembler code template starts with a @ it is a newline-separated
656 list of assembler code templates, one for each alternative. */
657 else if (template_code[0] == '@')
659 int found_star = 0;
661 for (cp = &template_code[1]; *cp; )
663 while (ISSPACE (*cp))
664 cp++;
665 if (*cp == '*')
666 found_star = 1;
667 while (!IS_VSPACE (*cp) && *cp != '\0')
668 ++cp;
670 d->template_code = 0;
671 if (found_star)
673 d->output_format = INSN_OUTPUT_FORMAT_FUNCTION;
674 puts ("\nstatic const char *");
675 printf ("output_%d (rtx *operands ATTRIBUTE_UNUSED, "
676 "rtx_insn *insn ATTRIBUTE_UNUSED)\n", d->code_number);
677 puts ("{");
678 puts (" switch (which_alternative)\n {");
680 else
682 d->output_format = INSN_OUTPUT_FORMAT_MULTI;
683 printf ("\nstatic const char * const output_%d[] = {\n",
684 d->code_number);
687 for (i = 0, cp = &template_code[1]; *cp; )
689 const char *ep, *sp, *bp;
691 while (ISSPACE (*cp))
692 cp++;
694 bp = cp;
695 if (found_star)
697 printf (" case %d:", i);
698 if (*cp == '*')
700 printf ("\n ");
701 cp++;
703 else
704 printf (" return \"");
706 else
707 printf (" \"");
709 for (ep = sp = cp; !IS_VSPACE (*ep) && *ep != '\0'; ++ep)
710 if (!ISSPACE (*ep))
711 sp = ep + 1;
713 if (sp != ep)
714 message_with_line (d->lineno,
715 "trailing whitespace in output template");
717 while (cp < sp)
719 putchar (*cp);
720 cp++;
723 if (!found_star)
724 puts ("\",");
725 else if (*bp != '*')
726 puts ("\";");
727 else
729 /* The usual action will end with a return.
730 If there is neither break or return at the end, this is
731 assumed to be intentional; this allows to have multiple
732 consecutive alternatives share some code. */
733 puts ("");
735 i++;
737 if (i == 1)
738 message_with_line (d->lineno,
739 "'@' is redundant for output template with single alternative");
740 if (i != d->n_alternatives)
741 error_with_line (d->lineno,
742 "wrong number of alternatives in the output template");
744 if (found_star)
745 puts (" default: gcc_unreachable ();\n }\n}");
746 else
747 printf ("};\n");
749 else
751 d->template_code = template_code;
752 d->output_format = INSN_OUTPUT_FORMAT_SINGLE;
756 /* Check insn D for consistency in number of constraint alternatives. */
758 static void
759 validate_insn_alternatives (struct data *d)
761 int n = 0, start;
763 /* Make sure all the operands have the same number of alternatives
764 in their constraints. Let N be that number. */
765 for (start = 0; start < d->n_operands; start++)
766 if (d->operand[start].n_alternatives > 0)
768 int len, i;
769 const char *p;
770 char c;
771 int which_alternative = 0;
772 int alternative_count_unsure = 0;
773 bool seen_write = false;
775 for (p = d->operand[start].constraint; (c = *p); p += len)
777 if ((c == '%' || c == '=' || c == '+')
778 && p != d->operand[start].constraint)
779 error_with_line (d->lineno,
780 "character '%c' can only be used at the"
781 " beginning of a constraint string", c);
783 if (c == '=' || c == '+')
784 seen_write = true;
786 /* Earlyclobber operands must always be marked write-only
787 or read/write. */
788 if (!seen_write && c == '&')
789 error_with_line (d->lineno,
790 "earlyclobber operands may not be"
791 " read-only in alternative %d",
792 which_alternative);
794 if (ISSPACE (c) || strchr (indep_constraints, c))
795 len = 1;
796 else if (ISDIGIT (c))
798 const char *q = p;
800 q++;
801 while (ISDIGIT (*q));
802 len = q - p;
804 else
805 len = mdep_constraint_len (p, d->lineno, start);
807 if (c == ',')
809 which_alternative++;
810 continue;
813 for (i = 1; i < len; i++)
814 if (p[i] == '\0')
816 error_with_line (d->lineno,
817 "NUL in alternative %d of operand %d",
818 which_alternative, start);
819 alternative_count_unsure = 1;
820 break;
822 else if (strchr (",#*", p[i]))
824 error_with_line (d->lineno,
825 "'%c' in alternative %d of operand %d",
826 p[i], which_alternative, start);
827 alternative_count_unsure = 1;
830 if (!alternative_count_unsure)
832 if (n == 0)
833 n = d->operand[start].n_alternatives;
834 else if (n != d->operand[start].n_alternatives)
835 error_with_line (d->lineno,
836 "wrong number of alternatives in operand %d",
837 start);
841 /* Record the insn's overall number of alternatives. */
842 d->n_alternatives = n;
845 /* Verify that there are no gaps in operand numbers for INSNs. */
847 static void
848 validate_insn_operands (struct data *d)
850 int i;
852 for (i = 0; i < d->n_operands; ++i)
853 if (d->operand[i].seen == 0)
854 error_with_line (d->lineno, "missing operand %d", i);
857 static void
858 validate_optab_operands (struct data *d)
860 if (!d->name || d->name[0] == '\0' || d->name[0] == '*')
861 return;
863 /* Miscellaneous tests. */
864 if (strncmp (d->name, "cstore", 6) == 0
865 && d->name[strlen (d->name) - 1] == '4'
866 && d->operand[0].mode == VOIDmode)
868 message_with_line (d->lineno, "missing mode for operand 0 of cstore");
869 have_error = 1;
873 /* Look at a define_insn just read. Assign its code number. Record
874 on idata the template and the number of arguments. If the insn has
875 a hairy output action, output a function for now. */
877 static void
878 gen_insn (rtx insn, int lineno)
880 struct pattern_stats stats;
881 struct data *d = XNEW (struct data);
882 int i;
884 d->code_number = next_code_number;
885 d->index_number = next_index_number;
886 d->filename = read_md_filename;
887 d->lineno = lineno;
888 if (XSTR (insn, 0)[0])
889 d->name = XSTR (insn, 0);
890 else
891 d->name = 0;
893 /* Build up the list in the same order as the insns are seen
894 in the machine description. */
895 d->next = 0;
896 *idata_end = d;
897 idata_end = &d->next;
899 memset (d->operand, 0, sizeof (d->operand));
901 for (i = 0; i < XVECLEN (insn, 1); i++)
902 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
904 get_pattern_stats (&stats, XVEC (insn, 1));
905 d->n_generator_args = stats.num_generator_args;
906 d->n_operands = stats.num_insn_operands;
907 d->n_dups = stats.num_dups;
909 validate_insn_operands (d);
910 validate_insn_alternatives (d);
911 validate_optab_operands (d);
912 place_operands (d);
913 process_template (d, XTMPL (insn, 3));
916 /* Look at a define_peephole just read. Assign its code number.
917 Record on idata the template and the number of arguments.
918 If the insn has a hairy output action, output it now. */
920 static void
921 gen_peephole (rtx peep, int lineno)
923 struct pattern_stats stats;
924 struct data *d = XNEW (struct data);
925 int i;
927 d->code_number = next_code_number;
928 d->index_number = next_index_number;
929 d->filename = read_md_filename;
930 d->lineno = lineno;
931 d->name = 0;
933 /* Build up the list in the same order as the insns are seen
934 in the machine description. */
935 d->next = 0;
936 *idata_end = d;
937 idata_end = &d->next;
939 memset (d->operand, 0, sizeof (d->operand));
941 /* Get the number of operands by scanning all the patterns of the
942 peephole optimizer. But ignore all the rest of the information
943 thus obtained. */
944 for (i = 0; i < XVECLEN (peep, 0); i++)
945 scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
947 get_pattern_stats (&stats, XVEC (peep, 0));
948 d->n_generator_args = 0;
949 d->n_operands = stats.num_insn_operands;
950 d->n_dups = 0;
952 validate_insn_alternatives (d);
953 place_operands (d);
954 process_template (d, XTMPL (peep, 2));
957 /* Process a define_expand just read. Assign its code number,
958 only for the purposes of `insn_gen_function'. */
960 static void
961 gen_expand (rtx insn, int lineno)
963 struct pattern_stats stats;
964 struct data *d = XNEW (struct data);
965 int i;
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 if (XSTR (insn, 0)[0])
972 d->name = XSTR (insn, 0);
973 else
974 d->name = 0;
976 /* Build up the list in the same order as the insns are seen
977 in the machine description. */
978 d->next = 0;
979 *idata_end = d;
980 idata_end = &d->next;
982 memset (d->operand, 0, sizeof (d->operand));
984 /* Scan the operands to get the specified predicates and modes,
985 since expand_binop needs to know them. */
987 if (XVEC (insn, 1))
988 for (i = 0; i < XVECLEN (insn, 1); i++)
989 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
991 get_pattern_stats (&stats, XVEC (insn, 1));
992 d->n_generator_args = stats.num_generator_args;
993 d->n_operands = stats.num_insn_operands;
994 d->n_dups = stats.num_dups;
995 d->template_code = 0;
996 d->output_format = INSN_OUTPUT_FORMAT_NONE;
998 validate_insn_alternatives (d);
999 validate_optab_operands (d);
1000 place_operands (d);
1003 /* Process a define_split just read. Assign its code number,
1004 only for reasons of consistency and to simplify genrecog. */
1006 static void
1007 gen_split (rtx split, int lineno)
1009 struct pattern_stats stats;
1010 struct data *d = XNEW (struct data);
1011 int i;
1013 d->code_number = next_code_number;
1014 d->index_number = next_index_number;
1015 d->filename = read_md_filename;
1016 d->lineno = lineno;
1017 d->name = 0;
1019 /* Build up the list in the same order as the insns are seen
1020 in the machine description. */
1021 d->next = 0;
1022 *idata_end = d;
1023 idata_end = &d->next;
1025 memset (d->operand, 0, sizeof (d->operand));
1027 /* Get the number of operands by scanning all the patterns of the
1028 split patterns. But ignore all the rest of the information thus
1029 obtained. */
1030 for (i = 0; i < XVECLEN (split, 0); i++)
1031 scan_operands (d, XVECEXP (split, 0, i), 0, 0);
1033 get_pattern_stats (&stats, XVEC (split, 0));
1034 d->n_generator_args = 0;
1035 d->n_operands = stats.num_insn_operands;
1036 d->n_dups = 0;
1037 d->n_alternatives = 0;
1038 d->template_code = 0;
1039 d->output_format = INSN_OUTPUT_FORMAT_NONE;
1041 place_operands (d);
1044 static void
1045 init_insn_for_nothing (void)
1047 memset (&nothing, 0, sizeof (nothing));
1048 nothing.name = "*placeholder_for_nothing";
1049 nothing.filename = "<internal>";
1052 extern int main (int, char **);
1055 main (int argc, char **argv)
1057 rtx desc;
1059 progname = "genoutput";
1061 init_insn_for_nothing ();
1063 if (!init_rtx_reader_args (argc, argv))
1064 return (FATAL_EXIT_CODE);
1066 output_prologue ();
1067 next_index_number = 0;
1069 /* Read the machine description. */
1071 while (1)
1073 int line_no;
1075 desc = read_md_rtx (&line_no, &next_code_number);
1076 if (desc == NULL)
1077 break;
1079 switch (GET_CODE (desc))
1081 case DEFINE_INSN:
1082 gen_insn (desc, line_no);
1083 break;
1085 case DEFINE_PEEPHOLE:
1086 gen_peephole (desc, line_no);
1087 break;
1089 case DEFINE_EXPAND:
1090 gen_expand (desc, line_no);
1091 break;
1093 case DEFINE_SPLIT:
1094 case DEFINE_PEEPHOLE2:
1095 gen_split (desc, line_no);
1096 break;
1098 case DEFINE_CONSTRAINT:
1099 case DEFINE_REGISTER_CONSTRAINT:
1100 case DEFINE_ADDRESS_CONSTRAINT:
1101 case DEFINE_MEMORY_CONSTRAINT:
1102 note_constraint (desc, line_no);
1103 break;
1105 default:
1106 break;
1108 next_index_number++;
1111 printf ("\n\n");
1112 output_operand_data ();
1113 output_insn_data ();
1114 output_get_insn_name ();
1116 fflush (stdout);
1117 return (ferror (stdout) != 0 || have_error
1118 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
1121 /* Return the number of occurrences of character C in string S or
1122 -1 if S is the null string. */
1124 static int
1125 n_occurrences (int c, const char *s)
1127 int n = 0;
1129 if (s == 0 || *s == '\0')
1130 return -1;
1132 while (*s)
1133 n += (*s++ == c);
1135 return n;
1138 /* Remove whitespace in `s' by moving up characters until the end.
1139 Return a new string. */
1141 static const char *
1142 strip_whitespace (const char *s)
1144 char *p, *q;
1145 char ch;
1147 if (s == 0)
1148 return 0;
1150 p = q = XNEWVEC (char, strlen (s) + 1);
1151 while ((ch = *s++) != '\0')
1152 if (! ISSPACE (ch))
1153 *p++ = ch;
1155 *p = '\0';
1156 return q;
1159 /* Record just enough information about a constraint to allow checking
1160 of operand constraint strings above, in validate_insn_alternatives.
1161 Does not validate most properties of the constraint itself; does
1162 enforce no duplicate names, no overlap with MI constraints, and no
1163 prefixes. EXP is the define_*constraint form, LINENO the line number
1164 reported by the reader. */
1165 static void
1166 note_constraint (rtx exp, int lineno)
1168 const char *name = XSTR (exp, 0);
1169 struct constraint_data **iter, **slot, *new_cdata;
1171 if (strcmp (name, "TARGET_MEM_CONSTRAINT") == 0)
1172 name = general_mem;
1173 unsigned int namelen = strlen (name);
1175 if (strchr (indep_constraints, name[0]))
1177 if (name[1] == '\0')
1178 error_with_line (lineno, "constraint letter '%s' cannot be "
1179 "redefined by the machine description", name);
1180 else
1181 error_with_line (lineno, "constraint name '%s' cannot be defined by "
1182 "the machine description, as it begins with '%c'",
1183 name, name[0]);
1184 return;
1187 slot = &constraints_by_letter_table[(unsigned int)name[0]];
1188 for (iter = slot; *iter; iter = &(*iter)->next_this_letter)
1190 /* This causes slot to end up pointing to the
1191 next_this_letter field of the last constraint with a name
1192 of equal or greater length than the new constraint; hence
1193 the new constraint will be inserted after all previous
1194 constraints with names of the same length. */
1195 if ((*iter)->namelen >= namelen)
1196 slot = iter;
1198 if (!strcmp ((*iter)->name, name))
1200 error_with_line (lineno, "redefinition of constraint '%s'", name);
1201 message_with_line ((*iter)->lineno, "previous definition is here");
1202 return;
1204 else if (!strncmp ((*iter)->name, name, (*iter)->namelen))
1206 error_with_line (lineno, "defining constraint '%s' here", name);
1207 message_with_line ((*iter)->lineno, "renders constraint '%s' "
1208 "(defined here) a prefix", (*iter)->name);
1209 return;
1211 else if (!strncmp ((*iter)->name, name, namelen))
1213 error_with_line (lineno, "constraint '%s' is a prefix", name);
1214 message_with_line ((*iter)->lineno, "of constraint '%s' "
1215 "(defined here)", (*iter)->name);
1216 return;
1219 new_cdata = XNEWVAR (struct constraint_data, sizeof (struct constraint_data) + namelen);
1220 strcpy (CONST_CAST (char *, new_cdata->name), name);
1221 new_cdata->namelen = namelen;
1222 new_cdata->lineno = lineno;
1223 new_cdata->next_this_letter = *slot;
1224 *slot = new_cdata;
1227 /* Return the length of the constraint name beginning at position S
1228 of an operand constraint string, or issue an error message if there
1229 is no such constraint. Does not expect to be called for generic
1230 constraints. */
1231 static int
1232 mdep_constraint_len (const char *s, int lineno, int opno)
1234 struct constraint_data *p;
1236 p = constraints_by_letter_table[(unsigned int)s[0]];
1238 if (p)
1239 for (; p; p = p->next_this_letter)
1240 if (!strncmp (s, p->name, p->namelen))
1241 return p->namelen;
1243 error_with_line (lineno,
1244 "error: undefined machine-specific constraint "
1245 "at this point: \"%s\"", s);
1246 message_with_line (lineno, "note: in operand %d", opno);
1247 return 1; /* safe */