2 Copyright (C) 1987-2017 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 pointer use the given iterator value. */
80 void (*apply_iterator
) (void *, int);
83 /* Records one use of an iterator. */
85 /* The iterator itself. */
86 struct mapping
*iterator
;
88 /* The location of the use, as passed to the apply_iterator callback. */
92 /* Records one use of an attribute (the "<[iterator:]attribute>" syntax)
93 in a non-string rtx field. */
94 struct attribute_use
{
95 /* The group that describes the use site. */
96 struct iterator_group
*group
;
98 /* The name of the attribute, possibly with an "iterator:" prefix. */
101 /* The location of the use, as passed to GROUP's apply_iterator callback. */
105 /* This struct is used to link subst_attr named ATTR_NAME with
106 corresponding define_subst named ITER_NAME. */
107 struct subst_attr_to_iter_mapping
113 /* Hash-table to store links between subst-attributes and
115 htab_t subst_attr_to_iter_map
= NULL
;
116 /* This global stores name of subst-iterator which is currently being
118 const char *current_iterator_name
;
120 static void validate_const_int (const char *);
121 static void one_time_initialization (void);
123 /* Global singleton. */
124 rtx_reader
*rtx_reader_ptr
= NULL
;
126 /* The mode and code iterator structures. */
127 static struct iterator_group modes
, codes
, ints
, substs
;
129 /* All iterators used in the current rtx. */
130 static vec
<mapping
*> current_iterators
;
132 /* The list of all iterator uses in the current rtx. */
133 static vec
<iterator_use
> iterator_uses
;
135 /* The list of all attribute uses in the current rtx. */
136 static vec
<attribute_use
> attribute_uses
;
138 /* Implementations of the iterator_group callbacks for modes. */
141 find_mode (const char *name
)
145 for (i
= 0; i
< NUM_MACHINE_MODES
; i
++)
146 if (strcmp (GET_MODE_NAME (i
), name
) == 0)
149 fatal_with_file_and_line ("unknown mode `%s'", name
);
153 apply_mode_iterator (void *loc
, int mode
)
155 PUT_MODE ((rtx
) loc
, (machine_mode
) mode
);
158 /* In compact dumps, the code of insns is prefixed with "c", giving "cinsn",
159 "cnote" etc, and CODE_LABEL is special-cased as "clabel". */
161 struct compact_insn_name
{
166 static const compact_insn_name compact_insn_names
[] = {
167 { DEBUG_INSN
, "cdebug_insn" },
169 { JUMP_INSN
, "cjump_insn" },
170 { CALL_INSN
, "ccall_insn" },
171 { JUMP_TABLE_DATA
, "cjump_table_data" },
172 { BARRIER
, "cbarrier" },
173 { CODE_LABEL
, "clabel" },
177 /* Implementations of the iterator_group callbacks for codes. */
180 find_code (const char *name
)
184 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
185 if (strcmp (GET_RTX_NAME (i
), name
) == 0)
188 for (i
= 0; i
< (signed)ARRAY_SIZE (compact_insn_names
); i
++)
189 if (strcmp (compact_insn_names
[i
].name
, name
) == 0)
190 return compact_insn_names
[i
].code
;
192 fatal_with_file_and_line ("unknown rtx code `%s'", name
);
196 apply_code_iterator (void *loc
, int code
)
198 PUT_CODE ((rtx
) loc
, (enum rtx_code
) code
);
201 /* Implementations of the iterator_group callbacks for ints. */
203 /* Since GCC does not construct a table of valid constants,
204 we have to accept any int as valid. No cross-checking can
208 find_int (const char *name
)
210 validate_const_int (name
);
215 apply_int_iterator (void *loc
, int value
)
220 #ifdef GENERATOR_FILE
222 /* This routine adds attribute or does nothing depending on VALUE. When
223 VALUE is 1, it does nothing - the first duplicate of original
224 template is kept untouched when it's subjected to a define_subst.
225 When VALUE isn't 1, the routine modifies RTL-template LOC, adding
226 attribute, named exactly as define_subst, which later will be
227 applied. If such attribute has already been added, then no the
228 routine has no effect. */
230 apply_subst_iterator (void *loc
, int value
)
234 rtvec attrs_vec
, new_attrs_vec
;
238 gcc_assert (GET_CODE (rt
) == DEFINE_INSN
239 || GET_CODE (rt
) == DEFINE_EXPAND
);
241 attrs_vec
= XVEC (rt
, 4);
243 /* If we've already added attribute 'current_iterator_name', then we
244 have nothing to do now. */
247 for (i
= 0; i
< GET_NUM_ELEM (attrs_vec
); i
++)
249 if (strcmp (XSTR (attrs_vec
->elem
[i
], 0), current_iterator_name
) == 0)
254 /* Add attribute with subst name - it serves as a mark for
255 define_subst which later would be applied to this pattern. */
256 new_attr
= rtx_alloc (SET_ATTR
);
257 PUT_CODE (new_attr
, SET_ATTR
);
258 XSTR (new_attr
, 0) = xstrdup (current_iterator_name
);
259 XSTR (new_attr
, 1) = xstrdup ("yes");
263 new_attrs_vec
= rtvec_alloc (1);
264 new_attrs_vec
->elem
[0] = new_attr
;
268 new_attrs_vec
= rtvec_alloc (GET_NUM_ELEM (attrs_vec
) + 1);
269 memcpy (&new_attrs_vec
->elem
[0], &attrs_vec
->elem
[0],
270 GET_NUM_ELEM (attrs_vec
) * sizeof (rtx
));
271 new_attrs_vec
->elem
[GET_NUM_ELEM (attrs_vec
)] = new_attr
;
273 XVEC (rt
, 4) = new_attrs_vec
;
276 /* Map subst-attribute ATTR to subst iterator ITER. */
279 bind_subst_iter_and_attr (const char *iter
, const char *attr
)
281 struct subst_attr_to_iter_mapping
*value
;
283 if (!subst_attr_to_iter_map
)
284 subst_attr_to_iter_map
=
285 htab_create (1, leading_string_hash
, leading_string_eq_p
, 0);
286 value
= XNEW (struct subst_attr_to_iter_mapping
);
287 value
->attr_name
= xstrdup (attr
);
288 value
->iter_name
= xstrdup (iter
);
289 slot
= htab_find_slot (subst_attr_to_iter_map
, value
, INSERT
);
293 #endif /* #ifdef GENERATOR_FILE */
295 /* Return name of a subst-iterator, corresponding to subst-attribute ATTR. */
298 find_subst_iter_by_attr (const char *attr
)
300 char *iter_name
= NULL
;
301 struct subst_attr_to_iter_mapping
*value
;
302 value
= (struct subst_attr_to_iter_mapping
*)
303 htab_find (subst_attr_to_iter_map
, &attr
);
305 iter_name
= value
->iter_name
;
309 /* Map attribute string P to its current value. Return null if the attribute
312 static struct map_value
*
313 map_attr_string (const char *p
)
316 struct mapping
*iterator
;
320 int iterator_name_len
;
322 /* Peel off any "iterator:" prefix. Set ATTR to the start of the
324 attr
= strchr (p
, ':');
327 iterator_name_len
= -1;
332 iterator_name_len
= attr
- p
;
336 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
338 /* If an iterator name was specified, check that it matches. */
339 if (iterator_name_len
>= 0
340 && (strncmp (p
, iterator
->name
, iterator_name_len
) != 0
341 || iterator
->name
[iterator_name_len
] != 0))
344 /* Find the attribute specification. */
345 m
= (struct mapping
*) htab_find (iterator
->group
->attrs
, &attr
);
348 /* In contrast to code/mode/int iterators, attributes of subst
349 iterators are linked to one specific subst-iterator. So, if
350 we are dealing with subst-iterator, we should check if it's
351 the one which linked with the given attribute. */
352 if (iterator
->group
== &substs
)
354 char *iter_name
= find_subst_iter_by_attr (attr
);
355 if (strcmp (iter_name
, iterator
->name
) != 0)
358 /* Find the attribute value associated with the current
360 for (v
= m
->values
; v
; v
= v
->next
)
361 if (v
->number
== iterator
->current_value
->number
)
368 /* Apply the current iterator values to STRING. Return the new string
369 if any changes were needed, otherwise return STRING itself. */
372 md_reader::apply_iterator_to_string (const char *string
)
374 char *base
, *copy
, *p
, *start
, *end
;
380 base
= p
= copy
= ASTRDUP (string
);
381 while ((start
= strchr (p
, '<')) && (end
= strchr (start
, '>')))
386 v
= map_attr_string (p
);
391 /* Add everything between the last copied byte and the '<',
392 then add in the attribute value. */
393 obstack_grow (&m_string_obstack
, base
, start
- base
);
394 obstack_grow (&m_string_obstack
, v
->string
, strlen (v
->string
));
399 obstack_grow (&m_string_obstack
, base
, strlen (base
) + 1);
400 copy
= XOBFINISH (&m_string_obstack
, char *);
401 copy_md_ptr_loc (copy
, string
);
407 /* Return a deep copy of X, substituting the current iterator
408 values into any strings. */
411 md_reader::copy_rtx_for_iterators (rtx original
)
413 const char *format_ptr
, *p
;
420 /* Create a shallow copy of ORIGINAL. */
421 x
= rtx_alloc (GET_CODE (original
));
422 memcpy (x
, original
, RTX_CODE_SIZE (GET_CODE (original
)));
424 /* Change each string and recursively change each rtx. */
425 format_ptr
= GET_RTX_FORMAT (GET_CODE (original
));
426 for (i
= 0; format_ptr
[i
] != 0; i
++)
427 switch (format_ptr
[i
])
430 while (XTMPL (x
, i
) != (p
= apply_iterator_to_string (XTMPL (x
, i
))))
436 while (XSTR (x
, i
) != (p
= apply_iterator_to_string (XSTR (x
, i
))))
441 XEXP (x
, i
) = copy_rtx_for_iterators (XEXP (x
, i
));
446 if (XVEC (original
, i
))
448 XVEC (x
, i
) = rtvec_alloc (XVECLEN (original
, i
));
449 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
451 = copy_rtx_for_iterators (XVECEXP (original
, i
, j
));
461 #ifdef GENERATOR_FILE
463 /* Return a condition that must satisfy both ORIGINAL and EXTRA. If ORIGINAL
464 has the form "&& ..." (as used in define_insn_and_splits), assume that
465 EXTRA is already satisfied. Empty strings are treated like "true". */
468 add_condition_to_string (const char *original
, const char *extra
)
470 if (original
!= 0 && original
[0] == '&' && original
[1] == '&')
472 return rtx_reader_ptr
->join_c_conditions (original
, extra
);
475 /* Like add_condition, but applied to all conditions in rtx X. */
478 add_condition_to_rtx (rtx x
, const char *extra
)
480 switch (GET_CODE (x
))
485 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
489 case DEFINE_PEEPHOLE
:
490 case DEFINE_PEEPHOLE2
:
491 case DEFINE_COND_EXEC
:
492 XSTR (x
, 1) = add_condition_to_string (XSTR (x
, 1), extra
);
495 case DEFINE_INSN_AND_SPLIT
:
496 XSTR (x
, 2) = add_condition_to_string (XSTR (x
, 2), extra
);
497 XSTR (x
, 4) = add_condition_to_string (XSTR (x
, 4), extra
);
505 /* Apply the current iterator values to all attribute_uses. */
508 apply_attribute_uses (void)
514 FOR_EACH_VEC_ELT (attribute_uses
, i
, ause
)
516 v
= map_attr_string (ause
->value
);
518 fatal_with_file_and_line ("unknown iterator value `%s'", ause
->value
);
519 ause
->group
->apply_iterator (ause
->ptr
,
520 ause
->group
->find_builtin (v
->string
));
524 /* A htab_traverse callback for iterators. Add all used iterators
525 to current_iterators. */
528 add_current_iterators (void **slot
, void *data ATTRIBUTE_UNUSED
)
530 struct mapping
*iterator
;
532 iterator
= (struct mapping
*) *slot
;
533 if (iterator
->current_value
)
534 current_iterators
.safe_push (iterator
);
538 /* Expand all iterators in the current rtx, which is given as ORIGINAL.
539 Build a list of expanded rtxes in the EXPR_LIST pointed to by QUEUE. */
542 apply_iterators (rtx original
, vec
<rtx
> *queue
)
545 const char *condition
;
547 struct mapping
*iterator
;
551 if (iterator_uses
.is_empty ())
553 /* Raise an error if any attributes were used. */
554 apply_attribute_uses ();
555 queue
->safe_push (original
);
559 /* Clear out the iterators from the previous run. */
560 FOR_EACH_VEC_ELT (current_iterators
, i
, iterator
)
561 iterator
->current_value
= NULL
;
562 current_iterators
.truncate (0);
564 /* Mark the iterators that we need this time. */
565 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
566 iuse
->iterator
->current_value
= iuse
->iterator
->values
;
568 /* Get the list of iterators that are in use, preserving the
569 definition order within each group. */
570 htab_traverse (modes
.iterators
, add_current_iterators
, NULL
);
571 htab_traverse (codes
.iterators
, add_current_iterators
, NULL
);
572 htab_traverse (ints
.iterators
, add_current_iterators
, NULL
);
573 htab_traverse (substs
.iterators
, add_current_iterators
, NULL
);
574 gcc_assert (!current_iterators
.is_empty ());
578 /* Apply the current iterator values. Accumulate a condition to
579 say when the resulting rtx can be used. */
581 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
583 if (iuse
->iterator
->group
== &substs
)
585 v
= iuse
->iterator
->current_value
;
586 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
587 condition
= rtx_reader_ptr
->join_c_conditions (condition
, v
->string
);
589 apply_attribute_uses ();
590 x
= rtx_reader_ptr
->copy_rtx_for_iterators (original
);
591 add_condition_to_rtx (x
, condition
);
593 /* We apply subst iterator after RTL-template is copied, as during
594 subst-iterator processing, we could add an attribute to the
595 RTL-template, and we don't want to do it in the original one. */
596 FOR_EACH_VEC_ELT (iterator_uses
, i
, iuse
)
598 v
= iuse
->iterator
->current_value
;
599 if (iuse
->iterator
->group
== &substs
)
602 current_iterator_name
= iuse
->iterator
->name
;
603 iuse
->iterator
->group
->apply_iterator (iuse
->ptr
, v
->number
);
606 /* Add the new rtx to the end of the queue. */
607 queue
->safe_push (x
);
609 /* Lexicographically increment the iterator value sequence.
610 That is, cycle through iterator values, starting from the right,
611 and stopping when one of them doesn't wrap around. */
612 i
= current_iterators
.length ();
618 iterator
= current_iterators
[i
];
619 iterator
->current_value
= iterator
->current_value
->next
;
620 if (iterator
->current_value
)
622 iterator
->current_value
= iterator
->values
;
626 #endif /* #ifdef GENERATOR_FILE */
628 /* Add a new "mapping" structure to hashtable TABLE. NAME is the name
629 of the mapping and GROUP is the group to which it belongs. */
631 static struct mapping
*
632 add_mapping (struct iterator_group
*group
, htab_t table
, const char *name
)
637 m
= XNEW (struct mapping
);
638 m
->name
= xstrdup (name
);
641 m
->current_value
= NULL
;
643 slot
= htab_find_slot (table
, m
, INSERT
);
645 fatal_with_file_and_line ("`%s' already defined", name
);
651 /* Add the pair (NUMBER, STRING) to a list of map_value structures.
652 END_PTR points to the current null terminator for the list; return
653 a pointer the new null terminator. */
655 static struct map_value
**
656 add_map_value (struct map_value
**end_ptr
, int number
, const char *string
)
658 struct map_value
*value
;
660 value
= XNEW (struct map_value
);
662 value
->number
= number
;
663 value
->string
= string
;
669 /* Do one-time initialization of the mode and code attributes. */
672 initialize_iterators (void)
674 struct mapping
*lower
, *upper
;
675 struct map_value
**lower_ptr
, **upper_ptr
;
679 modes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
680 modes
.iterators
= htab_create (13, leading_string_hash
,
681 leading_string_eq_p
, 0);
682 modes
.find_builtin
= find_mode
;
683 modes
.apply_iterator
= apply_mode_iterator
;
685 codes
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
686 codes
.iterators
= htab_create (13, leading_string_hash
,
687 leading_string_eq_p
, 0);
688 codes
.find_builtin
= find_code
;
689 codes
.apply_iterator
= apply_code_iterator
;
691 ints
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
692 ints
.iterators
= htab_create (13, leading_string_hash
,
693 leading_string_eq_p
, 0);
694 ints
.find_builtin
= find_int
;
695 ints
.apply_iterator
= apply_int_iterator
;
697 substs
.attrs
= htab_create (13, leading_string_hash
, leading_string_eq_p
, 0);
698 substs
.iterators
= htab_create (13, leading_string_hash
,
699 leading_string_eq_p
, 0);
700 substs
.find_builtin
= find_int
; /* We don't use it, anyway. */
701 #ifdef GENERATOR_FILE
702 substs
.apply_iterator
= apply_subst_iterator
;
705 lower
= add_mapping (&modes
, modes
.attrs
, "mode");
706 upper
= add_mapping (&modes
, modes
.attrs
, "MODE");
707 lower_ptr
= &lower
->values
;
708 upper_ptr
= &upper
->values
;
709 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
711 copy
= xstrdup (GET_MODE_NAME (i
));
712 for (p
= copy
; *p
!= 0; p
++)
715 upper_ptr
= add_map_value (upper_ptr
, i
, GET_MODE_NAME (i
));
716 lower_ptr
= add_map_value (lower_ptr
, i
, copy
);
719 lower
= add_mapping (&codes
, codes
.attrs
, "code");
720 upper
= add_mapping (&codes
, codes
.attrs
, "CODE");
721 lower_ptr
= &lower
->values
;
722 upper_ptr
= &upper
->values
;
723 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
725 copy
= xstrdup (GET_RTX_NAME (i
));
726 for (p
= copy
; *p
!= 0; p
++)
729 lower_ptr
= add_map_value (lower_ptr
, i
, GET_RTX_NAME (i
));
730 upper_ptr
= add_map_value (upper_ptr
, i
, copy
);
734 /* Provide a version of a function to read a long long if the system does
736 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_LONG && !HAVE_DECL_ATOLL && !defined(HAVE_ATOQ)
737 HOST_WIDE_INT
atoll (const char *);
740 atoll (const char *p
)
743 HOST_WIDE_INT tmp_wide
;
755 HOST_WIDE_INT new_wide
= tmp_wide
*10 + (*p
- '0');
756 if (new_wide
< tmp_wide
)
758 /* Return INT_MAX equiv on overflow. */
759 tmp_wide
= HOST_WIDE_INT_M1U
>> 1;
767 tmp_wide
= -tmp_wide
;
773 #ifdef GENERATOR_FILE
774 /* Process a define_conditions directive, starting with the optional
775 space after the "define_conditions". The directive looks like this:
783 It's not intended to appear in machine descriptions. It is
784 generated by (the program generated by) genconditions.c, and
785 slipped in at the beginning of the sequence of MD files read by
786 most of the other generators. */
788 md_reader::read_conditions ()
792 require_char_ws ('[');
794 while ( (c
= read_skip_spaces ()) != ']')
801 fatal_expected_char ('(', c
);
804 validate_const_int (name
.string
);
805 value
= atoi (name
.string
);
807 require_char_ws ('"');
808 expr
= read_quoted_string ();
810 require_char_ws (')');
812 add_c_test (expr
, value
);
815 #endif /* #ifdef GENERATOR_FILE */
818 validate_const_int (const char *string
)
824 while (*cp
&& ISSPACE (*cp
))
826 if (*cp
== '-' || *cp
== '+')
837 fatal_with_file_and_line ("invalid decimal constant \"%s\"\n", string
);
841 validate_const_wide_int (const char *string
)
847 while (*cp
&& ISSPACE (*cp
))
849 /* Skip the leading 0x. */
850 if (cp
[0] == '0' || cp
[1] == 'x')
857 if (! ISXDIGIT (*cp
))
860 fatal_with_file_and_line ("invalid hex constant \"%s\"\n", string
);
863 /* Record that PTR uses iterator ITERATOR. */
866 record_iterator_use (struct mapping
*iterator
, void *ptr
)
868 struct iterator_use iuse
= {iterator
, ptr
};
869 iterator_uses
.safe_push (iuse
);
872 /* Record that PTR uses attribute VALUE, which must match a built-in
873 value from group GROUP. */
876 record_attribute_use (struct iterator_group
*group
, void *ptr
,
879 struct attribute_use ause
= {group
, value
, ptr
};
880 attribute_uses
.safe_push (ause
);
883 /* Interpret NAME as either a built-in value, iterator or attribute
884 for group GROUP. PTR is the value to pass to GROUP's apply_iterator
888 md_reader::record_potential_iterator_use (struct iterator_group
*group
,
889 void *ptr
, const char *name
)
895 if (name
[0] == '<' && name
[len
- 1] == '>')
897 /* Copy the attribute string into permanent storage, without the
898 angle brackets around it. */
899 obstack_grow0 (&m_string_obstack
, name
+ 1, len
- 2);
900 record_attribute_use (group
, ptr
, XOBFINISH (&m_string_obstack
, char *));
904 m
= (struct mapping
*) htab_find (group
->iterators
, &name
);
906 record_iterator_use (m
, ptr
);
908 group
->apply_iterator (ptr
, group
->find_builtin (name
));
912 #ifdef GENERATOR_FILE
914 /* Finish reading a declaration of the form:
916 (define... <name> [<value1> ... <valuen>])
918 from the MD file, where each <valuei> is either a bare symbol name or a
919 "(<name> <string>)" pair. The "(define..." part has already been read.
921 Represent the declaration as a "mapping" structure; add it to TABLE
922 (which belongs to GROUP) and return it. */
925 md_reader::read_mapping (struct iterator_group
*group
, htab_t table
)
929 struct map_value
**end_ptr
;
933 /* Read the mapping name and create a structure for it. */
935 m
= add_mapping (group
, table
, name
.string
);
937 require_char_ws ('[');
939 /* Read each value. */
940 end_ptr
= &m
->values
;
941 c
= read_skip_spaces ();
946 /* A bare symbol name that is implicitly paired to an
954 /* A "(name string)" pair. */
956 string
= read_string (false);
957 require_char_ws (')');
959 number
= group
->find_builtin (name
.string
);
960 end_ptr
= add_map_value (end_ptr
, number
, string
);
961 c
= read_skip_spaces ();
968 /* For iterator with name ATTR_NAME generate define_attr with values
969 'yes' and 'no'. This attribute is used to mark templates to which
970 define_subst ATTR_NAME should be applied. This attribute is set and
971 defined implicitly and automatically. */
973 add_define_attr_for_define_subst (const char *attr_name
, vec
<rtx
> *queue
)
975 rtx const_str
, return_rtx
;
977 return_rtx
= rtx_alloc (DEFINE_ATTR
);
978 PUT_CODE (return_rtx
, DEFINE_ATTR
);
980 const_str
= rtx_alloc (CONST_STRING
);
981 PUT_CODE (const_str
, CONST_STRING
);
982 XSTR (const_str
, 0) = xstrdup ("no");
984 XSTR (return_rtx
, 0) = xstrdup (attr_name
);
985 XSTR (return_rtx
, 1) = xstrdup ("no,yes");
986 XEXP (return_rtx
, 2) = const_str
;
988 queue
->safe_push (return_rtx
);
991 /* This routine generates DEFINE_SUBST_ATTR expression with operands
992 ATTR_OPERANDS and places it to QUEUE. */
994 add_define_subst_attr (const char **attr_operands
, vec
<rtx
> *queue
)
999 return_rtx
= rtx_alloc (DEFINE_SUBST_ATTR
);
1000 PUT_CODE (return_rtx
, DEFINE_SUBST_ATTR
);
1002 for (i
= 0; i
< 4; i
++)
1003 XSTR (return_rtx
, i
) = xstrdup (attr_operands
[i
]);
1005 queue
->safe_push (return_rtx
);
1008 /* Read define_subst_attribute construction. It has next form:
1009 (define_subst_attribute <attribute_name> <iterator_name> <value1> <value2>)
1010 Attribute is substituted with value1 when no subst is applied and with
1011 value2 in the opposite case.
1012 Attributes are added to SUBST_ATTRS_TABLE.
1013 In case the iterator is encountered for the first time, it's added to
1014 SUBST_ITERS_TABLE. Also, implicit define_attr is generated. */
1017 read_subst_mapping (htab_t subst_iters_table
, htab_t subst_attrs_table
,
1021 struct map_value
**end_ptr
;
1022 const char *attr_operands
[4];
1025 for (i
= 0; i
< 4; i
++)
1026 attr_operands
[i
] = rtx_reader_ptr
->read_string (false);
1028 add_define_subst_attr (attr_operands
, queue
);
1030 bind_subst_iter_and_attr (attr_operands
[1], attr_operands
[0]);
1032 m
= (struct mapping
*) htab_find (substs
.iterators
, &attr_operands
[1]);
1035 m
= add_mapping (&substs
, subst_iters_table
, attr_operands
[1]);
1036 end_ptr
= &m
->values
;
1037 end_ptr
= add_map_value (end_ptr
, 1, "");
1038 end_ptr
= add_map_value (end_ptr
, 2, "");
1040 add_define_attr_for_define_subst (attr_operands
[1], queue
);
1043 m
= add_mapping (&substs
, subst_attrs_table
, attr_operands
[0]);
1044 end_ptr
= &m
->values
;
1045 end_ptr
= add_map_value (end_ptr
, 1, attr_operands
[2]);
1046 end_ptr
= add_map_value (end_ptr
, 2, attr_operands
[3]);
1049 /* Check newly-created code iterator ITERATOR to see whether every code has the
1053 check_code_iterator (struct mapping
*iterator
)
1055 struct map_value
*v
;
1056 enum rtx_code bellwether
;
1058 bellwether
= (enum rtx_code
) iterator
->values
->number
;
1059 for (v
= iterator
->values
->next
; v
!= 0; v
= v
->next
)
1060 if (strcmp (GET_RTX_FORMAT (bellwether
), GET_RTX_FORMAT (v
->number
)) != 0)
1061 fatal_with_file_and_line ("code iterator `%s' combines "
1062 "different rtx formats", iterator
->name
);
1065 /* Read an rtx-related declaration from the MD file, given that it
1066 starts with directive name RTX_NAME. Return true if it expands to
1067 one or more rtxes (as defined by rtx.def). When returning true,
1068 store the list of rtxes as an EXPR_LIST in *X. */
1071 rtx_reader::read_rtx (const char *rtx_name
, vec
<rtx
> *rtxen
)
1073 /* Handle various rtx-related declarations that aren't themselves
1074 encoded as rtxes. */
1075 if (strcmp (rtx_name
, "define_conditions") == 0)
1080 if (strcmp (rtx_name
, "define_mode_attr") == 0)
1082 read_mapping (&modes
, modes
.attrs
);
1085 if (strcmp (rtx_name
, "define_mode_iterator") == 0)
1087 read_mapping (&modes
, modes
.iterators
);
1090 if (strcmp (rtx_name
, "define_code_attr") == 0)
1092 read_mapping (&codes
, codes
.attrs
);
1095 if (strcmp (rtx_name
, "define_code_iterator") == 0)
1097 check_code_iterator (read_mapping (&codes
, codes
.iterators
));
1100 if (strcmp (rtx_name
, "define_int_attr") == 0)
1102 read_mapping (&ints
, ints
.attrs
);
1105 if (strcmp (rtx_name
, "define_int_iterator") == 0)
1107 read_mapping (&ints
, ints
.iterators
);
1110 if (strcmp (rtx_name
, "define_subst_attr") == 0)
1112 read_subst_mapping (substs
.iterators
, substs
.attrs
, rtxen
);
1114 /* READ_SUBST_MAPPING could generate a new DEFINE_ATTR. Return
1115 TRUE to process it. */
1119 apply_iterators (rtx_reader_ptr
->read_rtx_code (rtx_name
), rtxen
);
1120 iterator_uses
.truncate (0);
1121 attribute_uses
.truncate (0);
1126 #endif /* #ifdef GENERATOR_FILE */
1128 /* Do one-time initialization. */
1131 one_time_initialization (void)
1133 static bool initialized
= false;
1137 initialize_iterators ();
1142 /* Consume characters until encountering a character in TERMINATOR_CHARS,
1143 consuming the terminator character if CONSUME_TERMINATOR is true.
1144 Return all characters before the terminator as an allocated buffer. */
1147 rtx_reader::read_until (const char *terminator_chars
, bool consume_terminator
)
1149 int ch
= read_skip_spaces ();
1155 if (strchr (terminator_chars
, ch
))
1157 if (!consume_terminator
)
1163 buf
.safe_push ('\0');
1164 return xstrdup (buf
.address ());
1167 /* Subroutine of read_rtx_code, for parsing zero or more flags. */
1170 read_flags (rtx return_rtx
)
1174 int ch
= read_char ();
1181 int flag_char
= read_char ();
1185 RTX_FLAG (return_rtx
, in_struct
) = 1;
1188 RTX_FLAG (return_rtx
, volatil
) = 1;
1191 RTX_FLAG (return_rtx
, unchanging
) = 1;
1194 RTX_FLAG (return_rtx
, frame_related
) = 1;
1197 RTX_FLAG (return_rtx
, jump
) = 1;
1200 RTX_FLAG (return_rtx
, call
) = 1;
1203 RTX_FLAG (return_rtx
, return_val
) = 1;
1206 fatal_with_file_and_line ("unrecognized flag: `%c'", flag_char
);
1211 /* Return the numeric value n for GET_REG_NOTE_NAME (n) for STRING,
1212 or fail if STRING isn't recognized. */
1215 parse_reg_note_name (const char *string
)
1217 for (int i
= 0; i
< REG_NOTE_MAX
; i
++)
1218 if (0 == strcmp (string
, GET_REG_NOTE_NAME (i
)))
1220 fatal_with_file_and_line ("unrecognized REG_NOTE name: `%s'", string
);
1223 /* Subroutine of read_rtx and read_nested_rtx. CODE_NAME is the name of
1224 either an rtx code or a code iterator. Parse the rest of the rtx and
1228 rtx_reader::read_rtx_code (const char *code_name
)
1231 struct mapping
*iterator
= NULL
;
1232 const char *format_ptr
;
1233 struct md_name name
;
1238 /* Linked list structure for making RTXs: */
1241 struct rtx_list
*next
;
1242 rtx value
; /* Value of this node. */
1245 /* Handle reuse_rtx ids e.g. "(0|scratch:DI)". */
1246 if (ISDIGIT (code_name
[0]))
1248 reuse_id
= atoi (code_name
);
1249 while (char ch
= *code_name
++)
1254 /* Handle "reuse_rtx". */
1255 if (strcmp (code_name
, "reuse_rtx") == 0)
1258 unsigned idx
= atoi (name
.string
);
1259 /* Look it up by ID. */
1260 gcc_assert (idx
< m_reuse_rtx_by_id
.length ());
1261 return_rtx
= m_reuse_rtx_by_id
[idx
];
1265 /* If this code is an iterator, build the rtx using the iterator's
1267 #ifdef GENERATOR_FILE
1268 iterator
= (struct mapping
*) htab_find (codes
.iterators
, &code_name
);
1270 code
= (enum rtx_code
) iterator
->values
->number
;
1272 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1274 code
= (enum rtx_code
) codes
.find_builtin (code_name
);
1277 /* If we end up with an insn expression then we free this space below. */
1278 return_rtx
= rtx_alloc (code
);
1279 format_ptr
= GET_RTX_FORMAT (code
);
1280 memset (return_rtx
, 0, RTX_CODE_SIZE (code
));
1281 PUT_CODE (return_rtx
, code
);
1285 /* Store away for later reuse. */
1286 m_reuse_rtx_by_id
.safe_grow_cleared (reuse_id
+ 1);
1287 m_reuse_rtx_by_id
[reuse_id
] = return_rtx
;
1291 record_iterator_use (iterator
, return_rtx
);
1293 /* Check for flags. */
1294 read_flags (return_rtx
);
1296 /* Read REG_NOTE names for EXPR_LIST and INSN_LIST. */
1297 if ((GET_CODE (return_rtx
) == EXPR_LIST
1298 || GET_CODE (return_rtx
) == INSN_LIST
1299 || GET_CODE (return_rtx
) == INT_LIST
)
1300 && !m_in_call_function_usage
)
1302 char ch
= read_char ();
1306 PUT_MODE_RAW (return_rtx
,
1307 (machine_mode
)parse_reg_note_name (name
.string
));
1313 /* If what follows is `: mode ', read it and
1314 store the mode in the rtx. */
1316 c
= read_skip_spaces ();
1320 record_potential_iterator_use (&modes
, return_rtx
, name
.string
);
1325 if (INSN_CHAIN_CODE_P (code
))
1328 INSN_UID (return_rtx
) = atoi (name
.string
);
1331 /* Use the format_ptr to parse the various operands of this rtx. */
1332 for (int idx
= 0; format_ptr
[idx
] != 0; idx
++)
1333 return_rtx
= read_rtx_operand (return_rtx
, idx
);
1335 /* Handle any additional information that after the regular fields
1336 (e.g. when parsing function dumps). */
1337 handle_any_trailing_information (return_rtx
);
1339 if (CONST_WIDE_INT_P (return_rtx
))
1342 validate_const_wide_int (name
.string
);
1344 const char *s
= name
.string
;
1347 int gs
= HOST_BITS_PER_WIDE_INT
/4;
1349 char * buf
= XALLOCAVEC (char, gs
+ 1);
1350 unsigned HOST_WIDE_INT wi
;
1353 /* Skip the leading spaces. */
1354 while (*s
&& ISSPACE (*s
))
1357 /* Skip the leading 0x. */
1358 gcc_assert (s
[0] == '0');
1359 gcc_assert (s
[1] == 'x');
1364 wlen
= (len
+ gs
- 1) / gs
; /* Number of words needed */
1366 return_rtx
= const_wide_int_alloc (wlen
);
1370 #if HOST_BITS_PER_WIDE_INT == 64
1371 sscanf (s
+ pos
, "%16" HOST_WIDE_INT_PRINT
"x", &wi
);
1373 sscanf (s
+ pos
, "%8" HOST_WIDE_INT_PRINT
"x", &wi
);
1375 CWI_ELT (return_rtx
, index
++) = wi
;
1378 strncpy (buf
, s
, gs
- pos
);
1380 sscanf (buf
, "%" HOST_WIDE_INT_PRINT
"x", &wi
);
1381 CWI_ELT (return_rtx
, index
++) = wi
;
1382 /* TODO: After reading, do we want to canonicalize with:
1383 value = lookup_const_wide_int (value); ? */
1387 c
= read_skip_spaces ();
1388 /* Syntactic sugar for AND and IOR, allowing Lisp-like
1389 arbitrary number of arguments for them. */
1391 && (GET_CODE (return_rtx
) == AND
1392 || GET_CODE (return_rtx
) == IOR
))
1393 return read_rtx_variadic (return_rtx
);
1399 /* Subroutine of read_rtx_code. Parse operand IDX within RETURN_RTX,
1400 based on the corresponding format character within GET_RTX_FORMAT
1401 for the GET_CODE (RETURN_RTX), and return RETURN_RTX.
1402 This is a virtual function, so that function_reader can override
1403 some parsing, and potentially return a different rtx. */
1406 rtx_reader::read_rtx_operand (rtx return_rtx
, int idx
)
1408 RTX_CODE code
= GET_CODE (return_rtx
);
1409 const char *format_ptr
= GET_RTX_FORMAT (code
);
1411 struct md_name name
;
1413 switch (format_ptr
[idx
])
1415 /* 0 means a field for internal use only.
1416 Don't expect it to be present in the input. */
1419 ORIGINAL_REGNO (return_rtx
) = REGNO (return_rtx
);
1423 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1427 XEXP (return_rtx
, idx
) = read_nested_rtx ();
1431 /* 'V' is an optional vector: if a closeparen follows,
1432 just store NULL for this element. */
1433 c
= read_skip_spaces ();
1437 XVEC (return_rtx
, idx
) = 0;
1440 /* Now process the vector. */
1445 /* Obstack to store scratch vector in. */
1446 struct obstack vector_stack
;
1447 int list_counter
= 0;
1448 rtvec return_vec
= NULL_RTVEC
;
1450 require_char_ws ('[');
1452 /* Add expressions to a list, while keeping a count. */
1453 obstack_init (&vector_stack
);
1454 while ((c
= read_skip_spaces ()) && c
!= ']')
1457 fatal_expected_char (']', c
);
1460 obstack_ptr_grow (&vector_stack
, read_nested_rtx ());
1462 if (list_counter
> 0)
1464 return_vec
= rtvec_alloc (list_counter
);
1465 memcpy (&return_vec
->elem
[0], obstack_finish (&vector_stack
),
1466 list_counter
* sizeof (rtx
));
1468 else if (format_ptr
[idx
] == 'E')
1469 fatal_with_file_and_line ("vector must have at least one element");
1470 XVEC (return_rtx
, idx
) = return_vec
;
1471 obstack_free (&vector_stack
, NULL
);
1472 /* close bracket gotten */
1483 c
= read_skip_spaces ();
1487 /* 'S' fields are optional and should be NULL if no string
1488 was given. Also allow normal 's' and 'T' strings to be
1489 omitted, treating them in the same way as empty strings. */
1490 XSTR (return_rtx
, idx
) = (format_ptr
[idx
] == 'S' ? NULL
: "");
1494 /* The output template slot of a DEFINE_INSN,
1495 DEFINE_INSN_AND_SPLIT, or DEFINE_PEEPHOLE automatically
1496 gets a star inserted as its first character, if it is
1497 written with a brace block instead of a string constant. */
1498 star_if_braced
= (format_ptr
[idx
] == 'T');
1500 stringbuf
= read_string (star_if_braced
);
1504 #ifdef GENERATOR_FILE
1505 /* For insn patterns, we want to provide a default name
1506 based on the file and line, like "*foo.md:12", if the
1507 given name is blank. These are only for define_insn and
1508 define_insn_and_split, to aid debugging. */
1509 if (*stringbuf
== '\0'
1511 && (GET_CODE (return_rtx
) == DEFINE_INSN
1512 || GET_CODE (return_rtx
) == DEFINE_INSN_AND_SPLIT
))
1514 struct obstack
*string_obstack
= get_string_obstack ();
1516 const char *read_md_filename
= get_filename ();
1517 const char *fn
= (read_md_filename
? read_md_filename
: "rtx");
1519 for (slash
= fn
; *slash
; slash
++)
1520 if (*slash
== '/' || *slash
== '\\' || *slash
== ':')
1522 obstack_1grow (string_obstack
, '*');
1523 obstack_grow (string_obstack
, fn
, strlen (fn
));
1524 sprintf (line_name
, ":%d", get_lineno ());
1525 obstack_grow (string_obstack
, line_name
, strlen (line_name
)+1);
1526 stringbuf
= XOBFINISH (string_obstack
, char *);
1529 /* Find attr-names in the string. */
1531 char *start
, *end
, *ptr
;
1535 while ((start
= strchr (end
, '<')) && (end
= strchr (start
, '>')))
1537 if ((end
- start
- 1 > 0)
1538 && (end
- start
- 1 < (int)sizeof (tmpstr
)))
1540 strncpy (tmpstr
, start
+1, end
-start
-1);
1541 tmpstr
[end
-start
-1] = 0;
1547 = (struct mapping
*) htab_find (substs
.attrs
, &ptr
);
1550 /* Here we should find linked subst-iter. */
1551 str
= find_subst_iter_by_attr (ptr
);
1553 m
= (struct mapping
*) htab_find (substs
.iterators
, &str
);
1558 record_iterator_use (m
, return_rtx
);
1560 #endif /* #ifdef GENERATOR_FILE */
1562 const char *string_ptr
= finalize_string (stringbuf
);
1565 XTMPL (return_rtx
, idx
) = string_ptr
;
1567 XSTR (return_rtx
, idx
) = string_ptr
;
1573 HOST_WIDE_INT tmp_wide
;
1575 validate_const_int (name
.string
);
1576 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1577 tmp_wide
= atoi (name
.string
);
1579 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
1580 tmp_wide
= atol (name
.string
);
1582 /* Prefer atoll over atoq, since the former is in the ISO C99 standard.
1583 But prefer not to use our hand-rolled function above either. */
1584 #if HAVE_DECL_ATOLL || !defined(HAVE_ATOQ)
1585 tmp_wide
= atoll (name
.string
);
1587 tmp_wide
= atoq (name
.string
);
1591 XWINT (return_rtx
, idx
) = tmp_wide
;
1597 /* Can be an iterator or an integer constant. */
1599 record_potential_iterator_use (&ints
, &XINT (return_rtx
, idx
),
1605 validate_const_int (name
.string
);
1606 set_regno_raw (return_rtx
, atoi (name
.string
), 1);
1607 REG_ATTRS (return_rtx
) = NULL
;
1617 /* Read a nested rtx construct from the MD file and return it. */
1620 rtx_reader::read_nested_rtx ()
1622 struct md_name name
;
1625 /* In compact dumps, trailing "(nil)" values can be omitted.
1626 Handle such dumps. */
1627 if (peek_char () == ')')
1630 require_char_ws ('(');
1633 if (strcmp (name
.string
, "nil") == 0)
1636 return_rtx
= read_rtx_code (name
.string
);
1638 require_char_ws (')');
1640 return_rtx
= postprocess (return_rtx
);
1645 /* Mutually recursive subroutine of read_rtx which reads
1646 (thing x1 x2 x3 ...) and produces RTL as if
1647 (thing x1 (thing x2 (thing x3 ...))) had been written.
1648 When called, FORM is (thing x1 x2), and the file position
1649 is just past the leading parenthesis of x3. Only works
1650 for THINGs which are dyadic expressions, e.g. AND, IOR. */
1652 rtx_reader::read_rtx_variadic (rtx form
)
1661 q
= rtx_alloc (GET_CODE (p
));
1662 PUT_MODE (q
, GET_MODE (p
));
1664 XEXP (q
, 0) = XEXP (p
, 1);
1665 XEXP (q
, 1) = read_nested_rtx ();
1669 c
= read_skip_spaces ();
1676 /* Constructor for class rtx_reader. */
1678 rtx_reader::rtx_reader (bool compact
)
1679 : md_reader (compact
),
1680 m_in_call_function_usage (false)
1682 /* Set the global singleton pointer. */
1683 rtx_reader_ptr
= this;
1685 one_time_initialization ();
1688 /* Destructor for class rtx_reader. */
1690 rtx_reader::~rtx_reader ()
1692 /* Clear the global singleton pointer. */
1693 rtx_reader_ptr
= NULL
;