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