2 Copyright (C) 1987-2016 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
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
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/>. */
22 /* Disable rtl checking; it conflicts with the iterator handling. */
23 #undef ENABLE_RTL_CHECKING
26 #include "coretypes.h"
31 #include "gensupport.h"
33 /* One element in a singly-linked list of (integer, string) pairs. */
35 struct map_value
*next
;
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. */
44 /* The name of the iterator or attribute. */
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 /* A structure for abstracting the common parts of iterators. */
58 struct iterator_group
{
59 /* Tables of "mapping" structures, one for attributes and one for
61 htab_t attrs
, iterators
;
63 /* Treat the given string as the name of a standard mode, etc., and
64 return its integer value. */
65 int (*find_builtin
) (const char *);
67 /* Make the given pointer use the given iterator value. */
68 void (*apply_iterator
) (void *, int);
71 /* Records one use of an iterator. */
73 /* The iterator itself. */
74 struct mapping
*iterator
;
76 /* The location of the use, as passed to the apply_iterator callback. */
80 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
81 in a non-string rtx field. */
82 struct attribute_use
{
83 /* The group that describes the use site. */
84 struct iterator_group
*group
;
86 /* The name of the attribute, possibly with an "iterator:" prefix. */
89 /* The location of the use, as passed to GROUP's apply_iterator callback. */
93 /* This struct is used to link subst_attr named ATTR_NAME with
94 corresponding define_subst named ITER_NAME. */
95 struct subst_attr_to_iter_mapping
101 /* Hash-table to store links between subst-attributes and
103 htab_t subst_attr_to_iter_map
= NULL
;
104 /* This global stores name of subst-iterator which is currently being
106 const char *current_iterator_name
;
108 static void validate_const_int (const char *);
109 static rtx
read_rtx_code (const char *);
110 static void read_rtx_operand (rtx
, int);
111 static rtx
read_nested_rtx (void);
112 static rtx
read_rtx_variadic (rtx
);
114 /* The mode and code iterator structures. */
115 static struct iterator_group modes
, codes
, ints
, substs
;
117 /* All iterators used in the current rtx. */
118 static vec
<mapping
*> current_iterators
;
120 /* The list of all iterator uses in the current rtx. */
121 static vec
<iterator_use
> iterator_uses
;
123 /* The list of all attribute uses in the current rtx. */
124 static vec
<attribute_use
> attribute_uses
;
126 /* Implementations of the iterator_group callbacks for modes. */
129 find_mode (const char *name
)
133 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
134 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
137 fatal_with_file_and_line ("unknown mode `%s'", name
);
141 apply_mode_iterator (void *loc
, int mode
)
143 PUT_MODE ((rtx
) loc
, (machine_mode
) mode
);
146 /* Implementations of the iterator_group callbacks for codes. */
149 find_code (const char *name
)
153 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
154 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
157 fatal_with_file_and_line ("unknown rtx code `%s'", name
);
161 apply_code_iterator (void *loc
, int code
)
163 PUT_CODE ((rtx
) loc
, (enum rtx_code
) code
);
166 /* Implementations of the iterator_group callbacks for ints. */
168 /* Since GCC does not construct a table of valid constants,
169 we have to accept any int as valid. No cross-checking can
173 find_int (const char *name
)
175 validate_const_int (name
);
180 apply_int_iterator (void *loc
, int value
)
185 /* This routine adds attribute or does nothing depending on VALUE. When
186 VALUE is 1, it does nothing - the first duplicate of original
187 template is kept untouched when it's subjected to a define_subst.
188 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
189 attribute, named exactly as define_subst, which later will be
190 applied. If such attribute has already been added, then no the
191 routine has no effect. */
193 apply_subst_iterator (void *loc
, int value
)
197 rtvec attrs_vec
, new_attrs_vec
;
201 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
202 || GET_CODE (rt
) == DEFINE_EXPAND
);
204 attrs_vec
= XVEC (rt
, 4);
206 /* If we've already added attribute 'current_iterator_name', then we
207 have nothing to do now. */
210 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
212 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
217 /* Add attribute with subst name - it serves as a mark for
218 define_subst which later would be applied to this pattern. */
219 new_attr
= rtx_alloc (SET_ATTR
);
220 PUT_CODE (new_attr
, SET_ATTR
);
221 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
222 XSTR (new_attr
, 1) = xstrdup ("yes");
226 new_attrs_vec
= rtvec_alloc (1);
227 new_attrs_vec
->elem
[0] = new_attr
;
231 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
232 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
233 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
234 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
236 XVEC (rt
, 4) = new_attrs_vec
;
239 /* Map subst-attribute ATTR to subst iterator ITER. */
242 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
244 struct subst_attr_to_iter_mapping
*value
;
246 if (!subst_attr_to_iter_map
)
247 subst_attr_to_iter_map
=
248 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
249 value
= XNEW (struct subst_attr_to_iter_mapping
);
250 value
->attr_name
= xstrdup (attr
);
251 value
->iter_name
= xstrdup (iter
);
252 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
256 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
259 find_subst_iter_by_attr (const char *attr
)
261 char *iter_name
= NULL
;
262 struct subst_attr_to_iter_mapping
*value
;
263 value
= (struct subst_attr_to_iter_mapping
*)
264 htab_find (subst_attr_to_iter_map
, &attr
);
266 iter_name
= value
->iter_name
;
270 /* Map attribute string P to its current value. Return null if the attribute
273 static struct map_value
*
274 map_attr_string (const char *p
)
277 struct mapping
*iterator
;
281 int iterator_name_len
;
283 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
285 attr
= strchr (p
, ':');
288 iterator_name_len
= -1;
293 iterator_name_len
= attr
- p
;
297 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
299 /* If an iterator name was specified, check that it matches. */
300 if (iterator_name_len
>= 0
301 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
302 || iterator
->name
[iterator_name_len
] != 0))
305 /* Find the attribute specification. */
306 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
309 /* In contrast to code/mode/int iterators, attributes of subst
310 iterators are linked to one specific subst-iterator. So, if
311 we are dealing with subst-iterator, we should check if it's
312 the one which linked with the given attribute. */
313 if (iterator
->group
== &substs
)
315 char *iter_name
= find_subst_iter_by_attr (attr
);
316 if (strcmp (iter_name
, iterator
->name
) != 0)
319 /* Find the attribute value associated with the current
321 for (v
= m
->values
; v
; v
= v
->next
)
322 if (v
->number
== iterator
->current_value
->number
)
329 /* Apply the current iterator values to STRING. Return the new string
330 if any changes were needed, otherwise return STRING itself. */
333 apply_iterator_to_string (const char *string
)
335 char *base
, *copy
, *p
, *start
, *end
;
341 base
= p
= copy
= ASTRDUP (string
);
342 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
347 v
= map_attr_string (p
);
352 /* Add everything between the last copied byte and the '<',
353 then add in the attribute value. */
354 obstack_grow (&string_obstack
, base
, start
- base
);
355 obstack_grow (&string_obstack
, v
->string
, strlen (v
->string
));
360 obstack_grow (&string_obstack
, base
, strlen (base
) + 1);
361 copy
= XOBFINISH (&string_obstack
, char *);
362 copy_md_ptr_loc (copy
, string
);
368 /* Return a deep copy of X, substituting the current iterator
369 values into any strings. */
372 copy_rtx_for_iterators (rtx original
)
374 const char *format_ptr
, *p
;
381 /* Create a shallow copy of ORIGINAL. */
382 x
= rtx_alloc (GET_CODE (original
));
383 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
385 /* Change each string and recursively change each rtx. */
386 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
387 for (i
= 0; format_ptr
[i
] != 0; i
++)
388 switch (format_ptr
[i
])
391 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
397 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
402 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
407 if (XVEC (original
, i
))
409 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
410 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
412 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
422 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
423 has the form "&& ..." (as used in define_insn_and_splits), assume that
424 EXTRA is already satisfied. Empty strings are treated like "true". */
427 add_condition_to_string (const char *original
, const char *extra
)
429 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
431 return join_c_conditions (original
, extra
);
434 /* Like add_condition, but applied to all conditions in rtx X. */
437 add_condition_to_rtx (rtx x
, const char *extra
)
439 switch (GET_CODE (x
))
444 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
448 case DEFINE_PEEPHOLE
:
449 case DEFINE_PEEPHOLE2
:
450 case DEFINE_COND_EXEC
:
451 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
454 case DEFINE_INSN_AND_SPLIT
:
455 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
456 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
464 /* Apply the current iterator values to all attribute_uses. */
467 apply_attribute_uses (void)
473 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
475 v
= map_attr_string (ause
->value
);
477 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
478 ause
->group
->apply_iterator (ause
->ptr
,
479 ause
->group
->find_builtin (v
->string
));
483 /* A htab_traverse callback for iterators. Add all used iterators
484 to current_iterators. */
487 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
489 struct mapping
*iterator
;
491 iterator
= (struct mapping
*) *slot
;
492 if (iterator
->current_value
)
493 current_iterators
.safe_push (iterator
);
497 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
498 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
501 apply_iterators (rtx original
, vec
<rtx
> *queue
)
504 const char *condition
;
506 struct mapping
*iterator
;
510 if (iterator_uses
.is_empty ())
512 /* Raise an error if any attributes were used. */
513 apply_attribute_uses ();
514 queue
->safe_push (original
);
518 /* Clear out the iterators from the previous run. */
519 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
520 iterator
->current_value
= NULL
;
521 current_iterators
.truncate (0);
523 /* Mark the iterators that we need this time. */
524 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
525 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
527 /* Get the list of iterators that are in use, preserving the
528 definition order within each group. */
529 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
530 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
531 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
532 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
533 gcc_assert (!current_iterators
.is_empty ());
537 /* Apply the current iterator values. Accumulate a condition to
538 say when the resulting rtx can be used. */
540 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
542 if (iuse
->iterator
->group
== &substs
)
544 v
= iuse
->iterator
->current_value
;
545 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
546 condition
= join_c_conditions (condition
, v
->string
);
548 apply_attribute_uses ();
549 x
= copy_rtx_for_iterators (original
);
550 add_condition_to_rtx (x
, condition
);
552 /* We apply subst iterator after RTL-template is copied, as during
553 subst-iterator processing, we could add an attribute to the
554 RTL-template, and we don't want to do it in the original one. */
555 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
557 v
= iuse
->iterator
->current_value
;
558 if (iuse
->iterator
->group
== &substs
)
561 current_iterator_name
= iuse
->iterator
->name
;
562 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
565 /* Add the new rtx to the end of the queue. */
566 queue
->safe_push (x
);
568 /* Lexicographically increment the iterator value sequence.
569 That is, cycle through iterator values, starting from the right,
570 and stopping when one of them doesn't wrap around. */
571 i
= current_iterators
.length ();
577 iterator
= current_iterators
[i
];
578 iterator
->current_value
= iterator
->current_value
->next
;
579 if (iterator
->current_value
)
581 iterator
->current_value
= iterator
->values
;
586 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
587 of the mapping and GROUP is the group to which it belongs. */
589 static struct mapping
*
590 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
595 m
= XNEW (struct mapping
);
596 m
->name
= xstrdup (name
);
599 m
->current_value
= NULL
;
601 slot
= htab_find_slot (table
, m
, INSERT
);
603 fatal_with_file_and_line ("`%s' already defined", name
);
609 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
610 END_PTR points to the current null terminator for the list; return
611 a pointer the new null terminator. */
613 static struct map_value
**
614 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
616 struct map_value
*value
;
618 value
= XNEW (struct map_value
);
620 value
->number
= number
;
621 value
->string
= string
;
627 /* Do one-time initialization of the mode and code attributes. */
630 initialize_iterators (void)
632 struct mapping
*lower
, *upper
;
633 struct map_value
**lower_ptr
, **upper_ptr
;
637 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
638 modes
.iterators
= htab_create (13, leading_string_hash
,
639 leading_string_eq_p
, 0);
640 modes
.find_builtin
= find_mode
;
641 modes
.apply_iterator
= apply_mode_iterator
;
643 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
644 codes
.iterators
= htab_create (13, leading_string_hash
,
645 leading_string_eq_p
, 0);
646 codes
.find_builtin
= find_code
;
647 codes
.apply_iterator
= apply_code_iterator
;
649 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
650 ints
.iterators
= htab_create (13, leading_string_hash
,
651 leading_string_eq_p
, 0);
652 ints
.find_builtin
= find_int
;
653 ints
.apply_iterator
= apply_int_iterator
;
655 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
656 substs
.iterators
= htab_create (13, leading_string_hash
,
657 leading_string_eq_p
, 0);
658 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
659 substs
.apply_iterator
= apply_subst_iterator
;
661 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
662 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
663 lower_ptr
= &lower
->values
;
664 upper_ptr
= &upper
->values
;
665 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
667 copy
= xstrdup (GET_MODE_NAME (i
));
668 for (p
= copy
; *p
!= 0; p
++)
671 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
672 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
675 lower
= add_mapping (&codes
, codes
.attrs
, "code");
676 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
677 lower_ptr
= &lower
->values
;
678 upper_ptr
= &upper
->values
;
679 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
681 copy
= xstrdup (GET_RTX_NAME (i
));
682 for (p
= copy
; *p
!= 0; p
++)
685 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
686 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
690 /* Provide a version of a function to read a long long if the system does
692 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
693 HOST_WIDE_INT
atoll (const char *);
696 atoll (const char *p
)
699 HOST_WIDE_INT tmp_wide
;
711 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
712 if (new_wide
< tmp_wide
)
714 /* Return INT_MAX equiv on overflow. */
715 tmp_wide
= HOST_WIDE_INT_M1U
>> 1;
723 tmp_wide
= -tmp_wide
;
728 /* Process a define_conditions directive, starting with the optional
729 space after the "define_conditions". The directive looks like this:
737 It's not intended to appear in machine descriptions. It is
738 generated by (the program generated by) genconditions.c, and
739 slipped in at the beginning of the sequence of MD files read by
740 most of the other generators. */
742 read_conditions (void)
746 require_char_ws ('[');
748 while ( (c
= read_skip_spaces ()) != ']')
755 fatal_expected_char ('(', c
);
758 validate_const_int (name
.string
);
759 value
= atoi (name
.string
);
761 require_char_ws ('"');
762 expr
= read_quoted_string ();
764 require_char_ws (')');
766 add_c_test (expr
, value
);
771 validate_const_int (const char *string
)
777 while (*cp
&& ISSPACE (*cp
))
779 if (*cp
== '-' || *cp
== '+')
790 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
794 validate_const_wide_int (const char *string
)
800 while (*cp
&& ISSPACE (*cp
))
802 /* Skip the leading 0x. */
803 if (cp
[0] == '0' || cp
[1] == 'x')
810 if (! ISXDIGIT (*cp
))
813 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
816 /* Record that PTR uses iterator ITERATOR. */
819 record_iterator_use (struct mapping
*iterator
, void *ptr
)
821 struct iterator_use iuse
= {iterator
, ptr
};
822 iterator_uses
.safe_push (iuse
);
825 /* Record that PTR uses attribute VALUE, which must match a built-in
826 value from group GROUP. */
829 record_attribute_use (struct iterator_group
*group
, void *ptr
,
832 struct attribute_use ause
= {group
, value
, ptr
};
833 attribute_uses
.safe_push (ause
);
836 /* Interpret NAME as either a built-in value, iterator or attribute
837 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
841 record_potential_iterator_use (struct iterator_group
*group
, void *ptr
,
848 if (name
[0] == '<' && name
[len
- 1] == '>')
850 /* Copy the attribute string into permanent storage, without the
851 angle brackets around it. */
852 obstack_grow0 (&string_obstack
, name
+ 1, len
- 2);
853 record_attribute_use (group
, ptr
, XOBFINISH (&string_obstack
, char *));
857 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
859 record_iterator_use (m
, ptr
);
861 group
->apply_iterator (ptr
, group
->find_builtin (name
));
865 /* Finish reading a declaration of the form:
867 (define... <name> [<value1> ... <valuen>])
869 from the MD file, where each <valuei> is either a bare symbol name or a
870 "(<name> <string>)" pair. The "(define..." part has already been read.
872 Represent the declaration as a "mapping" structure; add it to TABLE
873 (which belongs to GROUP) and return it. */
875 static struct mapping
*
876 read_mapping (struct iterator_group
*group
, htab_t table
)
880 struct map_value
**end_ptr
;
884 /* Read the mapping name and create a structure for it. */
886 m
= add_mapping (group
, table
, name
.string
);
888 require_char_ws ('[');
890 /* Read each value. */
891 end_ptr
= &m
->values
;
892 c
= read_skip_spaces ();
897 /* A bare symbol name that is implicitly paired to an
905 /* A "(name string)" pair. */
907 string
= read_string (false);
908 require_char_ws (')');
910 number
= group
->find_builtin (name
.string
);
911 end_ptr
= add_map_value (end_ptr
, number
, string
);
912 c
= read_skip_spaces ();
919 /* For iterator with name ATTR_NAME generate define_attr with values
920 'yes' and 'no'. This attribute is used to mark templates to which
921 define_subst ATTR_NAME should be applied. This attribute is set and
922 defined implicitly and automatically. */
924 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
926 rtx const_str
, return_rtx
;
928 return_rtx
= rtx_alloc (DEFINE_ATTR
);
929 PUT_CODE (return_rtx
, DEFINE_ATTR
);
931 const_str
= rtx_alloc (CONST_STRING
);
932 PUT_CODE (const_str
, CONST_STRING
);
933 XSTR (const_str
, 0) = xstrdup ("no");
935 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
936 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
937 XEXP (return_rtx
, 2) = const_str
;
939 queue
->safe_push (return_rtx
);
942 /* This routine generates DEFINE_SUBST_ATTR expression with operands
943 ATTR_OPERANDS and places it to QUEUE. */
945 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
950 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
951 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
953 for (i
= 0; i
< 4; i
++)
954 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
956 queue
->safe_push (return_rtx
);
959 /* Read define_subst_attribute construction. It has next form:
960 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
961 Attribute is substituted with value1 when no subst is applied and with
962 value2 in the opposite case.
963 Attributes are added to SUBST_ATTRS_TABLE.
964 In case the iterator is encountered for the first time, it's added to
965 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
968 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
972 struct map_value
**end_ptr
;
973 const char *attr_operands
[4];
976 for (i
= 0; i
< 4; i
++)
977 attr_operands
[i
] = read_string (false);
979 add_define_subst_attr (attr_operands
, queue
);
981 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
983 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
986 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
987 end_ptr
= &m
->values
;
988 end_ptr
= add_map_value (end_ptr
, 1, "");
989 end_ptr
= add_map_value (end_ptr
, 2, "");
991 add_define_attr_for_define_subst (attr_operands
[1], queue
);
994 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
995 end_ptr
= &m
->values
;
996 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
997 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
1000 /* Check newly-created code iterator ITERATOR to see whether every code has the
1004 check_code_iterator (struct mapping
*iterator
)
1006 struct map_value
*v
;
1007 enum rtx_code bellwether
;
1009 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1010 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1011 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1012 fatal_with_file_and_line ("code iterator `%s' combines "
1013 "different rtx formats", iterator
->name
);
1016 /* Read an rtx-related declaration from the MD file, given that it
1017 starts with directive name RTX_NAME. Return true if it expands to
1018 one or more rtxes (as defined by rtx.def). When returning true,
1019 store the list of rtxes as an EXPR_LIST in *X. */
1022 read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1024 static bool initialized
= false;
1026 /* Do one-time initialization. */
1029 initialize_iterators ();
1033 /* Handle various rtx-related declarations that aren't themselves
1034 encoded as rtxes. */
1035 if (strcmp (rtx_name
, "define_conditions") == 0)
1040 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1042 read_mapping (&modes
, modes
.attrs
);
1045 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1047 read_mapping (&modes
, modes
.iterators
);
1050 if (strcmp (rtx_name
, "define_code_attr") == 0)
1052 read_mapping (&codes
, codes
.attrs
);
1055 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1057 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1060 if (strcmp (rtx_name
, "define_int_attr") == 0)
1062 read_mapping (&ints
, ints
.attrs
);
1065 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1067 read_mapping (&ints
, ints
.iterators
);
1070 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1072 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1074 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1075 TRUE to process it. */
1079 apply_iterators (read_rtx_code (rtx_name
), rtxen
);
1080 iterator_uses
.truncate (0);
1081 attribute_uses
.truncate (0);
1086 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1087 either an rtx code or a code iterator. Parse the rest of the rtx and
1091 read_rtx_code (const char *code_name
)
1094 struct mapping
*iterator
;
1095 const char *format_ptr
;
1096 struct md_name name
;
1100 /* Linked list structure for making RTXs: */
1103 struct rtx_list
*next
;
1104 rtx value
; /* Value of this node. */
1107 /* If this code is an iterator, build the rtx using the iterator's
1109 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1111 code
= (enum rtx_code
) iterator
->values
->number
;
1113 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1115 /* If we end up with an insn expression then we free this space below. */
1116 return_rtx
= rtx_alloc (code
);
1117 format_ptr
= GET_RTX_FORMAT (code
);
1118 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1119 PUT_CODE (return_rtx
, code
);
1122 record_iterator_use (iterator
, return_rtx
);
1124 /* If what follows is `: mode ', read it and
1125 store the mode in the rtx. */
1127 c
= read_skip_spaces ();
1131 record_potential_iterator_use (&modes
, return_rtx
, name
.string
);
1136 for (int idx
= 0; format_ptr
[idx
] != 0; idx
++)
1137 read_rtx_operand (return_rtx
, idx
);
1139 if (CONST_WIDE_INT_P (return_rtx
))
1142 validate_const_wide_int (name
.string
);
1144 const char *s
= name
.string
;
1147 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1149 char * buf
= XALLOCAVEC (char, gs
+ 1);
1150 unsigned HOST_WIDE_INT wi
;
1153 /* Skip the leading spaces. */
1154 while (*s
&& ISSPACE (*s
))
1157 /* Skip the leading 0x. */
1158 gcc_assert (s
[0] == '0');
1159 gcc_assert (s
[1] == 'x');
1164 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1166 return_rtx
= const_wide_int_alloc (wlen
);
1170 #if HOST_BITS_PER_WIDE_INT == 64
1171 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1173 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1175 CWI_ELT (return_rtx
, index
++) = wi
;
1178 strncpy (buf
, s
, gs
- pos
);
1180 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1181 CWI_ELT (return_rtx
, index
++) = wi
;
1182 /* TODO: After reading, do we want to canonicalize with:
1183 value = lookup_const_wide_int (value); ? */
1187 c
= read_skip_spaces ();
1188 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1189 arbitrary number of arguments for them. */
1191 && (GET_CODE (return_rtx
) == AND
1192 || GET_CODE (return_rtx
) == IOR
))
1193 return read_rtx_variadic (return_rtx
);
1199 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1200 based on the corresponding format character within GET_RTX_FORMAT
1201 for the GET_CODE (RETURN_RTX). */
1204 read_rtx_operand (rtx return_rtx
, int idx
)
1206 RTX_CODE code
= GET_CODE (return_rtx
);
1207 const char *format_ptr
= GET_RTX_FORMAT (code
);
1209 struct md_name name
;
1211 switch (format_ptr
[idx
])
1213 /* 0 means a field for internal use only.
1214 Don't expect it to be present in the input. */
1217 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1222 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1226 /* 'V' is an optional vector: if a closeparen follows,
1227 just store NULL for this element. */
1228 c
= read_skip_spaces ();
1232 XVEC (return_rtx
, idx
) = 0;
1235 /* Now process the vector. */
1240 /* Obstack to store scratch vector in. */
1241 struct obstack vector_stack
;
1242 int list_counter
= 0;
1243 rtvec return_vec
= NULL_RTVEC
;
1245 require_char_ws ('[');
1247 /* Add expressions to a list, while keeping a count. */
1248 obstack_init (&vector_stack
);
1249 while ((c
= read_skip_spaces ()) && c
!= ']')
1252 fatal_expected_char (']', c
);
1255 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1257 if (list_counter
> 0)
1259 return_vec
= rtvec_alloc (list_counter
);
1260 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1261 list_counter
* sizeof (rtx
));
1263 else if (format_ptr
[idx
] == 'E')
1264 fatal_with_file_and_line ("vector must have at least one element");
1265 XVEC (return_rtx
, idx
) = return_vec
;
1266 obstack_free (&vector_stack
, NULL
);
1267 /* close bracket gotten */
1278 c
= read_skip_spaces ();
1282 /* 'S' fields are optional and should be NULL if no string
1283 was given. Also allow normal 's' and 'T' strings to be
1284 omitted, treating them in the same way as empty strings. */
1285 XSTR (return_rtx
, idx
) = (format_ptr
[idx
] == 'S' ? NULL
: "");
1289 /* The output template slot of a DEFINE_INSN,
1290 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1291 gets a star inserted as its first character, if it is
1292 written with a brace block instead of a string constant. */
1293 star_if_braced
= (format_ptr
[idx
] == 'T');
1295 stringbuf
= read_string (star_if_braced
);
1297 /* For insn patterns, we want to provide a default name
1298 based on the file and line, like "*foo.md:12", if the
1299 given name is blank. These are only for define_insn and
1300 define_insn_and_split, to aid debugging. */
1301 if (*stringbuf
== '\0'
1303 && (GET_CODE (return_rtx
) == DEFINE_INSN
1304 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1307 const char *read_md_filename
= rtx_reader_ptr
->get_filename ();
1308 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1310 for (slash
= fn
; *slash
; slash
++)
1311 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1313 obstack_1grow (&string_obstack
, '*');
1314 obstack_grow (&string_obstack
, fn
, strlen (fn
));
1315 sprintf (line_name
, ":%d", rtx_reader_ptr
->get_lineno ());
1316 obstack_grow (&string_obstack
, line_name
, strlen (line_name
)+1);
1317 stringbuf
= XOBFINISH (&string_obstack
, char *);
1320 /* Find attr-names in the string. */
1322 char *start
, *end
, *ptr
;
1326 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1328 if ((end
- start
- 1 > 0)
1329 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1331 strncpy (tmpstr
, start
+1, end
-start
-1);
1332 tmpstr
[end
-start
-1] = 0;
1338 = (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1341 /* Here we should find linked subst-iter. */
1342 str
= find_subst_iter_by_attr (ptr
);
1344 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1349 record_iterator_use (m
, return_rtx
);
1353 XTMPL (return_rtx
, idx
) = stringbuf
;
1355 XSTR (return_rtx
, idx
) = stringbuf
;
1361 HOST_WIDE_INT tmp_wide
;
1363 validate_const_int (name
.string
);
1364 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1365 tmp_wide
= atoi (name
.string
);
1367 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1368 tmp_wide
= atol (name
.string
);
1370 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1371 But prefer not to use our hand-rolled function above either. */
1372 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1373 tmp_wide
= atoll (name
.string
);
1375 tmp_wide
= atoq (name
.string
);
1379 XWINT (return_rtx
, idx
) = tmp_wide
;
1385 /* Can be an iterator or an integer constant. */
1387 record_potential_iterator_use (&ints
, &XINT (return_rtx
, idx
),
1393 validate_const_int (name
.string
);
1394 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1395 REG_ATTRS (return_rtx
) = NULL
;
1403 /* Read a nested rtx construct from the MD file and return it. */
1406 read_nested_rtx (void)
1408 struct md_name name
;
1411 require_char_ws ('(');
1414 if (strcmp (name
.string
, "nil") == 0)
1417 return_rtx
= read_rtx_code (name
.string
);
1419 require_char_ws (')');
1424 /* Mutually recursive subroutine of read_rtx which reads
1425 (thing x1 x2 x3 ...) and produces RTL as if
1426 (thing x1 (thing x2 (thing x3 ...))) had been written.
1427 When called, FORM is (thing x1 x2), and the file position
1428 is just past the leading parenthesis of x3. Only works
1429 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1431 read_rtx_variadic (rtx form
)
1440 q
= rtx_alloc (GET_CODE (p
));
1441 PUT_MODE (q
, GET_MODE (p
));
1443 XEXP (q
, 0) = XEXP (p
, 1);
1444 XEXP (q
, 1) = read_nested_rtx ();
1448 c
= read_skip_spaces ();