rtl.def (DEFINE_COND_EXEC): New.
[gcc.git] / gcc / gensupport.c
1 /* Support routines for the various generation passes.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "hconfig.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "obstack.h"
25 #include "errors.h"
26 #include "gensupport.h"
27
28
29 static struct obstack obstack;
30 struct obstack *rtl_obstack = &obstack;
31
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
34
35 static int sequence_num;
36 static int errors;
37
38 static int predicable_default;
39 static const char *predicable_true;
40 static const char *predicable_false;
41
42 /* We initially queue all patterns, process the define_insn and
43 define_cond_exec patterns, then return them one at a time. */
44
45 struct queue_elem
46 {
47 rtx data;
48 int lineno;
49 struct queue_elem *next;
50 };
51
52 static struct queue_elem *define_attr_queue;
53 static struct queue_elem **define_attr_tail = &define_attr_queue;
54 static struct queue_elem *define_insn_queue;
55 static struct queue_elem **define_insn_tail = &define_insn_queue;
56 static struct queue_elem *define_cond_exec_queue;
57 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
58 static struct queue_elem *other_queue;
59 static struct queue_elem **other_tail = &other_queue;
60
61 static void queue_pattern PARAMS ((rtx, struct queue_elem ***, int));
62 static void remove_constraints PARAMS ((rtx));
63 static void process_rtx PARAMS ((rtx, int));
64
65 static int is_predicable PARAMS ((struct queue_elem *));
66 static void identify_predicable_attribute PARAMS ((void));
67 static int n_alternatives PARAMS ((const char *));
68 static void collect_insn_data PARAMS ((rtx, int *, int *));
69 static rtx alter_predicate_for_insn PARAMS ((rtx, int, int, int));
70 static const char *alter_test_for_insn PARAMS ((struct queue_elem *,
71 struct queue_elem *));
72 static char *shift_output_template PARAMS ((char *, const char *, int));
73 static const char *alter_output_for_insn PARAMS ((struct queue_elem *,
74 struct queue_elem *,
75 int, int));
76 static void process_one_cond_exec PARAMS ((struct queue_elem *));
77 static void process_define_cond_exec PARAMS ((void));
78 \f
79 void
80 message_with_line VPARAMS ((int lineno, const char *msg, ...))
81 {
82 #ifndef ANSI_PROTOTYPES
83 int lineno;
84 const char *msg;
85 #endif
86 va_list ap;
87
88 VA_START (ap, msg);
89
90 #ifndef ANSI_PROTOTYPES
91 lineno = va_arg (ap, int);
92 msg = va_arg (ap, const char *);
93 #endif
94
95 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
96 vfprintf (stderr, msg, ap);
97 fputc ('\n', stderr);
98
99 va_end (ap);
100 }
101 \f
102 /* Queue PATTERN on LIST_TAIL. */
103
104 static void
105 queue_pattern (pattern, list_tail, lineno)
106 rtx pattern;
107 struct queue_elem ***list_tail;
108 int lineno;
109 {
110 struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e));
111 e->data = pattern;
112 e->lineno = lineno;
113 e->next = NULL;
114 **list_tail = e;
115 *list_tail = &e->next;
116 }
117
118 /* Recursively remove constraints from an rtx. */
119
120 static void
121 remove_constraints (part)
122 rtx part;
123 {
124 register int i, j;
125 register const char *format_ptr;
126
127 if (part == 0)
128 return;
129
130 if (GET_CODE (part) == MATCH_OPERAND)
131 XSTR (part, 2) = "";
132 else if (GET_CODE (part) == MATCH_SCRATCH)
133 XSTR (part, 1) = "";
134
135 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
136
137 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
138 switch (*format_ptr++)
139 {
140 case 'e':
141 case 'u':
142 remove_constraints (XEXP (part, i));
143 break;
144 case 'E':
145 if (XVEC (part, i) != NULL)
146 for (j = 0; j < XVECLEN (part, i); j++)
147 remove_constraints (XVECEXP (part, i, j));
148 break;
149 }
150 }
151
152 /* Process a top level rtx in some way, queueing as appropriate. */
153
154 static void
155 process_rtx (desc, lineno)
156 rtx desc;
157 int lineno;
158 {
159 switch (GET_CODE (desc))
160 {
161 case DEFINE_INSN:
162 queue_pattern (desc, &define_insn_tail, lineno);
163 break;
164
165 case DEFINE_COND_EXEC:
166 queue_pattern (desc, &define_cond_exec_tail, lineno);
167 break;
168
169 case DEFINE_ATTR:
170 queue_pattern (desc, &define_attr_tail, lineno);
171 break;
172
173 case DEFINE_INSN_AND_SPLIT:
174 {
175 const char *split_cond;
176 rtx split;
177
178 /* Create a split with values from the insn_and_split. */
179 split = rtx_alloc (DEFINE_SPLIT);
180 XEXP (split, 0) = copy_rtx (XEXP (desc, 1));
181 remove_constraints (XEXP (split, 0));
182
183 /* If the split condition starts with "&&", append it to the
184 insn condition to create the new split condition. */
185 split_cond = XSTR (desc, 4);
186 if (split_cond[0] == '&' && split_cond[1] == '&')
187 {
188 const char *insn_cond = XSTR (desc, 2);
189 size_t insn_cond_len = strlen (insn_cond);
190 size_t split_cond_len = strlen (split_cond);
191 char *combined;
192
193 combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1);
194 memcpy (combined, insn_cond, insn_cond_len);
195 memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1);
196
197 split_cond = combined;
198 }
199 XSTR (split, 1) = split_cond;
200 XVEC (split, 2) = XVEC (desc, 5);
201 XSTR (split, 3) = XSTR (desc, 6);
202
203 /* Fix up the DEFINE_INSN. */
204 PUT_CODE (desc, DEFINE_INSN);
205 XVEC (desc, 4) = XVEC (desc, 7);
206
207 /* Queue them. */
208 queue_pattern (desc, &define_insn_tail, lineno);
209 queue_pattern (split, &other_tail, lineno);
210 break;
211 }
212
213 default:
214 queue_pattern (desc, &other_tail, lineno);
215 break;
216 }
217 }
218 \f
219 /* Return true if attribute PREDICABLE is true for ELEM, which holds
220 a DEFINE_INSN. */
221
222 static int
223 is_predicable (elem)
224 struct queue_elem *elem;
225 {
226 rtvec vec = XVEC (elem->data, 4);
227 const char *value;
228 int i;
229
230 if (! vec)
231 return predicable_default;
232
233 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
234 {
235 rtx sub = RTVEC_ELT (vec, i);
236 switch (GET_CODE (sub))
237 {
238 case SET_ATTR:
239 if (strcmp (XSTR (sub, 0), "predicable") == 0)
240 {
241 value = XSTR (sub, 1);
242 goto found;
243 }
244 break;
245
246 case SET_ATTR_ALTERNATIVE:
247 if (strcmp (XSTR (sub, 0), "predicable") == 0)
248 {
249 message_with_line (elem->lineno,
250 "multiple alternatives for `predicable'");
251 errors = 1;
252 return 0;
253 }
254 break;
255
256 case SET:
257 if (GET_CODE (SET_DEST (sub)) != ATTR
258 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
259 break;
260 sub = SET_SRC (sub);
261 if (GET_CODE (sub) == CONST_STRING)
262 {
263 value = XSTR (sub, 0);
264 goto found;
265 }
266
267 /* ??? It would be possible to handle this if we really tried.
268 It's not easy though, and I'm not going to bother until it
269 really proves necessary. */
270 message_with_line (elem->lineno,
271 "non-constant value for `predicable'");
272 errors = 1;
273 return 0;
274
275 default:
276 abort ();
277 }
278 }
279
280 return predicable_default;
281
282 found:
283 /* Verify that predicability does not vary on the alternative. */
284 /* ??? It should be possible to handle this by simply eliminating
285 the non-predicable alternatives from the insn. FRV would like
286 to do this. Delay this until we've got the basics solid. */
287 if (strchr (value, ',') != NULL)
288 {
289 message_with_line (elem->lineno,
290 "multiple alternatives for `predicable'");
291 errors = 1;
292 return 0;
293 }
294
295 /* Find out which value we're looking at. */
296 if (strcmp (value, predicable_true) == 0)
297 return 1;
298 if (strcmp (value, predicable_false) == 0)
299 return 0;
300
301 message_with_line (elem->lineno,
302 "Unknown value `%s' for `predicable' attribute",
303 value);
304 errors = 1;
305 return 0;
306 }
307
308 /* Examine the attribute "predicable"; discover its boolean values
309 and its default. */
310
311 static void
312 identify_predicable_attribute ()
313 {
314 struct queue_elem *elem;
315 char *true, *false;
316 const char *value;
317 size_t len;
318
319 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
320 for (elem = define_attr_queue; elem ; elem = elem->next)
321 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
322 goto found;
323
324 message_with_line (define_cond_exec_queue->lineno,
325 "Attribute `predicable' not defined");
326 errors = 1;
327 return;
328
329 found:
330 value = XSTR (elem->data, 1);
331 len = strlen (value);
332 false = (char *) xmalloc (len + 1);
333 memcpy (false, value, len + 1);
334
335 true = strchr (false, ',');
336 if (true == NULL || strchr (++true, ',') != NULL)
337 {
338 message_with_line (elem->lineno,
339 "Attribute `predicable' is not a boolean");
340 errors = 1;
341 return;
342 }
343 true[-1] = '\0';
344
345 predicable_true = true;
346 predicable_false = false;
347
348 switch (GET_CODE (XEXP (elem->data, 2)))
349 {
350 case CONST_STRING:
351 value = XSTR (XEXP (elem->data, 2), 0);
352 break;
353
354 case CONST:
355 message_with_line (elem->lineno,
356 "Attribute `predicable' cannot be const");
357 errors = 1;
358 return;
359
360 default:
361 message_with_line (elem->lineno,
362 "Attribute `predicable' must have a constant default");
363 errors = 1;
364 return;
365 }
366
367 if (strcmp (value, true) == 0)
368 predicable_default = 1;
369 else if (strcmp (value, false) == 0)
370 predicable_default = 0;
371 else
372 {
373 message_with_line (elem->lineno,
374 "Unknown value `%s' for `predicable' attribute",
375 value);
376 errors = 1;
377 }
378 }
379
380 /* Return the number of alternatives in constraint S. */
381
382 static int
383 n_alternatives (s)
384 const char *s;
385 {
386 int n = 1;
387
388 if (s)
389 while (*s)
390 n += (*s++ == ',');
391
392 return n;
393 }
394
395 /* Determine how many alternatives there are in INSN, and how many
396 operands. */
397
398 static void
399 collect_insn_data (pattern, palt, pmax)
400 rtx pattern;
401 int *palt, *pmax;
402 {
403 const char *fmt;
404 enum rtx_code code;
405 int i, j, len;
406
407 code = GET_CODE (pattern);
408 switch (code)
409 {
410 case MATCH_OPERAND:
411 *palt = n_alternatives (XSTR (pattern, 2));
412 /* FALLTHRU */
413
414 case MATCH_OPERATOR:
415 case MATCH_SCRATCH:
416 case MATCH_PARALLEL:
417 case MATCH_INSN:
418 i = XINT (pattern, 0);
419 if (i > *pmax)
420 *pmax = i;
421 break;
422
423 default:
424 break;
425 }
426
427 fmt = GET_RTX_FORMAT (code);
428 len = GET_RTX_LENGTH (code);
429 for (i = 0; i < len; i++)
430 {
431 switch (fmt[i])
432 {
433 case 'e': case 'u':
434 collect_insn_data (XEXP (pattern, i), palt, pmax);
435 break;
436
437 case 'V':
438 if (XVEC (pattern, i) == NULL)
439 break;
440 /* FALLTHRU */
441 case 'E':
442 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
443 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
444 break;
445
446 case 'i': case 'w': case '0': case 's': case 'S':
447 break;
448
449 default:
450 abort ();
451 }
452 }
453 }
454
455 static rtx
456 alter_predicate_for_insn (pattern, alt, max_op, lineno)
457 rtx pattern;
458 int alt, max_op, lineno;
459 {
460 const char *fmt;
461 enum rtx_code code;
462 int i, j, len;
463
464 code = GET_CODE (pattern);
465 switch (code)
466 {
467 case MATCH_OPERAND:
468 {
469 const char *c = XSTR (pattern, 2);
470
471 if (n_alternatives (c) != 1)
472 {
473 message_with_line (lineno,
474 "Too many alternatives for operand %d",
475 XINT (pattern, 0));
476 errors = 1;
477 return NULL;
478 }
479
480 /* Replicate C as needed to fill out ALT alternatives. */
481 if (c && *c && alt > 1)
482 {
483 size_t c_len = strlen (c);
484 size_t len = alt * (c_len + 1);
485 char *new_c = (char *) xmalloc (len);
486
487 memcpy (new_c, c, c_len);
488 for (i = 1; i < alt; ++i)
489 {
490 new_c[i * (c_len + 1) - 1] = ',';
491 memcpy (&new_c[i * (c_len + 1)], c, c_len);
492 }
493 new_c[len - 1] = '\0';
494 XSTR (pattern, 2) = new_c;
495 }
496 }
497 /* FALLTHRU */
498
499 case MATCH_OPERATOR:
500 case MATCH_SCRATCH:
501 case MATCH_PARALLEL:
502 case MATCH_INSN:
503 XINT (pattern, 0) += max_op;
504 break;
505
506 default:
507 break;
508 }
509
510 fmt = GET_RTX_FORMAT (code);
511 len = GET_RTX_LENGTH (code);
512 for (i = 0; i < len; i++)
513 {
514 rtx r;
515
516 switch (fmt[i])
517 {
518 case 'e': case 'u':
519 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
520 max_op, lineno);
521 if (r == NULL)
522 return r;
523 break;
524
525 case 'E':
526 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
527 {
528 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
529 alt, max_op, lineno);
530 if (r == NULL)
531 return r;
532 }
533 break;
534
535 case 'i': case 'w': case '0': case 's':
536 break;
537
538 default:
539 abort ();
540 }
541 }
542
543 return pattern;
544 }
545
546 static const char *
547 alter_test_for_insn (ce_elem, insn_elem)
548 struct queue_elem *ce_elem, *insn_elem;
549 {
550 const char *ce_test, *insn_test;
551 char *new_test;
552 size_t len, ce_len, insn_len;
553
554 ce_test = XSTR (ce_elem->data, 1);
555 insn_test = XSTR (insn_elem->data, 2);
556 if (!ce_test || *ce_test == '\0')
557 return insn_test;
558 if (!insn_test || *insn_test == '\0')
559 return ce_test;
560
561 ce_len = strlen (ce_test);
562 insn_len = strlen (insn_test);
563 len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1;
564 new_test = (char *) xmalloc (len);
565
566 sprintf (new_test, "(%s) && (%s)", ce_test, insn_test);
567
568 return new_test;
569 }
570
571 /* Adjust all of the operand numbers in OLD to match the shift they'll
572 get from an operand displacement of DISP. Return a pointer after the
573 adjusted string. */
574
575 static char *
576 shift_output_template (new, old, disp)
577 char *new;
578 const char *old;
579 int disp;
580 {
581 while (*old)
582 {
583 char c = *old++;
584 *new++ = c;
585 if (c == '%')
586 {
587 c = *old++;
588 if (ISDIGIT ((unsigned char) c))
589 c += disp;
590 else if (ISUPPER ((unsigned char) c)
591 || ISLOWER ((unsigned char) c))
592 {
593 *new++ = c;
594 c = *old++ + disp;
595 }
596 *new++ = c;
597 }
598 }
599
600 return new;
601 }
602
603 static const char *
604 alter_output_for_insn (ce_elem, insn_elem, alt, max_op)
605 struct queue_elem *ce_elem, *insn_elem;
606 int alt, max_op;
607 {
608 const char *ce_out, *insn_out;
609 char *new, *p;
610 size_t len, ce_len, insn_len;
611
612 /* ??? Could coordinate with genoutput to not duplicate code here. */
613
614 ce_out = XSTR (ce_elem->data, 2);
615 insn_out = XSTR (insn_elem->data, 3);
616 if (!ce_out || *ce_out == '\0')
617 return insn_out;
618
619 ce_len = strlen (ce_out);
620 insn_len = strlen (insn_out);
621
622 if (*insn_out == '*')
623 /* You must take care of the predicate yourself. */
624 return insn_out;
625
626 if (*insn_out == '@')
627 {
628 len = (ce_len + 1) * alt + insn_len + 1;
629 p = new = xmalloc (len);
630
631 do
632 {
633 do
634 *p++ = *insn_out++;
635 while (ISSPACE ((unsigned char) *insn_out));
636
637 if (*insn_out != '#')
638 {
639 p = shift_output_template (p, ce_out, max_op);
640 *p++ = ' ';
641 }
642
643 do
644 *p++ = *insn_out++;
645 while (*insn_out && *insn_out != '\n');
646 }
647 while (*insn_out);
648 *p = '\0';
649 }
650 else
651 {
652 len = ce_len + 1 + insn_len + 1;
653 new = xmalloc (len);
654
655 p = shift_output_template (new, ce_out, max_op);
656 *p++ = ' ';
657 memcpy (p, insn_out, insn_len + 1);
658 }
659
660 return new;
661 }
662
663 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
664
665 static void
666 process_one_cond_exec (ce_elem)
667 struct queue_elem *ce_elem;
668 {
669 struct queue_elem *insn_elem;
670 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
671 {
672 int alternatives, max_operand;
673 rtx pred, insn, pattern;
674
675 if (! is_predicable (insn_elem))
676 continue;
677
678 alternatives = 1;
679 max_operand = -1;
680 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
681 max_operand += 1;
682
683 if (XVECLEN (ce_elem->data, 0) != 1)
684 {
685 message_with_line (ce_elem->lineno,
686 "too many patterns in predicate");
687 errors = 1;
688 return;
689 }
690
691 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
692 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
693 ce_elem->lineno);
694 if (pred == NULL)
695 return;
696
697 /* Construct a new pattern for the new insn. */
698 insn = copy_rtx (insn_elem->data);
699 XSTR (insn, 0) = "";
700 pattern = rtx_alloc (COND_EXEC);
701 XEXP (pattern, 0) = pred;
702 if (XVECLEN (insn, 1) == 1)
703 {
704 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
705 XVECEXP (insn, 1, 0) = pattern;
706 PUT_NUM_ELEM (XVEC (insn, 1), 1);
707 }
708 else
709 {
710 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
711 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
712 XVEC (insn, 1) = rtvec_alloc (1);
713 XVECEXP (insn, 1, 0) = pattern;
714 }
715
716 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
717 XSTR (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
718 alternatives, max_operand);
719
720 /* ??? Set `predicable' to false. Not crucial since it's really
721 only used here, and we won't reprocess this new pattern. */
722
723 /* Put the new pattern on the `other' list so that it
724 (a) is not reprocessed by other define_cond_exec patterns
725 (b) appears after all normal define_insn patterns.
726
727 ??? B is debatable. If one has normal insns that match
728 cond_exec patterns, they will be preferred over these
729 generated patterns. Whether this matters in practice, or if
730 it's a good thing, or whether we should thread these new
731 patterns into the define_insn chain just after their generator
732 is something we'll have to experiment with. */
733
734 queue_pattern (insn, &other_tail, insn_elem->lineno);
735 }
736 }
737
738 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
739 patterns appropriately. */
740
741 static void
742 process_define_cond_exec ()
743 {
744 struct queue_elem *elem;
745
746 identify_predicable_attribute ();
747 if (errors)
748 return;
749
750 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
751 process_one_cond_exec (elem);
752 }
753 \f
754 /* The entry point for initializing the reader. */
755
756 int
757 init_md_reader (filename)
758 const char *filename;
759 {
760 FILE *input_file;
761 int c;
762
763 read_rtx_filename = filename;
764 input_file = fopen (filename, "r");
765 if (input_file == 0)
766 {
767 perror (filename);
768 return FATAL_EXIT_CODE;
769 }
770
771 obstack_init (rtl_obstack);
772 errors = 0;
773 sequence_num = 0;
774
775 /* Read the entire file. */
776 while (1)
777 {
778 rtx desc;
779 int lineno;
780
781 c = read_skip_spaces (input_file);
782 if (c == EOF)
783 break;
784
785 ungetc (c, input_file);
786 lineno = read_rtx_lineno;
787 desc = read_rtx (input_file);
788 process_rtx (desc, lineno);
789 }
790 fclose (input_file);
791
792 /* Process define_cond_exec patterns. */
793 if (define_cond_exec_queue != NULL)
794 process_define_cond_exec ();
795
796 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
797 }
798
799 /* The entry point for reading a single rtx from an md file. */
800
801 rtx
802 read_md_rtx (lineno, seqnr)
803 int *lineno;
804 int *seqnr;
805 {
806 struct queue_elem **queue, *elem;
807 rtx desc;
808
809 /* Read all patterns from a given queue before moving on to the next. */
810 if (define_attr_queue != NULL)
811 queue = &define_attr_queue;
812 else if (define_insn_queue != NULL)
813 queue = &define_insn_queue;
814 else if (other_queue != NULL)
815 queue = &other_queue;
816 else
817 return NULL_RTX;
818
819 elem = *queue;
820 *queue = elem->next;
821 desc = elem->data;
822 *lineno = elem->lineno;
823 *seqnr = sequence_num;
824
825 free (elem);
826
827 switch (GET_CODE (desc))
828 {
829 case DEFINE_INSN:
830 case DEFINE_EXPAND:
831 case DEFINE_SPLIT:
832 case DEFINE_PEEPHOLE:
833 case DEFINE_PEEPHOLE2:
834 sequence_num++;
835 break;
836
837 default:
838 break;
839 }
840
841 return desc;
842 }