2 Copyright (C) 1987-2018 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/>. */
20 /* This file is compiled twice: once for the generator programs
21 once for the compiler. */
28 /* Disable rtl checking; it conflicts with the iterator handling. */
29 #undef ENABLE_RTL_CHECKING
32 #include "coretypes.h"
37 #include "gensupport.h"
39 #ifndef GENERATOR_FILE
45 /* One element in a singly-linked list of (integer, string) pairs. */
47 struct map_value
*next
;
52 /* Maps an iterator or attribute name to a list of (integer, string) pairs.
53 The integers are iterator values; the strings are either C conditions
54 or attribute values. */
56 /* The name of the iterator or attribute. */
59 /* The group (modes or codes) to which the iterator or attribute belongs. */
60 struct iterator_group
*group
;
62 /* The list of (integer, string) pairs. */
63 struct map_value
*values
;
65 /* For iterators, records the current value of the iterator. */
66 struct map_value
*current_value
;
69 /* A structure for abstracting the common parts of iterators. */
70 struct iterator_group
{
71 /* Tables of "mapping" structures, one for attributes and one for
73 htab_t attrs
, iterators
;
75 /* Treat the given string as the name of a standard mode, etc., and
76 return its integer value. */
77 int (*find_builtin
) (const char *);
79 /* Make the given rtx use the iterator value given by the third argument.
80 If the iterator applies to operands, the second argument gives the
81 operand index, otherwise it is ignored. */
82 void (*apply_iterator
) (rtx
, unsigned int, int);
85 /* Records one use of an iterator. */
87 /* The iterator itself. */
88 struct mapping
*iterator
;
90 /* The location of the use, as passed to the apply_iterator callback.
91 The index is the number of the operand that used the iterator
92 if applicable, otherwise it is ignored. */
97 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
98 in a non-string rtx field. */
99 struct attribute_use
{
100 /* The group that describes the use site. */
101 struct iterator_group
*group
;
103 /* The name of the attribute, possibly with an "iterator:" prefix. */
106 /* The location of the use, as passed to GROUP's apply_iterator callback.
107 The index is the number of the operand that used the iterator
108 if applicable, otherwise it is ignored. */
113 /* This struct is used to link subst_attr named ATTR_NAME with
114 corresponding define_subst named ITER_NAME. */
115 struct subst_attr_to_iter_mapping
121 /* Hash-table to store links between subst-attributes and
123 htab_t subst_attr_to_iter_map
= NULL
;
124 /* This global stores name of subst-iterator which is currently being
126 const char *current_iterator_name
;
128 static void validate_const_int (const char *);
129 static void one_time_initialization (void);
131 /* Global singleton. */
132 rtx_reader
*rtx_reader_ptr
= NULL
;
134 /* The mode and code iterator structures. */
135 static struct iterator_group modes
, codes
, ints
, substs
;
137 /* All iterators used in the current rtx. */
138 static vec
<mapping
*> current_iterators
;
140 /* The list of all iterator uses in the current rtx. */
141 static vec
<iterator_use
> iterator_uses
;
143 /* The list of all attribute uses in the current rtx. */
144 static vec
<attribute_use
> attribute_uses
;
146 /* Implementations of the iterator_group callbacks for modes. */
149 find_mode (const char *name
)
153 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
154 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
157 fatal_with_file_and_line ("unknown mode `%s'", name
);
161 apply_mode_iterator (rtx x
, unsigned int, int mode
)
163 PUT_MODE (x
, (machine_mode
) mode
);
166 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
167 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
169 struct compact_insn_name
{
174 static const compact_insn_name compact_insn_names
[] = {
175 { DEBUG_INSN
, "cdebug_insn" },
177 { JUMP_INSN
, "cjump_insn" },
178 { CALL_INSN
, "ccall_insn" },
179 { JUMP_TABLE_DATA
, "cjump_table_data" },
180 { BARRIER
, "cbarrier" },
181 { CODE_LABEL
, "clabel" },
185 /* Implementations of the iterator_group callbacks for codes. */
188 find_code (const char *name
)
192 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
193 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
196 for (i
= 0; i
< (signed)ARRAY_SIZE (compact_insn_names
); i
++)
197 if (strcmp (compact_insn_names
[i
].name
, name
) == 0)
198 return compact_insn_names
[i
].code
;
200 fatal_with_file_and_line ("unknown rtx code `%s'", name
);
204 apply_code_iterator (rtx x
, unsigned int, int code
)
206 PUT_CODE (x
, (enum rtx_code
) code
);
209 /* Implementations of the iterator_group callbacks for ints. */
211 /* Since GCC does not construct a table of valid constants,
212 we have to accept any int as valid. No cross-checking can
216 find_int (const char *name
)
218 validate_const_int (name
);
223 apply_int_iterator (rtx x
, unsigned int index
, int value
)
225 if (GET_CODE (x
) == SUBREG
)
226 SUBREG_BYTE (x
) = value
;
228 XINT (x
, index
) = value
;
231 #ifdef GENERATOR_FILE
233 /* This routine adds attribute or does nothing depending on VALUE. When
234 VALUE is 1, it does nothing - the first duplicate of original
235 template is kept untouched when it's subjected to a define_subst.
236 When VALUE isn't 1, the routine modifies RTL-template RT, adding
237 attribute, named exactly as define_subst, which later will be
238 applied. If such attribute has already been added, then no the
239 routine has no effect. */
241 apply_subst_iterator (rtx rt
, unsigned int, int value
)
244 rtvec attrs_vec
, new_attrs_vec
;
248 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
249 || GET_CODE (rt
) == DEFINE_EXPAND
);
251 attrs_vec
= XVEC (rt
, 4);
253 /* If we've already added attribute 'current_iterator_name', then we
254 have nothing to do now. */
257 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
259 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
264 /* Add attribute with subst name - it serves as a mark for
265 define_subst which later would be applied to this pattern. */
266 new_attr
= rtx_alloc (SET_ATTR
);
267 PUT_CODE (new_attr
, SET_ATTR
);
268 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
269 XSTR (new_attr
, 1) = xstrdup ("yes");
273 new_attrs_vec
= rtvec_alloc (1);
274 new_attrs_vec
->elem
[0] = new_attr
;
278 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
279 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
280 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
281 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
283 XVEC (rt
, 4) = new_attrs_vec
;
286 /* Map subst-attribute ATTR to subst iterator ITER. */
289 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
291 struct subst_attr_to_iter_mapping
*value
;
293 if (!subst_attr_to_iter_map
)
294 subst_attr_to_iter_map
=
295 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
296 value
= XNEW (struct subst_attr_to_iter_mapping
);
297 value
->attr_name
= xstrdup (attr
);
298 value
->iter_name
= xstrdup (iter
);
299 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
303 #endif /* #ifdef GENERATOR_FILE */
305 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
308 find_subst_iter_by_attr (const char *attr
)
310 char *iter_name
= NULL
;
311 struct subst_attr_to_iter_mapping
*value
;
312 value
= (struct subst_attr_to_iter_mapping
*)
313 htab_find (subst_attr_to_iter_map
, &attr
);
315 iter_name
= value
->iter_name
;
319 /* Map attribute string P to its current value. Return null if the attribute
322 static struct map_value
*
323 map_attr_string (const char *p
)
326 struct mapping
*iterator
;
330 int iterator_name_len
;
332 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
334 attr
= strchr (p
, ':');
337 iterator_name_len
= -1;
342 iterator_name_len
= attr
- p
;
346 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
348 /* If an iterator name was specified, check that it matches. */
349 if (iterator_name_len
>= 0
350 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
351 || iterator
->name
[iterator_name_len
] != 0))
354 /* Find the attribute specification. */
355 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
358 /* In contrast to code/mode/int iterators, attributes of subst
359 iterators are linked to one specific subst-iterator. So, if
360 we are dealing with subst-iterator, we should check if it's
361 the one which linked with the given attribute. */
362 if (iterator
->group
== &substs
)
364 char *iter_name
= find_subst_iter_by_attr (attr
);
365 if (strcmp (iter_name
, iterator
->name
) != 0)
368 /* Find the attribute value associated with the current
370 for (v
= m
->values
; v
; v
= v
->next
)
371 if (v
->number
== iterator
->current_value
->number
)
378 /* Apply the current iterator values to STRING. Return the new string
379 if any changes were needed, otherwise return STRING itself. */
382 md_reader::apply_iterator_to_string (const char *string
)
384 char *base
, *copy
, *p
, *start
, *end
;
390 base
= p
= copy
= ASTRDUP (string
);
391 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
396 v
= map_attr_string (p
);
401 /* Add everything between the last copied byte and the '<',
402 then add in the attribute value. */
403 obstack_grow (&m_string_obstack
, base
, start
- base
);
404 obstack_grow (&m_string_obstack
, v
->string
, strlen (v
->string
));
409 obstack_grow (&m_string_obstack
, base
, strlen (base
) + 1);
410 copy
= XOBFINISH (&m_string_obstack
, char *);
411 copy_md_ptr_loc (copy
, string
);
417 /* Return a deep copy of X, substituting the current iterator
418 values into any strings. */
421 md_reader::copy_rtx_for_iterators (rtx original
)
423 const char *format_ptr
, *p
;
430 /* Create a shallow copy of ORIGINAL. */
431 x
= rtx_alloc (GET_CODE (original
));
432 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
434 /* Change each string and recursively change each rtx. */
435 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
436 for (i
= 0; format_ptr
[i
] != 0; i
++)
437 switch (format_ptr
[i
])
440 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
446 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
451 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
456 if (XVEC (original
, i
))
458 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
459 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
461 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
471 #ifdef GENERATOR_FILE
473 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
474 has the form "&& ..." (as used in define_insn_and_splits), assume that
475 EXTRA is already satisfied. Empty strings are treated like "true". */
478 add_condition_to_string (const char *original
, const char *extra
)
480 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
482 return rtx_reader_ptr
->join_c_conditions (original
, extra
);
485 /* Like add_condition, but applied to all conditions in rtx X. */
488 add_condition_to_rtx (rtx x
, const char *extra
)
490 switch (GET_CODE (x
))
495 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
499 case DEFINE_PEEPHOLE
:
500 case DEFINE_PEEPHOLE2
:
501 case DEFINE_COND_EXEC
:
502 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
505 case DEFINE_INSN_AND_SPLIT
:
506 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
507 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
515 /* Apply the current iterator values to all attribute_uses. */
518 apply_attribute_uses (void)
524 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
526 v
= map_attr_string (ause
->value
);
528 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
529 ause
->group
->apply_iterator (ause
->x
, ause
->index
,
530 ause
->group
->find_builtin (v
->string
));
534 /* A htab_traverse callback for iterators. Add all used iterators
535 to current_iterators. */
538 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
540 struct mapping
*iterator
;
542 iterator
= (struct mapping
*) *slot
;
543 if (iterator
->current_value
)
544 current_iterators
.safe_push (iterator
);
548 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
549 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
552 apply_iterators (rtx original
, vec
<rtx
> *queue
)
555 const char *condition
;
557 struct mapping
*iterator
;
561 if (iterator_uses
.is_empty ())
563 /* Raise an error if any attributes were used. */
564 apply_attribute_uses ();
565 queue
->safe_push (original
);
569 /* Clear out the iterators from the previous run. */
570 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
571 iterator
->current_value
= NULL
;
572 current_iterators
.truncate (0);
574 /* Mark the iterators that we need this time. */
575 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
576 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
578 /* Get the list of iterators that are in use, preserving the
579 definition order within each group. */
580 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
581 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
582 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
583 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
584 gcc_assert (!current_iterators
.is_empty ());
588 /* Apply the current iterator values. Accumulate a condition to
589 say when the resulting rtx can be used. */
591 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
593 if (iuse
->iterator
->group
== &substs
)
595 v
= iuse
->iterator
->current_value
;
596 iuse
->iterator
->group
->apply_iterator (iuse
->x
, iuse
->index
,
598 condition
= rtx_reader_ptr
->join_c_conditions (condition
, v
->string
);
600 apply_attribute_uses ();
601 x
= rtx_reader_ptr
->copy_rtx_for_iterators (original
);
602 add_condition_to_rtx (x
, condition
);
604 /* We apply subst iterator after RTL-template is copied, as during
605 subst-iterator processing, we could add an attribute to the
606 RTL-template, and we don't want to do it in the original one. */
607 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
609 v
= iuse
->iterator
->current_value
;
610 if (iuse
->iterator
->group
== &substs
)
614 current_iterator_name
= iuse
->iterator
->name
;
615 iuse
->iterator
->group
->apply_iterator (iuse
->x
, iuse
->index
,
619 /* Add the new rtx to the end of the queue. */
620 queue
->safe_push (x
);
622 /* Lexicographically increment the iterator value sequence.
623 That is, cycle through iterator values, starting from the right,
624 and stopping when one of them doesn't wrap around. */
625 i
= current_iterators
.length ();
631 iterator
= current_iterators
[i
];
632 iterator
->current_value
= iterator
->current_value
->next
;
633 if (iterator
->current_value
)
635 iterator
->current_value
= iterator
->values
;
639 #endif /* #ifdef GENERATOR_FILE */
641 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
642 of the mapping and GROUP is the group to which it belongs. */
644 static struct mapping
*
645 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
650 m
= XNEW (struct mapping
);
651 m
->name
= xstrdup (name
);
654 m
->current_value
= NULL
;
656 slot
= htab_find_slot (table
, m
, INSERT
);
658 fatal_with_file_and_line ("`%s' already defined", name
);
664 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
665 END_PTR points to the current null terminator for the list; return
666 a pointer the new null terminator. */
668 static struct map_value
**
669 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
671 struct map_value
*value
;
673 value
= XNEW (struct map_value
);
675 value
->number
= number
;
676 value
->string
= string
;
682 /* Do one-time initialization of the mode and code attributes. */
685 initialize_iterators (void)
687 struct mapping
*lower
, *upper
;
688 struct map_value
**lower_ptr
, **upper_ptr
;
692 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
693 modes
.iterators
= htab_create (13, leading_string_hash
,
694 leading_string_eq_p
, 0);
695 modes
.find_builtin
= find_mode
;
696 modes
.apply_iterator
= apply_mode_iterator
;
698 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
699 codes
.iterators
= htab_create (13, leading_string_hash
,
700 leading_string_eq_p
, 0);
701 codes
.find_builtin
= find_code
;
702 codes
.apply_iterator
= apply_code_iterator
;
704 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
705 ints
.iterators
= htab_create (13, leading_string_hash
,
706 leading_string_eq_p
, 0);
707 ints
.find_builtin
= find_int
;
708 ints
.apply_iterator
= apply_int_iterator
;
710 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
711 substs
.iterators
= htab_create (13, leading_string_hash
,
712 leading_string_eq_p
, 0);
713 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
714 #ifdef GENERATOR_FILE
715 substs
.apply_iterator
= apply_subst_iterator
;
718 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
719 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
720 lower_ptr
= &lower
->values
;
721 upper_ptr
= &upper
->values
;
722 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
724 copy
= xstrdup (GET_MODE_NAME (i
));
725 for (p
= copy
; *p
!= 0; p
++)
728 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
729 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
732 lower
= add_mapping (&codes
, codes
.attrs
, "code");
733 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
734 lower_ptr
= &lower
->values
;
735 upper_ptr
= &upper
->values
;
736 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
738 copy
= xstrdup (GET_RTX_NAME (i
));
739 for (p
= copy
; *p
!= 0; p
++)
742 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
743 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
747 /* Provide a version of a function to read a long long if the system does
749 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
750 HOST_WIDE_INT
atoll (const char *);
753 atoll (const char *p
)
756 HOST_WIDE_INT tmp_wide
;
768 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
769 if (new_wide
< tmp_wide
)
771 /* Return INT_MAX equiv on overflow. */
772 tmp_wide
= HOST_WIDE_INT_M1U
>> 1;
780 tmp_wide
= -tmp_wide
;
786 #ifdef GENERATOR_FILE
787 /* Process a define_conditions directive, starting with the optional
788 space after the "define_conditions". The directive looks like this:
796 It's not intended to appear in machine descriptions. It is
797 generated by (the program generated by) genconditions.c, and
798 slipped in at the beginning of the sequence of MD files read by
799 most of the other generators. */
801 md_reader::read_conditions ()
805 require_char_ws ('[');
807 while ( (c
= read_skip_spaces ()) != ']')
814 fatal_expected_char ('(', c
);
817 validate_const_int (name
.string
);
818 value
= atoi (name
.string
);
820 require_char_ws ('"');
821 expr
= read_quoted_string ();
823 require_char_ws (')');
825 add_c_test (expr
, value
);
828 #endif /* #ifdef GENERATOR_FILE */
831 validate_const_int (const char *string
)
837 while (*cp
&& ISSPACE (*cp
))
839 if (*cp
== '-' || *cp
== '+')
850 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
854 validate_const_wide_int (const char *string
)
860 while (*cp
&& ISSPACE (*cp
))
862 /* Skip the leading 0x. */
863 if (cp
[0] == '0' || cp
[1] == 'x')
870 if (! ISXDIGIT (*cp
))
873 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
876 /* Record that X uses iterator ITERATOR. If the use is in an operand
877 of X, INDEX is the index of that operand, otherwise it is ignored. */
880 record_iterator_use (struct mapping
*iterator
, rtx x
, unsigned int index
)
882 struct iterator_use iuse
= {iterator
, x
, index
};
883 iterator_uses
.safe_push (iuse
);
886 /* Record that X uses attribute VALUE, which must match a built-in
887 value from group GROUP. If the use is in an operand of X, INDEX
888 is the index of that operand, otherwise it is ignored. */
891 record_attribute_use (struct iterator_group
*group
, rtx x
,
892 unsigned int index
, const char *value
)
894 struct attribute_use ause
= {group
, value
, x
, index
};
895 attribute_uses
.safe_push (ause
);
898 /* Interpret NAME as either a built-in value, iterator or attribute
899 for group GROUP. X and INDEX are the values to pass to GROUP's
900 apply_iterator callback. */
903 md_reader::record_potential_iterator_use (struct iterator_group
*group
,
904 rtx x
, unsigned int index
,
911 if (name
[0] == '<' && name
[len
- 1] == '>')
913 /* Copy the attribute string into permanent storage, without the
914 angle brackets around it. */
915 obstack_grow0 (&m_string_obstack
, name
+ 1, len
- 2);
916 record_attribute_use (group
, x
, index
,
917 XOBFINISH (&m_string_obstack
, char *));
921 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
923 record_iterator_use (m
, x
, index
);
925 group
->apply_iterator (x
, index
, group
->find_builtin (name
));
929 #ifdef GENERATOR_FILE
931 /* Finish reading a declaration of the form:
933 (define... <name> [<value1> ... <valuen>])
935 from the MD file, where each <valuei> is either a bare symbol name or a
936 "(<name> <string>)" pair. The "(define..." part has already been read.
938 Represent the declaration as a "mapping" structure; add it to TABLE
939 (which belongs to GROUP) and return it. */
942 md_reader::read_mapping (struct iterator_group
*group
, htab_t table
)
946 struct map_value
**end_ptr
;
950 /* Read the mapping name and create a structure for it. */
952 m
= add_mapping (group
, table
, name
.string
);
954 require_char_ws ('[');
956 /* Read each value. */
957 end_ptr
= &m
->values
;
958 c
= read_skip_spaces ();
963 /* A bare symbol name that is implicitly paired to an
971 /* A "(name string)" pair. */
973 string
= read_string (false);
974 require_char_ws (')');
976 number
= group
->find_builtin (name
.string
);
977 end_ptr
= add_map_value (end_ptr
, number
, string
);
978 c
= read_skip_spaces ();
985 /* For iterator with name ATTR_NAME generate define_attr with values
986 'yes' and 'no'. This attribute is used to mark templates to which
987 define_subst ATTR_NAME should be applied. This attribute is set and
988 defined implicitly and automatically. */
990 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
992 rtx const_str
, return_rtx
;
994 return_rtx
= rtx_alloc (DEFINE_ATTR
);
995 PUT_CODE (return_rtx
, DEFINE_ATTR
);
997 const_str
= rtx_alloc (CONST_STRING
);
998 PUT_CODE (const_str
, CONST_STRING
);
999 XSTR (const_str
, 0) = xstrdup ("no");
1001 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
1002 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
1003 XEXP (return_rtx
, 2) = const_str
;
1005 queue
->safe_push (return_rtx
);
1008 /* This routine generates DEFINE_SUBST_ATTR expression with operands
1009 ATTR_OPERANDS and places it to QUEUE. */
1011 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
1016 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
1017 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
1019 for (i
= 0; i
< 4; i
++)
1020 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
1022 queue
->safe_push (return_rtx
);
1025 /* Read define_subst_attribute construction. It has next form:
1026 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1027 Attribute is substituted with value1 when no subst is applied and with
1028 value2 in the opposite case.
1029 Attributes are added to SUBST_ATTRS_TABLE.
1030 In case the iterator is encountered for the first time, it's added to
1031 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1034 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
1038 struct map_value
**end_ptr
;
1039 const char *attr_operands
[4];
1042 for (i
= 0; i
< 4; i
++)
1043 attr_operands
[i
] = rtx_reader_ptr
->read_string (false);
1045 add_define_subst_attr (attr_operands
, queue
);
1047 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
1049 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
1052 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
1053 end_ptr
= &m
->values
;
1054 end_ptr
= add_map_value (end_ptr
, 1, "");
1055 end_ptr
= add_map_value (end_ptr
, 2, "");
1057 add_define_attr_for_define_subst (attr_operands
[1], queue
);
1060 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
1061 end_ptr
= &m
->values
;
1062 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
1063 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
1066 /* Check newly-created code iterator ITERATOR to see whether every code has the
1070 check_code_iterator (struct mapping
*iterator
)
1072 struct map_value
*v
;
1073 enum rtx_code bellwether
;
1075 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1076 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1077 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1078 fatal_with_file_and_line ("code iterator `%s' combines "
1079 "different rtx formats", iterator
->name
);
1082 /* Read an rtx-related declaration from the MD file, given that it
1083 starts with directive name RTX_NAME. Return true if it expands to
1084 one or more rtxes (as defined by rtx.def). When returning true,
1085 store the list of rtxes as an EXPR_LIST in *X. */
1088 rtx_reader::read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1090 /* Handle various rtx-related declarations that aren't themselves
1091 encoded as rtxes. */
1092 if (strcmp (rtx_name
, "define_conditions") == 0)
1097 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1099 read_mapping (&modes
, modes
.attrs
);
1102 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1104 read_mapping (&modes
, modes
.iterators
);
1107 if (strcmp (rtx_name
, "define_code_attr") == 0)
1109 read_mapping (&codes
, codes
.attrs
);
1112 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1114 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1117 if (strcmp (rtx_name
, "define_int_attr") == 0)
1119 read_mapping (&ints
, ints
.attrs
);
1122 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1124 read_mapping (&ints
, ints
.iterators
);
1127 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1129 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1131 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1132 TRUE to process it. */
1136 apply_iterators (rtx_reader_ptr
->read_rtx_code (rtx_name
), rtxen
);
1137 iterator_uses
.truncate (0);
1138 attribute_uses
.truncate (0);
1143 #endif /* #ifdef GENERATOR_FILE */
1145 /* Do one-time initialization. */
1148 one_time_initialization (void)
1150 static bool initialized
= false;
1154 initialize_iterators ();
1159 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1160 consuming the terminator character if CONSUME_TERMINATOR is true.
1161 Return all characters before the terminator as an allocated buffer. */
1164 rtx_reader::read_until (const char *terminator_chars
, bool consume_terminator
)
1166 int ch
= read_skip_spaces ();
1172 if (strchr (terminator_chars
, ch
))
1174 if (!consume_terminator
)
1180 buf
.safe_push ('\0');
1181 return xstrdup (buf
.address ());
1184 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1187 read_flags (rtx return_rtx
)
1191 int ch
= read_char ();
1198 int flag_char
= read_char ();
1202 RTX_FLAG (return_rtx
, in_struct
) = 1;
1205 RTX_FLAG (return_rtx
, volatil
) = 1;
1208 RTX_FLAG (return_rtx
, unchanging
) = 1;
1211 RTX_FLAG (return_rtx
, frame_related
) = 1;
1214 RTX_FLAG (return_rtx
, jump
) = 1;
1217 RTX_FLAG (return_rtx
, call
) = 1;
1220 RTX_FLAG (return_rtx
, return_val
) = 1;
1223 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char
);
1228 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1229 or fail if STRING isn't recognized. */
1232 parse_reg_note_name (const char *string
)
1234 for (int i
= 0; i
< REG_NOTE_MAX
; i
++)
1235 if (strcmp (string
, GET_REG_NOTE_NAME (i
)) == 0)
1237 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string
);
1240 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1241 either an rtx code or a code iterator. Parse the rest of the rtx and
1245 rtx_reader::read_rtx_code (const char *code_name
)
1248 struct mapping
*iterator
= NULL
;
1249 const char *format_ptr
;
1250 struct md_name name
;
1255 /* Linked list structure for making RTXs: */
1258 struct rtx_list
*next
;
1259 rtx value
; /* Value of this node. */
1262 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1263 if (ISDIGIT (code_name
[0]))
1265 reuse_id
= atoi (code_name
);
1266 while (char ch
= *code_name
++)
1271 /* Handle "reuse_rtx". */
1272 if (strcmp (code_name
, "reuse_rtx") == 0)
1275 unsigned idx
= atoi (name
.string
);
1276 /* Look it up by ID. */
1277 gcc_assert (idx
< m_reuse_rtx_by_id
.length ());
1278 return_rtx
= m_reuse_rtx_by_id
[idx
];
1282 /* If this code is an iterator, build the rtx using the iterator's
1284 #ifdef GENERATOR_FILE
1285 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1287 code
= (enum rtx_code
) iterator
->values
->number
;
1289 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1291 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1294 /* If we end up with an insn expression then we free this space below. */
1295 return_rtx
= rtx_alloc (code
);
1296 format_ptr
= GET_RTX_FORMAT (code
);
1297 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1298 PUT_CODE (return_rtx
, code
);
1302 /* Store away for later reuse. */
1303 m_reuse_rtx_by_id
.safe_grow_cleared (reuse_id
+ 1);
1304 m_reuse_rtx_by_id
[reuse_id
] = return_rtx
;
1308 record_iterator_use (iterator
, return_rtx
, 0);
1310 /* Check for flags. */
1311 read_flags (return_rtx
);
1313 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1314 if ((GET_CODE (return_rtx
) == EXPR_LIST
1315 || GET_CODE (return_rtx
) == INSN_LIST
1316 || GET_CODE (return_rtx
) == INT_LIST
)
1317 && !m_in_call_function_usage
)
1319 char ch
= read_char ();
1323 PUT_MODE_RAW (return_rtx
,
1324 (machine_mode
)parse_reg_note_name (name
.string
));
1330 /* If what follows is `: mode ', read it and
1331 store the mode in the rtx. */
1333 c
= read_skip_spaces ();
1337 record_potential_iterator_use (&modes
, return_rtx
, 0, name
.string
);
1342 if (INSN_CHAIN_CODE_P (code
))
1345 INSN_UID (return_rtx
) = atoi (name
.string
);
1348 /* Use the format_ptr to parse the various operands of this rtx. */
1349 for (int idx
= 0; format_ptr
[idx
] != 0; idx
++)
1350 return_rtx
= read_rtx_operand (return_rtx
, idx
);
1352 /* Handle any additional information that after the regular fields
1353 (e.g. when parsing function dumps). */
1354 handle_any_trailing_information (return_rtx
);
1356 if (CONST_WIDE_INT_P (return_rtx
))
1359 validate_const_wide_int (name
.string
);
1361 const char *s
= name
.string
;
1364 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1366 char * buf
= XALLOCAVEC (char, gs
+ 1);
1367 unsigned HOST_WIDE_INT wi
;
1370 /* Skip the leading spaces. */
1371 while (*s
&& ISSPACE (*s
))
1374 /* Skip the leading 0x. */
1375 gcc_assert (s
[0] == '0');
1376 gcc_assert (s
[1] == 'x');
1381 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1383 return_rtx
= const_wide_int_alloc (wlen
);
1387 #if HOST_BITS_PER_WIDE_INT == 64
1388 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1390 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1392 CWI_ELT (return_rtx
, index
++) = wi
;
1395 strncpy (buf
, s
, gs
- pos
);
1397 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1398 CWI_ELT (return_rtx
, index
++) = wi
;
1399 /* TODO: After reading, do we want to canonicalize with:
1400 value = lookup_const_wide_int (value); ? */
1404 c
= read_skip_spaces ();
1405 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1406 arbitrary number of arguments for them. */
1408 && (GET_CODE (return_rtx
) == AND
1409 || GET_CODE (return_rtx
) == IOR
))
1410 return read_rtx_variadic (return_rtx
);
1416 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1417 based on the corresponding format character within GET_RTX_FORMAT
1418 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1419 This is a virtual function, so that function_reader can override
1420 some parsing, and potentially return a different rtx. */
1423 rtx_reader::read_rtx_operand (rtx return_rtx
, int idx
)
1425 RTX_CODE code
= GET_CODE (return_rtx
);
1426 const char *format_ptr
= GET_RTX_FORMAT (code
);
1428 struct md_name name
;
1430 switch (format_ptr
[idx
])
1432 /* 0 means a field for internal use only.
1433 Don't expect it to be present in the input. */
1436 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1440 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1444 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1448 /* 'V' is an optional vector: if a closeparen follows,
1449 just store NULL for this element. */
1450 c
= read_skip_spaces ();
1454 XVEC (return_rtx
, idx
) = 0;
1457 /* Now process the vector. */
1462 /* Obstack to store scratch vector in. */
1463 struct obstack vector_stack
;
1464 int list_counter
= 0;
1465 rtvec return_vec
= NULL_RTVEC
;
1467 require_char_ws ('[');
1469 /* Add expressions to a list, while keeping a count. */
1470 obstack_init (&vector_stack
);
1471 while ((c
= read_skip_spaces ()) && c
!= ']')
1474 fatal_expected_char (']', c
);
1477 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1479 if (list_counter
> 0)
1481 return_vec
= rtvec_alloc (list_counter
);
1482 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1483 list_counter
* sizeof (rtx
));
1485 else if (format_ptr
[idx
] == 'E')
1486 fatal_with_file_and_line ("vector must have at least one element");
1487 XVEC (return_rtx
, idx
) = return_vec
;
1488 obstack_free (&vector_stack
, NULL
);
1489 /* close bracket gotten */
1500 c
= read_skip_spaces ();
1504 /* 'S' fields are optional and should be NULL if no string
1505 was given. Also allow normal 's' and 'T' strings to be
1506 omitted, treating them in the same way as empty strings. */
1507 XSTR (return_rtx
, idx
) = (format_ptr
[idx
] == 'S' ? NULL
: "");
1511 /* The output template slot of a DEFINE_INSN,
1512 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1513 gets a star inserted as its first character, if it is
1514 written with a brace block instead of a string constant. */
1515 star_if_braced
= (format_ptr
[idx
] == 'T');
1517 stringbuf
= read_string (star_if_braced
);
1521 #ifdef GENERATOR_FILE
1522 /* For insn patterns, we want to provide a default name
1523 based on the file and line, like "*foo.md:12", if the
1524 given name is blank. These are only for define_insn and
1525 define_insn_and_split, to aid debugging. */
1526 if (*stringbuf
== '\0'
1528 && (GET_CODE (return_rtx
) == DEFINE_INSN
1529 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1531 struct obstack
*string_obstack
= get_string_obstack ();
1533 const char *read_md_filename
= get_filename ();
1534 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1536 for (slash
= fn
; *slash
; slash
++)
1537 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1539 obstack_1grow (string_obstack
, '*');
1540 obstack_grow (string_obstack
, fn
, strlen (fn
));
1541 sprintf (line_name
, ":%d", get_lineno ());
1542 obstack_grow (string_obstack
, line_name
, strlen (line_name
)+1);
1543 stringbuf
= XOBFINISH (string_obstack
, char *);
1546 /* Find attr-names in the string. */
1548 char *start
, *end
, *ptr
;
1552 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1554 if ((end
- start
- 1 > 0)
1555 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1557 strncpy (tmpstr
, start
+1, end
-start
-1);
1558 tmpstr
[end
-start
-1] = 0;
1564 = (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1567 /* Here we should find linked subst-iter. */
1568 str
= find_subst_iter_by_attr (ptr
);
1570 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1575 record_iterator_use (m
, return_rtx
, 0);
1577 #endif /* #ifdef GENERATOR_FILE */
1579 const char *string_ptr
= finalize_string (stringbuf
);
1582 XTMPL (return_rtx
, idx
) = string_ptr
;
1584 XSTR (return_rtx
, idx
) = string_ptr
;
1590 HOST_WIDE_INT tmp_wide
;
1592 validate_const_int (name
.string
);
1593 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1594 tmp_wide
= atoi (name
.string
);
1596 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1597 tmp_wide
= atol (name
.string
);
1599 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1600 But prefer not to use our hand-rolled function above either. */
1601 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1602 tmp_wide
= atoll (name
.string
);
1604 tmp_wide
= atoq (name
.string
);
1608 XWINT (return_rtx
, idx
) = tmp_wide
;
1615 /* Can be an iterator or an integer constant. */
1617 record_potential_iterator_use (&ints
, return_rtx
, idx
, name
.string
);
1622 validate_const_int (name
.string
);
1623 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1624 REG_ATTRS (return_rtx
) = NULL
;
1634 /* Read a nested rtx construct from the MD file and return it. */
1637 rtx_reader::read_nested_rtx ()
1639 struct md_name name
;
1642 /* In compact dumps, trailing "(nil)" values can be omitted.
1643 Handle such dumps. */
1644 if (peek_char () == ')')
1647 require_char_ws ('(');
1650 if (strcmp (name
.string
, "nil") == 0)
1653 return_rtx
= read_rtx_code (name
.string
);
1655 require_char_ws (')');
1657 return_rtx
= postprocess (return_rtx
);
1662 /* Mutually recursive subroutine of read_rtx which reads
1663 (thing x1 x2 x3 ...) and produces RTL as if
1664 (thing x1 (thing x2 (thing x3 ...))) had been written.
1665 When called, FORM is (thing x1 x2), and the file position
1666 is just past the leading parenthesis of x3. Only works
1667 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1669 rtx_reader::read_rtx_variadic (rtx form
)
1678 q
= rtx_alloc (GET_CODE (p
));
1679 PUT_MODE (q
, GET_MODE (p
));
1681 XEXP (q
, 0) = XEXP (p
, 1);
1682 XEXP (q
, 1) = read_nested_rtx ();
1686 c
= read_skip_spaces ();
1693 /* Constructor for class rtx_reader. */
1695 rtx_reader::rtx_reader (bool compact
)
1696 : md_reader (compact
),
1697 m_in_call_function_usage (false)
1699 /* Set the global singleton pointer. */
1700 rtx_reader_ptr
= this;
1702 one_time_initialization ();
1705 /* Destructor for class rtx_reader. */
1707 rtx_reader::~rtx_reader ()
1709 /* Clear the global singleton pointer. */
1710 rtx_reader_ptr
= NULL
;