xtensa: constantsynth: Add new 3-insns synthesis pattern
[official-gcc.git] / gcc / read-rtl.cc
blob62c7895af606592bd6b0a6ded74083d98baa55e3
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 struct md_constant tmp_def;
288 tmp_def.name = const_cast<char *> (name);
289 auto htab = rtx_reader_ptr->get_md_constants ();
290 if (auto def = (struct md_constant *) htab_find (htab, &tmp_def))
291 name = def->value;
293 validate_const_int (name);
294 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
295 tmp = atoi (name);
296 #else
297 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
298 tmp = atol (name);
299 #else
300 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
301 But prefer not to use our hand-rolled function above either. */
302 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
303 tmp = atoll (name);
304 #else
305 tmp = atoq (name);
306 #endif
307 #endif
308 #endif
309 return tmp;
312 static void
313 apply_int_iterator (rtx x, unsigned int index, HOST_WIDE_INT value)
315 RTX_CODE code = GET_CODE (x);
316 const char *format_ptr = GET_RTX_FORMAT (code);
318 switch (format_ptr[index])
320 case 'i':
321 case 'n':
322 XINT (x, index) = value;
323 break;
324 case 'w':
325 XWINT (x, index) = value;
326 break;
327 case 'p':
328 gcc_assert (code == SUBREG);
329 SUBREG_BYTE (x) = value;
330 break;
331 default:
332 gcc_unreachable ();
336 static const char *
337 get_int_token (int value)
339 char buffer[HOST_BITS_PER_INT + 1];
340 sprintf (buffer, "%d", value);
341 return xstrdup (buffer);
344 #ifdef GENERATOR_FILE
346 /* This routine adds attribute or does nothing depending on VALUE. When
347 VALUE is 1, it does nothing - the first duplicate of original
348 template is kept untouched when it's subjected to a define_subst.
349 When VALUE isn't 1, the routine modifies RTL-template RT, adding
350 attribute, named exactly as define_subst, which later will be
351 applied. If such attribute has already been added, then no the
352 routine has no effect. */
353 static void
354 apply_subst_iterator (rtx rt, unsigned int, HOST_WIDE_INT value)
356 rtx new_attr;
357 rtvec attrs_vec, new_attrs_vec;
358 int i;
359 /* define_split has no attributes. */
360 if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT)
361 return;
362 gcc_assert (GET_CODE (rt) == DEFINE_INSN
363 || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT
364 || GET_CODE (rt) == DEFINE_INSN_AND_REWRITE
365 || GET_CODE (rt) == DEFINE_EXPAND);
367 int attrs = (GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7
368 : GET_CODE (rt) == DEFINE_INSN_AND_REWRITE ? 6 : 4);
369 attrs_vec = XVEC (rt, attrs);
371 /* If we've already added attribute 'current_iterator_name', then we
372 have nothing to do now. */
373 if (attrs_vec)
375 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
377 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
378 return;
382 /* Add attribute with subst name - it serves as a mark for
383 define_subst which later would be applied to this pattern. */
384 new_attr = rtx_alloc (SET_ATTR);
385 PUT_CODE (new_attr, SET_ATTR);
386 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
387 XSTR (new_attr, 1) = xstrdup ("yes");
389 if (!attrs_vec)
391 new_attrs_vec = rtvec_alloc (1);
392 new_attrs_vec->elem[0] = new_attr;
394 else
396 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
397 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
398 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
399 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
401 XVEC (rt, attrs) = new_attrs_vec;
404 /* Map subst-attribute ATTR to subst iterator ITER. */
406 static void
407 bind_subst_iter_and_attr (const char *iter, const char *attr)
409 struct subst_attr_to_iter_mapping *value;
410 void **slot;
411 if (!subst_attr_to_iter_map)
412 subst_attr_to_iter_map =
413 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
414 value = XNEW (struct subst_attr_to_iter_mapping);
415 value->attr_name = xstrdup (attr);
416 value->iter_name = xstrdup (iter);
417 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
418 *slot = value;
421 #endif /* #ifdef GENERATOR_FILE */
423 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
425 static char*
426 find_subst_iter_by_attr (const char *attr)
428 char *iter_name = NULL;
429 struct subst_attr_to_iter_mapping *value;
430 value = (struct subst_attr_to_iter_mapping*)
431 htab_find (subst_attr_to_iter_map, &attr);
432 if (value)
433 iter_name = value->iter_name;
434 return iter_name;
437 /* Map attribute string P to its current value. Return null if the attribute
438 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
439 there. Report any errors against location LOC. */
441 static struct map_value *
442 map_attr_string (file_location loc, const char *p, mapping **iterator_out = 0)
444 const char *attr;
445 struct mapping *iterator;
446 unsigned int i;
447 struct mapping *m;
448 struct map_value *v;
449 int iterator_name_len;
450 struct map_value *res = NULL;
451 struct mapping *prev = NULL;
453 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
454 attribute name. */
455 attr = strchr (p, ':');
456 if (attr == 0)
458 iterator_name_len = -1;
459 attr = p;
461 else
463 iterator_name_len = attr - p;
464 attr++;
467 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
469 /* If an iterator name was specified, check that it matches. */
470 if (iterator_name_len >= 0
471 && (strncmp (p, iterator->name, iterator_name_len) != 0
472 || iterator->name[iterator_name_len] != 0))
473 continue;
475 /* Find the attribute specification. */
476 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
477 if (m)
479 /* In contrast to code/mode/int iterators, attributes of subst
480 iterators are linked to one specific subst-iterator. So, if
481 we are dealing with subst-iterator, we should check if it's
482 the one which linked with the given attribute. */
483 if (iterator->group == &substs)
485 char *iter_name = find_subst_iter_by_attr (attr);
486 if (strcmp (iter_name, iterator->name) != 0)
487 continue;
489 /* Find the attribute value associated with the current
490 iterator value. */
491 for (v = m->values; v; v = v->next)
492 if (v->number == iterator->current_value->number)
494 if (res && strcmp (v->string, res->string) != 0)
496 error_at (loc, "ambiguous attribute '%s'; could be"
497 " '%s' (via '%s:%s') or '%s' (via '%s:%s')",
498 attr, res->string, prev->name, attr,
499 v->string, iterator->name, attr);
500 return v;
502 if (iterator_out)
503 *iterator_out = iterator;
504 prev = iterator;
505 res = v;
509 return res;
512 /* Apply the current iterator values to STRING. Return the new string
513 if any changes were needed, otherwise return STRING itself. */
515 const char *
516 md_reader::apply_iterator_to_string (const char *string)
518 char *base, *copy, *p, *start, *end;
519 struct map_value *v;
521 if (string == 0 || string[0] == 0)
522 return string;
524 file_location loc = get_md_ptr_loc (string)->loc;
525 base = p = copy = ASTRDUP (string);
526 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
528 p = start + 1;
530 *end = 0;
531 v = map_attr_string (loc, p);
532 *end = '>';
533 if (v == 0)
534 continue;
536 /* Add everything between the last copied byte and the '<',
537 then add in the attribute value. */
538 obstack_grow (&m_string_obstack, base, start - base);
539 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
540 base = end + 1;
542 if (base != copy)
544 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
545 copy = XOBFINISH (&m_string_obstack, char *);
546 copy_md_ptr_loc (copy, string);
547 return copy;
549 return string;
552 /* Return a deep copy of X, substituting the current iterator
553 values into any strings. */
556 md_reader::copy_rtx_for_iterators (rtx original)
558 const char *format_ptr, *p;
559 int i, j;
560 rtx x;
562 if (original == 0)
563 return original;
565 /* Create a shallow copy of ORIGINAL. */
566 x = rtx_alloc (GET_CODE (original));
567 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
569 /* Change each string and recursively change each rtx. */
570 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
571 for (i = 0; format_ptr[i] != 0; i++)
572 switch (format_ptr[i])
574 case 'T':
575 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
576 XTMPL (x, i) = p;
577 break;
579 case 'S':
580 case 's':
581 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
582 XSTR (x, i) = p;
583 break;
585 case 'e':
586 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
587 break;
589 case 'V':
590 case 'E':
591 if (XVEC (original, i))
593 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
594 for (j = 0; j < XVECLEN (x, i); j++)
595 XVECEXP (x, i, j)
596 = copy_rtx_for_iterators (XVECEXP (original, i, j));
598 break;
600 default:
601 break;
603 return x;
606 #ifdef GENERATOR_FILE
608 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
609 has the form "&& ..." (as used in define_insn_and_splits), assume that
610 EXTRA is already satisfied. Empty strings are treated like "true". */
612 static const char *
613 add_condition_to_string (const char *original, const char *extra)
615 if (original != 0 && original[0] == '&' && original[1] == '&')
616 return original;
617 return rtx_reader_ptr->join_c_conditions (original, extra);
620 /* Like add_condition, but applied to all conditions in rtx X. */
622 static void
623 add_condition_to_rtx (rtx x, const char *extra)
625 switch (GET_CODE (x))
627 case DEFINE_INSN:
628 case DEFINE_EXPAND:
629 case DEFINE_SUBST:
630 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
631 break;
633 case DEFINE_SPLIT:
634 case DEFINE_PEEPHOLE:
635 case DEFINE_PEEPHOLE2:
636 case DEFINE_COND_EXEC:
637 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
638 break;
640 case DEFINE_INSN_AND_SPLIT:
641 case DEFINE_INSN_AND_REWRITE:
642 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
643 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
644 break;
646 default:
647 break;
651 /* Apply the current iterator values to all attribute_uses. */
653 static void
654 apply_attribute_uses (void)
656 struct map_value *v;
657 attribute_use *ause;
658 unsigned int i;
660 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
662 v = map_attr_string (ause->loc, ause->value);
663 if (!v)
664 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
665 ause->group->apply_iterator (ause->x, ause->index,
666 ause->group->find_builtin (v->string));
670 /* A htab_traverse callback for iterators. Add all used iterators
671 to current_iterators. */
673 static int
674 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
676 struct mapping *iterator;
678 iterator = (struct mapping *) *slot;
679 if (iterator->current_value)
680 current_iterators.safe_push (iterator);
681 return 1;
684 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
685 be many instances of two overloaded_names having the same name but
686 different arguments, so hashing on the name should be good enough in
687 practice. */
689 static hashval_t
690 overloaded_name_hash (const void *uncast_oname)
692 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
693 return htab_hash_string (oname->name);
696 /* Return true if two overloaded_names are similar enough to share
697 the same generated functions. */
699 static int
700 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
702 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
703 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
704 if (strcmp (oname1->name, oname2->name) != 0
705 || oname1->arg_types.length () != oname2->arg_types.length ())
706 return 0;
708 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
709 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
710 return 0;
712 return 1;
715 /* Return true if X has an instruction name in XSTR (X, 0). */
717 static bool
718 named_rtx_p (rtx x)
720 switch (GET_CODE (x))
722 case DEFINE_EXPAND:
723 case DEFINE_INSN:
724 case DEFINE_INSN_AND_SPLIT:
725 case DEFINE_INSN_AND_REWRITE:
726 return true;
728 default:
729 return false;
733 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
734 If so, return the associated overloaded_name and add the iterator for
735 each argument to ITERATORS. Return null otherwise. */
737 overloaded_name *
738 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
740 /* Check for the leading '@'. */
741 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
742 return NULL;
744 /* Remove the '@', so that no other code needs to worry about it. */
745 const char *name = XSTR (original, 0);
746 file_location loc = get_md_ptr_loc (name)->loc;
747 copy_md_ptr_loc (name + 1, name);
748 name += 1;
749 XSTR (original, 0) = name;
751 /* Build a copy of the name without the '<...>' attribute strings.
752 Add the iterator associated with each such attribute string to ITERATORS
753 and add an associated argument to TMP_ONAME. */
754 char *copy = ASTRDUP (name);
755 char *base = copy, *start, *end;
756 overloaded_name tmp_oname;
757 tmp_oname.arg_types.create (current_iterators.length ());
758 bool pending_underscore_p = false;
759 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
761 *end = 0;
762 mapping *iterator;
763 if (!map_attr_string (loc, start + 1, &iterator))
764 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
765 *end = '>';
767 /* Remove a trailing underscore, so that we don't end a name
768 with "_" or turn "_<...>_" into "__". */
769 if (start != base && start[-1] == '_')
771 start -= 1;
772 pending_underscore_p = true;
775 /* Add the text between either the last '>' or the start of
776 the string and this '<'. */
777 obstack_grow (&m_string_obstack, base, start - base);
778 base = end + 1;
780 /* If there's a character we need to keep after the '>', check
781 whether we should prefix it with a previously-dropped '_'. */
782 if (base[0] != 0 && base[0] != '<')
784 if (pending_underscore_p && base[0] != '_')
785 obstack_1grow (&m_string_obstack, '_');
786 pending_underscore_p = false;
789 /* Record an argument for ITERATOR. */
790 iterators->safe_push (iterator);
791 tmp_oname.arg_types.safe_push (iterator->group->type);
793 if (base == copy)
794 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
796 size_t length = obstack_object_size (&m_string_obstack);
797 if (length == 0)
798 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
800 /* Get the completed name. */
801 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
802 char *new_name = XOBFINISH (&m_string_obstack, char *);
803 tmp_oname.name = new_name;
805 if (!m_overloads_htab)
806 m_overloads_htab = htab_create (31, overloaded_name_hash,
807 overloaded_name_eq_p, NULL);
809 /* See whether another pattern had the same overload name and list
810 of argument types. Create a new permanent one if not. */
811 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
812 overloaded_name *oname = (overloaded_name *) *slot;
813 if (!oname)
815 *slot = oname = new overloaded_name;
816 oname->name = tmp_oname.name;
817 oname->arg_types = tmp_oname.arg_types;
818 oname->next = NULL;
819 oname->first_instance = NULL;
820 oname->next_instance_ptr = &oname->first_instance;
822 *m_next_overload_ptr = oname;
823 m_next_overload_ptr = &oname->next;
825 else
827 obstack_free (&m_string_obstack, new_name);
828 tmp_oname.arg_types.release ();
831 return oname;
834 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
835 gives the iterator associated with argument I of ONAME. */
837 static void
838 add_overload_instance (overloaded_name *oname, const vec<mapping *> &iterators, rtx x)
840 /* Create the instance. */
841 overloaded_instance *instance = new overloaded_instance;
842 instance->next = NULL;
843 instance->arg_values.create (oname->arg_types.length ());
844 for (unsigned int i = 0; i < iterators.length (); ++i)
846 int value = iterators[i]->current_value->number;
847 const char *name = iterators[i]->group->get_c_token (value);
848 instance->arg_values.quick_push (name);
850 instance->name = XSTR (x, 0);
851 instance->insn = x;
853 /* Chain it onto the end of ONAME's list. */
854 *oname->next_instance_ptr = instance;
855 oname->next_instance_ptr = &instance->next;
858 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
859 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
861 static void
862 apply_iterators (rtx original, vec<rtx> *queue)
864 unsigned int i;
865 const char *condition;
866 iterator_use *iuse;
867 struct mapping *iterator;
868 struct map_value *v;
869 rtx x;
871 if (iterator_uses.is_empty ())
873 /* Raise an error if any attributes were used. */
874 apply_attribute_uses ();
876 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
877 fatal_with_file_and_line ("'@' used without iterators");
879 queue->safe_push (original);
880 return;
883 /* Clear out the iterators from the previous run. */
884 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
885 iterator->current_value = NULL;
886 current_iterators.truncate (0);
888 /* Mark the iterators that we need this time. */
889 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
890 iuse->iterator->current_value = iuse->iterator->values;
892 /* Get the list of iterators that are in use, preserving the
893 definition order within each group. */
894 htab_traverse (modes.iterators, add_current_iterators, NULL);
895 htab_traverse (codes.iterators, add_current_iterators, NULL);
896 htab_traverse (ints.iterators, add_current_iterators, NULL);
897 htab_traverse (substs.iterators, add_current_iterators, NULL);
898 gcc_assert (!current_iterators.is_empty ());
900 /* Check whether this is a '@' overloaded pattern. */
901 auto_vec<mapping *, 16> iterators;
902 overloaded_name *oname
903 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
905 for (;;)
907 /* Apply the current iterator values. Accumulate a condition to
908 say when the resulting rtx can be used. */
909 condition = "";
910 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
912 if (iuse->iterator->group == &substs)
913 continue;
914 v = iuse->iterator->current_value;
915 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
916 v->number);
917 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
919 apply_attribute_uses ();
920 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
921 add_condition_to_rtx (x, condition);
923 /* We apply subst iterator after RTL-template is copied, as during
924 subst-iterator processing, we could add an attribute to the
925 RTL-template, and we don't want to do it in the original one. */
926 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
928 v = iuse->iterator->current_value;
929 if (iuse->iterator->group == &substs)
931 iuse->x = x;
932 iuse->index = 0;
933 current_iterator_name = iuse->iterator->name;
934 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
935 v->number);
939 if (oname)
940 add_overload_instance (oname, iterators, x);
942 /* Add the new rtx to the end of the queue. */
943 queue->safe_push (x);
945 /* Lexicographically increment the iterator value sequence.
946 That is, cycle through iterator values, starting from the right,
947 and stopping when one of them doesn't wrap around. */
948 i = current_iterators.length ();
949 for (;;)
951 if (i == 0)
952 return;
953 i--;
954 iterator = current_iterators[i];
955 iterator->current_value = iterator->current_value->next;
956 if (iterator->current_value)
957 break;
958 iterator->current_value = iterator->values;
962 #endif /* #ifdef GENERATOR_FILE */
964 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
965 of the mapping and GROUP is the group to which it belongs. */
967 static struct mapping *
968 add_mapping (struct iterator_group *group, htab_t table, const char *name)
970 struct mapping *m;
971 void **slot;
973 m = XNEW (struct mapping);
974 m->name = xstrdup (name);
975 m->group = group;
976 m->values = 0;
977 m->current_value = NULL;
979 slot = htab_find_slot (table, m, INSERT);
980 if (*slot != 0)
981 fatal_with_file_and_line ("`%s' already defined", name);
983 *slot = m;
984 return m;
987 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
988 END_PTR points to the current null terminator for the list; return
989 a pointer the new null terminator. */
991 static struct map_value **
992 add_map_value (struct map_value **end_ptr, int number, const char *string)
994 struct map_value *value;
996 value = XNEW (struct map_value);
997 value->next = 0;
998 value->number = number;
999 value->string = string;
1001 *end_ptr = value;
1002 return &value->next;
1005 /* Do one-time initialization of the mode and code attributes. */
1007 static void
1008 initialize_iterators (void)
1010 struct mapping *lower, *upper;
1011 struct map_value **lower_ptr, **upper_ptr;
1012 char *copy, *p;
1013 int i;
1015 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1016 modes.iterators = htab_create (13, leading_string_hash,
1017 leading_string_eq_p, 0);
1018 modes.type = "machine_mode";
1019 modes.find_builtin = find_mode;
1020 modes.apply_iterator = apply_mode_iterator;
1021 modes.get_c_token = get_mode_token;
1023 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1024 codes.iterators = htab_create (13, leading_string_hash,
1025 leading_string_eq_p, 0);
1026 codes.type = "rtx_code";
1027 codes.find_builtin = find_code;
1028 codes.apply_iterator = apply_code_iterator;
1029 codes.get_c_token = get_code_token;
1031 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1032 ints.iterators = htab_create (13, leading_string_hash,
1033 leading_string_eq_p, 0);
1034 ints.type = "int";
1035 ints.find_builtin = find_int;
1036 ints.apply_iterator = apply_int_iterator;
1037 ints.get_c_token = get_int_token;
1039 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
1040 substs.iterators = htab_create (13, leading_string_hash,
1041 leading_string_eq_p, 0);
1042 substs.type = "int";
1043 substs.find_builtin = find_int; /* We don't use it, anyway. */
1044 #ifdef GENERATOR_FILE
1045 substs.apply_iterator = apply_subst_iterator;
1046 #endif
1047 substs.get_c_token = get_int_token;
1049 lower = add_mapping (&modes, modes.attrs, "mode");
1050 upper = add_mapping (&modes, modes.attrs, "MODE");
1051 lower_ptr = &lower->values;
1052 upper_ptr = &upper->values;
1053 for (i = 0; i < MAX_MACHINE_MODE; i++)
1055 copy = xstrdup (GET_MODE_NAME (i));
1056 for (p = copy; *p != 0; p++)
1057 *p = TOLOWER (*p);
1059 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
1060 lower_ptr = add_map_value (lower_ptr, i, copy);
1063 lower = add_mapping (&codes, codes.attrs, "code");
1064 upper = add_mapping (&codes, codes.attrs, "CODE");
1065 lower_ptr = &lower->values;
1066 upper_ptr = &upper->values;
1067 for (i = 0; i < NUM_RTX_CODE; i++)
1069 copy = xstrdup (GET_RTX_NAME (i));
1070 for (p = copy; *p != 0; p++)
1071 *p = TOUPPER (*p);
1073 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
1074 upper_ptr = add_map_value (upper_ptr, i, copy);
1079 #ifdef GENERATOR_FILE
1080 /* Process a define_conditions directive, starting with the optional
1081 space after the "define_conditions". The directive looks like this:
1083 (define_conditions [
1084 (number "string")
1085 (number "string")
1089 It's not intended to appear in machine descriptions. It is
1090 generated by (the program generated by) genconditions.cc, and
1091 slipped in at the beginning of the sequence of MD files read by
1092 most of the other generators. */
1093 void
1094 md_reader::read_conditions ()
1096 int c;
1098 require_char_ws ('[');
1100 while ( (c = read_skip_spaces ()) != ']')
1102 struct md_name name;
1103 char *expr;
1104 int value;
1106 if (c != '(')
1107 fatal_expected_char ('(', c);
1109 read_name (&name);
1110 validate_const_int (name.string);
1111 value = atoi (name.string);
1113 require_char_ws ('"');
1114 expr = read_quoted_string ();
1116 require_char_ws (')');
1118 add_c_test (expr, value);
1121 #endif /* #ifdef GENERATOR_FILE */
1123 static void
1124 validate_const_int (const char *string)
1126 const char *cp;
1127 int valid = 1;
1129 cp = string;
1130 while (*cp && ISSPACE (*cp))
1131 cp++;
1132 if (*cp == '-' || *cp == '+')
1133 cp++;
1134 if (*cp == 0)
1135 valid = 0;
1136 for (; *cp; cp++)
1137 if (! ISDIGIT (*cp))
1139 valid = 0;
1140 break;
1142 if (!valid)
1143 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
1146 static void
1147 validate_const_wide_int (const char *string)
1149 const char *cp;
1150 int valid = 1;
1152 cp = string;
1153 while (*cp && ISSPACE (*cp))
1154 cp++;
1155 /* Skip the leading 0x. */
1156 if (cp[0] == '0' || cp[1] == 'x')
1157 cp += 2;
1158 else
1159 valid = 0;
1160 if (*cp == 0)
1161 valid = 0;
1162 for (; *cp; cp++)
1163 if (! ISXDIGIT (*cp))
1164 valid = 0;
1165 if (!valid)
1166 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
1169 /* Record that X uses iterator ITERATOR. If the use is in an operand
1170 of X, INDEX is the index of that operand, otherwise it is ignored. */
1172 static void
1173 record_iterator_use (struct mapping *iterator, rtx x, unsigned int index)
1175 struct iterator_use iuse = {iterator, x, index};
1176 iterator_uses.safe_push (iuse);
1179 /* Record that X uses attribute VALUE at location LOC, where VALUE must
1180 match a built-in value from group GROUP. If the use is in an operand
1181 of X, INDEX is the index of that operand, otherwise it is ignored. */
1183 static void
1184 record_attribute_use (struct iterator_group *group, file_location loc, rtx x,
1185 unsigned int index, const char *value)
1187 struct attribute_use ause = {group, loc, value, x, index};
1188 attribute_uses.safe_push (ause);
1191 /* Interpret NAME as either a built-in value, iterator or attribute
1192 for group GROUP. X and INDEX are the values to pass to GROUP's
1193 apply_iterator callback. LOC is the location of the use. */
1195 void
1196 md_reader::record_potential_iterator_use (struct iterator_group *group,
1197 file_location loc,
1198 rtx x, unsigned int index,
1199 const char *name)
1201 struct mapping *m;
1202 size_t len;
1204 len = strlen (name);
1205 if (name[0] == '<' && name[len - 1] == '>')
1207 /* Copy the attribute string into permanent storage, without the
1208 angle brackets around it. */
1209 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
1210 record_attribute_use (group, loc, x, index,
1211 XOBFINISH (&m_string_obstack, char *));
1213 else
1215 m = (struct mapping *) htab_find (group->iterators, &name);
1216 if (m != 0)
1217 record_iterator_use (m, x, index);
1218 else
1219 group->apply_iterator (x, index, group->find_builtin (name));
1223 #ifdef GENERATOR_FILE
1225 /* Finish reading a declaration of the form:
1227 (define... <name> [<value1> ... <valuen>])
1229 from the MD file, where each <valuei> is either a bare symbol name or a
1230 "(<name> <string>)" pair. The "(define..." part has already been read.
1232 Represent the declaration as a "mapping" structure; add it to TABLE
1233 (which belongs to GROUP) and return it. */
1235 struct mapping *
1236 md_reader::read_mapping (struct iterator_group *group, htab_t table)
1238 struct md_name name;
1239 struct mapping *m;
1240 struct map_value **end_ptr;
1241 const char *string;
1242 int number, c;
1244 /* Read the mapping name and create a structure for it. */
1245 read_name (&name);
1246 m = add_mapping (group, table, name.string);
1248 require_char_ws ('[');
1250 /* Read each value. */
1251 end_ptr = &m->values;
1252 c = read_skip_spaces ();
1255 if (c != '(')
1257 /* A bare symbol name that is implicitly paired to an
1258 empty string. */
1259 unread_char (c);
1260 read_name (&name);
1261 string = "";
1263 else
1265 /* A "(name string)" pair. */
1266 read_name (&name);
1267 string = read_string (false);
1268 require_char_ws (')');
1270 number = group->find_builtin (name.string);
1271 end_ptr = add_map_value (end_ptr, number, string);
1272 c = read_skip_spaces ();
1274 while (c != ']');
1276 return m;
1279 /* For iterator with name ATTR_NAME generate define_attr with values
1280 'yes' and 'no'. This attribute is used to mark templates to which
1281 define_subst ATTR_NAME should be applied. This attribute is set and
1282 defined implicitly and automatically. */
1283 static void
1284 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
1286 rtx const_str, return_rtx;
1288 return_rtx = rtx_alloc (DEFINE_ATTR);
1289 PUT_CODE (return_rtx, DEFINE_ATTR);
1291 const_str = rtx_alloc (CONST_STRING);
1292 PUT_CODE (const_str, CONST_STRING);
1293 XSTR (const_str, 0) = xstrdup ("no");
1295 XSTR (return_rtx, 0) = xstrdup (attr_name);
1296 XSTR (return_rtx, 1) = xstrdup ("no,yes");
1297 XEXP (return_rtx, 2) = const_str;
1299 queue->safe_push (return_rtx);
1302 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1303 ATTR_OPERANDS and places it to QUEUE. */
1304 static void
1305 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
1307 rtx return_rtx;
1308 int i;
1310 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
1311 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
1313 for (i = 0; i < 4; i++)
1314 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
1316 queue->safe_push (return_rtx);
1319 /* Read define_subst_attribute construction. It has next form:
1320 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1321 Attribute is substituted with value1 when no subst is applied and with
1322 value2 in the opposite case.
1323 Attributes are added to SUBST_ATTRS_TABLE.
1324 In case the iterator is encountered for the first time, it's added to
1325 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1327 static void
1328 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
1329 vec<rtx> *queue)
1331 struct mapping *m;
1332 struct map_value **end_ptr;
1333 const char *attr_operands[4];
1334 int i;
1336 for (i = 0; i < 4; i++)
1337 attr_operands[i] = rtx_reader_ptr->read_string (false);
1339 add_define_subst_attr (attr_operands, queue);
1341 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1343 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1344 if (!m)
1346 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1347 end_ptr = &m->values;
1348 end_ptr = add_map_value (end_ptr, 1, "");
1349 add_map_value (end_ptr, 2, "");
1351 add_define_attr_for_define_subst (attr_operands[1], queue);
1354 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1355 end_ptr = &m->values;
1356 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1357 add_map_value (end_ptr, 2, attr_operands[3]);
1360 /* Check newly-created code iterator ITERATOR to see whether every code has the
1361 same format. */
1363 static void
1364 check_code_iterator (struct mapping *iterator)
1366 struct map_value *v;
1367 enum rtx_code bellwether;
1369 bellwether = (enum rtx_code) iterator->values->number;
1370 for (v = iterator->values->next; v != 0; v = v->next)
1371 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1372 fatal_with_file_and_line ("code iterator `%s' combines "
1373 "`%s' and `%s', which have different "
1374 "rtx formats", iterator->name,
1375 GET_RTX_NAME (bellwether),
1376 GET_RTX_NAME (v->number));
1379 /* Check that all values of attribute ATTR are rtx codes that have a
1380 consistent format. Return a representative code. */
1382 static rtx_code
1383 check_code_attribute (mapping *attr)
1385 rtx_code bellwether = UNKNOWN;
1386 for (map_value *v = attr->values; v != 0; v = v->next)
1388 rtx_code code = maybe_find_code (v->string);
1389 if (code == UNKNOWN)
1390 fatal_with_file_and_line ("code attribute `%s' contains "
1391 "unrecognized rtx code `%s'",
1392 attr->name, v->string);
1393 if (bellwether == UNKNOWN)
1394 bellwether = code;
1395 else if (strcmp (GET_RTX_FORMAT (bellwether),
1396 GET_RTX_FORMAT (code)) != 0)
1397 fatal_with_file_and_line ("code attribute `%s' combines "
1398 "`%s' and `%s', which have different "
1399 "rtx formats", attr->name,
1400 GET_RTX_NAME (bellwether),
1401 GET_RTX_NAME (code));
1403 return bellwether;
1406 /* Read an rtx-related declaration from the MD file, given that it
1407 starts with directive name RTX_NAME. Return true if it expands to
1408 one or more rtxes (as defined by rtx.def). When returning true,
1409 store the list of rtxes as an EXPR_LIST in *X. */
1411 bool
1412 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1414 /* Handle various rtx-related declarations that aren't themselves
1415 encoded as rtxes. */
1416 if (strcmp (rtx_name, "define_conditions") == 0)
1418 read_conditions ();
1419 return false;
1421 if (strcmp (rtx_name, "define_mode_attr") == 0)
1423 read_mapping (&modes, modes.attrs);
1424 return false;
1426 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1428 read_mapping (&modes, modes.iterators);
1429 return false;
1431 if (strcmp (rtx_name, "define_code_attr") == 0)
1433 read_mapping (&codes, codes.attrs);
1434 return false;
1436 if (strcmp (rtx_name, "define_code_iterator") == 0)
1438 check_code_iterator (read_mapping (&codes, codes.iterators));
1439 return false;
1441 if (strcmp (rtx_name, "define_int_attr") == 0)
1443 read_mapping (&ints, ints.attrs);
1444 return false;
1446 if (strcmp (rtx_name, "define_int_iterator") == 0)
1448 read_mapping (&ints, ints.iterators);
1449 return false;
1451 if (strcmp (rtx_name, "define_subst_attr") == 0)
1453 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1455 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1456 TRUE to process it. */
1457 return true;
1460 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1461 iterator_uses.truncate (0);
1462 attribute_uses.truncate (0);
1464 return true;
1467 #endif /* #ifdef GENERATOR_FILE */
1469 /* Do one-time initialization. */
1471 static void
1472 one_time_initialization (void)
1474 static bool initialized = false;
1476 if (!initialized)
1478 initialize_iterators ();
1479 initialized = true;
1483 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1484 consuming the terminator character if CONSUME_TERMINATOR is true.
1485 Return all characters before the terminator as an allocated buffer. */
1487 char *
1488 rtx_reader::read_until (const char *terminator_chars, bool consume_terminator)
1490 int ch = read_skip_spaces ();
1491 unread_char (ch);
1492 auto_vec<char> buf;
1493 while (1)
1495 ch = read_char ();
1496 if (strchr (terminator_chars, ch))
1498 if (!consume_terminator)
1499 unread_char (ch);
1500 break;
1502 buf.safe_push (ch);
1504 buf.safe_push ('\0');
1505 return xstrdup (buf.address ());
1508 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1510 static void
1511 read_flags (rtx return_rtx)
1513 while (1)
1515 int ch = read_char ();
1516 if (ch != '/')
1518 unread_char (ch);
1519 break;
1522 int flag_char = read_char ();
1523 switch (flag_char)
1525 case 's':
1526 RTX_FLAG (return_rtx, in_struct) = 1;
1527 break;
1528 case 'v':
1529 RTX_FLAG (return_rtx, volatil) = 1;
1530 break;
1531 case 'u':
1532 RTX_FLAG (return_rtx, unchanging) = 1;
1533 break;
1534 case 'f':
1535 RTX_FLAG (return_rtx, frame_related) = 1;
1536 break;
1537 case 'j':
1538 RTX_FLAG (return_rtx, jump) = 1;
1539 break;
1540 case 'c':
1541 RTX_FLAG (return_rtx, call) = 1;
1542 break;
1543 case 'i':
1544 RTX_FLAG (return_rtx, return_val) = 1;
1545 break;
1546 default:
1547 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char);
1552 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1553 or fail if STRING isn't recognized. */
1555 static int
1556 parse_reg_note_name (const char *string)
1558 for (int i = 0; i < REG_NOTE_MAX; i++)
1559 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1560 return i;
1561 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1564 /* Allocate an rtx for code NAME. If NAME is a code iterator or code
1565 attribute, record its use for later and use one of its possible
1566 values as an interim rtx code. */
1569 rtx_reader::rtx_alloc_for_name (const char *name)
1571 #ifdef GENERATOR_FILE
1572 size_t len = strlen (name);
1573 if (name[0] == '<' && name[len - 1] == '>')
1575 /* Copy the attribute string into permanent storage, without the
1576 angle brackets around it. */
1577 obstack *strings = get_string_obstack ();
1578 obstack_grow0 (strings, name + 1, len - 2);
1579 char *deferred_name = XOBFINISH (strings, char *);
1581 /* Find the name of the attribute. */
1582 const char *attr = strchr (deferred_name, ':');
1583 if (!attr)
1584 attr = deferred_name;
1586 /* Find the attribute itself. */
1587 mapping *m = (mapping *) htab_find (codes.attrs, &attr);
1588 if (!m)
1589 fatal_with_file_and_line ("unknown code attribute `%s'", attr);
1591 /* Pick the first possible code for now, and record the attribute
1592 use for later. */
1593 rtx x = rtx_alloc (check_code_attribute (m));
1594 record_attribute_use (&codes, get_current_location (),
1595 x, 0, deferred_name);
1596 return x;
1599 mapping *iterator = (mapping *) htab_find (codes.iterators, &name);
1600 if (iterator != 0)
1602 /* Pick the first possible code for now, and record the iterator
1603 use for later. */
1604 rtx x = rtx_alloc (rtx_code (iterator->values->number));
1605 record_iterator_use (iterator, x, 0);
1606 return x;
1608 #endif
1610 return rtx_alloc (rtx_code (codes.find_builtin (name)));
1613 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1614 either an rtx code or a code iterator. Parse the rest of the rtx and
1615 return it. */
1618 rtx_reader::read_rtx_code (const char *code_name)
1620 RTX_CODE code;
1621 const char *format_ptr;
1622 struct md_name name;
1623 rtx return_rtx;
1624 int c;
1625 long reuse_id = -1;
1627 /* Linked list structure for making RTXs: */
1628 struct rtx_list
1630 struct rtx_list *next;
1631 rtx value; /* Value of this node. */
1634 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1635 if (ISDIGIT (code_name[0]))
1637 reuse_id = atoi (code_name);
1638 while (char ch = *code_name++)
1639 if (ch == '|')
1640 break;
1643 /* Handle "reuse_rtx". */
1644 if (strcmp (code_name, "reuse_rtx") == 0)
1646 read_name (&name);
1647 unsigned idx = atoi (name.string);
1648 /* Look it up by ID. */
1649 gcc_assert (idx < m_reuse_rtx_by_id.length ());
1650 return_rtx = m_reuse_rtx_by_id[idx];
1651 return return_rtx;
1654 /* Handle "const_double_zero". */
1655 if (strcmp (code_name, "const_double_zero") == 0)
1657 code = CONST_DOUBLE;
1658 return_rtx = rtx_alloc (code);
1659 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1660 PUT_CODE (return_rtx, code);
1661 c = read_skip_spaces ();
1662 if (c == ':')
1664 file_location loc = read_name (&name);
1665 record_potential_iterator_use (&modes, loc, return_rtx, 0,
1666 name.string);
1668 else
1669 unread_char (c);
1670 return return_rtx;
1673 /* If we end up with an insn expression then we free this space below. */
1674 return_rtx = rtx_alloc_for_name (code_name);
1675 code = GET_CODE (return_rtx);
1676 format_ptr = GET_RTX_FORMAT (code);
1677 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1678 PUT_CODE (return_rtx, code);
1680 if (reuse_id != -1)
1682 /* Store away for later reuse. */
1683 m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1, true);
1684 m_reuse_rtx_by_id[reuse_id] = return_rtx;
1687 /* Check for flags. */
1688 read_flags (return_rtx);
1690 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1691 if ((GET_CODE (return_rtx) == EXPR_LIST
1692 || GET_CODE (return_rtx) == INSN_LIST
1693 || GET_CODE (return_rtx) == INT_LIST)
1694 && !m_in_call_function_usage)
1696 char ch = read_char ();
1697 if (ch == ':')
1699 read_name (&name);
1700 PUT_MODE_RAW (return_rtx,
1701 (machine_mode)parse_reg_note_name (name.string));
1703 else
1704 unread_char (ch);
1707 /* If what follows is `: mode ', read it and
1708 store the mode in the rtx. */
1710 c = read_skip_spaces ();
1711 if (c == ':')
1713 file_location loc = read_name (&name);
1714 record_potential_iterator_use (&modes, loc, return_rtx, 0, name.string);
1716 else
1717 unread_char (c);
1719 if (INSN_CHAIN_CODE_P (code))
1721 read_name (&name);
1722 INSN_UID (return_rtx) = atoi (name.string);
1725 /* Use the format_ptr to parse the various operands of this rtx. */
1726 for (int idx = 0; format_ptr[idx] != 0; idx++)
1727 return_rtx = read_rtx_operand (return_rtx, idx);
1729 /* Handle any additional information that after the regular fields
1730 (e.g. when parsing function dumps). */
1731 handle_any_trailing_information (return_rtx);
1733 if (CONST_WIDE_INT_P (return_rtx))
1735 read_name (&name);
1736 validate_const_wide_int (name.string);
1738 const char *s = name.string;
1739 int len;
1740 int index = 0;
1741 int gs = HOST_BITS_PER_WIDE_INT/4;
1742 int pos;
1743 char * buf = XALLOCAVEC (char, gs + 1);
1744 unsigned HOST_WIDE_INT wi;
1745 int wlen;
1747 /* Skip the leading spaces. */
1748 while (*s && ISSPACE (*s))
1749 s++;
1751 /* Skip the leading 0x. */
1752 gcc_assert (s[0] == '0');
1753 gcc_assert (s[1] == 'x');
1754 s += 2;
1756 len = strlen (s);
1757 pos = len - gs;
1758 wlen = (len + gs - 1) / gs; /* Number of words needed */
1760 return_rtx = const_wide_int_alloc (wlen);
1762 while (pos > 0)
1764 #if HOST_BITS_PER_WIDE_INT == 64
1765 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1766 #else
1767 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1768 #endif
1769 CWI_ELT (return_rtx, index++) = wi;
1770 pos -= gs;
1772 strncpy (buf, s, gs - pos);
1773 buf [gs - pos] = 0;
1774 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1775 CWI_ELT (return_rtx, index++) = wi;
1776 /* TODO: After reading, do we want to canonicalize with:
1777 value = lookup_const_wide_int (value); ? */
1781 c = read_skip_spaces ();
1782 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1783 arbitrary number of arguments for them. */
1784 if (c == '('
1785 && (GET_CODE (return_rtx) == AND
1786 || GET_CODE (return_rtx) == IOR))
1787 return read_rtx_variadic (return_rtx);
1789 unread_char (c);
1790 return return_rtx;
1793 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1794 based on the corresponding format character within GET_RTX_FORMAT
1795 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1796 This is a virtual function, so that function_reader can override
1797 some parsing, and potentially return a different rtx. */
1800 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1802 RTX_CODE code = GET_CODE (return_rtx);
1803 const char *format_ptr = GET_RTX_FORMAT (code);
1804 int c;
1805 struct md_name name;
1807 switch (format_ptr[idx])
1809 /* 0 means a field for internal use only.
1810 Don't expect it to be present in the input. */
1811 case '0':
1812 if (code == REG)
1813 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1814 break;
1816 case 'e':
1817 XEXP (return_rtx, idx) = read_nested_rtx ();
1818 break;
1820 case 'u':
1821 XEXP (return_rtx, idx) = read_nested_rtx ();
1822 break;
1824 case 'V':
1825 /* 'V' is an optional vector: if a closeparen follows,
1826 just store NULL for this element. */
1827 c = read_skip_spaces ();
1828 unread_char (c);
1829 if (c == ')')
1831 XVEC (return_rtx, idx) = 0;
1832 break;
1834 /* Now process the vector. */
1835 /* FALLTHRU */
1837 case 'E':
1839 /* Obstack to store scratch vector in. */
1840 struct obstack vector_stack;
1841 int list_counter = 0;
1842 rtvec return_vec = NULL_RTVEC;
1843 rtx saved_rtx = NULL_RTX;
1845 require_char_ws ('[');
1847 /* Add expressions to a list, while keeping a count. */
1848 obstack_init (&vector_stack);
1849 while ((c = read_skip_spaces ()) && c != ']')
1851 if (c == EOF)
1852 fatal_expected_char (']', c);
1853 unread_char (c);
1855 rtx value;
1856 int repeat_count = 1;
1857 if (c == 'r')
1859 /* Process "repeated xN" directive. */
1860 read_name (&name);
1861 if (strcmp (name.string, "repeated"))
1862 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1863 name.string);
1864 read_name (&name);
1865 if (!sscanf (name.string, "x%d", &repeat_count))
1866 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1867 name.string);
1869 /* We already saw one of the instances. */
1870 repeat_count--;
1871 value = saved_rtx;
1873 else
1874 value = read_nested_rtx ();
1876 for (; repeat_count > 0; repeat_count--)
1878 list_counter++;
1879 obstack_ptr_grow (&vector_stack, value);
1881 saved_rtx = value;
1883 if (list_counter > 0)
1885 return_vec = rtvec_alloc (list_counter);
1886 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1887 list_counter * sizeof (rtx));
1889 else if (format_ptr[idx] == 'E')
1890 fatal_with_file_and_line ("vector must have at least one element");
1891 XVEC (return_rtx, idx) = return_vec;
1892 obstack_free (&vector_stack, NULL);
1893 /* close bracket gotten */
1895 break;
1897 case 'S':
1898 case 'T':
1899 case 's':
1901 char *stringbuf;
1902 int star_if_braced;
1904 c = read_skip_spaces ();
1905 unread_char (c);
1906 if (c == ')')
1908 /* 'S' fields are optional and should be NULL if no string
1909 was given. Also allow normal 's' and 'T' strings to be
1910 omitted, treating them in the same way as empty strings. */
1911 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1912 break;
1915 /* The output template slot of a DEFINE_INSN, DEFINE_INSN_AND_SPLIT,
1916 DEFINE_INSN_AND_REWRITE or DEFINE_PEEPHOLE automatically
1917 gets a star inserted as its first character, if it is
1918 written with a brace block instead of a string constant. */
1919 star_if_braced = (format_ptr[idx] == 'T');
1921 stringbuf = read_string (star_if_braced);
1922 if (!stringbuf)
1923 break;
1925 #ifdef GENERATOR_FILE
1926 /* For insn patterns, we want to provide a default name
1927 based on the file and line, like "*foo.md:12", if the
1928 given name is blank. These are only for define_insn and
1929 define_insn_and_split, to aid debugging. */
1930 if (*stringbuf == '\0'
1931 && idx == 0
1932 && (GET_CODE (return_rtx) == DEFINE_INSN
1933 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT
1934 || GET_CODE (return_rtx) == DEFINE_INSN_AND_REWRITE))
1936 const char *old_stringbuf = stringbuf;
1937 struct obstack *string_obstack = get_string_obstack ();
1938 char line_name[20];
1939 const char *read_md_filename = get_filename ();
1940 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1941 const char *slash;
1942 for (slash = fn; *slash; slash ++)
1943 if (*slash == '/' || *slash == '\\' || *slash == ':')
1944 fn = slash + 1;
1945 obstack_1grow (string_obstack, '*');
1946 obstack_grow (string_obstack, fn, strlen (fn));
1947 sprintf (line_name, ":%d", get_lineno ());
1948 obstack_grow (string_obstack, line_name, strlen (line_name)+1);
1949 stringbuf = XOBFINISH (string_obstack, char *);
1950 copy_md_ptr_loc (stringbuf, old_stringbuf);
1953 /* Find attr-names in the string. */
1954 char *str;
1955 char *start, *end, *ptr;
1956 char tmpstr[256];
1957 ptr = &tmpstr[0];
1958 end = stringbuf;
1959 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1961 if ((end - start - 1 > 0)
1962 && (end - start - 1 < (int)sizeof (tmpstr)))
1964 strncpy (tmpstr, start+1, end-start-1);
1965 tmpstr[end-start-1] = 0;
1966 end++;
1968 else
1969 break;
1970 struct mapping *m
1971 = (struct mapping *) htab_find (substs.attrs, &ptr);
1972 if (m != 0)
1974 /* Here we should find linked subst-iter. */
1975 str = find_subst_iter_by_attr (ptr);
1976 if (str)
1977 m = (struct mapping *) htab_find (substs.iterators, &str);
1978 else
1979 m = 0;
1981 if (m != 0)
1982 record_iterator_use (m, return_rtx, 0);
1984 #endif /* #ifdef GENERATOR_FILE */
1986 const char *string_ptr = finalize_string (stringbuf);
1988 if (star_if_braced)
1989 XTMPL (return_rtx, idx) = string_ptr;
1990 else
1991 XSTR (return_rtx, idx) = string_ptr;
1993 break;
1995 case 'i':
1996 case 'n':
1997 case 'w':
1998 case 'p':
2000 /* Can be an iterator or an integer constant. */
2001 file_location loc = read_name (&name);
2002 record_potential_iterator_use (&ints, loc, return_rtx, idx,
2003 name.string);
2004 break;
2007 case 'r':
2008 read_name (&name);
2009 validate_const_int (name.string);
2010 set_regno_raw (return_rtx, atoi (name.string), 1);
2011 REG_ATTRS (return_rtx) = NULL;
2012 break;
2014 default:
2015 gcc_unreachable ();
2018 return return_rtx;
2021 /* Read a nested rtx construct from the MD file and return it. */
2024 rtx_reader::read_nested_rtx ()
2026 struct md_name name;
2027 rtx return_rtx;
2029 /* In compact dumps, trailing "(nil)" values can be omitted.
2030 Handle such dumps. */
2031 if (peek_char () == ')')
2032 return NULL_RTX;
2034 require_char_ws ('(');
2036 read_name (&name);
2037 if (strcmp (name.string, "nil") == 0)
2038 return_rtx = NULL;
2039 else
2040 return_rtx = read_rtx_code (name.string);
2042 require_char_ws (')');
2044 return_rtx = postprocess (return_rtx);
2046 return return_rtx;
2049 /* Mutually recursive subroutine of read_rtx which reads
2050 (thing x1 x2 x3 ...) and produces RTL as if
2051 (thing x1 (thing x2 (thing x3 ...))) had been written.
2052 When called, FORM is (thing x1 x2), and the file position
2053 is just past the leading parenthesis of x3. Only works
2054 for THINGs which are dyadic expressions, e.g. AND, IOR. */
2056 rtx_reader::read_rtx_variadic (rtx form)
2058 char c = '(';
2059 rtx p = form, q;
2063 unread_char (c);
2065 q = rtx_alloc (GET_CODE (p));
2066 PUT_MODE (q, GET_MODE (p));
2068 XEXP (q, 0) = XEXP (p, 1);
2069 XEXP (q, 1) = read_nested_rtx ();
2071 XEXP (p, 1) = q;
2072 p = q;
2073 c = read_skip_spaces ();
2075 while (c == '(');
2076 unread_char (c);
2077 return form;
2080 /* Constructor for class rtx_reader. */
2082 rtx_reader::rtx_reader (bool compact)
2083 : md_reader (compact),
2084 m_in_call_function_usage (false)
2086 /* Set the global singleton pointer. */
2087 rtx_reader_ptr = this;
2089 one_time_initialization ();
2092 /* Destructor for class rtx_reader. */
2094 rtx_reader::~rtx_reader ()
2096 /* Clear the global singleton pointer. */
2097 rtx_reader_ptr = NULL;