* arm.h (TARGET_CPU_CPP_BUILTINS): Remove Maverick support.
[official-gcc.git] / gcc / read-rtl.c
blob71ecf5376421738c8bd8d7442b5a4d73e82ce4b8
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;
689 iuse = VEC_safe_push (iterator_use, heap, iterator_uses, NULL);
690 iuse->iterator = iterator;
691 iuse->ptr = ptr;
694 /* Record that PTR uses attribute VALUE, which must match a built-in
695 value from group GROUP. */
697 static void
698 record_attribute_use (struct iterator_group *group, void *ptr,
699 const char *value)
701 struct attribute_use *ause;
703 ause = VEC_safe_push (attribute_use, heap, attribute_uses, NULL);
704 ause->group = group;
705 ause->value = value;
706 ause->ptr = ptr;
709 /* Interpret NAME as either a built-in value, iterator or attribute
710 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
711 callback. */
713 static void
714 record_potential_iterator_use (struct iterator_group *group, void *ptr,
715 const char *name)
717 struct mapping *m;
718 size_t len;
720 len = strlen (name);
721 if (name[0] == '<' && name[len - 1] == '>')
723 /* Copy the attribute string into permanent storage, without the
724 angle brackets around it. */
725 obstack_grow0 (&string_obstack, name + 1, len - 2);
726 record_attribute_use (group, ptr, XOBFINISH (&string_obstack, char *));
728 else
730 m = (struct mapping *) htab_find (group->iterators, &name);
731 if (m != 0)
732 record_iterator_use (m, ptr);
733 else
734 group->apply_iterator (ptr, group->find_builtin (name));
738 /* Finish reading a declaration of the form:
740 (define... <name> [<value1> ... <valuen>])
742 from the MD file, where each <valuei> is either a bare symbol name or a
743 "(<name> <string>)" pair. The "(define..." part has already been read.
745 Represent the declaration as a "mapping" structure; add it to TABLE
746 (which belongs to GROUP) and return it. */
748 static struct mapping *
749 read_mapping (struct iterator_group *group, htab_t table)
751 struct md_name name;
752 struct mapping *m;
753 struct map_value **end_ptr;
754 const char *string;
755 int number, c;
757 /* Read the mapping name and create a structure for it. */
758 read_name (&name);
759 m = add_mapping (group, table, name.string);
761 c = read_skip_spaces ();
762 if (c != '[')
763 fatal_expected_char ('[', c);
765 /* Read each value. */
766 end_ptr = &m->values;
767 c = read_skip_spaces ();
770 if (c != '(')
772 /* A bare symbol name that is implicitly paired to an
773 empty string. */
774 unread_char (c);
775 read_name (&name);
776 string = "";
778 else
780 /* A "(name string)" pair. */
781 read_name (&name);
782 string = read_string (false);
783 c = read_skip_spaces ();
784 if (c != ')')
785 fatal_expected_char (')', c);
787 number = group->find_builtin (name.string);
788 end_ptr = add_map_value (end_ptr, number, string);
789 c = read_skip_spaces ();
791 while (c != ']');
793 return m;
796 /* Check newly-created code iterator ITERATOR to see whether every code has the
797 same format. */
799 static void
800 check_code_iterator (struct mapping *iterator)
802 struct map_value *v;
803 enum rtx_code bellwether;
805 bellwether = (enum rtx_code) iterator->values->number;
806 for (v = iterator->values->next; v != 0; v = v->next)
807 if (strcmp (GET_RTX_FORMAT (bellwether), GET_RTX_FORMAT (v->number)) != 0)
808 fatal_with_file_and_line ("code iterator `%s' combines "
809 "different rtx formats", iterator->name);
812 /* Read an rtx-related declaration from the MD file, given that it
813 starts with directive name RTX_NAME. Return true if it expands to
814 one or more rtxes (as defined by rtx.def). When returning true,
815 store the list of rtxes as an EXPR_LIST in *X. */
817 bool
818 read_rtx (const char *rtx_name, rtx *x)
820 static rtx queue_head;
822 /* Do one-time initialization. */
823 if (queue_head == 0)
825 initialize_iterators ();
826 queue_head = rtx_alloc (EXPR_LIST);
829 /* Handle various rtx-related declarations that aren't themselves
830 encoded as rtxes. */
831 if (strcmp (rtx_name, "define_conditions") == 0)
833 read_conditions ();
834 return false;
836 if (strcmp (rtx_name, "define_mode_attr") == 0)
838 read_mapping (&modes, modes.attrs);
839 return false;
841 if (strcmp (rtx_name, "define_mode_iterator") == 0)
843 read_mapping (&modes, modes.iterators);
844 return false;
846 if (strcmp (rtx_name, "define_code_attr") == 0)
848 read_mapping (&codes, codes.attrs);
849 return false;
851 if (strcmp (rtx_name, "define_code_iterator") == 0)
853 check_code_iterator (read_mapping (&codes, codes.iterators));
854 return false;
856 if (strcmp (rtx_name, "define_int_attr") == 0)
858 read_mapping (&ints, ints.attrs);
859 return false;
861 if (strcmp (rtx_name, "define_int_iterator") == 0)
863 read_mapping (&ints, ints.iterators);
864 return false;
867 apply_iterators (read_rtx_code (rtx_name), &queue_head);
868 VEC_truncate (iterator_use, iterator_uses, 0);
869 VEC_truncate (attribute_use, attribute_uses, 0);
871 *x = queue_head;
872 return true;
875 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
876 either an rtx code or a code iterator. Parse the rest of the rtx and
877 return it. */
879 static rtx
880 read_rtx_code (const char *code_name)
882 int i;
883 RTX_CODE code;
884 struct mapping *iterator;
885 const char *format_ptr;
886 struct md_name name;
887 rtx return_rtx;
888 int c;
889 HOST_WIDE_INT tmp_wide;
891 /* Linked list structure for making RTXs: */
892 struct rtx_list
894 struct rtx_list *next;
895 rtx value; /* Value of this node. */
898 /* If this code is an iterator, build the rtx using the iterator's
899 first value. */
900 iterator = (struct mapping *) htab_find (codes.iterators, &code_name);
901 if (iterator != 0)
902 code = (enum rtx_code) iterator->values->number;
903 else
904 code = (enum rtx_code) codes.find_builtin (code_name);
906 /* If we end up with an insn expression then we free this space below. */
907 return_rtx = rtx_alloc (code);
908 format_ptr = GET_RTX_FORMAT (code);
909 PUT_CODE (return_rtx, code);
911 if (iterator)
912 record_iterator_use (iterator, return_rtx);
914 /* If what follows is `: mode ', read it and
915 store the mode in the rtx. */
917 i = read_skip_spaces ();
918 if (i == ':')
920 read_name (&name);
921 record_potential_iterator_use (&modes, return_rtx, name.string);
923 else
924 unread_char (i);
926 for (i = 0; format_ptr[i] != 0; i++)
927 switch (format_ptr[i])
929 /* 0 means a field for internal use only.
930 Don't expect it to be present in the input. */
931 case '0':
932 break;
934 case 'e':
935 case 'u':
936 XEXP (return_rtx, i) = read_nested_rtx ();
937 break;
939 case 'V':
940 /* 'V' is an optional vector: if a closeparen follows,
941 just store NULL for this element. */
942 c = read_skip_spaces ();
943 unread_char (c);
944 if (c == ')')
946 XVEC (return_rtx, i) = 0;
947 break;
949 /* Now process the vector. */
951 case 'E':
953 /* Obstack to store scratch vector in. */
954 struct obstack vector_stack;
955 int list_counter = 0;
956 rtvec return_vec = NULL_RTVEC;
958 c = read_skip_spaces ();
959 if (c != '[')
960 fatal_expected_char ('[', c);
962 /* Add expressions to a list, while keeping a count. */
963 obstack_init (&vector_stack);
964 while ((c = read_skip_spaces ()) && c != ']')
966 if (c == EOF)
967 fatal_expected_char (']', c);
968 unread_char (c);
969 list_counter++;
970 obstack_ptr_grow (&vector_stack, read_nested_rtx ());
972 if (list_counter > 0)
974 return_vec = rtvec_alloc (list_counter);
975 memcpy (&return_vec->elem[0], obstack_finish (&vector_stack),
976 list_counter * sizeof (rtx));
978 else if (format_ptr[i] == 'E')
979 fatal_with_file_and_line ("vector must have at least one element");
980 XVEC (return_rtx, i) = return_vec;
981 obstack_free (&vector_stack, NULL);
982 /* close bracket gotten */
984 break;
986 case 'S':
987 case 'T':
988 case 's':
990 char *stringbuf;
991 int star_if_braced;
993 c = read_skip_spaces ();
994 unread_char (c);
995 if (c == ')')
997 /* 'S' fields are optional and should be NULL if no string
998 was given. Also allow normal 's' and 'T' strings to be
999 omitted, treating them in the same way as empty strings. */
1000 XSTR (return_rtx, i) = (format_ptr[i] == 'S' ? NULL : "");
1001 break;
1004 /* The output template slot of a DEFINE_INSN,
1005 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1006 gets a star inserted as its first character, if it is
1007 written with a brace block instead of a string constant. */
1008 star_if_braced = (format_ptr[i] == 'T');
1010 stringbuf = read_string (star_if_braced);
1012 /* For insn patterns, we want to provide a default name
1013 based on the file and line, like "*foo.md:12", if the
1014 given name is blank. These are only for define_insn and
1015 define_insn_and_split, to aid debugging. */
1016 if (*stringbuf == '\0'
1017 && i == 0
1018 && (GET_CODE (return_rtx) == DEFINE_INSN
1019 || GET_CODE (return_rtx) == DEFINE_INSN_AND_SPLIT))
1021 char line_name[20];
1022 const char *fn = (read_md_filename ? read_md_filename : "rtx");
1023 const char *slash;
1024 for (slash = fn; *slash; slash ++)
1025 if (*slash == '/' || *slash == '\\' || *slash == ':')
1026 fn = slash + 1;
1027 obstack_1grow (&string_obstack, '*');
1028 obstack_grow (&string_obstack, fn, strlen (fn));
1029 sprintf (line_name, ":%d", read_md_lineno);
1030 obstack_grow (&string_obstack, line_name, strlen (line_name)+1);
1031 stringbuf = XOBFINISH (&string_obstack, char *);
1034 if (star_if_braced)
1035 XTMPL (return_rtx, i) = stringbuf;
1036 else
1037 XSTR (return_rtx, i) = stringbuf;
1039 break;
1041 case 'w':
1042 read_name (&name);
1043 validate_const_int (name.string);
1044 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1045 tmp_wide = atoi (name.string);
1046 #else
1047 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1048 tmp_wide = atol (name.string);
1049 #else
1050 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1051 But prefer not to use our hand-rolled function above either. */
1052 #if defined(HAVE_ATOLL) || !defined(HAVE_ATOQ)
1053 tmp_wide = atoll (name.string);
1054 #else
1055 tmp_wide = atoq (name.string);
1056 #endif
1057 #endif
1058 #endif
1059 XWINT (return_rtx, i) = tmp_wide;
1060 break;
1062 case 'i':
1063 case 'n':
1064 /* Can be an iterator or an integer constant. */
1065 read_name (&name);
1066 record_potential_iterator_use (&ints, &XINT (return_rtx, i),
1067 name.string);
1068 break;
1070 default:
1071 gcc_unreachable ();
1074 c = read_skip_spaces ();
1075 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1076 arbitrary number of arguments for them. */
1077 if (c == '('
1078 && (GET_CODE (return_rtx) == AND
1079 || GET_CODE (return_rtx) == IOR))
1080 return read_rtx_variadic (return_rtx);
1082 unread_char (c);
1083 return return_rtx;
1086 /* Read a nested rtx construct from the MD file and return it. */
1088 static rtx
1089 read_nested_rtx (void)
1091 struct md_name name;
1092 int c;
1093 rtx return_rtx;
1095 c = read_skip_spaces ();
1096 if (c != '(')
1097 fatal_expected_char ('(', c);
1099 read_name (&name);
1100 if (strcmp (name.string, "nil") == 0)
1101 return_rtx = NULL;
1102 else
1103 return_rtx = read_rtx_code (name.string);
1105 c = read_skip_spaces ();
1106 if (c != ')')
1107 fatal_expected_char (')', c);
1109 return return_rtx;
1112 /* Mutually recursive subroutine of read_rtx which reads
1113 (thing x1 x2 x3 ...) and produces RTL as if
1114 (thing x1 (thing x2 (thing x3 ...))) had been written.
1115 When called, FORM is (thing x1 x2), and the file position
1116 is just past the leading parenthesis of x3. Only works
1117 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1118 static rtx
1119 read_rtx_variadic (rtx form)
1121 char c = '(';
1122 rtx p = form, q;
1126 unread_char (c);
1128 q = rtx_alloc (GET_CODE (p));
1129 PUT_MODE (q, GET_MODE (p));
1131 XEXP (q, 0) = XEXP (p, 1);
1132 XEXP (q, 1) = read_nested_rtx ();
1134 XEXP (p, 1) = q;
1135 p = q;
1136 c = read_skip_spaces ();
1138 while (c == '(');
1139 unread_char (c);
1140 return form;