2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 /* Vector definitions for the above. */
58 typedef struct mapping
*mapping_ptr
;
60 /* A structure for abstracting the common parts of iterators. */
61 struct iterator_group
{
62 /* Tables of "mapping" structures, one for attributes and one for
64 htab_t attrs
, iterators
;
66 /* Treat the given string as the name of a standard mode, etc., and
67 return its integer value. */
68 int (*find_builtin
) (const char *);
70 /* Make the given pointer use the given iterator value. */
71 void (*apply_iterator
) (void *, int);
74 /* Records one use of an iterator. */
76 /* The iterator itself. */
77 struct mapping
*iterator
;
79 /* The location of the use, as passed to the apply_iterator callback. */
83 /* Vector definitions for the above. */
84 typedef struct iterator_use iterator_use
;
86 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
87 in a non-string rtx field. */
88 struct attribute_use
{
89 /* The group that describes the use site. */
90 struct iterator_group
*group
;
92 /* The name of the attribute, possibly with an "iterator:" prefix. */
95 /* The location of the use, as passed to GROUP's apply_iterator callback. */
99 /* Vector definitions for the above. */
100 typedef struct attribute_use attribute_use
;
102 /* This struct is used to link subst_attr named ATTR_NAME with
103 corresponding define_subst named ITER_NAME. */
104 struct subst_attr_to_iter_mapping
110 /* Hash-table to store links between subst-attributes and
112 htab_t subst_attr_to_iter_map
= NULL
;
113 /* This global stores name of subst-iterator which is currently being
115 const char *current_iterator_name
;
117 static void validate_const_int (const char *);
118 static rtx
read_rtx_code (const char *);
119 static rtx
read_nested_rtx (void);
120 static rtx
read_rtx_variadic (rtx
);
122 /* The mode and code iterator structures. */
123 static struct iterator_group modes
, codes
, ints
, substs
;
125 /* All iterators used in the current rtx. */
126 static vec
<mapping_ptr
> current_iterators
;
128 /* The list of all iterator uses in the current rtx. */
129 static vec
<iterator_use
> iterator_uses
;
131 /* The list of all attribute uses in the current rtx. */
132 static vec
<attribute_use
> attribute_uses
;
134 /* Implementations of the iterator_group callbacks for modes. */
137 find_mode (const char *name
)
141 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
142 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
145 fatal_with_file_and_line ("unknown mode `%s'", name
);
149 apply_mode_iterator (void *loc
, int mode
)
151 PUT_MODE ((rtx
) loc
, (machine_mode
) mode
);
154 /* Implementations of the iterator_group callbacks for codes. */
157 find_code (const char *name
)
161 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
162 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
165 fatal_with_file_and_line ("unknown rtx code `%s'", name
);
169 apply_code_iterator (void *loc
, int code
)
171 PUT_CODE ((rtx
) loc
, (enum rtx_code
) code
);
174 /* Implementations of the iterator_group callbacks for ints. */
176 /* Since GCC does not construct a table of valid constants,
177 we have to accept any int as valid. No cross-checking can
181 find_int (const char *name
)
183 validate_const_int (name
);
188 apply_int_iterator (void *loc
, int value
)
193 /* This routine adds attribute or does nothing depending on VALUE. When
194 VALUE is 1, it does nothing - the first duplicate of original
195 template is kept untouched when it's subjected to a define_subst.
196 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
197 attribute, named exactly as define_subst, which later will be
198 applied. If such attribute has already been added, then no the
199 routine has no effect. */
201 apply_subst_iterator (void *loc
, int value
)
205 rtvec attrs_vec
, new_attrs_vec
;
209 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
210 || GET_CODE (rt
) == DEFINE_EXPAND
);
212 attrs_vec
= XVEC (rt
, 4);
214 /* If we've already added attribute 'current_iterator_name', then we
215 have nothing to do now. */
218 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
220 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
225 /* Add attribute with subst name - it serves as a mark for
226 define_subst which later would be applied to this pattern. */
227 new_attr
= rtx_alloc (SET_ATTR
);
228 PUT_CODE (new_attr
, SET_ATTR
);
229 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
230 XSTR (new_attr
, 1) = xstrdup ("yes");
234 new_attrs_vec
= rtvec_alloc (1);
235 new_attrs_vec
->elem
[0] = new_attr
;
239 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
240 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
241 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
242 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
244 XVEC (rt
, 4) = new_attrs_vec
;
247 /* Map subst-attribute ATTR to subst iterator ITER. */
250 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
252 struct subst_attr_to_iter_mapping
*value
;
254 if (!subst_attr_to_iter_map
)
255 subst_attr_to_iter_map
=
256 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
257 value
= XNEW (struct subst_attr_to_iter_mapping
);
258 value
->attr_name
= xstrdup (attr
);
259 value
->iter_name
= xstrdup (iter
);
260 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
264 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
267 find_subst_iter_by_attr (const char *attr
)
269 char *iter_name
= NULL
;
270 struct subst_attr_to_iter_mapping
*value
;
271 value
= (struct subst_attr_to_iter_mapping
*)
272 htab_find (subst_attr_to_iter_map
, &attr
);
274 iter_name
= value
->iter_name
;
278 /* Map attribute string P to its current value. Return null if the attribute
281 static struct map_value
*
282 map_attr_string (const char *p
)
285 struct mapping
*iterator
;
289 int iterator_name_len
;
291 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
293 attr
= strchr (p
, ':');
296 iterator_name_len
= -1;
301 iterator_name_len
= attr
- p
;
305 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
307 /* If an iterator name was specified, check that it matches. */
308 if (iterator_name_len
>= 0
309 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
310 || iterator
->name
[iterator_name_len
] != 0))
313 /* Find the attribute specification. */
314 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
317 /* In contrast to code/mode/int iterators, attributes of subst
318 iterators are linked to one specific subst-iterator. So, if
319 we are dealing with subst-iterator, we should check if it's
320 the one which linked with the given attribute. */
321 if (iterator
->group
== &substs
)
323 char *iter_name
= find_subst_iter_by_attr (attr
);
324 if (strcmp (iter_name
, iterator
->name
) != 0)
327 /* Find the attribute value associated with the current
329 for (v
= m
->values
; v
; v
= v
->next
)
330 if (v
->number
== iterator
->current_value
->number
)
337 /* Apply the current iterator values to STRING. Return the new string
338 if any changes were needed, otherwise return STRING itself. */
341 apply_iterator_to_string (const char *string
)
343 char *base
, *copy
, *p
, *start
, *end
;
349 base
= p
= copy
= ASTRDUP (string
);
350 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
355 v
= map_attr_string (p
);
360 /* Add everything between the last copied byte and the '<',
361 then add in the attribute value. */
362 obstack_grow (&string_obstack
, base
, start
- base
);
363 obstack_grow (&string_obstack
, v
->string
, strlen (v
->string
));
368 obstack_grow (&string_obstack
, base
, strlen (base
) + 1);
369 copy
= XOBFINISH (&string_obstack
, char *);
370 copy_md_ptr_loc (copy
, string
);
376 /* Return a deep copy of X, substituting the current iterator
377 values into any strings. */
380 copy_rtx_for_iterators (rtx original
)
382 const char *format_ptr
, *p
;
389 /* Create a shallow copy of ORIGINAL. */
390 x
= rtx_alloc (GET_CODE (original
));
391 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
393 /* Change each string and recursively change each rtx. */
394 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
395 for (i
= 0; format_ptr
[i
] != 0; i
++)
396 switch (format_ptr
[i
])
399 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
405 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
410 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
415 if (XVEC (original
, i
))
417 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
418 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
420 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
430 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
431 has the form "&& ..." (as used in define_insn_and_splits), assume that
432 EXTRA is already satisfied. Empty strings are treated like "true". */
435 add_condition_to_string (const char *original
, const char *extra
)
437 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
439 return join_c_conditions (original
, extra
);
442 /* Like add_condition, but applied to all conditions in rtx X. */
445 add_condition_to_rtx (rtx x
, const char *extra
)
447 switch (GET_CODE (x
))
452 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
456 case DEFINE_PEEPHOLE
:
457 case DEFINE_PEEPHOLE2
:
458 case DEFINE_COND_EXEC
:
459 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
462 case DEFINE_INSN_AND_SPLIT
:
463 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
464 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
472 /* Apply the current iterator values to all attribute_uses. */
475 apply_attribute_uses (void)
481 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
483 v
= map_attr_string (ause
->value
);
485 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
486 ause
->group
->apply_iterator (ause
->ptr
,
487 ause
->group
->find_builtin (v
->string
));
491 /* A htab_traverse callback for iterators. Add all used iterators
492 to current_iterators. */
495 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
497 struct mapping
*iterator
;
499 iterator
= (struct mapping
*) *slot
;
500 if (iterator
->current_value
)
501 current_iterators
.safe_push (iterator
);
505 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
506 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
509 apply_iterators (rtx original
, vec
<rtx
> *queue
)
512 const char *condition
;
514 struct mapping
*iterator
;
518 if (iterator_uses
.is_empty ())
520 /* Raise an error if any attributes were used. */
521 apply_attribute_uses ();
522 queue
->safe_push (original
);
526 /* Clear out the iterators from the previous run. */
527 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
528 iterator
->current_value
= NULL
;
529 current_iterators
.truncate (0);
531 /* Mark the iterators that we need this time. */
532 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
533 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
535 /* Get the list of iterators that are in use, preserving the
536 definition order within each group. */
537 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
538 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
539 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
540 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
541 gcc_assert (!current_iterators
.is_empty ());
545 /* Apply the current iterator values. Accumulate a condition to
546 say when the resulting rtx can be used. */
548 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
550 if (iuse
->iterator
->group
== &substs
)
552 v
= iuse
->iterator
->current_value
;
553 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
554 condition
= join_c_conditions (condition
, v
->string
);
556 apply_attribute_uses ();
557 x
= copy_rtx_for_iterators (original
);
558 add_condition_to_rtx (x
, condition
);
560 /* We apply subst iterator after RTL-template is copied, as during
561 subst-iterator processing, we could add an attribute to the
562 RTL-template, and we don't want to do it in the original one. */
563 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
565 v
= iuse
->iterator
->current_value
;
566 if (iuse
->iterator
->group
== &substs
)
569 current_iterator_name
= iuse
->iterator
->name
;
570 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
573 /* Add the new rtx to the end of the queue. */
574 queue
->safe_push (x
);
576 /* Lexicographically increment the iterator value sequence.
577 That is, cycle through iterator values, starting from the right,
578 and stopping when one of them doesn't wrap around. */
579 i
= current_iterators
.length ();
585 iterator
= current_iterators
[i
];
586 iterator
->current_value
= iterator
->current_value
->next
;
587 if (iterator
->current_value
)
589 iterator
->current_value
= iterator
->values
;
594 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
595 of the mapping and GROUP is the group to which it belongs. */
597 static struct mapping
*
598 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
603 m
= XNEW (struct mapping
);
604 m
->name
= xstrdup (name
);
607 m
->current_value
= NULL
;
609 slot
= htab_find_slot (table
, m
, INSERT
);
611 fatal_with_file_and_line ("`%s' already defined", name
);
617 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
618 END_PTR points to the current null terminator for the list; return
619 a pointer the new null terminator. */
621 static struct map_value
**
622 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
624 struct map_value
*value
;
626 value
= XNEW (struct map_value
);
628 value
->number
= number
;
629 value
->string
= string
;
635 /* Do one-time initialization of the mode and code attributes. */
638 initialize_iterators (void)
640 struct mapping
*lower
, *upper
;
641 struct map_value
**lower_ptr
, **upper_ptr
;
645 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
646 modes
.iterators
= htab_create (13, leading_string_hash
,
647 leading_string_eq_p
, 0);
648 modes
.find_builtin
= find_mode
;
649 modes
.apply_iterator
= apply_mode_iterator
;
651 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
652 codes
.iterators
= htab_create (13, leading_string_hash
,
653 leading_string_eq_p
, 0);
654 codes
.find_builtin
= find_code
;
655 codes
.apply_iterator
= apply_code_iterator
;
657 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
658 ints
.iterators
= htab_create (13, leading_string_hash
,
659 leading_string_eq_p
, 0);
660 ints
.find_builtin
= find_int
;
661 ints
.apply_iterator
= apply_int_iterator
;
663 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
664 substs
.iterators
= htab_create (13, leading_string_hash
,
665 leading_string_eq_p
, 0);
666 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
667 substs
.apply_iterator
= apply_subst_iterator
;
669 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
670 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
671 lower_ptr
= &lower
->values
;
672 upper_ptr
= &upper
->values
;
673 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
675 copy
= xstrdup (GET_MODE_NAME (i
));
676 for (p
= copy
; *p
!= 0; p
++)
679 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
680 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
683 lower
= add_mapping (&codes
, codes
.attrs
, "code");
684 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
685 lower_ptr
= &lower
->values
;
686 upper_ptr
= &upper
->values
;
687 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
689 copy
= xstrdup (GET_RTX_NAME (i
));
690 for (p
= copy
; *p
!= 0; p
++)
693 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
694 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
698 /* Provide a version of a function to read a long long if the system does
700 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
701 HOST_WIDE_INT
atoll (const char *);
704 atoll (const char *p
)
707 HOST_WIDE_INT tmp_wide
;
719 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
720 if (new_wide
< tmp_wide
)
722 /* Return INT_MAX equiv on overflow. */
723 tmp_wide
= (~(unsigned HOST_WIDE_INT
) 0) >> 1;
731 tmp_wide
= -tmp_wide
;
736 /* Process a define_conditions directive, starting with the optional
737 space after the "define_conditions". The directive looks like this:
745 It's not intended to appear in machine descriptions. It is
746 generated by (the program generated by) genconditions.c, and
747 slipped in at the beginning of the sequence of MD files read by
748 most of the other generators. */
750 read_conditions (void)
754 c
= read_skip_spaces ();
756 fatal_expected_char ('[', c
);
758 while ( (c
= read_skip_spaces ()) != ']')
765 fatal_expected_char ('(', c
);
768 validate_const_int (name
.string
);
769 value
= atoi (name
.string
);
771 c
= read_skip_spaces ();
773 fatal_expected_char ('"', c
);
774 expr
= read_quoted_string ();
776 c
= read_skip_spaces ();
778 fatal_expected_char (')', c
);
780 add_c_test (expr
, value
);
785 validate_const_int (const char *string
)
791 while (*cp
&& ISSPACE (*cp
))
793 if (*cp
== '-' || *cp
== '+')
804 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
808 validate_const_wide_int (const char *string
)
814 while (*cp
&& ISSPACE (*cp
))
816 /* Skip the leading 0x. */
817 if (cp
[0] == '0' || cp
[1] == 'x')
824 if (! ISXDIGIT (*cp
))
827 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
830 /* Record that PTR uses iterator ITERATOR. */
833 record_iterator_use (struct mapping
*iterator
, void *ptr
)
835 struct iterator_use iuse
= {iterator
, ptr
};
836 iterator_uses
.safe_push (iuse
);
839 /* Record that PTR uses attribute VALUE, which must match a built-in
840 value from group GROUP. */
843 record_attribute_use (struct iterator_group
*group
, void *ptr
,
846 struct attribute_use ause
= {group
, value
, ptr
};
847 attribute_uses
.safe_push (ause
);
850 /* Interpret NAME as either a built-in value, iterator or attribute
851 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
855 record_potential_iterator_use (struct iterator_group
*group
, void *ptr
,
862 if (name
[0] == '<' && name
[len
- 1] == '>')
864 /* Copy the attribute string into permanent storage, without the
865 angle brackets around it. */
866 obstack_grow0 (&string_obstack
, name
+ 1, len
- 2);
867 record_attribute_use (group
, ptr
, XOBFINISH (&string_obstack
, char *));
871 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
873 record_iterator_use (m
, ptr
);
875 group
->apply_iterator (ptr
, group
->find_builtin (name
));
879 /* Finish reading a declaration of the form:
881 (define... <name> [<value1> ... <valuen>])
883 from the MD file, where each <valuei> is either a bare symbol name or a
884 "(<name> <string>)" pair. The "(define..." part has already been read.
886 Represent the declaration as a "mapping" structure; add it to TABLE
887 (which belongs to GROUP) and return it. */
889 static struct mapping
*
890 read_mapping (struct iterator_group
*group
, htab_t table
)
894 struct map_value
**end_ptr
;
898 /* Read the mapping name and create a structure for it. */
900 m
= add_mapping (group
, table
, name
.string
);
902 c
= read_skip_spaces ();
904 fatal_expected_char ('[', c
);
906 /* Read each value. */
907 end_ptr
= &m
->values
;
908 c
= read_skip_spaces ();
913 /* A bare symbol name that is implicitly paired to an
921 /* A "(name string)" pair. */
923 string
= read_string (false);
924 c
= read_skip_spaces ();
926 fatal_expected_char (')', c
);
928 number
= group
->find_builtin (name
.string
);
929 end_ptr
= add_map_value (end_ptr
, number
, string
);
930 c
= read_skip_spaces ();
937 /* For iterator with name ATTR_NAME generate define_attr with values
938 'yes' and 'no'. This attribute is used to mark templates to which
939 define_subst ATTR_NAME should be applied. This attribute is set and
940 defined implicitly and automatically. */
942 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
944 rtx const_str
, return_rtx
;
946 return_rtx
= rtx_alloc (DEFINE_ATTR
);
947 PUT_CODE (return_rtx
, DEFINE_ATTR
);
949 const_str
= rtx_alloc (CONST_STRING
);
950 PUT_CODE (const_str
, CONST_STRING
);
951 XSTR (const_str
, 0) = xstrdup ("no");
953 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
954 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
955 XEXP (return_rtx
, 2) = const_str
;
957 queue
->safe_push (return_rtx
);
960 /* This routine generates DEFINE_SUBST_ATTR expression with operands
961 ATTR_OPERANDS and places it to QUEUE. */
963 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
968 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
969 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
971 for (i
= 0; i
< 4; i
++)
972 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
974 queue
->safe_push (return_rtx
);
977 /* Read define_subst_attribute construction. It has next form:
978 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
979 Attribute is substituted with value1 when no subst is applied and with
980 value2 in the opposite case.
981 Attributes are added to SUBST_ATTRS_TABLE.
982 In case the iterator is encountered for the first time, it's added to
983 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
986 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
990 struct map_value
**end_ptr
;
991 const char *attr_operands
[4];
994 for (i
= 0; i
< 4; i
++)
995 attr_operands
[i
] = read_string (false);
997 add_define_subst_attr (attr_operands
, queue
);
999 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
1001 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
1004 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
1005 end_ptr
= &m
->values
;
1006 end_ptr
= add_map_value (end_ptr
, 1, "");
1007 end_ptr
= add_map_value (end_ptr
, 2, "");
1009 add_define_attr_for_define_subst (attr_operands
[1], queue
);
1012 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
1013 end_ptr
= &m
->values
;
1014 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
1015 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
1018 /* Check newly-created code iterator ITERATOR to see whether every code has the
1022 check_code_iterator (struct mapping
*iterator
)
1024 struct map_value
*v
;
1025 enum rtx_code bellwether
;
1027 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1028 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1029 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1030 fatal_with_file_and_line ("code iterator `%s' combines "
1031 "different rtx formats", iterator
->name
);
1034 /* Read an rtx-related declaration from the MD file, given that it
1035 starts with directive name RTX_NAME. Return true if it expands to
1036 one or more rtxes (as defined by rtx.def). When returning true,
1037 store the list of rtxes as an EXPR_LIST in *X. */
1040 read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1042 static bool initialized
= false;
1044 /* Do one-time initialization. */
1047 initialize_iterators ();
1051 /* Handle various rtx-related declarations that aren't themselves
1052 encoded as rtxes. */
1053 if (strcmp (rtx_name
, "define_conditions") == 0)
1058 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1060 read_mapping (&modes
, modes
.attrs
);
1063 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1065 read_mapping (&modes
, modes
.iterators
);
1068 if (strcmp (rtx_name
, "define_code_attr") == 0)
1070 read_mapping (&codes
, codes
.attrs
);
1073 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1075 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1078 if (strcmp (rtx_name
, "define_int_attr") == 0)
1080 read_mapping (&ints
, ints
.attrs
);
1083 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1085 read_mapping (&ints
, ints
.iterators
);
1088 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1090 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1092 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1093 TRUE to process it. */
1097 apply_iterators (read_rtx_code (rtx_name
), rtxen
);
1098 iterator_uses
.truncate (0);
1099 attribute_uses
.truncate (0);
1104 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1105 either an rtx code or a code iterator. Parse the rest of the rtx and
1109 read_rtx_code (const char *code_name
)
1113 struct mapping
*iterator
, *m
;
1114 const char *format_ptr
;
1115 struct md_name name
;
1118 HOST_WIDE_INT tmp_wide
;
1120 char *start
, *end
, *ptr
;
1123 /* Linked list structure for making RTXs: */
1126 struct rtx_list
*next
;
1127 rtx value
; /* Value of this node. */
1130 /* If this code is an iterator, build the rtx using the iterator's
1132 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1134 code
= (enum rtx_code
) iterator
->values
->number
;
1136 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1138 /* If we end up with an insn expression then we free this space below. */
1139 return_rtx
= rtx_alloc (code
);
1140 format_ptr
= GET_RTX_FORMAT (code
);
1141 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1142 PUT_CODE (return_rtx
, code
);
1145 record_iterator_use (iterator
, return_rtx
);
1147 /* If what follows is `: mode ', read it and
1148 store the mode in the rtx. */
1150 i
= read_skip_spaces ();
1154 record_potential_iterator_use (&modes
, return_rtx
, name
.string
);
1159 for (i
= 0; format_ptr
[i
] != 0; i
++)
1160 switch (format_ptr
[i
])
1162 /* 0 means a field for internal use only.
1163 Don't expect it to be present in the input. */
1166 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1171 XEXP (return_rtx
, i
) = read_nested_rtx ();
1175 /* 'V' is an optional vector: if a closeparen follows,
1176 just store NULL for this element. */
1177 c
= read_skip_spaces ();
1181 XVEC (return_rtx
, i
) = 0;
1184 /* Now process the vector. */
1188 /* Obstack to store scratch vector in. */
1189 struct obstack vector_stack
;
1190 int list_counter
= 0;
1191 rtvec return_vec
= NULL_RTVEC
;
1193 c
= read_skip_spaces ();
1195 fatal_expected_char ('[', c
);
1197 /* Add expressions to a list, while keeping a count. */
1198 obstack_init (&vector_stack
);
1199 while ((c
= read_skip_spaces ()) && c
!= ']')
1202 fatal_expected_char (']', c
);
1205 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1207 if (list_counter
> 0)
1209 return_vec
= rtvec_alloc (list_counter
);
1210 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1211 list_counter
* sizeof (rtx
));
1213 else if (format_ptr
[i
] == 'E')
1214 fatal_with_file_and_line ("vector must have at least one element");
1215 XVEC (return_rtx
, i
) = return_vec
;
1216 obstack_free (&vector_stack
, NULL
);
1217 /* close bracket gotten */
1228 c
= read_skip_spaces ();
1232 /* 'S' fields are optional and should be NULL if no string
1233 was given. Also allow normal 's' and 'T' strings to be
1234 omitted, treating them in the same way as empty strings. */
1235 XSTR (return_rtx
, i
) = (format_ptr
[i
] == 'S' ? NULL
: "");
1239 /* The output template slot of a DEFINE_INSN,
1240 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1241 gets a star inserted as its first character, if it is
1242 written with a brace block instead of a string constant. */
1243 star_if_braced
= (format_ptr
[i
] == 'T');
1245 stringbuf
= read_string (star_if_braced
);
1247 /* For insn patterns, we want to provide a default name
1248 based on the file and line, like "*foo.md:12", if the
1249 given name is blank. These are only for define_insn and
1250 define_insn_and_split, to aid debugging. */
1251 if (*stringbuf
== '\0'
1253 && (GET_CODE (return_rtx
) == DEFINE_INSN
1254 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1257 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1259 for (slash
= fn
; *slash
; slash
++)
1260 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1262 obstack_1grow (&string_obstack
, '*');
1263 obstack_grow (&string_obstack
, fn
, strlen (fn
));
1264 sprintf (line_name
, ":%d", read_md_lineno
);
1265 obstack_grow (&string_obstack
, line_name
, strlen (line_name
)+1);
1266 stringbuf
= XOBFINISH (&string_obstack
, char *);
1269 /* Find attr-names in the string. */
1272 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1274 if ((end
- start
- 1 > 0)
1275 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1277 strncpy (tmpstr
, start
+1, end
-start
-1);
1278 tmpstr
[end
-start
-1] = 0;
1283 m
= (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1286 /* Here we should find linked subst-iter. */
1287 str
= find_subst_iter_by_attr (ptr
);
1289 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1294 record_iterator_use (m
, return_rtx
);
1298 XTMPL (return_rtx
, i
) = stringbuf
;
1300 XSTR (return_rtx
, i
) = stringbuf
;
1306 validate_const_int (name
.string
);
1307 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1308 tmp_wide
= atoi (name
.string
);
1310 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1311 tmp_wide
= atol (name
.string
);
1313 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1314 But prefer not to use our hand-rolled function above either. */
1315 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1316 tmp_wide
= atoll (name
.string
);
1318 tmp_wide
= atoq (name
.string
);
1322 XWINT (return_rtx
, i
) = tmp_wide
;
1327 /* Can be an iterator or an integer constant. */
1329 record_potential_iterator_use (&ints
, &XINT (return_rtx
, i
),
1335 validate_const_int (name
.string
);
1336 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1337 REG_ATTRS (return_rtx
) = NULL
;
1344 if (CONST_WIDE_INT_P (return_rtx
))
1347 validate_const_wide_int (name
.string
);
1349 const char *s
= name
.string
;
1352 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1354 char * buf
= XALLOCAVEC (char, gs
+ 1);
1355 unsigned HOST_WIDE_INT wi
;
1358 /* Skip the leading spaces. */
1359 while (*s
&& ISSPACE (*s
))
1362 /* Skip the leading 0x. */
1363 gcc_assert (s
[0] == '0');
1364 gcc_assert (s
[1] == 'x');
1369 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1371 return_rtx
= const_wide_int_alloc (wlen
);
1375 #if HOST_BITS_PER_WIDE_INT == 64
1376 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1378 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1380 CWI_ELT (return_rtx
, index
++) = wi
;
1383 strncpy (buf
, s
, gs
- pos
);
1385 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1386 CWI_ELT (return_rtx
, index
++) = wi
;
1387 /* TODO: After reading, do we want to canonicalize with:
1388 value = lookup_const_wide_int (value); ? */
1392 c
= read_skip_spaces ();
1393 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1394 arbitrary number of arguments for them. */
1396 && (GET_CODE (return_rtx
) == AND
1397 || GET_CODE (return_rtx
) == IOR
))
1398 return read_rtx_variadic (return_rtx
);
1404 /* Read a nested rtx construct from the MD file and return it. */
1407 read_nested_rtx (void)
1409 struct md_name name
;
1413 c
= read_skip_spaces ();
1415 fatal_expected_char ('(', c
);
1418 if (strcmp (name
.string
, "nil") == 0)
1421 return_rtx
= read_rtx_code (name
.string
);
1423 c
= read_skip_spaces ();
1425 fatal_expected_char (')', c
);
1430 /* Mutually recursive subroutine of read_rtx which reads
1431 (thing x1 x2 x3 ...) and produces RTL as if
1432 (thing x1 (thing x2 (thing x3 ...))) had been written.
1433 When called, FORM is (thing x1 x2), and the file position
1434 is just past the leading parenthesis of x3. Only works
1435 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1437 read_rtx_variadic (rtx form
)
1446 q
= rtx_alloc (GET_CODE (p
));
1447 PUT_MODE (q
, GET_MODE (p
));
1449 XEXP (q
, 0) = XEXP (p
, 1);
1450 XEXP (q
, 1) = read_nested_rtx ();
1454 c
= read_skip_spaces ();