[AArch64] Use new target pass registration framework for FMA steering pass
[official-gcc.git] / gcc / read-rtl.c
blob925ea451c9d0591a1b01079ccda752f2a5286a71
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "bconfig.h"
22 /* Disable rtl checking; it conflicts with the iterator handling. */
23 #undef ENABLE_RTL_CHECKING
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "obstack.h"
30 #include "read-md.h"
31 #include "gensupport.h"
33 /* One element in a singly-linked list of (integer, string) pairs. */
34 struct map_value {
35 struct map_value *next;
36 int number;
37 const char *string;
40 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
41 The integers are iterator values; the strings are either C conditions
42 or attribute values. */
43 struct mapping {
44 /* The name of the iterator or attribute. */
45 const char *name;
47 /* The group (modes or codes) to which the iterator or attribute belongs. */
48 struct iterator_group *group;
50 /* The list of (integer, string) pairs. */
51 struct map_value *values;
53 /* For iterators, records the current value of the iterator. */
54 struct map_value *current_value;
57 /* A structure for abstracting the common parts of iterators. */
58 struct iterator_group {
59 /* Tables of "mapping" structures, one for attributes and one for
60 iterators. */
61 htab_t attrs, iterators;
63 /* Treat the given string as the name of a standard mode, etc., and
64 return its integer value. */
65 int (*find_builtin) (const char *);
67 /* Make the given pointer use the given iterator value. */
68 void (*apply_iterator) (void *, int);
71 /* Records one use of an iterator. */
72 struct iterator_use {
73 /* The iterator itself. */
74 struct mapping *iterator;
76 /* The location of the use, as passed to the apply_iterator callback. */
77 void *ptr;
80 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
81 in a non-string rtx field. */
82 struct attribute_use {
83 /* The group that describes the use site. */
84 struct iterator_group *group;
86 /* The name of the attribute, possibly with an "iterator:" prefix. */
87 const char *value;
89 /* The location of the use, as passed to GROUP's apply_iterator callback. */
90 void *ptr;
93 /* This struct is used to link subst_attr named ATTR_NAME with
94 corresponding define_subst named ITER_NAME. */
95 struct subst_attr_to_iter_mapping
97 char *attr_name;
98 char *iter_name;
101 /* Hash-table to store links between subst-attributes and
102 define_substs. */
103 htab_t subst_attr_to_iter_map = NULL;
104 /* This global stores name of subst-iterator which is currently being
105 processed. */
106 const char *current_iterator_name;
108 static void validate_const_int (const char *);
109 static rtx read_rtx_code (const char *);
110 static void read_rtx_operand (rtx, int);
111 static rtx read_nested_rtx (void);
112 static rtx read_rtx_variadic (rtx);
114 /* The mode and code iterator structures. */
115 static struct iterator_group modes, codes, ints, substs;
117 /* All iterators used in the current rtx. */
118 static vec<mapping *> current_iterators;
120 /* The list of all iterator uses in the current rtx. */
121 static vec<iterator_use> iterator_uses;
123 /* The list of all attribute uses in the current rtx. */
124 static vec<attribute_use> attribute_uses;
126 /* Implementations of the iterator_group callbacks for modes. */
128 static int
129 find_mode (const char *name)
131 int i;
133 for (i = 0; i < NUM_MACHINE_MODES; i++)
134 if (strcmp (GET_MODE_NAME (i), name) == 0)
135 return i;
137 fatal_with_file_and_line ("unknown mode `%s'", name);
140 static void
141 apply_mode_iterator (void *loc, int mode)
143 PUT_MODE ((rtx) loc, (machine_mode) mode);
146 /* Implementations of the iterator_group callbacks for codes. */
148 static int
149 find_code (const char *name)
151 int i;
153 for (i = 0; i < NUM_RTX_CODE; i++)
154 if (strcmp (GET_RTX_NAME (i), name) == 0)
155 return i;
157 fatal_with_file_and_line ("unknown rtx code `%s'", name);
160 static void
161 apply_code_iterator (void *loc, int code)
163 PUT_CODE ((rtx) loc, (enum rtx_code) code);
166 /* Implementations of the iterator_group callbacks for ints. */
168 /* Since GCC does not construct a table of valid constants,
169 we have to accept any int as valid. No cross-checking can
170 be done. */
172 static int
173 find_int (const char *name)
175 validate_const_int (name);
176 return atoi (name);
179 static void
180 apply_int_iterator (void *loc, int value)
182 *(int *)loc = value;
185 /* This routine adds attribute or does nothing depending on VALUE. When
186 VALUE is 1, it does nothing - the first duplicate of original
187 template is kept untouched when it's subjected to a define_subst.
188 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
189 attribute, named exactly as define_subst, which later will be
190 applied. If such attribute has already been added, then no the
191 routine has no effect. */
192 static void
193 apply_subst_iterator (void *loc, int value)
195 rtx rt = (rtx)loc;
196 rtx new_attr;
197 rtvec attrs_vec, new_attrs_vec;
198 int i;
199 if (value == 1)
200 return;
201 gcc_assert (GET_CODE (rt) == DEFINE_INSN
202 || GET_CODE (rt) == DEFINE_EXPAND);
204 attrs_vec = XVEC (rt, 4);
206 /* If we've already added attribute 'current_iterator_name', then we
207 have nothing to do now. */
208 if (attrs_vec)
210 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
212 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
213 return;
217 /* Add attribute with subst name - it serves as a mark for
218 define_subst which later would be applied to this pattern. */
219 new_attr = rtx_alloc (SET_ATTR);
220 PUT_CODE (new_attr, SET_ATTR);
221 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
222 XSTR (new_attr, 1) = xstrdup ("yes");
224 if (!attrs_vec)
226 new_attrs_vec = rtvec_alloc (1);
227 new_attrs_vec->elem[0] = new_attr;
229 else
231 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
232 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
233 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
234 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
236 XVEC (rt, 4) = new_attrs_vec;
239 /* Map subst-attribute ATTR to subst iterator ITER. */
241 static void
242 bind_subst_iter_and_attr (const char *iter, const char *attr)
244 struct subst_attr_to_iter_mapping *value;
245 void **slot;
246 if (!subst_attr_to_iter_map)
247 subst_attr_to_iter_map =
248 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
249 value = XNEW (struct subst_attr_to_iter_mapping);
250 value->attr_name = xstrdup (attr);
251 value->iter_name = xstrdup (iter);
252 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
253 *slot = value;
256 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
258 static char*
259 find_subst_iter_by_attr (const char *attr)
261 char *iter_name = NULL;
262 struct subst_attr_to_iter_mapping *value;
263 value = (struct subst_attr_to_iter_mapping*)
264 htab_find (subst_attr_to_iter_map, &attr);
265 if (value)
266 iter_name = value->iter_name;
267 return iter_name;
270 /* Map attribute string P to its current value. Return null if the attribute
271 isn't known. */
273 static struct map_value *
274 map_attr_string (const char *p)
276 const char *attr;
277 struct mapping *iterator;
278 unsigned int i;
279 struct mapping *m;
280 struct map_value *v;
281 int iterator_name_len;
283 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
284 attribute name. */
285 attr = strchr (p, ':');
286 if (attr == 0)
288 iterator_name_len = -1;
289 attr = p;
291 else
293 iterator_name_len = attr - p;
294 attr++;
297 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
299 /* If an iterator name was specified, check that it matches. */
300 if (iterator_name_len >= 0
301 && (strncmp (p, iterator->name, iterator_name_len) != 0
302 || iterator->name[iterator_name_len] != 0))
303 continue;
305 /* Find the attribute specification. */
306 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
307 if (m)
309 /* In contrast to code/mode/int iterators, attributes of subst
310 iterators are linked to one specific subst-iterator. So, if
311 we are dealing with subst-iterator, we should check if it's
312 the one which linked with the given attribute. */
313 if (iterator->group == &substs)
315 char *iter_name = find_subst_iter_by_attr (attr);
316 if (strcmp (iter_name, iterator->name) != 0)
317 continue;
319 /* Find the attribute value associated with the current
320 iterator value. */
321 for (v = m->values; v; v = v->next)
322 if (v->number == iterator->current_value->number)
323 return v;
326 return NULL;
329 /* Apply the current iterator values to STRING. Return the new string
330 if any changes were needed, otherwise return STRING itself. */
332 static const char *
333 apply_iterator_to_string (const char *string)
335 char *base, *copy, *p, *start, *end;
336 struct map_value *v;
338 if (string == 0)
339 return string;
341 base = p = copy = ASTRDUP (string);
342 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
344 p = start + 1;
346 *end = 0;
347 v = map_attr_string (p);
348 *end = '>';
349 if (v == 0)
350 continue;
352 /* Add everything between the last copied byte and the '<',
353 then add in the attribute value. */
354 obstack_grow (&string_obstack, base, start - base);
355 obstack_grow (&string_obstack, v->string, strlen (v->string));
356 base = end + 1;
358 if (base != copy)
360 obstack_grow (&string_obstack, base, strlen (base) + 1);
361 copy = XOBFINISH (&string_obstack, char *);
362 copy_md_ptr_loc (copy, string);
363 return copy;
365 return string;
368 /* Return a deep copy of X, substituting the current iterator
369 values into any strings. */
371 static rtx
372 copy_rtx_for_iterators (rtx original)
374 const char *format_ptr, *p;
375 int i, j;
376 rtx x;
378 if (original == 0)
379 return original;
381 /* Create a shallow copy of ORIGINAL. */
382 x = rtx_alloc (GET_CODE (original));
383 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
385 /* Change each string and recursively change each rtx. */
386 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
387 for (i = 0; format_ptr[i] != 0; i++)
388 switch (format_ptr[i])
390 case 'T':
391 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
392 XTMPL (x, i) = p;
393 break;
395 case 'S':
396 case 's':
397 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
398 XSTR (x, i) = p;
399 break;
401 case 'e':
402 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
403 break;
405 case 'V':
406 case 'E':
407 if (XVEC (original, i))
409 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
410 for (j = 0; j < XVECLEN (x, i); j++)
411 XVECEXP (x, i, j)
412 = copy_rtx_for_iterators (XVECEXP (original, i, j));
414 break;
416 default:
417 break;
419 return x;
422 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
423 has the form "&& ..." (as used in define_insn_and_splits), assume that
424 EXTRA is already satisfied. Empty strings are treated like "true". */
426 static const char *
427 add_condition_to_string (const char *original, const char *extra)
429 if (original != 0 && original[0] == '&' && original[1] == '&')
430 return original;
431 return join_c_conditions (original, extra);
434 /* Like add_condition, but applied to all conditions in rtx X. */
436 static void
437 add_condition_to_rtx (rtx x, const char *extra)
439 switch (GET_CODE (x))
441 case DEFINE_INSN:
442 case DEFINE_EXPAND:
443 case DEFINE_SUBST:
444 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
445 break;
447 case DEFINE_SPLIT:
448 case DEFINE_PEEPHOLE:
449 case DEFINE_PEEPHOLE2:
450 case DEFINE_COND_EXEC:
451 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
452 break;
454 case DEFINE_INSN_AND_SPLIT:
455 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
456 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
457 break;
459 default:
460 break;
464 /* Apply the current iterator values to all attribute_uses. */
466 static void
467 apply_attribute_uses (void)
469 struct map_value *v;
470 attribute_use *ause;
471 unsigned int i;
473 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
475 v = map_attr_string (ause->value);
476 if (!v)
477 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
478 ause->group->apply_iterator (ause->ptr,
479 ause->group->find_builtin (v->string));
483 /* A htab_traverse callback for iterators. Add all used iterators
484 to current_iterators. */
486 static int
487 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
489 struct mapping *iterator;
491 iterator = (struct mapping *) *slot;
492 if (iterator->current_value)
493 current_iterators.safe_push (iterator);
494 return 1;
497 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
498 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
500 static void
501 apply_iterators (rtx original, vec<rtx> *queue)
503 unsigned int i;
504 const char *condition;
505 iterator_use *iuse;
506 struct mapping *iterator;
507 struct map_value *v;
508 rtx x;
510 if (iterator_uses.is_empty ())
512 /* Raise an error if any attributes were used. */
513 apply_attribute_uses ();
514 queue->safe_push (original);
515 return;
518 /* Clear out the iterators from the previous run. */
519 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
520 iterator->current_value = NULL;
521 current_iterators.truncate (0);
523 /* Mark the iterators that we need this time. */
524 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
525 iuse->iterator->current_value = iuse->iterator->values;
527 /* Get the list of iterators that are in use, preserving the
528 definition order within each group. */
529 htab_traverse (modes.iterators, add_current_iterators, NULL);
530 htab_traverse (codes.iterators, add_current_iterators, NULL);
531 htab_traverse (ints.iterators, add_current_iterators, NULL);
532 htab_traverse (substs.iterators, add_current_iterators, NULL);
533 gcc_assert (!current_iterators.is_empty ());
535 for (;;)
537 /* Apply the current iterator values. Accumulate a condition to
538 say when the resulting rtx can be used. */
539 condition = "";
540 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
542 if (iuse->iterator->group == &substs)
543 continue;
544 v = iuse->iterator->current_value;
545 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
546 condition = join_c_conditions (condition, v->string);
548 apply_attribute_uses ();
549 x = copy_rtx_for_iterators (original);
550 add_condition_to_rtx (x, condition);
552 /* We apply subst iterator after RTL-template is copied, as during
553 subst-iterator processing, we could add an attribute to the
554 RTL-template, and we don't want to do it in the original one. */
555 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
557 v = iuse->iterator->current_value;
558 if (iuse->iterator->group == &substs)
560 iuse->ptr = x;
561 current_iterator_name = iuse->iterator->name;
562 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
565 /* Add the new rtx to the end of the queue. */
566 queue->safe_push (x);
568 /* Lexicographically increment the iterator value sequence.
569 That is, cycle through iterator values, starting from the right,
570 and stopping when one of them doesn't wrap around. */
571 i = current_iterators.length ();
572 for (;;)
574 if (i == 0)
575 return;
576 i--;
577 iterator = current_iterators[i];
578 iterator->current_value = iterator->current_value->next;
579 if (iterator->current_value)
580 break;
581 iterator->current_value = iterator->values;
586 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
587 of the mapping and GROUP is the group to which it belongs. */
589 static struct mapping *
590 add_mapping (struct iterator_group *group, htab_t table, const char *name)
592 struct mapping *m;
593 void **slot;
595 m = XNEW (struct mapping);
596 m->name = xstrdup (name);
597 m->group = group;
598 m->values = 0;
599 m->current_value = NULL;
601 slot = htab_find_slot (table, m, INSERT);
602 if (*slot != 0)
603 fatal_with_file_and_line ("`%s' already defined", name);
605 *slot = m;
606 return m;
609 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
610 END_PTR points to the current null terminator for the list; return
611 a pointer the new null terminator. */
613 static struct map_value **
614 add_map_value (struct map_value **end_ptr, int number, const char *string)
616 struct map_value *value;
618 value = XNEW (struct map_value);
619 value->next = 0;
620 value->number = number;
621 value->string = string;
623 *end_ptr = value;
624 return &value->next;
627 /* Do one-time initialization of the mode and code attributes. */
629 static void
630 initialize_iterators (void)
632 struct mapping *lower, *upper;
633 struct map_value **lower_ptr, **upper_ptr;
634 char *copy, *p;
635 int i;
637 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
638 modes.iterators = htab_create (13, leading_string_hash,
639 leading_string_eq_p, 0);
640 modes.find_builtin = find_mode;
641 modes.apply_iterator = apply_mode_iterator;
643 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
644 codes.iterators = htab_create (13, leading_string_hash,
645 leading_string_eq_p, 0);
646 codes.find_builtin = find_code;
647 codes.apply_iterator = apply_code_iterator;
649 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
650 ints.iterators = htab_create (13, leading_string_hash,
651 leading_string_eq_p, 0);
652 ints.find_builtin = find_int;
653 ints.apply_iterator = apply_int_iterator;
655 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
656 substs.iterators = htab_create (13, leading_string_hash,
657 leading_string_eq_p, 0);
658 substs.find_builtin = find_int; /* We don't use it, anyway. */
659 substs.apply_iterator = apply_subst_iterator;
661 lower = add_mapping (&modes, modes.attrs, "mode");
662 upper = add_mapping (&modes, modes.attrs, "MODE");
663 lower_ptr = &lower->values;
664 upper_ptr = &upper->values;
665 for (i = 0; i < MAX_MACHINE_MODE; i++)
667 copy = xstrdup (GET_MODE_NAME (i));
668 for (p = copy; *p != 0; p++)
669 *p = TOLOWER (*p);
671 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
672 lower_ptr = add_map_value (lower_ptr, i, copy);
675 lower = add_mapping (&codes, codes.attrs, "code");
676 upper = add_mapping (&codes, codes.attrs, "CODE");
677 lower_ptr = &lower->values;
678 upper_ptr = &upper->values;
679 for (i = 0; i < NUM_RTX_CODE; i++)
681 copy = xstrdup (GET_RTX_NAME (i));
682 for (p = copy; *p != 0; p++)
683 *p = TOUPPER (*p);
685 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
686 upper_ptr = add_map_value (upper_ptr, i, copy);
690 /* Provide a version of a function to read a long long if the system does
691 not provide one. */
692 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
693 HOST_WIDE_INT atoll (const char *);
695 HOST_WIDE_INT
696 atoll (const char *p)
698 int neg = 0;
699 HOST_WIDE_INT tmp_wide;
701 while (ISSPACE (*p))
702 p++;
703 if (*p == '-')
704 neg = 1, p++;
705 else if (*p == '+')
706 p++;
708 tmp_wide = 0;
709 while (ISDIGIT (*p))
711 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
712 if (new_wide < tmp_wide)
714 /* Return INT_MAX equiv on overflow. */
715 tmp_wide = HOST_WIDE_INT_M1U >> 1;
716 break;
718 tmp_wide = new_wide;
719 p++;
722 if (neg)
723 tmp_wide = -tmp_wide;
724 return tmp_wide;
726 #endif
728 /* Process a define_conditions directive, starting with the optional
729 space after the "define_conditions". The directive looks like this:
731 (define_conditions [
732 (number "string")
733 (number "string")
737 It's not intended to appear in machine descriptions. It is
738 generated by (the program generated by) genconditions.c, and
739 slipped in at the beginning of the sequence of MD files read by
740 most of the other generators. */
741 static void
742 read_conditions (void)
744 int c;
746 require_char_ws ('[');
748 while ( (c = read_skip_spaces ()) != ']')
750 struct md_name name;
751 char *expr;
752 int value;
754 if (c != '(')
755 fatal_expected_char ('(', c);
757 read_name (&name);
758 validate_const_int (name.string);
759 value = atoi (name.string);
761 require_char_ws ('"');
762 expr = read_quoted_string ();
764 require_char_ws (')');
766 add_c_test (expr, value);
770 static void
771 validate_const_int (const char *string)
773 const char *cp;
774 int valid = 1;
776 cp = string;
777 while (*cp && ISSPACE (*cp))
778 cp++;
779 if (*cp == '-' || *cp == '+')
780 cp++;
781 if (*cp == 0)
782 valid = 0;
783 for (; *cp; cp++)
784 if (! ISDIGIT (*cp))
786 valid = 0;
787 break;
789 if (!valid)
790 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
793 static void
794 validate_const_wide_int (const char *string)
796 const char *cp;
797 int valid = 1;
799 cp = string;
800 while (*cp && ISSPACE (*cp))
801 cp++;
802 /* Skip the leading 0x. */
803 if (cp[0] == '0' || cp[1] == 'x')
804 cp += 2;
805 else
806 valid = 0;
807 if (*cp == 0)
808 valid = 0;
809 for (; *cp; cp++)
810 if (! ISXDIGIT (*cp))
811 valid = 0;
812 if (!valid)
813 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
816 /* Record that PTR uses iterator ITERATOR. */
818 static void
819 record_iterator_use (struct mapping *iterator, void *ptr)
821 struct iterator_use iuse = {iterator, ptr};
822 iterator_uses.safe_push (iuse);
825 /* Record that PTR uses attribute VALUE, which must match a built-in
826 value from group GROUP. */
828 static void
829 record_attribute_use (struct iterator_group *group, void *ptr,
830 const char *value)
832 struct attribute_use ause = {group, value, ptr};
833 attribute_uses.safe_push (ause);
836 /* Interpret NAME as either a built-in value, iterator or attribute
837 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
838 callback. */
840 static void
841 record_potential_iterator_use (struct iterator_group *group, void *ptr,
842 const char *name)
844 struct mapping *m;
845 size_t len;
847 len = strlen (name);
848 if (name[0] == '<' && name[len - 1] == '>')
850 /* Copy the attribute string into permanent storage, without the
851 angle brackets around it. */
852 obstack_grow0 (&string_obstack, name + 1, len - 2);
853 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
855 else
857 m = (struct mapping *) htab_find (group->iterators, &name);
858 if (m != 0)
859 record_iterator_use (m, ptr);
860 else
861 group->apply_iterator (ptr, group->find_builtin (name));
865 /* Finish reading a declaration of the form:
867 (define... <name> [<value1> ... <valuen>])
869 from the MD file, where each <valuei> is either a bare symbol name or a
870 "(<name> <string>)" pair. The "(define..." part has already been read.
872 Represent the declaration as a "mapping" structure; add it to TABLE
873 (which belongs to GROUP) and return it. */
875 static struct mapping *
876 read_mapping (struct iterator_group *group, htab_t table)
878 struct md_name name;
879 struct mapping *m;
880 struct map_value **end_ptr;
881 const char *string;
882 int number, c;
884 /* Read the mapping name and create a structure for it. */
885 read_name (&name);
886 m = add_mapping (group, table, name.string);
888 require_char_ws ('[');
890 /* Read each value. */
891 end_ptr = &m->values;
892 c = read_skip_spaces ();
895 if (c != '(')
897 /* A bare symbol name that is implicitly paired to an
898 empty string. */
899 unread_char (c);
900 read_name (&name);
901 string = "";
903 else
905 /* A "(name string)" pair. */
906 read_name (&name);
907 string = read_string (false);
908 require_char_ws (')');
910 number = group->find_builtin (name.string);
911 end_ptr = add_map_value (end_ptr, number, string);
912 c = read_skip_spaces ();
914 while (c != ']');
916 return m;
919 /* For iterator with name ATTR_NAME generate define_attr with values
920 'yes' and 'no'. This attribute is used to mark templates to which
921 define_subst ATTR_NAME should be applied. This attribute is set and
922 defined implicitly and automatically. */
923 static void
924 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
926 rtx const_str, return_rtx;
928 return_rtx = rtx_alloc (DEFINE_ATTR);
929 PUT_CODE (return_rtx, DEFINE_ATTR);
931 const_str = rtx_alloc (CONST_STRING);
932 PUT_CODE (const_str, CONST_STRING);
933 XSTR (const_str, 0) = xstrdup ("no");
935 XSTR (return_rtx, 0) = xstrdup (attr_name);
936 XSTR (return_rtx, 1) = xstrdup ("no,yes");
937 XEXP (return_rtx, 2) = const_str;
939 queue->safe_push (return_rtx);
942 /* This routine generates DEFINE_SUBST_ATTR expression with operands
943 ATTR_OPERANDS and places it to QUEUE. */
944 static void
945 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
947 rtx return_rtx;
948 int i;
950 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
951 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
953 for (i = 0; i < 4; i++)
954 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
956 queue->safe_push (return_rtx);
959 /* Read define_subst_attribute construction. It has next form:
960 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
961 Attribute is substituted with value1 when no subst is applied and with
962 value2 in the opposite case.
963 Attributes are added to SUBST_ATTRS_TABLE.
964 In case the iterator is encountered for the first time, it's added to
965 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
967 static void
968 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
969 vec<rtx> *queue)
971 struct mapping *m;
972 struct map_value **end_ptr;
973 const char *attr_operands[4];
974 int i;
976 for (i = 0; i < 4; i++)
977 attr_operands[i] = read_string (false);
979 add_define_subst_attr (attr_operands, queue);
981 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
983 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
984 if (!m)
986 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
987 end_ptr = &m->values;
988 end_ptr = add_map_value (end_ptr, 1, "");
989 end_ptr = add_map_value (end_ptr, 2, "");
991 add_define_attr_for_define_subst (attr_operands[1], queue);
994 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
995 end_ptr = &m->values;
996 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
997 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
1000 /* Check newly-created code iterator ITERATOR to see whether every code has the
1001 same format. */
1003 static void
1004 check_code_iterator (struct mapping *iterator)
1006 struct map_value *v;
1007 enum rtx_code bellwether;
1009 bellwether = (enum rtx_code) iterator->values->number;
1010 for (v = iterator->values->next; v != 0; v = v->next)
1011 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1012 fatal_with_file_and_line ("code iterator `%s' combines "
1013 "different rtx formats", iterator->name);
1016 /* Read an rtx-related declaration from the MD file, given that it
1017 starts with directive name RTX_NAME. Return true if it expands to
1018 one or more rtxes (as defined by rtx.def). When returning true,
1019 store the list of rtxes as an EXPR_LIST in *X. */
1021 bool
1022 read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1024 static bool initialized = false;
1026 /* Do one-time initialization. */
1027 if (!initialized)
1029 initialize_iterators ();
1030 initialized = true;
1033 /* Handle various rtx-related declarations that aren't themselves
1034 encoded as rtxes. */
1035 if (strcmp (rtx_name, "define_conditions") == 0)
1037 read_conditions ();
1038 return false;
1040 if (strcmp (rtx_name, "define_mode_attr") == 0)
1042 read_mapping (&modes, modes.attrs);
1043 return false;
1045 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1047 read_mapping (&modes, modes.iterators);
1048 return false;
1050 if (strcmp (rtx_name, "define_code_attr") == 0)
1052 read_mapping (&codes, codes.attrs);
1053 return false;
1055 if (strcmp (rtx_name, "define_code_iterator") == 0)
1057 check_code_iterator (read_mapping (&codes, codes.iterators));
1058 return false;
1060 if (strcmp (rtx_name, "define_int_attr") == 0)
1062 read_mapping (&ints, ints.attrs);
1063 return false;
1065 if (strcmp (rtx_name, "define_int_iterator") == 0)
1067 read_mapping (&ints, ints.iterators);
1068 return false;
1070 if (strcmp (rtx_name, "define_subst_attr") == 0)
1072 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1074 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1075 TRUE to process it. */
1076 return true;
1079 apply_iterators (read_rtx_code (rtx_name), rtxen);
1080 iterator_uses.truncate (0);
1081 attribute_uses.truncate (0);
1083 return true;
1086 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1087 either an rtx code or a code iterator. Parse the rest of the rtx and
1088 return it. */
1090 static rtx
1091 read_rtx_code (const char *code_name)
1093 RTX_CODE code;
1094 struct mapping *iterator;
1095 const char *format_ptr;
1096 struct md_name name;
1097 rtx return_rtx;
1098 int c;
1100 /* Linked list structure for making RTXs: */
1101 struct rtx_list
1103 struct rtx_list *next;
1104 rtx value; /* Value of this node. */
1107 /* If this code is an iterator, build the rtx using the iterator's
1108 first value. */
1109 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1110 if (iterator != 0)
1111 code = (enum rtx_code) iterator->values->number;
1112 else
1113 code = (enum rtx_code) codes.find_builtin (code_name);
1115 /* If we end up with an insn expression then we free this space below. */
1116 return_rtx = rtx_alloc (code);
1117 format_ptr = GET_RTX_FORMAT (code);
1118 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1119 PUT_CODE (return_rtx, code);
1121 if (iterator)
1122 record_iterator_use (iterator, return_rtx);
1124 /* If what follows is `: mode ', read it and
1125 store the mode in the rtx. */
1127 c = read_skip_spaces ();
1128 if (c == ':')
1130 read_name (&name);
1131 record_potential_iterator_use (&modes, return_rtx, name.string);
1133 else
1134 unread_char (c);
1136 for (int idx = 0; format_ptr[idx] != 0; idx++)
1137 read_rtx_operand (return_rtx, idx);
1139 if (CONST_WIDE_INT_P (return_rtx))
1141 read_name (&name);
1142 validate_const_wide_int (name.string);
1144 const char *s = name.string;
1145 int len;
1146 int index = 0;
1147 int gs = HOST_BITS_PER_WIDE_INT/4;
1148 int pos;
1149 char * buf = XALLOCAVEC (char, gs + 1);
1150 unsigned HOST_WIDE_INT wi;
1151 int wlen;
1153 /* Skip the leading spaces. */
1154 while (*s && ISSPACE (*s))
1155 s++;
1157 /* Skip the leading 0x. */
1158 gcc_assert (s[0] == '0');
1159 gcc_assert (s[1] == 'x');
1160 s += 2;
1162 len = strlen (s);
1163 pos = len - gs;
1164 wlen = (len + gs - 1) / gs; /* Number of words needed */
1166 return_rtx = const_wide_int_alloc (wlen);
1168 while (pos > 0)
1170 #if HOST_BITS_PER_WIDE_INT == 64
1171 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1172 #else
1173 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1174 #endif
1175 CWI_ELT (return_rtx, index++) = wi;
1176 pos -= gs;
1178 strncpy (buf, s, gs - pos);
1179 buf [gs - pos] = 0;
1180 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1181 CWI_ELT (return_rtx, index++) = wi;
1182 /* TODO: After reading, do we want to canonicalize with:
1183 value = lookup_const_wide_int (value); ? */
1187 c = read_skip_spaces ();
1188 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1189 arbitrary number of arguments for them. */
1190 if (c == '('
1191 && (GET_CODE (return_rtx) == AND
1192 || GET_CODE (return_rtx) == IOR))
1193 return read_rtx_variadic (return_rtx);
1195 unread_char (c);
1196 return return_rtx;
1199 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1200 based on the corresponding format character within GET_RTX_FORMAT
1201 for the GET_CODE (RETURN_RTX). */
1203 static void
1204 read_rtx_operand (rtx return_rtx, int idx)
1206 RTX_CODE code = GET_CODE (return_rtx);
1207 const char *format_ptr = GET_RTX_FORMAT (code);
1208 int c;
1209 struct md_name name;
1211 switch (format_ptr[idx])
1213 /* 0 means a field for internal use only.
1214 Don't expect it to be present in the input. */
1215 case '0':
1216 if (code == REG)
1217 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1218 break;
1220 case 'e':
1221 case 'u':
1222 XEXP (return_rtx, idx) = read_nested_rtx ();
1223 break;
1225 case 'V':
1226 /* 'V' is an optional vector: if a closeparen follows,
1227 just store NULL for this element. */
1228 c = read_skip_spaces ();
1229 unread_char (c);
1230 if (c == ')')
1232 XVEC (return_rtx, idx) = 0;
1233 break;
1235 /* Now process the vector. */
1236 /* FALLTHRU */
1238 case 'E':
1240 /* Obstack to store scratch vector in. */
1241 struct obstack vector_stack;
1242 int list_counter = 0;
1243 rtvec return_vec = NULL_RTVEC;
1245 require_char_ws ('[');
1247 /* Add expressions to a list, while keeping a count. */
1248 obstack_init (&vector_stack);
1249 while ((c = read_skip_spaces ()) && c != ']')
1251 if (c == EOF)
1252 fatal_expected_char (']', c);
1253 unread_char (c);
1254 list_counter++;
1255 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1257 if (list_counter > 0)
1259 return_vec = rtvec_alloc (list_counter);
1260 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1261 list_counter * sizeof (rtx));
1263 else if (format_ptr[idx] == 'E')
1264 fatal_with_file_and_line ("vector must have at least one element");
1265 XVEC (return_rtx, idx) = return_vec;
1266 obstack_free (&vector_stack, NULL);
1267 /* close bracket gotten */
1269 break;
1271 case 'S':
1272 case 'T':
1273 case 's':
1275 char *stringbuf;
1276 int star_if_braced;
1278 c = read_skip_spaces ();
1279 unread_char (c);
1280 if (c == ')')
1282 /* 'S' fields are optional and should be NULL if no string
1283 was given. Also allow normal 's' and 'T' strings to be
1284 omitted, treating them in the same way as empty strings. */
1285 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1286 break;
1289 /* The output template slot of a DEFINE_INSN,
1290 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1291 gets a star inserted as its first character, if it is
1292 written with a brace block instead of a string constant. */
1293 star_if_braced = (format_ptr[idx] == 'T');
1295 stringbuf = read_string (star_if_braced);
1297 /* For insn patterns, we want to provide a default name
1298 based on the file and line, like "*foo.md:12", if the
1299 given name is blank. These are only for define_insn and
1300 define_insn_and_split, to aid debugging. */
1301 if (*stringbuf == '\0'
1302 && idx == 0
1303 && (GET_CODE (return_rtx) == DEFINE_INSN
1304 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1306 char line_name[20];
1307 const char *read_md_filename = rtx_reader_ptr->get_filename ();
1308 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1309 const char *slash;
1310 for (slash = fn; *slash; slash ++)
1311 if (*slash == '/' || *slash == '\\' || *slash == ':')
1312 fn = slash + 1;
1313 obstack_1grow (&string_obstack, '*');
1314 obstack_grow (&string_obstack, fn, strlen (fn));
1315 sprintf (line_name, ":%d", rtx_reader_ptr->get_lineno ());
1316 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1317 stringbuf = XOBFINISH (&string_obstack, char *);
1320 /* Find attr-names in the string. */
1321 char *str;
1322 char *start, *end, *ptr;
1323 char tmpstr[256];
1324 ptr = &tmpstr[0];
1325 end = stringbuf;
1326 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1328 if ((end - start - 1 > 0)
1329 && (end - start - 1 < (int)sizeof (tmpstr)))
1331 strncpy (tmpstr, start+1, end-start-1);
1332 tmpstr[end-start-1] = 0;
1333 end++;
1335 else
1336 break;
1337 struct mapping *m
1338 = (struct mapping *) htab_find (substs.attrs, &ptr);
1339 if (m != 0)
1341 /* Here we should find linked subst-iter. */
1342 str = find_subst_iter_by_attr (ptr);
1343 if (str)
1344 m = (struct mapping *) htab_find (substs.iterators, &str);
1345 else
1346 m = 0;
1348 if (m != 0)
1349 record_iterator_use (m, return_rtx);
1352 if (star_if_braced)
1353 XTMPL (return_rtx, idx) = stringbuf;
1354 else
1355 XSTR (return_rtx, idx) = stringbuf;
1357 break;
1359 case 'w':
1361 HOST_WIDE_INT tmp_wide;
1362 read_name (&name);
1363 validate_const_int (name.string);
1364 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1365 tmp_wide = atoi (name.string);
1366 #else
1367 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1368 tmp_wide = atol (name.string);
1369 #else
1370 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1371 But prefer not to use our hand-rolled function above either. */
1372 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1373 tmp_wide = atoll (name.string);
1374 #else
1375 tmp_wide = atoq (name.string);
1376 #endif
1377 #endif
1378 #endif
1379 XWINT (return_rtx, idx) = tmp_wide;
1381 break;
1383 case 'i':
1384 case 'n':
1385 /* Can be an iterator or an integer constant. */
1386 read_name (&name);
1387 record_potential_iterator_use (&ints, &XINT (return_rtx, idx),
1388 name.string);
1389 break;
1391 case 'r':
1392 read_name (&name);
1393 validate_const_int (name.string);
1394 set_regno_raw (return_rtx, atoi (name.string), 1);
1395 REG_ATTRS (return_rtx) = NULL;
1396 break;
1398 default:
1399 gcc_unreachable ();
1403 /* Read a nested rtx construct from the MD file and return it. */
1405 static rtx
1406 read_nested_rtx (void)
1408 struct md_name name;
1409 rtx return_rtx;
1411 require_char_ws ('(');
1413 read_name (&name);
1414 if (strcmp (name.string, "nil") == 0)
1415 return_rtx = NULL;
1416 else
1417 return_rtx = read_rtx_code (name.string);
1419 require_char_ws (')');
1421 return return_rtx;
1424 /* Mutually recursive subroutine of read_rtx which reads
1425 (thing x1 x2 x3 ...) and produces RTL as if
1426 (thing x1 (thing x2 (thing x3 ...))) had been written.
1427 When called, FORM is (thing x1 x2), and the file position
1428 is just past the leading parenthesis of x3. Only works
1429 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1430 static rtx
1431 read_rtx_variadic (rtx form)
1433 char c = '(';
1434 rtx p = form, q;
1438 unread_char (c);
1440 q = rtx_alloc (GET_CODE (p));
1441 PUT_MODE (q, GET_MODE (p));
1443 XEXP (q, 0) = XEXP (p, 1);
1444 XEXP (q, 1) = read_nested_rtx ();
1446 XEXP (p, 1) = q;
1447 p = q;
1448 c = read_skip_spaces ();
1450 while (c == '(');
1451 unread_char (c);
1452 return form;