* gcc.dg/torture/pr66345.c: Fix assumption about size_t type.
[official-gcc.git] / gcc / read-rtl.c
blobe8c849fee9bcfa1f1bed0a3901441372b0cfa284
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 /* Vector definitions for the above. */
84 typedef struct iterator_use iterator_use;
86 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
87 in a non-string rtx field. */
88 struct attribute_use {
89 /* The group that describes the use site. */
90 struct iterator_group *group;
92 /* The name of the attribute, possibly with an "iterator:" prefix. */
93 const char *value;
95 /* The location of the use, as passed to GROUP's apply_iterator callback. */
96 void *ptr;
99 /* Vector definitions for the above. */
100 typedef struct attribute_use attribute_use;
102 /* This struct is used to link subst_attr named ATTR_NAME with
103 corresponding define_subst named ITER_NAME. */
104 struct subst_attr_to_iter_mapping
106 char *attr_name;
107 char *iter_name;
110 /* Hash-table to store links between subst-attributes and
111 define_substs. */
112 htab_t subst_attr_to_iter_map = NULL;
113 /* This global stores name of subst-iterator which is currently being
114 processed. */
115 const char *current_iterator_name;
117 static void validate_const_int (const char *);
118 static rtx read_rtx_code (const char *);
119 static rtx read_nested_rtx (void);
120 static rtx read_rtx_variadic (rtx);
122 /* The mode and code iterator structures. */
123 static struct iterator_group modes, codes, ints, substs;
125 /* All iterators used in the current rtx. */
126 static vec<mapping_ptr> current_iterators;
128 /* The list of all iterator uses in the current rtx. */
129 static vec<iterator_use> iterator_uses;
131 /* The list of all attribute uses in the current rtx. */
132 static vec<attribute_use> attribute_uses;
134 /* Implementations of the iterator_group callbacks for modes. */
136 static int
137 find_mode (const char *name)
139 int i;
141 for (i = 0; i < NUM_MACHINE_MODES; i++)
142 if (strcmp (GET_MODE_NAME (i), name) == 0)
143 return i;
145 fatal_with_file_and_line ("unknown mode `%s'", name);
148 static void
149 apply_mode_iterator (void *loc, int mode)
151 PUT_MODE ((rtx) loc, (machine_mode) mode);
154 /* Implementations of the iterator_group callbacks for codes. */
156 static int
157 find_code (const char *name)
159 int i;
161 for (i = 0; i < NUM_RTX_CODE; i++)
162 if (strcmp (GET_RTX_NAME (i), name) == 0)
163 return i;
165 fatal_with_file_and_line ("unknown rtx code `%s'", name);
168 static void
169 apply_code_iterator (void *loc, int code)
171 PUT_CODE ((rtx) loc, (enum rtx_code) code);
174 /* Implementations of the iterator_group callbacks for ints. */
176 /* Since GCC does not construct a table of valid constants,
177 we have to accept any int as valid. No cross-checking can
178 be done. */
180 static int
181 find_int (const char *name)
183 validate_const_int (name);
184 return atoi (name);
187 static void
188 apply_int_iterator (void *loc, int value)
190 *(int *)loc = value;
193 /* This routine adds attribute or does nothing depending on VALUE. When
194 VALUE is 1, it does nothing - the first duplicate of original
195 template is kept untouched when it's subjected to a define_subst.
196 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
197 attribute, named exactly as define_subst, which later will be
198 applied. If such attribute has already been added, then no the
199 routine has no effect. */
200 static void
201 apply_subst_iterator (void *loc, int value)
203 rtx rt = (rtx)loc;
204 rtx new_attr;
205 rtvec attrs_vec, new_attrs_vec;
206 int i;
207 if (value == 1)
208 return;
209 gcc_assert (GET_CODE (rt) == DEFINE_INSN
210 || GET_CODE (rt) == DEFINE_EXPAND);
212 attrs_vec = XVEC (rt, 4);
214 /* If we've already added attribute 'current_iterator_name', then we
215 have nothing to do now. */
216 if (attrs_vec)
218 for (i = 0; i < GET_NUM_ELEM (attrs_vec); i++)
220 if (strcmp (XSTR (attrs_vec->elem[i], 0), current_iterator_name) == 0)
221 return;
225 /* Add attribute with subst name - it serves as a mark for
226 define_subst which later would be applied to this pattern. */
227 new_attr = rtx_alloc (SET_ATTR);
228 PUT_CODE (new_attr, SET_ATTR);
229 XSTR (new_attr, 0) = xstrdup (current_iterator_name);
230 XSTR (new_attr, 1) = xstrdup ("yes");
232 if (!attrs_vec)
234 new_attrs_vec = rtvec_alloc (1);
235 new_attrs_vec->elem[0] = new_attr;
237 else
239 new_attrs_vec = rtvec_alloc (GET_NUM_ELEM (attrs_vec) + 1);
240 memcpy (&new_attrs_vec->elem[0], &attrs_vec->elem[0],
241 GET_NUM_ELEM (attrs_vec) * sizeof (rtx));
242 new_attrs_vec->elem[GET_NUM_ELEM (attrs_vec)] = new_attr;
244 XVEC (rt, 4) = new_attrs_vec;
247 /* Map subst-attribute ATTR to subst iterator ITER. */
249 static void
250 bind_subst_iter_and_attr (const char *iter, const char *attr)
252 struct subst_attr_to_iter_mapping *value;
253 void **slot;
254 if (!subst_attr_to_iter_map)
255 subst_attr_to_iter_map =
256 htab_create (1, leading_string_hash, leading_string_eq_p, 0);
257 value = XNEW (struct subst_attr_to_iter_mapping);
258 value->attr_name = xstrdup (attr);
259 value->iter_name = xstrdup (iter);
260 slot = htab_find_slot (subst_attr_to_iter_map, value, INSERT);
261 *slot = value;
264 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
266 static char*
267 find_subst_iter_by_attr (const char *attr)
269 char *iter_name = NULL;
270 struct subst_attr_to_iter_mapping *value;
271 value = (struct subst_attr_to_iter_mapping*)
272 htab_find (subst_attr_to_iter_map, &attr);
273 if (value)
274 iter_name = value->iter_name;
275 return iter_name;
278 /* Map attribute string P to its current value. Return null if the attribute
279 isn't known. */
281 static struct map_value *
282 map_attr_string (const char *p)
284 const char *attr;
285 struct mapping *iterator;
286 unsigned int i;
287 struct mapping *m;
288 struct map_value *v;
289 int iterator_name_len;
291 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
292 attribute name. */
293 attr = strchr (p, ':');
294 if (attr == 0)
296 iterator_name_len = -1;
297 attr = p;
299 else
301 iterator_name_len = attr - p;
302 attr++;
305 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
307 /* If an iterator name was specified, check that it matches. */
308 if (iterator_name_len >= 0
309 && (strncmp (p, iterator->name, iterator_name_len) != 0
310 || iterator->name[iterator_name_len] != 0))
311 continue;
313 /* Find the attribute specification. */
314 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
315 if (m)
317 /* In contrast to code/mode/int iterators, attributes of subst
318 iterators are linked to one specific subst-iterator. So, if
319 we are dealing with subst-iterator, we should check if it's
320 the one which linked with the given attribute. */
321 if (iterator->group == &substs)
323 char *iter_name = find_subst_iter_by_attr (attr);
324 if (strcmp (iter_name, iterator->name) != 0)
325 continue;
327 /* Find the attribute value associated with the current
328 iterator value. */
329 for (v = m->values; v; v = v->next)
330 if (v->number == iterator->current_value->number)
331 return v;
334 return NULL;
337 /* Apply the current iterator values to STRING. Return the new string
338 if any changes were needed, otherwise return STRING itself. */
340 static const char *
341 apply_iterator_to_string (const char *string)
343 char *base, *copy, *p, *start, *end;
344 struct map_value *v;
346 if (string == 0)
347 return string;
349 base = p = copy = ASTRDUP (string);
350 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
352 p = start + 1;
354 *end = 0;
355 v = map_attr_string (p);
356 *end = '>';
357 if (v == 0)
358 continue;
360 /* Add everything between the last copied byte and the '<',
361 then add in the attribute value. */
362 obstack_grow (&string_obstack, base, start - base);
363 obstack_grow (&string_obstack, v->string, strlen (v->string));
364 base = end + 1;
366 if (base != copy)
368 obstack_grow (&string_obstack, base, strlen (base) + 1);
369 copy = XOBFINISH (&string_obstack, char *);
370 copy_md_ptr_loc (copy, string);
371 return copy;
373 return string;
376 /* Return a deep copy of X, substituting the current iterator
377 values into any strings. */
379 static rtx
380 copy_rtx_for_iterators (rtx original)
382 const char *format_ptr, *p;
383 int i, j;
384 rtx x;
386 if (original == 0)
387 return original;
389 /* Create a shallow copy of ORIGINAL. */
390 x = rtx_alloc (GET_CODE (original));
391 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
393 /* Change each string and recursively change each rtx. */
394 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
395 for (i = 0; format_ptr[i] != 0; i++)
396 switch (format_ptr[i])
398 case 'T':
399 while (XTMPL (x, i) != (p = apply_iterator_to_string (XTMPL (x, i))))
400 XTMPL (x, i) = p;
401 break;
403 case 'S':
404 case 's':
405 while (XSTR (x, i) != (p = apply_iterator_to_string (XSTR (x, i))))
406 XSTR (x, i) = p;
407 break;
409 case 'e':
410 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
411 break;
413 case 'V':
414 case 'E':
415 if (XVEC (original, i))
417 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
418 for (j = 0; j < XVECLEN (x, i); j++)
419 XVECEXP (x, i, j)
420 = copy_rtx_for_iterators (XVECEXP (original, i, j));
422 break;
424 default:
425 break;
427 return x;
430 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
431 has the form "&& ..." (as used in define_insn_and_splits), assume that
432 EXTRA is already satisfied. Empty strings are treated like "true". */
434 static const char *
435 add_condition_to_string (const char *original, const char *extra)
437 if (original != 0 && original[0] == '&' && original[1] == '&')
438 return original;
439 return join_c_conditions (original, extra);
442 /* Like add_condition, but applied to all conditions in rtx X. */
444 static void
445 add_condition_to_rtx (rtx x, const char *extra)
447 switch (GET_CODE (x))
449 case DEFINE_INSN:
450 case DEFINE_EXPAND:
451 case DEFINE_SUBST:
452 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
453 break;
455 case DEFINE_SPLIT:
456 case DEFINE_PEEPHOLE:
457 case DEFINE_PEEPHOLE2:
458 case DEFINE_COND_EXEC:
459 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
460 break;
462 case DEFINE_INSN_AND_SPLIT:
463 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
464 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
465 break;
467 default:
468 break;
472 /* Apply the current iterator values to all attribute_uses. */
474 static void
475 apply_attribute_uses (void)
477 struct map_value *v;
478 attribute_use *ause;
479 unsigned int i;
481 FOR_EACH_VEC_ELT (attribute_uses, i, ause)
483 v = map_attr_string (ause->value);
484 if (!v)
485 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
486 ause->group->apply_iterator (ause->ptr,
487 ause->group->find_builtin (v->string));
491 /* A htab_traverse callback for iterators. Add all used iterators
492 to current_iterators. */
494 static int
495 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
497 struct mapping *iterator;
499 iterator = (struct mapping *) *slot;
500 if (iterator->current_value)
501 current_iterators.safe_push (iterator);
502 return 1;
505 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
506 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
508 static void
509 apply_iterators (rtx original, rtx *queue)
511 unsigned int i;
512 const char *condition;
513 iterator_use *iuse;
514 struct mapping *iterator;
515 struct map_value *v;
516 rtx x;
518 if (iterator_uses.is_empty ())
520 /* Raise an error if any attributes were used. */
521 apply_attribute_uses ();
522 XEXP (*queue, 0) = original;
523 XEXP (*queue, 1) = NULL_RTX;
524 return;
527 /* Clear out the iterators from the previous run. */
528 FOR_EACH_VEC_ELT (current_iterators, i, iterator)
529 iterator->current_value = NULL;
530 current_iterators.truncate (0);
532 /* Mark the iterators that we need this time. */
533 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
534 iuse->iterator->current_value = iuse->iterator->values;
536 /* Get the list of iterators that are in use, preserving the
537 definition order within each group. */
538 htab_traverse (modes.iterators, add_current_iterators, NULL);
539 htab_traverse (codes.iterators, add_current_iterators, NULL);
540 htab_traverse (ints.iterators, add_current_iterators, NULL);
541 htab_traverse (substs.iterators, add_current_iterators, NULL);
542 gcc_assert (!current_iterators.is_empty ());
544 for (;;)
546 /* Apply the current iterator values. Accumulate a condition to
547 say when the resulting rtx can be used. */
548 condition = "";
549 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
551 if (iuse->iterator->group == &substs)
552 continue;
553 v = iuse->iterator->current_value;
554 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
555 condition = join_c_conditions (condition, v->string);
557 apply_attribute_uses ();
558 x = copy_rtx_for_iterators (original);
559 add_condition_to_rtx (x, condition);
561 /* We apply subst iterator after RTL-template is copied, as during
562 subst-iterator processing, we could add an attribute to the
563 RTL-template, and we don't want to do it in the original one. */
564 FOR_EACH_VEC_ELT (iterator_uses, i, iuse)
566 v = iuse->iterator->current_value;
567 if (iuse->iterator->group == &substs)
569 iuse->ptr = x;
570 current_iterator_name = iuse->iterator->name;
571 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
574 /* Add the new rtx to the end of the queue. */
575 XEXP (*queue, 0) = x;
576 XEXP (*queue, 1) = NULL_RTX;
578 /* Lexicographically increment the iterator value sequence.
579 That is, cycle through iterator values, starting from the right,
580 and stopping when one of them doesn't wrap around. */
581 i = current_iterators.length ();
582 for (;;)
584 if (i == 0)
585 return;
586 i--;
587 iterator = current_iterators[i];
588 iterator->current_value = iterator->current_value->next;
589 if (iterator->current_value)
590 break;
591 iterator->current_value = iterator->values;
594 /* At least one more rtx to go. Allocate room for it. */
595 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
596 queue = &XEXP (*queue, 1);
600 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
601 of the mapping and GROUP is the group to which it belongs. */
603 static struct mapping *
604 add_mapping (struct iterator_group *group, htab_t table, const char *name)
606 struct mapping *m;
607 void **slot;
609 m = XNEW (struct mapping);
610 m->name = xstrdup (name);
611 m->group = group;
612 m->values = 0;
613 m->current_value = NULL;
615 slot = htab_find_slot (table, m, INSERT);
616 if (*slot != 0)
617 fatal_with_file_and_line ("`%s' already defined", name);
619 *slot = m;
620 return m;
623 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
624 END_PTR points to the current null terminator for the list; return
625 a pointer the new null terminator. */
627 static struct map_value **
628 add_map_value (struct map_value **end_ptr, int number, const char *string)
630 struct map_value *value;
632 value = XNEW (struct map_value);
633 value->next = 0;
634 value->number = number;
635 value->string = string;
637 *end_ptr = value;
638 return &value->next;
641 /* Do one-time initialization of the mode and code attributes. */
643 static void
644 initialize_iterators (void)
646 struct mapping *lower, *upper;
647 struct map_value **lower_ptr, **upper_ptr;
648 char *copy, *p;
649 int i;
651 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
652 modes.iterators = htab_create (13, leading_string_hash,
653 leading_string_eq_p, 0);
654 modes.find_builtin = find_mode;
655 modes.apply_iterator = apply_mode_iterator;
657 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
658 codes.iterators = htab_create (13, leading_string_hash,
659 leading_string_eq_p, 0);
660 codes.find_builtin = find_code;
661 codes.apply_iterator = apply_code_iterator;
663 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
664 ints.iterators = htab_create (13, leading_string_hash,
665 leading_string_eq_p, 0);
666 ints.find_builtin = find_int;
667 ints.apply_iterator = apply_int_iterator;
669 substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
670 substs.iterators = htab_create (13, leading_string_hash,
671 leading_string_eq_p, 0);
672 substs.find_builtin = find_int; /* We don't use it, anyway. */
673 substs.apply_iterator = apply_subst_iterator;
675 lower = add_mapping (&modes, modes.attrs, "mode");
676 upper = add_mapping (&modes, modes.attrs, "MODE");
677 lower_ptr = &lower->values;
678 upper_ptr = &upper->values;
679 for (i = 0; i < MAX_MACHINE_MODE; i++)
681 copy = xstrdup (GET_MODE_NAME (i));
682 for (p = copy; *p != 0; p++)
683 *p = TOLOWER (*p);
685 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
686 lower_ptr = add_map_value (lower_ptr, i, copy);
689 lower = add_mapping (&codes, codes.attrs, "code");
690 upper = add_mapping (&codes, codes.attrs, "CODE");
691 lower_ptr = &lower->values;
692 upper_ptr = &upper->values;
693 for (i = 0; i < NUM_RTX_CODE; i++)
695 copy = xstrdup (GET_RTX_NAME (i));
696 for (p = copy; *p != 0; p++)
697 *p = TOUPPER (*p);
699 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
700 upper_ptr = add_map_value (upper_ptr, i, copy);
704 /* Provide a version of a function to read a long long if the system does
705 not provide one. */
706 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
707 HOST_WIDE_INT atoll (const char *);
709 HOST_WIDE_INT
710 atoll (const char *p)
712 int neg = 0;
713 HOST_WIDE_INT tmp_wide;
715 while (ISSPACE (*p))
716 p++;
717 if (*p == '-')
718 neg = 1, p++;
719 else if (*p == '+')
720 p++;
722 tmp_wide = 0;
723 while (ISDIGIT (*p))
725 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
726 if (new_wide < tmp_wide)
728 /* Return INT_MAX equiv on overflow. */
729 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
730 break;
732 tmp_wide = new_wide;
733 p++;
736 if (neg)
737 tmp_wide = -tmp_wide;
738 return tmp_wide;
740 #endif
742 /* Process a define_conditions directive, starting with the optional
743 space after the "define_conditions". The directive looks like this:
745 (define_conditions [
746 (number "string")
747 (number "string")
751 It's not intended to appear in machine descriptions. It is
752 generated by (the program generated by) genconditions.c, and
753 slipped in at the beginning of the sequence of MD files read by
754 most of the other generators. */
755 static void
756 read_conditions (void)
758 int c;
760 c = read_skip_spaces ();
761 if (c != '[')
762 fatal_expected_char ('[', c);
764 while ( (c = read_skip_spaces ()) != ']')
766 struct md_name name;
767 char *expr;
768 int value;
770 if (c != '(')
771 fatal_expected_char ('(', c);
773 read_name (&name);
774 validate_const_int (name.string);
775 value = atoi (name.string);
777 c = read_skip_spaces ();
778 if (c != '"')
779 fatal_expected_char ('"', c);
780 expr = read_quoted_string ();
782 c = read_skip_spaces ();
783 if (c != ')')
784 fatal_expected_char (')', c);
786 add_c_test (expr, value);
790 static void
791 validate_const_int (const char *string)
793 const char *cp;
794 int valid = 1;
796 cp = string;
797 while (*cp && ISSPACE (*cp))
798 cp++;
799 if (*cp == '-' || *cp == '+')
800 cp++;
801 if (*cp == 0)
802 valid = 0;
803 for (; *cp; cp++)
804 if (! ISDIGIT (*cp))
806 valid = 0;
807 break;
809 if (!valid)
810 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
813 static void
814 validate_const_wide_int (const char *string)
816 const char *cp;
817 int valid = 1;
819 cp = string;
820 while (*cp && ISSPACE (*cp))
821 cp++;
822 /* Skip the leading 0x. */
823 if (cp[0] == '0' || cp[1] == 'x')
824 cp += 2;
825 else
826 valid = 0;
827 if (*cp == 0)
828 valid = 0;
829 for (; *cp; cp++)
830 if (! ISXDIGIT (*cp))
831 valid = 0;
832 if (!valid)
833 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string);
836 /* Record that PTR uses iterator ITERATOR. */
838 static void
839 record_iterator_use (struct mapping *iterator, void *ptr)
841 struct iterator_use iuse = {iterator, ptr};
842 iterator_uses.safe_push (iuse);
845 /* Record that PTR uses attribute VALUE, which must match a built-in
846 value from group GROUP. */
848 static void
849 record_attribute_use (struct iterator_group *group, void *ptr,
850 const char *value)
852 struct attribute_use ause = {group, value, ptr};
853 attribute_uses.safe_push (ause);
856 /* Interpret NAME as either a built-in value, iterator or attribute
857 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
858 callback. */
860 static void
861 record_potential_iterator_use (struct iterator_group *group, void *ptr,
862 const char *name)
864 struct mapping *m;
865 size_t len;
867 len = strlen (name);
868 if (name[0] == '<' && name[len - 1] == '>')
870 /* Copy the attribute string into permanent storage, without the
871 angle brackets around it. */
872 obstack_grow0 (&string_obstack, name + 1, len - 2);
873 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
875 else
877 m = (struct mapping *) htab_find (group->iterators, &name);
878 if (m != 0)
879 record_iterator_use (m, ptr);
880 else
881 group->apply_iterator (ptr, group->find_builtin (name));
885 /* Finish reading a declaration of the form:
887 (define... <name> [<value1> ... <valuen>])
889 from the MD file, where each <valuei> is either a bare symbol name or a
890 "(<name> <string>)" pair. The "(define..." part has already been read.
892 Represent the declaration as a "mapping" structure; add it to TABLE
893 (which belongs to GROUP) and return it. */
895 static struct mapping *
896 read_mapping (struct iterator_group *group, htab_t table)
898 struct md_name name;
899 struct mapping *m;
900 struct map_value **end_ptr;
901 const char *string;
902 int number, c;
904 /* Read the mapping name and create a structure for it. */
905 read_name (&name);
906 m = add_mapping (group, table, name.string);
908 c = read_skip_spaces ();
909 if (c != '[')
910 fatal_expected_char ('[', c);
912 /* Read each value. */
913 end_ptr = &m->values;
914 c = read_skip_spaces ();
917 if (c != '(')
919 /* A bare symbol name that is implicitly paired to an
920 empty string. */
921 unread_char (c);
922 read_name (&name);
923 string = "";
925 else
927 /* A "(name string)" pair. */
928 read_name (&name);
929 string = read_string (false);
930 c = read_skip_spaces ();
931 if (c != ')')
932 fatal_expected_char (')', c);
934 number = group->find_builtin (name.string);
935 end_ptr = add_map_value (end_ptr, number, string);
936 c = read_skip_spaces ();
938 while (c != ']');
940 return m;
943 /* For iterator with name ATTR_NAME generate define_attr with values
944 'yes' and 'no'. This attribute is used to mark templates to which
945 define_subst ATTR_NAME should be applied. This attribute is set and
946 defined implicitly and automatically. */
947 static void
948 add_define_attr_for_define_subst (const char *attr_name, rtx *queue)
950 rtx const_str, return_rtx;
952 return_rtx = rtx_alloc (DEFINE_ATTR);
953 PUT_CODE (return_rtx, DEFINE_ATTR);
955 const_str = rtx_alloc (CONST_STRING);
956 PUT_CODE (const_str, CONST_STRING);
957 XSTR (const_str, 0) = xstrdup ("no");
959 XSTR (return_rtx, 0) = xstrdup (attr_name);
960 XSTR (return_rtx, 1) = xstrdup ("no,yes");
961 XEXP (return_rtx, 2) = const_str;
963 XEXP (*queue, 0) = return_rtx;
964 XEXP (*queue, 1) = NULL_RTX;
967 /* This routine generates DEFINE_SUBST_ATTR expression with operands
968 ATTR_OPERANDS and places it to QUEUE. */
969 static void
970 add_define_subst_attr (const char **attr_operands, rtx *queue)
972 rtx return_rtx;
973 int i;
975 return_rtx = rtx_alloc (DEFINE_SUBST_ATTR);
976 PUT_CODE (return_rtx, DEFINE_SUBST_ATTR);
978 for (i = 0; i < 4; i++)
979 XSTR (return_rtx, i) = xstrdup (attr_operands[i]);
981 XEXP (*queue, 0) = return_rtx;
982 XEXP (*queue, 1) = NULL_RTX;
985 /* Read define_subst_attribute construction. It has next form:
986 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
987 Attribute is substituted with value1 when no subst is applied and with
988 value2 in the opposite case.
989 Attributes are added to SUBST_ATTRS_TABLE.
990 In case the iterator is encountered for the first time, it's added to
991 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
993 static void
994 read_subst_mapping (htab_t subst_iters_table, htab_t subst_attrs_table,
995 rtx *queue)
997 struct mapping *m;
998 struct map_value **end_ptr;
999 const char *attr_operands[4];
1000 rtx * queue_elem = queue;
1001 int i;
1003 for (i = 0; i < 4; i++)
1004 attr_operands[i] = read_string (false);
1006 add_define_subst_attr (attr_operands, queue_elem);
1008 bind_subst_iter_and_attr (attr_operands[1], attr_operands[0]);
1010 m = (struct mapping *) htab_find (substs.iterators, &attr_operands[1]);
1011 if (!m)
1013 m = add_mapping (&substs, subst_iters_table, attr_operands[1]);
1014 end_ptr = &m->values;
1015 end_ptr = add_map_value (end_ptr, 1, "");
1016 end_ptr = add_map_value (end_ptr, 2, "");
1018 /* Add element to the queue. */
1019 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
1020 queue_elem = &XEXP (*queue, 1);
1022 add_define_attr_for_define_subst (attr_operands[1], queue_elem);
1025 m = add_mapping (&substs, subst_attrs_table, attr_operands[0]);
1026 end_ptr = &m->values;
1027 end_ptr = add_map_value (end_ptr, 1, attr_operands[2]);
1028 end_ptr = add_map_value (end_ptr, 2, attr_operands[3]);
1031 /* Check newly-created code iterator ITERATOR to see whether every code has the
1032 same format. */
1034 static void
1035 check_code_iterator (struct mapping *iterator)
1037 struct map_value *v;
1038 enum rtx_code bellwether;
1040 bellwether = (enum rtx_code) iterator->values->number;
1041 for (v = iterator->values->next; v != 0; v = v->next)
1042 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
1043 fatal_with_file_and_line ("code iterator `%s' combines "
1044 "different rtx formats", iterator->name);
1047 /* Read an rtx-related declaration from the MD file, given that it
1048 starts with directive name RTX_NAME. Return true if it expands to
1049 one or more rtxes (as defined by rtx.def). When returning true,
1050 store the list of rtxes as an EXPR_LIST in *X. */
1052 bool
1053 read_rtx (const char *rtx_name, rtx *x)
1055 static rtx queue_head;
1057 /* Do one-time initialization. */
1058 if (queue_head == 0)
1060 initialize_iterators ();
1061 queue_head = rtx_alloc (EXPR_LIST);
1064 /* Handle various rtx-related declarations that aren't themselves
1065 encoded as rtxes. */
1066 if (strcmp (rtx_name, "define_conditions") == 0)
1068 read_conditions ();
1069 return false;
1071 if (strcmp (rtx_name, "define_mode_attr") == 0)
1073 read_mapping (&modes, modes.attrs);
1074 return false;
1076 if (strcmp (rtx_name, "define_mode_iterator") == 0)
1078 read_mapping (&modes, modes.iterators);
1079 return false;
1081 if (strcmp (rtx_name, "define_code_attr") == 0)
1083 read_mapping (&codes, codes.attrs);
1084 return false;
1086 if (strcmp (rtx_name, "define_code_iterator") == 0)
1088 check_code_iterator (read_mapping (&codes, codes.iterators));
1089 return false;
1091 if (strcmp (rtx_name, "define_int_attr") == 0)
1093 read_mapping (&ints, ints.attrs);
1094 return false;
1096 if (strcmp (rtx_name, "define_int_iterator") == 0)
1098 read_mapping (&ints, ints.iterators);
1099 return false;
1101 if (strcmp (rtx_name, "define_subst_attr") == 0)
1103 read_subst_mapping (substs.iterators, substs.attrs, &queue_head);
1104 *x = queue_head;
1106 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1107 TRUE to process it. */
1108 return true;
1111 apply_iterators (read_rtx_code (rtx_name), &queue_head);
1112 iterator_uses.truncate (0);
1113 attribute_uses.truncate (0);
1115 *x = queue_head;
1116 return true;
1119 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1120 either an rtx code or a code iterator. Parse the rest of the rtx and
1121 return it. */
1123 static rtx
1124 read_rtx_code (const char *code_name)
1126 int i;
1127 RTX_CODE code;
1128 struct mapping *iterator, *m;
1129 const char *format_ptr;
1130 struct md_name name;
1131 rtx return_rtx;
1132 int c;
1133 HOST_WIDE_INT tmp_wide;
1134 char *str;
1135 char *start, *end, *ptr;
1136 char tmpstr[256];
1138 /* Linked list structure for making RTXs: */
1139 struct rtx_list
1141 struct rtx_list *next;
1142 rtx value; /* Value of this node. */
1145 /* If this code is an iterator, build the rtx using the iterator's
1146 first value. */
1147 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
1148 if (iterator != 0)
1149 code = (enum rtx_code) iterator->values->number;
1150 else
1151 code = (enum rtx_code) codes.find_builtin (code_name);
1153 /* If we end up with an insn expression then we free this space below. */
1154 return_rtx = rtx_alloc (code);
1155 format_ptr = GET_RTX_FORMAT (code);
1156 memset (return_rtx, 0, RTX_CODE_SIZE (code));
1157 PUT_CODE (return_rtx, code);
1159 if (iterator)
1160 record_iterator_use (iterator, return_rtx);
1162 /* If what follows is `: mode ', read it and
1163 store the mode in the rtx. */
1165 i = read_skip_spaces ();
1166 if (i == ':')
1168 read_name (&name);
1169 record_potential_iterator_use (&modes, return_rtx, name.string);
1171 else
1172 unread_char (i);
1174 for (i = 0; format_ptr[i] != 0; i++)
1175 switch (format_ptr[i])
1177 /* 0 means a field for internal use only.
1178 Don't expect it to be present in the input. */
1179 case '0':
1180 if (code == REG)
1181 ORIGINAL_REGNO (return_rtx) = REGNO (return_rtx);
1182 break;
1184 case 'e':
1185 case 'u':
1186 XEXP (return_rtx, i) = read_nested_rtx ();
1187 break;
1189 case 'V':
1190 /* 'V' is an optional vector: if a closeparen follows,
1191 just store NULL for this element. */
1192 c = read_skip_spaces ();
1193 unread_char (c);
1194 if (c == ')')
1196 XVEC (return_rtx, i) = 0;
1197 break;
1199 /* Now process the vector. */
1201 case 'E':
1203 /* Obstack to store scratch vector in. */
1204 struct obstack vector_stack;
1205 int list_counter = 0;
1206 rtvec return_vec = NULL_RTVEC;
1208 c = read_skip_spaces ();
1209 if (c != '[')
1210 fatal_expected_char ('[', c);
1212 /* Add expressions to a list, while keeping a count. */
1213 obstack_init (&vector_stack);
1214 while ((c = read_skip_spaces ()) && c != ']')
1216 if (c == EOF)
1217 fatal_expected_char (']', c);
1218 unread_char (c);
1219 list_counter++;
1220 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
1222 if (list_counter > 0)
1224 return_vec = rtvec_alloc (list_counter);
1225 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
1226 list_counter * sizeof (rtx));
1228 else if (format_ptr[i] == 'E')
1229 fatal_with_file_and_line ("vector must have at least one element");
1230 XVEC (return_rtx, i) = return_vec;
1231 obstack_free (&vector_stack, NULL);
1232 /* close bracket gotten */
1234 break;
1236 case 'S':
1237 case 'T':
1238 case 's':
1240 char *stringbuf;
1241 int star_if_braced;
1243 c = read_skip_spaces ();
1244 unread_char (c);
1245 if (c == ')')
1247 /* 'S' fields are optional and should be NULL if no string
1248 was given. Also allow normal 's' and 'T' strings to be
1249 omitted, treating them in the same way as empty strings. */
1250 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1251 break;
1254 /* The output template slot of a DEFINE_INSN,
1255 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1256 gets a star inserted as its first character, if it is
1257 written with a brace block instead of a string constant. */
1258 star_if_braced = (format_ptr[i] == 'T');
1260 stringbuf = read_string (star_if_braced);
1262 /* For insn patterns, we want to provide a default name
1263 based on the file and line, like "*foo.md:12", if the
1264 given name is blank. These are only for define_insn and
1265 define_insn_and_split, to aid debugging. */
1266 if (*stringbuf == '\0'
1267 && i == 0
1268 && (GET_CODE (return_rtx) == DEFINE_INSN
1269 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1271 char line_name[20];
1272 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1273 const char *slash;
1274 for (slash = fn; *slash; slash ++)
1275 if (*slash == '/' || *slash == '\\' || *slash == ':')
1276 fn = slash + 1;
1277 obstack_1grow (&string_obstack, '*');
1278 obstack_grow (&string_obstack, fn, strlen (fn));
1279 sprintf (line_name, ":%d", read_md_lineno);
1280 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1281 stringbuf = XOBFINISH (&string_obstack, char *);
1284 /* Find attr-names in the string. */
1285 ptr = &tmpstr[0];
1286 end = stringbuf;
1287 while ((start = strchr (end, '<')) && (end = strchr (start, '>')))
1289 if ((end - start - 1 > 0)
1290 && (end - start - 1 < (int)sizeof (tmpstr)))
1292 strncpy (tmpstr, start+1, end-start-1);
1293 tmpstr[end-start-1] = 0;
1294 end++;
1296 else
1297 break;
1298 m = (struct mapping *) htab_find (substs.attrs, &ptr);
1299 if (m != 0)
1301 /* Here we should find linked subst-iter. */
1302 str = find_subst_iter_by_attr (ptr);
1303 if (str)
1304 m = (struct mapping *) htab_find (substs.iterators, &str);
1305 else
1306 m = 0;
1308 if (m != 0)
1309 record_iterator_use (m, return_rtx);
1312 if (star_if_braced)
1313 XTMPL (return_rtx, i) = stringbuf;
1314 else
1315 XSTR (return_rtx, i) = stringbuf;
1317 break;
1319 case 'w':
1320 read_name (&name);
1321 validate_const_int (name.string);
1322 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1323 tmp_wide = atoi (name.string);
1324 #else
1325 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1326 tmp_wide = atol (name.string);
1327 #else
1328 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1329 But prefer not to use our hand-rolled function above either. */
1330 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1331 tmp_wide = atoll (name.string);
1332 #else
1333 tmp_wide = atoq (name.string);
1334 #endif
1335 #endif
1336 #endif
1337 XWINT (return_rtx, i) = tmp_wide;
1338 break;
1340 case 'i':
1341 case 'n':
1342 /* Can be an iterator or an integer constant. */
1343 read_name (&name);
1344 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1345 name.string);
1346 break;
1348 case 'r':
1349 read_name (&name);
1350 validate_const_int (name.string);
1351 set_regno_raw (return_rtx, atoi (name.string), 1);
1352 REG_ATTRS (return_rtx) = NULL;
1353 break;
1355 default:
1356 gcc_unreachable ();
1359 if (CONST_WIDE_INT_P (return_rtx))
1361 read_name (&name);
1362 validate_const_wide_int (name.string);
1364 const char *s = name.string;
1365 int len;
1366 int index = 0;
1367 int gs = HOST_BITS_PER_WIDE_INT/4;
1368 int pos;
1369 char * buf = XALLOCAVEC (char, gs + 1);
1370 unsigned HOST_WIDE_INT wi;
1371 int wlen;
1373 /* Skip the leading spaces. */
1374 while (*s && ISSPACE (*s))
1375 s++;
1377 /* Skip the leading 0x. */
1378 gcc_assert (s[0] == '0');
1379 gcc_assert (s[1] == 'x');
1380 s += 2;
1382 len = strlen (s);
1383 pos = len - gs;
1384 wlen = (len + gs - 1) / gs; /* Number of words needed */
1386 return_rtx = const_wide_int_alloc (wlen);
1388 while (pos > 0)
1390 #if HOST_BITS_PER_WIDE_INT == 64
1391 sscanf (s + pos, "%16" HOST_WIDE_INT_PRINT "x", &wi);
1392 #else
1393 sscanf (s + pos, "%8" HOST_WIDE_INT_PRINT "x", &wi);
1394 #endif
1395 CWI_ELT (return_rtx, index++) = wi;
1396 pos -= gs;
1398 strncpy (buf, s, gs - pos);
1399 buf [gs - pos] = 0;
1400 sscanf (buf, "%" HOST_WIDE_INT_PRINT "x", &wi);
1401 CWI_ELT (return_rtx, index++) = wi;
1402 /* TODO: After reading, do we want to canonicalize with:
1403 value = lookup_const_wide_int (value); ? */
1407 c = read_skip_spaces ();
1408 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1409 arbitrary number of arguments for them. */
1410 if (c == '('
1411 && (GET_CODE (return_rtx) == AND
1412 || GET_CODE (return_rtx) == IOR))
1413 return read_rtx_variadic (return_rtx);
1415 unread_char (c);
1416 return return_rtx;
1419 /* Read a nested rtx construct from the MD file and return it. */
1421 static rtx
1422 read_nested_rtx (void)
1424 struct md_name name;
1425 int c;
1426 rtx return_rtx;
1428 c = read_skip_spaces ();
1429 if (c != '(')
1430 fatal_expected_char ('(', c);
1432 read_name (&name);
1433 if (strcmp (name.string, "nil") == 0)
1434 return_rtx = NULL;
1435 else
1436 return_rtx = read_rtx_code (name.string);
1438 c = read_skip_spaces ();
1439 if (c != ')')
1440 fatal_expected_char (')', c);
1442 return return_rtx;
1445 /* Mutually recursive subroutine of read_rtx which reads
1446 (thing x1 x2 x3 ...) and produces RTL as if
1447 (thing x1 (thing x2 (thing x3 ...))) had been written.
1448 When called, FORM is (thing x1 x2), and the file position
1449 is just past the leading parenthesis of x3. Only works
1450 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1451 static rtx
1452 read_rtx_variadic (rtx form)
1454 char c = '(';
1455 rtx p = form, q;
1459 unread_char (c);
1461 q = rtx_alloc (GET_CODE (p));
1462 PUT_MODE (q, GET_MODE (p));
1464 XEXP (q, 0) = XEXP (p, 1);
1465 XEXP (q, 1) = read_nested_rtx ();
1467 XEXP (p, 1) = q;
1468 p = q;
1469 c = read_skip_spaces ();
1471 while (c == '(');
1472 unread_char (c);
1473 return form;