Define arm_arch_core_flags in a single file
[official-gcc.git] / gcc / read-rtl.c
blobf74c8751a9882d66c62b82eba365fecf20f2f714
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 /* Global singleton. */
111 rtx_reader *rtx_reader_ptr = NULL;
113 /* The mode and code iterator structures. */
114 static struct iterator_group modes, codes, ints, substs;
116 /* All iterators used in the current rtx. */
117 static vec<mapping *> current_iterators;
119 /* The list of all iterator uses in the current rtx. */
120 static vec<iterator_use> iterator_uses;
122 /* The list of all attribute uses in the current rtx. */
123 static vec<attribute_use> attribute_uses;
125 /* Implementations of the iterator_group callbacks for modes. */
127 static int
128 find_mode (const char *name)
130 int i;
132 for (i = 0; i < NUM_MACHINE_MODES; i++)
133 if (strcmp (GET_MODE_NAME (i), name) == 0)
134 return i;
136 fatal_with_file_and_line ("unknown mode `%s'", name);
139 static void
140 apply_mode_iterator (void *loc, int mode)
142 PUT_MODE ((rtx) loc, (machine_mode) mode);
145 /* Implementations of the iterator_group callbacks for codes. */
147 static int
148 find_code (const char *name)
150 int i;
152 for (i = 0; i < NUM_RTX_CODE; i++)
153 if (strcmp (GET_RTX_NAME (i), name) == 0)
154 return i;
156 fatal_with_file_and_line ("unknown rtx code `%s'", name);
159 static void
160 apply_code_iterator (void *loc, int code)
162 PUT_CODE ((rtx) loc, (enum rtx_code) code);
165 /* Implementations of the iterator_group callbacks for ints. */
167 /* Since GCC does not construct a table of valid constants,
168 we have to accept any int as valid. No cross-checking can
169 be done. */
171 static int
172 find_int (const char *name)
174 validate_const_int (name);
175 return atoi (name);
178 static void
179 apply_int_iterator (void *loc, int value)
181 *(int *)loc = value;
184 /* This routine adds attribute or does nothing depending on VALUE. When
185 VALUE is 1, it does nothing - the first duplicate of original
186 template is kept untouched when it's subjected to a define_subst.
187 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
188 attribute, named exactly as define_subst, which later will be
189 applied. If such attribute has already been added, then no the
190 routine has no effect. */
191 static void
192 apply_subst_iterator (void *loc, int value)
194 rtx rt = (rtx)loc;
195 rtx new_attr;
196 rtvec attrs_vec, new_attrs_vec;
197 int i;
198 if (value == 1)
199 return;
200 gcc_assert (GET_CODE (rt) == DEFINE_INSN
201 || GET_CODE (rt) == DEFINE_EXPAND);
203 attrs_vec = XVEC (rt, 4);
205 /* If we've already added attribute 'current_iterator_name', then we
206 have nothing to do now. */
207 if (attrs_vec)
209 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
211 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
212 return;
216 /* Add attribute with subst name - it serves as a mark for
217 define_subst which later would be applied to this pattern. */
218 new_attr = rtx_alloc (SET_ATTR);
219 PUT_CODE (new_attr, SET_ATTR);
220 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
221 XSTR (new_attr, 1) = xstrdup ("yes");
223 if (!attrs_vec)
225 new_attrs_vec = rtvec_alloc (1);
226 new_attrs_vec->elem[0] = new_attr;
228 else
230 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
231 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
232 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
233 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
235 XVEC (rt, 4) = new_attrs_vec;
238 /* Map subst-attribute ATTR to subst iterator ITER. */
240 static void
241 bind_subst_iter_and_attr (const char *iter, const char *attr)
243 struct subst_attr_to_iter_mapping *value;
244 void **slot;
245 if (!subst_attr_to_iter_map)
246 subst_attr_to_iter_map =
247 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
248 value = XNEW (struct subst_attr_to_iter_mapping);
249 value->attr_name = xstrdup (attr);
250 value->iter_name = xstrdup (iter);
251 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
252 *slot = value;
255 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
257 static char*
258 find_subst_iter_by_attr (const char *attr)
260 char *iter_name = NULL;
261 struct subst_attr_to_iter_mapping *value;
262 value = (struct subst_attr_to_iter_mapping*)
263 htab_find (subst_attr_to_iter_map, &attr);
264 if (value)
265 iter_name = value->iter_name;
266 return iter_name;
269 /* Map attribute string P to its current value. Return null if the attribute
270 isn't known. */
272 static struct map_value *
273 map_attr_string (const char *p)
275 const char *attr;
276 struct mapping *iterator;
277 unsigned int i;
278 struct mapping *m;
279 struct map_value *v;
280 int iterator_name_len;
282 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
283 attribute name. */
284 attr = strchr (p, ':');
285 if (attr == 0)
287 iterator_name_len = -1;
288 attr = p;
290 else
292 iterator_name_len = attr - p;
293 attr++;
296 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
298 /* If an iterator name was specified, check that it matches. */
299 if (iterator_name_len >= 0
300 && (strncmp (p, iterator->name, iterator_name_len) != 0
301 || iterator->name[iterator_name_len] != 0))
302 continue;
304 /* Find the attribute specification. */
305 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
306 if (m)
308 /* In contrast to code/mode/int iterators, attributes of subst
309 iterators are linked to one specific subst-iterator. So, if
310 we are dealing with subst-iterator, we should check if it's
311 the one which linked with the given attribute. */
312 if (iterator->group == &substs)
314 char *iter_name = find_subst_iter_by_attr (attr);
315 if (strcmp (iter_name, iterator->name) != 0)
316 continue;
318 /* Find the attribute value associated with the current
319 iterator value. */
320 for (v = m->values; v; v = v->next)
321 if (v->number == iterator->current_value->number)
322 return v;
325 return NULL;
328 /* Apply the current iterator values to STRING. Return the new string
329 if any changes were needed, otherwise return STRING itself. */
331 const char *
332 md_reader::apply_iterator_to_string (const char *string)
334 char *base, *copy, *p, *start, *end;
335 struct map_value *v;
337 if (string == 0)
338 return string;
340 base = p = copy = ASTRDUP (string);
341 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
343 p = start + 1;
345 *end = 0;
346 v = map_attr_string (p);
347 *end = '>';
348 if (v == 0)
349 continue;
351 /* Add everything between the last copied byte and the '<',
352 then add in the attribute value. */
353 obstack_grow (&m_string_obstack, base, start - base);
354 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
355 base = end + 1;
357 if (base != copy)
359 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
360 copy = XOBFINISH (&m_string_obstack, char *);
361 copy_md_ptr_loc (copy, string);
362 return copy;
364 return string;
367 /* Return a deep copy of X, substituting the current iterator
368 values into any strings. */
371 md_reader::copy_rtx_for_iterators (rtx original)
373 const char *format_ptr, *p;
374 int i, j;
375 rtx x;
377 if (original == 0)
378 return original;
380 /* Create a shallow copy of ORIGINAL. */
381 x = rtx_alloc (GET_CODE (original));
382 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
384 /* Change each string and recursively change each rtx. */
385 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
386 for (i = 0; format_ptr[i] != 0; i++)
387 switch (format_ptr[i])
389 case 'T':
390 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
391 XTMPL (x, i) = p;
392 break;
394 case 'S':
395 case 's':
396 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
397 XSTR (x, i) = p;
398 break;
400 case 'e':
401 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
402 break;
404 case 'V':
405 case 'E':
406 if (XVEC (original, i))
408 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
409 for (j = 0; j < XVECLEN (x, i); j++)
410 XVECEXP (x, i, j)
411 = copy_rtx_for_iterators (XVECEXP (original, i, j));
413 break;
415 default:
416 break;
418 return x;
421 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
422 has the form "&& ..." (as used in define_insn_and_splits), assume that
423 EXTRA is already satisfied. Empty strings are treated like "true". */
425 static const char *
426 add_condition_to_string (const char *original, const char *extra)
428 if (original != 0 && original[0] == '&' && original[1] == '&')
429 return original;
430 return rtx_reader_ptr->join_c_conditions (original, extra);
433 /* Like add_condition, but applied to all conditions in rtx X. */
435 static void
436 add_condition_to_rtx (rtx x, const char *extra)
438 switch (GET_CODE (x))
440 case DEFINE_INSN:
441 case DEFINE_EXPAND:
442 case DEFINE_SUBST:
443 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
444 break;
446 case DEFINE_SPLIT:
447 case DEFINE_PEEPHOLE:
448 case DEFINE_PEEPHOLE2:
449 case DEFINE_COND_EXEC:
450 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
451 break;
453 case DEFINE_INSN_AND_SPLIT:
454 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
455 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
456 break;
458 default:
459 break;
463 /* Apply the current iterator values to all attribute_uses. */
465 static void
466 apply_attribute_uses (void)
468 struct map_value *v;
469 attribute_use *ause;
470 unsigned int i;
472 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
474 v = map_attr_string (ause->value);
475 if (!v)
476 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
477 ause->group->apply_iterator (ause->ptr,
478 ause->group->find_builtin (v->string));
482 /* A htab_traverse callback for iterators. Add all used iterators
483 to current_iterators. */
485 static int
486 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
488 struct mapping *iterator;
490 iterator = (struct mapping *) *slot;
491 if (iterator->current_value)
492 current_iterators.safe_push (iterator);
493 return 1;
496 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
497 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
499 static void
500 apply_iterators (rtx original, vec<rtx> *queue)
502 unsigned int i;
503 const char *condition;
504 iterator_use *iuse;
505 struct mapping *iterator;
506 struct map_value *v;
507 rtx x;
509 if (iterator_uses.is_empty ())
511 /* Raise an error if any attributes were used. */
512 apply_attribute_uses ();
513 queue->safe_push (original);
514 return;
517 /* Clear out the iterators from the previous run. */
518 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
519 iterator->current_value = NULL;
520 current_iterators.truncate (0);
522 /* Mark the iterators that we need this time. */
523 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
524 iuse->iterator->current_value = iuse->iterator->values;
526 /* Get the list of iterators that are in use, preserving the
527 definition order within each group. */
528 htab_traverse (modes.iterators, add_current_iterators, NULL);
529 htab_traverse (codes.iterators, add_current_iterators, NULL);
530 htab_traverse (ints.iterators, add_current_iterators, NULL);
531 htab_traverse (substs.iterators, add_current_iterators, NULL);
532 gcc_assert (!current_iterators.is_empty ());
534 for (;;)
536 /* Apply the current iterator values. Accumulate a condition to
537 say when the resulting rtx can be used. */
538 condition = "";
539 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
541 if (iuse->iterator->group == &substs)
542 continue;
543 v = iuse->iterator->current_value;
544 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
545 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
547 apply_attribute_uses ();
548 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
549 add_condition_to_rtx (x, condition);
551 /* We apply subst iterator after RTL-template is copied, as during
552 subst-iterator processing, we could add an attribute to the
553 RTL-template, and we don't want to do it in the original one. */
554 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
556 v = iuse->iterator->current_value;
557 if (iuse->iterator->group == &substs)
559 iuse->ptr = x;
560 current_iterator_name = iuse->iterator->name;
561 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
564 /* Add the new rtx to the end of the queue. */
565 queue->safe_push (x);
567 /* Lexicographically increment the iterator value sequence.
568 That is, cycle through iterator values, starting from the right,
569 and stopping when one of them doesn't wrap around. */
570 i = current_iterators.length ();
571 for (;;)
573 if (i == 0)
574 return;
575 i--;
576 iterator = current_iterators[i];
577 iterator->current_value = iterator->current_value->next;
578 if (iterator->current_value)
579 break;
580 iterator->current_value = iterator->values;
585 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
586 of the mapping and GROUP is the group to which it belongs. */
588 static struct mapping *
589 add_mapping (struct iterator_group *group, htab_t table, const char *name)
591 struct mapping *m;
592 void **slot;
594 m = XNEW (struct mapping);
595 m->name = xstrdup (name);
596 m->group = group;
597 m->values = 0;
598 m->current_value = NULL;
600 slot = htab_find_slot (table, m, INSERT);
601 if (*slot != 0)
602 fatal_with_file_and_line ("`%s' already defined", name);
604 *slot = m;
605 return m;
608 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
609 END_PTR points to the current null terminator for the list; return
610 a pointer the new null terminator. */
612 static struct map_value **
613 add_map_value (struct map_value **end_ptr, int number, const char *string)
615 struct map_value *value;
617 value = XNEW (struct map_value);
618 value->next = 0;
619 value->number = number;
620 value->string = string;
622 *end_ptr = value;
623 return &value->next;
626 /* Do one-time initialization of the mode and code attributes. */
628 static void
629 initialize_iterators (void)
631 struct mapping *lower, *upper;
632 struct map_value **lower_ptr, **upper_ptr;
633 char *copy, *p;
634 int i;
636 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
637 modes.iterators = htab_create (13, leading_string_hash,
638 leading_string_eq_p, 0);
639 modes.find_builtin = find_mode;
640 modes.apply_iterator = apply_mode_iterator;
642 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
643 codes.iterators = htab_create (13, leading_string_hash,
644 leading_string_eq_p, 0);
645 codes.find_builtin = find_code;
646 codes.apply_iterator = apply_code_iterator;
648 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
649 ints.iterators = htab_create (13, leading_string_hash,
650 leading_string_eq_p, 0);
651 ints.find_builtin = find_int;
652 ints.apply_iterator = apply_int_iterator;
654 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
655 substs.iterators = htab_create (13, leading_string_hash,
656 leading_string_eq_p, 0);
657 substs.find_builtin = find_int; /* We don't use it, anyway. */
658 substs.apply_iterator = apply_subst_iterator;
660 lower = add_mapping (&modes, modes.attrs, "mode");
661 upper = add_mapping (&modes, modes.attrs, "MODE");
662 lower_ptr = &lower->values;
663 upper_ptr = &upper->values;
664 for (i = 0; i < MAX_MACHINE_MODE; i++)
666 copy = xstrdup (GET_MODE_NAME (i));
667 for (p = copy; *p != 0; p++)
668 *p = TOLOWER (*p);
670 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
671 lower_ptr = add_map_value (lower_ptr, i, copy);
674 lower = add_mapping (&codes, codes.attrs, "code");
675 upper = add_mapping (&codes, codes.attrs, "CODE");
676 lower_ptr = &lower->values;
677 upper_ptr = &upper->values;
678 for (i = 0; i < NUM_RTX_CODE; i++)
680 copy = xstrdup (GET_RTX_NAME (i));
681 for (p = copy; *p != 0; p++)
682 *p = TOUPPER (*p);
684 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
685 upper_ptr = add_map_value (upper_ptr, i, copy);
689 /* Provide a version of a function to read a long long if the system does
690 not provide one. */
691 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
692 HOST_WIDE_INT atoll (const char *);
694 HOST_WIDE_INT
695 atoll (const char *p)
697 int neg = 0;
698 HOST_WIDE_INT tmp_wide;
700 while (ISSPACE (*p))
701 p++;
702 if (*p == '-')
703 neg = 1, p++;
704 else if (*p == '+')
705 p++;
707 tmp_wide = 0;
708 while (ISDIGIT (*p))
710 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
711 if (new_wide < tmp_wide)
713 /* Return INT_MAX equiv on overflow. */
714 tmp_wide = HOST_WIDE_INT_M1U >> 1;
715 break;
717 tmp_wide = new_wide;
718 p++;
721 if (neg)
722 tmp_wide = -tmp_wide;
723 return tmp_wide;
725 #endif
727 /* Process a define_conditions directive, starting with the optional
728 space after the "define_conditions". The directive looks like this:
730 (define_conditions [
731 (number "string")
732 (number "string")
736 It's not intended to appear in machine descriptions. It is
737 generated by (the program generated by) genconditions.c, and
738 slipped in at the beginning of the sequence of MD files read by
739 most of the other generators. */
740 void
741 md_reader::read_conditions ()
743 int c;
745 require_char_ws ('[');
747 while ( (c = read_skip_spaces ()) != ']')
749 struct md_name name;
750 char *expr;
751 int value;
753 if (c != '(')
754 fatal_expected_char ('(', c);
756 read_name (&name);
757 validate_const_int (name.string);
758 value = atoi (name.string);
760 require_char_ws ('"');
761 expr = read_quoted_string ();
763 require_char_ws (')');
765 add_c_test (expr, value);
769 static void
770 validate_const_int (const char *string)
772 const char *cp;
773 int valid = 1;
775 cp = string;
776 while (*cp && ISSPACE (*cp))
777 cp++;
778 if (*cp == '-' || *cp == '+')
779 cp++;
780 if (*cp == 0)
781 valid = 0;
782 for (; *cp; cp++)
783 if (! ISDIGIT (*cp))
785 valid = 0;
786 break;
788 if (!valid)
789 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
792 static void
793 validate_const_wide_int (const char *string)
795 const char *cp;
796 int valid = 1;
798 cp = string;
799 while (*cp && ISSPACE (*cp))
800 cp++;
801 /* Skip the leading 0x. */
802 if (cp[0] == '0' || cp[1] == 'x')
803 cp += 2;
804 else
805 valid = 0;
806 if (*cp == 0)
807 valid = 0;
808 for (; *cp; cp++)
809 if (! ISXDIGIT (*cp))
810 valid = 0;
811 if (!valid)
812 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
815 /* Record that PTR uses iterator ITERATOR. */
817 static void
818 record_iterator_use (struct mapping *iterator, void *ptr)
820 struct iterator_use iuse = {iterator, ptr};
821 iterator_uses.safe_push (iuse);
824 /* Record that PTR uses attribute VALUE, which must match a built-in
825 value from group GROUP. */
827 static void
828 record_attribute_use (struct iterator_group *group, void *ptr,
829 const char *value)
831 struct attribute_use ause = {group, value, ptr};
832 attribute_uses.safe_push (ause);
835 /* Interpret NAME as either a built-in value, iterator or attribute
836 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
837 callback. */
839 void
840 md_reader::record_potential_iterator_use (struct iterator_group *group,
841 void *ptr, const char *name)
843 struct mapping *m;
844 size_t len;
846 len = strlen (name);
847 if (name[0] == '<' && name[len - 1] == '>')
849 /* Copy the attribute string into permanent storage, without the
850 angle brackets around it. */
851 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
852 record_attribute_use (group, ptr, XOBFINISH (&m_string_obstack, char *));
854 else
856 m = (struct mapping *) htab_find (group->iterators, &name);
857 if (m != 0)
858 record_iterator_use (m, ptr);
859 else
860 group->apply_iterator (ptr, group->find_builtin (name));
864 /* Finish reading a declaration of the form:
866 (define... <name> [<value1> ... <valuen>])
868 from the MD file, where each <valuei> is either a bare symbol name or a
869 "(<name> <string>)" pair. The "(define..." part has already been read.
871 Represent the declaration as a "mapping" structure; add it to TABLE
872 (which belongs to GROUP) and return it. */
874 struct mapping *
875 md_reader::read_mapping (struct iterator_group *group, htab_t table)
877 struct md_name name;
878 struct mapping *m;
879 struct map_value **end_ptr;
880 const char *string;
881 int number, c;
883 /* Read the mapping name and create a structure for it. */
884 read_name (&name);
885 m = add_mapping (group, table, name.string);
887 require_char_ws ('[');
889 /* Read each value. */
890 end_ptr = &m->values;
891 c = read_skip_spaces ();
894 if (c != '(')
896 /* A bare symbol name that is implicitly paired to an
897 empty string. */
898 unread_char (c);
899 read_name (&name);
900 string = "";
902 else
904 /* A "(name string)" pair. */
905 read_name (&name);
906 string = read_string (false);
907 require_char_ws (')');
909 number = group->find_builtin (name.string);
910 end_ptr = add_map_value (end_ptr, number, string);
911 c = read_skip_spaces ();
913 while (c != ']');
915 return m;
918 /* For iterator with name ATTR_NAME generate define_attr with values
919 'yes' and 'no'. This attribute is used to mark templates to which
920 define_subst ATTR_NAME should be applied. This attribute is set and
921 defined implicitly and automatically. */
922 static void
923 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
925 rtx const_str, return_rtx;
927 return_rtx = rtx_alloc (DEFINE_ATTR);
928 PUT_CODE (return_rtx, DEFINE_ATTR);
930 const_str = rtx_alloc (CONST_STRING);
931 PUT_CODE (const_str, CONST_STRING);
932 XSTR (const_str, 0) = xstrdup ("no");
934 XSTR (return_rtx, 0) = xstrdup (attr_name);
935 XSTR (return_rtx, 1) = xstrdup ("no,yes");
936 XEXP (return_rtx, 2) = const_str;
938 queue->safe_push (return_rtx);
941 /* This routine generates DEFINE_SUBST_ATTR expression with operands
942 ATTR_OPERANDS and places it to QUEUE. */
943 static void
944 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
946 rtx return_rtx;
947 int i;
949 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
950 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
952 for (i = 0; i < 4; i++)
953 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
955 queue->safe_push (return_rtx);
958 /* Read define_subst_attribute construction. It has next form:
959 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
960 Attribute is substituted with value1 when no subst is applied and with
961 value2 in the opposite case.
962 Attributes are added to SUBST_ATTRS_TABLE.
963 In case the iterator is encountered for the first time, it's added to
964 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
966 static void
967 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
968 vec<rtx> *queue)
970 struct mapping *m;
971 struct map_value **end_ptr;
972 const char *attr_operands[4];
973 int i;
975 for (i = 0; i < 4; i++)
976 attr_operands[i] = rtx_reader_ptr->read_string (false);
978 add_define_subst_attr (attr_operands, queue);
980 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
982 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
983 if (!m)
985 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
986 end_ptr = &m->values;
987 end_ptr = add_map_value (end_ptr, 1, "");
988 end_ptr = add_map_value (end_ptr, 2, "");
990 add_define_attr_for_define_subst (attr_operands[1], queue);
993 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
994 end_ptr = &m->values;
995 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
996 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
999 /* Check newly-created code iterator ITERATOR to see whether every code has the
1000 same format. */
1002 static void
1003 check_code_iterator (struct mapping *iterator)
1005 struct map_value *v;
1006 enum rtx_code bellwether;
1008 bellwether = (enum rtx_code) iterator->values->number;
1009 for (v = iterator->values->next; v != 0; v = v->next)
1010 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1011 fatal_with_file_and_line ("code iterator `%s' combines "
1012 "different rtx formats", iterator->name);
1015 /* Read an rtx-related declaration from the MD file, given that it
1016 starts with directive name RTX_NAME. Return true if it expands to
1017 one or more rtxes (as defined by rtx.def). When returning true,
1018 store the list of rtxes as an EXPR_LIST in *X. */
1020 bool
1021 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1023 static bool initialized = false;
1025 /* Do one-time initialization. */
1026 if (!initialized)
1028 initialize_iterators ();
1029 initialized = true;
1032 /* Handle various rtx-related declarations that aren't themselves
1033 encoded as rtxes. */
1034 if (strcmp (rtx_name, "define_conditions") == 0)
1036 read_conditions ();
1037 return false;
1039 if (strcmp (rtx_name, "define_mode_attr") == 0)
1041 read_mapping (&modes, modes.attrs);
1042 return false;
1044 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1046 read_mapping (&modes, modes.iterators);
1047 return false;
1049 if (strcmp (rtx_name, "define_code_attr") == 0)
1051 read_mapping (&codes, codes.attrs);
1052 return false;
1054 if (strcmp (rtx_name, "define_code_iterator") == 0)
1056 check_code_iterator (read_mapping (&codes, codes.iterators));
1057 return false;
1059 if (strcmp (rtx_name, "define_int_attr") == 0)
1061 read_mapping (&ints, ints.attrs);
1062 return false;
1064 if (strcmp (rtx_name, "define_int_iterator") == 0)
1066 read_mapping (&ints, ints.iterators);
1067 return false;
1069 if (strcmp (rtx_name, "define_subst_attr") == 0)
1071 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1073 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1074 TRUE to process it. */
1075 return true;
1078 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1079 iterator_uses.truncate (0);
1080 attribute_uses.truncate (0);
1082 return true;
1085 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1086 either an rtx code or a code iterator. Parse the rest of the rtx and
1087 return it. */
1090 rtx_reader::read_rtx_code (const char *code_name)
1092 RTX_CODE code;
1093 struct mapping *iterator;
1094 const char *format_ptr;
1095 struct md_name name;
1096 rtx return_rtx;
1097 int c;
1099 /* Linked list structure for making RTXs: */
1100 struct rtx_list
1102 struct rtx_list *next;
1103 rtx value; /* Value of this node. */
1106 /* If this code is an iterator, build the rtx using the iterator's
1107 first value. */
1108 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1109 if (iterator != 0)
1110 code = (enum rtx_code) iterator->values->number;
1111 else
1112 code = (enum rtx_code) codes.find_builtin (code_name);
1114 /* If we end up with an insn expression then we free this space below. */
1115 return_rtx = rtx_alloc (code);
1116 format_ptr = GET_RTX_FORMAT (code);
1117 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1118 PUT_CODE (return_rtx, code);
1120 if (iterator)
1121 record_iterator_use (iterator, return_rtx);
1123 /* If what follows is `: mode ', read it and
1124 store the mode in the rtx. */
1126 c = read_skip_spaces ();
1127 if (c == ':')
1129 read_name (&name);
1130 record_potential_iterator_use (&modes, return_rtx, name.string);
1132 else
1133 unread_char (c);
1135 for (int idx = 0; format_ptr[idx] != 0; idx++)
1136 read_rtx_operand (return_rtx, idx);
1138 if (CONST_WIDE_INT_P (return_rtx))
1140 read_name (&name);
1141 validate_const_wide_int (name.string);
1143 const char *s = name.string;
1144 int len;
1145 int index = 0;
1146 int gs = HOST_BITS_PER_WIDE_INT/4;
1147 int pos;
1148 char * buf = XALLOCAVEC (char, gs + 1);
1149 unsigned HOST_WIDE_INT wi;
1150 int wlen;
1152 /* Skip the leading spaces. */
1153 while (*s && ISSPACE (*s))
1154 s++;
1156 /* Skip the leading 0x. */
1157 gcc_assert (s[0] == '0');
1158 gcc_assert (s[1] == 'x');
1159 s += 2;
1161 len = strlen (s);
1162 pos = len - gs;
1163 wlen = (len + gs - 1) / gs; /* Number of words needed */
1165 return_rtx = const_wide_int_alloc (wlen);
1167 while (pos > 0)
1169 #if HOST_BITS_PER_WIDE_INT == 64
1170 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1171 #else
1172 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1173 #endif
1174 CWI_ELT (return_rtx, index++) = wi;
1175 pos -= gs;
1177 strncpy (buf, s, gs - pos);
1178 buf [gs - pos] = 0;
1179 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1180 CWI_ELT (return_rtx, index++) = wi;
1181 /* TODO: After reading, do we want to canonicalize with:
1182 value = lookup_const_wide_int (value); ? */
1186 c = read_skip_spaces ();
1187 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1188 arbitrary number of arguments for them. */
1189 if (c == '('
1190 && (GET_CODE (return_rtx) == AND
1191 || GET_CODE (return_rtx) == IOR))
1192 return read_rtx_variadic (return_rtx);
1194 unread_char (c);
1195 return return_rtx;
1198 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1199 based on the corresponding format character within GET_RTX_FORMAT
1200 for the GET_CODE (RETURN_RTX). */
1202 void
1203 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1205 RTX_CODE code = GET_CODE (return_rtx);
1206 const char *format_ptr = GET_RTX_FORMAT (code);
1207 int c;
1208 struct md_name name;
1210 switch (format_ptr[idx])
1212 /* 0 means a field for internal use only.
1213 Don't expect it to be present in the input. */
1214 case '0':
1215 if (code == REG)
1216 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1217 break;
1219 case 'e':
1220 case 'u':
1221 XEXP (return_rtx, idx) = read_nested_rtx ();
1222 break;
1224 case 'V':
1225 /* 'V' is an optional vector: if a closeparen follows,
1226 just store NULL for this element. */
1227 c = read_skip_spaces ();
1228 unread_char (c);
1229 if (c == ')')
1231 XVEC (return_rtx, idx) = 0;
1232 break;
1234 /* Now process the vector. */
1235 /* FALLTHRU */
1237 case 'E':
1239 /* Obstack to store scratch vector in. */
1240 struct obstack vector_stack;
1241 int list_counter = 0;
1242 rtvec return_vec = NULL_RTVEC;
1244 require_char_ws ('[');
1246 /* Add expressions to a list, while keeping a count. */
1247 obstack_init (&vector_stack);
1248 while ((c = read_skip_spaces ()) && c != ']')
1250 if (c == EOF)
1251 fatal_expected_char (']', c);
1252 unread_char (c);
1253 list_counter++;
1254 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1256 if (list_counter > 0)
1258 return_vec = rtvec_alloc (list_counter);
1259 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1260 list_counter * sizeof (rtx));
1262 else if (format_ptr[idx] == 'E')
1263 fatal_with_file_and_line ("vector must have at least one element");
1264 XVEC (return_rtx, idx) = return_vec;
1265 obstack_free (&vector_stack, NULL);
1266 /* close bracket gotten */
1268 break;
1270 case 'S':
1271 case 'T':
1272 case 's':
1274 char *stringbuf;
1275 int star_if_braced;
1276 struct obstack *string_obstack = get_string_obstack ();
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 = 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", 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. */
1406 rtx_reader::read_nested_rtx ()
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. */
1431 rtx_reader::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;
1455 /* Constructor for class rtx_reader. */
1457 rtx_reader::rtx_reader ()
1458 : md_reader ()
1460 /* Set the global singleton pointer. */
1461 rtx_reader_ptr = this;
1464 /* Destructor for class rtx_reader. */
1466 rtx_reader::~rtx_reader ()
1468 /* Clear the global singleton pointer. */
1469 rtx_reader_ptr = NULL;