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