2013-10-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / read-rtl.c
blobcd58b1f823815a59fbe764982fc5585a3d718b13
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
22 /* Disable rtl checking; it conflicts with the iterator handling. */
23 #undef ENABLE_RTL_CHECKING
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "obstack.h"
30 #include "hashtab.h"
31 #include "read-md.h"
32 #include "gensupport.h"
34 /* One element in a singly-linked list of (integer, string) pairs. */
35 struct map_value {
36 struct map_value *next;
37 int number;
38 const char *string;
41 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
42 The integers are iterator values; the strings are either C conditions
43 or attribute values. */
44 struct mapping {
45 /* The name of the iterator or attribute. */
46 const char *name;
48 /* The group (modes or codes) to which the iterator or attribute belongs. */
49 struct iterator_group *group;
51 /* The list of (integer, string) pairs. */
52 struct map_value *values;
54 /* For iterators, records the current value of the iterator. */
55 struct map_value *current_value;
58 /* Vector definitions for the above. */
59 typedef struct mapping *mapping_ptr;
61 /* A structure for abstracting the common parts of iterators. */
62 struct iterator_group {
63 /* Tables of "mapping" structures, one for attributes and one for
64 iterators. */
65 htab_t attrs, iterators;
67 /* Treat the given string as the name of a standard mode, etc., and
68 return its integer value. */
69 int (*find_builtin) (const char *);
71 /* Make the given pointer use the given iterator value. */
72 void (*apply_iterator) (void *, int);
75 /* Records one use of an iterator. */
76 struct iterator_use {
77 /* The iterator itself. */
78 struct mapping *iterator;
80 /* The location of the use, as passed to the apply_iterator callback. */
81 void *ptr;
84 /* Vector definitions for the above. */
85 typedef struct iterator_use iterator_use;
87 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
88 in a non-string rtx field. */
89 struct attribute_use {
90 /* The group that describes the use site. */
91 struct iterator_group *group;
93 /* The name of the attribute, possibly with an "iterator:" prefix. */
94 const char *value;
96 /* The location of the use, as passed to GROUP's apply_iterator callback. */
97 void *ptr;
100 /* Vector definitions for the above. */
101 typedef struct attribute_use attribute_use;
103 /* This struct is used to link subst_attr named ATTR_NAME with
104 corresponding define_subst named ITER_NAME. */
105 struct subst_attr_to_iter_mapping
107 char *attr_name;
108 char *iter_name;
111 /* Hash-table to store links between subst-attributes and
112 define_substs. */
113 htab_t subst_attr_to_iter_map = NULL;
114 /* This global stores name of subst-iterator which is currently being
115 processed. */
116 const char *current_iterator_name;
118 static void validate_const_int (const char *);
119 static rtx read_rtx_code (const char *);
120 static rtx read_nested_rtx (void);
121 static rtx read_rtx_variadic (rtx);
123 /* The mode and code iterator structures. */
124 static struct iterator_group modes, codes, ints, substs;
126 /* All iterators used in the current rtx. */
127 static vec<mapping_ptr> current_iterators;
129 /* The list of all iterator uses in the current rtx. */
130 static vec<iterator_use> iterator_uses;
132 /* The list of all attribute uses in the current rtx. */
133 static vec<attribute_use> attribute_uses;
135 /* Implementations of the iterator_group callbacks for modes. */
137 static int
138 find_mode (const char *name)
140 int i;
142 for (i = 0; i < NUM_MACHINE_MODES; i++)
143 if (strcmp (GET_MODE_NAME (i), name) == 0)
144 return i;
146 fatal_with_file_and_line ("unknown mode `%s'", name);
149 static void
150 apply_mode_iterator (void *loc, int mode)
152 PUT_MODE ((rtx) loc, (enum machine_mode) mode);
155 /* Implementations of the iterator_group callbacks for codes. */
157 static int
158 find_code (const char *name)
160 int i;
162 for (i = 0; i < NUM_RTX_CODE; i++)
163 if (strcmp (GET_RTX_NAME (i), name) == 0)
164 return i;
166 fatal_with_file_and_line ("unknown rtx code `%s'", name);
169 static void
170 apply_code_iterator (void *loc, int code)
172 PUT_CODE ((rtx) loc, (enum rtx_code) code);
175 /* Implementations of the iterator_group callbacks for ints. */
177 /* Since GCC does not construct a table of valid constants,
178 we have to accept any int as valid. No cross-checking can
179 be done. */
181 static int
182 find_int (const char *name)
184 validate_const_int (name);
185 return atoi (name);
188 static void
189 apply_int_iterator (void *loc, int value)
191 *(int *)loc = value;
194 /* This routine adds attribute or does nothing depending on VALUE. When
195 VALUE is 1, it does nothing - the first duplicate of original
196 template is kept untouched when it's subjected to a define_subst.
197 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
198 attribute, named exactly as define_subst, which later will be
199 applied. If such attribute has already been added, then no the
200 routine has no effect. */
201 static void
202 apply_subst_iterator (void *loc, int value)
204 rtx rt = (rtx)loc;
205 rtx new_attr;
206 rtvec attrs_vec, new_attrs_vec;
207 int i;
208 if (value == 1)
209 return;
210 gcc_assert (GET_CODE (rt) == DEFINE_INSN
211 || GET_CODE (rt) == DEFINE_EXPAND);
213 attrs_vec = XVEC (rt, 4);
215 /* If we've already added attribute 'current_iterator_name', then we
216 have nothing to do now. */
217 if (attrs_vec)
219 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
221 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
222 return;
226 /* Add attribute with subst name - it serves as a mark for
227 define_subst which later would be applied to this pattern. */
228 new_attr = rtx_alloc (SET_ATTR);
229 PUT_CODE (new_attr, SET_ATTR);
230 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
231 XSTR (new_attr, 1) = xstrdup ("yes");
233 if (!attrs_vec)
235 new_attrs_vec = rtvec_alloc (1);
236 new_attrs_vec->elem[0] = new_attr;
238 else
240 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
241 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
242 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
243 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
245 XVEC (rt, 4) = new_attrs_vec;
248 /* Map subst-attribute ATTR to subst iterator ITER. */
250 static void
251 bind_subst_iter_and_attr (const char *iter, const char *attr)
253 struct subst_attr_to_iter_mapping *value;
254 void **slot;
255 if (!subst_attr_to_iter_map)
256 subst_attr_to_iter_map =
257 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
258 value = XNEW (struct subst_attr_to_iter_mapping);
259 value->attr_name = xstrdup (attr);
260 value->iter_name = xstrdup (iter);
261 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
262 *slot = value;
265 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
267 static char*
268 find_subst_iter_by_attr (const char *attr)
270 char *iter_name = NULL;
271 struct subst_attr_to_iter_mapping *value;
272 value = (struct subst_attr_to_iter_mapping*)
273 htab_find (subst_attr_to_iter_map, &attr);
274 if (value)
275 iter_name = value->iter_name;
276 return iter_name;
279 /* Map attribute string P to its current value. Return null if the attribute
280 isn't known. */
282 static struct map_value *
283 map_attr_string (const char *p)
285 const char *attr;
286 struct mapping *iterator;
287 unsigned int i;
288 struct mapping *m;
289 struct map_value *v;
290 int iterator_name_len;
292 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
293 attribute name. */
294 attr = strchr (p, ':');
295 if (attr == 0)
297 iterator_name_len = -1;
298 attr = p;
300 else
302 iterator_name_len = attr - p;
303 attr++;
306 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
308 /* If an iterator name was specified, check that it matches. */
309 if (iterator_name_len >= 0
310 && (strncmp (p, iterator->name, iterator_name_len) != 0
311 || iterator->name[iterator_name_len] != 0))
312 continue;
314 /* Find the attribute specification. */
315 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
316 if (m)
318 /* In contrast to code/mode/int iterators, attributes of subst
319 iterators are linked to one specific subst-iterator. So, if
320 we are dealing with subst-iterator, we should check if it's
321 the one which linked with the given attribute. */
322 if (iterator->group == &substs)
324 char *iter_name = find_subst_iter_by_attr (attr);
325 if (strcmp (iter_name, iterator->name) != 0)
326 continue;
328 /* Find the attribute value associated with the current
329 iterator value. */
330 for (v = m->values; v; v = v->next)
331 if (v->number == iterator->current_value->number)
332 return v;
335 return NULL;
338 /* Apply the current iterator values to STRING. Return the new string
339 if any changes were needed, otherwise return STRING itself. */
341 static const char *
342 apply_iterator_to_string (const char *string)
344 char *base, *copy, *p, *start, *end;
345 struct map_value *v;
347 if (string == 0)
348 return string;
350 base = p = copy = ASTRDUP (string);
351 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
353 p = start + 1;
355 *end = 0;
356 v = map_attr_string (p);
357 *end = '>';
358 if (v == 0)
359 continue;
361 /* Add everything between the last copied byte and the '<',
362 then add in the attribute value. */
363 obstack_grow (&string_obstack, base, start - base);
364 obstack_grow (&string_obstack, v->string, strlen (v->string));
365 base = end + 1;
367 if (base != copy)
369 obstack_grow (&string_obstack, base, strlen (base) + 1);
370 copy = XOBFINISH (&string_obstack, char *);
371 copy_md_ptr_loc (copy, string);
372 return copy;
374 return string;
377 /* Return a deep copy of X, substituting the current iterator
378 values into any strings. */
380 static rtx
381 copy_rtx_for_iterators (rtx original)
383 const char *format_ptr;
384 int i, j;
385 rtx x;
387 if (original == 0)
388 return original;
390 /* Create a shallow copy of ORIGINAL. */
391 x = rtx_alloc (GET_CODE (original));
392 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
394 /* Change each string and recursively change each rtx. */
395 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
396 for (i = 0; format_ptr[i] != 0; i++)
397 switch (format_ptr[i])
399 case 'T':
400 XTMPL (x, i) = apply_iterator_to_string (XTMPL (x, i));
401 break;
403 case 'S':
404 case 's':
405 XSTR (x, i) = apply_iterator_to_string (XSTR (x, i));
406 break;
408 case 'e':
409 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
410 break;
412 case 'V':
413 case 'E':
414 if (XVEC (original, i))
416 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
417 for (j = 0; j < XVECLEN (x, i); j++)
418 XVECEXP (x, i, j)
419 = copy_rtx_for_iterators (XVECEXP (original, i, j));
421 break;
423 default:
424 break;
426 return x;
429 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
430 has the form "&& ..." (as used in define_insn_and_splits), assume that
431 EXTRA is already satisfied. Empty strings are treated like "true". */
433 static const char *
434 add_condition_to_string (const char *original, const char *extra)
436 if (original != 0 && original[0] == '&' && original[1] == '&')
437 return original;
438 return join_c_conditions (original, extra);
441 /* Like add_condition, but applied to all conditions in rtx X. */
443 static void
444 add_condition_to_rtx (rtx x, const char *extra)
446 switch (GET_CODE (x))
448 case DEFINE_INSN:
449 case DEFINE_EXPAND:
450 case DEFINE_SUBST:
451 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
452 break;
454 case DEFINE_SPLIT:
455 case DEFINE_PEEPHOLE:
456 case DEFINE_PEEPHOLE2:
457 case DEFINE_COND_EXEC:
458 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
459 break;
461 case DEFINE_INSN_AND_SPLIT:
462 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
463 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
464 break;
466 default:
467 break;
471 /* Apply the current iterator values to all attribute_uses. */
473 static void
474 apply_attribute_uses (void)
476 struct map_value *v;
477 attribute_use *ause;
478 unsigned int i;
480 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
482 v = map_attr_string (ause->value);
483 if (!v)
484 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
485 ause->group->apply_iterator (ause->ptr,
486 ause->group->find_builtin (v->string));
490 /* A htab_traverse callback for iterators. Add all used iterators
491 to current_iterators. */
493 static int
494 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
496 struct mapping *iterator;
498 iterator = (struct mapping *) *slot;
499 if (iterator->current_value)
500 current_iterators.safe_push (iterator);
501 return 1;
504 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
505 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
507 static void
508 apply_iterators (rtx original, rtx *queue)
510 unsigned int i;
511 const char *condition;
512 iterator_use *iuse;
513 struct mapping *iterator;
514 struct map_value *v;
515 rtx x;
517 if (iterator_uses.is_empty ())
519 /* Raise an error if any attributes were used. */
520 apply_attribute_uses ();
521 XEXP (*queue, 0) = original;
522 XEXP (*queue, 1) = NULL_RTX;
523 return;
526 /* Clear out the iterators from the previous run. */
527 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
528 iterator->current_value = NULL;
529 current_iterators.truncate (0);
531 /* Mark the iterators that we need this time. */
532 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
533 iuse->iterator->current_value = iuse->iterator->values;
535 /* Get the list of iterators that are in use, preserving the
536 definition order within each group. */
537 htab_traverse (modes.iterators, add_current_iterators, NULL);
538 htab_traverse (codes.iterators, add_current_iterators, NULL);
539 htab_traverse (ints.iterators, add_current_iterators, NULL);
540 htab_traverse (substs.iterators, add_current_iterators, NULL);
541 gcc_assert (!current_iterators.is_empty ());
543 for (;;)
545 /* Apply the current iterator values. Accumulate a condition to
546 say when the resulting rtx can be used. */
547 condition = "";
548 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
550 if (iuse->iterator->group == &substs)
551 continue;
552 v = iuse->iterator->current_value;
553 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
554 condition = join_c_conditions (condition, v->string);
556 apply_attribute_uses ();
557 x = copy_rtx_for_iterators (original);
558 add_condition_to_rtx (x, condition);
560 /* We apply subst iterator after RTL-template is copied, as during
561 subst-iterator processing, we could add an attribute to the
562 RTL-template, and we don't want to do it in the original one. */
563 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
565 v = iuse->iterator->current_value;
566 if (iuse->iterator->group == &substs)
568 iuse->ptr = x;
569 current_iterator_name = iuse->iterator->name;
570 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
573 /* Add the new rtx to the end of the queue. */
574 XEXP (*queue, 0) = x;
575 XEXP (*queue, 1) = NULL_RTX;
577 /* Lexicographically increment the iterator value sequence.
578 That is, cycle through iterator values, starting from the right,
579 and stopping when one of them doesn't wrap around. */
580 i = current_iterators.length ();
581 for (;;)
583 if (i == 0)
584 return;
585 i--;
586 iterator = current_iterators[i];
587 iterator->current_value = iterator->current_value->next;
588 if (iterator->current_value)
589 break;
590 iterator->current_value = iterator->values;
593 /* At least one more rtx to go. Allocate room for it. */
594 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
595 queue = &XEXP (*queue, 1);
599 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
600 of the mapping and GROUP is the group to which it belongs. */
602 static struct mapping *
603 add_mapping (struct iterator_group *group, htab_t table, const char *name)
605 struct mapping *m;
606 void **slot;
608 m = XNEW (struct mapping);
609 m->name = xstrdup (name);
610 m->group = group;
611 m->values = 0;
612 m->current_value = NULL;
614 slot = htab_find_slot (table, m, INSERT);
615 if (*slot != 0)
616 fatal_with_file_and_line ("`%s' already defined", name);
618 *slot = m;
619 return m;
622 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
623 END_PTR points to the current null terminator for the list; return
624 a pointer the new null terminator. */
626 static struct map_value **
627 add_map_value (struct map_value **end_ptr, int number, const char *string)
629 struct map_value *value;
631 value = XNEW (struct map_value);
632 value->next = 0;
633 value->number = number;
634 value->string = string;
636 *end_ptr = value;
637 return &value->next;
640 /* Do one-time initialization of the mode and code attributes. */
642 static void
643 initialize_iterators (void)
645 struct mapping *lower, *upper;
646 struct map_value **lower_ptr, **upper_ptr;
647 char *copy, *p;
648 int i;
650 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
651 modes.iterators = htab_create (13, leading_string_hash,
652 leading_string_eq_p, 0);
653 modes.find_builtin = find_mode;
654 modes.apply_iterator = apply_mode_iterator;
656 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
657 codes.iterators = htab_create (13, leading_string_hash,
658 leading_string_eq_p, 0);
659 codes.find_builtin = find_code;
660 codes.apply_iterator = apply_code_iterator;
662 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
663 ints.iterators = htab_create (13, leading_string_hash,
664 leading_string_eq_p, 0);
665 ints.find_builtin = find_int;
666 ints.apply_iterator = apply_int_iterator;
668 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
669 substs.iterators = htab_create (13, leading_string_hash,
670 leading_string_eq_p, 0);
671 substs.find_builtin = find_int; /* We don't use it, anyway. */
672 substs.apply_iterator = apply_subst_iterator;
674 lower = add_mapping (&modes, modes.attrs, "mode");
675 upper = add_mapping (&modes, modes.attrs, "MODE");
676 lower_ptr = &lower->values;
677 upper_ptr = &upper->values;
678 for (i = 0; i < MAX_MACHINE_MODE; i++)
680 copy = xstrdup (GET_MODE_NAME (i));
681 for (p = copy; *p != 0; p++)
682 *p = TOLOWER (*p);
684 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
685 lower_ptr = add_map_value (lower_ptr, i, copy);
688 lower = add_mapping (&codes, codes.attrs, "code");
689 upper = add_mapping (&codes, codes.attrs, "CODE");
690 lower_ptr = &lower->values;
691 upper_ptr = &upper->values;
692 for (i = 0; i < NUM_RTX_CODE; i++)
694 copy = xstrdup (GET_RTX_NAME (i));
695 for (p = copy; *p != 0; p++)
696 *p = TOUPPER (*p);
698 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
699 upper_ptr = add_map_value (upper_ptr, i, copy);
703 /* Provide a version of a function to read a long long if the system does
704 not provide one. */
705 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
706 HOST_WIDE_INT atoll (const char *);
708 HOST_WIDE_INT
709 atoll (const char *p)
711 int neg = 0;
712 HOST_WIDE_INT tmp_wide;
714 while (ISSPACE (*p))
715 p++;
716 if (*p == '-')
717 neg = 1, p++;
718 else if (*p == '+')
719 p++;
721 tmp_wide = 0;
722 while (ISDIGIT (*p))
724 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
725 if (new_wide < tmp_wide)
727 /* Return INT_MAX equiv on overflow. */
728 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
729 break;
731 tmp_wide = new_wide;
732 p++;
735 if (neg)
736 tmp_wide = -tmp_wide;
737 return tmp_wide;
739 #endif
741 /* Process a define_conditions directive, starting with the optional
742 space after the "define_conditions". The directive looks like this:
744 (define_conditions [
745 (number "string")
746 (number "string")
750 It's not intended to appear in machine descriptions. It is
751 generated by (the program generated by) genconditions.c, and
752 slipped in at the beginning of the sequence of MD files read by
753 most of the other generators. */
754 static void
755 read_conditions (void)
757 int c;
759 c = read_skip_spaces ();
760 if (c != '[')
761 fatal_expected_char ('[', c);
763 while ( (c = read_skip_spaces ()) != ']')
765 struct md_name name;
766 char *expr;
767 int value;
769 if (c != '(')
770 fatal_expected_char ('(', c);
772 read_name (&name);
773 validate_const_int (name.string);
774 value = atoi (name.string);
776 c = read_skip_spaces ();
777 if (c != '"')
778 fatal_expected_char ('"', c);
779 expr = read_quoted_string ();
781 c = read_skip_spaces ();
782 if (c != ')')
783 fatal_expected_char (')', c);
785 add_c_test (expr, value);
789 static void
790 validate_const_int (const char *string)
792 const char *cp;
793 int valid = 1;
795 cp = string;
796 while (*cp && ISSPACE (*cp))
797 cp++;
798 if (*cp == '-' || *cp == '+')
799 cp++;
800 if (*cp == 0)
801 valid = 0;
802 for (; *cp; cp++)
803 if (! ISDIGIT (*cp))
804 valid = 0;
805 if (!valid)
806 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
809 /* Record that PTR uses iterator ITERATOR. */
811 static void
812 record_iterator_use (struct mapping *iterator, void *ptr)
814 struct iterator_use iuse = {iterator, ptr};
815 iterator_uses.safe_push (iuse);
818 /* Record that PTR uses attribute VALUE, which must match a built-in
819 value from group GROUP. */
821 static void
822 record_attribute_use (struct iterator_group *group, void *ptr,
823 const char *value)
825 struct attribute_use ause = {group, value, ptr};
826 attribute_uses.safe_push (ause);
829 /* Interpret NAME as either a built-in value, iterator or attribute
830 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
831 callback. */
833 static void
834 record_potential_iterator_use (struct iterator_group *group, void *ptr,
835 const char *name)
837 struct mapping *m;
838 size_t len;
840 len = strlen (name);
841 if (name[0] == '<' && name[len - 1] == '>')
843 /* Copy the attribute string into permanent storage, without the
844 angle brackets around it. */
845 obstack_grow0 (&string_obstack, name + 1, len - 2);
846 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
848 else
850 m = (struct mapping *) htab_find (group->iterators, &name);
851 if (m != 0)
852 record_iterator_use (m, ptr);
853 else
854 group->apply_iterator (ptr, group->find_builtin (name));
858 /* Finish reading a declaration of the form:
860 (define... <name> [<value1> ... <valuen>])
862 from the MD file, where each <valuei> is either a bare symbol name or a
863 "(<name> <string>)" pair. The "(define..." part has already been read.
865 Represent the declaration as a "mapping" structure; add it to TABLE
866 (which belongs to GROUP) and return it. */
868 static struct mapping *
869 read_mapping (struct iterator_group *group, htab_t table)
871 struct md_name name;
872 struct mapping *m;
873 struct map_value **end_ptr;
874 const char *string;
875 int number, c;
877 /* Read the mapping name and create a structure for it. */
878 read_name (&name);
879 m = add_mapping (group, table, name.string);
881 c = read_skip_spaces ();
882 if (c != '[')
883 fatal_expected_char ('[', c);
885 /* Read each value. */
886 end_ptr = &m->values;
887 c = read_skip_spaces ();
890 if (c != '(')
892 /* A bare symbol name that is implicitly paired to an
893 empty string. */
894 unread_char (c);
895 read_name (&name);
896 string = "";
898 else
900 /* A "(name string)" pair. */
901 read_name (&name);
902 string = read_string (false);
903 c = read_skip_spaces ();
904 if (c != ')')
905 fatal_expected_char (')', c);
907 number = group->find_builtin (name.string);
908 end_ptr = add_map_value (end_ptr, number, string);
909 c = read_skip_spaces ();
911 while (c != ']');
913 return m;
916 /* For iterator with name ATTR_NAME generate define_attr with values
917 'yes' and 'no'. This attribute is used to mark templates to which
918 define_subst ATTR_NAME should be applied. This attribute is set and
919 defined implicitly and automatically. */
920 static void
921 add_define_attr_for_define_subst (const char *attr_name, rtx *queue)
923 rtx const_str, return_rtx;
925 return_rtx = rtx_alloc (DEFINE_ATTR);
926 PUT_CODE (return_rtx, DEFINE_ATTR);
928 const_str = rtx_alloc (CONST_STRING);
929 PUT_CODE (const_str, CONST_STRING);
930 XSTR (const_str, 0) = xstrdup ("no");
932 XSTR (return_rtx, 0) = xstrdup (attr_name);
933 XSTR (return_rtx, 1) = xstrdup ("no,yes");
934 XEXP (return_rtx, 2) = const_str;
936 XEXP (*queue, 0) = return_rtx;
937 XEXP (*queue, 1) = NULL_RTX;
940 /* This routine generates DEFINE_SUBST_ATTR expression with operands
941 ATTR_OPERANDS and places it to QUEUE. */
942 static void
943 add_define_subst_attr (const char **attr_operands, rtx *queue)
945 rtx return_rtx;
946 int i;
948 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
949 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
951 for (i = 0; i < 4; i++)
952 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
954 XEXP (*queue, 0) = return_rtx;
955 XEXP (*queue, 1) = NULL_RTX;
958 /* Read define_subst_attribute construction. It has next form:
959 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
960 Attribute is substituted with value1 when no subst is applied and with
961 value2 in the opposite case.
962 Attributes are added to SUBST_ATTRS_TABLE.
963 In case the iterator is encountered for the first time, it's added to
964 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
966 static void
967 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
968 rtx *queue)
970 struct mapping *m;
971 struct map_value **end_ptr;
972 const char *attr_operands[4];
973 rtx * queue_elem = queue;
974 int i;
976 for (i = 0; i < 4; i++)
977 attr_operands[i] = read_string (false);
979 add_define_subst_attr (attr_operands, queue_elem);
981 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
983 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
984 if (!m)
986 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
987 end_ptr = &m->values;
988 end_ptr = add_map_value (end_ptr, 1, "");
989 end_ptr = add_map_value (end_ptr, 2, "");
991 /* Add element to the queue. */
992 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
993 queue_elem = &XEXP (*queue, 1);
995 add_define_attr_for_define_subst (attr_operands[1], queue_elem);
998 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
999 end_ptr = &m->values;
1000 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1001 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
1004 /* Check newly-created code iterator ITERATOR to see whether every code has the
1005 same format. */
1007 static void
1008 check_code_iterator (struct mapping *iterator)
1010 struct map_value *v;
1011 enum rtx_code bellwether;
1013 bellwether = (enum rtx_code) iterator->values->number;
1014 for (v = iterator->values->next; v != 0; v = v->next)
1015 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1016 fatal_with_file_and_line ("code iterator `%s' combines "
1017 "different rtx formats", iterator->name);
1020 /* Read an rtx-related declaration from the MD file, given that it
1021 starts with directive name RTX_NAME. Return true if it expands to
1022 one or more rtxes (as defined by rtx.def). When returning true,
1023 store the list of rtxes as an EXPR_LIST in *X. */
1025 bool
1026 read_rtx (const char *rtx_name, rtx *x)
1028 static rtx queue_head;
1030 /* Do one-time initialization. */
1031 if (queue_head == 0)
1033 initialize_iterators ();
1034 queue_head = rtx_alloc (EXPR_LIST);
1037 /* Handle various rtx-related declarations that aren't themselves
1038 encoded as rtxes. */
1039 if (strcmp (rtx_name, "define_conditions") == 0)
1041 read_conditions ();
1042 return false;
1044 if (strcmp (rtx_name, "define_mode_attr") == 0)
1046 read_mapping (&modes, modes.attrs);
1047 return false;
1049 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1051 read_mapping (&modes, modes.iterators);
1052 return false;
1054 if (strcmp (rtx_name, "define_code_attr") == 0)
1056 read_mapping (&codes, codes.attrs);
1057 return false;
1059 if (strcmp (rtx_name, "define_code_iterator") == 0)
1061 check_code_iterator (read_mapping (&codes, codes.iterators));
1062 return false;
1064 if (strcmp (rtx_name, "define_int_attr") == 0)
1066 read_mapping (&ints, ints.attrs);
1067 return false;
1069 if (strcmp (rtx_name, "define_int_iterator") == 0)
1071 read_mapping (&ints, ints.iterators);
1072 return false;
1074 if (strcmp (rtx_name, "define_subst_attr") == 0)
1076 read_subst_mapping (substs.iterators, substs.attrs, &queue_head);
1077 *x = queue_head;
1079 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1080 TRUE to process it. */
1081 return true;
1084 apply_iterators (read_rtx_code (rtx_name), &queue_head);
1085 iterator_uses.truncate (0);
1086 attribute_uses.truncate (0);
1088 *x = queue_head;
1089 return true;
1092 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1093 either an rtx code or a code iterator. Parse the rest of the rtx and
1094 return it. */
1096 static rtx
1097 read_rtx_code (const char *code_name)
1099 int i;
1100 RTX_CODE code;
1101 struct mapping *iterator, *m;
1102 const char *format_ptr;
1103 struct md_name name;
1104 rtx return_rtx;
1105 int c;
1106 HOST_WIDE_INT tmp_wide;
1107 char *str;
1108 char *start, *end, *ptr;
1109 char tmpstr[256];
1111 /* Linked list structure for making RTXs: */
1112 struct rtx_list
1114 struct rtx_list *next;
1115 rtx value; /* Value of this node. */
1118 /* If this code is an iterator, build the rtx using the iterator's
1119 first value. */
1120 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1121 if (iterator != 0)
1122 code = (enum rtx_code) iterator->values->number;
1123 else
1124 code = (enum rtx_code) codes.find_builtin (code_name);
1126 /* If we end up with an insn expression then we free this space below. */
1127 return_rtx = rtx_alloc (code);
1128 format_ptr = GET_RTX_FORMAT (code);
1129 PUT_CODE (return_rtx, code);
1131 if (iterator)
1132 record_iterator_use (iterator, return_rtx);
1134 /* If what follows is `: mode ', read it and
1135 store the mode in the rtx. */
1137 i = read_skip_spaces ();
1138 if (i == ':')
1140 read_name (&name);
1141 record_potential_iterator_use (&modes, return_rtx, name.string);
1143 else
1144 unread_char (i);
1146 for (i = 0; format_ptr[i] != 0; i++)
1147 switch (format_ptr[i])
1149 /* 0 means a field for internal use only.
1150 Don't expect it to be present in the input. */
1151 case '0':
1152 break;
1154 case 'e':
1155 case 'u':
1156 XEXP (return_rtx, i) = read_nested_rtx ();
1157 break;
1159 case 'V':
1160 /* 'V' is an optional vector: if a closeparen follows,
1161 just store NULL for this element. */
1162 c = read_skip_spaces ();
1163 unread_char (c);
1164 if (c == ')')
1166 XVEC (return_rtx, i) = 0;
1167 break;
1169 /* Now process the vector. */
1171 case 'E':
1173 /* Obstack to store scratch vector in. */
1174 struct obstack vector_stack;
1175 int list_counter = 0;
1176 rtvec return_vec = NULL_RTVEC;
1178 c = read_skip_spaces ();
1179 if (c != '[')
1180 fatal_expected_char ('[', c);
1182 /* Add expressions to a list, while keeping a count. */
1183 obstack_init (&vector_stack);
1184 while ((c = read_skip_spaces ()) && c != ']')
1186 if (c == EOF)
1187 fatal_expected_char (']', c);
1188 unread_char (c);
1189 list_counter++;
1190 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1192 if (list_counter > 0)
1194 return_vec = rtvec_alloc (list_counter);
1195 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1196 list_counter * sizeof (rtx));
1198 else if (format_ptr[i] == 'E')
1199 fatal_with_file_and_line ("vector must have at least one element");
1200 XVEC (return_rtx, i) = return_vec;
1201 obstack_free (&vector_stack, NULL);
1202 /* close bracket gotten */
1204 break;
1206 case 'S':
1207 case 'T':
1208 case 's':
1210 char *stringbuf;
1211 int star_if_braced;
1213 c = read_skip_spaces ();
1214 unread_char (c);
1215 if (c == ')')
1217 /* 'S' fields are optional and should be NULL if no string
1218 was given. Also allow normal 's' and 'T' strings to be
1219 omitted, treating them in the same way as empty strings. */
1220 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1221 break;
1224 /* The output template slot of a DEFINE_INSN,
1225 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1226 gets a star inserted as its first character, if it is
1227 written with a brace block instead of a string constant. */
1228 star_if_braced = (format_ptr[i] == 'T');
1230 stringbuf = read_string (star_if_braced);
1232 /* For insn patterns, we want to provide a default name
1233 based on the file and line, like "*foo.md:12", if the
1234 given name is blank. These are only for define_insn and
1235 define_insn_and_split, to aid debugging. */
1236 if (*stringbuf == '\0'
1237 && i == 0
1238 && (GET_CODE (return_rtx) == DEFINE_INSN
1239 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1241 char line_name[20];
1242 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1243 const char *slash;
1244 for (slash = fn; *slash; slash ++)
1245 if (*slash == '/' || *slash == '\\' || *slash == ':')
1246 fn = slash + 1;
1247 obstack_1grow (&string_obstack, '*');
1248 obstack_grow (&string_obstack, fn, strlen (fn));
1249 sprintf (line_name, ":%d", read_md_lineno);
1250 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1251 stringbuf = XOBFINISH (&string_obstack, char *);
1254 /* Find attr-names in the string. */
1255 ptr = &tmpstr[0];
1256 end = stringbuf;
1257 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1259 if ((end - start - 1 > 0)
1260 && (end - start - 1 < (int)sizeof (tmpstr)))
1262 strncpy (tmpstr, start+1, end-start-1);
1263 tmpstr[end-start-1] = 0;
1264 end++;
1266 else
1267 break;
1268 m = (struct mapping *) htab_find (substs.attrs, &ptr);
1269 if (m != 0)
1271 /* Here we should find linked subst-iter. */
1272 str = find_subst_iter_by_attr (ptr);
1273 if (str)
1274 m = (struct mapping *) htab_find (substs.iterators, &str);
1275 else
1276 m = 0;
1278 if (m != 0)
1279 record_iterator_use (m, return_rtx);
1282 if (star_if_braced)
1283 XTMPL (return_rtx, i) = stringbuf;
1284 else
1285 XSTR (return_rtx, i) = stringbuf;
1287 break;
1289 case 'w':
1290 read_name (&name);
1291 validate_const_int (name.string);
1292 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1293 tmp_wide = atoi (name.string);
1294 #else
1295 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1296 tmp_wide = atol (name.string);
1297 #else
1298 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1299 But prefer not to use our hand-rolled function above either. */
1300 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1301 tmp_wide = atoll (name.string);
1302 #else
1303 tmp_wide = atoq (name.string);
1304 #endif
1305 #endif
1306 #endif
1307 XWINT (return_rtx, i) = tmp_wide;
1308 break;
1310 case 'i':
1311 case 'n':
1312 /* Can be an iterator or an integer constant. */
1313 read_name (&name);
1314 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1315 name.string);
1316 break;
1318 default:
1319 gcc_unreachable ();
1322 c = read_skip_spaces ();
1323 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1324 arbitrary number of arguments for them. */
1325 if (c == '('
1326 && (GET_CODE (return_rtx) == AND
1327 || GET_CODE (return_rtx) == IOR))
1328 return read_rtx_variadic (return_rtx);
1330 unread_char (c);
1331 return return_rtx;
1334 /* Read a nested rtx construct from the MD file and return it. */
1336 static rtx
1337 read_nested_rtx (void)
1339 struct md_name name;
1340 int c;
1341 rtx return_rtx;
1343 c = read_skip_spaces ();
1344 if (c != '(')
1345 fatal_expected_char ('(', c);
1347 read_name (&name);
1348 if (strcmp (name.string, "nil") == 0)
1349 return_rtx = NULL;
1350 else
1351 return_rtx = read_rtx_code (name.string);
1353 c = read_skip_spaces ();
1354 if (c != ')')
1355 fatal_expected_char (')', c);
1357 return return_rtx;
1360 /* Mutually recursive subroutine of read_rtx which reads
1361 (thing x1 x2 x3 ...) and produces RTL as if
1362 (thing x1 (thing x2 (thing x3 ...))) had been written.
1363 When called, FORM is (thing x1 x2), and the file position
1364 is just past the leading parenthesis of x3. Only works
1365 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1366 static rtx
1367 read_rtx_variadic (rtx form)
1369 char c = '(';
1370 rtx p = form, q;
1374 unread_char (c);
1376 q = rtx_alloc (GET_CODE (p));
1377 PUT_MODE (q, GET_MODE (p));
1379 XEXP (q, 0) = XEXP (p, 1);
1380 XEXP (q, 1) = read_nested_rtx ();
1382 XEXP (p, 1) = q;
1383 p = q;
1384 c = read_skip_spaces ();
1386 while (c == '(');
1387 unread_char (c);
1388 return form;