Add GCC support to ENQCMD.
[official-gcc.git] / gcc / read-rtl.c
blobf72b2c35c7db01a90a908872154a5b109c117e64
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2019 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 #ifndef GENERATOR_FILE
40 #include "function.h"
41 #include "memmodel.h"
42 #include "emit-rtl.h"
43 #endif
45 /* One element in a singly-linked list of (integer, string) pairs. */
46 struct map_value {
47 struct map_value *next;
48 int number;
49 const char *string;
52 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
53 The integers are iterator values; the strings are either C conditions
54 or attribute values. */
55 struct mapping {
56 /* The name of the iterator or attribute. */
57 const char *name;
59 /* The group (modes or codes) to which the iterator or attribute belongs. */
60 struct iterator_group *group;
62 /* The list of (integer, string) pairs. */
63 struct map_value *values;
65 /* For iterators, records the current value of the iterator. */
66 struct map_value *current_value;
69 /* A structure for abstracting the common parts of iterators. */
70 struct iterator_group {
71 /* Tables of "mapping" structures, one for attributes and one for
72 iterators. */
73 htab_t attrs, iterators;
75 /* The C++ type of the iterator, such as "machine_mode" for modes. */
76 const char *type;
78 /* Treat the given string as the name of a standard mode, etc., and
79 return its integer value. */
80 int (*find_builtin) (const char *);
82 /* Make the given rtx use the iterator value given by the third argument.
83 If the iterator applies to operands, the second argument gives the
84 operand index, otherwise it is ignored. */
85 void (*apply_iterator) (rtx, unsigned int, int);
87 /* Return the C token for the given standard mode, code, etc. */
88 const char *(*get_c_token) (int);
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 name of the attribute, possibly with an "iterator:" prefix. */
110 const char *value;
112 /* The location of the use, as passed to GROUP's apply_iterator callback.
113 The index is the number of the operand that used the iterator
114 if applicable, otherwise it is ignored. */
115 rtx x;
116 unsigned int index;
119 /* This struct is used to link subst_attr named ATTR_NAME with
120 corresponding define_subst named ITER_NAME. */
121 struct subst_attr_to_iter_mapping
123 char *attr_name;
124 char *iter_name;
127 /* Hash-table to store links between subst-attributes and
128 define_substs. */
129 htab_t subst_attr_to_iter_map = NULL;
130 /* This global stores name of subst-iterator which is currently being
131 processed. */
132 const char *current_iterator_name;
134 static void validate_const_int (const char *);
135 static void one_time_initialization (void);
137 /* Global singleton. */
138 rtx_reader *rtx_reader_ptr = NULL;
140 /* The mode and code iterator structures. */
141 static struct iterator_group modes, codes, ints, substs;
143 /* All iterators used in the current rtx. */
144 static vec<mapping *> current_iterators;
146 /* The list of all iterator uses in the current rtx. */
147 static vec<iterator_use> iterator_uses;
149 /* The list of all attribute uses in the current rtx. */
150 static vec<attribute_use> attribute_uses;
152 /* Implementations of the iterator_group callbacks for modes. */
154 static int
155 find_mode (const char *name)
157 int i;
159 for (i = 0; i < NUM_MACHINE_MODES; i++)
160 if (strcmp (GET_MODE_NAME (i), name) == 0)
161 return i;
163 fatal_with_file_and_line ("unknown mode `%s'", name);
166 static void
167 apply_mode_iterator (rtx x, unsigned int, int mode)
169 PUT_MODE (x, (machine_mode) mode);
172 static const char *
173 get_mode_token (int mode)
175 return concat ("E_", GET_MODE_NAME (mode), "mode", NULL);
178 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
179 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
181 struct compact_insn_name {
182 RTX_CODE code;
183 const char *name;
186 static const compact_insn_name compact_insn_names[] = {
187 { DEBUG_INSN, "cdebug_insn" },
188 { INSN, "cinsn" },
189 { JUMP_INSN, "cjump_insn" },
190 { CALL_INSN, "ccall_insn" },
191 { JUMP_TABLE_DATA, "cjump_table_data" },
192 { BARRIER, "cbarrier" },
193 { CODE_LABEL, "clabel" },
194 { NOTE, "cnote" }
197 /* Return the rtx code for NAME, or UNKNOWN if NAME isn't a valid rtx code. */
199 static rtx_code
200 maybe_find_code (const char *name)
202 for (int i = 0; i < NUM_RTX_CODE; i++)
203 if (strcmp (GET_RTX_NAME (i), name) == 0)
204 return (rtx_code) i;
206 for (int i = 0; i < (signed)ARRAY_SIZE (compact_insn_names); i++)
207 if (strcmp (compact_insn_names[i].name, name) == 0)
208 return compact_insn_names[i].code;
210 return UNKNOWN;
213 /* Implementations of the iterator_group callbacks for codes. */
215 static int
216 find_code (const char *name)
218 rtx_code code = maybe_find_code (name);
219 if (code == UNKNOWN)
220 fatal_with_file_and_line ("unknown rtx code `%s'", name);
221 return code;
224 static void
225 apply_code_iterator (rtx x, unsigned int, int code)
227 PUT_CODE (x, (enum rtx_code) code);
230 static const char *
231 get_code_token (int code)
233 char *name = xstrdup (GET_RTX_NAME (code));
234 for (int i = 0; name[i]; ++i)
235 name[i] = TOUPPER (name[i]);
236 return name;
239 /* Implementations of the iterator_group callbacks for ints. */
241 /* Since GCC does not construct a table of valid constants,
242 we have to accept any int as valid. No cross-checking can
243 be done. */
245 static int
246 find_int (const char *name)
248 validate_const_int (name);
249 return atoi (name);
252 static void
253 apply_int_iterator (rtx x, unsigned int index, int value)
255 if (GET_CODE (x) == SUBREG)
256 SUBREG_BYTE (x) = value;
257 else
258 XINT (x, index) = value;
261 static const char *
262 get_int_token (int value)
264 char buffer[HOST_BITS_PER_INT + 1];
265 sprintf (buffer, "%d", value);
266 return xstrdup (buffer);
269 #ifdef GENERATOR_FILE
271 /* This routine adds attribute or does nothing depending on VALUE. When
272 VALUE is 1, it does nothing - the first duplicate of original
273 template is kept untouched when it's subjected to a define_subst.
274 When VALUE isn't 1, the routine modifies RTL-template RT, adding
275 attribute, named exactly as define_subst, which later will be
276 applied. If such attribute has already been added, then no the
277 routine has no effect. */
278 static void
279 apply_subst_iterator (rtx rt, unsigned int, int value)
281 rtx new_attr;
282 rtvec attrs_vec, new_attrs_vec;
283 int i;
284 /* define_split has no attributes. */
285 if (value == 1 || GET_CODE (rt) == DEFINE_SPLIT)
286 return;
287 gcc_assert (GET_CODE (rt) == DEFINE_INSN
288 || GET_CODE (rt) == DEFINE_INSN_AND_SPLIT
289 || GET_CODE (rt) == DEFINE_EXPAND);
291 int attrs = GET_CODE (rt) == DEFINE_INSN_AND_SPLIT ? 7 : 4;
292 attrs_vec = XVEC (rt, attrs);
294 /* If we've already added attribute 'current_iterator_name', then we
295 have nothing to do now. */
296 if (attrs_vec)
298 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
300 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
301 return;
305 /* Add attribute with subst name - it serves as a mark for
306 define_subst which later would be applied to this pattern. */
307 new_attr = rtx_alloc (SET_ATTR);
308 PUT_CODE (new_attr, SET_ATTR);
309 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
310 XSTR (new_attr, 1) = xstrdup ("yes");
312 if (!attrs_vec)
314 new_attrs_vec = rtvec_alloc (1);
315 new_attrs_vec->elem[0] = new_attr;
317 else
319 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
320 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
321 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
322 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
324 XVEC (rt, attrs) = new_attrs_vec;
327 /* Map subst-attribute ATTR to subst iterator ITER. */
329 static void
330 bind_subst_iter_and_attr (const char *iter, const char *attr)
332 struct subst_attr_to_iter_mapping *value;
333 void **slot;
334 if (!subst_attr_to_iter_map)
335 subst_attr_to_iter_map =
336 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
337 value = XNEW (struct subst_attr_to_iter_mapping);
338 value->attr_name = xstrdup (attr);
339 value->iter_name = xstrdup (iter);
340 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
341 *slot = value;
344 #endif /* #ifdef GENERATOR_FILE */
346 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
348 static char*
349 find_subst_iter_by_attr (const char *attr)
351 char *iter_name = NULL;
352 struct subst_attr_to_iter_mapping *value;
353 value = (struct subst_attr_to_iter_mapping*)
354 htab_find (subst_attr_to_iter_map, &attr);
355 if (value)
356 iter_name = value->iter_name;
357 return iter_name;
360 /* Map attribute string P to its current value. Return null if the attribute
361 isn't known. If ITERATOR_OUT is nonnull, store the associated iterator
362 there. */
364 static struct map_value *
365 map_attr_string (const char *p, mapping **iterator_out = 0)
367 const char *attr;
368 struct mapping *iterator;
369 unsigned int i;
370 struct mapping *m;
371 struct map_value *v;
372 int iterator_name_len;
374 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
375 attribute name. */
376 attr = strchr (p, ':');
377 if (attr == 0)
379 iterator_name_len = -1;
380 attr = p;
382 else
384 iterator_name_len = attr - p;
385 attr++;
388 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
390 /* If an iterator name was specified, check that it matches. */
391 if (iterator_name_len >= 0
392 && (strncmp (p, iterator->name, iterator_name_len) != 0
393 || iterator->name[iterator_name_len] != 0))
394 continue;
396 /* Find the attribute specification. */
397 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
398 if (m)
400 /* In contrast to code/mode/int iterators, attributes of subst
401 iterators are linked to one specific subst-iterator. So, if
402 we are dealing with subst-iterator, we should check if it's
403 the one which linked with the given attribute. */
404 if (iterator->group == &substs)
406 char *iter_name = find_subst_iter_by_attr (attr);
407 if (strcmp (iter_name, iterator->name) != 0)
408 continue;
410 /* Find the attribute value associated with the current
411 iterator value. */
412 for (v = m->values; v; v = v->next)
413 if (v->number == iterator->current_value->number)
415 if (iterator_out)
416 *iterator_out = iterator;
417 return v;
421 return NULL;
424 /* Apply the current iterator values to STRING. Return the new string
425 if any changes were needed, otherwise return STRING itself. */
427 const char *
428 md_reader::apply_iterator_to_string (const char *string)
430 char *base, *copy, *p, *start, *end;
431 struct map_value *v;
433 if (string == 0)
434 return string;
436 base = p = copy = ASTRDUP (string);
437 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
439 p = start + 1;
441 *end = 0;
442 v = map_attr_string (p);
443 *end = '>';
444 if (v == 0)
445 continue;
447 /* Add everything between the last copied byte and the '<',
448 then add in the attribute value. */
449 obstack_grow (&m_string_obstack, base, start - base);
450 obstack_grow (&m_string_obstack, v->string, strlen (v->string));
451 base = end + 1;
453 if (base != copy)
455 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
456 copy = XOBFINISH (&m_string_obstack, char *);
457 copy_md_ptr_loc (copy, string);
458 return copy;
460 return string;
463 /* Return a deep copy of X, substituting the current iterator
464 values into any strings. */
467 md_reader::copy_rtx_for_iterators (rtx original)
469 const char *format_ptr, *p;
470 int i, j;
471 rtx x;
473 if (original == 0)
474 return original;
476 /* Create a shallow copy of ORIGINAL. */
477 x = rtx_alloc (GET_CODE (original));
478 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
480 /* Change each string and recursively change each rtx. */
481 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
482 for (i = 0; format_ptr[i] != 0; i++)
483 switch (format_ptr[i])
485 case 'T':
486 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
487 XTMPL (x, i) = p;
488 break;
490 case 'S':
491 case 's':
492 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
493 XSTR (x, i) = p;
494 break;
496 case 'e':
497 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
498 break;
500 case 'V':
501 case 'E':
502 if (XVEC (original, i))
504 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
505 for (j = 0; j < XVECLEN (x, i); j++)
506 XVECEXP (x, i, j)
507 = copy_rtx_for_iterators (XVECEXP (original, i, j));
509 break;
511 default:
512 break;
514 return x;
517 #ifdef GENERATOR_FILE
519 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
520 has the form "&& ..." (as used in define_insn_and_splits), assume that
521 EXTRA is already satisfied. Empty strings are treated like "true". */
523 static const char *
524 add_condition_to_string (const char *original, const char *extra)
526 if (original != 0 && original[0] == '&' && original[1] == '&')
527 return original;
528 return rtx_reader_ptr->join_c_conditions (original, extra);
531 /* Like add_condition, but applied to all conditions in rtx X. */
533 static void
534 add_condition_to_rtx (rtx x, const char *extra)
536 switch (GET_CODE (x))
538 case DEFINE_INSN:
539 case DEFINE_EXPAND:
540 case DEFINE_SUBST:
541 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
542 break;
544 case DEFINE_SPLIT:
545 case DEFINE_PEEPHOLE:
546 case DEFINE_PEEPHOLE2:
547 case DEFINE_COND_EXEC:
548 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
549 break;
551 case DEFINE_INSN_AND_SPLIT:
552 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
553 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
554 break;
556 default:
557 break;
561 /* Apply the current iterator values to all attribute_uses. */
563 static void
564 apply_attribute_uses (void)
566 struct map_value *v;
567 attribute_use *ause;
568 unsigned int i;
570 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
572 v = map_attr_string (ause->value);
573 if (!v)
574 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
575 ause->group->apply_iterator (ause->x, ause->index,
576 ause->group->find_builtin (v->string));
580 /* A htab_traverse callback for iterators. Add all used iterators
581 to current_iterators. */
583 static int
584 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
586 struct mapping *iterator;
588 iterator = (struct mapping *) *slot;
589 if (iterator->current_value)
590 current_iterators.safe_push (iterator);
591 return 1;
594 /* Return a hash value for overloaded_name UNCAST_ONAME. There shouldn't
595 be many instances of two overloaded_names having the same name but
596 different arguments, so hashing on the name should be good enough in
597 practice. */
599 static hashval_t
600 overloaded_name_hash (const void *uncast_oname)
602 const overloaded_name *oname = (const overloaded_name *) uncast_oname;
603 return htab_hash_string (oname->name);
606 /* Return true if two overloaded_names are similar enough to share
607 the same generated functions. */
609 static int
610 overloaded_name_eq_p (const void *uncast_oname1, const void *uncast_oname2)
612 const overloaded_name *oname1 = (const overloaded_name *) uncast_oname1;
613 const overloaded_name *oname2 = (const overloaded_name *) uncast_oname2;
614 if (strcmp (oname1->name, oname2->name) != 0
615 || oname1->arg_types.length () != oname2->arg_types.length ())
616 return 0;
618 for (unsigned int i = 0; i < oname1->arg_types.length (); ++i)
619 if (strcmp (oname1->arg_types[i], oname2->arg_types[i]) != 0)
620 return 0;
622 return 1;
625 /* Return true if X has an instruction name in XSTR (X, 0). */
627 static bool
628 named_rtx_p (rtx x)
630 switch (GET_CODE (x))
632 case DEFINE_EXPAND:
633 case DEFINE_INSN:
634 case DEFINE_INSN_AND_SPLIT:
635 return true;
637 default:
638 return false;
642 /* Check whether ORIGINAL is a named pattern whose name starts with '@'.
643 If so, return the associated overloaded_name and add the iterator for
644 each argument to ITERATORS. Return null otherwise. */
646 overloaded_name *
647 md_reader::handle_overloaded_name (rtx original, vec<mapping *> *iterators)
649 /* Check for the leading '@'. */
650 if (!named_rtx_p (original) || XSTR (original, 0)[0] != '@')
651 return NULL;
653 /* Remove the '@', so that no other code needs to worry about it. */
654 const char *name = XSTR (original, 0);
655 copy_md_ptr_loc (name + 1, name);
656 name += 1;
657 XSTR (original, 0) = name;
659 /* Build a copy of the name without the '<...>' attribute strings.
660 Add the iterator associated with each such attribute string to ITERATORS
661 and add an associated argument to TMP_ONAME. */
662 char *copy = ASTRDUP (name);
663 char *base = copy, *start, *end;
664 overloaded_name tmp_oname;
665 tmp_oname.arg_types.create (current_iterators.length ());
666 bool pending_underscore_p = false;
667 while ((start = strchr (base, '<')) && (end = strchr (start, '>')))
669 *end = 0;
670 mapping *iterator;
671 if (!map_attr_string (start + 1, &iterator))
672 fatal_with_file_and_line ("unknown iterator `%s'", start + 1);
673 *end = '>';
675 /* Remove a trailing underscore, so that we don't end a name
676 with "_" or turn "_<...>_" into "__". */
677 if (start != base && start[-1] == '_')
679 start -= 1;
680 pending_underscore_p = true;
683 /* Add the text between either the last '>' or the start of
684 the string and this '<'. */
685 obstack_grow (&m_string_obstack, base, start - base);
686 base = end + 1;
688 /* If there's a character we need to keep after the '>', check
689 whether we should prefix it with a previously-dropped '_'. */
690 if (base[0] != 0 && base[0] != '<')
692 if (pending_underscore_p && base[0] != '_')
693 obstack_1grow (&m_string_obstack, '_');
694 pending_underscore_p = false;
697 /* Record an argument for ITERATOR. */
698 iterators->safe_push (iterator);
699 tmp_oname.arg_types.safe_push (iterator->group->type);
701 if (base == copy)
702 fatal_with_file_and_line ("no iterator attributes in name `%s'", name);
704 size_t length = obstack_object_size (&m_string_obstack);
705 if (length == 0)
706 fatal_with_file_and_line ("`%s' only contains iterator attributes", name);
708 /* Get the completed name. */
709 obstack_grow (&m_string_obstack, base, strlen (base) + 1);
710 char *new_name = XOBFINISH (&m_string_obstack, char *);
711 tmp_oname.name = new_name;
713 if (!m_overloads_htab)
714 m_overloads_htab = htab_create (31, overloaded_name_hash,
715 overloaded_name_eq_p, NULL);
717 /* See whether another pattern had the same overload name and list
718 of argument types. Create a new permanent one if not. */
719 void **slot = htab_find_slot (m_overloads_htab, &tmp_oname, INSERT);
720 overloaded_name *oname = (overloaded_name *) *slot;
721 if (!oname)
723 *slot = oname = new overloaded_name;
724 oname->name = tmp_oname.name;
725 oname->arg_types = tmp_oname.arg_types;
726 oname->next = NULL;
727 oname->first_instance = NULL;
728 oname->next_instance_ptr = &oname->first_instance;
730 *m_next_overload_ptr = oname;
731 m_next_overload_ptr = &oname->next;
733 else
735 obstack_free (&m_string_obstack, new_name);
736 tmp_oname.arg_types.release ();
739 return oname;
742 /* Add an instance of ONAME for instruction pattern X. ITERATORS[I]
743 gives the iterator associated with argument I of ONAME. */
745 static void
746 add_overload_instance (overloaded_name *oname, vec<mapping *> iterators, rtx x)
748 /* Create the instance. */
749 overloaded_instance *instance = new overloaded_instance;
750 instance->next = NULL;
751 instance->arg_values.create (oname->arg_types.length ());
752 for (unsigned int i = 0; i < iterators.length (); ++i)
754 int value = iterators[i]->current_value->number;
755 const char *name = iterators[i]->group->get_c_token (value);
756 instance->arg_values.quick_push (name);
758 instance->name = XSTR (x, 0);
759 instance->insn = x;
761 /* Chain it onto the end of ONAME's list. */
762 *oname->next_instance_ptr = instance;
763 oname->next_instance_ptr = &instance->next;
766 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
767 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
769 static void
770 apply_iterators (rtx original, vec<rtx> *queue)
772 unsigned int i;
773 const char *condition;
774 iterator_use *iuse;
775 struct mapping *iterator;
776 struct map_value *v;
777 rtx x;
779 if (iterator_uses.is_empty ())
781 /* Raise an error if any attributes were used. */
782 apply_attribute_uses ();
784 if (named_rtx_p (original) && XSTR (original, 0)[0] == '@')
785 fatal_with_file_and_line ("'@' used without iterators");
787 queue->safe_push (original);
788 return;
791 /* Clear out the iterators from the previous run. */
792 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
793 iterator->current_value = NULL;
794 current_iterators.truncate (0);
796 /* Mark the iterators that we need this time. */
797 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
798 iuse->iterator->current_value = iuse->iterator->values;
800 /* Get the list of iterators that are in use, preserving the
801 definition order within each group. */
802 htab_traverse (modes.iterators, add_current_iterators, NULL);
803 htab_traverse (codes.iterators, add_current_iterators, NULL);
804 htab_traverse (ints.iterators, add_current_iterators, NULL);
805 htab_traverse (substs.iterators, add_current_iterators, NULL);
806 gcc_assert (!current_iterators.is_empty ());
808 /* Check whether this is a '@' overloaded pattern. */
809 auto_vec<mapping *, 16> iterators;
810 overloaded_name *oname
811 = rtx_reader_ptr->handle_overloaded_name (original, &iterators);
813 for (;;)
815 /* Apply the current iterator values. Accumulate a condition to
816 say when the resulting rtx can be used. */
817 condition = "";
818 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
820 if (iuse->iterator->group == &substs)
821 continue;
822 v = iuse->iterator->current_value;
823 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
824 v->number);
825 condition = rtx_reader_ptr->join_c_conditions (condition, v->string);
827 apply_attribute_uses ();
828 x = rtx_reader_ptr->copy_rtx_for_iterators (original);
829 add_condition_to_rtx (x, condition);
831 /* We apply subst iterator after RTL-template is copied, as during
832 subst-iterator processing, we could add an attribute to the
833 RTL-template, and we don't want to do it in the original one. */
834 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
836 v = iuse->iterator->current_value;
837 if (iuse->iterator->group == &substs)
839 iuse->x = x;
840 iuse->index = 0;
841 current_iterator_name = iuse->iterator->name;
842 iuse->iterator->group->apply_iterator (iuse->x, iuse->index,
843 v->number);
847 if (oname)
848 add_overload_instance (oname, iterators, x);
850 /* Add the new rtx to the end of the queue. */
851 queue->safe_push (x);
853 /* Lexicographically increment the iterator value sequence.
854 That is, cycle through iterator values, starting from the right,
855 and stopping when one of them doesn't wrap around. */
856 i = current_iterators.length ();
857 for (;;)
859 if (i == 0)
860 return;
861 i--;
862 iterator = current_iterators[i];
863 iterator->current_value = iterator->current_value->next;
864 if (iterator->current_value)
865 break;
866 iterator->current_value = iterator->values;
870 #endif /* #ifdef GENERATOR_FILE */
872 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
873 of the mapping and GROUP is the group to which it belongs. */
875 static struct mapping *
876 add_mapping (struct iterator_group *group, htab_t table, const char *name)
878 struct mapping *m;
879 void **slot;
881 m = XNEW (struct mapping);
882 m->name = xstrdup (name);
883 m->group = group;
884 m->values = 0;
885 m->current_value = NULL;
887 slot = htab_find_slot (table, m, INSERT);
888 if (*slot != 0)
889 fatal_with_file_and_line ("`%s' already defined", name);
891 *slot = m;
892 return m;
895 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
896 END_PTR points to the current null terminator for the list; return
897 a pointer the new null terminator. */
899 static struct map_value **
900 add_map_value (struct map_value **end_ptr, int number, const char *string)
902 struct map_value *value;
904 value = XNEW (struct map_value);
905 value->next = 0;
906 value->number = number;
907 value->string = string;
909 *end_ptr = value;
910 return &value->next;
913 /* Do one-time initialization of the mode and code attributes. */
915 static void
916 initialize_iterators (void)
918 struct mapping *lower, *upper;
919 struct map_value **lower_ptr, **upper_ptr;
920 char *copy, *p;
921 int i;
923 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
924 modes.iterators = htab_create (13, leading_string_hash,
925 leading_string_eq_p, 0);
926 modes.type = "machine_mode";
927 modes.find_builtin = find_mode;
928 modes.apply_iterator = apply_mode_iterator;
929 modes.get_c_token = get_mode_token;
931 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
932 codes.iterators = htab_create (13, leading_string_hash,
933 leading_string_eq_p, 0);
934 codes.type = "rtx_code";
935 codes.find_builtin = find_code;
936 codes.apply_iterator = apply_code_iterator;
937 codes.get_c_token = get_code_token;
939 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
940 ints.iterators = htab_create (13, leading_string_hash,
941 leading_string_eq_p, 0);
942 ints.type = "int";
943 ints.find_builtin = find_int;
944 ints.apply_iterator = apply_int_iterator;
945 ints.get_c_token = get_int_token;
947 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
948 substs.iterators = htab_create (13, leading_string_hash,
949 leading_string_eq_p, 0);
950 substs.type = "int";
951 substs.find_builtin = find_int; /* We don't use it, anyway. */
952 #ifdef GENERATOR_FILE
953 substs.apply_iterator = apply_subst_iterator;
954 #endif
955 substs.get_c_token = get_int_token;
957 lower = add_mapping (&modes, modes.attrs, "mode");
958 upper = add_mapping (&modes, modes.attrs, "MODE");
959 lower_ptr = &lower->values;
960 upper_ptr = &upper->values;
961 for (i = 0; i < MAX_MACHINE_MODE; i++)
963 copy = xstrdup (GET_MODE_NAME (i));
964 for (p = copy; *p != 0; p++)
965 *p = TOLOWER (*p);
967 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
968 lower_ptr = add_map_value (lower_ptr, i, copy);
971 lower = add_mapping (&codes, codes.attrs, "code");
972 upper = add_mapping (&codes, codes.attrs, "CODE");
973 lower_ptr = &lower->values;
974 upper_ptr = &upper->values;
975 for (i = 0; i < NUM_RTX_CODE; i++)
977 copy = xstrdup (GET_RTX_NAME (i));
978 for (p = copy; *p != 0; p++)
979 *p = TOUPPER (*p);
981 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
982 upper_ptr = add_map_value (upper_ptr, i, copy);
986 /* Provide a version of a function to read a long long if the system does
987 not provide one. */
988 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
989 HOST_WIDE_INT atoll (const char *);
991 HOST_WIDE_INT
992 atoll (const char *p)
994 int neg = 0;
995 HOST_WIDE_INT tmp_wide;
997 while (ISSPACE (*p))
998 p++;
999 if (*p == '-')
1000 neg = 1, p++;
1001 else if (*p == '+')
1002 p++;
1004 tmp_wide = 0;
1005 while (ISDIGIT (*p))
1007 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
1008 if (new_wide < tmp_wide)
1010 /* Return INT_MAX equiv on overflow. */
1011 tmp_wide = HOST_WIDE_INT_M1U >> 1;
1012 break;
1014 tmp_wide = new_wide;
1015 p++;
1018 if (neg)
1019 tmp_wide = -tmp_wide;
1020 return tmp_wide;
1022 #endif
1025 #ifdef GENERATOR_FILE
1026 /* Process a define_conditions directive, starting with the optional
1027 space after the "define_conditions". The directive looks like this:
1029 (define_conditions [
1030 (number "string")
1031 (number "string")
1035 It's not intended to appear in machine descriptions. It is
1036 generated by (the program generated by) genconditions.c, and
1037 slipped in at the beginning of the sequence of MD files read by
1038 most of the other generators. */
1039 void
1040 md_reader::read_conditions ()
1042 int c;
1044 require_char_ws ('[');
1046 while ( (c = read_skip_spaces ()) != ']')
1048 struct md_name name;
1049 char *expr;
1050 int value;
1052 if (c != '(')
1053 fatal_expected_char ('(', c);
1055 read_name (&name);
1056 validate_const_int (name.string);
1057 value = atoi (name.string);
1059 require_char_ws ('"');
1060 expr = read_quoted_string ();
1062 require_char_ws (')');
1064 add_c_test (expr, value);
1067 #endif /* #ifdef GENERATOR_FILE */
1069 static void
1070 validate_const_int (const char *string)
1072 const char *cp;
1073 int valid = 1;
1075 cp = string;
1076 while (*cp && ISSPACE (*cp))
1077 cp++;
1078 if (*cp == '-' || *cp == '+')
1079 cp++;
1080 if (*cp == 0)
1081 valid = 0;
1082 for (; *cp; cp++)
1083 if (! ISDIGIT (*cp))
1085 valid = 0;
1086 break;
1088 if (!valid)
1089 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
1092 static void
1093 validate_const_wide_int (const char *string)
1095 const char *cp;
1096 int valid = 1;
1098 cp = string;
1099 while (*cp && ISSPACE (*cp))
1100 cp++;
1101 /* Skip the leading 0x. */
1102 if (cp[0] == '0' || cp[1] == 'x')
1103 cp += 2;
1104 else
1105 valid = 0;
1106 if (*cp == 0)
1107 valid = 0;
1108 for (; *cp; cp++)
1109 if (! ISXDIGIT (*cp))
1110 valid = 0;
1111 if (!valid)
1112 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
1115 /* Record that X uses iterator ITERATOR. If the use is in an operand
1116 of X, INDEX is the index of that operand, otherwise it is ignored. */
1118 static void
1119 record_iterator_use (struct mapping *iterator, rtx x, unsigned int index)
1121 struct iterator_use iuse = {iterator, x, index};
1122 iterator_uses.safe_push (iuse);
1125 /* Record that X uses attribute VALUE, which must match a built-in
1126 value from group GROUP. If the use is in an operand of X, INDEX
1127 is the index of that operand, otherwise it is ignored. */
1129 static void
1130 record_attribute_use (struct iterator_group *group, rtx x,
1131 unsigned int index, const char *value)
1133 struct attribute_use ause = {group, value, x, index};
1134 attribute_uses.safe_push (ause);
1137 /* Interpret NAME as either a built-in value, iterator or attribute
1138 for group GROUP. X and INDEX are the values to pass to GROUP's
1139 apply_iterator callback. */
1141 void
1142 md_reader::record_potential_iterator_use (struct iterator_group *group,
1143 rtx x, unsigned int index,
1144 const char *name)
1146 struct mapping *m;
1147 size_t len;
1149 len = strlen (name);
1150 if (name[0] == '<' && name[len - 1] == '>')
1152 /* Copy the attribute string into permanent storage, without the
1153 angle brackets around it. */
1154 obstack_grow0 (&m_string_obstack, name + 1, len - 2);
1155 record_attribute_use (group, x, index,
1156 XOBFINISH (&m_string_obstack, char *));
1158 else
1160 m = (struct mapping *) htab_find (group->iterators, &name);
1161 if (m != 0)
1162 record_iterator_use (m, x, index);
1163 else
1164 group->apply_iterator (x, index, group->find_builtin (name));
1168 #ifdef GENERATOR_FILE
1170 /* Finish reading a declaration of the form:
1172 (define... <name> [<value1> ... <valuen>])
1174 from the MD file, where each <valuei> is either a bare symbol name or a
1175 "(<name> <string>)" pair. The "(define..." part has already been read.
1177 Represent the declaration as a "mapping" structure; add it to TABLE
1178 (which belongs to GROUP) and return it. */
1180 struct mapping *
1181 md_reader::read_mapping (struct iterator_group *group, htab_t table)
1183 struct md_name name;
1184 struct mapping *m;
1185 struct map_value **end_ptr;
1186 const char *string;
1187 int number, c;
1189 /* Read the mapping name and create a structure for it. */
1190 read_name (&name);
1191 m = add_mapping (group, table, name.string);
1193 require_char_ws ('[');
1195 /* Read each value. */
1196 end_ptr = &m->values;
1197 c = read_skip_spaces ();
1200 if (c != '(')
1202 /* A bare symbol name that is implicitly paired to an
1203 empty string. */
1204 unread_char (c);
1205 read_name (&name);
1206 string = "";
1208 else
1210 /* A "(name string)" pair. */
1211 read_name (&name);
1212 string = read_string (false);
1213 require_char_ws (')');
1215 number = group->find_builtin (name.string);
1216 end_ptr = add_map_value (end_ptr, number, string);
1217 c = read_skip_spaces ();
1219 while (c != ']');
1221 return m;
1224 /* For iterator with name ATTR_NAME generate define_attr with values
1225 'yes' and 'no'. This attribute is used to mark templates to which
1226 define_subst ATTR_NAME should be applied. This attribute is set and
1227 defined implicitly and automatically. */
1228 static void
1229 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
1231 rtx const_str, return_rtx;
1233 return_rtx = rtx_alloc (DEFINE_ATTR);
1234 PUT_CODE (return_rtx, DEFINE_ATTR);
1236 const_str = rtx_alloc (CONST_STRING);
1237 PUT_CODE (const_str, CONST_STRING);
1238 XSTR (const_str, 0) = xstrdup ("no");
1240 XSTR (return_rtx, 0) = xstrdup (attr_name);
1241 XSTR (return_rtx, 1) = xstrdup ("no,yes");
1242 XEXP (return_rtx, 2) = const_str;
1244 queue->safe_push (return_rtx);
1247 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1248 ATTR_OPERANDS and places it to QUEUE. */
1249 static void
1250 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
1252 rtx return_rtx;
1253 int i;
1255 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
1256 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
1258 for (i = 0; i < 4; i++)
1259 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
1261 queue->safe_push (return_rtx);
1264 /* Read define_subst_attribute construction. It has next form:
1265 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1266 Attribute is substituted with value1 when no subst is applied and with
1267 value2 in the opposite case.
1268 Attributes are added to SUBST_ATTRS_TABLE.
1269 In case the iterator is encountered for the first time, it's added to
1270 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1272 static void
1273 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
1274 vec<rtx> *queue)
1276 struct mapping *m;
1277 struct map_value **end_ptr;
1278 const char *attr_operands[4];
1279 int i;
1281 for (i = 0; i < 4; i++)
1282 attr_operands[i] = rtx_reader_ptr->read_string (false);
1284 add_define_subst_attr (attr_operands, queue);
1286 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1288 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1289 if (!m)
1291 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1292 end_ptr = &m->values;
1293 end_ptr = add_map_value (end_ptr, 1, "");
1294 end_ptr = add_map_value (end_ptr, 2, "");
1296 add_define_attr_for_define_subst (attr_operands[1], queue);
1299 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1300 end_ptr = &m->values;
1301 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1302 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
1305 /* Check newly-created code iterator ITERATOR to see whether every code has the
1306 same format. */
1308 static void
1309 check_code_iterator (struct mapping *iterator)
1311 struct map_value *v;
1312 enum rtx_code bellwether;
1314 bellwether = (enum rtx_code) iterator->values->number;
1315 for (v = iterator->values->next; v != 0; v = v->next)
1316 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1317 fatal_with_file_and_line ("code iterator `%s' combines "
1318 "`%s' and `%s', which have different "
1319 "rtx formats", iterator->name,
1320 GET_RTX_NAME (bellwether),
1321 GET_RTX_NAME (v->number));
1324 /* Check that all values of attribute ATTR are rtx codes that have a
1325 consistent format. Return a representative code. */
1327 static rtx_code
1328 check_code_attribute (mapping *attr)
1330 rtx_code bellwether = UNKNOWN;
1331 for (map_value *v = attr->values; v != 0; v = v->next)
1333 rtx_code code = maybe_find_code (v->string);
1334 if (code == UNKNOWN)
1335 fatal_with_file_and_line ("code attribute `%s' contains "
1336 "unrecognized rtx code `%s'",
1337 attr->name, v->string);
1338 if (bellwether == UNKNOWN)
1339 bellwether = code;
1340 else if (strcmp (GET_RTX_FORMAT (bellwether),
1341 GET_RTX_FORMAT (code)) != 0)
1342 fatal_with_file_and_line ("code attribute `%s' combines "
1343 "`%s' and `%s', which have different "
1344 "rtx formats", attr->name,
1345 GET_RTX_NAME (bellwether),
1346 GET_RTX_NAME (code));
1348 return bellwether;
1351 /* Read an rtx-related declaration from the MD file, given that it
1352 starts with directive name RTX_NAME. Return true if it expands to
1353 one or more rtxes (as defined by rtx.def). When returning true,
1354 store the list of rtxes as an EXPR_LIST in *X. */
1356 bool
1357 rtx_reader::read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1359 /* Handle various rtx-related declarations that aren't themselves
1360 encoded as rtxes. */
1361 if (strcmp (rtx_name, "define_conditions") == 0)
1363 read_conditions ();
1364 return false;
1366 if (strcmp (rtx_name, "define_mode_attr") == 0)
1368 read_mapping (&modes, modes.attrs);
1369 return false;
1371 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1373 read_mapping (&modes, modes.iterators);
1374 return false;
1376 if (strcmp (rtx_name, "define_code_attr") == 0)
1378 read_mapping (&codes, codes.attrs);
1379 return false;
1381 if (strcmp (rtx_name, "define_code_iterator") == 0)
1383 check_code_iterator (read_mapping (&codes, codes.iterators));
1384 return false;
1386 if (strcmp (rtx_name, "define_int_attr") == 0)
1388 read_mapping (&ints, ints.attrs);
1389 return false;
1391 if (strcmp (rtx_name, "define_int_iterator") == 0)
1393 read_mapping (&ints, ints.iterators);
1394 return false;
1396 if (strcmp (rtx_name, "define_subst_attr") == 0)
1398 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1400 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1401 TRUE to process it. */
1402 return true;
1405 apply_iterators (rtx_reader_ptr->read_rtx_code (rtx_name), rtxen);
1406 iterator_uses.truncate (0);
1407 attribute_uses.truncate (0);
1409 return true;
1412 #endif /* #ifdef GENERATOR_FILE */
1414 /* Do one-time initialization. */
1416 static void
1417 one_time_initialization (void)
1419 static bool initialized = false;
1421 if (!initialized)
1423 initialize_iterators ();
1424 initialized = true;
1428 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1429 consuming the terminator character if CONSUME_TERMINATOR is true.
1430 Return all characters before the terminator as an allocated buffer. */
1432 char *
1433 rtx_reader::read_until (const char *terminator_chars, bool consume_terminator)
1435 int ch = read_skip_spaces ();
1436 unread_char (ch);
1437 auto_vec<char> buf;
1438 while (1)
1440 ch = read_char ();
1441 if (strchr (terminator_chars, ch))
1443 if (!consume_terminator)
1444 unread_char (ch);
1445 break;
1447 buf.safe_push (ch);
1449 buf.safe_push ('\0');
1450 return xstrdup (buf.address ());
1453 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1455 static void
1456 read_flags (rtx return_rtx)
1458 while (1)
1460 int ch = read_char ();
1461 if (ch != '/')
1463 unread_char (ch);
1464 break;
1467 int flag_char = read_char ();
1468 switch (flag_char)
1470 case 's':
1471 RTX_FLAG (return_rtx, in_struct) = 1;
1472 break;
1473 case 'v':
1474 RTX_FLAG (return_rtx, volatil) = 1;
1475 break;
1476 case 'u':
1477 RTX_FLAG (return_rtx, unchanging) = 1;
1478 break;
1479 case 'f':
1480 RTX_FLAG (return_rtx, frame_related) = 1;
1481 break;
1482 case 'j':
1483 RTX_FLAG (return_rtx, jump) = 1;
1484 break;
1485 case 'c':
1486 RTX_FLAG (return_rtx, call) = 1;
1487 break;
1488 case 'i':
1489 RTX_FLAG (return_rtx, return_val) = 1;
1490 break;
1491 default:
1492 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char);
1497 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1498 or fail if STRING isn't recognized. */
1500 static int
1501 parse_reg_note_name (const char *string)
1503 for (int i = 0; i < REG_NOTE_MAX; i++)
1504 if (strcmp (string, GET_REG_NOTE_NAME (i)) == 0)
1505 return i;
1506 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string);
1509 /* Allocate an rtx for code NAME. If NAME is a code iterator or code
1510 attribute, record its use for later and use one of its possible
1511 values as an interim rtx code. */
1514 rtx_reader::rtx_alloc_for_name (const char *name)
1516 #ifdef GENERATOR_FILE
1517 size_t len = strlen (name);
1518 if (name[0] == '<' && name[len - 1] == '>')
1520 /* Copy the attribute string into permanent storage, without the
1521 angle brackets around it. */
1522 obstack *strings = get_string_obstack ();
1523 obstack_grow0 (strings, name + 1, len - 2);
1524 char *deferred_name = XOBFINISH (strings, char *);
1526 /* Find the name of the attribute. */
1527 const char *attr = strchr (deferred_name, ':');
1528 if (!attr)
1529 attr = deferred_name;
1531 /* Find the attribute itself. */
1532 mapping *m = (mapping *) htab_find (codes.attrs, &attr);
1533 if (!m)
1534 fatal_with_file_and_line ("unknown code attribute `%s'", attr);
1536 /* Pick the first possible code for now, and record the attribute
1537 use for later. */
1538 rtx x = rtx_alloc (check_code_attribute (m));
1539 record_attribute_use (&codes, x, 0, deferred_name);
1540 return x;
1543 mapping *iterator = (mapping *) htab_find (codes.iterators, &name);
1544 if (iterator != 0)
1546 /* Pick the first possible code for now, and record the iterator
1547 use for later. */
1548 rtx x = rtx_alloc (rtx_code (iterator->values->number));
1549 record_iterator_use (iterator, x, 0);
1550 return x;
1552 #endif
1554 return rtx_alloc (rtx_code (codes.find_builtin (name)));
1557 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1558 either an rtx code or a code iterator. Parse the rest of the rtx and
1559 return it. */
1562 rtx_reader::read_rtx_code (const char *code_name)
1564 RTX_CODE code;
1565 const char *format_ptr;
1566 struct md_name name;
1567 rtx return_rtx;
1568 int c;
1569 long reuse_id = -1;
1571 /* Linked list structure for making RTXs: */
1572 struct rtx_list
1574 struct rtx_list *next;
1575 rtx value; /* Value of this node. */
1578 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1579 if (ISDIGIT (code_name[0]))
1581 reuse_id = atoi (code_name);
1582 while (char ch = *code_name++)
1583 if (ch == '|')
1584 break;
1587 /* Handle "reuse_rtx". */
1588 if (strcmp (code_name, "reuse_rtx") == 0)
1590 read_name (&name);
1591 unsigned idx = atoi (name.string);
1592 /* Look it up by ID. */
1593 gcc_assert (idx < m_reuse_rtx_by_id.length ());
1594 return_rtx = m_reuse_rtx_by_id[idx];
1595 return return_rtx;
1598 /* If we end up with an insn expression then we free this space below. */
1599 return_rtx = rtx_alloc_for_name (code_name);
1600 code = GET_CODE (return_rtx);
1601 format_ptr = GET_RTX_FORMAT (code);
1602 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1603 PUT_CODE (return_rtx, code);
1605 if (reuse_id != -1)
1607 /* Store away for later reuse. */
1608 m_reuse_rtx_by_id.safe_grow_cleared (reuse_id + 1);
1609 m_reuse_rtx_by_id[reuse_id] = return_rtx;
1612 /* Check for flags. */
1613 read_flags (return_rtx);
1615 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1616 if ((GET_CODE (return_rtx) == EXPR_LIST
1617 || GET_CODE (return_rtx) == INSN_LIST
1618 || GET_CODE (return_rtx) == INT_LIST)
1619 && !m_in_call_function_usage)
1621 char ch = read_char ();
1622 if (ch == ':')
1624 read_name (&name);
1625 PUT_MODE_RAW (return_rtx,
1626 (machine_mode)parse_reg_note_name (name.string));
1628 else
1629 unread_char (ch);
1632 /* If what follows is `: mode ', read it and
1633 store the mode in the rtx. */
1635 c = read_skip_spaces ();
1636 if (c == ':')
1638 read_name (&name);
1639 record_potential_iterator_use (&modes, return_rtx, 0, name.string);
1641 else
1642 unread_char (c);
1644 if (INSN_CHAIN_CODE_P (code))
1646 read_name (&name);
1647 INSN_UID (return_rtx) = atoi (name.string);
1650 /* Use the format_ptr to parse the various operands of this rtx. */
1651 for (int idx = 0; format_ptr[idx] != 0; idx++)
1652 return_rtx = read_rtx_operand (return_rtx, idx);
1654 /* Handle any additional information that after the regular fields
1655 (e.g. when parsing function dumps). */
1656 handle_any_trailing_information (return_rtx);
1658 if (CONST_WIDE_INT_P (return_rtx))
1660 read_name (&name);
1661 validate_const_wide_int (name.string);
1663 const char *s = name.string;
1664 int len;
1665 int index = 0;
1666 int gs = HOST_BITS_PER_WIDE_INT/4;
1667 int pos;
1668 char * buf = XALLOCAVEC (char, gs + 1);
1669 unsigned HOST_WIDE_INT wi;
1670 int wlen;
1672 /* Skip the leading spaces. */
1673 while (*s && ISSPACE (*s))
1674 s++;
1676 /* Skip the leading 0x. */
1677 gcc_assert (s[0] == '0');
1678 gcc_assert (s[1] == 'x');
1679 s += 2;
1681 len = strlen (s);
1682 pos = len - gs;
1683 wlen = (len + gs - 1) / gs; /* Number of words needed */
1685 return_rtx = const_wide_int_alloc (wlen);
1687 while (pos > 0)
1689 #if HOST_BITS_PER_WIDE_INT == 64
1690 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1691 #else
1692 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1693 #endif
1694 CWI_ELT (return_rtx, index++) = wi;
1695 pos -= gs;
1697 strncpy (buf, s, gs - pos);
1698 buf [gs - pos] = 0;
1699 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1700 CWI_ELT (return_rtx, index++) = wi;
1701 /* TODO: After reading, do we want to canonicalize with:
1702 value = lookup_const_wide_int (value); ? */
1706 c = read_skip_spaces ();
1707 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1708 arbitrary number of arguments for them. */
1709 if (c == '('
1710 && (GET_CODE (return_rtx) == AND
1711 || GET_CODE (return_rtx) == IOR))
1712 return read_rtx_variadic (return_rtx);
1714 unread_char (c);
1715 return return_rtx;
1718 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1719 based on the corresponding format character within GET_RTX_FORMAT
1720 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1721 This is a virtual function, so that function_reader can override
1722 some parsing, and potentially return a different rtx. */
1725 rtx_reader::read_rtx_operand (rtx return_rtx, int idx)
1727 RTX_CODE code = GET_CODE (return_rtx);
1728 const char *format_ptr = GET_RTX_FORMAT (code);
1729 int c;
1730 struct md_name name;
1732 switch (format_ptr[idx])
1734 /* 0 means a field for internal use only.
1735 Don't expect it to be present in the input. */
1736 case '0':
1737 if (code == REG)
1738 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1739 break;
1741 case 'e':
1742 XEXP (return_rtx, idx) = read_nested_rtx ();
1743 break;
1745 case 'u':
1746 XEXP (return_rtx, idx) = read_nested_rtx ();
1747 break;
1749 case 'V':
1750 /* 'V' is an optional vector: if a closeparen follows,
1751 just store NULL for this element. */
1752 c = read_skip_spaces ();
1753 unread_char (c);
1754 if (c == ')')
1756 XVEC (return_rtx, idx) = 0;
1757 break;
1759 /* Now process the vector. */
1760 /* FALLTHRU */
1762 case 'E':
1764 /* Obstack to store scratch vector in. */
1765 struct obstack vector_stack;
1766 int list_counter = 0;
1767 rtvec return_vec = NULL_RTVEC;
1768 rtx saved_rtx = NULL_RTX;
1770 require_char_ws ('[');
1772 /* Add expressions to a list, while keeping a count. */
1773 obstack_init (&vector_stack);
1774 while ((c = read_skip_spaces ()) && c != ']')
1776 if (c == EOF)
1777 fatal_expected_char (']', c);
1778 unread_char (c);
1780 rtx value;
1781 int repeat_count = 1;
1782 if (c == 'r')
1784 /* Process "repeated xN" directive. */
1785 read_name (&name);
1786 if (strcmp (name.string, "repeated"))
1787 fatal_with_file_and_line ("invalid directive \"%s\"\n",
1788 name.string);
1789 read_name (&name);
1790 if (!sscanf (name.string, "x%d", &repeat_count))
1791 fatal_with_file_and_line ("invalid repeat count \"%s\"\n",
1792 name.string);
1794 /* We already saw one of the instances. */
1795 repeat_count--;
1796 value = saved_rtx;
1798 else
1799 value = read_nested_rtx ();
1801 for (; repeat_count > 0; repeat_count--)
1803 list_counter++;
1804 obstack_ptr_grow (&vector_stack, value);
1806 saved_rtx = value;
1808 if (list_counter > 0)
1810 return_vec = rtvec_alloc (list_counter);
1811 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1812 list_counter * sizeof (rtx));
1814 else if (format_ptr[idx] == 'E')
1815 fatal_with_file_and_line ("vector must have at least one element");
1816 XVEC (return_rtx, idx) = return_vec;
1817 obstack_free (&vector_stack, NULL);
1818 /* close bracket gotten */
1820 break;
1822 case 'S':
1823 case 'T':
1824 case 's':
1826 char *stringbuf;
1827 int star_if_braced;
1829 c = read_skip_spaces ();
1830 unread_char (c);
1831 if (c == ')')
1833 /* 'S' fields are optional and should be NULL if no string
1834 was given. Also allow normal 's' and 'T' strings to be
1835 omitted, treating them in the same way as empty strings. */
1836 XSTR (return_rtx, idx) = (format_ptr[idx] == 'S' ? NULL : "");
1837 break;
1840 /* The output template slot of a DEFINE_INSN,
1841 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1842 gets a star inserted as its first character, if it is
1843 written with a brace block instead of a string constant. */
1844 star_if_braced = (format_ptr[idx] == 'T');
1846 stringbuf = read_string (star_if_braced);
1847 if (!stringbuf)
1848 break;
1850 #ifdef GENERATOR_FILE
1851 /* For insn patterns, we want to provide a default name
1852 based on the file and line, like "*foo.md:12", if the
1853 given name is blank. These are only for define_insn and
1854 define_insn_and_split, to aid debugging. */
1855 if (*stringbuf == '\0'
1856 && idx == 0
1857 && (GET_CODE (return_rtx) == DEFINE_INSN
1858 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1860 struct obstack *string_obstack = get_string_obstack ();
1861 char line_name[20];
1862 const char *read_md_filename = get_filename ();
1863 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1864 const char *slash;
1865 for (slash = fn; *slash; slash ++)
1866 if (*slash == '/' || *slash == '\\' || *slash == ':')
1867 fn = slash + 1;
1868 obstack_1grow (string_obstack, '*');
1869 obstack_grow (string_obstack, fn, strlen (fn));
1870 sprintf (line_name, ":%d", get_lineno ());
1871 obstack_grow (string_obstack, line_name, strlen (line_name)+1);
1872 stringbuf = XOBFINISH (string_obstack, char *);
1875 /* Find attr-names in the string. */
1876 char *str;
1877 char *start, *end, *ptr;
1878 char tmpstr[256];
1879 ptr = &tmpstr[0];
1880 end = stringbuf;
1881 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1883 if ((end - start - 1 > 0)
1884 && (end - start - 1 < (int)sizeof (tmpstr)))
1886 strncpy (tmpstr, start+1, end-start-1);
1887 tmpstr[end-start-1] = 0;
1888 end++;
1890 else
1891 break;
1892 struct mapping *m
1893 = (struct mapping *) htab_find (substs.attrs, &ptr);
1894 if (m != 0)
1896 /* Here we should find linked subst-iter. */
1897 str = find_subst_iter_by_attr (ptr);
1898 if (str)
1899 m = (struct mapping *) htab_find (substs.iterators, &str);
1900 else
1901 m = 0;
1903 if (m != 0)
1904 record_iterator_use (m, return_rtx, 0);
1906 #endif /* #ifdef GENERATOR_FILE */
1908 const char *string_ptr = finalize_string (stringbuf);
1910 if (star_if_braced)
1911 XTMPL (return_rtx, idx) = string_ptr;
1912 else
1913 XSTR (return_rtx, idx) = string_ptr;
1915 break;
1917 case 'w':
1919 HOST_WIDE_INT tmp_wide;
1920 read_name (&name);
1921 validate_const_int (name.string);
1922 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1923 tmp_wide = atoi (name.string);
1924 #else
1925 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1926 tmp_wide = atol (name.string);
1927 #else
1928 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1929 But prefer not to use our hand-rolled function above either. */
1930 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1931 tmp_wide = atoll (name.string);
1932 #else
1933 tmp_wide = atoq (name.string);
1934 #endif
1935 #endif
1936 #endif
1937 XWINT (return_rtx, idx) = tmp_wide;
1939 break;
1941 case 'i':
1942 case 'n':
1943 case 'p':
1944 /* Can be an iterator or an integer constant. */
1945 read_name (&name);
1946 record_potential_iterator_use (&ints, return_rtx, idx, name.string);
1947 break;
1949 case 'r':
1950 read_name (&name);
1951 validate_const_int (name.string);
1952 set_regno_raw (return_rtx, atoi (name.string), 1);
1953 REG_ATTRS (return_rtx) = NULL;
1954 break;
1956 default:
1957 gcc_unreachable ();
1960 return return_rtx;
1963 /* Read a nested rtx construct from the MD file and return it. */
1966 rtx_reader::read_nested_rtx ()
1968 struct md_name name;
1969 rtx return_rtx;
1971 /* In compact dumps, trailing "(nil)" values can be omitted.
1972 Handle such dumps. */
1973 if (peek_char () == ')')
1974 return NULL_RTX;
1976 require_char_ws ('(');
1978 read_name (&name);
1979 if (strcmp (name.string, "nil") == 0)
1980 return_rtx = NULL;
1981 else
1982 return_rtx = read_rtx_code (name.string);
1984 require_char_ws (')');
1986 return_rtx = postprocess (return_rtx);
1988 return return_rtx;
1991 /* Mutually recursive subroutine of read_rtx which reads
1992 (thing x1 x2 x3 ...) and produces RTL as if
1993 (thing x1 (thing x2 (thing x3 ...))) had been written.
1994 When called, FORM is (thing x1 x2), and the file position
1995 is just past the leading parenthesis of x3. Only works
1996 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1998 rtx_reader::read_rtx_variadic (rtx form)
2000 char c = '(';
2001 rtx p = form, q;
2005 unread_char (c);
2007 q = rtx_alloc (GET_CODE (p));
2008 PUT_MODE (q, GET_MODE (p));
2010 XEXP (q, 0) = XEXP (p, 1);
2011 XEXP (q, 1) = read_nested_rtx ();
2013 XEXP (p, 1) = q;
2014 p = q;
2015 c = read_skip_spaces ();
2017 while (c == '(');
2018 unread_char (c);
2019 return form;
2022 /* Constructor for class rtx_reader. */
2024 rtx_reader::rtx_reader (bool compact)
2025 : md_reader (compact),
2026 m_in_call_function_usage (false)
2028 /* Set the global singleton pointer. */
2029 rtx_reader_ptr = this;
2031 one_time_initialization ();
2034 /* Destructor for class rtx_reader. */
2036 rtx_reader::~rtx_reader ()
2038 /* Clear the global singleton pointer. */
2039 rtx_reader_ptr = NULL;