c, c++: attribute format on a ctor with a vbase [PR101833, PR47634]
[official-gcc.git] / gcc / read-rtl.cc
blob798d24859914a45c3644921fa6b24156233edc5d
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2022 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 /* This file is compiled twice: once for the generator programs
21 once for the compiler. */
22 #ifdef GENERATOR_FILE
23 #include "bconfig.h"
24 #else
25 #include "config.h"
26 #endif
28 /* Disable rtl checking; it conflicts with the iterator handling. */
29 #undef ENABLE_RTL_CHECKING
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "rtl.h"
35 #include "obstack.h"
36 #include "read-md.h"
37 #include "gensupport.h"
39 /* One element in a singly-linked list of (integer, string) pairs. */
40 struct map_value {
41 struct map_value *next;
42 int number;
43 const char *string;
46 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
47 The integers are iterator values; the strings are either C conditions
48 or attribute values. */
49 struct mapping {
50 /* The name of the iterator or attribute. */
51 const char *name;
53 /* The group (modes or codes) to which the iterator or attribute belongs. */
54 struct iterator_group *group;
56 /* The list of (integer, string) pairs. */
57 struct map_value *values;
59 /* For iterators, records the current value of the iterator. */
60 struct map_value *current_value;
63 /* A structure for abstracting the common parts of iterators. */
64 struct iterator_group {
65 /* Tables of "mapping" structures, one for attributes and one for
66 iterators. */
67 htab_t attrs, iterators;
69 /* The C++ type of the iterator, such as "machine_mode" for modes. */
70 const char *type;
72 /* Treat the given string as the name of a standard mode, etc., and
73 return its integer value. */
74 HOST_WIDE_INT (*find_builtin) (const char *);
76 /* Make the given rtx use the iterator value given by the third argument.
77 If the iterator applies to operands, the second argument gives the
78 operand index, otherwise it is ignored. */
79 void (*apply_iterator) (rtx, unsigned int, HOST_WIDE_INT);
81 /* Return the C token for the given standard mode, code, etc. */
82 const char *(*get_c_token) (int);
85 /* Records one use of an iterator. */
86 struct iterator_use {
87 /* The iterator itself. */
88 struct mapping *iterator;
90 /* The location of the use, as passed to the apply_iterator callback.
91 The index is the number of the operand that used the iterator
92 if applicable, otherwise it is ignored. */
93 rtx x;
94 unsigned int index;
97 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
98 in a non-string rtx field. */
99 struct attribute_use {
100 /* The group that describes the use site. */
101 struct iterator_group *group;
103 /* The location at which the use occurs. */
104 file_location loc;
106 /* The name of the attribute, possibly with an "iterator:" prefix. */
107 const char *value;
109 /* The location of the use, as passed to GROUP's apply_iterator callback.
110 The index is the number of the operand that used the iterator
111 if applicable, otherwise it is ignored. */
112 rtx x;
113 unsigned int index;
116 /* This struct is used to link subst_attr named ATTR_NAME with
117 corresponding define_subst named ITER_NAME. */
118 struct subst_attr_to_iter_mapping
120 char *attr_name;
121 char *iter_name;
124 /* Hash-table to store links between subst-attributes and
125 define_substs. */
126 htab_t subst_attr_to_iter_map = NULL;
127 /* This global stores name of subst-iterator which is currently being
128 processed. */
129 const char *current_iterator_name;
131 static void validate_const_int (const char *);
132 static void one_time_initialization (void);
134 /* Global singleton. */
135 rtx_reader *rtx_reader_ptr = NULL;
137 /* The mode and code iterator structures. */
138 static struct iterator_group modes, codes, ints, substs;
140 /* All iterators used in the current rtx. */
141 static vec<mapping *> current_iterators;
143 /* The list of all iterator uses in the current rtx. */
144 static vec<iterator_use> iterator_uses;
146 /* The list of all attribute uses in the current rtx. */
147 static vec<attribute_use> attribute_uses;
149 /* Provide a version of a function to read a long long if the system does
150 not provide one. */
151 #if (HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG \
152 && !HAVE_DECL_ATOLL \
153 && !defined (HAVE_ATOQ))
154 HOST_WIDE_INT atoll (const char *);
156 HOST_WIDE_INT
157 atoll (const char *p)
159 int neg = 0;
160 HOST_WIDE_INT tmp_wide;
162 while (ISSPACE (*p))
163 p++;
164 if (*p == '-')
165 neg = 1, p++;
166 else if (*p == '+')
167 p++;
169 tmp_wide = 0;
170 while (ISDIGIT (*p))
172 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
173 if (new_wide < tmp_wide)
175 /* Return INT_MAX equiv on overflow. */
176 tmp_wide = HOST_WIDE_INT_M1U >> 1;
177 break;
179 tmp_wide = new_wide;
180 p++;
183 if (neg)
184 tmp_wide = -tmp_wide;
185 return tmp_wide;
187 #endif
189 /* Implementations of the iterator_group callbacks for modes. */
191 static HOST_WIDE_INT
192 find_mode (const char *name)
194 int i;
196 for (i = 0; i < NUM_MACHINE_MODES; i++)
197 if (strcmp (GET_MODE_NAME (i), name) == 0)
198 return i;
200 fatal_with_file_and_line ("unknown mode `%s'", name);
203 static void
204 apply_mode_iterator (rtx x, unsigned int, HOST_WIDE_INT mode)
206 PUT_MODE (x, (machine_mode) mode);
209 static const char *
210 get_mode_token (int mode)
212 return concat ("E_", GET_MODE_NAME (mode), "mode", NULL);
215 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
216 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
218 struct compact_insn_name {
219 RTX_CODE code;
220 const char *name;
223 static const compact_insn_name compact_insn_names[] = {
224 { DEBUG_INSN, "cdebug_insn" },
225 { INSN, "cinsn" },
226 { JUMP_INSN, "cjump_insn" },
227 { CALL_INSN, "ccall_insn" },
228 { JUMP_TABLE_DATA, "cjump_table_data" },
229 { BARRIER, "cbarrier" },
230 { CODE_LABEL, "clabel" },
231 { NOTE, "cnote" }
234 /* Return the rtx code for NAME, or UNKNOWN if NAME isn't a valid rtx code. */
236 static rtx_code
237 maybe_find_code (const char *name)
239 for (int i = 0; i < NUM_RTX_CODE; i++)
240 if (strcmp (GET_RTX_NAME (i), name) == 0)
241 return (rtx_code) i;
243 for (int i = 0; i < (signed)ARRAY_SIZE (compact_insn_names); i++)
244 if (strcmp (compact_insn_names[i].name, name) == 0)
245 return compact_insn_names[i].code;
247 return UNKNOWN;
250 /* Implementations of the iterator_group callbacks for codes. */
252 static HOST_WIDE_INT
253 find_code (const char *name)
255 rtx_code code = maybe_find_code (name);
256 if (code == UNKNOWN)
257 fatal_with_file_and_line ("unknown rtx code `%s'", name);
258 return code;
261 static void
262 apply_code_iterator (rtx x, unsigned int, HOST_WIDE_INT code)
264 PUT_CODE (x, (enum rtx_code) code);
267 static const char *
268 get_code_token (int code)
270 char *name = xstrdup (GET_RTX_NAME (code));
271 for (int i = 0; name[i]; ++i)
272 name[i] = TOUPPER (name[i]);
273 return name;
276 /* Implementations of the iterator_group callbacks for ints. */
278 /* Since GCC does not construct a table of valid constants,
279 we have to accept any int as valid. No cross-checking can
280 be done. */
282 static HOST_WIDE_INT
283 find_int (const char *name)
285 HOST_WIDE_INT tmp;
287 validate_const_int (name);
288 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
289 tmp = atoi (name);
290 #else
291 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
292 tmp = atol (name);
293 #else
294 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
295 But prefer not to use our hand-rolled function above either. */
296 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
297 tmp = atoll (name);
298 #else
299 tmp = atoq (name);
300 #endif
301 #endif
302 #endif
303 return tmp;
306 static void
307 apply_int_iterator (rtx x, unsigned int index, HOST_WIDE_INT value)
309 RTX_CODE code = GET_CODE (x);
310 const char *format_ptr = GET_RTX_FORMAT (code);
312 switch (format_ptr[index])
314 case 'i':
315 case 'n':
316 XINT (x, index) = value;
317 break;
318 case 'w':
319 XWINT (x, index) = value;
320 break;
321 case 'p':
322 gcc_assert (code == SUBREG);
323 SUBREG_BYTE (x) = value;
324 break;
325 default:
326 gcc_unreachable ();
330 static const char *
331 get_int_token (int value)
333 char buffer[HOST_BITS_PER_INT + 1];
334 sprintf (buffer, "%d", value);
335 return xstrdup (buffer);
338 #ifdef GENERATOR_FILE
340 /* This routine adds attribute or does nothing depending on VALUE. When
341 VALUE is 1, it does nothing - the first duplicate of original
342 template is kept untouched when it's subjected to a define_subst.
343 When VALUE isn't 1, the routine modifies RTL-template RT, adding
344 attribute, named exactly as define_subst, which later will be
345 applied. If such attribute has already been added, then no the
346 routine has no effect. */
347 static void
348 apply_subst_iterator (rtx rt, unsigned int, HOST_WIDE_INT value)
350 rtx new_attr;
351 rtvec attrs_vec, new_attrs_vec;
352 int i;
353 /* define_split has no attributes. */
354 if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT)
355 return;
356 gcc_assert (GET_CODE (rt) == DEFINE_INSN
357 || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT
358 || GET_CODE (rt) == DEFINE_INSN_AND_REWRITE
359 || GET_CODE (rt) == DEFINE_EXPAND);
361 int attrs = (GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7
362 : GET_CODE (rt) == DEFINE_INSN_AND_REWRITE ? 6 : 4);
363 attrs_vec = XVEC (rt, attrs);
365 /* If we've already added attribute 'current_iterator_name', then we
366 have nothing to do now. */
367 if (attrs_vec)
369 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
371 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
372 return;
376 /* Add attribute with subst name - it serves as a mark for
377 define_subst which later would be applied to this pattern. */
378 new_attr = rtx_alloc (SET_ATTR);
379 PUT_CODE (new_attr, SET_ATTR);
380 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
381 XSTR (new_attr, 1) = xstrdup ("yes");
383 if (!attrs_vec)
385 new_attrs_vec = rtvec_alloc (1);
386 new_attrs_vec->elem[0] = new_attr;
388 else
390 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
391 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
392 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
393 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
395 XVEC (rt, attrs) = new_attrs_vec;
398 /* Map subst-attribute ATTR to subst iterator ITER. */
400 static void
401 bind_subst_iter_and_attr (const char *iter, const char *attr)
403 struct subst_attr_to_iter_mapping *value;
404 void **slot;
405 if (!subst_attr_to_iter_map)
406 subst_attr_to_iter_map =
407 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
408 value = XNEW (struct subst_attr_to_iter_mapping);
409 value->attr_name = xstrdup (attr);
410 value->iter_name = xstrdup (iter);
411 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
412 *slot = value;
415 #endif /* #ifdef GENERATOR_FILE */
417 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
419 static char*
420 find_subst_iter_by_attr (const char *attr)
422 char *iter_name = NULL;
423 struct subst_attr_to_iter_mapping *value;
424 value = (struct subst_attr_to_iter_mapping*)
425 htab_find (subst_attr_to_iter_map, &attr);
426 if (value)
427 iter_name = value->iter_name;
428 return iter_name;
431 /* Map attribute string P to its current value. Return null if the attribute
432 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
433 there. Report any errors against location LOC. */
435 static struct map_value *
436 map_attr_string (file_location loc, const char *p, mapping **iterator_out = 0)
438 const char *attr;
439 struct mapping *iterator;
440 unsigned int i;
441 struct mapping *m;
442 struct map_value *v;
443 int iterator_name_len;
444 struct map_value *res = NULL;
445 struct mapping *prev = NULL;
447 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
448 attribute name. */
449 attr = strchr (p, ':');
450 if (attr == 0)
452 iterator_name_len = -1;
453 attr = p;
455 else
457 iterator_name_len = attr - p;
458 attr++;
461 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
463 /* If an iterator name was specified, check that it matches. */
464 if (iterator_name_len >= 0
465 && (strncmp (p, iterator->name, iterator_name_len) != 0
466 || iterator->name[iterator_name_len] != 0))
467 continue;
469 /* Find the attribute specification. */
470 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
471 if (m)
473 /* In contrast to code/mode/int iterators, attributes of subst
474 iterators are linked to one specific subst-iterator. So, if
475 we are dealing with subst-iterator, we should check if it's
476 the one which linked with the given attribute. */
477 if (iterator->group == &substs)
479 char *iter_name = find_subst_iter_by_attr (attr);
480 if (strcmp (iter_name, iterator->name) != 0)
481 continue;
483 /* Find the attribute value associated with the current
484 iterator value. */
485 for (v = m->values; v; v = v->next)
486 if (v->number == iterator->current_value->number)
488 if (res && strcmp (v->string, res->string) != 0)
490 error_at (loc, "ambiguous attribute '%s'; could be"
491 " '%s' (via '%s:%s') or '%s' (via '%s:%s')",
492 attr, res->string, prev->name, attr,
493 v->string, iterator->name, attr);
494 return v;
496 if (iterator_out)
497 *iterator_out = iterator;
498 prev = iterator;
499 res = v;
503 return res;
506 /* Apply the current iterator values to STRING. Return the new string
507 if any changes were needed, otherwise return STRING itself. */
509 const char *
510 md_reader::apply_iterator_to_string (const char *string)
512 char *base, *copy, *p, *start, *end;
513 struct map_value *v;
515 if (string == 0 || string[0] == 0)
516 return string;
518 file_location loc = get_md_ptr_loc (string)->loc;
519 base = p = copy = ASTRDUP (string);
520 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
522 p = start + 1;
524 *end = 0;
525 v = map_attr_string (loc, p);
526 *end = '>';
527 if (v == 0)
528 continue;
530 /* Add everything between the last copied byte and the '<',
531 then add in the attribute value. */
532 obstack_grow (&m_string_obstack, base, start - base);
533 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
534 base = end + 1;
536 if (base != copy)
538 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
539 copy = XOBFINISH (&m_string_obstack, char *);
540 copy_md_ptr_loc (copy, string);
541 return copy;
543 return string;
546 /* Return a deep copy of X, substituting the current iterator
547 values into any strings. */
550 md_reader::copy_rtx_for_iterators (rtx original)
552 const char *format_ptr, *p;
553 int i, j;
554 rtx x;
556 if (original == 0)
557 return original;
559 /* Create a shallow copy of ORIGINAL. */
560 x = rtx_alloc (GET_CODE (original));
561 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
563 /* Change each string and recursively change each rtx. */
564 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
565 for (i = 0; format_ptr[i] != 0; i++)
566 switch (format_ptr[i])
568 case 'T':
569 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
570 XTMPL (x, i) = p;
571 break;
573 case 'S':
574 case 's':
575 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
576 XSTR (x, i) = p;
577 break;
579 case 'e':
580 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
581 break;
583 case 'V':
584 case 'E':
585 if (XVEC (original, i))
587 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
588 for (j = 0; j < XVECLEN (x, i); j++)
589 XVECEXP (x, i, j)
590 = copy_rtx_for_iterators (XVECEXP (original, i, j));
592 break;
594 default:
595 break;
597 return x;
600 #ifdef GENERATOR_FILE
602 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
603 has the form "&& ..." (as used in define_insn_and_splits), assume that
604 EXTRA is already satisfied. Empty strings are treated like "true". */
606 static const char *
607 add_condition_to_string (const char *original, const char *extra)
609 if (original != 0 && original[0] == '&' && original[1] == '&')
610 return original;
611 return rtx_reader_ptr->join_c_conditions (original, extra);
614 /* Like add_condition, but applied to all conditions in rtx X. */
616 static void
617 add_condition_to_rtx (rtx x, const char *extra)
619 switch (GET_CODE (x))
621 case DEFINE_INSN:
622 case DEFINE_EXPAND:
623 case DEFINE_SUBST:
624 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
625 break;
627 case DEFINE_SPLIT:
628 case DEFINE_PEEPHOLE:
629 case DEFINE_PEEPHOLE2:
630 case DEFINE_COND_EXEC:
631 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
632 break;
634 case DEFINE_INSN_AND_SPLIT:
635 case DEFINE_INSN_AND_REWRITE:
636 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
637 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
638 break;
640 default:
641 break;
645 /* Apply the current iterator values to all attribute_uses. */
647 static void
648 apply_attribute_uses (void)
650 struct map_value *v;
651 attribute_use *ause;
652 unsigned int i;
654 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
656 v = map_attr_string (ause->loc, ause->value);
657 if (!v)
658 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
659 ause->group->apply_iterator (ause->x, ause->index,
660 ause->group->find_builtin (v->string));
664 /* A htab_traverse callback for iterators. Add all used iterators
665 to current_iterators. */
667 static int
668 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
670 struct mapping *iterator;
672 iterator = (struct mapping *) *slot;
673 if (iterator->current_value)
674 current_iterators.safe_push (iterator);
675 return 1;
678 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
679 be many instances of two overloaded_names having the same name but
680 different arguments, so hashing on the name should be good enough in
681 practice. */
683 static hashval_t
684 overloaded_name_hash (const void *uncast_oname)
686 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
687 return htab_hash_string (oname->name);
690 /* Return true if two overloaded_names are similar enough to share
691 the same generated functions. */
693 static int
694 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
696 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
697 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
698 if (strcmp (oname1->name, oname2->name) != 0
699 || oname1->arg_types.length () != oname2->arg_types.length ())
700 return 0;
702 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
703 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
704 return 0;
706 return 1;
709 /* Return true if X has an instruction name in XSTR (X, 0). */
711 static bool
712 named_rtx_p (rtx x)
714 switch (GET_CODE (x))
716 case DEFINE_EXPAND:
717 case DEFINE_INSN:
718 case DEFINE_INSN_AND_SPLIT:
719 case DEFINE_INSN_AND_REWRITE:
720 return true;
722 default:
723 return false;
727 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
728 If so, return the associated overloaded_name and add the iterator for
729 each argument to ITERATORS. Return null otherwise. */
731 overloaded_name *
732 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
734 /* Check for the leading '@'. */
735 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
736 return NULL;
738 /* Remove the '@', so that no other code needs to worry about it. */
739 const char *name = XSTR (original, 0);
740 file_location loc = get_md_ptr_loc (name)->loc;
741 copy_md_ptr_loc (name + 1, name);
742 name += 1;
743 XSTR (original, 0) = name;
745 /* Build a copy of the name without the '<...>' attribute strings.
746 Add the iterator associated with each such attribute string to ITERATORS
747 and add an associated argument to TMP_ONAME. */
748 char *copy = ASTRDUP (name);
749 char *base = copy, *start, *end;
750 overloaded_name tmp_oname;
751 tmp_oname.arg_types.create (current_iterators.length ());
752 bool pending_underscore_p = false;
753 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
755 *end = 0;
756 mapping *iterator;
757 if (!map_attr_string (loc, start + 1, &iterator))
758 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
759 *end = '>';
761 /* Remove a trailing underscore, so that we don't end a name
762 with "_" or turn "_<...>_" into "__". */
763 if (start != base && start[-1] == '_')
765 start -= 1;
766 pending_underscore_p = true;
769 /* Add the text between either the last '>' or the start of
770 the string and this '<'. */
771 obstack_grow (&m_string_obstack, base, start - base);
772 base = end + 1;
774 /* If there's a character we need to keep after the '>', check
775 whether we should prefix it with a previously-dropped '_'. */
776 if (base[0] != 0 && base[0] != '<')
778 if (pending_underscore_p && base[0] != '_')
779 obstack_1grow (&m_string_obstack, '_');
780 pending_underscore_p = false;
783 /* Record an argument for ITERATOR. */
784 iterators->safe_push (iterator);
785 tmp_oname.arg_types.safe_push (iterator->group->type);
787 if (base == copy)
788 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
790 size_t length = obstack_object_size (&m_string_obstack);
791 if (length == 0)
792 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
794 /* Get the completed name. */
795 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
796 char *new_name = XOBFINISH (&m_string_obstack, char *);
797 tmp_oname.name = new_name;
799 if (!m_overloads_htab)
800 m_overloads_htab = htab_create (31, overloaded_name_hash,
801 overloaded_name_eq_p, NULL);
803 /* See whether another pattern had the same overload name and list
804 of argument types. Create a new permanent one if not. */
805 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
806 overloaded_name *oname = (overloaded_name *) *slot;
807 if (!oname)
809 *slot = oname = new overloaded_name;
810 oname->name = tmp_oname.name;
811 oname->arg_types = tmp_oname.arg_types;
812 oname->next = NULL;
813 oname->first_instance = NULL;
814 oname->next_instance_ptr = &oname->first_instance;
816 *m_next_overload_ptr = oname;
817 m_next_overload_ptr = &oname->next;
819 else
821 obstack_free (&m_string_obstack, new_name);
822 tmp_oname.arg_types.release ();
825 return oname;
828 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
829 gives the iterator associated with argument I of ONAME. */
831 static void
832 add_overload_instance (overloaded_name *oname, const vec<mapping *> &iterators, rtx x)
834 /* Create the instance. */
835 overloaded_instance *instance = new overloaded_instance;
836 instance->next = NULL;
837 instance->arg_values.create (oname->arg_types.length ());
838 for (unsigned int i = 0; i < iterators.length (); ++i)
840 int value = iterators[i]->current_value->number;
841 const char *name = iterators[i]->group->get_c_token (value);
842 instance->arg_values.quick_push (name);
844 instance->name = XSTR (x, 0);
845 instance->insn = x;
847 /* Chain it onto the end of ONAME's list. */
848 *oname->next_instance_ptr = instance;
849 oname->next_instance_ptr = &instance->next;
852 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
853 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
855 static void
856 apply_iterators (rtx original, vec<rtx> *queue)
858 unsigned int i;
859 const char *condition;
860 iterator_use *iuse;
861 struct mapping *iterator;
862 struct map_value *v;
863 rtx x;
865 if (iterator_uses.is_empty ())
867 /* Raise an error if any attributes were used. */
868 apply_attribute_uses ();
870 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
871 fatal_with_file_and_line ("'@' used without iterators");
873 queue->safe_push (original);
874 return;
877 /* Clear out the iterators from the previous run. */
878 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
879 iterator->current_value = NULL;
880 current_iterators.truncate (0);
882 /* Mark the iterators that we need this time. */
883 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
884 iuse->iterator->current_value = iuse->iterator->values;
886 /* Get the list of iterators that are in use, preserving the
887 definition order within each group. */
888 htab_traverse (modes.iterators, add_current_iterators, NULL);
889 htab_traverse (codes.iterators, add_current_iterators, NULL);
890 htab_traverse (ints.iterators, add_current_iterators, NULL);
891 htab_traverse (substs.iterators, add_current_iterators, NULL);
892 gcc_assert (!current_iterators.is_empty ());
894 /* Check whether this is a '@' overloaded pattern. */
895 auto_vec<mapping *, 16> iterators;
896 overloaded_name *oname
897 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
899 for (;;)
901 /* Apply the current iterator values. Accumulate a condition to
902 say when the resulting rtx can be used. */
903 condition = "";
904 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
906 if (iuse->iterator->group == &substs)
907 continue;
908 v = iuse->iterator->current_value;
909 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
910 v->number);
911 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
913 apply_attribute_uses ();
914 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
915 add_condition_to_rtx (x, condition);
917 /* We apply subst iterator after RTL-template is copied, as during
918 subst-iterator processing, we could add an attribute to the
919 RTL-template, and we don't want to do it in the original one. */
920 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
922 v = iuse->iterator->current_value;
923 if (iuse->iterator->group == &substs)
925 iuse->x = x;
926 iuse->index = 0;
927 current_iterator_name = iuse->iterator->name;
928 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
929 v->number);
933 if (oname)
934 add_overload_instance (oname, iterators, x);
936 /* Add the new rtx to the end of the queue. */
937 queue->safe_push (x);
939 /* Lexicographically increment the iterator value sequence.
940 That is, cycle through iterator values, starting from the right,
941 and stopping when one of them doesn't wrap around. */
942 i = current_iterators.length ();
943 for (;;)
945 if (i == 0)
946 return;
947 i--;
948 iterator = current_iterators[i];
949 iterator->current_value = iterator->current_value->next;
950 if (iterator->current_value)
951 break;
952 iterator->current_value = iterator->values;
956 #endif /* #ifdef GENERATOR_FILE */
958 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
959 of the mapping and GROUP is the group to which it belongs. */
961 static struct mapping *
962 add_mapping (struct iterator_group *group, htab_t table, const char *name)
964 struct mapping *m;
965 void **slot;
967 m = XNEW (struct mapping);
968 m->name = xstrdup (name);
969 m->group = group;
970 m->values = 0;
971 m->current_value = NULL;
973 slot = htab_find_slot (table, m, INSERT);
974 if (*slot != 0)
975 fatal_with_file_and_line ("`%s' already defined", name);
977 *slot = m;
978 return m;
981 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
982 END_PTR points to the current null terminator for the list; return
983 a pointer the new null terminator. */
985 static struct map_value **
986 add_map_value (struct map_value **end_ptr, int number, const char *string)
988 struct map_value *value;
990 value = XNEW (struct map_value);
991 value->next = 0;
992 value->number = number;
993 value->string = string;
995 *end_ptr = value;
996 return &value->next;
999 /* Do one-time initialization of the mode and code attributes. */
1001 static void
1002 initialize_iterators (void)
1004 struct mapping *lower, *upper;
1005 struct map_value **lower_ptr, **upper_ptr;
1006 char *copy, *p;
1007 int i;
1009 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1010 modes.iterators = htab_create (13, leading_string_hash,
1011 leading_string_eq_p, 0);
1012 modes.type = "machine_mode";
1013 modes.find_builtin = find_mode;
1014 modes.apply_iterator = apply_mode_iterator;
1015 modes.get_c_token = get_mode_token;
1017 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1018 codes.iterators = htab_create (13, leading_string_hash,
1019 leading_string_eq_p, 0);
1020 codes.type = "rtx_code";
1021 codes.find_builtin = find_code;
1022 codes.apply_iterator = apply_code_iterator;
1023 codes.get_c_token = get_code_token;
1025 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1026 ints.iterators = htab_create (13, leading_string_hash,
1027 leading_string_eq_p, 0);
1028 ints.type = "int";
1029 ints.find_builtin = find_int;
1030 ints.apply_iterator = apply_int_iterator;
1031 ints.get_c_token = get_int_token;
1033 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1034 substs.iterators = htab_create (13, leading_string_hash,
1035 leading_string_eq_p, 0);
1036 substs.type = "int";
1037 substs.find_builtin = find_int; /* We don't use it, anyway. */
1038 #ifdef GENERATOR_FILE
1039 substs.apply_iterator = apply_subst_iterator;
1040 #endif
1041 substs.get_c_token = get_int_token;
1043 lower = add_mapping (&modes, modes.attrs, "mode");
1044 upper = add_mapping (&modes, modes.attrs, "MODE");
1045 lower_ptr = &lower->values;
1046 upper_ptr = &upper->values;
1047 for (i = 0; i < MAX_MACHINE_MODE; i++)
1049 copy = xstrdup (GET_MODE_NAME (i));
1050 for (p = copy; *p != 0; p++)
1051 *p = TOLOWER (*p);
1053 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
1054 lower_ptr = add_map_value (lower_ptr, i, copy);
1057 lower = add_mapping (&codes, codes.attrs, "code");
1058 upper = add_mapping (&codes, codes.attrs, "CODE");
1059 lower_ptr = &lower->values;
1060 upper_ptr = &upper->values;
1061 for (i = 0; i < NUM_RTX_CODE; i++)
1063 copy = xstrdup (GET_RTX_NAME (i));
1064 for (p = copy; *p != 0; p++)
1065 *p = TOUPPER (*p);
1067 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
1068 upper_ptr = add_map_value (upper_ptr, i, copy);
1073 #ifdef GENERATOR_FILE
1074 /* Process a define_conditions directive, starting with the optional
1075 space after the "define_conditions". The directive looks like this:
1077 (define_conditions [
1078 (number "string")
1079 (number "string")
1083 It's not intended to appear in machine descriptions. It is
1084 generated by (the program generated by) genconditions.cc, and
1085 slipped in at the beginning of the sequence of MD files read by
1086 most of the other generators. */
1087 void
1088 md_reader::read_conditions ()
1090 int c;
1092 require_char_ws ('[');
1094 while ( (c = read_skip_spaces ()) != ']')
1096 struct md_name name;
1097 char *expr;
1098 int value;
1100 if (c != '(')
1101 fatal_expected_char ('(', c);
1103 read_name (&name);
1104 validate_const_int (name.string);
1105 value = atoi (name.string);
1107 require_char_ws ('"');
1108 expr = read_quoted_string ();
1110 require_char_ws (')');
1112 add_c_test (expr, value);
1115 #endif /* #ifdef GENERATOR_FILE */
1117 static void
1118 validate_const_int (const char *string)
1120 const char *cp;
1121 int valid = 1;
1123 cp = string;
1124 while (*cp && ISSPACE (*cp))
1125 cp++;
1126 if (*cp == '-' || *cp == '+')
1127 cp++;
1128 if (*cp == 0)
1129 valid = 0;
1130 for (; *cp; cp++)
1131 if (! ISDIGIT (*cp))
1133 valid = 0;
1134 break;
1136 if (!valid)
1137 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
1140 static void
1141 validate_const_wide_int (const char *string)
1143 const char *cp;
1144 int valid = 1;
1146 cp = string;
1147 while (*cp && ISSPACE (*cp))
1148 cp++;
1149 /* Skip the leading 0x. */
1150 if (cp[0] == '0' || cp[1] == 'x')
1151 cp += 2;
1152 else
1153 valid = 0;
1154 if (*cp == 0)
1155 valid = 0;
1156 for (; *cp; cp++)
1157 if (! ISXDIGIT (*cp))
1158 valid = 0;
1159 if (!valid)
1160 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
1163 /* Record that X uses iterator ITERATOR. If the use is in an operand
1164 of X, INDEX is the index of that operand, otherwise it is ignored. */
1166 static void
1167 record_iterator_use (struct mapping *iterator, rtx x, unsigned int index)
1169 struct iterator_use iuse = {iterator, x, index};
1170 iterator_uses.safe_push (iuse);
1173 /* Record that X uses attribute VALUE at location LOC, where VALUE must
1174 match a built-in value from group GROUP. If the use is in an operand
1175 of X, INDEX is the index of that operand, otherwise it is ignored. */
1177 static void
1178 record_attribute_use (struct iterator_group *group, file_location loc, rtx x,
1179 unsigned int index, const char *value)
1181 struct attribute_use ause = {group, loc, value, x, index};
1182 attribute_uses.safe_push (ause);
1185 /* Interpret NAME as either a built-in value, iterator or attribute
1186 for group GROUP. X and INDEX are the values to pass to GROUP's
1187 apply_iterator callback. LOC is the location of the use. */
1189 void
1190 md_reader::record_potential_iterator_use (struct iterator_group *group,
1191 file_location loc,
1192 rtx x, unsigned int index,
1193 const char *name)
1195 struct mapping *m;
1196 size_t len;
1198 len = strlen (name);
1199 if (name[0] == '<' && name[len - 1] == '>')
1201 /* Copy the attribute string into permanent storage, without the
1202 angle brackets around it. */
1203 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
1204 record_attribute_use (group, loc, x, index,
1205 XOBFINISH (&m_string_obstack, char *));
1207 else
1209 m = (struct mapping *) htab_find (group->iterators, &name);
1210 if (m != 0)
1211 record_iterator_use (m, x, index);
1212 else
1213 group->apply_iterator (x, index, group->find_builtin (name));
1217 #ifdef GENERATOR_FILE
1219 /* Finish reading a declaration of the form:
1221 (define... <name> [<value1> ... <valuen>])
1223 from the MD file, where each <valuei> is either a bare symbol name or a
1224 "(<name> <string>)" pair. The "(define..." part has already been read.
1226 Represent the declaration as a "mapping" structure; add it to TABLE
1227 (which belongs to GROUP) and return it. */
1229 struct mapping *
1230 md_reader::read_mapping (struct iterator_group *group, htab_t table)
1232 struct md_name name;
1233 struct mapping *m;
1234 struct map_value **end_ptr;
1235 const char *string;
1236 int number, c;
1238 /* Read the mapping name and create a structure for it. */
1239 read_name (&name);
1240 m = add_mapping (group, table, name.string);
1242 require_char_ws ('[');
1244 /* Read each value. */
1245 end_ptr = &m->values;
1246 c = read_skip_spaces ();
1249 if (c != '(')
1251 /* A bare symbol name that is implicitly paired to an
1252 empty string. */
1253 unread_char (c);
1254 read_name (&name);
1255 string = "";
1257 else
1259 /* A "(name string)" pair. */
1260 read_name (&name);
1261 string = read_string (false);
1262 require_char_ws (')');
1264 number = group->find_builtin (name.string);
1265 end_ptr = add_map_value (end_ptr, number, string);
1266 c = read_skip_spaces ();
1268 while (c != ']');
1270 return m;
1273 /* For iterator with name ATTR_NAME generate define_attr with values
1274 'yes' and 'no'. This attribute is used to mark templates to which
1275 define_subst ATTR_NAME should be applied. This attribute is set and
1276 defined implicitly and automatically. */
1277 static void
1278 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
1280 rtx const_str, return_rtx;
1282 return_rtx = rtx_alloc (DEFINE_ATTR);
1283 PUT_CODE (return_rtx, DEFINE_ATTR);
1285 const_str = rtx_alloc (CONST_STRING);
1286 PUT_CODE (const_str, CONST_STRING);
1287 XSTR (const_str, 0) = xstrdup ("no");
1289 XSTR (return_rtx, 0) = xstrdup (attr_name);
1290 XSTR (return_rtx, 1) = xstrdup ("no,yes");
1291 XEXP (return_rtx, 2) = const_str;
1293 queue->safe_push (return_rtx);
1296 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1297 ATTR_OPERANDS and places it to QUEUE. */
1298 static void
1299 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
1301 rtx return_rtx;
1302 int i;
1304 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
1305 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
1307 for (i = 0; i < 4; i++)
1308 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
1310 queue->safe_push (return_rtx);
1313 /* Read define_subst_attribute construction. It has next form:
1314 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1315 Attribute is substituted with value1 when no subst is applied and with
1316 value2 in the opposite case.
1317 Attributes are added to SUBST_ATTRS_TABLE.
1318 In case the iterator is encountered for the first time, it's added to
1319 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1321 static void
1322 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
1323 vec<rtx> *queue)
1325 struct mapping *m;
1326 struct map_value **end_ptr;
1327 const char *attr_operands[4];
1328 int i;
1330 for (i = 0; i < 4; i++)
1331 attr_operands[i] = rtx_reader_ptr->read_string (false);
1333 add_define_subst_attr (attr_operands, queue);
1335 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1337 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1338 if (!m)
1340 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1341 end_ptr = &m->values;
1342 end_ptr = add_map_value (end_ptr, 1, "");
1343 add_map_value (end_ptr, 2, "");
1345 add_define_attr_for_define_subst (attr_operands[1], queue);
1348 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1349 end_ptr = &m->values;
1350 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1351 add_map_value (end_ptr, 2, attr_operands[3]);
1354 /* Check newly-created code iterator ITERATOR to see whether every code has the
1355 same format. */
1357 static void
1358 check_code_iterator (struct mapping *iterator)
1360 struct map_value *v;
1361 enum rtx_code bellwether;
1363 bellwether = (enum rtx_code) iterator->values->number;
1364 for (v = iterator->values->next; v != 0; v = v->next)
1365 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1366 fatal_with_file_and_line ("code iterator `%s' combines "
1367 "`%s' and `%s', which have different "
1368 "rtx formats", iterator->name,
1369 GET_RTX_NAME (bellwether),
1370 GET_RTX_NAME (v->number));
1373 /* Check that all values of attribute ATTR are rtx codes that have a
1374 consistent format. Return a representative code. */
1376 static rtx_code
1377 check_code_attribute (mapping *attr)
1379 rtx_code bellwether = UNKNOWN;
1380 for (map_value *v = attr->values; v != 0; v = v->next)
1382 rtx_code code = maybe_find_code (v->string);
1383 if (code == UNKNOWN)
1384 fatal_with_file_and_line ("code attribute `%s' contains "
1385 "unrecognized rtx code `%s'",
1386 attr->name, v->string);
1387 if (bellwether == UNKNOWN)
1388 bellwether = code;
1389 else if (strcmp (GET_RTX_FORMAT (bellwether),
1390 GET_RTX_FORMAT (code)) != 0)
1391 fatal_with_file_and_line ("code attribute `%s' combines "
1392 "`%s' and `%s', which have different "
1393 "rtx formats", attr->name,
1394 GET_RTX_NAME (bellwether),
1395 GET_RTX_NAME (code));
1397 return bellwether;
1400 /* Read an rtx-related declaration from the MD file, given that it
1401 starts with directive name RTX_NAME. Return true if it expands to
1402 one or more rtxes (as defined by rtx.def). When returning true,
1403 store the list of rtxes as an EXPR_LIST in *X. */
1405 bool
1406 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1408 /* Handle various rtx-related declarations that aren't themselves
1409 encoded as rtxes. */
1410 if (strcmp (rtx_name, "define_conditions") == 0)
1412 read_conditions ();
1413 return false;
1415 if (strcmp (rtx_name, "define_mode_attr") == 0)
1417 read_mapping (&modes, modes.attrs);
1418 return false;
1420 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1422 read_mapping (&modes, modes.iterators);
1423 return false;
1425 if (strcmp (rtx_name, "define_code_attr") == 0)
1427 read_mapping (&codes, codes.attrs);
1428 return false;
1430 if (strcmp (rtx_name, "define_code_iterator") == 0)
1432 check_code_iterator (read_mapping (&codes, codes.iterators));
1433 return false;
1435 if (strcmp (rtx_name, "define_int_attr") == 0)
1437 read_mapping (&ints, ints.attrs);
1438 return false;
1440 if (strcmp (rtx_name, "define_int_iterator") == 0)
1442 read_mapping (&ints, ints.iterators);
1443 return false;
1445 if (strcmp (rtx_name, "define_subst_attr") == 0)
1447 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1449 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1450 TRUE to process it. */
1451 return true;
1454 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1455 iterator_uses.truncate (0);
1456 attribute_uses.truncate (0);
1458 return true;
1461 #endif /* #ifdef GENERATOR_FILE */
1463 /* Do one-time initialization. */
1465 static void
1466 one_time_initialization (void)
1468 static bool initialized = false;
1470 if (!initialized)
1472 initialize_iterators ();
1473 initialized = true;
1477 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1478 consuming the terminator character if CONSUME_TERMINATOR is true.
1479 Return all characters before the terminator as an allocated buffer. */
1481 char *
1482 rtx_reader::read_until (const char *terminator_chars, bool consume_terminator)
1484 int ch = read_skip_spaces ();
1485 unread_char (ch);
1486 auto_vec<char> buf;
1487 while (1)
1489 ch = read_char ();
1490 if (strchr (terminator_chars, ch))
1492 if (!consume_terminator)
1493 unread_char (ch);
1494 break;
1496 buf.safe_push (ch);
1498 buf.safe_push ('\0');
1499 return xstrdup (buf.address ());
1502 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1504 static void
1505 read_flags (rtx return_rtx)
1507 while (1)
1509 int ch = read_char ();
1510 if (ch != '/')
1512 unread_char (ch);
1513 break;
1516 int flag_char = read_char ();
1517 switch (flag_char)
1519 case 's':
1520 RTX_FLAG (return_rtx, in_struct) = 1;
1521 break;
1522 case 'v':
1523 RTX_FLAG (return_rtx, volatil) = 1;
1524 break;
1525 case 'u':
1526 RTX_FLAG (return_rtx, unchanging) = 1;
1527 break;
1528 case 'f':
1529 RTX_FLAG (return_rtx, frame_related) = 1;
1530 break;
1531 case 'j':
1532 RTX_FLAG (return_rtx, jump) = 1;
1533 break;
1534 case 'c':
1535 RTX_FLAG (return_rtx, call) = 1;
1536 break;
1537 case 'i':
1538 RTX_FLAG (return_rtx, return_val) = 1;
1539 break;
1540 default:
1541 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char);
1546 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1547 or fail if STRING isn't recognized. */
1549 static int
1550 parse_reg_note_name (const char *string)
1552 for (int i = 0; i < REG_NOTE_MAX; i++)
1553 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1554 return i;
1555 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1558 /* Allocate an rtx for code NAME. If NAME is a code iterator or code
1559 attribute, record its use for later and use one of its possible
1560 values as an interim rtx code. */
1563 rtx_reader::rtx_alloc_for_name (const char *name)
1565 #ifdef GENERATOR_FILE
1566 size_t len = strlen (name);
1567 if (name[0] == '<' && name[len - 1] == '>')
1569 /* Copy the attribute string into permanent storage, without the
1570 angle brackets around it. */
1571 obstack *strings = get_string_obstack ();
1572 obstack_grow0 (strings, name + 1, len - 2);
1573 char *deferred_name = XOBFINISH (strings, char *);
1575 /* Find the name of the attribute. */
1576 const char *attr = strchr (deferred_name, ':');
1577 if (!attr)
1578 attr = deferred_name;
1580 /* Find the attribute itself. */
1581 mapping *m = (mapping *) htab_find (codes.attrs, &attr);
1582 if (!m)
1583 fatal_with_file_and_line ("unknown code attribute `%s'", attr);
1585 /* Pick the first possible code for now, and record the attribute
1586 use for later. */
1587 rtx x = rtx_alloc (check_code_attribute (m));
1588 record_attribute_use (&codes, get_current_location (),
1589 x, 0, deferred_name);
1590 return x;
1593 mapping *iterator = (mapping *) htab_find (codes.iterators, &name);
1594 if (iterator != 0)
1596 /* Pick the first possible code for now, and record the iterator
1597 use for later. */
1598 rtx x = rtx_alloc (rtx_code (iterator->values->number));
1599 record_iterator_use (iterator, x, 0);
1600 return x;
1602 #endif
1604 return rtx_alloc (rtx_code (codes.find_builtin (name)));
1607 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1608 either an rtx code or a code iterator. Parse the rest of the rtx and
1609 return it. */
1612 rtx_reader::read_rtx_code (const char *code_name)
1614 RTX_CODE code;
1615 const char *format_ptr;
1616 struct md_name name;
1617 rtx return_rtx;
1618 int c;
1619 long reuse_id = -1;
1621 /* Linked list structure for making RTXs: */
1622 struct rtx_list
1624 struct rtx_list *next;
1625 rtx value; /* Value of this node. */
1628 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1629 if (ISDIGIT (code_name[0]))
1631 reuse_id = atoi (code_name);
1632 while (char ch = *code_name++)
1633 if (ch == '|')
1634 break;
1637 /* Handle "reuse_rtx". */
1638 if (strcmp (code_name, "reuse_rtx") == 0)
1640 read_name (&name);
1641 unsigned idx = atoi (name.string);
1642 /* Look it up by ID. */
1643 gcc_assert (idx < m_reuse_rtx_by_id.length ());
1644 return_rtx = m_reuse_rtx_by_id[idx];
1645 return return_rtx;
1648 /* Handle "const_double_zero". */
1649 if (strcmp (code_name, "const_double_zero") == 0)
1651 code = CONST_DOUBLE;
1652 return_rtx = rtx_alloc (code);
1653 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1654 PUT_CODE (return_rtx, code);
1655 c = read_skip_spaces ();
1656 if (c == ':')
1658 file_location loc = read_name (&name);
1659 record_potential_iterator_use (&modes, loc, return_rtx, 0,
1660 name.string);
1662 else
1663 unread_char (c);
1664 return return_rtx;
1667 /* If we end up with an insn expression then we free this space below. */
1668 return_rtx = rtx_alloc_for_name (code_name);
1669 code = GET_CODE (return_rtx);
1670 format_ptr = GET_RTX_FORMAT (code);
1671 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1672 PUT_CODE (return_rtx, code);
1674 if (reuse_id != -1)
1676 /* Store away for later reuse. */
1677 m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1, true);
1678 m_reuse_rtx_by_id[reuse_id] = return_rtx;
1681 /* Check for flags. */
1682 read_flags (return_rtx);
1684 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1685 if ((GET_CODE (return_rtx) == EXPR_LIST
1686 || GET_CODE (return_rtx) == INSN_LIST
1687 || GET_CODE (return_rtx) == INT_LIST)
1688 && !m_in_call_function_usage)
1690 char ch = read_char ();
1691 if (ch == ':')
1693 read_name (&name);
1694 PUT_MODE_RAW (return_rtx,
1695 (machine_mode)parse_reg_note_name (name.string));
1697 else
1698 unread_char (ch);
1701 /* If what follows is `: mode ', read it and
1702 store the mode in the rtx. */
1704 c = read_skip_spaces ();
1705 if (c == ':')
1707 file_location loc = read_name (&name);
1708 record_potential_iterator_use (&modes, loc, return_rtx, 0, name.string);
1710 else
1711 unread_char (c);
1713 if (INSN_CHAIN_CODE_P (code))
1715 read_name (&name);
1716 INSN_UID (return_rtx) = atoi (name.string);
1719 /* Use the format_ptr to parse the various operands of this rtx. */
1720 for (int idx = 0; format_ptr[idx] != 0; idx++)
1721 return_rtx = read_rtx_operand (return_rtx, idx);
1723 /* Handle any additional information that after the regular fields
1724 (e.g. when parsing function dumps). */
1725 handle_any_trailing_information (return_rtx);
1727 if (CONST_WIDE_INT_P (return_rtx))
1729 read_name (&name);
1730 validate_const_wide_int (name.string);
1732 const char *s = name.string;
1733 int len;
1734 int index = 0;
1735 int gs = HOST_BITS_PER_WIDE_INT/4;
1736 int pos;
1737 char * buf = XALLOCAVEC (char, gs + 1);
1738 unsigned HOST_WIDE_INT wi;
1739 int wlen;
1741 /* Skip the leading spaces. */
1742 while (*s && ISSPACE (*s))
1743 s++;
1745 /* Skip the leading 0x. */
1746 gcc_assert (s[0] == '0');
1747 gcc_assert (s[1] == 'x');
1748 s += 2;
1750 len = strlen (s);
1751 pos = len - gs;
1752 wlen = (len + gs - 1) / gs; /* Number of words needed */
1754 return_rtx = const_wide_int_alloc (wlen);
1756 while (pos > 0)
1758 #if HOST_BITS_PER_WIDE_INT == 64
1759 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1760 #else
1761 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1762 #endif
1763 CWI_ELT (return_rtx, index++) = wi;
1764 pos -= gs;
1766 strncpy (buf, s, gs - pos);
1767 buf [gs - pos] = 0;
1768 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1769 CWI_ELT (return_rtx, index++) = wi;
1770 /* TODO: After reading, do we want to canonicalize with:
1771 value = lookup_const_wide_int (value); ? */
1775 c = read_skip_spaces ();
1776 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1777 arbitrary number of arguments for them. */
1778 if (c == '('
1779 && (GET_CODE (return_rtx) == AND
1780 || GET_CODE (return_rtx) == IOR))
1781 return read_rtx_variadic (return_rtx);
1783 unread_char (c);
1784 return return_rtx;
1787 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1788 based on the corresponding format character within GET_RTX_FORMAT
1789 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1790 This is a virtual function, so that function_reader can override
1791 some parsing, and potentially return a different rtx. */
1794 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1796 RTX_CODE code = GET_CODE (return_rtx);
1797 const char *format_ptr = GET_RTX_FORMAT (code);
1798 int c;
1799 struct md_name name;
1801 switch (format_ptr[idx])
1803 /* 0 means a field for internal use only.
1804 Don't expect it to be present in the input. */
1805 case '0':
1806 if (code == REG)
1807 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1808 break;
1810 case 'e':
1811 XEXP (return_rtx, idx) = read_nested_rtx ();
1812 break;
1814 case 'u':
1815 XEXP (return_rtx, idx) = read_nested_rtx ();
1816 break;
1818 case 'V':
1819 /* 'V' is an optional vector: if a closeparen follows,
1820 just store NULL for this element. */
1821 c = read_skip_spaces ();
1822 unread_char (c);
1823 if (c == ')')
1825 XVEC (return_rtx, idx) = 0;
1826 break;
1828 /* Now process the vector. */
1829 /* FALLTHRU */
1831 case 'E':
1833 /* Obstack to store scratch vector in. */
1834 struct obstack vector_stack;
1835 int list_counter = 0;
1836 rtvec return_vec = NULL_RTVEC;
1837 rtx saved_rtx = NULL_RTX;
1839 require_char_ws ('[');
1841 /* Add expressions to a list, while keeping a count. */
1842 obstack_init (&vector_stack);
1843 while ((c = read_skip_spaces ()) && c != ']')
1845 if (c == EOF)
1846 fatal_expected_char (']', c);
1847 unread_char (c);
1849 rtx value;
1850 int repeat_count = 1;
1851 if (c == 'r')
1853 /* Process "repeated xN" directive. */
1854 read_name (&name);
1855 if (strcmp (name.string, "repeated"))
1856 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1857 name.string);
1858 read_name (&name);
1859 if (!sscanf (name.string, "x%d", &repeat_count))
1860 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1861 name.string);
1863 /* We already saw one of the instances. */
1864 repeat_count--;
1865 value = saved_rtx;
1867 else
1868 value = read_nested_rtx ();
1870 for (; repeat_count > 0; repeat_count--)
1872 list_counter++;
1873 obstack_ptr_grow (&vector_stack, value);
1875 saved_rtx = value;
1877 if (list_counter > 0)
1879 return_vec = rtvec_alloc (list_counter);
1880 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1881 list_counter * sizeof (rtx));
1883 else if (format_ptr[idx] == 'E')
1884 fatal_with_file_and_line ("vector must have at least one element");
1885 XVEC (return_rtx, idx) = return_vec;
1886 obstack_free (&vector_stack, NULL);
1887 /* close bracket gotten */
1889 break;
1891 case 'S':
1892 case 'T':
1893 case 's':
1895 char *stringbuf;
1896 int star_if_braced;
1898 c = read_skip_spaces ();
1899 unread_char (c);
1900 if (c == ')')
1902 /* 'S' fields are optional and should be NULL if no string
1903 was given. Also allow normal 's' and 'T' strings to be
1904 omitted, treating them in the same way as empty strings. */
1905 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1906 break;
1909 /* The output template slot of a DEFINE_INSN, DEFINE_INSN_AND_SPLIT,
1910 DEFINE_INSN_AND_REWRITE or DEFINE_PEEPHOLE automatically
1911 gets a star inserted as its first character, if it is
1912 written with a brace block instead of a string constant. */
1913 star_if_braced = (format_ptr[idx] == 'T');
1915 stringbuf = read_string (star_if_braced);
1916 if (!stringbuf)
1917 break;
1919 #ifdef GENERATOR_FILE
1920 /* For insn patterns, we want to provide a default name
1921 based on the file and line, like "*foo.md:12", if the
1922 given name is blank. These are only for define_insn and
1923 define_insn_and_split, to aid debugging. */
1924 if (*stringbuf == '\0'
1925 && idx == 0
1926 && (GET_CODE (return_rtx) == DEFINE_INSN
1927 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT
1928 || GET_CODE (return_rtx) == DEFINE_INSN_AND_REWRITE))
1930 const char *old_stringbuf = stringbuf;
1931 struct obstack *string_obstack = get_string_obstack ();
1932 char line_name[20];
1933 const char *read_md_filename = get_filename ();
1934 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1935 const char *slash;
1936 for (slash = fn; *slash; slash ++)
1937 if (*slash == '/' || *slash == '\\' || *slash == ':')
1938 fn = slash + 1;
1939 obstack_1grow (string_obstack, '*');
1940 obstack_grow (string_obstack, fn, strlen (fn));
1941 sprintf (line_name, ":%d", get_lineno ());
1942 obstack_grow (string_obstack, line_name, strlen (line_name)+1);
1943 stringbuf = XOBFINISH (string_obstack, char *);
1944 copy_md_ptr_loc (stringbuf, old_stringbuf);
1947 /* Find attr-names in the string. */
1948 char *str;
1949 char *start, *end, *ptr;
1950 char tmpstr[256];
1951 ptr = &tmpstr[0];
1952 end = stringbuf;
1953 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1955 if ((end - start - 1 > 0)
1956 && (end - start - 1 < (int)sizeof (tmpstr)))
1958 strncpy (tmpstr, start+1, end-start-1);
1959 tmpstr[end-start-1] = 0;
1960 end++;
1962 else
1963 break;
1964 struct mapping *m
1965 = (struct mapping *) htab_find (substs.attrs, &ptr);
1966 if (m != 0)
1968 /* Here we should find linked subst-iter. */
1969 str = find_subst_iter_by_attr (ptr);
1970 if (str)
1971 m = (struct mapping *) htab_find (substs.iterators, &str);
1972 else
1973 m = 0;
1975 if (m != 0)
1976 record_iterator_use (m, return_rtx, 0);
1978 #endif /* #ifdef GENERATOR_FILE */
1980 const char *string_ptr = finalize_string (stringbuf);
1982 if (star_if_braced)
1983 XTMPL (return_rtx, idx) = string_ptr;
1984 else
1985 XSTR (return_rtx, idx) = string_ptr;
1987 break;
1989 case 'i':
1990 case 'n':
1991 case 'w':
1992 case 'p':
1994 /* Can be an iterator or an integer constant. */
1995 file_location loc = read_name (&name);
1996 record_potential_iterator_use (&ints, loc, return_rtx, idx,
1997 name.string);
1998 break;
2001 case 'r':
2002 read_name (&name);
2003 validate_const_int (name.string);
2004 set_regno_raw (return_rtx, atoi (name.string), 1);
2005 REG_ATTRS (return_rtx) = NULL;
2006 break;
2008 default:
2009 gcc_unreachable ();
2012 return return_rtx;
2015 /* Read a nested rtx construct from the MD file and return it. */
2018 rtx_reader::read_nested_rtx ()
2020 struct md_name name;
2021 rtx return_rtx;
2023 /* In compact dumps, trailing "(nil)" values can be omitted.
2024 Handle such dumps. */
2025 if (peek_char () == ')')
2026 return NULL_RTX;
2028 require_char_ws ('(');
2030 read_name (&name);
2031 if (strcmp (name.string, "nil") == 0)
2032 return_rtx = NULL;
2033 else
2034 return_rtx = read_rtx_code (name.string);
2036 require_char_ws (')');
2038 return_rtx = postprocess (return_rtx);
2040 return return_rtx;
2043 /* Mutually recursive subroutine of read_rtx which reads
2044 (thing x1 x2 x3 ...) and produces RTL as if
2045 (thing x1 (thing x2 (thing x3 ...))) had been written.
2046 When called, FORM is (thing x1 x2), and the file position
2047 is just past the leading parenthesis of x3. Only works
2048 for THINGs which are dyadic expressions, e.g. AND, IOR. */
2050 rtx_reader::read_rtx_variadic (rtx form)
2052 char c = '(';
2053 rtx p = form, q;
2057 unread_char (c);
2059 q = rtx_alloc (GET_CODE (p));
2060 PUT_MODE (q, GET_MODE (p));
2062 XEXP (q, 0) = XEXP (p, 1);
2063 XEXP (q, 1) = read_nested_rtx ();
2065 XEXP (p, 1) = q;
2066 p = q;
2067 c = read_skip_spaces ();
2069 while (c == '(');
2070 unread_char (c);
2071 return form;
2074 /* Constructor for class rtx_reader. */
2076 rtx_reader::rtx_reader (bool compact)
2077 : md_reader (compact),
2078 m_in_call_function_usage (false)
2080 /* Set the global singleton pointer. */
2081 rtx_reader_ptr = this;
2083 one_time_initialization ();
2086 /* Destructor for class rtx_reader. */
2088 rtx_reader::~rtx_reader ()
2090 /* Clear the global singleton pointer. */
2091 rtx_reader_ptr = NULL;