MIPS compact branch support
[official-gcc.git] / gcc / read-rtl.c
blobff08505badc4011b49ee4dff34f43c6a6c1919b5
1 /* RTL reader for GCC.
2 Copyright (C) 1987-2015 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 "read-md.h"
31 #include "gensupport.h"
33 /* One element in a singly-linked list of (integer, string) pairs. */
34 struct map_value {
35 struct map_value *next;
36 int number;
37 const char *string;
40 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
41 The integers are iterator values; the strings are either C conditions
42 or attribute values. */
43 struct mapping {
44 /* The name of the iterator or attribute. */
45 const char *name;
47 /* The group (modes or codes) to which the iterator or attribute belongs. */
48 struct iterator_group *group;
50 /* The list of (integer, string) pairs. */
51 struct map_value *values;
53 /* For iterators, records the current value of the iterator. */
54 struct map_value *current_value;
57 /* Vector definitions for the above. */
58 typedef struct mapping *mapping_ptr;
60 /* A structure for abstracting the common parts of iterators. */
61 struct iterator_group {
62 /* Tables of "mapping" structures, one for attributes and one for
63 iterators. */
64 htab_t attrs, iterators;
66 /* Treat the given string as the name of a standard mode, etc., and
67 return its integer value. */
68 int (*find_builtin) (const char *);
70 /* Make the given pointer use the given iterator value. */
71 void (*apply_iterator) (void *, int);
74 /* Records one use of an iterator. */
75 struct iterator_use {
76 /* The iterator itself. */
77 struct mapping *iterator;
79 /* The location of the use, as passed to the apply_iterator callback. */
80 void *ptr;
83 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
84 in a non-string rtx field. */
85 struct attribute_use {
86 /* The group that describes the use site. */
87 struct iterator_group *group;
89 /* The name of the attribute, possibly with an "iterator:" prefix. */
90 const char *value;
92 /* The location of the use, as passed to GROUP's apply_iterator callback. */
93 void *ptr;
96 /* This struct is used to link subst_attr named ATTR_NAME with
97 corresponding define_subst named ITER_NAME. */
98 struct subst_attr_to_iter_mapping
100 char *attr_name;
101 char *iter_name;
104 /* Hash-table to store links between subst-attributes and
105 define_substs. */
106 htab_t subst_attr_to_iter_map = NULL;
107 /* This global stores name of subst-iterator which is currently being
108 processed. */
109 const char *current_iterator_name;
111 static void validate_const_int (const char *);
112 static rtx read_rtx_code (const char *);
113 static rtx read_nested_rtx (void);
114 static rtx read_rtx_variadic (rtx);
116 /* The mode and code iterator structures. */
117 static struct iterator_group modes, codes, ints, substs;
119 /* All iterators used in the current rtx. */
120 static vec<mapping_ptr> current_iterators;
122 /* The list of all iterator uses in the current rtx. */
123 static vec<iterator_use> iterator_uses;
125 /* The list of all attribute uses in the current rtx. */
126 static vec<attribute_use> attribute_uses;
128 /* Implementations of the iterator_group callbacks for modes. */
130 static int
131 find_mode (const char *name)
133 int i;
135 for (i = 0; i < NUM_MACHINE_MODES; i++)
136 if (strcmp (GET_MODE_NAME (i), name) == 0)
137 return i;
139 fatal_with_file_and_line ("unknown mode `%s'", name);
142 static void
143 apply_mode_iterator (void *loc, int mode)
145 PUT_MODE ((rtx) loc, (machine_mode) mode);
148 /* Implementations of the iterator_group callbacks for codes. */
150 static int
151 find_code (const char *name)
153 int i;
155 for (i = 0; i < NUM_RTX_CODE; i++)
156 if (strcmp (GET_RTX_NAME (i), name) == 0)
157 return i;
159 fatal_with_file_and_line ("unknown rtx code `%s'", name);
162 static void
163 apply_code_iterator (void *loc, int code)
165 PUT_CODE ((rtx) loc, (enum rtx_code) code);
168 /* Implementations of the iterator_group callbacks for ints. */
170 /* Since GCC does not construct a table of valid constants,
171 we have to accept any int as valid. No cross-checking can
172 be done. */
174 static int
175 find_int (const char *name)
177 validate_const_int (name);
178 return atoi (name);
181 static void
182 apply_int_iterator (void *loc, int value)
184 *(int *)loc = value;
187 /* This routine adds attribute or does nothing depending on VALUE. When
188 VALUE is 1, it does nothing - the first duplicate of original
189 template is kept untouched when it's subjected to a define_subst.
190 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
191 attribute, named exactly as define_subst, which later will be
192 applied. If such attribute has already been added, then no the
193 routine has no effect. */
194 static void
195 apply_subst_iterator (void *loc, int value)
197 rtx rt = (rtx)loc;
198 rtx new_attr;
199 rtvec attrs_vec, new_attrs_vec;
200 int i;
201 if (value == 1)
202 return;
203 gcc_assert (GET_CODE (rt) == DEFINE_INSN
204 || GET_CODE (rt) == DEFINE_EXPAND);
206 attrs_vec = XVEC (rt, 4);
208 /* If we've already added attribute 'current_iterator_name', then we
209 have nothing to do now. */
210 if (attrs_vec)
212 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
214 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
215 return;
219 /* Add attribute with subst name - it serves as a mark for
220 define_subst which later would be applied to this pattern. */
221 new_attr = rtx_alloc (SET_ATTR);
222 PUT_CODE (new_attr, SET_ATTR);
223 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
224 XSTR (new_attr, 1) = xstrdup ("yes");
226 if (!attrs_vec)
228 new_attrs_vec = rtvec_alloc (1);
229 new_attrs_vec->elem[0] = new_attr;
231 else
233 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
234 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
235 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
236 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
238 XVEC (rt, 4) = new_attrs_vec;
241 /* Map subst-attribute ATTR to subst iterator ITER. */
243 static void
244 bind_subst_iter_and_attr (const char *iter, const char *attr)
246 struct subst_attr_to_iter_mapping *value;
247 void **slot;
248 if (!subst_attr_to_iter_map)
249 subst_attr_to_iter_map =
250 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
251 value = XNEW (struct subst_attr_to_iter_mapping);
252 value->attr_name = xstrdup (attr);
253 value->iter_name = xstrdup (iter);
254 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
255 *slot = value;
258 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
260 static char*
261 find_subst_iter_by_attr (const char *attr)
263 char *iter_name = NULL;
264 struct subst_attr_to_iter_mapping *value;
265 value = (struct subst_attr_to_iter_mapping*)
266 htab_find (subst_attr_to_iter_map, &attr);
267 if (value)
268 iter_name = value->iter_name;
269 return iter_name;
272 /* Map attribute string P to its current value. Return null if the attribute
273 isn't known. */
275 static struct map_value *
276 map_attr_string (const char *p)
278 const char *attr;
279 struct mapping *iterator;
280 unsigned int i;
281 struct mapping *m;
282 struct map_value *v;
283 int iterator_name_len;
285 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
286 attribute name. */
287 attr = strchr (p, ':');
288 if (attr == 0)
290 iterator_name_len = -1;
291 attr = p;
293 else
295 iterator_name_len = attr - p;
296 attr++;
299 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
301 /* If an iterator name was specified, check that it matches. */
302 if (iterator_name_len >= 0
303 && (strncmp (p, iterator->name, iterator_name_len) != 0
304 || iterator->name[iterator_name_len] != 0))
305 continue;
307 /* Find the attribute specification. */
308 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
309 if (m)
311 /* In contrast to code/mode/int iterators, attributes of subst
312 iterators are linked to one specific subst-iterator. So, if
313 we are dealing with subst-iterator, we should check if it's
314 the one which linked with the given attribute. */
315 if (iterator->group == &substs)
317 char *iter_name = find_subst_iter_by_attr (attr);
318 if (strcmp (iter_name, iterator->name) != 0)
319 continue;
321 /* Find the attribute value associated with the current
322 iterator value. */
323 for (v = m->values; v; v = v->next)
324 if (v->number == iterator->current_value->number)
325 return v;
328 return NULL;
331 /* Apply the current iterator values to STRING. Return the new string
332 if any changes were needed, otherwise return STRING itself. */
334 static const char *
335 apply_iterator_to_string (const char *string)
337 char *base, *copy, *p, *start, *end;
338 struct map_value *v;
340 if (string == 0)
341 return string;
343 base = p = copy = ASTRDUP (string);
344 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
346 p = start + 1;
348 *end = 0;
349 v = map_attr_string (p);
350 *end = '>';
351 if (v == 0)
352 continue;
354 /* Add everything between the last copied byte and the '<',
355 then add in the attribute value. */
356 obstack_grow (&string_obstack, base, start - base);
357 obstack_grow (&string_obstack, v->string, strlen (v->string));
358 base = end + 1;
360 if (base != copy)
362 obstack_grow (&string_obstack, base, strlen (base) + 1);
363 copy = XOBFINISH (&string_obstack, char *);
364 copy_md_ptr_loc (copy, string);
365 return copy;
367 return string;
370 /* Return a deep copy of X, substituting the current iterator
371 values into any strings. */
373 static rtx
374 copy_rtx_for_iterators (rtx original)
376 const char *format_ptr, *p;
377 int i, j;
378 rtx x;
380 if (original == 0)
381 return original;
383 /* Create a shallow copy of ORIGINAL. */
384 x = rtx_alloc (GET_CODE (original));
385 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
387 /* Change each string and recursively change each rtx. */
388 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
389 for (i = 0; format_ptr[i] != 0; i++)
390 switch (format_ptr[i])
392 case 'T':
393 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
394 XTMPL (x, i) = p;
395 break;
397 case 'S':
398 case 's':
399 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
400 XSTR (x, i) = p;
401 break;
403 case 'e':
404 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
405 break;
407 case 'V':
408 case 'E':
409 if (XVEC (original, i))
411 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
412 for (j = 0; j < XVECLEN (x, i); j++)
413 XVECEXP (x, i, j)
414 = copy_rtx_for_iterators (XVECEXP (original, i, j));
416 break;
418 default:
419 break;
421 return x;
424 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
425 has the form "&& ..." (as used in define_insn_and_splits), assume that
426 EXTRA is already satisfied. Empty strings are treated like "true". */
428 static const char *
429 add_condition_to_string (const char *original, const char *extra)
431 if (original != 0 && original[0] == '&' && original[1] == '&')
432 return original;
433 return join_c_conditions (original, extra);
436 /* Like add_condition, but applied to all conditions in rtx X. */
438 static void
439 add_condition_to_rtx (rtx x, const char *extra)
441 switch (GET_CODE (x))
443 case DEFINE_INSN:
444 case DEFINE_EXPAND:
445 case DEFINE_SUBST:
446 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
447 break;
449 case DEFINE_SPLIT:
450 case DEFINE_PEEPHOLE:
451 case DEFINE_PEEPHOLE2:
452 case DEFINE_COND_EXEC:
453 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
454 break;
456 case DEFINE_INSN_AND_SPLIT:
457 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
458 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
459 break;
461 default:
462 break;
466 /* Apply the current iterator values to all attribute_uses. */
468 static void
469 apply_attribute_uses (void)
471 struct map_value *v;
472 attribute_use *ause;
473 unsigned int i;
475 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
477 v = map_attr_string (ause->value);
478 if (!v)
479 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
480 ause->group->apply_iterator (ause->ptr,
481 ause->group->find_builtin (v->string));
485 /* A htab_traverse callback for iterators. Add all used iterators
486 to current_iterators. */
488 static int
489 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
491 struct mapping *iterator;
493 iterator = (struct mapping *) *slot;
494 if (iterator->current_value)
495 current_iterators.safe_push (iterator);
496 return 1;
499 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
500 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
502 static void
503 apply_iterators (rtx original, vec<rtx> *queue)
505 unsigned int i;
506 const char *condition;
507 iterator_use *iuse;
508 struct mapping *iterator;
509 struct map_value *v;
510 rtx x;
512 if (iterator_uses.is_empty ())
514 /* Raise an error if any attributes were used. */
515 apply_attribute_uses ();
516 queue->safe_push (original);
517 return;
520 /* Clear out the iterators from the previous run. */
521 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
522 iterator->current_value = NULL;
523 current_iterators.truncate (0);
525 /* Mark the iterators that we need this time. */
526 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
527 iuse->iterator->current_value = iuse->iterator->values;
529 /* Get the list of iterators that are in use, preserving the
530 definition order within each group. */
531 htab_traverse (modes.iterators, add_current_iterators, NULL);
532 htab_traverse (codes.iterators, add_current_iterators, NULL);
533 htab_traverse (ints.iterators, add_current_iterators, NULL);
534 htab_traverse (substs.iterators, add_current_iterators, NULL);
535 gcc_assert (!current_iterators.is_empty ());
537 for (;;)
539 /* Apply the current iterator values. Accumulate a condition to
540 say when the resulting rtx can be used. */
541 condition = "";
542 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
544 if (iuse->iterator->group == &substs)
545 continue;
546 v = iuse->iterator->current_value;
547 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
548 condition = join_c_conditions (condition, v->string);
550 apply_attribute_uses ();
551 x = copy_rtx_for_iterators (original);
552 add_condition_to_rtx (x, condition);
554 /* We apply subst iterator after RTL-template is copied, as during
555 subst-iterator processing, we could add an attribute to the
556 RTL-template, and we don't want to do it in the original one. */
557 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
559 v = iuse->iterator->current_value;
560 if (iuse->iterator->group == &substs)
562 iuse->ptr = x;
563 current_iterator_name = iuse->iterator->name;
564 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
567 /* Add the new rtx to the end of the queue. */
568 queue->safe_push (x);
570 /* Lexicographically increment the iterator value sequence.
571 That is, cycle through iterator values, starting from the right,
572 and stopping when one of them doesn't wrap around. */
573 i = current_iterators.length ();
574 for (;;)
576 if (i == 0)
577 return;
578 i--;
579 iterator = current_iterators[i];
580 iterator->current_value = iterator->current_value->next;
581 if (iterator->current_value)
582 break;
583 iterator->current_value = iterator->values;
588 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
589 of the mapping and GROUP is the group to which it belongs. */
591 static struct mapping *
592 add_mapping (struct iterator_group *group, htab_t table, const char *name)
594 struct mapping *m;
595 void **slot;
597 m = XNEW (struct mapping);
598 m->name = xstrdup (name);
599 m->group = group;
600 m->values = 0;
601 m->current_value = NULL;
603 slot = htab_find_slot (table, m, INSERT);
604 if (*slot != 0)
605 fatal_with_file_and_line ("`%s' already defined", name);
607 *slot = m;
608 return m;
611 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
612 END_PTR points to the current null terminator for the list; return
613 a pointer the new null terminator. */
615 static struct map_value **
616 add_map_value (struct map_value **end_ptr, int number, const char *string)
618 struct map_value *value;
620 value = XNEW (struct map_value);
621 value->next = 0;
622 value->number = number;
623 value->string = string;
625 *end_ptr = value;
626 return &value->next;
629 /* Do one-time initialization of the mode and code attributes. */
631 static void
632 initialize_iterators (void)
634 struct mapping *lower, *upper;
635 struct map_value **lower_ptr, **upper_ptr;
636 char *copy, *p;
637 int i;
639 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
640 modes.iterators = htab_create (13, leading_string_hash,
641 leading_string_eq_p, 0);
642 modes.find_builtin = find_mode;
643 modes.apply_iterator = apply_mode_iterator;
645 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
646 codes.iterators = htab_create (13, leading_string_hash,
647 leading_string_eq_p, 0);
648 codes.find_builtin = find_code;
649 codes.apply_iterator = apply_code_iterator;
651 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
652 ints.iterators = htab_create (13, leading_string_hash,
653 leading_string_eq_p, 0);
654 ints.find_builtin = find_int;
655 ints.apply_iterator = apply_int_iterator;
657 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
658 substs.iterators = htab_create (13, leading_string_hash,
659 leading_string_eq_p, 0);
660 substs.find_builtin = find_int; /* We don't use it, anyway. */
661 substs.apply_iterator = apply_subst_iterator;
663 lower = add_mapping (&modes, modes.attrs, "mode");
664 upper = add_mapping (&modes, modes.attrs, "MODE");
665 lower_ptr = &lower->values;
666 upper_ptr = &upper->values;
667 for (i = 0; i < MAX_MACHINE_MODE; i++)
669 copy = xstrdup (GET_MODE_NAME (i));
670 for (p = copy; *p != 0; p++)
671 *p = TOLOWER (*p);
673 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
674 lower_ptr = add_map_value (lower_ptr, i, copy);
677 lower = add_mapping (&codes, codes.attrs, "code");
678 upper = add_mapping (&codes, codes.attrs, "CODE");
679 lower_ptr = &lower->values;
680 upper_ptr = &upper->values;
681 for (i = 0; i < NUM_RTX_CODE; i++)
683 copy = xstrdup (GET_RTX_NAME (i));
684 for (p = copy; *p != 0; p++)
685 *p = TOUPPER (*p);
687 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
688 upper_ptr = add_map_value (upper_ptr, i, copy);
692 /* Provide a version of a function to read a long long if the system does
693 not provide one. */
694 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
695 HOST_WIDE_INT atoll (const char *);
697 HOST_WIDE_INT
698 atoll (const char *p)
700 int neg = 0;
701 HOST_WIDE_INT tmp_wide;
703 while (ISSPACE (*p))
704 p++;
705 if (*p == '-')
706 neg = 1, p++;
707 else if (*p == '+')
708 p++;
710 tmp_wide = 0;
711 while (ISDIGIT (*p))
713 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
714 if (new_wide < tmp_wide)
716 /* Return INT_MAX equiv on overflow. */
717 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
718 break;
720 tmp_wide = new_wide;
721 p++;
724 if (neg)
725 tmp_wide = -tmp_wide;
726 return tmp_wide;
728 #endif
730 /* Process a define_conditions directive, starting with the optional
731 space after the "define_conditions". The directive looks like this:
733 (define_conditions [
734 (number "string")
735 (number "string")
739 It's not intended to appear in machine descriptions. It is
740 generated by (the program generated by) genconditions.c, and
741 slipped in at the beginning of the sequence of MD files read by
742 most of the other generators. */
743 static void
744 read_conditions (void)
746 int c;
748 c = read_skip_spaces ();
749 if (c != '[')
750 fatal_expected_char ('[', c);
752 while ( (c = read_skip_spaces ()) != ']')
754 struct md_name name;
755 char *expr;
756 int value;
758 if (c != '(')
759 fatal_expected_char ('(', c);
761 read_name (&name);
762 validate_const_int (name.string);
763 value = atoi (name.string);
765 c = read_skip_spaces ();
766 if (c != '"')
767 fatal_expected_char ('"', c);
768 expr = read_quoted_string ();
770 c = read_skip_spaces ();
771 if (c != ')')
772 fatal_expected_char (')', c);
774 add_c_test (expr, value);
778 static void
779 validate_const_int (const char *string)
781 const char *cp;
782 int valid = 1;
784 cp = string;
785 while (*cp && ISSPACE (*cp))
786 cp++;
787 if (*cp == '-' || *cp == '+')
788 cp++;
789 if (*cp == 0)
790 valid = 0;
791 for (; *cp; cp++)
792 if (! ISDIGIT (*cp))
794 valid = 0;
795 break;
797 if (!valid)
798 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
801 static void
802 validate_const_wide_int (const char *string)
804 const char *cp;
805 int valid = 1;
807 cp = string;
808 while (*cp && ISSPACE (*cp))
809 cp++;
810 /* Skip the leading 0x. */
811 if (cp[0] == '0' || cp[1] == 'x')
812 cp += 2;
813 else
814 valid = 0;
815 if (*cp == 0)
816 valid = 0;
817 for (; *cp; cp++)
818 if (! ISXDIGIT (*cp))
819 valid = 0;
820 if (!valid)
821 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
824 /* Record that PTR uses iterator ITERATOR. */
826 static void
827 record_iterator_use (struct mapping *iterator, void *ptr)
829 struct iterator_use iuse = {iterator, ptr};
830 iterator_uses.safe_push (iuse);
833 /* Record that PTR uses attribute VALUE, which must match a built-in
834 value from group GROUP. */
836 static void
837 record_attribute_use (struct iterator_group *group, void *ptr,
838 const char *value)
840 struct attribute_use ause = {group, value, ptr};
841 attribute_uses.safe_push (ause);
844 /* Interpret NAME as either a built-in value, iterator or attribute
845 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
846 callback. */
848 static void
849 record_potential_iterator_use (struct iterator_group *group, void *ptr,
850 const char *name)
852 struct mapping *m;
853 size_t len;
855 len = strlen (name);
856 if (name[0] == '<' && name[len - 1] == '>')
858 /* Copy the attribute string into permanent storage, without the
859 angle brackets around it. */
860 obstack_grow0 (&string_obstack, name + 1, len - 2);
861 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
863 else
865 m = (struct mapping *) htab_find (group->iterators, &name);
866 if (m != 0)
867 record_iterator_use (m, ptr);
868 else
869 group->apply_iterator (ptr, group->find_builtin (name));
873 /* Finish reading a declaration of the form:
875 (define... <name> [<value1> ... <valuen>])
877 from the MD file, where each <valuei> is either a bare symbol name or a
878 "(<name> <string>)" pair. The "(define..." part has already been read.
880 Represent the declaration as a "mapping" structure; add it to TABLE
881 (which belongs to GROUP) and return it. */
883 static struct mapping *
884 read_mapping (struct iterator_group *group, htab_t table)
886 struct md_name name;
887 struct mapping *m;
888 struct map_value **end_ptr;
889 const char *string;
890 int number, c;
892 /* Read the mapping name and create a structure for it. */
893 read_name (&name);
894 m = add_mapping (group, table, name.string);
896 c = read_skip_spaces ();
897 if (c != '[')
898 fatal_expected_char ('[', c);
900 /* Read each value. */
901 end_ptr = &m->values;
902 c = read_skip_spaces ();
905 if (c != '(')
907 /* A bare symbol name that is implicitly paired to an
908 empty string. */
909 unread_char (c);
910 read_name (&name);
911 string = "";
913 else
915 /* A "(name string)" pair. */
916 read_name (&name);
917 string = read_string (false);
918 c = read_skip_spaces ();
919 if (c != ')')
920 fatal_expected_char (')', c);
922 number = group->find_builtin (name.string);
923 end_ptr = add_map_value (end_ptr, number, string);
924 c = read_skip_spaces ();
926 while (c != ']');
928 return m;
931 /* For iterator with name ATTR_NAME generate define_attr with values
932 'yes' and 'no'. This attribute is used to mark templates to which
933 define_subst ATTR_NAME should be applied. This attribute is set and
934 defined implicitly and automatically. */
935 static void
936 add_define_attr_for_define_subst (const char *attr_name, vec<rtx> *queue)
938 rtx const_str, return_rtx;
940 return_rtx = rtx_alloc (DEFINE_ATTR);
941 PUT_CODE (return_rtx, DEFINE_ATTR);
943 const_str = rtx_alloc (CONST_STRING);
944 PUT_CODE (const_str, CONST_STRING);
945 XSTR (const_str, 0) = xstrdup ("no");
947 XSTR (return_rtx, 0) = xstrdup (attr_name);
948 XSTR (return_rtx, 1) = xstrdup ("no,yes");
949 XEXP (return_rtx, 2) = const_str;
951 queue->safe_push (return_rtx);
954 /* This routine generates DEFINE_SUBST_ATTR expression with operands
955 ATTR_OPERANDS and places it to QUEUE. */
956 static void
957 add_define_subst_attr (const char **attr_operands, vec<rtx> *queue)
959 rtx return_rtx;
960 int i;
962 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
963 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
965 for (i = 0; i < 4; i++)
966 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
968 queue->safe_push (return_rtx);
971 /* Read define_subst_attribute construction. It has next form:
972 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
973 Attribute is substituted with value1 when no subst is applied and with
974 value2 in the opposite case.
975 Attributes are added to SUBST_ATTRS_TABLE.
976 In case the iterator is encountered for the first time, it's added to
977 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
979 static void
980 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
981 vec<rtx> *queue)
983 struct mapping *m;
984 struct map_value **end_ptr;
985 const char *attr_operands[4];
986 int i;
988 for (i = 0; i < 4; i++)
989 attr_operands[i] = read_string (false);
991 add_define_subst_attr (attr_operands, queue);
993 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
995 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
996 if (!m)
998 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
999 end_ptr = &m->values;
1000 end_ptr = add_map_value (end_ptr, 1, "");
1001 end_ptr = add_map_value (end_ptr, 2, "");
1003 add_define_attr_for_define_subst (attr_operands[1], queue);
1006 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1007 end_ptr = &m->values;
1008 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1009 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
1012 /* Check newly-created code iterator ITERATOR to see whether every code has the
1013 same format. */
1015 static void
1016 check_code_iterator (struct mapping *iterator)
1018 struct map_value *v;
1019 enum rtx_code bellwether;
1021 bellwether = (enum rtx_code) iterator->values->number;
1022 for (v = iterator->values->next; v != 0; v = v->next)
1023 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1024 fatal_with_file_and_line ("code iterator `%s' combines "
1025 "different rtx formats", iterator->name);
1028 /* Read an rtx-related declaration from the MD file, given that it
1029 starts with directive name RTX_NAME. Return true if it expands to
1030 one or more rtxes (as defined by rtx.def). When returning true,
1031 store the list of rtxes as an EXPR_LIST in *X. */
1033 bool
1034 read_rtx (const char *rtx_name, vec<rtx> *rtxen)
1036 static bool initialized = false;
1038 /* Do one-time initialization. */
1039 if (!initialized)
1041 initialize_iterators ();
1042 initialized = true;
1045 /* Handle various rtx-related declarations that aren't themselves
1046 encoded as rtxes. */
1047 if (strcmp (rtx_name, "define_conditions") == 0)
1049 read_conditions ();
1050 return false;
1052 if (strcmp (rtx_name, "define_mode_attr") == 0)
1054 read_mapping (&modes, modes.attrs);
1055 return false;
1057 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1059 read_mapping (&modes, modes.iterators);
1060 return false;
1062 if (strcmp (rtx_name, "define_code_attr") == 0)
1064 read_mapping (&codes, codes.attrs);
1065 return false;
1067 if (strcmp (rtx_name, "define_code_iterator") == 0)
1069 check_code_iterator (read_mapping (&codes, codes.iterators));
1070 return false;
1072 if (strcmp (rtx_name, "define_int_attr") == 0)
1074 read_mapping (&ints, ints.attrs);
1075 return false;
1077 if (strcmp (rtx_name, "define_int_iterator") == 0)
1079 read_mapping (&ints, ints.iterators);
1080 return false;
1082 if (strcmp (rtx_name, "define_subst_attr") == 0)
1084 read_subst_mapping (substs.iterators, substs.attrs, rtxen);
1086 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1087 TRUE to process it. */
1088 return true;
1091 apply_iterators (read_rtx_code (rtx_name), rtxen);
1092 iterator_uses.truncate (0);
1093 attribute_uses.truncate (0);
1095 return true;
1098 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1099 either an rtx code or a code iterator. Parse the rest of the rtx and
1100 return it. */
1102 static rtx
1103 read_rtx_code (const char *code_name)
1105 int i;
1106 RTX_CODE code;
1107 struct mapping *iterator, *m;
1108 const char *format_ptr;
1109 struct md_name name;
1110 rtx return_rtx;
1111 int c;
1112 HOST_WIDE_INT tmp_wide;
1113 char *str;
1114 char *start, *end, *ptr;
1115 char tmpstr[256];
1117 /* Linked list structure for making RTXs: */
1118 struct rtx_list
1120 struct rtx_list *next;
1121 rtx value; /* Value of this node. */
1124 /* If this code is an iterator, build the rtx using the iterator's
1125 first value. */
1126 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1127 if (iterator != 0)
1128 code = (enum rtx_code) iterator->values->number;
1129 else
1130 code = (enum rtx_code) codes.find_builtin (code_name);
1132 /* If we end up with an insn expression then we free this space below. */
1133 return_rtx = rtx_alloc (code);
1134 format_ptr = GET_RTX_FORMAT (code);
1135 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1136 PUT_CODE (return_rtx, code);
1138 if (iterator)
1139 record_iterator_use (iterator, return_rtx);
1141 /* If what follows is `: mode ', read it and
1142 store the mode in the rtx. */
1144 i = read_skip_spaces ();
1145 if (i == ':')
1147 read_name (&name);
1148 record_potential_iterator_use (&modes, return_rtx, name.string);
1150 else
1151 unread_char (i);
1153 for (i = 0; format_ptr[i] != 0; i++)
1154 switch (format_ptr[i])
1156 /* 0 means a field for internal use only.
1157 Don't expect it to be present in the input. */
1158 case '0':
1159 if (code == REG)
1160 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1161 break;
1163 case 'e':
1164 case 'u':
1165 XEXP (return_rtx, i) = read_nested_rtx ();
1166 break;
1168 case 'V':
1169 /* 'V' is an optional vector: if a closeparen follows,
1170 just store NULL for this element. */
1171 c = read_skip_spaces ();
1172 unread_char (c);
1173 if (c == ')')
1175 XVEC (return_rtx, i) = 0;
1176 break;
1178 /* Now process the vector. */
1180 case 'E':
1182 /* Obstack to store scratch vector in. */
1183 struct obstack vector_stack;
1184 int list_counter = 0;
1185 rtvec return_vec = NULL_RTVEC;
1187 c = read_skip_spaces ();
1188 if (c != '[')
1189 fatal_expected_char ('[', c);
1191 /* Add expressions to a list, while keeping a count. */
1192 obstack_init (&vector_stack);
1193 while ((c = read_skip_spaces ()) && c != ']')
1195 if (c == EOF)
1196 fatal_expected_char (']', c);
1197 unread_char (c);
1198 list_counter++;
1199 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1201 if (list_counter > 0)
1203 return_vec = rtvec_alloc (list_counter);
1204 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1205 list_counter * sizeof (rtx));
1207 else if (format_ptr[i] == 'E')
1208 fatal_with_file_and_line ("vector must have at least one element");
1209 XVEC (return_rtx, i) = return_vec;
1210 obstack_free (&vector_stack, NULL);
1211 /* close bracket gotten */
1213 break;
1215 case 'S':
1216 case 'T':
1217 case 's':
1219 char *stringbuf;
1220 int star_if_braced;
1222 c = read_skip_spaces ();
1223 unread_char (c);
1224 if (c == ')')
1226 /* 'S' fields are optional and should be NULL if no string
1227 was given. Also allow normal 's' and 'T' strings to be
1228 omitted, treating them in the same way as empty strings. */
1229 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1230 break;
1233 /* The output template slot of a DEFINE_INSN,
1234 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1235 gets a star inserted as its first character, if it is
1236 written with a brace block instead of a string constant. */
1237 star_if_braced = (format_ptr[i] == 'T');
1239 stringbuf = read_string (star_if_braced);
1241 /* For insn patterns, we want to provide a default name
1242 based on the file and line, like "*foo.md:12", if the
1243 given name is blank. These are only for define_insn and
1244 define_insn_and_split, to aid debugging. */
1245 if (*stringbuf == '\0'
1246 && i == 0
1247 && (GET_CODE (return_rtx) == DEFINE_INSN
1248 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1250 char line_name[20];
1251 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1252 const char *slash;
1253 for (slash = fn; *slash; slash ++)
1254 if (*slash == '/' || *slash == '\\' || *slash == ':')
1255 fn = slash + 1;
1256 obstack_1grow (&string_obstack, '*');
1257 obstack_grow (&string_obstack, fn, strlen (fn));
1258 sprintf (line_name, ":%d", read_md_lineno);
1259 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1260 stringbuf = XOBFINISH (&string_obstack, char *);
1263 /* Find attr-names in the string. */
1264 ptr = &tmpstr[0];
1265 end = stringbuf;
1266 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1268 if ((end - start - 1 > 0)
1269 && (end - start - 1 < (int)sizeof (tmpstr)))
1271 strncpy (tmpstr, start+1, end-start-1);
1272 tmpstr[end-start-1] = 0;
1273 end++;
1275 else
1276 break;
1277 m = (struct mapping *) htab_find (substs.attrs, &ptr);
1278 if (m != 0)
1280 /* Here we should find linked subst-iter. */
1281 str = find_subst_iter_by_attr (ptr);
1282 if (str)
1283 m = (struct mapping *) htab_find (substs.iterators, &str);
1284 else
1285 m = 0;
1287 if (m != 0)
1288 record_iterator_use (m, return_rtx);
1291 if (star_if_braced)
1292 XTMPL (return_rtx, i) = stringbuf;
1293 else
1294 XSTR (return_rtx, i) = stringbuf;
1296 break;
1298 case 'w':
1299 read_name (&name);
1300 validate_const_int (name.string);
1301 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1302 tmp_wide = atoi (name.string);
1303 #else
1304 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1305 tmp_wide = atol (name.string);
1306 #else
1307 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1308 But prefer not to use our hand-rolled function above either. */
1309 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1310 tmp_wide = atoll (name.string);
1311 #else
1312 tmp_wide = atoq (name.string);
1313 #endif
1314 #endif
1315 #endif
1316 XWINT (return_rtx, i) = tmp_wide;
1317 break;
1319 case 'i':
1320 case 'n':
1321 /* Can be an iterator or an integer constant. */
1322 read_name (&name);
1323 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1324 name.string);
1325 break;
1327 case 'r':
1328 read_name (&name);
1329 validate_const_int (name.string);
1330 set_regno_raw (return_rtx, atoi (name.string), 1);
1331 REG_ATTRS (return_rtx) = NULL;
1332 break;
1334 default:
1335 gcc_unreachable ();
1338 if (CONST_WIDE_INT_P (return_rtx))
1340 read_name (&name);
1341 validate_const_wide_int (name.string);
1343 const char *s = name.string;
1344 int len;
1345 int index = 0;
1346 int gs = HOST_BITS_PER_WIDE_INT/4;
1347 int pos;
1348 char * buf = XALLOCAVEC (char, gs + 1);
1349 unsigned HOST_WIDE_INT wi;
1350 int wlen;
1352 /* Skip the leading spaces. */
1353 while (*s && ISSPACE (*s))
1354 s++;
1356 /* Skip the leading 0x. */
1357 gcc_assert (s[0] == '0');
1358 gcc_assert (s[1] == 'x');
1359 s += 2;
1361 len = strlen (s);
1362 pos = len - gs;
1363 wlen = (len + gs - 1) / gs; /* Number of words needed */
1365 return_rtx = const_wide_int_alloc (wlen);
1367 while (pos > 0)
1369 #if HOST_BITS_PER_WIDE_INT == 64
1370 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1371 #else
1372 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1373 #endif
1374 CWI_ELT (return_rtx, index++) = wi;
1375 pos -= gs;
1377 strncpy (buf, s, gs - pos);
1378 buf [gs - pos] = 0;
1379 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1380 CWI_ELT (return_rtx, index++) = wi;
1381 /* TODO: After reading, do we want to canonicalize with:
1382 value = lookup_const_wide_int (value); ? */
1386 c = read_skip_spaces ();
1387 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1388 arbitrary number of arguments for them. */
1389 if (c == '('
1390 && (GET_CODE (return_rtx) == AND
1391 || GET_CODE (return_rtx) == IOR))
1392 return read_rtx_variadic (return_rtx);
1394 unread_char (c);
1395 return return_rtx;
1398 /* Read a nested rtx construct from the MD file and return it. */
1400 static rtx
1401 read_nested_rtx (void)
1403 struct md_name name;
1404 int c;
1405 rtx return_rtx;
1407 c = read_skip_spaces ();
1408 if (c != '(')
1409 fatal_expected_char ('(', c);
1411 read_name (&name);
1412 if (strcmp (name.string, "nil") == 0)
1413 return_rtx = NULL;
1414 else
1415 return_rtx = read_rtx_code (name.string);
1417 c = read_skip_spaces ();
1418 if (c != ')')
1419 fatal_expected_char (')', c);
1421 return return_rtx;
1424 /* Mutually recursive subroutine of read_rtx which reads
1425 (thing x1 x2 x3 ...) and produces RTL as if
1426 (thing x1 (thing x2 (thing x3 ...))) had been written.
1427 When called, FORM is (thing x1 x2), and the file position
1428 is just past the leading parenthesis of x3. Only works
1429 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1430 static rtx
1431 read_rtx_variadic (rtx form)
1433 char c = '(';
1434 rtx p = form, q;
1438 unread_char (c);
1440 q = rtx_alloc (GET_CODE (p));
1441 PUT_MODE (q, GET_MODE (p));
1443 XEXP (q, 0) = XEXP (p, 1);
1444 XEXP (q, 1) = read_nested_rtx ();
1446 XEXP (p, 1) = q;
1447 p = q;
1448 c = read_skip_spaces ();
1450 while (c == '(');
1451 unread_char (c);
1452 return form;