c++: more dummy non_constant_p arg avoidance
[official-gcc.git] / gcc / read-rtl.cc
blob292f8b72d434cc40aab42ade589494e0fcbb585e
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2023 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);
84 /* True if each iterator name should be treated as an attribute that
85 maps to the C token produced by get_c_token. This means that for
86 an iterator ITER, <ITER> can be used in strings to refer to the
87 current value of ITER, as a C token. */
88 bool has_self_attr;
91 /* Records one use of an iterator. */
92 struct iterator_use {
93 /* The iterator itself. */
94 struct mapping *iterator;
96 /* The location of the use, as passed to the apply_iterator callback.
97 The index is the number of the operand that used the iterator
98 if applicable, otherwise it is ignored. */
99 rtx x;
100 unsigned int index;
103 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
104 in a non-string rtx field. */
105 struct attribute_use {
106 /* The group that describes the use site. */
107 struct iterator_group *group;
109 /* The location at which the use occurs. */
110 file_location loc;
112 /* The name of the attribute, possibly with an "iterator:" prefix. */
113 const char *value;
115 /* The location of the use, as passed to GROUP's apply_iterator callback.
116 The index is the number of the operand that used the iterator
117 if applicable, otherwise it is ignored. */
118 rtx x;
119 unsigned int index;
122 /* This struct is used to link subst_attr named ATTR_NAME with
123 corresponding define_subst named ITER_NAME. */
124 struct subst_attr_to_iter_mapping
126 char *attr_name;
127 char *iter_name;
130 /* Hash-table to store links between subst-attributes and
131 define_substs. */
132 htab_t subst_attr_to_iter_map = NULL;
133 /* This global stores name of subst-iterator which is currently being
134 processed. */
135 const char *current_iterator_name;
137 static void validate_const_int (const char *);
138 static void one_time_initialization (void);
140 /* Global singleton. */
141 rtx_reader *rtx_reader_ptr = NULL;
143 /* The mode and code iterator structures. */
144 static struct iterator_group modes, codes, ints, substs;
146 /* All iterators used in the current rtx. */
147 static vec<mapping *> current_iterators;
149 /* The list of all iterator uses in the current rtx. */
150 static vec<iterator_use> iterator_uses;
152 /* The list of all attribute uses in the current rtx. */
153 static vec<attribute_use> attribute_uses;
155 /* Provide a version of a function to read a long long if the system does
156 not provide one. */
157 #if (HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG \
158 && !HAVE_DECL_ATOLL \
159 && !defined (HAVE_ATOQ))
160 HOST_WIDE_INT atoll (const char *);
162 HOST_WIDE_INT
163 atoll (const char *p)
165 int neg = 0;
166 HOST_WIDE_INT tmp_wide;
168 while (ISSPACE (*p))
169 p++;
170 if (*p == '-')
171 neg = 1, p++;
172 else if (*p == '+')
173 p++;
175 tmp_wide = 0;
176 while (ISDIGIT (*p))
178 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
179 if (new_wide < tmp_wide)
181 /* Return INT_MAX equiv on overflow. */
182 tmp_wide = HOST_WIDE_INT_M1U >> 1;
183 break;
185 tmp_wide = new_wide;
186 p++;
189 if (neg)
190 tmp_wide = -tmp_wide;
191 return tmp_wide;
193 #endif
195 /* Implementations of the iterator_group callbacks for modes. */
197 static HOST_WIDE_INT
198 find_mode (const char *name)
200 int i;
202 for (i = 0; i < NUM_MACHINE_MODES; i++)
203 if (strcmp (GET_MODE_NAME (i), name) == 0)
204 return i;
206 fatal_with_file_and_line ("unknown mode `%s'", name);
209 static void
210 apply_mode_iterator (rtx x, unsigned int, HOST_WIDE_INT mode)
212 PUT_MODE (x, (machine_mode) mode);
215 static const char *
216 get_mode_token (int mode)
218 return concat ("E_", GET_MODE_NAME (mode), "mode", NULL);
221 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
222 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
224 struct compact_insn_name {
225 RTX_CODE code;
226 const char *name;
229 static const compact_insn_name compact_insn_names[] = {
230 { DEBUG_INSN, "cdebug_insn" },
231 { INSN, "cinsn" },
232 { JUMP_INSN, "cjump_insn" },
233 { CALL_INSN, "ccall_insn" },
234 { JUMP_TABLE_DATA, "cjump_table_data" },
235 { BARRIER, "cbarrier" },
236 { CODE_LABEL, "clabel" },
237 { NOTE, "cnote" }
240 /* Return the rtx code for NAME, or UNKNOWN if NAME isn't a valid rtx code. */
242 static rtx_code
243 maybe_find_code (const char *name)
245 for (int i = 0; i < NUM_RTX_CODE; i++)
246 if (strcmp (GET_RTX_NAME (i), name) == 0)
247 return (rtx_code) i;
249 for (int i = 0; i < (signed)ARRAY_SIZE (compact_insn_names); i++)
250 if (strcmp (compact_insn_names[i].name, name) == 0)
251 return compact_insn_names[i].code;
253 return UNKNOWN;
256 /* Implementations of the iterator_group callbacks for codes. */
258 static HOST_WIDE_INT
259 find_code (const char *name)
261 rtx_code code = maybe_find_code (name);
262 if (code == UNKNOWN)
263 fatal_with_file_and_line ("unknown rtx code `%s'", name);
264 return code;
267 static void
268 apply_code_iterator (rtx x, unsigned int, HOST_WIDE_INT code)
270 PUT_CODE (x, (enum rtx_code) code);
273 static const char *
274 get_code_token (int code)
276 char *name = xstrdup (GET_RTX_NAME (code));
277 for (int i = 0; name[i]; ++i)
278 name[i] = TOUPPER (name[i]);
279 return name;
282 /* Implementations of the iterator_group callbacks for ints. */
284 /* Since GCC does not construct a table of valid constants,
285 we have to accept any int as valid. No cross-checking can
286 be done. */
288 static HOST_WIDE_INT
289 find_int (const char *name)
291 HOST_WIDE_INT tmp;
293 struct md_constant tmp_def;
294 tmp_def.name = const_cast<char *> (name);
295 auto htab = rtx_reader_ptr->get_md_constants ();
296 if (auto def = (struct md_constant *) htab_find (htab, &tmp_def))
297 name = def->value;
299 validate_const_int (name);
300 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
301 tmp = atoi (name);
302 #else
303 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
304 tmp = atol (name);
305 #else
306 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
307 But prefer not to use our hand-rolled function above either. */
308 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
309 tmp = atoll (name);
310 #else
311 tmp = atoq (name);
312 #endif
313 #endif
314 #endif
315 return tmp;
318 static void
319 apply_int_iterator (rtx x, unsigned int index, HOST_WIDE_INT value)
321 RTX_CODE code = GET_CODE (x);
322 const char *format_ptr = GET_RTX_FORMAT (code);
324 switch (format_ptr[index])
326 case 'i':
327 case 'n':
328 XINT (x, index) = value;
329 break;
330 case 'w':
331 XWINT (x, index) = value;
332 break;
333 case 'p':
334 gcc_assert (code == SUBREG);
335 SUBREG_BYTE (x) = value;
336 break;
337 default:
338 gcc_unreachable ();
342 static const char *
343 get_int_token (int value)
345 char buffer[HOST_BITS_PER_INT + 1];
346 sprintf (buffer, "%d", value);
347 return xstrdup (buffer);
350 #ifdef GENERATOR_FILE
352 /* This routine adds attribute or does nothing depending on VALUE. When
353 VALUE is 1, it does nothing - the first duplicate of original
354 template is kept untouched when it's subjected to a define_subst.
355 When VALUE isn't 1, the routine modifies RTL-template RT, adding
356 attribute, named exactly as define_subst, which later will be
357 applied. If such attribute has already been added, then no the
358 routine has no effect. */
359 static void
360 apply_subst_iterator (rtx rt, unsigned int, HOST_WIDE_INT value)
362 rtx new_attr;
363 rtvec attrs_vec, new_attrs_vec;
364 int i;
365 /* define_split has no attributes. */
366 if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT)
367 return;
368 gcc_assert (GET_CODE (rt) == DEFINE_INSN
369 || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT
370 || GET_CODE (rt) == DEFINE_INSN_AND_REWRITE
371 || GET_CODE (rt) == DEFINE_EXPAND);
373 int attrs = (GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7
374 : GET_CODE (rt) == DEFINE_INSN_AND_REWRITE ? 6 : 4);
375 attrs_vec = XVEC (rt, attrs);
377 /* If we've already added attribute 'current_iterator_name', then we
378 have nothing to do now. */
379 if (attrs_vec)
381 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
383 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
384 return;
388 /* Add attribute with subst name - it serves as a mark for
389 define_subst which later would be applied to this pattern. */
390 new_attr = rtx_alloc (SET_ATTR);
391 PUT_CODE (new_attr, SET_ATTR);
392 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
393 XSTR (new_attr, 1) = xstrdup ("yes");
395 if (!attrs_vec)
397 new_attrs_vec = rtvec_alloc (1);
398 new_attrs_vec->elem[0] = new_attr;
400 else
402 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
403 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
404 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
405 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
407 XVEC (rt, attrs) = new_attrs_vec;
410 /* Map subst-attribute ATTR to subst iterator ITER. */
412 static void
413 bind_subst_iter_and_attr (const char *iter, const char *attr)
415 struct subst_attr_to_iter_mapping *value;
416 void **slot;
417 if (!subst_attr_to_iter_map)
418 subst_attr_to_iter_map =
419 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
420 value = XNEW (struct subst_attr_to_iter_mapping);
421 value->attr_name = xstrdup (attr);
422 value->iter_name = xstrdup (iter);
423 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
424 *slot = value;
427 #endif /* #ifdef GENERATOR_FILE */
429 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
431 static char*
432 find_subst_iter_by_attr (const char *attr)
434 char *iter_name = NULL;
435 struct subst_attr_to_iter_mapping *value;
436 value = (struct subst_attr_to_iter_mapping*)
437 htab_find (subst_attr_to_iter_map, &attr);
438 if (value)
439 iter_name = value->iter_name;
440 return iter_name;
443 /* Map attribute string P to its current value. Return null if the attribute
444 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
445 there. Report any errors against location LOC. */
447 static struct map_value *
448 map_attr_string (file_location loc, const char *p, mapping **iterator_out = 0)
450 const char *attr;
451 struct mapping *iterator;
452 unsigned int i;
453 struct mapping *m;
454 struct map_value *v;
455 int iterator_name_len;
456 struct map_value *res = NULL;
457 struct mapping *prev = NULL;
459 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
460 attribute name. */
461 attr = strchr (p, ':');
462 if (attr == 0)
464 iterator_name_len = -1;
465 attr = p;
467 else
469 iterator_name_len = attr - p;
470 attr++;
473 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
475 /* If an iterator name was specified, check that it matches. */
476 if (iterator_name_len >= 0
477 && (strncmp (p, iterator->name, iterator_name_len) != 0
478 || iterator->name[iterator_name_len] != 0))
479 continue;
481 if (iterator->group->has_self_attr
482 && strcmp (attr, iterator->name) == 0)
484 if (iterator_out)
485 *iterator_out = iterator;
486 int number = iterator->current_value->number;
487 const char *string = iterator->group->get_c_token (number);
488 if (res && strcmp (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 string, iterator->name, attr);
494 return res;
496 prev = iterator;
497 res = new map_value { nullptr, number, string };
500 /* Find the attribute specification. */
501 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
502 if (m)
504 /* In contrast to code/mode/int iterators, attributes of subst
505 iterators are linked to one specific subst-iterator. So, if
506 we are dealing with subst-iterator, we should check if it's
507 the one which linked with the given attribute. */
508 if (iterator->group == &substs)
510 char *iter_name = find_subst_iter_by_attr (attr);
511 if (strcmp (iter_name, iterator->name) != 0)
512 continue;
514 /* Find the attribute value associated with the current
515 iterator value. */
516 for (v = m->values; v; v = v->next)
517 if (v->number == iterator->current_value->number)
519 if (res && strcmp (v->string, res->string) != 0)
521 error_at (loc, "ambiguous attribute '%s'; could be"
522 " '%s' (via '%s:%s') or '%s' (via '%s:%s')",
523 attr, res->string, prev->name, attr,
524 v->string, iterator->name, attr);
525 return v;
527 if (iterator_out)
528 *iterator_out = iterator;
529 prev = iterator;
530 res = v;
534 return res;
537 /* Apply the current iterator values to STRING. Return the new string
538 if any changes were needed, otherwise return STRING itself. */
540 const char *
541 md_reader::apply_iterator_to_string (const char *string)
543 char *base, *copy, *p, *start, *end;
544 struct map_value *v;
546 if (string == 0 || string[0] == 0)
547 return string;
549 file_location loc = get_md_ptr_loc (string)->loc;
550 base = p = copy = ASTRDUP (string);
551 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
553 p = start + 1;
555 *end = 0;
556 v = map_attr_string (loc, p);
557 *end = '>';
558 if (v == 0)
559 continue;
561 /* Add everything between the last copied byte and the '<',
562 then add in the attribute value. */
563 obstack_grow (&m_string_obstack, base, start - base);
564 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
565 base = end + 1;
567 if (base != copy)
569 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
570 copy = XOBFINISH (&m_string_obstack, char *);
571 copy_md_ptr_loc (copy, string);
572 return copy;
574 return string;
577 /* Return a deep copy of X, substituting the current iterator
578 values into any strings. */
581 md_reader::copy_rtx_for_iterators (rtx original)
583 const char *format_ptr, *p;
584 int i, j;
585 rtx x;
587 if (original == 0)
588 return original;
590 /* Create a shallow copy of ORIGINAL. */
591 x = rtx_alloc (GET_CODE (original));
592 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
594 /* Change each string and recursively change each rtx. */
595 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
596 for (i = 0; format_ptr[i] != 0; i++)
597 switch (format_ptr[i])
599 case 'T':
600 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
601 XTMPL (x, i) = p;
602 break;
604 case 'S':
605 case 's':
606 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
607 XSTR (x, i) = p;
608 break;
610 case 'e':
611 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
612 break;
614 case 'V':
615 case 'E':
616 if (XVEC (original, i))
618 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
619 for (j = 0; j < XVECLEN (x, i); j++)
620 XVECEXP (x, i, j)
621 = copy_rtx_for_iterators (XVECEXP (original, i, j));
623 break;
625 default:
626 break;
628 return x;
631 #ifdef GENERATOR_FILE
633 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
634 has the form "&& ..." (as used in define_insn_and_splits), assume that
635 EXTRA is already satisfied. Empty strings are treated like "true". */
637 static const char *
638 add_condition_to_string (const char *original, const char *extra)
640 if (original != 0 && original[0] == '&' && original[1] == '&')
641 return original;
642 return rtx_reader_ptr->join_c_conditions (original, extra);
645 /* Like add_condition, but applied to all conditions in rtx X. */
647 static void
648 add_condition_to_rtx (rtx x, const char *extra)
650 switch (GET_CODE (x))
652 case DEFINE_INSN:
653 case DEFINE_EXPAND:
654 case DEFINE_SUBST:
655 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
656 break;
658 case DEFINE_SPLIT:
659 case DEFINE_PEEPHOLE:
660 case DEFINE_PEEPHOLE2:
661 case DEFINE_COND_EXEC:
662 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
663 break;
665 case DEFINE_INSN_AND_SPLIT:
666 case DEFINE_INSN_AND_REWRITE:
667 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
668 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
669 break;
671 default:
672 break;
676 /* Apply the current iterator values to all attribute_uses. */
678 static void
679 apply_attribute_uses (void)
681 struct map_value *v;
682 attribute_use *ause;
683 unsigned int i;
685 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
687 v = map_attr_string (ause->loc, ause->value);
688 if (!v)
689 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
690 ause->group->apply_iterator (ause->x, ause->index,
691 ause->group->find_builtin (v->string));
695 /* A htab_traverse callback for iterators. Add all used iterators
696 to current_iterators. */
698 static int
699 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
701 struct mapping *iterator;
703 iterator = (struct mapping *) *slot;
704 if (iterator->current_value)
705 current_iterators.safe_push (iterator);
706 return 1;
709 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
710 be many instances of two overloaded_names having the same name but
711 different arguments, so hashing on the name should be good enough in
712 practice. */
714 static hashval_t
715 overloaded_name_hash (const void *uncast_oname)
717 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
718 return htab_hash_string (oname->name);
721 /* Return true if two overloaded_names are similar enough to share
722 the same generated functions. */
724 static int
725 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
727 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
728 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
729 if (strcmp (oname1->name, oname2->name) != 0
730 || oname1->arg_types.length () != oname2->arg_types.length ())
731 return 0;
733 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
734 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
735 return 0;
737 return 1;
740 /* Return true if X has an instruction name in XSTR (X, 0). */
742 static bool
743 named_rtx_p (rtx x)
745 switch (GET_CODE (x))
747 case DEFINE_EXPAND:
748 case DEFINE_INSN:
749 case DEFINE_INSN_AND_SPLIT:
750 case DEFINE_INSN_AND_REWRITE:
751 return true;
753 default:
754 return false;
758 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
759 If so, return the associated overloaded_name and add the iterator for
760 each argument to ITERATORS. Return null otherwise. */
762 overloaded_name *
763 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
765 /* Check for the leading '@'. */
766 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
767 return NULL;
769 /* Remove the '@', so that no other code needs to worry about it. */
770 const char *name = XSTR (original, 0);
771 file_location loc = get_md_ptr_loc (name)->loc;
772 copy_md_ptr_loc (name + 1, name);
773 name += 1;
774 XSTR (original, 0) = name;
776 /* Build a copy of the name without the '<...>' attribute strings.
777 Add the iterator associated with each such attribute string to ITERATORS
778 and add an associated argument to TMP_ONAME. */
779 char *copy = ASTRDUP (name);
780 char *base = copy, *start, *end;
781 overloaded_name tmp_oname;
782 tmp_oname.arg_types.create (current_iterators.length ());
783 bool pending_underscore_p = false;
784 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
786 *end = 0;
787 mapping *iterator;
788 if (!map_attr_string (loc, start + 1, &iterator))
789 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
790 *end = '>';
792 /* Remove a trailing underscore, so that we don't end a name
793 with "_" or turn "_<...>_" into "__". */
794 if (start != base && start[-1] == '_')
796 start -= 1;
797 pending_underscore_p = true;
800 /* Add the text between either the last '>' or the start of
801 the string and this '<'. */
802 obstack_grow (&m_string_obstack, base, start - base);
803 base = end + 1;
805 /* If there's a character we need to keep after the '>', check
806 whether we should prefix it with a previously-dropped '_'. */
807 if (base[0] != 0 && base[0] != '<')
809 if (pending_underscore_p && base[0] != '_')
810 obstack_1grow (&m_string_obstack, '_');
811 pending_underscore_p = false;
814 /* Record an argument for ITERATOR. */
815 iterators->safe_push (iterator);
816 tmp_oname.arg_types.safe_push (iterator->group->type);
818 if (base == copy)
819 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
821 size_t length = obstack_object_size (&m_string_obstack);
822 if (length == 0)
823 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
825 /* Get the completed name. */
826 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
827 char *new_name = XOBFINISH (&m_string_obstack, char *);
828 tmp_oname.name = new_name;
830 if (!m_overloads_htab)
831 m_overloads_htab = htab_create (31, overloaded_name_hash,
832 overloaded_name_eq_p, NULL);
834 /* See whether another pattern had the same overload name and list
835 of argument types. Create a new permanent one if not. */
836 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
837 overloaded_name *oname = (overloaded_name *) *slot;
838 if (!oname)
840 *slot = oname = new overloaded_name;
841 oname->name = tmp_oname.name;
842 oname->arg_types = tmp_oname.arg_types;
843 oname->next = NULL;
844 oname->first_instance = NULL;
845 oname->next_instance_ptr = &oname->first_instance;
847 *m_next_overload_ptr = oname;
848 m_next_overload_ptr = &oname->next;
850 else
852 obstack_free (&m_string_obstack, new_name);
853 tmp_oname.arg_types.release ();
856 return oname;
859 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
860 gives the iterator associated with argument I of ONAME. */
862 static void
863 add_overload_instance (overloaded_name *oname, const vec<mapping *> &iterators, rtx x)
865 /* Create the instance. */
866 overloaded_instance *instance = new overloaded_instance;
867 instance->next = NULL;
868 instance->arg_values.create (oname->arg_types.length ());
869 for (unsigned int i = 0; i < iterators.length (); ++i)
871 int value = iterators[i]->current_value->number;
872 const char *name = iterators[i]->group->get_c_token (value);
873 instance->arg_values.quick_push (name);
875 instance->name = XSTR (x, 0);
876 instance->insn = x;
878 /* Chain it onto the end of ONAME's list. */
879 *oname->next_instance_ptr = instance;
880 oname->next_instance_ptr = &instance->next;
883 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
884 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
886 static void
887 apply_iterators (rtx original, vec<rtx> *queue)
889 unsigned int i;
890 const char *condition;
891 iterator_use *iuse;
892 struct mapping *iterator;
893 struct map_value *v;
894 rtx x;
896 if (iterator_uses.is_empty ())
898 /* Raise an error if any attributes were used. */
899 apply_attribute_uses ();
901 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
902 fatal_with_file_and_line ("'@' used without iterators");
904 queue->safe_push (original);
905 return;
908 /* Clear out the iterators from the previous run. */
909 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
910 iterator->current_value = NULL;
911 current_iterators.truncate (0);
913 /* Mark the iterators that we need this time. */
914 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
915 iuse->iterator->current_value = iuse->iterator->values;
917 /* Get the list of iterators that are in use, preserving the
918 definition order within each group. */
919 htab_traverse (modes.iterators, add_current_iterators, NULL);
920 htab_traverse (codes.iterators, add_current_iterators, NULL);
921 htab_traverse (ints.iterators, add_current_iterators, NULL);
922 htab_traverse (substs.iterators, add_current_iterators, NULL);
923 gcc_assert (!current_iterators.is_empty ());
925 /* Check whether this is a '@' overloaded pattern. */
926 auto_vec<mapping *, 16> iterators;
927 overloaded_name *oname
928 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
930 for (;;)
932 /* Apply the current iterator values. Accumulate a condition to
933 say when the resulting rtx can be used. */
934 condition = "";
935 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
937 if (iuse->iterator->group == &substs)
938 continue;
939 v = iuse->iterator->current_value;
940 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
941 v->number);
942 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
944 apply_attribute_uses ();
945 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
946 add_condition_to_rtx (x, condition);
948 /* We apply subst iterator after RTL-template is copied, as during
949 subst-iterator processing, we could add an attribute to the
950 RTL-template, and we don't want to do it in the original one. */
951 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
953 v = iuse->iterator->current_value;
954 if (iuse->iterator->group == &substs)
956 iuse->x = x;
957 iuse->index = 0;
958 current_iterator_name = iuse->iterator->name;
959 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
960 v->number);
964 if (oname)
965 add_overload_instance (oname, iterators, x);
967 /* Add the new rtx to the end of the queue. */
968 queue->safe_push (x);
970 /* Lexicographically increment the iterator value sequence.
971 That is, cycle through iterator values, starting from the right,
972 and stopping when one of them doesn't wrap around. */
973 i = current_iterators.length ();
974 for (;;)
976 if (i == 0)
977 return;
978 i--;
979 iterator = current_iterators[i];
980 iterator->current_value = iterator->current_value->next;
981 if (iterator->current_value)
982 break;
983 iterator->current_value = iterator->values;
987 #endif /* #ifdef GENERATOR_FILE */
989 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
990 of the mapping and GROUP is the group to which it belongs. */
992 static struct mapping *
993 add_mapping (struct iterator_group *group, htab_t table, const char *name)
995 struct mapping *m;
996 void **slot;
998 m = XNEW (struct mapping);
999 m->name = xstrdup (name);
1000 m->group = group;
1001 m->values = 0;
1002 m->current_value = NULL;
1004 slot = htab_find_slot (table, m, INSERT);
1005 if (*slot != 0)
1006 fatal_with_file_and_line ("`%s' already defined", name);
1008 *slot = m;
1009 return m;
1012 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
1013 END_PTR points to the current null terminator for the list; return
1014 a pointer the new null terminator. */
1016 static struct map_value **
1017 add_map_value (struct map_value **end_ptr, int number, const char *string)
1019 struct map_value *value;
1021 value = XNEW (struct map_value);
1022 value->next = 0;
1023 value->number = number;
1024 value->string = string;
1026 *end_ptr = value;
1027 return &value->next;
1030 /* Do one-time initialization of the mode and code attributes. */
1032 static void
1033 initialize_iterators (void)
1035 struct mapping *lower, *upper;
1036 struct map_value **lower_ptr, **upper_ptr;
1037 char *copy, *p;
1038 int i;
1040 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1041 modes.iterators = htab_create (13, leading_string_hash,
1042 leading_string_eq_p, 0);
1043 modes.type = "machine_mode";
1044 modes.find_builtin = find_mode;
1045 modes.apply_iterator = apply_mode_iterator;
1046 modes.get_c_token = get_mode_token;
1048 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1049 codes.iterators = htab_create (13, leading_string_hash,
1050 leading_string_eq_p, 0);
1051 codes.type = "rtx_code";
1052 codes.find_builtin = find_code;
1053 codes.apply_iterator = apply_code_iterator;
1054 codes.get_c_token = get_code_token;
1056 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1057 ints.iterators = htab_create (13, leading_string_hash,
1058 leading_string_eq_p, 0);
1059 ints.type = "int";
1060 ints.find_builtin = find_int;
1061 ints.apply_iterator = apply_int_iterator;
1062 ints.get_c_token = get_int_token;
1063 ints.has_self_attr = true;
1065 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1066 substs.iterators = htab_create (13, leading_string_hash,
1067 leading_string_eq_p, 0);
1068 substs.type = "int";
1069 substs.find_builtin = find_int; /* We don't use it, anyway. */
1070 #ifdef GENERATOR_FILE
1071 substs.apply_iterator = apply_subst_iterator;
1072 #endif
1073 substs.get_c_token = get_int_token;
1075 lower = add_mapping (&modes, modes.attrs, "mode");
1076 upper = add_mapping (&modes, modes.attrs, "MODE");
1077 lower_ptr = &lower->values;
1078 upper_ptr = &upper->values;
1079 for (i = 0; i < MAX_MACHINE_MODE; i++)
1081 copy = xstrdup (GET_MODE_NAME (i));
1082 for (p = copy; *p != 0; p++)
1083 *p = TOLOWER (*p);
1085 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
1086 lower_ptr = add_map_value (lower_ptr, i, copy);
1089 lower = add_mapping (&codes, codes.attrs, "code");
1090 upper = add_mapping (&codes, codes.attrs, "CODE");
1091 lower_ptr = &lower->values;
1092 upper_ptr = &upper->values;
1093 for (i = 0; i < NUM_RTX_CODE; i++)
1095 copy = xstrdup (GET_RTX_NAME (i));
1096 for (p = copy; *p != 0; p++)
1097 *p = TOUPPER (*p);
1099 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
1100 upper_ptr = add_map_value (upper_ptr, i, copy);
1105 #ifdef GENERATOR_FILE
1106 /* Process a define_conditions directive, starting with the optional
1107 space after the "define_conditions". The directive looks like this:
1109 (define_conditions [
1110 (number "string")
1111 (number "string")
1115 It's not intended to appear in machine descriptions. It is
1116 generated by (the program generated by) genconditions.cc, and
1117 slipped in at the beginning of the sequence of MD files read by
1118 most of the other generators. */
1119 void
1120 md_reader::read_conditions ()
1122 int c;
1124 require_char_ws ('[');
1126 while ( (c = read_skip_spaces ()) != ']')
1128 struct md_name name;
1129 char *expr;
1130 int value;
1132 if (c != '(')
1133 fatal_expected_char ('(', c);
1135 read_name (&name);
1136 validate_const_int (name.string);
1137 value = atoi (name.string);
1139 require_char_ws ('"');
1140 expr = read_quoted_string ();
1142 require_char_ws (')');
1144 add_c_test (expr, value);
1147 #endif /* #ifdef GENERATOR_FILE */
1149 static void
1150 validate_const_int (const char *string)
1152 const char *cp;
1153 int valid = 1;
1155 cp = string;
1156 while (*cp && ISSPACE (*cp))
1157 cp++;
1158 if (*cp == '-' || *cp == '+')
1159 cp++;
1160 if (*cp == 0)
1161 valid = 0;
1162 for (; *cp; cp++)
1163 if (! ISDIGIT (*cp))
1165 valid = 0;
1166 break;
1168 if (!valid)
1169 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
1172 static void
1173 validate_const_wide_int (const char *string)
1175 const char *cp;
1176 int valid = 1;
1178 cp = string;
1179 while (*cp && ISSPACE (*cp))
1180 cp++;
1181 /* Skip the leading 0x. */
1182 if (cp[0] == '0' || cp[1] == 'x')
1183 cp += 2;
1184 else
1185 valid = 0;
1186 if (*cp == 0)
1187 valid = 0;
1188 for (; *cp; cp++)
1189 if (! ISXDIGIT (*cp))
1190 valid = 0;
1191 if (!valid)
1192 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
1195 /* Record that X uses iterator ITERATOR. If the use is in an operand
1196 of X, INDEX is the index of that operand, otherwise it is ignored. */
1198 static void
1199 record_iterator_use (struct mapping *iterator, rtx x, unsigned int index)
1201 struct iterator_use iuse = {iterator, x, index};
1202 iterator_uses.safe_push (iuse);
1205 /* Record that X uses attribute VALUE at location LOC, where VALUE must
1206 match a built-in value from group GROUP. If the use is in an operand
1207 of X, INDEX is the index of that operand, otherwise it is ignored. */
1209 static void
1210 record_attribute_use (struct iterator_group *group, file_location loc, rtx x,
1211 unsigned int index, const char *value)
1213 struct attribute_use ause = {group, loc, value, x, index};
1214 attribute_uses.safe_push (ause);
1217 /* Interpret NAME as either a built-in value, iterator or attribute
1218 for group GROUP. X and INDEX are the values to pass to GROUP's
1219 apply_iterator callback. LOC is the location of the use. */
1221 void
1222 md_reader::record_potential_iterator_use (struct iterator_group *group,
1223 file_location loc,
1224 rtx x, unsigned int index,
1225 const char *name)
1227 struct mapping *m;
1228 size_t len;
1230 len = strlen (name);
1231 if (name[0] == '<' && name[len - 1] == '>')
1233 /* Copy the attribute string into permanent storage, without the
1234 angle brackets around it. */
1235 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
1236 record_attribute_use (group, loc, x, index,
1237 XOBFINISH (&m_string_obstack, char *));
1239 else
1241 m = (struct mapping *) htab_find (group->iterators, &name);
1242 if (m != 0)
1243 record_iterator_use (m, x, index);
1244 else
1245 group->apply_iterator (x, index, group->find_builtin (name));
1249 #ifdef GENERATOR_FILE
1251 /* Finish reading a declaration of the form:
1253 (define... <name> [<value1> ... <valuen>])
1255 from the MD file, where each <valuei> is either a bare symbol name or a
1256 "(<name> <string>)" pair. The "(define..." part has already been read.
1258 Represent the declaration as a "mapping" structure; add it to TABLE
1259 (which belongs to GROUP) and return it. */
1261 struct mapping *
1262 md_reader::read_mapping (struct iterator_group *group, htab_t table)
1264 struct md_name name;
1265 struct mapping *m;
1266 struct map_value **end_ptr;
1267 const char *string;
1268 int number, c;
1270 /* Read the mapping name and create a structure for it. */
1271 read_name (&name);
1272 m = add_mapping (group, table, name.string);
1274 require_char_ws ('[');
1276 /* Read each value. */
1277 end_ptr = &m->values;
1278 c = read_skip_spaces ();
1281 if (c != '(')
1283 /* A bare symbol name that is implicitly paired to an
1284 empty string. */
1285 unread_char (c);
1286 read_name (&name);
1287 string = "";
1289 else
1291 /* A "(name string)" pair. */
1292 read_name (&name);
1293 string = read_string (false);
1294 require_char_ws (')');
1296 number = group->find_builtin (name.string);
1297 end_ptr = add_map_value (end_ptr, number, string);
1298 c = read_skip_spaces ();
1300 while (c != ']');
1302 return m;
1305 /* For iterator with name ATTR_NAME generate define_attr with values
1306 'yes' and 'no'. This attribute is used to mark templates to which
1307 define_subst ATTR_NAME should be applied. This attribute is set and
1308 defined implicitly and automatically. */
1309 static void
1310 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
1312 rtx const_str, return_rtx;
1314 return_rtx = rtx_alloc (DEFINE_ATTR);
1315 PUT_CODE (return_rtx, DEFINE_ATTR);
1317 const_str = rtx_alloc (CONST_STRING);
1318 PUT_CODE (const_str, CONST_STRING);
1319 XSTR (const_str, 0) = xstrdup ("no");
1321 XSTR (return_rtx, 0) = xstrdup (attr_name);
1322 XSTR (return_rtx, 1) = xstrdup ("no,yes");
1323 XEXP (return_rtx, 2) = const_str;
1325 queue->safe_push (return_rtx);
1328 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1329 ATTR_OPERANDS and places it to QUEUE. */
1330 static void
1331 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
1333 rtx return_rtx;
1334 int i;
1336 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
1337 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
1339 for (i = 0; i < 4; i++)
1340 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
1342 queue->safe_push (return_rtx);
1345 /* Read define_subst_attribute construction. It has next form:
1346 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1347 Attribute is substituted with value1 when no subst is applied and with
1348 value2 in the opposite case.
1349 Attributes are added to SUBST_ATTRS_TABLE.
1350 In case the iterator is encountered for the first time, it's added to
1351 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1353 static void
1354 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
1355 vec<rtx> *queue)
1357 struct mapping *m;
1358 struct map_value **end_ptr;
1359 const char *attr_operands[4];
1360 int i;
1362 for (i = 0; i < 4; i++)
1363 attr_operands[i] = rtx_reader_ptr->read_string (false);
1365 add_define_subst_attr (attr_operands, queue);
1367 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1369 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1370 if (!m)
1372 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1373 end_ptr = &m->values;
1374 end_ptr = add_map_value (end_ptr, 1, "");
1375 add_map_value (end_ptr, 2, "");
1377 add_define_attr_for_define_subst (attr_operands[1], queue);
1380 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1381 end_ptr = &m->values;
1382 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1383 add_map_value (end_ptr, 2, attr_operands[3]);
1386 /* Check newly-created code iterator ITERATOR to see whether every code has the
1387 same format. */
1389 static void
1390 check_code_iterator (struct mapping *iterator)
1392 struct map_value *v;
1393 enum rtx_code bellwether;
1395 bellwether = (enum rtx_code) iterator->values->number;
1396 for (v = iterator->values->next; v != 0; v = v->next)
1397 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1398 fatal_with_file_and_line ("code iterator `%s' combines "
1399 "`%s' and `%s', which have different "
1400 "rtx formats", iterator->name,
1401 GET_RTX_NAME (bellwether),
1402 GET_RTX_NAME (v->number));
1405 /* Check that all values of attribute ATTR are rtx codes that have a
1406 consistent format. Return a representative code. */
1408 static rtx_code
1409 check_code_attribute (mapping *attr)
1411 rtx_code bellwether = UNKNOWN;
1412 for (map_value *v = attr->values; v != 0; v = v->next)
1414 rtx_code code = maybe_find_code (v->string);
1415 if (code == UNKNOWN)
1416 fatal_with_file_and_line ("code attribute `%s' contains "
1417 "unrecognized rtx code `%s'",
1418 attr->name, v->string);
1419 if (bellwether == UNKNOWN)
1420 bellwether = code;
1421 else if (strcmp (GET_RTX_FORMAT (bellwether),
1422 GET_RTX_FORMAT (code)) != 0)
1423 fatal_with_file_and_line ("code attribute `%s' combines "
1424 "`%s' and `%s', which have different "
1425 "rtx formats", attr->name,
1426 GET_RTX_NAME (bellwether),
1427 GET_RTX_NAME (code));
1429 return bellwether;
1432 /* Read an rtx-related declaration from the MD file, given that it
1433 starts with directive name RTX_NAME. Return true if it expands to
1434 one or more rtxes (as defined by rtx.def). When returning true,
1435 store the list of rtxes as an EXPR_LIST in *X. */
1437 bool
1438 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1440 /* Handle various rtx-related declarations that aren't themselves
1441 encoded as rtxes. */
1442 if (strcmp (rtx_name, "define_conditions") == 0)
1444 read_conditions ();
1445 return false;
1447 if (strcmp (rtx_name, "define_mode_attr") == 0)
1449 read_mapping (&modes, modes.attrs);
1450 return false;
1452 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1454 read_mapping (&modes, modes.iterators);
1455 return false;
1457 if (strcmp (rtx_name, "define_code_attr") == 0)
1459 read_mapping (&codes, codes.attrs);
1460 return false;
1462 if (strcmp (rtx_name, "define_code_iterator") == 0)
1464 check_code_iterator (read_mapping (&codes, codes.iterators));
1465 return false;
1467 if (strcmp (rtx_name, "define_int_attr") == 0)
1469 read_mapping (&ints, ints.attrs);
1470 return false;
1472 if (strcmp (rtx_name, "define_int_iterator") == 0)
1474 read_mapping (&ints, ints.iterators);
1475 return false;
1477 if (strcmp (rtx_name, "define_subst_attr") == 0)
1479 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1481 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1482 TRUE to process it. */
1483 return true;
1486 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1487 iterator_uses.truncate (0);
1488 attribute_uses.truncate (0);
1490 return true;
1493 #endif /* #ifdef GENERATOR_FILE */
1495 /* Do one-time initialization. */
1497 static void
1498 one_time_initialization (void)
1500 static bool initialized = false;
1502 if (!initialized)
1504 initialize_iterators ();
1505 initialized = true;
1509 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1510 consuming the terminator character if CONSUME_TERMINATOR is true.
1511 Return all characters before the terminator as an allocated buffer. */
1513 char *
1514 rtx_reader::read_until (const char *terminator_chars, bool consume_terminator)
1516 int ch = read_skip_spaces ();
1517 unread_char (ch);
1518 auto_vec<char> buf;
1519 while (1)
1521 ch = read_char ();
1522 if (strchr (terminator_chars, ch))
1524 if (!consume_terminator)
1525 unread_char (ch);
1526 break;
1528 buf.safe_push (ch);
1530 buf.safe_push ('\0');
1531 return xstrdup (buf.address ());
1534 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1536 static void
1537 read_flags (rtx return_rtx)
1539 while (1)
1541 int ch = read_char ();
1542 if (ch != '/')
1544 unread_char (ch);
1545 break;
1548 int flag_char = read_char ();
1549 switch (flag_char)
1551 case 's':
1552 RTX_FLAG (return_rtx, in_struct) = 1;
1553 break;
1554 case 'v':
1555 RTX_FLAG (return_rtx, volatil) = 1;
1556 break;
1557 case 'u':
1558 RTX_FLAG (return_rtx, unchanging) = 1;
1559 break;
1560 case 'f':
1561 RTX_FLAG (return_rtx, frame_related) = 1;
1562 break;
1563 case 'j':
1564 RTX_FLAG (return_rtx, jump) = 1;
1565 break;
1566 case 'c':
1567 RTX_FLAG (return_rtx, call) = 1;
1568 break;
1569 case 'i':
1570 RTX_FLAG (return_rtx, return_val) = 1;
1571 break;
1572 default:
1573 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char);
1578 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1579 or fail if STRING isn't recognized. */
1581 static int
1582 parse_reg_note_name (const char *string)
1584 for (int i = 0; i < REG_NOTE_MAX; i++)
1585 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1586 return i;
1587 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1590 /* Allocate an rtx for code NAME. If NAME is a code iterator or code
1591 attribute, record its use for later and use one of its possible
1592 values as an interim rtx code. */
1595 rtx_reader::rtx_alloc_for_name (const char *name)
1597 #ifdef GENERATOR_FILE
1598 size_t len = strlen (name);
1599 if (name[0] == '<' && name[len - 1] == '>')
1601 /* Copy the attribute string into permanent storage, without the
1602 angle brackets around it. */
1603 obstack *strings = get_string_obstack ();
1604 obstack_grow0 (strings, name + 1, len - 2);
1605 char *deferred_name = XOBFINISH (strings, char *);
1607 /* Find the name of the attribute. */
1608 const char *attr = strchr (deferred_name, ':');
1609 if (!attr)
1610 attr = deferred_name;
1612 /* Find the attribute itself. */
1613 mapping *m = (mapping *) htab_find (codes.attrs, &attr);
1614 if (!m)
1615 fatal_with_file_and_line ("unknown code attribute `%s'", attr);
1617 /* Pick the first possible code for now, and record the attribute
1618 use for later. */
1619 rtx x = rtx_alloc (check_code_attribute (m));
1620 record_attribute_use (&codes, get_current_location (),
1621 x, 0, deferred_name);
1622 return x;
1625 mapping *iterator = (mapping *) htab_find (codes.iterators, &name);
1626 if (iterator != 0)
1628 /* Pick the first possible code for now, and record the iterator
1629 use for later. */
1630 rtx x = rtx_alloc (rtx_code (iterator->values->number));
1631 record_iterator_use (iterator, x, 0);
1632 return x;
1634 #endif
1636 return rtx_alloc (rtx_code (codes.find_builtin (name)));
1639 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1640 either an rtx code or a code iterator. Parse the rest of the rtx and
1641 return it. */
1644 rtx_reader::read_rtx_code (const char *code_name)
1646 RTX_CODE code;
1647 const char *format_ptr;
1648 struct md_name name;
1649 rtx return_rtx;
1650 int c;
1651 long reuse_id = -1;
1653 /* Linked list structure for making RTXs: */
1654 struct rtx_list
1656 struct rtx_list *next;
1657 rtx value; /* Value of this node. */
1660 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1661 if (ISDIGIT (code_name[0]))
1663 reuse_id = atoi (code_name);
1664 while (char ch = *code_name++)
1665 if (ch == '|')
1666 break;
1669 /* Handle "reuse_rtx". */
1670 if (strcmp (code_name, "reuse_rtx") == 0)
1672 read_name (&name);
1673 unsigned idx = atoi (name.string);
1674 /* Look it up by ID. */
1675 gcc_assert (idx < m_reuse_rtx_by_id.length ());
1676 return_rtx = m_reuse_rtx_by_id[idx];
1677 return return_rtx;
1680 /* Handle "const_double_zero". */
1681 if (strcmp (code_name, "const_double_zero") == 0)
1683 code = CONST_DOUBLE;
1684 return_rtx = rtx_alloc (code);
1685 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1686 PUT_CODE (return_rtx, code);
1687 c = read_skip_spaces ();
1688 if (c == ':')
1690 file_location loc = read_name (&name);
1691 record_potential_iterator_use (&modes, loc, return_rtx, 0,
1692 name.string);
1694 else
1695 unread_char (c);
1696 return return_rtx;
1699 /* If we end up with an insn expression then we free this space below. */
1700 return_rtx = rtx_alloc_for_name (code_name);
1701 code = GET_CODE (return_rtx);
1702 format_ptr = GET_RTX_FORMAT (code);
1703 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1704 PUT_CODE (return_rtx, code);
1706 if (reuse_id != -1)
1708 /* Store away for later reuse. */
1709 m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1, true);
1710 m_reuse_rtx_by_id[reuse_id] = return_rtx;
1713 /* Check for flags. */
1714 read_flags (return_rtx);
1716 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1717 if ((GET_CODE (return_rtx) == EXPR_LIST
1718 || GET_CODE (return_rtx) == INSN_LIST
1719 || GET_CODE (return_rtx) == INT_LIST)
1720 && !m_in_call_function_usage)
1722 char ch = read_char ();
1723 if (ch == ':')
1725 read_name (&name);
1726 PUT_MODE_RAW (return_rtx,
1727 (machine_mode)parse_reg_note_name (name.string));
1729 else
1730 unread_char (ch);
1733 /* If what follows is `: mode ', read it and
1734 store the mode in the rtx. */
1736 c = read_skip_spaces ();
1737 if (c == ':')
1739 file_location loc = read_name (&name);
1740 record_potential_iterator_use (&modes, loc, return_rtx, 0, name.string);
1742 else
1743 unread_char (c);
1745 if (INSN_CHAIN_CODE_P (code))
1747 read_name (&name);
1748 INSN_UID (return_rtx) = atoi (name.string);
1751 /* Use the format_ptr to parse the various operands of this rtx. */
1752 for (int idx = 0; format_ptr[idx] != 0; idx++)
1753 return_rtx = read_rtx_operand (return_rtx, idx);
1755 /* Handle any additional information that after the regular fields
1756 (e.g. when parsing function dumps). */
1757 handle_any_trailing_information (return_rtx);
1759 if (CONST_WIDE_INT_P (return_rtx))
1761 read_name (&name);
1762 validate_const_wide_int (name.string);
1764 const char *s = name.string;
1765 int len;
1766 int index = 0;
1767 int gs = HOST_BITS_PER_WIDE_INT/4;
1768 int pos;
1769 char * buf = XALLOCAVEC (char, gs + 1);
1770 unsigned HOST_WIDE_INT wi;
1771 int wlen;
1773 /* Skip the leading spaces. */
1774 while (*s && ISSPACE (*s))
1775 s++;
1777 /* Skip the leading 0x. */
1778 gcc_assert (s[0] == '0');
1779 gcc_assert (s[1] == 'x');
1780 s += 2;
1782 len = strlen (s);
1783 pos = len - gs;
1784 wlen = (len + gs - 1) / gs; /* Number of words needed */
1786 return_rtx = const_wide_int_alloc (wlen);
1788 while (pos > 0)
1790 #if HOST_BITS_PER_WIDE_INT == 64
1791 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1792 #else
1793 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1794 #endif
1795 CWI_ELT (return_rtx, index++) = wi;
1796 pos -= gs;
1798 strncpy (buf, s, gs - pos);
1799 buf [gs - pos] = 0;
1800 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1801 CWI_ELT (return_rtx, index++) = wi;
1802 /* TODO: After reading, do we want to canonicalize with:
1803 value = lookup_const_wide_int (value); ? */
1807 c = read_skip_spaces ();
1808 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1809 arbitrary number of arguments for them. */
1810 if (c == '('
1811 && (GET_CODE (return_rtx) == AND
1812 || GET_CODE (return_rtx) == IOR))
1813 return read_rtx_variadic (return_rtx);
1815 unread_char (c);
1816 return return_rtx;
1819 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1820 based on the corresponding format character within GET_RTX_FORMAT
1821 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1822 This is a virtual function, so that function_reader can override
1823 some parsing, and potentially return a different rtx. */
1826 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1828 RTX_CODE code = GET_CODE (return_rtx);
1829 const char *format_ptr = GET_RTX_FORMAT (code);
1830 int c;
1831 struct md_name name;
1833 switch (format_ptr[idx])
1835 /* 0 means a field for internal use only.
1836 Don't expect it to be present in the input. */
1837 case '0':
1838 if (code == REG)
1839 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1840 break;
1842 case 'e':
1843 XEXP (return_rtx, idx) = read_nested_rtx ();
1844 break;
1846 case 'u':
1847 XEXP (return_rtx, idx) = read_nested_rtx ();
1848 break;
1850 case 'V':
1851 /* 'V' is an optional vector: if a closeparen follows,
1852 just store NULL for this element. */
1853 c = read_skip_spaces ();
1854 unread_char (c);
1855 if (c == ')')
1857 XVEC (return_rtx, idx) = 0;
1858 break;
1860 /* Now process the vector. */
1861 /* FALLTHRU */
1863 case 'E':
1865 /* Obstack to store scratch vector in. */
1866 struct obstack vector_stack;
1867 int list_counter = 0;
1868 rtvec return_vec = NULL_RTVEC;
1869 rtx saved_rtx = NULL_RTX;
1871 require_char_ws ('[');
1873 /* Add expressions to a list, while keeping a count. */
1874 obstack_init (&vector_stack);
1875 while ((c = read_skip_spaces ()) && c != ']')
1877 if (c == EOF)
1878 fatal_expected_char (']', c);
1879 unread_char (c);
1881 rtx value;
1882 int repeat_count = 1;
1883 if (c == 'r')
1885 /* Process "repeated xN" directive. */
1886 read_name (&name);
1887 if (strcmp (name.string, "repeated"))
1888 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1889 name.string);
1890 read_name (&name);
1891 if (!sscanf (name.string, "x%d", &repeat_count))
1892 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1893 name.string);
1895 /* We already saw one of the instances. */
1896 repeat_count--;
1897 value = saved_rtx;
1899 else
1900 value = read_nested_rtx ();
1902 for (; repeat_count > 0; repeat_count--)
1904 list_counter++;
1905 obstack_ptr_grow (&vector_stack, value);
1907 saved_rtx = value;
1909 if (list_counter > 0)
1911 return_vec = rtvec_alloc (list_counter);
1912 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1913 list_counter * sizeof (rtx));
1915 else if (format_ptr[idx] == 'E')
1916 fatal_with_file_and_line ("vector must have at least one element");
1917 XVEC (return_rtx, idx) = return_vec;
1918 obstack_free (&vector_stack, NULL);
1919 /* close bracket gotten */
1921 break;
1923 case 'S':
1924 case 'T':
1925 case 's':
1927 char *stringbuf;
1928 int star_if_braced;
1930 c = read_skip_spaces ();
1931 unread_char (c);
1932 if (c == ')')
1934 /* 'S' fields are optional and should be NULL if no string
1935 was given. Also allow normal 's' and 'T' strings to be
1936 omitted, treating them in the same way as empty strings. */
1937 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1938 break;
1941 /* The output template slot of a DEFINE_INSN, DEFINE_INSN_AND_SPLIT,
1942 DEFINE_INSN_AND_REWRITE or DEFINE_PEEPHOLE automatically
1943 gets a star inserted as its first character, if it is
1944 written with a brace block instead of a string constant. */
1945 star_if_braced = (format_ptr[idx] == 'T');
1947 stringbuf = read_string (star_if_braced);
1948 if (!stringbuf)
1949 break;
1951 #ifdef GENERATOR_FILE
1952 /* For insn patterns, we want to provide a default name
1953 based on the file and line, like "*foo.md:12", if the
1954 given name is blank. These are only for define_insn and
1955 define_insn_and_split, to aid debugging. */
1956 if (*stringbuf == '\0'
1957 && idx == 0
1958 && (GET_CODE (return_rtx) == DEFINE_INSN
1959 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT
1960 || GET_CODE (return_rtx) == DEFINE_INSN_AND_REWRITE))
1962 const char *old_stringbuf = stringbuf;
1963 struct obstack *string_obstack = get_string_obstack ();
1964 char line_name[20];
1965 const char *read_md_filename = get_filename ();
1966 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1967 const char *slash;
1968 for (slash = fn; *slash; slash ++)
1969 if (*slash == '/' || *slash == '\\' || *slash == ':')
1970 fn = slash + 1;
1971 obstack_1grow (string_obstack, '*');
1972 obstack_grow (string_obstack, fn, strlen (fn));
1973 sprintf (line_name, ":%d", get_lineno ());
1974 obstack_grow (string_obstack, line_name, strlen (line_name)+1);
1975 stringbuf = XOBFINISH (string_obstack, char *);
1976 copy_md_ptr_loc (stringbuf, old_stringbuf);
1979 /* Find attr-names in the string. */
1980 char *str;
1981 char *start, *end, *ptr;
1982 char tmpstr[256];
1983 ptr = &tmpstr[0];
1984 end = stringbuf;
1985 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1987 if ((end - start - 1 > 0)
1988 && (end - start - 1 < (int)sizeof (tmpstr)))
1990 strncpy (tmpstr, start+1, end-start-1);
1991 tmpstr[end-start-1] = 0;
1992 end++;
1994 else
1995 break;
1996 struct mapping *m
1997 = (struct mapping *) htab_find (substs.attrs, &ptr);
1998 if (m != 0)
2000 /* Here we should find linked subst-iter. */
2001 str = find_subst_iter_by_attr (ptr);
2002 if (str)
2003 m = (struct mapping *) htab_find (substs.iterators, &str);
2004 else
2005 m = 0;
2007 if (m != 0)
2008 record_iterator_use (m, return_rtx, 0);
2010 #endif /* #ifdef GENERATOR_FILE */
2012 const char *string_ptr = finalize_string (stringbuf);
2014 if (star_if_braced)
2015 XTMPL (return_rtx, idx) = string_ptr;
2016 else
2017 XSTR (return_rtx, idx) = string_ptr;
2019 break;
2021 case 'i':
2022 case 'n':
2023 case 'w':
2024 case 'p':
2026 /* Can be an iterator or an integer constant. */
2027 file_location loc = read_name (&name);
2028 record_potential_iterator_use (&ints, loc, return_rtx, idx,
2029 name.string);
2030 break;
2033 case 'r':
2034 read_name (&name);
2035 validate_const_int (name.string);
2036 set_regno_raw (return_rtx, atoi (name.string), 1);
2037 REG_ATTRS (return_rtx) = NULL;
2038 break;
2040 default:
2041 gcc_unreachable ();
2044 return return_rtx;
2047 /* Read a nested rtx construct from the MD file and return it. */
2050 rtx_reader::read_nested_rtx ()
2052 struct md_name name;
2053 rtx return_rtx;
2055 /* In compact dumps, trailing "(nil)" values can be omitted.
2056 Handle such dumps. */
2057 if (peek_char () == ')')
2058 return NULL_RTX;
2060 require_char_ws ('(');
2062 read_name (&name);
2063 if (strcmp (name.string, "nil") == 0)
2064 return_rtx = NULL;
2065 else
2066 return_rtx = read_rtx_code (name.string);
2068 require_char_ws (')');
2070 return_rtx = postprocess (return_rtx);
2072 return return_rtx;
2075 /* Mutually recursive subroutine of read_rtx which reads
2076 (thing x1 x2 x3 ...) and produces RTL as if
2077 (thing x1 (thing x2 (thing x3 ...))) had been written.
2078 When called, FORM is (thing x1 x2), and the file position
2079 is just past the leading parenthesis of x3. Only works
2080 for THINGs which are dyadic expressions, e.g. AND, IOR. */
2082 rtx_reader::read_rtx_variadic (rtx form)
2084 char c = '(';
2085 rtx p = form, q;
2089 unread_char (c);
2091 q = rtx_alloc (GET_CODE (p));
2092 PUT_MODE (q, GET_MODE (p));
2094 XEXP (q, 0) = XEXP (p, 1);
2095 XEXP (q, 1) = read_nested_rtx ();
2097 XEXP (p, 1) = q;
2098 p = q;
2099 c = read_skip_spaces ();
2101 while (c == '(');
2102 unread_char (c);
2103 return form;
2106 /* Constructor for class rtx_reader. */
2108 rtx_reader::rtx_reader (bool compact)
2109 : md_reader (compact),
2110 m_in_call_function_usage (false)
2112 /* Set the global singleton pointer. */
2113 rtx_reader_ptr = this;
2115 one_time_initialization ();
2118 /* Destructor for class rtx_reader. */
2120 rtx_reader::~rtx_reader ()
2122 /* Clear the global singleton pointer. */
2123 rtx_reader_ptr = NULL;