1 /* Support routines for the various generation passes.
2 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
26 #include "gensupport.h"
29 static struct obstack obstack;
30 struct obstack *rtl_obstack = &obstack;
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
35 static int sequence_num;
38 static int predicable_default;
39 static const char *predicable_true;
40 static const char *predicable_false;
42 /* We initially queue all patterns, process the define_insn and
43 define_cond_exec patterns, then return them one at a time. */
49 struct queue_elem *next;
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;
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));
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 *,
76 static void process_one_cond_exec PARAMS ((struct queue_elem *));
77 static void process_define_cond_exec PARAMS ((void));
80 message_with_line VPARAMS ((int lineno, const char *msg, ...))
82 #ifndef ANSI_PROTOTYPES
90 #ifndef ANSI_PROTOTYPES
91 lineno = va_arg (ap, int);
92 msg = va_arg (ap, const char *);
95 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
96 vfprintf (stderr, msg, ap);
102 /* Queue PATTERN on LIST_TAIL. */
105 queue_pattern (pattern, list_tail, lineno)
107 struct queue_elem ***list_tail;
110 struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e));
115 *list_tail = &e->next;
118 /* Recursively remove constraints from an rtx. */
121 remove_constraints (part)
125 register const char *format_ptr;
130 if (GET_CODE (part) == MATCH_OPERAND)
132 else if (GET_CODE (part) == MATCH_SCRATCH)
135 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
137 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
138 switch (*format_ptr++)
142 remove_constraints (XEXP (part, i));
145 if (XVEC (part, i) != NULL)
146 for (j = 0; j < XVECLEN (part, i); j++)
147 remove_constraints (XVECEXP (part, i, j));
152 /* Process a top level rtx in some way, queueing as appropriate. */
155 process_rtx (desc, lineno)
159 switch (GET_CODE (desc))
162 queue_pattern (desc, &define_insn_tail, lineno);
165 case DEFINE_COND_EXEC:
166 queue_pattern (desc, &define_cond_exec_tail, lineno);
170 queue_pattern (desc, &define_attr_tail, lineno);
173 case DEFINE_INSN_AND_SPLIT:
175 const char *split_cond;
179 /* Create a split with values from the insn_and_split. */
180 split = rtx_alloc (DEFINE_SPLIT);
182 i = XVECLEN (desc, 1);
183 XEXP (split, 0) = rtvec_alloc (i);
186 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
187 remove_constraints (XVECEXP (split, 0, i));
190 /* If the split condition starts with "&&", append it to the
191 insn condition to create the new split condition. */
192 split_cond = XSTR (desc, 4);
193 if (split_cond[0] == '&' && split_cond[1] == '&')
195 const char *insn_cond = XSTR (desc, 2);
196 size_t insn_cond_len = strlen (insn_cond);
197 size_t split_cond_len = strlen (split_cond);
200 combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1);
201 memcpy (combined, insn_cond, insn_cond_len);
202 memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1);
204 split_cond = combined;
206 XSTR (split, 1) = split_cond;
207 XVEC (split, 2) = XVEC (desc, 5);
208 XSTR (split, 3) = XSTR (desc, 6);
210 /* Fix up the DEFINE_INSN. */
211 PUT_CODE (desc, DEFINE_INSN);
212 XVEC (desc, 4) = XVEC (desc, 7);
215 queue_pattern (desc, &define_insn_tail, lineno);
216 queue_pattern (split, &other_tail, lineno);
221 queue_pattern (desc, &other_tail, lineno);
226 /* Return true if attribute PREDICABLE is true for ELEM, which holds
231 struct queue_elem *elem;
233 rtvec vec = XVEC (elem->data, 4);
238 return predicable_default;
240 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
242 rtx sub = RTVEC_ELT (vec, i);
243 switch (GET_CODE (sub))
246 if (strcmp (XSTR (sub, 0), "predicable") == 0)
248 value = XSTR (sub, 1);
253 case SET_ATTR_ALTERNATIVE:
254 if (strcmp (XSTR (sub, 0), "predicable") == 0)
256 message_with_line (elem->lineno,
257 "multiple alternatives for `predicable'");
264 if (GET_CODE (SET_DEST (sub)) != ATTR
265 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
268 if (GET_CODE (sub) == CONST_STRING)
270 value = XSTR (sub, 0);
274 /* ??? It would be possible to handle this if we really tried.
275 It's not easy though, and I'm not going to bother until it
276 really proves necessary. */
277 message_with_line (elem->lineno,
278 "non-constant value for `predicable'");
287 return predicable_default;
290 /* Verify that predicability does not vary on the alternative. */
291 /* ??? It should be possible to handle this by simply eliminating
292 the non-predicable alternatives from the insn. FRV would like
293 to do this. Delay this until we've got the basics solid. */
294 if (strchr (value, ',') != NULL)
296 message_with_line (elem->lineno,
297 "multiple alternatives for `predicable'");
302 /* Find out which value we're looking at. */
303 if (strcmp (value, predicable_true) == 0)
305 if (strcmp (value, predicable_false) == 0)
308 message_with_line (elem->lineno,
309 "Unknown value `%s' for `predicable' attribute",
315 /* Examine the attribute "predicable"; discover its boolean values
319 identify_predicable_attribute ()
321 struct queue_elem *elem;
326 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
327 for (elem = define_attr_queue; elem ; elem = elem->next)
328 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
331 message_with_line (define_cond_exec_queue->lineno,
332 "Attribute `predicable' not defined");
337 value = XSTR (elem->data, 1);
338 len = strlen (value);
339 false = (char *) xmalloc (len + 1);
340 memcpy (false, value, len + 1);
342 true = strchr (false, ',');
343 if (true == NULL || strchr (++true, ',') != NULL)
345 message_with_line (elem->lineno,
346 "Attribute `predicable' is not a boolean");
352 predicable_true = true;
353 predicable_false = false;
355 switch (GET_CODE (XEXP (elem->data, 2)))
358 value = XSTR (XEXP (elem->data, 2), 0);
362 message_with_line (elem->lineno,
363 "Attribute `predicable' cannot be const");
368 message_with_line (elem->lineno,
369 "Attribute `predicable' must have a constant default");
374 if (strcmp (value, true) == 0)
375 predicable_default = 1;
376 else if (strcmp (value, false) == 0)
377 predicable_default = 0;
380 message_with_line (elem->lineno,
381 "Unknown value `%s' for `predicable' attribute",
387 /* Return the number of alternatives in constraint S. */
402 /* Determine how many alternatives there are in INSN, and how many
406 collect_insn_data (pattern, palt, pmax)
414 code = GET_CODE (pattern);
418 *palt = n_alternatives (XSTR (pattern, 2));
425 i = XINT (pattern, 0);
434 fmt = GET_RTX_FORMAT (code);
435 len = GET_RTX_LENGTH (code);
436 for (i = 0; i < len; i++)
441 collect_insn_data (XEXP (pattern, i), palt, pmax);
445 if (XVEC (pattern, i) == NULL)
449 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
450 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
453 case 'i': case 'w': case '0': case 's': case 'S':
463 alter_predicate_for_insn (pattern, alt, max_op, lineno)
465 int alt, max_op, lineno;
471 code = GET_CODE (pattern);
476 const char *c = XSTR (pattern, 2);
478 if (n_alternatives (c) != 1)
480 message_with_line (lineno,
481 "Too many alternatives for operand %d",
487 /* Replicate C as needed to fill out ALT alternatives. */
488 if (c && *c && alt > 1)
490 size_t c_len = strlen (c);
491 size_t len = alt * (c_len + 1);
492 char *new_c = (char *) xmalloc (len);
494 memcpy (new_c, c, c_len);
495 for (i = 1; i < alt; ++i)
497 new_c[i * (c_len + 1) - 1] = ',';
498 memcpy (&new_c[i * (c_len + 1)], c, c_len);
500 new_c[len - 1] = '\0';
501 XSTR (pattern, 2) = new_c;
510 XINT (pattern, 0) += max_op;
517 fmt = GET_RTX_FORMAT (code);
518 len = GET_RTX_LENGTH (code);
519 for (i = 0; i < len; i++)
526 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
533 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
535 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
536 alt, max_op, lineno);
542 case 'i': case 'w': case '0': case 's':
554 alter_test_for_insn (ce_elem, insn_elem)
555 struct queue_elem *ce_elem, *insn_elem;
557 const char *ce_test, *insn_test;
559 size_t len, ce_len, insn_len;
561 ce_test = XSTR (ce_elem->data, 1);
562 insn_test = XSTR (insn_elem->data, 2);
563 if (!ce_test || *ce_test == '\0')
565 if (!insn_test || *insn_test == '\0')
568 ce_len = strlen (ce_test);
569 insn_len = strlen (insn_test);
570 len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1;
571 new_test = (char *) xmalloc (len);
573 sprintf (new_test, "(%s) && (%s)", ce_test, insn_test);
578 /* Adjust all of the operand numbers in OLD to match the shift they'll
579 get from an operand displacement of DISP. Return a pointer after the
583 shift_output_template (new, old, disp)
595 if (ISDIGIT ((unsigned char) c))
597 else if (ISUPPER ((unsigned char) c)
598 || ISLOWER ((unsigned char) c))
611 alter_output_for_insn (ce_elem, insn_elem, alt, max_op)
612 struct queue_elem *ce_elem, *insn_elem;
615 const char *ce_out, *insn_out;
617 size_t len, ce_len, insn_len;
619 /* ??? Could coordinate with genoutput to not duplicate code here. */
621 ce_out = XSTR (ce_elem->data, 2);
622 insn_out = XSTR (insn_elem->data, 3);
623 if (!ce_out || *ce_out == '\0')
626 ce_len = strlen (ce_out);
627 insn_len = strlen (insn_out);
629 if (*insn_out == '*')
630 /* You must take care of the predicate yourself. */
633 if (*insn_out == '@')
635 len = (ce_len + 1) * alt + insn_len + 1;
636 p = new = xmalloc (len);
642 while (ISSPACE ((unsigned char) *insn_out));
644 if (*insn_out != '#')
646 p = shift_output_template (p, ce_out, max_op);
652 while (*insn_out && *insn_out != '\n');
659 len = ce_len + 1 + insn_len + 1;
662 p = shift_output_template (new, ce_out, max_op);
664 memcpy (p, insn_out, insn_len + 1);
670 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
673 process_one_cond_exec (ce_elem)
674 struct queue_elem *ce_elem;
676 struct queue_elem *insn_elem;
677 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
679 int alternatives, max_operand;
680 rtx pred, insn, pattern;
682 if (! is_predicable (insn_elem))
687 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
690 if (XVECLEN (ce_elem->data, 0) != 1)
692 message_with_line (ce_elem->lineno,
693 "too many patterns in predicate");
698 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
699 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
704 /* Construct a new pattern for the new insn. */
705 insn = copy_rtx (insn_elem->data);
707 pattern = rtx_alloc (COND_EXEC);
708 XEXP (pattern, 0) = pred;
709 if (XVECLEN (insn, 1) == 1)
711 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
712 XVECEXP (insn, 1, 0) = pattern;
713 PUT_NUM_ELEM (XVEC (insn, 1), 1);
717 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
718 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
719 XVEC (insn, 1) = rtvec_alloc (1);
720 XVECEXP (insn, 1, 0) = pattern;
723 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
724 XSTR (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
725 alternatives, max_operand);
727 /* ??? Set `predicable' to false. Not crucial since it's really
728 only used here, and we won't reprocess this new pattern. */
730 /* Put the new pattern on the `other' list so that it
731 (a) is not reprocessed by other define_cond_exec patterns
732 (b) appears after all normal define_insn patterns.
734 ??? B is debatable. If one has normal insns that match
735 cond_exec patterns, they will be preferred over these
736 generated patterns. Whether this matters in practice, or if
737 it's a good thing, or whether we should thread these new
738 patterns into the define_insn chain just after their generator
739 is something we'll have to experiment with. */
741 queue_pattern (insn, &other_tail, insn_elem->lineno);
745 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
746 patterns appropriately. */
749 process_define_cond_exec ()
751 struct queue_elem *elem;
753 identify_predicable_attribute ();
757 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
758 process_one_cond_exec (elem);
761 /* The entry point for initializing the reader. */
764 init_md_reader (filename)
765 const char *filename;
770 read_rtx_filename = filename;
771 input_file = fopen (filename, "r");
775 return FATAL_EXIT_CODE;
778 obstack_init (rtl_obstack);
782 /* Read the entire file. */
788 c = read_skip_spaces (input_file);
792 ungetc (c, input_file);
793 lineno = read_rtx_lineno;
794 desc = read_rtx (input_file);
795 process_rtx (desc, lineno);
799 /* Process define_cond_exec patterns. */
800 if (define_cond_exec_queue != NULL)
801 process_define_cond_exec ();
803 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
806 /* The entry point for reading a single rtx from an md file. */
809 read_md_rtx (lineno, seqnr)
813 struct queue_elem **queue, *elem;
816 /* Read all patterns from a given queue before moving on to the next. */
817 if (define_attr_queue != NULL)
818 queue = &define_attr_queue;
819 else if (define_insn_queue != NULL)
820 queue = &define_insn_queue;
821 else if (other_queue != NULL)
822 queue = &other_queue;
829 *lineno = elem->lineno;
830 *seqnr = sequence_num;
834 switch (GET_CODE (desc))
839 case DEFINE_PEEPHOLE:
840 case DEFINE_PEEPHOLE2: