[ARM][committed] Sort ARMv8 processors by alphabetic order
[official-gcc.git] / gcc / read-rtl.c
blob7a2021a3a23c016e2e4948ce64ab915bd6db5fc7
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 *);
110 /* The mode and code iterator structures. */
111 static struct iterator_group modes, codes, ints, substs;
113 /* All iterators used in the current rtx. */
114 static vec<mapping *> current_iterators;
116 /* The list of all iterator uses in the current rtx. */
117 static vec<iterator_use> iterator_uses;
119 /* The list of all attribute uses in the current rtx. */
120 static vec<attribute_use> attribute_uses;
122 /* Implementations of the iterator_group callbacks for modes. */
124 static int
125 find_mode (const char *name)
127 int i;
129 for (i = 0; i < NUM_MACHINE_MODES; i++)
130 if (strcmp (GET_MODE_NAME (i), name) == 0)
131 return i;
133 fatal_with_file_and_line ("unknown mode `%s'", name);
136 static void
137 apply_mode_iterator (void *loc, int mode)
139 PUT_MODE ((rtx) loc, (machine_mode) mode);
142 /* Implementations of the iterator_group callbacks for codes. */
144 static int
145 find_code (const char *name)
147 int i;
149 for (i = 0; i < NUM_RTX_CODE; i++)
150 if (strcmp (GET_RTX_NAME (i), name) == 0)
151 return i;
153 fatal_with_file_and_line ("unknown rtx code `%s'", name);
156 static void
157 apply_code_iterator (void *loc, int code)
159 PUT_CODE ((rtx) loc, (enum rtx_code) code);
162 /* Implementations of the iterator_group callbacks for ints. */
164 /* Since GCC does not construct a table of valid constants,
165 we have to accept any int as valid. No cross-checking can
166 be done. */
168 static int
169 find_int (const char *name)
171 validate_const_int (name);
172 return atoi (name);
175 static void
176 apply_int_iterator (void *loc, int value)
178 *(int *)loc = value;
181 /* This routine adds attribute or does nothing depending on VALUE. When
182 VALUE is 1, it does nothing - the first duplicate of original
183 template is kept untouched when it's subjected to a define_subst.
184 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
185 attribute, named exactly as define_subst, which later will be
186 applied. If such attribute has already been added, then no the
187 routine has no effect. */
188 static void
189 apply_subst_iterator (void *loc, int value)
191 rtx rt = (rtx)loc;
192 rtx new_attr;
193 rtvec attrs_vec, new_attrs_vec;
194 int i;
195 if (value == 1)
196 return;
197 gcc_assert (GET_CODE (rt) == DEFINE_INSN
198 || GET_CODE (rt) == DEFINE_EXPAND);
200 attrs_vec = XVEC (rt, 4);
202 /* If we've already added attribute 'current_iterator_name', then we
203 have nothing to do now. */
204 if (attrs_vec)
206 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
208 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
209 return;
213 /* Add attribute with subst name - it serves as a mark for
214 define_subst which later would be applied to this pattern. */
215 new_attr = rtx_alloc (SET_ATTR);
216 PUT_CODE (new_attr, SET_ATTR);
217 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
218 XSTR (new_attr, 1) = xstrdup ("yes");
220 if (!attrs_vec)
222 new_attrs_vec = rtvec_alloc (1);
223 new_attrs_vec->elem[0] = new_attr;
225 else
227 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
228 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
229 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
230 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
232 XVEC (rt, 4) = new_attrs_vec;
235 /* Map subst-attribute ATTR to subst iterator ITER. */
237 static void
238 bind_subst_iter_and_attr (const char *iter, const char *attr)
240 struct subst_attr_to_iter_mapping *value;
241 void **slot;
242 if (!subst_attr_to_iter_map)
243 subst_attr_to_iter_map =
244 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
245 value = XNEW (struct subst_attr_to_iter_mapping);
246 value->attr_name = xstrdup (attr);
247 value->iter_name = xstrdup (iter);
248 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
249 *slot = value;
252 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
254 static char*
255 find_subst_iter_by_attr (const char *attr)
257 char *iter_name = NULL;
258 struct subst_attr_to_iter_mapping *value;
259 value = (struct subst_attr_to_iter_mapping*)
260 htab_find (subst_attr_to_iter_map, &attr);
261 if (value)
262 iter_name = value->iter_name;
263 return iter_name;
266 /* Map attribute string P to its current value. Return null if the attribute
267 isn't known. */
269 static struct map_value *
270 map_attr_string (const char *p)
272 const char *attr;
273 struct mapping *iterator;
274 unsigned int i;
275 struct mapping *m;
276 struct map_value *v;
277 int iterator_name_len;
279 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
280 attribute name. */
281 attr = strchr (p, ':');
282 if (attr == 0)
284 iterator_name_len = -1;
285 attr = p;
287 else
289 iterator_name_len = attr - p;
290 attr++;
293 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
295 /* If an iterator name was specified, check that it matches. */
296 if (iterator_name_len >= 0
297 && (strncmp (p, iterator->name, iterator_name_len) != 0
298 || iterator->name[iterator_name_len] != 0))
299 continue;
301 /* Find the attribute specification. */
302 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
303 if (m)
305 /* In contrast to code/mode/int iterators, attributes of subst
306 iterators are linked to one specific subst-iterator. So, if
307 we are dealing with subst-iterator, we should check if it's
308 the one which linked with the given attribute. */
309 if (iterator->group == &substs)
311 char *iter_name = find_subst_iter_by_attr (attr);
312 if (strcmp (iter_name, iterator->name) != 0)
313 continue;
315 /* Find the attribute value associated with the current
316 iterator value. */
317 for (v = m->values; v; v = v->next)
318 if (v->number == iterator->current_value->number)
319 return v;
322 return NULL;
325 /* Apply the current iterator values to STRING. Return the new string
326 if any changes were needed, otherwise return STRING itself. */
328 const char *
329 rtx_reader::apply_iterator_to_string (const char *string)
331 char *base, *copy, *p, *start, *end;
332 struct map_value *v;
334 if (string == 0)
335 return string;
337 base = p = copy = ASTRDUP (string);
338 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
340 p = start + 1;
342 *end = 0;
343 v = map_attr_string (p);
344 *end = '>';
345 if (v == 0)
346 continue;
348 /* Add everything between the last copied byte and the '<',
349 then add in the attribute value. */
350 obstack_grow (&m_string_obstack, base, start - base);
351 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
352 base = end + 1;
354 if (base != copy)
356 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
357 copy = XOBFINISH (&m_string_obstack, char *);
358 copy_md_ptr_loc (copy, string);
359 return copy;
361 return string;
364 /* Return a deep copy of X, substituting the current iterator
365 values into any strings. */
368 rtx_reader::copy_rtx_for_iterators (rtx original)
370 const char *format_ptr, *p;
371 int i, j;
372 rtx x;
374 if (original == 0)
375 return original;
377 /* Create a shallow copy of ORIGINAL. */
378 x = rtx_alloc (GET_CODE (original));
379 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
381 /* Change each string and recursively change each rtx. */
382 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
383 for (i = 0; format_ptr[i] != 0; i++)
384 switch (format_ptr[i])
386 case 'T':
387 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
388 XTMPL (x, i) = p;
389 break;
391 case 'S':
392 case 's':
393 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
394 XSTR (x, i) = p;
395 break;
397 case 'e':
398 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
399 break;
401 case 'V':
402 case 'E':
403 if (XVEC (original, i))
405 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
406 for (j = 0; j < XVECLEN (x, i); j++)
407 XVECEXP (x, i, j)
408 = copy_rtx_for_iterators (XVECEXP (original, i, j));
410 break;
412 default:
413 break;
415 return x;
418 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
419 has the form "&& ..." (as used in define_insn_and_splits), assume that
420 EXTRA is already satisfied. Empty strings are treated like "true". */
422 static const char *
423 add_condition_to_string (const char *original, const char *extra)
425 if (original != 0 && original[0] == '&' && original[1] == '&')
426 return original;
427 return rtx_reader_ptr->join_c_conditions (original, extra);
430 /* Like add_condition, but applied to all conditions in rtx X. */
432 static void
433 add_condition_to_rtx (rtx x, const char *extra)
435 switch (GET_CODE (x))
437 case DEFINE_INSN:
438 case DEFINE_EXPAND:
439 case DEFINE_SUBST:
440 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
441 break;
443 case DEFINE_SPLIT:
444 case DEFINE_PEEPHOLE:
445 case DEFINE_PEEPHOLE2:
446 case DEFINE_COND_EXEC:
447 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
448 break;
450 case DEFINE_INSN_AND_SPLIT:
451 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
452 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
453 break;
455 default:
456 break;
460 /* Apply the current iterator values to all attribute_uses. */
462 static void
463 apply_attribute_uses (void)
465 struct map_value *v;
466 attribute_use *ause;
467 unsigned int i;
469 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
471 v = map_attr_string (ause->value);
472 if (!v)
473 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
474 ause->group->apply_iterator (ause->ptr,
475 ause->group->find_builtin (v->string));
479 /* A htab_traverse callback for iterators. Add all used iterators
480 to current_iterators. */
482 static int
483 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
485 struct mapping *iterator;
487 iterator = (struct mapping *) *slot;
488 if (iterator->current_value)
489 current_iterators.safe_push (iterator);
490 return 1;
493 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
494 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
496 static void
497 apply_iterators (rtx original, vec<rtx> *queue)
499 unsigned int i;
500 const char *condition;
501 iterator_use *iuse;
502 struct mapping *iterator;
503 struct map_value *v;
504 rtx x;
506 if (iterator_uses.is_empty ())
508 /* Raise an error if any attributes were used. */
509 apply_attribute_uses ();
510 queue->safe_push (original);
511 return;
514 /* Clear out the iterators from the previous run. */
515 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
516 iterator->current_value = NULL;
517 current_iterators.truncate (0);
519 /* Mark the iterators that we need this time. */
520 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
521 iuse->iterator->current_value = iuse->iterator->values;
523 /* Get the list of iterators that are in use, preserving the
524 definition order within each group. */
525 htab_traverse (modes.iterators, add_current_iterators, NULL);
526 htab_traverse (codes.iterators, add_current_iterators, NULL);
527 htab_traverse (ints.iterators, add_current_iterators, NULL);
528 htab_traverse (substs.iterators, add_current_iterators, NULL);
529 gcc_assert (!current_iterators.is_empty ());
531 for (;;)
533 /* Apply the current iterator values. Accumulate a condition to
534 say when the resulting rtx can be used. */
535 condition = "";
536 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
538 if (iuse->iterator->group == &substs)
539 continue;
540 v = iuse->iterator->current_value;
541 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
542 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
544 apply_attribute_uses ();
545 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
546 add_condition_to_rtx (x, condition);
548 /* We apply subst iterator after RTL-template is copied, as during
549 subst-iterator processing, we could add an attribute to the
550 RTL-template, and we don't want to do it in the original one. */
551 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
553 v = iuse->iterator->current_value;
554 if (iuse->iterator->group == &substs)
556 iuse->ptr = x;
557 current_iterator_name = iuse->iterator->name;
558 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
561 /* Add the new rtx to the end of the queue. */
562 queue->safe_push (x);
564 /* Lexicographically increment the iterator value sequence.
565 That is, cycle through iterator values, starting from the right,
566 and stopping when one of them doesn't wrap around. */
567 i = current_iterators.length ();
568 for (;;)
570 if (i == 0)
571 return;
572 i--;
573 iterator = current_iterators[i];
574 iterator->current_value = iterator->current_value->next;
575 if (iterator->current_value)
576 break;
577 iterator->current_value = iterator->values;
582 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
583 of the mapping and GROUP is the group to which it belongs. */
585 static struct mapping *
586 add_mapping (struct iterator_group *group, htab_t table, const char *name)
588 struct mapping *m;
589 void **slot;
591 m = XNEW (struct mapping);
592 m->name = xstrdup (name);
593 m->group = group;
594 m->values = 0;
595 m->current_value = NULL;
597 slot = htab_find_slot (table, m, INSERT);
598 if (*slot != 0)
599 fatal_with_file_and_line ("`%s' already defined", name);
601 *slot = m;
602 return m;
605 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
606 END_PTR points to the current null terminator for the list; return
607 a pointer the new null terminator. */
609 static struct map_value **
610 add_map_value (struct map_value **end_ptr, int number, const char *string)
612 struct map_value *value;
614 value = XNEW (struct map_value);
615 value->next = 0;
616 value->number = number;
617 value->string = string;
619 *end_ptr = value;
620 return &value->next;
623 /* Do one-time initialization of the mode and code attributes. */
625 static void
626 initialize_iterators (void)
628 struct mapping *lower, *upper;
629 struct map_value **lower_ptr, **upper_ptr;
630 char *copy, *p;
631 int i;
633 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
634 modes.iterators = htab_create (13, leading_string_hash,
635 leading_string_eq_p, 0);
636 modes.find_builtin = find_mode;
637 modes.apply_iterator = apply_mode_iterator;
639 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
640 codes.iterators = htab_create (13, leading_string_hash,
641 leading_string_eq_p, 0);
642 codes.find_builtin = find_code;
643 codes.apply_iterator = apply_code_iterator;
645 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
646 ints.iterators = htab_create (13, leading_string_hash,
647 leading_string_eq_p, 0);
648 ints.find_builtin = find_int;
649 ints.apply_iterator = apply_int_iterator;
651 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
652 substs.iterators = htab_create (13, leading_string_hash,
653 leading_string_eq_p, 0);
654 substs.find_builtin = find_int; /* We don't use it, anyway. */
655 substs.apply_iterator = apply_subst_iterator;
657 lower = add_mapping (&modes, modes.attrs, "mode");
658 upper = add_mapping (&modes, modes.attrs, "MODE");
659 lower_ptr = &lower->values;
660 upper_ptr = &upper->values;
661 for (i = 0; i < MAX_MACHINE_MODE; i++)
663 copy = xstrdup (GET_MODE_NAME (i));
664 for (p = copy; *p != 0; p++)
665 *p = TOLOWER (*p);
667 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
668 lower_ptr = add_map_value (lower_ptr, i, copy);
671 lower = add_mapping (&codes, codes.attrs, "code");
672 upper = add_mapping (&codes, codes.attrs, "CODE");
673 lower_ptr = &lower->values;
674 upper_ptr = &upper->values;
675 for (i = 0; i < NUM_RTX_CODE; i++)
677 copy = xstrdup (GET_RTX_NAME (i));
678 for (p = copy; *p != 0; p++)
679 *p = TOUPPER (*p);
681 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
682 upper_ptr = add_map_value (upper_ptr, i, copy);
686 /* Provide a version of a function to read a long long if the system does
687 not provide one. */
688 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
689 HOST_WIDE_INT atoll (const char *);
691 HOST_WIDE_INT
692 atoll (const char *p)
694 int neg = 0;
695 HOST_WIDE_INT tmp_wide;
697 while (ISSPACE (*p))
698 p++;
699 if (*p == '-')
700 neg = 1, p++;
701 else if (*p == '+')
702 p++;
704 tmp_wide = 0;
705 while (ISDIGIT (*p))
707 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
708 if (new_wide < tmp_wide)
710 /* Return INT_MAX equiv on overflow. */
711 tmp_wide = HOST_WIDE_INT_M1U >> 1;
712 break;
714 tmp_wide = new_wide;
715 p++;
718 if (neg)
719 tmp_wide = -tmp_wide;
720 return tmp_wide;
722 #endif
724 /* Process a define_conditions directive, starting with the optional
725 space after the "define_conditions". The directive looks like this:
727 (define_conditions [
728 (number "string")
729 (number "string")
733 It's not intended to appear in machine descriptions. It is
734 generated by (the program generated by) genconditions.c, and
735 slipped in at the beginning of the sequence of MD files read by
736 most of the other generators. */
737 void
738 rtx_reader::read_conditions ()
740 int c;
742 require_char_ws ('[');
744 while ( (c = read_skip_spaces ()) != ']')
746 struct md_name name;
747 char *expr;
748 int value;
750 if (c != '(')
751 fatal_expected_char ('(', c);
753 read_name (&name);
754 validate_const_int (name.string);
755 value = atoi (name.string);
757 require_char_ws ('"');
758 expr = read_quoted_string ();
760 require_char_ws (')');
762 add_c_test (expr, value);
766 static void
767 validate_const_int (const char *string)
769 const char *cp;
770 int valid = 1;
772 cp = string;
773 while (*cp && ISSPACE (*cp))
774 cp++;
775 if (*cp == '-' || *cp == '+')
776 cp++;
777 if (*cp == 0)
778 valid = 0;
779 for (; *cp; cp++)
780 if (! ISDIGIT (*cp))
782 valid = 0;
783 break;
785 if (!valid)
786 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
789 static void
790 validate_const_wide_int (const char *string)
792 const char *cp;
793 int valid = 1;
795 cp = string;
796 while (*cp && ISSPACE (*cp))
797 cp++;
798 /* Skip the leading 0x. */
799 if (cp[0] == '0' || cp[1] == 'x')
800 cp += 2;
801 else
802 valid = 0;
803 if (*cp == 0)
804 valid = 0;
805 for (; *cp; cp++)
806 if (! ISXDIGIT (*cp))
807 valid = 0;
808 if (!valid)
809 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
812 /* Record that PTR uses iterator ITERATOR. */
814 static void
815 record_iterator_use (struct mapping *iterator, void *ptr)
817 struct iterator_use iuse = {iterator, ptr};
818 iterator_uses.safe_push (iuse);
821 /* Record that PTR uses attribute VALUE, which must match a built-in
822 value from group GROUP. */
824 static void
825 record_attribute_use (struct iterator_group *group, void *ptr,
826 const char *value)
828 struct attribute_use ause = {group, value, ptr};
829 attribute_uses.safe_push (ause);
832 /* Interpret NAME as either a built-in value, iterator or attribute
833 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
834 callback. */
836 void
837 rtx_reader::record_potential_iterator_use (struct iterator_group *group,
838 void *ptr, const char *name)
840 struct mapping *m;
841 size_t len;
843 len = strlen (name);
844 if (name[0] == '<' && name[len - 1] == '>')
846 /* Copy the attribute string into permanent storage, without the
847 angle brackets around it. */
848 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
849 record_attribute_use (group, ptr, XOBFINISH (&m_string_obstack, char *));
851 else
853 m = (struct mapping *) htab_find (group->iterators, &name);
854 if (m != 0)
855 record_iterator_use (m, ptr);
856 else
857 group->apply_iterator (ptr, group->find_builtin (name));
861 /* Finish reading a declaration of the form:
863 (define... <name> [<value1> ... <valuen>])
865 from the MD file, where each <valuei> is either a bare symbol name or a
866 "(<name> <string>)" pair. The "(define..." part has already been read.
868 Represent the declaration as a "mapping" structure; add it to TABLE
869 (which belongs to GROUP) and return it. */
871 struct mapping *
872 rtx_reader::read_mapping (struct iterator_group *group, htab_t table)
874 struct md_name name;
875 struct mapping *m;
876 struct map_value **end_ptr;
877 const char *string;
878 int number, c;
880 /* Read the mapping name and create a structure for it. */
881 read_name (&name);
882 m = add_mapping (group, table, name.string);
884 require_char_ws ('[');
886 /* Read each value. */
887 end_ptr = &m->values;
888 c = read_skip_spaces ();
891 if (c != '(')
893 /* A bare symbol name that is implicitly paired to an
894 empty string. */
895 unread_char (c);
896 read_name (&name);
897 string = "";
899 else
901 /* A "(name string)" pair. */
902 read_name (&name);
903 string = read_string (false);
904 require_char_ws (')');
906 number = group->find_builtin (name.string);
907 end_ptr = add_map_value (end_ptr, number, string);
908 c = read_skip_spaces ();
910 while (c != ']');
912 return m;
915 /* For iterator with name ATTR_NAME generate define_attr with values
916 'yes' and 'no'. This attribute is used to mark templates to which
917 define_subst ATTR_NAME should be applied. This attribute is set and
918 defined implicitly and automatically. */
919 static void
920 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
922 rtx const_str, return_rtx;
924 return_rtx = rtx_alloc (DEFINE_ATTR);
925 PUT_CODE (return_rtx, DEFINE_ATTR);
927 const_str = rtx_alloc (CONST_STRING);
928 PUT_CODE (const_str, CONST_STRING);
929 XSTR (const_str, 0) = xstrdup ("no");
931 XSTR (return_rtx, 0) = xstrdup (attr_name);
932 XSTR (return_rtx, 1) = xstrdup ("no,yes");
933 XEXP (return_rtx, 2) = const_str;
935 queue->safe_push (return_rtx);
938 /* This routine generates DEFINE_SUBST_ATTR expression with operands
939 ATTR_OPERANDS and places it to QUEUE. */
940 static void
941 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
943 rtx return_rtx;
944 int i;
946 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
947 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
949 for (i = 0; i < 4; i++)
950 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
952 queue->safe_push (return_rtx);
955 /* Read define_subst_attribute construction. It has next form:
956 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
957 Attribute is substituted with value1 when no subst is applied and with
958 value2 in the opposite case.
959 Attributes are added to SUBST_ATTRS_TABLE.
960 In case the iterator is encountered for the first time, it's added to
961 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
963 static void
964 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
965 vec<rtx> *queue)
967 struct mapping *m;
968 struct map_value **end_ptr;
969 const char *attr_operands[4];
970 int i;
972 for (i = 0; i < 4; i++)
973 attr_operands[i] = rtx_reader_ptr->read_string (false);
975 add_define_subst_attr (attr_operands, queue);
977 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
979 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
980 if (!m)
982 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
983 end_ptr = &m->values;
984 end_ptr = add_map_value (end_ptr, 1, "");
985 end_ptr = add_map_value (end_ptr, 2, "");
987 add_define_attr_for_define_subst (attr_operands[1], queue);
990 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
991 end_ptr = &m->values;
992 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
993 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
996 /* Check newly-created code iterator ITERATOR to see whether every code has the
997 same format. */
999 static void
1000 check_code_iterator (struct mapping *iterator)
1002 struct map_value *v;
1003 enum rtx_code bellwether;
1005 bellwether = (enum rtx_code) iterator->values->number;
1006 for (v = iterator->values->next; v != 0; v = v->next)
1007 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1008 fatal_with_file_and_line ("code iterator `%s' combines "
1009 "different rtx formats", iterator->name);
1012 /* Read an rtx-related declaration from the MD file, given that it
1013 starts with directive name RTX_NAME. Return true if it expands to
1014 one or more rtxes (as defined by rtx.def). When returning true,
1015 store the list of rtxes as an EXPR_LIST in *X. */
1017 bool
1018 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1020 static bool initialized = false;
1022 /* Do one-time initialization. */
1023 if (!initialized)
1025 initialize_iterators ();
1026 initialized = true;
1029 /* Handle various rtx-related declarations that aren't themselves
1030 encoded as rtxes. */
1031 if (strcmp (rtx_name, "define_conditions") == 0)
1033 read_conditions ();
1034 return false;
1036 if (strcmp (rtx_name, "define_mode_attr") == 0)
1038 read_mapping (&modes, modes.attrs);
1039 return false;
1041 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1043 read_mapping (&modes, modes.iterators);
1044 return false;
1046 if (strcmp (rtx_name, "define_code_attr") == 0)
1048 read_mapping (&codes, codes.attrs);
1049 return false;
1051 if (strcmp (rtx_name, "define_code_iterator") == 0)
1053 check_code_iterator (read_mapping (&codes, codes.iterators));
1054 return false;
1056 if (strcmp (rtx_name, "define_int_attr") == 0)
1058 read_mapping (&ints, ints.attrs);
1059 return false;
1061 if (strcmp (rtx_name, "define_int_iterator") == 0)
1063 read_mapping (&ints, ints.iterators);
1064 return false;
1066 if (strcmp (rtx_name, "define_subst_attr") == 0)
1068 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1070 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1071 TRUE to process it. */
1072 return true;
1075 apply_iterators (read_rtx_code (rtx_name), rtxen);
1076 iterator_uses.truncate (0);
1077 attribute_uses.truncate (0);
1079 return true;
1082 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1083 either an rtx code or a code iterator. Parse the rest of the rtx and
1084 return it. */
1087 rtx_reader::read_rtx_code (const char *code_name)
1089 RTX_CODE code;
1090 struct mapping *iterator;
1091 const char *format_ptr;
1092 struct md_name name;
1093 rtx return_rtx;
1094 int c;
1096 /* Linked list structure for making RTXs: */
1097 struct rtx_list
1099 struct rtx_list *next;
1100 rtx value; /* Value of this node. */
1103 /* If this code is an iterator, build the rtx using the iterator's
1104 first value. */
1105 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1106 if (iterator != 0)
1107 code = (enum rtx_code) iterator->values->number;
1108 else
1109 code = (enum rtx_code) codes.find_builtin (code_name);
1111 /* If we end up with an insn expression then we free this space below. */
1112 return_rtx = rtx_alloc (code);
1113 format_ptr = GET_RTX_FORMAT (code);
1114 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1115 PUT_CODE (return_rtx, code);
1117 if (iterator)
1118 record_iterator_use (iterator, return_rtx);
1120 /* If what follows is `: mode ', read it and
1121 store the mode in the rtx. */
1123 c = read_skip_spaces ();
1124 if (c == ':')
1126 read_name (&name);
1127 record_potential_iterator_use (&modes, return_rtx, name.string);
1129 else
1130 unread_char (c);
1132 for (int idx = 0; format_ptr[idx] != 0; idx++)
1133 read_rtx_operand (return_rtx, idx);
1135 if (CONST_WIDE_INT_P (return_rtx))
1137 read_name (&name);
1138 validate_const_wide_int (name.string);
1140 const char *s = name.string;
1141 int len;
1142 int index = 0;
1143 int gs = HOST_BITS_PER_WIDE_INT/4;
1144 int pos;
1145 char * buf = XALLOCAVEC (char, gs + 1);
1146 unsigned HOST_WIDE_INT wi;
1147 int wlen;
1149 /* Skip the leading spaces. */
1150 while (*s && ISSPACE (*s))
1151 s++;
1153 /* Skip the leading 0x. */
1154 gcc_assert (s[0] == '0');
1155 gcc_assert (s[1] == 'x');
1156 s += 2;
1158 len = strlen (s);
1159 pos = len - gs;
1160 wlen = (len + gs - 1) / gs; /* Number of words needed */
1162 return_rtx = const_wide_int_alloc (wlen);
1164 while (pos > 0)
1166 #if HOST_BITS_PER_WIDE_INT == 64
1167 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1168 #else
1169 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1170 #endif
1171 CWI_ELT (return_rtx, index++) = wi;
1172 pos -= gs;
1174 strncpy (buf, s, gs - pos);
1175 buf [gs - pos] = 0;
1176 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1177 CWI_ELT (return_rtx, index++) = wi;
1178 /* TODO: After reading, do we want to canonicalize with:
1179 value = lookup_const_wide_int (value); ? */
1183 c = read_skip_spaces ();
1184 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1185 arbitrary number of arguments for them. */
1186 if (c == '('
1187 && (GET_CODE (return_rtx) == AND
1188 || GET_CODE (return_rtx) == IOR))
1189 return read_rtx_variadic (return_rtx);
1191 unread_char (c);
1192 return return_rtx;
1195 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1196 based on the corresponding format character within GET_RTX_FORMAT
1197 for the GET_CODE (RETURN_RTX). */
1199 void
1200 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1202 RTX_CODE code = GET_CODE (return_rtx);
1203 const char *format_ptr = GET_RTX_FORMAT (code);
1204 int c;
1205 struct md_name name;
1207 switch (format_ptr[idx])
1209 /* 0 means a field for internal use only.
1210 Don't expect it to be present in the input. */
1211 case '0':
1212 if (code == REG)
1213 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1214 break;
1216 case 'e':
1217 case 'u':
1218 XEXP (return_rtx, idx) = read_nested_rtx ();
1219 break;
1221 case 'V':
1222 /* 'V' is an optional vector: if a closeparen follows,
1223 just store NULL for this element. */
1224 c = read_skip_spaces ();
1225 unread_char (c);
1226 if (c == ')')
1228 XVEC (return_rtx, idx) = 0;
1229 break;
1231 /* Now process the vector. */
1232 /* FALLTHRU */
1234 case 'E':
1236 /* Obstack to store scratch vector in. */
1237 struct obstack vector_stack;
1238 int list_counter = 0;
1239 rtvec return_vec = NULL_RTVEC;
1241 require_char_ws ('[');
1243 /* Add expressions to a list, while keeping a count. */
1244 obstack_init (&vector_stack);
1245 while ((c = read_skip_spaces ()) && c != ']')
1247 if (c == EOF)
1248 fatal_expected_char (']', c);
1249 unread_char (c);
1250 list_counter++;
1251 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1253 if (list_counter > 0)
1255 return_vec = rtvec_alloc (list_counter);
1256 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1257 list_counter * sizeof (rtx));
1259 else if (format_ptr[idx] == 'E')
1260 fatal_with_file_and_line ("vector must have at least one element");
1261 XVEC (return_rtx, idx) = return_vec;
1262 obstack_free (&vector_stack, NULL);
1263 /* close bracket gotten */
1265 break;
1267 case 'S':
1268 case 'T':
1269 case 's':
1271 char *stringbuf;
1272 int star_if_braced;
1274 c = read_skip_spaces ();
1275 unread_char (c);
1276 if (c == ')')
1278 /* 'S' fields are optional and should be NULL if no string
1279 was given. Also allow normal 's' and 'T' strings to be
1280 omitted, treating them in the same way as empty strings. */
1281 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1282 break;
1285 /* The output template slot of a DEFINE_INSN,
1286 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1287 gets a star inserted as its first character, if it is
1288 written with a brace block instead of a string constant. */
1289 star_if_braced = (format_ptr[idx] == 'T');
1291 stringbuf = read_string (star_if_braced);
1293 /* For insn patterns, we want to provide a default name
1294 based on the file and line, like "*foo.md:12", if the
1295 given name is blank. These are only for define_insn and
1296 define_insn_and_split, to aid debugging. */
1297 if (*stringbuf == '\0'
1298 && idx == 0
1299 && (GET_CODE (return_rtx) == DEFINE_INSN
1300 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1302 char line_name[20];
1303 const char *read_md_filename = get_filename ();
1304 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1305 const char *slash;
1306 for (slash = fn; *slash; slash ++)
1307 if (*slash == '/' || *slash == '\\' || *slash == ':')
1308 fn = slash + 1;
1309 obstack_1grow (&m_string_obstack, '*');
1310 obstack_grow (&m_string_obstack, fn, strlen (fn));
1311 sprintf (line_name, ":%d", get_lineno ());
1312 obstack_grow (&m_string_obstack, line_name, strlen (line_name)+1);
1313 stringbuf = XOBFINISH (&m_string_obstack, char *);
1316 /* Find attr-names in the string. */
1317 char *str;
1318 char *start, *end, *ptr;
1319 char tmpstr[256];
1320 ptr = &tmpstr[0];
1321 end = stringbuf;
1322 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1324 if ((end - start - 1 > 0)
1325 && (end - start - 1 < (int)sizeof (tmpstr)))
1327 strncpy (tmpstr, start+1, end-start-1);
1328 tmpstr[end-start-1] = 0;
1329 end++;
1331 else
1332 break;
1333 struct mapping *m
1334 = (struct mapping *) htab_find (substs.attrs, &ptr);
1335 if (m != 0)
1337 /* Here we should find linked subst-iter. */
1338 str = find_subst_iter_by_attr (ptr);
1339 if (str)
1340 m = (struct mapping *) htab_find (substs.iterators, &str);
1341 else
1342 m = 0;
1344 if (m != 0)
1345 record_iterator_use (m, return_rtx);
1348 if (star_if_braced)
1349 XTMPL (return_rtx, idx) = stringbuf;
1350 else
1351 XSTR (return_rtx, idx) = stringbuf;
1353 break;
1355 case 'w':
1357 HOST_WIDE_INT tmp_wide;
1358 read_name (&name);
1359 validate_const_int (name.string);
1360 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1361 tmp_wide = atoi (name.string);
1362 #else
1363 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1364 tmp_wide = atol (name.string);
1365 #else
1366 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1367 But prefer not to use our hand-rolled function above either. */
1368 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1369 tmp_wide = atoll (name.string);
1370 #else
1371 tmp_wide = atoq (name.string);
1372 #endif
1373 #endif
1374 #endif
1375 XWINT (return_rtx, idx) = tmp_wide;
1377 break;
1379 case 'i':
1380 case 'n':
1381 /* Can be an iterator or an integer constant. */
1382 read_name (&name);
1383 record_potential_iterator_use (&ints, &XINT (return_rtx, idx),
1384 name.string);
1385 break;
1387 case 'r':
1388 read_name (&name);
1389 validate_const_int (name.string);
1390 set_regno_raw (return_rtx, atoi (name.string), 1);
1391 REG_ATTRS (return_rtx) = NULL;
1392 break;
1394 default:
1395 gcc_unreachable ();
1399 /* Read a nested rtx construct from the MD file and return it. */
1402 rtx_reader::read_nested_rtx ()
1404 struct md_name name;
1405 rtx return_rtx;
1407 require_char_ws ('(');
1409 read_name (&name);
1410 if (strcmp (name.string, "nil") == 0)
1411 return_rtx = NULL;
1412 else
1413 return_rtx = read_rtx_code (name.string);
1415 require_char_ws (')');
1417 return return_rtx;
1420 /* Mutually recursive subroutine of read_rtx which reads
1421 (thing x1 x2 x3 ...) and produces RTL as if
1422 (thing x1 (thing x2 (thing x3 ...))) had been written.
1423 When called, FORM is (thing x1 x2), and the file position
1424 is just past the leading parenthesis of x3. Only works
1425 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1427 rtx_reader::read_rtx_variadic (rtx form)
1429 char c = '(';
1430 rtx p = form, q;
1434 unread_char (c);
1436 q = rtx_alloc (GET_CODE (p));
1437 PUT_MODE (q, GET_MODE (p));
1439 XEXP (q, 0) = XEXP (p, 1);
1440 XEXP (q, 1) = read_nested_rtx ();
1442 XEXP (p, 1) = q;
1443 p = q;
1444 c = read_skip_spaces ();
1446 while (c == '(');
1447 unread_char (c);
1448 return form;