ChangeLog/
[official-gcc.git] / gcc / read-rtl.c
blob30c2fb69484eaf5f42016d8e21fbd07dd1ef415a
1 /* RTL reader for GCC.
2 Copyright (C) 1987, 1988, 1991, 1994, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "bconfig.h"
24 /* Disable rtl checking; it conflicts with the iterator handling. */
25 #undef ENABLE_RTL_CHECKING
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "obstack.h"
32 #include "hashtab.h"
33 #include "read-md.h"
34 #include "gensupport.h"
36 /* One element in a singly-linked list of (integer, string) pairs. */
37 struct map_value {
38 struct map_value *next;
39 int number;
40 const char *string;
43 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
44 The integers are iterator values; the strings are either C conditions
45 or attribute values. */
46 struct mapping {
47 /* The name of the iterator or attribute. */
48 const char *name;
50 /* The group (modes or codes) to which the iterator or attribute belongs. */
51 struct iterator_group *group;
53 /* The list of (integer, string) pairs. */
54 struct map_value *values;
56 /* For iterators, records the current value of the iterator. */
57 struct map_value *current_value;
60 /* Vector definitions for the above. */
61 typedef struct mapping *mapping_ptr;
62 DEF_VEC_P (mapping_ptr);
63 DEF_VEC_ALLOC_P (mapping_ptr, heap);
65 /* A structure for abstracting the common parts of iterators. */
66 struct iterator_group {
67 /* Tables of "mapping" structures, one for attributes and one for
68 iterators. */
69 htab_t attrs, iterators;
71 /* Treat the given string as the name of a standard mode, etc., and
72 return its integer value. */
73 int (*find_builtin) (const char *);
75 /* Make the given pointer use the given iterator value. */
76 void (*apply_iterator) (void *, int);
79 /* Records one use of an iterator. */
80 struct iterator_use {
81 /* The iterator itself. */
82 struct mapping *iterator;
84 /* The location of the use, as passed to the apply_iterator callback. */
85 void *ptr;
88 /* Vector definitions for the above. */
89 typedef struct iterator_use iterator_use;
90 DEF_VEC_O (iterator_use);
91 DEF_VEC_ALLOC_O (iterator_use, heap);
93 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
94 in a non-string rtx field. */
95 struct attribute_use {
96 /* The group that describes the use site. */
97 struct iterator_group *group;
99 /* The name of the attribute, possibly with an "iterator:" prefix. */
100 const char *value;
102 /* The location of the use, as passed to GROUP's apply_iterator callback. */
103 void *ptr;
106 /* Vector definitions for the above. */
107 typedef struct attribute_use attribute_use;
108 DEF_VEC_O (attribute_use);
109 DEF_VEC_ALLOC_O (attribute_use, heap);
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;
119 /* All iterators used in the current rtx. */
120 static VEC (mapping_ptr, heap) *current_iterators;
122 /* The list of all iterator uses in the current rtx. */
123 static VEC (iterator_use, heap) *iterator_uses;
125 /* The list of all attribute uses in the current rtx. */
126 static VEC (attribute_use, heap) *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, (enum 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 /* Map attribute string P to its current value. Return null if the attribute
188 isn't known. */
190 static struct map_value *
191 map_attr_string (const char *p)
193 const char *attr;
194 struct mapping *iterator;
195 unsigned int i;
196 struct mapping *m;
197 struct map_value *v;
198 int iterator_name_len;
200 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
201 attribute name. */
202 attr = strchr (p, ':');
203 if (attr == 0)
205 iterator_name_len = -1;
206 attr = p;
208 else
210 iterator_name_len = attr - p;
211 attr++;
214 FOR_EACH_VEC_ELT (mapping_ptr, current_iterators, i, iterator)
216 /* If an iterator name was specified, check that it matches. */
217 if (iterator_name_len >= 0
218 && (strncmp (p, iterator->name, iterator_name_len) != 0
219 || iterator->name[iterator_name_len] != 0))
220 continue;
222 /* Find the attribute specification. */
223 m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
224 if (m)
225 /* Find the attribute value associated with the current
226 iterator value. */
227 for (v = m->values; v; v = v->next)
228 if (v->number == iterator->current_value->number)
229 return v;
231 return NULL;
234 /* Apply the current iterator values to STRING. Return the new string
235 if any changes were needed, otherwise return STRING itself. */
237 static const char *
238 apply_iterator_to_string (const char *string)
240 char *base, *copy, *p, *start, *end;
241 struct map_value *v;
243 if (string == 0)
244 return string;
246 base = p = copy = ASTRDUP (string);
247 while ((start = strchr (p, '<')) && (end = strchr (start, '>')))
249 p = start + 1;
251 *end = 0;
252 v = map_attr_string (p);
253 *end = '>';
254 if (v == 0)
255 continue;
257 /* Add everything between the last copied byte and the '<',
258 then add in the attribute value. */
259 obstack_grow (&string_obstack, base, start - base);
260 obstack_grow (&string_obstack, v->string, strlen (v->string));
261 base = end + 1;
263 if (base != copy)
265 obstack_grow (&string_obstack, base, strlen (base) + 1);
266 copy = XOBFINISH (&string_obstack, char *);
267 copy_md_ptr_loc (copy, string);
268 return copy;
270 return string;
273 /* Return a deep copy of X, substituting the current iterator
274 values into any strings. */
276 static rtx
277 copy_rtx_for_iterators (rtx original)
279 const char *format_ptr;
280 int i, j;
281 rtx x;
283 if (original == 0)
284 return original;
286 /* Create a shallow copy of ORIGINAL. */
287 x = rtx_alloc (GET_CODE (original));
288 memcpy (x, original, RTX_CODE_SIZE (GET_CODE (original)));
290 /* Change each string and recursively change each rtx. */
291 format_ptr = GET_RTX_FORMAT (GET_CODE (original));
292 for (i = 0; format_ptr[i] != 0; i++)
293 switch (format_ptr[i])
295 case 'T':
296 XTMPL (x, i) = apply_iterator_to_string (XTMPL (x, i));
297 break;
299 case 'S':
300 case 's':
301 XSTR (x, i) = apply_iterator_to_string (XSTR (x, i));
302 break;
304 case 'e':
305 XEXP (x, i) = copy_rtx_for_iterators (XEXP (x, i));
306 break;
308 case 'V':
309 case 'E':
310 if (XVEC (original, i))
312 XVEC (x, i) = rtvec_alloc (XVECLEN (original, i));
313 for (j = 0; j < XVECLEN (x, i); j++)
314 XVECEXP (x, i, j)
315 = copy_rtx_for_iterators (XVECEXP (original, i, j));
317 break;
319 default:
320 break;
322 return x;
325 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
326 has the form "&& ..." (as used in define_insn_and_splits), assume that
327 EXTRA is already satisfied. Empty strings are treated like "true". */
329 static const char *
330 add_condition_to_string (const char *original, const char *extra)
332 if (original != 0 && original[0] == '&' && original[1] == '&')
333 return original;
334 return join_c_conditions (original, extra);
337 /* Like add_condition, but applied to all conditions in rtx X. */
339 static void
340 add_condition_to_rtx (rtx x, const char *extra)
342 switch (GET_CODE (x))
344 case DEFINE_INSN:
345 case DEFINE_EXPAND:
346 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
347 break;
349 case DEFINE_SPLIT:
350 case DEFINE_PEEPHOLE:
351 case DEFINE_PEEPHOLE2:
352 case DEFINE_COND_EXEC:
353 XSTR (x, 1) = add_condition_to_string (XSTR (x, 1), extra);
354 break;
356 case DEFINE_INSN_AND_SPLIT:
357 XSTR (x, 2) = add_condition_to_string (XSTR (x, 2), extra);
358 XSTR (x, 4) = add_condition_to_string (XSTR (x, 4), extra);
359 break;
361 default:
362 break;
366 /* Apply the current iterator values to all attribute_uses. */
368 static void
369 apply_attribute_uses (void)
371 struct map_value *v;
372 attribute_use *ause;
373 unsigned int i;
375 FOR_EACH_VEC_ELT (attribute_use, attribute_uses, i, ause)
377 v = map_attr_string (ause->value);
378 if (!v)
379 fatal_with_file_and_line ("unknown iterator value `%s'", ause->value);
380 ause->group->apply_iterator (ause->ptr,
381 ause->group->find_builtin (v->string));
385 /* A htab_traverse callback for iterators. Add all used iterators
386 to current_iterators. */
388 static int
389 add_current_iterators (void **slot, void *data ATTRIBUTE_UNUSED)
391 struct mapping *iterator;
393 iterator = (struct mapping *) *slot;
394 if (iterator->current_value)
395 VEC_safe_push (mapping_ptr, heap, current_iterators, iterator);
396 return 1;
399 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
400 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
402 static void
403 apply_iterators (rtx original, rtx *queue)
405 unsigned int i;
406 const char *condition;
407 iterator_use *iuse;
408 struct mapping *iterator;
409 struct map_value *v;
410 rtx x;
412 if (VEC_empty (iterator_use, iterator_uses))
414 /* Raise an error if any attributes were used. */
415 apply_attribute_uses ();
416 XEXP (*queue, 0) = original;
417 XEXP (*queue, 1) = NULL_RTX;
418 return;
421 /* Clear out the iterators from the previous run. */
422 FOR_EACH_VEC_ELT (mapping_ptr, current_iterators, i, iterator)
423 iterator->current_value = NULL;
424 VEC_truncate (mapping_ptr, current_iterators, 0);
426 /* Mark the iterators that we need this time. */
427 FOR_EACH_VEC_ELT (iterator_use, iterator_uses, i, iuse)
428 iuse->iterator->current_value = iuse->iterator->values;
430 /* Get the list of iterators that are in use, preserving the
431 definition order within each group. */
432 htab_traverse (modes.iterators, add_current_iterators, NULL);
433 htab_traverse (codes.iterators, add_current_iterators, NULL);
434 htab_traverse (ints.iterators, add_current_iterators, NULL);
435 gcc_assert (!VEC_empty (mapping_ptr, current_iterators));
437 for (;;)
439 /* Apply the current iterator values. Accumulate a condition to
440 say when the resulting rtx can be used. */
441 condition = NULL;
442 FOR_EACH_VEC_ELT (iterator_use, iterator_uses, i, iuse)
444 v = iuse->iterator->current_value;
445 iuse->iterator->group->apply_iterator (iuse->ptr, v->number);
446 condition = join_c_conditions (condition, v->string);
448 apply_attribute_uses ();
449 x = copy_rtx_for_iterators (original);
450 add_condition_to_rtx (x, condition);
452 /* Add the new rtx to the end of the queue. */
453 XEXP (*queue, 0) = x;
454 XEXP (*queue, 1) = NULL_RTX;
456 /* Lexicographically increment the iterator value sequence.
457 That is, cycle through iterator values, starting from the right,
458 and stopping when one of them doesn't wrap around. */
459 i = VEC_length (mapping_ptr, current_iterators);
460 for (;;)
462 if (i == 0)
463 return;
464 i--;
465 iterator = VEC_index (mapping_ptr, current_iterators, i);
466 iterator->current_value = iterator->current_value->next;
467 if (iterator->current_value)
468 break;
469 iterator->current_value = iterator->values;
472 /* At least one more rtx to go. Allocate room for it. */
473 XEXP (*queue, 1) = rtx_alloc (EXPR_LIST);
474 queue = &XEXP (*queue, 1);
478 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
479 of the mapping and GROUP is the group to which it belongs. */
481 static struct mapping *
482 add_mapping (struct iterator_group *group, htab_t table, const char *name)
484 struct mapping *m;
485 void **slot;
487 m = XNEW (struct mapping);
488 m->name = xstrdup (name);
489 m->group = group;
490 m->values = 0;
491 m->current_value = NULL;
493 slot = htab_find_slot (table, m, INSERT);
494 if (*slot != 0)
495 fatal_with_file_and_line ("`%s' already defined", name);
497 *slot = m;
498 return m;
501 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
502 END_PTR points to the current null terminator for the list; return
503 a pointer the new null terminator. */
505 static struct map_value **
506 add_map_value (struct map_value **end_ptr, int number, const char *string)
508 struct map_value *value;
510 value = XNEW (struct map_value);
511 value->next = 0;
512 value->number = number;
513 value->string = string;
515 *end_ptr = value;
516 return &value->next;
519 /* Do one-time initialization of the mode and code attributes. */
521 static void
522 initialize_iterators (void)
524 struct mapping *lower, *upper;
525 struct map_value **lower_ptr, **upper_ptr;
526 char *copy, *p;
527 int i;
529 modes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
530 modes.iterators = htab_create (13, leading_string_hash,
531 leading_string_eq_p, 0);
532 modes.find_builtin = find_mode;
533 modes.apply_iterator = apply_mode_iterator;
535 codes.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
536 codes.iterators = htab_create (13, leading_string_hash,
537 leading_string_eq_p, 0);
538 codes.find_builtin = find_code;
539 codes.apply_iterator = apply_code_iterator;
541 ints.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
542 ints.iterators = htab_create (13, leading_string_hash,
543 leading_string_eq_p, 0);
544 ints.find_builtin = find_int;
545 ints.apply_iterator = apply_int_iterator;
547 lower = add_mapping (&modes, modes.attrs, "mode");
548 upper = add_mapping (&modes, modes.attrs, "MODE");
549 lower_ptr = &lower->values;
550 upper_ptr = &upper->values;
551 for (i = 0; i < MAX_MACHINE_MODE; i++)
553 copy = xstrdup (GET_MODE_NAME (i));
554 for (p = copy; *p != 0; p++)
555 *p = TOLOWER (*p);
557 upper_ptr = add_map_value (upper_ptr, i, GET_MODE_NAME (i));
558 lower_ptr = add_map_value (lower_ptr, i, copy);
561 lower = add_mapping (&codes, codes.attrs, "code");
562 upper = add_mapping (&codes, codes.attrs, "CODE");
563 lower_ptr = &lower->values;
564 upper_ptr = &upper->values;
565 for (i = 0; i < NUM_RTX_CODE; i++)
567 copy = xstrdup (GET_RTX_NAME (i));
568 for (p = copy; *p != 0; p++)
569 *p = TOUPPER (*p);
571 lower_ptr = add_map_value (lower_ptr, i, GET_RTX_NAME (i));
572 upper_ptr = add_map_value (upper_ptr, i, copy);
576 /* Provide a version of a function to read a long long if the system does
577 not provide one. */
578 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !defined(HAVE_ATOLL) && !defined(HAVE_ATOQ)
579 HOST_WIDE_INT atoll (const char *);
581 HOST_WIDE_INT
582 atoll (const char *p)
584 int neg = 0;
585 HOST_WIDE_INT tmp_wide;
587 while (ISSPACE (*p))
588 p++;
589 if (*p == '-')
590 neg = 1, p++;
591 else if (*p == '+')
592 p++;
594 tmp_wide = 0;
595 while (ISDIGIT (*p))
597 HOST_WIDE_INT new_wide = tmp_wide*10 + (*p - '0');
598 if (new_wide < tmp_wide)
600 /* Return INT_MAX equiv on overflow. */
601 tmp_wide = (~(unsigned HOST_WIDE_INT) 0) >> 1;
602 break;
604 tmp_wide = new_wide;
605 p++;
608 if (neg)
609 tmp_wide = -tmp_wide;
610 return tmp_wide;
612 #endif
614 /* Process a define_conditions directive, starting with the optional
615 space after the "define_conditions". The directive looks like this:
617 (define_conditions [
618 (number "string")
619 (number "string")
623 It's not intended to appear in machine descriptions. It is
624 generated by (the program generated by) genconditions.c, and
625 slipped in at the beginning of the sequence of MD files read by
626 most of the other generators. */
627 static void
628 read_conditions (void)
630 int c;
632 c = read_skip_spaces ();
633 if (c != '[')
634 fatal_expected_char ('[', c);
636 while ( (c = read_skip_spaces ()) != ']')
638 struct md_name name;
639 char *expr;
640 int value;
642 if (c != '(')
643 fatal_expected_char ('(', c);
645 read_name (&name);
646 validate_const_int (name.string);
647 value = atoi (name.string);
649 c = read_skip_spaces ();
650 if (c != '"')
651 fatal_expected_char ('"', c);
652 expr = read_quoted_string ();
654 c = read_skip_spaces ();
655 if (c != ')')
656 fatal_expected_char (')', c);
658 add_c_test (expr, value);
662 static void
663 validate_const_int (const char *string)
665 const char *cp;
666 int valid = 1;
668 cp = string;
669 while (*cp && ISSPACE (*cp))
670 cp++;
671 if (*cp == '-' || *cp == '+')
672 cp++;
673 if (*cp == 0)
674 valid = 0;
675 for (; *cp; cp++)
676 if (! ISDIGIT (*cp))
677 valid = 0;
678 if (!valid)
679 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string);
682 /* Record that PTR uses iterator ITERATOR. */
684 static void
685 record_iterator_use (struct mapping *iterator, void *ptr)
687 struct iterator_use iuse = {iterator, ptr};
688 VEC_safe_push (iterator_use, heap, iterator_uses, iuse);
691 /* Record that PTR uses attribute VALUE, which must match a built-in
692 value from group GROUP. */
694 static void
695 record_attribute_use (struct iterator_group *group, void *ptr,
696 const char *value)
698 struct attribute_use ause = {group, value, ptr};
699 VEC_safe_push (attribute_use, heap, attribute_uses, ause);
702 /* Interpret NAME as either a built-in value, iterator or attribute
703 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
704 callback. */
706 static void
707 record_potential_iterator_use (struct iterator_group *group, void *ptr,
708 const char *name)
710 struct mapping *m;
711 size_t len;
713 len = strlen (name);
714 if (name[0] == '<' && name[len - 1] == '>')
716 /* Copy the attribute string into permanent storage, without the
717 angle brackets around it. */
718 obstack_grow0 (&string_obstack, name + 1, len - 2);
719 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
721 else
723 m = (struct mapping *) htab_find (group->iterators, &name);
724 if (m != 0)
725 record_iterator_use (m, ptr);
726 else
727 group->apply_iterator (ptr, group->find_builtin (name));
731 /* Finish reading a declaration of the form:
733 (define... <name> [<value1> ... <valuen>])
735 from the MD file, where each <valuei> is either a bare symbol name or a
736 "(<name> <string>)" pair. The "(define..." part has already been read.
738 Represent the declaration as a "mapping" structure; add it to TABLE
739 (which belongs to GROUP) and return it. */
741 static struct mapping *
742 read_mapping (struct iterator_group *group, htab_t table)
744 struct md_name name;
745 struct mapping *m;
746 struct map_value **end_ptr;
747 const char *string;
748 int number, c;
750 /* Read the mapping name and create a structure for it. */
751 read_name (&name);
752 m = add_mapping (group, table, name.string);
754 c = read_skip_spaces ();
755 if (c != '[')
756 fatal_expected_char ('[', c);
758 /* Read each value. */
759 end_ptr = &m->values;
760 c = read_skip_spaces ();
763 if (c != '(')
765 /* A bare symbol name that is implicitly paired to an
766 empty string. */
767 unread_char (c);
768 read_name (&name);
769 string = "";
771 else
773 /* A "(name string)" pair. */
774 read_name (&name);
775 string = read_string (false);
776 c = read_skip_spaces ();
777 if (c != ')')
778 fatal_expected_char (')', c);
780 number = group->find_builtin (name.string);
781 end_ptr = add_map_value (end_ptr, number, string);
782 c = read_skip_spaces ();
784 while (c != ']');
786 return m;
789 /* Check newly-created code iterator ITERATOR to see whether every code has the
790 same format. */
792 static void
793 check_code_iterator (struct mapping *iterator)
795 struct map_value *v;
796 enum rtx_code bellwether;
798 bellwether = (enum rtx_code) iterator->values->number;
799 for (v = iterator->values->next; v != 0; v = v->next)
800 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
801 fatal_with_file_and_line ("code iterator `%s' combines "
802 "different rtx formats", iterator->name);
805 /* Read an rtx-related declaration from the MD file, given that it
806 starts with directive name RTX_NAME. Return true if it expands to
807 one or more rtxes (as defined by rtx.def). When returning true,
808 store the list of rtxes as an EXPR_LIST in *X. */
810 bool
811 read_rtx (const char *rtx_name, rtx *x)
813 static rtx queue_head;
815 /* Do one-time initialization. */
816 if (queue_head == 0)
818 initialize_iterators ();
819 queue_head = rtx_alloc (EXPR_LIST);
822 /* Handle various rtx-related declarations that aren't themselves
823 encoded as rtxes. */
824 if (strcmp (rtx_name, "define_conditions") == 0)
826 read_conditions ();
827 return false;
829 if (strcmp (rtx_name, "define_mode_attr") == 0)
831 read_mapping (&modes, modes.attrs);
832 return false;
834 if (strcmp (rtx_name, "define_mode_iterator") == 0)
836 read_mapping (&modes, modes.iterators);
837 return false;
839 if (strcmp (rtx_name, "define_code_attr") == 0)
841 read_mapping (&codes, codes.attrs);
842 return false;
844 if (strcmp (rtx_name, "define_code_iterator") == 0)
846 check_code_iterator (read_mapping (&codes, codes.iterators));
847 return false;
849 if (strcmp (rtx_name, "define_int_attr") == 0)
851 read_mapping (&ints, ints.attrs);
852 return false;
854 if (strcmp (rtx_name, "define_int_iterator") == 0)
856 read_mapping (&ints, ints.iterators);
857 return false;
860 apply_iterators (read_rtx_code (rtx_name), &queue_head);
861 VEC_truncate (iterator_use, iterator_uses, 0);
862 VEC_truncate (attribute_use, attribute_uses, 0);
864 *x = queue_head;
865 return true;
868 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
869 either an rtx code or a code iterator. Parse the rest of the rtx and
870 return it. */
872 static rtx
873 read_rtx_code (const char *code_name)
875 int i;
876 RTX_CODE code;
877 struct mapping *iterator;
878 const char *format_ptr;
879 struct md_name name;
880 rtx return_rtx;
881 int c;
882 HOST_WIDE_INT tmp_wide;
884 /* Linked list structure for making RTXs: */
885 struct rtx_list
887 struct rtx_list *next;
888 rtx value; /* Value of this node. */
891 /* If this code is an iterator, build the rtx using the iterator's
892 first value. */
893 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
894 if (iterator != 0)
895 code = (enum rtx_code) iterator->values->number;
896 else
897 code = (enum rtx_code) codes.find_builtin (code_name);
899 /* If we end up with an insn expression then we free this space below. */
900 return_rtx = rtx_alloc (code);
901 format_ptr = GET_RTX_FORMAT (code);
902 PUT_CODE (return_rtx, code);
904 if (iterator)
905 record_iterator_use (iterator, return_rtx);
907 /* If what follows is `: mode ', read it and
908 store the mode in the rtx. */
910 i = read_skip_spaces ();
911 if (i == ':')
913 read_name (&name);
914 record_potential_iterator_use (&modes, return_rtx, name.string);
916 else
917 unread_char (i);
919 for (i = 0; format_ptr[i] != 0; i++)
920 switch (format_ptr[i])
922 /* 0 means a field for internal use only.
923 Don't expect it to be present in the input. */
924 case '0':
925 break;
927 case 'e':
928 case 'u':
929 XEXP (return_rtx, i) = read_nested_rtx ();
930 break;
932 case 'V':
933 /* 'V' is an optional vector: if a closeparen follows,
934 just store NULL for this element. */
935 c = read_skip_spaces ();
936 unread_char (c);
937 if (c == ')')
939 XVEC (return_rtx, i) = 0;
940 break;
942 /* Now process the vector. */
944 case 'E':
946 /* Obstack to store scratch vector in. */
947 struct obstack vector_stack;
948 int list_counter = 0;
949 rtvec return_vec = NULL_RTVEC;
951 c = read_skip_spaces ();
952 if (c != '[')
953 fatal_expected_char ('[', c);
955 /* Add expressions to a list, while keeping a count. */
956 obstack_init (&vector_stack);
957 while ((c = read_skip_spaces ()) && c != ']')
959 if (c == EOF)
960 fatal_expected_char (']', c);
961 unread_char (c);
962 list_counter++;
963 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
965 if (list_counter > 0)
967 return_vec = rtvec_alloc (list_counter);
968 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
969 list_counter * sizeof (rtx));
971 else if (format_ptr[i] == 'E')
972 fatal_with_file_and_line ("vector must have at least one element");
973 XVEC (return_rtx, i) = return_vec;
974 obstack_free (&vector_stack, NULL);
975 /* close bracket gotten */
977 break;
979 case 'S':
980 case 'T':
981 case 's':
983 char *stringbuf;
984 int star_if_braced;
986 c = read_skip_spaces ();
987 unread_char (c);
988 if (c == ')')
990 /* 'S' fields are optional and should be NULL if no string
991 was given. Also allow normal 's' and 'T' strings to be
992 omitted, treating them in the same way as empty strings. */
993 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
994 break;
997 /* The output template slot of a DEFINE_INSN,
998 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
999 gets a star inserted as its first character, if it is
1000 written with a brace block instead of a string constant. */
1001 star_if_braced = (format_ptr[i] == 'T');
1003 stringbuf = read_string (star_if_braced);
1005 /* For insn patterns, we want to provide a default name
1006 based on the file and line, like "*foo.md:12", if the
1007 given name is blank. These are only for define_insn and
1008 define_insn_and_split, to aid debugging. */
1009 if (*stringbuf == '\0'
1010 && i == 0
1011 && (GET_CODE (return_rtx) == DEFINE_INSN
1012 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1014 char line_name[20];
1015 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1016 const char *slash;
1017 for (slash = fn; *slash; slash ++)
1018 if (*slash == '/' || *slash == '\\' || *slash == ':')
1019 fn = slash + 1;
1020 obstack_1grow (&string_obstack, '*');
1021 obstack_grow (&string_obstack, fn, strlen (fn));
1022 sprintf (line_name, ":%d", read_md_lineno);
1023 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1024 stringbuf = XOBFINISH (&string_obstack, char *);
1027 if (star_if_braced)
1028 XTMPL (return_rtx, i) = stringbuf;
1029 else
1030 XSTR (return_rtx, i) = stringbuf;
1032 break;
1034 case 'w':
1035 read_name (&name);
1036 validate_const_int (name.string);
1037 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1038 tmp_wide = atoi (name.string);
1039 #else
1040 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1041 tmp_wide = atol (name.string);
1042 #else
1043 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1044 But prefer not to use our hand-rolled function above either. */
1045 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1046 tmp_wide = atoll (name.string);
1047 #else
1048 tmp_wide = atoq (name.string);
1049 #endif
1050 #endif
1051 #endif
1052 XWINT (return_rtx, i) = tmp_wide;
1053 break;
1055 case 'i':
1056 case 'n':
1057 /* Can be an iterator or an integer constant. */
1058 read_name (&name);
1059 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1060 name.string);
1061 break;
1063 default:
1064 gcc_unreachable ();
1067 c = read_skip_spaces ();
1068 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1069 arbitrary number of arguments for them. */
1070 if (c == '('
1071 && (GET_CODE (return_rtx) == AND
1072 || GET_CODE (return_rtx) == IOR))
1073 return read_rtx_variadic (return_rtx);
1075 unread_char (c);
1076 return return_rtx;
1079 /* Read a nested rtx construct from the MD file and return it. */
1081 static rtx
1082 read_nested_rtx (void)
1084 struct md_name name;
1085 int c;
1086 rtx return_rtx;
1088 c = read_skip_spaces ();
1089 if (c != '(')
1090 fatal_expected_char ('(', c);
1092 read_name (&name);
1093 if (strcmp (name.string, "nil") == 0)
1094 return_rtx = NULL;
1095 else
1096 return_rtx = read_rtx_code (name.string);
1098 c = read_skip_spaces ();
1099 if (c != ')')
1100 fatal_expected_char (')', c);
1102 return return_rtx;
1105 /* Mutually recursive subroutine of read_rtx which reads
1106 (thing x1 x2 x3 ...) and produces RTL as if
1107 (thing x1 (thing x2 (thing x3 ...))) had been written.
1108 When called, FORM is (thing x1 x2), and the file position
1109 is just past the leading parenthesis of x3. Only works
1110 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1111 static rtx
1112 read_rtx_variadic (rtx form)
1114 char c = '(';
1115 rtx p = form, q;
1119 unread_char (c);
1121 q = rtx_alloc (GET_CODE (p));
1122 PUT_MODE (q, GET_MODE (p));
1124 XEXP (q, 0) = XEXP (p, 1);
1125 XEXP (q, 1) = read_nested_rtx ();
1127 XEXP (p, 1) = q;
1128 p = q;
1129 c = read_skip_spaces ();
1131 while (c == '(');
1132 unread_char (c);
1133 return form;