1 /* Support routines for the various generation passes.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 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 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "gensupport.h"
29 /* In case some macros used by files we include need it, define this here. */
32 static struct obstack obstack;
33 struct obstack *rtl_obstack = &obstack;
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 static int sequence_num;
41 static int predicable_default;
42 static const char *predicable_true;
43 static const char *predicable_false;
45 /* We initially queue all patterns, process the define_insn and
46 define_cond_exec patterns, then return them one at a time. */
52 struct queue_elem *next;
55 static struct queue_elem *define_attr_queue;
56 static struct queue_elem **define_attr_tail = &define_attr_queue;
57 static struct queue_elem *define_insn_queue;
58 static struct queue_elem **define_insn_tail = &define_insn_queue;
59 static struct queue_elem *define_cond_exec_queue;
60 static struct queue_elem **define_cond_exec_tail = &define_cond_exec_queue;
61 static struct queue_elem *other_queue;
62 static struct queue_elem **other_tail = &other_queue;
64 static void queue_pattern PARAMS ((rtx, struct queue_elem ***, int));
65 static void remove_constraints PARAMS ((rtx));
66 static void process_rtx PARAMS ((rtx, int));
68 static int is_predicable PARAMS ((struct queue_elem *));
69 static void identify_predicable_attribute PARAMS ((void));
70 static int n_alternatives PARAMS ((const char *));
71 static void collect_insn_data PARAMS ((rtx, int *, int *));
72 static rtx alter_predicate_for_insn PARAMS ((rtx, int, int, int));
73 static const char *alter_test_for_insn PARAMS ((struct queue_elem *,
74 struct queue_elem *));
75 static char *shift_output_template PARAMS ((char *, const char *, int));
76 static const char *alter_output_for_insn PARAMS ((struct queue_elem *,
79 static void process_one_cond_exec PARAMS ((struct queue_elem *));
80 static void process_define_cond_exec PARAMS ((void));
83 message_with_line VPARAMS ((int lineno, const char *msg, ...))
86 VA_FIXEDARG (ap, int, lineno);
87 VA_FIXEDARG (ap, const char *, msg);
89 fprintf (stderr, "%s:%d: ", read_rtx_filename, lineno);
90 vfprintf (stderr, msg, ap);
96 /* Make a version of gen_rtx_CONST_INT so that GEN_INT can be used in
97 the gensupport programs. */
100 gen_rtx_CONST_INT (mode, arg)
101 enum machine_mode mode ATTRIBUTE_UNUSED;
104 rtx rt = rtx_alloc (CONST_INT);
110 /* Queue PATTERN on LIST_TAIL. */
113 queue_pattern (pattern, list_tail, lineno)
115 struct queue_elem ***list_tail;
118 struct queue_elem *e = (struct queue_elem *) xmalloc (sizeof (*e));
123 *list_tail = &e->next;
126 /* Recursively remove constraints from an rtx. */
129 remove_constraints (part)
133 const char *format_ptr;
138 if (GET_CODE (part) == MATCH_OPERAND)
140 else if (GET_CODE (part) == MATCH_SCRATCH)
143 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
145 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
146 switch (*format_ptr++)
150 remove_constraints (XEXP (part, i));
153 if (XVEC (part, i) != NULL)
154 for (j = 0; j < XVECLEN (part, i); j++)
155 remove_constraints (XVECEXP (part, i, j));
160 /* Process a top level rtx in some way, queueing as appropriate. */
163 process_rtx (desc, lineno)
167 switch (GET_CODE (desc))
170 queue_pattern (desc, &define_insn_tail, lineno);
173 case DEFINE_COND_EXEC:
174 queue_pattern (desc, &define_cond_exec_tail, lineno);
178 queue_pattern (desc, &define_attr_tail, lineno);
181 case DEFINE_INSN_AND_SPLIT:
183 const char *split_cond;
188 /* Create a split with values from the insn_and_split. */
189 split = rtx_alloc (DEFINE_SPLIT);
191 i = XVECLEN (desc, 1);
192 XVEC (split, 0) = rtvec_alloc (i);
195 XVECEXP (split, 0, i) = copy_rtx (XVECEXP (desc, 1, i));
196 remove_constraints (XVECEXP (split, 0, i));
199 /* If the split condition starts with "&&", append it to the
200 insn condition to create the new split condition. */
201 split_cond = XSTR (desc, 4);
202 if (split_cond[0] == '&' && split_cond[1] == '&')
204 const char *insn_cond = XSTR (desc, 2);
205 size_t insn_cond_len = strlen (insn_cond);
206 size_t split_cond_len = strlen (split_cond);
209 combined = (char *) xmalloc (insn_cond_len + split_cond_len + 1);
210 memcpy (combined, insn_cond, insn_cond_len);
211 memcpy (combined + insn_cond_len, split_cond, split_cond_len + 1);
213 split_cond = combined;
215 XSTR (split, 1) = split_cond;
216 XVEC (split, 2) = XVEC (desc, 5);
217 XSTR (split, 3) = XSTR (desc, 6);
219 /* Fix up the DEFINE_INSN. */
220 attr = XVEC (desc, 7);
221 PUT_CODE (desc, DEFINE_INSN);
222 XVEC (desc, 4) = attr;
225 queue_pattern (desc, &define_insn_tail, lineno);
226 queue_pattern (split, &other_tail, lineno);
231 queue_pattern (desc, &other_tail, lineno);
236 /* Return true if attribute PREDICABLE is true for ELEM, which holds
241 struct queue_elem *elem;
243 rtvec vec = XVEC (elem->data, 4);
248 return predicable_default;
250 for (i = GET_NUM_ELEM (vec) - 1; i >= 0; --i)
252 rtx sub = RTVEC_ELT (vec, i);
253 switch (GET_CODE (sub))
256 if (strcmp (XSTR (sub, 0), "predicable") == 0)
258 value = XSTR (sub, 1);
263 case SET_ATTR_ALTERNATIVE:
264 if (strcmp (XSTR (sub, 0), "predicable") == 0)
266 message_with_line (elem->lineno,
267 "multiple alternatives for `predicable'");
274 if (GET_CODE (SET_DEST (sub)) != ATTR
275 || strcmp (XSTR (SET_DEST (sub), 0), "predicable") != 0)
278 if (GET_CODE (sub) == CONST_STRING)
280 value = XSTR (sub, 0);
284 /* ??? It would be possible to handle this if we really tried.
285 It's not easy though, and I'm not going to bother until it
286 really proves necessary. */
287 message_with_line (elem->lineno,
288 "non-constant value for `predicable'");
297 return predicable_default;
300 /* Verify that predicability does not vary on the alternative. */
301 /* ??? It should be possible to handle this by simply eliminating
302 the non-predicable alternatives from the insn. FRV would like
303 to do this. Delay this until we've got the basics solid. */
304 if (strchr (value, ',') != NULL)
306 message_with_line (elem->lineno,
307 "multiple alternatives for `predicable'");
312 /* Find out which value we're looking at. */
313 if (strcmp (value, predicable_true) == 0)
315 if (strcmp (value, predicable_false) == 0)
318 message_with_line (elem->lineno,
319 "Unknown value `%s' for `predicable' attribute",
325 /* Examine the attribute "predicable"; discover its boolean values
329 identify_predicable_attribute ()
331 struct queue_elem *elem;
332 char *p_true, *p_false;
336 /* Look for the DEFINE_ATTR for `predicable', which must exist. */
337 for (elem = define_attr_queue; elem ; elem = elem->next)
338 if (strcmp (XSTR (elem->data, 0), "predicable") == 0)
341 message_with_line (define_cond_exec_queue->lineno,
342 "Attribute `predicable' not defined");
347 value = XSTR (elem->data, 1);
348 len = strlen (value);
349 p_false = (char *) xmalloc (len + 1);
350 memcpy (p_false, value, len + 1);
352 p_true = strchr (p_false, ',');
353 if (p_true == NULL || strchr (++p_true, ',') != NULL)
355 message_with_line (elem->lineno,
356 "Attribute `predicable' is not a boolean");
362 predicable_true = p_true;
363 predicable_false = p_false;
365 switch (GET_CODE (XEXP (elem->data, 2)))
368 value = XSTR (XEXP (elem->data, 2), 0);
372 message_with_line (elem->lineno,
373 "Attribute `predicable' cannot be const");
378 message_with_line (elem->lineno,
379 "Attribute `predicable' must have a constant default");
384 if (strcmp (value, p_true) == 0)
385 predicable_default = 1;
386 else if (strcmp (value, p_false) == 0)
387 predicable_default = 0;
390 message_with_line (elem->lineno,
391 "Unknown value `%s' for `predicable' attribute",
397 /* Return the number of alternatives in constraint S. */
412 /* Determine how many alternatives there are in INSN, and how many
416 collect_insn_data (pattern, palt, pmax)
424 code = GET_CODE (pattern);
428 i = n_alternatives (XSTR (pattern, 2));
429 *palt = (i > *palt ? i : *palt);
436 i = XINT (pattern, 0);
445 fmt = GET_RTX_FORMAT (code);
446 len = GET_RTX_LENGTH (code);
447 for (i = 0; i < len; i++)
452 collect_insn_data (XEXP (pattern, i), palt, pmax);
456 if (XVEC (pattern, i) == NULL)
460 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
461 collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
464 case 'i': case 'w': case '0': case 's': case 'S': case 'T':
474 alter_predicate_for_insn (pattern, alt, max_op, lineno)
476 int alt, max_op, lineno;
482 code = GET_CODE (pattern);
487 const char *c = XSTR (pattern, 2);
489 if (n_alternatives (c) != 1)
491 message_with_line (lineno,
492 "Too many alternatives for operand %d",
498 /* Replicate C as needed to fill out ALT alternatives. */
499 if (c && *c && alt > 1)
501 size_t c_len = strlen (c);
502 size_t len = alt * (c_len + 1);
503 char *new_c = (char *) xmalloc (len);
505 memcpy (new_c, c, c_len);
506 for (i = 1; i < alt; ++i)
508 new_c[i * (c_len + 1) - 1] = ',';
509 memcpy (&new_c[i * (c_len + 1)], c, c_len);
511 new_c[len - 1] = '\0';
512 XSTR (pattern, 2) = new_c;
521 XINT (pattern, 0) += max_op;
528 fmt = GET_RTX_FORMAT (code);
529 len = GET_RTX_LENGTH (code);
530 for (i = 0; i < len; i++)
537 r = alter_predicate_for_insn (XEXP (pattern, i), alt,
544 for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
546 r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
547 alt, max_op, lineno);
553 case 'i': case 'w': case '0': case 's':
565 alter_test_for_insn (ce_elem, insn_elem)
566 struct queue_elem *ce_elem, *insn_elem;
568 const char *ce_test, *insn_test;
570 size_t len, ce_len, insn_len;
572 ce_test = XSTR (ce_elem->data, 1);
573 insn_test = XSTR (insn_elem->data, 2);
574 if (!ce_test || *ce_test == '\0')
576 if (!insn_test || *insn_test == '\0')
579 ce_len = strlen (ce_test);
580 insn_len = strlen (insn_test);
581 len = 1 + ce_len + 1 + 4 + 1 + insn_len + 1 + 1;
582 new_test = (char *) xmalloc (len);
584 sprintf (new_test, "(%s) && (%s)", ce_test, insn_test);
589 /* Adjust all of the operand numbers in OLD to match the shift they'll
590 get from an operand displacement of DISP. Return a pointer after the
594 shift_output_template (new, old, disp)
606 if (ISDIGIT ((unsigned char) c))
608 else if (ISALPHA (c))
621 alter_output_for_insn (ce_elem, insn_elem, alt, max_op)
622 struct queue_elem *ce_elem, *insn_elem;
625 const char *ce_out, *insn_out;
627 size_t len, ce_len, insn_len;
629 /* ??? Could coordinate with genoutput to not duplicate code here. */
631 ce_out = XSTR (ce_elem->data, 2);
632 insn_out = XTMPL (insn_elem->data, 3);
633 if (!ce_out || *ce_out == '\0')
636 ce_len = strlen (ce_out);
637 insn_len = strlen (insn_out);
639 if (*insn_out == '*')
640 /* You must take care of the predicate yourself. */
643 if (*insn_out == '@')
645 len = (ce_len + 1) * alt + insn_len + 1;
646 p = new = xmalloc (len);
652 while (ISSPACE ((unsigned char) *insn_out));
654 if (*insn_out != '#')
656 p = shift_output_template (p, ce_out, max_op);
662 while (*insn_out && *insn_out != '\n');
669 len = ce_len + 1 + insn_len + 1;
672 p = shift_output_template (new, ce_out, max_op);
674 memcpy (p, insn_out, insn_len + 1);
680 /* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
683 process_one_cond_exec (ce_elem)
684 struct queue_elem *ce_elem;
686 struct queue_elem *insn_elem;
687 for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
689 int alternatives, max_operand;
690 rtx pred, insn, pattern;
692 if (! is_predicable (insn_elem))
697 collect_insn_data (insn_elem->data, &alternatives, &max_operand);
700 if (XVECLEN (ce_elem->data, 0) != 1)
702 message_with_line (ce_elem->lineno,
703 "too many patterns in predicate");
708 pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
709 pred = alter_predicate_for_insn (pred, alternatives, max_operand,
714 /* Construct a new pattern for the new insn. */
715 insn = copy_rtx (insn_elem->data);
717 pattern = rtx_alloc (COND_EXEC);
718 XEXP (pattern, 0) = pred;
719 if (XVECLEN (insn, 1) == 1)
721 XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
722 XVECEXP (insn, 1, 0) = pattern;
723 PUT_NUM_ELEM (XVEC (insn, 1), 1);
727 XEXP (pattern, 1) = rtx_alloc (PARALLEL);
728 XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
729 XVEC (insn, 1) = rtvec_alloc (1);
730 XVECEXP (insn, 1, 0) = pattern;
733 XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
734 XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
735 alternatives, max_operand);
737 /* ??? Set `predicable' to false. Not crucial since it's really
738 only used here, and we won't reprocess this new pattern. */
740 /* Put the new pattern on the `other' list so that it
741 (a) is not reprocessed by other define_cond_exec patterns
742 (b) appears after all normal define_insn patterns.
744 ??? B is debatable. If one has normal insns that match
745 cond_exec patterns, they will be preferred over these
746 generated patterns. Whether this matters in practice, or if
747 it's a good thing, or whether we should thread these new
748 patterns into the define_insn chain just after their generator
749 is something we'll have to experiment with. */
751 queue_pattern (insn, &other_tail, insn_elem->lineno);
755 /* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
756 patterns appropriately. */
759 process_define_cond_exec ()
761 struct queue_elem *elem;
763 identify_predicable_attribute ();
767 for (elem = define_cond_exec_queue; elem ; elem = elem->next)
768 process_one_cond_exec (elem);
771 /* The entry point for initializing the reader. */
774 init_md_reader (filename)
775 const char *filename;
780 read_rtx_filename = filename;
781 input_file = fopen (filename, "r");
785 return FATAL_EXIT_CODE;
788 obstack_init (rtl_obstack);
792 /* Read the entire file. */
798 c = read_skip_spaces (input_file);
802 ungetc (c, input_file);
803 lineno = read_rtx_lineno;
804 desc = read_rtx (input_file);
805 process_rtx (desc, lineno);
809 /* Process define_cond_exec patterns. */
810 if (define_cond_exec_queue != NULL)
811 process_define_cond_exec ();
813 return errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
816 /* The entry point for reading a single rtx from an md file. */
819 read_md_rtx (lineno, seqnr)
823 struct queue_elem **queue, *elem;
826 /* Read all patterns from a given queue before moving on to the next. */
827 if (define_attr_queue != NULL)
828 queue = &define_attr_queue;
829 else if (define_insn_queue != NULL)
830 queue = &define_insn_queue;
831 else if (other_queue != NULL)
832 queue = &other_queue;
839 *lineno = elem->lineno;
840 *seqnr = sequence_num;
844 switch (GET_CODE (desc))
849 case DEFINE_PEEPHOLE:
850 case DEFINE_PEEPHOLE2:
861 /* Until we can use the versions in libiberty. */
866 size_t len = strlen (input) + 1;
867 char *output = xmalloc (len);
868 memcpy (output, input, len);
873 xcalloc (nelem, elsize)
874 size_t nelem, elsize;
878 if (nelem == 0 || elsize == 0)
881 newmem = really_call_calloc (nelem, elsize);
883 fatal ("virtual memory exhausted");
894 ptr = (PTR) really_call_realloc (old, size);
896 ptr = (PTR) really_call_malloc (size);
898 fatal ("virtual memory exhausted");
906 PTR val = (PTR) really_call_malloc (size);
909 fatal ("virtual memory exhausted");